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" |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprOpenMP.h" |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtCXX.h" |
| 26 | #include "clang/AST/StmtObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/Analyses/FormatString.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 28 | #include "clang/Basic/CharInfo.h" |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 33 | #include "clang/Sema/Lookup.h" |
| 34 | #include "clang/Sema/ScopeInfo.h" |
| 35 | #include "clang/Sema/Sema.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallBitVector.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallString.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ConvertUTF.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 050379b | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 41 | #include <limits> |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 42 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 43 | using namespace sema; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 44 | |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 45 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 46 | unsigned ByteNo) const { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 47 | return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, |
| 48 | Context.getTargetInfo()); |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 51 | /// Checks that a call expression's argument count is the desired number. |
| 52 | /// This is useful when doing custom type-checking. Returns true on error. |
| 53 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 54 | unsigned argCount = call->getNumArgs(); |
| 55 | if (argCount == desiredArgCount) return false; |
| 56 | |
| 57 | if (argCount < desiredArgCount) |
| 58 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 59 | << 0 /*function call*/ << desiredArgCount << argCount |
| 60 | << call->getSourceRange(); |
| 61 | |
| 62 | // Highlight all the excess arguments. |
| 63 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 64 | call->getArg(argCount - 1)->getLocEnd()); |
| 65 | |
| 66 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 67 | << 0 /*function call*/ << desiredArgCount << argCount |
| 68 | << call->getArg(1)->getSourceRange(); |
| 69 | } |
| 70 | |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 71 | /// Check that the first argument to __builtin_annotation is an integer |
| 72 | /// and the second argument is a non-wide string literal. |
| 73 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { |
| 74 | if (checkArgCount(S, TheCall, 2)) |
| 75 | return true; |
| 76 | |
| 77 | // First argument should be an integer. |
| 78 | Expr *ValArg = TheCall->getArg(0); |
| 79 | QualType Ty = ValArg->getType(); |
| 80 | if (!Ty->isIntegerType()) { |
| 81 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) |
| 82 | << ValArg->getSourceRange(); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 85 | |
| 86 | // Second argument should be a constant string. |
| 87 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); |
| 88 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); |
| 89 | if (!Literal || !Literal->isAscii()) { |
| 90 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) |
| 91 | << StrArg->getSourceRange(); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | TheCall->setType(Ty); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 99 | /// Check that the argument to __builtin_addressof is a glvalue, and set the |
| 100 | /// result type to the corresponding pointer type. |
| 101 | static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) { |
| 102 | if (checkArgCount(S, TheCall, 1)) |
| 103 | return true; |
| 104 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 105 | ExprResult Arg(TheCall->getArg(0)); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 106 | QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart()); |
| 107 | if (ResultType.isNull()) |
| 108 | return true; |
| 109 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 110 | TheCall->setArg(0, Arg.get()); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 111 | TheCall->setType(ResultType); |
| 112 | return false; |
| 113 | } |
| 114 | |
John McCall | 03107a4 | 2015-10-29 20:48:01 +0000 | [diff] [blame] | 115 | static bool SemaBuiltinOverflow(Sema &S, CallExpr *TheCall) { |
| 116 | if (checkArgCount(S, TheCall, 3)) |
| 117 | return true; |
| 118 | |
| 119 | // First two arguments should be integers. |
| 120 | for (unsigned I = 0; I < 2; ++I) { |
| 121 | Expr *Arg = TheCall->getArg(I); |
| 122 | QualType Ty = Arg->getType(); |
| 123 | if (!Ty->isIntegerType()) { |
| 124 | S.Diag(Arg->getLocStart(), diag::err_overflow_builtin_must_be_int) |
| 125 | << Ty << Arg->getSourceRange(); |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Third argument should be a pointer to a non-const integer. |
| 131 | // IRGen correctly handles volatile, restrict, and address spaces, and |
| 132 | // the other qualifiers aren't possible. |
| 133 | { |
| 134 | Expr *Arg = TheCall->getArg(2); |
| 135 | QualType Ty = Arg->getType(); |
| 136 | const auto *PtrTy = Ty->getAs<PointerType>(); |
| 137 | if (!(PtrTy && PtrTy->getPointeeType()->isIntegerType() && |
| 138 | !PtrTy->getPointeeType().isConstQualified())) { |
| 139 | S.Diag(Arg->getLocStart(), diag::err_overflow_builtin_must_be_ptr_int) |
| 140 | << Ty << Arg->getSourceRange(); |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 148 | static void SemaBuiltinMemChkCall(Sema &S, FunctionDecl *FDecl, |
| 149 | CallExpr *TheCall, unsigned SizeIdx, |
| 150 | unsigned DstSizeIdx) { |
| 151 | if (TheCall->getNumArgs() <= SizeIdx || |
| 152 | TheCall->getNumArgs() <= DstSizeIdx) |
| 153 | return; |
| 154 | |
| 155 | const Expr *SizeArg = TheCall->getArg(SizeIdx); |
| 156 | const Expr *DstSizeArg = TheCall->getArg(DstSizeIdx); |
| 157 | |
| 158 | llvm::APSInt Size, DstSize; |
| 159 | |
| 160 | // find out if both sizes are known at compile time |
| 161 | if (!SizeArg->EvaluateAsInt(Size, S.Context) || |
| 162 | !DstSizeArg->EvaluateAsInt(DstSize, S.Context)) |
| 163 | return; |
| 164 | |
| 165 | if (Size.ule(DstSize)) |
| 166 | return; |
| 167 | |
| 168 | // confirmed overflow so generate the diagnostic. |
| 169 | IdentifierInfo *FnName = FDecl->getIdentifier(); |
| 170 | SourceLocation SL = TheCall->getLocStart(); |
| 171 | SourceRange SR = TheCall->getSourceRange(); |
| 172 | |
| 173 | S.Diag(SL, diag::warn_memcpy_chk_overflow) << SR << FnName; |
| 174 | } |
| 175 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 176 | static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { |
| 177 | if (checkArgCount(S, BuiltinCall, 2)) |
| 178 | return true; |
| 179 | |
| 180 | SourceLocation BuiltinLoc = BuiltinCall->getLocStart(); |
| 181 | Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); |
| 182 | Expr *Call = BuiltinCall->getArg(0); |
| 183 | Expr *Chain = BuiltinCall->getArg(1); |
| 184 | |
| 185 | if (Call->getStmtClass() != Stmt::CallExprClass) { |
| 186 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) |
| 187 | << Call->getSourceRange(); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | auto CE = cast<CallExpr>(Call); |
| 192 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 193 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) |
| 194 | << Call->getSourceRange(); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | const Decl *TargetDecl = CE->getCalleeDecl(); |
| 199 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) |
| 200 | if (FD->getBuiltinID()) { |
| 201 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) |
| 202 | << Call->getSourceRange(); |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { |
| 207 | S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) |
| 208 | << Call->getSourceRange(); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | ExprResult ChainResult = S.UsualUnaryConversions(Chain); |
| 213 | if (ChainResult.isInvalid()) |
| 214 | return true; |
| 215 | if (!ChainResult.get()->getType()->isPointerType()) { |
| 216 | S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) |
| 217 | << Chain->getSourceRange(); |
| 218 | return true; |
| 219 | } |
| 220 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 221 | QualType ReturnTy = CE->getCallReturnType(S.Context); |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 222 | QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; |
| 223 | QualType BuiltinTy = S.Context.getFunctionType( |
| 224 | ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); |
| 225 | QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); |
| 226 | |
| 227 | Builtin = |
| 228 | S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); |
| 229 | |
| 230 | BuiltinCall->setType(CE->getType()); |
| 231 | BuiltinCall->setValueKind(CE->getValueKind()); |
| 232 | BuiltinCall->setObjectKind(CE->getObjectKind()); |
| 233 | BuiltinCall->setCallee(Builtin); |
| 234 | BuiltinCall->setArg(1, ChainResult.get()); |
| 235 | |
| 236 | return false; |
| 237 | } |
| 238 | |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 239 | static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, |
| 240 | Scope::ScopeFlags NeededScopeFlags, |
| 241 | unsigned DiagID) { |
| 242 | // Scopes aren't available during instantiation. Fortunately, builtin |
| 243 | // functions cannot be template args so they cannot be formed through template |
| 244 | // instantiation. Therefore checking once during the parse is sufficient. |
| 245 | if (!SemaRef.ActiveTemplateInstantiations.empty()) |
| 246 | return false; |
| 247 | |
| 248 | Scope *S = SemaRef.getCurScope(); |
| 249 | while (S && !S->isSEHExceptScope()) |
| 250 | S = S->getParent(); |
| 251 | if (!S || !(S->getFlags() & NeededScopeFlags)) { |
| 252 | auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 253 | SemaRef.Diag(TheCall->getExprLoc(), DiagID) |
| 254 | << DRE->getDecl()->getIdentifier(); |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
Xiuli Pan | bb4d8d3 | 2016-01-26 04:03:48 +0000 | [diff] [blame] | 261 | /// Returns readable name for a call. |
| 262 | static StringRef getFunctionName(CallExpr *Call) { |
| 263 | return cast<FunctionDecl>(Call->getCalleeDecl())->getName(); |
| 264 | } |
| 265 | |
| 266 | /// Returns OpenCL access qual. |
| 267 | // TODO: Refine OpenCLImageAccessAttr to OpenCLAccessAttr since pipe can use |
| 268 | // it too |
| 269 | static OpenCLImageAccessAttr *getOpenCLArgAccess(const Decl *D) { |
| 270 | if (D->hasAttr<OpenCLImageAccessAttr>()) |
| 271 | return D->getAttr<OpenCLImageAccessAttr>(); |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
| 275 | /// Returns true if pipe element type is different from the pointer. |
| 276 | static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) { |
| 277 | const Expr *Arg0 = Call->getArg(0); |
| 278 | // First argument type should always be pipe. |
| 279 | if (!Arg0->getType()->isPipeType()) { |
| 280 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_first_arg) |
| 281 | << getFunctionName(Call) << Arg0->getSourceRange(); |
| 282 | return true; |
| 283 | } |
| 284 | OpenCLImageAccessAttr *AccessQual = |
| 285 | getOpenCLArgAccess(cast<DeclRefExpr>(Arg0)->getDecl()); |
| 286 | // Validates the access qualifier is compatible with the call. |
| 287 | // OpenCL v2.0 s6.13.16 - The access qualifiers for pipe should only be |
| 288 | // read_only and write_only, and assumed to be read_only if no qualifier is |
| 289 | // specified. |
| 290 | bool isValid = true; |
| 291 | bool ReadOnly = getFunctionName(Call).find("read") != StringRef::npos; |
| 292 | if (ReadOnly) |
| 293 | isValid = AccessQual == nullptr || AccessQual->isReadOnly(); |
| 294 | else |
| 295 | isValid = AccessQual != nullptr && AccessQual->isWriteOnly(); |
| 296 | if (!isValid) { |
| 297 | const char *AM = ReadOnly ? "read_only" : "write_only"; |
| 298 | S.Diag(Arg0->getLocStart(), |
| 299 | diag::err_opencl_builtin_pipe_invalid_access_modifier) |
| 300 | << AM << Arg0->getSourceRange(); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | /// Returns true if pipe element type is different from the pointer. |
| 308 | static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) { |
| 309 | const Expr *Arg0 = Call->getArg(0); |
| 310 | const Expr *ArgIdx = Call->getArg(Idx); |
| 311 | const PipeType *PipeTy = cast<PipeType>(Arg0->getType()); |
| 312 | const Type *EltTy = PipeTy->getElementType().getTypePtr(); |
| 313 | const PointerType *ArgTy = |
| 314 | dyn_cast<PointerType>(ArgIdx->getType().getTypePtr()); |
| 315 | // The Idx argument should be a pointer and the type of the pointer and |
| 316 | // the type of pipe element should also be the same. |
| 317 | if (!ArgTy || EltTy != ArgTy->getPointeeType().getTypePtr()) { |
| 318 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) |
| 319 | << getFunctionName(Call) |
| 320 | << S.Context.getPointerType(PipeTy->getElementType()) |
| 321 | << ArgIdx->getSourceRange(); |
| 322 | return true; |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | // \brief Performs semantic analysis for the read/write_pipe call. |
| 328 | // \param S Reference to the semantic analyzer. |
| 329 | // \param Call A pointer to the builtin call. |
| 330 | // \return True if a semantic error has been found, false otherwise. |
| 331 | static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) { |
| 332 | // Two kinds of read/write pipe |
| 333 | // From OpenCL C Specification 6.13.16.2 the built-in read/write |
| 334 | // functions have following forms. |
| 335 | switch (Call->getNumArgs()) { |
| 336 | case 2: { |
| 337 | if (checkOpenCLPipeArg(S, Call)) |
| 338 | return true; |
| 339 | // The call with 2 arguments should be |
| 340 | // read/write_pipe(pipe T, T*) |
| 341 | // check packet type T |
| 342 | if (checkOpenCLPipePacketType(S, Call, 1)) |
| 343 | return true; |
| 344 | } break; |
| 345 | |
| 346 | case 4: { |
| 347 | if (checkOpenCLPipeArg(S, Call)) |
| 348 | return true; |
| 349 | // The call with 4 arguments should be |
| 350 | // read/write_pipe(pipe T, reserve_id_t, uint, T*) |
| 351 | // check reserve_id_t |
| 352 | if (!Call->getArg(1)->getType()->isReserveIDT()) { |
| 353 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) |
| 354 | << getFunctionName(Call) << S.Context.OCLReserveIDTy |
| 355 | << Call->getArg(1)->getSourceRange(); |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | // check the index |
| 360 | const Expr *Arg2 = Call->getArg(2); |
| 361 | if (!Arg2->getType()->isIntegerType() && |
| 362 | !Arg2->getType()->isUnsignedIntegerType()) { |
| 363 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) |
| 364 | << getFunctionName(Call) << S.Context.UnsignedIntTy |
| 365 | << Arg2->getSourceRange(); |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | // check packet type T |
| 370 | if (checkOpenCLPipePacketType(S, Call, 3)) |
| 371 | return true; |
| 372 | } break; |
| 373 | default: |
| 374 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_arg_num) |
| 375 | << getFunctionName(Call) << Call->getSourceRange(); |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | // \brief Performs a semantic analysis on the {work_group_/sub_group_ |
| 383 | // /_}reserve_{read/write}_pipe |
| 384 | // \param S Reference to the semantic analyzer. |
| 385 | // \param Call The call to the builtin function to be analyzed. |
| 386 | // \return True if a semantic error was found, false otherwise. |
| 387 | static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) { |
| 388 | if (checkArgCount(S, Call, 2)) |
| 389 | return true; |
| 390 | |
| 391 | if (checkOpenCLPipeArg(S, Call)) |
| 392 | return true; |
| 393 | |
| 394 | // check the reserve size |
| 395 | if (!Call->getArg(1)->getType()->isIntegerType() && |
| 396 | !Call->getArg(1)->getType()->isUnsignedIntegerType()) { |
| 397 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) |
| 398 | << getFunctionName(Call) << S.Context.UnsignedIntTy |
| 399 | << Call->getArg(1)->getSourceRange(); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | // \brief Performs a semantic analysis on {work_group_/sub_group_ |
| 407 | // /_}commit_{read/write}_pipe |
| 408 | // \param S Reference to the semantic analyzer. |
| 409 | // \param Call The call to the builtin function to be analyzed. |
| 410 | // \return True if a semantic error was found, false otherwise. |
| 411 | static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) { |
| 412 | if (checkArgCount(S, Call, 2)) |
| 413 | return true; |
| 414 | |
| 415 | if (checkOpenCLPipeArg(S, Call)) |
| 416 | return true; |
| 417 | |
| 418 | // check reserve_id_t |
| 419 | if (!Call->getArg(1)->getType()->isReserveIDT()) { |
| 420 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) |
| 421 | << getFunctionName(Call) << S.Context.OCLReserveIDTy |
| 422 | << Call->getArg(1)->getSourceRange(); |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | // \brief Performs a semantic analysis on the call to built-in Pipe |
| 430 | // Query Functions. |
| 431 | // \param S Reference to the semantic analyzer. |
| 432 | // \param Call The call to the builtin function to be analyzed. |
| 433 | // \return True if a semantic error was found, false otherwise. |
| 434 | static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) { |
| 435 | if (checkArgCount(S, Call, 1)) |
| 436 | return true; |
| 437 | |
| 438 | if (!Call->getArg(0)->getType()->isPipeType()) { |
| 439 | S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_first_arg) |
| 440 | << getFunctionName(Call) << Call->getArg(0)->getSourceRange(); |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | return false; |
| 445 | } |
| 446 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 447 | ExprResult |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 448 | Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, |
| 449 | CallExpr *TheCall) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 450 | ExprResult TheCallResult(TheCall); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 3be167f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 452 | // Find out if any arguments are required to be integer constant expressions. |
| 453 | unsigned ICEArguments = 0; |
| 454 | ASTContext::GetBuiltinTypeError Error; |
| 455 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 456 | if (Error != ASTContext::GE_None) |
| 457 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 458 | |
| 459 | // If any arguments are required to be ICE's, check and diagnose. |
| 460 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 461 | // Skip arguments not required to be ICE's. |
| 462 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 463 | |
| 464 | llvm::APSInt Result; |
| 465 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 466 | return true; |
| 467 | ICEArguments &= ~(1 << ArgNo); |
| 468 | } |
| 469 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 470 | switch (BuiltinID) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 471 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 472 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 473 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 474 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 475 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 476 | break; |
Ted Kremenek | a174c52 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 477 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 478 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 479 | if (SemaBuiltinVAStart(TheCall)) |
| 480 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 481 | break; |
Saleem Abdulrasool | 202aac1 | 2014-07-22 02:01:04 +0000 | [diff] [blame] | 482 | case Builtin::BI__va_start: { |
| 483 | switch (Context.getTargetInfo().getTriple().getArch()) { |
| 484 | case llvm::Triple::arm: |
| 485 | case llvm::Triple::thumb: |
| 486 | if (SemaBuiltinVAStartARM(TheCall)) |
| 487 | return ExprError(); |
| 488 | break; |
| 489 | default: |
| 490 | if (SemaBuiltinVAStart(TheCall)) |
| 491 | return ExprError(); |
| 492 | break; |
| 493 | } |
| 494 | break; |
| 495 | } |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 496 | case Builtin::BI__builtin_isgreater: |
| 497 | case Builtin::BI__builtin_isgreaterequal: |
| 498 | case Builtin::BI__builtin_isless: |
| 499 | case Builtin::BI__builtin_islessequal: |
| 500 | case Builtin::BI__builtin_islessgreater: |
| 501 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 502 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 503 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 504 | break; |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 505 | case Builtin::BI__builtin_fpclassify: |
| 506 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 507 | return ExprError(); |
| 508 | break; |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 509 | case Builtin::BI__builtin_isfinite: |
| 510 | case Builtin::BI__builtin_isinf: |
| 511 | case Builtin::BI__builtin_isinf_sign: |
| 512 | case Builtin::BI__builtin_isnan: |
| 513 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 514 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 515 | return ExprError(); |
| 516 | break; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 517 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 518 | return SemaBuiltinShuffleVector(TheCall); |
| 519 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 520 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 521 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 522 | if (SemaBuiltinPrefetch(TheCall)) |
| 523 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 524 | break; |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 525 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 526 | case Builtin::BI__builtin_assume: |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 527 | if (SemaBuiltinAssume(TheCall)) |
| 528 | return ExprError(); |
| 529 | break; |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 530 | case Builtin::BI__builtin_assume_aligned: |
| 531 | if (SemaBuiltinAssumeAligned(TheCall)) |
| 532 | return ExprError(); |
| 533 | break; |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 534 | case Builtin::BI__builtin_object_size: |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 535 | if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 536 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 537 | break; |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 538 | case Builtin::BI__builtin_longjmp: |
| 539 | if (SemaBuiltinLongjmp(TheCall)) |
| 540 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 541 | break; |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 542 | case Builtin::BI__builtin_setjmp: |
| 543 | if (SemaBuiltinSetjmp(TheCall)) |
| 544 | return ExprError(); |
| 545 | break; |
David Majnemer | c403a1c | 2015-03-20 17:03:35 +0000 | [diff] [blame] | 546 | case Builtin::BI_setjmp: |
| 547 | case Builtin::BI_setjmpex: |
| 548 | if (checkArgCount(*this, TheCall, 1)) |
| 549 | return true; |
| 550 | break; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 551 | |
| 552 | case Builtin::BI__builtin_classify_type: |
| 553 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 554 | TheCall->setType(Context.IntTy); |
| 555 | break; |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 556 | case Builtin::BI__builtin_constant_p: |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 557 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 558 | TheCall->setType(Context.IntTy); |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 559 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 560 | case Builtin::BI__sync_fetch_and_add: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 561 | case Builtin::BI__sync_fetch_and_add_1: |
| 562 | case Builtin::BI__sync_fetch_and_add_2: |
| 563 | case Builtin::BI__sync_fetch_and_add_4: |
| 564 | case Builtin::BI__sync_fetch_and_add_8: |
| 565 | case Builtin::BI__sync_fetch_and_add_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 566 | case Builtin::BI__sync_fetch_and_sub: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 567 | case Builtin::BI__sync_fetch_and_sub_1: |
| 568 | case Builtin::BI__sync_fetch_and_sub_2: |
| 569 | case Builtin::BI__sync_fetch_and_sub_4: |
| 570 | case Builtin::BI__sync_fetch_and_sub_8: |
| 571 | case Builtin::BI__sync_fetch_and_sub_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 572 | case Builtin::BI__sync_fetch_and_or: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 573 | case Builtin::BI__sync_fetch_and_or_1: |
| 574 | case Builtin::BI__sync_fetch_and_or_2: |
| 575 | case Builtin::BI__sync_fetch_and_or_4: |
| 576 | case Builtin::BI__sync_fetch_and_or_8: |
| 577 | case Builtin::BI__sync_fetch_and_or_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 578 | case Builtin::BI__sync_fetch_and_and: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 579 | case Builtin::BI__sync_fetch_and_and_1: |
| 580 | case Builtin::BI__sync_fetch_and_and_2: |
| 581 | case Builtin::BI__sync_fetch_and_and_4: |
| 582 | case Builtin::BI__sync_fetch_and_and_8: |
| 583 | case Builtin::BI__sync_fetch_and_and_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 584 | case Builtin::BI__sync_fetch_and_xor: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 585 | case Builtin::BI__sync_fetch_and_xor_1: |
| 586 | case Builtin::BI__sync_fetch_and_xor_2: |
| 587 | case Builtin::BI__sync_fetch_and_xor_4: |
| 588 | case Builtin::BI__sync_fetch_and_xor_8: |
| 589 | case Builtin::BI__sync_fetch_and_xor_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 590 | case Builtin::BI__sync_fetch_and_nand: |
| 591 | case Builtin::BI__sync_fetch_and_nand_1: |
| 592 | case Builtin::BI__sync_fetch_and_nand_2: |
| 593 | case Builtin::BI__sync_fetch_and_nand_4: |
| 594 | case Builtin::BI__sync_fetch_and_nand_8: |
| 595 | case Builtin::BI__sync_fetch_and_nand_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 596 | case Builtin::BI__sync_add_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 597 | case Builtin::BI__sync_add_and_fetch_1: |
| 598 | case Builtin::BI__sync_add_and_fetch_2: |
| 599 | case Builtin::BI__sync_add_and_fetch_4: |
| 600 | case Builtin::BI__sync_add_and_fetch_8: |
| 601 | case Builtin::BI__sync_add_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 602 | case Builtin::BI__sync_sub_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 603 | case Builtin::BI__sync_sub_and_fetch_1: |
| 604 | case Builtin::BI__sync_sub_and_fetch_2: |
| 605 | case Builtin::BI__sync_sub_and_fetch_4: |
| 606 | case Builtin::BI__sync_sub_and_fetch_8: |
| 607 | case Builtin::BI__sync_sub_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 608 | case Builtin::BI__sync_and_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 609 | case Builtin::BI__sync_and_and_fetch_1: |
| 610 | case Builtin::BI__sync_and_and_fetch_2: |
| 611 | case Builtin::BI__sync_and_and_fetch_4: |
| 612 | case Builtin::BI__sync_and_and_fetch_8: |
| 613 | case Builtin::BI__sync_and_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 614 | case Builtin::BI__sync_or_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 615 | case Builtin::BI__sync_or_and_fetch_1: |
| 616 | case Builtin::BI__sync_or_and_fetch_2: |
| 617 | case Builtin::BI__sync_or_and_fetch_4: |
| 618 | case Builtin::BI__sync_or_and_fetch_8: |
| 619 | case Builtin::BI__sync_or_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 620 | case Builtin::BI__sync_xor_and_fetch: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 621 | case Builtin::BI__sync_xor_and_fetch_1: |
| 622 | case Builtin::BI__sync_xor_and_fetch_2: |
| 623 | case Builtin::BI__sync_xor_and_fetch_4: |
| 624 | case Builtin::BI__sync_xor_and_fetch_8: |
| 625 | case Builtin::BI__sync_xor_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 626 | case Builtin::BI__sync_nand_and_fetch: |
| 627 | case Builtin::BI__sync_nand_and_fetch_1: |
| 628 | case Builtin::BI__sync_nand_and_fetch_2: |
| 629 | case Builtin::BI__sync_nand_and_fetch_4: |
| 630 | case Builtin::BI__sync_nand_and_fetch_8: |
| 631 | case Builtin::BI__sync_nand_and_fetch_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 632 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 633 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 634 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 635 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 636 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 637 | case Builtin::BI__sync_val_compare_and_swap_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 638 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 639 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 640 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 641 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 642 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 643 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 644 | case Builtin::BI__sync_lock_test_and_set: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 645 | case Builtin::BI__sync_lock_test_and_set_1: |
| 646 | case Builtin::BI__sync_lock_test_and_set_2: |
| 647 | case Builtin::BI__sync_lock_test_and_set_4: |
| 648 | case Builtin::BI__sync_lock_test_and_set_8: |
| 649 | case Builtin::BI__sync_lock_test_and_set_16: |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 650 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 651 | case Builtin::BI__sync_lock_release_1: |
| 652 | case Builtin::BI__sync_lock_release_2: |
| 653 | case Builtin::BI__sync_lock_release_4: |
| 654 | case Builtin::BI__sync_lock_release_8: |
| 655 | case Builtin::BI__sync_lock_release_16: |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 656 | case Builtin::BI__sync_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 657 | case Builtin::BI__sync_swap_1: |
| 658 | case Builtin::BI__sync_swap_2: |
| 659 | case Builtin::BI__sync_swap_4: |
| 660 | case Builtin::BI__sync_swap_8: |
| 661 | case Builtin::BI__sync_swap_16: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 662 | return SemaBuiltinAtomicOverloaded(TheCallResult); |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 663 | case Builtin::BI__builtin_nontemporal_load: |
| 664 | case Builtin::BI__builtin_nontemporal_store: |
| 665 | return SemaBuiltinNontemporalOverloaded(TheCallResult); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 666 | #define BUILTIN(ID, TYPE, ATTRS) |
| 667 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ |
| 668 | case Builtin::BI##ID: \ |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 669 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 670 | #include "clang/Basic/Builtins.def" |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 671 | case Builtin::BI__builtin_annotation: |
Julien Lerouge | 4a5b444 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 672 | if (SemaBuiltinAnnotation(*this, TheCall)) |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 673 | return ExprError(); |
| 674 | break; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 675 | case Builtin::BI__builtin_addressof: |
| 676 | if (SemaBuiltinAddressof(*this, TheCall)) |
| 677 | return ExprError(); |
| 678 | break; |
John McCall | 03107a4 | 2015-10-29 20:48:01 +0000 | [diff] [blame] | 679 | case Builtin::BI__builtin_add_overflow: |
| 680 | case Builtin::BI__builtin_sub_overflow: |
| 681 | case Builtin::BI__builtin_mul_overflow: |
Craig Topper | a86e70d | 2015-11-07 06:16:14 +0000 | [diff] [blame] | 682 | if (SemaBuiltinOverflow(*this, TheCall)) |
| 683 | return ExprError(); |
| 684 | break; |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 685 | case Builtin::BI__builtin_operator_new: |
| 686 | case Builtin::BI__builtin_operator_delete: |
| 687 | if (!getLangOpts().CPlusPlus) { |
| 688 | Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) |
| 689 | << (BuiltinID == Builtin::BI__builtin_operator_new |
| 690 | ? "__builtin_operator_new" |
| 691 | : "__builtin_operator_delete") |
| 692 | << "C++"; |
| 693 | return ExprError(); |
| 694 | } |
| 695 | // CodeGen assumes it can find the global new and delete to call, |
| 696 | // so ensure that they are declared. |
| 697 | DeclareGlobalNewDelete(); |
| 698 | break; |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 699 | |
| 700 | // check secure string manipulation functions where overflows |
| 701 | // are detectable at compile time |
| 702 | case Builtin::BI__builtin___memcpy_chk: |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 703 | case Builtin::BI__builtin___memmove_chk: |
| 704 | case Builtin::BI__builtin___memset_chk: |
| 705 | case Builtin::BI__builtin___strlcat_chk: |
| 706 | case Builtin::BI__builtin___strlcpy_chk: |
| 707 | case Builtin::BI__builtin___strncat_chk: |
| 708 | case Builtin::BI__builtin___strncpy_chk: |
| 709 | case Builtin::BI__builtin___stpncpy_chk: |
| 710 | SemaBuiltinMemChkCall(*this, FDecl, TheCall, 2, 3); |
| 711 | break; |
Steven Wu | 566c14e | 2014-09-24 04:37:33 +0000 | [diff] [blame] | 712 | case Builtin::BI__builtin___memccpy_chk: |
| 713 | SemaBuiltinMemChkCall(*this, FDecl, TheCall, 3, 4); |
| 714 | break; |
Fariborz Jahanian | 3e6a0be | 2014-09-18 17:58:27 +0000 | [diff] [blame] | 715 | case Builtin::BI__builtin___snprintf_chk: |
| 716 | case Builtin::BI__builtin___vsnprintf_chk: |
| 717 | SemaBuiltinMemChkCall(*this, FDecl, TheCall, 1, 3); |
| 718 | break; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 719 | |
| 720 | case Builtin::BI__builtin_call_with_static_chain: |
| 721 | if (SemaBuiltinCallWithStaticChain(*this, TheCall)) |
| 722 | return ExprError(); |
| 723 | break; |
Reid Kleckner | 1d59f99 | 2015-01-22 01:36:17 +0000 | [diff] [blame] | 724 | |
| 725 | case Builtin::BI__exception_code: |
| 726 | case Builtin::BI_exception_code: { |
| 727 | if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, |
| 728 | diag::err_seh___except_block)) |
| 729 | return ExprError(); |
| 730 | break; |
| 731 | } |
| 732 | case Builtin::BI__exception_info: |
| 733 | case Builtin::BI_exception_info: { |
| 734 | if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, |
| 735 | diag::err_seh___except_filter)) |
| 736 | return ExprError(); |
| 737 | break; |
| 738 | } |
| 739 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 740 | case Builtin::BI__GetExceptionInfo: |
| 741 | if (checkArgCount(*this, TheCall, 1)) |
| 742 | return ExprError(); |
| 743 | |
| 744 | if (CheckCXXThrowOperand( |
| 745 | TheCall->getLocStart(), |
| 746 | Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), |
| 747 | TheCall)) |
| 748 | return ExprError(); |
| 749 | |
| 750 | TheCall->setType(Context.VoidPtrTy); |
| 751 | break; |
Xiuli Pan | bb4d8d3 | 2016-01-26 04:03:48 +0000 | [diff] [blame] | 752 | case Builtin::BIread_pipe: |
| 753 | case Builtin::BIwrite_pipe: |
| 754 | // Since those two functions are declared with var args, we need a semantic |
| 755 | // check for the argument. |
| 756 | if (SemaBuiltinRWPipe(*this, TheCall)) |
| 757 | return ExprError(); |
| 758 | break; |
| 759 | case Builtin::BIreserve_read_pipe: |
| 760 | case Builtin::BIreserve_write_pipe: |
| 761 | case Builtin::BIwork_group_reserve_read_pipe: |
| 762 | case Builtin::BIwork_group_reserve_write_pipe: |
| 763 | case Builtin::BIsub_group_reserve_read_pipe: |
| 764 | case Builtin::BIsub_group_reserve_write_pipe: |
| 765 | if (SemaBuiltinReserveRWPipe(*this, TheCall)) |
| 766 | return ExprError(); |
| 767 | // Since return type of reserve_read/write_pipe built-in function is |
| 768 | // reserve_id_t, which is not defined in the builtin def file , we used int |
| 769 | // as return type and need to override the return type of these functions. |
| 770 | TheCall->setType(Context.OCLReserveIDTy); |
| 771 | break; |
| 772 | case Builtin::BIcommit_read_pipe: |
| 773 | case Builtin::BIcommit_write_pipe: |
| 774 | case Builtin::BIwork_group_commit_read_pipe: |
| 775 | case Builtin::BIwork_group_commit_write_pipe: |
| 776 | case Builtin::BIsub_group_commit_read_pipe: |
| 777 | case Builtin::BIsub_group_commit_write_pipe: |
| 778 | if (SemaBuiltinCommitRWPipe(*this, TheCall)) |
| 779 | return ExprError(); |
| 780 | break; |
| 781 | case Builtin::BIget_pipe_num_packets: |
| 782 | case Builtin::BIget_pipe_max_packets: |
| 783 | if (SemaBuiltinPipePackets(*this, TheCall)) |
| 784 | return ExprError(); |
| 785 | break; |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 786 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 787 | } |
Richard Smith | 760520b | 2014-06-03 23:27:44 +0000 | [diff] [blame] | 788 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 789 | // Since the target specific builtins for each arch overlap, only check those |
| 790 | // of the arch we are compiling for. |
Artem Belevich | 9674a64 | 2015-09-22 17:23:05 +0000 | [diff] [blame] | 791 | if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 792 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 793 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 794 | case llvm::Triple::armeb: |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 795 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 796 | case llvm::Triple::thumbeb: |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 797 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 798 | return ExprError(); |
| 799 | break; |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 800 | case llvm::Triple::aarch64: |
| 801 | case llvm::Triple::aarch64_be: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 802 | if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 803 | return ExprError(); |
| 804 | break; |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 805 | case llvm::Triple::mips: |
| 806 | case llvm::Triple::mipsel: |
| 807 | case llvm::Triple::mips64: |
| 808 | case llvm::Triple::mips64el: |
| 809 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) |
| 810 | return ExprError(); |
| 811 | break; |
Ulrich Weigand | 3a610eb | 2015-04-01 12:54:25 +0000 | [diff] [blame] | 812 | case llvm::Triple::systemz: |
| 813 | if (CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall)) |
| 814 | return ExprError(); |
| 815 | break; |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 816 | case llvm::Triple::x86: |
| 817 | case llvm::Triple::x86_64: |
| 818 | if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall)) |
| 819 | return ExprError(); |
| 820 | break; |
Kit Barton | e50adcb | 2015-03-30 19:40:59 +0000 | [diff] [blame] | 821 | case llvm::Triple::ppc: |
| 822 | case llvm::Triple::ppc64: |
| 823 | case llvm::Triple::ppc64le: |
| 824 | if (CheckPPCBuiltinFunctionCall(BuiltinID, TheCall)) |
| 825 | return ExprError(); |
| 826 | break; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 827 | default: |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 832 | return TheCallResult; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 835 | // Get the valid immediate range for the specified NEON type code. |
Tim Northover | 3402dc7 | 2014-02-12 12:04:59 +0000 | [diff] [blame] | 836 | static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) { |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 837 | NeonTypeFlags Type(t); |
Tim Northover | 3402dc7 | 2014-02-12 12:04:59 +0000 | [diff] [blame] | 838 | int IsQuad = ForceQuad ? true : Type.isQuad(); |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 839 | switch (Type.getEltType()) { |
| 840 | case NeonTypeFlags::Int8: |
| 841 | case NeonTypeFlags::Poly8: |
| 842 | return shift ? 7 : (8 << IsQuad) - 1; |
| 843 | case NeonTypeFlags::Int16: |
| 844 | case NeonTypeFlags::Poly16: |
| 845 | return shift ? 15 : (4 << IsQuad) - 1; |
| 846 | case NeonTypeFlags::Int32: |
| 847 | return shift ? 31 : (2 << IsQuad) - 1; |
| 848 | case NeonTypeFlags::Int64: |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 849 | case NeonTypeFlags::Poly64: |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 850 | return shift ? 63 : (1 << IsQuad) - 1; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 851 | case NeonTypeFlags::Poly128: |
| 852 | return shift ? 127 : (1 << IsQuad) - 1; |
Bob Wilson | 98bc98c | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 853 | case NeonTypeFlags::Float16: |
| 854 | assert(!shift && "cannot shift float types!"); |
| 855 | return (4 << IsQuad) - 1; |
| 856 | case NeonTypeFlags::Float32: |
| 857 | assert(!shift && "cannot shift float types!"); |
| 858 | return (2 << IsQuad) - 1; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 859 | case NeonTypeFlags::Float64: |
| 860 | assert(!shift && "cannot shift float types!"); |
| 861 | return (1 << IsQuad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 862 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 863 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 866 | /// getNeonEltType - Return the QualType corresponding to the elements of |
| 867 | /// the vector type specified by the NeonTypeFlags. This is used to check |
| 868 | /// the pointer arguments for Neon load/store intrinsics. |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 869 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 870 | bool IsPolyUnsigned, bool IsInt64Long) { |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 871 | switch (Flags.getEltType()) { |
| 872 | case NeonTypeFlags::Int8: |
| 873 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; |
| 874 | case NeonTypeFlags::Int16: |
| 875 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; |
| 876 | case NeonTypeFlags::Int32: |
| 877 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; |
| 878 | case NeonTypeFlags::Int64: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 879 | if (IsInt64Long) |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 880 | return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy; |
| 881 | else |
| 882 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy |
| 883 | : Context.LongLongTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 884 | case NeonTypeFlags::Poly8: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 885 | return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 886 | case NeonTypeFlags::Poly16: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 887 | return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy; |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 888 | case NeonTypeFlags::Poly64: |
Kevin Qin | 78b8653 | 2015-05-14 08:18:05 +0000 | [diff] [blame] | 889 | if (IsInt64Long) |
| 890 | return Context.UnsignedLongTy; |
| 891 | else |
| 892 | return Context.UnsignedLongLongTy; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 893 | case NeonTypeFlags::Poly128: |
| 894 | break; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 895 | case NeonTypeFlags::Float16: |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 896 | return Context.HalfTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 897 | case NeonTypeFlags::Float32: |
| 898 | return Context.FloatTy; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 899 | case NeonTypeFlags::Float64: |
| 900 | return Context.DoubleTy; |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 901 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 902 | llvm_unreachable("Invalid NeonTypeFlag!"); |
Bob Wilson | e4d7723 | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 905 | bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 906 | llvm::APSInt Result; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 907 | uint64_t mask = 0; |
| 908 | unsigned TV = 0; |
| 909 | int PtrArgNum = -1; |
| 910 | bool HasConstPtr = false; |
| 911 | switch (BuiltinID) { |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 912 | #define GET_NEON_OVERLOAD_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 913 | #include "clang/Basic/arm_neon.inc" |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 914 | #undef GET_NEON_OVERLOAD_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 918 | // the immediate which specifies which variant to emit. |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 919 | unsigned ImmArg = TheCall->getNumArgs()-1; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 920 | if (mask) { |
| 921 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) |
| 922 | return true; |
| 923 | |
| 924 | TV = Result.getLimitedValue(64); |
| 925 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) |
| 926 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 927 | << TheCall->getArg(ImmArg)->getSourceRange(); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | if (PtrArgNum >= 0) { |
| 931 | // Check that pointer arguments have the specified type. |
| 932 | Expr *Arg = TheCall->getArg(PtrArgNum); |
| 933 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) |
| 934 | Arg = ICE->getSubExpr(); |
| 935 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); |
| 936 | QualType RHSTy = RHS.get()->getType(); |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 937 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 938 | llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 939 | bool IsPolyUnsigned = Arch == llvm::Triple::aarch64; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 940 | bool IsInt64Long = |
| 941 | Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong; |
| 942 | QualType EltTy = |
| 943 | getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 944 | if (HasConstPtr) |
| 945 | EltTy = EltTy.withConst(); |
| 946 | QualType LHSTy = Context.getPointerType(EltTy); |
| 947 | AssignConvertType ConvTy; |
| 948 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| 949 | if (RHS.isInvalid()) |
| 950 | return true; |
| 951 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, |
| 952 | RHS.get(), AA_Assigning)) |
| 953 | return true; |
| 954 | } |
| 955 | |
| 956 | // For NEON intrinsics which take an immediate value as part of the |
| 957 | // instruction, range check them here. |
| 958 | unsigned i = 0, l = 0, u = 0; |
| 959 | switch (BuiltinID) { |
| 960 | default: |
| 961 | return false; |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 962 | #define GET_NEON_IMMEDIATE_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 963 | #include "clang/Basic/arm_neon.inc" |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 964 | #undef GET_NEON_IMMEDIATE_CHECK |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 965 | } |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 966 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 967 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 970 | bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall, |
| 971 | unsigned MaxWidth) { |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 972 | assert((BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 973 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 974 | BuiltinID == ARM::BI__builtin_arm_strex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 975 | BuiltinID == ARM::BI__builtin_arm_stlex || |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 976 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 977 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
| 978 | BuiltinID == AArch64::BI__builtin_arm_strex || |
| 979 | BuiltinID == AArch64::BI__builtin_arm_stlex) && |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 980 | "unexpected ARM builtin"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 981 | bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 982 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
| 983 | BuiltinID == AArch64::BI__builtin_arm_ldrex || |
| 984 | BuiltinID == AArch64::BI__builtin_arm_ldaex; |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 985 | |
| 986 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 987 | |
| 988 | // Ensure that we have the proper number of arguments. |
| 989 | if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2)) |
| 990 | return true; |
| 991 | |
| 992 | // Inspect the pointer argument of the atomic builtin. This should always be |
| 993 | // a pointer type, whose element is an integral scalar or pointer type. |
| 994 | // Because it is a pointer type, we don't have to worry about any implicit |
| 995 | // casts here. |
| 996 | Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1); |
| 997 | ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg); |
| 998 | if (PointerArgRes.isInvalid()) |
| 999 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1000 | PointerArg = PointerArgRes.get(); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1001 | |
| 1002 | const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); |
| 1003 | if (!pointerType) { |
| 1004 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 1005 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
| 1009 | // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next |
| 1010 | // task is to insert the appropriate casts into the AST. First work out just |
| 1011 | // what the appropriate type is. |
| 1012 | QualType ValType = pointerType->getPointeeType(); |
| 1013 | QualType AddrType = ValType.getUnqualifiedType().withVolatile(); |
| 1014 | if (IsLdrex) |
| 1015 | AddrType.addConst(); |
| 1016 | |
| 1017 | // Issue a warning if the cast is dodgy. |
| 1018 | CastKind CastNeeded = CK_NoOp; |
| 1019 | if (!AddrType.isAtLeastAsQualifiedAs(ValType)) { |
| 1020 | CastNeeded = CK_BitCast; |
| 1021 | Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers) |
| 1022 | << PointerArg->getType() |
| 1023 | << Context.getPointerType(AddrType) |
| 1024 | << AA_Passing << PointerArg->getSourceRange(); |
| 1025 | } |
| 1026 | |
| 1027 | // Finally, do the cast and replace the argument with the corrected version. |
| 1028 | AddrType = Context.getPointerType(AddrType); |
| 1029 | PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded); |
| 1030 | if (PointerArgRes.isInvalid()) |
| 1031 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1032 | PointerArg = PointerArgRes.get(); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1033 | |
| 1034 | TheCall->setArg(IsLdrex ? 0 : 1, PointerArg); |
| 1035 | |
| 1036 | // In general, we allow ints, floats and pointers to be loaded and stored. |
| 1037 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
| 1038 | !ValType->isBlockPointerType() && !ValType->isFloatingType()) { |
| 1039 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr) |
| 1040 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | // But ARM doesn't have instructions to deal with 128-bit versions. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 1045 | if (Context.getTypeSize(ValType) > MaxWidth) { |
| 1046 | assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate"); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1047 | Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size) |
| 1048 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 1049 | return true; |
| 1050 | } |
| 1051 | |
| 1052 | switch (ValType.getObjCLifetime()) { |
| 1053 | case Qualifiers::OCL_None: |
| 1054 | case Qualifiers::OCL_ExplicitNone: |
| 1055 | // okay |
| 1056 | break; |
| 1057 | |
| 1058 | case Qualifiers::OCL_Weak: |
| 1059 | case Qualifiers::OCL_Strong: |
| 1060 | case Qualifiers::OCL_Autoreleasing: |
| 1061 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 1062 | << ValType << PointerArg->getSourceRange(); |
| 1063 | return true; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | if (IsLdrex) { |
| 1068 | TheCall->setType(ValType); |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
| 1072 | // Initialize the argument to be stored. |
| 1073 | ExprResult ValArg = TheCall->getArg(0); |
| 1074 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 1075 | Context, ValType, /*consume*/ false); |
| 1076 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
| 1077 | if (ValArg.isInvalid()) |
| 1078 | return true; |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1079 | TheCall->setArg(0, ValArg.get()); |
Tim Northover | 58d2bb1 | 2013-10-29 12:32:58 +0000 | [diff] [blame] | 1080 | |
| 1081 | // __builtin_arm_strex always returns an int. It's marked as such in the .def, |
| 1082 | // but the custom checker bypasses all default analysis. |
| 1083 | TheCall->setType(Context.IntTy); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1084 | return false; |
| 1085 | } |
| 1086 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 1087 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 1088 | llvm::APSInt Result; |
| 1089 | |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1090 | if (BuiltinID == ARM::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 1091 | BuiltinID == ARM::BI__builtin_arm_ldaex || |
| 1092 | BuiltinID == ARM::BI__builtin_arm_strex || |
| 1093 | BuiltinID == ARM::BI__builtin_arm_stlex) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 1094 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64); |
Tim Northover | 6aacd49 | 2013-07-16 09:47:53 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Yi Kong | 26d104a | 2014-08-13 19:18:14 +0000 | [diff] [blame] | 1097 | if (BuiltinID == ARM::BI__builtin_arm_prefetch) { |
| 1098 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
| 1099 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 1); |
| 1100 | } |
| 1101 | |
Luke Cheeseman | 59b2d83 | 2015-06-15 17:51:01 +0000 | [diff] [blame] | 1102 | if (BuiltinID == ARM::BI__builtin_arm_rsr64 || |
| 1103 | BuiltinID == ARM::BI__builtin_arm_wsr64) |
| 1104 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false); |
| 1105 | |
| 1106 | if (BuiltinID == ARM::BI__builtin_arm_rsr || |
| 1107 | BuiltinID == ARM::BI__builtin_arm_rsrp || |
| 1108 | BuiltinID == ARM::BI__builtin_arm_wsr || |
| 1109 | BuiltinID == ARM::BI__builtin_arm_wsrp) |
| 1110 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); |
| 1111 | |
Tim Northover | 1267041 | 2014-02-19 10:37:05 +0000 | [diff] [blame] | 1112 | if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) |
| 1113 | return true; |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 1114 | |
Yi Kong | 4efadfb | 2014-07-03 16:01:25 +0000 | [diff] [blame] | 1115 | // For intrinsics which take an immediate value as part of the instruction, |
| 1116 | // range check them here. |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 1117 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 1118 | switch (BuiltinID) { |
| 1119 | default: return false; |
Nate Begeman | 1194bd2 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 1120 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 1121 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 1122 | case ARM::BI__builtin_arm_vcvtr_f: |
| 1123 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Weiming Zhao | 87bb492 | 2013-11-12 21:42:50 +0000 | [diff] [blame] | 1124 | case ARM::BI__builtin_arm_dmb: |
Yi Kong | 4efadfb | 2014-07-03 16:01:25 +0000 | [diff] [blame] | 1125 | case ARM::BI__builtin_arm_dsb: |
Yi Kong | 1d268af | 2014-08-26 12:48:06 +0000 | [diff] [blame] | 1126 | case ARM::BI__builtin_arm_isb: |
| 1127 | case ARM::BI__builtin_arm_dbg: l = 0; u = 15; break; |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1128 | } |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 1129 | |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 1130 | // FIXME: VFP Intrinsics should error if VFP not present. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1131 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1132 | } |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 1133 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 1134 | bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 1135 | CallExpr *TheCall) { |
| 1136 | llvm::APSInt Result; |
| 1137 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 1138 | if (BuiltinID == AArch64::BI__builtin_arm_ldrex || |
Tim Northover | 3acd6bd | 2014-07-02 12:56:02 +0000 | [diff] [blame] | 1139 | BuiltinID == AArch64::BI__builtin_arm_ldaex || |
| 1140 | BuiltinID == AArch64::BI__builtin_arm_strex || |
| 1141 | BuiltinID == AArch64::BI__builtin_arm_stlex) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 1142 | return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128); |
| 1143 | } |
| 1144 | |
Yi Kong | a554843 | 2014-08-13 19:18:20 +0000 | [diff] [blame] | 1145 | if (BuiltinID == AArch64::BI__builtin_arm_prefetch) { |
| 1146 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
| 1147 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) || |
| 1148 | SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) || |
| 1149 | SemaBuiltinConstantArgRange(TheCall, 4, 0, 1); |
| 1150 | } |
| 1151 | |
Luke Cheeseman | 59b2d83 | 2015-06-15 17:51:01 +0000 | [diff] [blame] | 1152 | if (BuiltinID == AArch64::BI__builtin_arm_rsr64 || |
| 1153 | BuiltinID == AArch64::BI__builtin_arm_wsr64) |
| 1154 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, false); |
| 1155 | |
| 1156 | if (BuiltinID == AArch64::BI__builtin_arm_rsr || |
| 1157 | BuiltinID == AArch64::BI__builtin_arm_rsrp || |
| 1158 | BuiltinID == AArch64::BI__builtin_arm_wsr || |
| 1159 | BuiltinID == AArch64::BI__builtin_arm_wsrp) |
| 1160 | return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); |
| 1161 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 1162 | if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) |
| 1163 | return true; |
| 1164 | |
Yi Kong | 19a29ac | 2014-07-17 10:52:06 +0000 | [diff] [blame] | 1165 | // For intrinsics which take an immediate value as part of the instruction, |
| 1166 | // range check them here. |
| 1167 | unsigned i = 0, l = 0, u = 0; |
| 1168 | switch (BuiltinID) { |
| 1169 | default: return false; |
| 1170 | case AArch64::BI__builtin_arm_dmb: |
| 1171 | case AArch64::BI__builtin_arm_dsb: |
| 1172 | case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break; |
| 1173 | } |
| 1174 | |
Yi Kong | 19a29ac | 2014-07-17 10:52:06 +0000 | [diff] [blame] | 1175 | return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 1178 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 1179 | unsigned i = 0, l = 0, u = 0; |
| 1180 | switch (BuiltinID) { |
| 1181 | default: return false; |
| 1182 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; |
| 1183 | 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] | 1184 | case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; |
| 1185 | case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; |
| 1186 | case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; |
| 1187 | case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; |
| 1188 | 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] | 1189 | } |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 1190 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 1191 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Simon Atanasyan | ecedf3d | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Kit Barton | e50adcb | 2015-03-30 19:40:59 +0000 | [diff] [blame] | 1194 | bool Sema::CheckPPCBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
| 1195 | unsigned i = 0, l = 0, u = 0; |
Nemanja Ivanovic | 239eec7 | 2015-04-09 23:58:16 +0000 | [diff] [blame] | 1196 | bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde || |
| 1197 | BuiltinID == PPC::BI__builtin_divdeu || |
| 1198 | BuiltinID == PPC::BI__builtin_bpermd; |
| 1199 | bool IsTarget64Bit = Context.getTargetInfo() |
| 1200 | .getTypeWidth(Context |
| 1201 | .getTargetInfo() |
| 1202 | .getIntPtrType()) == 64; |
| 1203 | bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe || |
| 1204 | BuiltinID == PPC::BI__builtin_divweu || |
| 1205 | BuiltinID == PPC::BI__builtin_divde || |
| 1206 | BuiltinID == PPC::BI__builtin_divdeu; |
| 1207 | |
| 1208 | if (Is64BitBltin && !IsTarget64Bit) |
| 1209 | return Diag(TheCall->getLocStart(), diag::err_64_bit_builtin_32_bit_tgt) |
| 1210 | << TheCall->getSourceRange(); |
| 1211 | |
| 1212 | if ((IsBltinExtDiv && !Context.getTargetInfo().hasFeature("extdiv")) || |
| 1213 | (BuiltinID == PPC::BI__builtin_bpermd && |
| 1214 | !Context.getTargetInfo().hasFeature("bpermd"))) |
| 1215 | return Diag(TheCall->getLocStart(), diag::err_ppc_builtin_only_on_pwr7) |
| 1216 | << TheCall->getSourceRange(); |
| 1217 | |
Kit Barton | e50adcb | 2015-03-30 19:40:59 +0000 | [diff] [blame] | 1218 | switch (BuiltinID) { |
| 1219 | default: return false; |
| 1220 | case PPC::BI__builtin_altivec_crypto_vshasigmaw: |
| 1221 | case PPC::BI__builtin_altivec_crypto_vshasigmad: |
| 1222 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || |
| 1223 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
| 1224 | case PPC::BI__builtin_tbegin: |
| 1225 | case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break; |
| 1226 | case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break; |
| 1227 | case PPC::BI__builtin_tabortwc: |
| 1228 | case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break; |
| 1229 | case PPC::BI__builtin_tabortwci: |
| 1230 | case PPC::BI__builtin_tabortdci: |
| 1231 | return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) || |
| 1232 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); |
| 1233 | } |
| 1234 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
| 1235 | } |
| 1236 | |
Ulrich Weigand | 3a610eb | 2015-04-01 12:54:25 +0000 | [diff] [blame] | 1237 | bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID, |
| 1238 | CallExpr *TheCall) { |
| 1239 | if (BuiltinID == SystemZ::BI__builtin_tabort) { |
| 1240 | Expr *Arg = TheCall->getArg(0); |
| 1241 | llvm::APSInt AbortCode(32); |
| 1242 | if (Arg->isIntegerConstantExpr(AbortCode, Context) && |
| 1243 | AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256) |
| 1244 | return Diag(Arg->getLocStart(), diag::err_systemz_invalid_tabort_code) |
| 1245 | << Arg->getSourceRange(); |
| 1246 | } |
| 1247 | |
Ulrich Weigand | 5722c0f | 2015-05-05 19:36:42 +0000 | [diff] [blame] | 1248 | // For intrinsics which take an immediate value as part of the instruction, |
| 1249 | // range check them here. |
| 1250 | unsigned i = 0, l = 0, u = 0; |
| 1251 | switch (BuiltinID) { |
| 1252 | default: return false; |
| 1253 | case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break; |
| 1254 | case SystemZ::BI__builtin_s390_verimb: |
| 1255 | case SystemZ::BI__builtin_s390_verimh: |
| 1256 | case SystemZ::BI__builtin_s390_verimf: |
| 1257 | case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break; |
| 1258 | case SystemZ::BI__builtin_s390_vfaeb: |
| 1259 | case SystemZ::BI__builtin_s390_vfaeh: |
| 1260 | case SystemZ::BI__builtin_s390_vfaef: |
| 1261 | case SystemZ::BI__builtin_s390_vfaebs: |
| 1262 | case SystemZ::BI__builtin_s390_vfaehs: |
| 1263 | case SystemZ::BI__builtin_s390_vfaefs: |
| 1264 | case SystemZ::BI__builtin_s390_vfaezb: |
| 1265 | case SystemZ::BI__builtin_s390_vfaezh: |
| 1266 | case SystemZ::BI__builtin_s390_vfaezf: |
| 1267 | case SystemZ::BI__builtin_s390_vfaezbs: |
| 1268 | case SystemZ::BI__builtin_s390_vfaezhs: |
| 1269 | case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break; |
| 1270 | case SystemZ::BI__builtin_s390_vfidb: |
| 1271 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) || |
| 1272 | SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); |
| 1273 | case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break; |
| 1274 | case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break; |
| 1275 | case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break; |
| 1276 | case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break; |
| 1277 | case SystemZ::BI__builtin_s390_vstrcb: |
| 1278 | case SystemZ::BI__builtin_s390_vstrch: |
| 1279 | case SystemZ::BI__builtin_s390_vstrcf: |
| 1280 | case SystemZ::BI__builtin_s390_vstrczb: |
| 1281 | case SystemZ::BI__builtin_s390_vstrczh: |
| 1282 | case SystemZ::BI__builtin_s390_vstrczf: |
| 1283 | case SystemZ::BI__builtin_s390_vstrcbs: |
| 1284 | case SystemZ::BI__builtin_s390_vstrchs: |
| 1285 | case SystemZ::BI__builtin_s390_vstrcfs: |
| 1286 | case SystemZ::BI__builtin_s390_vstrczbs: |
| 1287 | case SystemZ::BI__builtin_s390_vstrczhs: |
| 1288 | case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break; |
| 1289 | } |
| 1290 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Ulrich Weigand | 3a610eb | 2015-04-01 12:54:25 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Craig Topper | 5ba2c50 | 2015-11-07 08:08:31 +0000 | [diff] [blame] | 1293 | /// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *). |
| 1294 | /// This checks that the target supports __builtin_cpu_supports and |
| 1295 | /// that the string argument is constant and valid. |
| 1296 | static bool SemaBuiltinCpuSupports(Sema &S, CallExpr *TheCall) { |
| 1297 | Expr *Arg = TheCall->getArg(0); |
| 1298 | |
| 1299 | // Check if the argument is a string literal. |
| 1300 | if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) |
| 1301 | return S.Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal) |
| 1302 | << Arg->getSourceRange(); |
| 1303 | |
| 1304 | // Check the contents of the string. |
| 1305 | StringRef Feature = |
| 1306 | cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); |
| 1307 | if (!S.Context.getTargetInfo().validateCpuSupports(Feature)) |
| 1308 | return S.Diag(TheCall->getLocStart(), diag::err_invalid_cpu_supports) |
| 1309 | << Arg->getSourceRange(); |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1313 | bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1314 | unsigned i = 0, l = 0, u = 0; |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1315 | switch (BuiltinID) { |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1316 | default: return false; |
Eric Christopher | d983270 | 2015-06-29 21:00:05 +0000 | [diff] [blame] | 1317 | case X86::BI__builtin_cpu_supports: |
Craig Topper | 5ba2c50 | 2015-11-07 08:08:31 +0000 | [diff] [blame] | 1318 | return SemaBuiltinCpuSupports(*this, TheCall); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1319 | case X86::BI__builtin_ms_va_start: |
| 1320 | return SemaBuiltinMSVAStart(TheCall); |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1321 | case X86::BI_mm_prefetch: i = 1; l = 0; u = 3; break; |
Craig Topper | 1601525 | 2015-01-31 06:31:23 +0000 | [diff] [blame] | 1322 | 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] | 1323 | case X86::BI__builtin_ia32_vpermil2pd: |
| 1324 | case X86::BI__builtin_ia32_vpermil2pd256: |
| 1325 | case X86::BI__builtin_ia32_vpermil2ps: |
| 1326 | 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] | 1327 | case X86::BI__builtin_ia32_cmpb128_mask: |
| 1328 | case X86::BI__builtin_ia32_cmpw128_mask: |
| 1329 | case X86::BI__builtin_ia32_cmpd128_mask: |
| 1330 | case X86::BI__builtin_ia32_cmpq128_mask: |
| 1331 | case X86::BI__builtin_ia32_cmpb256_mask: |
| 1332 | case X86::BI__builtin_ia32_cmpw256_mask: |
| 1333 | case X86::BI__builtin_ia32_cmpd256_mask: |
| 1334 | case X86::BI__builtin_ia32_cmpq256_mask: |
| 1335 | case X86::BI__builtin_ia32_cmpb512_mask: |
| 1336 | case X86::BI__builtin_ia32_cmpw512_mask: |
| 1337 | case X86::BI__builtin_ia32_cmpd512_mask: |
| 1338 | case X86::BI__builtin_ia32_cmpq512_mask: |
| 1339 | case X86::BI__builtin_ia32_ucmpb128_mask: |
| 1340 | case X86::BI__builtin_ia32_ucmpw128_mask: |
| 1341 | case X86::BI__builtin_ia32_ucmpd128_mask: |
| 1342 | case X86::BI__builtin_ia32_ucmpq128_mask: |
| 1343 | case X86::BI__builtin_ia32_ucmpb256_mask: |
| 1344 | case X86::BI__builtin_ia32_ucmpw256_mask: |
| 1345 | case X86::BI__builtin_ia32_ucmpd256_mask: |
| 1346 | case X86::BI__builtin_ia32_ucmpq256_mask: |
| 1347 | case X86::BI__builtin_ia32_ucmpb512_mask: |
| 1348 | case X86::BI__builtin_ia32_ucmpw512_mask: |
| 1349 | case X86::BI__builtin_ia32_ucmpd512_mask: |
| 1350 | 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] | 1351 | case X86::BI__builtin_ia32_roundps: |
| 1352 | case X86::BI__builtin_ia32_roundpd: |
| 1353 | case X86::BI__builtin_ia32_roundps256: |
| 1354 | case X86::BI__builtin_ia32_roundpd256: i = 1, l = 0; u = 15; break; |
| 1355 | case X86::BI__builtin_ia32_roundss: |
| 1356 | case X86::BI__builtin_ia32_roundsd: i = 2, l = 0; u = 15; break; |
| 1357 | case X86::BI__builtin_ia32_cmpps: |
| 1358 | case X86::BI__builtin_ia32_cmpss: |
| 1359 | case X86::BI__builtin_ia32_cmppd: |
| 1360 | case X86::BI__builtin_ia32_cmpsd: |
| 1361 | case X86::BI__builtin_ia32_cmpps256: |
| 1362 | case X86::BI__builtin_ia32_cmppd256: |
| 1363 | case X86::BI__builtin_ia32_cmpps512_mask: |
| 1364 | 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] | 1365 | case X86::BI__builtin_ia32_vpcomub: |
| 1366 | case X86::BI__builtin_ia32_vpcomuw: |
| 1367 | case X86::BI__builtin_ia32_vpcomud: |
| 1368 | case X86::BI__builtin_ia32_vpcomuq: |
| 1369 | case X86::BI__builtin_ia32_vpcomb: |
| 1370 | case X86::BI__builtin_ia32_vpcomw: |
| 1371 | case X86::BI__builtin_ia32_vpcomd: |
| 1372 | 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] | 1373 | } |
Craig Topper | dd84ec5 | 2014-12-27 07:00:08 +0000 | [diff] [blame] | 1374 | return SemaBuiltinConstantArgRange(TheCall, i, l, u); |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1377 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo |
| 1378 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. |
| 1379 | /// Returns true when the format fits the function and the FormatStringInfo has |
| 1380 | /// been populated. |
| 1381 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, |
| 1382 | FormatStringInfo *FSI) { |
| 1383 | FSI->HasVAListArg = Format->getFirstArg() == 0; |
| 1384 | FSI->FormatIdx = Format->getFormatIdx() - 1; |
| 1385 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1386 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1387 | // The way the format attribute works in GCC, the implicit this argument |
| 1388 | // of member functions is counted. However, it doesn't appear in our own |
| 1389 | // lists, so decrement format_idx in that case. |
| 1390 | if (IsCXXMember) { |
| 1391 | if(FSI->FormatIdx == 0) |
| 1392 | return false; |
| 1393 | --FSI->FormatIdx; |
| 1394 | if (FSI->FirstDataArg != 0) |
| 1395 | --FSI->FirstDataArg; |
| 1396 | } |
| 1397 | return true; |
| 1398 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1400 | /// Checks if a the given expression evaluates to null. |
| 1401 | /// |
| 1402 | /// \brief Returns true if the value evaluates to null. |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 1403 | static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1404 | // If the expression has non-null type, it doesn't evaluate to null. |
| 1405 | if (auto nullability |
| 1406 | = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) { |
| 1407 | if (*nullability == NullabilityKind::NonNull) |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1411 | // As a special case, transparent unions initialized with zero are |
| 1412 | // considered null for the purposes of the nonnull attribute. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1413 | if (const RecordType *UT = Expr->getType()->getAsUnionType()) { |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1414 | if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1415 | if (const CompoundLiteralExpr *CLE = |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1416 | dyn_cast<CompoundLiteralExpr>(Expr)) |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1417 | if (const InitListExpr *ILE = |
| 1418 | dyn_cast<InitListExpr>(CLE->getInitializer())) |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1419 | Expr = ILE->getInit(0); |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | bool Result; |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 1423 | return (!Expr->isValueDependent() && |
| 1424 | Expr->EvaluateAsBooleanCondition(Result, S.Context) && |
| 1425 | !Result); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static void CheckNonNullArgument(Sema &S, |
| 1429 | const Expr *ArgExpr, |
| 1430 | SourceLocation CallSiteLoc) { |
| 1431 | if (CheckNonNullExpr(S, ArgExpr)) |
Eric Fiselier | 18677d5 | 2015-10-09 00:17:57 +0000 | [diff] [blame] | 1432 | S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr, |
| 1433 | S.PDiag(diag::warn_null_arg) << ArgExpr->getSourceRange()); |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1436 | bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) { |
| 1437 | FormatStringInfo FSI; |
| 1438 | if ((GetFormatStringType(Format) == FST_NSString) && |
| 1439 | getFormatStringInfo(Format, false, &FSI)) { |
| 1440 | Idx = FSI.FormatIdx; |
| 1441 | return true; |
| 1442 | } |
| 1443 | return false; |
| 1444 | } |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1445 | /// \brief Diagnose use of %s directive in an NSString which is being passed |
| 1446 | /// as formatting string to formatting method. |
| 1447 | static void |
| 1448 | DiagnoseCStringFormatDirectiveInCFAPI(Sema &S, |
| 1449 | const NamedDecl *FDecl, |
| 1450 | Expr **Args, |
| 1451 | unsigned NumArgs) { |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1452 | unsigned Idx = 0; |
| 1453 | bool Format = false; |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1454 | ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily(); |
| 1455 | if (SFFamily == ObjCStringFormatFamily::SFF_CFString) { |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1456 | Idx = 2; |
| 1457 | Format = true; |
| 1458 | } |
| 1459 | else |
| 1460 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
| 1461 | if (S.GetFormatNSStringIdx(I, Idx)) { |
| 1462 | Format = true; |
| 1463 | break; |
| 1464 | } |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1465 | } |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1466 | if (!Format || NumArgs <= Idx) |
| 1467 | return; |
| 1468 | const Expr *FormatExpr = Args[Idx]; |
| 1469 | if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr)) |
| 1470 | FormatExpr = CSCE->getSubExpr(); |
| 1471 | const StringLiteral *FormatString; |
| 1472 | if (const ObjCStringLiteral *OSL = |
| 1473 | dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) |
| 1474 | FormatString = OSL->getString(); |
| 1475 | else |
| 1476 | FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts()); |
| 1477 | if (!FormatString) |
| 1478 | return; |
| 1479 | if (S.FormatStringHasSArg(FormatString)) { |
| 1480 | S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string) |
| 1481 | << "%s" << 1 << 1; |
| 1482 | S.Diag(FDecl->getLocation(), diag::note_entity_declared_at) |
| 1483 | << FDecl->getDeclName(); |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1487 | /// Determine whether the given type has a non-null nullability annotation. |
| 1488 | static bool isNonNullType(ASTContext &ctx, QualType type) { |
| 1489 | if (auto nullability = type->getNullability(ctx)) |
| 1490 | return *nullability == NullabilityKind::NonNull; |
| 1491 | |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1495 | static void CheckNonNullArguments(Sema &S, |
Ted Kremenek | a146db3 | 2014-01-17 06:24:47 +0000 | [diff] [blame] | 1496 | const NamedDecl *FDecl, |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1497 | const FunctionProtoType *Proto, |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1498 | ArrayRef<const Expr *> Args, |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1499 | SourceLocation CallSiteLoc) { |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1500 | assert((FDecl || Proto) && "Need a function declaration or prototype"); |
| 1501 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1502 | // Check the attributes attached to the method/function itself. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1503 | llvm::SmallBitVector NonNullArgs; |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1504 | if (FDecl) { |
| 1505 | // Handle the nonnull attribute on the function/method declaration itself. |
| 1506 | for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { |
| 1507 | if (!NonNull->args_size()) { |
| 1508 | // Easy case: all pointer arguments are nonnull. |
| 1509 | for (const auto *Arg : Args) |
| 1510 | if (S.isValidPointerAttrType(Arg->getType())) |
| 1511 | CheckNonNullArgument(S, Arg, CallSiteLoc); |
| 1512 | return; |
| 1513 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1515 | for (unsigned Val : NonNull->args()) { |
| 1516 | if (Val >= Args.size()) |
| 1517 | continue; |
| 1518 | if (NonNullArgs.empty()) |
| 1519 | NonNullArgs.resize(Args.size()); |
| 1520 | NonNullArgs.set(Val); |
| 1521 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1522 | } |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1523 | } |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1525 | if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) { |
| 1526 | // Handle the nonnull attribute on the parameters of the |
| 1527 | // function/method. |
| 1528 | ArrayRef<ParmVarDecl*> parms; |
| 1529 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) |
| 1530 | parms = FD->parameters(); |
| 1531 | else |
| 1532 | parms = cast<ObjCMethodDecl>(FDecl)->parameters(); |
| 1533 | |
| 1534 | unsigned ParamIndex = 0; |
| 1535 | for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); |
| 1536 | I != E; ++I, ++ParamIndex) { |
| 1537 | const ParmVarDecl *PVD = *I; |
| 1538 | if (PVD->hasAttr<NonNullAttr>() || |
| 1539 | isNonNullType(S.Context, PVD->getType())) { |
| 1540 | if (NonNullArgs.empty()) |
| 1541 | NonNullArgs.resize(Args.size()); |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1542 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1543 | NonNullArgs.set(ParamIndex); |
| 1544 | } |
| 1545 | } |
| 1546 | } else { |
| 1547 | // If we have a non-function, non-method declaration but no |
| 1548 | // function prototype, try to dig out the function prototype. |
| 1549 | if (!Proto) { |
| 1550 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) { |
| 1551 | QualType type = VD->getType().getNonReferenceType(); |
| 1552 | if (auto pointerType = type->getAs<PointerType>()) |
| 1553 | type = pointerType->getPointeeType(); |
| 1554 | else if (auto blockType = type->getAs<BlockPointerType>()) |
| 1555 | type = blockType->getPointeeType(); |
| 1556 | // FIXME: data member pointers? |
| 1557 | |
| 1558 | // Dig out the function prototype, if there is one. |
| 1559 | Proto = type->getAs<FunctionProtoType>(); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | // Fill in non-null argument information from the nullability |
| 1564 | // information on the parameter types (if we have them). |
| 1565 | if (Proto) { |
| 1566 | unsigned Index = 0; |
| 1567 | for (auto paramType : Proto->getParamTypes()) { |
| 1568 | if (isNonNullType(S.Context, paramType)) { |
| 1569 | if (NonNullArgs.empty()) |
| 1570 | NonNullArgs.resize(Args.size()); |
| 1571 | |
| 1572 | NonNullArgs.set(Index); |
| 1573 | } |
| 1574 | |
| 1575 | ++Index; |
| 1576 | } |
| 1577 | } |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1578 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1580 | // Check for non-null arguments. |
| 1581 | for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size(); |
| 1582 | ArgIndex != ArgIndexEnd; ++ArgIndex) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1583 | if (NonNullArgs[ArgIndex]) |
| 1584 | CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc); |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1585 | } |
Ted Kremenek | 2bc7333 | 2014-01-17 06:24:43 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1588 | /// Handles the checks for format strings, non-POD arguments to vararg |
| 1589 | /// functions, and NULL arguments passed to non-NULL parameters. |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1590 | void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, |
| 1591 | ArrayRef<const Expr *> Args, bool IsMemberFunction, |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1592 | SourceLocation Loc, SourceRange Range, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1593 | VariadicCallType CallType) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1594 | // 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] | 1595 | if (CurContext->isDependentContext()) |
| 1596 | return; |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 1597 | |
Ted Kremenek | b8176da | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 1598 | // Printf and scanf checking. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1599 | llvm::SmallBitVector CheckedVarArgs; |
| 1600 | if (FDecl) { |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1601 | for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { |
Benjamin Kramer | 989ab8b | 2013-08-09 09:39:17 +0000 | [diff] [blame] | 1602 | // Only create vector if there are format attributes. |
| 1603 | CheckedVarArgs.resize(Args.size()); |
| 1604 | |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1605 | CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, |
Benjamin Kramer | f62e81d | 2013-08-08 11:08:26 +0000 | [diff] [blame] | 1606 | CheckedVarArgs); |
Benjamin Kramer | 989ab8b | 2013-08-09 09:39:17 +0000 | [diff] [blame] | 1607 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1608 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Refuse POD arguments that weren't caught by the format string |
| 1611 | // checks above. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1612 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1613 | unsigned NumParams = Proto ? Proto->getNumParams() |
| 1614 | : FDecl && isa<FunctionDecl>(FDecl) |
| 1615 | ? cast<FunctionDecl>(FDecl)->getNumParams() |
| 1616 | : FDecl && isa<ObjCMethodDecl>(FDecl) |
| 1617 | ? cast<ObjCMethodDecl>(FDecl)->param_size() |
| 1618 | : 0; |
| 1619 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1620 | for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { |
Ted Kremenek | 241f1ef | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 1621 | // Args[ArgIdx] can be null in malformed code. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1622 | if (const Expr *Arg = Args[ArgIdx]) { |
| 1623 | if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) |
| 1624 | checkVariadicArgument(Arg, CallType); |
| 1625 | } |
Ted Kremenek | 241f1ef | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 1626 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 1627 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1629 | if (FDecl || Proto) { |
| 1630 | CheckNonNullArguments(*this, FDecl, Proto, Args, Loc); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1631 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1632 | // Type safety checking. |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1633 | if (FDecl) { |
| 1634 | for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) |
| 1635 | CheckArgumentWithTypeTag(I, Args.data()); |
| 1636 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1637 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /// CheckConstructorCall - Check a constructor call for correctness and safety |
| 1641 | /// properties not enforced by the C type system. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 1642 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, |
| 1643 | ArrayRef<const Expr *> Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1644 | const FunctionProtoType *Proto, |
| 1645 | SourceLocation Loc) { |
| 1646 | VariadicCallType CallType = |
| 1647 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1648 | checkCall(FDecl, Proto, Args, /*IsMemberFunction=*/true, Loc, SourceRange(), |
| 1649 | CallType); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 1653 | /// and safety properties not strictly enforced by the C type system. |
| 1654 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, |
| 1655 | const FunctionProtoType *Proto) { |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 1656 | bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && |
| 1657 | isa<CXXMethodDecl>(FDecl); |
| 1658 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || |
| 1659 | IsMemberOperatorCall; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1660 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, |
| 1661 | TheCall->getCallee()); |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 1662 | Expr** Args = TheCall->getArgs(); |
| 1663 | unsigned NumArgs = TheCall->getNumArgs(); |
Eli Friedman | adf4218 | 2012-10-11 00:34:15 +0000 | [diff] [blame] | 1664 | if (IsMemberOperatorCall) { |
Eli Friedman | 726d11c | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 1665 | // If this is a call to a member operator, hide the first argument |
| 1666 | // from checkCall. |
| 1667 | // FIXME: Our choice of AST representation here is less than ideal. |
| 1668 | ++Args; |
| 1669 | --NumArgs; |
| 1670 | } |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1671 | checkCall(FDecl, Proto, llvm::makeArrayRef(Args, NumArgs), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1672 | IsMemberFunction, TheCall->getRParenLoc(), |
| 1673 | TheCall->getCallee()->getSourceRange(), CallType); |
| 1674 | |
| 1675 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 1676 | // None of the checks below are needed for functions that don't have |
| 1677 | // simple names (e.g., C++ conversion functions). |
| 1678 | if (!FnInfo) |
| 1679 | return false; |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1680 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 1681 | CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo); |
Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 1682 | if (getLangOpts().ObjC1) |
| 1683 | DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 1684 | |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 1685 | unsigned CMId = FDecl->getMemoryFunctionKind(); |
| 1686 | if (CMId == 0) |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 1687 | return false; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 1688 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 1689 | // Handle memory setting and copying functions. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 1690 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 1691 | CheckStrlcpycatArguments(TheCall, FnInfo); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 1692 | else if (CMId == Builtin::BIstrncat) |
| 1693 | CheckStrncatArguments(TheCall, FnInfo); |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 1694 | else |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 1695 | CheckMemaccessArguments(TheCall, CMId, FnInfo); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 1696 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1697 | return false; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1700 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, |
Dmitri Gribenko | 1debc46 | 2013-05-05 19:42:09 +0000 | [diff] [blame] | 1701 | ArrayRef<const Expr *> Args) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1702 | VariadicCallType CallType = |
| 1703 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1704 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1705 | checkCall(Method, nullptr, Args, |
| 1706 | /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(), |
| 1707 | CallType); |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1708 | |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1712 | bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, |
| 1713 | const FunctionProtoType *Proto) { |
Aaron Ballman | b673c65 | 2015-04-23 16:14:19 +0000 | [diff] [blame] | 1714 | QualType Ty; |
| 1715 | if (const auto *V = dyn_cast<VarDecl>(NDecl)) |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1716 | Ty = V->getType().getNonReferenceType(); |
Aaron Ballman | b673c65 | 2015-04-23 16:14:19 +0000 | [diff] [blame] | 1717 | else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1718 | Ty = F->getType().getNonReferenceType(); |
Aaron Ballman | b673c65 | 2015-04-23 16:14:19 +0000 | [diff] [blame] | 1719 | else |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1720 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1722 | if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() && |
| 1723 | !Ty->isFunctionProtoType()) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1724 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1726 | VariadicCallType CallType; |
Richard Trieu | 72ae173 | 2013-06-20 23:21:54 +0000 | [diff] [blame] | 1727 | if (!Proto || !Proto->isVariadic()) { |
Richard Trieu | 664c4c6 | 2013-06-20 21:03:13 +0000 | [diff] [blame] | 1728 | CallType = VariadicDoesNotApply; |
| 1729 | } else if (Ty->isBlockPointerType()) { |
| 1730 | CallType = VariadicBlock; |
| 1731 | } else { // Ty->isFunctionPointerType() |
| 1732 | CallType = VariadicFunction; |
| 1733 | } |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1735 | checkCall(NDecl, Proto, |
| 1736 | llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), |
| 1737 | /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1738 | TheCall->getCallee()->getSourceRange(), CallType); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1739 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 1740 | return false; |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1743 | /// Checks function calls when a FunctionDecl or a NamedDecl is not available, |
| 1744 | /// such as function pointers returned from functions. |
| 1745 | bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1746 | VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1747 | TheCall->getCallee()); |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1748 | checkCall(/*FDecl=*/nullptr, Proto, |
Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 1749 | llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 1750 | /*IsMemberFunction=*/false, TheCall->getRParenLoc(), |
Richard Trieu | 41bc099 | 2013-06-22 00:20:41 +0000 | [diff] [blame] | 1751 | TheCall->getCallee()->getSourceRange(), CallType); |
| 1752 | |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 1756 | static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { |
| 1757 | if (Ordering < AtomicExpr::AO_ABI_memory_order_relaxed || |
| 1758 | Ordering > AtomicExpr::AO_ABI_memory_order_seq_cst) |
| 1759 | return false; |
| 1760 | |
| 1761 | switch (Op) { |
| 1762 | case AtomicExpr::AO__c11_atomic_init: |
| 1763 | llvm_unreachable("There is no ordering argument for an init"); |
| 1764 | |
| 1765 | case AtomicExpr::AO__c11_atomic_load: |
| 1766 | case AtomicExpr::AO__atomic_load_n: |
| 1767 | case AtomicExpr::AO__atomic_load: |
| 1768 | return Ordering != AtomicExpr::AO_ABI_memory_order_release && |
| 1769 | Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; |
| 1770 | |
| 1771 | case AtomicExpr::AO__c11_atomic_store: |
| 1772 | case AtomicExpr::AO__atomic_store: |
| 1773 | case AtomicExpr::AO__atomic_store_n: |
| 1774 | return Ordering != AtomicExpr::AO_ABI_memory_order_consume && |
| 1775 | Ordering != AtomicExpr::AO_ABI_memory_order_acquire && |
| 1776 | Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; |
| 1777 | |
| 1778 | default: |
| 1779 | return true; |
| 1780 | } |
| 1781 | } |
| 1782 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1783 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, |
| 1784 | AtomicExpr::AtomicOp Op) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1785 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 1786 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1787 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1788 | // All these operations take one of the following forms: |
| 1789 | enum { |
| 1790 | // C __c11_atomic_init(A *, C) |
| 1791 | Init, |
| 1792 | // C __c11_atomic_load(A *, int) |
| 1793 | Load, |
| 1794 | // void __atomic_load(A *, CP, int) |
| 1795 | Copy, |
| 1796 | // C __c11_atomic_add(A *, M, int) |
| 1797 | Arithmetic, |
| 1798 | // C __atomic_exchange_n(A *, CP, int) |
| 1799 | Xchg, |
| 1800 | // void __atomic_exchange(A *, C *, CP, int) |
| 1801 | GNUXchg, |
| 1802 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) |
| 1803 | C11CmpXchg, |
| 1804 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) |
| 1805 | GNUCmpXchg |
| 1806 | } Form = Init; |
| 1807 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; |
| 1808 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; |
| 1809 | // where: |
| 1810 | // C is an appropriate type, |
| 1811 | // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, |
| 1812 | // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, |
| 1813 | // M is C if C is an integer, and ptrdiff_t if C is a pointer, and |
| 1814 | // the int parameters are for orderings. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1815 | |
Gabor Horvath | 98bd098 | 2015-03-16 09:59:54 +0000 | [diff] [blame] | 1816 | static_assert(AtomicExpr::AO__c11_atomic_init == 0 && |
| 1817 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == |
| 1818 | AtomicExpr::AO__atomic_load, |
| 1819 | "need to update code for modified C11 atomics"); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1820 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && |
| 1821 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; |
| 1822 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || |
| 1823 | Op == AtomicExpr::AO__atomic_store_n || |
| 1824 | Op == AtomicExpr::AO__atomic_exchange_n || |
| 1825 | Op == AtomicExpr::AO__atomic_compare_exchange_n; |
| 1826 | bool IsAddSub = false; |
| 1827 | |
| 1828 | switch (Op) { |
| 1829 | case AtomicExpr::AO__c11_atomic_init: |
| 1830 | Form = Init; |
| 1831 | break; |
| 1832 | |
| 1833 | case AtomicExpr::AO__c11_atomic_load: |
| 1834 | case AtomicExpr::AO__atomic_load_n: |
| 1835 | Form = Load; |
| 1836 | break; |
| 1837 | |
| 1838 | case AtomicExpr::AO__c11_atomic_store: |
| 1839 | case AtomicExpr::AO__atomic_load: |
| 1840 | case AtomicExpr::AO__atomic_store: |
| 1841 | case AtomicExpr::AO__atomic_store_n: |
| 1842 | Form = Copy; |
| 1843 | break; |
| 1844 | |
| 1845 | case AtomicExpr::AO__c11_atomic_fetch_add: |
| 1846 | case AtomicExpr::AO__c11_atomic_fetch_sub: |
| 1847 | case AtomicExpr::AO__atomic_fetch_add: |
| 1848 | case AtomicExpr::AO__atomic_fetch_sub: |
| 1849 | case AtomicExpr::AO__atomic_add_fetch: |
| 1850 | case AtomicExpr::AO__atomic_sub_fetch: |
| 1851 | IsAddSub = true; |
| 1852 | // Fall through. |
| 1853 | case AtomicExpr::AO__c11_atomic_fetch_and: |
| 1854 | case AtomicExpr::AO__c11_atomic_fetch_or: |
| 1855 | case AtomicExpr::AO__c11_atomic_fetch_xor: |
| 1856 | case AtomicExpr::AO__atomic_fetch_and: |
| 1857 | case AtomicExpr::AO__atomic_fetch_or: |
| 1858 | case AtomicExpr::AO__atomic_fetch_xor: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 1859 | case AtomicExpr::AO__atomic_fetch_nand: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1860 | case AtomicExpr::AO__atomic_and_fetch: |
| 1861 | case AtomicExpr::AO__atomic_or_fetch: |
| 1862 | case AtomicExpr::AO__atomic_xor_fetch: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 1863 | case AtomicExpr::AO__atomic_nand_fetch: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1864 | Form = Arithmetic; |
| 1865 | break; |
| 1866 | |
| 1867 | case AtomicExpr::AO__c11_atomic_exchange: |
| 1868 | case AtomicExpr::AO__atomic_exchange_n: |
| 1869 | Form = Xchg; |
| 1870 | break; |
| 1871 | |
| 1872 | case AtomicExpr::AO__atomic_exchange: |
| 1873 | Form = GNUXchg; |
| 1874 | break; |
| 1875 | |
| 1876 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: |
| 1877 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: |
| 1878 | Form = C11CmpXchg; |
| 1879 | break; |
| 1880 | |
| 1881 | case AtomicExpr::AO__atomic_compare_exchange: |
| 1882 | case AtomicExpr::AO__atomic_compare_exchange_n: |
| 1883 | Form = GNUCmpXchg; |
| 1884 | break; |
| 1885 | } |
| 1886 | |
| 1887 | // Check we have the right number of arguments. |
| 1888 | if (TheCall->getNumArgs() < NumArgs[Form]) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1889 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1890 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1891 | << TheCall->getCallee()->getSourceRange(); |
| 1892 | return ExprError(); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1893 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { |
| 1894 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1895 | diag::err_typecheck_call_too_many_args) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1896 | << 0 << NumArgs[Form] << TheCall->getNumArgs() |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1897 | << TheCall->getCallee()->getSourceRange(); |
| 1898 | return ExprError(); |
| 1899 | } |
| 1900 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1901 | // Inspect the first argument of the atomic operation. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 1902 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1903 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 1904 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 1905 | if (!pointerType) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1906 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1907 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1908 | return ExprError(); |
| 1909 | } |
| 1910 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1911 | // For a __c11 builtin, this should be a pointer to an _Atomic type. |
| 1912 | QualType AtomTy = pointerType->getPointeeType(); // 'A' |
| 1913 | QualType ValType = AtomTy; // 'C' |
| 1914 | if (IsC11) { |
| 1915 | if (!AtomTy->isAtomicType()) { |
| 1916 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 1917 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1918 | return ExprError(); |
| 1919 | } |
Richard Smith | e00921a | 2012-09-15 06:09:58 +0000 | [diff] [blame] | 1920 | if (AtomTy.isConstQualified()) { |
| 1921 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic) |
| 1922 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1923 | return ExprError(); |
| 1924 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1925 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
Eric Fiselier | a3a7c56 | 2015-10-04 00:11:02 +0000 | [diff] [blame] | 1926 | } else if (Form != Load && Op != AtomicExpr::AO__atomic_load) { |
| 1927 | if (ValType.isConstQualified()) { |
| 1928 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_pointer) |
| 1929 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1930 | return ExprError(); |
| 1931 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1932 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1933 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1934 | // For an arithmetic operation, the implied arithmetic must be well-formed. |
| 1935 | if (Form == Arithmetic) { |
| 1936 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. |
| 1937 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 1938 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 1939 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1940 | return ExprError(); |
| 1941 | } |
| 1942 | if (!IsAddSub && !ValType->isIntegerType()) { |
| 1943 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) |
| 1944 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1945 | return ExprError(); |
| 1946 | } |
David Majnemer | e85cff8 | 2015-01-28 05:48:06 +0000 | [diff] [blame] | 1947 | if (IsC11 && ValType->isPointerType() && |
| 1948 | RequireCompleteType(Ptr->getLocStart(), ValType->getPointeeType(), |
| 1949 | diag::err_incomplete_type)) { |
| 1950 | return ExprError(); |
| 1951 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1952 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 1953 | // For __atomic_*_n operations, the value type must be a scalar integral or |
| 1954 | // 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] | 1955 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1956 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); |
| 1957 | return ExprError(); |
| 1958 | } |
| 1959 | |
Eli Friedman | aa76981 | 2013-09-11 03:49:34 +0000 | [diff] [blame] | 1960 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && |
| 1961 | !AtomTy->isScalarType()) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1962 | // For GNU atomics, require a trivially-copyable type. This is not part of |
| 1963 | // the GNU atomics specification, but we enforce it for sanity. |
| 1964 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1965 | << Ptr->getType() << Ptr->getSourceRange(); |
| 1966 | return ExprError(); |
| 1967 | } |
| 1968 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1969 | switch (ValType.getObjCLifetime()) { |
| 1970 | case Qualifiers::OCL_None: |
| 1971 | case Qualifiers::OCL_ExplicitNone: |
| 1972 | // okay |
| 1973 | break; |
| 1974 | |
| 1975 | case Qualifiers::OCL_Weak: |
| 1976 | case Qualifiers::OCL_Strong: |
| 1977 | case Qualifiers::OCL_Autoreleasing: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1978 | // FIXME: Can this happen? By this point, ValType should be known |
| 1979 | // to be trivially copyable. |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1980 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 1981 | << ValType << Ptr->getSourceRange(); |
| 1982 | return ExprError(); |
| 1983 | } |
| 1984 | |
David Majnemer | c6eb650 | 2015-06-03 00:26:35 +0000 | [diff] [blame] | 1985 | // atomic_fetch_or takes a pointer to a volatile 'A'. We shouldn't let the |
| 1986 | // volatile-ness of the pointee-type inject itself into the result or the |
| 1987 | // other operands. |
| 1988 | ValType.removeLocalVolatile(); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1989 | QualType ResultType = ValType; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1990 | if (Form == Copy || Form == GNUXchg || Form == Init) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1991 | ResultType = Context.VoidTy; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1992 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1993 | ResultType = Context.BoolTy; |
| 1994 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 1995 | // The type of a parameter passed 'by value'. In the GNU atomics, such |
| 1996 | // arguments are actually passed as pointers. |
| 1997 | QualType ByValType = ValType; // 'CP' |
| 1998 | if (!IsC11 && !IsN) |
| 1999 | ByValType = Ptr->getType(); |
| 2000 | |
Eric Fiselier | a3a7c56 | 2015-10-04 00:11:02 +0000 | [diff] [blame] | 2001 | // FIXME: __atomic_load allows the first argument to be a a pointer to const |
| 2002 | // but not the second argument. We need to manually remove possible const |
| 2003 | // qualifiers. |
| 2004 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2005 | // The first argument --- the pointer --- has a fixed type; we |
| 2006 | // deduce the types of the rest of the arguments accordingly. Walk |
| 2007 | // the remaining arguments, converting them to the deduced value type. |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2008 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2009 | QualType Ty; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2010 | if (i < NumVals[Form] + 1) { |
| 2011 | switch (i) { |
| 2012 | case 1: |
| 2013 | // The second argument is the non-atomic operand. For arithmetic, this |
| 2014 | // is always passed by value, and for a compare_exchange it is always |
| 2015 | // passed by address. For the rest, GNU uses by-address and C11 uses |
| 2016 | // by-value. |
| 2017 | assert(Form != Load); |
| 2018 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) |
| 2019 | Ty = ValType; |
| 2020 | else if (Form == Copy || Form == Xchg) |
| 2021 | Ty = ByValType; |
| 2022 | else if (Form == Arithmetic) |
| 2023 | Ty = Context.getPointerDiffType(); |
Anastasia Stulova | 76fd105 | 2015-12-22 15:14:54 +0000 | [diff] [blame] | 2024 | else { |
| 2025 | Expr *ValArg = TheCall->getArg(i); |
| 2026 | unsigned AS = 0; |
| 2027 | // Keep address space of non-atomic pointer type. |
| 2028 | if (const PointerType *PtrTy = |
| 2029 | ValArg->getType()->getAs<PointerType>()) { |
| 2030 | AS = PtrTy->getPointeeType().getAddressSpace(); |
| 2031 | } |
| 2032 | Ty = Context.getPointerType( |
| 2033 | Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); |
| 2034 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2035 | break; |
| 2036 | case 2: |
| 2037 | // The third argument to compare_exchange / GNU exchange is a |
| 2038 | // (pointer to a) desired value. |
| 2039 | Ty = ByValType; |
| 2040 | break; |
| 2041 | case 3: |
| 2042 | // The fourth argument to GNU compare_exchange is a 'weak' flag. |
| 2043 | Ty = Context.BoolTy; |
| 2044 | break; |
| 2045 | } |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2046 | } else { |
| 2047 | // The order(s) are always converted to int. |
| 2048 | Ty = Context.IntTy; |
| 2049 | } |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2050 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2051 | InitializedEntity Entity = |
| 2052 | InitializedEntity::InitializeParameter(Context, Ty, false); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2053 | ExprResult Arg = TheCall->getArg(i); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2054 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 2055 | if (Arg.isInvalid()) |
| 2056 | return true; |
| 2057 | TheCall->setArg(i, Arg.get()); |
| 2058 | } |
| 2059 | |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2060 | // Permute the arguments into a 'consistent' order. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 2061 | SmallVector<Expr*, 5> SubExprs; |
| 2062 | SubExprs.push_back(Ptr); |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2063 | switch (Form) { |
| 2064 | case Init: |
| 2065 | // Note, AtomicExpr::getVal1() has a special case for this atomic. |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2066 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2067 | break; |
| 2068 | case Load: |
| 2069 | SubExprs.push_back(TheCall->getArg(1)); // Order |
| 2070 | break; |
| 2071 | case Copy: |
| 2072 | case Arithmetic: |
| 2073 | case Xchg: |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 2074 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 2075 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2076 | break; |
| 2077 | case GNUXchg: |
| 2078 | // Note, AtomicExpr::getVal2() has a special case for this atomic. |
| 2079 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 2080 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 2081 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 2082 | break; |
| 2083 | case C11CmpXchg: |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 2084 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 2085 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 2086 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
David Chisnall | 891ec28 | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 2087 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 2088 | break; |
| 2089 | case GNUCmpXchg: |
| 2090 | SubExprs.push_back(TheCall->getArg(4)); // Order |
| 2091 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 2092 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail |
| 2093 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 2094 | SubExprs.push_back(TheCall->getArg(3)); // Weak |
| 2095 | break; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2096 | } |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 2097 | |
| 2098 | if (SubExprs.size() >= 2 && Form != Init) { |
| 2099 | llvm::APSInt Result(32); |
| 2100 | if (SubExprs[1]->isIntegerConstantExpr(Result, Context) && |
| 2101 | !isValidOrderingForOp(Result.getSExtValue(), Op)) |
Tim Northover | c83472e | 2014-03-11 11:35:10 +0000 | [diff] [blame] | 2102 | Diag(SubExprs[1]->getLocStart(), |
| 2103 | diag::warn_atomic_op_has_invalid_memory_order) |
| 2104 | << SubExprs[1]->getSourceRange(); |
Tim Northover | e94a34c | 2014-03-11 10:49:14 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 2107 | AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 2108 | SubExprs, ResultType, Op, |
| 2109 | TheCall->getRParenLoc()); |
| 2110 | |
| 2111 | if ((Op == AtomicExpr::AO__c11_atomic_load || |
| 2112 | (Op == AtomicExpr::AO__c11_atomic_store)) && |
| 2113 | Context.AtomicUsesUnsupportedLibcall(AE)) |
| 2114 | Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) << |
| 2115 | ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1); |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 2116 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2117 | return AE; |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 2121 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 2122 | /// normal type-checking on the given argument, updating the call in |
| 2123 | /// place. This is useful when a builtin function requires custom |
| 2124 | /// type-checking for some of its arguments but not necessarily all of |
| 2125 | /// them. |
| 2126 | /// |
| 2127 | /// Returns true on error. |
| 2128 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 2129 | FunctionDecl *Fn = E->getDirectCallee(); |
| 2130 | assert(Fn && "builtin call without direct callee!"); |
| 2131 | |
| 2132 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 2133 | InitializedEntity Entity = |
| 2134 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 2135 | |
| 2136 | ExprResult Arg = E->getArg(0); |
| 2137 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 2138 | if (Arg.isInvalid()) |
| 2139 | return true; |
| 2140 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2141 | E->setArg(ArgIndex, Arg.get()); |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | } |
| 2144 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2145 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 2146 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 2147 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 2148 | /// promoted the types of arguments because all of these calls are prototyped as |
| 2149 | /// void(...). |
| 2150 | /// |
| 2151 | /// This function goes through and does final semantic checking for these |
| 2152 | /// builtins, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2153 | ExprResult |
| 2154 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2155 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2156 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 2157 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 2158 | |
| 2159 | // 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] | 2160 | if (TheCall->getNumArgs() < 1) { |
| 2161 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 2162 | << 0 << 1 << TheCall->getNumArgs() |
| 2163 | << TheCall->getCallee()->getSourceRange(); |
| 2164 | return ExprError(); |
| 2165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2167 | // Inspect the first argument of the atomic builtin. This should always be |
| 2168 | // a pointer type, whose element is an integral scalar or pointer type. |
| 2169 | // Because it is a pointer type, we don't have to worry about any implicit |
| 2170 | // casts here. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2171 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2172 | Expr *FirstArg = TheCall->getArg(0); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2173 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); |
| 2174 | if (FirstArgResult.isInvalid()) |
| 2175 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2176 | FirstArg = FirstArgResult.get(); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2177 | TheCall->setArg(0, FirstArg); |
| 2178 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2179 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 2180 | if (!pointerType) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2181 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 2182 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 2183 | return ExprError(); |
| 2184 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2186 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | bb3bcd8 | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 2187 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2188 | !ValType->isBlockPointerType()) { |
| 2189 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 2190 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 2191 | return ExprError(); |
| 2192 | } |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2193 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2194 | switch (ValType.getObjCLifetime()) { |
| 2195 | case Qualifiers::OCL_None: |
| 2196 | case Qualifiers::OCL_ExplicitNone: |
| 2197 | // okay |
| 2198 | break; |
| 2199 | |
| 2200 | case Qualifiers::OCL_Weak: |
| 2201 | case Qualifiers::OCL_Strong: |
| 2202 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2203 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2204 | << ValType << FirstArg->getSourceRange(); |
| 2205 | return ExprError(); |
| 2206 | } |
| 2207 | |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 2208 | // Strip any qualifiers off ValType. |
| 2209 | ValType = ValType.getUnqualifiedType(); |
| 2210 | |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2211 | // The majority of builtins return a value, but a few have special return |
| 2212 | // types, so allow them to override appropriately below. |
| 2213 | QualType ResultType = ValType; |
| 2214 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2215 | // We need to figure out which concrete builtin this maps onto. For example, |
| 2216 | // __sync_fetch_and_add with a 2 byte object turns into |
| 2217 | // __sync_fetch_and_add_2. |
| 2218 | #define BUILTIN_ROW(x) \ |
| 2219 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 2220 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2222 | static const unsigned BuiltinIndices[][5] = { |
| 2223 | BUILTIN_ROW(__sync_fetch_and_add), |
| 2224 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 2225 | BUILTIN_ROW(__sync_fetch_and_or), |
| 2226 | BUILTIN_ROW(__sync_fetch_and_and), |
| 2227 | BUILTIN_ROW(__sync_fetch_and_xor), |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2228 | BUILTIN_ROW(__sync_fetch_and_nand), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2230 | BUILTIN_ROW(__sync_add_and_fetch), |
| 2231 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 2232 | BUILTIN_ROW(__sync_and_and_fetch), |
| 2233 | BUILTIN_ROW(__sync_or_and_fetch), |
| 2234 | BUILTIN_ROW(__sync_xor_and_fetch), |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2235 | BUILTIN_ROW(__sync_nand_and_fetch), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2237 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 2238 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 2239 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 2240 | BUILTIN_ROW(__sync_lock_release), |
| 2241 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2242 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | #undef BUILTIN_ROW |
| 2244 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2245 | // Determine the index of the size. |
| 2246 | unsigned SizeIndex; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 2247 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2248 | case 1: SizeIndex = 0; break; |
| 2249 | case 2: SizeIndex = 1; break; |
| 2250 | case 4: SizeIndex = 2; break; |
| 2251 | case 8: SizeIndex = 3; break; |
| 2252 | case 16: SizeIndex = 4; break; |
| 2253 | default: |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2254 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 2255 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 2256 | return ExprError(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2257 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2259 | // Each of these builtins has one pointer argument, followed by some number of |
| 2260 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 2261 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 2262 | // as the number of fixed args. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 2263 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2264 | unsigned BuiltinIndex, NumFixed = 1; |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2265 | bool WarnAboutSemanticsChange = false; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2266 | switch (BuiltinID) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2267 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2268 | case Builtin::BI__sync_fetch_and_add: |
| 2269 | case Builtin::BI__sync_fetch_and_add_1: |
| 2270 | case Builtin::BI__sync_fetch_and_add_2: |
| 2271 | case Builtin::BI__sync_fetch_and_add_4: |
| 2272 | case Builtin::BI__sync_fetch_and_add_8: |
| 2273 | case Builtin::BI__sync_fetch_and_add_16: |
| 2274 | BuiltinIndex = 0; |
| 2275 | break; |
| 2276 | |
| 2277 | case Builtin::BI__sync_fetch_and_sub: |
| 2278 | case Builtin::BI__sync_fetch_and_sub_1: |
| 2279 | case Builtin::BI__sync_fetch_and_sub_2: |
| 2280 | case Builtin::BI__sync_fetch_and_sub_4: |
| 2281 | case Builtin::BI__sync_fetch_and_sub_8: |
| 2282 | case Builtin::BI__sync_fetch_and_sub_16: |
| 2283 | BuiltinIndex = 1; |
| 2284 | break; |
| 2285 | |
| 2286 | case Builtin::BI__sync_fetch_and_or: |
| 2287 | case Builtin::BI__sync_fetch_and_or_1: |
| 2288 | case Builtin::BI__sync_fetch_and_or_2: |
| 2289 | case Builtin::BI__sync_fetch_and_or_4: |
| 2290 | case Builtin::BI__sync_fetch_and_or_8: |
| 2291 | case Builtin::BI__sync_fetch_and_or_16: |
| 2292 | BuiltinIndex = 2; |
| 2293 | break; |
| 2294 | |
| 2295 | case Builtin::BI__sync_fetch_and_and: |
| 2296 | case Builtin::BI__sync_fetch_and_and_1: |
| 2297 | case Builtin::BI__sync_fetch_and_and_2: |
| 2298 | case Builtin::BI__sync_fetch_and_and_4: |
| 2299 | case Builtin::BI__sync_fetch_and_and_8: |
| 2300 | case Builtin::BI__sync_fetch_and_and_16: |
| 2301 | BuiltinIndex = 3; |
| 2302 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2304 | case Builtin::BI__sync_fetch_and_xor: |
| 2305 | case Builtin::BI__sync_fetch_and_xor_1: |
| 2306 | case Builtin::BI__sync_fetch_and_xor_2: |
| 2307 | case Builtin::BI__sync_fetch_and_xor_4: |
| 2308 | case Builtin::BI__sync_fetch_and_xor_8: |
| 2309 | case Builtin::BI__sync_fetch_and_xor_16: |
| 2310 | BuiltinIndex = 4; |
| 2311 | break; |
| 2312 | |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2313 | case Builtin::BI__sync_fetch_and_nand: |
| 2314 | case Builtin::BI__sync_fetch_and_nand_1: |
| 2315 | case Builtin::BI__sync_fetch_and_nand_2: |
| 2316 | case Builtin::BI__sync_fetch_and_nand_4: |
| 2317 | case Builtin::BI__sync_fetch_and_nand_8: |
| 2318 | case Builtin::BI__sync_fetch_and_nand_16: |
| 2319 | BuiltinIndex = 5; |
| 2320 | WarnAboutSemanticsChange = true; |
| 2321 | break; |
| 2322 | |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2323 | case Builtin::BI__sync_add_and_fetch: |
| 2324 | case Builtin::BI__sync_add_and_fetch_1: |
| 2325 | case Builtin::BI__sync_add_and_fetch_2: |
| 2326 | case Builtin::BI__sync_add_and_fetch_4: |
| 2327 | case Builtin::BI__sync_add_and_fetch_8: |
| 2328 | case Builtin::BI__sync_add_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2329 | BuiltinIndex = 6; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | |
| 2332 | case Builtin::BI__sync_sub_and_fetch: |
| 2333 | case Builtin::BI__sync_sub_and_fetch_1: |
| 2334 | case Builtin::BI__sync_sub_and_fetch_2: |
| 2335 | case Builtin::BI__sync_sub_and_fetch_4: |
| 2336 | case Builtin::BI__sync_sub_and_fetch_8: |
| 2337 | case Builtin::BI__sync_sub_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2338 | BuiltinIndex = 7; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2339 | break; |
| 2340 | |
| 2341 | case Builtin::BI__sync_and_and_fetch: |
| 2342 | case Builtin::BI__sync_and_and_fetch_1: |
| 2343 | case Builtin::BI__sync_and_and_fetch_2: |
| 2344 | case Builtin::BI__sync_and_and_fetch_4: |
| 2345 | case Builtin::BI__sync_and_and_fetch_8: |
| 2346 | case Builtin::BI__sync_and_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2347 | BuiltinIndex = 8; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2348 | break; |
| 2349 | |
| 2350 | case Builtin::BI__sync_or_and_fetch: |
| 2351 | case Builtin::BI__sync_or_and_fetch_1: |
| 2352 | case Builtin::BI__sync_or_and_fetch_2: |
| 2353 | case Builtin::BI__sync_or_and_fetch_4: |
| 2354 | case Builtin::BI__sync_or_and_fetch_8: |
| 2355 | case Builtin::BI__sync_or_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2356 | BuiltinIndex = 9; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2357 | break; |
| 2358 | |
| 2359 | case Builtin::BI__sync_xor_and_fetch: |
| 2360 | case Builtin::BI__sync_xor_and_fetch_1: |
| 2361 | case Builtin::BI__sync_xor_and_fetch_2: |
| 2362 | case Builtin::BI__sync_xor_and_fetch_4: |
| 2363 | case Builtin::BI__sync_xor_and_fetch_8: |
| 2364 | case Builtin::BI__sync_xor_and_fetch_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2365 | BuiltinIndex = 10; |
| 2366 | break; |
| 2367 | |
| 2368 | case Builtin::BI__sync_nand_and_fetch: |
| 2369 | case Builtin::BI__sync_nand_and_fetch_1: |
| 2370 | case Builtin::BI__sync_nand_and_fetch_2: |
| 2371 | case Builtin::BI__sync_nand_and_fetch_4: |
| 2372 | case Builtin::BI__sync_nand_and_fetch_8: |
| 2373 | case Builtin::BI__sync_nand_and_fetch_16: |
| 2374 | BuiltinIndex = 11; |
| 2375 | WarnAboutSemanticsChange = true; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2376 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2378 | case Builtin::BI__sync_val_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2379 | case Builtin::BI__sync_val_compare_and_swap_1: |
| 2380 | case Builtin::BI__sync_val_compare_and_swap_2: |
| 2381 | case Builtin::BI__sync_val_compare_and_swap_4: |
| 2382 | case Builtin::BI__sync_val_compare_and_swap_8: |
| 2383 | case Builtin::BI__sync_val_compare_and_swap_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2384 | BuiltinIndex = 12; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2385 | NumFixed = 2; |
| 2386 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2387 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2388 | case Builtin::BI__sync_bool_compare_and_swap: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2389 | case Builtin::BI__sync_bool_compare_and_swap_1: |
| 2390 | case Builtin::BI__sync_bool_compare_and_swap_2: |
| 2391 | case Builtin::BI__sync_bool_compare_and_swap_4: |
| 2392 | case Builtin::BI__sync_bool_compare_and_swap_8: |
| 2393 | case Builtin::BI__sync_bool_compare_and_swap_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2394 | BuiltinIndex = 13; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2395 | NumFixed = 2; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2396 | ResultType = Context.BoolTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2397 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2398 | |
| 2399 | case Builtin::BI__sync_lock_test_and_set: |
| 2400 | case Builtin::BI__sync_lock_test_and_set_1: |
| 2401 | case Builtin::BI__sync_lock_test_and_set_2: |
| 2402 | case Builtin::BI__sync_lock_test_and_set_4: |
| 2403 | case Builtin::BI__sync_lock_test_and_set_8: |
| 2404 | case Builtin::BI__sync_lock_test_and_set_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2405 | BuiltinIndex = 14; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2408 | case Builtin::BI__sync_lock_release: |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2409 | case Builtin::BI__sync_lock_release_1: |
| 2410 | case Builtin::BI__sync_lock_release_2: |
| 2411 | case Builtin::BI__sync_lock_release_4: |
| 2412 | case Builtin::BI__sync_lock_release_8: |
| 2413 | case Builtin::BI__sync_lock_release_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2414 | BuiltinIndex = 15; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2415 | NumFixed = 0; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2416 | ResultType = Context.VoidTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2417 | break; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2418 | |
| 2419 | case Builtin::BI__sync_swap: |
| 2420 | case Builtin::BI__sync_swap_1: |
| 2421 | case Builtin::BI__sync_swap_2: |
| 2422 | case Builtin::BI__sync_swap_4: |
| 2423 | case Builtin::BI__sync_swap_8: |
| 2424 | case Builtin::BI__sync_swap_16: |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2425 | BuiltinIndex = 16; |
Douglas Gregor | 7372248 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 2426 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2427 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2429 | // Now that we know how many fixed arguments we expect, first check that we |
| 2430 | // have at least that many. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2431 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 2432 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 2433 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 2434 | << TheCall->getCallee()->getSourceRange(); |
| 2435 | return ExprError(); |
| 2436 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | |
Hal Finkel | d2208b5 | 2014-10-02 20:53:50 +0000 | [diff] [blame] | 2438 | if (WarnAboutSemanticsChange) { |
| 2439 | Diag(TheCall->getLocEnd(), diag::warn_sync_fetch_and_nand_semantics_change) |
| 2440 | << TheCall->getCallee()->getSourceRange(); |
| 2441 | } |
| 2442 | |
Chris Lattner | 5b9241b | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 2443 | // Get the decl for the concrete builtin from this, we can tell what the |
| 2444 | // concrete integer type we should convert to is. |
| 2445 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 2446 | const char *NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID); |
Abramo Bagnara | 6cba23a | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 2447 | FunctionDecl *NewBuiltinDecl; |
| 2448 | if (NewBuiltinID == BuiltinID) |
| 2449 | NewBuiltinDecl = FDecl; |
| 2450 | else { |
| 2451 | // Perform builtin lookup to avoid redeclaring it. |
| 2452 | DeclarationName DN(&Context.Idents.get(NewBuiltinName)); |
| 2453 | LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName); |
| 2454 | LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); |
| 2455 | assert(Res.getFoundDecl()); |
| 2456 | NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2457 | if (!NewBuiltinDecl) |
Abramo Bagnara | 6cba23a | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 2458 | return ExprError(); |
| 2459 | } |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2460 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2461 | // The first argument --- the pointer --- has a fixed type; we |
| 2462 | // deduce the types of the rest of the arguments accordingly. Walk |
| 2463 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2464 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2465 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2467 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 2468 | // 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] | 2469 | // Initialize the argument. |
| 2470 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 2471 | ValType, /*consume*/ false); |
| 2472 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2473 | if (Arg.isInvalid()) |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2474 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2476 | // Okay, we have something that *can* be converted to the right type. Check |
| 2477 | // to see if there is a potentially weird extension going on here. This can |
| 2478 | // happen when you do an atomic operation on something like an char* and |
| 2479 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 2480 | // for things like 45.123 -> char, etc. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | // FIXME: Do this check. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2482 | TheCall->setArg(i+1, Arg.get()); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2483 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2485 | ASTContext& Context = this->getASTContext(); |
| 2486 | |
| 2487 | // Create a new DeclRefExpr to refer to the new decl. |
| 2488 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 2489 | Context, |
| 2490 | DRE->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2491 | SourceLocation(), |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2492 | NewBuiltinDecl, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2493 | /*enclosing*/ false, |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2494 | DRE->getLocation(), |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2495 | Context.BuiltinFnTy, |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 2496 | DRE->getValueKind()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2498 | // Set the callee in the CallExpr. |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 2499 | // FIXME: This loses syntactic information. |
| 2500 | QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); |
| 2501 | ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, |
| 2502 | CK_BuiltinFnToFnPtr); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2503 | TheCall->setCallee(PromotedCall.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | |
Chandler Carruth | bc8cab1 | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 2505 | // Change the result type of the call to match the original value type. This |
| 2506 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 2507 | // gracefully. |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 2508 | TheCall->setType(ResultType); |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 2509 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2510 | return TheCallResult; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Michael Zolotukhin | 84df123 | 2015-09-08 23:52:33 +0000 | [diff] [blame] | 2513 | /// SemaBuiltinNontemporalOverloaded - We have a call to |
| 2514 | /// __builtin_nontemporal_store or __builtin_nontemporal_load, which is an |
| 2515 | /// overloaded function based on the pointer type of its last argument. |
| 2516 | /// |
| 2517 | /// This function goes through and does final semantic checking for these |
| 2518 | /// builtins. |
| 2519 | ExprResult Sema::SemaBuiltinNontemporalOverloaded(ExprResult TheCallResult) { |
| 2520 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
| 2521 | DeclRefExpr *DRE = |
| 2522 | cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 2523 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 2524 | unsigned BuiltinID = FDecl->getBuiltinID(); |
| 2525 | assert((BuiltinID == Builtin::BI__builtin_nontemporal_store || |
| 2526 | BuiltinID == Builtin::BI__builtin_nontemporal_load) && |
| 2527 | "Unexpected nontemporal load/store builtin!"); |
| 2528 | bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store; |
| 2529 | unsigned numArgs = isStore ? 2 : 1; |
| 2530 | |
| 2531 | // Ensure that we have the proper number of arguments. |
| 2532 | if (checkArgCount(*this, TheCall, numArgs)) |
| 2533 | return ExprError(); |
| 2534 | |
| 2535 | // Inspect the last argument of the nontemporal builtin. This should always |
| 2536 | // be a pointer type, from which we imply the type of the memory access. |
| 2537 | // Because it is a pointer type, we don't have to worry about any implicit |
| 2538 | // casts here. |
| 2539 | Expr *PointerArg = TheCall->getArg(numArgs - 1); |
| 2540 | ExprResult PointerArgResult = |
| 2541 | DefaultFunctionArrayLvalueConversion(PointerArg); |
| 2542 | |
| 2543 | if (PointerArgResult.isInvalid()) |
| 2544 | return ExprError(); |
| 2545 | PointerArg = PointerArgResult.get(); |
| 2546 | TheCall->setArg(numArgs - 1, PointerArg); |
| 2547 | |
| 2548 | const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); |
| 2549 | if (!pointerType) { |
| 2550 | Diag(DRE->getLocStart(), diag::err_nontemporal_builtin_must_be_pointer) |
| 2551 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 2552 | return ExprError(); |
| 2553 | } |
| 2554 | |
| 2555 | QualType ValType = pointerType->getPointeeType(); |
| 2556 | |
| 2557 | // Strip any qualifiers off ValType. |
| 2558 | ValType = ValType.getUnqualifiedType(); |
| 2559 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
| 2560 | !ValType->isBlockPointerType() && !ValType->isFloatingType() && |
| 2561 | !ValType->isVectorType()) { |
| 2562 | Diag(DRE->getLocStart(), |
| 2563 | diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) |
| 2564 | << PointerArg->getType() << PointerArg->getSourceRange(); |
| 2565 | return ExprError(); |
| 2566 | } |
| 2567 | |
| 2568 | if (!isStore) { |
| 2569 | TheCall->setType(ValType); |
| 2570 | return TheCallResult; |
| 2571 | } |
| 2572 | |
| 2573 | ExprResult ValArg = TheCall->getArg(0); |
| 2574 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 2575 | Context, ValType, /*consume*/ false); |
| 2576 | ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); |
| 2577 | if (ValArg.isInvalid()) |
| 2578 | return ExprError(); |
| 2579 | |
| 2580 | TheCall->setArg(0, ValArg.get()); |
| 2581 | TheCall->setType(Context.VoidTy); |
| 2582 | return TheCallResult; |
| 2583 | } |
| 2584 | |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 2585 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2586 | /// CFString constructor is correct |
Steve Naroff | fb46e86 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 2587 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 2588 | /// simplify the backend). |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 2589 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2590 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2591 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 2592 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2593 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2594 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 2595 | << Arg->getSourceRange(); |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 2596 | return true; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2597 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 2599 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2600 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 2601 | unsigned NumBytes = String.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2602 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 2603 | const UTF8 *FromPtr = (const UTF8 *)String.data(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 2604 | UTF16 *ToPtr = &ToBuf[0]; |
| 2605 | |
| 2606 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 2607 | &ToPtr, ToPtr + NumBytes, |
| 2608 | strictConversion); |
| 2609 | // Check for conversion failure. |
| 2610 | if (Result != conversionOK) |
| 2611 | Diag(Arg->getLocStart(), |
| 2612 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 2613 | } |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 2614 | return false; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2617 | /// Check the arguments to '__builtin_va_start' or '__builtin_ms_va_start' |
| 2618 | /// for validity. Emit an error and return true on failure; return false |
| 2619 | /// on success. |
| 2620 | bool Sema::SemaBuiltinVAStartImpl(CallExpr *TheCall) { |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2621 | Expr *Fn = TheCall->getCallee(); |
| 2622 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 2623 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2624 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2625 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 2626 | << Fn->getSourceRange() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2628 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2629 | return true; |
| 2630 | } |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 2631 | |
| 2632 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2633 | return Diag(TheCall->getLocEnd(), |
| 2634 | diag::err_typecheck_call_too_few_args_at_least) |
| 2635 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 2638 | // Type-check the first argument normally. |
| 2639 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 2640 | return true; |
| 2641 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2642 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 2643 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2644 | bool isVariadic; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 2645 | if (CurBlock) |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 2646 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2647 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 2648 | isVariadic = FD->isVariadic(); |
| 2649 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2650 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 2652 | if (!isVariadic) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2653 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 2654 | return true; |
| 2655 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2657 | // Verify that the second argument to the builtin is the last argument of the |
| 2658 | // current function or method. |
| 2659 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | 73cc507 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 2660 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 2662 | // These are valid if SecondArgIsLastNamedArgument is false after the next |
| 2663 | // block. |
| 2664 | QualType Type; |
| 2665 | SourceLocation ParamLoc; |
| 2666 | |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 2667 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 2668 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2669 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 2670 | // Get the last formal in the current function. |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 2671 | const ParmVarDecl *LastArg; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 2672 | if (CurBlock) |
| 2673 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 2674 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2675 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2676 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 2677 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2678 | SecondArgIsLastNamedArgument = PV == LastArg; |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 2679 | |
| 2680 | Type = PV->getType(); |
| 2681 | ParamLoc = PV->getLocation(); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2682 | } |
| 2683 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2685 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2687 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
Nico Weber | 9eea764 | 2013-05-24 23:31:57 +0000 | [diff] [blame] | 2688 | else if (Type->isReferenceType()) { |
| 2689 | Diag(Arg->getLocStart(), |
| 2690 | diag::warn_va_start_of_reference_type_is_undefined); |
| 2691 | Diag(ParamLoc, diag::note_parameter_type) << Type; |
| 2692 | } |
| 2693 | |
Enea Zaffanella | b1b1b8a | 2013-11-07 08:14:26 +0000 | [diff] [blame] | 2694 | TheCall->setType(Context.VoidTy); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2695 | return false; |
Eli Friedman | f835303 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 2696 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2697 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2698 | /// Check the arguments to '__builtin_va_start' for validity, and that |
| 2699 | /// it was called from a function of the native ABI. |
| 2700 | /// Emit an error and return true on failure; return false on success. |
| 2701 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 2702 | // On x86-64 Unix, don't allow this in Win64 ABI functions. |
| 2703 | // On x64 Windows, don't allow this in System V ABI functions. |
| 2704 | // (Yes, that means there's no corresponding way to support variadic |
| 2705 | // System V ABI functions on Windows.) |
| 2706 | if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64) { |
| 2707 | unsigned OS = Context.getTargetInfo().getTriple().getOS(); |
| 2708 | clang::CallingConv CC = CC_C; |
| 2709 | if (const FunctionDecl *FD = getCurFunctionDecl()) |
| 2710 | CC = FD->getType()->getAs<FunctionType>()->getCallConv(); |
| 2711 | if ((OS == llvm::Triple::Win32 && CC == CC_X86_64SysV) || |
| 2712 | (OS != llvm::Triple::Win32 && CC == CC_X86_64Win64)) |
| 2713 | return Diag(TheCall->getCallee()->getLocStart(), |
| 2714 | diag::err_va_start_used_in_wrong_abi_function) |
| 2715 | << (OS != llvm::Triple::Win32); |
| 2716 | } |
| 2717 | return SemaBuiltinVAStartImpl(TheCall); |
| 2718 | } |
| 2719 | |
| 2720 | /// Check the arguments to '__builtin_ms_va_start' for validity, and that |
| 2721 | /// it was called from a Win64 ABI function. |
| 2722 | /// Emit an error and return true on failure; return false on success. |
| 2723 | bool Sema::SemaBuiltinMSVAStart(CallExpr *TheCall) { |
| 2724 | // This only makes sense for x86-64. |
| 2725 | const llvm::Triple &TT = Context.getTargetInfo().getTriple(); |
| 2726 | Expr *Callee = TheCall->getCallee(); |
| 2727 | if (TT.getArch() != llvm::Triple::x86_64) |
| 2728 | return Diag(Callee->getLocStart(), diag::err_x86_builtin_32_bit_tgt); |
| 2729 | // Don't allow this in System V ABI functions. |
| 2730 | clang::CallingConv CC = CC_C; |
| 2731 | if (const FunctionDecl *FD = getCurFunctionDecl()) |
| 2732 | CC = FD->getType()->getAs<FunctionType>()->getCallConv(); |
| 2733 | if (CC == CC_X86_64SysV || |
| 2734 | (TT.getOS() != llvm::Triple::Win32 && CC != CC_X86_64Win64)) |
| 2735 | return Diag(Callee->getLocStart(), |
| 2736 | diag::err_ms_va_start_used_in_sysv_function); |
| 2737 | return SemaBuiltinVAStartImpl(TheCall); |
| 2738 | } |
| 2739 | |
Saleem Abdulrasool | 202aac1 | 2014-07-22 02:01:04 +0000 | [diff] [blame] | 2740 | bool Sema::SemaBuiltinVAStartARM(CallExpr *Call) { |
| 2741 | // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, |
| 2742 | // const char *named_addr); |
| 2743 | |
| 2744 | Expr *Func = Call->getCallee(); |
| 2745 | |
| 2746 | if (Call->getNumArgs() < 3) |
| 2747 | return Diag(Call->getLocEnd(), |
| 2748 | diag::err_typecheck_call_too_few_args_at_least) |
| 2749 | << 0 /*function call*/ << 3 << Call->getNumArgs(); |
| 2750 | |
| 2751 | // Determine whether the current function is variadic or not. |
| 2752 | bool IsVariadic; |
| 2753 | if (BlockScopeInfo *CurBlock = getCurBlock()) |
| 2754 | IsVariadic = CurBlock->TheDecl->isVariadic(); |
| 2755 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 2756 | IsVariadic = FD->isVariadic(); |
| 2757 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2758 | IsVariadic = MD->isVariadic(); |
| 2759 | else |
| 2760 | llvm_unreachable("unexpected statement type"); |
| 2761 | |
| 2762 | if (!IsVariadic) { |
| 2763 | Diag(Func->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 2764 | return true; |
| 2765 | } |
| 2766 | |
| 2767 | // Type-check the first argument normally. |
| 2768 | if (checkBuiltinArgument(*this, Call, 0)) |
| 2769 | return true; |
| 2770 | |
Benjamin Kramer | e0ca6e1 | 2015-03-01 18:09:50 +0000 | [diff] [blame] | 2771 | const struct { |
Saleem Abdulrasool | 202aac1 | 2014-07-22 02:01:04 +0000 | [diff] [blame] | 2772 | unsigned ArgNo; |
| 2773 | QualType Type; |
| 2774 | } ArgumentTypes[] = { |
| 2775 | { 1, Context.getPointerType(Context.CharTy.withConst()) }, |
| 2776 | { 2, Context.getSizeType() }, |
| 2777 | }; |
| 2778 | |
| 2779 | for (const auto &AT : ArgumentTypes) { |
| 2780 | const Expr *Arg = Call->getArg(AT.ArgNo)->IgnoreParens(); |
| 2781 | if (Arg->getType().getCanonicalType() == AT.Type.getCanonicalType()) |
| 2782 | continue; |
| 2783 | Diag(Arg->getLocStart(), diag::err_typecheck_convert_incompatible) |
| 2784 | << Arg->getType() << AT.Type << 1 /* different class */ |
| 2785 | << 0 /* qualifier difference */ << 3 /* parameter mismatch */ |
| 2786 | << AT.ArgNo + 1 << Arg->getType() << AT.Type; |
| 2787 | } |
| 2788 | |
| 2789 | return false; |
| 2790 | } |
| 2791 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2792 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 2793 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2794 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 2795 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 2796 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2797 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2798 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2800 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2801 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2802 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 2803 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2805 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 2806 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2807 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2808 | // Do standard promotions between the two arguments, returning their common |
| 2809 | // type. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2810 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2811 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 2812 | return true; |
Daniel Dunbar | 96f8677 | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 2813 | |
| 2814 | // Make sure any conversions are pushed back into the call; this is |
| 2815 | // type safe since unordered compare builtins are declared as "_Bool |
| 2816 | // foo(...)". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2817 | TheCall->setArg(0, OrigArg0.get()); |
| 2818 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2820 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2821 | return false; |
| 2822 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2823 | // If the common type isn't a real floating type, then the arguments were |
| 2824 | // invalid for this operation. |
Eli Friedman | 93ee5ca | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 2825 | if (Res.isNull() || !Res->isRealFloatingType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2826 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2827 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2828 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 2829 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 2831 | return false; |
| 2832 | } |
| 2833 | |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 2834 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 2835 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2836 | /// to check everything. We expect the last argument to be a floating point |
| 2837 | /// value. |
| 2838 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 2839 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2840 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2841 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2842 | if (TheCall->getNumArgs() > NumArgs) |
| 2843 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2844 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2845 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2846 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2847 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 2848 | |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 2849 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2851 | if (OrigArg->isTypeDependent()) |
| 2852 | return false; |
| 2853 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2854 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2855 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2857 | diag::err_typecheck_call_invalid_unary_fp) |
| 2858 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2860 | // If this is an implicit conversion from float -> double, remove it. |
| 2861 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 2862 | Expr *CastArg = Cast->getSubExpr(); |
| 2863 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 2864 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 2865 | "promotion from float to double is the only expected cast here"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2866 | Cast->setSubExpr(nullptr); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2867 | TheCall->setArg(NumArgs-1, CastArg); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 2871 | return false; |
| 2872 | } |
| 2873 | |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2874 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 2875 | // This is declared to take (...), so we have to check everything. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2876 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2877 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2878 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 2879 | diag::err_typecheck_call_too_few_args_at_least) |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 2880 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 2881 | << TheCall->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2882 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2883 | // Determine which of the following types of shufflevector we're checking: |
| 2884 | // 1) unary, vector mask: (lhs, mask) |
| 2885 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 2886 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 2887 | QualType resType = TheCall->getArg(0)->getType(); |
| 2888 | unsigned numElements = 0; |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2889 | |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2890 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 2891 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2892 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 2893 | QualType RHSType = TheCall->getArg(1)->getType(); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2894 | |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 2895 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) |
| 2896 | return ExprError(Diag(TheCall->getLocStart(), |
| 2897 | diag::err_shufflevector_non_vector) |
| 2898 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 2899 | TheCall->getArg(1)->getLocEnd())); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2900 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2901 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 2902 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2903 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2904 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 2905 | // with mask. If so, verify that RHS is an integer vector type with the |
| 2906 | // same number of elts as lhs. |
| 2907 | if (TheCall->getNumArgs() == 2) { |
Sylvestre Ledru | 8e5d82e | 2013-07-06 08:00:09 +0000 | [diff] [blame] | 2908 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2909 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 2910 | return ExprError(Diag(TheCall->getLocStart(), |
| 2911 | diag::err_shufflevector_incompatible_vector) |
| 2912 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 2913 | TheCall->getArg(1)->getLocEnd())); |
Craig Topper | 61d01cc | 2013-07-19 04:46:31 +0000 | [diff] [blame] | 2914 | } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Craig Topper | baca389 | 2013-07-29 06:47:04 +0000 | [diff] [blame] | 2915 | return ExprError(Diag(TheCall->getLocStart(), |
| 2916 | diag::err_shufflevector_incompatible_vector) |
| 2917 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 2918 | TheCall->getArg(1)->getLocEnd())); |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2919 | } else if (numElements != numResElements) { |
| 2920 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2921 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2922 | VectorType::GenericVector); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2923 | } |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 2927 | if (TheCall->getArg(i)->isTypeDependent() || |
| 2928 | TheCall->getArg(i)->isValueDependent()) |
| 2929 | continue; |
| 2930 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 2931 | llvm::APSInt Result(32); |
| 2932 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 2933 | return ExprError(Diag(TheCall->getLocStart(), |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 2934 | diag::err_shufflevector_nonconstant_argument) |
| 2935 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2936 | |
Craig Topper | 50ad5b7 | 2013-08-03 17:40:38 +0000 | [diff] [blame] | 2937 | // Allow -1 which will be translated to undef in the IR. |
| 2938 | if (Result.isSigned() && Result.isAllOnesValue()) |
| 2939 | continue; |
| 2940 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 2941 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2942 | return ExprError(Diag(TheCall->getLocStart(), |
Craig Topper | 304602a | 2013-07-28 21:50:10 +0000 | [diff] [blame] | 2943 | diag::err_shufflevector_argument_too_large) |
| 2944 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2947 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2948 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 2949 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2950 | exprs.push_back(TheCall->getArg(i)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2951 | TheCall->setArg(i, nullptr); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2954 | return new (Context) ShuffleVectorExpr(Context, exprs, resType, |
| 2955 | TheCall->getCallee()->getLocStart(), |
| 2956 | TheCall->getRParenLoc()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2957 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 2958 | |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 2959 | /// SemaConvertVectorExpr - Handle __builtin_convertvector |
| 2960 | ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, |
| 2961 | SourceLocation BuiltinLoc, |
| 2962 | SourceLocation RParenLoc) { |
| 2963 | ExprValueKind VK = VK_RValue; |
| 2964 | ExprObjectKind OK = OK_Ordinary; |
| 2965 | QualType DstTy = TInfo->getType(); |
| 2966 | QualType SrcTy = E->getType(); |
| 2967 | |
| 2968 | if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) |
| 2969 | return ExprError(Diag(BuiltinLoc, |
| 2970 | diag::err_convertvector_non_vector) |
| 2971 | << E->getSourceRange()); |
| 2972 | if (!DstTy->isVectorType() && !DstTy->isDependentType()) |
| 2973 | return ExprError(Diag(BuiltinLoc, |
| 2974 | diag::err_convertvector_non_vector_type)); |
| 2975 | |
| 2976 | if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { |
| 2977 | unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements(); |
| 2978 | unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements(); |
| 2979 | if (SrcElts != DstElts) |
| 2980 | return ExprError(Diag(BuiltinLoc, |
| 2981 | diag::err_convertvector_incompatible_vector) |
| 2982 | << E->getSourceRange()); |
| 2983 | } |
| 2984 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2985 | return new (Context) |
| 2986 | ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc); |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 2989 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 2990 | // This is declared to take (const void*, ...) and can take two |
| 2991 | // optional constant int args. |
| 2992 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2993 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 2994 | |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2995 | if (NumArgs > 3) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 2996 | return Diag(TheCall->getLocEnd(), |
| 2997 | diag::err_typecheck_call_too_many_args_at_most) |
| 2998 | << 0 /*function call*/ << 3 << NumArgs |
| 2999 | << TheCall->getSourceRange(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 3000 | |
| 3001 | // Argument 0 is checked for us and the remaining arguments must be |
| 3002 | // constant integers. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 3003 | for (unsigned i = 1; i != NumArgs; ++i) |
| 3004 | if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3005 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
Warren Hunt | 20e4a5d | 2014-02-21 23:08:53 +0000 | [diff] [blame] | 3007 | return false; |
| 3008 | } |
| 3009 | |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 3010 | /// SemaBuiltinAssume - Handle __assume (MS Extension). |
| 3011 | // __assume does not evaluate its arguments, and should warn if its argument |
| 3012 | // has side effects. |
| 3013 | bool Sema::SemaBuiltinAssume(CallExpr *TheCall) { |
| 3014 | Expr *Arg = TheCall->getArg(0); |
| 3015 | if (Arg->isInstantiationDependent()) return false; |
| 3016 | |
| 3017 | if (Arg->HasSideEffects(Context)) |
David Majnemer | 5123664 | 2015-02-26 00:57:33 +0000 | [diff] [blame] | 3018 | Diag(Arg->getLocStart(), diag::warn_assume_side_effects) |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 3019 | << Arg->getSourceRange() |
| 3020 | << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); |
| 3021 | |
| 3022 | return false; |
| 3023 | } |
| 3024 | |
| 3025 | /// Handle __builtin_assume_aligned. This is declared |
| 3026 | /// as (const void*, size_t, ...) and can take one optional constant int arg. |
| 3027 | bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) { |
| 3028 | unsigned NumArgs = TheCall->getNumArgs(); |
| 3029 | |
| 3030 | if (NumArgs > 3) |
| 3031 | return Diag(TheCall->getLocEnd(), |
| 3032 | diag::err_typecheck_call_too_many_args_at_most) |
| 3033 | << 0 /*function call*/ << 3 << NumArgs |
| 3034 | << TheCall->getSourceRange(); |
| 3035 | |
| 3036 | // The alignment must be a constant integer. |
| 3037 | Expr *Arg = TheCall->getArg(1); |
| 3038 | |
| 3039 | // We can't check the value of a dependent argument. |
| 3040 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 3041 | llvm::APSInt Result; |
| 3042 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 3043 | return true; |
| 3044 | |
| 3045 | if (!Result.isPowerOf2()) |
| 3046 | return Diag(TheCall->getLocStart(), |
| 3047 | diag::err_alignment_not_power_of_two) |
| 3048 | << Arg->getSourceRange(); |
| 3049 | } |
| 3050 | |
| 3051 | if (NumArgs > 2) { |
| 3052 | ExprResult Arg(TheCall->getArg(2)); |
| 3053 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 3054 | Context.getSizeType(), false); |
| 3055 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 3056 | if (Arg.isInvalid()) return true; |
| 3057 | TheCall->setArg(2, Arg.get()); |
| 3058 | } |
Hal Finkel | f041733 | 2014-07-17 14:25:55 +0000 | [diff] [blame] | 3059 | |
| 3060 | return false; |
| 3061 | } |
| 3062 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3063 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 3064 | /// TheCall is a constant expression. |
| 3065 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 3066 | llvm::APSInt &Result) { |
| 3067 | Expr *Arg = TheCall->getArg(ArgNum); |
| 3068 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 3069 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 3070 | |
| 3071 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 3072 | |
| 3073 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 3074 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 63448c3 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 3075 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3076 | |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3077 | return false; |
| 3078 | } |
| 3079 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 3080 | /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr |
| 3081 | /// TheCall is a constant expression in the range [Low, High]. |
| 3082 | bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, |
| 3083 | int Low, int High) { |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3084 | llvm::APSInt Result; |
Douglas Gregor | 98c3cfc | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 3085 | |
| 3086 | // We can't check the value of a dependent argument. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 3087 | Expr *Arg = TheCall->getArg(ArgNum); |
| 3088 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
Douglas Gregor | 98c3cfc | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 3089 | return false; |
| 3090 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3091 | // Check constant-ness first. |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 3092 | if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3093 | return true; |
| 3094 | |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 3095 | if (Result.getSExtValue() < Low || Result.getSExtValue() > High) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3096 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Richard Sandiford | 28940af | 2014-04-16 08:47:51 +0000 | [diff] [blame] | 3097 | << Low << High << Arg->getSourceRange(); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 3098 | |
| 3099 | return false; |
| 3100 | } |
| 3101 | |
Luke Cheeseman | 59b2d83 | 2015-06-15 17:51:01 +0000 | [diff] [blame] | 3102 | /// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr |
| 3103 | /// TheCall is an ARM/AArch64 special register string literal. |
| 3104 | bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall, |
| 3105 | int ArgNum, unsigned ExpectedFieldNum, |
| 3106 | bool AllowName) { |
| 3107 | bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 || |
| 3108 | BuiltinID == ARM::BI__builtin_arm_wsr64 || |
| 3109 | BuiltinID == ARM::BI__builtin_arm_rsr || |
| 3110 | BuiltinID == ARM::BI__builtin_arm_rsrp || |
| 3111 | BuiltinID == ARM::BI__builtin_arm_wsr || |
| 3112 | BuiltinID == ARM::BI__builtin_arm_wsrp; |
| 3113 | bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 || |
| 3114 | BuiltinID == AArch64::BI__builtin_arm_wsr64 || |
| 3115 | BuiltinID == AArch64::BI__builtin_arm_rsr || |
| 3116 | BuiltinID == AArch64::BI__builtin_arm_rsrp || |
| 3117 | BuiltinID == AArch64::BI__builtin_arm_wsr || |
| 3118 | BuiltinID == AArch64::BI__builtin_arm_wsrp; |
| 3119 | assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin."); |
| 3120 | |
| 3121 | // We can't check the value of a dependent argument. |
| 3122 | Expr *Arg = TheCall->getArg(ArgNum); |
| 3123 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 3124 | return false; |
| 3125 | |
| 3126 | // Check if the argument is a string literal. |
| 3127 | if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) |
| 3128 | return Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal) |
| 3129 | << Arg->getSourceRange(); |
| 3130 | |
| 3131 | // Check the type of special register given. |
| 3132 | StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); |
| 3133 | SmallVector<StringRef, 6> Fields; |
| 3134 | Reg.split(Fields, ":"); |
| 3135 | |
| 3136 | if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1)) |
| 3137 | return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg) |
| 3138 | << Arg->getSourceRange(); |
| 3139 | |
| 3140 | // If the string is the name of a register then we cannot check that it is |
| 3141 | // valid here but if the string is of one the forms described in ACLE then we |
| 3142 | // can check that the supplied fields are integers and within the valid |
| 3143 | // ranges. |
| 3144 | if (Fields.size() > 1) { |
| 3145 | bool FiveFields = Fields.size() == 5; |
| 3146 | |
| 3147 | bool ValidString = true; |
| 3148 | if (IsARMBuiltin) { |
| 3149 | ValidString &= Fields[0].startswith_lower("cp") || |
| 3150 | Fields[0].startswith_lower("p"); |
| 3151 | if (ValidString) |
| 3152 | Fields[0] = |
| 3153 | Fields[0].drop_front(Fields[0].startswith_lower("cp") ? 2 : 1); |
| 3154 | |
| 3155 | ValidString &= Fields[2].startswith_lower("c"); |
| 3156 | if (ValidString) |
| 3157 | Fields[2] = Fields[2].drop_front(1); |
| 3158 | |
| 3159 | if (FiveFields) { |
| 3160 | ValidString &= Fields[3].startswith_lower("c"); |
| 3161 | if (ValidString) |
| 3162 | Fields[3] = Fields[3].drop_front(1); |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | SmallVector<int, 5> Ranges; |
| 3167 | if (FiveFields) |
| 3168 | Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 7, 15, 15}); |
| 3169 | else |
| 3170 | Ranges.append({15, 7, 15}); |
| 3171 | |
| 3172 | for (unsigned i=0; i<Fields.size(); ++i) { |
| 3173 | int IntField; |
| 3174 | ValidString &= !Fields[i].getAsInteger(10, IntField); |
| 3175 | ValidString &= (IntField >= 0 && IntField <= Ranges[i]); |
| 3176 | } |
| 3177 | |
| 3178 | if (!ValidString) |
| 3179 | return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg) |
| 3180 | << Arg->getSourceRange(); |
| 3181 | |
| 3182 | } else if (IsAArch64Builtin && Fields.size() == 1) { |
| 3183 | // If the register name is one of those that appear in the condition below |
| 3184 | // and the special register builtin being used is one of the write builtins, |
| 3185 | // then we require that the argument provided for writing to the register |
| 3186 | // is an integer constant expression. This is because it will be lowered to |
| 3187 | // an MSR (immediate) instruction, so we need to know the immediate at |
| 3188 | // compile time. |
| 3189 | if (TheCall->getNumArgs() != 2) |
| 3190 | return false; |
| 3191 | |
| 3192 | std::string RegLower = Reg.lower(); |
| 3193 | if (RegLower != "spsel" && RegLower != "daifset" && RegLower != "daifclr" && |
| 3194 | RegLower != "pan" && RegLower != "uao") |
| 3195 | return false; |
| 3196 | |
| 3197 | return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); |
| 3198 | } |
| 3199 | |
| 3200 | return false; |
| 3201 | } |
| 3202 | |
Eli Friedman | c97d014 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 3203 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 3204 | /// This checks that the target supports __builtin_longjmp and |
| 3205 | /// that val is a constant 1. |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 3206 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 3207 | if (!Context.getTargetInfo().hasSjLjLowering()) |
| 3208 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_unsupported) |
| 3209 | << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd()); |
| 3210 | |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 3211 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3212 | llvm::APSInt Result; |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 3213 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 3214 | // TODO: This is less than ideal. Overload this to take a value. |
| 3215 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 3216 | return true; |
| 3217 | |
| 3218 | if (Result != 1) |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 3219 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 3220 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 3221 | |
| 3222 | return false; |
| 3223 | } |
| 3224 | |
Joerg Sonnenberger | 2717328 | 2015-03-11 23:46:32 +0000 | [diff] [blame] | 3225 | |
| 3226 | /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]). |
| 3227 | /// This checks that the target supports __builtin_setjmp. |
| 3228 | bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) { |
| 3229 | if (!Context.getTargetInfo().hasSjLjLowering()) |
| 3230 | return Diag(TheCall->getLocStart(), diag::err_builtin_setjmp_unsupported) |
| 3231 | << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd()); |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3235 | namespace { |
| 3236 | enum StringLiteralCheckType { |
| 3237 | SLCT_NotALiteral, |
| 3238 | SLCT_UncheckedLiteral, |
| 3239 | SLCT_CheckedLiteral |
| 3240 | }; |
| 3241 | } |
| 3242 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3243 | // Determine if an expression is a string literal or constant string. |
| 3244 | // If this function returns false on the arguments to a function expecting a |
| 3245 | // format string, we will usually need to emit a warning. |
| 3246 | // True string literals are then checked by CheckFormatString. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3247 | static StringLiteralCheckType |
| 3248 | checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, |
| 3249 | bool HasVAListArg, unsigned format_idx, |
| 3250 | unsigned firstDataArg, Sema::FormatStringType Type, |
| 3251 | Sema::VariadicCallType CallType, bool InFunctionCall, |
| 3252 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 3253 | tryAgain: |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 3254 | if (E->isTypeDependent() || E->isValueDependent()) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3255 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3256 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3257 | E = E->IgnoreParenCasts(); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3258 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3259 | if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) |
David Blaikie | 59fe3f8 | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 3260 | // Technically -Wformat-nonliteral does not warn about this case. |
| 3261 | // The behavior of printf and friends in this case is implementation |
| 3262 | // dependent. Ideally if the format string cannot be null then |
| 3263 | // it should have a 'nonnull' attribute in the function prototype. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3264 | return SLCT_UncheckedLiteral; |
David Blaikie | 59fe3f8 | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 3265 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3266 | switch (E->getStmtClass()) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3267 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3268 | case Stmt::ConditionalOperatorClass: { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3269 | // The expression is a literal if both sub-expressions were, and it was |
| 3270 | // completely checked only if both sub-expressions were checked. |
| 3271 | const AbstractConditionalOperator *C = |
| 3272 | cast<AbstractConditionalOperator>(E); |
| 3273 | StringLiteralCheckType Left = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3274 | checkFormatStringExpr(S, C->getTrueExpr(), Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3275 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3276 | Type, CallType, InFunctionCall, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3277 | if (Left == SLCT_NotALiteral) |
| 3278 | return SLCT_NotALiteral; |
| 3279 | StringLiteralCheckType Right = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3280 | checkFormatStringExpr(S, C->getFalseExpr(), Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3281 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3282 | Type, CallType, InFunctionCall, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3283 | return Left < Right ? Left : Right; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 3287 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 3288 | goto tryAgain; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3291 | case Stmt::OpaqueValueExprClass: |
| 3292 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 3293 | E = src; |
| 3294 | goto tryAgain; |
| 3295 | } |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3296 | return SLCT_NotALiteral; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3297 | |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 3298 | case Stmt::PredefinedExprClass: |
| 3299 | // While __func__, etc., are technically not string literals, they |
| 3300 | // cannot contain format specifiers and thus are not a security |
| 3301 | // liability. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3302 | return SLCT_UncheckedLiteral; |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 3303 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3304 | case Stmt::DeclRefExprClass: { |
| 3305 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3307 | // As an exception, do not flag errors for variables binding to |
| 3308 | // const string literals. |
| 3309 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 3310 | bool isConstant = false; |
| 3311 | QualType T = DR->getType(); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3312 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3313 | if (const ArrayType *AT = S.Context.getAsArrayType(T)) { |
| 3314 | isConstant = AT->getElementType().isConstant(S.Context); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3315 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3316 | isConstant = T.isConstant(S.Context) && |
| 3317 | PT->getPointeeType().isConstant(S.Context); |
Jean-Daniel Dupas | d5f7ef4 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 3318 | } else if (T->isObjCObjectPointerType()) { |
| 3319 | // In ObjC, there is usually no "const ObjectPointer" type, |
| 3320 | // so don't check if the pointee type is constant. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3321 | isConstant = T.isConstant(S.Context); |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3322 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3324 | if (isConstant) { |
Matt Beaumont-Gay | d873508 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 3325 | if (const Expr *Init = VD->getAnyInitializer()) { |
| 3326 | // Look through initializers like const char c[] = { "foo" } |
| 3327 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| 3328 | if (InitList->isStringLiteralInit()) |
| 3329 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); |
| 3330 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3331 | return checkFormatStringExpr(S, Init, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3332 | HasVAListArg, format_idx, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3333 | firstDataArg, Type, CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3334 | /*InFunctionCall*/false, CheckedVarArgs); |
Matt Beaumont-Gay | d873508 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 3335 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3336 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
Anders Carlsson | b012ca9 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 3338 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 3339 | // special check to see if the format string is a function parameter |
| 3340 | // of the function calling the printf function. If the function |
| 3341 | // has an attribute indicating it is a printf-like function, then we |
| 3342 | // should suppress warnings concerning non-literals being used in a call |
| 3343 | // to a vprintf function. For example: |
| 3344 | // |
| 3345 | // void |
| 3346 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 3347 | // va_list ap; |
| 3348 | // va_start(ap, fmt); |
| 3349 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 3350 | // ... |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3351 | // } |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 3352 | if (HasVAListArg) { |
| 3353 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { |
| 3354 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { |
| 3355 | int PVIndex = PV->getFunctionScopeIndex() + 1; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3356 | for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) { |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 3357 | // adjust for implicit parameter |
| 3358 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 3359 | if (MD->isInstance()) |
| 3360 | ++PVIndex; |
| 3361 | // We also check if the formats are compatible. |
| 3362 | // We can't pass a 'scanf' string to a 'printf' function. |
| 3363 | if (PVIndex == PVFormat->getFormatIdx() && |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3364 | Type == S.GetFormatStringType(PVFormat)) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3365 | return SLCT_UncheckedLiteral; |
Jean-Daniel Dupas | 58dab68 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3372 | return SLCT_NotALiteral; |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3373 | } |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3374 | |
Jean-Daniel Dupas | 6255bd1 | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 3375 | case Stmt::CallExprClass: |
| 3376 | case Stmt::CXXMemberCallExprClass: { |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 3377 | const CallExpr *CE = cast<CallExpr>(E); |
Jean-Daniel Dupas | 6255bd1 | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 3378 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { |
| 3379 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { |
| 3380 | unsigned ArgIndex = FA->getFormatIdx(); |
| 3381 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 3382 | if (MD->isInstance()) |
| 3383 | --ArgIndex; |
| 3384 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3386 | return checkFormatStringExpr(S, Arg, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3387 | HasVAListArg, format_idx, firstDataArg, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3388 | Type, CallType, InFunctionCall, |
| 3389 | CheckedVarArgs); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3390 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3391 | unsigned BuiltinID = FD->getBuiltinID(); |
| 3392 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || |
| 3393 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { |
| 3394 | const Expr *Arg = CE->getArg(0); |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3395 | return checkFormatStringExpr(S, Arg, Args, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3396 | HasVAListArg, format_idx, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3397 | firstDataArg, Type, CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3398 | InFunctionCall, CheckedVarArgs); |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3399 | } |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 3400 | } |
| 3401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3403 | return SLCT_NotALiteral; |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 3404 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3405 | case Stmt::ObjCStringLiteralClass: |
| 3406 | case Stmt::StringLiteralClass: { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3407 | const StringLiteral *StrE = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3409 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3410 | StrE = ObjCFExpr->getString(); |
| 3411 | else |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3412 | StrE = cast<StringLiteral>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3414 | if (StrE) { |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3415 | S.CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, firstDataArg, |
| 3416 | Type, InFunctionCall, CallType, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3417 | return SLCT_CheckedLiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3420 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 3423 | default: |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3424 | return SLCT_NotALiteral; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3425 | } |
| 3426 | } |
| 3427 | |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3428 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3429 | return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3430 | .Case("scanf", FST_Scanf) |
| 3431 | .Cases("printf", "printf0", FST_Printf) |
| 3432 | .Cases("NSString", "CFString", FST_NSString) |
| 3433 | .Case("strftime", FST_Strftime) |
| 3434 | .Case("strfmon", FST_Strfmon) |
| 3435 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 3436 | .Case("freebsd_kprintf", FST_FreeBSDKPrintf) |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 3437 | .Case("os_trace", FST_OSTrace) |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3438 | .Default(FST_Unknown); |
| 3439 | } |
| 3440 | |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3441 | /// CheckFormatArguments - Check calls to printf and scanf (and similar |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3442 | /// functions) for correct use of format strings. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3443 | /// Returns true if a format string has been fully checked. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3444 | bool Sema::CheckFormatArguments(const FormatAttr *Format, |
| 3445 | ArrayRef<const Expr *> Args, |
| 3446 | bool IsCXXMember, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3447 | VariadicCallType CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3448 | SourceLocation Loc, SourceRange Range, |
| 3449 | llvm::SmallBitVector &CheckedVarArgs) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3450 | FormatStringInfo FSI; |
| 3451 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3452 | return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3453 | FSI.FirstDataArg, GetFormatStringType(Format), |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3454 | CallType, Loc, Range, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3455 | return false; |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3456 | } |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3457 | |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3458 | bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3459 | bool HasVAListArg, unsigned format_idx, |
| 3460 | unsigned firstDataArg, FormatStringType Type, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3461 | VariadicCallType CallType, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3462 | SourceLocation Loc, SourceRange Range, |
| 3463 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3464 | // CHECK: printf/scanf-like function is called with no format string. |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3465 | if (format_idx >= Args.size()) { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3466 | Diag(Loc, diag::warn_missing_format_string) << Range; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3467 | return false; |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 3468 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3470 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3472 | // CHECK: format string is not a string literal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | // |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 3474 | // Dynamically generated format strings are difficult to |
| 3475 | // automatically vet at compile time. Requiring that format strings |
| 3476 | // are string literals: (1) permits the checking of format strings by |
| 3477 | // the compiler and thereby (2) can practically remove the source of |
| 3478 | // many format string exploits. |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 3479 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3480 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 3481 | // C string (e.g. "%d") |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | // 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] | 3483 | // the same format string checking logic for both ObjC and C strings. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3484 | StringLiteralCheckType CT = |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3485 | checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg, |
| 3486 | format_idx, firstDataArg, Type, CallType, |
| 3487 | /*IsFunctionCall*/true, CheckedVarArgs); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3488 | if (CT != SLCT_NotALiteral) |
| 3489 | // Literal format string found, check done! |
| 3490 | return CT == SLCT_CheckedLiteral; |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 3491 | |
Jean-Daniel Dupas | 6567f48 | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 3492 | // Strftime is particular as it always uses a single 'time' argument, |
| 3493 | // so it is safe to pass a non-literal string. |
| 3494 | if (Type == FST_Strftime) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3495 | return false; |
Jean-Daniel Dupas | 6567f48 | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 3496 | |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 3497 | // Do not emit diag when the string param is a macro expansion and the |
| 3498 | // format is either NSString or CFString. This is a hack to prevent |
| 3499 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros |
| 3500 | // 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] | 3501 | if (Type == FST_NSString && |
| 3502 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3503 | return false; |
Jean-Daniel Dupas | 537aa1a | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 3504 | |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 3505 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 3506 | // warn only with -Wformat-nonliteral. |
Eli Friedman | 0e5d677 | 2013-06-18 18:10:01 +0000 | [diff] [blame] | 3507 | if (Args.size() == firstDataArg) |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3508 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3509 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 3510 | << OrigFormatExpr->getSourceRange(); |
| 3511 | else |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3512 | Diag(Args[format_idx]->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3513 | diag::warn_format_nonliteral) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 3514 | << OrigFormatExpr->getSourceRange(); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3515 | return false; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 3516 | } |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 3517 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3518 | namespace { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3519 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 3520 | protected: |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3521 | Sema &S; |
| 3522 | const StringLiteral *FExpr; |
| 3523 | const Expr *OrigFormatExpr; |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 3524 | const unsigned FirstDataArg; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3525 | const unsigned NumDataArgs; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3526 | const char *Beg; // Start of format string. |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3527 | const bool HasVAListArg; |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3528 | ArrayRef<const Expr *> Args; |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3529 | unsigned FormatIdx; |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3530 | llvm::SmallBitVector CoveredArgs; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3531 | bool usesPositionalArgs; |
| 3532 | bool atFirstArg; |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3533 | bool inFunctionCall; |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3534 | Sema::VariadicCallType CallType; |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3535 | llvm::SmallBitVector &CheckedVarArgs; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3536 | public: |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3537 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 3538 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3539 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3540 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3541 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3542 | Sema::VariadicCallType callType, |
| 3543 | llvm::SmallBitVector &CheckedVarArgs) |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3544 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3545 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), |
| 3546 | Beg(beg), HasVAListArg(hasVAListArg), |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3547 | Args(Args), FormatIdx(formatIdx), |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3548 | usesPositionalArgs(false), atFirstArg(true), |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3549 | inFunctionCall(inFunctionCall), CallType(callType), |
| 3550 | CheckedVarArgs(CheckedVarArgs) { |
| 3551 | CoveredArgs.resize(numDataArgs); |
| 3552 | CoveredArgs.reset(); |
| 3553 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3554 | |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 3555 | void DoneProcessing(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3556 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3557 | void HandleIncompleteSpecifier(const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3558 | unsigned specifierLen) override; |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3559 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3560 | void HandleInvalidLengthModifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3561 | const analyze_format_string::FormatSpecifier &FS, |
| 3562 | const analyze_format_string::ConversionSpecifier &CS, |
| 3563 | const char *startSpecifier, unsigned specifierLen, |
| 3564 | unsigned DiagID); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3565 | |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3566 | void HandleNonStandardLengthModifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3567 | const analyze_format_string::FormatSpecifier &FS, |
| 3568 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3569 | |
| 3570 | void HandleNonStandardConversionSpecifier( |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3571 | const analyze_format_string::ConversionSpecifier &CS, |
| 3572 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3573 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3574 | void HandlePosition(const char *startPos, unsigned posLen) override; |
Hans Wennborg | aa8c61c | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 3575 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3576 | void HandleInvalidPosition(const char *startSpecifier, |
| 3577 | unsigned specifierLen, |
| 3578 | analyze_format_string::PositionContext p) override; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3579 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3580 | void HandleZeroPosition(const char *startPos, unsigned posLen) override; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3581 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3582 | void HandleNullChar(const char *nullCharacter) override; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3583 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3584 | template <typename Range> |
| 3585 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, |
| 3586 | const Expr *ArgumentExpr, |
| 3587 | PartialDiagnostic PDiag, |
| 3588 | SourceLocation StringLoc, |
| 3589 | bool IsStringLocation, Range StringRange, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 3590 | ArrayRef<FixItHint> Fixit = None); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3591 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3592 | protected: |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3593 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 3594 | const char *startSpec, |
| 3595 | unsigned specifierLen, |
| 3596 | const char *csStart, unsigned csLen); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3597 | |
| 3598 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 3599 | const char *startSpec, |
| 3600 | unsigned specifierLen); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3601 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 3602 | SourceRange getFormatStringRange(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3603 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 3604 | unsigned specifierLen); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3605 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3606 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 3607 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3608 | |
| 3609 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 3610 | const analyze_format_string::ConversionSpecifier &CS, |
| 3611 | const char *startSpecifier, unsigned specifierLen, |
| 3612 | unsigned argIndex); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3613 | |
| 3614 | template <typename Range> |
| 3615 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 3616 | bool IsStringLocation, Range StringRange, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 3617 | ArrayRef<FixItHint> Fixit = None); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3618 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3619 | } |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3620 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3621 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3622 | return OrigFormatExpr->getSourceRange(); |
| 3623 | } |
| 3624 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3625 | CharSourceRange CheckFormatHandler:: |
| 3626 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3627 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 3628 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 3629 | |
| 3630 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 3631 | End = End.getLocWithOffset(1); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 3632 | |
| 3633 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3636 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3637 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3640 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 3641 | unsigned specifierLen){ |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3642 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), |
| 3643 | getLocationOfByte(startSpecifier), |
| 3644 | /*IsStringLocation*/true, |
| 3645 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | c22f78d | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3648 | void CheckFormatHandler::HandleInvalidLengthModifier( |
| 3649 | const analyze_format_string::FormatSpecifier &FS, |
| 3650 | const analyze_format_string::ConversionSpecifier &CS, |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3651 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3652 | using namespace analyze_format_string; |
| 3653 | |
| 3654 | const LengthModifier &LM = FS.getLengthModifier(); |
| 3655 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 3656 | |
| 3657 | // See if we know how to fix this length modifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3658 | Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3659 | if (FixedLM) { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3660 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3661 | getLocationOfByte(LM.getStart()), |
| 3662 | /*IsStringLocation*/true, |
| 3663 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3664 | |
| 3665 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 3666 | << FixedLM->toString() |
| 3667 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 3668 | |
| 3669 | } else { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3670 | FixItHint Hint; |
| 3671 | if (DiagID == diag::warn_format_nonsensical_length) |
| 3672 | Hint = FixItHint::CreateRemoval(LMRange); |
| 3673 | |
| 3674 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3675 | getLocationOfByte(LM.getStart()), |
| 3676 | /*IsStringLocation*/true, |
| 3677 | getSpecifierRange(startSpecifier, specifierLen), |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3678 | Hint); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3682 | void CheckFormatHandler::HandleNonStandardLengthModifier( |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3683 | const analyze_format_string::FormatSpecifier &FS, |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3684 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3685 | using namespace analyze_format_string; |
| 3686 | |
| 3687 | const LengthModifier &LM = FS.getLengthModifier(); |
| 3688 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); |
| 3689 | |
| 3690 | // See if we know how to fix this length modifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3691 | Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 3692 | if (FixedLM) { |
| 3693 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3694 | << LM.toString() << 0, |
| 3695 | getLocationOfByte(LM.getStart()), |
| 3696 | /*IsStringLocation*/true, |
| 3697 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3698 | |
| 3699 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) |
| 3700 | << FixedLM->toString() |
| 3701 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); |
| 3702 | |
| 3703 | } else { |
| 3704 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3705 | << LM.toString() << 0, |
| 3706 | getLocationOfByte(LM.getStart()), |
| 3707 | /*IsStringLocation*/true, |
| 3708 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3709 | } |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( |
| 3713 | const analyze_format_string::ConversionSpecifier &CS, |
| 3714 | const char *startSpecifier, unsigned specifierLen) { |
Jordan Rose | 4c266aa | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 3715 | using namespace analyze_format_string; |
| 3716 | |
| 3717 | // See if we know how to fix this conversion specifier. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3718 | Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); |
Jordan Rose | 4c266aa | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 3719 | if (FixedCS) { |
| 3720 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3721 | << CS.toString() << /*conversion specifier*/1, |
| 3722 | getLocationOfByte(CS.getStart()), |
| 3723 | /*IsStringLocation*/true, |
| 3724 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3725 | |
| 3726 | CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); |
| 3727 | S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) |
| 3728 | << FixedCS->toString() |
| 3729 | << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); |
| 3730 | } else { |
| 3731 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) |
| 3732 | << CS.toString() << /*conversion specifier*/1, |
| 3733 | getLocationOfByte(CS.getStart()), |
| 3734 | /*IsStringLocation*/true, |
| 3735 | getSpecifierRange(startSpecifier, specifierLen)); |
| 3736 | } |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Hans Wennborg | aa8c61c | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 3739 | void CheckFormatHandler::HandlePosition(const char *startPos, |
| 3740 | unsigned posLen) { |
| 3741 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), |
| 3742 | getLocationOfByte(startPos), |
| 3743 | /*IsStringLocation*/true, |
| 3744 | getSpecifierRange(startPos, posLen)); |
| 3745 | } |
| 3746 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3747 | void |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3748 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 3749 | analyze_format_string::PositionContext p) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3750 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) |
| 3751 | << (unsigned) p, |
| 3752 | getLocationOfByte(startPos), /*IsStringLocation*/true, |
| 3753 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3756 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3757 | unsigned posLen) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3758 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), |
| 3759 | getLocationOfByte(startPos), |
| 3760 | /*IsStringLocation*/true, |
| 3761 | getSpecifierRange(startPos, posLen)); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3764 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3765 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 3766 | // The presence of a null character is likely an error. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3767 | EmitFormatDiagnostic( |
| 3768 | S.PDiag(diag::warn_printf_format_string_contains_null_char), |
| 3769 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, |
| 3770 | getFormatStringRange()); |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 3771 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3772 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3773 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3774 | // Note that this may return NULL if there was an error parsing or building |
| 3775 | // one of the argument expressions. |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3776 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3777 | return Args[FirstDataArg + i]; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | void CheckFormatHandler::DoneProcessing() { |
| 3781 | // Does the number of data arguments exceed the number of |
| 3782 | // format conversions in the format string? |
| 3783 | if (!HasVAListArg) { |
| 3784 | // Find any arguments that weren't covered. |
| 3785 | CoveredArgs.flip(); |
| 3786 | signed notCoveredArg = CoveredArgs.find_first(); |
| 3787 | if (notCoveredArg >= 0) { |
| 3788 | assert((unsigned)notCoveredArg < NumDataArgs); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 3789 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { |
| 3790 | SourceLocation Loc = E->getLocStart(); |
| 3791 | if (!S.getSourceManager().isInSystemMacro(Loc)) { |
| 3792 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), |
| 3793 | Loc, /*IsStringLocation*/false, |
| 3794 | getFormatStringRange()); |
| 3795 | } |
Bob Wilson | 23cd434 | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 3796 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3801 | bool |
| 3802 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 3803 | SourceLocation Loc, |
| 3804 | const char *startSpec, |
| 3805 | unsigned specifierLen, |
| 3806 | const char *csStart, |
| 3807 | unsigned csLen) { |
| 3808 | |
| 3809 | bool keepGoing = true; |
| 3810 | if (argIndex < NumDataArgs) { |
| 3811 | // Consider the argument coverered, even though the specifier doesn't |
| 3812 | // make sense. |
| 3813 | CoveredArgs.set(argIndex); |
| 3814 | } |
| 3815 | else { |
| 3816 | // If argIndex exceeds the number of data arguments we |
| 3817 | // don't issue a warning because that is just a cascade of warnings (and |
| 3818 | // they may have intended '%%' anyway). We don't want to continue processing |
| 3819 | // the format string after this point, however, as we will like just get |
| 3820 | // gibberish when trying to match arguments. |
| 3821 | keepGoing = false; |
| 3822 | } |
| 3823 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3824 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) |
| 3825 | << StringRef(csStart, csLen), |
| 3826 | Loc, /*IsStringLocation*/true, |
| 3827 | getSpecifierRange(startSpec, specifierLen)); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3828 | |
| 3829 | return keepGoing; |
| 3830 | } |
| 3831 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3832 | void |
| 3833 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, |
| 3834 | const char *startSpec, |
| 3835 | unsigned specifierLen) { |
| 3836 | EmitFormatDiagnostic( |
| 3837 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), |
| 3838 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); |
| 3839 | } |
| 3840 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3841 | bool |
| 3842 | CheckFormatHandler::CheckNumArgs( |
| 3843 | const analyze_format_string::FormatSpecifier &FS, |
| 3844 | const analyze_format_string::ConversionSpecifier &CS, |
| 3845 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 3846 | |
| 3847 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3848 | PartialDiagnostic PDiag = FS.usesPositionalArg() |
| 3849 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) |
| 3850 | << (argIndex+1) << NumDataArgs) |
| 3851 | : S.PDiag(diag::warn_printf_insufficient_data_args); |
| 3852 | EmitFormatDiagnostic( |
| 3853 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, |
| 3854 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 3855 | return false; |
| 3856 | } |
| 3857 | return true; |
| 3858 | } |
| 3859 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3860 | template<typename Range> |
| 3861 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, |
| 3862 | SourceLocation Loc, |
| 3863 | bool IsStringLocation, |
| 3864 | Range StringRange, |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3865 | ArrayRef<FixItHint> FixIt) { |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3866 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3867 | Loc, IsStringLocation, StringRange, FixIt); |
| 3868 | } |
| 3869 | |
| 3870 | /// \brief If the format string is not within the funcion call, emit a note |
| 3871 | /// so that the function call and string are in diagnostic messages. |
| 3872 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 3873 | /// \param InFunctionCall if true, the format string is within the function |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3874 | /// call and only one diagnostic message will be produced. Otherwise, an |
| 3875 | /// extra note will be emitted pointing to location of the format string. |
| 3876 | /// |
| 3877 | /// \param ArgumentExpr the expression that is passed as the format string |
| 3878 | /// argument in the function call. Used for getting locations when two |
| 3879 | /// diagnostics are emitted. |
| 3880 | /// |
| 3881 | /// \param PDiag the callee should already have provided any strings for the |
| 3882 | /// diagnostic message. This function only adds locations and fixits |
| 3883 | /// to diagnostics. |
| 3884 | /// |
| 3885 | /// \param Loc primary location for diagnostic. If two diagnostics are |
| 3886 | /// required, one will be at Loc and a new SourceLocation will be created for |
| 3887 | /// the other one. |
| 3888 | /// |
| 3889 | /// \param IsStringLocation if true, Loc points to the format string should be |
| 3890 | /// used for the note. Otherwise, Loc points to the argument list and will |
| 3891 | /// be used with PDiag. |
| 3892 | /// |
| 3893 | /// \param StringRange some or all of the string to highlight. This is |
| 3894 | /// templated so it can accept either a CharSourceRange or a SourceRange. |
| 3895 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 3896 | /// \param FixIt optional fix it hint for the format string. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3897 | template<typename Range> |
| 3898 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, |
| 3899 | const Expr *ArgumentExpr, |
| 3900 | PartialDiagnostic PDiag, |
| 3901 | SourceLocation Loc, |
| 3902 | bool IsStringLocation, |
| 3903 | Range StringRange, |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3904 | ArrayRef<FixItHint> FixIt) { |
| 3905 | if (InFunctionCall) { |
| 3906 | const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); |
| 3907 | D << StringRange; |
Alexander Kornienko | a9b01eb | 2015-02-25 14:40:56 +0000 | [diff] [blame] | 3908 | D << FixIt; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3909 | } else { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3910 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) |
| 3911 | << ArgumentExpr->getSourceRange(); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 3912 | |
| 3913 | const Sema::SemaDiagnosticBuilder &Note = |
| 3914 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), |
| 3915 | diag::note_format_string_defined); |
| 3916 | |
| 3917 | Note << StringRange; |
Alexander Kornienko | a9b01eb | 2015-02-25 14:40:56 +0000 | [diff] [blame] | 3918 | Note << FixIt; |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3922 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 3923 | |
| 3924 | namespace { |
| 3925 | class CheckPrintfHandler : public CheckFormatHandler { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3926 | bool ObjCContext; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3927 | public: |
| 3928 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 3929 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3930 | unsigned numDataArgs, bool isObjC, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3931 | const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 3932 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3933 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 3934 | Sema::VariadicCallType CallType, |
| 3935 | llvm::SmallBitVector &CheckedVarArgs) |
| 3936 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 3937 | numDataArgs, beg, hasVAListArg, Args, |
| 3938 | formatIdx, inFunctionCall, CallType, CheckedVarArgs), |
| 3939 | ObjCContext(isObjC) |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3940 | {} |
| 3941 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3942 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3943 | bool HandleInvalidPrintfConversionSpecifier( |
| 3944 | const analyze_printf::PrintfSpecifier &FS, |
| 3945 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3946 | unsigned specifierLen) override; |
| 3947 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3948 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 3949 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 3950 | unsigned specifierLen) override; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3951 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 3952 | const char *StartSpecifier, |
| 3953 | unsigned SpecifierLen, |
| 3954 | const Expr *E); |
| 3955 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3956 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 3957 | const char *startSpecifier, unsigned specifierLen); |
| 3958 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 3959 | const analyze_printf::OptionalAmount &Amt, |
| 3960 | unsigned type, |
| 3961 | const char *startSpecifier, unsigned specifierLen); |
| 3962 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 3963 | const analyze_printf::OptionalFlag &flag, |
| 3964 | const char *startSpecifier, unsigned specifierLen); |
| 3965 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 3966 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 3967 | const analyze_printf::OptionalFlag &flag, |
| 3968 | const char *startSpecifier, unsigned specifierLen); |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 3969 | bool checkForCStrMembers(const analyze_printf::ArgType &AT, |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 3970 | const Expr *E); |
Ted Kremenek | 2b41771 | 2015-07-02 05:39:16 +0000 | [diff] [blame] | 3971 | |
| 3972 | void HandleEmptyObjCModifierFlag(const char *startFlag, |
| 3973 | unsigned flagLen) override; |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3974 | |
Ted Kremenek | 2b41771 | 2015-07-02 05:39:16 +0000 | [diff] [blame] | 3975 | void HandleInvalidObjCModifierFlag(const char *startFlag, |
| 3976 | unsigned flagLen) override; |
| 3977 | |
| 3978 | void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, |
| 3979 | const char *flagsEnd, |
| 3980 | const char *conversionPosition) |
| 3981 | override; |
| 3982 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3983 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3984 | |
| 3985 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 3986 | const analyze_printf::PrintfSpecifier &FS, |
| 3987 | const char *startSpecifier, |
| 3988 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 3989 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3990 | FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3991 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 3992 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 3993 | getLocationOfByte(CS.getStart()), |
| 3994 | startSpecifier, specifierLen, |
| 3995 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 94af575 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3998 | bool CheckPrintfHandler::HandleAmount( |
| 3999 | const analyze_format_string::OptionalAmount &Amt, |
| 4000 | unsigned k, const char *startSpecifier, |
| 4001 | unsigned specifierLen) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4002 | |
| 4003 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4004 | if (!HasVAListArg) { |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 4005 | unsigned argIndex = Amt.getArgIndex(); |
| 4006 | if (argIndex >= NumDataArgs) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4007 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) |
| 4008 | << k, |
| 4009 | getLocationOfByte(Amt.getStart()), |
| 4010 | /*IsStringLocation*/true, |
| 4011 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4012 | // Don't do any more checking. We will just emit |
| 4013 | // spurious errors. |
| 4014 | return false; |
| 4015 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4016 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4017 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 605b011 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 4018 | // Although not in conformance with C99, we also allow the argument to be |
| 4019 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 4020 | // doesn't emit a warning for that case. |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 4021 | CoveredArgs.set(argIndex); |
| 4022 | const Expr *Arg = getDataArg(argIndex); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 4023 | if (!Arg) |
| 4024 | return false; |
| 4025 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4026 | QualType T = Arg->getType(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4027 | |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 4028 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); |
| 4029 | assert(AT.isValid()); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4030 | |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 4031 | if (!AT.matchesType(S.Context, T)) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4032 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 4033 | << k << AT.getRepresentativeTypeName(S.Context) |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4034 | << T << Arg->getSourceRange(), |
| 4035 | getLocationOfByte(Amt.getStart()), |
| 4036 | /*IsStringLocation*/true, |
| 4037 | getSpecifierRange(startSpecifier, specifierLen)); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4038 | // Don't do any more checking. We will just emit |
| 4039 | // spurious errors. |
| 4040 | return false; |
| 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | return true; |
| 4045 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4046 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4047 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4048 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4049 | const analyze_printf::OptionalAmount &Amt, |
| 4050 | unsigned type, |
| 4051 | const char *startSpecifier, |
| 4052 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4053 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 4054 | FS.getConversionSpecifier(); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4055 | |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4056 | FixItHint fixit = |
| 4057 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant |
| 4058 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
| 4059 | Amt.getConstantLength())) |
| 4060 | : FixItHint(); |
| 4061 | |
| 4062 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) |
| 4063 | << type << CS.toString(), |
| 4064 | getLocationOfByte(Amt.getStart()), |
| 4065 | /*IsStringLocation*/true, |
| 4066 | getSpecifierRange(startSpecifier, specifierLen), |
| 4067 | fixit); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4070 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4071 | const analyze_printf::OptionalFlag &flag, |
| 4072 | const char *startSpecifier, |
| 4073 | unsigned specifierLen) { |
| 4074 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4075 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 4076 | FS.getConversionSpecifier(); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4077 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) |
| 4078 | << flag.toString() << CS.toString(), |
| 4079 | getLocationOfByte(flag.getPosition()), |
| 4080 | /*IsStringLocation*/true, |
| 4081 | getSpecifierRange(startSpecifier, specifierLen), |
| 4082 | FixItHint::CreateRemoval( |
| 4083 | getSpecifierRange(flag.getPosition(), 1))); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4087 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4088 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 4089 | const analyze_printf::OptionalFlag &flag, |
| 4090 | const char *startSpecifier, |
| 4091 | unsigned specifierLen) { |
| 4092 | // Warn about ignored flag with a fixit removal. |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4093 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) |
| 4094 | << ignoredFlag.toString() << flag.toString(), |
| 4095 | getLocationOfByte(ignoredFlag.getPosition()), |
| 4096 | /*IsStringLocation*/true, |
| 4097 | getSpecifierRange(startSpecifier, specifierLen), |
| 4098 | FixItHint::CreateRemoval( |
| 4099 | getSpecifierRange(ignoredFlag.getPosition(), 1))); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Ted Kremenek | 2b41771 | 2015-07-02 05:39:16 +0000 | [diff] [blame] | 4102 | // void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, |
| 4103 | // bool IsStringLocation, Range StringRange, |
| 4104 | // ArrayRef<FixItHint> Fixit = None); |
| 4105 | |
| 4106 | void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag, |
| 4107 | unsigned flagLen) { |
| 4108 | // Warn about an empty flag. |
| 4109 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag), |
| 4110 | getLocationOfByte(startFlag), |
| 4111 | /*IsStringLocation*/true, |
| 4112 | getSpecifierRange(startFlag, flagLen)); |
| 4113 | } |
| 4114 | |
| 4115 | void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag, |
| 4116 | unsigned flagLen) { |
| 4117 | // Warn about an invalid flag. |
| 4118 | auto Range = getSpecifierRange(startFlag, flagLen); |
| 4119 | StringRef flag(startFlag, flagLen); |
| 4120 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag, |
| 4121 | getLocationOfByte(startFlag), |
| 4122 | /*IsStringLocation*/true, |
| 4123 | Range, FixItHint::CreateRemoval(Range)); |
| 4124 | } |
| 4125 | |
| 4126 | void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion( |
| 4127 | const char *flagsStart, const char *flagsEnd, const char *conversionPosition) { |
| 4128 | // Warn about using '[...]' without a '@' conversion. |
| 4129 | auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1); |
| 4130 | auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion; |
| 4131 | EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1), |
| 4132 | getLocationOfByte(conversionPosition), |
| 4133 | /*IsStringLocation*/true, |
| 4134 | Range, FixItHint::CreateRemoval(Range)); |
| 4135 | } |
| 4136 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4137 | // Determines if the specified is a C++ class or struct containing |
| 4138 | // a member with the specified name and kind (e.g. a CXXMethodDecl named |
| 4139 | // "c_str()"). |
| 4140 | template<typename MemberKind> |
| 4141 | static llvm::SmallPtrSet<MemberKind*, 1> |
| 4142 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { |
| 4143 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4144 | llvm::SmallPtrSet<MemberKind*, 1> Results; |
| 4145 | |
| 4146 | if (!RT) |
| 4147 | return Results; |
| 4148 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4149 | if (!RD || !RD->getDefinition()) |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4150 | return Results; |
| 4151 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4152 | LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4153 | Sema::LookupMemberName); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4154 | R.suppressDiagnostics(); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4155 | |
| 4156 | // We just need to include all members of the right kind turned up by the |
| 4157 | // filter, at this point. |
| 4158 | if (S.LookupQualifiedName(R, RT->getDecl())) |
| 4159 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 4160 | NamedDecl *decl = (*I)->getUnderlyingDecl(); |
| 4161 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) |
| 4162 | Results.insert(FK); |
| 4163 | } |
| 4164 | return Results; |
| 4165 | } |
| 4166 | |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4167 | /// Check if we could call '.c_str()' on an object. |
| 4168 | /// |
| 4169 | /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't |
| 4170 | /// allow the call, or if it would be ambiguous). |
| 4171 | bool Sema::hasCStrMethod(const Expr *E) { |
| 4172 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 4173 | MethodSet Results = |
| 4174 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); |
| 4175 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 4176 | MI != ME; ++MI) |
| 4177 | if ((*MI)->getMinRequiredArguments() == 0) |
| 4178 | return true; |
| 4179 | return false; |
| 4180 | } |
| 4181 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4182 | // 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] | 4183 | // better diagnostic if so. AT is assumed to be valid. |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4184 | // Returns true when a c_str() conversion method is found. |
| 4185 | bool CheckPrintfHandler::checkForCStrMembers( |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4186 | const analyze_printf::ArgType &AT, const Expr *E) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4187 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; |
| 4188 | |
| 4189 | MethodSet Results = |
| 4190 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); |
| 4191 | |
| 4192 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); |
| 4193 | MI != ME; ++MI) { |
| 4194 | const CXXMethodDecl *Method = *MI; |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4195 | if (Method->getMinRequiredArguments() == 0 && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4196 | AT.matchesType(S.Context, Method->getReturnType())) { |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4197 | // FIXME: Suggest parens if the expression needs them. |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4198 | SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd()); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4199 | S.Diag(E->getLocStart(), diag::note_printf_c_str) |
| 4200 | << "c_str()" |
| 4201 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); |
| 4202 | return true; |
| 4203 | } |
| 4204 | } |
| 4205 | |
| 4206 | return false; |
| 4207 | } |
| 4208 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4209 | bool |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4210 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | d31b263 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 4211 | &FS, |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4212 | const char *startSpecifier, |
| 4213 | unsigned specifierLen) { |
| 4214 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4215 | using namespace analyze_format_string; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 4216 | using namespace analyze_printf; |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4217 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4218 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 4219 | if (FS.consumesDataArgument()) { |
| 4220 | if (atFirstArg) { |
| 4221 | atFirstArg = false; |
| 4222 | usesPositionalArgs = FS.usesPositionalArg(); |
| 4223 | } |
| 4224 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4225 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 4226 | startSpecifier, specifierLen); |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 4227 | return false; |
| 4228 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4229 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4230 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 4231 | // First check if the field width, precision, and conversion specifier |
| 4232 | // have matching data arguments. |
| 4233 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 4234 | startSpecifier, specifierLen)) { |
| 4235 | return false; |
| 4236 | } |
| 4237 | |
| 4238 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 4239 | startSpecifier, specifierLen)) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 4240 | return false; |
| 4241 | } |
| 4242 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 4243 | if (!CS.consumesDataArgument()) { |
| 4244 | // FIXME: Technically specifying a precision or field width here |
| 4245 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 4246 | return true; |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 4247 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4248 | |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 4249 | // Consume the argument. |
| 4250 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | 09597b4 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 4251 | if (argIndex < NumDataArgs) { |
| 4252 | // The check to see if the argIndex is valid will come later. |
| 4253 | // We set the bit here because we may exit early from this |
| 4254 | // function if we encounter some other error. |
| 4255 | CoveredArgs.set(argIndex); |
| 4256 | } |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 4257 | |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 4258 | // FreeBSD kernel extensions. |
| 4259 | if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || |
| 4260 | CS.getKind() == ConversionSpecifier::FreeBSDDArg) { |
| 4261 | // We need at least two arguments. |
| 4262 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) |
| 4263 | return false; |
| 4264 | |
| 4265 | // Claim the second argument. |
| 4266 | CoveredArgs.set(argIndex + 1); |
| 4267 | |
| 4268 | // Type check the first argument (int for %b, pointer for %D) |
| 4269 | const Expr *Ex = getDataArg(argIndex); |
| 4270 | const analyze_printf::ArgType &AT = |
| 4271 | (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? |
| 4272 | ArgType(S.Context.IntTy) : ArgType::CPointerTy; |
| 4273 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) |
| 4274 | EmitFormatDiagnostic( |
| 4275 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 4276 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 4277 | << false << Ex->getSourceRange(), |
| 4278 | Ex->getLocStart(), /*IsStringLocation*/false, |
| 4279 | getSpecifierRange(startSpecifier, specifierLen)); |
| 4280 | |
| 4281 | // Type check the second argument (char * for both %b and %D) |
| 4282 | Ex = getDataArg(argIndex + 1); |
| 4283 | const analyze_printf::ArgType &AT2 = ArgType::CStrTy; |
| 4284 | if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) |
| 4285 | EmitFormatDiagnostic( |
| 4286 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 4287 | << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() |
| 4288 | << false << Ex->getSourceRange(), |
| 4289 | Ex->getLocStart(), /*IsStringLocation*/false, |
| 4290 | getSpecifierRange(startSpecifier, specifierLen)); |
| 4291 | |
| 4292 | return true; |
| 4293 | } |
| 4294 | |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 4295 | // Check for using an Objective-C specific conversion specifier |
| 4296 | // in a non-ObjC literal. |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 4297 | if (!ObjCContext && CS.isObjCArg()) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4298 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 4299 | specifierLen); |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 4300 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4301 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4302 | // Check for invalid use of field width |
| 4303 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 4304 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4305 | startSpecifier, specifierLen); |
| 4306 | } |
| 4307 | |
| 4308 | // Check for invalid use of precision |
| 4309 | if (!FS.hasValidPrecision()) { |
| 4310 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 4311 | startSpecifier, specifierLen); |
| 4312 | } |
| 4313 | |
| 4314 | // Check each flag does not conflict with any other component. |
Ted Kremenek | bf4832c | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 4315 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 4316 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4317 | if (!FS.hasValidLeadingZeros()) |
| 4318 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 4319 | if (!FS.hasValidPlusPrefix()) |
| 4320 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 4321 | if (!FS.hasValidSpacePrefix()) |
| 4322 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4323 | if (!FS.hasValidAlternativeForm()) |
| 4324 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 4325 | if (!FS.hasValidLeftJustified()) |
| 4326 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 4327 | |
| 4328 | // Check that flags are not ignored by another flag |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 4329 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 4330 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 4331 | startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4332 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 4333 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 4334 | startSpecifier, specifierLen); |
| 4335 | |
| 4336 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4337 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4338 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 4339 | diag::warn_format_nonsensical_length); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4340 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4341 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4342 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4343 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 4344 | diag::warn_format_non_standard_conversion_spec); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 4345 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4346 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 4347 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 4348 | |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 4349 | // The remaining checks depend on the data arguments. |
| 4350 | if (HasVAListArg) |
| 4351 | return true; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4352 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 4353 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 4354 | return false; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4355 | |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 4356 | const Expr *Arg = getDataArg(argIndex); |
| 4357 | if (!Arg) |
| 4358 | return true; |
| 4359 | |
| 4360 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4363 | static bool requiresParensToAddCast(const Expr *E) { |
| 4364 | // FIXME: We should have a general way to reason about operator |
| 4365 | // precedence and whether parens are actually needed here. |
| 4366 | // Take care of a few common cases where they aren't. |
| 4367 | const Expr *Inside = E->IgnoreImpCasts(); |
| 4368 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) |
| 4369 | Inside = POE->getSyntacticForm()->IgnoreImpCasts(); |
| 4370 | |
| 4371 | switch (Inside->getStmtClass()) { |
| 4372 | case Stmt::ArraySubscriptExprClass: |
| 4373 | case Stmt::CallExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 4374 | case Stmt::CharacterLiteralClass: |
| 4375 | case Stmt::CXXBoolLiteralExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4376 | case Stmt::DeclRefExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 4377 | case Stmt::FloatingLiteralClass: |
| 4378 | case Stmt::IntegerLiteralClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4379 | case Stmt::MemberExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 4380 | case Stmt::ObjCArrayLiteralClass: |
| 4381 | case Stmt::ObjCBoolLiteralExprClass: |
| 4382 | case Stmt::ObjCBoxedExprClass: |
| 4383 | case Stmt::ObjCDictionaryLiteralClass: |
| 4384 | case Stmt::ObjCEncodeExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4385 | case Stmt::ObjCIvarRefExprClass: |
| 4386 | case Stmt::ObjCMessageExprClass: |
| 4387 | case Stmt::ObjCPropertyRefExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 4388 | case Stmt::ObjCStringLiteralClass: |
| 4389 | case Stmt::ObjCSubscriptRefExprClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4390 | case Stmt::ParenExprClass: |
Jordan Rose | ea0fdfe | 2012-12-05 18:44:44 +0000 | [diff] [blame] | 4391 | case Stmt::StringLiteralClass: |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4392 | case Stmt::UnaryOperatorClass: |
| 4393 | return false; |
| 4394 | default: |
| 4395 | return true; |
| 4396 | } |
| 4397 | } |
| 4398 | |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 4399 | static std::pair<QualType, StringRef> |
| 4400 | shouldNotPrintDirectly(const ASTContext &Context, |
| 4401 | QualType IntendedTy, |
| 4402 | const Expr *E) { |
| 4403 | // Use a 'while' to peel off layers of typedefs. |
| 4404 | QualType TyTy = IntendedTy; |
| 4405 | while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { |
| 4406 | StringRef Name = UserTy->getDecl()->getName(); |
| 4407 | QualType CastTy = llvm::StringSwitch<QualType>(Name) |
| 4408 | .Case("NSInteger", Context.LongTy) |
| 4409 | .Case("NSUInteger", Context.UnsignedLongTy) |
| 4410 | .Case("SInt32", Context.IntTy) |
| 4411 | .Case("UInt32", Context.UnsignedIntTy) |
| 4412 | .Default(QualType()); |
| 4413 | |
| 4414 | if (!CastTy.isNull()) |
| 4415 | return std::make_pair(CastTy, Name); |
| 4416 | |
| 4417 | TyTy = UserTy->desugar(); |
| 4418 | } |
| 4419 | |
| 4420 | // Strip parens if necessary. |
| 4421 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 4422 | return shouldNotPrintDirectly(Context, |
| 4423 | PE->getSubExpr()->getType(), |
| 4424 | PE->getSubExpr()); |
| 4425 | |
| 4426 | // If this is a conditional expression, then its result type is constructed |
| 4427 | // via usual arithmetic conversions and thus there might be no necessary |
| 4428 | // typedef sugar there. Recurse to operands to check for NSInteger & |
| 4429 | // Co. usage condition. |
| 4430 | if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 4431 | QualType TrueTy, FalseTy; |
| 4432 | StringRef TrueName, FalseName; |
| 4433 | |
| 4434 | std::tie(TrueTy, TrueName) = |
| 4435 | shouldNotPrintDirectly(Context, |
| 4436 | CO->getTrueExpr()->getType(), |
| 4437 | CO->getTrueExpr()); |
| 4438 | std::tie(FalseTy, FalseName) = |
| 4439 | shouldNotPrintDirectly(Context, |
| 4440 | CO->getFalseExpr()->getType(), |
| 4441 | CO->getFalseExpr()); |
| 4442 | |
| 4443 | if (TrueTy == FalseTy) |
| 4444 | return std::make_pair(TrueTy, TrueName); |
| 4445 | else if (TrueTy.isNull()) |
| 4446 | return std::make_pair(FalseTy, FalseName); |
| 4447 | else if (FalseTy.isNull()) |
| 4448 | return std::make_pair(TrueTy, TrueName); |
| 4449 | } |
| 4450 | |
| 4451 | return std::make_pair(QualType(), StringRef()); |
| 4452 | } |
| 4453 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4454 | bool |
| 4455 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, |
| 4456 | const char *StartSpecifier, |
| 4457 | unsigned SpecifierLen, |
| 4458 | const Expr *E) { |
| 4459 | using namespace analyze_format_string; |
| 4460 | using namespace analyze_printf; |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 4461 | // Now type check the data expression that matches the |
| 4462 | // format specifier. |
Hans Wennborg | c3b3da0 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 4463 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, |
| 4464 | ObjCContext); |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4465 | if (!AT.isValid()) |
| 4466 | return true; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4467 | |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4468 | QualType ExprTy = E->getType(); |
Ted Kremenek | 3365e52 | 2013-04-10 06:26:26 +0000 | [diff] [blame] | 4469 | while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { |
| 4470 | ExprTy = TET->getUnderlyingExpr()->getType(); |
| 4471 | } |
| 4472 | |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4473 | analyze_printf::ArgType::MatchKind match = AT.matchesType(S.Context, ExprTy); |
| 4474 | |
| 4475 | if (match == analyze_printf::ArgType::Match) { |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4476 | return true; |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4477 | } |
Jordan Rose | 9870998 | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 4478 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4479 | // Look through argument promotions for our error message's reported type. |
| 4480 | // This includes the integral and floating promotions, but excludes array |
| 4481 | // and function pointer decay; seeing that an argument intended to be a |
| 4482 | // string has type 'char [6]' is probably more confusing than 'char *'. |
| 4483 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 4484 | if (ICE->getCastKind() == CK_IntegralCast || |
| 4485 | ICE->getCastKind() == CK_FloatingCast) { |
| 4486 | E = ICE->getSubExpr(); |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4487 | ExprTy = E->getType(); |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4488 | |
| 4489 | // Check if we didn't match because of an implicit cast from a 'char' |
| 4490 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 4491 | // function. |
| 4492 | if (ICE->getType() == S.Context.IntTy || |
| 4493 | ICE->getType() == S.Context.UnsignedIntTy) { |
| 4494 | // All further checking is done on the subexpression. |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4495 | if (AT.matchesType(S.Context, ExprTy)) |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4496 | return true; |
Ted Kremenek | 12a37de | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 4497 | } |
Jordan Rose | 9870998 | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 4498 | } |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4499 | } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { |
| 4500 | // Special case for 'a', which has type 'int' in C. |
| 4501 | // Note, however, that we do /not/ want to treat multibyte constants like |
| 4502 | // 'MooV' as characters! This form is deprecated but still exists. |
| 4503 | if (ExprTy == S.Context.IntTy) |
| 4504 | if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) |
| 4505 | ExprTy = S.Context.CharTy; |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4506 | } |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 4507 | |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 4508 | // Look through enums to their underlying type. |
| 4509 | bool IsEnum = false; |
| 4510 | if (auto EnumTy = ExprTy->getAs<EnumType>()) { |
| 4511 | ExprTy = EnumTy->getDecl()->getIntegerType(); |
| 4512 | IsEnum = true; |
| 4513 | } |
| 4514 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4515 | // %C in an Objective-C context prints a unichar, not a wchar_t. |
| 4516 | // If the argument is an integer of some kind, believe the %C and suggest |
| 4517 | // a cast instead of changing the conversion specifier. |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4518 | QualType IntendedTy = ExprTy; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4519 | if (ObjCContext && |
| 4520 | FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { |
| 4521 | if (ExprTy->isIntegralOrUnscopedEnumerationType() && |
| 4522 | !ExprTy->isCharType()) { |
| 4523 | // 'unichar' is defined as a typedef of unsigned short, but we should |
| 4524 | // prefer using the typedef if it is visible. |
| 4525 | IntendedTy = S.Context.UnsignedShortTy; |
Ted Kremenek | da2f405 | 2013-10-15 05:25:17 +0000 | [diff] [blame] | 4526 | |
| 4527 | // While we are here, check if the value is an IntegerLiteral that happens |
| 4528 | // to be within the valid range. |
| 4529 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { |
| 4530 | const llvm::APInt &V = IL->getValue(); |
| 4531 | if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) |
| 4532 | return true; |
| 4533 | } |
| 4534 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4535 | LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(), |
| 4536 | Sema::LookupOrdinaryName); |
| 4537 | if (S.LookupName(Result, S.getCurScope())) { |
| 4538 | NamedDecl *ND = Result.getFoundDecl(); |
| 4539 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
| 4540 | if (TD->getUnderlyingType() == IntendedTy) |
| 4541 | IntendedTy = S.Context.getTypedefType(TD); |
| 4542 | } |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | // Special-case some of Darwin's platform-independence types by suggesting |
| 4547 | // casts to primitive types that are known to be large enough. |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 4548 | bool ShouldNotPrintDirectly = false; StringRef CastTyName; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4549 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 4550 | QualType CastTy; |
| 4551 | std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); |
| 4552 | if (!CastTy.isNull()) { |
| 4553 | IntendedTy = CastTy; |
| 4554 | ShouldNotPrintDirectly = true; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4558 | // We may be able to offer a FixItHint if it is a supported type. |
| 4559 | PrintfSpecifier fixedFS = FS; |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4560 | bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(), |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4561 | S.Context, ObjCContext); |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 4562 | |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4563 | if (success) { |
| 4564 | // Get the fix string from the fixed format specifier |
| 4565 | SmallString<16> buf; |
| 4566 | llvm::raw_svector_ostream os(buf); |
| 4567 | fixedFS.toString(os); |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 4568 | |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4569 | CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); |
| 4570 | |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 4571 | if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) { |
Daniel Jasper | ad8d849 | 2015-03-04 14:18:20 +0000 | [diff] [blame] | 4572 | unsigned diag = diag::warn_format_conversion_argument_type_mismatch; |
| 4573 | if (match == analyze_format_string::ArgType::NoMatchPedantic) { |
| 4574 | diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
| 4575 | } |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4576 | // In this case, the specifier is wrong and should be changed to match |
| 4577 | // the argument. |
Daniel Jasper | ad8d849 | 2015-03-04 14:18:20 +0000 | [diff] [blame] | 4578 | EmitFormatDiagnostic(S.PDiag(diag) |
| 4579 | << AT.getRepresentativeTypeName(S.Context) |
| 4580 | << IntendedTy << IsEnum << E->getSourceRange(), |
| 4581 | E->getLocStart(), |
| 4582 | /*IsStringLocation*/ false, SpecRange, |
| 4583 | FixItHint::CreateReplacement(SpecRange, os.str())); |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4584 | |
| 4585 | } else { |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4586 | // The canonical type for formatting this value is different from the |
| 4587 | // actual type of the expression. (This occurs, for example, with Darwin's |
| 4588 | // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but |
| 4589 | // should be printed as 'long' for 64-bit compatibility.) |
| 4590 | // Rather than emitting a normal format/argument mismatch, we want to |
| 4591 | // add a cast to the recommended type (and correct the format string |
| 4592 | // if necessary). |
| 4593 | SmallString<16> CastBuf; |
| 4594 | llvm::raw_svector_ostream CastFix(CastBuf); |
| 4595 | CastFix << "("; |
| 4596 | IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); |
| 4597 | CastFix << ")"; |
| 4598 | |
| 4599 | SmallVector<FixItHint,4> Hints; |
| 4600 | if (!AT.matchesType(S.Context, IntendedTy)) |
| 4601 | Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); |
| 4602 | |
| 4603 | if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { |
| 4604 | // If there's already a cast present, just replace it. |
| 4605 | SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); |
| 4606 | Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); |
| 4607 | |
| 4608 | } else if (!requiresParensToAddCast(E)) { |
| 4609 | // If the expression has high enough precedence, |
| 4610 | // just write the C-style cast. |
| 4611 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 4612 | CastFix.str())); |
| 4613 | } else { |
| 4614 | // Otherwise, add parens around the expression as well as the cast. |
| 4615 | CastFix << "("; |
| 4616 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), |
| 4617 | CastFix.str())); |
| 4618 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4619 | SourceLocation After = S.getLocForEndOfToken(E->getLocEnd()); |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4620 | Hints.push_back(FixItHint::CreateInsertion(After, ")")); |
| 4621 | } |
| 4622 | |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4623 | if (ShouldNotPrintDirectly) { |
| 4624 | // The expression has a type that should not be printed directly. |
| 4625 | // We extract the name from the typedef because we don't want to show |
| 4626 | // the underlying type in the diagnostic. |
Anton Korobeynikov | 5f951ee | 2014-11-14 22:09:15 +0000 | [diff] [blame] | 4627 | StringRef Name; |
| 4628 | if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy)) |
| 4629 | Name = TypedefTy->getDecl()->getName(); |
| 4630 | else |
| 4631 | Name = CastTyName; |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4632 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast) |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 4633 | << Name << IntendedTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4634 | << E->getSourceRange(), |
| 4635 | E->getLocStart(), /*IsStringLocation=*/false, |
| 4636 | SpecRange, Hints); |
| 4637 | } else { |
| 4638 | // In this case, the expression could be printed using a different |
| 4639 | // specifier, but we've decided that the specifier is probably correct |
| 4640 | // and we should cast instead. Just use the normal warning message. |
| 4641 | EmitFormatDiagnostic( |
Jordan Rose | bc53ed1 | 2014-05-31 04:12:14 +0000 | [diff] [blame] | 4642 | S.PDiag(diag::warn_format_conversion_argument_type_mismatch) |
| 4643 | << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum |
Jordan Rose | 0e5badd | 2012-12-05 18:44:49 +0000 | [diff] [blame] | 4644 | << E->getSourceRange(), |
| 4645 | E->getLocStart(), /*IsStringLocation*/false, |
| 4646 | SpecRange, Hints); |
| 4647 | } |
Jordan Rose | aee3438 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 4648 | } |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4649 | } else { |
| 4650 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, |
| 4651 | SpecifierLen); |
| 4652 | // Since the warning for passing non-POD types to variadic functions |
| 4653 | // was deferred until now, we emit a warning for non-POD |
| 4654 | // arguments here. |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4655 | switch (S.isValidVarArgType(ExprTy)) { |
| 4656 | case Sema::VAK_Valid: |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4657 | case Sema::VAK_ValidInCXX11: { |
| 4658 | unsigned diag = diag::warn_format_conversion_argument_type_mismatch; |
| 4659 | if (match == analyze_printf::ArgType::NoMatchPedantic) { |
| 4660 | diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
| 4661 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4662 | |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4663 | EmitFormatDiagnostic( |
| 4664 | S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy |
| 4665 | << IsEnum << CSR << E->getSourceRange(), |
| 4666 | E->getLocStart(), /*IsStringLocation*/ false, CSR); |
| 4667 | break; |
| 4668 | } |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4669 | case Sema::VAK_Undefined: |
Hans Wennborg | d9dd4d2 | 2014-09-29 23:06:57 +0000 | [diff] [blame] | 4670 | case Sema::VAK_MSVCUndefined: |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4671 | EmitFormatDiagnostic( |
| 4672 | S.PDiag(diag::warn_non_pod_vararg_with_format_string) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4673 | << S.getLangOpts().CPlusPlus11 |
Jordan Rose | 598ec09 | 2012-12-05 18:44:40 +0000 | [diff] [blame] | 4674 | << ExprTy |
Jordan Rose | 22b7471 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 4675 | << CallType |
| 4676 | << AT.getRepresentativeTypeName(S.Context) |
| 4677 | << CSR |
| 4678 | << E->getSourceRange(), |
| 4679 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
Richard Smith | 2868a73 | 2014-02-28 01:36:39 +0000 | [diff] [blame] | 4680 | checkForCStrMembers(AT, E); |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4681 | break; |
| 4682 | |
| 4683 | case Sema::VAK_Invalid: |
| 4684 | if (ExprTy->isObjCObjectType()) |
| 4685 | EmitFormatDiagnostic( |
| 4686 | S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) |
| 4687 | << S.getLangOpts().CPlusPlus11 |
| 4688 | << ExprTy |
| 4689 | << CallType |
| 4690 | << AT.getRepresentativeTypeName(S.Context) |
| 4691 | << CSR |
| 4692 | << E->getSourceRange(), |
| 4693 | E->getLocStart(), /*IsStringLocation*/false, CSR); |
| 4694 | else |
| 4695 | // FIXME: If this is an initializer list, suggest removing the braces |
| 4696 | // or inserting a cast to the target type. |
| 4697 | S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format) |
| 4698 | << isa<InitListExpr>(E) << ExprTy << CallType |
| 4699 | << AT.getRepresentativeTypeName(S.Context) |
| 4700 | << E->getSourceRange(); |
| 4701 | break; |
| 4702 | } |
| 4703 | |
| 4704 | assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && |
| 4705 | "format string specifier index out of range"); |
| 4706 | CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4709 | return true; |
| 4710 | } |
| 4711 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4712 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 4713 | |
| 4714 | namespace { |
| 4715 | class CheckScanfHandler : public CheckFormatHandler { |
| 4716 | public: |
| 4717 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 4718 | const Expr *origFormatExpr, unsigned firstDataArg, |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 4719 | unsigned numDataArgs, const char *beg, bool hasVAListArg, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4720 | ArrayRef<const Expr *> Args, |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 4721 | unsigned formatIdx, bool inFunctionCall, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4722 | Sema::VariadicCallType CallType, |
| 4723 | llvm::SmallBitVector &CheckedVarArgs) |
| 4724 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 4725 | numDataArgs, beg, hasVAListArg, |
| 4726 | Args, formatIdx, inFunctionCall, CallType, |
| 4727 | CheckedVarArgs) |
Jordan Rose | 3e0ec58 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 4728 | {} |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4729 | |
| 4730 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 4731 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4732 | unsigned specifierLen) override; |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 4733 | |
| 4734 | bool HandleInvalidScanfConversionSpecifier( |
| 4735 | const analyze_scanf::ScanfSpecifier &FS, |
| 4736 | const char *startSpecifier, |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4737 | unsigned specifierLen) override; |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 4738 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4739 | void HandleIncompleteScanList(const char *start, const char *end) override; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4740 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4741 | } |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4742 | |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 4743 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 4744 | const char *end) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4745 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), |
| 4746 | getLocationOfByte(end), /*IsStringLocation*/true, |
| 4747 | getSpecifierRange(start, end - start)); |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 4750 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 4751 | const analyze_scanf::ScanfSpecifier &FS, |
| 4752 | const char *startSpecifier, |
| 4753 | unsigned specifierLen) { |
| 4754 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4755 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 4756 | FS.getConversionSpecifier(); |
| 4757 | |
| 4758 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 4759 | getLocationOfByte(CS.getStart()), |
| 4760 | startSpecifier, specifierLen, |
| 4761 | CS.getStart(), CS.getLength()); |
| 4762 | } |
| 4763 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4764 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 4765 | const analyze_scanf::ScanfSpecifier &FS, |
| 4766 | const char *startSpecifier, |
| 4767 | unsigned specifierLen) { |
| 4768 | |
| 4769 | using namespace analyze_scanf; |
| 4770 | using namespace analyze_format_string; |
| 4771 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 4772 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4773 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 4774 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 4775 | // be used to decide if we are using positional arguments consistently. |
| 4776 | if (FS.consumesDataArgument()) { |
| 4777 | if (atFirstArg) { |
| 4778 | atFirstArg = false; |
| 4779 | usesPositionalArgs = FS.usesPositionalArg(); |
| 4780 | } |
| 4781 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4782 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), |
| 4783 | startSpecifier, specifierLen); |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 4784 | return false; |
| 4785 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | // Check if the field with is non-zero. |
| 4789 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 4790 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 4791 | if (Amt.getConstantAmount() == 0) { |
| 4792 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 4793 | Amt.getConstantLength()); |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4794 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), |
| 4795 | getLocationOfByte(Amt.getStart()), |
| 4796 | /*IsStringLocation*/true, R, |
| 4797 | FixItHint::CreateRemoval(R)); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4798 | } |
| 4799 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4800 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4801 | if (!FS.consumesDataArgument()) { |
| 4802 | // FIXME: Technically specifying a precision or field width here |
| 4803 | // makes no sense. Worth issuing a warning at some point. |
| 4804 | return true; |
| 4805 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4806 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4807 | // Consume the argument. |
| 4808 | unsigned argIndex = FS.getArgIndex(); |
| 4809 | if (argIndex < NumDataArgs) { |
| 4810 | // The check to see if the argIndex is valid will come later. |
| 4811 | // We set the bit here because we may exit early from this |
| 4812 | // function if we encounter some other error. |
| 4813 | CoveredArgs.set(argIndex); |
| 4814 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4815 | |
Ted Kremenek | 4407ea4 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 4816 | // Check the length modifier is valid with the given conversion specifier. |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4817 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4818 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 4819 | diag::warn_format_nonsensical_length); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4820 | else if (!FS.hasStandardLengthModifier()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4821 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4822 | else if (!FS.hasStandardLengthConversionCombination()) |
Jordan Rose | 2f9cc04 | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 4823 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, |
| 4824 | diag::warn_format_non_standard_conversion_spec); |
Hans Wennborg | c9dd946 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 4825 | |
Jordan Rose | 9230359 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 4826 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) |
| 4827 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); |
| 4828 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4829 | // The remaining checks depend on the data arguments. |
| 4830 | if (HasVAListArg) |
| 4831 | return true; |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4832 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 4833 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4834 | return false; |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4835 | |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4836 | // Check that the argument type matches the format specifier. |
| 4837 | const Expr *Ex = getDataArg(argIndex); |
Jordan Rose | 58bbe42 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 4838 | if (!Ex) |
| 4839 | return true; |
| 4840 | |
Hans Wennborg | b1ab2a8 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 4841 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4842 | |
| 4843 | if (!AT.isValid()) { |
| 4844 | return true; |
| 4845 | } |
| 4846 | |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4847 | analyze_format_string::ArgType::MatchKind match = |
| 4848 | AT.matchesType(S.Context, Ex->getType()); |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4849 | if (match == analyze_format_string::ArgType::Match) { |
| 4850 | return true; |
| 4851 | } |
Seth Cantrell | b480296 | 2015-03-04 03:12:10 +0000 | [diff] [blame] | 4852 | |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4853 | ScanfSpecifier fixedFS = FS; |
| 4854 | bool success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), |
| 4855 | S.getLangOpts(), S.Context); |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4856 | |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4857 | unsigned diag = diag::warn_format_conversion_argument_type_mismatch; |
| 4858 | if (match == analyze_format_string::ArgType::NoMatchPedantic) { |
| 4859 | diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; |
| 4860 | } |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4861 | |
Seth Cantrell | 7934007 | 2015-03-04 05:58:08 +0000 | [diff] [blame] | 4862 | if (success) { |
| 4863 | // Get the fix string from the fixed format specifier. |
| 4864 | SmallString<128> buf; |
| 4865 | llvm::raw_svector_ostream os(buf); |
| 4866 | fixedFS.toString(os); |
| 4867 | |
| 4868 | EmitFormatDiagnostic( |
| 4869 | S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) |
| 4870 | << Ex->getType() << false << Ex->getSourceRange(), |
| 4871 | Ex->getLocStart(), |
| 4872 | /*IsStringLocation*/ false, |
| 4873 | getSpecifierRange(startSpecifier, specifierLen), |
| 4874 | FixItHint::CreateReplacement( |
| 4875 | getSpecifierRange(startSpecifier, specifierLen), os.str())); |
| 4876 | } else { |
| 4877 | EmitFormatDiagnostic(S.PDiag(diag) |
| 4878 | << AT.getRepresentativeTypeName(S.Context) |
| 4879 | << Ex->getType() << false << Ex->getSourceRange(), |
| 4880 | Ex->getLocStart(), |
| 4881 | /*IsStringLocation*/ false, |
| 4882 | getSpecifierRange(startSpecifier, specifierLen)); |
Hans Wennborg | b1a5e09 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 4883 | } |
| 4884 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4885 | return true; |
| 4886 | } |
| 4887 | |
| 4888 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 4889 | const Expr *OrigFormatExpr, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4890 | ArrayRef<const Expr *> Args, |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 4891 | bool HasVAListArg, unsigned format_idx, |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 4892 | unsigned firstDataArg, FormatStringType Type, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4893 | bool inFunctionCall, VariadicCallType CallType, |
| 4894 | llvm::SmallBitVector &CheckedVarArgs) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4895 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4896 | // CHECK: is the format string a wide literal? |
Richard Smith | 4060f77 | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 4897 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4898 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 4899 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4900 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), |
| 4901 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4902 | return; |
| 4903 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4904 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4905 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4906 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 4907 | const char *Str = StrRef.data(); |
Benjamin Kramer | 6c6a4f4 | 2014-02-20 17:05:38 +0000 | [diff] [blame] | 4908 | // Account for cases where the string literal is truncated in a declaration. |
| 4909 | const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); |
| 4910 | assert(T && "String literal not of constant array type!"); |
| 4911 | size_t TypeSize = T->getSize().getZExtValue(); |
| 4912 | 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] | 4913 | const unsigned numDataArgs = Args.size() - firstDataArg; |
Benjamin Kramer | 6c6a4f4 | 2014-02-20 17:05:38 +0000 | [diff] [blame] | 4914 | |
| 4915 | // Emit a warning if the string literal is truncated and does not contain an |
| 4916 | // embedded null character. |
| 4917 | if (TypeSize <= StrRef.size() && |
| 4918 | StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) { |
| 4919 | CheckFormatHandler::EmitFormatDiagnostic( |
| 4920 | *this, inFunctionCall, Args[format_idx], |
| 4921 | PDiag(diag::warn_printf_format_string_not_null_terminated), |
| 4922 | FExpr->getLocStart(), |
| 4923 | /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); |
| 4924 | return; |
| 4925 | } |
| 4926 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4927 | // CHECK: empty format string? |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 4928 | if (StrLen == 0 && numDataArgs > 0) { |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4929 | CheckFormatHandler::EmitFormatDiagnostic( |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 4930 | *this, inFunctionCall, Args[format_idx], |
Richard Trieu | 03cf7b7 | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 4931 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), |
| 4932 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 4933 | return; |
| 4934 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4935 | |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 4936 | if (Type == FST_Printf || Type == FST_NSString || |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 4937 | Type == FST_FreeBSDKPrintf || Type == FST_OSTrace) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4938 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 4939 | numDataArgs, (Type == FST_NSString || Type == FST_OSTrace), |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4940 | Str, HasVAListArg, Args, format_idx, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4941 | inFunctionCall, CallType, CheckedVarArgs); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4942 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 4943 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, |
Jordan Rose | 510260c | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 4944 | getLangOpts(), |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 4945 | Context.getTargetInfo(), |
| 4946 | Type == FST_FreeBSDKPrintf)) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4947 | H.DoneProcessing(); |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 4948 | } else if (Type == FST_Scanf) { |
Jordan Rose | 97c6f2b | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 4949 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 4950 | Str, HasVAListArg, Args, format_idx, |
Richard Smith | d7293d7 | 2013-08-05 18:49:43 +0000 | [diff] [blame] | 4951 | inFunctionCall, CallType, CheckedVarArgs); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4952 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 4953 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, |
Jordan Rose | 510260c | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 4954 | getLangOpts(), |
| 4955 | Context.getTargetInfo())) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 4956 | H.DoneProcessing(); |
Jean-Daniel Dupas | 028573e7 | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 4957 | } // TODO: handle other formats |
Ted Kremenek | c70ee86 | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 4960 | bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { |
| 4961 | // Str - The format string. NOTE: this is NOT null-terminated! |
| 4962 | StringRef StrRef = FExpr->getString(); |
| 4963 | const char *Str = StrRef.data(); |
| 4964 | // Account for cases where the string literal is truncated in a declaration. |
| 4965 | const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); |
| 4966 | assert(T && "String literal not of constant array type!"); |
| 4967 | size_t TypeSize = T->getSize().getZExtValue(); |
| 4968 | size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); |
| 4969 | return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, |
| 4970 | getLangOpts(), |
| 4971 | Context.getTargetInfo()); |
| 4972 | } |
| 4973 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 4974 | //===--- CHECK: Warn on use of wrong absolute value function. -------------===// |
| 4975 | |
| 4976 | // Returns the related absolute value function that is larger, of 0 if one |
| 4977 | // does not exist. |
| 4978 | static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { |
| 4979 | switch (AbsFunction) { |
| 4980 | default: |
| 4981 | return 0; |
| 4982 | |
| 4983 | case Builtin::BI__builtin_abs: |
| 4984 | return Builtin::BI__builtin_labs; |
| 4985 | case Builtin::BI__builtin_labs: |
| 4986 | return Builtin::BI__builtin_llabs; |
| 4987 | case Builtin::BI__builtin_llabs: |
| 4988 | return 0; |
| 4989 | |
| 4990 | case Builtin::BI__builtin_fabsf: |
| 4991 | return Builtin::BI__builtin_fabs; |
| 4992 | case Builtin::BI__builtin_fabs: |
| 4993 | return Builtin::BI__builtin_fabsl; |
| 4994 | case Builtin::BI__builtin_fabsl: |
| 4995 | return 0; |
| 4996 | |
| 4997 | case Builtin::BI__builtin_cabsf: |
| 4998 | return Builtin::BI__builtin_cabs; |
| 4999 | case Builtin::BI__builtin_cabs: |
| 5000 | return Builtin::BI__builtin_cabsl; |
| 5001 | case Builtin::BI__builtin_cabsl: |
| 5002 | return 0; |
| 5003 | |
| 5004 | case Builtin::BIabs: |
| 5005 | return Builtin::BIlabs; |
| 5006 | case Builtin::BIlabs: |
| 5007 | return Builtin::BIllabs; |
| 5008 | case Builtin::BIllabs: |
| 5009 | return 0; |
| 5010 | |
| 5011 | case Builtin::BIfabsf: |
| 5012 | return Builtin::BIfabs; |
| 5013 | case Builtin::BIfabs: |
| 5014 | return Builtin::BIfabsl; |
| 5015 | case Builtin::BIfabsl: |
| 5016 | return 0; |
| 5017 | |
| 5018 | case Builtin::BIcabsf: |
| 5019 | return Builtin::BIcabs; |
| 5020 | case Builtin::BIcabs: |
| 5021 | return Builtin::BIcabsl; |
| 5022 | case Builtin::BIcabsl: |
| 5023 | return 0; |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | // Returns the argument type of the absolute value function. |
| 5028 | static QualType getAbsoluteValueArgumentType(ASTContext &Context, |
| 5029 | unsigned AbsType) { |
| 5030 | if (AbsType == 0) |
| 5031 | return QualType(); |
| 5032 | |
| 5033 | ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; |
| 5034 | QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); |
| 5035 | if (Error != ASTContext::GE_None) |
| 5036 | return QualType(); |
| 5037 | |
| 5038 | const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); |
| 5039 | if (!FT) |
| 5040 | return QualType(); |
| 5041 | |
| 5042 | if (FT->getNumParams() != 1) |
| 5043 | return QualType(); |
| 5044 | |
| 5045 | return FT->getParamType(0); |
| 5046 | } |
| 5047 | |
| 5048 | // Returns the best absolute value function, or zero, based on type and |
| 5049 | // current absolute value function. |
| 5050 | static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, |
| 5051 | unsigned AbsFunctionKind) { |
| 5052 | unsigned BestKind = 0; |
| 5053 | uint64_t ArgSize = Context.getTypeSize(ArgType); |
| 5054 | for (unsigned Kind = AbsFunctionKind; Kind != 0; |
| 5055 | Kind = getLargerAbsoluteValueFunction(Kind)) { |
| 5056 | QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); |
| 5057 | if (Context.getTypeSize(ParamType) >= ArgSize) { |
| 5058 | if (BestKind == 0) |
| 5059 | BestKind = Kind; |
| 5060 | else if (Context.hasSameType(ParamType, ArgType)) { |
| 5061 | BestKind = Kind; |
| 5062 | break; |
| 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | return BestKind; |
| 5067 | } |
| 5068 | |
| 5069 | enum AbsoluteValueKind { |
| 5070 | AVK_Integer, |
| 5071 | AVK_Floating, |
| 5072 | AVK_Complex |
| 5073 | }; |
| 5074 | |
| 5075 | static AbsoluteValueKind getAbsoluteValueKind(QualType T) { |
| 5076 | if (T->isIntegralOrEnumerationType()) |
| 5077 | return AVK_Integer; |
| 5078 | if (T->isRealFloatingType()) |
| 5079 | return AVK_Floating; |
| 5080 | if (T->isAnyComplexType()) |
| 5081 | return AVK_Complex; |
| 5082 | |
| 5083 | llvm_unreachable("Type not integer, floating, or complex"); |
| 5084 | } |
| 5085 | |
| 5086 | // Changes the absolute value function to a different type. Preserves whether |
| 5087 | // the function is a builtin. |
| 5088 | static unsigned changeAbsFunction(unsigned AbsKind, |
| 5089 | AbsoluteValueKind ValueKind) { |
| 5090 | switch (ValueKind) { |
| 5091 | case AVK_Integer: |
| 5092 | switch (AbsKind) { |
| 5093 | default: |
| 5094 | return 0; |
| 5095 | case Builtin::BI__builtin_fabsf: |
| 5096 | case Builtin::BI__builtin_fabs: |
| 5097 | case Builtin::BI__builtin_fabsl: |
| 5098 | case Builtin::BI__builtin_cabsf: |
| 5099 | case Builtin::BI__builtin_cabs: |
| 5100 | case Builtin::BI__builtin_cabsl: |
| 5101 | return Builtin::BI__builtin_abs; |
| 5102 | case Builtin::BIfabsf: |
| 5103 | case Builtin::BIfabs: |
| 5104 | case Builtin::BIfabsl: |
| 5105 | case Builtin::BIcabsf: |
| 5106 | case Builtin::BIcabs: |
| 5107 | case Builtin::BIcabsl: |
| 5108 | return Builtin::BIabs; |
| 5109 | } |
| 5110 | case AVK_Floating: |
| 5111 | switch (AbsKind) { |
| 5112 | default: |
| 5113 | return 0; |
| 5114 | case Builtin::BI__builtin_abs: |
| 5115 | case Builtin::BI__builtin_labs: |
| 5116 | case Builtin::BI__builtin_llabs: |
| 5117 | case Builtin::BI__builtin_cabsf: |
| 5118 | case Builtin::BI__builtin_cabs: |
| 5119 | case Builtin::BI__builtin_cabsl: |
| 5120 | return Builtin::BI__builtin_fabsf; |
| 5121 | case Builtin::BIabs: |
| 5122 | case Builtin::BIlabs: |
| 5123 | case Builtin::BIllabs: |
| 5124 | case Builtin::BIcabsf: |
| 5125 | case Builtin::BIcabs: |
| 5126 | case Builtin::BIcabsl: |
| 5127 | return Builtin::BIfabsf; |
| 5128 | } |
| 5129 | case AVK_Complex: |
| 5130 | switch (AbsKind) { |
| 5131 | default: |
| 5132 | return 0; |
| 5133 | case Builtin::BI__builtin_abs: |
| 5134 | case Builtin::BI__builtin_labs: |
| 5135 | case Builtin::BI__builtin_llabs: |
| 5136 | case Builtin::BI__builtin_fabsf: |
| 5137 | case Builtin::BI__builtin_fabs: |
| 5138 | case Builtin::BI__builtin_fabsl: |
| 5139 | return Builtin::BI__builtin_cabsf; |
| 5140 | case Builtin::BIabs: |
| 5141 | case Builtin::BIlabs: |
| 5142 | case Builtin::BIllabs: |
| 5143 | case Builtin::BIfabsf: |
| 5144 | case Builtin::BIfabs: |
| 5145 | case Builtin::BIfabsl: |
| 5146 | return Builtin::BIcabsf; |
| 5147 | } |
| 5148 | } |
| 5149 | llvm_unreachable("Unable to convert function"); |
| 5150 | } |
| 5151 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 5152 | static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5153 | const IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 5154 | if (!FnInfo) |
| 5155 | return 0; |
| 5156 | |
| 5157 | switch (FDecl->getBuiltinID()) { |
| 5158 | default: |
| 5159 | return 0; |
| 5160 | case Builtin::BI__builtin_abs: |
| 5161 | case Builtin::BI__builtin_fabs: |
| 5162 | case Builtin::BI__builtin_fabsf: |
| 5163 | case Builtin::BI__builtin_fabsl: |
| 5164 | case Builtin::BI__builtin_labs: |
| 5165 | case Builtin::BI__builtin_llabs: |
| 5166 | case Builtin::BI__builtin_cabs: |
| 5167 | case Builtin::BI__builtin_cabsf: |
| 5168 | case Builtin::BI__builtin_cabsl: |
| 5169 | case Builtin::BIabs: |
| 5170 | case Builtin::BIlabs: |
| 5171 | case Builtin::BIllabs: |
| 5172 | case Builtin::BIfabs: |
| 5173 | case Builtin::BIfabsf: |
| 5174 | case Builtin::BIfabsl: |
| 5175 | case Builtin::BIcabs: |
| 5176 | case Builtin::BIcabsf: |
| 5177 | case Builtin::BIcabsl: |
| 5178 | return FDecl->getBuiltinID(); |
| 5179 | } |
| 5180 | llvm_unreachable("Unknown Builtin type"); |
| 5181 | } |
| 5182 | |
| 5183 | // If the replacement is valid, emit a note with replacement function. |
| 5184 | // Additionally, suggest including the proper header if not already included. |
| 5185 | static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5186 | unsigned AbsKind, QualType ArgType) { |
| 5187 | bool EmitHeaderHint = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5188 | const char *HeaderName = nullptr; |
| 5189 | const char *FunctionName = nullptr; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5190 | if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { |
| 5191 | FunctionName = "std::abs"; |
| 5192 | if (ArgType->isIntegralOrEnumerationType()) { |
| 5193 | HeaderName = "cstdlib"; |
| 5194 | } else if (ArgType->isRealFloatingType()) { |
| 5195 | HeaderName = "cmath"; |
| 5196 | } else { |
| 5197 | llvm_unreachable("Invalid Type"); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5198 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5199 | |
| 5200 | // Lookup all std::abs |
| 5201 | if (NamespaceDecl *Std = S.getStdNamespace()) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5202 | LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5203 | R.suppressDiagnostics(); |
| 5204 | S.LookupQualifiedName(R, Std); |
| 5205 | |
| 5206 | for (const auto *I : R) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5207 | const FunctionDecl *FDecl = nullptr; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5208 | if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { |
| 5209 | FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); |
| 5210 | } else { |
| 5211 | FDecl = dyn_cast<FunctionDecl>(I); |
| 5212 | } |
| 5213 | if (!FDecl) |
| 5214 | continue; |
| 5215 | |
| 5216 | // Found std::abs(), check that they are the right ones. |
| 5217 | if (FDecl->getNumParams() != 1) |
| 5218 | continue; |
| 5219 | |
| 5220 | // Check that the parameter type can handle the argument. |
| 5221 | QualType ParamType = FDecl->getParamDecl(0)->getType(); |
| 5222 | if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && |
| 5223 | S.Context.getTypeSize(ArgType) <= |
| 5224 | S.Context.getTypeSize(ParamType)) { |
| 5225 | // Found a function, don't need the header hint. |
| 5226 | EmitHeaderHint = false; |
| 5227 | break; |
| 5228 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5229 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5230 | } |
| 5231 | } else { |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 5232 | FunctionName = S.Context.BuiltinInfo.getName(AbsKind); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5233 | HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); |
| 5234 | |
| 5235 | if (HeaderName) { |
| 5236 | DeclarationName DN(&S.Context.Idents.get(FunctionName)); |
| 5237 | LookupResult R(S, DN, Loc, Sema::LookupAnyName); |
| 5238 | R.suppressDiagnostics(); |
| 5239 | S.LookupName(R, S.getCurScope()); |
| 5240 | |
| 5241 | if (R.isSingleResult()) { |
| 5242 | FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); |
| 5243 | if (FD && FD->getBuiltinID() == AbsKind) { |
| 5244 | EmitHeaderHint = false; |
| 5245 | } else { |
| 5246 | return; |
| 5247 | } |
| 5248 | } else if (!R.empty()) { |
| 5249 | return; |
| 5250 | } |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | S.Diag(Loc, diag::note_replace_abs_function) |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5255 | << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5256 | |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5257 | if (!HeaderName) |
| 5258 | return; |
| 5259 | |
| 5260 | if (!EmitHeaderHint) |
| 5261 | return; |
| 5262 | |
Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 5263 | S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName |
| 5264 | << FunctionName; |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | static bool IsFunctionStdAbs(const FunctionDecl *FDecl) { |
| 5268 | if (!FDecl) |
| 5269 | return false; |
| 5270 | |
| 5271 | if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr("abs")) |
| 5272 | return false; |
| 5273 | |
| 5274 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(FDecl->getDeclContext()); |
| 5275 | |
| 5276 | while (ND && ND->isInlineNamespace()) { |
| 5277 | ND = dyn_cast<NamespaceDecl>(ND->getDeclContext()); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5278 | } |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5279 | |
| 5280 | if (!ND || !ND->getIdentifier() || !ND->getIdentifier()->isStr("std")) |
| 5281 | return false; |
| 5282 | |
| 5283 | if (!isa<TranslationUnitDecl>(ND->getDeclContext())) |
| 5284 | return false; |
| 5285 | |
| 5286 | return true; |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | // Warn when using the wrong abs() function. |
| 5290 | void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, |
| 5291 | const FunctionDecl *FDecl, |
| 5292 | IdentifierInfo *FnInfo) { |
| 5293 | if (Call->getNumArgs() != 1) |
| 5294 | return; |
| 5295 | |
| 5296 | unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5297 | bool IsStdAbs = IsFunctionStdAbs(FDecl); |
| 5298 | if (AbsKind == 0 && !IsStdAbs) |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5299 | return; |
| 5300 | |
| 5301 | QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); |
| 5302 | QualType ParamType = Call->getArg(0)->getType(); |
| 5303 | |
Alp Toker | 5d96e0a | 2014-07-11 20:53:51 +0000 | [diff] [blame] | 5304 | // Unsigned types cannot be negative. Suggest removing the absolute value |
| 5305 | // function call. |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5306 | if (ArgType->isUnsignedIntegerType()) { |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5307 | const char *FunctionName = |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 5308 | IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5309 | Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; |
| 5310 | Diag(Call->getExprLoc(), diag::note_remove_abs) |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5311 | << FunctionName |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5312 | << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); |
| 5313 | return; |
| 5314 | } |
| 5315 | |
David Majnemer | 7f77eb9 | 2015-11-15 03:04:34 +0000 | [diff] [blame] | 5316 | // Taking the absolute value of a pointer is very suspicious, they probably |
| 5317 | // wanted to index into an array, dereference a pointer, call a function, etc. |
| 5318 | if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) { |
| 5319 | unsigned DiagType = 0; |
| 5320 | if (ArgType->isFunctionType()) |
| 5321 | DiagType = 1; |
| 5322 | else if (ArgType->isArrayType()) |
| 5323 | DiagType = 2; |
| 5324 | |
| 5325 | Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType; |
| 5326 | return; |
| 5327 | } |
| 5328 | |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5329 | // std::abs has overloads which prevent most of the absolute value problems |
| 5330 | // from occurring. |
| 5331 | if (IsStdAbs) |
| 5332 | return; |
| 5333 | |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5334 | AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); |
| 5335 | AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); |
| 5336 | |
| 5337 | // The argument and parameter are the same kind. Check if they are the right |
| 5338 | // size. |
| 5339 | if (ArgValueKind == ParamValueKind) { |
| 5340 | if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) |
| 5341 | return; |
| 5342 | |
| 5343 | unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); |
| 5344 | Diag(Call->getExprLoc(), diag::warn_abs_too_small) |
| 5345 | << FDecl << ArgType << ParamType; |
| 5346 | |
| 5347 | if (NewAbsKind == 0) |
| 5348 | return; |
| 5349 | |
| 5350 | emitReplacement(*this, Call->getExprLoc(), |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5351 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5352 | return; |
| 5353 | } |
| 5354 | |
| 5355 | // ArgValueKind != ParamValueKind |
| 5356 | // The wrong type of absolute value function was used. Attempt to find the |
| 5357 | // proper one. |
| 5358 | unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); |
| 5359 | NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); |
| 5360 | if (NewAbsKind == 0) |
| 5361 | return; |
| 5362 | |
| 5363 | Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) |
| 5364 | << FDecl << ParamValueKind << ArgValueKind; |
| 5365 | |
| 5366 | emitReplacement(*this, Call->getExprLoc(), |
Richard Trieu | beffb83 | 2014-04-15 23:47:53 +0000 | [diff] [blame] | 5367 | Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); |
Richard Trieu | 7eb0b2c | 2014-02-26 01:17:28 +0000 | [diff] [blame] | 5368 | return; |
| 5369 | } |
| 5370 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5371 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 5372 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5373 | /// \brief Takes the expression passed to the size_t parameter of functions |
| 5374 | /// such as memcmp, strncat, etc and warns if it's a comparison. |
| 5375 | /// |
| 5376 | /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. |
| 5377 | static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, |
| 5378 | IdentifierInfo *FnName, |
| 5379 | SourceLocation FnLoc, |
| 5380 | SourceLocation RParenLoc) { |
| 5381 | const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); |
| 5382 | if (!Size) |
| 5383 | return false; |
| 5384 | |
| 5385 | // if E is binop and op is >, <, >=, <=, ==, &&, ||: |
| 5386 | if (!Size->isComparisonOp() && !Size->isEqualityOp() && !Size->isLogicalOp()) |
| 5387 | return false; |
| 5388 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5389 | SourceRange SizeRange = Size->getSourceRange(); |
| 5390 | S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) |
| 5391 | << SizeRange << FnName; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 5392 | S.Diag(FnLoc, diag::note_memsize_comparison_paren) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5393 | << FnName << FixItHint::CreateInsertion( |
| 5394 | S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")") |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5395 | << FixItHint::CreateRemoval(RParenLoc); |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 5396 | S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5397 | << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5398 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), |
| 5399 | ")"); |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5400 | |
| 5401 | return true; |
| 5402 | } |
| 5403 | |
Reid Kleckner | 5fb5b12 | 2014-06-27 23:58:21 +0000 | [diff] [blame] | 5404 | /// \brief Determine whether the given type is or contains a dynamic class type |
| 5405 | /// (e.g., whether it has a vtable). |
| 5406 | static const CXXRecordDecl *getContainedDynamicClass(QualType T, |
| 5407 | bool &IsContained) { |
| 5408 | // Look through array types while ignoring qualifiers. |
| 5409 | const Type *Ty = T->getBaseElementTypeUnsafe(); |
| 5410 | IsContained = false; |
| 5411 | |
| 5412 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 5413 | RD = RD ? RD->getDefinition() : nullptr; |
| 5414 | if (!RD) |
| 5415 | return nullptr; |
| 5416 | |
| 5417 | if (RD->isDynamicClass()) |
| 5418 | return RD; |
| 5419 | |
| 5420 | // Check all the fields. If any bases were dynamic, the class is dynamic. |
| 5421 | // It's impossible for a class to transitively contain itself by value, so |
| 5422 | // infinite recursion is impossible. |
| 5423 | for (auto *FD : RD->fields()) { |
| 5424 | bool SubContained; |
| 5425 | if (const CXXRecordDecl *ContainedRD = |
| 5426 | getContainedDynamicClass(FD->getType(), SubContained)) { |
| 5427 | IsContained = true; |
| 5428 | return ContainedRD; |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | return nullptr; |
Douglas Gregor | a74926b | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 5435 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5436 | /// otherwise returns NULL. |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5437 | static const Expr *getSizeOfExprArg(const Expr *E) { |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 5438 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5439 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 5440 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 5441 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 5442 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5443 | return nullptr; |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5444 | } |
| 5445 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 5446 | /// \brief If E is a sizeof expression, returns its argument type. |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5447 | static QualType getSizeOfArgType(const Expr *E) { |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5448 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 5449 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 5450 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 5451 | return SizeOf->getTypeOfArgument(); |
| 5452 | |
| 5453 | return QualType(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5456 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 5457 | /// |
Chandler Carruth | ac68726 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 5458 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 5459 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 5460 | /// function calls. |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5461 | /// |
| 5462 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 5463 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 5464 | unsigned BId, |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 5465 | IdentifierInfo *FnName) { |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 5466 | assert(BId != 0); |
| 5467 | |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 5468 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 5469 | // we have enough arguments, and if not, abort further checking. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 5470 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 5471 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 5472 | return; |
| 5473 | |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 5474 | unsigned LastArg = (BId == Builtin::BImemset || |
| 5475 | BId == Builtin::BIstrndup ? 1 : 2); |
| 5476 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 5477 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5478 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5479 | if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, |
| 5480 | Call->getLocStart(), Call->getRParenLoc())) |
| 5481 | return; |
| 5482 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5483 | // We have special checking when the length is a sizeof expression. |
| 5484 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 5485 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 5486 | llvm::FoldingSetNodeID SizeOfArgID; |
| 5487 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 5488 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 5489 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 5490 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5491 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 5492 | QualType DestTy = Dest->getType(); |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5493 | QualType PointeeTy; |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 5494 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5495 | PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5496 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5497 | // Never warn about void type pointers. This can be used to suppress |
| 5498 | // false positives. |
| 5499 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 5500 | continue; |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5501 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5502 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 5503 | // actually comparing the expressions for equality. Because computing the |
| 5504 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 5505 | // enabled. |
| 5506 | if (SizeOfArg && |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5507 | !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, |
| 5508 | SizeOfArg->getExprLoc())) { |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5509 | // We only compute IDs for expressions if the warning is enabled, and |
| 5510 | // cache the sizeof arg's ID. |
| 5511 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 5512 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 5513 | llvm::FoldingSetNodeID DestID; |
| 5514 | Dest->Profile(DestID, Context, true); |
| 5515 | if (DestID == SizeOfArgID) { |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 5516 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 5517 | // over sizeof(src) as well. |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5518 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 5519 | StringRef ReadableName = FnName->getName(); |
| 5520 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5521 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 5522 | if (UnaryOp->getOpcode() == UO_AddrOf) |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5523 | ActionIdx = 1; // If its an address-of operator, just remove it. |
Fariborz Jahanian | 4d365ba | 2013-01-30 01:12:44 +0000 | [diff] [blame] | 5524 | if (!PointeeTy->isIncompleteType() && |
| 5525 | (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5526 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 5527 | // suggest an explicit length. |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 5528 | |
| 5529 | // If the function is defined as a builtin macro, do not show macro |
| 5530 | // expansion. |
| 5531 | SourceLocation SL = SizeOfArg->getExprLoc(); |
| 5532 | SourceRange DSR = Dest->getSourceRange(); |
| 5533 | SourceRange SSR = SizeOfArg->getSourceRange(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5534 | SourceManager &SM = getSourceManager(); |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 5535 | |
| 5536 | if (SM.isMacroArgExpansion(SL)) { |
| 5537 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); |
| 5538 | SL = SM.getSpellingLoc(SL); |
| 5539 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), |
| 5540 | SM.getSpellingLoc(DSR.getEnd())); |
| 5541 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), |
| 5542 | SM.getSpellingLoc(SSR.getEnd())); |
| 5543 | } |
| 5544 | |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 5545 | DiagRuntimeBehavior(SL, SizeOfArg, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5546 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 5547 | << ReadableName |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 5548 | << PointeeTy |
| 5549 | << DestTy |
Anna Zaks | 869aecc | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 5550 | << DSR |
Anna Zaks | d08d915 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 5551 | << SSR); |
| 5552 | DiagRuntimeBehavior(SL, SizeOfArg, |
| 5553 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) |
| 5554 | << ActionIdx |
| 5555 | << SSR); |
| 5556 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 5557 | break; |
| 5558 | } |
| 5559 | } |
| 5560 | |
| 5561 | // Also check for cases where the sizeof argument is the exact same |
| 5562 | // type as the memory argument, and where it points to a user-defined |
| 5563 | // record type. |
| 5564 | if (SizeOfArgTy != QualType()) { |
| 5565 | if (PointeeTy->isRecordType() && |
| 5566 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 5567 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 5568 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 5569 | << FnName << SizeOfArgTy << ArgIdx |
| 5570 | << PointeeTy << Dest->getSourceRange() |
| 5571 | << LenExpr->getSourceRange()); |
| 5572 | break; |
| 5573 | } |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 5574 | } |
Nico Weber | bac8b6b | 2015-03-21 17:56:44 +0000 | [diff] [blame] | 5575 | } else if (DestTy->isArrayType()) { |
| 5576 | PointeeTy = DestTy; |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5577 | } |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 5578 | |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5579 | if (PointeeTy == QualType()) |
| 5580 | continue; |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 5581 | |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5582 | // Always complain about dynamic classes. |
| 5583 | bool IsContained; |
| 5584 | if (const CXXRecordDecl *ContainedRD = |
| 5585 | getContainedDynamicClass(PointeeTy, IsContained)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5586 | |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5587 | unsigned OperationType = 0; |
| 5588 | // "overwritten" if we're warning about the destination for any call |
| 5589 | // but memcmp; otherwise a verb appropriate to the call. |
| 5590 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { |
| 5591 | if (BId == Builtin::BImemcpy) |
| 5592 | OperationType = 1; |
| 5593 | else if(BId == Builtin::BImemmove) |
| 5594 | OperationType = 2; |
| 5595 | else if (BId == Builtin::BImemcmp) |
| 5596 | OperationType = 3; |
| 5597 | } |
| 5598 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5599 | DiagRuntimeBehavior( |
| 5600 | Dest->getExprLoc(), Dest, |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5601 | PDiag(diag::warn_dyn_class_memaccess) |
| 5602 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) |
| 5603 | << FnName << IsContained << ContainedRD << OperationType |
| 5604 | << Call->getCallee()->getSourceRange()); |
| 5605 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && |
| 5606 | BId != Builtin::BImemset) |
| 5607 | DiagRuntimeBehavior( |
| 5608 | Dest->getExprLoc(), Dest, |
| 5609 | PDiag(diag::warn_arc_object_memaccess) |
| 5610 | << ArgIdx << FnName << PointeeTy |
| 5611 | << Call->getCallee()->getSourceRange()); |
| 5612 | else |
| 5613 | continue; |
| 5614 | |
| 5615 | DiagRuntimeBehavior( |
| 5616 | Dest->getExprLoc(), Dest, |
| 5617 | PDiag(diag::note_bad_memaccess_silence) |
| 5618 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 5619 | break; |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5620 | } |
Nico Weber | c44b35e | 2015-03-21 17:37:46 +0000 | [diff] [blame] | 5621 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5624 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 5625 | // This intentionally does not ignore all integer constant expressions because |
| 5626 | // we don't want to remove sizeof(). |
| 5627 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 5628 | Ex = Ex->IgnoreParenCasts(); |
| 5629 | |
| 5630 | for (;;) { |
| 5631 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 5632 | if (!BO || !BO->isAdditiveOp()) |
| 5633 | break; |
| 5634 | |
| 5635 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 5636 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 5637 | |
| 5638 | if (isa<IntegerLiteral>(RHS)) |
| 5639 | Ex = LHS; |
| 5640 | else if (isa<IntegerLiteral>(LHS)) |
| 5641 | Ex = RHS; |
| 5642 | else |
| 5643 | break; |
| 5644 | } |
| 5645 | |
| 5646 | return Ex; |
| 5647 | } |
| 5648 | |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 5649 | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, |
| 5650 | ASTContext &Context) { |
| 5651 | // Only handle constant-sized or VLAs, but not flexible members. |
| 5652 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { |
| 5653 | // Only issue the FIXIT for arrays of size > 1. |
| 5654 | if (CAT->getSize().getSExtValue() <= 1) |
| 5655 | return false; |
| 5656 | } else if (!Ty->isVariableArrayType()) { |
| 5657 | return false; |
| 5658 | } |
| 5659 | return true; |
| 5660 | } |
| 5661 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5662 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 5663 | // be the size of the source, instead of the destination. |
| 5664 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 5665 | IdentifierInfo *FnName) { |
| 5666 | |
| 5667 | // Don't crash if the user has the wrong number of arguments |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 5668 | unsigned NumArgs = Call->getNumArgs(); |
| 5669 | if ((NumArgs != 3) && (NumArgs != 4)) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5670 | return; |
| 5671 | |
| 5672 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 5673 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5674 | const Expr *CompareWithSrc = nullptr; |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5675 | |
| 5676 | if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, |
| 5677 | Call->getLocStart(), Call->getRParenLoc())) |
| 5678 | return; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5679 | |
| 5680 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 5681 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 5682 | CompareWithSrc = Ex; |
| 5683 | else { |
| 5684 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 5685 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 5686 | if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && |
| 5687 | SizeCall->getNumArgs() == 1) |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5688 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | if (!CompareWithSrc) |
| 5693 | return; |
| 5694 | |
| 5695 | // Determine if the argument to sizeof/strlen is equal to the source |
| 5696 | // argument. In principle there's all kinds of things you could do |
| 5697 | // here, for instance creating an == expression and evaluating it with |
| 5698 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 5699 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 5700 | if (!SrcArgDRE) |
| 5701 | return; |
| 5702 | |
| 5703 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 5704 | if (!CompareWithSrcDRE || |
| 5705 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 5706 | return; |
| 5707 | |
| 5708 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 5709 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 5710 | << OriginalSizeArg->getSourceRange() << FnName; |
| 5711 | |
| 5712 | // Output a FIXIT hint if the destination is an array (rather than a |
| 5713 | // pointer to an array). This could be enhanced to handle some |
| 5714 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 5715 | // we could say 'sizeof(array)-2'. |
| 5716 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 5717 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5718 | return; |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5719 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5720 | SmallString<128> sizeString; |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5721 | llvm::raw_svector_ostream OS(sizeString); |
| 5722 | OS << "sizeof("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5723 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 5724 | OS << ")"; |
| 5725 | |
| 5726 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 5727 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 5728 | OS.str()); |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5731 | /// Check if two expressions refer to the same declaration. |
| 5732 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { |
| 5733 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) |
| 5734 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) |
| 5735 | return D1->getDecl() == D2->getDecl(); |
| 5736 | return false; |
| 5737 | } |
| 5738 | |
| 5739 | static const Expr *getStrlenExprArg(const Expr *E) { |
| 5740 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| 5741 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 5742 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5743 | return nullptr; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5744 | return CE->getArg(0)->IgnoreParenCasts(); |
| 5745 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5746 | return nullptr; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | // Warn on anti-patterns as the 'size' argument to strncat. |
| 5750 | // The correct size argument should look like following: |
| 5751 | // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); |
| 5752 | void Sema::CheckStrncatArguments(const CallExpr *CE, |
| 5753 | IdentifierInfo *FnName) { |
| 5754 | // Don't crash if the user has the wrong number of arguments. |
| 5755 | if (CE->getNumArgs() < 3) |
| 5756 | return; |
| 5757 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); |
| 5758 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); |
| 5759 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); |
| 5760 | |
Nico Weber | 0e6daef | 2013-12-26 23:38:39 +0000 | [diff] [blame] | 5761 | if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(), |
| 5762 | CE->getRParenLoc())) |
| 5763 | return; |
| 5764 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5765 | // Identify common expressions, which are wrongly used as the size argument |
| 5766 | // to strncat and may lead to buffer overflows. |
| 5767 | unsigned PatternType = 0; |
| 5768 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { |
| 5769 | // - sizeof(dst) |
| 5770 | if (referToTheSameDecl(SizeOfArg, DstArg)) |
| 5771 | PatternType = 1; |
| 5772 | // - sizeof(src) |
| 5773 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) |
| 5774 | PatternType = 2; |
| 5775 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { |
| 5776 | if (BE->getOpcode() == BO_Sub) { |
| 5777 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); |
| 5778 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); |
| 5779 | // - sizeof(dst) - strlen(dst) |
| 5780 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && |
| 5781 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) |
| 5782 | PatternType = 1; |
| 5783 | // - sizeof(src) - (anything) |
| 5784 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) |
| 5785 | PatternType = 2; |
| 5786 | } |
| 5787 | } |
| 5788 | |
| 5789 | if (PatternType == 0) |
| 5790 | return; |
| 5791 | |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5792 | // Generate the diagnostic. |
| 5793 | SourceLocation SL = LenArg->getLocStart(); |
| 5794 | SourceRange SR = LenArg->getSourceRange(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5795 | SourceManager &SM = getSourceManager(); |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5796 | |
| 5797 | // If the function is defined as a builtin macro, do not show macro expansion. |
| 5798 | if (SM.isMacroArgExpansion(SL)) { |
| 5799 | SL = SM.getSpellingLoc(SL); |
| 5800 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), |
| 5801 | SM.getSpellingLoc(SR.getEnd())); |
| 5802 | } |
| 5803 | |
Anna Zaks | 13b0857 | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 5804 | // Check if the destination is an array (rather than a pointer to an array). |
| 5805 | QualType DstTy = DstArg->getType(); |
| 5806 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, |
| 5807 | Context); |
| 5808 | if (!isKnownSizeArray) { |
| 5809 | if (PatternType == 1) |
| 5810 | Diag(SL, diag::warn_strncat_wrong_size) << SR; |
| 5811 | else |
| 5812 | Diag(SL, diag::warn_strncat_src_size) << SR; |
| 5813 | return; |
| 5814 | } |
| 5815 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5816 | if (PatternType == 1) |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5817 | Diag(SL, diag::warn_strncat_large_size) << SR; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5818 | else |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5819 | Diag(SL, diag::warn_strncat_src_size) << SR; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5820 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5821 | SmallString<128> sizeString; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5822 | llvm::raw_svector_ostream OS(sizeString); |
| 5823 | OS << "sizeof("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5824 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5825 | OS << ") - "; |
| 5826 | OS << "strlen("; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5827 | DstArg->printPretty(OS, nullptr, getPrintingPolicy()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5828 | OS << ") - 1"; |
| 5829 | |
Anna Zaks | 5069aa3 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 5830 | Diag(SL, diag::note_strncat_wrong_size) |
| 5831 | << FixItHint::CreateReplacement(SR, OS.str()); |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5834 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 5835 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5836 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5837 | Decl *ParentDecl); |
| 5838 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5839 | Decl *ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5840 | |
| 5841 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 5842 | /// of a stack variable. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5843 | static void |
| 5844 | CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType, |
| 5845 | SourceLocation ReturnLoc) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5846 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5847 | Expr *stackE = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5848 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5849 | |
| 5850 | // Perform checking for returned stack addresses, local blocks, |
| 5851 | // label addresses or references to temporaries. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5852 | if (lhsType->isPointerType() || |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5853 | (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5854 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5855 | } else if (lhsType->isReferenceType()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5856 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5857 | } |
| 5858 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5859 | if (!stackE) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5860 | return; // Nothing suspicious was found. |
| 5861 | |
| 5862 | SourceLocation diagLoc; |
| 5863 | SourceRange diagRange; |
| 5864 | if (refVars.empty()) { |
| 5865 | diagLoc = stackE->getLocStart(); |
| 5866 | diagRange = stackE->getSourceRange(); |
| 5867 | } else { |
| 5868 | // We followed through a reference variable. 'stackE' contains the |
| 5869 | // problematic expression but we will warn at the return statement pointing |
| 5870 | // at the reference variable. We will later display the "trail" of |
| 5871 | // reference variables using notes. |
| 5872 | diagLoc = refVars[0]->getLocStart(); |
| 5873 | diagRange = refVars[0]->getSourceRange(); |
| 5874 | } |
| 5875 | |
| 5876 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
Craig Topper | da7b27f | 2015-11-17 05:40:09 +0000 | [diff] [blame] | 5877 | S.Diag(diagLoc, diag::warn_ret_stack_addr_ref) << lhsType->isReferenceType() |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5878 | << DR->getDecl()->getDeclName() << diagRange; |
| 5879 | } else if (isa<BlockExpr>(stackE)) { // local block. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5880 | S.Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5881 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5882 | S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5883 | } else { // local temporary. |
Craig Topper | da7b27f | 2015-11-17 05:40:09 +0000 | [diff] [blame] | 5884 | S.Diag(diagLoc, diag::warn_ret_local_temp_addr_ref) |
| 5885 | << lhsType->isReferenceType() << diagRange; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | // Display the "trail" of reference variables that we followed until we |
| 5889 | // found the problematic expression using notes. |
| 5890 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 5891 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 5892 | // If this var binds to another reference var, show the range of the next |
| 5893 | // var, otherwise the var binds to the problematic expression, in which case |
| 5894 | // show the range of the expression. |
| 5895 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 5896 | : stackE->getSourceRange(); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 5897 | S.Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 5898 | << VD->getDeclName() << range; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 5903 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5904 | /// to a location on the stack, a local block, an address of a label, or a |
| 5905 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5906 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5907 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 5908 | /// above problematic expressions (2) is something we cannot determine leads to |
| 5909 | /// a problematic expression based on such local checking. |
| 5910 | /// |
| 5911 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 5912 | /// the expression that they point to. Such variables are added to the |
| 5913 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5914 | /// |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 5915 | /// EvalAddr processes expressions that are pointers that are used as |
| 5916 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5917 | /// At the base case of the recursion is a check for the above problematic |
| 5918 | /// expressions. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5919 | /// |
| 5920 | /// This implementation handles: |
| 5921 | /// |
| 5922 | /// * pointer-to-pointer casts |
| 5923 | /// * implicit conversions from array references to pointers |
| 5924 | /// * taking the address of fields |
| 5925 | /// * arbitrary interplay between "&" and "*" operators |
| 5926 | /// * pointer arithmetic from an address of a stack variable |
| 5927 | /// * 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] | 5928 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 5929 | Decl *ParentDecl) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5930 | if (E->isTypeDependent()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5931 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5932 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5933 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5934 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5935 | E->getType()->isBlockPointerType() || |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5936 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5937 | "EvalAddr only works on pointers"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5938 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5939 | E = E->IgnoreParens(); |
| 5940 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5941 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 5942 | // viewed AST node. We then recursively traverse the AST by calling |
| 5943 | // EvalAddr and EvalVal appropriately. |
| 5944 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5945 | case Stmt::DeclRefExprClass: { |
| 5946 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 5947 | |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 5948 | // 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] | 5949 | if (DR->refersToEnclosingVariableOrCapture()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5950 | return nullptr; |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 5951 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5952 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 5953 | // If this is a reference variable, follow through to the expression that |
| 5954 | // it points to. |
| 5955 | if (V->hasLocalStorage() && |
| 5956 | V->getType()->isReferenceType() && V->hasInit()) { |
| 5957 | // Add the reference variable to the "trail". |
| 5958 | refVars.push_back(DR); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5959 | return EvalAddr(V->getInit(), refVars, ParentDecl); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5962 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 5963 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5964 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5965 | case Stmt::UnaryOperatorClass: { |
| 5966 | // The only unary operator that make sense to handle here |
| 5967 | // is AddrOf. All others don't make sense as pointers. |
| 5968 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5969 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5970 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5971 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5972 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5973 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 5974 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5975 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5976 | case Stmt::BinaryOperatorClass: { |
| 5977 | // Handle pointer arithmetic. All other binary operators are not valid |
| 5978 | // in this context. |
| 5979 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5980 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5981 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5982 | if (op != BO_Add && op != BO_Sub) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5983 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5984 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5985 | Expr *Base = B->getLHS(); |
| 5986 | |
| 5987 | // Determine which argument is the real pointer base. It could be |
| 5988 | // the RHS argument instead of the LHS. |
| 5989 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5990 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5991 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 5992 | return EvalAddr(Base, refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5993 | } |
Steve Naroff | 2752a17 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 5994 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 5995 | // For conditional operators we need to see if either the LHS or RHS are |
| 5996 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 5997 | case Stmt::ConditionalOperatorClass: { |
| 5998 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5999 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 6000 | // Handle the GNU extension for missing LHS. |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 6001 | // FIXME: That isn't a ConditionalOperator, so doesn't get here. |
| 6002 | if (Expr *LHSExpr = C->getLHS()) { |
| 6003 | // In C++, we can have a throw-expression, which has 'void' type. |
| 6004 | if (!LHSExpr->getType()->isVoidType()) |
| 6005 | if (Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl)) |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 6006 | return LHS; |
| 6007 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 6008 | |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 6009 | // In C++, we can have a throw-expression, which has 'void' type. |
| 6010 | if (C->getRHS()->getType()->isVoidType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6011 | return nullptr; |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 6012 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6013 | return EvalAddr(C->getRHS(), refVars, ParentDecl); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 6014 | } |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 6015 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6016 | case Stmt::BlockExprClass: |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 6017 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6018 | return E; // local block. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6019 | return nullptr; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6020 | |
| 6021 | case Stmt::AddrLabelExprClass: |
| 6022 | return E; // address of label. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6023 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6024 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6025 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, |
| 6026 | ParentDecl); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6027 | |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 6028 | // For casts, we need to handle conversions from arrays to |
| 6029 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 6030 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 6031 | case Stmt::CStyleCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6032 | case Stmt::CXXFunctionalCastExprClass: |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6033 | case Stmt::ObjCBridgedCastExprClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6034 | case Stmt::CXXStaticCastExprClass: |
| 6035 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 6036 | case Stmt::CXXConstCastExprClass: |
| 6037 | case Stmt::CXXReinterpretCastExprClass: { |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6038 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 6039 | switch (cast<CastExpr>(E)->getCastKind()) { |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6040 | case CK_LValueToRValue: |
| 6041 | case CK_NoOp: |
| 6042 | case CK_BaseToDerived: |
| 6043 | case CK_DerivedToBase: |
| 6044 | case CK_UncheckedDerivedToBase: |
| 6045 | case CK_Dynamic: |
| 6046 | case CK_CPointerToObjCPointerCast: |
| 6047 | case CK_BlockPointerToObjCPointerCast: |
| 6048 | case CK_AnyPointerToBlockPointerCast: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6049 | return EvalAddr(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6050 | |
| 6051 | case CK_ArrayToPointerDecay: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6052 | return EvalVal(SubExpr, refVars, ParentDecl); |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6053 | |
Richard Trieu | dadefde | 2014-07-02 04:39:38 +0000 | [diff] [blame] | 6054 | case CK_BitCast: |
| 6055 | if (SubExpr->getType()->isAnyPointerType() || |
| 6056 | SubExpr->getType()->isBlockPointerType() || |
| 6057 | SubExpr->getType()->isObjCQualifiedIdType()) |
| 6058 | return EvalAddr(SubExpr, refVars, ParentDecl); |
| 6059 | else |
| 6060 | return nullptr; |
| 6061 | |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6062 | default: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6063 | return nullptr; |
Eli Friedman | 8195ad7 | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 6064 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 6065 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6066 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6067 | case Stmt::MaterializeTemporaryExprClass: |
| 6068 | if (Expr *Result = EvalAddr( |
| 6069 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6070 | refVars, ParentDecl)) |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6071 | return Result; |
| 6072 | |
| 6073 | return E; |
| 6074 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 6075 | // Everything else: we simply don't reason about them. |
| 6076 | default: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6077 | return nullptr; |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 6078 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6079 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6080 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6081 | |
| 6082 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 6083 | /// See the comments for EvalAddr for more details. |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6084 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, |
| 6085 | Decl *ParentDecl) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 6086 | do { |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 6087 | // We should only be called for evaluating non-pointer expressions, or |
| 6088 | // expressions with a pointer type that are not used as references but instead |
| 6089 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6090 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6091 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 6092 | // viewed AST node. We then recursively traverse the AST by calling |
| 6093 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 6094 | |
| 6095 | E = E->IgnoreParens(); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6096 | switch (E->getStmtClass()) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 6097 | case Stmt::ImplicitCastExprClass: { |
| 6098 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6099 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 6100 | E = IE->getSubExpr(); |
| 6101 | continue; |
| 6102 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6103 | return nullptr; |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6106 | case Stmt::ExprWithCleanupsClass: |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6107 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); |
John McCall | 28fc709 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 6108 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6109 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6110 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 6111 | // variable's name. If it's not a reference variable we check if it has |
| 6112 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6113 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6114 | |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 6115 | // 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] | 6116 | if (DR->refersToEnclosingVariableOrCapture()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6117 | return nullptr; |
Richard Smith | 40f08eb | 2014-01-30 22:05:38 +0000 | [diff] [blame] | 6118 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6119 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { |
| 6120 | // Check if it refers to itself, e.g. "int& i = i;". |
| 6121 | if (V == ParentDecl) |
| 6122 | return DR; |
| 6123 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6124 | if (V->hasLocalStorage()) { |
| 6125 | if (!V->getType()->isReferenceType()) |
| 6126 | return DR; |
| 6127 | |
| 6128 | // Reference variable, follow through to the expression that |
| 6129 | // it points to. |
| 6130 | if (V->hasInit()) { |
| 6131 | // Add the reference variable to the "trail". |
| 6132 | refVars.push_back(DR); |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6133 | return EvalVal(V->getInit(), refVars, V); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6134 | } |
| 6135 | } |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6137 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6138 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6139 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6140 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6141 | case Stmt::UnaryOperatorClass: { |
| 6142 | // The only unary operator that make sense to handle here |
| 6143 | // is Deref. All others don't resolve to a "name." This includes |
| 6144 | // handling all sorts of rvalues passed to a unary operator. |
| 6145 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6146 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6147 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6148 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6149 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6150 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6151 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6152 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6153 | case Stmt::ArraySubscriptExprClass: { |
| 6154 | // Array subscripts are potential references to data on the stack. We |
| 6155 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 6156 | // has local storage. |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6157 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6158 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 6160 | case Stmt::OMPArraySectionExprClass: { |
| 6161 | return EvalAddr(cast<OMPArraySectionExpr>(E)->getBase(), refVars, |
| 6162 | ParentDecl); |
| 6163 | } |
| 6164 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6165 | case Stmt::ConditionalOperatorClass: { |
| 6166 | // 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] | 6167 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6168 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 6169 | |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 6170 | // Handle the GNU extension for missing LHS. |
Richard Smith | 6a6a4bb | 2014-01-27 04:19:56 +0000 | [diff] [blame] | 6171 | if (Expr *LHSExpr = C->getLHS()) { |
| 6172 | // In C++, we can have a throw-expression, which has 'void' type. |
| 6173 | if (!LHSExpr->getType()->isVoidType()) |
| 6174 | if (Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl)) |
| 6175 | return LHS; |
| 6176 | } |
| 6177 | |
| 6178 | // In C++, we can have a throw-expression, which has 'void' type. |
| 6179 | if (C->getRHS()->getType()->isVoidType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6180 | return nullptr; |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 6181 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6182 | return EvalVal(C->getRHS(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6183 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6184 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6185 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 6186 | case Stmt::MemberExprClass: { |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6187 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6188 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6189 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 6190 | if (M->isArrow()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6191 | return nullptr; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 6192 | |
| 6193 | // Check whether the member type is itself a reference, in which case |
| 6194 | // we're not going to refer to the member, but to what the member refers to. |
| 6195 | if (M->getMemberDecl()->getType()->isReferenceType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6196 | return nullptr; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 6197 | |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6198 | return EvalVal(M->getBase(), refVars, ParentDecl); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6199 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6200 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6201 | case Stmt::MaterializeTemporaryExprClass: |
| 6202 | if (Expr *Result = EvalVal( |
| 6203 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
Argyrios Kyrtzidis | b4015e1 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 6204 | refVars, ParentDecl)) |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6205 | return Result; |
| 6206 | |
| 6207 | return E; |
| 6208 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6209 | default: |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 6210 | // Check that we don't return or take the address of a reference to a |
| 6211 | // temporary. This is only useful in C++. |
| 6212 | if (!E->isTypeDependent() && E->isRValue()) |
| 6213 | return E; |
| 6214 | |
| 6215 | // Everything else: we simply don't reason about them. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6216 | return nullptr; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6217 | } |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 6218 | } while (true); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 6219 | } |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 6220 | |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 6221 | void |
| 6222 | Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, |
| 6223 | SourceLocation ReturnLoc, |
| 6224 | bool isObjCMethod, |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 6225 | const AttrVec *Attrs, |
| 6226 | const FunctionDecl *FD) { |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 6227 | CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc); |
| 6228 | |
| 6229 | // Check if the return value is null but should not be. |
Douglas Gregor | b4866e8 | 2015-06-19 18:13:19 +0000 | [diff] [blame] | 6230 | if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || |
| 6231 | (!isObjCMethod && isNonNullType(Context, lhsType))) && |
Benjamin Kramer | ae852a6 | 2014-02-23 14:34:50 +0000 | [diff] [blame] | 6232 | CheckNonNullExpr(*this, RetValExp)) |
| 6233 | Diag(ReturnLoc, diag::warn_null_ret) |
| 6234 | << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 6235 | |
| 6236 | // C++11 [basic.stc.dynamic.allocation]p4: |
| 6237 | // If an allocation function declared with a non-throwing |
| 6238 | // exception-specification fails to allocate storage, it shall return |
| 6239 | // a null pointer. Any other allocation function that fails to allocate |
| 6240 | // storage shall indicate failure only by throwing an exception [...] |
| 6241 | if (FD) { |
| 6242 | OverloadedOperatorKind Op = FD->getOverloadedOperator(); |
| 6243 | if (Op == OO_New || Op == OO_Array_New) { |
| 6244 | const FunctionProtoType *Proto |
| 6245 | = FD->getType()->castAs<FunctionProtoType>(); |
| 6246 | if (!Proto->isNothrow(Context, /*ResultIfDependent*/true) && |
| 6247 | CheckNonNullExpr(*this, RetValExp)) |
| 6248 | Diag(ReturnLoc, diag::warn_operator_new_returns_null) |
| 6249 | << FD << getLangOpts().CPlusPlus11; |
| 6250 | } |
| 6251 | } |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 6254 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 6255 | |
| 6256 | /// Check for comparisons of floating point operands using != and ==. |
| 6257 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 6258 | /// to do what the programmer intended. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6259 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6260 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 6261 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Special case: check for x == x (which is OK). |
| 6264 | // Do not emit warnings for such cases. |
| 6265 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 6266 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 6267 | if (DRL->getDecl() == DRR->getDecl()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6268 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | |
| 6270 | |
Ted Kremenek | eda40e2 | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 6271 | // Special case: check for comparisons against literals that can be exactly |
| 6272 | // represented by APFloat. In such cases, do not emit a warning. This |
| 6273 | // is a heuristic: often comparison against such literals are used to |
| 6274 | // detect if a value in a variable has not changed. This clearly can |
| 6275 | // lead to false negatives. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6276 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 6277 | if (FLL->isExact()) |
| 6278 | return; |
| 6279 | } else |
| 6280 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) |
| 6281 | if (FLR->isExact()) |
| 6282 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 6284 | // Check for comparisons with builtin types. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6285 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 6286 | if (CL->getBuiltinCallee()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6287 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6288 | |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6289 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 6290 | if (CR->getBuiltinCallee()) |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6291 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6292 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 6293 | // Emit the diagnostic. |
David Blaikie | 1f4ff15 | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 6294 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 6295 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 6296 | } |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6297 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6298 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 6299 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6300 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6301 | namespace { |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6302 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6303 | /// Structure recording the 'active' range of an integer-valued |
| 6304 | /// expression. |
| 6305 | struct IntRange { |
| 6306 | /// The number of bits active in the int. |
| 6307 | unsigned Width; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6308 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6309 | /// True if the int is known not to have negative values. |
| 6310 | bool NonNegative; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6311 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6312 | IntRange(unsigned Width, bool NonNegative) |
| 6313 | : Width(Width), NonNegative(NonNegative) |
| 6314 | {} |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6315 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6316 | /// Returns the range of the bool type. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6317 | static IntRange forBoolType() { |
| 6318 | return IntRange(1, true); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6321 | /// Returns the range of an opaque value of the given integral type. |
| 6322 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 6323 | return forValueOfCanonicalType(C, |
| 6324 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6327 | /// Returns the range of an opaque value of a canonical integral type. |
| 6328 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6329 | assert(T->isCanonicalUnqualified()); |
| 6330 | |
| 6331 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 6332 | T = VT->getElementType().getTypePtr(); |
| 6333 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 6334 | T = CT->getElementType().getTypePtr(); |
Justin Bogner | 4f42fc4 | 2014-07-21 18:01:53 +0000 | [diff] [blame] | 6335 | if (const AtomicType *AT = dyn_cast<AtomicType>(T)) |
| 6336 | T = AT->getValueType().getTypePtr(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6337 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 6338 | // For enum types, use the known bit width of the enumerators. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6339 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 6340 | EnumDecl *Enum = ET->getDecl(); |
| 6341 | if (!Enum->isCompleteDefinition()) |
| 6342 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 6343 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 6344 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 6345 | unsigned NumNegative = Enum->getNumNegativeBits(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6346 | |
David Majnemer | 6a42665 | 2013-06-07 22:07:20 +0000 | [diff] [blame] | 6347 | if (NumNegative == 0) |
| 6348 | return IntRange(NumPositive, true/*NonNegative*/); |
| 6349 | else |
| 6350 | return IntRange(std::max(NumPositive + 1, NumNegative), |
| 6351 | false/*NonNegative*/); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6352 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6353 | |
| 6354 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 6355 | assert(BT->isInteger()); |
| 6356 | |
| 6357 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 6358 | } |
| 6359 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6360 | /// Returns the "target" range of a canonical integral type, i.e. |
| 6361 | /// the range of values expressible in the type. |
| 6362 | /// |
| 6363 | /// This matches forValueOfCanonicalType except that enums have the |
| 6364 | /// full range of their type, not the range of their enumerators. |
| 6365 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 6366 | assert(T->isCanonicalUnqualified()); |
| 6367 | |
| 6368 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 6369 | T = VT->getElementType().getTypePtr(); |
| 6370 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 6371 | T = CT->getElementType().getTypePtr(); |
Justin Bogner | 4f42fc4 | 2014-07-21 18:01:53 +0000 | [diff] [blame] | 6372 | if (const AtomicType *AT = dyn_cast<AtomicType>(T)) |
| 6373 | T = AT->getValueType().getTypePtr(); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6374 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 3168dcf | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 6375 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6376 | |
| 6377 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 6378 | assert(BT->isInteger()); |
| 6379 | |
| 6380 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 6381 | } |
| 6382 | |
| 6383 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 6384 | static IntRange join(IntRange L, IntRange R) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6385 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6386 | L.NonNegative && R.NonNegative); |
| 6387 | } |
| 6388 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 6389 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 6390 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6391 | return IntRange(std::min(L.Width, R.Width), |
| 6392 | L.NonNegative || R.NonNegative); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6393 | } |
| 6394 | }; |
| 6395 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6396 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, |
| 6397 | unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6398 | if (value.isSigned() && value.isNegative()) |
| 6399 | return IntRange(value.getMinSignedBits(), false); |
| 6400 | |
| 6401 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6402 | value = value.trunc(MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6403 | |
| 6404 | // isNonNegative() just checks the sign bit without considering |
| 6405 | // signedness. |
| 6406 | return IntRange(value.getActiveBits(), true); |
| 6407 | } |
| 6408 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6409 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
| 6410 | unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6411 | if (result.isInt()) |
| 6412 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 6413 | |
| 6414 | if (result.isVector()) { |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 6415 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 6416 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 6417 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 6418 | R = IntRange::join(R, El); |
| 6419 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6420 | return R; |
| 6421 | } |
| 6422 | |
| 6423 | if (result.isComplexInt()) { |
| 6424 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 6425 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 6426 | return IntRange::join(R, I); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 6430 | // Assume it might use arbitrary bits. |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 6431 | // FIXME: The only reason we need to pass the type in here is to get |
| 6432 | // the sign right on this one case. It would be nice if APValue |
| 6433 | // preserved this. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6434 | assert(result.isLValue() || result.isAddrLabelDiff()); |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 6435 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6436 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6437 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6438 | static QualType GetExprType(const Expr *E) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6439 | QualType Ty = E->getType(); |
| 6440 | if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) |
| 6441 | Ty = AtomicRHS->getValueType(); |
| 6442 | return Ty; |
| 6443 | } |
| 6444 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6445 | /// Pseudo-evaluate the given integer expression, estimating the |
| 6446 | /// range of values it might take. |
| 6447 | /// |
| 6448 | /// \param MaxWidth - the width to which the value will be truncated |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6449 | static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6450 | E = E->IgnoreParens(); |
| 6451 | |
| 6452 | // Try a full evaluation first. |
| 6453 | Expr::EvalResult result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6454 | if (E->EvaluateAsRValue(result, C)) |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6455 | return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6456 | |
| 6457 | // I think we only want to look through implicit casts here; if the |
| 6458 | // user has an explicit widening cast, we should treat the value as |
| 6459 | // being of the new, wider type. |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6460 | if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) { |
Eli Friedman | 8349dc1 | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 6461 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6462 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 6463 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6464 | IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6465 | |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 6466 | bool isIntegerCast = CE->getCastKind() == CK_IntegralCast || |
| 6467 | CE->getCastKind() == CK_BooleanToSignedIntegral; |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6468 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6469 | // 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] | 6470 | if (!isIntegerCast) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6471 | return OutputTypeRange; |
| 6472 | |
| 6473 | IntRange SubRange |
| 6474 | = GetExprRange(C, CE->getSubExpr(), |
| 6475 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 6476 | |
| 6477 | // Bail out if the subexpr's range is as wide as the cast type. |
| 6478 | if (SubRange.Width >= OutputTypeRange.Width) |
| 6479 | return OutputTypeRange; |
| 6480 | |
| 6481 | // Otherwise, we take the smaller width, and we're non-negative if |
| 6482 | // either the output type or the subexpr is. |
| 6483 | return IntRange(SubRange.Width, |
| 6484 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 6485 | } |
| 6486 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6487 | if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6488 | // If we can fold the condition, just take that operand. |
| 6489 | bool CondResult; |
| 6490 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 6491 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 6492 | : CO->getFalseExpr(), |
| 6493 | MaxWidth); |
| 6494 | |
| 6495 | // Otherwise, conservatively merge. |
| 6496 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 6497 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 6498 | return IntRange::join(L, R); |
| 6499 | } |
| 6500 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6501 | if (const auto *BO = dyn_cast<BinaryOperator>(E)) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6502 | switch (BO->getOpcode()) { |
| 6503 | |
| 6504 | // Boolean-valued operations are single-bit and positive. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6505 | case BO_LAnd: |
| 6506 | case BO_LOr: |
| 6507 | case BO_LT: |
| 6508 | case BO_GT: |
| 6509 | case BO_LE: |
| 6510 | case BO_GE: |
| 6511 | case BO_EQ: |
| 6512 | case BO_NE: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6513 | return IntRange::forBoolType(); |
| 6514 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 6515 | // The type of the assignments is the type of the LHS, so the RHS |
| 6516 | // is not necessarily the same type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6517 | case BO_MulAssign: |
| 6518 | case BO_DivAssign: |
| 6519 | case BO_RemAssign: |
| 6520 | case BO_AddAssign: |
| 6521 | case BO_SubAssign: |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 6522 | case BO_XorAssign: |
| 6523 | case BO_OrAssign: |
| 6524 | // TODO: bitfields? |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6525 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 6526 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 6527 | // Simple assignments just pass through the RHS, which will have |
| 6528 | // been coerced to the LHS type. |
| 6529 | case BO_Assign: |
| 6530 | // TODO: bitfields? |
| 6531 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 6532 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6533 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6534 | case BO_PtrMemD: |
| 6535 | case BO_PtrMemI: |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6536 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6537 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6538 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6539 | case BO_And: |
| 6540 | case BO_AndAssign: |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6541 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 6542 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 6543 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6544 | // Left shift gets black-listed based on a judgement call. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6545 | case BO_Shl: |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 6546 | // ...except that we want to treat '1 << (blah)' as logically |
| 6547 | // positive. It's an important idiom. |
| 6548 | if (IntegerLiteral *I |
| 6549 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 6550 | if (I->getValue() == 1) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6551 | IntRange R = IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 6552 | return IntRange(R.Width, /*NonNegative*/ true); |
| 6553 | } |
| 6554 | } |
| 6555 | // fallthrough |
| 6556 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6557 | case BO_ShlAssign: |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6558 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6559 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6560 | // Right shift by a constant can narrow its left argument. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6561 | case BO_Shr: |
| 6562 | case BO_ShrAssign: { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6563 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 6564 | |
| 6565 | // If the shift amount is a positive constant, drop the width by |
| 6566 | // that much. |
| 6567 | llvm::APSInt shift; |
| 6568 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 6569 | shift.isNonNegative()) { |
| 6570 | unsigned zext = shift.getZExtValue(); |
| 6571 | if (zext >= L.Width) |
| 6572 | L.Width = (L.NonNegative ? 0 : 1); |
| 6573 | else |
| 6574 | L.Width -= zext; |
| 6575 | } |
| 6576 | |
| 6577 | return L; |
| 6578 | } |
| 6579 | |
| 6580 | // Comma acts as its right operand. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6581 | case BO_Comma: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6582 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 6583 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 6584 | // Black-list pointer subtractions. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6585 | case BO_Sub: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6586 | if (BO->getLHS()->getType()->isPointerType()) |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6587 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 6588 | break; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 6589 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 6590 | // The width of a division result is mostly determined by the size |
| 6591 | // of the LHS. |
| 6592 | case BO_Div: { |
| 6593 | // Don't 'pre-truncate' the operands. |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6594 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 6595 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 6596 | |
| 6597 | // If the divisor is constant, use that. |
| 6598 | llvm::APSInt divisor; |
| 6599 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 6600 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 6601 | if (log2 >= L.Width) |
| 6602 | L.Width = (L.NonNegative ? 0 : 1); |
| 6603 | else |
| 6604 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 6605 | return L; |
| 6606 | } |
| 6607 | |
| 6608 | // Otherwise, just use the LHS's width. |
| 6609 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 6610 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 6611 | } |
| 6612 | |
| 6613 | // The result of a remainder can't be larger than the result of |
| 6614 | // either side. |
| 6615 | case BO_Rem: { |
| 6616 | // Don't 'pre-truncate' the operands. |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6617 | unsigned opWidth = C.getIntWidth(GetExprType(E)); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 6618 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 6619 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 6620 | |
| 6621 | IntRange meet = IntRange::meet(L, R); |
| 6622 | meet.Width = std::min(meet.Width, MaxWidth); |
| 6623 | return meet; |
| 6624 | } |
| 6625 | |
| 6626 | // The default behavior is okay for these. |
| 6627 | case BO_Mul: |
| 6628 | case BO_Add: |
| 6629 | case BO_Xor: |
| 6630 | case BO_Or: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6631 | break; |
| 6632 | } |
| 6633 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 6634 | // The default case is to treat the operation as if it were closed |
| 6635 | // on the narrowest type that encompasses both operands. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6636 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 6637 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 6638 | return IntRange::join(L, R); |
| 6639 | } |
| 6640 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6641 | if (const auto *UO = dyn_cast<UnaryOperator>(E)) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6642 | switch (UO->getOpcode()) { |
| 6643 | // Boolean-valued operations are white-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6644 | case UO_LNot: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6645 | return IntRange::forBoolType(); |
| 6646 | |
| 6647 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6648 | case UO_Deref: |
| 6649 | case UO_AddrOf: // should be impossible |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6650 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6651 | |
| 6652 | default: |
| 6653 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 6654 | } |
| 6655 | } |
| 6656 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6657 | if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Ted Kremenek | a553fbf | 2013-10-14 18:55:27 +0000 | [diff] [blame] | 6658 | return GetExprRange(C, OVE->getSourceExpr(), MaxWidth); |
| 6659 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6660 | if (const auto *BitField = E->getSourceBitField()) |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6661 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 6662 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6663 | |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6664 | return IntRange::forValueOfType(C, GetExprType(E)); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 6665 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6666 | |
Daniel Marjamaki | d3e1ded | 2016-01-25 09:29:38 +0000 | [diff] [blame] | 6667 | static IntRange GetExprRange(ASTContext &C, const Expr *E) { |
Eli Friedman | e6d3395 | 2013-07-08 20:20:06 +0000 | [diff] [blame] | 6668 | return GetExprRange(C, E, C.getIntWidth(GetExprType(E))); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6669 | } |
| 6670 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6671 | /// Checks whether the given value, which currently has the given |
| 6672 | /// source semantics, has the same value when coerced through the |
| 6673 | /// target semantics. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6674 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 6675 | const llvm::fltSemantics &Src, |
| 6676 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6677 | llvm::APFloat truncated = value; |
| 6678 | |
| 6679 | bool ignored; |
| 6680 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 6681 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 6682 | |
| 6683 | return truncated.bitwiseIsEqual(value); |
| 6684 | } |
| 6685 | |
| 6686 | /// Checks whether the given value, which currently has the given |
| 6687 | /// source semantics, has the same value when coerced through the |
| 6688 | /// target semantics. |
| 6689 | /// |
| 6690 | /// The value might be a vector of floats (or a complex number). |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6691 | static bool IsSameFloatAfterCast(const APValue &value, |
| 6692 | const llvm::fltSemantics &Src, |
| 6693 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6694 | if (value.isFloat()) |
| 6695 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 6696 | |
| 6697 | if (value.isVector()) { |
| 6698 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 6699 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 6700 | return false; |
| 6701 | return true; |
| 6702 | } |
| 6703 | |
| 6704 | assert(value.isComplexFloat()); |
| 6705 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 6706 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 6707 | } |
| 6708 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6709 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6710 | |
Ted Kremenek | 6274be4 | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 6711 | static bool IsZero(Sema &S, Expr *E) { |
| 6712 | // Suppress cases where we are comparing against an enum constant. |
| 6713 | if (const DeclRefExpr *DR = |
| 6714 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 6715 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 6716 | return false; |
| 6717 | |
| 6718 | // Suppress cases where the '0' value is expanded from a macro. |
| 6719 | if (E->getLocStart().isMacroID()) |
| 6720 | return false; |
| 6721 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6722 | llvm::APSInt Value; |
| 6723 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 6724 | } |
| 6725 | |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6726 | static bool HasEnumType(Expr *E) { |
| 6727 | // Strip off implicit integral promotions. |
| 6728 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 6729 | if (ICE->getCastKind() != CK_IntegralCast && |
| 6730 | ICE->getCastKind() != CK_NoOp) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6731 | break; |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 6732 | E = ICE->getSubExpr(); |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
| 6735 | return E->getType()->isEnumeralType(); |
| 6736 | } |
| 6737 | |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6738 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
Richard Trieu | 3659456 | 2013-11-01 21:47:19 +0000 | [diff] [blame] | 6739 | // Disable warning in template instantiations. |
| 6740 | if (!S.ActiveTemplateInstantiations.empty()) |
| 6741 | return; |
| 6742 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6743 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | b14dbd7 | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 6744 | if (E->isValueDependent()) |
| 6745 | return; |
| 6746 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6747 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6748 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6749 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6750 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6751 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6752 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6753 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6754 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6755 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6756 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6757 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6758 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6759 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6760 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 6761 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6762 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 6763 | } |
| 6764 | } |
| 6765 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6766 | static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6767 | Expr *Constant, Expr *Other, |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6768 | llvm::APSInt Value, |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6769 | bool RhsConstant) { |
Richard Trieu | dd51d74 | 2013-11-01 21:19:43 +0000 | [diff] [blame] | 6770 | // Disable warning in template instantiations. |
| 6771 | if (!S.ActiveTemplateInstantiations.empty()) |
| 6772 | return; |
| 6773 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6774 | // TODO: Investigate using GetExprRange() to get tighter bounds |
| 6775 | // on the bit ranges. |
| 6776 | QualType OtherT = Other->getType(); |
David Majnemer | 7800f1f | 2015-05-23 01:32:17 +0000 | [diff] [blame] | 6777 | if (const auto *AT = OtherT->getAs<AtomicType>()) |
Justin Bogner | 4f42fc4 | 2014-07-21 18:01:53 +0000 | [diff] [blame] | 6778 | OtherT = AT->getValueType(); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6779 | IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); |
| 6780 | unsigned OtherWidth = OtherRange.Width; |
| 6781 | |
| 6782 | bool OtherIsBooleanType = Other->isKnownToHaveBooleanValue(); |
| 6783 | |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6784 | // 0 values are handled later by CheckTrivialUnsignedComparison(). |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6785 | if ((Value == 0) && (!OtherIsBooleanType)) |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6786 | return; |
| 6787 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6788 | BinaryOperatorKind op = E->getOpcode(); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6789 | bool IsTrue = true; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6790 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6791 | // Used for diagnostic printout. |
| 6792 | enum { |
| 6793 | LiteralConstant = 0, |
| 6794 | CXXBoolLiteralTrue, |
| 6795 | CXXBoolLiteralFalse |
| 6796 | } LiteralOrBoolConstant = LiteralConstant; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6797 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6798 | if (!OtherIsBooleanType) { |
| 6799 | QualType ConstantT = Constant->getType(); |
| 6800 | QualType CommonT = E->getLHS()->getType(); |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6801 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6802 | if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT)) |
| 6803 | return; |
| 6804 | assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) && |
| 6805 | "comparison with non-integer type"); |
| 6806 | |
| 6807 | bool ConstantSigned = ConstantT->isSignedIntegerType(); |
| 6808 | bool CommonSigned = CommonT->isSignedIntegerType(); |
| 6809 | |
| 6810 | bool EqualityOnly = false; |
| 6811 | |
| 6812 | if (CommonSigned) { |
| 6813 | // The common type is signed, therefore no signed to unsigned conversion. |
| 6814 | if (!OtherRange.NonNegative) { |
| 6815 | // Check that the constant is representable in type OtherT. |
| 6816 | if (ConstantSigned) { |
| 6817 | if (OtherWidth >= Value.getMinSignedBits()) |
| 6818 | return; |
| 6819 | } else { // !ConstantSigned |
| 6820 | if (OtherWidth >= Value.getActiveBits() + 1) |
| 6821 | return; |
| 6822 | } |
| 6823 | } else { // !OtherSigned |
| 6824 | // Check that the constant is representable in type OtherT. |
| 6825 | // Negative values are out of range. |
| 6826 | if (ConstantSigned) { |
| 6827 | if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits()) |
| 6828 | return; |
| 6829 | } else { // !ConstantSigned |
| 6830 | if (OtherWidth >= Value.getActiveBits()) |
| 6831 | return; |
| 6832 | } |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6833 | } |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6834 | } else { // !CommonSigned |
| 6835 | if (OtherRange.NonNegative) { |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6836 | if (OtherWidth >= Value.getActiveBits()) |
| 6837 | return; |
Craig Topper | cf36016 | 2014-06-18 05:13:11 +0000 | [diff] [blame] | 6838 | } else { // OtherSigned |
| 6839 | assert(!ConstantSigned && |
| 6840 | "Two signed types converted to unsigned types."); |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6841 | // Check to see if the constant is representable in OtherT. |
| 6842 | if (OtherWidth > Value.getActiveBits()) |
| 6843 | return; |
| 6844 | // Check to see if the constant is equivalent to a negative value |
| 6845 | // cast to CommonT. |
| 6846 | if (S.Context.getIntWidth(ConstantT) == |
| 6847 | S.Context.getIntWidth(CommonT) && |
| 6848 | Value.isNegative() && Value.getMinSignedBits() <= OtherWidth) |
| 6849 | return; |
| 6850 | // The constant value rests between values that OtherT can represent |
| 6851 | // after conversion. Relational comparison still works, but equality |
| 6852 | // comparisons will be tautological. |
| 6853 | EqualityOnly = true; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6854 | } |
| 6855 | } |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6856 | |
| 6857 | bool PositiveConstant = !ConstantSigned || Value.isNonNegative(); |
| 6858 | |
| 6859 | if (op == BO_EQ || op == BO_NE) { |
| 6860 | IsTrue = op == BO_NE; |
| 6861 | } else if (EqualityOnly) { |
| 6862 | return; |
| 6863 | } else if (RhsConstant) { |
| 6864 | if (op == BO_GT || op == BO_GE) |
| 6865 | IsTrue = !PositiveConstant; |
| 6866 | else // op == BO_LT || op == BO_LE |
| 6867 | IsTrue = PositiveConstant; |
| 6868 | } else { |
| 6869 | if (op == BO_LT || op == BO_LE) |
| 6870 | IsTrue = !PositiveConstant; |
| 6871 | else // op == BO_GT || op == BO_GE |
| 6872 | IsTrue = PositiveConstant; |
Richard Trieu | 560910c | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 6873 | } |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 6874 | } else { |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6875 | // Other isKnownToHaveBooleanValue |
| 6876 | enum CompareBoolWithConstantResult { AFals, ATrue, Unkwn }; |
| 6877 | enum ConstantValue { LT_Zero, Zero, One, GT_One, SizeOfConstVal }; |
| 6878 | enum ConstantSide { Lhs, Rhs, SizeOfConstSides }; |
| 6879 | |
| 6880 | static const struct LinkedConditions { |
| 6881 | CompareBoolWithConstantResult BO_LT_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6882 | CompareBoolWithConstantResult BO_GT_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6883 | CompareBoolWithConstantResult BO_LE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6884 | CompareBoolWithConstantResult BO_GE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6885 | CompareBoolWithConstantResult BO_EQ_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6886 | CompareBoolWithConstantResult BO_NE_OP[SizeOfConstSides][SizeOfConstVal]; |
| 6887 | |
| 6888 | } TruthTable = { |
| 6889 | // Constant on LHS. | Constant on RHS. | |
| 6890 | // LT_Zero| Zero | One |GT_One| LT_Zero| Zero | One |GT_One| |
| 6891 | { { ATrue, Unkwn, AFals, AFals }, { AFals, AFals, Unkwn, ATrue } }, |
| 6892 | { { AFals, AFals, Unkwn, ATrue }, { ATrue, Unkwn, AFals, AFals } }, |
| 6893 | { { ATrue, ATrue, Unkwn, AFals }, { AFals, Unkwn, ATrue, ATrue } }, |
| 6894 | { { AFals, Unkwn, ATrue, ATrue }, { ATrue, ATrue, Unkwn, AFals } }, |
| 6895 | { { AFals, Unkwn, Unkwn, AFals }, { AFals, Unkwn, Unkwn, AFals } }, |
| 6896 | { { ATrue, Unkwn, Unkwn, ATrue }, { ATrue, Unkwn, Unkwn, ATrue } } |
| 6897 | }; |
| 6898 | |
| 6899 | bool ConstantIsBoolLiteral = isa<CXXBoolLiteralExpr>(Constant); |
| 6900 | |
| 6901 | enum ConstantValue ConstVal = Zero; |
| 6902 | if (Value.isUnsigned() || Value.isNonNegative()) { |
| 6903 | if (Value == 0) { |
| 6904 | LiteralOrBoolConstant = |
| 6905 | ConstantIsBoolLiteral ? CXXBoolLiteralFalse : LiteralConstant; |
| 6906 | ConstVal = Zero; |
| 6907 | } else if (Value == 1) { |
| 6908 | LiteralOrBoolConstant = |
| 6909 | ConstantIsBoolLiteral ? CXXBoolLiteralTrue : LiteralConstant; |
| 6910 | ConstVal = One; |
| 6911 | } else { |
| 6912 | LiteralOrBoolConstant = LiteralConstant; |
| 6913 | ConstVal = GT_One; |
| 6914 | } |
| 6915 | } else { |
| 6916 | ConstVal = LT_Zero; |
| 6917 | } |
| 6918 | |
| 6919 | CompareBoolWithConstantResult CmpRes; |
| 6920 | |
| 6921 | switch (op) { |
| 6922 | case BO_LT: |
| 6923 | CmpRes = TruthTable.BO_LT_OP[RhsConstant][ConstVal]; |
| 6924 | break; |
| 6925 | case BO_GT: |
| 6926 | CmpRes = TruthTable.BO_GT_OP[RhsConstant][ConstVal]; |
| 6927 | break; |
| 6928 | case BO_LE: |
| 6929 | CmpRes = TruthTable.BO_LE_OP[RhsConstant][ConstVal]; |
| 6930 | break; |
| 6931 | case BO_GE: |
| 6932 | CmpRes = TruthTable.BO_GE_OP[RhsConstant][ConstVal]; |
| 6933 | break; |
| 6934 | case BO_EQ: |
| 6935 | CmpRes = TruthTable.BO_EQ_OP[RhsConstant][ConstVal]; |
| 6936 | break; |
| 6937 | case BO_NE: |
| 6938 | CmpRes = TruthTable.BO_NE_OP[RhsConstant][ConstVal]; |
| 6939 | break; |
| 6940 | default: |
| 6941 | CmpRes = Unkwn; |
| 6942 | break; |
| 6943 | } |
| 6944 | |
| 6945 | if (CmpRes == AFals) { |
| 6946 | IsTrue = false; |
| 6947 | } else if (CmpRes == ATrue) { |
| 6948 | IsTrue = true; |
| 6949 | } else { |
| 6950 | return; |
| 6951 | } |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6952 | } |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 6953 | |
| 6954 | // If this is a comparison to an enum constant, include that |
| 6955 | // constant in the diagnostic. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6956 | const EnumConstantDecl *ED = nullptr; |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 6957 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) |
| 6958 | ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 6959 | |
| 6960 | SmallString<64> PrettySourceValue; |
| 6961 | llvm::raw_svector_ostream OS(PrettySourceValue); |
| 6962 | if (ED) |
Ted Kremenek | e943ce1 | 2013-03-15 22:02:46 +0000 | [diff] [blame] | 6963 | OS << '\'' << *ED << "' (" << Value << ")"; |
Ted Kremenek | b7d7dd4 | 2013-03-15 21:50:10 +0000 | [diff] [blame] | 6964 | else |
| 6965 | OS << Value; |
| 6966 | |
Richard Trieu | 0f09774 | 2014-04-04 04:13:47 +0000 | [diff] [blame] | 6967 | S.DiagRuntimeBehavior( |
| 6968 | E->getOperatorLoc(), E, |
| 6969 | S.PDiag(diag::warn_out_of_range_compare) |
| 6970 | << OS.str() << LiteralOrBoolConstant |
| 6971 | << OtherT << (OtherIsBooleanType && !OtherT->isBooleanType()) << IsTrue |
| 6972 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6975 | /// Analyze the operands of the given comparison. Implements the |
| 6976 | /// fallback case from AnalyzeComparison. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6977 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6978 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 6979 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6980 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 6981 | |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6982 | /// \brief Implements -Wsign-compare. |
| 6983 | /// |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 6984 | /// \param E the binary operator to check for warnings |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 6985 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 6986 | // The type the comparison is being performed in. |
| 6987 | QualType T = E->getLHS()->getType(); |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 6988 | |
| 6989 | // Only analyze comparison operators where both sides have been converted to |
| 6990 | // the same type. |
| 6991 | if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) |
| 6992 | return AnalyzeImpConvsInComparison(S, E); |
| 6993 | |
| 6994 | // Don't analyze value-dependent comparisons directly. |
Fariborz Jahanian | 282071e | 2012-09-18 17:46:26 +0000 | [diff] [blame] | 6995 | if (E->isValueDependent()) |
| 6996 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 6997 | |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 6998 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 6999 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 7000 | |
| 7001 | bool IsComparisonConstant = false; |
| 7002 | |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 7003 | // Check whether an integer constant comparison results in a value |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 7004 | // of 'true' or 'false'. |
| 7005 | if (T->isIntegralType(S.Context)) { |
| 7006 | llvm::APSInt RHSValue; |
| 7007 | bool IsRHSIntegralLiteral = |
| 7008 | RHS->isIntegerConstantExpr(RHSValue, S.Context); |
| 7009 | llvm::APSInt LHSValue; |
| 7010 | bool IsLHSIntegralLiteral = |
| 7011 | LHS->isIntegerConstantExpr(LHSValue, S.Context); |
| 7012 | if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral) |
| 7013 | DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true); |
| 7014 | else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral) |
| 7015 | DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false); |
| 7016 | else |
| 7017 | IsComparisonConstant = |
| 7018 | (IsRHSIntegralLiteral && IsLHSIntegralLiteral); |
Fariborz Jahanian | 2f4e33a | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 7019 | } else if (!T->hasUnsignedIntegerRepresentation()) |
| 7020 | IsComparisonConstant = E->isIntegerConstantExpr(S.Context); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 7021 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7022 | // We don't do anything special if this isn't an unsigned integral |
| 7023 | // comparison: we're only interested in integral comparisons, and |
| 7024 | // 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] | 7025 | // |
| 7026 | // We also don't care about value-dependent expressions or expressions |
| 7027 | // whose result is a constant. |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 7028 | if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7029 | return AnalyzeImpConvsInComparison(S, E); |
Fariborz Jahanian | b188542 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 7030 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7031 | // Check to see if one of the (unmodified) operands is of different |
| 7032 | // signedness. |
| 7033 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 7034 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 7035 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7036 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 7037 | signedOperand = LHS; |
| 7038 | unsignedOperand = RHS; |
| 7039 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 7040 | signedOperand = RHS; |
| 7041 | unsignedOperand = LHS; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 7042 | } else { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7043 | CheckTrivialUnsignedComparison(S, E); |
| 7044 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 7045 | } |
| 7046 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7047 | // Otherwise, calculate the effective range of the signed operand. |
| 7048 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 7049 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7050 | // Go ahead and analyze implicit conversions in the operands. Note |
| 7051 | // that we skip the implicit conversions on both sides. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 7052 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 7053 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 7054 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7055 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 7056 | // but we should still check for comparisons which are always true |
| 7057 | // or false. |
| 7058 | if (signedRange.NonNegative) |
| 7059 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 7060 | |
| 7061 | // For (in)equality comparisons, if the unsigned operand is a |
| 7062 | // constant which cannot collide with a overflowed signed operand, |
| 7063 | // then reinterpreting the signed operand as unsigned will not |
| 7064 | // change the result of the comparison. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7065 | if (E->isEqualityOp()) { |
| 7066 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 7067 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 7068 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7069 | // We should never be unable to prove that the unsigned operand is |
| 7070 | // non-negative. |
| 7071 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 7072 | |
| 7073 | if (unsignedRange.Width < comparisonWidth) |
| 7074 | return; |
| 7075 | } |
| 7076 | |
Douglas Gregor | bfb4a21 | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 7077 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, |
| 7078 | S.PDiag(diag::warn_mixed_sign_comparison) |
| 7079 | << LHS->getType() << RHS->getType() |
| 7080 | << LHS->getSourceRange() << RHS->getSourceRange()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 7081 | } |
| 7082 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7083 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 7084 | /// |
| 7085 | /// Returns true if there was something fishy about the attempt. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 7086 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 7087 | SourceLocation InitLoc) { |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7088 | assert(Bitfield->isBitField()); |
| 7089 | if (Bitfield->isInvalidDecl()) |
| 7090 | return false; |
| 7091 | |
John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 7092 | // White-list bool bitfields. |
| 7093 | if (Bitfield->getType()->isBooleanType()) |
| 7094 | return false; |
| 7095 | |
Douglas Gregor | 789adec | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 7096 | // Ignore value- or type-dependent expressions. |
| 7097 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 7098 | Bitfield->getBitWidth()->isTypeDependent() || |
| 7099 | Init->isValueDependent() || |
| 7100 | Init->isTypeDependent()) |
| 7101 | return false; |
| 7102 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7103 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 7104 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7105 | llvm::APSInt Value; |
| 7106 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7107 | return false; |
| 7108 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7109 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7110 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7111 | |
| 7112 | if (OriginalWidth <= FieldWidth) |
| 7113 | return false; |
| 7114 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 7115 | // Compute the value which the bitfield will contain. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 7116 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 7117 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7118 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 7119 | // Check whether the stored value is equal to the original value. |
| 7120 | TruncatedValue = TruncatedValue.extend(OriginalWidth); |
Richard Trieu | c320c74 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 7121 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7122 | return false; |
| 7123 | |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 7124 | // Special-case bitfields of width 1: booleans are naturally 0/1, and |
Eli Friedman | e1ffd49 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 7125 | // therefore don't strictly fit into a signed bitfield of width 1. |
| 7126 | if (FieldWidth == 1 && Value == 1) |
Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 7127 | return false; |
| 7128 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7129 | std::string PrettyValue = Value.toString(10); |
| 7130 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 7131 | |
| 7132 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 7133 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 7134 | << Init->getSourceRange(); |
| 7135 | |
| 7136 | return true; |
| 7137 | } |
| 7138 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 7139 | /// Analyze the given simple or compound assignment for warning-worthy |
| 7140 | /// operations. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 7141 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 7142 | // Just recurse on the LHS. |
| 7143 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 7144 | |
| 7145 | // We want to recurse on the RHS as normal unless we're assigning to |
| 7146 | // a bitfield. |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 7147 | if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7148 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7149 | E->getOperatorLoc())) { |
| 7150 | // Recurse, ignoring any implicit conversions on the RHS. |
| 7151 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 7152 | E->getOperatorLoc()); |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 7153 | } |
| 7154 | } |
| 7155 | |
| 7156 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 7157 | } |
| 7158 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7159 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 7160 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 7161 | SourceLocation CContext, unsigned diag, |
| 7162 | bool pruneControlFlow = false) { |
| 7163 | if (pruneControlFlow) { |
| 7164 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 7165 | S.PDiag(diag) |
| 7166 | << SourceType << T << E->getSourceRange() |
| 7167 | << SourceRange(CContext)); |
| 7168 | return; |
| 7169 | } |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 7170 | S.Diag(E->getExprLoc(), diag) |
| 7171 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 7172 | } |
| 7173 | |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 7174 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Ted Kremenek | 8a92c8b | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 7175 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 7176 | SourceLocation CContext, unsigned diag, |
| 7177 | bool pruneControlFlow = false) { |
| 7178 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 7181 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 7182 | /// cast wouldn't lose information. |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 7183 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 7184 | SourceLocation CContext) { |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 7185 | // 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] | 7186 | bool isExact = false; |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 7187 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | d0f079d | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 7188 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 7189 | T->hasUnsignedIntegerRepresentation()); |
| 7190 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 7191 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 7192 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 7193 | return; |
| 7194 | |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 7195 | // FIXME: Force the precision of the source value down so we don't print |
| 7196 | // digits which are usually useless (we don't really care here if we |
| 7197 | // truncate a digit by accident in edge cases). Ideally, APFloat::toString |
| 7198 | // would automatically print the shortest representation, but it's a bit |
| 7199 | // tricky to implement. |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7200 | SmallString<16> PrettySourceValue; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 7201 | unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); |
| 7202 | precision = (precision * 59 + 195) / 196; |
| 7203 | Value.toString(PrettySourceValue, precision); |
| 7204 | |
David Blaikie | 9b88cc0 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 7205 | SmallString<16> PrettyTargetValue; |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7206 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) |
Aaron Ballman | dbc441e | 2015-12-30 14:26:07 +0000 | [diff] [blame] | 7207 | PrettyTargetValue = Value.isZero() ? "false" : "true"; |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7208 | else |
David Blaikie | 9b88cc0 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 7209 | IntegerValue.toString(PrettyTargetValue); |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7210 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 7211 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7212 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue |
| 7213 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 7216 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 7217 | if (!Range.Width) return "0"; |
| 7218 | |
| 7219 | llvm::APSInt ValueInRange = Value; |
| 7220 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 7221 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 7222 | return ValueInRange.toString(10); |
| 7223 | } |
| 7224 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 7225 | static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { |
| 7226 | if (!isa<ImplicitCastExpr>(Ex)) |
| 7227 | return false; |
| 7228 | |
| 7229 | Expr *InnerE = Ex->IgnoreParenImpCasts(); |
| 7230 | const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); |
| 7231 | const Type *Source = |
| 7232 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); |
| 7233 | if (Target->isDependentType()) |
| 7234 | return false; |
| 7235 | |
| 7236 | const BuiltinType *FloatCandidateBT = |
| 7237 | dyn_cast<BuiltinType>(ToBool ? Source : Target); |
| 7238 | const Type *BoolCandidateType = ToBool ? Target : Source; |
| 7239 | |
| 7240 | return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && |
| 7241 | FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); |
| 7242 | } |
| 7243 | |
| 7244 | void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, |
| 7245 | SourceLocation CC) { |
| 7246 | unsigned NumArgs = TheCall->getNumArgs(); |
| 7247 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 7248 | Expr *CurrA = TheCall->getArg(i); |
| 7249 | if (!IsImplicitBoolFloatConversion(S, CurrA, true)) |
| 7250 | continue; |
| 7251 | |
| 7252 | bool IsSwapped = ((i > 0) && |
| 7253 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); |
| 7254 | IsSwapped |= ((i < (NumArgs - 1)) && |
| 7255 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); |
| 7256 | if (IsSwapped) { |
| 7257 | // Warn on this floating-point to bool conversion. |
| 7258 | DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), |
| 7259 | CurrA->getType(), CC, |
| 7260 | diag::warn_impcast_floating_point_to_bool); |
| 7261 | } |
| 7262 | } |
| 7263 | } |
| 7264 | |
Richard Trieu | 5b99350 | 2014-10-15 03:42:06 +0000 | [diff] [blame] | 7265 | static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, |
| 7266 | SourceLocation CC) { |
| 7267 | if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, |
| 7268 | E->getExprLoc())) |
| 7269 | return; |
| 7270 | |
Richard Trieu | 09d6b80 | 2016-01-08 23:35:06 +0000 | [diff] [blame] | 7271 | // Don't warn on functions which have return type nullptr_t. |
| 7272 | if (isa<CallExpr>(E)) |
| 7273 | return; |
| 7274 | |
Richard Trieu | 5b99350 | 2014-10-15 03:42:06 +0000 | [diff] [blame] | 7275 | // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). |
| 7276 | const Expr::NullPointerConstantKind NullKind = |
| 7277 | E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull); |
| 7278 | if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr) |
| 7279 | return; |
| 7280 | |
| 7281 | // Return if target type is a safe conversion. |
| 7282 | if (T->isAnyPointerType() || T->isBlockPointerType() || |
| 7283 | T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) |
| 7284 | return; |
| 7285 | |
| 7286 | SourceLocation Loc = E->getSourceRange().getBegin(); |
| 7287 | |
| 7288 | // __null is usually wrapped in a macro. Go up a macro if that is the case. |
| 7289 | if (NullKind == Expr::NPCK_GNUNull) { |
Richard Trieu | fc014f2 | 2016-01-09 01:10:17 +0000 | [diff] [blame] | 7290 | if (Loc.isMacroID()) { |
Richard Trieu | 3a5c958 | 2016-01-26 02:51:55 +0000 | [diff] [blame] | 7291 | StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( |
| 7292 | Loc, S.SourceMgr, S.getLangOpts()); |
Richard Trieu | fc014f2 | 2016-01-09 01:10:17 +0000 | [diff] [blame] | 7293 | if (MacroName == "NULL") |
| 7294 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; |
| 7295 | } |
Richard Trieu | 5b99350 | 2014-10-15 03:42:06 +0000 | [diff] [blame] | 7296 | } |
| 7297 | |
| 7298 | // Only warn if the null and context location are in the same macro expansion. |
| 7299 | if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) |
| 7300 | return; |
| 7301 | |
| 7302 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) |
| 7303 | << (NullKind == Expr::NPCK_CXX11_nullptr) << T << clang::SourceRange(CC) |
| 7304 | << FixItHint::CreateReplacement(Loc, |
| 7305 | S.getFixItZeroLiteralForType(T, Loc)); |
| 7306 | } |
| 7307 | |
Douglas Gregor | 5054cb0 | 2015-07-07 03:58:22 +0000 | [diff] [blame] | 7308 | static void checkObjCArrayLiteral(Sema &S, QualType TargetType, |
| 7309 | ObjCArrayLiteral *ArrayLiteral); |
| 7310 | static void checkObjCDictionaryLiteral(Sema &S, QualType TargetType, |
| 7311 | ObjCDictionaryLiteral *DictionaryLiteral); |
| 7312 | |
| 7313 | /// Check a single element within a collection literal against the |
| 7314 | /// target element type. |
| 7315 | static void checkObjCCollectionLiteralElement(Sema &S, |
| 7316 | QualType TargetElementType, |
| 7317 | Expr *Element, |
| 7318 | unsigned ElementKind) { |
| 7319 | // Skip a bitcast to 'id' or qualified 'id'. |
| 7320 | if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) { |
| 7321 | if (ICE->getCastKind() == CK_BitCast && |
| 7322 | ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>()) |
| 7323 | Element = ICE->getSubExpr(); |
| 7324 | } |
| 7325 | |
| 7326 | QualType ElementType = Element->getType(); |
| 7327 | ExprResult ElementResult(Element); |
| 7328 | if (ElementType->getAs<ObjCObjectPointerType>() && |
| 7329 | S.CheckSingleAssignmentConstraints(TargetElementType, |
| 7330 | ElementResult, |
| 7331 | false, false) |
| 7332 | != Sema::Compatible) { |
| 7333 | S.Diag(Element->getLocStart(), |
| 7334 | diag::warn_objc_collection_literal_element) |
| 7335 | << ElementType << ElementKind << TargetElementType |
| 7336 | << Element->getSourceRange(); |
| 7337 | } |
| 7338 | |
| 7339 | if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element)) |
| 7340 | checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral); |
| 7341 | else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element)) |
| 7342 | checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral); |
| 7343 | } |
| 7344 | |
| 7345 | /// Check an Objective-C array literal being converted to the given |
| 7346 | /// target type. |
| 7347 | static void checkObjCArrayLiteral(Sema &S, QualType TargetType, |
| 7348 | ObjCArrayLiteral *ArrayLiteral) { |
| 7349 | if (!S.NSArrayDecl) |
| 7350 | return; |
| 7351 | |
| 7352 | const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>(); |
| 7353 | if (!TargetObjCPtr) |
| 7354 | return; |
| 7355 | |
| 7356 | if (TargetObjCPtr->isUnspecialized() || |
| 7357 | TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl() |
| 7358 | != S.NSArrayDecl->getCanonicalDecl()) |
| 7359 | return; |
| 7360 | |
| 7361 | auto TypeArgs = TargetObjCPtr->getTypeArgs(); |
| 7362 | if (TypeArgs.size() != 1) |
| 7363 | return; |
| 7364 | |
| 7365 | QualType TargetElementType = TypeArgs[0]; |
| 7366 | for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) { |
| 7367 | checkObjCCollectionLiteralElement(S, TargetElementType, |
| 7368 | ArrayLiteral->getElement(I), |
| 7369 | 0); |
| 7370 | } |
| 7371 | } |
| 7372 | |
| 7373 | /// Check an Objective-C dictionary literal being converted to the given |
| 7374 | /// target type. |
| 7375 | static void checkObjCDictionaryLiteral( |
| 7376 | Sema &S, QualType TargetType, |
| 7377 | ObjCDictionaryLiteral *DictionaryLiteral) { |
| 7378 | if (!S.NSDictionaryDecl) |
| 7379 | return; |
| 7380 | |
| 7381 | const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>(); |
| 7382 | if (!TargetObjCPtr) |
| 7383 | return; |
| 7384 | |
| 7385 | if (TargetObjCPtr->isUnspecialized() || |
| 7386 | TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl() |
| 7387 | != S.NSDictionaryDecl->getCanonicalDecl()) |
| 7388 | return; |
| 7389 | |
| 7390 | auto TypeArgs = TargetObjCPtr->getTypeArgs(); |
| 7391 | if (TypeArgs.size() != 2) |
| 7392 | return; |
| 7393 | |
| 7394 | QualType TargetKeyType = TypeArgs[0]; |
| 7395 | QualType TargetObjectType = TypeArgs[1]; |
| 7396 | for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) { |
| 7397 | auto Element = DictionaryLiteral->getKeyValueElement(I); |
| 7398 | checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1); |
| 7399 | checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2); |
| 7400 | } |
| 7401 | } |
| 7402 | |
Richard Trieu | fc404c7 | 2016-02-05 23:02:38 +0000 | [diff] [blame] | 7403 | // Helper function to filter out cases for constant width constant conversion. |
| 7404 | // Don't warn on char array initialization or for non-decimal values. |
| 7405 | static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, |
| 7406 | SourceLocation CC) { |
| 7407 | // If initializing from a constant, and the constant starts with '0', |
| 7408 | // then it is a binary, octal, or hexadecimal. Allow these constants |
| 7409 | // to fill all the bits, even if there is a sign change. |
| 7410 | if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) { |
| 7411 | const char FirstLiteralCharacter = |
| 7412 | S.getSourceManager().getCharacterData(IntLit->getLocStart())[0]; |
| 7413 | if (FirstLiteralCharacter == '0') |
| 7414 | return false; |
| 7415 | } |
| 7416 | |
| 7417 | // If the CC location points to a '{', and the type is char, then assume |
| 7418 | // assume it is an array initialization. |
| 7419 | if (CC.isValid() && T->isCharType()) { |
| 7420 | const char FirstContextCharacter = |
| 7421 | S.getSourceManager().getCharacterData(CC)[0]; |
| 7422 | if (FirstContextCharacter == '{') |
| 7423 | return false; |
| 7424 | } |
| 7425 | |
| 7426 | return true; |
| 7427 | } |
| 7428 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7429 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7430 | SourceLocation CC, bool *ICContext = nullptr) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7431 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7432 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7433 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 7434 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 7435 | if (Source == Target) return; |
| 7436 | if (Target->isDependentType()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7437 | |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 7438 | // If the conversion context location is invalid don't complain. We also |
| 7439 | // don't want to emit a warning if the issue occurs from the expansion of |
| 7440 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 7441 | // delay this check as long as possible. Once we detect we are in that |
| 7442 | // scenario, we just return. |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7443 | if (CC.isInvalid()) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7444 | return; |
| 7445 | |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 7446 | // Diagnose implicit casts to bool. |
| 7447 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 7448 | if (isa<StringLiteral>(E)) |
| 7449 | // Warn on string literal to bool. Checks for string literals in logical |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 7450 | // and expressions, for instance, assert(0 && "error here"), are |
| 7451 | // prevented by a check in AnalyzeImplicitConversions(). |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 7452 | return DiagnoseImpCast(S, E, T, CC, |
| 7453 | diag::warn_impcast_string_literal_to_bool); |
Richard Trieu | 1e632af | 2014-01-28 23:40:26 +0000 | [diff] [blame] | 7454 | if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || |
| 7455 | isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { |
| 7456 | // This covers the literal expressions that evaluate to Objective-C |
| 7457 | // objects. |
| 7458 | return DiagnoseImpCast(S, E, T, CC, |
| 7459 | diag::warn_impcast_objective_c_literal_to_bool); |
| 7460 | } |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7461 | if (Source->isPointerType() || Source->canDecayToPointerType()) { |
| 7462 | // Warn on pointer to bool conversion that is always true. |
| 7463 | S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, |
| 7464 | SourceRange(CC)); |
Lang Hames | df5c121 | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 7465 | } |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 7466 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7467 | |
Douglas Gregor | 5054cb0 | 2015-07-07 03:58:22 +0000 | [diff] [blame] | 7468 | // Check implicit casts from Objective-C collection literals to specialized |
| 7469 | // collection types, e.g., NSArray<NSString *> *. |
| 7470 | if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E)) |
| 7471 | checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral); |
| 7472 | else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) |
| 7473 | checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral); |
| 7474 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7475 | // Strip vector types. |
| 7476 | if (isa<VectorType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7477 | if (!isa<VectorType>(Target)) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 7478 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7479 | return; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7480 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7481 | } |
Chris Lattner | ee7286f | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 7482 | |
| 7483 | // If the vector cast is cast between two vectors of the same size, it is |
| 7484 | // a bitcast, not a conversion. |
| 7485 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 7486 | return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7487 | |
| 7488 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 7489 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 7490 | } |
Stephen Canon | 3ba640d | 2014-04-03 10:33:25 +0000 | [diff] [blame] | 7491 | if (auto VecTy = dyn_cast<VectorType>(Target)) |
| 7492 | Target = VecTy->getElementType().getTypePtr(); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7493 | |
| 7494 | // Strip complex types. |
| 7495 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7496 | if (!isa<ComplexType>(Target)) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 7497 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7498 | return; |
| 7499 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7500 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7501 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7502 | |
| 7503 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 7504 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 7505 | } |
| 7506 | |
| 7507 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 7508 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 7509 | |
| 7510 | // If the source is floating point... |
| 7511 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 7512 | // ...and the target is floating point... |
| 7513 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 7514 | // ...then warn if we're dropping FP rank. |
| 7515 | |
| 7516 | // Builtin FP kinds are ordered by increasing FP rank. |
| 7517 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 7518 | // Don't warn about float constants that are precisely |
| 7519 | // representable in the target type. |
| 7520 | Expr::EvalResult result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7521 | if (E->EvaluateAsRValue(result, S.Context)) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7522 | // Value might be a float, a float vector, or a float complex. |
| 7523 | if (IsSameFloatAfterCast(result.Val, |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7524 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 7525 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7526 | return; |
| 7527 | } |
| 7528 | |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 7529 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7530 | return; |
| 7531 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7532 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
George Burgess IV | 148e0d3 | 2015-10-29 00:28:52 +0000 | [diff] [blame] | 7533 | |
| 7534 | } |
| 7535 | // ... or possibly if we're increasing rank, too |
| 7536 | else if (TargetBT->getKind() > SourceBT->getKind()) { |
| 7537 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 7538 | return; |
| 7539 | |
| 7540 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_double_promotion); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7541 | } |
| 7542 | return; |
| 7543 | } |
| 7544 | |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7545 | // If the target is integral, always warn. |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7546 | if (TargetBT && TargetBT->isInteger()) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 7547 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7548 | return; |
| 7549 | |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 7550 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 042ce8e | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 7551 | // We also want to warn on, e.g., "int i = -1.234" |
| 7552 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 7553 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 7554 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 7555 | |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 7556 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 7557 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 7558 | } else { |
| 7559 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 7560 | } |
| 7561 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7562 | |
Richard Smith | 54894fd | 2015-12-30 01:06:52 +0000 | [diff] [blame] | 7563 | // Detect the case where a call result is converted from floating-point to |
| 7564 | // to bool, and the final argument to the call is converted from bool, to |
| 7565 | // discover this typo: |
| 7566 | // |
| 7567 | // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;" |
| 7568 | // |
| 7569 | // FIXME: This is an incredibly special case; is there some more general |
| 7570 | // way to detect this class of misplaced-parentheses bug? |
| 7571 | if (Target->isBooleanType() && isa<CallExpr>(E)) { |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 7572 | // Check last argument of function call to see if it is an |
| 7573 | // implicit cast from a type matching the type the result |
| 7574 | // is being cast to. |
| 7575 | CallExpr *CEx = cast<CallExpr>(E); |
Richard Smith | 54894fd | 2015-12-30 01:06:52 +0000 | [diff] [blame] | 7576 | if (unsigned NumArgs = CEx->getNumArgs()) { |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 7577 | Expr *LastA = CEx->getArg(NumArgs - 1); |
| 7578 | Expr *InnerE = LastA->IgnoreParenImpCasts(); |
Richard Smith | 54894fd | 2015-12-30 01:06:52 +0000 | [diff] [blame] | 7579 | if (isa<ImplicitCastExpr>(LastA) && |
| 7580 | InnerE->getType()->isBooleanType()) { |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 7581 | // Warn on this floating-point to bool conversion |
| 7582 | DiagnoseImpCast(S, E, T, CC, |
| 7583 | diag::warn_impcast_floating_point_to_bool); |
| 7584 | } |
| 7585 | } |
| 7586 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7587 | return; |
| 7588 | } |
| 7589 | |
Richard Trieu | 5b99350 | 2014-10-15 03:42:06 +0000 | [diff] [blame] | 7590 | DiagnoseNullConversion(S, E, T, CC); |
Richard Trieu | beaf345 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 7591 | |
David Blaikie | 9366d2b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 7592 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
| 7593 | return; |
| 7594 | |
David Blaikie | 7555b6a | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 7595 | // TODO: remove this early return once the false positives for constant->bool |
| 7596 | // in templates, macros, etc, are reduced or removed. |
| 7597 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 7598 | return; |
| 7599 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7600 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 7601 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 7602 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7603 | if (SourceRange.Width > TargetRange.Width) { |
Sam Panzer | 6fffec6 | 2013-03-28 19:07:11 +0000 | [diff] [blame] | 7604 | // If the source is a constant, use a default-on diagnostic. |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7605 | // TODO: this should happen for bitfield stores, too. |
| 7606 | llvm::APSInt Value(32); |
Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 7607 | if (E->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) { |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7608 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 7609 | return; |
| 7610 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 7611 | std::string PrettySourceValue = Value.toString(10); |
| 7612 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7613 | |
Ted Kremenek | 33ba995 | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 7614 | S.DiagRuntimeBehavior(E->getExprLoc(), E, |
| 7615 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 7616 | << PrettySourceValue << PrettyTargetValue |
| 7617 | << E->getType() << T << E->getSourceRange() |
| 7618 | << clang::SourceRange(CC)); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 7619 | return; |
| 7620 | } |
| 7621 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7622 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
| 7623 | if (S.SourceMgr.isInSystemMacro(CC)) |
| 7624 | return; |
| 7625 | |
David Blaikie | 9455da0 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 7626 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 7627 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, |
| 7628 | /* pruneControlFlow */ true); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7629 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7630 | } |
| 7631 | |
Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 7632 | if (TargetRange.Width == SourceRange.Width && !TargetRange.NonNegative && |
| 7633 | SourceRange.NonNegative && Source->isSignedIntegerType()) { |
| 7634 | // Warn when doing a signed to signed conversion, warn if the positive |
| 7635 | // source value is exactly the width of the target type, which will |
| 7636 | // cause a negative value to be stored. |
| 7637 | |
| 7638 | llvm::APSInt Value; |
Richard Trieu | fc404c7 | 2016-02-05 23:02:38 +0000 | [diff] [blame] | 7639 | if (E->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects) && |
| 7640 | !S.SourceMgr.isInSystemMacro(CC)) { |
| 7641 | if (isSameWidthConstantConversion(S, E, T, CC)) { |
| 7642 | std::string PrettySourceValue = Value.toString(10); |
| 7643 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 7644 | |
Richard Trieu | fc404c7 | 2016-02-05 23:02:38 +0000 | [diff] [blame] | 7645 | S.DiagRuntimeBehavior( |
| 7646 | E->getExprLoc(), E, |
| 7647 | S.PDiag(diag::warn_impcast_integer_precision_constant) |
| 7648 | << PrettySourceValue << PrettyTargetValue << E->getType() << T |
| 7649 | << E->getSourceRange() << clang::SourceRange(CC)); |
| 7650 | return; |
Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 7651 | } |
| 7652 | } |
Richard Trieu | fc404c7 | 2016-02-05 23:02:38 +0000 | [diff] [blame] | 7653 | |
Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 7654 | // Fall through for non-constants to give a sign conversion warning. |
| 7655 | } |
| 7656 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7657 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 7658 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 7659 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7660 | |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 7661 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7662 | return; |
| 7663 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7664 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 7665 | |
| 7666 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 7667 | // We also want to warn about it in -Wconversion. |
| 7668 | // So if -Wconversion is off, use a completely identical diagnostic |
| 7669 | // in the sign-compare group. |
| 7670 | // The conditional-checking code will |
| 7671 | if (ICContext) { |
| 7672 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 7673 | *ICContext = true; |
| 7674 | } |
| 7675 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7676 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 7679 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 7680 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 7681 | // type, to give us better diagnostics. |
| 7682 | QualType SourceType = E->getType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7683 | if (!S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 7684 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 7685 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 7686 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 7687 | SourceType = S.Context.getTypeDeclType(Enum); |
| 7688 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 7689 | } |
| 7690 | } |
| 7691 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 7692 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 7693 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 7694 | if (SourceEnum->getDecl()->hasNameForLinkage() && |
| 7695 | TargetEnum->getDecl()->hasNameForLinkage() && |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7696 | SourceEnum != TargetEnum) { |
Matt Beaumont-Gay | 7a57ada | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 7697 | if (S.SourceMgr.isInSystemMacro(CC)) |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7698 | return; |
| 7699 | |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 7700 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 7701 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 7702 | } |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 7703 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 7704 | return; |
| 7705 | } |
| 7706 | |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 7707 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 7708 | SourceLocation CC, QualType T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7709 | |
| 7710 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7711 | SourceLocation CC, bool &ICContext) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7712 | E = E->IgnoreParenImpCasts(); |
| 7713 | |
| 7714 | if (isa<ConditionalOperator>(E)) |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 7715 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7716 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7717 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7718 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7719 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7720 | return; |
| 7721 | } |
| 7722 | |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 7723 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, |
| 7724 | SourceLocation CC, QualType T) { |
Richard Trieu | bd3305b | 2014-08-07 02:09:05 +0000 | [diff] [blame] | 7725 | AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7726 | |
| 7727 | bool Suspicious = false; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7728 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 7729 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7730 | |
| 7731 | // If -Wconversion would have warned about either of the candidates |
| 7732 | // for a signedness conversion to the context type... |
| 7733 | if (!Suspicious) return; |
| 7734 | |
| 7735 | // ...but it's currently ignored... |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 7736 | if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7737 | return; |
| 7738 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7739 | // ...then check whether it would have warned about either of the |
| 7740 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | bb43dec | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 7741 | if (E->getType() == T) return; |
| 7742 | |
| 7743 | Suspicious = false; |
| 7744 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 7745 | E->getType(), CC, &Suspicious); |
| 7746 | if (!Suspicious) |
| 7747 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7748 | E->getType(), CC, &Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7749 | } |
| 7750 | |
Richard Trieu | 6572489 | 2014-11-15 06:37:39 +0000 | [diff] [blame] | 7751 | /// CheckBoolLikeConversion - Check conversion of given expression to boolean. |
| 7752 | /// Input argument E is a logical expression. |
| 7753 | static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { |
| 7754 | if (S.getLangOpts().Bool) |
| 7755 | return; |
| 7756 | CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); |
| 7757 | } |
| 7758 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7759 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 7760 | /// implicit conversions in the given expression. There are a couple |
| 7761 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7762 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
Fariborz Jahanian | 148c8c8 | 2014-04-07 16:32:54 +0000 | [diff] [blame] | 7763 | QualType T = OrigE->getType(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7764 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 7765 | |
Douglas Gregor | 6e8da6a | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 7766 | if (E->isTypeDependent() || E->isValueDependent()) |
| 7767 | return; |
Fariborz Jahanian | ad95da7 | 2014-04-04 19:33:39 +0000 | [diff] [blame] | 7768 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7769 | // For conditional operators, we analyze the arguments as if they |
| 7770 | // were being fed directly into the output. |
| 7771 | if (isa<ConditionalOperator>(E)) { |
| 7772 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
David Blaikie | 18e9ac7 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 7773 | CheckConditionalOperator(S, CO, CC, T); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7774 | return; |
| 7775 | } |
| 7776 | |
Hans Wennborg | f4ad232 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 7777 | // Check implicit argument conversions for function calls. |
| 7778 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) |
| 7779 | CheckImplicitArgumentConversions(S, Call, CC); |
| 7780 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7781 | // Go ahead and check any implicit conversions we might have skipped. |
| 7782 | // The non-canonical typecheck is just an optimization; |
| 7783 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 7784 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7785 | CheckImplicitConversion(S, E, T, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7786 | |
| 7787 | // Now continue drilling into this expression. |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 7788 | |
| 7789 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 7790 | // The bound subexpressions in a PseudoObjectExpr are not reachable |
| 7791 | // as transitive children. |
| 7792 | // FIXME: Use a more uniform representation for this. |
| 7793 | for (auto *SE : POE->semantics()) |
| 7794 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE)) |
| 7795 | AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC); |
Fariborz Jahanian | 2cb4a95 | 2013-05-15 19:03:04 +0000 | [diff] [blame] | 7796 | } |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 7797 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7798 | // Skip past explicit casts. |
| 7799 | if (isa<ExplicitCastExpr>(E)) { |
| 7800 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7801 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7802 | } |
| 7803 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 7804 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 7805 | // Do a somewhat different check with comparison operators. |
| 7806 | if (BO->isComparisonOp()) |
| 7807 | return AnalyzeComparison(S, BO); |
| 7808 | |
Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 7809 | // And with simple assignments. |
| 7810 | if (BO->getOpcode() == BO_Assign) |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 7811 | return AnalyzeAssignment(S, BO); |
| 7812 | } |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7813 | |
| 7814 | // These break the otherwise-useful invariant below. Fortunately, |
| 7815 | // we don't really need to recurse into them, because any internal |
| 7816 | // expressions should have been analyzed already when they were |
| 7817 | // built into statements. |
| 7818 | if (isa<StmtExpr>(E)) return; |
| 7819 | |
| 7820 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7821 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7822 | |
| 7823 | // Now just recurse over the expression's children. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7824 | CC = E->getExprLoc(); |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 7825 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 7826 | bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 7827 | for (Stmt *SubStmt : E->children()) { |
| 7828 | Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt); |
Douglas Gregor | 8c50e7c | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 7829 | if (!ChildExpr) |
| 7830 | continue; |
| 7831 | |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 7832 | if (IsLogicalAndOperator && |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 7833 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
Richard Trieu | 955231d | 2014-01-25 01:10:35 +0000 | [diff] [blame] | 7834 | // Ignore checking string literals that are in logical and operators. |
| 7835 | // This is a common pattern for asserts. |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 7836 | continue; |
| 7837 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 7838 | } |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7839 | |
Fariborz Jahanian | b7859dd | 2014-11-14 17:12:50 +0000 | [diff] [blame] | 7840 | if (BO && BO->isLogicalOp()) { |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7841 | Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); |
| 7842 | if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) |
Fariborz Jahanian | 0fc95ad | 2014-12-18 23:14:51 +0000 | [diff] [blame] | 7843 | ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7844 | |
| 7845 | SubExpr = BO->getRHS()->IgnoreParenImpCasts(); |
| 7846 | if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) |
Fariborz Jahanian | 0fc95ad | 2014-12-18 23:14:51 +0000 | [diff] [blame] | 7847 | ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); |
Fariborz Jahanian | b7859dd | 2014-11-14 17:12:50 +0000 | [diff] [blame] | 7848 | } |
Richard Trieu | 791b86e | 2014-11-19 06:08:18 +0000 | [diff] [blame] | 7849 | |
Fariborz Jahanian | b7859dd | 2014-11-14 17:12:50 +0000 | [diff] [blame] | 7850 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) |
| 7851 | if (U->getOpcode() == UO_LNot) |
Richard Trieu | 6572489 | 2014-11-15 06:37:39 +0000 | [diff] [blame] | 7852 | ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
| 7855 | } // end anonymous namespace |
| 7856 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7857 | // Helper function for Sema::DiagnoseAlwaysNonNullPointer. |
| 7858 | // Returns true when emitting a warning about taking the address of a reference. |
| 7859 | static bool CheckForReference(Sema &SemaRef, const Expr *E, |
| 7860 | PartialDiagnostic PD) { |
| 7861 | E = E->IgnoreParenImpCasts(); |
| 7862 | |
| 7863 | const FunctionDecl *FD = nullptr; |
| 7864 | |
| 7865 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 7866 | if (!DRE->getDecl()->getType()->isReferenceType()) |
| 7867 | return false; |
| 7868 | } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 7869 | if (!M->getMemberDecl()->getType()->isReferenceType()) |
| 7870 | return false; |
| 7871 | } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 7872 | if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7873 | return false; |
| 7874 | FD = Call->getDirectCallee(); |
| 7875 | } else { |
| 7876 | return false; |
| 7877 | } |
| 7878 | |
| 7879 | SemaRef.Diag(E->getExprLoc(), PD); |
| 7880 | |
| 7881 | // If possible, point to location of function. |
| 7882 | if (FD) { |
| 7883 | SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; |
| 7884 | } |
| 7885 | |
| 7886 | return true; |
| 7887 | } |
| 7888 | |
Richard Trieu | 4cbff5c | 2014-08-08 22:41:43 +0000 | [diff] [blame] | 7889 | // Returns true if the SourceLocation is expanded from any macro body. |
| 7890 | // Returns false if the SourceLocation is invalid, is from not in a macro |
| 7891 | // expansion, or is from expanded from a top-level macro argument. |
| 7892 | static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { |
| 7893 | if (Loc.isInvalid()) |
| 7894 | return false; |
| 7895 | |
| 7896 | while (Loc.isMacroID()) { |
| 7897 | if (SM.isMacroBodyExpansion(Loc)) |
| 7898 | return true; |
| 7899 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
| 7900 | } |
| 7901 | |
| 7902 | return false; |
| 7903 | } |
| 7904 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7905 | /// \brief Diagnose pointers that are always non-null. |
| 7906 | /// \param E the expression containing the pointer |
| 7907 | /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is |
| 7908 | /// compared to a null pointer |
| 7909 | /// \param IsEqual True when the comparison is equal to a null pointer |
| 7910 | /// \param Range Extra SourceRange to highlight in the diagnostic |
| 7911 | void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, |
| 7912 | Expr::NullPointerConstantKind NullKind, |
| 7913 | bool IsEqual, SourceRange Range) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 7914 | if (!E) |
| 7915 | return; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7916 | |
| 7917 | // Don't warn inside macros. |
Richard Trieu | 4cbff5c | 2014-08-08 22:41:43 +0000 | [diff] [blame] | 7918 | if (E->getExprLoc().isMacroID()) { |
| 7919 | const SourceManager &SM = getSourceManager(); |
| 7920 | if (IsInAnyMacroBody(SM, E->getExprLoc()) || |
| 7921 | IsInAnyMacroBody(SM, Range.getBegin())) |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7922 | return; |
Richard Trieu | 4cbff5c | 2014-08-08 22:41:43 +0000 | [diff] [blame] | 7923 | } |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7924 | E = E->IgnoreImpCasts(); |
| 7925 | |
| 7926 | const bool IsCompare = NullKind != Expr::NPCK_NotNull; |
| 7927 | |
Richard Trieu | f743275 | 2014-06-06 21:39:26 +0000 | [diff] [blame] | 7928 | if (isa<CXXThisExpr>(E)) { |
| 7929 | unsigned DiagID = IsCompare ? diag::warn_this_null_compare |
| 7930 | : diag::warn_this_bool_conversion; |
| 7931 | Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; |
| 7932 | return; |
| 7933 | } |
| 7934 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7935 | bool IsAddressOf = false; |
| 7936 | |
| 7937 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 7938 | if (UO->getOpcode() != UO_AddrOf) |
| 7939 | return; |
| 7940 | IsAddressOf = true; |
| 7941 | E = UO->getSubExpr(); |
| 7942 | } |
| 7943 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 7944 | if (IsAddressOf) { |
| 7945 | unsigned DiagID = IsCompare |
| 7946 | ? diag::warn_address_of_reference_null_compare |
| 7947 | : diag::warn_address_of_reference_bool_conversion; |
| 7948 | PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range |
| 7949 | << IsEqual; |
| 7950 | if (CheckForReference(*this, E, PD)) { |
| 7951 | return; |
| 7952 | } |
| 7953 | } |
| 7954 | |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 7955 | auto ComplainAboutNonnullParamOrCall = [&](bool IsParam) { |
| 7956 | std::string Str; |
| 7957 | llvm::raw_string_ostream S(Str); |
| 7958 | E->printPretty(S, nullptr, getPrintingPolicy()); |
| 7959 | unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare |
| 7960 | : diag::warn_cast_nonnull_to_bool; |
| 7961 | Diag(E->getExprLoc(), DiagID) << IsParam << S.str() |
| 7962 | << E->getSourceRange() << Range << IsEqual; |
| 7963 | }; |
| 7964 | |
| 7965 | // If we have a CallExpr that is tagged with returns_nonnull, we can complain. |
| 7966 | if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) { |
| 7967 | if (auto *Callee = Call->getDirectCallee()) { |
| 7968 | if (Callee->hasAttr<ReturnsNonNullAttr>()) { |
| 7969 | ComplainAboutNonnullParamOrCall(false); |
| 7970 | return; |
| 7971 | } |
| 7972 | } |
| 7973 | } |
| 7974 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7975 | // Expect to find a single Decl. Skip anything more complicated. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7976 | ValueDecl *D = nullptr; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 7977 | if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { |
| 7978 | D = R->getDecl(); |
| 7979 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { |
| 7980 | D = M->getMemberDecl(); |
| 7981 | } |
| 7982 | |
| 7983 | // Weak Decls can be null. |
| 7984 | if (!D || D->isWeak()) |
| 7985 | return; |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 7986 | |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 7987 | // Check for parameter decl with nonnull attribute |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 7988 | if (const auto* PV = dyn_cast<ParmVarDecl>(D)) { |
| 7989 | if (getCurFunction() && |
| 7990 | !getCurFunction()->ModifiedNonNullParams.count(PV)) { |
| 7991 | if (PV->hasAttr<NonNullAttr>()) { |
| 7992 | ComplainAboutNonnullParamOrCall(true); |
| 7993 | return; |
| 7994 | } |
| 7995 | |
| 7996 | if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
| 7997 | auto ParamIter = std::find(FD->param_begin(), FD->param_end(), PV); |
| 7998 | assert(ParamIter != FD->param_end()); |
| 7999 | unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); |
| 8000 | |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 8001 | for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { |
| 8002 | if (!NonNull->args_size()) { |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 8003 | ComplainAboutNonnullParamOrCall(true); |
| 8004 | return; |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 8005 | } |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 8006 | |
| 8007 | for (unsigned ArgNo : NonNull->args()) { |
| 8008 | if (ArgNo == ParamNo) { |
| 8009 | ComplainAboutNonnullParamOrCall(true); |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 8010 | return; |
| 8011 | } |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 8012 | } |
| 8013 | } |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 8014 | } |
| 8015 | } |
George Burgess IV | 850269a | 2015-12-08 22:02:00 +0000 | [diff] [blame] | 8016 | } |
| 8017 | |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 8018 | QualType T = D->getType(); |
| 8019 | const bool IsArray = T->isArrayType(); |
| 8020 | const bool IsFunction = T->isFunctionType(); |
| 8021 | |
Richard Trieu | c1888e0 | 2014-06-28 23:25:37 +0000 | [diff] [blame] | 8022 | // Address of function is used to silence the function warning. |
| 8023 | if (IsAddressOf && IsFunction) { |
| 8024 | return; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 8025 | } |
| 8026 | |
| 8027 | // Found nothing. |
| 8028 | if (!IsAddressOf && !IsFunction && !IsArray) |
| 8029 | return; |
| 8030 | |
| 8031 | // Pretty print the expression for the diagnostic. |
| 8032 | std::string Str; |
| 8033 | llvm::raw_string_ostream S(Str); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8034 | E->printPretty(S, nullptr, getPrintingPolicy()); |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 8035 | |
| 8036 | unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare |
| 8037 | : diag::warn_impcast_pointer_to_bool; |
Craig Topper | fa1340f | 2015-12-23 05:44:46 +0000 | [diff] [blame] | 8038 | enum { |
| 8039 | AddressOf, |
| 8040 | FunctionPointer, |
| 8041 | ArrayPointer |
| 8042 | } DiagType; |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 8043 | if (IsAddressOf) |
| 8044 | DiagType = AddressOf; |
| 8045 | else if (IsFunction) |
| 8046 | DiagType = FunctionPointer; |
| 8047 | else if (IsArray) |
| 8048 | DiagType = ArrayPointer; |
| 8049 | else |
| 8050 | llvm_unreachable("Could not determine diagnostic."); |
| 8051 | Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() |
| 8052 | << Range << IsEqual; |
| 8053 | |
| 8054 | if (!IsFunction) |
| 8055 | return; |
| 8056 | |
| 8057 | // Suggest '&' to silence the function warning. |
| 8058 | Diag(E->getExprLoc(), diag::note_function_warning_silence) |
| 8059 | << FixItHint::CreateInsertion(E->getLocStart(), "&"); |
| 8060 | |
| 8061 | // Check to see if '()' fixit should be emitted. |
| 8062 | QualType ReturnType; |
| 8063 | UnresolvedSet<4> NonTemplateOverloads; |
| 8064 | tryExprAsCall(*E, ReturnType, NonTemplateOverloads); |
| 8065 | if (ReturnType.isNull()) |
| 8066 | return; |
| 8067 | |
| 8068 | if (IsCompare) { |
| 8069 | // There are two cases here. If there is null constant, the only suggest |
| 8070 | // for a pointer return type. If the null is 0, then suggest if the return |
| 8071 | // type is a pointer or an integer type. |
| 8072 | if (!ReturnType->isPointerType()) { |
| 8073 | if (NullKind == Expr::NPCK_ZeroExpression || |
| 8074 | NullKind == Expr::NPCK_ZeroLiteral) { |
| 8075 | if (!ReturnType->isIntegerType()) |
| 8076 | return; |
| 8077 | } else { |
| 8078 | return; |
| 8079 | } |
| 8080 | } |
| 8081 | } else { // !IsCompare |
| 8082 | // For function to bool, only suggest if the function pointer has bool |
| 8083 | // return type. |
| 8084 | if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) |
| 8085 | return; |
| 8086 | } |
| 8087 | Diag(E->getExprLoc(), diag::note_function_to_function_call) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 8088 | << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()"); |
Richard Trieu | 3bb8b56 | 2014-02-26 02:36:06 +0000 | [diff] [blame] | 8089 | } |
| 8090 | |
| 8091 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 8092 | /// Diagnoses "dangerous" implicit conversions within the given |
| 8093 | /// expression (which is a full expression). Implements -Wconversion |
| 8094 | /// and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 8095 | /// |
| 8096 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 8097 | /// the most location of the syntactic entity requiring the implicit |
| 8098 | /// conversion |
| 8099 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 8100 | // Don't diagnose in unevaluated contexts. |
David Blaikie | 131fcb4 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 8101 | if (isUnevaluatedContext()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 8102 | return; |
| 8103 | |
| 8104 | // Don't diagnose for value- or type-dependent expressions. |
| 8105 | if (E->isTypeDependent() || E->isValueDependent()) |
| 8106 | return; |
| 8107 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8108 | // Check for array bounds violations in cases where the check isn't triggered |
| 8109 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 8110 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 8111 | CheckArrayAccess(E); |
| 8112 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 8113 | // This is not the right CC for (e.g.) a variable initialization. |
| 8114 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Richard Trieu | 6572489 | 2014-11-15 06:37:39 +0000 | [diff] [blame] | 8117 | /// CheckBoolLikeConversion - Check conversion of given expression to boolean. |
| 8118 | /// Input argument E is a logical expression. |
| 8119 | void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { |
| 8120 | ::CheckBoolLikeConversion(*this, E, CC); |
| 8121 | } |
| 8122 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8123 | /// Diagnose when expression is an integer constant expression and its evaluation |
| 8124 | /// results in integer overflow |
| 8125 | void Sema::CheckForIntOverflow (Expr *E) { |
Fariborz Jahanian | c694e69 | 2014-10-14 20:27:05 +0000 | [diff] [blame] | 8126 | if (isa<BinaryOperator>(E->IgnoreParenCasts())) |
| 8127 | E->IgnoreParenCasts()->EvaluateForOverflow(Context); |
Akira Hatanaka | f5c1361 | 2016-01-11 17:22:01 +0000 | [diff] [blame] | 8128 | else if (auto InitList = dyn_cast<InitListExpr>(E)) |
| 8129 | for (Expr *E : InitList->inits()) |
| 8130 | if (isa<BinaryOperator>(E->IgnoreParenCasts())) |
| 8131 | E->IgnoreParenCasts()->EvaluateForOverflow(Context); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8132 | } |
| 8133 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8134 | namespace { |
| 8135 | /// \brief Visitor for expressions which looks for unsequenced operations on the |
| 8136 | /// same object. |
| 8137 | class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> { |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 8138 | typedef EvaluatedExprVisitor<SequenceChecker> Base; |
| 8139 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8140 | /// \brief A tree of sequenced regions within an expression. Two regions are |
| 8141 | /// unsequenced if one is an ancestor or a descendent of the other. When we |
| 8142 | /// finish processing an expression with sequencing, such as a comma |
| 8143 | /// expression, we fold its tree nodes into its parent, since they are |
| 8144 | /// unsequenced with respect to nodes we will visit later. |
| 8145 | class SequenceTree { |
| 8146 | struct Value { |
| 8147 | explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} |
| 8148 | unsigned Parent : 31; |
| 8149 | bool Merged : 1; |
| 8150 | }; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8151 | SmallVector<Value, 8> Values; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8152 | |
| 8153 | public: |
| 8154 | /// \brief A region within an expression which may be sequenced with respect |
| 8155 | /// to some other region. |
| 8156 | class Seq { |
| 8157 | explicit Seq(unsigned N) : Index(N) {} |
| 8158 | unsigned Index; |
| 8159 | friend class SequenceTree; |
| 8160 | public: |
| 8161 | Seq() : Index(0) {} |
| 8162 | }; |
| 8163 | |
| 8164 | SequenceTree() { Values.push_back(Value(0)); } |
| 8165 | Seq root() const { return Seq(0); } |
| 8166 | |
| 8167 | /// \brief Create a new sequence of operations, which is an unsequenced |
| 8168 | /// subset of \p Parent. This sequence of operations is sequenced with |
| 8169 | /// respect to other children of \p Parent. |
| 8170 | Seq allocate(Seq Parent) { |
| 8171 | Values.push_back(Value(Parent.Index)); |
| 8172 | return Seq(Values.size() - 1); |
| 8173 | } |
| 8174 | |
| 8175 | /// \brief Merge a sequence of operations into its parent. |
| 8176 | void merge(Seq S) { |
| 8177 | Values[S.Index].Merged = true; |
| 8178 | } |
| 8179 | |
| 8180 | /// \brief Determine whether two operations are unsequenced. This operation |
| 8181 | /// is asymmetric: \p Cur should be the more recent sequence, and \p Old |
| 8182 | /// should have been merged into its parent as appropriate. |
| 8183 | bool isUnsequenced(Seq Cur, Seq Old) { |
| 8184 | unsigned C = representative(Cur.Index); |
| 8185 | unsigned Target = representative(Old.Index); |
| 8186 | while (C >= Target) { |
| 8187 | if (C == Target) |
| 8188 | return true; |
| 8189 | C = Values[C].Parent; |
| 8190 | } |
| 8191 | return false; |
| 8192 | } |
| 8193 | |
| 8194 | private: |
| 8195 | /// \brief Pick a representative for a sequence. |
| 8196 | unsigned representative(unsigned K) { |
| 8197 | if (Values[K].Merged) |
| 8198 | // Perform path compression as we go. |
| 8199 | return Values[K].Parent = representative(Values[K].Parent); |
| 8200 | return K; |
| 8201 | } |
| 8202 | }; |
| 8203 | |
| 8204 | /// An object for which we can track unsequenced uses. |
| 8205 | typedef NamedDecl *Object; |
| 8206 | |
| 8207 | /// Different flavors of object usage which we track. We only track the |
| 8208 | /// least-sequenced usage of each kind. |
| 8209 | enum UsageKind { |
| 8210 | /// A read of an object. Multiple unsequenced reads are OK. |
| 8211 | UK_Use, |
| 8212 | /// A modification of an object which is sequenced before the value |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 8213 | /// computation of the expression, such as ++n in C++. |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8214 | UK_ModAsValue, |
| 8215 | /// A modification of an object which is not sequenced before the value |
| 8216 | /// computation of the expression, such as n++. |
| 8217 | UK_ModAsSideEffect, |
| 8218 | |
| 8219 | UK_Count = UK_ModAsSideEffect + 1 |
| 8220 | }; |
| 8221 | |
| 8222 | struct Usage { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8223 | Usage() : Use(nullptr), Seq() {} |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8224 | Expr *Use; |
| 8225 | SequenceTree::Seq Seq; |
| 8226 | }; |
| 8227 | |
| 8228 | struct UsageInfo { |
| 8229 | UsageInfo() : Diagnosed(false) {} |
| 8230 | Usage Uses[UK_Count]; |
| 8231 | /// Have we issued a diagnostic for this variable already? |
| 8232 | bool Diagnosed; |
| 8233 | }; |
| 8234 | typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap; |
| 8235 | |
| 8236 | Sema &SemaRef; |
| 8237 | /// Sequenced regions within the expression. |
| 8238 | SequenceTree Tree; |
| 8239 | /// Declaration modifications and references which we have seen. |
| 8240 | UsageInfoMap UsageMap; |
| 8241 | /// The region we are currently within. |
| 8242 | SequenceTree::Seq Region; |
| 8243 | /// Filled in with declarations which were modified as a side-effect |
| 8244 | /// (that is, post-increment operations). |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8245 | SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect; |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 8246 | /// Expressions to check later. We defer checking these to reduce |
| 8247 | /// stack usage. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8248 | SmallVectorImpl<Expr *> &WorkList; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8249 | |
| 8250 | /// RAII object wrapping the visitation of a sequenced subexpression of an |
| 8251 | /// expression. At the end of this process, the side-effects of the evaluation |
| 8252 | /// become sequenced with respect to the value computation of the result, so |
| 8253 | /// we downgrade any UK_ModAsSideEffect within the evaluation to |
| 8254 | /// UK_ModAsValue. |
| 8255 | struct SequencedSubexpression { |
| 8256 | SequencedSubexpression(SequenceChecker &Self) |
| 8257 | : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { |
| 8258 | Self.ModAsSideEffect = &ModAsSideEffect; |
| 8259 | } |
| 8260 | ~SequencedSubexpression() { |
Richard Smith | e8efd99 | 2014-12-03 01:05:50 +0000 | [diff] [blame] | 8261 | for (auto MI = ModAsSideEffect.rbegin(), ME = ModAsSideEffect.rend(); |
| 8262 | MI != ME; ++MI) { |
| 8263 | UsageInfo &U = Self.UsageMap[MI->first]; |
| 8264 | auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect]; |
| 8265 | Self.addUsage(U, MI->first, SideEffectUsage.Use, UK_ModAsValue); |
| 8266 | SideEffectUsage = MI->second; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8267 | } |
| 8268 | Self.ModAsSideEffect = OldModAsSideEffect; |
| 8269 | } |
| 8270 | |
| 8271 | SequenceChecker &Self; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8272 | SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; |
| 8273 | SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8274 | }; |
| 8275 | |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8276 | /// RAII object wrapping the visitation of a subexpression which we might |
| 8277 | /// choose to evaluate as a constant. If any subexpression is evaluated and |
| 8278 | /// found to be non-constant, this allows us to suppress the evaluation of |
| 8279 | /// the outer expression. |
| 8280 | class EvaluationTracker { |
| 8281 | public: |
| 8282 | EvaluationTracker(SequenceChecker &Self) |
| 8283 | : Self(Self), Prev(Self.EvalTracker), EvalOK(true) { |
| 8284 | Self.EvalTracker = this; |
| 8285 | } |
| 8286 | ~EvaluationTracker() { |
| 8287 | Self.EvalTracker = Prev; |
| 8288 | if (Prev) |
| 8289 | Prev->EvalOK &= EvalOK; |
| 8290 | } |
| 8291 | |
| 8292 | bool evaluate(const Expr *E, bool &Result) { |
| 8293 | if (!EvalOK || E->isValueDependent()) |
| 8294 | return false; |
| 8295 | EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context); |
| 8296 | return EvalOK; |
| 8297 | } |
| 8298 | |
| 8299 | private: |
| 8300 | SequenceChecker &Self; |
| 8301 | EvaluationTracker *Prev; |
| 8302 | bool EvalOK; |
| 8303 | } *EvalTracker; |
| 8304 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8305 | /// \brief Find the object which is produced by the specified expression, |
| 8306 | /// if any. |
| 8307 | Object getObject(Expr *E, bool Mod) const { |
| 8308 | E = E->IgnoreParenCasts(); |
| 8309 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 8310 | if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) |
| 8311 | return getObject(UO->getSubExpr(), Mod); |
| 8312 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 8313 | if (BO->getOpcode() == BO_Comma) |
| 8314 | return getObject(BO->getRHS(), Mod); |
| 8315 | if (Mod && BO->isAssignmentOp()) |
| 8316 | return getObject(BO->getLHS(), Mod); |
| 8317 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 8318 | // FIXME: Check for more interesting cases, like "x.n = ++x.n". |
| 8319 | if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) |
| 8320 | return ME->getMemberDecl(); |
| 8321 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 8322 | // FIXME: If this is a reference, map through to its value. |
| 8323 | return DRE->getDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8324 | return nullptr; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8325 | } |
| 8326 | |
| 8327 | /// \brief Note that an object was modified or used by an expression. |
| 8328 | void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) { |
| 8329 | Usage &U = UI.Uses[UK]; |
| 8330 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) { |
| 8331 | if (UK == UK_ModAsSideEffect && ModAsSideEffect) |
| 8332 | ModAsSideEffect->push_back(std::make_pair(O, U)); |
| 8333 | U.Use = Ref; |
| 8334 | U.Seq = Region; |
| 8335 | } |
| 8336 | } |
| 8337 | /// \brief Check whether a modification or use conflicts with a prior usage. |
| 8338 | void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind, |
| 8339 | bool IsModMod) { |
| 8340 | if (UI.Diagnosed) |
| 8341 | return; |
| 8342 | |
| 8343 | const Usage &U = UI.Uses[OtherKind]; |
| 8344 | if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) |
| 8345 | return; |
| 8346 | |
| 8347 | Expr *Mod = U.Use; |
| 8348 | Expr *ModOrUse = Ref; |
| 8349 | if (OtherKind == UK_Use) |
| 8350 | std::swap(Mod, ModOrUse); |
| 8351 | |
| 8352 | SemaRef.Diag(Mod->getExprLoc(), |
| 8353 | IsModMod ? diag::warn_unsequenced_mod_mod |
| 8354 | : diag::warn_unsequenced_mod_use) |
| 8355 | << O << SourceRange(ModOrUse->getExprLoc()); |
| 8356 | UI.Diagnosed = true; |
| 8357 | } |
| 8358 | |
| 8359 | void notePreUse(Object O, Expr *Use) { |
| 8360 | UsageInfo &U = UsageMap[O]; |
| 8361 | // Uses conflict with other modifications. |
| 8362 | checkUsage(O, U, Use, UK_ModAsValue, false); |
| 8363 | } |
| 8364 | void notePostUse(Object O, Expr *Use) { |
| 8365 | UsageInfo &U = UsageMap[O]; |
| 8366 | checkUsage(O, U, Use, UK_ModAsSideEffect, false); |
| 8367 | addUsage(U, O, Use, UK_Use); |
| 8368 | } |
| 8369 | |
| 8370 | void notePreMod(Object O, Expr *Mod) { |
| 8371 | UsageInfo &U = UsageMap[O]; |
| 8372 | // Modifications conflict with other modifications and with uses. |
| 8373 | checkUsage(O, U, Mod, UK_ModAsValue, true); |
| 8374 | checkUsage(O, U, Mod, UK_Use, false); |
| 8375 | } |
| 8376 | void notePostMod(Object O, Expr *Use, UsageKind UK) { |
| 8377 | UsageInfo &U = UsageMap[O]; |
| 8378 | checkUsage(O, U, Use, UK_ModAsSideEffect, true); |
| 8379 | addUsage(U, O, Use, UK); |
| 8380 | } |
| 8381 | |
| 8382 | public: |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8383 | SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8384 | : Base(S.Context), SemaRef(S), Region(Tree.root()), |
| 8385 | ModAsSideEffect(nullptr), WorkList(WorkList), EvalTracker(nullptr) { |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8386 | Visit(E); |
| 8387 | } |
| 8388 | |
| 8389 | void VisitStmt(Stmt *S) { |
| 8390 | // Skip all statements which aren't expressions for now. |
| 8391 | } |
| 8392 | |
| 8393 | void VisitExpr(Expr *E) { |
| 8394 | // By default, just recurse to evaluated subexpressions. |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 8395 | Base::VisitStmt(E); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8396 | } |
| 8397 | |
| 8398 | void VisitCastExpr(CastExpr *E) { |
| 8399 | Object O = Object(); |
| 8400 | if (E->getCastKind() == CK_LValueToRValue) |
| 8401 | O = getObject(E->getSubExpr(), false); |
| 8402 | |
| 8403 | if (O) |
| 8404 | notePreUse(O, E); |
| 8405 | VisitExpr(E); |
| 8406 | if (O) |
| 8407 | notePostUse(O, E); |
| 8408 | } |
| 8409 | |
| 8410 | void VisitBinComma(BinaryOperator *BO) { |
| 8411 | // C++11 [expr.comma]p1: |
| 8412 | // Every value computation and side effect associated with the left |
| 8413 | // expression is sequenced before every value computation and side |
| 8414 | // effect associated with the right expression. |
| 8415 | SequenceTree::Seq LHS = Tree.allocate(Region); |
| 8416 | SequenceTree::Seq RHS = Tree.allocate(Region); |
| 8417 | SequenceTree::Seq OldRegion = Region; |
| 8418 | |
| 8419 | { |
| 8420 | SequencedSubexpression SeqLHS(*this); |
| 8421 | Region = LHS; |
| 8422 | Visit(BO->getLHS()); |
| 8423 | } |
| 8424 | |
| 8425 | Region = RHS; |
| 8426 | Visit(BO->getRHS()); |
| 8427 | |
| 8428 | Region = OldRegion; |
| 8429 | |
| 8430 | // Forget that LHS and RHS are sequenced. They are both unsequenced |
| 8431 | // with respect to other stuff. |
| 8432 | Tree.merge(LHS); |
| 8433 | Tree.merge(RHS); |
| 8434 | } |
| 8435 | |
| 8436 | void VisitBinAssign(BinaryOperator *BO) { |
| 8437 | // The modification is sequenced after the value computation of the LHS |
| 8438 | // and RHS, so check it before inspecting the operands and update the |
| 8439 | // map afterwards. |
| 8440 | Object O = getObject(BO->getLHS(), true); |
| 8441 | if (!O) |
| 8442 | return VisitExpr(BO); |
| 8443 | |
| 8444 | notePreMod(O, BO); |
| 8445 | |
| 8446 | // C++11 [expr.ass]p7: |
| 8447 | // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated |
| 8448 | // only once. |
| 8449 | // |
| 8450 | // Therefore, for a compound assignment operator, O is considered used |
| 8451 | // everywhere except within the evaluation of E1 itself. |
| 8452 | if (isa<CompoundAssignOperator>(BO)) |
| 8453 | notePreUse(O, BO); |
| 8454 | |
| 8455 | Visit(BO->getLHS()); |
| 8456 | |
| 8457 | if (isa<CompoundAssignOperator>(BO)) |
| 8458 | notePostUse(O, BO); |
| 8459 | |
| 8460 | Visit(BO->getRHS()); |
| 8461 | |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 8462 | // C++11 [expr.ass]p1: |
| 8463 | // the assignment is sequenced [...] before the value computation of the |
| 8464 | // assignment expression. |
| 8465 | // C11 6.5.16/3 has no such rule. |
| 8466 | notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
| 8467 | : UK_ModAsSideEffect); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8468 | } |
| 8469 | void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) { |
| 8470 | VisitBinAssign(CAO); |
| 8471 | } |
| 8472 | |
| 8473 | void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 8474 | void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } |
| 8475 | void VisitUnaryPreIncDec(UnaryOperator *UO) { |
| 8476 | Object O = getObject(UO->getSubExpr(), true); |
| 8477 | if (!O) |
| 8478 | return VisitExpr(UO); |
| 8479 | |
| 8480 | notePreMod(O, UO); |
| 8481 | Visit(UO->getSubExpr()); |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 8482 | // C++11 [expr.pre.incr]p1: |
| 8483 | // the expression ++x is equivalent to x+=1 |
| 8484 | notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue |
| 8485 | : UK_ModAsSideEffect); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8486 | } |
| 8487 | |
| 8488 | void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 8489 | void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } |
| 8490 | void VisitUnaryPostIncDec(UnaryOperator *UO) { |
| 8491 | Object O = getObject(UO->getSubExpr(), true); |
| 8492 | if (!O) |
| 8493 | return VisitExpr(UO); |
| 8494 | |
| 8495 | notePreMod(O, UO); |
| 8496 | Visit(UO->getSubExpr()); |
| 8497 | notePostMod(O, UO, UK_ModAsSideEffect); |
| 8498 | } |
| 8499 | |
| 8500 | /// Don't visit the RHS of '&&' or '||' if it might not be evaluated. |
| 8501 | void VisitBinLOr(BinaryOperator *BO) { |
| 8502 | // The side-effects of the LHS of an '&&' are sequenced before the |
| 8503 | // value computation of the RHS, and hence before the value computation |
| 8504 | // of the '&&' itself, unless the LHS evaluates to zero. We treat them |
| 8505 | // as if they were unconditionally sequenced. |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8506 | EvaluationTracker Eval(*this); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8507 | { |
| 8508 | SequencedSubexpression Sequenced(*this); |
| 8509 | Visit(BO->getLHS()); |
| 8510 | } |
| 8511 | |
| 8512 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8513 | if (Eval.evaluate(BO->getLHS(), Result)) { |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 8514 | if (!Result) |
| 8515 | Visit(BO->getRHS()); |
| 8516 | } else { |
| 8517 | // Check for unsequenced operations in the RHS, treating it as an |
| 8518 | // entirely separate evaluation. |
| 8519 | // |
| 8520 | // FIXME: If there are operations in the RHS which are unsequenced |
| 8521 | // with respect to operations outside the RHS, and those operations |
| 8522 | // are unconditionally evaluated, diagnose them. |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 8523 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 8524 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8525 | } |
| 8526 | void VisitBinLAnd(BinaryOperator *BO) { |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8527 | EvaluationTracker Eval(*this); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8528 | { |
| 8529 | SequencedSubexpression Sequenced(*this); |
| 8530 | Visit(BO->getLHS()); |
| 8531 | } |
| 8532 | |
| 8533 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8534 | if (Eval.evaluate(BO->getLHS(), Result)) { |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 8535 | if (Result) |
| 8536 | Visit(BO->getRHS()); |
| 8537 | } else { |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 8538 | WorkList.push_back(BO->getRHS()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 8539 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8540 | } |
| 8541 | |
| 8542 | // Only visit the condition, unless we can be sure which subexpression will |
| 8543 | // be chosen. |
| 8544 | void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) { |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8545 | EvaluationTracker Eval(*this); |
Richard Smith | 83e37bee | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 8546 | { |
| 8547 | SequencedSubexpression Sequenced(*this); |
| 8548 | Visit(CO->getCond()); |
| 8549 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8550 | |
| 8551 | bool Result; |
Richard Smith | 40238f0 | 2013-06-20 22:21:56 +0000 | [diff] [blame] | 8552 | if (Eval.evaluate(CO->getCond(), Result)) |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8553 | Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 8554 | else { |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 8555 | WorkList.push_back(CO->getTrueExpr()); |
| 8556 | WorkList.push_back(CO->getFalseExpr()); |
Richard Smith | 01a7fba | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 8557 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8558 | } |
| 8559 | |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 8560 | void VisitCallExpr(CallExpr *CE) { |
| 8561 | // C++11 [intro.execution]p15: |
| 8562 | // When calling a function [...], every value computation and side effect |
| 8563 | // associated with any argument expression, or with the postfix expression |
| 8564 | // designating the called function, is sequenced before execution of every |
| 8565 | // expression or statement in the body of the function [and thus before |
| 8566 | // the value computation of its result]. |
| 8567 | SequencedSubexpression Sequenced(*this); |
| 8568 | Base::VisitCallExpr(CE); |
| 8569 | |
| 8570 | // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. |
| 8571 | } |
| 8572 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8573 | void VisitCXXConstructExpr(CXXConstructExpr *CCE) { |
Richard Smith | e3dbfe0 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 8574 | // This is a call, so all subexpressions are sequenced before the result. |
| 8575 | SequencedSubexpression Sequenced(*this); |
| 8576 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8577 | if (!CCE->isListInitialization()) |
| 8578 | return VisitExpr(CCE); |
| 8579 | |
| 8580 | // In C++11, list initializations are sequenced. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8581 | SmallVector<SequenceTree::Seq, 32> Elts; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8582 | SequenceTree::Seq Parent = Region; |
| 8583 | for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(), |
| 8584 | E = CCE->arg_end(); |
| 8585 | I != E; ++I) { |
| 8586 | Region = Tree.allocate(Parent); |
| 8587 | Elts.push_back(Region); |
| 8588 | Visit(*I); |
| 8589 | } |
| 8590 | |
| 8591 | // Forget that the initializers are sequenced. |
| 8592 | Region = Parent; |
| 8593 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 8594 | Tree.merge(Elts[I]); |
| 8595 | } |
| 8596 | |
| 8597 | void VisitInitListExpr(InitListExpr *ILE) { |
| 8598 | if (!SemaRef.getLangOpts().CPlusPlus11) |
| 8599 | return VisitExpr(ILE); |
| 8600 | |
| 8601 | // In C++11, list initializations are sequenced. |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8602 | SmallVector<SequenceTree::Seq, 32> Elts; |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8603 | SequenceTree::Seq Parent = Region; |
| 8604 | for (unsigned I = 0; I < ILE->getNumInits(); ++I) { |
| 8605 | Expr *E = ILE->getInit(I); |
| 8606 | if (!E) continue; |
| 8607 | Region = Tree.allocate(Parent); |
| 8608 | Elts.push_back(Region); |
| 8609 | Visit(E); |
| 8610 | } |
| 8611 | |
| 8612 | // Forget that the initializers are sequenced. |
| 8613 | Region = Parent; |
| 8614 | for (unsigned I = 0; I < Elts.size(); ++I) |
| 8615 | Tree.merge(Elts[I]); |
| 8616 | } |
| 8617 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8618 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8619 | |
| 8620 | void Sema::CheckUnsequencedOperations(Expr *E) { |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8621 | SmallVector<Expr *, 8> WorkList; |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 8622 | WorkList.push_back(E); |
| 8623 | while (!WorkList.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 8624 | Expr *Item = WorkList.pop_back_val(); |
Richard Smith | d33f520 | 2013-01-17 23:18:09 +0000 | [diff] [blame] | 8625 | SequenceChecker(*this, Item, WorkList); |
| 8626 | } |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8627 | } |
| 8628 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8629 | void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, |
| 8630 | bool IsConstexpr) { |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8631 | CheckImplicitConversions(E, CheckLoc); |
| 8632 | CheckUnsequencedOperations(E); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8633 | if (!IsConstexpr && !E->isValueDependent()) |
| 8634 | CheckForIntOverflow(E); |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 8635 | } |
| 8636 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 8637 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 8638 | FieldDecl *BitField, |
| 8639 | Expr *Init) { |
| 8640 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 8641 | } |
| 8642 | |
David Majnemer | 61a5bbf | 2015-04-07 22:08:51 +0000 | [diff] [blame] | 8643 | static void diagnoseArrayStarInParamType(Sema &S, QualType PType, |
| 8644 | SourceLocation Loc) { |
| 8645 | if (!PType->isVariablyModifiedType()) |
| 8646 | return; |
| 8647 | if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { |
| 8648 | diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); |
| 8649 | return; |
| 8650 | } |
David Majnemer | df8f73f | 2015-04-09 19:53:25 +0000 | [diff] [blame] | 8651 | if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { |
| 8652 | diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); |
| 8653 | return; |
| 8654 | } |
David Majnemer | 61a5bbf | 2015-04-07 22:08:51 +0000 | [diff] [blame] | 8655 | if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { |
| 8656 | diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); |
| 8657 | return; |
| 8658 | } |
| 8659 | |
| 8660 | const ArrayType *AT = S.Context.getAsArrayType(PType); |
| 8661 | if (!AT) |
| 8662 | return; |
| 8663 | |
| 8664 | if (AT->getSizeModifier() != ArrayType::Star) { |
| 8665 | diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); |
| 8666 | return; |
| 8667 | } |
| 8668 | |
| 8669 | S.Diag(Loc, diag::err_array_star_in_function_definition); |
| 8670 | } |
| 8671 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8672 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 8673 | /// function are appropriate for the definition of a function. This |
| 8674 | /// takes care of any checks that cannot be performed on the |
| 8675 | /// declaration itself, e.g., that the types of each of the function |
| 8676 | /// parameters are complete. |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 8677 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P, |
| 8678 | ParmVarDecl *const *PEnd, |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8679 | bool CheckParameterNames) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8680 | bool HasInvalidParm = false; |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8681 | for (; P != PEnd; ++P) { |
| 8682 | ParmVarDecl *Param = *P; |
| 8683 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8684 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 8685 | // function declarator that is part of a function definition of |
| 8686 | // that function shall not have incomplete type. |
| 8687 | // |
| 8688 | // This is also C++ [dcl.fct]p6. |
| 8689 | if (!Param->isInvalidDecl() && |
| 8690 | RequireCompleteType(Param->getLocation(), Param->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 8691 | diag::err_typecheck_decl_incomplete_type)) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8692 | Param->setInvalidDecl(); |
| 8693 | HasInvalidParm = true; |
| 8694 | } |
| 8695 | |
| 8696 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 8697 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8698 | if (CheckParameterNames && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8699 | Param->getIdentifier() == nullptr && |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8700 | !Param->isImplicit() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8701 | !getLangOpts().CPlusPlus) |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8702 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 8703 | |
| 8704 | // C99 6.7.5.3p12: |
| 8705 | // If the function declarator is not part of a definition of that |
| 8706 | // function, parameters may have incomplete type and may use the [*] |
| 8707 | // notation in their sequences of declarator specifiers to specify |
| 8708 | // variable length array types. |
| 8709 | QualType PType = Param->getOriginalType(); |
David Majnemer | 61a5bbf | 2015-04-07 22:08:51 +0000 | [diff] [blame] | 8710 | // FIXME: This diagnostic should point the '[*]' if source-location |
| 8711 | // information is added for it. |
| 8712 | diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 8713 | |
| 8714 | // MSVC destroys objects passed by value in the callee. Therefore a |
| 8715 | // 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] | 8716 | // object's destructor. However, we don't perform any direct access check |
| 8717 | // on the dtor. |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 8718 | if (getLangOpts().CPlusPlus && Context.getTargetInfo() |
| 8719 | .getCXXABI() |
| 8720 | .areArgsDestroyedLeftToRightInCallee()) { |
Hans Wennborg | 13ac4bd | 2014-01-13 19:24:31 +0000 | [diff] [blame] | 8721 | if (!Param->isInvalidDecl()) { |
| 8722 | if (const RecordType *RT = Param->getType()->getAs<RecordType>()) { |
| 8723 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 8724 | if (!ClassDecl->isInvalidDecl() && |
| 8725 | !ClassDecl->hasIrrelevantDestructor() && |
| 8726 | !ClassDecl->isDependentContext()) { |
| 8727 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 8728 | MarkFunctionReferenced(Param->getLocation(), Destructor); |
| 8729 | DiagnoseUseOfDecl(Destructor, Param->getLocation()); |
| 8730 | } |
Hans Wennborg | 0f3c10c | 2014-01-13 17:23:24 +0000 | [diff] [blame] | 8731 | } |
| 8732 | } |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 8733 | } |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8734 | |
| 8735 | // Parameters with the pass_object_size attribute only need to be marked |
| 8736 | // constant at function definitions. Because we lack information about |
| 8737 | // whether we're on a declaration or definition when we're instantiating the |
| 8738 | // attribute, we need to check for constness here. |
| 8739 | if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>()) |
| 8740 | if (!Param->getType().isConstQualified()) |
| 8741 | Diag(Param->getLocation(), diag::err_attribute_pointers_only) |
| 8742 | << Attr->getSpelling() << 1; |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 8743 | } |
| 8744 | |
| 8745 | return HasInvalidParm; |
| 8746 | } |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 8747 | |
| 8748 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 8749 | /// pointer cast increases the alignment requirements. |
| 8750 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 8751 | // This is actually a lot of work to potentially be doing on every |
| 8752 | // 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] | 8753 | if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 8754 | return; |
| 8755 | |
| 8756 | // Ignore dependent types. |
| 8757 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 8758 | return; |
| 8759 | |
| 8760 | // Require that the destination be a pointer type. |
| 8761 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 8762 | if (!DestPtr) return; |
| 8763 | |
| 8764 | // If the destination has alignment 1, we're done. |
| 8765 | QualType DestPointee = DestPtr->getPointeeType(); |
| 8766 | if (DestPointee->isIncompleteType()) return; |
| 8767 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 8768 | if (DestAlign.isOne()) return; |
| 8769 | |
| 8770 | // Require that the source be a pointer type. |
| 8771 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 8772 | if (!SrcPtr) return; |
| 8773 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 8774 | |
| 8775 | // Whitelist casts from cv void*. We already implicitly |
| 8776 | // whitelisted casts to cv void*, since they have alignment 1. |
| 8777 | // Also whitelist casts involving incomplete types, which implicitly |
| 8778 | // includes 'void'. |
| 8779 | if (SrcPointee->isIncompleteType()) return; |
| 8780 | |
| 8781 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 8782 | if (SrcAlign >= DestAlign) return; |
| 8783 | |
| 8784 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 8785 | << Op->getType() << T |
| 8786 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 8787 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 8788 | << TRange << Op->getSourceRange(); |
| 8789 | } |
| 8790 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8791 | static const Type* getElementType(const Expr *BaseExpr) { |
| 8792 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 8793 | if (EltType->isAnyPointerType()) |
| 8794 | return EltType->getPointeeType().getTypePtr(); |
| 8795 | else if (EltType->isArrayType()) |
| 8796 | return EltType->getBaseElementTypeUnsafe(); |
| 8797 | return EltType; |
| 8798 | } |
| 8799 | |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 8800 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 8801 | /// array member of a struct. |
| 8802 | /// |
| 8803 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 8804 | /// commonly used to emulate flexible arrays in C89 code. |
| 8805 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 8806 | const NamedDecl *ND) { |
| 8807 | if (Size != 1 || !ND) return false; |
| 8808 | |
| 8809 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 8810 | if (!FD) return false; |
| 8811 | |
| 8812 | // Don't consider sizes resulting from macro expansions or template argument |
| 8813 | // substitution to form C89 tail-padded arrays. |
Sean Callanan | 06a48a6 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 8814 | |
| 8815 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 8816 | while (TInfo) { |
| 8817 | TypeLoc TL = TInfo->getTypeLoc(); |
| 8818 | // Look through typedefs. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8819 | if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) { |
| 8820 | const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 8821 | TInfo = TDL->getTypeSourceInfo(); |
| 8822 | continue; |
| 8823 | } |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8824 | if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) { |
| 8825 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); |
Chad Rosier | 7029992 | 2013-02-06 00:58:34 +0000 | [diff] [blame] | 8826 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 8827 | return false; |
| 8828 | } |
Ted Kremenek | 7ebb493 | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 8829 | break; |
Sean Callanan | 06a48a6 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 8830 | } |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 8831 | |
| 8832 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
Matt Beaumont-Gay | c93b489 | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 8833 | if (!RD) return false; |
| 8834 | if (RD->isUnion()) return false; |
| 8835 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8836 | if (!CRD->isStandardLayout()) return false; |
| 8837 | } |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 8838 | |
Benjamin Kramer | 8c54367 | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 8839 | // See if this is the last field decl in the record. |
| 8840 | const Decl *D = FD; |
| 8841 | while ((D = D->getNextDeclInContext())) |
| 8842 | if (isa<FieldDecl>(D)) |
| 8843 | return false; |
| 8844 | return true; |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 8845 | } |
| 8846 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8847 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8848 | const ArraySubscriptExpr *ASE, |
Richard Smith | 13f6718 | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 8849 | bool AllowOnePastEnd, bool IndexNegated) { |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8850 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8851 | if (IndexExpr->isValueDependent()) |
| 8852 | return; |
| 8853 | |
Matt Beaumont-Gay | 9d570c4 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 8854 | const Type *EffectiveType = getElementType(BaseExpr); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8855 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8856 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8857 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8858 | if (!ArrayTy) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8859 | return; |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8860 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8861 | llvm::APSInt index; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 8862 | if (!IndexExpr->EvaluateAsInt(index, Context, Expr::SE_AllowSideEffects)) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8863 | return; |
Richard Smith | 13f6718 | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 8864 | if (IndexNegated) |
| 8865 | index = -index; |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 8866 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8867 | const NamedDecl *ND = nullptr; |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8868 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 8869 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 8870 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8871 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8872 | |
Ted Kremenek | e4b316c | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 8873 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 8874 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8875 | if (!size.isStrictlyPositive()) |
| 8876 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8877 | |
| 8878 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | 7c29980 | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 8879 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8880 | // Make sure we're comparing apples to apples when comparing index to size |
| 8881 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 8882 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | 0fb0bb1 | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 8883 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | e535376 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 8884 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8885 | if (ptrarith_typesize != array_typesize) { |
| 8886 | // There's a cast to a different size type involved |
| 8887 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 8888 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 8889 | // multiple of ptrarith_typesize |
| 8890 | if (ptrarith_typesize * ratio == array_typesize) |
| 8891 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 8892 | } |
| 8893 | } |
| 8894 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8895 | if (size.getBitWidth() > index.getBitWidth()) |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8896 | index = index.zext(size.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 8897 | else if (size.getBitWidth() < index.getBitWidth()) |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8898 | size = size.zext(index.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 8899 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8900 | // For array subscripting the index must be less than size, but for pointer |
| 8901 | // arithmetic also allow the index (offset) to be equal to size since |
| 8902 | // computing the next address after the end of the array is legal and |
| 8903 | // commonly done e.g. in C++ iterators and range-based for loops. |
Eli Friedman | 84e6e5c | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 8904 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 8905 | return; |
| 8906 | |
| 8907 | // Also don't warn for arrays of size 1 which are members of some |
| 8908 | // structure. These are often used to approximate flexible arrays in C89 |
| 8909 | // code. |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8910 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 8911 | return; |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8912 | |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8913 | // Suppress the warning if the subscript expression (as identified by the |
| 8914 | // ']' location) and the index expression are both from macro expansions |
| 8915 | // within a system header. |
| 8916 | if (ASE) { |
| 8917 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( |
| 8918 | ASE->getRBracketLoc()); |
| 8919 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { |
| 8920 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( |
| 8921 | IndexExpr->getLocStart()); |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 8922 | if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8923 | return; |
| 8924 | } |
| 8925 | } |
| 8926 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8927 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8928 | if (ASE) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8929 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 8930 | |
| 8931 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 8932 | PDiag(DiagID) << index.toString(10, true) |
| 8933 | << size.toString(10, true) |
| 8934 | << (unsigned)size.getLimitedValue(~0U) |
| 8935 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 8936 | } else { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8937 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8938 | if (!ASE) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8939 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 8940 | if (index.isNegative()) index = -index; |
| 8941 | } |
| 8942 | |
| 8943 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 8944 | PDiag(DiagID) << index.toString(10, true) |
| 8945 | << IndexExpr->getSourceRange()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8946 | } |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8947 | |
Matt Beaumont-Gay | b233982 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 8948 | if (!ND) { |
| 8949 | // Try harder to find a NamedDecl to point at in the note. |
| 8950 | while (const ArraySubscriptExpr *ASE = |
| 8951 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) |
| 8952 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); |
| 8953 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 8954 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
| 8955 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
| 8956 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
| 8957 | } |
| 8958 | |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 8959 | if (ND) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8960 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 8961 | PDiag(diag::note_array_index_out_of_bounds) |
| 8962 | << ND->getDeclName()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8963 | } |
| 8964 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8965 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8966 | int AllowOnePastEnd = 0; |
| 8967 | while (expr) { |
| 8968 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8969 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8970 | case Stmt::ArraySubscriptExprClass: { |
| 8971 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
Matt Beaumont-Gay | 5533a55 | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 8972 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8973 | AllowOnePastEnd > 0); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8974 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8975 | } |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 8976 | case Stmt::OMPArraySectionExprClass: { |
| 8977 | const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr); |
| 8978 | if (ASE->getLowerBound()) |
| 8979 | CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(), |
| 8980 | /*ASE=*/nullptr, AllowOnePastEnd > 0); |
| 8981 | return; |
| 8982 | } |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8983 | case Stmt::UnaryOperatorClass: { |
| 8984 | // Only unwrap the * and & unary operators |
| 8985 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 8986 | expr = UO->getSubExpr(); |
| 8987 | switch (UO->getOpcode()) { |
| 8988 | case UO_AddrOf: |
| 8989 | AllowOnePastEnd++; |
| 8990 | break; |
| 8991 | case UO_Deref: |
| 8992 | AllowOnePastEnd--; |
| 8993 | break; |
| 8994 | default: |
| 8995 | return; |
| 8996 | } |
| 8997 | break; |
| 8998 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8999 | case Stmt::ConditionalOperatorClass: { |
| 9000 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 9001 | if (const Expr *lhs = cond->getLHS()) |
| 9002 | CheckArrayAccess(lhs); |
| 9003 | if (const Expr *rhs = cond->getRHS()) |
| 9004 | CheckArrayAccess(rhs); |
| 9005 | return; |
| 9006 | } |
| 9007 | default: |
| 9008 | return; |
| 9009 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 9010 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 9011 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9012 | |
| 9013 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 9014 | |
| 9015 | namespace { |
| 9016 | struct RetainCycleOwner { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9017 | RetainCycleOwner() : Variable(nullptr), Indirect(false) {} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9018 | VarDecl *Variable; |
| 9019 | SourceRange Range; |
| 9020 | SourceLocation Loc; |
| 9021 | bool Indirect; |
| 9022 | |
| 9023 | void setLocsFrom(Expr *e) { |
| 9024 | Loc = e->getExprLoc(); |
| 9025 | Range = e->getSourceRange(); |
| 9026 | } |
| 9027 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 9028 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9029 | |
| 9030 | /// Consider whether capturing the given variable can possibly lead to |
| 9031 | /// a retain cycle. |
| 9032 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 9033 | // In ARC, it's captured strongly iff the variable has __strong |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9034 | // lifetime. In MRR, it's captured strongly if the variable is |
| 9035 | // __block and has an appropriate type. |
| 9036 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 9037 | return false; |
| 9038 | |
| 9039 | owner.Variable = var; |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 9040 | if (ref) |
| 9041 | owner.setLocsFrom(ref); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9042 | return true; |
| 9043 | } |
| 9044 | |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 9045 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9046 | while (true) { |
| 9047 | e = e->IgnoreParens(); |
| 9048 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 9049 | switch (cast->getCastKind()) { |
| 9050 | case CK_BitCast: |
| 9051 | case CK_LValueBitCast: |
| 9052 | case CK_LValueToRValue: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9053 | case CK_ARCReclaimReturnedObject: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9054 | e = cast->getSubExpr(); |
| 9055 | continue; |
| 9056 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9057 | default: |
| 9058 | return false; |
| 9059 | } |
| 9060 | } |
| 9061 | |
| 9062 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 9063 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 9064 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 9065 | return false; |
| 9066 | |
| 9067 | // Try to find a retain cycle in the base. |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 9068 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9069 | return false; |
| 9070 | |
| 9071 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 9072 | owner.Indirect = true; |
| 9073 | return true; |
| 9074 | } |
| 9075 | |
| 9076 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 9077 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 9078 | if (!var) return false; |
| 9079 | return considerVariable(var, ref, owner); |
| 9080 | } |
| 9081 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9082 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 9083 | if (member->isArrow()) return false; |
| 9084 | |
| 9085 | // Don't count this as an indirect ownership. |
| 9086 | e = member->getBase(); |
| 9087 | continue; |
| 9088 | } |
| 9089 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 9090 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { |
| 9091 | // Only pay attention to pseudo-objects on property references. |
| 9092 | ObjCPropertyRefExpr *pre |
| 9093 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() |
| 9094 | ->IgnoreParens()); |
| 9095 | if (!pre) return false; |
| 9096 | if (pre->isImplicitProperty()) return false; |
| 9097 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
| 9098 | if (!property->isRetaining() && |
| 9099 | !(property->getPropertyIvarDecl() && |
| 9100 | property->getPropertyIvarDecl()->getType() |
| 9101 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 9102 | return false; |
| 9103 | |
| 9104 | owner.Indirect = true; |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 9105 | if (pre->isSuperReceiver()) { |
| 9106 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); |
| 9107 | if (!owner.Variable) |
| 9108 | return false; |
| 9109 | owner.Loc = pre->getLocation(); |
| 9110 | owner.Range = pre->getSourceRange(); |
| 9111 | return true; |
| 9112 | } |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 9113 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) |
| 9114 | ->getSourceExpr()); |
| 9115 | continue; |
| 9116 | } |
| 9117 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9118 | // Array ivars? |
| 9119 | |
| 9120 | return false; |
| 9121 | } |
| 9122 | } |
| 9123 | |
| 9124 | namespace { |
| 9125 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 9126 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 9127 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 9128 | Context(Context), Variable(variable), Capturer(nullptr), |
| 9129 | VarWillBeReased(false) {} |
| 9130 | ASTContext &Context; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9131 | VarDecl *Variable; |
| 9132 | Expr *Capturer; |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 9133 | bool VarWillBeReased; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9134 | |
| 9135 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 9136 | if (ref->getDecl() == Variable && !Capturer) |
| 9137 | Capturer = ref; |
| 9138 | } |
| 9139 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9140 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 9141 | if (Capturer) return; |
| 9142 | Visit(ref->getBase()); |
| 9143 | if (Capturer && ref->isFreeIvar()) |
| 9144 | Capturer = ref; |
| 9145 | } |
| 9146 | |
| 9147 | void VisitBlockExpr(BlockExpr *block) { |
| 9148 | // Look inside nested blocks |
| 9149 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 9150 | Visit(block->getBlockDecl()->getBody()); |
| 9151 | } |
Fariborz Jahanian | 0e33754 | 2012-08-31 20:04:47 +0000 | [diff] [blame] | 9152 | |
| 9153 | void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { |
| 9154 | if (Capturer) return; |
| 9155 | if (OVE->getSourceExpr()) |
| 9156 | Visit(OVE->getSourceExpr()); |
| 9157 | } |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 9158 | void VisitBinaryOperator(BinaryOperator *BinOp) { |
| 9159 | if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign) |
| 9160 | return; |
| 9161 | Expr *LHS = BinOp->getLHS(); |
| 9162 | if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) { |
| 9163 | if (DRE->getDecl() != Variable) |
| 9164 | return; |
| 9165 | if (Expr *RHS = BinOp->getRHS()) { |
| 9166 | RHS = RHS->IgnoreParenCasts(); |
| 9167 | llvm::APSInt Value; |
| 9168 | VarWillBeReased = |
| 9169 | (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); |
| 9170 | } |
| 9171 | } |
| 9172 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9173 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 9174 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9175 | |
| 9176 | /// Check whether the given argument is a block which captures a |
| 9177 | /// variable. |
| 9178 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 9179 | assert(owner.Variable && owner.Loc.isValid()); |
| 9180 | |
| 9181 | e = e->IgnoreParenCasts(); |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 9182 | |
| 9183 | // Look through [^{...} copy] and Block_copy(^{...}). |
| 9184 | if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { |
| 9185 | Selector Cmd = ME->getSelector(); |
| 9186 | if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { |
| 9187 | e = ME->getInstanceReceiver(); |
| 9188 | if (!e) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9189 | return nullptr; |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 9190 | e = e->IgnoreParenCasts(); |
| 9191 | } |
| 9192 | } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { |
| 9193 | if (CE->getNumArgs() == 1) { |
| 9194 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); |
Ted Kremenek | b67c6cc | 2012-10-02 04:36:54 +0000 | [diff] [blame] | 9195 | if (Fn) { |
| 9196 | const IdentifierInfo *FnI = Fn->getIdentifier(); |
| 9197 | if (FnI && FnI->isStr("_Block_copy")) { |
| 9198 | e = CE->getArg(0)->IgnoreParenCasts(); |
| 9199 | } |
| 9200 | } |
Jordan Rose | 67e887c | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 9201 | } |
| 9202 | } |
| 9203 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9204 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 9205 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9206 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9207 | |
| 9208 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 9209 | visitor.Visit(block->getBlockDecl()->getBody()); |
Fariborz Jahanian | 8df9e24 | 2014-06-12 20:57:14 +0000 | [diff] [blame] | 9210 | return visitor.VarWillBeReased ? nullptr : visitor.Capturer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9211 | } |
| 9212 | |
| 9213 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 9214 | RetainCycleOwner &owner) { |
| 9215 | assert(capturer); |
| 9216 | assert(owner.Variable && owner.Loc.isValid()); |
| 9217 | |
| 9218 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 9219 | << owner.Variable << capturer->getSourceRange(); |
| 9220 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 9221 | << owner.Indirect << owner.Range; |
| 9222 | } |
| 9223 | |
| 9224 | /// Check for a keyword selector that starts with the word 'add' or |
| 9225 | /// 'set'. |
| 9226 | static bool isSetterLikeSelector(Selector sel) { |
| 9227 | if (sel.isUnarySelector()) return false; |
| 9228 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9229 | StringRef str = sel.getNameForSlot(0); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9230 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
Ted Kremenek | 764d63a | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 9231 | if (str.startswith("set")) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9232 | str = str.substr(3); |
Ted Kremenek | 764d63a | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 9233 | else if (str.startswith("add")) { |
| 9234 | // Specially whitelist 'addOperationWithBlock:'. |
| 9235 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) |
| 9236 | return false; |
| 9237 | str = str.substr(3); |
| 9238 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9239 | else |
| 9240 | return false; |
| 9241 | |
| 9242 | if (str.empty()) return true; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 9243 | return !isLowercase(str.front()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9244 | } |
| 9245 | |
Benjamin Kramer | 3a74345 | 2015-03-09 15:03:32 +0000 | [diff] [blame] | 9246 | static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S, |
| 9247 | ObjCMessageExpr *Message) { |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9248 | bool IsMutableArray = S.NSAPIObj->isSubclassOfNSClass( |
| 9249 | Message->getReceiverInterface(), |
| 9250 | NSAPI::ClassId_NSMutableArray); |
| 9251 | if (!IsMutableArray) { |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9252 | return None; |
| 9253 | } |
| 9254 | |
| 9255 | Selector Sel = Message->getSelector(); |
| 9256 | |
| 9257 | Optional<NSAPI::NSArrayMethodKind> MKOpt = |
| 9258 | S.NSAPIObj->getNSArrayMethodKind(Sel); |
| 9259 | if (!MKOpt) { |
| 9260 | return None; |
| 9261 | } |
| 9262 | |
| 9263 | NSAPI::NSArrayMethodKind MK = *MKOpt; |
| 9264 | |
| 9265 | switch (MK) { |
| 9266 | case NSAPI::NSMutableArr_addObject: |
| 9267 | case NSAPI::NSMutableArr_insertObjectAtIndex: |
| 9268 | case NSAPI::NSMutableArr_setObjectAtIndexedSubscript: |
| 9269 | return 0; |
| 9270 | case NSAPI::NSMutableArr_replaceObjectAtIndex: |
| 9271 | return 1; |
| 9272 | |
| 9273 | default: |
| 9274 | return None; |
| 9275 | } |
| 9276 | |
| 9277 | return None; |
| 9278 | } |
| 9279 | |
| 9280 | static |
| 9281 | Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S, |
| 9282 | ObjCMessageExpr *Message) { |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9283 | bool IsMutableDictionary = S.NSAPIObj->isSubclassOfNSClass( |
| 9284 | Message->getReceiverInterface(), |
| 9285 | NSAPI::ClassId_NSMutableDictionary); |
| 9286 | if (!IsMutableDictionary) { |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9287 | return None; |
| 9288 | } |
| 9289 | |
| 9290 | Selector Sel = Message->getSelector(); |
| 9291 | |
| 9292 | Optional<NSAPI::NSDictionaryMethodKind> MKOpt = |
| 9293 | S.NSAPIObj->getNSDictionaryMethodKind(Sel); |
| 9294 | if (!MKOpt) { |
| 9295 | return None; |
| 9296 | } |
| 9297 | |
| 9298 | NSAPI::NSDictionaryMethodKind MK = *MKOpt; |
| 9299 | |
| 9300 | switch (MK) { |
| 9301 | case NSAPI::NSMutableDict_setObjectForKey: |
| 9302 | case NSAPI::NSMutableDict_setValueForKey: |
| 9303 | case NSAPI::NSMutableDict_setObjectForKeyedSubscript: |
| 9304 | return 0; |
| 9305 | |
| 9306 | default: |
| 9307 | return None; |
| 9308 | } |
| 9309 | |
| 9310 | return None; |
| 9311 | } |
| 9312 | |
| 9313 | static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) { |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9314 | bool IsMutableSet = S.NSAPIObj->isSubclassOfNSClass( |
| 9315 | Message->getReceiverInterface(), |
| 9316 | NSAPI::ClassId_NSMutableSet); |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9317 | |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9318 | bool IsMutableOrderedSet = S.NSAPIObj->isSubclassOfNSClass( |
| 9319 | Message->getReceiverInterface(), |
| 9320 | NSAPI::ClassId_NSMutableOrderedSet); |
| 9321 | if (!IsMutableSet && !IsMutableOrderedSet) { |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9322 | return None; |
| 9323 | } |
| 9324 | |
| 9325 | Selector Sel = Message->getSelector(); |
| 9326 | |
| 9327 | Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel); |
| 9328 | if (!MKOpt) { |
| 9329 | return None; |
| 9330 | } |
| 9331 | |
| 9332 | NSAPI::NSSetMethodKind MK = *MKOpt; |
| 9333 | |
| 9334 | switch (MK) { |
| 9335 | case NSAPI::NSMutableSet_addObject: |
| 9336 | case NSAPI::NSOrderedSet_setObjectAtIndex: |
| 9337 | case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript: |
| 9338 | case NSAPI::NSOrderedSet_insertObjectAtIndex: |
| 9339 | return 0; |
| 9340 | case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject: |
| 9341 | return 1; |
| 9342 | } |
| 9343 | |
| 9344 | return None; |
| 9345 | } |
| 9346 | |
| 9347 | void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) { |
| 9348 | if (!Message->isInstanceMessage()) { |
| 9349 | return; |
| 9350 | } |
| 9351 | |
| 9352 | Optional<int> ArgOpt; |
| 9353 | |
| 9354 | if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) && |
| 9355 | !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) && |
| 9356 | !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) { |
| 9357 | return; |
| 9358 | } |
| 9359 | |
| 9360 | int ArgIndex = *ArgOpt; |
| 9361 | |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9362 | Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts(); |
| 9363 | if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) { |
| 9364 | Arg = OE->getSourceExpr()->IgnoreImpCasts(); |
| 9365 | } |
| 9366 | |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9367 | if (Message->getReceiverKind() == ObjCMessageExpr::SuperInstance) { |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9368 | if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9369 | if (ArgRE->isObjCSelfExpr()) { |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9370 | Diag(Message->getSourceRange().getBegin(), |
| 9371 | diag::warn_objc_circular_container) |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9372 | << ArgRE->getDecl()->getName() << StringRef("super"); |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9373 | } |
| 9374 | } |
Alex Denisov | 5dfac81 | 2015-08-06 04:51:14 +0000 | [diff] [blame] | 9375 | } else { |
| 9376 | Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts(); |
| 9377 | |
| 9378 | if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) { |
| 9379 | Receiver = OE->getSourceExpr()->IgnoreImpCasts(); |
| 9380 | } |
| 9381 | |
| 9382 | if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) { |
| 9383 | if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { |
| 9384 | if (ReceiverRE->getDecl() == ArgRE->getDecl()) { |
| 9385 | ValueDecl *Decl = ReceiverRE->getDecl(); |
| 9386 | Diag(Message->getSourceRange().getBegin(), |
| 9387 | diag::warn_objc_circular_container) |
| 9388 | << Decl->getName() << Decl->getName(); |
| 9389 | if (!ArgRE->isObjCSelfExpr()) { |
| 9390 | Diag(Decl->getLocation(), |
| 9391 | diag::note_objc_circular_container_declared_here) |
| 9392 | << Decl->getName(); |
| 9393 | } |
| 9394 | } |
| 9395 | } |
| 9396 | } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) { |
| 9397 | if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) { |
| 9398 | if (IvarRE->getDecl() == IvarArgRE->getDecl()) { |
| 9399 | ObjCIvarDecl *Decl = IvarRE->getDecl(); |
| 9400 | Diag(Message->getSourceRange().getBegin(), |
| 9401 | diag::warn_objc_circular_container) |
| 9402 | << Decl->getName() << Decl->getName(); |
| 9403 | Diag(Decl->getLocation(), |
| 9404 | diag::note_objc_circular_container_declared_here) |
| 9405 | << Decl->getName(); |
| 9406 | } |
Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 9407 | } |
| 9408 | } |
| 9409 | } |
| 9410 | |
| 9411 | } |
| 9412 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9413 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 9414 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 9415 | // Only check instance methods whose selector looks like a setter. |
| 9416 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 9417 | return; |
| 9418 | |
| 9419 | // Try to find a variable that the receiver is strongly owned by. |
| 9420 | RetainCycleOwner owner; |
| 9421 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 9422 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9423 | return; |
| 9424 | } else { |
| 9425 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 9426 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 9427 | owner.Loc = msg->getSuperLoc(); |
| 9428 | owner.Range = msg->getSuperLoc(); |
| 9429 | } |
| 9430 | |
| 9431 | // Check whether the receiver is captured by any of the arguments. |
| 9432 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 9433 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 9434 | return diagnoseRetainCycle(*this, capturer, owner); |
| 9435 | } |
| 9436 | |
| 9437 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 9438 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 9439 | RetainCycleOwner owner; |
Fariborz Jahanian | edbc345 | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 9440 | if (!findRetainCycleOwner(*this, receiver, owner)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9441 | return; |
| 9442 | |
| 9443 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 9444 | diagnoseRetainCycle(*this, capturer, owner); |
| 9445 | } |
| 9446 | |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 9447 | void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { |
| 9448 | RetainCycleOwner Owner; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 9449 | if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner)) |
Jordan Rose | fa9e4ba | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 9450 | return; |
| 9451 | |
| 9452 | // Because we don't have an expression for the variable, we have to set the |
| 9453 | // location explicitly here. |
| 9454 | Owner.Loc = Var->getLocation(); |
| 9455 | Owner.Range = Var->getSourceRange(); |
| 9456 | |
| 9457 | if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) |
| 9458 | diagnoseRetainCycle(*this, Capturer, Owner); |
| 9459 | } |
| 9460 | |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 9461 | static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, |
| 9462 | Expr *RHS, bool isProperty) { |
| 9463 | // Check if RHS is an Objective-C object literal, which also can get |
| 9464 | // immediately zapped in a weak reference. Note that we explicitly |
| 9465 | // allow ObjCStringLiterals, since those are designed to never really die. |
| 9466 | RHS = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 9467 | |
Ted Kremenek | 6487335 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 9468 | // This enum needs to match with the 'select' in |
| 9469 | // warn_objc_arc_literal_assign (off-by-1). |
| 9470 | Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); |
| 9471 | if (Kind == Sema::LK_String || Kind == Sema::LK_None) |
| 9472 | return false; |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 9473 | |
| 9474 | S.Diag(Loc, diag::warn_arc_literal_assign) |
Ted Kremenek | 6487335 | 2012-12-21 22:46:35 +0000 | [diff] [blame] | 9475 | << (unsigned) Kind |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 9476 | << (isProperty ? 0 : 1) |
| 9477 | << RHS->getSourceRange(); |
Ted Kremenek | 44c2a2a | 2012-12-21 21:59:39 +0000 | [diff] [blame] | 9478 | |
| 9479 | return true; |
Ted Kremenek | 9304da9 | 2012-12-21 08:04:28 +0000 | [diff] [blame] | 9480 | } |
| 9481 | |
Ted Kremenek | c1f014a | 2012-12-21 19:45:30 +0000 | [diff] [blame] | 9482 | static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, |
| 9483 | Qualifiers::ObjCLifetime LT, |
| 9484 | Expr *RHS, bool isProperty) { |
| 9485 | // Strip off any implicit cast added to get to the one ARC-specific. |
| 9486 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
| 9487 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
| 9488 | S.Diag(Loc, diag::warn_arc_retained_assign) |
| 9489 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 9490 | << (isProperty ? 0 : 1) |
| 9491 | << RHS->getSourceRange(); |
| 9492 | return true; |
| 9493 | } |
| 9494 | RHS = cast->getSubExpr(); |
| 9495 | } |
| 9496 | |
| 9497 | if (LT == Qualifiers::OCL_Weak && |
| 9498 | checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) |
| 9499 | return true; |
| 9500 | |
| 9501 | return false; |
| 9502 | } |
| 9503 | |
Ted Kremenek | b36234d | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 9504 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
| 9505 | QualType LHS, Expr *RHS) { |
| 9506 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 9507 | |
| 9508 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
| 9509 | return false; |
| 9510 | |
| 9511 | if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) |
| 9512 | return true; |
| 9513 | |
| 9514 | return false; |
| 9515 | } |
| 9516 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9517 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 9518 | Expr *LHS, Expr *RHS) { |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 9519 | QualType LHSType; |
| 9520 | // PropertyRef on LHS type need be directly obtained from |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 9521 | // its declaration as it has a PseudoType. |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 9522 | ObjCPropertyRefExpr *PRE |
| 9523 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); |
| 9524 | if (PRE && !PRE->isImplicitProperty()) { |
| 9525 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 9526 | if (PD) |
| 9527 | LHSType = PD->getType(); |
| 9528 | } |
| 9529 | |
| 9530 | if (LHSType.isNull()) |
| 9531 | LHSType = LHS->getType(); |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 9532 | |
| 9533 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 9534 | |
| 9535 | if (LT == Qualifiers::OCL_Weak) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 9536 | if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 9537 | getCurFunction()->markSafeWeakUse(LHS); |
| 9538 | } |
| 9539 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9540 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 9541 | return; |
Jordan Rose | 657b5f4 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 9542 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9543 | // FIXME. Check for other life times. |
| 9544 | if (LT != Qualifiers::OCL_None) |
| 9545 | return; |
| 9546 | |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 9547 | if (PRE) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9548 | if (PRE->isImplicitProperty()) |
| 9549 | return; |
| 9550 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 9551 | if (!PD) |
| 9552 | return; |
| 9553 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 9554 | unsigned Attributes = PD->getPropertyAttributes(); |
| 9555 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 9556 | // when 'assign' attribute was not explicitly specified |
| 9557 | // by user, ignore it and rely on property type itself |
| 9558 | // for lifetime info. |
| 9559 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); |
| 9560 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && |
| 9561 | LHSType->isObjCRetainableType()) |
| 9562 | return; |
| 9563 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9564 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9565 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9566 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 9567 | << RHS->getSourceRange(); |
| 9568 | return; |
| 9569 | } |
| 9570 | RHS = cast->getSubExpr(); |
| 9571 | } |
Fariborz Jahanian | c72a807 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 9572 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 9573 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { |
Ted Kremenek | b36234d | 2012-12-21 08:04:20 +0000 | [diff] [blame] | 9574 | if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) |
| 9575 | return; |
Fariborz Jahanian | dabd133 | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 9576 | } |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 9577 | } |
| 9578 | } |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9579 | |
| 9580 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// |
| 9581 | |
| 9582 | namespace { |
| 9583 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, |
| 9584 | SourceLocation StmtLoc, |
| 9585 | const NullStmt *Body) { |
| 9586 | // Do not warn if the body is a macro that expands to nothing, e.g: |
| 9587 | // |
| 9588 | // #define CALL(x) |
| 9589 | // if (condition) |
| 9590 | // CALL(0); |
| 9591 | // |
| 9592 | if (Body->hasLeadingEmptyMacro()) |
| 9593 | return false; |
| 9594 | |
| 9595 | // Get line numbers of statement and body. |
| 9596 | bool StmtLineInvalid; |
Dmitri Gribenko | ad80af8 | 2015-03-15 01:08:23 +0000 | [diff] [blame] | 9597 | unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9598 | &StmtLineInvalid); |
| 9599 | if (StmtLineInvalid) |
| 9600 | return false; |
| 9601 | |
| 9602 | bool BodyLineInvalid; |
| 9603 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), |
| 9604 | &BodyLineInvalid); |
| 9605 | if (BodyLineInvalid) |
| 9606 | return false; |
| 9607 | |
| 9608 | // Warn if null statement and body are on the same line. |
| 9609 | if (StmtLine != BodyLine) |
| 9610 | return false; |
| 9611 | |
| 9612 | return true; |
| 9613 | } |
| 9614 | } // Unnamed namespace |
| 9615 | |
| 9616 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, |
| 9617 | const Stmt *Body, |
| 9618 | unsigned DiagID) { |
| 9619 | // Since this is a syntactic check, don't emit diagnostic for template |
| 9620 | // instantiations, this just adds noise. |
| 9621 | if (CurrentInstantiationScope) |
| 9622 | return; |
| 9623 | |
| 9624 | // The body should be a null statement. |
| 9625 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 9626 | if (!NBody) |
| 9627 | return; |
| 9628 | |
| 9629 | // Do the usual checks. |
| 9630 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 9631 | return; |
| 9632 | |
| 9633 | Diag(NBody->getSemiLoc(), DiagID); |
| 9634 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 9635 | } |
| 9636 | |
| 9637 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, |
| 9638 | const Stmt *PossibleBody) { |
| 9639 | assert(!CurrentInstantiationScope); // Ensured by caller |
| 9640 | |
| 9641 | SourceLocation StmtLoc; |
| 9642 | const Stmt *Body; |
| 9643 | unsigned DiagID; |
| 9644 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { |
| 9645 | StmtLoc = FS->getRParenLoc(); |
| 9646 | Body = FS->getBody(); |
| 9647 | DiagID = diag::warn_empty_for_body; |
| 9648 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { |
| 9649 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); |
| 9650 | Body = WS->getBody(); |
| 9651 | DiagID = diag::warn_empty_while_body; |
| 9652 | } else |
| 9653 | return; // Neither `for' nor `while'. |
| 9654 | |
| 9655 | // The body should be a null statement. |
| 9656 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); |
| 9657 | if (!NBody) |
| 9658 | return; |
| 9659 | |
| 9660 | // Skip expensive checks if diagnostic is disabled. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 9661 | if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9662 | return; |
| 9663 | |
| 9664 | // Do the usual checks. |
| 9665 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) |
| 9666 | return; |
| 9667 | |
| 9668 | // `for(...);' and `while(...);' are popular idioms, so in order to keep |
| 9669 | // noise level low, emit diagnostics only if for/while is followed by a |
| 9670 | // CompoundStmt, e.g.: |
| 9671 | // for (int i = 0; i < n; i++); |
| 9672 | // { |
| 9673 | // a(i); |
| 9674 | // } |
| 9675 | // or if for/while is followed by a statement with more indentation |
| 9676 | // than for/while itself: |
| 9677 | // for (int i = 0; i < n; i++); |
| 9678 | // a(i); |
| 9679 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); |
| 9680 | if (!ProbableTypo) { |
| 9681 | bool BodyColInvalid; |
| 9682 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( |
| 9683 | PossibleBody->getLocStart(), |
| 9684 | &BodyColInvalid); |
| 9685 | if (BodyColInvalid) |
| 9686 | return; |
| 9687 | |
| 9688 | bool StmtColInvalid; |
| 9689 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( |
| 9690 | S->getLocStart(), |
| 9691 | &StmtColInvalid); |
| 9692 | if (StmtColInvalid) |
| 9693 | return; |
| 9694 | |
| 9695 | if (BodyCol > StmtCol) |
| 9696 | ProbableTypo = true; |
| 9697 | } |
| 9698 | |
| 9699 | if (ProbableTypo) { |
| 9700 | Diag(NBody->getSemiLoc(), DiagID); |
| 9701 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); |
| 9702 | } |
| 9703 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9704 | |
Richard Trieu | 36d0b2b | 2015-01-13 02:32:02 +0000 | [diff] [blame] | 9705 | //===--- CHECK: Warn on self move with std::move. -------------------------===// |
| 9706 | |
| 9707 | /// DiagnoseSelfMove - Emits a warning if a value is moved to itself. |
| 9708 | void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, |
| 9709 | SourceLocation OpLoc) { |
| 9710 | |
| 9711 | if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) |
| 9712 | return; |
| 9713 | |
| 9714 | if (!ActiveTemplateInstantiations.empty()) |
| 9715 | return; |
| 9716 | |
| 9717 | // Strip parens and casts away. |
| 9718 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 9719 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 9720 | |
| 9721 | // Check for a call expression |
| 9722 | const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr); |
| 9723 | if (!CE || CE->getNumArgs() != 1) |
| 9724 | return; |
| 9725 | |
| 9726 | // Check for a call to std::move |
| 9727 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 9728 | if (!FD || !FD->isInStdNamespace() || !FD->getIdentifier() || |
| 9729 | !FD->getIdentifier()->isStr("move")) |
| 9730 | return; |
| 9731 | |
| 9732 | // Get argument from std::move |
| 9733 | RHSExpr = CE->getArg(0); |
| 9734 | |
| 9735 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 9736 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 9737 | |
| 9738 | // Two DeclRefExpr's, check that the decls are the same. |
| 9739 | if (LHSDeclRef && RHSDeclRef) { |
| 9740 | if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) |
| 9741 | return; |
| 9742 | if (LHSDeclRef->getDecl()->getCanonicalDecl() != |
| 9743 | RHSDeclRef->getDecl()->getCanonicalDecl()) |
| 9744 | return; |
| 9745 | |
| 9746 | Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() |
| 9747 | << LHSExpr->getSourceRange() |
| 9748 | << RHSExpr->getSourceRange(); |
| 9749 | return; |
| 9750 | } |
| 9751 | |
| 9752 | // Member variables require a different approach to check for self moves. |
| 9753 | // MemberExpr's are the same if every nested MemberExpr refers to the same |
| 9754 | // Decl and that the base Expr's are DeclRefExpr's with the same Decl or |
| 9755 | // the base Expr's are CXXThisExpr's. |
| 9756 | const Expr *LHSBase = LHSExpr; |
| 9757 | const Expr *RHSBase = RHSExpr; |
| 9758 | const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); |
| 9759 | const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); |
| 9760 | if (!LHSME || !RHSME) |
| 9761 | return; |
| 9762 | |
| 9763 | while (LHSME && RHSME) { |
| 9764 | if (LHSME->getMemberDecl()->getCanonicalDecl() != |
| 9765 | RHSME->getMemberDecl()->getCanonicalDecl()) |
| 9766 | return; |
| 9767 | |
| 9768 | LHSBase = LHSME->getBase(); |
| 9769 | RHSBase = RHSME->getBase(); |
| 9770 | LHSME = dyn_cast<MemberExpr>(LHSBase); |
| 9771 | RHSME = dyn_cast<MemberExpr>(RHSBase); |
| 9772 | } |
| 9773 | |
| 9774 | LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); |
| 9775 | RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); |
| 9776 | if (LHSDeclRef && RHSDeclRef) { |
| 9777 | if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) |
| 9778 | return; |
| 9779 | if (LHSDeclRef->getDecl()->getCanonicalDecl() != |
| 9780 | RHSDeclRef->getDecl()->getCanonicalDecl()) |
| 9781 | return; |
| 9782 | |
| 9783 | Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() |
| 9784 | << LHSExpr->getSourceRange() |
| 9785 | << RHSExpr->getSourceRange(); |
| 9786 | return; |
| 9787 | } |
| 9788 | |
| 9789 | if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) |
| 9790 | Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() |
| 9791 | << LHSExpr->getSourceRange() |
| 9792 | << RHSExpr->getSourceRange(); |
| 9793 | } |
| 9794 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9795 | //===--- Layout compatibility ----------------------------------------------// |
| 9796 | |
| 9797 | namespace { |
| 9798 | |
| 9799 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); |
| 9800 | |
| 9801 | /// \brief Check if two enumeration types are layout-compatible. |
| 9802 | bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { |
| 9803 | // C++11 [dcl.enum] p8: |
| 9804 | // Two enumeration types are layout-compatible if they have the same |
| 9805 | // underlying type. |
| 9806 | return ED1->isComplete() && ED2->isComplete() && |
| 9807 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); |
| 9808 | } |
| 9809 | |
| 9810 | /// \brief Check if two fields are layout-compatible. |
| 9811 | bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { |
| 9812 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) |
| 9813 | return false; |
| 9814 | |
| 9815 | if (Field1->isBitField() != Field2->isBitField()) |
| 9816 | return false; |
| 9817 | |
| 9818 | if (Field1->isBitField()) { |
| 9819 | // Make sure that the bit-fields are the same length. |
| 9820 | unsigned Bits1 = Field1->getBitWidthValue(C); |
| 9821 | unsigned Bits2 = Field2->getBitWidthValue(C); |
| 9822 | |
| 9823 | if (Bits1 != Bits2) |
| 9824 | return false; |
| 9825 | } |
| 9826 | |
| 9827 | return true; |
| 9828 | } |
| 9829 | |
| 9830 | /// \brief Check if two standard-layout structs are layout-compatible. |
| 9831 | /// (C++11 [class.mem] p17) |
| 9832 | bool isLayoutCompatibleStruct(ASTContext &C, |
| 9833 | RecordDecl *RD1, |
| 9834 | RecordDecl *RD2) { |
| 9835 | // If both records are C++ classes, check that base classes match. |
| 9836 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { |
| 9837 | // If one of records is a CXXRecordDecl we are in C++ mode, |
| 9838 | // thus the other one is a CXXRecordDecl, too. |
| 9839 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); |
| 9840 | // Check number of base classes. |
| 9841 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) |
| 9842 | return false; |
| 9843 | |
| 9844 | // Check the base classes. |
| 9845 | for (CXXRecordDecl::base_class_const_iterator |
| 9846 | Base1 = D1CXX->bases_begin(), |
| 9847 | BaseEnd1 = D1CXX->bases_end(), |
| 9848 | Base2 = D2CXX->bases_begin(); |
| 9849 | Base1 != BaseEnd1; |
| 9850 | ++Base1, ++Base2) { |
| 9851 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) |
| 9852 | return false; |
| 9853 | } |
| 9854 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { |
| 9855 | // If only RD2 is a C++ class, it should have zero base classes. |
| 9856 | if (D2CXX->getNumBases() > 0) |
| 9857 | return false; |
| 9858 | } |
| 9859 | |
| 9860 | // Check the fields. |
| 9861 | RecordDecl::field_iterator Field2 = RD2->field_begin(), |
| 9862 | Field2End = RD2->field_end(), |
| 9863 | Field1 = RD1->field_begin(), |
| 9864 | Field1End = RD1->field_end(); |
| 9865 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { |
| 9866 | if (!isLayoutCompatible(C, *Field1, *Field2)) |
| 9867 | return false; |
| 9868 | } |
| 9869 | if (Field1 != Field1End || Field2 != Field2End) |
| 9870 | return false; |
| 9871 | |
| 9872 | return true; |
| 9873 | } |
| 9874 | |
| 9875 | /// \brief Check if two standard-layout unions are layout-compatible. |
| 9876 | /// (C++11 [class.mem] p18) |
| 9877 | bool isLayoutCompatibleUnion(ASTContext &C, |
| 9878 | RecordDecl *RD1, |
| 9879 | RecordDecl *RD2) { |
| 9880 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9881 | for (auto *Field2 : RD2->fields()) |
| 9882 | UnmatchedFields.insert(Field2); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9883 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9884 | for (auto *Field1 : RD1->fields()) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9885 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator |
| 9886 | I = UnmatchedFields.begin(), |
| 9887 | E = UnmatchedFields.end(); |
| 9888 | |
| 9889 | for ( ; I != E; ++I) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9890 | if (isLayoutCompatible(C, Field1, *I)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9891 | bool Result = UnmatchedFields.erase(*I); |
| 9892 | (void) Result; |
| 9893 | assert(Result); |
| 9894 | break; |
| 9895 | } |
| 9896 | } |
| 9897 | if (I == E) |
| 9898 | return false; |
| 9899 | } |
| 9900 | |
| 9901 | return UnmatchedFields.empty(); |
| 9902 | } |
| 9903 | |
| 9904 | bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { |
| 9905 | if (RD1->isUnion() != RD2->isUnion()) |
| 9906 | return false; |
| 9907 | |
| 9908 | if (RD1->isUnion()) |
| 9909 | return isLayoutCompatibleUnion(C, RD1, RD2); |
| 9910 | else |
| 9911 | return isLayoutCompatibleStruct(C, RD1, RD2); |
| 9912 | } |
| 9913 | |
| 9914 | /// \brief Check if two types are layout-compatible in C++11 sense. |
| 9915 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { |
| 9916 | if (T1.isNull() || T2.isNull()) |
| 9917 | return false; |
| 9918 | |
| 9919 | // C++11 [basic.types] p11: |
| 9920 | // If two types T1 and T2 are the same type, then T1 and T2 are |
| 9921 | // layout-compatible types. |
| 9922 | if (C.hasSameType(T1, T2)) |
| 9923 | return true; |
| 9924 | |
| 9925 | T1 = T1.getCanonicalType().getUnqualifiedType(); |
| 9926 | T2 = T2.getCanonicalType().getUnqualifiedType(); |
| 9927 | |
| 9928 | const Type::TypeClass TC1 = T1->getTypeClass(); |
| 9929 | const Type::TypeClass TC2 = T2->getTypeClass(); |
| 9930 | |
| 9931 | if (TC1 != TC2) |
| 9932 | return false; |
| 9933 | |
| 9934 | if (TC1 == Type::Enum) { |
| 9935 | return isLayoutCompatible(C, |
| 9936 | cast<EnumType>(T1)->getDecl(), |
| 9937 | cast<EnumType>(T2)->getDecl()); |
| 9938 | } else if (TC1 == Type::Record) { |
| 9939 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) |
| 9940 | return false; |
| 9941 | |
| 9942 | return isLayoutCompatible(C, |
| 9943 | cast<RecordType>(T1)->getDecl(), |
| 9944 | cast<RecordType>(T2)->getDecl()); |
| 9945 | } |
| 9946 | |
| 9947 | return false; |
| 9948 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 9949 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 9950 | |
| 9951 | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// |
| 9952 | |
| 9953 | namespace { |
| 9954 | /// \brief Given a type tag expression find the type tag itself. |
| 9955 | /// |
| 9956 | /// \param TypeExpr Type tag expression, as it appears in user's code. |
| 9957 | /// |
| 9958 | /// \param VD Declaration of an identifier that appears in a type tag. |
| 9959 | /// |
| 9960 | /// \param MagicValue Type tag magic value. |
| 9961 | bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, |
| 9962 | const ValueDecl **VD, uint64_t *MagicValue) { |
| 9963 | while(true) { |
| 9964 | if (!TypeExpr) |
| 9965 | return false; |
| 9966 | |
| 9967 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); |
| 9968 | |
| 9969 | switch (TypeExpr->getStmtClass()) { |
| 9970 | case Stmt::UnaryOperatorClass: { |
| 9971 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); |
| 9972 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { |
| 9973 | TypeExpr = UO->getSubExpr(); |
| 9974 | continue; |
| 9975 | } |
| 9976 | return false; |
| 9977 | } |
| 9978 | |
| 9979 | case Stmt::DeclRefExprClass: { |
| 9980 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); |
| 9981 | *VD = DRE->getDecl(); |
| 9982 | return true; |
| 9983 | } |
| 9984 | |
| 9985 | case Stmt::IntegerLiteralClass: { |
| 9986 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); |
| 9987 | llvm::APInt MagicValueAPInt = IL->getValue(); |
| 9988 | if (MagicValueAPInt.getActiveBits() <= 64) { |
| 9989 | *MagicValue = MagicValueAPInt.getZExtValue(); |
| 9990 | return true; |
| 9991 | } else |
| 9992 | return false; |
| 9993 | } |
| 9994 | |
| 9995 | case Stmt::BinaryConditionalOperatorClass: |
| 9996 | case Stmt::ConditionalOperatorClass: { |
| 9997 | const AbstractConditionalOperator *ACO = |
| 9998 | cast<AbstractConditionalOperator>(TypeExpr); |
| 9999 | bool Result; |
| 10000 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { |
| 10001 | if (Result) |
| 10002 | TypeExpr = ACO->getTrueExpr(); |
| 10003 | else |
| 10004 | TypeExpr = ACO->getFalseExpr(); |
| 10005 | continue; |
| 10006 | } |
| 10007 | return false; |
| 10008 | } |
| 10009 | |
| 10010 | case Stmt::BinaryOperatorClass: { |
| 10011 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); |
| 10012 | if (BO->getOpcode() == BO_Comma) { |
| 10013 | TypeExpr = BO->getRHS(); |
| 10014 | continue; |
| 10015 | } |
| 10016 | return false; |
| 10017 | } |
| 10018 | |
| 10019 | default: |
| 10020 | return false; |
| 10021 | } |
| 10022 | } |
| 10023 | } |
| 10024 | |
| 10025 | /// \brief Retrieve the C type corresponding to type tag TypeExpr. |
| 10026 | /// |
| 10027 | /// \param TypeExpr Expression that specifies a type tag. |
| 10028 | /// |
| 10029 | /// \param MagicValues Registered magic values. |
| 10030 | /// |
| 10031 | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong |
| 10032 | /// kind. |
| 10033 | /// |
| 10034 | /// \param TypeInfo Information about the corresponding C type. |
| 10035 | /// |
| 10036 | /// \returns true if the corresponding C type was found. |
| 10037 | bool GetMatchingCType( |
| 10038 | const IdentifierInfo *ArgumentKind, |
| 10039 | const Expr *TypeExpr, const ASTContext &Ctx, |
| 10040 | const llvm::DenseMap<Sema::TypeTagMagicValue, |
| 10041 | Sema::TypeTagData> *MagicValues, |
| 10042 | bool &FoundWrongKind, |
| 10043 | Sema::TypeTagData &TypeInfo) { |
| 10044 | FoundWrongKind = false; |
| 10045 | |
| 10046 | // Variable declaration that has type_tag_for_datatype attribute. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10047 | const ValueDecl *VD = nullptr; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 10048 | |
| 10049 | uint64_t MagicValue; |
| 10050 | |
| 10051 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) |
| 10052 | return false; |
| 10053 | |
| 10054 | if (VD) { |
Benjamin Kramer | ae852a6 | 2014-02-23 14:34:50 +0000 | [diff] [blame] | 10055 | if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 10056 | if (I->getArgumentKind() != ArgumentKind) { |
| 10057 | FoundWrongKind = true; |
| 10058 | return false; |
| 10059 | } |
| 10060 | TypeInfo.Type = I->getMatchingCType(); |
| 10061 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); |
| 10062 | TypeInfo.MustBeNull = I->getMustBeNull(); |
| 10063 | return true; |
| 10064 | } |
| 10065 | return false; |
| 10066 | } |
| 10067 | |
| 10068 | if (!MagicValues) |
| 10069 | return false; |
| 10070 | |
| 10071 | llvm::DenseMap<Sema::TypeTagMagicValue, |
| 10072 | Sema::TypeTagData>::const_iterator I = |
| 10073 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); |
| 10074 | if (I == MagicValues->end()) |
| 10075 | return false; |
| 10076 | |
| 10077 | TypeInfo = I->second; |
| 10078 | return true; |
| 10079 | } |
| 10080 | } // unnamed namespace |
| 10081 | |
| 10082 | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, |
| 10083 | uint64_t MagicValue, QualType Type, |
| 10084 | bool LayoutCompatible, |
| 10085 | bool MustBeNull) { |
| 10086 | if (!TypeTagForDatatypeMagicValues) |
| 10087 | TypeTagForDatatypeMagicValues.reset( |
| 10088 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); |
| 10089 | |
| 10090 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); |
| 10091 | (*TypeTagForDatatypeMagicValues)[Magic] = |
| 10092 | TypeTagData(Type, LayoutCompatible, MustBeNull); |
| 10093 | } |
| 10094 | |
| 10095 | namespace { |
| 10096 | bool IsSameCharType(QualType T1, QualType T2) { |
| 10097 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); |
| 10098 | if (!BT1) |
| 10099 | return false; |
| 10100 | |
| 10101 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); |
| 10102 | if (!BT2) |
| 10103 | return false; |
| 10104 | |
| 10105 | BuiltinType::Kind T1Kind = BT1->getKind(); |
| 10106 | BuiltinType::Kind T2Kind = BT2->getKind(); |
| 10107 | |
| 10108 | return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || |
| 10109 | (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || |
| 10110 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || |
| 10111 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); |
| 10112 | } |
| 10113 | } // unnamed namespace |
| 10114 | |
| 10115 | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, |
| 10116 | const Expr * const *ExprArgs) { |
| 10117 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); |
| 10118 | bool IsPointerAttr = Attr->getIsPointer(); |
| 10119 | |
| 10120 | const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; |
| 10121 | bool FoundWrongKind; |
| 10122 | TypeTagData TypeInfo; |
| 10123 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, |
| 10124 | TypeTagForDatatypeMagicValues.get(), |
| 10125 | FoundWrongKind, TypeInfo)) { |
| 10126 | if (FoundWrongKind) |
| 10127 | Diag(TypeTagExpr->getExprLoc(), |
| 10128 | diag::warn_type_tag_for_datatype_wrong_kind) |
| 10129 | << TypeTagExpr->getSourceRange(); |
| 10130 | return; |
| 10131 | } |
| 10132 | |
| 10133 | const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; |
| 10134 | if (IsPointerAttr) { |
| 10135 | // Skip implicit cast of pointer to `void *' (as a function argument). |
| 10136 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) |
Dmitri Gribenko | 5ac744e | 2012-11-03 16:07:49 +0000 | [diff] [blame] | 10137 | if (ICE->getType()->isVoidPointerType() && |
Dmitri Gribenko | f21203b | 2012-11-03 22:10:18 +0000 | [diff] [blame] | 10138 | ICE->getCastKind() == CK_BitCast) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 10139 | ArgumentExpr = ICE->getSubExpr(); |
| 10140 | } |
| 10141 | QualType ArgumentType = ArgumentExpr->getType(); |
| 10142 | |
| 10143 | // Passing a `void*' pointer shouldn't trigger a warning. |
| 10144 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) |
| 10145 | return; |
| 10146 | |
| 10147 | if (TypeInfo.MustBeNull) { |
| 10148 | // Type tag with matching void type requires a null pointer. |
| 10149 | if (!ArgumentExpr->isNullPointerConstant(Context, |
| 10150 | Expr::NPC_ValueDependentIsNotNull)) { |
| 10151 | Diag(ArgumentExpr->getExprLoc(), |
| 10152 | diag::warn_type_safety_null_pointer_required) |
| 10153 | << ArgumentKind->getName() |
| 10154 | << ArgumentExpr->getSourceRange() |
| 10155 | << TypeTagExpr->getSourceRange(); |
| 10156 | } |
| 10157 | return; |
| 10158 | } |
| 10159 | |
| 10160 | QualType RequiredType = TypeInfo.Type; |
| 10161 | if (IsPointerAttr) |
| 10162 | RequiredType = Context.getPointerType(RequiredType); |
| 10163 | |
| 10164 | bool mismatch = false; |
| 10165 | if (!TypeInfo.LayoutCompatible) { |
| 10166 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); |
| 10167 | |
| 10168 | // C++11 [basic.fundamental] p1: |
| 10169 | // Plain char, signed char, and unsigned char are three distinct types. |
| 10170 | // |
| 10171 | // But we treat plain `char' as equivalent to `signed char' or `unsigned |
| 10172 | // char' depending on the current char signedness mode. |
| 10173 | if (mismatch) |
| 10174 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), |
| 10175 | RequiredType->getPointeeType())) || |
| 10176 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) |
| 10177 | mismatch = false; |
| 10178 | } else |
| 10179 | if (IsPointerAttr) |
| 10180 | mismatch = !isLayoutCompatible(Context, |
| 10181 | ArgumentType->getPointeeType(), |
| 10182 | RequiredType->getPointeeType()); |
| 10183 | else |
| 10184 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); |
| 10185 | |
| 10186 | if (mismatch) |
| 10187 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) |
Aaron Ballman | 25dc1e1 | 2014-01-03 02:14:08 +0000 | [diff] [blame] | 10188 | << ArgumentType << ArgumentKind |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 10189 | << TypeInfo.LayoutCompatible << RequiredType |
| 10190 | << ArgumentExpr->getSourceRange() |
| 10191 | << TypeTagExpr->getSourceRange(); |
| 10192 | } |