1 //===- SemaChecking.cpp - Extra Semantic Checking -------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements extra semantic analysis beyond what is enforced 10 // by the C type system. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/APValue.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Attr.h" 17 #include "clang/AST/AttrIterator.h" 18 #include "clang/AST/CharUnits.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclBase.h" 21 #include "clang/AST/DeclCXX.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/DeclarationName.h" 24 #include "clang/AST/EvaluatedExprVisitor.h" 25 #include "clang/AST/Expr.h" 26 #include "clang/AST/ExprCXX.h" 27 #include "clang/AST/ExprObjC.h" 28 #include "clang/AST/ExprOpenMP.h" 29 #include "clang/AST/FormatString.h" 30 #include "clang/AST/NSAPI.h" 31 #include "clang/AST/NonTrivialTypeVisitor.h" 32 #include "clang/AST/OperationKinds.h" 33 #include "clang/AST/Stmt.h" 34 #include "clang/AST/TemplateBase.h" 35 #include "clang/AST/Type.h" 36 #include "clang/AST/TypeLoc.h" 37 #include "clang/AST/UnresolvedSet.h" 38 #include "clang/Basic/AddressSpaces.h" 39 #include "clang/Basic/CharInfo.h" 40 #include "clang/Basic/Diagnostic.h" 41 #include "clang/Basic/IdentifierTable.h" 42 #include "clang/Basic/LLVM.h" 43 #include "clang/Basic/LangOptions.h" 44 #include "clang/Basic/OpenCLOptions.h" 45 #include "clang/Basic/OperatorKinds.h" 46 #include "clang/Basic/PartialDiagnostic.h" 47 #include "clang/Basic/SourceLocation.h" 48 #include "clang/Basic/SourceManager.h" 49 #include "clang/Basic/Specifiers.h" 50 #include "clang/Basic/SyncScope.h" 51 #include "clang/Basic/TargetBuiltins.h" 52 #include "clang/Basic/TargetCXXABI.h" 53 #include "clang/Basic/TargetInfo.h" 54 #include "clang/Basic/TypeTraits.h" 55 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. 56 #include "clang/Sema/Initialization.h" 57 #include "clang/Sema/Lookup.h" 58 #include "clang/Sema/Ownership.h" 59 #include "clang/Sema/Scope.h" 60 #include "clang/Sema/ScopeInfo.h" 61 #include "clang/Sema/Sema.h" 62 #include "clang/Sema/SemaInternal.h" 63 #include "llvm/ADT/APFloat.h" 64 #include "llvm/ADT/APInt.h" 65 #include "llvm/ADT/APSInt.h" 66 #include "llvm/ADT/ArrayRef.h" 67 #include "llvm/ADT/DenseMap.h" 68 #include "llvm/ADT/FoldingSet.h" 69 #include "llvm/ADT/None.h" 70 #include "llvm/ADT/Optional.h" 71 #include "llvm/ADT/STLExtras.h" 72 #include "llvm/ADT/SmallBitVector.h" 73 #include "llvm/ADT/SmallPtrSet.h" 74 #include "llvm/ADT/SmallString.h" 75 #include "llvm/ADT/SmallVector.h" 76 #include "llvm/ADT/StringRef.h" 77 #include "llvm/ADT/StringSwitch.h" 78 #include "llvm/ADT/Triple.h" 79 #include "llvm/Support/AtomicOrdering.h" 80 #include "llvm/Support/Casting.h" 81 #include "llvm/Support/Compiler.h" 82 #include "llvm/Support/ConvertUTF.h" 83 #include "llvm/Support/ErrorHandling.h" 84 #include "llvm/Support/Format.h" 85 #include "llvm/Support/Locale.h" 86 #include "llvm/Support/MathExtras.h" 87 #include "llvm/Support/raw_ostream.h" 88 #include <algorithm> 89 #include <cassert> 90 #include <cstddef> 91 #include <cstdint> 92 #include <functional> 93 #include <limits> 94 #include <string> 95 #include <tuple> 96 #include <utility> 97 98 using namespace clang; 99 using namespace sema; 100 101 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, 102 unsigned ByteNo) const { 103 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, 104 Context.getTargetInfo()); 105 } 106 107 /// Checks that a call expression's argument count is the desired number. 108 /// This is useful when doing custom type-checking. Returns true on error. 109 static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { 110 unsigned argCount = call->getNumArgs(); 111 if (argCount == desiredArgCount) return false; 112 113 if (argCount < desiredArgCount) 114 return S.Diag(call->getEndLoc(), diag::err_typecheck_call_too_few_args) 115 << 0 /*function call*/ << desiredArgCount << argCount 116 << call->getSourceRange(); 117 118 // Highlight all the excess arguments. 119 SourceRange range(call->getArg(desiredArgCount)->getBeginLoc(), 120 call->getArg(argCount - 1)->getEndLoc()); 121 122 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) 123 << 0 /*function call*/ << desiredArgCount << argCount 124 << call->getArg(1)->getSourceRange(); 125 } 126 127 /// Check that the first argument to __builtin_annotation is an integer 128 /// and the second argument is a non-wide string literal. 129 static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { 130 if (checkArgCount(S, TheCall, 2)) 131 return true; 132 133 // First argument should be an integer. 134 Expr *ValArg = TheCall->getArg(0); 135 QualType Ty = ValArg->getType(); 136 if (!Ty->isIntegerType()) { 137 S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg) 138 << ValArg->getSourceRange(); 139 return true; 140 } 141 142 // Second argument should be a constant string. 143 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); 144 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); 145 if (!Literal || !Literal->isAscii()) { 146 S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg) 147 << StrArg->getSourceRange(); 148 return true; 149 } 150 151 TheCall->setType(Ty); 152 return false; 153 } 154 155 static bool SemaBuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) { 156 // We need at least one argument. 157 if (TheCall->getNumArgs() < 1) { 158 S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 159 << 0 << 1 << TheCall->getNumArgs() 160 << TheCall->getCallee()->getSourceRange(); 161 return true; 162 } 163 164 // All arguments should be wide string literals. 165 for (Expr *Arg : TheCall->arguments()) { 166 auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 167 if (!Literal || !Literal->isWide()) { 168 S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str) 169 << Arg->getSourceRange(); 170 return true; 171 } 172 } 173 174 return false; 175 } 176 177 /// Check that the argument to __builtin_addressof is a glvalue, and set the 178 /// result type to the corresponding pointer type. 179 static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) { 180 if (checkArgCount(S, TheCall, 1)) 181 return true; 182 183 ExprResult Arg(TheCall->getArg(0)); 184 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc()); 185 if (ResultType.isNull()) 186 return true; 187 188 TheCall->setArg(0, Arg.get()); 189 TheCall->setType(ResultType); 190 return false; 191 } 192 193 static bool SemaBuiltinOverflow(Sema &S, CallExpr *TheCall) { 194 if (checkArgCount(S, TheCall, 3)) 195 return true; 196 197 // First two arguments should be integers. 198 for (unsigned I = 0; I < 2; ++I) { 199 ExprResult Arg = TheCall->getArg(I); 200 QualType Ty = Arg.get()->getType(); 201 if (!Ty->isIntegerType()) { 202 S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int) 203 << Ty << Arg.get()->getSourceRange(); 204 return true; 205 } 206 InitializedEntity Entity = InitializedEntity::InitializeParameter( 207 S.getASTContext(), Ty, /*consume*/ false); 208 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 209 if (Arg.isInvalid()) 210 return true; 211 TheCall->setArg(I, Arg.get()); 212 } 213 214 // Third argument should be a pointer to a non-const integer. 215 // IRGen correctly handles volatile, restrict, and address spaces, and 216 // the other qualifiers aren't possible. 217 { 218 ExprResult Arg = TheCall->getArg(2); 219 QualType Ty = Arg.get()->getType(); 220 const auto *PtrTy = Ty->getAs<PointerType>(); 221 if (!(PtrTy && PtrTy->getPointeeType()->isIntegerType() && 222 !PtrTy->getPointeeType().isConstQualified())) { 223 S.Diag(Arg.get()->getBeginLoc(), 224 diag::err_overflow_builtin_must_be_ptr_int) 225 << Ty << Arg.get()->getSourceRange(); 226 return true; 227 } 228 InitializedEntity Entity = InitializedEntity::InitializeParameter( 229 S.getASTContext(), Ty, /*consume*/ false); 230 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 231 if (Arg.isInvalid()) 232 return true; 233 TheCall->setArg(2, Arg.get()); 234 } 235 return false; 236 } 237 238 static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { 239 if (checkArgCount(S, BuiltinCall, 2)) 240 return true; 241 242 SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc(); 243 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); 244 Expr *Call = BuiltinCall->getArg(0); 245 Expr *Chain = BuiltinCall->getArg(1); 246 247 if (Call->getStmtClass() != Stmt::CallExprClass) { 248 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) 249 << Call->getSourceRange(); 250 return true; 251 } 252 253 auto CE = cast<CallExpr>(Call); 254 if (CE->getCallee()->getType()->isBlockPointerType()) { 255 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) 256 << Call->getSourceRange(); 257 return true; 258 } 259 260 const Decl *TargetDecl = CE->getCalleeDecl(); 261 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) 262 if (FD->getBuiltinID()) { 263 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) 264 << Call->getSourceRange(); 265 return true; 266 } 267 268 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { 269 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) 270 << Call->getSourceRange(); 271 return true; 272 } 273 274 ExprResult ChainResult = S.UsualUnaryConversions(Chain); 275 if (ChainResult.isInvalid()) 276 return true; 277 if (!ChainResult.get()->getType()->isPointerType()) { 278 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) 279 << Chain->getSourceRange(); 280 return true; 281 } 282 283 QualType ReturnTy = CE->getCallReturnType(S.Context); 284 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; 285 QualType BuiltinTy = S.Context.getFunctionType( 286 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); 287 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); 288 289 Builtin = 290 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); 291 292 BuiltinCall->setType(CE->getType()); 293 BuiltinCall->setValueKind(CE->getValueKind()); 294 BuiltinCall->setObjectKind(CE->getObjectKind()); 295 BuiltinCall->setCallee(Builtin); 296 BuiltinCall->setArg(1, ChainResult.get()); 297 298 return false; 299 } 300 301 /// Check a call to BuiltinID for buffer overflows. If BuiltinID is a 302 /// __builtin_*_chk function, then use the object size argument specified in the 303 /// source. Otherwise, infer the object size using __builtin_object_size. 304 void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, 305 CallExpr *TheCall) { 306 // FIXME: There are some more useful checks we could be doing here: 307 // - Analyze the format string of sprintf to see how much of buffer is used. 308 // - Evaluate strlen of strcpy arguments, use as object size. 309 310 unsigned BuiltinID = FD->getBuiltinID(/*ConsiderWrappers=*/true); 311 if (!BuiltinID) 312 return; 313 314 unsigned DiagID = 0; 315 bool IsChkVariant = false; 316 unsigned SizeIndex, ObjectIndex; 317 switch (BuiltinID) { 318 default: 319 return; 320 case Builtin::BI__builtin___memcpy_chk: 321 case Builtin::BI__builtin___memmove_chk: 322 case Builtin::BI__builtin___memset_chk: 323 case Builtin::BI__builtin___strlcat_chk: 324 case Builtin::BI__builtin___strlcpy_chk: 325 case Builtin::BI__builtin___strncat_chk: 326 case Builtin::BI__builtin___strncpy_chk: 327 case Builtin::BI__builtin___stpncpy_chk: 328 case Builtin::BI__builtin___memccpy_chk: { 329 DiagID = diag::warn_builtin_chk_overflow; 330 IsChkVariant = true; 331 SizeIndex = TheCall->getNumArgs() - 2; 332 ObjectIndex = TheCall->getNumArgs() - 1; 333 break; 334 } 335 336 case Builtin::BI__builtin___snprintf_chk: 337 case Builtin::BI__builtin___vsnprintf_chk: { 338 DiagID = diag::warn_builtin_chk_overflow; 339 IsChkVariant = true; 340 SizeIndex = 1; 341 ObjectIndex = 3; 342 break; 343 } 344 345 case Builtin::BIstrncat: 346 case Builtin::BI__builtin_strncat: 347 case Builtin::BIstrncpy: 348 case Builtin::BI__builtin_strncpy: 349 case Builtin::BIstpncpy: 350 case Builtin::BI__builtin_stpncpy: { 351 // Whether these functions overflow depends on the runtime strlen of the 352 // string, not just the buffer size, so emitting the "always overflow" 353 // diagnostic isn't quite right. We should still diagnose passing a buffer 354 // size larger than the destination buffer though; this is a runtime abort 355 // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise. 356 DiagID = diag::warn_fortify_source_size_mismatch; 357 SizeIndex = TheCall->getNumArgs() - 1; 358 ObjectIndex = 0; 359 break; 360 } 361 362 case Builtin::BImemcpy: 363 case Builtin::BI__builtin_memcpy: 364 case Builtin::BImemmove: 365 case Builtin::BI__builtin_memmove: 366 case Builtin::BImemset: 367 case Builtin::BI__builtin_memset: { 368 DiagID = diag::warn_fortify_source_overflow; 369 SizeIndex = TheCall->getNumArgs() - 1; 370 ObjectIndex = 0; 371 break; 372 } 373 case Builtin::BIsnprintf: 374 case Builtin::BI__builtin_snprintf: 375 case Builtin::BIvsnprintf: 376 case Builtin::BI__builtin_vsnprintf: { 377 DiagID = diag::warn_fortify_source_size_mismatch; 378 SizeIndex = 1; 379 ObjectIndex = 0; 380 break; 381 } 382 } 383 384 llvm::APSInt ObjectSize; 385 // For __builtin___*_chk, the object size is explicitly provided by the caller 386 // (usually using __builtin_object_size). Use that value to check this call. 387 if (IsChkVariant) { 388 Expr::EvalResult Result; 389 Expr *SizeArg = TheCall->getArg(ObjectIndex); 390 if (!SizeArg->EvaluateAsInt(Result, getASTContext())) 391 return; 392 ObjectSize = Result.Val.getInt(); 393 394 // Otherwise, try to evaluate an imaginary call to __builtin_object_size. 395 } else { 396 // If the parameter has a pass_object_size attribute, then we should use its 397 // (potentially) more strict checking mode. Otherwise, conservatively assume 398 // type 0. 399 int BOSType = 0; 400 if (const auto *POS = 401 FD->getParamDecl(ObjectIndex)->getAttr<PassObjectSizeAttr>()) 402 BOSType = POS->getType(); 403 404 Expr *ObjArg = TheCall->getArg(ObjectIndex); 405 uint64_t Result; 406 if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType)) 407 return; 408 // Get the object size in the target's size_t width. 409 const TargetInfo &TI = getASTContext().getTargetInfo(); 410 unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType()); 411 ObjectSize = llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth); 412 } 413 414 // Evaluate the number of bytes of the object that this call will use. 415 Expr::EvalResult Result; 416 Expr *UsedSizeArg = TheCall->getArg(SizeIndex); 417 if (!UsedSizeArg->EvaluateAsInt(Result, getASTContext())) 418 return; 419 llvm::APSInt UsedSize = Result.Val.getInt(); 420 421 if (UsedSize.ule(ObjectSize)) 422 return; 423 424 StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID); 425 // Skim off the details of whichever builtin was called to produce a better 426 // diagnostic, as it's unlikley that the user wrote the __builtin explicitly. 427 if (IsChkVariant) { 428 FunctionName = FunctionName.drop_front(std::strlen("__builtin___")); 429 FunctionName = FunctionName.drop_back(std::strlen("_chk")); 430 } else if (FunctionName.startswith("__builtin_")) { 431 FunctionName = FunctionName.drop_front(std::strlen("__builtin_")); 432 } 433 434 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 435 PDiag(DiagID) 436 << FunctionName << ObjectSize.toString(/*Radix=*/10) 437 << UsedSize.toString(/*Radix=*/10)); 438 } 439 440 static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, 441 Scope::ScopeFlags NeededScopeFlags, 442 unsigned DiagID) { 443 // Scopes aren't available during instantiation. Fortunately, builtin 444 // functions cannot be template args so they cannot be formed through template 445 // instantiation. Therefore checking once during the parse is sufficient. 446 if (SemaRef.inTemplateInstantiation()) 447 return false; 448 449 Scope *S = SemaRef.getCurScope(); 450 while (S && !S->isSEHExceptScope()) 451 S = S->getParent(); 452 if (!S || !(S->getFlags() & NeededScopeFlags)) { 453 auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 454 SemaRef.Diag(TheCall->getExprLoc(), DiagID) 455 << DRE->getDecl()->getIdentifier(); 456 return true; 457 } 458 459 return false; 460 } 461 462 static inline bool isBlockPointer(Expr *Arg) { 463 return Arg->getType()->isBlockPointerType(); 464 } 465 466 /// OpenCL C v2.0, s6.13.17.2 - Checks that the block parameters are all local 467 /// void*, which is a requirement of device side enqueue. 468 static bool checkOpenCLBlockArgs(Sema &S, Expr *BlockArg) { 469 const BlockPointerType *BPT = 470 cast<BlockPointerType>(BlockArg->getType().getCanonicalType()); 471 ArrayRef<QualType> Params = 472 BPT->getPointeeType()->getAs<FunctionProtoType>()->getParamTypes(); 473 unsigned ArgCounter = 0; 474 bool IllegalParams = false; 475 // Iterate through the block parameters until either one is found that is not 476 // a local void*, or the block is valid. 477 for (ArrayRef<QualType>::iterator I = Params.begin(), E = Params.end(); 478 I != E; ++I, ++ArgCounter) { 479 if (!(*I)->isPointerType() || !(*I)->getPointeeType()->isVoidType() || 480 (*I)->getPointeeType().getQualifiers().getAddressSpace() != 481 LangAS::opencl_local) { 482 // Get the location of the error. If a block literal has been passed 483 // (BlockExpr) then we can point straight to the offending argument, 484 // else we just point to the variable reference. 485 SourceLocation ErrorLoc; 486 if (isa<BlockExpr>(BlockArg)) { 487 BlockDecl *BD = cast<BlockExpr>(BlockArg)->getBlockDecl(); 488 ErrorLoc = BD->getParamDecl(ArgCounter)->getBeginLoc(); 489 } else if (isa<DeclRefExpr>(BlockArg)) { 490 ErrorLoc = cast<DeclRefExpr>(BlockArg)->getBeginLoc(); 491 } 492 S.Diag(ErrorLoc, 493 diag::err_opencl_enqueue_kernel_blocks_non_local_void_args); 494 IllegalParams = true; 495 } 496 } 497 498 return IllegalParams; 499 } 500 501 static bool checkOpenCLSubgroupExt(Sema &S, CallExpr *Call) { 502 if (!S.getOpenCLOptions().isEnabled("cl_khr_subgroups")) { 503 S.Diag(Call->getBeginLoc(), diag::err_opencl_requires_extension) 504 << 1 << Call->getDirectCallee() << "cl_khr_subgroups"; 505 return true; 506 } 507 return false; 508 } 509 510 static bool SemaOpenCLBuiltinNDRangeAndBlock(Sema &S, CallExpr *TheCall) { 511 if (checkArgCount(S, TheCall, 2)) 512 return true; 513 514 if (checkOpenCLSubgroupExt(S, TheCall)) 515 return true; 516 517 // First argument is an ndrange_t type. 518 Expr *NDRangeArg = TheCall->getArg(0); 519 if (NDRangeArg->getType().getUnqualifiedType().getAsString() != "ndrange_t") { 520 S.Diag(NDRangeArg->getBeginLoc(), diag::err_opencl_builtin_expected_type) 521 << TheCall->getDirectCallee() << "'ndrange_t'"; 522 return true; 523 } 524 525 Expr *BlockArg = TheCall->getArg(1); 526 if (!isBlockPointer(BlockArg)) { 527 S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type) 528 << TheCall->getDirectCallee() << "block"; 529 return true; 530 } 531 return checkOpenCLBlockArgs(S, BlockArg); 532 } 533 534 /// OpenCL C v2.0, s6.13.17.6 - Check the argument to the 535 /// get_kernel_work_group_size 536 /// and get_kernel_preferred_work_group_size_multiple builtin functions. 537 static bool SemaOpenCLBuiltinKernelWorkGroupSize(Sema &S, CallExpr *TheCall) { 538 if (checkArgCount(S, TheCall, 1)) 539 return true; 540 541 Expr *BlockArg = TheCall->getArg(0); 542 if (!isBlockPointer(BlockArg)) { 543 S.Diag(BlockArg->getBeginLoc(), diag::err_opencl_builtin_expected_type) 544 << TheCall->getDirectCallee() << "block"; 545 return true; 546 } 547 return checkOpenCLBlockArgs(S, BlockArg); 548 } 549 550 /// Diagnose integer type and any valid implicit conversion to it. 551 static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E, 552 const QualType &IntType); 553 554 static bool checkOpenCLEnqueueLocalSizeArgs(Sema &S, CallExpr *TheCall, 555 unsigned Start, unsigned End) { 556 bool IllegalParams = false; 557 for (unsigned I = Start; I <= End; ++I) 558 IllegalParams |= checkOpenCLEnqueueIntType(S, TheCall->getArg(I), 559 S.Context.getSizeType()); 560 return IllegalParams; 561 } 562 563 /// OpenCL v2.0, s6.13.17.1 - Check that sizes are provided for all 564 /// 'local void*' parameter of passed block. 565 static bool checkOpenCLEnqueueVariadicArgs(Sema &S, CallExpr *TheCall, 566 Expr *BlockArg, 567 unsigned NumNonVarArgs) { 568 const BlockPointerType *BPT = 569 cast<BlockPointerType>(BlockArg->getType().getCanonicalType()); 570 unsigned NumBlockParams = 571 BPT->getPointeeType()->getAs<FunctionProtoType>()->getNumParams(); 572 unsigned TotalNumArgs = TheCall->getNumArgs(); 573 574 // For each argument passed to the block, a corresponding uint needs to 575 // be passed to describe the size of the local memory. 576 if (TotalNumArgs != NumBlockParams + NumNonVarArgs) { 577 S.Diag(TheCall->getBeginLoc(), 578 diag::err_opencl_enqueue_kernel_local_size_args); 579 return true; 580 } 581 582 // Check that the sizes of the local memory are specified by integers. 583 return checkOpenCLEnqueueLocalSizeArgs(S, TheCall, NumNonVarArgs, 584 TotalNumArgs - 1); 585 } 586 587 /// OpenCL C v2.0, s6.13.17 - Enqueue kernel function contains four different 588 /// overload formats specified in Table 6.13.17.1. 589 /// int enqueue_kernel(queue_t queue, 590 /// kernel_enqueue_flags_t flags, 591 /// const ndrange_t ndrange, 592 /// void (^block)(void)) 593 /// int enqueue_kernel(queue_t queue, 594 /// kernel_enqueue_flags_t flags, 595 /// const ndrange_t ndrange, 596 /// uint num_events_in_wait_list, 597 /// clk_event_t *event_wait_list, 598 /// clk_event_t *event_ret, 599 /// void (^block)(void)) 600 /// int enqueue_kernel(queue_t queue, 601 /// kernel_enqueue_flags_t flags, 602 /// const ndrange_t ndrange, 603 /// void (^block)(local void*, ...), 604 /// uint size0, ...) 605 /// int enqueue_kernel(queue_t queue, 606 /// kernel_enqueue_flags_t flags, 607 /// const ndrange_t ndrange, 608 /// uint num_events_in_wait_list, 609 /// clk_event_t *event_wait_list, 610 /// clk_event_t *event_ret, 611 /// void (^block)(local void*, ...), 612 /// uint size0, ...) 613 static bool SemaOpenCLBuiltinEnqueueKernel(Sema &S, CallExpr *TheCall) { 614 unsigned NumArgs = TheCall->getNumArgs(); 615 616 if (NumArgs < 4) { 617 S.Diag(TheCall->getBeginLoc(), diag::err_typecheck_call_too_few_args); 618 return true; 619 } 620 621 Expr *Arg0 = TheCall->getArg(0); 622 Expr *Arg1 = TheCall->getArg(1); 623 Expr *Arg2 = TheCall->getArg(2); 624 Expr *Arg3 = TheCall->getArg(3); 625 626 // First argument always needs to be a queue_t type. 627 if (!Arg0->getType()->isQueueT()) { 628 S.Diag(TheCall->getArg(0)->getBeginLoc(), 629 diag::err_opencl_builtin_expected_type) 630 << TheCall->getDirectCallee() << S.Context.OCLQueueTy; 631 return true; 632 } 633 634 // Second argument always needs to be a kernel_enqueue_flags_t enum value. 635 if (!Arg1->getType()->isIntegerType()) { 636 S.Diag(TheCall->getArg(1)->getBeginLoc(), 637 diag::err_opencl_builtin_expected_type) 638 << TheCall->getDirectCallee() << "'kernel_enqueue_flags_t' (i.e. uint)"; 639 return true; 640 } 641 642 // Third argument is always an ndrange_t type. 643 if (Arg2->getType().getUnqualifiedType().getAsString() != "ndrange_t") { 644 S.Diag(TheCall->getArg(2)->getBeginLoc(), 645 diag::err_opencl_builtin_expected_type) 646 << TheCall->getDirectCallee() << "'ndrange_t'"; 647 return true; 648 } 649 650 // With four arguments, there is only one form that the function could be 651 // called in: no events and no variable arguments. 652 if (NumArgs == 4) { 653 // check that the last argument is the right block type. 654 if (!isBlockPointer(Arg3)) { 655 S.Diag(Arg3->getBeginLoc(), diag::err_opencl_builtin_expected_type) 656 << TheCall->getDirectCallee() << "block"; 657 return true; 658 } 659 // we have a block type, check the prototype 660 const BlockPointerType *BPT = 661 cast<BlockPointerType>(Arg3->getType().getCanonicalType()); 662 if (BPT->getPointeeType()->getAs<FunctionProtoType>()->getNumParams() > 0) { 663 S.Diag(Arg3->getBeginLoc(), 664 diag::err_opencl_enqueue_kernel_blocks_no_args); 665 return true; 666 } 667 return false; 668 } 669 // we can have block + varargs. 670 if (isBlockPointer(Arg3)) 671 return (checkOpenCLBlockArgs(S, Arg3) || 672 checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg3, 4)); 673 // last two cases with either exactly 7 args or 7 args and varargs. 674 if (NumArgs >= 7) { 675 // check common block argument. 676 Expr *Arg6 = TheCall->getArg(6); 677 if (!isBlockPointer(Arg6)) { 678 S.Diag(Arg6->getBeginLoc(), diag::err_opencl_builtin_expected_type) 679 << TheCall->getDirectCallee() << "block"; 680 return true; 681 } 682 if (checkOpenCLBlockArgs(S, Arg6)) 683 return true; 684 685 // Forth argument has to be any integer type. 686 if (!Arg3->getType()->isIntegerType()) { 687 S.Diag(TheCall->getArg(3)->getBeginLoc(), 688 diag::err_opencl_builtin_expected_type) 689 << TheCall->getDirectCallee() << "integer"; 690 return true; 691 } 692 // check remaining common arguments. 693 Expr *Arg4 = TheCall->getArg(4); 694 Expr *Arg5 = TheCall->getArg(5); 695 696 // Fifth argument is always passed as a pointer to clk_event_t. 697 if (!Arg4->isNullPointerConstant(S.Context, 698 Expr::NPC_ValueDependentIsNotNull) && 699 !Arg4->getType()->getPointeeOrArrayElementType()->isClkEventT()) { 700 S.Diag(TheCall->getArg(4)->getBeginLoc(), 701 diag::err_opencl_builtin_expected_type) 702 << TheCall->getDirectCallee() 703 << S.Context.getPointerType(S.Context.OCLClkEventTy); 704 return true; 705 } 706 707 // Sixth argument is always passed as a pointer to clk_event_t. 708 if (!Arg5->isNullPointerConstant(S.Context, 709 Expr::NPC_ValueDependentIsNotNull) && 710 !(Arg5->getType()->isPointerType() && 711 Arg5->getType()->getPointeeType()->isClkEventT())) { 712 S.Diag(TheCall->getArg(5)->getBeginLoc(), 713 diag::err_opencl_builtin_expected_type) 714 << TheCall->getDirectCallee() 715 << S.Context.getPointerType(S.Context.OCLClkEventTy); 716 return true; 717 } 718 719 if (NumArgs == 7) 720 return false; 721 722 return checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg6, 7); 723 } 724 725 // None of the specific case has been detected, give generic error 726 S.Diag(TheCall->getBeginLoc(), 727 diag::err_opencl_enqueue_kernel_incorrect_args); 728 return true; 729 } 730 731 /// Returns OpenCL access qual. 732 static OpenCLAccessAttr *getOpenCLArgAccess(const Decl *D) { 733 return D->getAttr<OpenCLAccessAttr>(); 734 } 735 736 /// Returns true if pipe element type is different from the pointer. 737 static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) { 738 const Expr *Arg0 = Call->getArg(0); 739 // First argument type should always be pipe. 740 if (!Arg0->getType()->isPipeType()) { 741 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg) 742 << Call->getDirectCallee() << Arg0->getSourceRange(); 743 return true; 744 } 745 OpenCLAccessAttr *AccessQual = 746 getOpenCLArgAccess(cast<DeclRefExpr>(Arg0)->getDecl()); 747 // Validates the access qualifier is compatible with the call. 748 // OpenCL v2.0 s6.13.16 - The access qualifiers for pipe should only be 749 // read_only and write_only, and assumed to be read_only if no qualifier is 750 // specified. 751 switch (Call->getDirectCallee()->getBuiltinID()) { 752 case Builtin::BIread_pipe: 753 case Builtin::BIreserve_read_pipe: 754 case Builtin::BIcommit_read_pipe: 755 case Builtin::BIwork_group_reserve_read_pipe: 756 case Builtin::BIsub_group_reserve_read_pipe: 757 case Builtin::BIwork_group_commit_read_pipe: 758 case Builtin::BIsub_group_commit_read_pipe: 759 if (!(!AccessQual || AccessQual->isReadOnly())) { 760 S.Diag(Arg0->getBeginLoc(), 761 diag::err_opencl_builtin_pipe_invalid_access_modifier) 762 << "read_only" << Arg0->getSourceRange(); 763 return true; 764 } 765 break; 766 case Builtin::BIwrite_pipe: 767 case Builtin::BIreserve_write_pipe: 768 case Builtin::BIcommit_write_pipe: 769 case Builtin::BIwork_group_reserve_write_pipe: 770 case Builtin::BIsub_group_reserve_write_pipe: 771 case Builtin::BIwork_group_commit_write_pipe: 772 case Builtin::BIsub_group_commit_write_pipe: 773 if (!(AccessQual && AccessQual->isWriteOnly())) { 774 S.Diag(Arg0->getBeginLoc(), 775 diag::err_opencl_builtin_pipe_invalid_access_modifier) 776 << "write_only" << Arg0->getSourceRange(); 777 return true; 778 } 779 break; 780 default: 781 break; 782 } 783 return false; 784 } 785 786 /// Returns true if pipe element type is different from the pointer. 787 static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) { 788 const Expr *Arg0 = Call->getArg(0); 789 const Expr *ArgIdx = Call->getArg(Idx); 790 const PipeType *PipeTy = cast<PipeType>(Arg0->getType()); 791 const QualType EltTy = PipeTy->getElementType(); 792 const PointerType *ArgTy = ArgIdx->getType()->getAs<PointerType>(); 793 // The Idx argument should be a pointer and the type of the pointer and 794 // the type of pipe element should also be the same. 795 if (!ArgTy || 796 !S.Context.hasSameType( 797 EltTy, ArgTy->getPointeeType()->getCanonicalTypeInternal())) { 798 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) 799 << Call->getDirectCallee() << S.Context.getPointerType(EltTy) 800 << ArgIdx->getType() << ArgIdx->getSourceRange(); 801 return true; 802 } 803 return false; 804 } 805 806 // Performs semantic analysis for the read/write_pipe call. 807 // \param S Reference to the semantic analyzer. 808 // \param Call A pointer to the builtin call. 809 // \return True if a semantic error has been found, false otherwise. 810 static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) { 811 // OpenCL v2.0 s6.13.16.2 - The built-in read/write 812 // functions have two forms. 813 switch (Call->getNumArgs()) { 814 case 2: 815 if (checkOpenCLPipeArg(S, Call)) 816 return true; 817 // The call with 2 arguments should be 818 // read/write_pipe(pipe T, T*). 819 // Check packet type T. 820 if (checkOpenCLPipePacketType(S, Call, 1)) 821 return true; 822 break; 823 824 case 4: { 825 if (checkOpenCLPipeArg(S, Call)) 826 return true; 827 // The call with 4 arguments should be 828 // read/write_pipe(pipe T, reserve_id_t, uint, T*). 829 // Check reserve_id_t. 830 if (!Call->getArg(1)->getType()->isReserveIDT()) { 831 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) 832 << Call->getDirectCallee() << S.Context.OCLReserveIDTy 833 << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange(); 834 return true; 835 } 836 837 // Check the index. 838 const Expr *Arg2 = Call->getArg(2); 839 if (!Arg2->getType()->isIntegerType() && 840 !Arg2->getType()->isUnsignedIntegerType()) { 841 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) 842 << Call->getDirectCallee() << S.Context.UnsignedIntTy 843 << Arg2->getType() << Arg2->getSourceRange(); 844 return true; 845 } 846 847 // Check packet type T. 848 if (checkOpenCLPipePacketType(S, Call, 3)) 849 return true; 850 } break; 851 default: 852 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_arg_num) 853 << Call->getDirectCallee() << Call->getSourceRange(); 854 return true; 855 } 856 857 return false; 858 } 859 860 // Performs a semantic analysis on the {work_group_/sub_group_ 861 // /_}reserve_{read/write}_pipe 862 // \param S Reference to the semantic analyzer. 863 // \param Call The call to the builtin function to be analyzed. 864 // \return True if a semantic error was found, false otherwise. 865 static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) { 866 if (checkArgCount(S, Call, 2)) 867 return true; 868 869 if (checkOpenCLPipeArg(S, Call)) 870 return true; 871 872 // Check the reserve size. 873 if (!Call->getArg(1)->getType()->isIntegerType() && 874 !Call->getArg(1)->getType()->isUnsignedIntegerType()) { 875 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) 876 << Call->getDirectCallee() << S.Context.UnsignedIntTy 877 << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange(); 878 return true; 879 } 880 881 // Since return type of reserve_read/write_pipe built-in function is 882 // reserve_id_t, which is not defined in the builtin def file , we used int 883 // as return type and need to override the return type of these functions. 884 Call->setType(S.Context.OCLReserveIDTy); 885 886 return false; 887 } 888 889 // Performs a semantic analysis on {work_group_/sub_group_ 890 // /_}commit_{read/write}_pipe 891 // \param S Reference to the semantic analyzer. 892 // \param Call The call to the builtin function to be analyzed. 893 // \return True if a semantic error was found, false otherwise. 894 static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) { 895 if (checkArgCount(S, Call, 2)) 896 return true; 897 898 if (checkOpenCLPipeArg(S, Call)) 899 return true; 900 901 // Check reserve_id_t. 902 if (!Call->getArg(1)->getType()->isReserveIDT()) { 903 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_invalid_arg) 904 << Call->getDirectCallee() << S.Context.OCLReserveIDTy 905 << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange(); 906 return true; 907 } 908 909 return false; 910 } 911 912 // Performs a semantic analysis on the call to built-in Pipe 913 // Query Functions. 914 // \param S Reference to the semantic analyzer. 915 // \param Call The call to the builtin function to be analyzed. 916 // \return True if a semantic error was found, false otherwise. 917 static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) { 918 if (checkArgCount(S, Call, 1)) 919 return true; 920 921 if (!Call->getArg(0)->getType()->isPipeType()) { 922 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_pipe_first_arg) 923 << Call->getDirectCallee() << Call->getArg(0)->getSourceRange(); 924 return true; 925 } 926 927 return false; 928 } 929 930 // OpenCL v2.0 s6.13.9 - Address space qualifier functions. 931 // Performs semantic analysis for the to_global/local/private call. 932 // \param S Reference to the semantic analyzer. 933 // \param BuiltinID ID of the builtin function. 934 // \param Call A pointer to the builtin call. 935 // \return True if a semantic error has been found, false otherwise. 936 static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID, 937 CallExpr *Call) { 938 if (Call->getNumArgs() != 1) { 939 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_arg_num) 940 << Call->getDirectCallee() << Call->getSourceRange(); 941 return true; 942 } 943 944 auto RT = Call->getArg(0)->getType(); 945 if (!RT->isPointerType() || RT->getPointeeType() 946 .getAddressSpace() == LangAS::opencl_constant) { 947 S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_invalid_arg) 948 << Call->getArg(0) << Call->getDirectCallee() << Call->getSourceRange(); 949 return true; 950 } 951 952 if (RT->getPointeeType().getAddressSpace() != LangAS::opencl_generic) { 953 S.Diag(Call->getArg(0)->getBeginLoc(), 954 diag::warn_opencl_generic_address_space_arg) 955 << Call->getDirectCallee()->getNameInfo().getAsString() 956 << Call->getArg(0)->getSourceRange(); 957 } 958 959 RT = RT->getPointeeType(); 960 auto Qual = RT.getQualifiers(); 961 switch (BuiltinID) { 962 case Builtin::BIto_global: 963 Qual.setAddressSpace(LangAS::opencl_global); 964 break; 965 case Builtin::BIto_local: 966 Qual.setAddressSpace(LangAS::opencl_local); 967 break; 968 case Builtin::BIto_private: 969 Qual.setAddressSpace(LangAS::opencl_private); 970 break; 971 default: 972 llvm_unreachable("Invalid builtin function"); 973 } 974 Call->setType(S.Context.getPointerType(S.Context.getQualifiedType( 975 RT.getUnqualifiedType(), Qual))); 976 977 return false; 978 } 979 980 static ExprResult SemaBuiltinLaunder(Sema &S, CallExpr *TheCall) { 981 if (checkArgCount(S, TheCall, 1)) 982 return ExprError(); 983 984 // Compute __builtin_launder's parameter type from the argument. 985 // The parameter type is: 986 // * The type of the argument if it's not an array or function type, 987 // Otherwise, 988 // * The decayed argument type. 989 QualType ParamTy = [&]() { 990 QualType ArgTy = TheCall->getArg(0)->getType(); 991 if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe()) 992 return S.Context.getPointerType(Ty->getElementType()); 993 if (ArgTy->isFunctionType()) { 994 return S.Context.getPointerType(ArgTy); 995 } 996 return ArgTy; 997 }(); 998 999 TheCall->setType(ParamTy); 1000 1001 auto DiagSelect = [&]() -> llvm::Optional<unsigned> { 1002 if (!ParamTy->isPointerType()) 1003 return 0; 1004 if (ParamTy->isFunctionPointerType()) 1005 return 1; 1006 if (ParamTy->isVoidPointerType()) 1007 return 2; 1008 return llvm::Optional<unsigned>{}; 1009 }(); 1010 if (DiagSelect.hasValue()) { 1011 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg) 1012 << DiagSelect.getValue() << TheCall->getSourceRange(); 1013 return ExprError(); 1014 } 1015 1016 // We either have an incomplete class type, or we have a class template 1017 // whose instantiation has not been forced. Example: 1018 // 1019 // template <class T> struct Foo { T value; }; 1020 // Foo<int> *p = nullptr; 1021 // auto *d = __builtin_launder(p); 1022 if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(), 1023 diag::err_incomplete_type)) 1024 return ExprError(); 1025 1026 assert(ParamTy->getPointeeType()->isObjectType() && 1027 "Unhandled non-object pointer case"); 1028 1029 InitializedEntity Entity = 1030 InitializedEntity::InitializeParameter(S.Context, ParamTy, false); 1031 ExprResult Arg = 1032 S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0)); 1033 if (Arg.isInvalid()) 1034 return ExprError(); 1035 TheCall->setArg(0, Arg.get()); 1036 1037 return TheCall; 1038 } 1039 1040 // Emit an error and return true if the current architecture is not in the list 1041 // of supported architectures. 1042 static bool 1043 CheckBuiltinTargetSupport(Sema &S, unsigned BuiltinID, CallExpr *TheCall, 1044 ArrayRef<llvm::Triple::ArchType> SupportedArchs) { 1045 llvm::Triple::ArchType CurArch = 1046 S.getASTContext().getTargetInfo().getTriple().getArch(); 1047 if (llvm::is_contained(SupportedArchs, CurArch)) 1048 return false; 1049 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1050 << TheCall->getSourceRange(); 1051 return true; 1052 } 1053 1054 ExprResult 1055 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, 1056 CallExpr *TheCall) { 1057 ExprResult TheCallResult(TheCall); 1058 1059 // Find out if any arguments are required to be integer constant expressions. 1060 unsigned ICEArguments = 0; 1061 ASTContext::GetBuiltinTypeError Error; 1062 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); 1063 if (Error != ASTContext::GE_None) 1064 ICEArguments = 0; // Don't diagnose previously diagnosed errors. 1065 1066 // If any arguments are required to be ICE's, check and diagnose. 1067 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { 1068 // Skip arguments not required to be ICE's. 1069 if ((ICEArguments & (1 << ArgNo)) == 0) continue; 1070 1071 llvm::APSInt Result; 1072 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) 1073 return true; 1074 ICEArguments &= ~(1 << ArgNo); 1075 } 1076 1077 switch (BuiltinID) { 1078 case Builtin::BI__builtin___CFStringMakeConstantString: 1079 assert(TheCall->getNumArgs() == 1 && 1080 "Wrong # arguments to builtin CFStringMakeConstantString"); 1081 if (CheckObjCString(TheCall->getArg(0))) 1082 return ExprError(); 1083 break; 1084 case Builtin::BI__builtin_ms_va_start: 1085 case Builtin::BI__builtin_stdarg_start: 1086 case Builtin::BI__builtin_va_start: 1087 if (SemaBuiltinVAStart(BuiltinID, TheCall)) 1088 return ExprError(); 1089 break; 1090 case Builtin::BI__va_start: { 1091 switch (Context.getTargetInfo().getTriple().getArch()) { 1092 case llvm::Triple::aarch64: 1093 case llvm::Triple::arm: 1094 case llvm::Triple::thumb: 1095 if (SemaBuiltinVAStartARMMicrosoft(TheCall)) 1096 return ExprError(); 1097 break; 1098 default: 1099 if (SemaBuiltinVAStart(BuiltinID, TheCall)) 1100 return ExprError(); 1101 break; 1102 } 1103 break; 1104 } 1105 1106 // The acquire, release, and no fence variants are ARM and AArch64 only. 1107 case Builtin::BI_interlockedbittestandset_acq: 1108 case Builtin::BI_interlockedbittestandset_rel: 1109 case Builtin::BI_interlockedbittestandset_nf: 1110 case Builtin::BI_interlockedbittestandreset_acq: 1111 case Builtin::BI_interlockedbittestandreset_rel: 1112 case Builtin::BI_interlockedbittestandreset_nf: 1113 if (CheckBuiltinTargetSupport( 1114 *this, BuiltinID, TheCall, 1115 {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64})) 1116 return ExprError(); 1117 break; 1118 1119 // The 64-bit bittest variants are x64, ARM, and AArch64 only. 1120 case Builtin::BI_bittest64: 1121 case Builtin::BI_bittestandcomplement64: 1122 case Builtin::BI_bittestandreset64: 1123 case Builtin::BI_bittestandset64: 1124 case Builtin::BI_interlockedbittestandreset64: 1125 case Builtin::BI_interlockedbittestandset64: 1126 if (CheckBuiltinTargetSupport(*this, BuiltinID, TheCall, 1127 {llvm::Triple::x86_64, llvm::Triple::arm, 1128 llvm::Triple::thumb, llvm::Triple::aarch64})) 1129 return ExprError(); 1130 break; 1131 1132 case Builtin::BI__builtin_isgreater: 1133 case Builtin::BI__builtin_isgreaterequal: 1134 case Builtin::BI__builtin_isless: 1135 case Builtin::BI__builtin_islessequal: 1136 case Builtin::BI__builtin_islessgreater: 1137 case Builtin::BI__builtin_isunordered: 1138 if (SemaBuiltinUnorderedCompare(TheCall)) 1139 return ExprError(); 1140 break; 1141 case Builtin::BI__builtin_fpclassify: 1142 if (SemaBuiltinFPClassification(TheCall, 6)) 1143 return ExprError(); 1144 break; 1145 case Builtin::BI__builtin_isfinite: 1146 case Builtin::BI__builtin_isinf: 1147 case Builtin::BI__builtin_isinf_sign: 1148 case Builtin::BI__builtin_isnan: 1149 case Builtin::BI__builtin_isnormal: 1150 case Builtin::BI__builtin_signbit: 1151 case Builtin::BI__builtin_signbitf: 1152 case Builtin::BI__builtin_signbitl: 1153 if (SemaBuiltinFPClassification(TheCall, 1)) 1154 return ExprError(); 1155 break; 1156 case Builtin::BI__builtin_shufflevector: 1157 return SemaBuiltinShuffleVector(TheCall); 1158 // TheCall will be freed by the smart pointer here, but that's fine, since 1159 // SemaBuiltinShuffleVector guts it, but then doesn't release it. 1160 case Builtin::BI__builtin_prefetch: 1161 if (SemaBuiltinPrefetch(TheCall)) 1162 return ExprError(); 1163 break; 1164 case Builtin::BI__builtin_alloca_with_align: 1165 if (SemaBuiltinAllocaWithAlign(TheCall)) 1166 return ExprError(); 1167 break; 1168 case Builtin::BI__assume: 1169 case Builtin::BI__builtin_assume: 1170 if (SemaBuiltinAssume(TheCall)) 1171 return ExprError(); 1172 break; 1173 case Builtin::BI__builtin_assume_aligned: 1174 if (SemaBuiltinAssumeAligned(TheCall)) 1175 return ExprError(); 1176 break; 1177 case Builtin::BI__builtin_dynamic_object_size: 1178 case Builtin::BI__builtin_object_size: 1179 if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3)) 1180 return ExprError(); 1181 break; 1182 case Builtin::BI__builtin_longjmp: 1183 if (SemaBuiltinLongjmp(TheCall)) 1184 return ExprError(); 1185 break; 1186 case Builtin::BI__builtin_setjmp: 1187 if (SemaBuiltinSetjmp(TheCall)) 1188 return ExprError(); 1189 break; 1190 case Builtin::BI_setjmp: 1191 case Builtin::BI_setjmpex: 1192 if (checkArgCount(*this, TheCall, 1)) 1193 return true; 1194 break; 1195 case Builtin::BI__builtin_classify_type: 1196 if (checkArgCount(*this, TheCall, 1)) return true; 1197 TheCall->setType(Context.IntTy); 1198 break; 1199 case Builtin::BI__builtin_constant_p: 1200 if (checkArgCount(*this, TheCall, 1)) return true; 1201 TheCall->setType(Context.IntTy); 1202 break; 1203 case Builtin::BI__builtin_launder: 1204 return SemaBuiltinLaunder(*this, TheCall); 1205 case Builtin::BI__sync_fetch_and_add: 1206 case Builtin::BI__sync_fetch_and_add_1: 1207 case Builtin::BI__sync_fetch_and_add_2: 1208 case Builtin::BI__sync_fetch_and_add_4: 1209 case Builtin::BI__sync_fetch_and_add_8: 1210 case Builtin::BI__sync_fetch_and_add_16: 1211 case Builtin::BI__sync_fetch_and_sub: 1212 case Builtin::BI__sync_fetch_and_sub_1: 1213 case Builtin::BI__sync_fetch_and_sub_2: 1214 case Builtin::BI__sync_fetch_and_sub_4: 1215 case Builtin::BI__sync_fetch_and_sub_8: 1216 case Builtin::BI__sync_fetch_and_sub_16: 1217 case Builtin::BI__sync_fetch_and_or: 1218 case Builtin::BI__sync_fetch_and_or_1: 1219 case Builtin::BI__sync_fetch_and_or_2: 1220 case Builtin::BI__sync_fetch_and_or_4: 1221 case Builtin::BI__sync_fetch_and_or_8: 1222 case Builtin::BI__sync_fetch_and_or_16: 1223 case Builtin::BI__sync_fetch_and_and: 1224 case Builtin::BI__sync_fetch_and_and_1: 1225 case Builtin::BI__sync_fetch_and_and_2: 1226 case Builtin::BI__sync_fetch_and_and_4: 1227 case Builtin::BI__sync_fetch_and_and_8: 1228 case Builtin::BI__sync_fetch_and_and_16: 1229 case Builtin::BI__sync_fetch_and_xor: 1230 case Builtin::BI__sync_fetch_and_xor_1: 1231 case Builtin::BI__sync_fetch_and_xor_2: 1232 case Builtin::BI__sync_fetch_and_xor_4: 1233 case Builtin::BI__sync_fetch_and_xor_8: 1234 case Builtin::BI__sync_fetch_and_xor_16: 1235 case Builtin::BI__sync_fetch_and_nand: 1236 case Builtin::BI__sync_fetch_and_nand_1: 1237 case Builtin::BI__sync_fetch_and_nand_2: 1238 case Builtin::BI__sync_fetch_and_nand_4: 1239 case Builtin::BI__sync_fetch_and_nand_8: 1240 case Builtin::BI__sync_fetch_and_nand_16: 1241 case Builtin::BI__sync_add_and_fetch: 1242 case Builtin::BI__sync_add_and_fetch_1: 1243 case Builtin::BI__sync_add_and_fetch_2: 1244 case Builtin::BI__sync_add_and_fetch_4: 1245 case Builtin::BI__sync_add_and_fetch_8: 1246 case Builtin::BI__sync_add_and_fetch_16: 1247 case Builtin::BI__sync_sub_and_fetch: 1248 case Builtin::BI__sync_sub_and_fetch_1: 1249 case Builtin::BI__sync_sub_and_fetch_2: 1250 case Builtin::BI__sync_sub_and_fetch_4: 1251 case Builtin::BI__sync_sub_and_fetch_8: 1252 case Builtin::BI__sync_sub_and_fetch_16: 1253 case Builtin::BI__sync_and_and_fetch: 1254 case Builtin::BI__sync_and_and_fetch_1: 1255 case Builtin::BI__sync_and_and_fetch_2: 1256 case Builtin::BI__sync_and_and_fetch_4: 1257 case Builtin::BI__sync_and_and_fetch_8: 1258 case Builtin::BI__sync_and_and_fetch_16: 1259 case Builtin::BI__sync_or_and_fetch: 1260 case Builtin::BI__sync_or_and_fetch_1: 1261 case Builtin::BI__sync_or_and_fetch_2: 1262 case Builtin::BI__sync_or_and_fetch_4: 1263 case Builtin::BI__sync_or_and_fetch_8: 1264 case Builtin::BI__sync_or_and_fetch_16: 1265 case Builtin::BI__sync_xor_and_fetch: 1266 case Builtin::BI__sync_xor_and_fetch_1: 1267 case Builtin::BI__sync_xor_and_fetch_2: 1268 case Builtin::BI__sync_xor_and_fetch_4: 1269 case Builtin::BI__sync_xor_and_fetch_8: 1270 case Builtin::BI__sync_xor_and_fetch_16: 1271 case Builtin::BI__sync_nand_and_fetch: 1272 case Builtin::BI__sync_nand_and_fetch_1: 1273 case Builtin::BI__sync_nand_and_fetch_2: 1274 case Builtin::BI__sync_nand_and_fetch_4: 1275 case Builtin::BI__sync_nand_and_fetch_8: 1276 case Builtin::BI__sync_nand_and_fetch_16: 1277 case Builtin::BI__sync_val_compare_and_swap: 1278 case Builtin::BI__sync_val_compare_and_swap_1: 1279 case Builtin::BI__sync_val_compare_and_swap_2: 1280 case Builtin::BI__sync_val_compare_and_swap_4: 1281 case Builtin::BI__sync_val_compare_and_swap_8: 1282 case Builtin::BI__sync_val_compare_and_swap_16: 1283 case Builtin::BI__sync_bool_compare_and_swap: 1284 case Builtin::BI__sync_bool_compare_and_swap_1: 1285 case Builtin::BI__sync_bool_compare_and_swap_2: 1286 case Builtin::BI__sync_bool_compare_and_swap_4: 1287 case Builtin::BI__sync_bool_compare_and_swap_8: 1288 case Builtin::BI__sync_bool_compare_and_swap_16: 1289 case Builtin::BI__sync_lock_test_and_set: 1290 case Builtin::BI__sync_lock_test_and_set_1: 1291 case Builtin::BI__sync_lock_test_and_set_2: 1292 case Builtin::BI__sync_lock_test_and_set_4: 1293 case Builtin::BI__sync_lock_test_and_set_8: 1294 case Builtin::BI__sync_lock_test_and_set_16: 1295 case Builtin::BI__sync_lock_release: 1296 case Builtin::BI__sync_lock_release_1: 1297 case Builtin::BI__sync_lock_release_2: 1298 case Builtin::BI__sync_lock_release_4: 1299 case Builtin::BI__sync_lock_release_8: 1300 case Builtin::BI__sync_lock_release_16: 1301 case Builtin::BI__sync_swap: 1302 case Builtin::BI__sync_swap_1: 1303 case Builtin::BI__sync_swap_2: 1304 case Builtin::BI__sync_swap_4: 1305 case Builtin::BI__sync_swap_8: 1306 case Builtin::BI__sync_swap_16: 1307 return SemaBuiltinAtomicOverloaded(TheCallResult); 1308 case Builtin::BI__sync_synchronize: 1309 Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst) 1310 << TheCall->getCallee()->getSourceRange(); 1311 break; 1312 case Builtin::BI__builtin_nontemporal_load: 1313 case Builtin::BI__builtin_nontemporal_store: 1314 return SemaBuiltinNontemporalOverloaded(TheCallResult); 1315 #define BUILTIN(ID, TYPE, ATTRS) 1316 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1317 case Builtin::BI##ID: \ 1318 return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); 1319 #include "clang/Basic/Builtins.def" 1320 case Builtin::BI__annotation: 1321 if (SemaBuiltinMSVCAnnotation(*this, TheCall)) 1322 return ExprError(); 1323 break; 1324 case Builtin::BI__builtin_annotation: 1325 if (SemaBuiltinAnnotation(*this, TheCall)) 1326 return ExprError(); 1327 break; 1328 case Builtin::BI__builtin_addressof: 1329 if (SemaBuiltinAddressof(*this, TheCall)) 1330 return ExprError(); 1331 break; 1332 case Builtin::BI__builtin_add_overflow: 1333 case Builtin::BI__builtin_sub_overflow: 1334 case Builtin::BI__builtin_mul_overflow: 1335 if (SemaBuiltinOverflow(*this, TheCall)) 1336 return ExprError(); 1337 break; 1338 case Builtin::BI__builtin_operator_new: 1339 case Builtin::BI__builtin_operator_delete: { 1340 bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete; 1341 ExprResult Res = 1342 SemaBuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete); 1343 if (Res.isInvalid()) 1344 CorrectDelayedTyposInExpr(TheCallResult.get()); 1345 return Res; 1346 } 1347 case Builtin::BI__builtin_dump_struct: { 1348 // We first want to ensure we are called with 2 arguments 1349 if (checkArgCount(*this, TheCall, 2)) 1350 return ExprError(); 1351 // Ensure that the first argument is of type 'struct XX *' 1352 const Expr *PtrArg = TheCall->getArg(0)->IgnoreParenImpCasts(); 1353 const QualType PtrArgType = PtrArg->getType(); 1354 if (!PtrArgType->isPointerType() || 1355 !PtrArgType->getPointeeType()->isRecordType()) { 1356 Diag(PtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible) 1357 << PtrArgType << "structure pointer" << 1 << 0 << 3 << 1 << PtrArgType 1358 << "structure pointer"; 1359 return ExprError(); 1360 } 1361 1362 // Ensure that the second argument is of type 'FunctionType' 1363 const Expr *FnPtrArg = TheCall->getArg(1)->IgnoreImpCasts(); 1364 const QualType FnPtrArgType = FnPtrArg->getType(); 1365 if (!FnPtrArgType->isPointerType()) { 1366 Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible) 1367 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 << 2 1368 << FnPtrArgType << "'int (*)(const char *, ...)'"; 1369 return ExprError(); 1370 } 1371 1372 const auto *FuncType = 1373 FnPtrArgType->getPointeeType()->getAs<FunctionType>(); 1374 1375 if (!FuncType) { 1376 Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible) 1377 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 << 2 1378 << FnPtrArgType << "'int (*)(const char *, ...)'"; 1379 return ExprError(); 1380 } 1381 1382 if (const auto *FT = dyn_cast<FunctionProtoType>(FuncType)) { 1383 if (!FT->getNumParams()) { 1384 Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible) 1385 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 1386 << 2 << FnPtrArgType << "'int (*)(const char *, ...)'"; 1387 return ExprError(); 1388 } 1389 QualType PT = FT->getParamType(0); 1390 if (!FT->isVariadic() || FT->getReturnType() != Context.IntTy || 1391 !PT->isPointerType() || !PT->getPointeeType()->isCharType() || 1392 !PT->getPointeeType().isConstQualified()) { 1393 Diag(FnPtrArg->getBeginLoc(), diag::err_typecheck_convert_incompatible) 1394 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3 1395 << 2 << FnPtrArgType << "'int (*)(const char *, ...)'"; 1396 return ExprError(); 1397 } 1398 } 1399 1400 TheCall->setType(Context.IntTy); 1401 break; 1402 } 1403 case Builtin::BI__builtin_call_with_static_chain: 1404 if (SemaBuiltinCallWithStaticChain(*this, TheCall)) 1405 return ExprError(); 1406 break; 1407 case Builtin::BI__exception_code: 1408 case Builtin::BI_exception_code: 1409 if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, 1410 diag::err_seh___except_block)) 1411 return ExprError(); 1412 break; 1413 case Builtin::BI__exception_info: 1414 case Builtin::BI_exception_info: 1415 if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, 1416 diag::err_seh___except_filter)) 1417 return ExprError(); 1418 break; 1419 case Builtin::BI__GetExceptionInfo: 1420 if (checkArgCount(*this, TheCall, 1)) 1421 return ExprError(); 1422 1423 if (CheckCXXThrowOperand( 1424 TheCall->getBeginLoc(), 1425 Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), 1426 TheCall)) 1427 return ExprError(); 1428 1429 TheCall->setType(Context.VoidPtrTy); 1430 break; 1431 // OpenCL v2.0, s6.13.16 - Pipe functions 1432 case Builtin::BIread_pipe: 1433 case Builtin::BIwrite_pipe: 1434 // Since those two functions are declared with var args, we need a semantic 1435 // check for the argument. 1436 if (SemaBuiltinRWPipe(*this, TheCall)) 1437 return ExprError(); 1438 break; 1439 case Builtin::BIreserve_read_pipe: 1440 case Builtin::BIreserve_write_pipe: 1441 case Builtin::BIwork_group_reserve_read_pipe: 1442 case Builtin::BIwork_group_reserve_write_pipe: 1443 if (SemaBuiltinReserveRWPipe(*this, TheCall)) 1444 return ExprError(); 1445 break; 1446 case Builtin::BIsub_group_reserve_read_pipe: 1447 case Builtin::BIsub_group_reserve_write_pipe: 1448 if (checkOpenCLSubgroupExt(*this, TheCall) || 1449 SemaBuiltinReserveRWPipe(*this, TheCall)) 1450 return ExprError(); 1451 break; 1452 case Builtin::BIcommit_read_pipe: 1453 case Builtin::BIcommit_write_pipe: 1454 case Builtin::BIwork_group_commit_read_pipe: 1455 case Builtin::BIwork_group_commit_write_pipe: 1456 if (SemaBuiltinCommitRWPipe(*this, TheCall)) 1457 return ExprError(); 1458 break; 1459 case Builtin::BIsub_group_commit_read_pipe: 1460 case Builtin::BIsub_group_commit_write_pipe: 1461 if (checkOpenCLSubgroupExt(*this, TheCall) || 1462 SemaBuiltinCommitRWPipe(*this, TheCall)) 1463 return ExprError(); 1464 break; 1465 case Builtin::BIget_pipe_num_packets: 1466 case Builtin::BIget_pipe_max_packets: 1467 if (SemaBuiltinPipePackets(*this, TheCall)) 1468 return ExprError(); 1469 break; 1470 case Builtin::BIto_global: 1471 case Builtin::BIto_local: 1472 case Builtin::BIto_private: 1473 if (SemaOpenCLBuiltinToAddr(*this, BuiltinID, TheCall)) 1474 return ExprError(); 1475 break; 1476 // OpenCL v2.0, s6.13.17 - Enqueue kernel functions. 1477 case Builtin::BIenqueue_kernel: 1478 if (SemaOpenCLBuiltinEnqueueKernel(*this, TheCall)) 1479 return ExprError(); 1480 break; 1481 case Builtin::BIget_kernel_work_group_size: 1482 case Builtin::BIget_kernel_preferred_work_group_size_multiple: 1483 if (SemaOpenCLBuiltinKernelWorkGroupSize(*this, TheCall)) 1484 return ExprError(); 1485 break; 1486 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange: 1487 case Builtin::BIget_kernel_sub_group_count_for_ndrange: 1488 if (SemaOpenCLBuiltinNDRangeAndBlock(*this, TheCall)) 1489 return ExprError(); 1490 break; 1491 case Builtin::BI__builtin_os_log_format: 1492 case Builtin::BI__builtin_os_log_format_buffer_size: 1493 if (SemaBuiltinOSLogFormat(TheCall)) 1494 return ExprError(); 1495 break; 1496 } 1497 1498 // Since the target specific builtins for each arch overlap, only check those 1499 // of the arch we are compiling for. 1500 if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) { 1501 switch (Context.getTargetInfo().getTriple().getArch()) { 1502 case llvm::Triple::arm: 1503 case llvm::Triple::armeb: 1504 case llvm::Triple::thumb: 1505 case llvm::Triple::thumbeb: 1506 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) 1507 return ExprError(); 1508 break; 1509 case llvm::Triple::aarch64: 1510 case llvm::Triple::aarch64_be: 1511 if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall)) 1512 return ExprError(); 1513 break; 1514 case llvm::Triple::hexagon: 1515 if (CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall)) 1516 return ExprError(); 1517 break; 1518 case llvm::Triple::mips: 1519 case llvm::Triple::mipsel: 1520 case llvm::Triple::mips64: 1521 case llvm::Triple::mips64el: 1522 if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) 1523 return ExprError(); 1524 break; 1525 case llvm::Triple::systemz: 1526 if (CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall)) 1527 return ExprError(); 1528 break; 1529 case llvm::Triple::x86: 1530 case llvm::Triple::x86_64: 1531 if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall)) 1532 return ExprError(); 1533 break; 1534 case llvm::Triple::ppc: 1535 case llvm::Triple::ppc64: 1536 case llvm::Triple::ppc64le: 1537 if (CheckPPCBuiltinFunctionCall(BuiltinID, TheCall)) 1538 return ExprError(); 1539 break; 1540 default: 1541 break; 1542 } 1543 } 1544 1545 return TheCallResult; 1546 } 1547 1548 // Get the valid immediate range for the specified NEON type code. 1549 static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) { 1550 NeonTypeFlags Type(t); 1551 int IsQuad = ForceQuad ? true : Type.isQuad(); 1552 switch (Type.getEltType()) { 1553 case NeonTypeFlags::Int8: 1554 case NeonTypeFlags::Poly8: 1555 return shift ? 7 : (8 << IsQuad) - 1; 1556 case NeonTypeFlags::Int16: 1557 case NeonTypeFlags::Poly16: 1558 return shift ? 15 : (4 << IsQuad) - 1; 1559 case NeonTypeFlags::Int32: 1560 return shift ? 31 : (2 << IsQuad) - 1; 1561 case NeonTypeFlags::Int64: 1562 case NeonTypeFlags::Poly64: 1563 return shift ? 63 : (1 << IsQuad) - 1; 1564 case NeonTypeFlags::Poly128: 1565 return shift ? 127 : (1 << IsQuad) - 1; 1566 case NeonTypeFlags::Float16: 1567 assert(!shift && "cannot shift float types!"); 1568 return (4 << IsQuad) - 1; 1569 case NeonTypeFlags::Float32: 1570 assert(!shift && "cannot shift float types!"); 1571 return (2 << IsQuad) - 1; 1572 case NeonTypeFlags::Float64: 1573 assert(!shift && "cannot shift float types!"); 1574 return (1 << IsQuad) - 1; 1575 } 1576 llvm_unreachable("Invalid NeonTypeFlag!"); 1577 } 1578 1579 /// getNeonEltType - Return the QualType corresponding to the elements of 1580 /// the vector type specified by the NeonTypeFlags. This is used to check 1581 /// the pointer arguments for Neon load/store intrinsics. 1582 static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context, 1583 bool IsPolyUnsigned, bool IsInt64Long) { 1584 switch (Flags.getEltType()) { 1585 case NeonTypeFlags::Int8: 1586 return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; 1587 case NeonTypeFlags::Int16: 1588 return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; 1589 case NeonTypeFlags::Int32: 1590 return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; 1591 case NeonTypeFlags::Int64: 1592 if (IsInt64Long) 1593 return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy; 1594 else 1595 return Flags.isUnsigned() ? Context.UnsignedLongLongTy 1596 : Context.LongLongTy; 1597 case NeonTypeFlags::Poly8: 1598 return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy; 1599 case NeonTypeFlags::Poly16: 1600 return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy; 1601 case NeonTypeFlags::Poly64: 1602 if (IsInt64Long) 1603 return Context.UnsignedLongTy; 1604 else 1605 return Context.UnsignedLongLongTy; 1606 case NeonTypeFlags::Poly128: 1607 break; 1608 case NeonTypeFlags::Float16: 1609 return Context.HalfTy; 1610 case NeonTypeFlags::Float32: 1611 return Context.FloatTy; 1612 case NeonTypeFlags::Float64: 1613 return Context.DoubleTy; 1614 } 1615 llvm_unreachable("Invalid NeonTypeFlag!"); 1616 } 1617 1618 bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 1619 llvm::APSInt Result; 1620 uint64_t mask = 0; 1621 unsigned TV = 0; 1622 int PtrArgNum = -1; 1623 bool HasConstPtr = false; 1624 switch (BuiltinID) { 1625 #define GET_NEON_OVERLOAD_CHECK 1626 #include "clang/Basic/arm_neon.inc" 1627 #include "clang/Basic/arm_fp16.inc" 1628 #undef GET_NEON_OVERLOAD_CHECK 1629 } 1630 1631 // For NEON intrinsics which are overloaded on vector element type, validate 1632 // the immediate which specifies which variant to emit. 1633 unsigned ImmArg = TheCall->getNumArgs()-1; 1634 if (mask) { 1635 if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) 1636 return true; 1637 1638 TV = Result.getLimitedValue(64); 1639 if ((TV > 63) || (mask & (1ULL << TV)) == 0) 1640 return Diag(TheCall->getBeginLoc(), diag::err_invalid_neon_type_code) 1641 << TheCall->getArg(ImmArg)->getSourceRange(); 1642 } 1643 1644 if (PtrArgNum >= 0) { 1645 // Check that pointer arguments have the specified type. 1646 Expr *Arg = TheCall->getArg(PtrArgNum); 1647 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) 1648 Arg = ICE->getSubExpr(); 1649 ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); 1650 QualType RHSTy = RHS.get()->getType(); 1651 1652 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); 1653 bool IsPolyUnsigned = Arch == llvm::Triple::aarch64 || 1654 Arch == llvm::Triple::aarch64_be; 1655 bool IsInt64Long = 1656 Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong; 1657 QualType EltTy = 1658 getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long); 1659 if (HasConstPtr) 1660 EltTy = EltTy.withConst(); 1661 QualType LHSTy = Context.getPointerType(EltTy); 1662 AssignConvertType ConvTy; 1663 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); 1664 if (RHS.isInvalid()) 1665 return true; 1666 if (DiagnoseAssignmentResult(ConvTy, Arg->getBeginLoc(), LHSTy, RHSTy, 1667 RHS.get(), AA_Assigning)) 1668 return true; 1669 } 1670 1671 // For NEON intrinsics which take an immediate value as part of the 1672 // instruction, range check them here. 1673 unsigned i = 0, l = 0, u = 0; 1674 switch (BuiltinID) { 1675 default: 1676 return false; 1677 #define GET_NEON_IMMEDIATE_CHECK 1678 #include "clang/Basic/arm_neon.inc" 1679 #include "clang/Basic/arm_fp16.inc" 1680 #undef GET_NEON_IMMEDIATE_CHECK 1681 } 1682 1683 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); 1684 } 1685 1686 bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall, 1687 unsigned MaxWidth) { 1688 assert((BuiltinID == ARM::BI__builtin_arm_ldrex || 1689 BuiltinID == ARM::BI__builtin_arm_ldaex || 1690 BuiltinID == ARM::BI__builtin_arm_strex || 1691 BuiltinID == ARM::BI__builtin_arm_stlex || 1692 BuiltinID == AArch64::BI__builtin_arm_ldrex || 1693 BuiltinID == AArch64::BI__builtin_arm_ldaex || 1694 BuiltinID == AArch64::BI__builtin_arm_strex || 1695 BuiltinID == AArch64::BI__builtin_arm_stlex) && 1696 "unexpected ARM builtin"); 1697 bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex || 1698 BuiltinID == ARM::BI__builtin_arm_ldaex || 1699 BuiltinID == AArch64::BI__builtin_arm_ldrex || 1700 BuiltinID == AArch64::BI__builtin_arm_ldaex; 1701 1702 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1703 1704 // Ensure that we have the proper number of arguments. 1705 if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2)) 1706 return true; 1707 1708 // Inspect the pointer argument of the atomic builtin. This should always be 1709 // a pointer type, whose element is an integral scalar or pointer type. 1710 // Because it is a pointer type, we don't have to worry about any implicit 1711 // casts here. 1712 Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1); 1713 ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg); 1714 if (PointerArgRes.isInvalid()) 1715 return true; 1716 PointerArg = PointerArgRes.get(); 1717 1718 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); 1719 if (!pointerType) { 1720 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) 1721 << PointerArg->getType() << PointerArg->getSourceRange(); 1722 return true; 1723 } 1724 1725 // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next 1726 // task is to insert the appropriate casts into the AST. First work out just 1727 // what the appropriate type is. 1728 QualType ValType = pointerType->getPointeeType(); 1729 QualType AddrType = ValType.getUnqualifiedType().withVolatile(); 1730 if (IsLdrex) 1731 AddrType.addConst(); 1732 1733 // Issue a warning if the cast is dodgy. 1734 CastKind CastNeeded = CK_NoOp; 1735 if (!AddrType.isAtLeastAsQualifiedAs(ValType)) { 1736 CastNeeded = CK_BitCast; 1737 Diag(DRE->getBeginLoc(), diag::ext_typecheck_convert_discards_qualifiers) 1738 << PointerArg->getType() << Context.getPointerType(AddrType) 1739 << AA_Passing << PointerArg->getSourceRange(); 1740 } 1741 1742 // Finally, do the cast and replace the argument with the corrected version. 1743 AddrType = Context.getPointerType(AddrType); 1744 PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded); 1745 if (PointerArgRes.isInvalid()) 1746 return true; 1747 PointerArg = PointerArgRes.get(); 1748 1749 TheCall->setArg(IsLdrex ? 0 : 1, PointerArg); 1750 1751 // In general, we allow ints, floats and pointers to be loaded and stored. 1752 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 1753 !ValType->isBlockPointerType() && !ValType->isFloatingType()) { 1754 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intfltptr) 1755 << PointerArg->getType() << PointerArg->getSourceRange(); 1756 return true; 1757 } 1758 1759 // But ARM doesn't have instructions to deal with 128-bit versions. 1760 if (Context.getTypeSize(ValType) > MaxWidth) { 1761 assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate"); 1762 Diag(DRE->getBeginLoc(), diag::err_atomic_exclusive_builtin_pointer_size) 1763 << PointerArg->getType() << PointerArg->getSourceRange(); 1764 return true; 1765 } 1766 1767 switch (ValType.getObjCLifetime()) { 1768 case Qualifiers::OCL_None: 1769 case Qualifiers::OCL_ExplicitNone: 1770 // okay 1771 break; 1772 1773 case Qualifiers::OCL_Weak: 1774 case Qualifiers::OCL_Strong: 1775 case Qualifiers::OCL_Autoreleasing: 1776 Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) 1777 << ValType << PointerArg->getSourceRange(); 1778 return true; 1779 } 1780 1781 if (IsLdrex) { 1782 TheCall->setType(ValType); 1783 return false; 1784 } 1785 1786 // Initialize the argument to be stored. 1787 ExprResult ValArg = TheCall->getArg(0); 1788 InitializedEntity Entity = InitializedEntity::InitializeParameter( 1789 Context, ValType, /*consume*/ false); 1790 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); 1791 if (ValArg.isInvalid()) 1792 return true; 1793 TheCall->setArg(0, ValArg.get()); 1794 1795 // __builtin_arm_strex always returns an int. It's marked as such in the .def, 1796 // but the custom checker bypasses all default analysis. 1797 TheCall->setType(Context.IntTy); 1798 return false; 1799 } 1800 1801 bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 1802 if (BuiltinID == ARM::BI__builtin_arm_ldrex || 1803 BuiltinID == ARM::BI__builtin_arm_ldaex || 1804 BuiltinID == ARM::BI__builtin_arm_strex || 1805 BuiltinID == ARM::BI__builtin_arm_stlex) { 1806 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64); 1807 } 1808 1809 if (BuiltinID == ARM::BI__builtin_arm_prefetch) { 1810 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || 1811 SemaBuiltinConstantArgRange(TheCall, 2, 0, 1); 1812 } 1813 1814 if (BuiltinID == ARM::BI__builtin_arm_rsr64 || 1815 BuiltinID == ARM::BI__builtin_arm_wsr64) 1816 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false); 1817 1818 if (BuiltinID == ARM::BI__builtin_arm_rsr || 1819 BuiltinID == ARM::BI__builtin_arm_rsrp || 1820 BuiltinID == ARM::BI__builtin_arm_wsr || 1821 BuiltinID == ARM::BI__builtin_arm_wsrp) 1822 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); 1823 1824 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) 1825 return true; 1826 1827 // For intrinsics which take an immediate value as part of the instruction, 1828 // range check them here. 1829 // FIXME: VFP Intrinsics should error if VFP not present. 1830 switch (BuiltinID) { 1831 default: return false; 1832 case ARM::BI__builtin_arm_ssat: 1833 return SemaBuiltinConstantArgRange(TheCall, 1, 1, 32); 1834 case ARM::BI__builtin_arm_usat: 1835 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31); 1836 case ARM::BI__builtin_arm_ssat16: 1837 return SemaBuiltinConstantArgRange(TheCall, 1, 1, 16); 1838 case ARM::BI__builtin_arm_usat16: 1839 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); 1840 case ARM::BI__builtin_arm_vcvtr_f: 1841 case ARM::BI__builtin_arm_vcvtr_d: 1842 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); 1843 case ARM::BI__builtin_arm_dmb: 1844 case ARM::BI__builtin_arm_dsb: 1845 case ARM::BI__builtin_arm_isb: 1846 case ARM::BI__builtin_arm_dbg: 1847 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15); 1848 } 1849 } 1850 1851 bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID, 1852 CallExpr *TheCall) { 1853 if (BuiltinID == AArch64::BI__builtin_arm_ldrex || 1854 BuiltinID == AArch64::BI__builtin_arm_ldaex || 1855 BuiltinID == AArch64::BI__builtin_arm_strex || 1856 BuiltinID == AArch64::BI__builtin_arm_stlex) { 1857 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128); 1858 } 1859 1860 if (BuiltinID == AArch64::BI__builtin_arm_prefetch) { 1861 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || 1862 SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) || 1863 SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) || 1864 SemaBuiltinConstantArgRange(TheCall, 4, 0, 1); 1865 } 1866 1867 if (BuiltinID == AArch64::BI__builtin_arm_rsr64 || 1868 BuiltinID == AArch64::BI__builtin_arm_wsr64) 1869 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); 1870 1871 if (BuiltinID == AArch64::BI__builtin_arm_rsr || 1872 BuiltinID == AArch64::BI__builtin_arm_rsrp || 1873 BuiltinID == AArch64::BI__builtin_arm_wsr || 1874 BuiltinID == AArch64::BI__builtin_arm_wsrp) 1875 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); 1876 1877 // Only check the valid encoding range. Any constant in this range would be 1878 // converted to a register of the form S1_2_C3_C4_5. Let the hardware throw 1879 // an exception for incorrect registers. This matches MSVC behavior. 1880 if (BuiltinID == AArch64::BI_ReadStatusReg || 1881 BuiltinID == AArch64::BI_WriteStatusReg) 1882 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 0x7fff); 1883 1884 if (BuiltinID == AArch64::BI__getReg) 1885 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31); 1886 1887 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) 1888 return true; 1889 1890 // For intrinsics which take an immediate value as part of the instruction, 1891 // range check them here. 1892 unsigned i = 0, l = 0, u = 0; 1893 switch (BuiltinID) { 1894 default: return false; 1895 case AArch64::BI__builtin_arm_dmb: 1896 case AArch64::BI__builtin_arm_dsb: 1897 case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break; 1898 } 1899 1900 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); 1901 } 1902 1903 bool Sema::CheckHexagonBuiltinCpu(unsigned BuiltinID, CallExpr *TheCall) { 1904 struct BuiltinAndString { 1905 unsigned BuiltinID; 1906 const char *Str; 1907 }; 1908 1909 static BuiltinAndString ValidCPU[] = { 1910 { Hexagon::BI__builtin_HEXAGON_A6_vcmpbeq_notany, "v65,v66" }, 1911 { Hexagon::BI__builtin_HEXAGON_A6_vminub_RdP, "v62,v65,v66" }, 1912 { Hexagon::BI__builtin_HEXAGON_F2_dfadd, "v66" }, 1913 { Hexagon::BI__builtin_HEXAGON_F2_dfsub, "v66" }, 1914 { Hexagon::BI__builtin_HEXAGON_M2_mnaci, "v66" }, 1915 { Hexagon::BI__builtin_HEXAGON_M6_vabsdiffb, "v62,v65,v66" }, 1916 { Hexagon::BI__builtin_HEXAGON_M6_vabsdiffub, "v62,v65,v66" }, 1917 { Hexagon::BI__builtin_HEXAGON_S2_mask, "v66" }, 1918 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_acc, "v60,v62,v65,v66" }, 1919 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_and, "v60,v62,v65,v66" }, 1920 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_nac, "v60,v62,v65,v66" }, 1921 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_or, "v60,v62,v65,v66" }, 1922 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p, "v60,v62,v65,v66" }, 1923 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_xacc, "v60,v62,v65,v66" }, 1924 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_acc, "v60,v62,v65,v66" }, 1925 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_and, "v60,v62,v65,v66" }, 1926 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_nac, "v60,v62,v65,v66" }, 1927 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_or, "v60,v62,v65,v66" }, 1928 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r, "v60,v62,v65,v66" }, 1929 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_xacc, "v60,v62,v65,v66" }, 1930 { Hexagon::BI__builtin_HEXAGON_S6_vsplatrbp, "v62,v65,v66" }, 1931 { Hexagon::BI__builtin_HEXAGON_S6_vtrunehb_ppp, "v62,v65,v66" }, 1932 { Hexagon::BI__builtin_HEXAGON_S6_vtrunohb_ppp, "v62,v65,v66" }, 1933 }; 1934 1935 static BuiltinAndString ValidHVX[] = { 1936 { Hexagon::BI__builtin_HEXAGON_V6_hi, "v60,v62,v65,v66" }, 1937 { Hexagon::BI__builtin_HEXAGON_V6_hi_128B, "v60,v62,v65,v66" }, 1938 { Hexagon::BI__builtin_HEXAGON_V6_lo, "v60,v62,v65,v66" }, 1939 { Hexagon::BI__builtin_HEXAGON_V6_lo_128B, "v60,v62,v65,v66" }, 1940 { Hexagon::BI__builtin_HEXAGON_V6_extractw, "v60,v62,v65,v66" }, 1941 { Hexagon::BI__builtin_HEXAGON_V6_extractw_128B, "v60,v62,v65,v66" }, 1942 { Hexagon::BI__builtin_HEXAGON_V6_lvsplatb, "v62,v65,v66" }, 1943 { Hexagon::BI__builtin_HEXAGON_V6_lvsplatb_128B, "v62,v65,v66" }, 1944 { Hexagon::BI__builtin_HEXAGON_V6_lvsplath, "v62,v65,v66" }, 1945 { Hexagon::BI__builtin_HEXAGON_V6_lvsplath_128B, "v62,v65,v66" }, 1946 { Hexagon::BI__builtin_HEXAGON_V6_lvsplatw, "v60,v62,v65,v66" }, 1947 { Hexagon::BI__builtin_HEXAGON_V6_lvsplatw_128B, "v60,v62,v65,v66" }, 1948 { Hexagon::BI__builtin_HEXAGON_V6_pred_and, "v60,v62,v65,v66" }, 1949 { Hexagon::BI__builtin_HEXAGON_V6_pred_and_128B, "v60,v62,v65,v66" }, 1950 { Hexagon::BI__builtin_HEXAGON_V6_pred_and_n, "v60,v62,v65,v66" }, 1951 { Hexagon::BI__builtin_HEXAGON_V6_pred_and_n_128B, "v60,v62,v65,v66" }, 1952 { Hexagon::BI__builtin_HEXAGON_V6_pred_not, "v60,v62,v65,v66" }, 1953 { Hexagon::BI__builtin_HEXAGON_V6_pred_not_128B, "v60,v62,v65,v66" }, 1954 { Hexagon::BI__builtin_HEXAGON_V6_pred_or, "v60,v62,v65,v66" }, 1955 { Hexagon::BI__builtin_HEXAGON_V6_pred_or_128B, "v60,v62,v65,v66" }, 1956 { Hexagon::BI__builtin_HEXAGON_V6_pred_or_n, "v60,v62,v65,v66" }, 1957 { Hexagon::BI__builtin_HEXAGON_V6_pred_or_n_128B, "v60,v62,v65,v66" }, 1958 { Hexagon::BI__builtin_HEXAGON_V6_pred_scalar2, "v60,v62,v65,v66" }, 1959 { Hexagon::BI__builtin_HEXAGON_V6_pred_scalar2_128B, "v60,v62,v65,v66" }, 1960 { Hexagon::BI__builtin_HEXAGON_V6_pred_scalar2v2, "v62,v65,v66" }, 1961 { Hexagon::BI__builtin_HEXAGON_V6_pred_scalar2v2_128B, "v62,v65,v66" }, 1962 { Hexagon::BI__builtin_HEXAGON_V6_pred_xor, "v60,v62,v65,v66" }, 1963 { Hexagon::BI__builtin_HEXAGON_V6_pred_xor_128B, "v60,v62,v65,v66" }, 1964 { Hexagon::BI__builtin_HEXAGON_V6_shuffeqh, "v62,v65,v66" }, 1965 { Hexagon::BI__builtin_HEXAGON_V6_shuffeqh_128B, "v62,v65,v66" }, 1966 { Hexagon::BI__builtin_HEXAGON_V6_shuffeqw, "v62,v65,v66" }, 1967 { Hexagon::BI__builtin_HEXAGON_V6_shuffeqw_128B, "v62,v65,v66" }, 1968 { Hexagon::BI__builtin_HEXAGON_V6_vabsb, "v65,v66" }, 1969 { Hexagon::BI__builtin_HEXAGON_V6_vabsb_128B, "v65,v66" }, 1970 { Hexagon::BI__builtin_HEXAGON_V6_vabsb_sat, "v65,v66" }, 1971 { Hexagon::BI__builtin_HEXAGON_V6_vabsb_sat_128B, "v65,v66" }, 1972 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffh, "v60,v62,v65,v66" }, 1973 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffh_128B, "v60,v62,v65,v66" }, 1974 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffub, "v60,v62,v65,v66" }, 1975 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffub_128B, "v60,v62,v65,v66" }, 1976 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffuh, "v60,v62,v65,v66" }, 1977 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffuh_128B, "v60,v62,v65,v66" }, 1978 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffw, "v60,v62,v65,v66" }, 1979 { Hexagon::BI__builtin_HEXAGON_V6_vabsdiffw_128B, "v60,v62,v65,v66" }, 1980 { Hexagon::BI__builtin_HEXAGON_V6_vabsh, "v60,v62,v65,v66" }, 1981 { Hexagon::BI__builtin_HEXAGON_V6_vabsh_128B, "v60,v62,v65,v66" }, 1982 { Hexagon::BI__builtin_HEXAGON_V6_vabsh_sat, "v60,v62,v65,v66" }, 1983 { Hexagon::BI__builtin_HEXAGON_V6_vabsh_sat_128B, "v60,v62,v65,v66" }, 1984 { Hexagon::BI__builtin_HEXAGON_V6_vabsw, "v60,v62,v65,v66" }, 1985 { Hexagon::BI__builtin_HEXAGON_V6_vabsw_128B, "v60,v62,v65,v66" }, 1986 { Hexagon::BI__builtin_HEXAGON_V6_vabsw_sat, "v60,v62,v65,v66" }, 1987 { Hexagon::BI__builtin_HEXAGON_V6_vabsw_sat_128B, "v60,v62,v65,v66" }, 1988 { Hexagon::BI__builtin_HEXAGON_V6_vaddb, "v60,v62,v65,v66" }, 1989 { Hexagon::BI__builtin_HEXAGON_V6_vaddb_128B, "v60,v62,v65,v66" }, 1990 { Hexagon::BI__builtin_HEXAGON_V6_vaddb_dv, "v60,v62,v65,v66" }, 1991 { Hexagon::BI__builtin_HEXAGON_V6_vaddb_dv_128B, "v60,v62,v65,v66" }, 1992 { Hexagon::BI__builtin_HEXAGON_V6_vaddbsat, "v62,v65,v66" }, 1993 { Hexagon::BI__builtin_HEXAGON_V6_vaddbsat_128B, "v62,v65,v66" }, 1994 { Hexagon::BI__builtin_HEXAGON_V6_vaddbsat_dv, "v62,v65,v66" }, 1995 { Hexagon::BI__builtin_HEXAGON_V6_vaddbsat_dv_128B, "v62,v65,v66" }, 1996 { Hexagon::BI__builtin_HEXAGON_V6_vaddcarry, "v62,v65,v66" }, 1997 { Hexagon::BI__builtin_HEXAGON_V6_vaddcarry_128B, "v62,v65,v66" }, 1998 { Hexagon::BI__builtin_HEXAGON_V6_vaddcarrysat, "v66" }, 1999 { Hexagon::BI__builtin_HEXAGON_V6_vaddcarrysat_128B, "v66" }, 2000 { Hexagon::BI__builtin_HEXAGON_V6_vaddclbh, "v62,v65,v66" }, 2001 { Hexagon::BI__builtin_HEXAGON_V6_vaddclbh_128B, "v62,v65,v66" }, 2002 { Hexagon::BI__builtin_HEXAGON_V6_vaddclbw, "v62,v65,v66" }, 2003 { Hexagon::BI__builtin_HEXAGON_V6_vaddclbw_128B, "v62,v65,v66" }, 2004 { Hexagon::BI__builtin_HEXAGON_V6_vaddh, "v60,v62,v65,v66" }, 2005 { Hexagon::BI__builtin_HEXAGON_V6_vaddh_128B, "v60,v62,v65,v66" }, 2006 { Hexagon::BI__builtin_HEXAGON_V6_vaddh_dv, "v60,v62,v65,v66" }, 2007 { Hexagon::BI__builtin_HEXAGON_V6_vaddh_dv_128B, "v60,v62,v65,v66" }, 2008 { Hexagon::BI__builtin_HEXAGON_V6_vaddhsat, "v60,v62,v65,v66" }, 2009 { Hexagon::BI__builtin_HEXAGON_V6_vaddhsat_128B, "v60,v62,v65,v66" }, 2010 { Hexagon::BI__builtin_HEXAGON_V6_vaddhsat_dv, "v60,v62,v65,v66" }, 2011 { Hexagon::BI__builtin_HEXAGON_V6_vaddhsat_dv_128B, "v60,v62,v65,v66" }, 2012 { Hexagon::BI__builtin_HEXAGON_V6_vaddhw, "v60,v62,v65,v66" }, 2013 { Hexagon::BI__builtin_HEXAGON_V6_vaddhw_128B, "v60,v62,v65,v66" }, 2014 { Hexagon::BI__builtin_HEXAGON_V6_vaddhw_acc, "v62,v65,v66" }, 2015 { Hexagon::BI__builtin_HEXAGON_V6_vaddhw_acc_128B, "v62,v65,v66" }, 2016 { Hexagon::BI__builtin_HEXAGON_V6_vaddubh, "v60,v62,v65,v66" }, 2017 { Hexagon::BI__builtin_HEXAGON_V6_vaddubh_128B, "v60,v62,v65,v66" }, 2018 { Hexagon::BI__builtin_HEXAGON_V6_vaddubh_acc, "v62,v65,v66" }, 2019 { Hexagon::BI__builtin_HEXAGON_V6_vaddubh_acc_128B, "v62,v65,v66" }, 2020 { Hexagon::BI__builtin_HEXAGON_V6_vaddubsat, "v60,v62,v65,v66" }, 2021 { Hexagon::BI__builtin_HEXAGON_V6_vaddubsat_128B, "v60,v62,v65,v66" }, 2022 { Hexagon::BI__builtin_HEXAGON_V6_vaddubsat_dv, "v60,v62,v65,v66" }, 2023 { Hexagon::BI__builtin_HEXAGON_V6_vaddubsat_dv_128B, "v60,v62,v65,v66" }, 2024 { Hexagon::BI__builtin_HEXAGON_V6_vaddububb_sat, "v62,v65,v66" }, 2025 { Hexagon::BI__builtin_HEXAGON_V6_vaddububb_sat_128B, "v62,v65,v66" }, 2026 { Hexagon::BI__builtin_HEXAGON_V6_vadduhsat, "v60,v62,v65,v66" }, 2027 { Hexagon::BI__builtin_HEXAGON_V6_vadduhsat_128B, "v60,v62,v65,v66" }, 2028 { Hexagon::BI__builtin_HEXAGON_V6_vadduhsat_dv, "v60,v62,v65,v66" }, 2029 { Hexagon::BI__builtin_HEXAGON_V6_vadduhsat_dv_128B, "v60,v62,v65,v66" }, 2030 { Hexagon::BI__builtin_HEXAGON_V6_vadduhw, "v60,v62,v65,v66" }, 2031 { Hexagon::BI__builtin_HEXAGON_V6_vadduhw_128B, "v60,v62,v65,v66" }, 2032 { Hexagon::BI__builtin_HEXAGON_V6_vadduhw_acc, "v62,v65,v66" }, 2033 { Hexagon::BI__builtin_HEXAGON_V6_vadduhw_acc_128B, "v62,v65,v66" }, 2034 { Hexagon::BI__builtin_HEXAGON_V6_vadduwsat, "v62,v65,v66" }, 2035 { Hexagon::BI__builtin_HEXAGON_V6_vadduwsat_128B, "v62,v65,v66" }, 2036 { Hexagon::BI__builtin_HEXAGON_V6_vadduwsat_dv, "v62,v65,v66" }, 2037 { Hexagon::BI__builtin_HEXAGON_V6_vadduwsat_dv_128B, "v62,v65,v66" }, 2038 { Hexagon::BI__builtin_HEXAGON_V6_vaddw, "v60,v62,v65,v66" }, 2039 { Hexagon::BI__builtin_HEXAGON_V6_vaddw_128B, "v60,v62,v65,v66" }, 2040 { Hexagon::BI__builtin_HEXAGON_V6_vaddw_dv, "v60,v62,v65,v66" }, 2041 { Hexagon::BI__builtin_HEXAGON_V6_vaddw_dv_128B, "v60,v62,v65,v66" }, 2042 { Hexagon::BI__builtin_HEXAGON_V6_vaddwsat, "v60,v62,v65,v66" }, 2043 { Hexagon::BI__builtin_HEXAGON_V6_vaddwsat_128B, "v60,v62,v65,v66" }, 2044 { Hexagon::BI__builtin_HEXAGON_V6_vaddwsat_dv, "v60,v62,v65,v66" }, 2045 { Hexagon::BI__builtin_HEXAGON_V6_vaddwsat_dv_128B, "v60,v62,v65,v66" }, 2046 { Hexagon::BI__builtin_HEXAGON_V6_valignb, "v60,v62,v65,v66" }, 2047 { Hexagon::BI__builtin_HEXAGON_V6_valignb_128B, "v60,v62,v65,v66" }, 2048 { Hexagon::BI__builtin_HEXAGON_V6_valignbi, "v60,v62,v65,v66" }, 2049 { Hexagon::BI__builtin_HEXAGON_V6_valignbi_128B, "v60,v62,v65,v66" }, 2050 { Hexagon::BI__builtin_HEXAGON_V6_vand, "v60,v62,v65,v66" }, 2051 { Hexagon::BI__builtin_HEXAGON_V6_vand_128B, "v60,v62,v65,v66" }, 2052 { Hexagon::BI__builtin_HEXAGON_V6_vandnqrt, "v62,v65,v66" }, 2053 { Hexagon::BI__builtin_HEXAGON_V6_vandnqrt_128B, "v62,v65,v66" }, 2054 { Hexagon::BI__builtin_HEXAGON_V6_vandnqrt_acc, "v62,v65,v66" }, 2055 { Hexagon::BI__builtin_HEXAGON_V6_vandnqrt_acc_128B, "v62,v65,v66" }, 2056 { Hexagon::BI__builtin_HEXAGON_V6_vandqrt, "v60,v62,v65,v66" }, 2057 { Hexagon::BI__builtin_HEXAGON_V6_vandqrt_128B, "v60,v62,v65,v66" }, 2058 { Hexagon::BI__builtin_HEXAGON_V6_vandqrt_acc, "v60,v62,v65,v66" }, 2059 { Hexagon::BI__builtin_HEXAGON_V6_vandqrt_acc_128B, "v60,v62,v65,v66" }, 2060 { Hexagon::BI__builtin_HEXAGON_V6_vandvnqv, "v62,v65,v66" }, 2061 { Hexagon::BI__builtin_HEXAGON_V6_vandvnqv_128B, "v62,v65,v66" }, 2062 { Hexagon::BI__builtin_HEXAGON_V6_vandvqv, "v62,v65,v66" }, 2063 { Hexagon::BI__builtin_HEXAGON_V6_vandvqv_128B, "v62,v65,v66" }, 2064 { Hexagon::BI__builtin_HEXAGON_V6_vandvrt, "v60,v62,v65,v66" }, 2065 { Hexagon::BI__builtin_HEXAGON_V6_vandvrt_128B, "v60,v62,v65,v66" }, 2066 { Hexagon::BI__builtin_HEXAGON_V6_vandvrt_acc, "v60,v62,v65,v66" }, 2067 { Hexagon::BI__builtin_HEXAGON_V6_vandvrt_acc_128B, "v60,v62,v65,v66" }, 2068 { Hexagon::BI__builtin_HEXAGON_V6_vaslh, "v60,v62,v65,v66" }, 2069 { Hexagon::BI__builtin_HEXAGON_V6_vaslh_128B, "v60,v62,v65,v66" }, 2070 { Hexagon::BI__builtin_HEXAGON_V6_vaslh_acc, "v65,v66" }, 2071 { Hexagon::BI__builtin_HEXAGON_V6_vaslh_acc_128B, "v65,v66" }, 2072 { Hexagon::BI__builtin_HEXAGON_V6_vaslhv, "v60,v62,v65,v66" }, 2073 { Hexagon::BI__builtin_HEXAGON_V6_vaslhv_128B, "v60,v62,v65,v66" }, 2074 { Hexagon::BI__builtin_HEXAGON_V6_vaslw, "v60,v62,v65,v66" }, 2075 { Hexagon::BI__builtin_HEXAGON_V6_vaslw_128B, "v60,v62,v65,v66" }, 2076 { Hexagon::BI__builtin_HEXAGON_V6_vaslw_acc, "v60,v62,v65,v66" }, 2077 { Hexagon::BI__builtin_HEXAGON_V6_vaslw_acc_128B, "v60,v62,v65,v66" }, 2078 { Hexagon::BI__builtin_HEXAGON_V6_vaslwv, "v60,v62,v65,v66" }, 2079 { Hexagon::BI__builtin_HEXAGON_V6_vaslwv_128B, "v60,v62,v65,v66" }, 2080 { Hexagon::BI__builtin_HEXAGON_V6_vasrh, "v60,v62,v65,v66" }, 2081 { Hexagon::BI__builtin_HEXAGON_V6_vasrh_128B, "v60,v62,v65,v66" }, 2082 { Hexagon::BI__builtin_HEXAGON_V6_vasrh_acc, "v65,v66" }, 2083 { Hexagon::BI__builtin_HEXAGON_V6_vasrh_acc_128B, "v65,v66" }, 2084 { Hexagon::BI__builtin_HEXAGON_V6_vasrhbrndsat, "v60,v62,v65,v66" }, 2085 { Hexagon::BI__builtin_HEXAGON_V6_vasrhbrndsat_128B, "v60,v62,v65,v66" }, 2086 { Hexagon::BI__builtin_HEXAGON_V6_vasrhbsat, "v62,v65,v66" }, 2087 { Hexagon::BI__builtin_HEXAGON_V6_vasrhbsat_128B, "v62,v65,v66" }, 2088 { Hexagon::BI__builtin_HEXAGON_V6_vasrhubrndsat, "v60,v62,v65,v66" }, 2089 { Hexagon::BI__builtin_HEXAGON_V6_vasrhubrndsat_128B, "v60,v62,v65,v66" }, 2090 { Hexagon::BI__builtin_HEXAGON_V6_vasrhubsat, "v60,v62,v65,v66" }, 2091 { Hexagon::BI__builtin_HEXAGON_V6_vasrhubsat_128B, "v60,v62,v65,v66" }, 2092 { Hexagon::BI__builtin_HEXAGON_V6_vasrhv, "v60,v62,v65,v66" }, 2093 { Hexagon::BI__builtin_HEXAGON_V6_vasrhv_128B, "v60,v62,v65,v66" }, 2094 { Hexagon::BI__builtin_HEXAGON_V6_vasr_into, "v66" }, 2095 { Hexagon::BI__builtin_HEXAGON_V6_vasr_into_128B, "v66" }, 2096 { Hexagon::BI__builtin_HEXAGON_V6_vasruhubrndsat, "v65,v66" }, 2097 { Hexagon::BI__builtin_HEXAGON_V6_vasruhubrndsat_128B, "v65,v66" }, 2098 { Hexagon::BI__builtin_HEXAGON_V6_vasruhubsat, "v65,v66" }, 2099 { Hexagon::BI__builtin_HEXAGON_V6_vasruhubsat_128B, "v65,v66" }, 2100 { Hexagon::BI__builtin_HEXAGON_V6_vasruwuhrndsat, "v62,v65,v66" }, 2101 { Hexagon::BI__builtin_HEXAGON_V6_vasruwuhrndsat_128B, "v62,v65,v66" }, 2102 { Hexagon::BI__builtin_HEXAGON_V6_vasruwuhsat, "v65,v66" }, 2103 { Hexagon::BI__builtin_HEXAGON_V6_vasruwuhsat_128B, "v65,v66" }, 2104 { Hexagon::BI__builtin_HEXAGON_V6_vasrw, "v60,v62,v65,v66" }, 2105 { Hexagon::BI__builtin_HEXAGON_V6_vasrw_128B, "v60,v62,v65,v66" }, 2106 { Hexagon::BI__builtin_HEXAGON_V6_vasrw_acc, "v60,v62,v65,v66" }, 2107 { Hexagon::BI__builtin_HEXAGON_V6_vasrw_acc_128B, "v60,v62,v65,v66" }, 2108 { Hexagon::BI__builtin_HEXAGON_V6_vasrwh, "v60,v62,v65,v66" }, 2109 { Hexagon::BI__builtin_HEXAGON_V6_vasrwh_128B, "v60,v62,v65,v66" }, 2110 { Hexagon::BI__builtin_HEXAGON_V6_vasrwhrndsat, "v60,v62,v65,v66" }, 2111 { Hexagon::BI__builtin_HEXAGON_V6_vasrwhrndsat_128B, "v60,v62,v65,v66" }, 2112 { Hexagon::BI__builtin_HEXAGON_V6_vasrwhsat, "v60,v62,v65,v66" }, 2113 { Hexagon::BI__builtin_HEXAGON_V6_vasrwhsat_128B, "v60,v62,v65,v66" }, 2114 { Hexagon::BI__builtin_HEXAGON_V6_vasrwuhrndsat, "v62,v65,v66" }, 2115 { Hexagon::BI__builtin_HEXAGON_V6_vasrwuhrndsat_128B, "v62,v65,v66" }, 2116 { Hexagon::BI__builtin_HEXAGON_V6_vasrwuhsat, "v60,v62,v65,v66" }, 2117 { Hexagon::BI__builtin_HEXAGON_V6_vasrwuhsat_128B, "v60,v62,v65,v66" }, 2118 { Hexagon::BI__builtin_HEXAGON_V6_vasrwv, "v60,v62,v65,v66" }, 2119 { Hexagon::BI__builtin_HEXAGON_V6_vasrwv_128B, "v60,v62,v65,v66" }, 2120 { Hexagon::BI__builtin_HEXAGON_V6_vassign, "v60,v62,v65,v66" }, 2121 { Hexagon::BI__builtin_HEXAGON_V6_vassign_128B, "v60,v62,v65,v66" }, 2122 { Hexagon::BI__builtin_HEXAGON_V6_vassignp, "v60,v62,v65,v66" }, 2123 { Hexagon::BI__builtin_HEXAGON_V6_vassignp_128B, "v60,v62,v65,v66" }, 2124 { Hexagon::BI__builtin_HEXAGON_V6_vavgb, "v65,v66" }, 2125 { Hexagon::BI__builtin_HEXAGON_V6_vavgb_128B, "v65,v66" }, 2126 { Hexagon::BI__builtin_HEXAGON_V6_vavgbrnd, "v65,v66" }, 2127 { Hexagon::BI__builtin_HEXAGON_V6_vavgbrnd_128B, "v65,v66" }, 2128 { Hexagon::BI__builtin_HEXAGON_V6_vavgh, "v60,v62,v65,v66" }, 2129 { Hexagon::BI__builtin_HEXAGON_V6_vavgh_128B, "v60,v62,v65,v66" }, 2130 { Hexagon::BI__builtin_HEXAGON_V6_vavghrnd, "v60,v62,v65,v66" }, 2131 { Hexagon::BI__builtin_HEXAGON_V6_vavghrnd_128B, "v60,v62,v65,v66" }, 2132 { Hexagon::BI__builtin_HEXAGON_V6_vavgub, "v60,v62,v65,v66" }, 2133 { Hexagon::BI__builtin_HEXAGON_V6_vavgub_128B, "v60,v62,v65,v66" }, 2134 { Hexagon::BI__builtin_HEXAGON_V6_vavgubrnd, "v60,v62,v65,v66" }, 2135 { Hexagon::BI__builtin_HEXAGON_V6_vavgubrnd_128B, "v60,v62,v65,v66" }, 2136 { Hexagon::BI__builtin_HEXAGON_V6_vavguh, "v60,v62,v65,v66" }, 2137 { Hexagon::BI__builtin_HEXAGON_V6_vavguh_128B, "v60,v62,v65,v66" }, 2138 { Hexagon::BI__builtin_HEXAGON_V6_vavguhrnd, "v60,v62,v65,v66" }, 2139 { Hexagon::BI__builtin_HEXAGON_V6_vavguhrnd_128B, "v60,v62,v65,v66" }, 2140 { Hexagon::BI__builtin_HEXAGON_V6_vavguw, "v65,v66" }, 2141 { Hexagon::BI__builtin_HEXAGON_V6_vavguw_128B, "v65,v66" }, 2142 { Hexagon::BI__builtin_HEXAGON_V6_vavguwrnd, "v65,v66" }, 2143 { Hexagon::BI__builtin_HEXAGON_V6_vavguwrnd_128B, "v65,v66" }, 2144 { Hexagon::BI__builtin_HEXAGON_V6_vavgw, "v60,v62,v65,v66" }, 2145 { Hexagon::BI__builtin_HEXAGON_V6_vavgw_128B, "v60,v62,v65,v66" }, 2146 { Hexagon::BI__builtin_HEXAGON_V6_vavgwrnd, "v60,v62,v65,v66" }, 2147 { Hexagon::BI__builtin_HEXAGON_V6_vavgwrnd_128B, "v60,v62,v65,v66" }, 2148 { Hexagon::BI__builtin_HEXAGON_V6_vcl0h, "v60,v62,v65,v66" }, 2149 { Hexagon::BI__builtin_HEXAGON_V6_vcl0h_128B, "v60,v62,v65,v66" }, 2150 { Hexagon::BI__builtin_HEXAGON_V6_vcl0w, "v60,v62,v65,v66" }, 2151 { Hexagon::BI__builtin_HEXAGON_V6_vcl0w_128B, "v60,v62,v65,v66" }, 2152 { Hexagon::BI__builtin_HEXAGON_V6_vcombine, "v60,v62,v65,v66" }, 2153 { Hexagon::BI__builtin_HEXAGON_V6_vcombine_128B, "v60,v62,v65,v66" }, 2154 { Hexagon::BI__builtin_HEXAGON_V6_vd0, "v60,v62,v65,v66" }, 2155 { Hexagon::BI__builtin_HEXAGON_V6_vd0_128B, "v60,v62,v65,v66" }, 2156 { Hexagon::BI__builtin_HEXAGON_V6_vdd0, "v65,v66" }, 2157 { Hexagon::BI__builtin_HEXAGON_V6_vdd0_128B, "v65,v66" }, 2158 { Hexagon::BI__builtin_HEXAGON_V6_vdealb, "v60,v62,v65,v66" }, 2159 { Hexagon::BI__builtin_HEXAGON_V6_vdealb_128B, "v60,v62,v65,v66" }, 2160 { Hexagon::BI__builtin_HEXAGON_V6_vdealb4w, "v60,v62,v65,v66" }, 2161 { Hexagon::BI__builtin_HEXAGON_V6_vdealb4w_128B, "v60,v62,v65,v66" }, 2162 { Hexagon::BI__builtin_HEXAGON_V6_vdealh, "v60,v62,v65,v66" }, 2163 { Hexagon::BI__builtin_HEXAGON_V6_vdealh_128B, "v60,v62,v65,v66" }, 2164 { Hexagon::BI__builtin_HEXAGON_V6_vdealvdd, "v60,v62,v65,v66" }, 2165 { Hexagon::BI__builtin_HEXAGON_V6_vdealvdd_128B, "v60,v62,v65,v66" }, 2166 { Hexagon::BI__builtin_HEXAGON_V6_vdelta, "v60,v62,v65,v66" }, 2167 { Hexagon::BI__builtin_HEXAGON_V6_vdelta_128B, "v60,v62,v65,v66" }, 2168 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus, "v60,v62,v65,v66" }, 2169 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_128B, "v60,v62,v65,v66" }, 2170 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_acc, "v60,v62,v65,v66" }, 2171 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_acc_128B, "v60,v62,v65,v66" }, 2172 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_dv, "v60,v62,v65,v66" }, 2173 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_dv_128B, "v60,v62,v65,v66" }, 2174 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_dv_acc, "v60,v62,v65,v66" }, 2175 { Hexagon::BI__builtin_HEXAGON_V6_vdmpybus_dv_acc_128B, "v60,v62,v65,v66" }, 2176 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb, "v60,v62,v65,v66" }, 2177 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_128B, "v60,v62,v65,v66" }, 2178 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_acc, "v60,v62,v65,v66" }, 2179 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_acc_128B, "v60,v62,v65,v66" }, 2180 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_dv, "v60,v62,v65,v66" }, 2181 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_dv_128B, "v60,v62,v65,v66" }, 2182 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_dv_acc, "v60,v62,v65,v66" }, 2183 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhb_dv_acc_128B, "v60,v62,v65,v66" }, 2184 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhisat, "v60,v62,v65,v66" }, 2185 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhisat_128B, "v60,v62,v65,v66" }, 2186 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhisat_acc, "v60,v62,v65,v66" }, 2187 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhisat_acc_128B, "v60,v62,v65,v66" }, 2188 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsat, "v60,v62,v65,v66" }, 2189 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsat_128B, "v60,v62,v65,v66" }, 2190 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsat_acc, "v60,v62,v65,v66" }, 2191 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsat_acc_128B, "v60,v62,v65,v66" }, 2192 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsuisat, "v60,v62,v65,v66" }, 2193 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsuisat_128B, "v60,v62,v65,v66" }, 2194 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsuisat_acc, "v60,v62,v65,v66" }, 2195 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsuisat_acc_128B, "v60,v62,v65,v66" }, 2196 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsusat, "v60,v62,v65,v66" }, 2197 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsusat_128B, "v60,v62,v65,v66" }, 2198 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsusat_acc, "v60,v62,v65,v66" }, 2199 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhsusat_acc_128B, "v60,v62,v65,v66" }, 2200 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhvsat, "v60,v62,v65,v66" }, 2201 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhvsat_128B, "v60,v62,v65,v66" }, 2202 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhvsat_acc, "v60,v62,v65,v66" }, 2203 { Hexagon::BI__builtin_HEXAGON_V6_vdmpyhvsat_acc_128B, "v60,v62,v65,v66" }, 2204 { Hexagon::BI__builtin_HEXAGON_V6_vdsaduh, "v60,v62,v65,v66" }, 2205 { Hexagon::BI__builtin_HEXAGON_V6_vdsaduh_128B, "v60,v62,v65,v66" }, 2206 { Hexagon::BI__builtin_HEXAGON_V6_vdsaduh_acc, "v60,v62,v65,v66" }, 2207 { Hexagon::BI__builtin_HEXAGON_V6_vdsaduh_acc_128B, "v60,v62,v65,v66" }, 2208 { Hexagon::BI__builtin_HEXAGON_V6_veqb, "v60,v62,v65,v66" }, 2209 { Hexagon::BI__builtin_HEXAGON_V6_veqb_128B, "v60,v62,v65,v66" }, 2210 { Hexagon::BI__builtin_HEXAGON_V6_veqb_and, "v60,v62,v65,v66" }, 2211 { Hexagon::BI__builtin_HEXAGON_V6_veqb_and_128B, "v60,v62,v65,v66" }, 2212 { Hexagon::BI__builtin_HEXAGON_V6_veqb_or, "v60,v62,v65,v66" }, 2213 { Hexagon::BI__builtin_HEXAGON_V6_veqb_or_128B, "v60,v62,v65,v66" }, 2214 { Hexagon::BI__builtin_HEXAGON_V6_veqb_xor, "v60,v62,v65,v66" }, 2215 { Hexagon::BI__builtin_HEXAGON_V6_veqb_xor_128B, "v60,v62,v65,v66" }, 2216 { Hexagon::BI__builtin_HEXAGON_V6_veqh, "v60,v62,v65,v66" }, 2217 { Hexagon::BI__builtin_HEXAGON_V6_veqh_128B, "v60,v62,v65,v66" }, 2218 { Hexagon::BI__builtin_HEXAGON_V6_veqh_and, "v60,v62,v65,v66" }, 2219 { Hexagon::BI__builtin_HEXAGON_V6_veqh_and_128B, "v60,v62,v65,v66" }, 2220 { Hexagon::BI__builtin_HEXAGON_V6_veqh_or, "v60,v62,v65,v66" }, 2221 { Hexagon::BI__builtin_HEXAGON_V6_veqh_or_128B, "v60,v62,v65,v66" }, 2222 { Hexagon::BI__builtin_HEXAGON_V6_veqh_xor, "v60,v62,v65,v66" }, 2223 { Hexagon::BI__builtin_HEXAGON_V6_veqh_xor_128B, "v60,v62,v65,v66" }, 2224 { Hexagon::BI__builtin_HEXAGON_V6_veqw, "v60,v62,v65,v66" }, 2225 { Hexagon::BI__builtin_HEXAGON_V6_veqw_128B, "v60,v62,v65,v66" }, 2226 { Hexagon::BI__builtin_HEXAGON_V6_veqw_and, "v60,v62,v65,v66" }, 2227 { Hexagon::BI__builtin_HEXAGON_V6_veqw_and_128B, "v60,v62,v65,v66" }, 2228 { Hexagon::BI__builtin_HEXAGON_V6_veqw_or, "v60,v62,v65,v66" }, 2229 { Hexagon::BI__builtin_HEXAGON_V6_veqw_or_128B, "v60,v62,v65,v66" }, 2230 { Hexagon::BI__builtin_HEXAGON_V6_veqw_xor, "v60,v62,v65,v66" }, 2231 { Hexagon::BI__builtin_HEXAGON_V6_veqw_xor_128B, "v60,v62,v65,v66" }, 2232 { Hexagon::BI__builtin_HEXAGON_V6_vgtb, "v60,v62,v65,v66" }, 2233 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_128B, "v60,v62,v65,v66" }, 2234 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_and, "v60,v62,v65,v66" }, 2235 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_and_128B, "v60,v62,v65,v66" }, 2236 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_or, "v60,v62,v65,v66" }, 2237 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_or_128B, "v60,v62,v65,v66" }, 2238 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_xor, "v60,v62,v65,v66" }, 2239 { Hexagon::BI__builtin_HEXAGON_V6_vgtb_xor_128B, "v60,v62,v65,v66" }, 2240 { Hexagon::BI__builtin_HEXAGON_V6_vgth, "v60,v62,v65,v66" }, 2241 { Hexagon::BI__builtin_HEXAGON_V6_vgth_128B, "v60,v62,v65,v66" }, 2242 { Hexagon::BI__builtin_HEXAGON_V6_vgth_and, "v60,v62,v65,v66" }, 2243 { Hexagon::BI__builtin_HEXAGON_V6_vgth_and_128B, "v60,v62,v65,v66" }, 2244 { Hexagon::BI__builtin_HEXAGON_V6_vgth_or, "v60,v62,v65,v66" }, 2245 { Hexagon::BI__builtin_HEXAGON_V6_vgth_or_128B, "v60,v62,v65,v66" }, 2246 { Hexagon::BI__builtin_HEXAGON_V6_vgth_xor, "v60,v62,v65,v66" }, 2247 { Hexagon::BI__builtin_HEXAGON_V6_vgth_xor_128B, "v60,v62,v65,v66" }, 2248 { Hexagon::BI__builtin_HEXAGON_V6_vgtub, "v60,v62,v65,v66" }, 2249 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_128B, "v60,v62,v65,v66" }, 2250 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_and, "v60,v62,v65,v66" }, 2251 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_and_128B, "v60,v62,v65,v66" }, 2252 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_or, "v60,v62,v65,v66" }, 2253 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_or_128B, "v60,v62,v65,v66" }, 2254 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_xor, "v60,v62,v65,v66" }, 2255 { Hexagon::BI__builtin_HEXAGON_V6_vgtub_xor_128B, "v60,v62,v65,v66" }, 2256 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh, "v60,v62,v65,v66" }, 2257 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_128B, "v60,v62,v65,v66" }, 2258 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_and, "v60,v62,v65,v66" }, 2259 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_and_128B, "v60,v62,v65,v66" }, 2260 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_or, "v60,v62,v65,v66" }, 2261 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_or_128B, "v60,v62,v65,v66" }, 2262 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_xor, "v60,v62,v65,v66" }, 2263 { Hexagon::BI__builtin_HEXAGON_V6_vgtuh_xor_128B, "v60,v62,v65,v66" }, 2264 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw, "v60,v62,v65,v66" }, 2265 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_128B, "v60,v62,v65,v66" }, 2266 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_and, "v60,v62,v65,v66" }, 2267 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_and_128B, "v60,v62,v65,v66" }, 2268 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_or, "v60,v62,v65,v66" }, 2269 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_or_128B, "v60,v62,v65,v66" }, 2270 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_xor, "v60,v62,v65,v66" }, 2271 { Hexagon::BI__builtin_HEXAGON_V6_vgtuw_xor_128B, "v60,v62,v65,v66" }, 2272 { Hexagon::BI__builtin_HEXAGON_V6_vgtw, "v60,v62,v65,v66" }, 2273 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_128B, "v60,v62,v65,v66" }, 2274 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_and, "v60,v62,v65,v66" }, 2275 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_and_128B, "v60,v62,v65,v66" }, 2276 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_or, "v60,v62,v65,v66" }, 2277 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_or_128B, "v60,v62,v65,v66" }, 2278 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_xor, "v60,v62,v65,v66" }, 2279 { Hexagon::BI__builtin_HEXAGON_V6_vgtw_xor_128B, "v60,v62,v65,v66" }, 2280 { Hexagon::BI__builtin_HEXAGON_V6_vinsertwr, "v60,v62,v65,v66" }, 2281 { Hexagon::BI__builtin_HEXAGON_V6_vinsertwr_128B, "v60,v62,v65,v66" }, 2282 { Hexagon::BI__builtin_HEXAGON_V6_vlalignb, "v60,v62,v65,v66" }, 2283 { Hexagon::BI__builtin_HEXAGON_V6_vlalignb_128B, "v60,v62,v65,v66" }, 2284 { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi, "v60,v62,v65,v66" }, 2285 { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi_128B, "v60,v62,v65,v66" }, 2286 { Hexagon::BI__builtin_HEXAGON_V6_vlsrb, "v62,v65,v66" }, 2287 { Hexagon::BI__builtin_HEXAGON_V6_vlsrb_128B, "v62,v65,v66" }, 2288 { Hexagon::BI__builtin_HEXAGON_V6_vlsrh, "v60,v62,v65,v66" }, 2289 { Hexagon::BI__builtin_HEXAGON_V6_vlsrh_128B, "v60,v62,v65,v66" }, 2290 { Hexagon::BI__builtin_HEXAGON_V6_vlsrhv, "v60,v62,v65,v66" }, 2291 { Hexagon::BI__builtin_HEXAGON_V6_vlsrhv_128B, "v60,v62,v65,v66" }, 2292 { Hexagon::BI__builtin_HEXAGON_V6_vlsrw, "v60,v62,v65,v66" }, 2293 { Hexagon::BI__builtin_HEXAGON_V6_vlsrw_128B, "v60,v62,v65,v66" }, 2294 { Hexagon::BI__builtin_HEXAGON_V6_vlsrwv, "v60,v62,v65,v66" }, 2295 { Hexagon::BI__builtin_HEXAGON_V6_vlsrwv_128B, "v60,v62,v65,v66" }, 2296 { Hexagon::BI__builtin_HEXAGON_V6_vlut4, "v65,v66" }, 2297 { Hexagon::BI__builtin_HEXAGON_V6_vlut4_128B, "v65,v66" }, 2298 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb, "v60,v62,v65,v66" }, 2299 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_128B, "v60,v62,v65,v66" }, 2300 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvbi, "v62,v65,v66" }, 2301 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvbi_128B, "v62,v65,v66" }, 2302 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_nm, "v62,v65,v66" }, 2303 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_nm_128B, "v62,v65,v66" }, 2304 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_oracc, "v60,v62,v65,v66" }, 2305 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_oracc_128B, "v60,v62,v65,v66" }, 2306 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_oracci, "v62,v65,v66" }, 2307 { Hexagon::BI__builtin_HEXAGON_V6_vlutvvb_oracci_128B, "v62,v65,v66" }, 2308 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh, "v60,v62,v65,v66" }, 2309 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_128B, "v60,v62,v65,v66" }, 2310 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwhi, "v62,v65,v66" }, 2311 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwhi_128B, "v62,v65,v66" }, 2312 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_nm, "v62,v65,v66" }, 2313 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_nm_128B, "v62,v65,v66" }, 2314 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_oracc, "v60,v62,v65,v66" }, 2315 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_oracc_128B, "v60,v62,v65,v66" }, 2316 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_oracci, "v62,v65,v66" }, 2317 { Hexagon::BI__builtin_HEXAGON_V6_vlutvwh_oracci_128B, "v62,v65,v66" }, 2318 { Hexagon::BI__builtin_HEXAGON_V6_vmaxb, "v62,v65,v66" }, 2319 { Hexagon::BI__builtin_HEXAGON_V6_vmaxb_128B, "v62,v65,v66" }, 2320 { Hexagon::BI__builtin_HEXAGON_V6_vmaxh, "v60,v62,v65,v66" }, 2321 { Hexagon::BI__builtin_HEXAGON_V6_vmaxh_128B, "v60,v62,v65,v66" }, 2322 { Hexagon::BI__builtin_HEXAGON_V6_vmaxub, "v60,v62,v65,v66" }, 2323 { Hexagon::BI__builtin_HEXAGON_V6_vmaxub_128B, "v60,v62,v65,v66" }, 2324 { Hexagon::BI__builtin_HEXAGON_V6_vmaxuh, "v60,v62,v65,v66" }, 2325 { Hexagon::BI__builtin_HEXAGON_V6_vmaxuh_128B, "v60,v62,v65,v66" }, 2326 { Hexagon::BI__builtin_HEXAGON_V6_vmaxw, "v60,v62,v65,v66" }, 2327 { Hexagon::BI__builtin_HEXAGON_V6_vmaxw_128B, "v60,v62,v65,v66" }, 2328 { Hexagon::BI__builtin_HEXAGON_V6_vminb, "v62,v65,v66" }, 2329 { Hexagon::BI__builtin_HEXAGON_V6_vminb_128B, "v62,v65,v66" }, 2330 { Hexagon::BI__builtin_HEXAGON_V6_vminh, "v60,v62,v65,v66" }, 2331 { Hexagon::BI__builtin_HEXAGON_V6_vminh_128B, "v60,v62,v65,v66" }, 2332 { Hexagon::BI__builtin_HEXAGON_V6_vminub, "v60,v62,v65,v66" }, 2333 { Hexagon::BI__builtin_HEXAGON_V6_vminub_128B, "v60,v62,v65,v66" }, 2334 { Hexagon::BI__builtin_HEXAGON_V6_vminuh, "v60,v62,v65,v66" }, 2335 { Hexagon::BI__builtin_HEXAGON_V6_vminuh_128B, "v60,v62,v65,v66" }, 2336 { Hexagon::BI__builtin_HEXAGON_V6_vminw, "v60,v62,v65,v66" }, 2337 { Hexagon::BI__builtin_HEXAGON_V6_vminw_128B, "v60,v62,v65,v66" }, 2338 { Hexagon::BI__builtin_HEXAGON_V6_vmpabus, "v60,v62,v65,v66" }, 2339 { Hexagon::BI__builtin_HEXAGON_V6_vmpabus_128B, "v60,v62,v65,v66" }, 2340 { Hexagon::BI__builtin_HEXAGON_V6_vmpabus_acc, "v60,v62,v65,v66" }, 2341 { Hexagon::BI__builtin_HEXAGON_V6_vmpabus_acc_128B, "v60,v62,v65,v66" }, 2342 { Hexagon::BI__builtin_HEXAGON_V6_vmpabusv, "v60,v62,v65,v66" }, 2343 { Hexagon::BI__builtin_HEXAGON_V6_vmpabusv_128B, "v60,v62,v65,v66" }, 2344 { Hexagon::BI__builtin_HEXAGON_V6_vmpabuu, "v65,v66" }, 2345 { Hexagon::BI__builtin_HEXAGON_V6_vmpabuu_128B, "v65,v66" }, 2346 { Hexagon::BI__builtin_HEXAGON_V6_vmpabuu_acc, "v65,v66" }, 2347 { Hexagon::BI__builtin_HEXAGON_V6_vmpabuu_acc_128B, "v65,v66" }, 2348 { Hexagon::BI__builtin_HEXAGON_V6_vmpabuuv, "v60,v62,v65,v66" }, 2349 { Hexagon::BI__builtin_HEXAGON_V6_vmpabuuv_128B, "v60,v62,v65,v66" }, 2350 { Hexagon::BI__builtin_HEXAGON_V6_vmpahb, "v60,v62,v65,v66" }, 2351 { Hexagon::BI__builtin_HEXAGON_V6_vmpahb_128B, "v60,v62,v65,v66" }, 2352 { Hexagon::BI__builtin_HEXAGON_V6_vmpahb_acc, "v60,v62,v65,v66" }, 2353 { Hexagon::BI__builtin_HEXAGON_V6_vmpahb_acc_128B, "v60,v62,v65,v66" }, 2354 { Hexagon::BI__builtin_HEXAGON_V6_vmpahhsat, "v65,v66" }, 2355 { Hexagon::BI__builtin_HEXAGON_V6_vmpahhsat_128B, "v65,v66" }, 2356 { Hexagon::BI__builtin_HEXAGON_V6_vmpauhb, "v62,v65,v66" }, 2357 { Hexagon::BI__builtin_HEXAGON_V6_vmpauhb_128B, "v62,v65,v66" }, 2358 { Hexagon::BI__builtin_HEXAGON_V6_vmpauhb_acc, "v62,v65,v66" }, 2359 { Hexagon::BI__builtin_HEXAGON_V6_vmpauhb_acc_128B, "v62,v65,v66" }, 2360 { Hexagon::BI__builtin_HEXAGON_V6_vmpauhuhsat, "v65,v66" }, 2361 { Hexagon::BI__builtin_HEXAGON_V6_vmpauhuhsat_128B, "v65,v66" }, 2362 { Hexagon::BI__builtin_HEXAGON_V6_vmpsuhuhsat, "v65,v66" }, 2363 { Hexagon::BI__builtin_HEXAGON_V6_vmpsuhuhsat_128B, "v65,v66" }, 2364 { Hexagon::BI__builtin_HEXAGON_V6_vmpybus, "v60,v62,v65,v66" }, 2365 { Hexagon::BI__builtin_HEXAGON_V6_vmpybus_128B, "v60,v62,v65,v66" }, 2366 { Hexagon::BI__builtin_HEXAGON_V6_vmpybus_acc, "v60,v62,v65,v66" }, 2367 { Hexagon::BI__builtin_HEXAGON_V6_vmpybus_acc_128B, "v60,v62,v65,v66" }, 2368 { Hexagon::BI__builtin_HEXAGON_V6_vmpybusv, "v60,v62,v65,v66" }, 2369 { Hexagon::BI__builtin_HEXAGON_V6_vmpybusv_128B, "v60,v62,v65,v66" }, 2370 { Hexagon::BI__builtin_HEXAGON_V6_vmpybusv_acc, "v60,v62,v65,v66" }, 2371 { Hexagon::BI__builtin_HEXAGON_V6_vmpybusv_acc_128B, "v60,v62,v65,v66" }, 2372 { Hexagon::BI__builtin_HEXAGON_V6_vmpybv, "v60,v62,v65,v66" }, 2373 { Hexagon::BI__builtin_HEXAGON_V6_vmpybv_128B, "v60,v62,v65,v66" }, 2374 { Hexagon::BI__builtin_HEXAGON_V6_vmpybv_acc, "v60,v62,v65,v66" }, 2375 { Hexagon::BI__builtin_HEXAGON_V6_vmpybv_acc_128B, "v60,v62,v65,v66" }, 2376 { Hexagon::BI__builtin_HEXAGON_V6_vmpyewuh, "v60,v62,v65,v66" }, 2377 { Hexagon::BI__builtin_HEXAGON_V6_vmpyewuh_128B, "v60,v62,v65,v66" }, 2378 { Hexagon::BI__builtin_HEXAGON_V6_vmpyewuh_64, "v62,v65,v66" }, 2379 { Hexagon::BI__builtin_HEXAGON_V6_vmpyewuh_64_128B, "v62,v65,v66" }, 2380 { Hexagon::BI__builtin_HEXAGON_V6_vmpyh, "v60,v62,v65,v66" }, 2381 { Hexagon::BI__builtin_HEXAGON_V6_vmpyh_128B, "v60,v62,v65,v66" }, 2382 { Hexagon::BI__builtin_HEXAGON_V6_vmpyh_acc, "v65,v66" }, 2383 { Hexagon::BI__builtin_HEXAGON_V6_vmpyh_acc_128B, "v65,v66" }, 2384 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhsat_acc, "v60,v62,v65,v66" }, 2385 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhsat_acc_128B, "v60,v62,v65,v66" }, 2386 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhsrs, "v60,v62,v65,v66" }, 2387 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhsrs_128B, "v60,v62,v65,v66" }, 2388 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhss, "v60,v62,v65,v66" }, 2389 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhss_128B, "v60,v62,v65,v66" }, 2390 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhus, "v60,v62,v65,v66" }, 2391 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhus_128B, "v60,v62,v65,v66" }, 2392 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhus_acc, "v60,v62,v65,v66" }, 2393 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhus_acc_128B, "v60,v62,v65,v66" }, 2394 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhv, "v60,v62,v65,v66" }, 2395 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhv_128B, "v60,v62,v65,v66" }, 2396 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhv_acc, "v60,v62,v65,v66" }, 2397 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhv_acc_128B, "v60,v62,v65,v66" }, 2398 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhvsrs, "v60,v62,v65,v66" }, 2399 { Hexagon::BI__builtin_HEXAGON_V6_vmpyhvsrs_128B, "v60,v62,v65,v66" }, 2400 { Hexagon::BI__builtin_HEXAGON_V6_vmpyieoh, "v60,v62,v65,v66" }, 2401 { Hexagon::BI__builtin_HEXAGON_V6_vmpyieoh_128B, "v60,v62,v65,v66" }, 2402 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiewh_acc, "v60,v62,v65,v66" }, 2403 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiewh_acc_128B, "v60,v62,v65,v66" }, 2404 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiewuh, "v60,v62,v65,v66" }, 2405 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiewuh_128B, "v60,v62,v65,v66" }, 2406 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiewuh_acc, "v60,v62,v65,v66" }, 2407 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiewuh_acc_128B, "v60,v62,v65,v66" }, 2408 { Hexagon::BI__builtin_HEXAGON_V6_vmpyih, "v60,v62,v65,v66" }, 2409 { Hexagon::BI__builtin_HEXAGON_V6_vmpyih_128B, "v60,v62,v65,v66" }, 2410 { Hexagon::BI__builtin_HEXAGON_V6_vmpyih_acc, "v60,v62,v65,v66" }, 2411 { Hexagon::BI__builtin_HEXAGON_V6_vmpyih_acc_128B, "v60,v62,v65,v66" }, 2412 { Hexagon::BI__builtin_HEXAGON_V6_vmpyihb, "v60,v62,v65,v66" }, 2413 { Hexagon::BI__builtin_HEXAGON_V6_vmpyihb_128B, "v60,v62,v65,v66" }, 2414 { Hexagon::BI__builtin_HEXAGON_V6_vmpyihb_acc, "v60,v62,v65,v66" }, 2415 { Hexagon::BI__builtin_HEXAGON_V6_vmpyihb_acc_128B, "v60,v62,v65,v66" }, 2416 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiowh, "v60,v62,v65,v66" }, 2417 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiowh_128B, "v60,v62,v65,v66" }, 2418 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwb, "v60,v62,v65,v66" }, 2419 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwb_128B, "v60,v62,v65,v66" }, 2420 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwb_acc, "v60,v62,v65,v66" }, 2421 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwb_acc_128B, "v60,v62,v65,v66" }, 2422 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwh, "v60,v62,v65,v66" }, 2423 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwh_128B, "v60,v62,v65,v66" }, 2424 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwh_acc, "v60,v62,v65,v66" }, 2425 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwh_acc_128B, "v60,v62,v65,v66" }, 2426 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwub, "v62,v65,v66" }, 2427 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwub_128B, "v62,v65,v66" }, 2428 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwub_acc, "v62,v65,v66" }, 2429 { Hexagon::BI__builtin_HEXAGON_V6_vmpyiwub_acc_128B, "v62,v65,v66" }, 2430 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh, "v60,v62,v65,v66" }, 2431 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_128B, "v60,v62,v65,v66" }, 2432 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_64_acc, "v62,v65,v66" }, 2433 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_64_acc_128B, "v62,v65,v66" }, 2434 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_rnd, "v60,v62,v65,v66" }, 2435 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_rnd_128B, "v60,v62,v65,v66" }, 2436 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_rnd_sacc, "v60,v62,v65,v66" }, 2437 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_rnd_sacc_128B, "v60,v62,v65,v66" }, 2438 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_sacc, "v60,v62,v65,v66" }, 2439 { Hexagon::BI__builtin_HEXAGON_V6_vmpyowh_sacc_128B, "v60,v62,v65,v66" }, 2440 { Hexagon::BI__builtin_HEXAGON_V6_vmpyub, "v60,v62,v65,v66" }, 2441 { Hexagon::BI__builtin_HEXAGON_V6_vmpyub_128B, "v60,v62,v65,v66" }, 2442 { Hexagon::BI__builtin_HEXAGON_V6_vmpyub_acc, "v60,v62,v65,v66" }, 2443 { Hexagon::BI__builtin_HEXAGON_V6_vmpyub_acc_128B, "v60,v62,v65,v66" }, 2444 { Hexagon::BI__builtin_HEXAGON_V6_vmpyubv, "v60,v62,v65,v66" }, 2445 { Hexagon::BI__builtin_HEXAGON_V6_vmpyubv_128B, "v60,v62,v65,v66" }, 2446 { Hexagon::BI__builtin_HEXAGON_V6_vmpyubv_acc, "v60,v62,v65,v66" }, 2447 { Hexagon::BI__builtin_HEXAGON_V6_vmpyubv_acc_128B, "v60,v62,v65,v66" }, 2448 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuh, "v60,v62,v65,v66" }, 2449 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuh_128B, "v60,v62,v65,v66" }, 2450 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuh_acc, "v60,v62,v65,v66" }, 2451 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuh_acc_128B, "v60,v62,v65,v66" }, 2452 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhe, "v65,v66" }, 2453 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhe_128B, "v65,v66" }, 2454 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhe_acc, "v65,v66" }, 2455 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhe_acc_128B, "v65,v66" }, 2456 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhv, "v60,v62,v65,v66" }, 2457 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhv_128B, "v60,v62,v65,v66" }, 2458 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhv_acc, "v60,v62,v65,v66" }, 2459 { Hexagon::BI__builtin_HEXAGON_V6_vmpyuhv_acc_128B, "v60,v62,v65,v66" }, 2460 { Hexagon::BI__builtin_HEXAGON_V6_vmux, "v60,v62,v65,v66" }, 2461 { Hexagon::BI__builtin_HEXAGON_V6_vmux_128B, "v60,v62,v65,v66" }, 2462 { Hexagon::BI__builtin_HEXAGON_V6_vnavgb, "v65,v66" }, 2463 { Hexagon::BI__builtin_HEXAGON_V6_vnavgb_128B, "v65,v66" }, 2464 { Hexagon::BI__builtin_HEXAGON_V6_vnavgh, "v60,v62,v65,v66" }, 2465 { Hexagon::BI__builtin_HEXAGON_V6_vnavgh_128B, "v60,v62,v65,v66" }, 2466 { Hexagon::BI__builtin_HEXAGON_V6_vnavgub, "v60,v62,v65,v66" }, 2467 { Hexagon::BI__builtin_HEXAGON_V6_vnavgub_128B, "v60,v62,v65,v66" }, 2468 { Hexagon::BI__builtin_HEXAGON_V6_vnavgw, "v60,v62,v65,v66" }, 2469 { Hexagon::BI__builtin_HEXAGON_V6_vnavgw_128B, "v60,v62,v65,v66" }, 2470 { Hexagon::BI__builtin_HEXAGON_V6_vnormamth, "v60,v62,v65,v66" }, 2471 { Hexagon::BI__builtin_HEXAGON_V6_vnormamth_128B, "v60,v62,v65,v66" }, 2472 { Hexagon::BI__builtin_HEXAGON_V6_vnormamtw, "v60,v62,v65,v66" }, 2473 { Hexagon::BI__builtin_HEXAGON_V6_vnormamtw_128B, "v60,v62,v65,v66" }, 2474 { Hexagon::BI__builtin_HEXAGON_V6_vnot, "v60,v62,v65,v66" }, 2475 { Hexagon::BI__builtin_HEXAGON_V6_vnot_128B, "v60,v62,v65,v66" }, 2476 { Hexagon::BI__builtin_HEXAGON_V6_vor, "v60,v62,v65,v66" }, 2477 { Hexagon::BI__builtin_HEXAGON_V6_vor_128B, "v60,v62,v65,v66" }, 2478 { Hexagon::BI__builtin_HEXAGON_V6_vpackeb, "v60,v62,v65,v66" }, 2479 { Hexagon::BI__builtin_HEXAGON_V6_vpackeb_128B, "v60,v62,v65,v66" }, 2480 { Hexagon::BI__builtin_HEXAGON_V6_vpackeh, "v60,v62,v65,v66" }, 2481 { Hexagon::BI__builtin_HEXAGON_V6_vpackeh_128B, "v60,v62,v65,v66" }, 2482 { Hexagon::BI__builtin_HEXAGON_V6_vpackhb_sat, "v60,v62,v65,v66" }, 2483 { Hexagon::BI__builtin_HEXAGON_V6_vpackhb_sat_128B, "v60,v62,v65,v66" }, 2484 { Hexagon::BI__builtin_HEXAGON_V6_vpackhub_sat, "v60,v62,v65,v66" }, 2485 { Hexagon::BI__builtin_HEXAGON_V6_vpackhub_sat_128B, "v60,v62,v65,v66" }, 2486 { Hexagon::BI__builtin_HEXAGON_V6_vpackob, "v60,v62,v65,v66" }, 2487 { Hexagon::BI__builtin_HEXAGON_V6_vpackob_128B, "v60,v62,v65,v66" }, 2488 { Hexagon::BI__builtin_HEXAGON_V6_vpackoh, "v60,v62,v65,v66" }, 2489 { Hexagon::BI__builtin_HEXAGON_V6_vpackoh_128B, "v60,v62,v65,v66" }, 2490 { Hexagon::BI__builtin_HEXAGON_V6_vpackwh_sat, "v60,v62,v65,v66" }, 2491 { Hexagon::BI__builtin_HEXAGON_V6_vpackwh_sat_128B, "v60,v62,v65,v66" }, 2492 { Hexagon::BI__builtin_HEXAGON_V6_vpackwuh_sat, "v60,v62,v65,v66" }, 2493 { Hexagon::BI__builtin_HEXAGON_V6_vpackwuh_sat_128B, "v60,v62,v65,v66" }, 2494 { Hexagon::BI__builtin_HEXAGON_V6_vpopcounth, "v60,v62,v65,v66" }, 2495 { Hexagon::BI__builtin_HEXAGON_V6_vpopcounth_128B, "v60,v62,v65,v66" }, 2496 { Hexagon::BI__builtin_HEXAGON_V6_vprefixqb, "v65,v66" }, 2497 { Hexagon::BI__builtin_HEXAGON_V6_vprefixqb_128B, "v65,v66" }, 2498 { Hexagon::BI__builtin_HEXAGON_V6_vprefixqh, "v65,v66" }, 2499 { Hexagon::BI__builtin_HEXAGON_V6_vprefixqh_128B, "v65,v66" }, 2500 { Hexagon::BI__builtin_HEXAGON_V6_vprefixqw, "v65,v66" }, 2501 { Hexagon::BI__builtin_HEXAGON_V6_vprefixqw_128B, "v65,v66" }, 2502 { Hexagon::BI__builtin_HEXAGON_V6_vrdelta, "v60,v62,v65,v66" }, 2503 { Hexagon::BI__builtin_HEXAGON_V6_vrdelta_128B, "v60,v62,v65,v66" }, 2504 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybub_rtt, "v65" }, 2505 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybub_rtt_128B, "v65" }, 2506 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybub_rtt_acc, "v65" }, 2507 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybub_rtt_acc_128B, "v65" }, 2508 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybus, "v60,v62,v65,v66" }, 2509 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybus_128B, "v60,v62,v65,v66" }, 2510 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybus_acc, "v60,v62,v65,v66" }, 2511 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybus_acc_128B, "v60,v62,v65,v66" }, 2512 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi, "v60,v62,v65,v66" }, 2513 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_128B, "v60,v62,v65,v66" }, 2514 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc, "v60,v62,v65,v66" }, 2515 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc_128B, "v60,v62,v65,v66" }, 2516 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusv, "v60,v62,v65,v66" }, 2517 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusv_128B, "v60,v62,v65,v66" }, 2518 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusv_acc, "v60,v62,v65,v66" }, 2519 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusv_acc_128B, "v60,v62,v65,v66" }, 2520 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybv, "v60,v62,v65,v66" }, 2521 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybv_128B, "v60,v62,v65,v66" }, 2522 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybv_acc, "v60,v62,v65,v66" }, 2523 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybv_acc_128B, "v60,v62,v65,v66" }, 2524 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub, "v60,v62,v65,v66" }, 2525 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_128B, "v60,v62,v65,v66" }, 2526 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_acc, "v60,v62,v65,v66" }, 2527 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_acc_128B, "v60,v62,v65,v66" }, 2528 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi, "v60,v62,v65,v66" }, 2529 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_128B, "v60,v62,v65,v66" }, 2530 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc, "v60,v62,v65,v66" }, 2531 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc_128B, "v60,v62,v65,v66" }, 2532 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_rtt, "v65" }, 2533 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_rtt_128B, "v65" }, 2534 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_rtt_acc, "v65" }, 2535 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyub_rtt_acc_128B, "v65" }, 2536 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubv, "v60,v62,v65,v66" }, 2537 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubv_128B, "v60,v62,v65,v66" }, 2538 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubv_acc, "v60,v62,v65,v66" }, 2539 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubv_acc_128B, "v60,v62,v65,v66" }, 2540 { Hexagon::BI__builtin_HEXAGON_V6_vror, "v60,v62,v65,v66" }, 2541 { Hexagon::BI__builtin_HEXAGON_V6_vror_128B, "v60,v62,v65,v66" }, 2542 { Hexagon::BI__builtin_HEXAGON_V6_vrotr, "v66" }, 2543 { Hexagon::BI__builtin_HEXAGON_V6_vrotr_128B, "v66" }, 2544 { Hexagon::BI__builtin_HEXAGON_V6_vroundhb, "v60,v62,v65,v66" }, 2545 { Hexagon::BI__builtin_HEXAGON_V6_vroundhb_128B, "v60,v62,v65,v66" }, 2546 { Hexagon::BI__builtin_HEXAGON_V6_vroundhub, "v60,v62,v65,v66" }, 2547 { Hexagon::BI__builtin_HEXAGON_V6_vroundhub_128B, "v60,v62,v65,v66" }, 2548 { Hexagon::BI__builtin_HEXAGON_V6_vrounduhub, "v62,v65,v66" }, 2549 { Hexagon::BI__builtin_HEXAGON_V6_vrounduhub_128B, "v62,v65,v66" }, 2550 { Hexagon::BI__builtin_HEXAGON_V6_vrounduwuh, "v62,v65,v66" }, 2551 { Hexagon::BI__builtin_HEXAGON_V6_vrounduwuh_128B, "v62,v65,v66" }, 2552 { Hexagon::BI__builtin_HEXAGON_V6_vroundwh, "v60,v62,v65,v66" }, 2553 { Hexagon::BI__builtin_HEXAGON_V6_vroundwh_128B, "v60,v62,v65,v66" }, 2554 { Hexagon::BI__builtin_HEXAGON_V6_vroundwuh, "v60,v62,v65,v66" }, 2555 { Hexagon::BI__builtin_HEXAGON_V6_vroundwuh_128B, "v60,v62,v65,v66" }, 2556 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi, "v60,v62,v65,v66" }, 2557 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_128B, "v60,v62,v65,v66" }, 2558 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc, "v60,v62,v65,v66" }, 2559 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc_128B, "v60,v62,v65,v66" }, 2560 { Hexagon::BI__builtin_HEXAGON_V6_vsatdw, "v66" }, 2561 { Hexagon::BI__builtin_HEXAGON_V6_vsatdw_128B, "v66" }, 2562 { Hexagon::BI__builtin_HEXAGON_V6_vsathub, "v60,v62,v65,v66" }, 2563 { Hexagon::BI__builtin_HEXAGON_V6_vsathub_128B, "v60,v62,v65,v66" }, 2564 { Hexagon::BI__builtin_HEXAGON_V6_vsatuwuh, "v62,v65,v66" }, 2565 { Hexagon::BI__builtin_HEXAGON_V6_vsatuwuh_128B, "v62,v65,v66" }, 2566 { Hexagon::BI__builtin_HEXAGON_V6_vsatwh, "v60,v62,v65,v66" }, 2567 { Hexagon::BI__builtin_HEXAGON_V6_vsatwh_128B, "v60,v62,v65,v66" }, 2568 { Hexagon::BI__builtin_HEXAGON_V6_vsb, "v60,v62,v65,v66" }, 2569 { Hexagon::BI__builtin_HEXAGON_V6_vsb_128B, "v60,v62,v65,v66" }, 2570 { Hexagon::BI__builtin_HEXAGON_V6_vsh, "v60,v62,v65,v66" }, 2571 { Hexagon::BI__builtin_HEXAGON_V6_vsh_128B, "v60,v62,v65,v66" }, 2572 { Hexagon::BI__builtin_HEXAGON_V6_vshufeh, "v60,v62,v65,v66" }, 2573 { Hexagon::BI__builtin_HEXAGON_V6_vshufeh_128B, "v60,v62,v65,v66" }, 2574 { Hexagon::BI__builtin_HEXAGON_V6_vshuffb, "v60,v62,v65,v66" }, 2575 { Hexagon::BI__builtin_HEXAGON_V6_vshuffb_128B, "v60,v62,v65,v66" }, 2576 { Hexagon::BI__builtin_HEXAGON_V6_vshuffeb, "v60,v62,v65,v66" }, 2577 { Hexagon::BI__builtin_HEXAGON_V6_vshuffeb_128B, "v60,v62,v65,v66" }, 2578 { Hexagon::BI__builtin_HEXAGON_V6_vshuffh, "v60,v62,v65,v66" }, 2579 { Hexagon::BI__builtin_HEXAGON_V6_vshuffh_128B, "v60,v62,v65,v66" }, 2580 { Hexagon::BI__builtin_HEXAGON_V6_vshuffob, "v60,v62,v65,v66" }, 2581 { Hexagon::BI__builtin_HEXAGON_V6_vshuffob_128B, "v60,v62,v65,v66" }, 2582 { Hexagon::BI__builtin_HEXAGON_V6_vshuffvdd, "v60,v62,v65,v66" }, 2583 { Hexagon::BI__builtin_HEXAGON_V6_vshuffvdd_128B, "v60,v62,v65,v66" }, 2584 { Hexagon::BI__builtin_HEXAGON_V6_vshufoeb, "v60,v62,v65,v66" }, 2585 { Hexagon::BI__builtin_HEXAGON_V6_vshufoeb_128B, "v60,v62,v65,v66" }, 2586 { Hexagon::BI__builtin_HEXAGON_V6_vshufoeh, "v60,v62,v65,v66" }, 2587 { Hexagon::BI__builtin_HEXAGON_V6_vshufoeh_128B, "v60,v62,v65,v66" }, 2588 { Hexagon::BI__builtin_HEXAGON_V6_vshufoh, "v60,v62,v65,v66" }, 2589 { Hexagon::BI__builtin_HEXAGON_V6_vshufoh_128B, "v60,v62,v65,v66" }, 2590 { Hexagon::BI__builtin_HEXAGON_V6_vsubb, "v60,v62,v65,v66" }, 2591 { Hexagon::BI__builtin_HEXAGON_V6_vsubb_128B, "v60,v62,v65,v66" }, 2592 { Hexagon::BI__builtin_HEXAGON_V6_vsubb_dv, "v60,v62,v65,v66" }, 2593 { Hexagon::BI__builtin_HEXAGON_V6_vsubb_dv_128B, "v60,v62,v65,v66" }, 2594 { Hexagon::BI__builtin_HEXAGON_V6_vsubbsat, "v62,v65,v66" }, 2595 { Hexagon::BI__builtin_HEXAGON_V6_vsubbsat_128B, "v62,v65,v66" }, 2596 { Hexagon::BI__builtin_HEXAGON_V6_vsubbsat_dv, "v62,v65,v66" }, 2597 { Hexagon::BI__builtin_HEXAGON_V6_vsubbsat_dv_128B, "v62,v65,v66" }, 2598 { Hexagon::BI__builtin_HEXAGON_V6_vsubcarry, "v62,v65,v66" }, 2599 { Hexagon::BI__builtin_HEXAGON_V6_vsubcarry_128B, "v62,v65,v66" }, 2600 { Hexagon::BI__builtin_HEXAGON_V6_vsubh, "v60,v62,v65,v66" }, 2601 { Hexagon::BI__builtin_HEXAGON_V6_vsubh_128B, "v60,v62,v65,v66" }, 2602 { Hexagon::BI__builtin_HEXAGON_V6_vsubh_dv, "v60,v62,v65,v66" }, 2603 { Hexagon::BI__builtin_HEXAGON_V6_vsubh_dv_128B, "v60,v62,v65,v66" }, 2604 { Hexagon::BI__builtin_HEXAGON_V6_vsubhsat, "v60,v62,v65,v66" }, 2605 { Hexagon::BI__builtin_HEXAGON_V6_vsubhsat_128B, "v60,v62,v65,v66" }, 2606 { Hexagon::BI__builtin_HEXAGON_V6_vsubhsat_dv, "v60,v62,v65,v66" }, 2607 { Hexagon::BI__builtin_HEXAGON_V6_vsubhsat_dv_128B, "v60,v62,v65,v66" }, 2608 { Hexagon::BI__builtin_HEXAGON_V6_vsubhw, "v60,v62,v65,v66" }, 2609 { Hexagon::BI__builtin_HEXAGON_V6_vsubhw_128B, "v60,v62,v65,v66" }, 2610 { Hexagon::BI__builtin_HEXAGON_V6_vsububh, "v60,v62,v65,v66" }, 2611 { Hexagon::BI__builtin_HEXAGON_V6_vsububh_128B, "v60,v62,v65,v66" }, 2612 { Hexagon::BI__builtin_HEXAGON_V6_vsububsat, "v60,v62,v65,v66" }, 2613 { Hexagon::BI__builtin_HEXAGON_V6_vsububsat_128B, "v60,v62,v65,v66" }, 2614 { Hexagon::BI__builtin_HEXAGON_V6_vsububsat_dv, "v60,v62,v65,v66" }, 2615 { Hexagon::BI__builtin_HEXAGON_V6_vsububsat_dv_128B, "v60,v62,v65,v66" }, 2616 { Hexagon::BI__builtin_HEXAGON_V6_vsubububb_sat, "v62,v65,v66" }, 2617 { Hexagon::BI__builtin_HEXAGON_V6_vsubububb_sat_128B, "v62,v65,v66" }, 2618 { Hexagon::BI__builtin_HEXAGON_V6_vsubuhsat, "v60,v62,v65,v66" }, 2619 { Hexagon::BI__builtin_HEXAGON_V6_vsubuhsat_128B, "v60,v62,v65,v66" }, 2620 { Hexagon::BI__builtin_HEXAGON_V6_vsubuhsat_dv, "v60,v62,v65,v66" }, 2621 { Hexagon::BI__builtin_HEXAGON_V6_vsubuhsat_dv_128B, "v60,v62,v65,v66" }, 2622 { Hexagon::BI__builtin_HEXAGON_V6_vsubuhw, "v60,v62,v65,v66" }, 2623 { Hexagon::BI__builtin_HEXAGON_V6_vsubuhw_128B, "v60,v62,v65,v66" }, 2624 { Hexagon::BI__builtin_HEXAGON_V6_vsubuwsat, "v62,v65,v66" }, 2625 { Hexagon::BI__builtin_HEXAGON_V6_vsubuwsat_128B, "v62,v65,v66" }, 2626 { Hexagon::BI__builtin_HEXAGON_V6_vsubuwsat_dv, "v62,v65,v66" }, 2627 { Hexagon::BI__builtin_HEXAGON_V6_vsubuwsat_dv_128B, "v62,v65,v66" }, 2628 { Hexagon::BI__builtin_HEXAGON_V6_vsubw, "v60,v62,v65,v66" }, 2629 { Hexagon::BI__builtin_HEXAGON_V6_vsubw_128B, "v60,v62,v65,v66" }, 2630 { Hexagon::BI__builtin_HEXAGON_V6_vsubw_dv, "v60,v62,v65,v66" }, 2631 { Hexagon::BI__builtin_HEXAGON_V6_vsubw_dv_128B, "v60,v62,v65,v66" }, 2632 { Hexagon::BI__builtin_HEXAGON_V6_vsubwsat, "v60,v62,v65,v66" }, 2633 { Hexagon::BI__builtin_HEXAGON_V6_vsubwsat_128B, "v60,v62,v65,v66" }, 2634 { Hexagon::BI__builtin_HEXAGON_V6_vsubwsat_dv, "v60,v62,v65,v66" }, 2635 { Hexagon::BI__builtin_HEXAGON_V6_vsubwsat_dv_128B, "v60,v62,v65,v66" }, 2636 { Hexagon::BI__builtin_HEXAGON_V6_vswap, "v60,v62,v65,v66" }, 2637 { Hexagon::BI__builtin_HEXAGON_V6_vswap_128B, "v60,v62,v65,v66" }, 2638 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyb, "v60,v62,v65,v66" }, 2639 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyb_128B, "v60,v62,v65,v66" }, 2640 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyb_acc, "v60,v62,v65,v66" }, 2641 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyb_acc_128B, "v60,v62,v65,v66" }, 2642 { Hexagon::BI__builtin_HEXAGON_V6_vtmpybus, "v60,v62,v65,v66" }, 2643 { Hexagon::BI__builtin_HEXAGON_V6_vtmpybus_128B, "v60,v62,v65,v66" }, 2644 { Hexagon::BI__builtin_HEXAGON_V6_vtmpybus_acc, "v60,v62,v65,v66" }, 2645 { Hexagon::BI__builtin_HEXAGON_V6_vtmpybus_acc_128B, "v60,v62,v65,v66" }, 2646 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyhb, "v60,v62,v65,v66" }, 2647 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyhb_128B, "v60,v62,v65,v66" }, 2648 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyhb_acc, "v60,v62,v65,v66" }, 2649 { Hexagon::BI__builtin_HEXAGON_V6_vtmpyhb_acc_128B, "v60,v62,v65,v66" }, 2650 { Hexagon::BI__builtin_HEXAGON_V6_vunpackb, "v60,v62,v65,v66" }, 2651 { Hexagon::BI__builtin_HEXAGON_V6_vunpackb_128B, "v60,v62,v65,v66" }, 2652 { Hexagon::BI__builtin_HEXAGON_V6_vunpackh, "v60,v62,v65,v66" }, 2653 { Hexagon::BI__builtin_HEXAGON_V6_vunpackh_128B, "v60,v62,v65,v66" }, 2654 { Hexagon::BI__builtin_HEXAGON_V6_vunpackob, "v60,v62,v65,v66" }, 2655 { Hexagon::BI__builtin_HEXAGON_V6_vunpackob_128B, "v60,v62,v65,v66" }, 2656 { Hexagon::BI__builtin_HEXAGON_V6_vunpackoh, "v60,v62,v65,v66" }, 2657 { Hexagon::BI__builtin_HEXAGON_V6_vunpackoh_128B, "v60,v62,v65,v66" }, 2658 { Hexagon::BI__builtin_HEXAGON_V6_vunpackub, "v60,v62,v65,v66" }, 2659 { Hexagon::BI__builtin_HEXAGON_V6_vunpackub_128B, "v60,v62,v65,v66" }, 2660 { Hexagon::BI__builtin_HEXAGON_V6_vunpackuh, "v60,v62,v65,v66" }, 2661 { Hexagon::BI__builtin_HEXAGON_V6_vunpackuh_128B, "v60,v62,v65,v66" }, 2662 { Hexagon::BI__builtin_HEXAGON_V6_vxor, "v60,v62,v65,v66" }, 2663 { Hexagon::BI__builtin_HEXAGON_V6_vxor_128B, "v60,v62,v65,v66" }, 2664 { Hexagon::BI__builtin_HEXAGON_V6_vzb, "v60,v62,v65,v66" }, 2665 { Hexagon::BI__builtin_HEXAGON_V6_vzb_128B, "v60,v62,v65,v66" }, 2666 { Hexagon::BI__builtin_HEXAGON_V6_vzh, "v60,v62,v65,v66" }, 2667 { Hexagon::BI__builtin_HEXAGON_V6_vzh_128B, "v60,v62,v65,v66" }, 2668 }; 2669 2670 // Sort the tables on first execution so we can binary search them. 2671 auto SortCmp = [](const BuiltinAndString &LHS, const BuiltinAndString &RHS) { 2672 return LHS.BuiltinID < RHS.BuiltinID; 2673 }; 2674 static const bool SortOnce = 2675 (llvm::sort(ValidCPU, SortCmp), 2676 llvm::sort(ValidHVX, SortCmp), true); 2677 (void)SortOnce; 2678 auto LowerBoundCmp = [](const BuiltinAndString &BI, unsigned BuiltinID) { 2679 return BI.BuiltinID < BuiltinID; 2680 }; 2681 2682 const TargetInfo &TI = Context.getTargetInfo(); 2683 2684 const BuiltinAndString *FC = 2685 std::lower_bound(std::begin(ValidCPU), std::end(ValidCPU), BuiltinID, 2686 LowerBoundCmp); 2687 if (FC != std::end(ValidCPU) && FC->BuiltinID == BuiltinID) { 2688 const TargetOptions &Opts = TI.getTargetOpts(); 2689 StringRef CPU = Opts.CPU; 2690 if (!CPU.empty()) { 2691 assert(CPU.startswith("hexagon") && "Unexpected CPU name"); 2692 CPU.consume_front("hexagon"); 2693 SmallVector<StringRef, 3> CPUs; 2694 StringRef(FC->Str).split(CPUs, ','); 2695 if (llvm::none_of(CPUs, [CPU](StringRef S) { return S == CPU; })) 2696 return Diag(TheCall->getBeginLoc(), 2697 diag::err_hexagon_builtin_unsupported_cpu); 2698 } 2699 } 2700 2701 const BuiltinAndString *FH = 2702 std::lower_bound(std::begin(ValidHVX), std::end(ValidHVX), BuiltinID, 2703 LowerBoundCmp); 2704 if (FH != std::end(ValidHVX) && FH->BuiltinID == BuiltinID) { 2705 if (!TI.hasFeature("hvx")) 2706 return Diag(TheCall->getBeginLoc(), 2707 diag::err_hexagon_builtin_requires_hvx); 2708 2709 SmallVector<StringRef, 3> HVXs; 2710 StringRef(FH->Str).split(HVXs, ','); 2711 bool IsValid = llvm::any_of(HVXs, 2712 [&TI] (StringRef V) { 2713 std::string F = "hvx" + V.str(); 2714 return TI.hasFeature(F); 2715 }); 2716 if (!IsValid) 2717 return Diag(TheCall->getBeginLoc(), 2718 diag::err_hexagon_builtin_unsupported_hvx); 2719 } 2720 2721 return false; 2722 } 2723 2724 bool Sema::CheckHexagonBuiltinArgument(unsigned BuiltinID, CallExpr *TheCall) { 2725 struct ArgInfo { 2726 uint8_t OpNum; 2727 bool IsSigned; 2728 uint8_t BitWidth; 2729 uint8_t Align; 2730 }; 2731 struct BuiltinInfo { 2732 unsigned BuiltinID; 2733 ArgInfo Infos[2]; 2734 }; 2735 2736 static BuiltinInfo Infos[] = { 2737 { Hexagon::BI__builtin_circ_ldd, {{ 3, true, 4, 3 }} }, 2738 { Hexagon::BI__builtin_circ_ldw, {{ 3, true, 4, 2 }} }, 2739 { Hexagon::BI__builtin_circ_ldh, {{ 3, true, 4, 1 }} }, 2740 { Hexagon::BI__builtin_circ_lduh, {{ 3, true, 4, 0 }} }, 2741 { Hexagon::BI__builtin_circ_ldb, {{ 3, true, 4, 0 }} }, 2742 { Hexagon::BI__builtin_circ_ldub, {{ 3, true, 4, 0 }} }, 2743 { Hexagon::BI__builtin_circ_std, {{ 3, true, 4, 3 }} }, 2744 { Hexagon::BI__builtin_circ_stw, {{ 3, true, 4, 2 }} }, 2745 { Hexagon::BI__builtin_circ_sth, {{ 3, true, 4, 1 }} }, 2746 { Hexagon::BI__builtin_circ_sthhi, {{ 3, true, 4, 1 }} }, 2747 { Hexagon::BI__builtin_circ_stb, {{ 3, true, 4, 0 }} }, 2748 2749 { Hexagon::BI__builtin_HEXAGON_L2_loadrub_pci, {{ 1, true, 4, 0 }} }, 2750 { Hexagon::BI__builtin_HEXAGON_L2_loadrb_pci, {{ 1, true, 4, 0 }} }, 2751 { Hexagon::BI__builtin_HEXAGON_L2_loadruh_pci, {{ 1, true, 4, 1 }} }, 2752 { Hexagon::BI__builtin_HEXAGON_L2_loadrh_pci, {{ 1, true, 4, 1 }} }, 2753 { Hexagon::BI__builtin_HEXAGON_L2_loadri_pci, {{ 1, true, 4, 2 }} }, 2754 { Hexagon::BI__builtin_HEXAGON_L2_loadrd_pci, {{ 1, true, 4, 3 }} }, 2755 { Hexagon::BI__builtin_HEXAGON_S2_storerb_pci, {{ 1, true, 4, 0 }} }, 2756 { Hexagon::BI__builtin_HEXAGON_S2_storerh_pci, {{ 1, true, 4, 1 }} }, 2757 { Hexagon::BI__builtin_HEXAGON_S2_storerf_pci, {{ 1, true, 4, 1 }} }, 2758 { Hexagon::BI__builtin_HEXAGON_S2_storeri_pci, {{ 1, true, 4, 2 }} }, 2759 { Hexagon::BI__builtin_HEXAGON_S2_storerd_pci, {{ 1, true, 4, 3 }} }, 2760 2761 { Hexagon::BI__builtin_HEXAGON_A2_combineii, {{ 1, true, 8, 0 }} }, 2762 { Hexagon::BI__builtin_HEXAGON_A2_tfrih, {{ 1, false, 16, 0 }} }, 2763 { Hexagon::BI__builtin_HEXAGON_A2_tfril, {{ 1, false, 16, 0 }} }, 2764 { Hexagon::BI__builtin_HEXAGON_A2_tfrpi, {{ 0, true, 8, 0 }} }, 2765 { Hexagon::BI__builtin_HEXAGON_A4_bitspliti, {{ 1, false, 5, 0 }} }, 2766 { Hexagon::BI__builtin_HEXAGON_A4_cmpbeqi, {{ 1, false, 8, 0 }} }, 2767 { Hexagon::BI__builtin_HEXAGON_A4_cmpbgti, {{ 1, true, 8, 0 }} }, 2768 { Hexagon::BI__builtin_HEXAGON_A4_cround_ri, {{ 1, false, 5, 0 }} }, 2769 { Hexagon::BI__builtin_HEXAGON_A4_round_ri, {{ 1, false, 5, 0 }} }, 2770 { Hexagon::BI__builtin_HEXAGON_A4_round_ri_sat, {{ 1, false, 5, 0 }} }, 2771 { Hexagon::BI__builtin_HEXAGON_A4_vcmpbeqi, {{ 1, false, 8, 0 }} }, 2772 { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgti, {{ 1, true, 8, 0 }} }, 2773 { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgtui, {{ 1, false, 7, 0 }} }, 2774 { Hexagon::BI__builtin_HEXAGON_A4_vcmpheqi, {{ 1, true, 8, 0 }} }, 2775 { Hexagon::BI__builtin_HEXAGON_A4_vcmphgti, {{ 1, true, 8, 0 }} }, 2776 { Hexagon::BI__builtin_HEXAGON_A4_vcmphgtui, {{ 1, false, 7, 0 }} }, 2777 { Hexagon::BI__builtin_HEXAGON_A4_vcmpweqi, {{ 1, true, 8, 0 }} }, 2778 { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgti, {{ 1, true, 8, 0 }} }, 2779 { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgtui, {{ 1, false, 7, 0 }} }, 2780 { Hexagon::BI__builtin_HEXAGON_C2_bitsclri, {{ 1, false, 6, 0 }} }, 2781 { Hexagon::BI__builtin_HEXAGON_C2_muxii, {{ 2, true, 8, 0 }} }, 2782 { Hexagon::BI__builtin_HEXAGON_C4_nbitsclri, {{ 1, false, 6, 0 }} }, 2783 { Hexagon::BI__builtin_HEXAGON_F2_dfclass, {{ 1, false, 5, 0 }} }, 2784 { Hexagon::BI__builtin_HEXAGON_F2_dfimm_n, {{ 0, false, 10, 0 }} }, 2785 { Hexagon::BI__builtin_HEXAGON_F2_dfimm_p, {{ 0, false, 10, 0 }} }, 2786 { Hexagon::BI__builtin_HEXAGON_F2_sfclass, {{ 1, false, 5, 0 }} }, 2787 { Hexagon::BI__builtin_HEXAGON_F2_sfimm_n, {{ 0, false, 10, 0 }} }, 2788 { Hexagon::BI__builtin_HEXAGON_F2_sfimm_p, {{ 0, false, 10, 0 }} }, 2789 { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addi, {{ 2, false, 6, 0 }} }, 2790 { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addr_u2, {{ 1, false, 6, 2 }} }, 2791 { Hexagon::BI__builtin_HEXAGON_S2_addasl_rrri, {{ 2, false, 3, 0 }} }, 2792 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_acc, {{ 2, false, 6, 0 }} }, 2793 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_and, {{ 2, false, 6, 0 }} }, 2794 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p, {{ 1, false, 6, 0 }} }, 2795 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_nac, {{ 2, false, 6, 0 }} }, 2796 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_or, {{ 2, false, 6, 0 }} }, 2797 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_xacc, {{ 2, false, 6, 0 }} }, 2798 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_acc, {{ 2, false, 5, 0 }} }, 2799 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_and, {{ 2, false, 5, 0 }} }, 2800 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r, {{ 1, false, 5, 0 }} }, 2801 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_nac, {{ 2, false, 5, 0 }} }, 2802 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_or, {{ 2, false, 5, 0 }} }, 2803 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_sat, {{ 1, false, 5, 0 }} }, 2804 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_xacc, {{ 2, false, 5, 0 }} }, 2805 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vh, {{ 1, false, 4, 0 }} }, 2806 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vw, {{ 1, false, 5, 0 }} }, 2807 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_acc, {{ 2, false, 6, 0 }} }, 2808 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_and, {{ 2, false, 6, 0 }} }, 2809 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p, {{ 1, false, 6, 0 }} }, 2810 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_nac, {{ 2, false, 6, 0 }} }, 2811 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_or, {{ 2, false, 6, 0 }} }, 2812 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd_goodsyntax, 2813 {{ 1, false, 6, 0 }} }, 2814 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd, {{ 1, false, 6, 0 }} }, 2815 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_acc, {{ 2, false, 5, 0 }} }, 2816 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_and, {{ 2, false, 5, 0 }} }, 2817 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r, {{ 1, false, 5, 0 }} }, 2818 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_nac, {{ 2, false, 5, 0 }} }, 2819 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_or, {{ 2, false, 5, 0 }} }, 2820 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd_goodsyntax, 2821 {{ 1, false, 5, 0 }} }, 2822 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd, {{ 1, false, 5, 0 }} }, 2823 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_svw_trun, {{ 1, false, 5, 0 }} }, 2824 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vh, {{ 1, false, 4, 0 }} }, 2825 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vw, {{ 1, false, 5, 0 }} }, 2826 { Hexagon::BI__builtin_HEXAGON_S2_clrbit_i, {{ 1, false, 5, 0 }} }, 2827 { Hexagon::BI__builtin_HEXAGON_S2_extractu, {{ 1, false, 5, 0 }, 2828 { 2, false, 5, 0 }} }, 2829 { Hexagon::BI__builtin_HEXAGON_S2_extractup, {{ 1, false, 6, 0 }, 2830 { 2, false, 6, 0 }} }, 2831 { Hexagon::BI__builtin_HEXAGON_S2_insert, {{ 2, false, 5, 0 }, 2832 { 3, false, 5, 0 }} }, 2833 { Hexagon::BI__builtin_HEXAGON_S2_insertp, {{ 2, false, 6, 0 }, 2834 { 3, false, 6, 0 }} }, 2835 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_acc, {{ 2, false, 6, 0 }} }, 2836 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_and, {{ 2, false, 6, 0 }} }, 2837 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p, {{ 1, false, 6, 0 }} }, 2838 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_nac, {{ 2, false, 6, 0 }} }, 2839 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_or, {{ 2, false, 6, 0 }} }, 2840 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_xacc, {{ 2, false, 6, 0 }} }, 2841 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_acc, {{ 2, false, 5, 0 }} }, 2842 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_and, {{ 2, false, 5, 0 }} }, 2843 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r, {{ 1, false, 5, 0 }} }, 2844 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_nac, {{ 2, false, 5, 0 }} }, 2845 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_or, {{ 2, false, 5, 0 }} }, 2846 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_xacc, {{ 2, false, 5, 0 }} }, 2847 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vh, {{ 1, false, 4, 0 }} }, 2848 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vw, {{ 1, false, 5, 0 }} }, 2849 { Hexagon::BI__builtin_HEXAGON_S2_setbit_i, {{ 1, false, 5, 0 }} }, 2850 { Hexagon::BI__builtin_HEXAGON_S2_tableidxb_goodsyntax, 2851 {{ 2, false, 4, 0 }, 2852 { 3, false, 5, 0 }} }, 2853 { Hexagon::BI__builtin_HEXAGON_S2_tableidxd_goodsyntax, 2854 {{ 2, false, 4, 0 }, 2855 { 3, false, 5, 0 }} }, 2856 { Hexagon::BI__builtin_HEXAGON_S2_tableidxh_goodsyntax, 2857 {{ 2, false, 4, 0 }, 2858 { 3, false, 5, 0 }} }, 2859 { Hexagon::BI__builtin_HEXAGON_S2_tableidxw_goodsyntax, 2860 {{ 2, false, 4, 0 }, 2861 { 3, false, 5, 0 }} }, 2862 { Hexagon::BI__builtin_HEXAGON_S2_togglebit_i, {{ 1, false, 5, 0 }} }, 2863 { Hexagon::BI__builtin_HEXAGON_S2_tstbit_i, {{ 1, false, 5, 0 }} }, 2864 { Hexagon::BI__builtin_HEXAGON_S2_valignib, {{ 2, false, 3, 0 }} }, 2865 { Hexagon::BI__builtin_HEXAGON_S2_vspliceib, {{ 2, false, 3, 0 }} }, 2866 { Hexagon::BI__builtin_HEXAGON_S4_addi_asl_ri, {{ 2, false, 5, 0 }} }, 2867 { Hexagon::BI__builtin_HEXAGON_S4_addi_lsr_ri, {{ 2, false, 5, 0 }} }, 2868 { Hexagon::BI__builtin_HEXAGON_S4_andi_asl_ri, {{ 2, false, 5, 0 }} }, 2869 { Hexagon::BI__builtin_HEXAGON_S4_andi_lsr_ri, {{ 2, false, 5, 0 }} }, 2870 { Hexagon::BI__builtin_HEXAGON_S4_clbaddi, {{ 1, true , 6, 0 }} }, 2871 { Hexagon::BI__builtin_HEXAGON_S4_clbpaddi, {{ 1, true, 6, 0 }} }, 2872 { Hexagon::BI__builtin_HEXAGON_S4_extract, {{ 1, false, 5, 0 }, 2873 { 2, false, 5, 0 }} }, 2874 { Hexagon::BI__builtin_HEXAGON_S4_extractp, {{ 1, false, 6, 0 }, 2875 { 2, false, 6, 0 }} }, 2876 { Hexagon::BI__builtin_HEXAGON_S4_lsli, {{ 0, true, 6, 0 }} }, 2877 { Hexagon::BI__builtin_HEXAGON_S4_ntstbit_i, {{ 1, false, 5, 0 }} }, 2878 { Hexagon::BI__builtin_HEXAGON_S4_ori_asl_ri, {{ 2, false, 5, 0 }} }, 2879 { Hexagon::BI__builtin_HEXAGON_S4_ori_lsr_ri, {{ 2, false, 5, 0 }} }, 2880 { Hexagon::BI__builtin_HEXAGON_S4_subi_asl_ri, {{ 2, false, 5, 0 }} }, 2881 { Hexagon::BI__builtin_HEXAGON_S4_subi_lsr_ri, {{ 2, false, 5, 0 }} }, 2882 { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate_acc, {{ 3, false, 2, 0 }} }, 2883 { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate, {{ 2, false, 2, 0 }} }, 2884 { Hexagon::BI__builtin_HEXAGON_S5_asrhub_rnd_sat_goodsyntax, 2885 {{ 1, false, 4, 0 }} }, 2886 { Hexagon::BI__builtin_HEXAGON_S5_asrhub_sat, {{ 1, false, 4, 0 }} }, 2887 { Hexagon::BI__builtin_HEXAGON_S5_vasrhrnd_goodsyntax, 2888 {{ 1, false, 4, 0 }} }, 2889 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p, {{ 1, false, 6, 0 }} }, 2890 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_acc, {{ 2, false, 6, 0 }} }, 2891 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_and, {{ 2, false, 6, 0 }} }, 2892 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_nac, {{ 2, false, 6, 0 }} }, 2893 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_or, {{ 2, false, 6, 0 }} }, 2894 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_xacc, {{ 2, false, 6, 0 }} }, 2895 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r, {{ 1, false, 5, 0 }} }, 2896 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_acc, {{ 2, false, 5, 0 }} }, 2897 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_and, {{ 2, false, 5, 0 }} }, 2898 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_nac, {{ 2, false, 5, 0 }} }, 2899 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_or, {{ 2, false, 5, 0 }} }, 2900 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_xacc, {{ 2, false, 5, 0 }} }, 2901 { Hexagon::BI__builtin_HEXAGON_V6_valignbi, {{ 2, false, 3, 0 }} }, 2902 { Hexagon::BI__builtin_HEXAGON_V6_valignbi_128B, {{ 2, false, 3, 0 }} }, 2903 { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi, {{ 2, false, 3, 0 }} }, 2904 { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi_128B, {{ 2, false, 3, 0 }} }, 2905 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi, {{ 2, false, 1, 0 }} }, 2906 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_128B, {{ 2, false, 1, 0 }} }, 2907 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc, {{ 3, false, 1, 0 }} }, 2908 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc_128B, 2909 {{ 3, false, 1, 0 }} }, 2910 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi, {{ 2, false, 1, 0 }} }, 2911 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_128B, {{ 2, false, 1, 0 }} }, 2912 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc, {{ 3, false, 1, 0 }} }, 2913 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc_128B, 2914 {{ 3, false, 1, 0 }} }, 2915 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi, {{ 2, false, 1, 0 }} }, 2916 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_128B, {{ 2, false, 1, 0 }} }, 2917 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc, {{ 3, false, 1, 0 }} }, 2918 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc_128B, 2919 {{ 3, false, 1, 0 }} }, 2920 }; 2921 2922 // Use a dynamically initialized static to sort the table exactly once on 2923 // first run. 2924 static const bool SortOnce = 2925 (llvm::sort(Infos, 2926 [](const BuiltinInfo &LHS, const BuiltinInfo &RHS) { 2927 return LHS.BuiltinID < RHS.BuiltinID; 2928 }), 2929 true); 2930 (void)SortOnce; 2931 2932 const BuiltinInfo *F = 2933 std::lower_bound(std::begin(Infos), std::end(Infos), BuiltinID, 2934 [](const BuiltinInfo &BI, unsigned BuiltinID) { 2935 return BI.BuiltinID < BuiltinID; 2936 }); 2937 if (F == std::end(Infos) || F->BuiltinID != BuiltinID) 2938 return false; 2939 2940 bool Error = false; 2941 2942 for (const ArgInfo &A : F->Infos) { 2943 // Ignore empty ArgInfo elements. 2944 if (A.BitWidth == 0) 2945 continue; 2946 2947 int32_t Min = A.IsSigned ? -(1 << (A.BitWidth - 1)) : 0; 2948 int32_t Max = (1 << (A.IsSigned ? A.BitWidth - 1 : A.BitWidth)) - 1; 2949 if (!A.Align) { 2950 Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max); 2951 } else { 2952 unsigned M = 1 << A.Align; 2953 Min *= M; 2954 Max *= M; 2955 Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max) | 2956 SemaBuiltinConstantArgMultiple(TheCall, A.OpNum, M); 2957 } 2958 } 2959 return Error; 2960 } 2961 2962 bool Sema::CheckHexagonBuiltinFunctionCall(unsigned BuiltinID, 2963 CallExpr *TheCall) { 2964 return CheckHexagonBuiltinCpu(BuiltinID, TheCall) || 2965 CheckHexagonBuiltinArgument(BuiltinID, TheCall); 2966 } 2967 2968 2969 // CheckMipsBuiltinFunctionCall - Checks the constant value passed to the 2970 // intrinsic is correct. The switch statement is ordered by DSP, MSA. The 2971 // ordering for DSP is unspecified. MSA is ordered by the data format used 2972 // by the underlying instruction i.e., df/m, df/n and then by size. 2973 // 2974 // FIXME: The size tests here should instead be tablegen'd along with the 2975 // definitions from include/clang/Basic/BuiltinsMips.def. 2976 // FIXME: GCC is strict on signedness for some of these intrinsics, we should 2977 // be too. 2978 bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 2979 unsigned i = 0, l = 0, u = 0, m = 0; 2980 switch (BuiltinID) { 2981 default: return false; 2982 case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; 2983 case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; 2984 case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; 2985 case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; 2986 case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; 2987 case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; 2988 case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break; 2989 // MSA intrinsics. Instructions (which the intrinsics maps to) which use the 2990 // df/m field. 2991 // These intrinsics take an unsigned 3 bit immediate. 2992 case Mips::BI__builtin_msa_bclri_b: 2993 case Mips::BI__builtin_msa_bnegi_b: 2994 case Mips::BI__builtin_msa_bseti_b: 2995 case Mips::BI__builtin_msa_sat_s_b: 2996 case Mips::BI__builtin_msa_sat_u_b: 2997 case Mips::BI__builtin_msa_slli_b: 2998 case Mips::BI__builtin_msa_srai_b: 2999 case Mips::BI__builtin_msa_srari_b: 3000 case Mips::BI__builtin_msa_srli_b: 3001 case Mips::BI__builtin_msa_srlri_b: i = 1; l = 0; u = 7; break; 3002 case Mips::BI__builtin_msa_binsli_b: 3003 case Mips::BI__builtin_msa_binsri_b: i = 2; l = 0; u = 7; break; 3004 // These intrinsics take an unsigned 4 bit immediate. 3005 case Mips::BI__builtin_msa_bclri_h: 3006 case Mips::BI__builtin_msa_bnegi_h: 3007 case Mips::BI__builtin_msa_bseti_h: 3008 case Mips::BI__builtin_msa_sat_s_h: 3009 case Mips::BI__builtin_msa_sat_u_h: 3010 case Mips::BI__builtin_msa_slli_h: 3011 case Mips::BI__builtin_msa_srai_h: 3012 case Mips::BI__builtin_msa_srari_h: 3013 case Mips::BI__builtin_msa_srli_h: 3014 case Mips::BI__builtin_msa_srlri_h: i = 1; l = 0; u = 15; break; 3015 case Mips::BI__builtin_msa_binsli_h: 3016 case Mips::BI__builtin_msa_binsri_h: i = 2; l = 0; u = 15; break; 3017 // These intrinsics take an unsigned 5 bit immediate. 3018 // The first block of intrinsics actually have an unsigned 5 bit field, 3019 // not a df/n field. 3020 case Mips::BI__builtin_msa_clei_u_b: 3021 case Mips::BI__builtin_msa_clei_u_h: 3022 case Mips::BI__builtin_msa_clei_u_w: 3023 case Mips::BI__builtin_msa_clei_u_d: 3024 case Mips::BI__builtin_msa_clti_u_b: 3025 case Mips::BI__builtin_msa_clti_u_h: 3026 case Mips::BI__builtin_msa_clti_u_w: 3027 case Mips::BI__builtin_msa_clti_u_d: 3028 case Mips::BI__builtin_msa_maxi_u_b: 3029 case Mips::BI__builtin_msa_maxi_u_h: 3030 case Mips::BI__builtin_msa_maxi_u_w: 3031 case Mips::BI__builtin_msa_maxi_u_d: 3032 case Mips::BI__builtin_msa_mini_u_b: 3033 case Mips::BI__builtin_msa_mini_u_h: 3034 case Mips::BI__builtin_msa_mini_u_w: 3035 case Mips::BI__builtin_msa_mini_u_d: 3036 case Mips::BI__builtin_msa_addvi_b: 3037 case Mips::BI__builtin_msa_addvi_h: 3038 case Mips::BI__builtin_msa_addvi_w: 3039 case Mips::BI__builtin_msa_addvi_d: 3040 case Mips::BI__builtin_msa_bclri_w: 3041 case Mips::BI__builtin_msa_bnegi_w: 3042 case Mips::BI__builtin_msa_bseti_w: 3043 case Mips::BI__builtin_msa_sat_s_w: 3044 case Mips::BI__builtin_msa_sat_u_w: 3045 case Mips::BI__builtin_msa_slli_w: 3046 case Mips::BI__builtin_msa_srai_w: 3047 case Mips::BI__builtin_msa_srari_w: 3048 case Mips::BI__builtin_msa_srli_w: 3049 case Mips::BI__builtin_msa_srlri_w: 3050 case Mips::BI__builtin_msa_subvi_b: 3051 case Mips::BI__builtin_msa_subvi_h: 3052 case Mips::BI__builtin_msa_subvi_w: 3053 case Mips::BI__builtin_msa_subvi_d: i = 1; l = 0; u = 31; break; 3054 case Mips::BI__builtin_msa_binsli_w: 3055 case Mips::BI__builtin_msa_binsri_w: i = 2; l = 0; u = 31; break; 3056 // These intrinsics take an unsigned 6 bit immediate. 3057 case Mips::BI__builtin_msa_bclri_d: 3058 case Mips::BI__builtin_msa_bnegi_d: 3059 case Mips::BI__builtin_msa_bseti_d: 3060 case Mips::BI__builtin_msa_sat_s_d: 3061 case Mips::BI__builtin_msa_sat_u_d: 3062 case Mips::BI__builtin_msa_slli_d: 3063 case Mips::BI__builtin_msa_srai_d: 3064 case Mips::BI__builtin_msa_srari_d: 3065 case Mips::BI__builtin_msa_srli_d: 3066 case Mips::BI__builtin_msa_srlri_d: i = 1; l = 0; u = 63; break; 3067 case Mips::BI__builtin_msa_binsli_d: 3068 case Mips::BI__builtin_msa_binsri_d: i = 2; l = 0; u = 63; break; 3069 // These intrinsics take a signed 5 bit immediate. 3070 case Mips::BI__builtin_msa_ceqi_b: 3071 case Mips::BI__builtin_msa_ceqi_h: 3072 case Mips::BI__builtin_msa_ceqi_w: 3073 case Mips::BI__builtin_msa_ceqi_d: 3074 case Mips::BI__builtin_msa_clti_s_b: 3075 case Mips::BI__builtin_msa_clti_s_h: 3076 case Mips::BI__builtin_msa_clti_s_w: 3077 case Mips::BI__builtin_msa_clti_s_d: 3078 case Mips::BI__builtin_msa_clei_s_b: 3079 case Mips::BI__builtin_msa_clei_s_h: 3080 case Mips::BI__builtin_msa_clei_s_w: 3081 case Mips::BI__builtin_msa_clei_s_d: 3082 case Mips::BI__builtin_msa_maxi_s_b: 3083 case Mips::BI__builtin_msa_maxi_s_h: 3084 case Mips::BI__builtin_msa_maxi_s_w: 3085 case Mips::BI__builtin_msa_maxi_s_d: 3086 case Mips::BI__builtin_msa_mini_s_b: 3087 case Mips::BI__builtin_msa_mini_s_h: 3088 case Mips::BI__builtin_msa_mini_s_w: 3089 case Mips::BI__builtin_msa_mini_s_d: i = 1; l = -16; u = 15; break; 3090 // These intrinsics take an unsigned 8 bit immediate. 3091 case Mips::BI__builtin_msa_andi_b: 3092 case Mips::BI__builtin_msa_nori_b: 3093 case Mips::BI__builtin_msa_ori_b: 3094 case Mips::BI__builtin_msa_shf_b: 3095 case Mips::BI__builtin_msa_shf_h: 3096 case Mips::BI__builtin_msa_shf_w: 3097 case Mips::BI__builtin_msa_xori_b: i = 1; l = 0; u = 255; break; 3098 case Mips::BI__builtin_msa_bseli_b: 3099 case Mips::BI__builtin_msa_bmnzi_b: 3100 case Mips::BI__builtin_msa_bmzi_b: i = 2; l = 0; u = 255; break; 3101 // df/n format 3102 // These intrinsics take an unsigned 4 bit immediate. 3103 case Mips::BI__builtin_msa_copy_s_b: 3104 case Mips::BI__builtin_msa_copy_u_b: 3105 case Mips::BI__builtin_msa_insve_b: 3106 case Mips::BI__builtin_msa_splati_b: i = 1; l = 0; u = 15; break; 3107 case Mips::BI__builtin_msa_sldi_b: i = 2; l = 0; u = 15; break; 3108 // These intrinsics take an unsigned 3 bit immediate. 3109 case Mips::BI__builtin_msa_copy_s_h: 3110 case Mips::BI__builtin_msa_copy_u_h: 3111 case Mips::BI__builtin_msa_insve_h: 3112 case Mips::BI__builtin_msa_splati_h: i = 1; l = 0; u = 7; break; 3113 case Mips::BI__builtin_msa_sldi_h: i = 2; l = 0; u = 7; break; 3114 // These intrinsics take an unsigned 2 bit immediate. 3115 case Mips::BI__builtin_msa_copy_s_w: 3116 case Mips::BI__builtin_msa_copy_u_w: 3117 case Mips::BI__builtin_msa_insve_w: 3118 case Mips::BI__builtin_msa_splati_w: i = 1; l = 0; u = 3; break; 3119 case Mips::BI__builtin_msa_sldi_w: i = 2; l = 0; u = 3; break; 3120 // These intrinsics take an unsigned 1 bit immediate. 3121 case Mips::BI__builtin_msa_copy_s_d: 3122 case Mips::BI__builtin_msa_copy_u_d: 3123 case Mips::BI__builtin_msa_insve_d: 3124 case Mips::BI__builtin_msa_splati_d: i = 1; l = 0; u = 1; break; 3125 case Mips::BI__builtin_msa_sldi_d: i = 2; l = 0; u = 1; break; 3126 // Memory offsets and immediate loads. 3127 // These intrinsics take a signed 10 bit immediate. 3128 case Mips::BI__builtin_msa_ldi_b: i = 0; l = -128; u = 255; break; 3129 case Mips::BI__builtin_msa_ldi_h: 3130 case Mips::BI__builtin_msa_ldi_w: 3131 case Mips::BI__builtin_msa_ldi_d: i = 0; l = -512; u = 511; break; 3132 case Mips::BI__builtin_msa_ld_b: i = 1; l = -512; u = 511; m = 1; break; 3133 case Mips::BI__builtin_msa_ld_h: i = 1; l = -1024; u = 1022; m = 2; break; 3134 case Mips::BI__builtin_msa_ld_w: i = 1; l = -2048; u = 2044; m = 4; break; 3135 case Mips::BI__builtin_msa_ld_d: i = 1; l = -4096; u = 4088; m = 8; break; 3136 case Mips::BI__builtin_msa_st_b: i = 2; l = -512; u = 511; m = 1; break; 3137 case Mips::BI__builtin_msa_st_h: i = 2; l = -1024; u = 1022; m = 2; break; 3138 case Mips::BI__builtin_msa_st_w: i = 2; l = -2048; u = 2044; m = 4; break; 3139 case Mips::BI__builtin_msa_st_d: i = 2; l = -4096; u = 4088; m = 8; break; 3140 } 3141 3142 if (!m) 3143 return SemaBuiltinConstantArgRange(TheCall, i, l, u); 3144 3145 return SemaBuiltinConstantArgRange(TheCall, i, l, u) || 3146 SemaBuiltinConstantArgMultiple(TheCall, i, m); 3147 } 3148 3149 bool Sema::CheckPPCBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 3150 unsigned i = 0, l = 0, u = 0; 3151 bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde || 3152 BuiltinID == PPC::BI__builtin_divdeu || 3153 BuiltinID == PPC::BI__builtin_bpermd; 3154 bool IsTarget64Bit = Context.getTargetInfo() 3155 .getTypeWidth(Context 3156 .getTargetInfo() 3157 .getIntPtrType()) == 64; 3158 bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe || 3159 BuiltinID == PPC::BI__builtin_divweu || 3160 BuiltinID == PPC::BI__builtin_divde || 3161 BuiltinID == PPC::BI__builtin_divdeu; 3162 3163 if (Is64BitBltin && !IsTarget64Bit) 3164 return Diag(TheCall->getBeginLoc(), diag::err_64_bit_builtin_32_bit_tgt) 3165 << TheCall->getSourceRange(); 3166 3167 if ((IsBltinExtDiv && !Context.getTargetInfo().hasFeature("extdiv")) || 3168 (BuiltinID == PPC::BI__builtin_bpermd && 3169 !Context.getTargetInfo().hasFeature("bpermd"))) 3170 return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_only_on_pwr7) 3171 << TheCall->getSourceRange(); 3172 3173 auto SemaVSXCheck = [&](CallExpr *TheCall) -> bool { 3174 if (!Context.getTargetInfo().hasFeature("vsx")) 3175 return Diag(TheCall->getBeginLoc(), diag::err_ppc_builtin_only_on_pwr7) 3176 << TheCall->getSourceRange(); 3177 return false; 3178 }; 3179 3180 switch (BuiltinID) { 3181 default: return false; 3182 case PPC::BI__builtin_altivec_crypto_vshasigmaw: 3183 case PPC::BI__builtin_altivec_crypto_vshasigmad: 3184 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || 3185 SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); 3186 case PPC::BI__builtin_tbegin: 3187 case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break; 3188 case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break; 3189 case PPC::BI__builtin_tabortwc: 3190 case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break; 3191 case PPC::BI__builtin_tabortwci: 3192 case PPC::BI__builtin_tabortdci: 3193 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) || 3194 SemaBuiltinConstantArgRange(TheCall, 2, 0, 31); 3195 case PPC::BI__builtin_vsx_xxpermdi: 3196 case PPC::BI__builtin_vsx_xxsldwi: 3197 return SemaBuiltinVSX(TheCall); 3198 case PPC::BI__builtin_unpack_vector_int128: 3199 return SemaVSXCheck(TheCall) || 3200 SemaBuiltinConstantArgRange(TheCall, 1, 0, 1); 3201 case PPC::BI__builtin_pack_vector_int128: 3202 return SemaVSXCheck(TheCall); 3203 } 3204 return SemaBuiltinConstantArgRange(TheCall, i, l, u); 3205 } 3206 3207 bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID, 3208 CallExpr *TheCall) { 3209 if (BuiltinID == SystemZ::BI__builtin_tabort) { 3210 Expr *Arg = TheCall->getArg(0); 3211 llvm::APSInt AbortCode(32); 3212 if (Arg->isIntegerConstantExpr(AbortCode, Context) && 3213 AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256) 3214 return Diag(Arg->getBeginLoc(), diag::err_systemz_invalid_tabort_code) 3215 << Arg->getSourceRange(); 3216 } 3217 3218 // For intrinsics which take an immediate value as part of the instruction, 3219 // range check them here. 3220 unsigned i = 0, l = 0, u = 0; 3221 switch (BuiltinID) { 3222 default: return false; 3223 case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break; 3224 case SystemZ::BI__builtin_s390_verimb: 3225 case SystemZ::BI__builtin_s390_verimh: 3226 case SystemZ::BI__builtin_s390_verimf: 3227 case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break; 3228 case SystemZ::BI__builtin_s390_vfaeb: 3229 case SystemZ::BI__builtin_s390_vfaeh: 3230 case SystemZ::BI__builtin_s390_vfaef: 3231 case SystemZ::BI__builtin_s390_vfaebs: 3232 case SystemZ::BI__builtin_s390_vfaehs: 3233 case SystemZ::BI__builtin_s390_vfaefs: 3234 case SystemZ::BI__builtin_s390_vfaezb: 3235 case SystemZ::BI__builtin_s390_vfaezh: 3236 case SystemZ::BI__builtin_s390_vfaezf: 3237 case SystemZ::BI__builtin_s390_vfaezbs: 3238 case SystemZ::BI__builtin_s390_vfaezhs: 3239 case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break; 3240 case SystemZ::BI__builtin_s390_vfisb: 3241 case SystemZ::BI__builtin_s390_vfidb: 3242 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) || 3243 SemaBuiltinConstantArgRange(TheCall, 2, 0, 15); 3244 case SystemZ::BI__builtin_s390_vftcisb: 3245 case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break; 3246 case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break; 3247 case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break; 3248 case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break; 3249 case SystemZ::BI__builtin_s390_vstrcb: 3250 case SystemZ::BI__builtin_s390_vstrch: 3251 case SystemZ::BI__builtin_s390_vstrcf: 3252 case SystemZ::BI__builtin_s390_vstrczb: 3253 case SystemZ::BI__builtin_s390_vstrczh: 3254 case SystemZ::BI__builtin_s390_vstrczf: 3255 case SystemZ::BI__builtin_s390_vstrcbs: 3256 case SystemZ::BI__builtin_s390_vstrchs: 3257 case SystemZ::BI__builtin_s390_vstrcfs: 3258 case SystemZ::BI__builtin_s390_vstrczbs: 3259 case SystemZ::BI__builtin_s390_vstrczhs: 3260 case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break; 3261 case SystemZ::BI__builtin_s390_vmslg: i = 3; l = 0; u = 15; break; 3262 case SystemZ::BI__builtin_s390_vfminsb: 3263 case SystemZ::BI__builtin_s390_vfmaxsb: 3264 case SystemZ::BI__builtin_s390_vfmindb: 3265 case SystemZ::BI__builtin_s390_vfmaxdb: i = 2; l = 0; u = 15; break; 3266 } 3267 return SemaBuiltinConstantArgRange(TheCall, i, l, u); 3268 } 3269 3270 /// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *). 3271 /// This checks that the target supports __builtin_cpu_supports and 3272 /// that the string argument is constant and valid. 3273 static bool SemaBuiltinCpuSupports(Sema &S, CallExpr *TheCall) { 3274 Expr *Arg = TheCall->getArg(0); 3275 3276 // Check if the argument is a string literal. 3277 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) 3278 return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 3279 << Arg->getSourceRange(); 3280 3281 // Check the contents of the string. 3282 StringRef Feature = 3283 cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); 3284 if (!S.Context.getTargetInfo().validateCpuSupports(Feature)) 3285 return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_supports) 3286 << Arg->getSourceRange(); 3287 return false; 3288 } 3289 3290 /// SemaBuiltinCpuIs - Handle __builtin_cpu_is(char *). 3291 /// This checks that the target supports __builtin_cpu_is and 3292 /// that the string argument is constant and valid. 3293 static bool SemaBuiltinCpuIs(Sema &S, CallExpr *TheCall) { 3294 Expr *Arg = TheCall->getArg(0); 3295 3296 // Check if the argument is a string literal. 3297 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) 3298 return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 3299 << Arg->getSourceRange(); 3300 3301 // Check the contents of the string. 3302 StringRef Feature = 3303 cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); 3304 if (!S.Context.getTargetInfo().validateCpuIs(Feature)) 3305 return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is) 3306 << Arg->getSourceRange(); 3307 return false; 3308 } 3309 3310 // Check if the rounding mode is legal. 3311 bool Sema::CheckX86BuiltinRoundingOrSAE(unsigned BuiltinID, CallExpr *TheCall) { 3312 // Indicates if this instruction has rounding control or just SAE. 3313 bool HasRC = false; 3314 3315 unsigned ArgNum = 0; 3316 switch (BuiltinID) { 3317 default: 3318 return false; 3319 case X86::BI__builtin_ia32_vcvttsd2si32: 3320 case X86::BI__builtin_ia32_vcvttsd2si64: 3321 case X86::BI__builtin_ia32_vcvttsd2usi32: 3322 case X86::BI__builtin_ia32_vcvttsd2usi64: 3323 case X86::BI__builtin_ia32_vcvttss2si32: 3324 case X86::BI__builtin_ia32_vcvttss2si64: 3325 case X86::BI__builtin_ia32_vcvttss2usi32: 3326 case X86::BI__builtin_ia32_vcvttss2usi64: 3327 ArgNum = 1; 3328 break; 3329 case X86::BI__builtin_ia32_maxpd512: 3330 case X86::BI__builtin_ia32_maxps512: 3331 case X86::BI__builtin_ia32_minpd512: 3332 case X86::BI__builtin_ia32_minps512: 3333 ArgNum = 2; 3334 break; 3335 case X86::BI__builtin_ia32_cvtps2pd512_mask: 3336 case X86::BI__builtin_ia32_cvttpd2dq512_mask: 3337 case X86::BI__builtin_ia32_cvttpd2qq512_mask: 3338 case X86::BI__builtin_ia32_cvttpd2udq512_mask: 3339 case X86::BI__builtin_ia32_cvttpd2uqq512_mask: 3340 case X86::BI__builtin_ia32_cvttps2dq512_mask: 3341 case X86::BI__builtin_ia32_cvttps2qq512_mask: 3342 case X86::BI__builtin_ia32_cvttps2udq512_mask: 3343 case X86::BI__builtin_ia32_cvttps2uqq512_mask: 3344 case X86::BI__builtin_ia32_exp2pd_mask: 3345 case X86::BI__builtin_ia32_exp2ps_mask: 3346 case X86::BI__builtin_ia32_getexppd512_mask: 3347 case X86::BI__builtin_ia32_getexpps512_mask: 3348 case X86::BI__builtin_ia32_rcp28pd_mask: 3349 case X86::BI__builtin_ia32_rcp28ps_mask: 3350 case X86::BI__builtin_ia32_rsqrt28pd_mask: 3351 case X86::BI__builtin_ia32_rsqrt28ps_mask: 3352 case X86::BI__builtin_ia32_vcomisd: 3353 case X86::BI__builtin_ia32_vcomiss: 3354 case X86::BI__builtin_ia32_vcvtph2ps512_mask: 3355 ArgNum = 3; 3356 break; 3357 case X86::BI__builtin_ia32_cmppd512_mask: 3358 case X86::BI__builtin_ia32_cmpps512_mask: 3359 case X86::BI__builtin_ia32_cmpsd_mask: 3360 case X86::BI__builtin_ia32_cmpss_mask: 3361 case X86::BI__builtin_ia32_cvtss2sd_round_mask: 3362 case X86::BI__builtin_ia32_getexpsd128_round_mask: 3363 case X86::BI__builtin_ia32_getexpss128_round_mask: 3364 case X86::BI__builtin_ia32_maxsd_round_mask: 3365 case X86::BI__builtin_ia32_maxss_round_mask: 3366 case X86::BI__builtin_ia32_minsd_round_mask: 3367 case X86::BI__builtin_ia32_minss_round_mask: 3368 case X86::BI__builtin_ia32_rcp28sd_round_mask: 3369 case X86::BI__builtin_ia32_rcp28ss_round_mask: 3370 case X86::BI__builtin_ia32_reducepd512_mask: 3371 case X86::BI__builtin_ia32_reduceps512_mask: 3372 case X86::BI__builtin_ia32_rndscalepd_mask: 3373 case X86::BI__builtin_ia32_rndscaleps_mask: 3374 case X86::BI__builtin_ia32_rsqrt28sd_round_mask: 3375 case X86::BI__builtin_ia32_rsqrt28ss_round_mask: 3376 ArgNum = 4; 3377 break; 3378 case X86::BI__builtin_ia32_fixupimmpd512_mask: 3379 case X86::BI__builtin_ia32_fixupimmpd512_maskz: 3380 case X86::BI__builtin_ia32_fixupimmps512_mask: 3381 case X86::BI__builtin_ia32_fixupimmps512_maskz: 3382 case X86::BI__builtin_ia32_fixupimmsd_mask: 3383 case X86::BI__builtin_ia32_fixupimmsd_maskz: 3384 case X86::BI__builtin_ia32_fixupimmss_mask: 3385 case X86::BI__builtin_ia32_fixupimmss_maskz: 3386 case X86::BI__builtin_ia32_rangepd512_mask: 3387 case X86::BI__builtin_ia32_rangeps512_mask: 3388 case X86::BI__builtin_ia32_rangesd128_round_mask: 3389 case X86::BI__builtin_ia32_rangess128_round_mask: 3390 case X86::BI__builtin_ia32_reducesd_mask: 3391 case X86::BI__builtin_ia32_reducess_mask: 3392 case X86::BI__builtin_ia32_rndscalesd_round_mask: 3393 case X86::BI__builtin_ia32_rndscaless_round_mask: 3394 ArgNum = 5; 3395 break; 3396 case X86::BI__builtin_ia32_vcvtsd2si64: 3397 case X86::BI__builtin_ia32_vcvtsd2si32: 3398 case X86::BI__builtin_ia32_vcvtsd2usi32: 3399 case X86::BI__builtin_ia32_vcvtsd2usi64: 3400 case X86::BI__builtin_ia32_vcvtss2si32: 3401 case X86::BI__builtin_ia32_vcvtss2si64: 3402 case X86::BI__builtin_ia32_vcvtss2usi32: 3403 case X86::BI__builtin_ia32_vcvtss2usi64: 3404 case X86::BI__builtin_ia32_sqrtpd512: 3405 case X86::BI__builtin_ia32_sqrtps512: 3406 ArgNum = 1; 3407 HasRC = true; 3408 break; 3409 case X86::BI__builtin_ia32_addpd512: 3410 case X86::BI__builtin_ia32_addps512: 3411 case X86::BI__builtin_ia32_divpd512: 3412 case X86::BI__builtin_ia32_divps512: 3413 case X86::BI__builtin_ia32_mulpd512: 3414 case X86::BI__builtin_ia32_mulps512: 3415 case X86::BI__builtin_ia32_subpd512: 3416 case X86::BI__builtin_ia32_subps512: 3417 case X86::BI__builtin_ia32_cvtsi2sd64: 3418 case X86::BI__builtin_ia32_cvtsi2ss32: 3419 case X86::BI__builtin_ia32_cvtsi2ss64: 3420 case X86::BI__builtin_ia32_cvtusi2sd64: 3421 case X86::BI__builtin_ia32_cvtusi2ss32: 3422 case X86::BI__builtin_ia32_cvtusi2ss64: 3423 ArgNum = 2; 3424 HasRC = true; 3425 break; 3426 case X86::BI__builtin_ia32_cvtdq2ps512_mask: 3427 case X86::BI__builtin_ia32_cvtudq2ps512_mask: 3428 case X86::BI__builtin_ia32_cvtpd2ps512_mask: 3429 case X86::BI__builtin_ia32_cvtpd2qq512_mask: 3430 case X86::BI__builtin_ia32_cvtpd2uqq512_mask: 3431 case X86::BI__builtin_ia32_cvtps2qq512_mask: 3432 case X86::BI__builtin_ia32_cvtps2uqq512_mask: 3433 case X86::BI__builtin_ia32_cvtqq2pd512_mask: 3434 case X86::BI__builtin_ia32_cvtqq2ps512_mask: 3435 case X86::BI__builtin_ia32_cvtuqq2pd512_mask: 3436 case X86::BI__builtin_ia32_cvtuqq2ps512_mask: 3437 ArgNum = 3; 3438 HasRC = true; 3439 break; 3440 case X86::BI__builtin_ia32_addss_round_mask: 3441 case X86::BI__builtin_ia32_addsd_round_mask: 3442 case X86::BI__builtin_ia32_divss_round_mask: 3443 case X86::BI__builtin_ia32_divsd_round_mask: 3444 case X86::BI__builtin_ia32_mulss_round_mask: 3445 case X86::BI__builtin_ia32_mulsd_round_mask: 3446 case X86::BI__builtin_ia32_subss_round_mask: 3447 case X86::BI__builtin_ia32_subsd_round_mask: 3448 case X86::BI__builtin_ia32_scalefpd512_mask: 3449 case X86::BI__builtin_ia32_scalefps512_mask: 3450 case X86::BI__builtin_ia32_scalefsd_round_mask: 3451 case X86::BI__builtin_ia32_scalefss_round_mask: 3452 case X86::BI__builtin_ia32_getmantpd512_mask: 3453 case X86::BI__builtin_ia32_getmantps512_mask: 3454 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: 3455 case X86::BI__builtin_ia32_sqrtsd_round_mask: 3456 case X86::BI__builtin_ia32_sqrtss_round_mask: 3457 case X86::BI__builtin_ia32_vfmaddsd3_mask: 3458 case X86::BI__builtin_ia32_vfmaddsd3_maskz: 3459 case X86::BI__builtin_ia32_vfmaddsd3_mask3: 3460 case X86::BI__builtin_ia32_vfmaddss3_mask: 3461 case X86::BI__builtin_ia32_vfmaddss3_maskz: 3462 case X86::BI__builtin_ia32_vfmaddss3_mask3: 3463 case X86::BI__builtin_ia32_vfmaddpd512_mask: 3464 case X86::BI__builtin_ia32_vfmaddpd512_maskz: 3465 case X86::BI__builtin_ia32_vfmaddpd512_mask3: 3466 case X86::BI__builtin_ia32_vfmsubpd512_mask3: 3467 case X86::BI__builtin_ia32_vfmaddps512_mask: 3468 case X86::BI__builtin_ia32_vfmaddps512_maskz: 3469 case X86::BI__builtin_ia32_vfmaddps512_mask3: 3470 case X86::BI__builtin_ia32_vfmsubps512_mask3: 3471 case X86::BI__builtin_ia32_vfmaddsubpd512_mask: 3472 case X86::BI__builtin_ia32_vfmaddsubpd512_maskz: 3473 case X86::BI__builtin_ia32_vfmaddsubpd512_mask3: 3474 case X86::BI__builtin_ia32_vfmsubaddpd512_mask3: 3475 case X86::BI__builtin_ia32_vfmaddsubps512_mask: 3476 case X86::BI__builtin_ia32_vfmaddsubps512_maskz: 3477 case X86::BI__builtin_ia32_vfmaddsubps512_mask3: 3478 case X86::BI__builtin_ia32_vfmsubaddps512_mask3: 3479 ArgNum = 4; 3480 HasRC = true; 3481 break; 3482 case X86::BI__builtin_ia32_getmantsd_round_mask: 3483 case X86::BI__builtin_ia32_getmantss_round_mask: 3484 ArgNum = 5; 3485 HasRC = true; 3486 break; 3487 } 3488 3489 llvm::APSInt Result; 3490 3491 // We can't check the value of a dependent argument. 3492 Expr *Arg = TheCall->getArg(ArgNum); 3493 if (Arg->isTypeDependent() || Arg->isValueDependent()) 3494 return false; 3495 3496 // Check constant-ness first. 3497 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) 3498 return true; 3499 3500 // Make sure rounding mode is either ROUND_CUR_DIRECTION or ROUND_NO_EXC bit 3501 // is set. If the intrinsic has rounding control(bits 1:0), make sure its only 3502 // combined with ROUND_NO_EXC. 3503 if (Result == 4/*ROUND_CUR_DIRECTION*/ || 3504 Result == 8/*ROUND_NO_EXC*/ || 3505 (HasRC && Result.getZExtValue() >= 8 && Result.getZExtValue() <= 11)) 3506 return false; 3507 3508 return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_rounding) 3509 << Arg->getSourceRange(); 3510 } 3511 3512 // Check if the gather/scatter scale is legal. 3513 bool Sema::CheckX86BuiltinGatherScatterScale(unsigned BuiltinID, 3514 CallExpr *TheCall) { 3515 unsigned ArgNum = 0; 3516 switch (BuiltinID) { 3517 default: 3518 return false; 3519 case X86::BI__builtin_ia32_gatherpfdpd: 3520 case X86::BI__builtin_ia32_gatherpfdps: 3521 case X86::BI__builtin_ia32_gatherpfqpd: 3522 case X86::BI__builtin_ia32_gatherpfqps: 3523 case X86::BI__builtin_ia32_scatterpfdpd: 3524 case X86::BI__builtin_ia32_scatterpfdps: 3525 case X86::BI__builtin_ia32_scatterpfqpd: 3526 case X86::BI__builtin_ia32_scatterpfqps: 3527 ArgNum = 3; 3528 break; 3529 case X86::BI__builtin_ia32_gatherd_pd: 3530 case X86::BI__builtin_ia32_gatherd_pd256: 3531 case X86::BI__builtin_ia32_gatherq_pd: 3532 case X86::BI__builtin_ia32_gatherq_pd256: 3533 case X86::BI__builtin_ia32_gatherd_ps: 3534 case X86::BI__builtin_ia32_gatherd_ps256: 3535 case X86::BI__builtin_ia32_gatherq_ps: 3536 case X86::BI__builtin_ia32_gatherq_ps256: 3537 case X86::BI__builtin_ia32_gatherd_q: 3538 case X86::BI__builtin_ia32_gatherd_q256: 3539 case X86::BI__builtin_ia32_gatherq_q: 3540 case X86::BI__builtin_ia32_gatherq_q256: 3541 case X86::BI__builtin_ia32_gatherd_d: 3542 case X86::BI__builtin_ia32_gatherd_d256: 3543 case X86::BI__builtin_ia32_gatherq_d: 3544 case X86::BI__builtin_ia32_gatherq_d256: 3545 case X86::BI__builtin_ia32_gather3div2df: 3546 case X86::BI__builtin_ia32_gather3div2di: 3547 case X86::BI__builtin_ia32_gather3div4df: 3548 case X86::BI__builtin_ia32_gather3div4di: 3549 case X86::BI__builtin_ia32_gather3div4sf: 3550 case X86::BI__builtin_ia32_gather3div4si: 3551 case X86::BI__builtin_ia32_gather3div8sf: 3552 case X86::BI__builtin_ia32_gather3div8si: 3553 case X86::BI__builtin_ia32_gather3siv2df: 3554 case X86::BI__builtin_ia32_gather3siv2di: 3555 case X86::BI__builtin_ia32_gather3siv4df: 3556 case X86::BI__builtin_ia32_gather3siv4di: 3557 case X86::BI__builtin_ia32_gather3siv4sf: 3558 case X86::BI__builtin_ia32_gather3siv4si: 3559 case X86::BI__builtin_ia32_gather3siv8sf: 3560 case X86::BI__builtin_ia32_gather3siv8si: 3561 case X86::BI__builtin_ia32_gathersiv8df: 3562 case X86::BI__builtin_ia32_gathersiv16sf: 3563 case X86::BI__builtin_ia32_gatherdiv8df: 3564 case X86::BI__builtin_ia32_gatherdiv16sf: 3565 case X86::BI__builtin_ia32_gathersiv8di: 3566 case X86::BI__builtin_ia32_gathersiv16si: 3567 case X86::BI__builtin_ia32_gatherdiv8di: 3568 case X86::BI__builtin_ia32_gatherdiv16si: 3569 case X86::BI__builtin_ia32_scatterdiv2df: 3570 case X86::BI__builtin_ia32_scatterdiv2di: 3571 case X86::BI__builtin_ia32_scatterdiv4df: 3572 case X86::BI__builtin_ia32_scatterdiv4di: 3573 case X86::BI__builtin_ia32_scatterdiv4sf: 3574 case X86::BI__builtin_ia32_scatterdiv4si: 3575 case X86::BI__builtin_ia32_scatterdiv8sf: 3576 case X86::BI__builtin_ia32_scatterdiv8si: 3577 case X86::BI__builtin_ia32_scattersiv2df: 3578 case X86::BI__builtin_ia32_scattersiv2di: 3579 case X86::BI__builtin_ia32_scattersiv4df: 3580 case X86::BI__builtin_ia32_scattersiv4di: 3581 case X86::BI__builtin_ia32_scattersiv4sf: 3582 case X86::BI__builtin_ia32_scattersiv4si: 3583 case X86::BI__builtin_ia32_scattersiv8sf: 3584 case X86::BI__builtin_ia32_scattersiv8si: 3585 case X86::BI__builtin_ia32_scattersiv8df: 3586 case X86::BI__builtin_ia32_scattersiv16sf: 3587 case X86::BI__builtin_ia32_scatterdiv8df: 3588 case X86::BI__builtin_ia32_scatterdiv16sf: 3589 case X86::BI__builtin_ia32_scattersiv8di: 3590 case X86::BI__builtin_ia32_scattersiv16si: 3591 case X86::BI__builtin_ia32_scatterdiv8di: 3592 case X86::BI__builtin_ia32_scatterdiv16si: 3593 ArgNum = 4; 3594 break; 3595 } 3596 3597 llvm::APSInt Result; 3598 3599 // We can't check the value of a dependent argument. 3600 Expr *Arg = TheCall->getArg(ArgNum); 3601 if (Arg->isTypeDependent() || Arg->isValueDependent()) 3602 return false; 3603 3604 // Check constant-ness first. 3605 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) 3606 return true; 3607 3608 if (Result == 1 || Result == 2 || Result == 4 || Result == 8) 3609 return false; 3610 3611 return Diag(TheCall->getBeginLoc(), diag::err_x86_builtin_invalid_scale) 3612 << Arg->getSourceRange(); 3613 } 3614 3615 static bool isX86_32Builtin(unsigned BuiltinID) { 3616 // These builtins only work on x86-32 targets. 3617 switch (BuiltinID) { 3618 case X86::BI__builtin_ia32_readeflags_u32: 3619 case X86::BI__builtin_ia32_writeeflags_u32: 3620 return true; 3621 } 3622 3623 return false; 3624 } 3625 3626 bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 3627 if (BuiltinID == X86::BI__builtin_cpu_supports) 3628 return SemaBuiltinCpuSupports(*this, TheCall); 3629 3630 if (BuiltinID == X86::BI__builtin_cpu_is) 3631 return SemaBuiltinCpuIs(*this, TheCall); 3632 3633 // Check for 32-bit only builtins on a 64-bit target. 3634 const llvm::Triple &TT = Context.getTargetInfo().getTriple(); 3635 if (TT.getArch() != llvm::Triple::x86 && isX86_32Builtin(BuiltinID)) 3636 return Diag(TheCall->getCallee()->getBeginLoc(), 3637 diag::err_32_bit_builtin_64_bit_tgt); 3638 3639 // If the intrinsic has rounding or SAE make sure its valid. 3640 if (CheckX86BuiltinRoundingOrSAE(BuiltinID, TheCall)) 3641 return true; 3642 3643 // If the intrinsic has a gather/scatter scale immediate make sure its valid. 3644 if (CheckX86BuiltinGatherScatterScale(BuiltinID, TheCall)) 3645 return true; 3646 3647 // For intrinsics which take an immediate value as part of the instruction, 3648 // range check them here. 3649 int i = 0, l = 0, u = 0; 3650 switch (BuiltinID) { 3651 default: 3652 return false; 3653 case X86::BI__builtin_ia32_vec_ext_v2si: 3654 case X86::BI__builtin_ia32_vec_ext_v2di: 3655 case X86::BI__builtin_ia32_vextractf128_pd256: 3656 case X86::BI__builtin_ia32_vextractf128_ps256: 3657 case X86::BI__builtin_ia32_vextractf128_si256: 3658 case X86::BI__builtin_ia32_extract128i256: 3659 case X86::BI__builtin_ia32_extractf64x4_mask: 3660 case X86::BI__builtin_ia32_extracti64x4_mask: 3661 case X86::BI__builtin_ia32_extractf32x8_mask: 3662 case X86::BI__builtin_ia32_extracti32x8_mask: 3663 case X86::BI__builtin_ia32_extractf64x2_256_mask: 3664 case X86::BI__builtin_ia32_extracti64x2_256_mask: 3665 case X86::BI__builtin_ia32_extractf32x4_256_mask: 3666 case X86::BI__builtin_ia32_extracti32x4_256_mask: 3667 i = 1; l = 0; u = 1; 3668 break; 3669 case X86::BI__builtin_ia32_vec_set_v2di: 3670 case X86::BI__builtin_ia32_vinsertf128_pd256: 3671 case X86::BI__builtin_ia32_vinsertf128_ps256: 3672 case X86::BI__builtin_ia32_vinsertf128_si256: 3673 case X86::BI__builtin_ia32_insert128i256: 3674 case X86::BI__builtin_ia32_insertf32x8: 3675 case X86::BI__builtin_ia32_inserti32x8: 3676 case X86::BI__builtin_ia32_insertf64x4: 3677 case X86::BI__builtin_ia32_inserti64x4: 3678 case X86::BI__builtin_ia32_insertf64x2_256: 3679 case X86::BI__builtin_ia32_inserti64x2_256: 3680 case X86::BI__builtin_ia32_insertf32x4_256: 3681 case X86::BI__builtin_ia32_inserti32x4_256: 3682 i = 2; l = 0; u = 1; 3683 break; 3684 case X86::BI__builtin_ia32_vpermilpd: 3685 case X86::BI__builtin_ia32_vec_ext_v4hi: 3686 case X86::BI__builtin_ia32_vec_ext_v4si: 3687 case X86::BI__builtin_ia32_vec_ext_v4sf: 3688 case X86::BI__builtin_ia32_vec_ext_v4di: 3689 case X86::BI__builtin_ia32_extractf32x4_mask: 3690 case X86::BI__builtin_ia32_extracti32x4_mask: 3691 case X86::BI__builtin_ia32_extractf64x2_512_mask: 3692 case X86::BI__builtin_ia32_extracti64x2_512_mask: 3693 i = 1; l = 0; u = 3; 3694 break; 3695 case X86::BI_mm_prefetch: 3696 case X86::BI__builtin_ia32_vec_ext_v8hi: 3697 case X86::BI__builtin_ia32_vec_ext_v8si: 3698 i = 1; l = 0; u = 7; 3699 break; 3700 case X86::BI__builtin_ia32_sha1rnds4: 3701 case X86::BI__builtin_ia32_blendpd: 3702 case X86::BI__builtin_ia32_shufpd: 3703 case X86::BI__builtin_ia32_vec_set_v4hi: 3704 case X86::BI__builtin_ia32_vec_set_v4si: 3705 case X86::BI__builtin_ia32_vec_set_v4di: 3706 case X86::BI__builtin_ia32_shuf_f32x4_256: 3707 case X86::BI__builtin_ia32_shuf_f64x2_256: 3708 case X86::BI__builtin_ia32_shuf_i32x4_256: 3709 case X86::BI__builtin_ia32_shuf_i64x2_256: 3710 case X86::BI__builtin_ia32_insertf64x2_512: 3711 case X86::BI__builtin_ia32_inserti64x2_512: 3712 case X86::BI__builtin_ia32_insertf32x4: 3713 case X86::BI__builtin_ia32_inserti32x4: 3714 i = 2; l = 0; u = 3; 3715 break; 3716 case X86::BI__builtin_ia32_vpermil2pd: 3717 case X86::BI__builtin_ia32_vpermil2pd256: 3718 case X86::BI__builtin_ia32_vpermil2ps: 3719 case X86::BI__builtin_ia32_vpermil2ps256: 3720 i = 3; l = 0; u = 3; 3721 break; 3722 case X86::BI__builtin_ia32_cmpb128_mask: 3723 case X86::BI__builtin_ia32_cmpw128_mask: 3724 case X86::BI__builtin_ia32_cmpd128_mask: 3725 case X86::BI__builtin_ia32_cmpq128_mask: 3726 case X86::BI__builtin_ia32_cmpb256_mask: 3727 case X86::BI__builtin_ia32_cmpw256_mask: 3728 case X86::BI__builtin_ia32_cmpd256_mask: 3729 case X86::BI__builtin_ia32_cmpq256_mask: 3730 case X86::BI__builtin_ia32_cmpb512_mask: 3731 case X86::BI__builtin_ia32_cmpw512_mask: 3732 case X86::BI__builtin_ia32_cmpd512_mask: 3733 case X86::BI__builtin_ia32_cmpq512_mask: 3734 case X86::BI__builtin_ia32_ucmpb128_mask: 3735 case X86::BI__builtin_ia32_ucmpw128_mask: 3736 case X86::BI__builtin_ia32_ucmpd128_mask: 3737 case X86::BI__builtin_ia32_ucmpq128_mask: 3738 case X86::BI__builtin_ia32_ucmpb256_mask: 3739 case X86::BI__builtin_ia32_ucmpw256_mask: 3740 case X86::BI__builtin_ia32_ucmpd256_mask: 3741 case X86::BI__builtin_ia32_ucmpq256_mask: 3742 case X86::BI__builtin_ia32_ucmpb512_mask: 3743 case X86::BI__builtin_ia32_ucmpw512_mask: 3744 case X86::BI__builtin_ia32_ucmpd512_mask: 3745 case X86::BI__builtin_ia32_ucmpq512_mask: 3746 case X86::BI__builtin_ia32_vpcomub: 3747 case X86::BI__builtin_ia32_vpcomuw: 3748 case X86::BI__builtin_ia32_vpcomud: 3749 case X86::BI__builtin_ia32_vpcomuq: 3750 case X86::BI__builtin_ia32_vpcomb: 3751 case X86::BI__builtin_ia32_vpcomw: 3752 case X86::BI__builtin_ia32_vpcomd: 3753 case X86::BI__builtin_ia32_vpcomq: 3754 case X86::BI__builtin_ia32_vec_set_v8hi: 3755 case X86::BI__builtin_ia32_vec_set_v8si: 3756 i = 2; l = 0; u = 7; 3757 break; 3758 case X86::BI__builtin_ia32_vpermilpd256: 3759 case X86::BI__builtin_ia32_roundps: 3760 case X86::BI__builtin_ia32_roundpd: 3761 case X86::BI__builtin_ia32_roundps256: 3762 case X86::BI__builtin_ia32_roundpd256: 3763 case X86::BI__builtin_ia32_getmantpd128_mask: 3764 case X86::BI__builtin_ia32_getmantpd256_mask: 3765 case X86::BI__builtin_ia32_getmantps128_mask: 3766 case X86::BI__builtin_ia32_getmantps256_mask: 3767 case X86::BI__builtin_ia32_getmantpd512_mask: 3768 case X86::BI__builtin_ia32_getmantps512_mask: 3769 case X86::BI__builtin_ia32_vec_ext_v16qi: 3770 case X86::BI__builtin_ia32_vec_ext_v16hi: 3771 i = 1; l = 0; u = 15; 3772 break; 3773 case X86::BI__builtin_ia32_pblendd128: 3774 case X86::BI__builtin_ia32_blendps: 3775 case X86::BI__builtin_ia32_blendpd256: 3776 case X86::BI__builtin_ia32_shufpd256: 3777 case X86::BI__builtin_ia32_roundss: 3778 case X86::BI__builtin_ia32_roundsd: 3779 case X86::BI__builtin_ia32_rangepd128_mask: 3780 case X86::BI__builtin_ia32_rangepd256_mask: 3781 case X86::BI__builtin_ia32_rangepd512_mask: 3782 case X86::BI__builtin_ia32_rangeps128_mask: 3783 case X86::BI__builtin_ia32_rangeps256_mask: 3784 case X86::BI__builtin_ia32_rangeps512_mask: 3785 case X86::BI__builtin_ia32_getmantsd_round_mask: 3786 case X86::BI__builtin_ia32_getmantss_round_mask: 3787 case X86::BI__builtin_ia32_vec_set_v16qi: 3788 case X86::BI__builtin_ia32_vec_set_v16hi: 3789 i = 2; l = 0; u = 15; 3790 break; 3791 case X86::BI__builtin_ia32_vec_ext_v32qi: 3792 i = 1; l = 0; u = 31; 3793 break; 3794 case X86::BI__builtin_ia32_cmpps: 3795 case X86::BI__builtin_ia32_cmpss: 3796 case X86::BI__builtin_ia32_cmppd: 3797 case X86::BI__builtin_ia32_cmpsd: 3798 case X86::BI__builtin_ia32_cmpps256: 3799 case X86::BI__builtin_ia32_cmppd256: 3800 case X86::BI__builtin_ia32_cmpps128_mask: 3801 case X86::BI__builtin_ia32_cmppd128_mask: 3802 case X86::BI__builtin_ia32_cmpps256_mask: 3803 case X86::BI__builtin_ia32_cmppd256_mask: 3804 case X86::BI__builtin_ia32_cmpps512_mask: 3805 case X86::BI__builtin_ia32_cmppd512_mask: 3806 case X86::BI__builtin_ia32_cmpsd_mask: 3807 case X86::BI__builtin_ia32_cmpss_mask: 3808 case X86::BI__builtin_ia32_vec_set_v32qi: 3809 i = 2; l = 0; u = 31; 3810 break; 3811 case X86::BI__builtin_ia32_permdf256: 3812 case X86::BI__builtin_ia32_permdi256: 3813 case X86::BI__builtin_ia32_permdf512: 3814 case X86::BI__builtin_ia32_permdi512: 3815 case X86::BI__builtin_ia32_vpermilps: 3816 case X86::BI__builtin_ia32_vpermilps256: 3817 case X86::BI__builtin_ia32_vpermilpd512: 3818 case X86::BI__builtin_ia32_vpermilps512: 3819 case X86::BI__builtin_ia32_pshufd: 3820 case X86::BI__builtin_ia32_pshufd256: 3821 case X86::BI__builtin_ia32_pshufd512: 3822 case X86::BI__builtin_ia32_pshufhw: 3823 case X86::BI__builtin_ia32_pshufhw256: 3824 case X86::BI__builtin_ia32_pshufhw512: 3825 case X86::BI__builtin_ia32_pshuflw: 3826 case X86::BI__builtin_ia32_pshuflw256: 3827 case X86::BI__builtin_ia32_pshuflw512: 3828 case X86::BI__builtin_ia32_vcvtps2ph: 3829 case X86::BI__builtin_ia32_vcvtps2ph_mask: 3830 case X86::BI__builtin_ia32_vcvtps2ph256: 3831 case X86::BI__builtin_ia32_vcvtps2ph256_mask: 3832 case X86::BI__builtin_ia32_vcvtps2ph512_mask: 3833 case X86::BI__builtin_ia32_rndscaleps_128_mask: 3834 case X86::BI__builtin_ia32_rndscalepd_128_mask: 3835 case X86::BI__builtin_ia32_rndscaleps_256_mask: 3836 case X86::BI__builtin_ia32_rndscalepd_256_mask: 3837 case X86::BI__builtin_ia32_rndscaleps_mask: 3838 case X86::BI__builtin_ia32_rndscalepd_mask: 3839 case X86::BI__builtin_ia32_reducepd128_mask: 3840 case X86::BI__builtin_ia32_reducepd256_mask: 3841 case X86::BI__builtin_ia32_reducepd512_mask: 3842 case X86::BI__builtin_ia32_reduceps128_mask: 3843 case X86::BI__builtin_ia32_reduceps256_mask: 3844 case X86::BI__builtin_ia32_reduceps512_mask: 3845 case X86::BI__builtin_ia32_prold512: 3846 case X86::BI__builtin_ia32_prolq512: 3847 case X86::BI__builtin_ia32_prold128: 3848 case X86::BI__builtin_ia32_prold256: 3849 case X86::BI__builtin_ia32_prolq128: 3850 case X86::BI__builtin_ia32_prolq256: 3851 case X86::BI__builtin_ia32_prord512: 3852 case X86::BI__builtin_ia32_prorq512: 3853 case X86::BI__builtin_ia32_prord128: 3854 case X86::BI__builtin_ia32_prord256: 3855 case X86::BI__builtin_ia32_prorq128: 3856 case X86::BI__builtin_ia32_prorq256: 3857 case X86::BI__builtin_ia32_fpclasspd128_mask: 3858 case X86::BI__builtin_ia32_fpclasspd256_mask: 3859 case X86::BI__builtin_ia32_fpclassps128_mask: 3860 case X86::BI__builtin_ia32_fpclassps256_mask: 3861 case X86::BI__builtin_ia32_fpclassps512_mask: 3862 case X86::BI__builtin_ia32_fpclasspd512_mask: 3863 case X86::BI__builtin_ia32_fpclasssd_mask: 3864 case X86::BI__builtin_ia32_fpclassss_mask: 3865 case X86::BI__builtin_ia32_pslldqi128_byteshift: 3866 case X86::BI__builtin_ia32_pslldqi256_byteshift: 3867 case X86::BI__builtin_ia32_pslldqi512_byteshift: 3868 case X86::BI__builtin_ia32_psrldqi128_byteshift: 3869 case X86::BI__builtin_ia32_psrldqi256_byteshift: 3870 case X86::BI__builtin_ia32_psrldqi512_byteshift: 3871 case X86::BI__builtin_ia32_kshiftliqi: 3872 case X86::BI__builtin_ia32_kshiftlihi: 3873 case X86::BI__builtin_ia32_kshiftlisi: 3874 case X86::BI__builtin_ia32_kshiftlidi: 3875 case X86::BI__builtin_ia32_kshiftriqi: 3876 case X86::BI__builtin_ia32_kshiftrihi: 3877 case X86::BI__builtin_ia32_kshiftrisi: 3878 case X86::BI__builtin_ia32_kshiftridi: 3879 i = 1; l = 0; u = 255; 3880 break; 3881 case X86::BI__builtin_ia32_vperm2f128_pd256: 3882 case X86::BI__builtin_ia32_vperm2f128_ps256: 3883 case X86::BI__builtin_ia32_vperm2f128_si256: 3884 case X86::BI__builtin_ia32_permti256: 3885 case X86::BI__builtin_ia32_pblendw128: 3886 case X86::BI__builtin_ia32_pblendw256: 3887 case X86::BI__builtin_ia32_blendps256: 3888 case X86::BI__builtin_ia32_pblendd256: 3889 case X86::BI__builtin_ia32_palignr128: 3890 case X86::BI__builtin_ia32_palignr256: 3891 case X86::BI__builtin_ia32_palignr512: 3892 case X86::BI__builtin_ia32_alignq512: 3893 case X86::BI__builtin_ia32_alignd512: 3894 case X86::BI__builtin_ia32_alignd128: 3895 case X86::BI__builtin_ia32_alignd256: 3896 case X86::BI__builtin_ia32_alignq128: 3897 case X86::BI__builtin_ia32_alignq256: 3898 case X86::BI__builtin_ia32_vcomisd: 3899 case X86::BI__builtin_ia32_vcomiss: 3900 case X86::BI__builtin_ia32_shuf_f32x4: 3901 case X86::BI__builtin_ia32_shuf_f64x2: 3902 case X86::BI__builtin_ia32_shuf_i32x4: 3903 case X86::BI__builtin_ia32_shuf_i64x2: 3904 case X86::BI__builtin_ia32_shufpd512: 3905 case X86::BI__builtin_ia32_shufps: 3906 case X86::BI__builtin_ia32_shufps256: 3907 case X86::BI__builtin_ia32_shufps512: 3908 case X86::BI__builtin_ia32_dbpsadbw128: 3909 case X86::BI__builtin_ia32_dbpsadbw256: 3910 case X86::BI__builtin_ia32_dbpsadbw512: 3911 case X86::BI__builtin_ia32_vpshldd128: 3912 case X86::BI__builtin_ia32_vpshldd256: 3913 case X86::BI__builtin_ia32_vpshldd512: 3914 case X86::BI__builtin_ia32_vpshldq128: 3915 case X86::BI__builtin_ia32_vpshldq256: 3916 case X86::BI__builtin_ia32_vpshldq512: 3917 case X86::BI__builtin_ia32_vpshldw128: 3918 case X86::BI__builtin_ia32_vpshldw256: 3919 case X86::BI__builtin_ia32_vpshldw512: 3920 case X86::BI__builtin_ia32_vpshrdd128: 3921 case X86::BI__builtin_ia32_vpshrdd256: 3922 case X86::BI__builtin_ia32_vpshrdd512: 3923 case X86::BI__builtin_ia32_vpshrdq128: 3924 case X86::BI__builtin_ia32_vpshrdq256: 3925 case X86::BI__builtin_ia32_vpshrdq512: 3926 case X86::BI__builtin_ia32_vpshrdw128: 3927 case X86::BI__builtin_ia32_vpshrdw256: 3928 case X86::BI__builtin_ia32_vpshrdw512: 3929 i = 2; l = 0; u = 255; 3930 break; 3931 case X86::BI__builtin_ia32_fixupimmpd512_mask: 3932 case X86::BI__builtin_ia32_fixupimmpd512_maskz: 3933 case X86::BI__builtin_ia32_fixupimmps512_mask: 3934 case X86::BI__builtin_ia32_fixupimmps512_maskz: 3935 case X86::BI__builtin_ia32_fixupimmsd_mask: 3936 case X86::BI__builtin_ia32_fixupimmsd_maskz: 3937 case X86::BI__builtin_ia32_fixupimmss_mask: 3938 case X86::BI__builtin_ia32_fixupimmss_maskz: 3939 case X86::BI__builtin_ia32_fixupimmpd128_mask: 3940 case X86::BI__builtin_ia32_fixupimmpd128_maskz: 3941 case X86::BI__builtin_ia32_fixupimmpd256_mask: 3942 case X86::BI__builtin_ia32_fixupimmpd256_maskz: 3943 case X86::BI__builtin_ia32_fixupimmps128_mask: 3944 case X86::BI__builtin_ia32_fixupimmps128_maskz: 3945 case X86::BI__builtin_ia32_fixupimmps256_mask: 3946 case X86::BI__builtin_ia32_fixupimmps256_maskz: 3947 case X86::BI__builtin_ia32_pternlogd512_mask: 3948 case X86::BI__builtin_ia32_pternlogd512_maskz: 3949 case X86::BI__builtin_ia32_pternlogq512_mask: 3950 case X86::BI__builtin_ia32_pternlogq512_maskz: 3951 case X86::BI__builtin_ia32_pternlogd128_mask: 3952 case X86::BI__builtin_ia32_pternlogd128_maskz: 3953 case X86::BI__builtin_ia32_pternlogd256_mask: 3954 case X86::BI__builtin_ia32_pternlogd256_maskz: 3955 case X86::BI__builtin_ia32_pternlogq128_mask: 3956 case X86::BI__builtin_ia32_pternlogq128_maskz: 3957 case X86::BI__builtin_ia32_pternlogq256_mask: 3958 case X86::BI__builtin_ia32_pternlogq256_maskz: 3959 i = 3; l = 0; u = 255; 3960 break; 3961 case X86::BI__builtin_ia32_gatherpfdpd: 3962 case X86::BI__builtin_ia32_gatherpfdps: 3963 case X86::BI__builtin_ia32_gatherpfqpd: 3964 case X86::BI__builtin_ia32_gatherpfqps: 3965 case X86::BI__builtin_ia32_scatterpfdpd: 3966 case X86::BI__builtin_ia32_scatterpfdps: 3967 case X86::BI__builtin_ia32_scatterpfqpd: 3968 case X86::BI__builtin_ia32_scatterpfqps: 3969 i = 4; l = 2; u = 3; 3970 break; 3971 case X86::BI__builtin_ia32_rndscalesd_round_mask: 3972 case X86::BI__builtin_ia32_rndscaless_round_mask: 3973 i = 4; l = 0; u = 255; 3974 break; 3975 } 3976 3977 // Note that we don't force a hard error on the range check here, allowing 3978 // template-generated or macro-generated dead code to potentially have out-of- 3979 // range values. These need to code generate, but don't need to necessarily 3980 // make any sense. We use a warning that defaults to an error. 3981 return SemaBuiltinConstantArgRange(TheCall, i, l, u, /*RangeIsError*/ false); 3982 } 3983 3984 /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo 3985 /// parameter with the FormatAttr's correct format_idx and firstDataArg. 3986 /// Returns true when the format fits the function and the FormatStringInfo has 3987 /// been populated. 3988 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, 3989 FormatStringInfo *FSI) { 3990 FSI->HasVAListArg = Format->getFirstArg() == 0; 3991 FSI->FormatIdx = Format->getFormatIdx() - 1; 3992 FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; 3993 3994 // The way the format attribute works in GCC, the implicit this argument 3995 // of member functions is counted. However, it doesn't appear in our own 3996 // lists, so decrement format_idx in that case. 3997 if (IsCXXMember) { 3998 if(FSI->FormatIdx == 0) 3999 return false; 4000 --FSI->FormatIdx; 4001 if (FSI->FirstDataArg != 0) 4002 --FSI->FirstDataArg; 4003 } 4004 return true; 4005 } 4006 4007 /// Checks if a the given expression evaluates to null. 4008 /// 4009 /// Returns true if the value evaluates to null. 4010 static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { 4011 // If the expression has non-null type, it doesn't evaluate to null. 4012 if (auto nullability 4013 = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) { 4014 if (*nullability == NullabilityKind::NonNull) 4015 return false; 4016 } 4017 4018 // As a special case, transparent unions initialized with zero are 4019 // considered null for the purposes of the nonnull attribute. 4020 if (const RecordType *UT = Expr->getType()->getAsUnionType()) { 4021 if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) 4022 if (const CompoundLiteralExpr *CLE = 4023 dyn_cast<CompoundLiteralExpr>(Expr)) 4024 if (const InitListExpr *ILE = 4025 dyn_cast<InitListExpr>(CLE->getInitializer())) 4026 Expr = ILE->getInit(0); 4027 } 4028 4029 bool Result; 4030 return (!Expr->isValueDependent() && 4031 Expr->EvaluateAsBooleanCondition(Result, S.Context) && 4032 !Result); 4033 } 4034 4035 static void CheckNonNullArgument(Sema &S, 4036 const Expr *ArgExpr, 4037 SourceLocation CallSiteLoc) { 4038 if (CheckNonNullExpr(S, ArgExpr)) 4039 S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr, 4040 S.PDiag(diag::warn_null_arg) << ArgExpr->getSourceRange()); 4041 } 4042 4043 bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) { 4044 FormatStringInfo FSI; 4045 if ((GetFormatStringType(Format) == FST_NSString) && 4046 getFormatStringInfo(Format, false, &FSI)) { 4047 Idx = FSI.FormatIdx; 4048 return true; 4049 } 4050 return false; 4051 } 4052 4053 /// Diagnose use of %s directive in an NSString which is being passed 4054 /// as formatting string to formatting method. 4055 static void 4056 DiagnoseCStringFormatDirectiveInCFAPI(Sema &S, 4057 const NamedDecl *FDecl, 4058 Expr **Args, 4059 unsigned NumArgs) { 4060 unsigned Idx = 0; 4061 bool Format = false; 4062 ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily(); 4063 if (SFFamily == ObjCStringFormatFamily::SFF_CFString) { 4064 Idx = 2; 4065 Format = true; 4066 } 4067 else 4068 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 4069 if (S.GetFormatNSStringIdx(I, Idx)) { 4070 Format = true; 4071 break; 4072 } 4073 } 4074 if (!Format || NumArgs <= Idx) 4075 return; 4076 const Expr *FormatExpr = Args[Idx]; 4077 if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr)) 4078 FormatExpr = CSCE->getSubExpr(); 4079 const StringLiteral *FormatString; 4080 if (const ObjCStringLiteral *OSL = 4081 dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) 4082 FormatString = OSL->getString(); 4083 else 4084 FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts()); 4085 if (!FormatString) 4086 return; 4087 if (S.FormatStringHasSArg(FormatString)) { 4088 S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string) 4089 << "%s" << 1 << 1; 4090 S.Diag(FDecl->getLocation(), diag::note_entity_declared_at) 4091 << FDecl->getDeclName(); 4092 } 4093 } 4094 4095 /// Determine whether the given type has a non-null nullability annotation. 4096 static bool isNonNullType(ASTContext &ctx, QualType type) { 4097 if (auto nullability = type->getNullability(ctx)) 4098 return *nullability == NullabilityKind::NonNull; 4099 4100 return false; 4101 } 4102 4103 static void CheckNonNullArguments(Sema &S, 4104 const NamedDecl *FDecl, 4105 const FunctionProtoType *Proto, 4106 ArrayRef<const Expr *> Args, 4107 SourceLocation CallSiteLoc) { 4108 assert((FDecl || Proto) && "Need a function declaration or prototype"); 4109 4110 // Check the attributes attached to the method/function itself. 4111 llvm::SmallBitVector NonNullArgs; 4112 if (FDecl) { 4113 // Handle the nonnull attribute on the function/method declaration itself. 4114 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { 4115 if (!NonNull->args_size()) { 4116 // Easy case: all pointer arguments are nonnull. 4117 for (const auto *Arg : Args) 4118 if (S.isValidPointerAttrType(Arg->getType())) 4119 CheckNonNullArgument(S, Arg, CallSiteLoc); 4120 return; 4121 } 4122 4123 for (const ParamIdx &Idx : NonNull->args()) { 4124 unsigned IdxAST = Idx.getASTIndex(); 4125 if (IdxAST >= Args.size()) 4126 continue; 4127 if (NonNullArgs.empty()) 4128 NonNullArgs.resize(Args.size()); 4129 NonNullArgs.set(IdxAST); 4130 } 4131 } 4132 } 4133 4134 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) { 4135 // Handle the nonnull attribute on the parameters of the 4136 // function/method. 4137 ArrayRef<ParmVarDecl*> parms; 4138 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) 4139 parms = FD->parameters(); 4140 else 4141 parms = cast<ObjCMethodDecl>(FDecl)->parameters(); 4142 4143 unsigned ParamIndex = 0; 4144 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); 4145 I != E; ++I, ++ParamIndex) { 4146 const ParmVarDecl *PVD = *I; 4147 if (PVD->hasAttr<NonNullAttr>() || 4148 isNonNullType(S.Context, PVD->getType())) { 4149 if (NonNullArgs.empty()) 4150 NonNullArgs.resize(Args.size()); 4151 4152 NonNullArgs.set(ParamIndex); 4153 } 4154 } 4155 } else { 4156 // If we have a non-function, non-method declaration but no 4157 // function prototype, try to dig out the function prototype. 4158 if (!Proto) { 4159 if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) { 4160 QualType type = VD->getType().getNonReferenceType(); 4161 if (auto pointerType = type->getAs<PointerType>()) 4162 type = pointerType->getPointeeType(); 4163 else if (auto blockType = type->getAs<BlockPointerType>()) 4164 type = blockType->getPointeeType(); 4165 // FIXME: data member pointers? 4166 4167 // Dig out the function prototype, if there is one. 4168 Proto = type->getAs<FunctionProtoType>(); 4169 } 4170 } 4171 4172 // Fill in non-null argument information from the nullability 4173 // information on the parameter types (if we have them). 4174 if (Proto) { 4175 unsigned Index = 0; 4176 for (auto paramType : Proto->getParamTypes()) { 4177 if (isNonNullType(S.Context, paramType)) { 4178 if (NonNullArgs.empty()) 4179 NonNullArgs.resize(Args.size()); 4180 4181 NonNullArgs.set(Index); 4182 } 4183 4184 ++Index; 4185 } 4186 } 4187 } 4188 4189 // Check for non-null arguments. 4190 for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size(); 4191 ArgIndex != ArgIndexEnd; ++ArgIndex) { 4192 if (NonNullArgs[ArgIndex]) 4193 CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc); 4194 } 4195 } 4196 4197 /// Handles the checks for format strings, non-POD arguments to vararg 4198 /// functions, NULL arguments passed to non-NULL parameters, and diagnose_if 4199 /// attributes. 4200 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, 4201 const Expr *ThisArg, ArrayRef<const Expr *> Args, 4202 bool IsMemberFunction, SourceLocation Loc, 4203 SourceRange Range, VariadicCallType CallType) { 4204 // FIXME: We should check as much as we can in the template definition. 4205 if (CurContext->isDependentContext()) 4206 return; 4207 4208 // Printf and scanf checking. 4209 llvm::SmallBitVector CheckedVarArgs; 4210 if (FDecl) { 4211 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 4212 // Only create vector if there are format attributes. 4213 CheckedVarArgs.resize(Args.size()); 4214 4215 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, 4216 CheckedVarArgs); 4217 } 4218 } 4219 4220 // Refuse POD arguments that weren't caught by the format string 4221 // checks above. 4222 auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl); 4223 if (CallType != VariadicDoesNotApply && 4224 (!FD || FD->getBuiltinID() != Builtin::BI__noop)) { 4225 unsigned NumParams = Proto ? Proto->getNumParams() 4226 : FDecl && isa<FunctionDecl>(FDecl) 4227 ? cast<FunctionDecl>(FDecl)->getNumParams() 4228 : FDecl && isa<ObjCMethodDecl>(FDecl) 4229 ? cast<ObjCMethodDecl>(FDecl)->param_size() 4230 : 0; 4231 4232 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { 4233 // Args[ArgIdx] can be null in malformed code. 4234 if (const Expr *Arg = Args[ArgIdx]) { 4235 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) 4236 checkVariadicArgument(Arg, CallType); 4237 } 4238 } 4239 } 4240 4241 if (FDecl || Proto) { 4242 CheckNonNullArguments(*this, FDecl, Proto, Args, Loc); 4243 4244 // Type safety checking. 4245 if (FDecl) { 4246 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) 4247 CheckArgumentWithTypeTag(I, Args, Loc); 4248 } 4249 } 4250 4251 if (FD) 4252 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc); 4253 } 4254 4255 /// CheckConstructorCall - Check a constructor call for correctness and safety 4256 /// properties not enforced by the C type system. 4257 void Sema::CheckConstructorCall(FunctionDecl *FDecl, 4258 ArrayRef<const Expr *> Args, 4259 const FunctionProtoType *Proto, 4260 SourceLocation Loc) { 4261 VariadicCallType CallType = 4262 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; 4263 checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true, 4264 Loc, SourceRange(), CallType); 4265 } 4266 4267 /// CheckFunctionCall - Check a direct function call for various correctness 4268 /// and safety properties not strictly enforced by the C type system. 4269 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, 4270 const FunctionProtoType *Proto) { 4271 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && 4272 isa<CXXMethodDecl>(FDecl); 4273 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || 4274 IsMemberOperatorCall; 4275 VariadicCallType CallType = getVariadicCallType(FDecl, Proto, 4276 TheCall->getCallee()); 4277 Expr** Args = TheCall->getArgs(); 4278 unsigned NumArgs = TheCall->getNumArgs(); 4279 4280 Expr *ImplicitThis = nullptr; 4281 if (IsMemberOperatorCall) { 4282 // If this is a call to a member operator, hide the first argument 4283 // from checkCall. 4284 // FIXME: Our choice of AST representation here is less than ideal. 4285 ImplicitThis = Args[0]; 4286 ++Args; 4287 --NumArgs; 4288 } else if (IsMemberFunction) 4289 ImplicitThis = 4290 cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument(); 4291 4292 checkCall(FDecl, Proto, ImplicitThis, llvm::makeArrayRef(Args, NumArgs), 4293 IsMemberFunction, TheCall->getRParenLoc(), 4294 TheCall->getCallee()->getSourceRange(), CallType); 4295 4296 IdentifierInfo *FnInfo = FDecl->getIdentifier(); 4297 // None of the checks below are needed for functions that don't have 4298 // simple names (e.g., C++ conversion functions). 4299 if (!FnInfo) 4300 return false; 4301 4302 CheckAbsoluteValueFunction(TheCall, FDecl); 4303 CheckMaxUnsignedZero(TheCall, FDecl); 4304 4305 if (getLangOpts().ObjC) 4306 DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs); 4307 4308 unsigned CMId = FDecl->getMemoryFunctionKind(); 4309 if (CMId == 0) 4310 return false; 4311 4312 // Handle memory setting and copying functions. 4313 if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) 4314 CheckStrlcpycatArguments(TheCall, FnInfo); 4315 else if (CMId == Builtin::BIstrncat) 4316 CheckStrncatArguments(TheCall, FnInfo); 4317 else 4318 CheckMemaccessArguments(TheCall, CMId, FnInfo); 4319 4320 return false; 4321 } 4322 4323 bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, 4324 ArrayRef<const Expr *> Args) { 4325 VariadicCallType CallType = 4326 Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; 4327 4328 checkCall(Method, nullptr, /*ThisArg=*/nullptr, Args, 4329 /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(), 4330 CallType); 4331 4332 return false; 4333 } 4334 4335 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, 4336 const FunctionProtoType *Proto) { 4337 QualType Ty; 4338 if (const auto *V = dyn_cast<VarDecl>(NDecl)) 4339 Ty = V->getType().getNonReferenceType(); 4340 else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) 4341 Ty = F->getType().getNonReferenceType(); 4342 else 4343 return false; 4344 4345 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() && 4346 !Ty->isFunctionProtoType()) 4347 return false; 4348 4349 VariadicCallType CallType; 4350 if (!Proto || !Proto->isVariadic()) { 4351 CallType = VariadicDoesNotApply; 4352 } else if (Ty->isBlockPointerType()) { 4353 CallType = VariadicBlock; 4354 } else { // Ty->isFunctionPointerType() 4355 CallType = VariadicFunction; 4356 } 4357 4358 checkCall(NDecl, Proto, /*ThisArg=*/nullptr, 4359 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 4360 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 4361 TheCall->getCallee()->getSourceRange(), CallType); 4362 4363 return false; 4364 } 4365 4366 /// Checks function calls when a FunctionDecl or a NamedDecl is not available, 4367 /// such as function pointers returned from functions. 4368 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { 4369 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, 4370 TheCall->getCallee()); 4371 checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr, 4372 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 4373 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 4374 TheCall->getCallee()->getSourceRange(), CallType); 4375 4376 return false; 4377 } 4378 4379 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { 4380 if (!llvm::isValidAtomicOrderingCABI(Ordering)) 4381 return false; 4382 4383 auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering; 4384 switch (Op) { 4385 case AtomicExpr::AO__c11_atomic_init: 4386 case AtomicExpr::AO__opencl_atomic_init: 4387 llvm_unreachable("There is no ordering argument for an init"); 4388 4389 case AtomicExpr::AO__c11_atomic_load: 4390 case AtomicExpr::AO__opencl_atomic_load: 4391 case AtomicExpr::AO__atomic_load_n: 4392 case AtomicExpr::AO__atomic_load: 4393 return OrderingCABI != llvm::AtomicOrderingCABI::release && 4394 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 4395 4396 case AtomicExpr::AO__c11_atomic_store: 4397 case AtomicExpr::AO__opencl_atomic_store: 4398 case AtomicExpr::AO__atomic_store: 4399 case AtomicExpr::AO__atomic_store_n: 4400 return OrderingCABI != llvm::AtomicOrderingCABI::consume && 4401 OrderingCABI != llvm::AtomicOrderingCABI::acquire && 4402 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 4403 4404 default: 4405 return true; 4406 } 4407 } 4408 4409 ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, 4410 AtomicExpr::AtomicOp Op) { 4411 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); 4412 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 4413 4414 // All the non-OpenCL operations take one of the following forms. 4415 // The OpenCL operations take the __c11 forms with one extra argument for 4416 // synchronization scope. 4417 enum { 4418 // C __c11_atomic_init(A *, C) 4419 Init, 4420 4421 // C __c11_atomic_load(A *, int) 4422 Load, 4423 4424 // void __atomic_load(A *, CP, int) 4425 LoadCopy, 4426 4427 // void __atomic_store(A *, CP, int) 4428 Copy, 4429 4430 // C __c11_atomic_add(A *, M, int) 4431 Arithmetic, 4432 4433 // C __atomic_exchange_n(A *, CP, int) 4434 Xchg, 4435 4436 // void __atomic_exchange(A *, C *, CP, int) 4437 GNUXchg, 4438 4439 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) 4440 C11CmpXchg, 4441 4442 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) 4443 GNUCmpXchg 4444 } Form = Init; 4445 4446 const unsigned NumForm = GNUCmpXchg + 1; 4447 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 }; 4448 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 }; 4449 // where: 4450 // C is an appropriate type, 4451 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, 4452 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, 4453 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and 4454 // the int parameters are for orderings. 4455 4456 static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm 4457 && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm, 4458 "need to update code for modified forms"); 4459 static_assert(AtomicExpr::AO__c11_atomic_init == 0 && 4460 AtomicExpr::AO__c11_atomic_fetch_xor + 1 == 4461 AtomicExpr::AO__atomic_load, 4462 "need to update code for modified C11 atomics"); 4463 bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_init && 4464 Op <= AtomicExpr::AO__opencl_atomic_fetch_max; 4465 bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_init && 4466 Op <= AtomicExpr::AO__c11_atomic_fetch_xor) || 4467 IsOpenCL; 4468 bool IsN = Op == AtomicExpr::AO__atomic_load_n || 4469 Op == AtomicExpr::AO__atomic_store_n || 4470 Op == AtomicExpr::AO__atomic_exchange_n || 4471 Op == AtomicExpr::AO__atomic_compare_exchange_n; 4472 bool IsAddSub = false; 4473 bool IsMinMax = false; 4474 4475 switch (Op) { 4476 case AtomicExpr::AO__c11_atomic_init: 4477 case AtomicExpr::AO__opencl_atomic_init: 4478 Form = Init; 4479 break; 4480 4481 case AtomicExpr::AO__c11_atomic_load: 4482 case AtomicExpr::AO__opencl_atomic_load: 4483 case AtomicExpr::AO__atomic_load_n: 4484 Form = Load; 4485 break; 4486 4487 case AtomicExpr::AO__atomic_load: 4488 Form = LoadCopy; 4489 break; 4490 4491 case AtomicExpr::AO__c11_atomic_store: 4492 case AtomicExpr::AO__opencl_atomic_store: 4493 case AtomicExpr::AO__atomic_store: 4494 case AtomicExpr::AO__atomic_store_n: 4495 Form = Copy; 4496 break; 4497 4498 case AtomicExpr::AO__c11_atomic_fetch_add: 4499 case AtomicExpr::AO__c11_atomic_fetch_sub: 4500 case AtomicExpr::AO__opencl_atomic_fetch_add: 4501 case AtomicExpr::AO__opencl_atomic_fetch_sub: 4502 case AtomicExpr::AO__opencl_atomic_fetch_min: 4503 case AtomicExpr::AO__opencl_atomic_fetch_max: 4504 case AtomicExpr::AO__atomic_fetch_add: 4505 case AtomicExpr::AO__atomic_fetch_sub: 4506 case AtomicExpr::AO__atomic_add_fetch: 4507 case AtomicExpr::AO__atomic_sub_fetch: 4508 IsAddSub = true; 4509 LLVM_FALLTHROUGH; 4510 case AtomicExpr::AO__c11_atomic_fetch_and: 4511 case AtomicExpr::AO__c11_atomic_fetch_or: 4512 case AtomicExpr::AO__c11_atomic_fetch_xor: 4513 case AtomicExpr::AO__opencl_atomic_fetch_and: 4514 case AtomicExpr::AO__opencl_atomic_fetch_or: 4515 case AtomicExpr::AO__opencl_atomic_fetch_xor: 4516 case AtomicExpr::AO__atomic_fetch_and: 4517 case AtomicExpr::AO__atomic_fetch_or: 4518 case AtomicExpr::AO__atomic_fetch_xor: 4519 case AtomicExpr::AO__atomic_fetch_nand: 4520 case AtomicExpr::AO__atomic_and_fetch: 4521 case AtomicExpr::AO__atomic_or_fetch: 4522 case AtomicExpr::AO__atomic_xor_fetch: 4523 case AtomicExpr::AO__atomic_nand_fetch: 4524 Form = Arithmetic; 4525 break; 4526 4527 case AtomicExpr::AO__atomic_fetch_min: 4528 case AtomicExpr::AO__atomic_fetch_max: 4529 IsMinMax = true; 4530 Form = Arithmetic; 4531 break; 4532 4533 case AtomicExpr::AO__c11_atomic_exchange: 4534 case AtomicExpr::AO__opencl_atomic_exchange: 4535 case AtomicExpr::AO__atomic_exchange_n: 4536 Form = Xchg; 4537 break; 4538 4539 case AtomicExpr::AO__atomic_exchange: 4540 Form = GNUXchg; 4541 break; 4542 4543 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 4544 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 4545 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong: 4546 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak: 4547 Form = C11CmpXchg; 4548 break; 4549 4550 case AtomicExpr::AO__atomic_compare_exchange: 4551 case AtomicExpr::AO__atomic_compare_exchange_n: 4552 Form = GNUCmpXchg; 4553 break; 4554 } 4555 4556 unsigned AdjustedNumArgs = NumArgs[Form]; 4557 if (IsOpenCL && Op != AtomicExpr::AO__opencl_atomic_init) 4558 ++AdjustedNumArgs; 4559 // Check we have the right number of arguments. 4560 if (TheCall->getNumArgs() < AdjustedNumArgs) { 4561 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 4562 << 0 << AdjustedNumArgs << TheCall->getNumArgs() 4563 << TheCall->getCallee()->getSourceRange(); 4564 return ExprError(); 4565 } else if (TheCall->getNumArgs() > AdjustedNumArgs) { 4566 Diag(TheCall->getArg(AdjustedNumArgs)->getBeginLoc(), 4567 diag::err_typecheck_call_too_many_args) 4568 << 0 << AdjustedNumArgs << TheCall->getNumArgs() 4569 << TheCall->getCallee()->getSourceRange(); 4570 return ExprError(); 4571 } 4572 4573 // Inspect the first argument of the atomic operation. 4574 Expr *Ptr = TheCall->getArg(0); 4575 ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr); 4576 if (ConvertedPtr.isInvalid()) 4577 return ExprError(); 4578 4579 Ptr = ConvertedPtr.get(); 4580 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); 4581 if (!pointerType) { 4582 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) 4583 << Ptr->getType() << Ptr->getSourceRange(); 4584 return ExprError(); 4585 } 4586 4587 // For a __c11 builtin, this should be a pointer to an _Atomic type. 4588 QualType AtomTy = pointerType->getPointeeType(); // 'A' 4589 QualType ValType = AtomTy; // 'C' 4590 if (IsC11) { 4591 if (!AtomTy->isAtomicType()) { 4592 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_atomic) 4593 << Ptr->getType() << Ptr->getSourceRange(); 4594 return ExprError(); 4595 } 4596 if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) || 4597 AtomTy.getAddressSpace() == LangAS::opencl_constant) { 4598 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_non_const_atomic) 4599 << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType() 4600 << Ptr->getSourceRange(); 4601 return ExprError(); 4602 } 4603 ValType = AtomTy->getAs<AtomicType>()->getValueType(); 4604 } else if (Form != Load && Form != LoadCopy) { 4605 if (ValType.isConstQualified()) { 4606 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_non_const_pointer) 4607 << Ptr->getType() << Ptr->getSourceRange(); 4608 return ExprError(); 4609 } 4610 } 4611 4612 // For an arithmetic operation, the implied arithmetic must be well-formed. 4613 if (Form == Arithmetic) { 4614 // gcc does not enforce these rules for GNU atomics, but we do so for sanity. 4615 if (IsAddSub && !ValType->isIntegerType() 4616 && !ValType->isPointerType()) { 4617 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_atomic_int_or_ptr) 4618 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 4619 return ExprError(); 4620 } 4621 if (IsMinMax) { 4622 const BuiltinType *BT = ValType->getAs<BuiltinType>(); 4623 if (!BT || (BT->getKind() != BuiltinType::Int && 4624 BT->getKind() != BuiltinType::UInt)) { 4625 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_int32_or_ptr); 4626 return ExprError(); 4627 } 4628 } 4629 if (!IsAddSub && !IsMinMax && !ValType->isIntegerType()) { 4630 Diag(DRE->getBeginLoc(), diag::err_atomic_op_bitwise_needs_atomic_int) 4631 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 4632 return ExprError(); 4633 } 4634 if (IsC11 && ValType->isPointerType() && 4635 RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(), 4636 diag::err_incomplete_type)) { 4637 return ExprError(); 4638 } 4639 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { 4640 // For __atomic_*_n operations, the value type must be a scalar integral or 4641 // pointer type which is 1, 2, 4, 8 or 16 bytes in length. 4642 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_atomic_int_or_ptr) 4643 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 4644 return ExprError(); 4645 } 4646 4647 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && 4648 !AtomTy->isScalarType()) { 4649 // For GNU atomics, require a trivially-copyable type. This is not part of 4650 // the GNU atomics specification, but we enforce it for sanity. 4651 Diag(DRE->getBeginLoc(), diag::err_atomic_op_needs_trivial_copy) 4652 << Ptr->getType() << Ptr->getSourceRange(); 4653 return ExprError(); 4654 } 4655 4656 switch (ValType.getObjCLifetime()) { 4657 case Qualifiers::OCL_None: 4658 case Qualifiers::OCL_ExplicitNone: 4659 // okay 4660 break; 4661 4662 case Qualifiers::OCL_Weak: 4663 case Qualifiers::OCL_Strong: 4664 case Qualifiers::OCL_Autoreleasing: 4665 // FIXME: Can this happen? By this point, ValType should be known 4666 // to be trivially copyable. 4667 Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) 4668 << ValType << Ptr->getSourceRange(); 4669 return ExprError(); 4670 } 4671 4672 // All atomic operations have an overload which takes a pointer to a volatile 4673 // 'A'. We shouldn't let the volatile-ness of the pointee-type inject itself 4674 // into the result or the other operands. Similarly atomic_load takes a 4675 // pointer to a const 'A'. 4676 ValType.removeLocalVolatile(); 4677 ValType.removeLocalConst(); 4678 QualType ResultType = ValType; 4679 if (Form == Copy || Form == LoadCopy || Form == GNUXchg || 4680 Form == Init) 4681 ResultType = Context.VoidTy; 4682 else if (Form == C11CmpXchg || Form == GNUCmpXchg) 4683 ResultType = Context.BoolTy; 4684 4685 // The type of a parameter passed 'by value'. In the GNU atomics, such 4686 // arguments are actually passed as pointers. 4687 QualType ByValType = ValType; // 'CP' 4688 bool IsPassedByAddress = false; 4689 if (!IsC11 && !IsN) { 4690 ByValType = Ptr->getType(); 4691 IsPassedByAddress = true; 4692 } 4693 4694 // The first argument's non-CV pointer type is used to deduce the type of 4695 // subsequent arguments, except for: 4696 // - weak flag (always converted to bool) 4697 // - memory order (always converted to int) 4698 // - scope (always converted to int) 4699 for (unsigned i = 0; i != TheCall->getNumArgs(); ++i) { 4700 QualType Ty; 4701 if (i < NumVals[Form] + 1) { 4702 switch (i) { 4703 case 0: 4704 // The first argument is always a pointer. It has a fixed type. 4705 // It is always dereferenced, a nullptr is undefined. 4706 CheckNonNullArgument(*this, TheCall->getArg(i), DRE->getBeginLoc()); 4707 // Nothing else to do: we already know all we want about this pointer. 4708 continue; 4709 case 1: 4710 // The second argument is the non-atomic operand. For arithmetic, this 4711 // is always passed by value, and for a compare_exchange it is always 4712 // passed by address. For the rest, GNU uses by-address and C11 uses 4713 // by-value. 4714 assert(Form != Load); 4715 if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) 4716 Ty = ValType; 4717 else if (Form == Copy || Form == Xchg) { 4718 if (IsPassedByAddress) 4719 // The value pointer is always dereferenced, a nullptr is undefined. 4720 CheckNonNullArgument(*this, TheCall->getArg(i), DRE->getBeginLoc()); 4721 Ty = ByValType; 4722 } else if (Form == Arithmetic) 4723 Ty = Context.getPointerDiffType(); 4724 else { 4725 Expr *ValArg = TheCall->getArg(i); 4726 // The value pointer is always dereferenced, a nullptr is undefined. 4727 CheckNonNullArgument(*this, ValArg, DRE->getBeginLoc()); 4728 LangAS AS = LangAS::Default; 4729 // Keep address space of non-atomic pointer type. 4730 if (const PointerType *PtrTy = 4731 ValArg->getType()->getAs<PointerType>()) { 4732 AS = PtrTy->getPointeeType().getAddressSpace(); 4733 } 4734 Ty = Context.getPointerType( 4735 Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); 4736 } 4737 break; 4738 case 2: 4739 // The third argument to compare_exchange / GNU exchange is the desired 4740 // value, either by-value (for the C11 and *_n variant) or as a pointer. 4741 if (IsPassedByAddress) 4742 CheckNonNullArgument(*this, TheCall->getArg(i), DRE->getBeginLoc()); 4743 Ty = ByValType; 4744 break; 4745 case 3: 4746 // The fourth argument to GNU compare_exchange is a 'weak' flag. 4747 Ty = Context.BoolTy; 4748 break; 4749 } 4750 } else { 4751 // The order(s) and scope are always converted to int. 4752 Ty = Context.IntTy; 4753 } 4754 4755 InitializedEntity Entity = 4756 InitializedEntity::InitializeParameter(Context, Ty, false); 4757 ExprResult Arg = TheCall->getArg(i); 4758 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4759 if (Arg.isInvalid()) 4760 return true; 4761 TheCall->setArg(i, Arg.get()); 4762 } 4763 4764 // Permute the arguments into a 'consistent' order. 4765 SmallVector<Expr*, 5> SubExprs; 4766 SubExprs.push_back(Ptr); 4767 switch (Form) { 4768 case Init: 4769 // Note, AtomicExpr::getVal1() has a special case for this atomic. 4770 SubExprs.push_back(TheCall->getArg(1)); // Val1 4771 break; 4772 case Load: 4773 SubExprs.push_back(TheCall->getArg(1)); // Order 4774 break; 4775 case LoadCopy: 4776 case Copy: 4777 case Arithmetic: 4778 case Xchg: 4779 SubExprs.push_back(TheCall->getArg(2)); // Order 4780 SubExprs.push_back(TheCall->getArg(1)); // Val1 4781 break; 4782 case GNUXchg: 4783 // Note, AtomicExpr::getVal2() has a special case for this atomic. 4784 SubExprs.push_back(TheCall->getArg(3)); // Order 4785 SubExprs.push_back(TheCall->getArg(1)); // Val1 4786 SubExprs.push_back(TheCall->getArg(2)); // Val2 4787 break; 4788 case C11CmpXchg: 4789 SubExprs.push_back(TheCall->getArg(3)); // Order 4790 SubExprs.push_back(TheCall->getArg(1)); // Val1 4791 SubExprs.push_back(TheCall->getArg(4)); // OrderFail 4792 SubExprs.push_back(TheCall->getArg(2)); // Val2 4793 break; 4794 case GNUCmpXchg: 4795 SubExprs.push_back(TheCall->getArg(4)); // Order 4796 SubExprs.push_back(TheCall->getArg(1)); // Val1 4797 SubExprs.push_back(TheCall->getArg(5)); // OrderFail 4798 SubExprs.push_back(TheCall->getArg(2)); // Val2 4799 SubExprs.push_back(TheCall->getArg(3)); // Weak 4800 break; 4801 } 4802 4803 if (SubExprs.size() >= 2 && Form != Init) { 4804 llvm::APSInt Result(32); 4805 if (SubExprs[1]->isIntegerConstantExpr(Result, Context) && 4806 !isValidOrderingForOp(Result.getSExtValue(), Op)) 4807 Diag(SubExprs[1]->getBeginLoc(), 4808 diag::warn_atomic_op_has_invalid_memory_order) 4809 << SubExprs[1]->getSourceRange(); 4810 } 4811 4812 if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { 4813 auto *Scope = TheCall->getArg(TheCall->getNumArgs() - 1); 4814 llvm::APSInt Result(32); 4815 if (Scope->isIntegerConstantExpr(Result, Context) && 4816 !ScopeModel->isValid(Result.getZExtValue())) { 4817 Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) 4818 << Scope->getSourceRange(); 4819 } 4820 SubExprs.push_back(Scope); 4821 } 4822 4823 AtomicExpr *AE = 4824 new (Context) AtomicExpr(TheCall->getCallee()->getBeginLoc(), SubExprs, 4825 ResultType, Op, TheCall->getRParenLoc()); 4826 4827 if ((Op == AtomicExpr::AO__c11_atomic_load || 4828 Op == AtomicExpr::AO__c11_atomic_store || 4829 Op == AtomicExpr::AO__opencl_atomic_load || 4830 Op == AtomicExpr::AO__opencl_atomic_store ) && 4831 Context.AtomicUsesUnsupportedLibcall(AE)) 4832 Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib) 4833 << ((Op == AtomicExpr::AO__c11_atomic_load || 4834 Op == AtomicExpr::AO__opencl_atomic_load) 4835 ? 0 4836 : 1); 4837 4838 return AE; 4839 } 4840 4841 /// checkBuiltinArgument - Given a call to a builtin function, perform 4842 /// normal type-checking on the given argument, updating the call in 4843 /// place. This is useful when a builtin function requires custom 4844 /// type-checking for some of its arguments but not necessarily all of 4845 /// them. 4846 /// 4847 /// Returns true on error. 4848 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { 4849 FunctionDecl *Fn = E->getDirectCallee(); 4850 assert(Fn && "builtin call without direct callee!"); 4851 4852 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); 4853 InitializedEntity Entity = 4854 InitializedEntity::InitializeParameter(S.Context, Param); 4855 4856 ExprResult Arg = E->getArg(0); 4857 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 4858 if (Arg.isInvalid()) 4859 return true; 4860 4861 E->setArg(ArgIndex, Arg.get()); 4862 return false; 4863 } 4864 4865 /// We have a call to a function like __sync_fetch_and_add, which is an 4866 /// overloaded function based on the pointer type of its first argument. 4867 /// The main ActOnCallExpr routines have already promoted the types of 4868 /// arguments because all of these calls are prototyped as void(...). 4869 /// 4870 /// This function goes through and does final semantic checking for these 4871 /// builtins, as well as generating any warnings. 4872 ExprResult 4873 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { 4874 CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get()); 4875 Expr *Callee = TheCall->getCallee(); 4876 DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts()); 4877 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4878 4879 // Ensure that we have at least one argument to do type inference from. 4880 if (TheCall->getNumArgs() < 1) { 4881 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4882 << 0 << 1 << TheCall->getNumArgs() << Callee->getSourceRange(); 4883 return ExprError(); 4884 } 4885 4886 // Inspect the first argument of the atomic builtin. This should always be 4887 // a pointer type, whose element is an integral scalar or pointer type. 4888 // Because it is a pointer type, we don't have to worry about any implicit 4889 // casts here. 4890 // FIXME: We don't allow floating point scalars as input. 4891 Expr *FirstArg = TheCall->getArg(0); 4892 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); 4893 if (FirstArgResult.isInvalid()) 4894 return ExprError(); 4895 FirstArg = FirstArgResult.get(); 4896 TheCall->setArg(0, FirstArg); 4897 4898 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); 4899 if (!pointerType) { 4900 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) 4901 << FirstArg->getType() << FirstArg->getSourceRange(); 4902 return ExprError(); 4903 } 4904 4905 QualType ValType = pointerType->getPointeeType(); 4906 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4907 !ValType->isBlockPointerType()) { 4908 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr) 4909 << FirstArg->getType() << FirstArg->getSourceRange(); 4910 return ExprError(); 4911 } 4912 4913 if (ValType.isConstQualified()) { 4914 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const) 4915 << FirstArg->getType() << FirstArg->getSourceRange(); 4916 return ExprError(); 4917 } 4918 4919 switch (ValType.getObjCLifetime()) { 4920 case Qualifiers::OCL_None: 4921 case Qualifiers::OCL_ExplicitNone: 4922 // okay 4923 break; 4924 4925 case Qualifiers::OCL_Weak: 4926 case Qualifiers::OCL_Strong: 4927 case Qualifiers::OCL_Autoreleasing: 4928 Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) 4929 << ValType << FirstArg->getSourceRange(); 4930 return ExprError(); 4931 } 4932 4933 // Strip any qualifiers off ValType. 4934 ValType = ValType.getUnqualifiedType(); 4935 4936 // The majority of builtins return a value, but a few have special return 4937 // types, so allow them to override appropriately below. 4938 QualType ResultType = ValType; 4939 4940 // We need to figure out which concrete builtin this maps onto. For example, 4941 // __sync_fetch_and_add with a 2 byte object turns into 4942 // __sync_fetch_and_add_2. 4943 #define BUILTIN_ROW(x) \ 4944 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ 4945 Builtin::BI##x##_8, Builtin::BI##x##_16 } 4946 4947 static const unsigned BuiltinIndices[][5] = { 4948 BUILTIN_ROW(__sync_fetch_and_add), 4949 BUILTIN_ROW(__sync_fetch_and_sub), 4950 BUILTIN_ROW(__sync_fetch_and_or), 4951 BUILTIN_ROW(__sync_fetch_and_and), 4952 BUILTIN_ROW(__sync_fetch_and_xor), 4953 BUILTIN_ROW(__sync_fetch_and_nand), 4954 4955 BUILTIN_ROW(__sync_add_and_fetch), 4956 BUILTIN_ROW(__sync_sub_and_fetch), 4957 BUILTIN_ROW(__sync_and_and_fetch), 4958 BUILTIN_ROW(__sync_or_and_fetch), 4959 BUILTIN_ROW(__sync_xor_and_fetch), 4960 BUILTIN_ROW(__sync_nand_and_fetch), 4961 4962 BUILTIN_ROW(__sync_val_compare_and_swap), 4963 BUILTIN_ROW(__sync_bool_compare_and_swap), 4964 BUILTIN_ROW(__sync_lock_test_and_set), 4965 BUILTIN_ROW(__sync_lock_release), 4966 BUILTIN_ROW(__sync_swap) 4967 }; 4968 #undef BUILTIN_ROW 4969 4970 // Determine the index of the size. 4971 unsigned SizeIndex; 4972 switch (Context.getTypeSizeInChars(ValType).getQuantity()) { 4973 case 1: SizeIndex = 0; break; 4974 case 2: SizeIndex = 1; break; 4975 case 4: SizeIndex = 2; break; 4976 case 8: SizeIndex = 3; break; 4977 case 16: SizeIndex = 4; break; 4978 default: 4979 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size) 4980 << FirstArg->getType() << FirstArg->getSourceRange(); 4981 return ExprError(); 4982 } 4983 4984 // Each of these builtins has one pointer argument, followed by some number of 4985 // values (0, 1 or 2) followed by a potentially empty varags list of stuff 4986 // that we ignore. Find out which row of BuiltinIndices to read from as well 4987 // as the number of fixed args. 4988 unsigned BuiltinID = FDecl->getBuiltinID(); 4989 unsigned BuiltinIndex, NumFixed = 1; 4990 bool WarnAboutSemanticsChange = false; 4991 switch (BuiltinID) { 4992 default: llvm_unreachable("Unknown overloaded atomic builtin!"); 4993 case Builtin::BI__sync_fetch_and_add: 4994 case Builtin::BI__sync_fetch_and_add_1: 4995 case Builtin::BI__sync_fetch_and_add_2: 4996 case Builtin::BI__sync_fetch_and_add_4: 4997 case Builtin::BI__sync_fetch_and_add_8: 4998 case Builtin::BI__sync_fetch_and_add_16: 4999 BuiltinIndex = 0; 5000 break; 5001 5002 case Builtin::BI__sync_fetch_and_sub: 5003 case Builtin::BI__sync_fetch_and_sub_1: 5004 case Builtin::BI__sync_fetch_and_sub_2: 5005 case Builtin::BI__sync_fetch_and_sub_4: 5006 case Builtin::BI__sync_fetch_and_sub_8: 5007 case Builtin::BI__sync_fetch_and_sub_16: 5008 BuiltinIndex = 1; 5009 break; 5010 5011 case Builtin::BI__sync_fetch_and_or: 5012 case Builtin::BI__sync_fetch_and_or_1: 5013 case Builtin::BI__sync_fetch_and_or_2: 5014 case Builtin::BI__sync_fetch_and_or_4: 5015 case Builtin::BI__sync_fetch_and_or_8: 5016 case Builtin::BI__sync_fetch_and_or_16: 5017 BuiltinIndex = 2; 5018 break; 5019 5020 case Builtin::BI__sync_fetch_and_and: 5021 case Builtin::BI__sync_fetch_and_and_1: 5022 case Builtin::BI__sync_fetch_and_and_2: 5023 case Builtin::BI__sync_fetch_and_and_4: 5024 case Builtin::BI__sync_fetch_and_and_8: 5025 case Builtin::BI__sync_fetch_and_and_16: 5026 BuiltinIndex = 3; 5027 break; 5028 5029 case Builtin::BI__sync_fetch_and_xor: 5030 case Builtin::BI__sync_fetch_and_xor_1: 5031 case Builtin::BI__sync_fetch_and_xor_2: 5032 case Builtin::BI__sync_fetch_and_xor_4: 5033 case Builtin::BI__sync_fetch_and_xor_8: 5034 case Builtin::BI__sync_fetch_and_xor_16: 5035 BuiltinIndex = 4; 5036 break; 5037 5038 case Builtin::BI__sync_fetch_and_nand: 5039 case Builtin::BI__sync_fetch_and_nand_1: 5040 case Builtin::BI__sync_fetch_and_nand_2: 5041 case Builtin::BI__sync_fetch_and_nand_4: 5042 case Builtin::BI__sync_fetch_and_nand_8: 5043 case Builtin::BI__sync_fetch_and_nand_16: 5044 BuiltinIndex = 5; 5045 WarnAboutSemanticsChange = true; 5046 break; 5047 5048 case Builtin::BI__sync_add_and_fetch: 5049 case Builtin::BI__sync_add_and_fetch_1: 5050 case Builtin::BI__sync_add_and_fetch_2: 5051 case Builtin::BI__sync_add_and_fetch_4: 5052 case Builtin::BI__sync_add_and_fetch_8: 5053 case Builtin::BI__sync_add_and_fetch_16: 5054 BuiltinIndex = 6; 5055 break; 5056 5057 case Builtin::BI__sync_sub_and_fetch: 5058 case Builtin::BI__sync_sub_and_fetch_1: 5059 case Builtin::BI__sync_sub_and_fetch_2: 5060 case Builtin::BI__sync_sub_and_fetch_4: 5061 case Builtin::BI__sync_sub_and_fetch_8: 5062 case Builtin::BI__sync_sub_and_fetch_16: 5063 BuiltinIndex = 7; 5064 break; 5065 5066 case Builtin::BI__sync_and_and_fetch: 5067 case Builtin::BI__sync_and_and_fetch_1: 5068 case Builtin::BI__sync_and_and_fetch_2: 5069 case Builtin::BI__sync_and_and_fetch_4: 5070 case Builtin::BI__sync_and_and_fetch_8: 5071 case Builtin::BI__sync_and_and_fetch_16: 5072 BuiltinIndex = 8; 5073 break; 5074 5075 case Builtin::BI__sync_or_and_fetch: 5076 case Builtin::BI__sync_or_and_fetch_1: 5077 case Builtin::BI__sync_or_and_fetch_2: 5078 case Builtin::BI__sync_or_and_fetch_4: 5079 case Builtin::BI__sync_or_and_fetch_8: 5080 case Builtin::BI__sync_or_and_fetch_16: 5081 BuiltinIndex = 9; 5082 break; 5083 5084 case Builtin::BI__sync_xor_and_fetch: 5085 case Builtin::BI__sync_xor_and_fetch_1: 5086 case Builtin::BI__sync_xor_and_fetch_2: 5087 case Builtin::BI__sync_xor_and_fetch_4: 5088 case Builtin::BI__sync_xor_and_fetch_8: 5089 case Builtin::BI__sync_xor_and_fetch_16: 5090 BuiltinIndex = 10; 5091 break; 5092 5093 case Builtin::BI__sync_nand_and_fetch: 5094 case Builtin::BI__sync_nand_and_fetch_1: 5095 case Builtin::BI__sync_nand_and_fetch_2: 5096 case Builtin::BI__sync_nand_and_fetch_4: 5097 case Builtin::BI__sync_nand_and_fetch_8: 5098 case Builtin::BI__sync_nand_and_fetch_16: 5099 BuiltinIndex = 11; 5100 WarnAboutSemanticsChange = true; 5101 break; 5102 5103 case Builtin::BI__sync_val_compare_and_swap: 5104 case Builtin::BI__sync_val_compare_and_swap_1: 5105 case Builtin::BI__sync_val_compare_and_swap_2: 5106 case Builtin::BI__sync_val_compare_and_swap_4: 5107 case Builtin::BI__sync_val_compare_and_swap_8: 5108 case Builtin::BI__sync_val_compare_and_swap_16: 5109 BuiltinIndex = 12; 5110 NumFixed = 2; 5111 break; 5112 5113 case Builtin::BI__sync_bool_compare_and_swap: 5114 case Builtin::BI__sync_bool_compare_and_swap_1: 5115 case Builtin::BI__sync_bool_compare_and_swap_2: 5116 case Builtin::BI__sync_bool_compare_and_swap_4: 5117 case Builtin::BI__sync_bool_compare_and_swap_8: 5118 case Builtin::BI__sync_bool_compare_and_swap_16: 5119 BuiltinIndex = 13; 5120 NumFixed = 2; 5121 ResultType = Context.BoolTy; 5122 break; 5123 5124 case Builtin::BI__sync_lock_test_and_set: 5125 case Builtin::BI__sync_lock_test_and_set_1: 5126 case Builtin::BI__sync_lock_test_and_set_2: 5127 case Builtin::BI__sync_lock_test_and_set_4: 5128 case Builtin::BI__sync_lock_test_and_set_8: 5129 case Builtin::BI__sync_lock_test_and_set_16: 5130 BuiltinIndex = 14; 5131 break; 5132 5133 case Builtin::BI__sync_lock_release: 5134 case Builtin::BI__sync_lock_release_1: 5135 case Builtin::BI__sync_lock_release_2: 5136 case Builtin::BI__sync_lock_release_4: 5137 case Builtin::BI__sync_lock_release_8: 5138 case Builtin::BI__sync_lock_release_16: 5139 BuiltinIndex = 15; 5140 NumFixed = 0; 5141 ResultType = Context.VoidTy; 5142 break; 5143 5144 case Builtin::BI__sync_swap: 5145 case Builtin::BI__sync_swap_1: 5146 case Builtin::BI__sync_swap_2: 5147 case Builtin::BI__sync_swap_4: 5148 case Builtin::BI__sync_swap_8: 5149 case Builtin::BI__sync_swap_16: 5150 BuiltinIndex = 16; 5151 break; 5152 } 5153 5154 // Now that we know how many fixed arguments we expect, first check that we 5155 // have at least that many. 5156 if (TheCall->getNumArgs() < 1+NumFixed) { 5157 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 5158 << 0 << 1 + NumFixed << TheCall->getNumArgs() 5159 << Callee->getSourceRange(); 5160 return ExprError(); 5161 } 5162 5163 Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst) 5164 << Callee->getSourceRange(); 5165 5166 if (WarnAboutSemanticsChange) { 5167 Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change) 5168 << Callee->getSourceRange(); 5169 } 5170 5171 // Get the decl for the concrete builtin from this, we can tell what the 5172 // concrete integer type we should convert to is. 5173 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; 5174 const char *NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID); 5175 FunctionDecl *NewBuiltinDecl; 5176 if (NewBuiltinID == BuiltinID) 5177 NewBuiltinDecl = FDecl; 5178 else { 5179 // Perform builtin lookup to avoid redeclaring it. 5180 DeclarationName DN(&Context.Idents.get(NewBuiltinName)); 5181 LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName); 5182 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); 5183 assert(Res.getFoundDecl()); 5184 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); 5185 if (!NewBuiltinDecl) 5186 return ExprError(); 5187 } 5188 5189 // The first argument --- the pointer --- has a fixed type; we 5190 // deduce the types of the rest of the arguments accordingly. Walk 5191 // the remaining arguments, converting them to the deduced value type. 5192 for (unsigned i = 0; i != NumFixed; ++i) { 5193 ExprResult Arg = TheCall->getArg(i+1); 5194 5195 // GCC does an implicit conversion to the pointer or integer ValType. This 5196 // can fail in some cases (1i -> int**), check for this error case now. 5197 // Initialize the argument. 5198 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 5199 ValType, /*consume*/ false); 5200 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 5201 if (Arg.isInvalid()) 5202 return ExprError(); 5203 5204 // Okay, we have something that *can* be converted to the right type. Check 5205 // to see if there is a potentially weird extension going on here. This can 5206 // happen when you do an atomic operation on something like an char* and 5207 // pass in 42. The 42 gets converted to char. This is even more strange 5208 // for things like 45.123 -> char, etc. 5209 // FIXME: Do this check. 5210 TheCall->setArg(i+1, Arg.get()); 5211 } 5212 5213 // Create a new DeclRefExpr to refer to the new decl. 5214 DeclRefExpr* NewDRE = DeclRefExpr::Create( 5215 Context, 5216 DRE->getQualifierLoc(), 5217 SourceLocation(), 5218 NewBuiltinDecl, 5219 /*enclosing*/ false, 5220 DRE->getLocation(), 5221 Context.BuiltinFnTy, 5222 DRE->getValueKind()); 5223 5224 // Set the callee in the CallExpr. 5225 // FIXME: This loses syntactic information. 5226 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); 5227 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, 5228 CK_BuiltinFnToFnPtr); 5229 TheCall->setCallee(PromotedCall.get()); 5230 5231 // Change the result type of the call to match the original value type. This 5232 // is arbitrary, but the codegen for these builtins ins design to handle it 5233 // gracefully. 5234 TheCall->setType(ResultType); 5235 5236 return TheCallResult; 5237 } 5238 5239 /// SemaBuiltinNontemporalOverloaded - We have a call to 5240 /// __builtin_nontemporal_store or __builtin_nontemporal_load, which is an 5241 /// overloaded function based on the pointer type of its last argument. 5242 /// 5243 /// This function goes through and does final semantic checking for these 5244 /// builtins. 5245 ExprResult Sema::SemaBuiltinNontemporalOverloaded(ExprResult TheCallResult) { 5246 CallExpr *TheCall = (CallExpr *)TheCallResult.get(); 5247 DeclRefExpr *DRE = 5248 cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 5249 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 5250 unsigned BuiltinID = FDecl->getBuiltinID(); 5251 assert((BuiltinID == Builtin::BI__builtin_nontemporal_store || 5252 BuiltinID == Builtin::BI__builtin_nontemporal_load) && 5253 "Unexpected nontemporal load/store builtin!"); 5254 bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store; 5255 unsigned numArgs = isStore ? 2 : 1; 5256 5257 // Ensure that we have the proper number of arguments. 5258 if (checkArgCount(*this, TheCall, numArgs)) 5259 return ExprError(); 5260 5261 // Inspect the last argument of the nontemporal builtin. This should always 5262 // be a pointer type, from which we imply the type of the memory access. 5263 // Because it is a pointer type, we don't have to worry about any implicit 5264 // casts here. 5265 Expr *PointerArg = TheCall->getArg(numArgs - 1); 5266 ExprResult PointerArgResult = 5267 DefaultFunctionArrayLvalueConversion(PointerArg); 5268 5269 if (PointerArgResult.isInvalid()) 5270 return ExprError(); 5271 PointerArg = PointerArgResult.get(); 5272 TheCall->setArg(numArgs - 1, PointerArg); 5273 5274 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); 5275 if (!pointerType) { 5276 Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer) 5277 << PointerArg->getType() << PointerArg->getSourceRange(); 5278 return ExprError(); 5279 } 5280 5281 QualType ValType = pointerType->getPointeeType(); 5282 5283 // Strip any qualifiers off ValType. 5284 ValType = ValType.getUnqualifiedType(); 5285 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 5286 !ValType->isBlockPointerType() && !ValType->isFloatingType() && 5287 !ValType->isVectorType()) { 5288 Diag(DRE->getBeginLoc(), 5289 diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) 5290 << PointerArg->getType() << PointerArg->getSourceRange(); 5291 return ExprError(); 5292 } 5293 5294 if (!isStore) { 5295 TheCall->setType(ValType); 5296 return TheCallResult; 5297 } 5298 5299 ExprResult ValArg = TheCall->getArg(0); 5300 InitializedEntity Entity = InitializedEntity::InitializeParameter( 5301 Context, ValType, /*consume*/ false); 5302 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); 5303 if (ValArg.isInvalid()) 5304 return ExprError(); 5305 5306 TheCall->setArg(0, ValArg.get()); 5307 TheCall->setType(Context.VoidTy); 5308 return TheCallResult; 5309 } 5310 5311 /// CheckObjCString - Checks that the argument to the builtin 5312 /// CFString constructor is correct 5313 /// Note: It might also make sense to do the UTF-16 conversion here (would 5314 /// simplify the backend). 5315 bool Sema::CheckObjCString(Expr *Arg) { 5316 Arg = Arg->IgnoreParenCasts(); 5317 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); 5318 5319 if (!Literal || !Literal->isAscii()) { 5320 Diag(Arg->getBeginLoc(), diag::err_cfstring_literal_not_string_constant) 5321 << Arg->getSourceRange(); 5322 return true; 5323 } 5324 5325 if (Literal->containsNonAsciiOrNull()) { 5326 StringRef String = Literal->getString(); 5327 unsigned NumBytes = String.size(); 5328 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes); 5329 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); 5330 llvm::UTF16 *ToPtr = &ToBuf[0]; 5331 5332 llvm::ConversionResult Result = 5333 llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, 5334 ToPtr + NumBytes, llvm::strictConversion); 5335 // Check for conversion failure. 5336 if (Result != llvm::conversionOK) 5337 Diag(Arg->getBeginLoc(), diag::warn_cfstring_truncated) 5338 << Arg->getSourceRange(); 5339 } 5340 return false; 5341 } 5342 5343 /// CheckObjCString - Checks that the format string argument to the os_log() 5344 /// and os_trace() functions is correct, and converts it to const char *. 5345 ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) { 5346 Arg = Arg->IgnoreParenCasts(); 5347 auto *Literal = dyn_cast<StringLiteral>(Arg); 5348 if (!Literal) { 5349 if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) { 5350 Literal = ObjcLiteral->getString(); 5351 } 5352 } 5353 5354 if (!Literal || (!Literal->isAscii() && !Literal->isUTF8())) { 5355 return ExprError( 5356 Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant) 5357 << Arg->getSourceRange()); 5358 } 5359 5360 ExprResult Result(Literal); 5361 QualType ResultTy = Context.getPointerType(Context.CharTy.withConst()); 5362 InitializedEntity Entity = 5363 InitializedEntity::InitializeParameter(Context, ResultTy, false); 5364 Result = PerformCopyInitialization(Entity, SourceLocation(), Result); 5365 return Result; 5366 } 5367 5368 /// Check that the user is calling the appropriate va_start builtin for the 5369 /// target and calling convention. 5370 static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) { 5371 const llvm::Triple &TT = S.Context.getTargetInfo().getTriple(); 5372 bool IsX64 = TT.getArch() == llvm::Triple::x86_64; 5373 bool IsAArch64 = TT.getArch() == llvm::Triple::aarch64; 5374 bool IsWindows = TT.isOSWindows(); 5375 bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start; 5376 if (IsX64 || IsAArch64) { 5377 CallingConv CC = CC_C; 5378 if (const FunctionDecl *FD = S.getCurFunctionDecl()) 5379 CC = FD->getType()->getAs<FunctionType>()->getCallConv(); 5380 if (IsMSVAStart) { 5381 // Don't allow this in System V ABI functions. 5382 if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64)) 5383 return S.Diag(Fn->getBeginLoc(), 5384 diag::err_ms_va_start_used_in_sysv_function); 5385 } else { 5386 // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions. 5387 // On x64 Windows, don't allow this in System V ABI functions. 5388 // (Yes, that means there's no corresponding way to support variadic 5389 // System V ABI functions on Windows.) 5390 if ((IsWindows && CC == CC_X86_64SysV) || 5391 (!IsWindows && CC == CC_Win64)) 5392 return S.Diag(Fn->getBeginLoc(), 5393 diag::err_va_start_used_in_wrong_abi_function) 5394 << !IsWindows; 5395 } 5396 return false; 5397 } 5398 5399 if (IsMSVAStart) 5400 return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only); 5401 return false; 5402 } 5403 5404 static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn, 5405 ParmVarDecl **LastParam = nullptr) { 5406 // Determine whether the current function, block, or obj-c method is variadic 5407 // and get its parameter list. 5408 bool IsVariadic = false; 5409 ArrayRef<ParmVarDecl *> Params; 5410 DeclContext *Caller = S.CurContext; 5411 if (auto *Block = dyn_cast<BlockDecl>(Caller)) { 5412 IsVariadic = Block->isVariadic(); 5413 Params = Block->parameters(); 5414 } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) { 5415 IsVariadic = FD->isVariadic(); 5416 Params = FD->parameters(); 5417 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) { 5418 IsVariadic = MD->isVariadic(); 5419 // FIXME: This isn't correct for methods (results in bogus warning). 5420 Params = MD->parameters(); 5421 } else if (isa<CapturedDecl>(Caller)) { 5422 // We don't support va_start in a CapturedDecl. 5423 S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt); 5424 return true; 5425 } else { 5426 // This must be some other declcontext that parses exprs. 5427 S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function); 5428 return true; 5429 } 5430 5431 if (!IsVariadic) { 5432 S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function); 5433 return true; 5434 } 5435 5436 if (LastParam) 5437 *LastParam = Params.empty() ? nullptr : Params.back(); 5438 5439 return false; 5440 } 5441 5442 /// Check the arguments to '__builtin_va_start' or '__builtin_ms_va_start' 5443 /// for validity. Emit an error and return true on failure; return false 5444 /// on success. 5445 bool Sema::SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) { 5446 Expr *Fn = TheCall->getCallee(); 5447 5448 if (checkVAStartABI(*this, BuiltinID, Fn)) 5449 return true; 5450 5451 if (TheCall->getNumArgs() > 2) { 5452 Diag(TheCall->getArg(2)->getBeginLoc(), 5453 diag::err_typecheck_call_too_many_args) 5454 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 5455 << Fn->getSourceRange() 5456 << SourceRange(TheCall->getArg(2)->getBeginLoc(), 5457 (*(TheCall->arg_end() - 1))->getEndLoc()); 5458 return true; 5459 } 5460 5461 if (TheCall->getNumArgs() < 2) { 5462 return Diag(TheCall->getEndLoc(), 5463 diag::err_typecheck_call_too_few_args_at_least) 5464 << 0 /*function call*/ << 2 << TheCall->getNumArgs(); 5465 } 5466 5467 // Type-check the first argument normally. 5468 if (checkBuiltinArgument(*this, TheCall, 0)) 5469 return true; 5470 5471 // Check that the current function is variadic, and get its last parameter. 5472 ParmVarDecl *LastParam; 5473 if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam)) 5474 return true; 5475 5476 // Verify that the second argument to the builtin is the last argument of the 5477 // current function or method. 5478 bool SecondArgIsLastNamedArgument = false; 5479 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); 5480 5481 // These are valid if SecondArgIsLastNamedArgument is false after the next 5482 // block. 5483 QualType Type; 5484 SourceLocation ParamLoc; 5485 bool IsCRegister = false; 5486 5487 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { 5488 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { 5489 SecondArgIsLastNamedArgument = PV == LastParam; 5490 5491 Type = PV->getType(); 5492 ParamLoc = PV->getLocation(); 5493 IsCRegister = 5494 PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus; 5495 } 5496 } 5497 5498 if (!SecondArgIsLastNamedArgument) 5499 Diag(TheCall->getArg(1)->getBeginLoc(), 5500 diag::warn_second_arg_of_va_start_not_last_named_param); 5501 else if (IsCRegister || Type->isReferenceType() || 5502 Type->isSpecificBuiltinType(BuiltinType::Float) || [=] { 5503 // Promotable integers are UB, but enumerations need a bit of 5504 // extra checking to see what their promotable type actually is. 5505 if (!Type->isPromotableIntegerType()) 5506 return false; 5507 if (!Type->isEnumeralType()) 5508 return true; 5509 const EnumDecl *ED = Type->getAs<EnumType>()->getDecl(); 5510 return !(ED && 5511 Context.typesAreCompatible(ED->getPromotionType(), Type)); 5512 }()) { 5513 unsigned Reason = 0; 5514 if (Type->isReferenceType()) Reason = 1; 5515 else if (IsCRegister) Reason = 2; 5516 Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason; 5517 Diag(ParamLoc, diag::note_parameter_type) << Type; 5518 } 5519 5520 TheCall->setType(Context.VoidTy); 5521 return false; 5522 } 5523 5524 bool Sema::SemaBuiltinVAStartARMMicrosoft(CallExpr *Call) { 5525 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, 5526 // const char *named_addr); 5527 5528 Expr *Func = Call->getCallee(); 5529 5530 if (Call->getNumArgs() < 3) 5531 return Diag(Call->getEndLoc(), 5532 diag::err_typecheck_call_too_few_args_at_least) 5533 << 0 /*function call*/ << 3 << Call->getNumArgs(); 5534 5535 // Type-check the first argument normally. 5536 if (checkBuiltinArgument(*this, Call, 0)) 5537 return true; 5538 5539 // Check that the current function is variadic. 5540 if (checkVAStartIsInVariadicFunction(*this, Func)) 5541 return true; 5542 5543 // __va_start on Windows does not validate the parameter qualifiers 5544 5545 const Expr *Arg1 = Call->getArg(1)->IgnoreParens(); 5546 const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr(); 5547 5548 const Expr *Arg2 = Call->getArg(2)->IgnoreParens(); 5549 const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr(); 5550 5551 const QualType &ConstCharPtrTy = 5552 Context.getPointerType(Context.CharTy.withConst()); 5553 if (!Arg1Ty->isPointerType() || 5554 Arg1Ty->getPointeeType().withoutLocalFastQualifiers() != Context.CharTy) 5555 Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible) 5556 << Arg1->getType() << ConstCharPtrTy << 1 /* different class */ 5557 << 0 /* qualifier difference */ 5558 << 3 /* parameter mismatch */ 5559 << 2 << Arg1->getType() << ConstCharPtrTy; 5560 5561 const QualType SizeTy = Context.getSizeType(); 5562 if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy) 5563 Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible) 5564 << Arg2->getType() << SizeTy << 1 /* different class */ 5565 << 0 /* qualifier difference */ 5566 << 3 /* parameter mismatch */ 5567 << 3 << Arg2->getType() << SizeTy; 5568 5569 return false; 5570 } 5571 5572 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and 5573 /// friends. This is declared to take (...), so we have to check everything. 5574 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { 5575 if (TheCall->getNumArgs() < 2) 5576 return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 5577 << 0 << 2 << TheCall->getNumArgs() /*function call*/; 5578 if (TheCall->getNumArgs() > 2) 5579 return Diag(TheCall->getArg(2)->getBeginLoc(), 5580 diag::err_typecheck_call_too_many_args) 5581 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 5582 << SourceRange(TheCall->getArg(2)->getBeginLoc(), 5583 (*(TheCall->arg_end() - 1))->getEndLoc()); 5584 5585 ExprResult OrigArg0 = TheCall->getArg(0); 5586 ExprResult OrigArg1 = TheCall->getArg(1); 5587 5588 // Do standard promotions between the two arguments, returning their common 5589 // type. 5590 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); 5591 if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) 5592 return true; 5593 5594 // Make sure any conversions are pushed back into the call; this is 5595 // type safe since unordered compare builtins are declared as "_Bool 5596 // foo(...)". 5597 TheCall->setArg(0, OrigArg0.get()); 5598 TheCall->setArg(1, OrigArg1.get()); 5599 5600 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) 5601 return false; 5602 5603 // If the common type isn't a real floating type, then the arguments were 5604 // invalid for this operation. 5605 if (Res.isNull() || !Res->isRealFloatingType()) 5606 return Diag(OrigArg0.get()->getBeginLoc(), 5607 diag::err_typecheck_call_invalid_ordered_compare) 5608 << OrigArg0.get()->getType() << OrigArg1.get()->getType() 5609 << SourceRange(OrigArg0.get()->getBeginLoc(), 5610 OrigArg1.get()->getEndLoc()); 5611 5612 return false; 5613 } 5614 5615 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like 5616 /// __builtin_isnan and friends. This is declared to take (...), so we have 5617 /// to check everything. We expect the last argument to be a floating point 5618 /// value. 5619 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { 5620 if (TheCall->getNumArgs() < NumArgs) 5621 return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 5622 << 0 << NumArgs << TheCall->getNumArgs() /*function call*/; 5623 if (TheCall->getNumArgs() > NumArgs) 5624 return Diag(TheCall->getArg(NumArgs)->getBeginLoc(), 5625 diag::err_typecheck_call_too_many_args) 5626 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() 5627 << SourceRange(TheCall->getArg(NumArgs)->getBeginLoc(), 5628 (*(TheCall->arg_end() - 1))->getEndLoc()); 5629 5630 Expr *OrigArg = TheCall->getArg(NumArgs-1); 5631 5632 if (OrigArg->isTypeDependent()) 5633 return false; 5634 5635 // This operation requires a non-_Complex floating-point number. 5636 if (!OrigArg->getType()->isRealFloatingType()) 5637 return Diag(OrigArg->getBeginLoc(), 5638 diag::err_typecheck_call_invalid_unary_fp) 5639 << OrigArg->getType() << OrigArg->getSourceRange(); 5640 5641 // If this is an implicit conversion from float -> float, double, or 5642 // long double, remove it. 5643 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { 5644 // Only remove standard FloatCasts, leaving other casts inplace 5645 if (Cast->getCastKind() == CK_FloatingCast) { 5646 Expr *CastArg = Cast->getSubExpr(); 5647 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { 5648 assert( 5649 (Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) || 5650 Cast->getType()->isSpecificBuiltinType(BuiltinType::Float) || 5651 Cast->getType()->isSpecificBuiltinType(BuiltinType::LongDouble)) && 5652 "promotion from float to either float, double, or long double is " 5653 "the only expected cast here"); 5654 Cast->setSubExpr(nullptr); 5655 TheCall->setArg(NumArgs-1, CastArg); 5656 } 5657 } 5658 } 5659 5660 return false; 5661 } 5662 5663 // Customized Sema Checking for VSX builtins that have the following signature: 5664 // vector [...] builtinName(vector [...], vector [...], const int); 5665 // Which takes the same type of vectors (any legal vector type) for the first 5666 // two arguments and takes compile time constant for the third argument. 5667 // Example builtins are : 5668 // vector double vec_xxpermdi(vector double, vector double, int); 5669 // vector short vec_xxsldwi(vector short, vector short, int); 5670 bool Sema::SemaBuiltinVSX(CallExpr *TheCall) { 5671 unsigned ExpectedNumArgs = 3; 5672 if (TheCall->getNumArgs() < ExpectedNumArgs) 5673 return Diag(TheCall->getEndLoc(), 5674 diag::err_typecheck_call_too_few_args_at_least) 5675 << 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs() 5676 << TheCall->getSourceRange(); 5677 5678 if (TheCall->getNumArgs() > ExpectedNumArgs) 5679 return Diag(TheCall->getEndLoc(), 5680 diag::err_typecheck_call_too_many_args_at_most) 5681 << 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs() 5682 << TheCall->getSourceRange(); 5683 5684 // Check the third argument is a compile time constant 5685 llvm::APSInt Value; 5686 if(!TheCall->getArg(2)->isIntegerConstantExpr(Value, Context)) 5687 return Diag(TheCall->getBeginLoc(), 5688 diag::err_vsx_builtin_nonconstant_argument) 5689 << 3 /* argument index */ << TheCall->getDirectCallee() 5690 << SourceRange(TheCall->getArg(2)->getBeginLoc(), 5691 TheCall->getArg(2)->getEndLoc()); 5692 5693 QualType Arg1Ty = TheCall->getArg(0)->getType(); 5694 QualType Arg2Ty = TheCall->getArg(1)->getType(); 5695 5696 // Check the type of argument 1 and argument 2 are vectors. 5697 SourceLocation BuiltinLoc = TheCall->getBeginLoc(); 5698 if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) || 5699 (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) { 5700 return Diag(BuiltinLoc, diag::err_vec_builtin_non_vector) 5701 << TheCall->getDirectCallee() 5702 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5703 TheCall->getArg(1)->getEndLoc()); 5704 } 5705 5706 // Check the first two arguments are the same type. 5707 if (!Context.hasSameUnqualifiedType(Arg1Ty, Arg2Ty)) { 5708 return Diag(BuiltinLoc, diag::err_vec_builtin_incompatible_vector) 5709 << TheCall->getDirectCallee() 5710 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5711 TheCall->getArg(1)->getEndLoc()); 5712 } 5713 5714 // When default clang type checking is turned off and the customized type 5715 // checking is used, the returning type of the function must be explicitly 5716 // set. Otherwise it is _Bool by default. 5717 TheCall->setType(Arg1Ty); 5718 5719 return false; 5720 } 5721 5722 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. 5723 // This is declared to take (...), so we have to check everything. 5724 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { 5725 if (TheCall->getNumArgs() < 2) 5726 return ExprError(Diag(TheCall->getEndLoc(), 5727 diag::err_typecheck_call_too_few_args_at_least) 5728 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 5729 << TheCall->getSourceRange()); 5730 5731 // Determine which of the following types of shufflevector we're checking: 5732 // 1) unary, vector mask: (lhs, mask) 5733 // 2) binary, scalar mask: (lhs, rhs, index, ..., index) 5734 QualType resType = TheCall->getArg(0)->getType(); 5735 unsigned numElements = 0; 5736 5737 if (!TheCall->getArg(0)->isTypeDependent() && 5738 !TheCall->getArg(1)->isTypeDependent()) { 5739 QualType LHSType = TheCall->getArg(0)->getType(); 5740 QualType RHSType = TheCall->getArg(1)->getType(); 5741 5742 if (!LHSType->isVectorType() || !RHSType->isVectorType()) 5743 return ExprError( 5744 Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector) 5745 << TheCall->getDirectCallee() 5746 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5747 TheCall->getArg(1)->getEndLoc())); 5748 5749 numElements = LHSType->getAs<VectorType>()->getNumElements(); 5750 unsigned numResElements = TheCall->getNumArgs() - 2; 5751 5752 // Check to see if we have a call with 2 vector arguments, the unary shuffle 5753 // with mask. If so, verify that RHS is an integer vector type with the 5754 // same number of elts as lhs. 5755 if (TheCall->getNumArgs() == 2) { 5756 if (!RHSType->hasIntegerRepresentation() || 5757 RHSType->getAs<VectorType>()->getNumElements() != numElements) 5758 return ExprError(Diag(TheCall->getBeginLoc(), 5759 diag::err_vec_builtin_incompatible_vector) 5760 << TheCall->getDirectCallee() 5761 << SourceRange(TheCall->getArg(1)->getBeginLoc(), 5762 TheCall->getArg(1)->getEndLoc())); 5763 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { 5764 return ExprError(Diag(TheCall->getBeginLoc(), 5765 diag::err_vec_builtin_incompatible_vector) 5766 << TheCall->getDirectCallee() 5767 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5768 TheCall->getArg(1)->getEndLoc())); 5769 } else if (numElements != numResElements) { 5770 QualType eltType = LHSType->getAs<VectorType>()->getElementType(); 5771 resType = Context.getVectorType(eltType, numResElements, 5772 VectorType::GenericVector); 5773 } 5774 } 5775 5776 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { 5777 if (TheCall->getArg(i)->isTypeDependent() || 5778 TheCall->getArg(i)->isValueDependent()) 5779 continue; 5780 5781 llvm::APSInt Result(32); 5782 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) 5783 return ExprError(Diag(TheCall->getBeginLoc(), 5784 diag::err_shufflevector_nonconstant_argument) 5785 << TheCall->getArg(i)->getSourceRange()); 5786 5787 // Allow -1 which will be translated to undef in the IR. 5788 if (Result.isSigned() && Result.isAllOnesValue()) 5789 continue; 5790 5791 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) 5792 return ExprError(Diag(TheCall->getBeginLoc(), 5793 diag::err_shufflevector_argument_too_large) 5794 << TheCall->getArg(i)->getSourceRange()); 5795 } 5796 5797 SmallVector<Expr*, 32> exprs; 5798 5799 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { 5800 exprs.push_back(TheCall->getArg(i)); 5801 TheCall->setArg(i, nullptr); 5802 } 5803 5804 return new (Context) ShuffleVectorExpr(Context, exprs, resType, 5805 TheCall->getCallee()->getBeginLoc(), 5806 TheCall->getRParenLoc()); 5807 } 5808 5809 /// SemaConvertVectorExpr - Handle __builtin_convertvector 5810 ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, 5811 SourceLocation BuiltinLoc, 5812 SourceLocation RParenLoc) { 5813 ExprValueKind VK = VK_RValue; 5814 ExprObjectKind OK = OK_Ordinary; 5815 QualType DstTy = TInfo->getType(); 5816 QualType SrcTy = E->getType(); 5817 5818 if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) 5819 return ExprError(Diag(BuiltinLoc, 5820 diag::err_convertvector_non_vector) 5821 << E->getSourceRange()); 5822 if (!DstTy->isVectorType() && !DstTy->isDependentType()) 5823 return ExprError(Diag(BuiltinLoc, 5824 diag::err_convertvector_non_vector_type)); 5825 5826 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { 5827 unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements(); 5828 unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements(); 5829 if (SrcElts != DstElts) 5830 return ExprError(Diag(BuiltinLoc, 5831 diag::err_convertvector_incompatible_vector) 5832 << E->getSourceRange()); 5833 } 5834 5835 return new (Context) 5836 ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc); 5837 } 5838 5839 /// SemaBuiltinPrefetch - Handle __builtin_prefetch. 5840 // This is declared to take (const void*, ...) and can take two 5841 // optional constant int args. 5842 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { 5843 unsigned NumArgs = TheCall->getNumArgs(); 5844 5845 if (NumArgs > 3) 5846 return Diag(TheCall->getEndLoc(), 5847 diag::err_typecheck_call_too_many_args_at_most) 5848 << 0 /*function call*/ << 3 << NumArgs << TheCall->getSourceRange(); 5849 5850 // Argument 0 is checked for us and the remaining arguments must be 5851 // constant integers. 5852 for (unsigned i = 1; i != NumArgs; ++i) 5853 if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) 5854 return true; 5855 5856 return false; 5857 } 5858 5859 /// SemaBuiltinAssume - Handle __assume (MS Extension). 5860 // __assume does not evaluate its arguments, and should warn if its argument 5861 // has side effects. 5862 bool Sema::SemaBuiltinAssume(CallExpr *TheCall) { 5863 Expr *Arg = TheCall->getArg(0); 5864 if (Arg->isInstantiationDependent()) return false; 5865 5866 if (Arg->HasSideEffects(Context)) 5867 Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects) 5868 << Arg->getSourceRange() 5869 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); 5870 5871 return false; 5872 } 5873 5874 /// Handle __builtin_alloca_with_align. This is declared 5875 /// as (size_t, size_t) where the second size_t must be a power of 2 greater 5876 /// than 8. 5877 bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) { 5878 // The alignment must be a constant integer. 5879 Expr *Arg = TheCall->getArg(1); 5880 5881 // We can't check the value of a dependent argument. 5882 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 5883 if (const auto *UE = 5884 dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts())) 5885 if (UE->getKind() == UETT_AlignOf || 5886 UE->getKind() == UETT_PreferredAlignOf) 5887 Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof) 5888 << Arg->getSourceRange(); 5889 5890 llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context); 5891 5892 if (!Result.isPowerOf2()) 5893 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5894 << Arg->getSourceRange(); 5895 5896 if (Result < Context.getCharWidth()) 5897 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small) 5898 << (unsigned)Context.getCharWidth() << Arg->getSourceRange(); 5899 5900 if (Result > std::numeric_limits<int32_t>::max()) 5901 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big) 5902 << std::numeric_limits<int32_t>::max() << Arg->getSourceRange(); 5903 } 5904 5905 return false; 5906 } 5907 5908 /// Handle __builtin_assume_aligned. This is declared 5909 /// as (const void*, size_t, ...) and can take one optional constant int arg. 5910 bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) { 5911 unsigned NumArgs = TheCall->getNumArgs(); 5912 5913 if (NumArgs > 3) 5914 return Diag(TheCall->getEndLoc(), 5915 diag::err_typecheck_call_too_many_args_at_most) 5916 << 0 /*function call*/ << 3 << NumArgs << TheCall->getSourceRange(); 5917 5918 // The alignment must be a constant integer. 5919 Expr *Arg = TheCall->getArg(1); 5920 5921 // We can't check the value of a dependent argument. 5922 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 5923 llvm::APSInt Result; 5924 if (SemaBuiltinConstantArg(TheCall, 1, Result)) 5925 return true; 5926 5927 if (!Result.isPowerOf2()) 5928 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5929 << Arg->getSourceRange(); 5930 } 5931 5932 if (NumArgs > 2) { 5933 ExprResult Arg(TheCall->getArg(2)); 5934 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 5935 Context.getSizeType(), false); 5936 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 5937 if (Arg.isInvalid()) return true; 5938 TheCall->setArg(2, Arg.get()); 5939 } 5940 5941 return false; 5942 } 5943 5944 bool Sema::SemaBuiltinOSLogFormat(CallExpr *TheCall) { 5945 unsigned BuiltinID = 5946 cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID(); 5947 bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size; 5948 5949 unsigned NumArgs = TheCall->getNumArgs(); 5950 unsigned NumRequiredArgs = IsSizeCall ? 1 : 2; 5951 if (NumArgs < NumRequiredArgs) { 5952 return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 5953 << 0 /* function call */ << NumRequiredArgs << NumArgs 5954 << TheCall->getSourceRange(); 5955 } 5956 if (NumArgs >= NumRequiredArgs + 0x100) { 5957 return Diag(TheCall->getEndLoc(), 5958 diag::err_typecheck_call_too_many_args_at_most) 5959 << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs 5960 << TheCall->getSourceRange(); 5961 } 5962 unsigned i = 0; 5963 5964 // For formatting call, check buffer arg. 5965 if (!IsSizeCall) { 5966 ExprResult Arg(TheCall->getArg(i)); 5967 InitializedEntity Entity = InitializedEntity::InitializeParameter( 5968 Context, Context.VoidPtrTy, false); 5969 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 5970 if (Arg.isInvalid()) 5971 return true; 5972 TheCall->setArg(i, Arg.get()); 5973 i++; 5974 } 5975 5976 // Check string literal arg. 5977 unsigned FormatIdx = i; 5978 { 5979 ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i)); 5980 if (Arg.isInvalid()) 5981 return true; 5982 TheCall->setArg(i, Arg.get()); 5983 i++; 5984 } 5985 5986 // Make sure variadic args are scalar. 5987 unsigned FirstDataArg = i; 5988 while (i < NumArgs) { 5989 ExprResult Arg = DefaultVariadicArgumentPromotion( 5990 TheCall->getArg(i), VariadicFunction, nullptr); 5991 if (Arg.isInvalid()) 5992 return true; 5993 CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType()); 5994 if (ArgSize.getQuantity() >= 0x100) { 5995 return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big) 5996 << i << (int)ArgSize.getQuantity() << 0xff 5997 << TheCall->getSourceRange(); 5998 } 5999 TheCall->setArg(i, Arg.get()); 6000 i++; 6001 } 6002 6003 // Check formatting specifiers. NOTE: We're only doing this for the non-size 6004 // call to avoid duplicate diagnostics. 6005 if (!IsSizeCall) { 6006 llvm::SmallBitVector CheckedVarArgs(NumArgs, false); 6007 ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs()); 6008 bool Success = CheckFormatArguments( 6009 Args, /*HasVAListArg*/ false, FormatIdx, FirstDataArg, FST_OSLog, 6010 VariadicFunction, TheCall->getBeginLoc(), SourceRange(), 6011 CheckedVarArgs); 6012 if (!Success) 6013 return true; 6014 } 6015 6016 if (IsSizeCall) { 6017 TheCall->setType(Context.getSizeType()); 6018 } else { 6019 TheCall->setType(Context.VoidPtrTy); 6020 } 6021 return false; 6022 } 6023 6024 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr 6025 /// TheCall is a constant expression. 6026 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, 6027 llvm::APSInt &Result) { 6028 Expr *Arg = TheCall->getArg(ArgNum); 6029 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 6030 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 6031 6032 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; 6033 6034 if (!Arg->isIntegerConstantExpr(Result, Context)) 6035 return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) 6036 << FDecl->getDeclName() << Arg->getSourceRange(); 6037 6038 return false; 6039 } 6040 6041 /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr 6042 /// TheCall is a constant expression in the range [Low, High]. 6043 bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, 6044 int Low, int High, bool RangeIsError) { 6045 llvm::APSInt Result; 6046 6047 // We can't check the value of a dependent argument. 6048 Expr *Arg = TheCall->getArg(ArgNum); 6049 if (Arg->isTypeDependent() || Arg->isValueDependent()) 6050 return false; 6051 6052 // Check constant-ness first. 6053 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) 6054 return true; 6055 6056 if (Result.getSExtValue() < Low || Result.getSExtValue() > High) { 6057 if (RangeIsError) 6058 return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range) 6059 << Result.toString(10) << Low << High << Arg->getSourceRange(); 6060 else 6061 // Defer the warning until we know if the code will be emitted so that 6062 // dead code can ignore this. 6063 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 6064 PDiag(diag::warn_argument_invalid_range) 6065 << Result.toString(10) << Low << High 6066 << Arg->getSourceRange()); 6067 } 6068 6069 return false; 6070 } 6071 6072 /// SemaBuiltinConstantArgMultiple - Handle a check if argument ArgNum of CallExpr 6073 /// TheCall is a constant expression is a multiple of Num.. 6074 bool Sema::SemaBuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum, 6075 unsigned Num) { 6076 llvm::APSInt Result; 6077 6078 // We can't check the value of a dependent argument. 6079 Expr *Arg = TheCall->getArg(ArgNum); 6080 if (Arg->isTypeDependent() || Arg->isValueDependent()) 6081 return false; 6082 6083 // Check constant-ness first. 6084 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) 6085 return true; 6086 6087 if (Result.getSExtValue() % Num != 0) 6088 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple) 6089 << Num << Arg->getSourceRange(); 6090 6091 return false; 6092 } 6093 6094 /// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr 6095 /// TheCall is an ARM/AArch64 special register string literal. 6096 bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall, 6097 int ArgNum, unsigned ExpectedFieldNum, 6098 bool AllowName) { 6099 bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 || 6100 BuiltinID == ARM::BI__builtin_arm_wsr64 || 6101 BuiltinID == ARM::BI__builtin_arm_rsr || 6102 BuiltinID == ARM::BI__builtin_arm_rsrp || 6103 BuiltinID == ARM::BI__builtin_arm_wsr || 6104 BuiltinID == ARM::BI__builtin_arm_wsrp; 6105 bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 || 6106 BuiltinID == AArch64::BI__builtin_arm_wsr64 || 6107 BuiltinID == AArch64::BI__builtin_arm_rsr || 6108 BuiltinID == AArch64::BI__builtin_arm_rsrp || 6109 BuiltinID == AArch64::BI__builtin_arm_wsr || 6110 BuiltinID == AArch64::BI__builtin_arm_wsrp; 6111 assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin."); 6112 6113 // We can't check the value of a dependent argument. 6114 Expr *Arg = TheCall->getArg(ArgNum); 6115 if (Arg->isTypeDependent() || Arg->isValueDependent()) 6116 return false; 6117 6118 // Check if the argument is a string literal. 6119 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) 6120 return Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 6121 << Arg->getSourceRange(); 6122 6123 // Check the type of special register given. 6124 StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString(); 6125 SmallVector<StringRef, 6> Fields; 6126 Reg.split(Fields, ":"); 6127 6128 if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1)) 6129 return Diag(TheCall->getBeginLoc(), diag::err_arm_invalid_specialreg) 6130 << Arg->getSourceRange(); 6131 6132 // If the string is the name of a register then we cannot check that it is 6133 // valid here but if the string is of one the forms described in ACLE then we 6134 // can check that the supplied fields are integers and within the valid 6135 // ranges. 6136 if (Fields.size() > 1) { 6137 bool FiveFields = Fields.size() == 5; 6138 6139 bool ValidString = true; 6140 if (IsARMBuiltin) { 6141 ValidString &= Fields[0].startswith_lower("cp") || 6142 Fields[0].startswith_lower("p"); 6143 if (ValidString) 6144 Fields[0] = 6145 Fields[0].drop_front(Fields[0].startswith_lower("cp") ? 2 : 1); 6146 6147 ValidString &= Fields[2].startswith_lower("c"); 6148 if (ValidString) 6149 Fields[2] = Fields[2].drop_front(1); 6150 6151 if (FiveFields) { 6152 ValidString &= Fields[3].startswith_lower("c"); 6153 if (ValidString) 6154 Fields[3] = Fields[3].drop_front(1); 6155 } 6156 } 6157 6158 SmallVector<int, 5> Ranges; 6159 if (FiveFields) 6160 Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 15, 15, 7}); 6161 else 6162 Ranges.append({15, 7, 15}); 6163 6164 for (unsigned i=0; i<Fields.size(); ++i) { 6165 int IntField; 6166 ValidString &= !Fields[i].getAsInteger(10, IntField); 6167 ValidString &= (IntField >= 0 && IntField <= Ranges[i]); 6168 } 6169 6170 if (!ValidString) 6171 return Diag(TheCall->getBeginLoc(), diag::err_arm_invalid_specialreg) 6172 << Arg->getSourceRange(); 6173 } else if (IsAArch64Builtin && Fields.size() == 1) { 6174 // If the register name is one of those that appear in the condition below 6175 // and the special register builtin being used is one of the write builtins, 6176 // then we require that the argument provided for writing to the register 6177 // is an integer constant expression. This is because it will be lowered to 6178 // an MSR (immediate) instruction, so we need to know the immediate at 6179 // compile time. 6180 if (TheCall->getNumArgs() != 2) 6181 return false; 6182 6183 std::string RegLower = Reg.lower(); 6184 if (RegLower != "spsel" && RegLower != "daifset" && RegLower != "daifclr" && 6185 RegLower != "pan" && RegLower != "uao") 6186 return false; 6187 6188 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15); 6189 } 6190 6191 return false; 6192 } 6193 6194 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). 6195 /// This checks that the target supports __builtin_longjmp and 6196 /// that val is a constant 1. 6197 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { 6198 if (!Context.getTargetInfo().hasSjLjLowering()) 6199 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported) 6200 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 6201 6202 Expr *Arg = TheCall->getArg(1); 6203 llvm::APSInt Result; 6204 6205 // TODO: This is less than ideal. Overload this to take a value. 6206 if (SemaBuiltinConstantArg(TheCall, 1, Result)) 6207 return true; 6208 6209 if (Result != 1) 6210 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val) 6211 << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc()); 6212 6213 return false; 6214 } 6215 6216 /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]). 6217 /// This checks that the target supports __builtin_setjmp. 6218 bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) { 6219 if (!Context.getTargetInfo().hasSjLjLowering()) 6220 return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported) 6221 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 6222 return false; 6223 } 6224 6225 namespace { 6226 6227 class UncoveredArgHandler { 6228 enum { Unknown = -1, AllCovered = -2 }; 6229 6230 signed FirstUncoveredArg = Unknown; 6231 SmallVector<const Expr *, 4> DiagnosticExprs; 6232 6233 public: 6234 UncoveredArgHandler() = default; 6235 6236 bool hasUncoveredArg() const { 6237 return (FirstUncoveredArg >= 0); 6238 } 6239 6240 unsigned getUncoveredArg() const { 6241 assert(hasUncoveredArg() && "no uncovered argument"); 6242 return FirstUncoveredArg; 6243 } 6244 6245 void setAllCovered() { 6246 // A string has been found with all arguments covered, so clear out 6247 // the diagnostics. 6248 DiagnosticExprs.clear(); 6249 FirstUncoveredArg = AllCovered; 6250 } 6251 6252 void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) { 6253 assert(NewFirstUncoveredArg >= 0 && "Outside range"); 6254 6255 // Don't update if a previous string covers all arguments. 6256 if (FirstUncoveredArg == AllCovered) 6257 return; 6258 6259 // UncoveredArgHandler tracks the highest uncovered argument index 6260 // and with it all the strings that match this index. 6261 if (NewFirstUncoveredArg == FirstUncoveredArg) 6262 DiagnosticExprs.push_back(StrExpr); 6263 else if (NewFirstUncoveredArg > FirstUncoveredArg) { 6264 DiagnosticExprs.clear(); 6265 DiagnosticExprs.push_back(StrExpr); 6266 FirstUncoveredArg = NewFirstUncoveredArg; 6267 } 6268 } 6269 6270 void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr); 6271 }; 6272 6273 enum StringLiteralCheckType { 6274 SLCT_NotALiteral, 6275 SLCT_UncheckedLiteral, 6276 SLCT_CheckedLiteral 6277 }; 6278 6279 } // namespace 6280 6281 static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend, 6282 BinaryOperatorKind BinOpKind, 6283 bool AddendIsRight) { 6284 unsigned BitWidth = Offset.getBitWidth(); 6285 unsigned AddendBitWidth = Addend.getBitWidth(); 6286 // There might be negative interim results. 6287 if (Addend.isUnsigned()) { 6288 Addend = Addend.zext(++AddendBitWidth); 6289 Addend.setIsSigned(true); 6290 } 6291 // Adjust the bit width of the APSInts. 6292 if (AddendBitWidth > BitWidth) { 6293 Offset = Offset.sext(AddendBitWidth); 6294 BitWidth = AddendBitWidth; 6295 } else if (BitWidth > AddendBitWidth) { 6296 Addend = Addend.sext(BitWidth); 6297 } 6298 6299 bool Ov = false; 6300 llvm::APSInt ResOffset = Offset; 6301 if (BinOpKind == BO_Add) 6302 ResOffset = Offset.sadd_ov(Addend, Ov); 6303 else { 6304 assert(AddendIsRight && BinOpKind == BO_Sub && 6305 "operator must be add or sub with addend on the right"); 6306 ResOffset = Offset.ssub_ov(Addend, Ov); 6307 } 6308 6309 // We add an offset to a pointer here so we should support an offset as big as 6310 // possible. 6311 if (Ov) { 6312 assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 && 6313 "index (intermediate) result too big"); 6314 Offset = Offset.sext(2 * BitWidth); 6315 sumOffsets(Offset, Addend, BinOpKind, AddendIsRight); 6316 return; 6317 } 6318 6319 Offset = ResOffset; 6320 } 6321 6322 namespace { 6323 6324 // This is a wrapper class around StringLiteral to support offsetted string 6325 // literals as format strings. It takes the offset into account when returning 6326 // the string and its length or the source locations to display notes correctly. 6327 class FormatStringLiteral { 6328 const StringLiteral *FExpr; 6329 int64_t Offset; 6330 6331 public: 6332 FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0) 6333 : FExpr(fexpr), Offset(Offset) {} 6334 6335 StringRef getString() const { 6336 return FExpr->getString().drop_front(Offset); 6337 } 6338 6339 unsigned getByteLength() const { 6340 return FExpr->getByteLength() - getCharByteWidth() * Offset; 6341 } 6342 6343 unsigned getLength() const { return FExpr->getLength() - Offset; } 6344 unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); } 6345 6346 StringLiteral::StringKind getKind() const { return FExpr->getKind(); } 6347 6348 QualType getType() const { return FExpr->getType(); } 6349 6350 bool isAscii() const { return FExpr->isAscii(); } 6351 bool isWide() const { return FExpr->isWide(); } 6352 bool isUTF8() const { return FExpr->isUTF8(); } 6353 bool isUTF16() const { return FExpr->isUTF16(); } 6354 bool isUTF32() const { return FExpr->isUTF32(); } 6355 bool isPascal() const { return FExpr->isPascal(); } 6356 6357 SourceLocation getLocationOfByte( 6358 unsigned ByteNo, const SourceManager &SM, const LangOptions &Features, 6359 const TargetInfo &Target, unsigned *StartToken = nullptr, 6360 unsigned *StartTokenByteOffset = nullptr) const { 6361 return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target, 6362 StartToken, StartTokenByteOffset); 6363 } 6364 6365 SourceLocation getBeginLoc() const LLVM_READONLY { 6366 return FExpr->getBeginLoc().getLocWithOffset(Offset); 6367 } 6368 6369 SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); } 6370 }; 6371 6372 } // namespace 6373 6374 static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr, 6375 const Expr *OrigFormatExpr, 6376 ArrayRef<const Expr *> Args, 6377 bool HasVAListArg, unsigned format_idx, 6378 unsigned firstDataArg, 6379 Sema::FormatStringType Type, 6380 bool inFunctionCall, 6381 Sema::VariadicCallType CallType, 6382 llvm::SmallBitVector &CheckedVarArgs, 6383 UncoveredArgHandler &UncoveredArg); 6384 6385 // Determine if an expression is a string literal or constant string. 6386 // If this function returns false on the arguments to a function expecting a 6387 // format string, we will usually need to emit a warning. 6388 // True string literals are then checked by CheckFormatString. 6389 static StringLiteralCheckType 6390 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, 6391 bool HasVAListArg, unsigned format_idx, 6392 unsigned firstDataArg, Sema::FormatStringType Type, 6393 Sema::VariadicCallType CallType, bool InFunctionCall, 6394 llvm::SmallBitVector &CheckedVarArgs, 6395 UncoveredArgHandler &UncoveredArg, 6396 llvm::APSInt Offset) { 6397 tryAgain: 6398 assert(Offset.isSigned() && "invalid offset"); 6399 6400 if (E->isTypeDependent() || E->isValueDependent()) 6401 return SLCT_NotALiteral; 6402 6403 E = E->IgnoreParenCasts(); 6404 6405 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) 6406 // Technically -Wformat-nonliteral does not warn about this case. 6407 // The behavior of printf and friends in this case is implementation 6408 // dependent. Ideally if the format string cannot be null then 6409 // it should have a 'nonnull' attribute in the function prototype. 6410 return SLCT_UncheckedLiteral; 6411 6412 switch (E->getStmtClass()) { 6413 case Stmt::BinaryConditionalOperatorClass: 6414 case Stmt::ConditionalOperatorClass: { 6415 // The expression is a literal if both sub-expressions were, and it was 6416 // completely checked only if both sub-expressions were checked. 6417 const AbstractConditionalOperator *C = 6418 cast<AbstractConditionalOperator>(E); 6419 6420 // Determine whether it is necessary to check both sub-expressions, for 6421 // example, because the condition expression is a constant that can be 6422 // evaluated at compile time. 6423 bool CheckLeft = true, CheckRight = true; 6424 6425 bool Cond; 6426 if (C->getCond()->EvaluateAsBooleanCondition(Cond, S.getASTContext())) { 6427 if (Cond) 6428 CheckRight = false; 6429 else 6430 CheckLeft = false; 6431 } 6432 6433 // We need to maintain the offsets for the right and the left hand side 6434 // separately to check if every possible indexed expression is a valid 6435 // string literal. They might have different offsets for different string 6436 // literals in the end. 6437 StringLiteralCheckType Left; 6438 if (!CheckLeft) 6439 Left = SLCT_UncheckedLiteral; 6440 else { 6441 Left = checkFormatStringExpr(S, C->getTrueExpr(), Args, 6442 HasVAListArg, format_idx, firstDataArg, 6443 Type, CallType, InFunctionCall, 6444 CheckedVarArgs, UncoveredArg, Offset); 6445 if (Left == SLCT_NotALiteral || !CheckRight) { 6446 return Left; 6447 } 6448 } 6449 6450 StringLiteralCheckType Right = 6451 checkFormatStringExpr(S, C->getFalseExpr(), Args, 6452 HasVAListArg, format_idx, firstDataArg, 6453 Type, CallType, InFunctionCall, CheckedVarArgs, 6454 UncoveredArg, Offset); 6455 6456 return (CheckLeft && Left < Right) ? Left : Right; 6457 } 6458 6459 case Stmt::ImplicitCastExprClass: 6460 E = cast<ImplicitCastExpr>(E)->getSubExpr(); 6461 goto tryAgain; 6462 6463 case Stmt::OpaqueValueExprClass: 6464 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { 6465 E = src; 6466 goto tryAgain; 6467 } 6468 return SLCT_NotALiteral; 6469 6470 case Stmt::PredefinedExprClass: 6471 // While __func__, etc., are technically not string literals, they 6472 // cannot contain format specifiers and thus are not a security 6473 // liability. 6474 return SLCT_UncheckedLiteral; 6475 6476 case Stmt::DeclRefExprClass: { 6477 const DeclRefExpr *DR = cast<DeclRefExpr>(E); 6478 6479 // As an exception, do not flag errors for variables binding to 6480 // const string literals. 6481 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 6482 bool isConstant = false; 6483 QualType T = DR->getType(); 6484 6485 if (const ArrayType *AT = S.Context.getAsArrayType(T)) { 6486 isConstant = AT->getElementType().isConstant(S.Context); 6487 } else if (const PointerType *PT = T->getAs<PointerType>()) { 6488 isConstant = T.isConstant(S.Context) && 6489 PT->getPointeeType().isConstant(S.Context); 6490 } else if (T->isObjCObjectPointerType()) { 6491 // In ObjC, there is usually no "const ObjectPointer" type, 6492 // so don't check if the pointee type is constant. 6493 isConstant = T.isConstant(S.Context); 6494 } 6495 6496 if (isConstant) { 6497 if (const Expr *Init = VD->getAnyInitializer()) { 6498 // Look through initializers like const char c[] = { "foo" } 6499 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { 6500 if (InitList->isStringLiteralInit()) 6501 Init = InitList->getInit(0)->IgnoreParenImpCasts(); 6502 } 6503 return checkFormatStringExpr(S, Init, Args, 6504 HasVAListArg, format_idx, 6505 firstDataArg, Type, CallType, 6506 /*InFunctionCall*/ false, CheckedVarArgs, 6507 UncoveredArg, Offset); 6508 } 6509 } 6510 6511 // For vprintf* functions (i.e., HasVAListArg==true), we add a 6512 // special check to see if the format string is a function parameter 6513 // of the function calling the printf function. If the function 6514 // has an attribute indicating it is a printf-like function, then we 6515 // should suppress warnings concerning non-literals being used in a call 6516 // to a vprintf function. For example: 6517 // 6518 // void 6519 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ 6520 // va_list ap; 6521 // va_start(ap, fmt); 6522 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". 6523 // ... 6524 // } 6525 if (HasVAListArg) { 6526 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { 6527 if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { 6528 int PVIndex = PV->getFunctionScopeIndex() + 1; 6529 for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) { 6530 // adjust for implicit parameter 6531 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) 6532 if (MD->isInstance()) 6533 ++PVIndex; 6534 // We also check if the formats are compatible. 6535 // We can't pass a 'scanf' string to a 'printf' function. 6536 if (PVIndex == PVFormat->getFormatIdx() && 6537 Type == S.GetFormatStringType(PVFormat)) 6538 return SLCT_UncheckedLiteral; 6539 } 6540 } 6541 } 6542 } 6543 } 6544 6545 return SLCT_NotALiteral; 6546 } 6547 6548 case Stmt::CallExprClass: 6549 case Stmt::CXXMemberCallExprClass: { 6550 const CallExpr *CE = cast<CallExpr>(E); 6551 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { 6552 bool IsFirst = true; 6553 StringLiteralCheckType CommonResult; 6554 for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) { 6555 const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex()); 6556 StringLiteralCheckType Result = checkFormatStringExpr( 6557 S, Arg, Args, HasVAListArg, format_idx, firstDataArg, Type, 6558 CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset); 6559 if (IsFirst) { 6560 CommonResult = Result; 6561 IsFirst = false; 6562 } 6563 } 6564 if (!IsFirst) 6565 return CommonResult; 6566 6567 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { 6568 unsigned BuiltinID = FD->getBuiltinID(); 6569 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || 6570 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { 6571 const Expr *Arg = CE->getArg(0); 6572 return checkFormatStringExpr(S, Arg, Args, 6573 HasVAListArg, format_idx, 6574 firstDataArg, Type, CallType, 6575 InFunctionCall, CheckedVarArgs, 6576 UncoveredArg, Offset); 6577 } 6578 } 6579 } 6580 6581 return SLCT_NotALiteral; 6582 } 6583 case Stmt::ObjCMessageExprClass: { 6584 const auto *ME = cast<ObjCMessageExpr>(E); 6585 if (const auto *ND = ME->getMethodDecl()) { 6586 if (const auto *FA = ND->getAttr<FormatArgAttr>()) { 6587 const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex()); 6588 return checkFormatStringExpr( 6589 S, Arg, Args, HasVAListArg, format_idx, firstDataArg, Type, 6590 CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset); 6591 } 6592 } 6593 6594 return SLCT_NotALiteral; 6595 } 6596 case Stmt::ObjCStringLiteralClass: 6597 case Stmt::StringLiteralClass: { 6598 const StringLiteral *StrE = nullptr; 6599 6600 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) 6601 StrE = ObjCFExpr->getString(); 6602 else 6603 StrE = cast<StringLiteral>(E); 6604 6605 if (StrE) { 6606 if (Offset.isNegative() || Offset > StrE->getLength()) { 6607 // TODO: It would be better to have an explicit warning for out of 6608 // bounds literals. 6609 return SLCT_NotALiteral; 6610 } 6611 FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue()); 6612 CheckFormatString(S, &FStr, E, Args, HasVAListArg, format_idx, 6613 firstDataArg, Type, InFunctionCall, CallType, 6614 CheckedVarArgs, UncoveredArg); 6615 return SLCT_CheckedLiteral; 6616 } 6617 6618 return SLCT_NotALiteral; 6619 } 6620 case Stmt::BinaryOperatorClass: { 6621 const BinaryOperator *BinOp = cast<BinaryOperator>(E); 6622 6623 // A string literal + an int offset is still a string literal. 6624 if (BinOp->isAdditiveOp()) { 6625 Expr::EvalResult LResult, RResult; 6626 6627 bool LIsInt = BinOp->getLHS()->EvaluateAsInt(LResult, S.Context); 6628 bool RIsInt = BinOp->getRHS()->EvaluateAsInt(RResult, S.Context); 6629 6630 if (LIsInt != RIsInt) { 6631 BinaryOperatorKind BinOpKind = BinOp->getOpcode(); 6632 6633 if (LIsInt) { 6634 if (BinOpKind == BO_Add) { 6635 sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt); 6636 E = BinOp->getRHS(); 6637 goto tryAgain; 6638 } 6639 } else { 6640 sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt); 6641 E = BinOp->getLHS(); 6642 goto tryAgain; 6643 } 6644 } 6645 } 6646 6647 return SLCT_NotALiteral; 6648 } 6649 case Stmt::UnaryOperatorClass: { 6650 const UnaryOperator *UnaOp = cast<UnaryOperator>(E); 6651 auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr()); 6652 if (UnaOp->getOpcode() == UO_AddrOf && ASE) { 6653 Expr::EvalResult IndexResult; 6654 if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context)) { 6655 sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add, 6656 /*RHS is int*/ true); 6657 E = ASE->getBase(); 6658 goto tryAgain; 6659 } 6660 } 6661 6662 return SLCT_NotALiteral; 6663 } 6664 6665 default: 6666 return SLCT_NotALiteral; 6667 } 6668 } 6669 6670 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { 6671 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) 6672 .Case("scanf", FST_Scanf) 6673 .Cases("printf", "printf0", FST_Printf) 6674 .Cases("NSString", "CFString", FST_NSString) 6675 .Case("strftime", FST_Strftime) 6676 .Case("strfmon", FST_Strfmon) 6677 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) 6678 .Case("freebsd_kprintf", FST_FreeBSDKPrintf) 6679 .Case("os_trace", FST_OSLog) 6680 .Case("os_log", FST_OSLog) 6681 .Default(FST_Unknown); 6682 } 6683 6684 /// CheckFormatArguments - Check calls to printf and scanf (and similar 6685 /// functions) for correct use of format strings. 6686 /// Returns true if a format string has been fully checked. 6687 bool Sema::CheckFormatArguments(const FormatAttr *Format, 6688 ArrayRef<const Expr *> Args, 6689 bool IsCXXMember, 6690 VariadicCallType CallType, 6691 SourceLocation Loc, SourceRange Range, 6692 llvm::SmallBitVector &CheckedVarArgs) { 6693 FormatStringInfo FSI; 6694 if (getFormatStringInfo(Format, IsCXXMember, &FSI)) 6695 return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx, 6696 FSI.FirstDataArg, GetFormatStringType(Format), 6697 CallType, Loc, Range, CheckedVarArgs); 6698 return false; 6699 } 6700 6701 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, 6702 bool HasVAListArg, unsigned format_idx, 6703 unsigned firstDataArg, FormatStringType Type, 6704 VariadicCallType CallType, 6705 SourceLocation Loc, SourceRange Range, 6706 llvm::SmallBitVector &CheckedVarArgs) { 6707 // CHECK: printf/scanf-like function is called with no format string. 6708 if (format_idx >= Args.size()) { 6709 Diag(Loc, diag::warn_missing_format_string) << Range; 6710 return false; 6711 } 6712 6713 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); 6714 6715 // CHECK: format string is not a string literal. 6716 // 6717 // Dynamically generated format strings are difficult to 6718 // automatically vet at compile time. Requiring that format strings 6719 // are string literals: (1) permits the checking of format strings by 6720 // the compiler and thereby (2) can practically remove the source of 6721 // many format string exploits. 6722 6723 // Format string can be either ObjC string (e.g. @"%d") or 6724 // C string (e.g. "%d") 6725 // ObjC string uses the same format specifiers as C string, so we can use 6726 // the same format string checking logic for both ObjC and C strings. 6727 UncoveredArgHandler UncoveredArg; 6728 StringLiteralCheckType CT = 6729 checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg, 6730 format_idx, firstDataArg, Type, CallType, 6731 /*IsFunctionCall*/ true, CheckedVarArgs, 6732 UncoveredArg, 6733 /*no string offset*/ llvm::APSInt(64, false) = 0); 6734 6735 // Generate a diagnostic where an uncovered argument is detected. 6736 if (UncoveredArg.hasUncoveredArg()) { 6737 unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg; 6738 assert(ArgIdx < Args.size() && "ArgIdx outside bounds"); 6739 UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]); 6740 } 6741 6742 if (CT != SLCT_NotALiteral) 6743 // Literal format string found, check done! 6744 return CT == SLCT_CheckedLiteral; 6745 6746 // Strftime is particular as it always uses a single 'time' argument, 6747 // so it is safe to pass a non-literal string. 6748 if (Type == FST_Strftime) 6749 return false; 6750 6751 // Do not emit diag when the string param is a macro expansion and the 6752 // format is either NSString or CFString. This is a hack to prevent 6753 // diag when using the NSLocalizedString and CFCopyLocalizedString macros 6754 // which are usually used in place of NS and CF string literals. 6755 SourceLocation FormatLoc = Args[format_idx]->getBeginLoc(); 6756 if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc)) 6757 return false; 6758 6759 // If there are no arguments specified, warn with -Wformat-security, otherwise 6760 // warn only with -Wformat-nonliteral. 6761 if (Args.size() == firstDataArg) { 6762 Diag(FormatLoc, diag::warn_format_nonliteral_noargs) 6763 << OrigFormatExpr->getSourceRange(); 6764 switch (Type) { 6765 default: 6766 break; 6767 case FST_Kprintf: 6768 case FST_FreeBSDKPrintf: 6769 case FST_Printf: 6770 Diag(FormatLoc, diag::note_format_security_fixit) 6771 << FixItHint::CreateInsertion(FormatLoc, "\"%s\", "); 6772 break; 6773 case FST_NSString: 6774 Diag(FormatLoc, diag::note_format_security_fixit) 6775 << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", "); 6776 break; 6777 } 6778 } else { 6779 Diag(FormatLoc, diag::warn_format_nonliteral) 6780 << OrigFormatExpr->getSourceRange(); 6781 } 6782 return false; 6783 } 6784 6785 namespace { 6786 6787 class CheckFormatHandler : public analyze_format_string::FormatStringHandler { 6788 protected: 6789 Sema &S; 6790 const FormatStringLiteral *FExpr; 6791 const Expr *OrigFormatExpr; 6792 const Sema::FormatStringType FSType; 6793 const unsigned FirstDataArg; 6794 const unsigned NumDataArgs; 6795 const char *Beg; // Start of format string. 6796 const bool HasVAListArg; 6797 ArrayRef<const Expr *> Args; 6798 unsigned FormatIdx; 6799 llvm::SmallBitVector CoveredArgs; 6800 bool usesPositionalArgs = false; 6801 bool atFirstArg = true; 6802 bool inFunctionCall; 6803 Sema::VariadicCallType CallType; 6804 llvm::SmallBitVector &CheckedVarArgs; 6805 UncoveredArgHandler &UncoveredArg; 6806 6807 public: 6808 CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr, 6809 const Expr *origFormatExpr, 6810 const Sema::FormatStringType type, unsigned firstDataArg, 6811 unsigned numDataArgs, const char *beg, bool hasVAListArg, 6812 ArrayRef<const Expr *> Args, unsigned formatIdx, 6813 bool inFunctionCall, Sema::VariadicCallType callType, 6814 llvm::SmallBitVector &CheckedVarArgs, 6815 UncoveredArgHandler &UncoveredArg) 6816 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type), 6817 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg), 6818 HasVAListArg(hasVAListArg), Args(Args), FormatIdx(formatIdx), 6819 inFunctionCall(inFunctionCall), CallType(callType), 6820 CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) { 6821 CoveredArgs.resize(numDataArgs); 6822 CoveredArgs.reset(); 6823 } 6824 6825 void DoneProcessing(); 6826 6827 void HandleIncompleteSpecifier(const char *startSpecifier, 6828 unsigned specifierLen) override; 6829 6830 void HandleInvalidLengthModifier( 6831 const analyze_format_string::FormatSpecifier &FS, 6832 const analyze_format_string::ConversionSpecifier &CS, 6833 const char *startSpecifier, unsigned specifierLen, 6834 unsigned DiagID); 6835 6836 void HandleNonStandardLengthModifier( 6837 const analyze_format_string::FormatSpecifier &FS, 6838 const char *startSpecifier, unsigned specifierLen); 6839 6840 void HandleNonStandardConversionSpecifier( 6841 const analyze_format_string::ConversionSpecifier &CS, 6842 const char *startSpecifier, unsigned specifierLen); 6843 6844 void HandlePosition(const char *startPos, unsigned posLen) override; 6845 6846 void HandleInvalidPosition(const char *startSpecifier, 6847 unsigned specifierLen, 6848 analyze_format_string::PositionContext p) override; 6849 6850 void HandleZeroPosition(const char *startPos, unsigned posLen) override; 6851 6852 void HandleNullChar(const char *nullCharacter) override; 6853 6854 template <typename Range> 6855 static void 6856 EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr, 6857 const PartialDiagnostic &PDiag, SourceLocation StringLoc, 6858 bool IsStringLocation, Range StringRange, 6859 ArrayRef<FixItHint> Fixit = None); 6860 6861 protected: 6862 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, 6863 const char *startSpec, 6864 unsigned specifierLen, 6865 const char *csStart, unsigned csLen); 6866 6867 void HandlePositionalNonpositionalArgs(SourceLocation Loc, 6868 const char *startSpec, 6869 unsigned specifierLen); 6870 6871 SourceRange getFormatStringRange(); 6872 CharSourceRange getSpecifierRange(const char *startSpecifier, 6873 unsigned specifierLen); 6874 SourceLocation getLocationOfByte(const char *x); 6875 6876 const Expr *getDataArg(unsigned i) const; 6877 6878 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, 6879 const analyze_format_string::ConversionSpecifier &CS, 6880 const char *startSpecifier, unsigned specifierLen, 6881 unsigned argIndex); 6882 6883 template <typename Range> 6884 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, 6885 bool IsStringLocation, Range StringRange, 6886 ArrayRef<FixItHint> Fixit = None); 6887 }; 6888 6889 } // namespace 6890 6891 SourceRange CheckFormatHandler::getFormatStringRange() { 6892 return OrigFormatExpr->getSourceRange(); 6893 } 6894 6895 CharSourceRange CheckFormatHandler:: 6896 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { 6897 SourceLocation Start = getLocationOfByte(startSpecifier); 6898 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); 6899 6900 // Advance the end SourceLocation by one due to half-open ranges. 6901 End = End.getLocWithOffset(1); 6902 6903 return CharSourceRange::getCharRange(Start, End); 6904 } 6905 6906 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { 6907 return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(), 6908 S.getLangOpts(), S.Context.getTargetInfo()); 6909 } 6910 6911 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, 6912 unsigned specifierLen){ 6913 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), 6914 getLocationOfByte(startSpecifier), 6915 /*IsStringLocation*/true, 6916 getSpecifierRange(startSpecifier, specifierLen)); 6917 } 6918 6919 void CheckFormatHandler::HandleInvalidLengthModifier( 6920 const analyze_format_string::FormatSpecifier &FS, 6921 const analyze_format_string::ConversionSpecifier &CS, 6922 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { 6923 using namespace analyze_format_string; 6924 6925 const LengthModifier &LM = FS.getLengthModifier(); 6926 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6927 6928 // See if we know how to fix this length modifier. 6929 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6930 if (FixedLM) { 6931 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6932 getLocationOfByte(LM.getStart()), 6933 /*IsStringLocation*/true, 6934 getSpecifierRange(startSpecifier, specifierLen)); 6935 6936 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6937 << FixedLM->toString() 6938 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6939 6940 } else { 6941 FixItHint Hint; 6942 if (DiagID == diag::warn_format_nonsensical_length) 6943 Hint = FixItHint::CreateRemoval(LMRange); 6944 6945 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6946 getLocationOfByte(LM.getStart()), 6947 /*IsStringLocation*/true, 6948 getSpecifierRange(startSpecifier, specifierLen), 6949 Hint); 6950 } 6951 } 6952 6953 void CheckFormatHandler::HandleNonStandardLengthModifier( 6954 const analyze_format_string::FormatSpecifier &FS, 6955 const char *startSpecifier, unsigned specifierLen) { 6956 using namespace analyze_format_string; 6957 6958 const LengthModifier &LM = FS.getLengthModifier(); 6959 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6960 6961 // See if we know how to fix this length modifier. 6962 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6963 if (FixedLM) { 6964 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6965 << LM.toString() << 0, 6966 getLocationOfByte(LM.getStart()), 6967 /*IsStringLocation*/true, 6968 getSpecifierRange(startSpecifier, specifierLen)); 6969 6970 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6971 << FixedLM->toString() 6972 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6973 6974 } else { 6975 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6976 << LM.toString() << 0, 6977 getLocationOfByte(LM.getStart()), 6978 /*IsStringLocation*/true, 6979 getSpecifierRange(startSpecifier, specifierLen)); 6980 } 6981 } 6982 6983 void CheckFormatHandler::HandleNonStandardConversionSpecifier( 6984 const analyze_format_string::ConversionSpecifier &CS, 6985 const char *startSpecifier, unsigned specifierLen) { 6986 using namespace analyze_format_string; 6987 6988 // See if we know how to fix this conversion specifier. 6989 Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); 6990 if (FixedCS) { 6991 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6992 << CS.toString() << /*conversion specifier*/1, 6993 getLocationOfByte(CS.getStart()), 6994 /*IsStringLocation*/true, 6995 getSpecifierRange(startSpecifier, specifierLen)); 6996 6997 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); 6998 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) 6999 << FixedCS->toString() 7000 << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); 7001 } else { 7002 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 7003 << CS.toString() << /*conversion specifier*/1, 7004 getLocationOfByte(CS.getStart()), 7005 /*IsStringLocation*/true, 7006 getSpecifierRange(startSpecifier, specifierLen)); 7007 } 7008 } 7009 7010 void CheckFormatHandler::HandlePosition(const char *startPos, 7011 unsigned posLen) { 7012 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), 7013 getLocationOfByte(startPos), 7014 /*IsStringLocation*/true, 7015 getSpecifierRange(startPos, posLen)); 7016 } 7017 7018 void 7019 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, 7020 analyze_format_string::PositionContext p) { 7021 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) 7022 << (unsigned) p, 7023 getLocationOfByte(startPos), /*IsStringLocation*/true, 7024 getSpecifierRange(startPos, posLen)); 7025 } 7026 7027 void CheckFormatHandler::HandleZeroPosition(const char *startPos, 7028 unsigned posLen) { 7029 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), 7030 getLocationOfByte(startPos), 7031 /*IsStringLocation*/true, 7032 getSpecifierRange(startPos, posLen)); 7033 } 7034 7035 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { 7036 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { 7037 // The presence of a null character is likely an error. 7038 EmitFormatDiagnostic( 7039 S.PDiag(diag::warn_printf_format_string_contains_null_char), 7040 getLocationOfByte(nullCharacter), /*IsStringLocation*/true, 7041 getFormatStringRange()); 7042 } 7043 } 7044 7045 // Note that this may return NULL if there was an error parsing or building 7046 // one of the argument expressions. 7047 const Expr *CheckFormatHandler::getDataArg(unsigned i) const { 7048 return Args[FirstDataArg + i]; 7049 } 7050 7051 void CheckFormatHandler::DoneProcessing() { 7052 // Does the number of data arguments exceed the number of 7053 // format conversions in the format string? 7054 if (!HasVAListArg) { 7055 // Find any arguments that weren't covered. 7056 CoveredArgs.flip(); 7057 signed notCoveredArg = CoveredArgs.find_first(); 7058 if (notCoveredArg >= 0) { 7059 assert((unsigned)notCoveredArg < NumDataArgs); 7060 UncoveredArg.Update(notCoveredArg, OrigFormatExpr); 7061 } else { 7062 UncoveredArg.setAllCovered(); 7063 } 7064 } 7065 } 7066 7067 void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall, 7068 const Expr *ArgExpr) { 7069 assert(hasUncoveredArg() && DiagnosticExprs.size() > 0 && 7070 "Invalid state"); 7071 7072 if (!ArgExpr) 7073 return; 7074 7075 SourceLocation Loc = ArgExpr->getBeginLoc(); 7076 7077 if (S.getSourceManager().isInSystemMacro(Loc)) 7078 return; 7079 7080 PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used); 7081 for (auto E : DiagnosticExprs) 7082 PDiag << E->getSourceRange(); 7083 7084 CheckFormatHandler::EmitFormatDiagnostic( 7085 S, IsFunctionCall, DiagnosticExprs[0], 7086 PDiag, Loc, /*IsStringLocation*/false, 7087 DiagnosticExprs[0]->getSourceRange()); 7088 } 7089 7090 bool 7091 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, 7092 SourceLocation Loc, 7093 const char *startSpec, 7094 unsigned specifierLen, 7095 const char *csStart, 7096 unsigned csLen) { 7097 bool keepGoing = true; 7098 if (argIndex < NumDataArgs) { 7099 // Consider the argument coverered, even though the specifier doesn't 7100 // make sense. 7101 CoveredArgs.set(argIndex); 7102 } 7103 else { 7104 // If argIndex exceeds the number of data arguments we 7105 // don't issue a warning because that is just a cascade of warnings (and 7106 // they may have intended '%%' anyway). We don't want to continue processing 7107 // the format string after this point, however, as we will like just get 7108 // gibberish when trying to match arguments. 7109 keepGoing = false; 7110 } 7111 7112 StringRef Specifier(csStart, csLen); 7113 7114 // If the specifier in non-printable, it could be the first byte of a UTF-8 7115 // sequence. In that case, print the UTF-8 code point. If not, print the byte 7116 // hex value. 7117 std::string CodePointStr; 7118 if (!llvm::sys::locale::isPrint(*csStart)) { 7119 llvm::UTF32 CodePoint; 7120 const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart); 7121 const llvm::UTF8 *E = 7122 reinterpret_cast<const llvm::UTF8 *>(csStart + csLen); 7123 llvm::ConversionResult Result = 7124 llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion); 7125 7126 if (Result != llvm::conversionOK) { 7127 unsigned char FirstChar = *csStart; 7128 CodePoint = (llvm::UTF32)FirstChar; 7129 } 7130 7131 llvm::raw_string_ostream OS(CodePointStr); 7132 if (CodePoint < 256) 7133 OS << "\\x" << llvm::format("%02x", CodePoint); 7134 else if (CodePoint <= 0xFFFF) 7135 OS << "\\u" << llvm::format("%04x", CodePoint); 7136 else 7137 OS << "\\U" << llvm::format("%08x", CodePoint); 7138 OS.flush(); 7139 Specifier = CodePointStr; 7140 } 7141 7142 EmitFormatDiagnostic( 7143 S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc, 7144 /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen)); 7145 7146 return keepGoing; 7147 } 7148 7149 void 7150 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, 7151 const char *startSpec, 7152 unsigned specifierLen) { 7153 EmitFormatDiagnostic( 7154 S.PDiag(diag::warn_format_mix_positional_nonpositional_args), 7155 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); 7156 } 7157 7158 bool 7159 CheckFormatHandler::CheckNumArgs( 7160 const analyze_format_string::FormatSpecifier &FS, 7161 const analyze_format_string::ConversionSpecifier &CS, 7162 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { 7163 7164 if (argIndex >= NumDataArgs) { 7165 PartialDiagnostic PDiag = FS.usesPositionalArg() 7166 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) 7167 << (argIndex+1) << NumDataArgs) 7168 : S.PDiag(diag::warn_printf_insufficient_data_args); 7169 EmitFormatDiagnostic( 7170 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, 7171 getSpecifierRange(startSpecifier, specifierLen)); 7172 7173 // Since more arguments than conversion tokens are given, by extension 7174 // all arguments are covered, so mark this as so. 7175 UncoveredArg.setAllCovered(); 7176 return false; 7177 } 7178 return true; 7179 } 7180 7181 template<typename Range> 7182 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, 7183 SourceLocation Loc, 7184 bool IsStringLocation, 7185 Range StringRange, 7186 ArrayRef<FixItHint> FixIt) { 7187 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, 7188 Loc, IsStringLocation, StringRange, FixIt); 7189 } 7190 7191 /// If the format string is not within the function call, emit a note 7192 /// so that the function call and string are in diagnostic messages. 7193 /// 7194 /// \param InFunctionCall if true, the format string is within the function 7195 /// call and only one diagnostic message will be produced. Otherwise, an 7196 /// extra note will be emitted pointing to location of the format string. 7197 /// 7198 /// \param ArgumentExpr the expression that is passed as the format string 7199 /// argument in the function call. Used for getting locations when two 7200 /// diagnostics are emitted. 7201 /// 7202 /// \param PDiag the callee should already have provided any strings for the 7203 /// diagnostic message. This function only adds locations and fixits 7204 /// to diagnostics. 7205 /// 7206 /// \param Loc primary location for diagnostic. If two diagnostics are 7207 /// required, one will be at Loc and a new SourceLocation will be created for 7208 /// the other one. 7209 /// 7210 /// \param IsStringLocation if true, Loc points to the format string should be 7211 /// used for the note. Otherwise, Loc points to the argument list and will 7212 /// be used with PDiag. 7213 /// 7214 /// \param StringRange some or all of the string to highlight. This is 7215 /// templated so it can accept either a CharSourceRange or a SourceRange. 7216 /// 7217 /// \param FixIt optional fix it hint for the format string. 7218 template <typename Range> 7219 void CheckFormatHandler::EmitFormatDiagnostic( 7220 Sema &S, bool InFunctionCall, const Expr *ArgumentExpr, 7221 const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation, 7222 Range StringRange, ArrayRef<FixItHint> FixIt) { 7223 if (InFunctionCall) { 7224 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); 7225 D << StringRange; 7226 D << FixIt; 7227 } else { 7228 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) 7229 << ArgumentExpr->getSourceRange(); 7230 7231 const Sema::SemaDiagnosticBuilder &Note = 7232 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), 7233 diag::note_format_string_defined); 7234 7235 Note << StringRange; 7236 Note << FixIt; 7237 } 7238 } 7239 7240 //===--- CHECK: Printf format string checking ------------------------------===// 7241 7242 namespace { 7243 7244 class CheckPrintfHandler : public CheckFormatHandler { 7245 public: 7246 CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr, 7247 const Expr *origFormatExpr, 7248 const Sema::FormatStringType type, unsigned firstDataArg, 7249 unsigned numDataArgs, bool isObjC, const char *beg, 7250 bool hasVAListArg, ArrayRef<const Expr *> Args, 7251 unsigned formatIdx, bool inFunctionCall, 7252 Sema::VariadicCallType CallType, 7253 llvm::SmallBitVector &CheckedVarArgs, 7254 UncoveredArgHandler &UncoveredArg) 7255 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 7256 numDataArgs, beg, hasVAListArg, Args, formatIdx, 7257 inFunctionCall, CallType, CheckedVarArgs, 7258 UncoveredArg) {} 7259 7260 bool isObjCContext() const { return FSType == Sema::FST_NSString; } 7261 7262 /// Returns true if '%@' specifiers are allowed in the format string. 7263 bool allowsObjCArg() const { 7264 return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog || 7265 FSType == Sema::FST_OSTrace; 7266 } 7267 7268 bool HandleInvalidPrintfConversionSpecifier( 7269 const analyze_printf::PrintfSpecifier &FS, 7270 const char *startSpecifier, 7271 unsigned specifierLen) override; 7272 7273 void handleInvalidMaskType(StringRef MaskType) override; 7274 7275 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 7276 const char *startSpecifier, 7277 unsigned specifierLen) override; 7278 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 7279 const char *StartSpecifier, 7280 unsigned SpecifierLen, 7281 const Expr *E); 7282 7283 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, 7284 const char *startSpecifier, unsigned specifierLen); 7285 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, 7286 const analyze_printf::OptionalAmount &Amt, 7287 unsigned type, 7288 const char *startSpecifier, unsigned specifierLen); 7289 void HandleFlag(const analyze_printf::PrintfSpecifier &FS, 7290 const analyze_printf::OptionalFlag &flag, 7291 const char *startSpecifier, unsigned specifierLen); 7292 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, 7293 const analyze_printf::OptionalFlag &ignoredFlag, 7294 const analyze_printf::OptionalFlag &flag, 7295 const char *startSpecifier, unsigned specifierLen); 7296 bool checkForCStrMembers(const analyze_printf::ArgType &AT, 7297 const Expr *E); 7298 7299 void HandleEmptyObjCModifierFlag(const char *startFlag, 7300 unsigned flagLen) override; 7301 7302 void HandleInvalidObjCModifierFlag(const char *startFlag, 7303 unsigned flagLen) override; 7304 7305 void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, 7306 const char *flagsEnd, 7307 const char *conversionPosition) 7308 override; 7309 }; 7310 7311 } // namespace 7312 7313 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( 7314 const analyze_printf::PrintfSpecifier &FS, 7315 const char *startSpecifier, 7316 unsigned specifierLen) { 7317 const analyze_printf::PrintfConversionSpecifier &CS = 7318 FS.getConversionSpecifier(); 7319 7320 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 7321 getLocationOfByte(CS.getStart()), 7322 startSpecifier, specifierLen, 7323 CS.getStart(), CS.getLength()); 7324 } 7325 7326 void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) { 7327 S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size); 7328 } 7329 7330 bool CheckPrintfHandler::HandleAmount( 7331 const analyze_format_string::OptionalAmount &Amt, 7332 unsigned k, const char *startSpecifier, 7333 unsigned specifierLen) { 7334 if (Amt.hasDataArgument()) { 7335 if (!HasVAListArg) { 7336 unsigned argIndex = Amt.getArgIndex(); 7337 if (argIndex >= NumDataArgs) { 7338 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) 7339 << k, 7340 getLocationOfByte(Amt.getStart()), 7341 /*IsStringLocation*/true, 7342 getSpecifierRange(startSpecifier, specifierLen)); 7343 // Don't do any more checking. We will just emit 7344 // spurious errors. 7345 return false; 7346 } 7347 7348 // Type check the data argument. It should be an 'int'. 7349 // Although not in conformance with C99, we also allow the argument to be 7350 // an 'unsigned int' as that is a reasonably safe case. GCC also 7351 // doesn't emit a warning for that case. 7352 CoveredArgs.set(argIndex); 7353 const Expr *Arg = getDataArg(argIndex); 7354 if (!Arg) 7355 return false; 7356 7357 QualType T = Arg->getType(); 7358 7359 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); 7360 assert(AT.isValid()); 7361 7362 if (!AT.matchesType(S.Context, T)) { 7363 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) 7364 << k << AT.getRepresentativeTypeName(S.Context) 7365 << T << Arg->getSourceRange(), 7366 getLocationOfByte(Amt.getStart()), 7367 /*IsStringLocation*/true, 7368 getSpecifierRange(startSpecifier, specifierLen)); 7369 // Don't do any more checking. We will just emit 7370 // spurious errors. 7371 return false; 7372 } 7373 } 7374 } 7375 return true; 7376 } 7377 7378 void CheckPrintfHandler::HandleInvalidAmount( 7379 const analyze_printf::PrintfSpecifier &FS, 7380 const analyze_printf::OptionalAmount &Amt, 7381 unsigned type, 7382 const char *startSpecifier, 7383 unsigned specifierLen) { 7384 const analyze_printf::PrintfConversionSpecifier &CS = 7385 FS.getConversionSpecifier(); 7386 7387 FixItHint fixit = 7388 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant 7389 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), 7390 Amt.getConstantLength())) 7391 : FixItHint(); 7392 7393 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) 7394 << type << CS.toString(), 7395 getLocationOfByte(Amt.getStart()), 7396 /*IsStringLocation*/true, 7397 getSpecifierRange(startSpecifier, specifierLen), 7398 fixit); 7399 } 7400 7401 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, 7402 const analyze_printf::OptionalFlag &flag, 7403 const char *startSpecifier, 7404 unsigned specifierLen) { 7405 // Warn about pointless flag with a fixit removal. 7406 const analyze_printf::PrintfConversionSpecifier &CS = 7407 FS.getConversionSpecifier(); 7408 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) 7409 << flag.toString() << CS.toString(), 7410 getLocationOfByte(flag.getPosition()), 7411 /*IsStringLocation*/true, 7412 getSpecifierRange(startSpecifier, specifierLen), 7413 FixItHint::CreateRemoval( 7414 getSpecifierRange(flag.getPosition(), 1))); 7415 } 7416 7417 void CheckPrintfHandler::HandleIgnoredFlag( 7418 const analyze_printf::PrintfSpecifier &FS, 7419 const analyze_printf::OptionalFlag &ignoredFlag, 7420 const analyze_printf::OptionalFlag &flag, 7421 const char *startSpecifier, 7422 unsigned specifierLen) { 7423 // Warn about ignored flag with a fixit removal. 7424 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) 7425 << ignoredFlag.toString() << flag.toString(), 7426 getLocationOfByte(ignoredFlag.getPosition()), 7427 /*IsStringLocation*/true, 7428 getSpecifierRange(startSpecifier, specifierLen), 7429 FixItHint::CreateRemoval( 7430 getSpecifierRange(ignoredFlag.getPosition(), 1))); 7431 } 7432 7433 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag, 7434 unsigned flagLen) { 7435 // Warn about an empty flag. 7436 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag), 7437 getLocationOfByte(startFlag), 7438 /*IsStringLocation*/true, 7439 getSpecifierRange(startFlag, flagLen)); 7440 } 7441 7442 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag, 7443 unsigned flagLen) { 7444 // Warn about an invalid flag. 7445 auto Range = getSpecifierRange(startFlag, flagLen); 7446 StringRef flag(startFlag, flagLen); 7447 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag, 7448 getLocationOfByte(startFlag), 7449 /*IsStringLocation*/true, 7450 Range, FixItHint::CreateRemoval(Range)); 7451 } 7452 7453 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion( 7454 const char *flagsStart, const char *flagsEnd, const char *conversionPosition) { 7455 // Warn about using '[...]' without a '@' conversion. 7456 auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1); 7457 auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion; 7458 EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1), 7459 getLocationOfByte(conversionPosition), 7460 /*IsStringLocation*/true, 7461 Range, FixItHint::CreateRemoval(Range)); 7462 } 7463 7464 // Determines if the specified is a C++ class or struct containing 7465 // a member with the specified name and kind (e.g. a CXXMethodDecl named 7466 // "c_str()"). 7467 template<typename MemberKind> 7468 static llvm::SmallPtrSet<MemberKind*, 1> 7469 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { 7470 const RecordType *RT = Ty->getAs<RecordType>(); 7471 llvm::SmallPtrSet<MemberKind*, 1> Results; 7472 7473 if (!RT) 7474 return Results; 7475 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 7476 if (!RD || !RD->getDefinition()) 7477 return Results; 7478 7479 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), 7480 Sema::LookupMemberName); 7481 R.suppressDiagnostics(); 7482 7483 // We just need to include all members of the right kind turned up by the 7484 // filter, at this point. 7485 if (S.LookupQualifiedName(R, RT->getDecl())) 7486 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 7487 NamedDecl *decl = (*I)->getUnderlyingDecl(); 7488 if (MemberKind *FK = dyn_cast<MemberKind>(decl)) 7489 Results.insert(FK); 7490 } 7491 return Results; 7492 } 7493 7494 /// Check if we could call '.c_str()' on an object. 7495 /// 7496 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't 7497 /// allow the call, or if it would be ambiguous). 7498 bool Sema::hasCStrMethod(const Expr *E) { 7499 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 7500 7501 MethodSet Results = 7502 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); 7503 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 7504 MI != ME; ++MI) 7505 if ((*MI)->getMinRequiredArguments() == 0) 7506 return true; 7507 return false; 7508 } 7509 7510 // Check if a (w)string was passed when a (w)char* was needed, and offer a 7511 // better diagnostic if so. AT is assumed to be valid. 7512 // Returns true when a c_str() conversion method is found. 7513 bool CheckPrintfHandler::checkForCStrMembers( 7514 const analyze_printf::ArgType &AT, const Expr *E) { 7515 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 7516 7517 MethodSet Results = 7518 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); 7519 7520 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 7521 MI != ME; ++MI) { 7522 const CXXMethodDecl *Method = *MI; 7523 if (Method->getMinRequiredArguments() == 0 && 7524 AT.matchesType(S.Context, Method->getReturnType())) { 7525 // FIXME: Suggest parens if the expression needs them. 7526 SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc()); 7527 S.Diag(E->getBeginLoc(), diag::note_printf_c_str) 7528 << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()"); 7529 return true; 7530 } 7531 } 7532 7533 return false; 7534 } 7535 7536 bool 7537 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier 7538 &FS, 7539 const char *startSpecifier, 7540 unsigned specifierLen) { 7541 using namespace analyze_format_string; 7542 using namespace analyze_printf; 7543 7544 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); 7545 7546 if (FS.consumesDataArgument()) { 7547 if (atFirstArg) { 7548 atFirstArg = false; 7549 usesPositionalArgs = FS.usesPositionalArg(); 7550 } 7551 else if (usesPositionalArgs != FS.usesPositionalArg()) { 7552 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 7553 startSpecifier, specifierLen); 7554 return false; 7555 } 7556 } 7557 7558 // First check if the field width, precision, and conversion specifier 7559 // have matching data arguments. 7560 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, 7561 startSpecifier, specifierLen)) { 7562 return false; 7563 } 7564 7565 if (!HandleAmount(FS.getPrecision(), /* precision */ 1, 7566 startSpecifier, specifierLen)) { 7567 return false; 7568 } 7569 7570 if (!CS.consumesDataArgument()) { 7571 // FIXME: Technically specifying a precision or field width here 7572 // makes no sense. Worth issuing a warning at some point. 7573 return true; 7574 } 7575 7576 // Consume the argument. 7577 unsigned argIndex = FS.getArgIndex(); 7578 if (argIndex < NumDataArgs) { 7579 // The check to see if the argIndex is valid will come later. 7580 // We set the bit here because we may exit early from this 7581 // function if we encounter some other error. 7582 CoveredArgs.set(argIndex); 7583 } 7584 7585 // FreeBSD kernel extensions. 7586 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || 7587 CS.getKind() == ConversionSpecifier::FreeBSDDArg) { 7588 // We need at least two arguments. 7589 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) 7590 return false; 7591 7592 // Claim the second argument. 7593 CoveredArgs.set(argIndex + 1); 7594 7595 // Type check the first argument (int for %b, pointer for %D) 7596 const Expr *Ex = getDataArg(argIndex); 7597 const analyze_printf::ArgType &AT = 7598 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? 7599 ArgType(S.Context.IntTy) : ArgType::CPointerTy; 7600 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) 7601 EmitFormatDiagnostic( 7602 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7603 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() 7604 << false << Ex->getSourceRange(), 7605 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7606 getSpecifierRange(startSpecifier, specifierLen)); 7607 7608 // Type check the second argument (char * for both %b and %D) 7609 Ex = getDataArg(argIndex + 1); 7610 const analyze_printf::ArgType &AT2 = ArgType::CStrTy; 7611 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) 7612 EmitFormatDiagnostic( 7613 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7614 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() 7615 << false << Ex->getSourceRange(), 7616 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7617 getSpecifierRange(startSpecifier, specifierLen)); 7618 7619 return true; 7620 } 7621 7622 // Check for using an Objective-C specific conversion specifier 7623 // in a non-ObjC literal. 7624 if (!allowsObjCArg() && CS.isObjCArg()) { 7625 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7626 specifierLen); 7627 } 7628 7629 // %P can only be used with os_log. 7630 if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) { 7631 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7632 specifierLen); 7633 } 7634 7635 // %n is not allowed with os_log. 7636 if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) { 7637 EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg), 7638 getLocationOfByte(CS.getStart()), 7639 /*IsStringLocation*/ false, 7640 getSpecifierRange(startSpecifier, specifierLen)); 7641 7642 return true; 7643 } 7644 7645 // Only scalars are allowed for os_trace. 7646 if (FSType == Sema::FST_OSTrace && 7647 (CS.getKind() == ConversionSpecifier::PArg || 7648 CS.getKind() == ConversionSpecifier::sArg || 7649 CS.getKind() == ConversionSpecifier::ObjCObjArg)) { 7650 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7651 specifierLen); 7652 } 7653 7654 // Check for use of public/private annotation outside of os_log(). 7655 if (FSType != Sema::FST_OSLog) { 7656 if (FS.isPublic().isSet()) { 7657 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7658 << "public", 7659 getLocationOfByte(FS.isPublic().getPosition()), 7660 /*IsStringLocation*/ false, 7661 getSpecifierRange(startSpecifier, specifierLen)); 7662 } 7663 if (FS.isPrivate().isSet()) { 7664 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7665 << "private", 7666 getLocationOfByte(FS.isPrivate().getPosition()), 7667 /*IsStringLocation*/ false, 7668 getSpecifierRange(startSpecifier, specifierLen)); 7669 } 7670 } 7671 7672 // Check for invalid use of field width 7673 if (!FS.hasValidFieldWidth()) { 7674 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, 7675 startSpecifier, specifierLen); 7676 } 7677 7678 // Check for invalid use of precision 7679 if (!FS.hasValidPrecision()) { 7680 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, 7681 startSpecifier, specifierLen); 7682 } 7683 7684 // Precision is mandatory for %P specifier. 7685 if (CS.getKind() == ConversionSpecifier::PArg && 7686 FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) { 7687 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision), 7688 getLocationOfByte(startSpecifier), 7689 /*IsStringLocation*/ false, 7690 getSpecifierRange(startSpecifier, specifierLen)); 7691 } 7692 7693 // Check each flag does not conflict with any other component. 7694 if (!FS.hasValidThousandsGroupingPrefix()) 7695 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); 7696 if (!FS.hasValidLeadingZeros()) 7697 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); 7698 if (!FS.hasValidPlusPrefix()) 7699 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); 7700 if (!FS.hasValidSpacePrefix()) 7701 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); 7702 if (!FS.hasValidAlternativeForm()) 7703 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); 7704 if (!FS.hasValidLeftJustified()) 7705 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); 7706 7707 // Check that flags are not ignored by another flag 7708 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' 7709 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), 7710 startSpecifier, specifierLen); 7711 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' 7712 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), 7713 startSpecifier, specifierLen); 7714 7715 // Check the length modifier is valid with the given conversion specifier. 7716 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7717 S.getLangOpts())) 7718 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7719 diag::warn_format_nonsensical_length); 7720 else if (!FS.hasStandardLengthModifier()) 7721 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7722 else if (!FS.hasStandardLengthConversionCombination()) 7723 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7724 diag::warn_format_non_standard_conversion_spec); 7725 7726 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7727 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7728 7729 // The remaining checks depend on the data arguments. 7730 if (HasVAListArg) 7731 return true; 7732 7733 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7734 return false; 7735 7736 const Expr *Arg = getDataArg(argIndex); 7737 if (!Arg) 7738 return true; 7739 7740 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); 7741 } 7742 7743 static bool requiresParensToAddCast(const Expr *E) { 7744 // FIXME: We should have a general way to reason about operator 7745 // precedence and whether parens are actually needed here. 7746 // Take care of a few common cases where they aren't. 7747 const Expr *Inside = E->IgnoreImpCasts(); 7748 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) 7749 Inside = POE->getSyntacticForm()->IgnoreImpCasts(); 7750 7751 switch (Inside->getStmtClass()) { 7752 case Stmt::ArraySubscriptExprClass: 7753 case Stmt::CallExprClass: 7754 case Stmt::CharacterLiteralClass: 7755 case Stmt::CXXBoolLiteralExprClass: 7756 case Stmt::DeclRefExprClass: 7757 case Stmt::FloatingLiteralClass: 7758 case Stmt::IntegerLiteralClass: 7759 case Stmt::MemberExprClass: 7760 case Stmt::ObjCArrayLiteralClass: 7761 case Stmt::ObjCBoolLiteralExprClass: 7762 case Stmt::ObjCBoxedExprClass: 7763 case Stmt::ObjCDictionaryLiteralClass: 7764 case Stmt::ObjCEncodeExprClass: 7765 case Stmt::ObjCIvarRefExprClass: 7766 case Stmt::ObjCMessageExprClass: 7767 case Stmt::ObjCPropertyRefExprClass: 7768 case Stmt::ObjCStringLiteralClass: 7769 case Stmt::ObjCSubscriptRefExprClass: 7770 case Stmt::ParenExprClass: 7771 case Stmt::StringLiteralClass: 7772 case Stmt::UnaryOperatorClass: 7773 return false; 7774 default: 7775 return true; 7776 } 7777 } 7778 7779 static std::pair<QualType, StringRef> 7780 shouldNotPrintDirectly(const ASTContext &Context, 7781 QualType IntendedTy, 7782 const Expr *E) { 7783 // Use a 'while' to peel off layers of typedefs. 7784 QualType TyTy = IntendedTy; 7785 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { 7786 StringRef Name = UserTy->getDecl()->getName(); 7787 QualType CastTy = llvm::StringSwitch<QualType>(Name) 7788 .Case("CFIndex", Context.getNSIntegerType()) 7789 .Case("NSInteger", Context.getNSIntegerType()) 7790 .Case("NSUInteger", Context.getNSUIntegerType()) 7791 .Case("SInt32", Context.IntTy) 7792 .Case("UInt32", Context.UnsignedIntTy) 7793 .Default(QualType()); 7794 7795 if (!CastTy.isNull()) 7796 return std::make_pair(CastTy, Name); 7797 7798 TyTy = UserTy->desugar(); 7799 } 7800 7801 // Strip parens if necessary. 7802 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 7803 return shouldNotPrintDirectly(Context, 7804 PE->getSubExpr()->getType(), 7805 PE->getSubExpr()); 7806 7807 // If this is a conditional expression, then its result type is constructed 7808 // via usual arithmetic conversions and thus there might be no necessary 7809 // typedef sugar there. Recurse to operands to check for NSInteger & 7810 // Co. usage condition. 7811 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 7812 QualType TrueTy, FalseTy; 7813 StringRef TrueName, FalseName; 7814 7815 std::tie(TrueTy, TrueName) = 7816 shouldNotPrintDirectly(Context, 7817 CO->getTrueExpr()->getType(), 7818 CO->getTrueExpr()); 7819 std::tie(FalseTy, FalseName) = 7820 shouldNotPrintDirectly(Context, 7821 CO->getFalseExpr()->getType(), 7822 CO->getFalseExpr()); 7823 7824 if (TrueTy == FalseTy) 7825 return std::make_pair(TrueTy, TrueName); 7826 else if (TrueTy.isNull()) 7827 return std::make_pair(FalseTy, FalseName); 7828 else if (FalseTy.isNull()) 7829 return std::make_pair(TrueTy, TrueName); 7830 } 7831 7832 return std::make_pair(QualType(), StringRef()); 7833 } 7834 7835 /// Return true if \p ICE is an implicit argument promotion of an arithmetic 7836 /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked 7837 /// type do not count. 7838 static bool 7839 isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) { 7840 QualType From = ICE->getSubExpr()->getType(); 7841 QualType To = ICE->getType(); 7842 // It's an integer promotion if the destination type is the promoted 7843 // source type. 7844 if (ICE->getCastKind() == CK_IntegralCast && 7845 From->isPromotableIntegerType() && 7846 S.Context.getPromotedIntegerType(From) == To) 7847 return true; 7848 // Look through vector types, since we do default argument promotion for 7849 // those in OpenCL. 7850 if (const auto *VecTy = From->getAs<ExtVectorType>()) 7851 From = VecTy->getElementType(); 7852 if (const auto *VecTy = To->getAs<ExtVectorType>()) 7853 To = VecTy->getElementType(); 7854 // It's a floating promotion if the source type is a lower rank. 7855 return ICE->getCastKind() == CK_FloatingCast && 7856 S.Context.getFloatingTypeOrder(From, To) < 0; 7857 } 7858 7859 bool 7860 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 7861 const char *StartSpecifier, 7862 unsigned SpecifierLen, 7863 const Expr *E) { 7864 using namespace analyze_format_string; 7865 using namespace analyze_printf; 7866 7867 // Now type check the data expression that matches the 7868 // format specifier. 7869 const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext()); 7870 if (!AT.isValid()) 7871 return true; 7872 7873 QualType ExprTy = E->getType(); 7874 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { 7875 ExprTy = TET->getUnderlyingExpr()->getType(); 7876 } 7877 7878 const analyze_printf::ArgType::MatchKind Match = 7879 AT.matchesType(S.Context, ExprTy); 7880 bool Pedantic = Match == analyze_printf::ArgType::NoMatchPedantic; 7881 if (Match == analyze_printf::ArgType::Match) 7882 return true; 7883 7884 // Look through argument promotions for our error message's reported type. 7885 // This includes the integral and floating promotions, but excludes array 7886 // and function pointer decay (seeing that an argument intended to be a 7887 // string has type 'char [6]' is probably more confusing than 'char *') and 7888 // certain bitfield promotions (bitfields can be 'demoted' to a lesser type). 7889 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 7890 if (isArithmeticArgumentPromotion(S, ICE)) { 7891 E = ICE->getSubExpr(); 7892 ExprTy = E->getType(); 7893 7894 // Check if we didn't match because of an implicit cast from a 'char' 7895 // or 'short' to an 'int'. This is done because printf is a varargs 7896 // function. 7897 if (ICE->getType() == S.Context.IntTy || 7898 ICE->getType() == S.Context.UnsignedIntTy) { 7899 // All further checking is done on the subexpression. 7900 if (AT.matchesType(S.Context, ExprTy)) 7901 return true; 7902 } 7903 } 7904 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { 7905 // Special case for 'a', which has type 'int' in C. 7906 // Note, however, that we do /not/ want to treat multibyte constants like 7907 // 'MooV' as characters! This form is deprecated but still exists. 7908 if (ExprTy == S.Context.IntTy) 7909 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) 7910 ExprTy = S.Context.CharTy; 7911 } 7912 7913 // Look through enums to their underlying type. 7914 bool IsEnum = false; 7915 if (auto EnumTy = ExprTy->getAs<EnumType>()) { 7916 ExprTy = EnumTy->getDecl()->getIntegerType(); 7917 IsEnum = true; 7918 } 7919 7920 // %C in an Objective-C context prints a unichar, not a wchar_t. 7921 // If the argument is an integer of some kind, believe the %C and suggest 7922 // a cast instead of changing the conversion specifier. 7923 QualType IntendedTy = ExprTy; 7924 if (isObjCContext() && 7925 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { 7926 if (ExprTy->isIntegralOrUnscopedEnumerationType() && 7927 !ExprTy->isCharType()) { 7928 // 'unichar' is defined as a typedef of unsigned short, but we should 7929 // prefer using the typedef if it is visible. 7930 IntendedTy = S.Context.UnsignedShortTy; 7931 7932 // While we are here, check if the value is an IntegerLiteral that happens 7933 // to be within the valid range. 7934 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { 7935 const llvm::APInt &V = IL->getValue(); 7936 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) 7937 return true; 7938 } 7939 7940 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(), 7941 Sema::LookupOrdinaryName); 7942 if (S.LookupName(Result, S.getCurScope())) { 7943 NamedDecl *ND = Result.getFoundDecl(); 7944 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) 7945 if (TD->getUnderlyingType() == IntendedTy) 7946 IntendedTy = S.Context.getTypedefType(TD); 7947 } 7948 } 7949 } 7950 7951 // Special-case some of Darwin's platform-independence types by suggesting 7952 // casts to primitive types that are known to be large enough. 7953 bool ShouldNotPrintDirectly = false; StringRef CastTyName; 7954 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { 7955 QualType CastTy; 7956 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); 7957 if (!CastTy.isNull()) { 7958 // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int 7959 // (long in ASTContext). Only complain to pedants. 7960 if ((CastTyName == "NSInteger" || CastTyName == "NSUInteger") && 7961 (AT.isSizeT() || AT.isPtrdiffT()) && 7962 AT.matchesType(S.Context, CastTy)) 7963 Pedantic = true; 7964 IntendedTy = CastTy; 7965 ShouldNotPrintDirectly = true; 7966 } 7967 } 7968 7969 // We may be able to offer a FixItHint if it is a supported type. 7970 PrintfSpecifier fixedFS = FS; 7971 bool Success = 7972 fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext()); 7973 7974 if (Success) { 7975 // Get the fix string from the fixed format specifier 7976 SmallString<16> buf; 7977 llvm::raw_svector_ostream os(buf); 7978 fixedFS.toString(os); 7979 7980 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); 7981 7982 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) { 7983 unsigned Diag = 7984 Pedantic 7985 ? diag::warn_format_conversion_argument_type_mismatch_pedantic 7986 : diag::warn_format_conversion_argument_type_mismatch; 7987 // In this case, the specifier is wrong and should be changed to match 7988 // the argument. 7989 EmitFormatDiagnostic(S.PDiag(Diag) 7990 << AT.getRepresentativeTypeName(S.Context) 7991 << IntendedTy << IsEnum << E->getSourceRange(), 7992 E->getBeginLoc(), 7993 /*IsStringLocation*/ false, SpecRange, 7994 FixItHint::CreateReplacement(SpecRange, os.str())); 7995 } else { 7996 // The canonical type for formatting this value is different from the 7997 // actual type of the expression. (This occurs, for example, with Darwin's 7998 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but 7999 // should be printed as 'long' for 64-bit compatibility.) 8000 // Rather than emitting a normal format/argument mismatch, we want to 8001 // add a cast to the recommended type (and correct the format string 8002 // if necessary). 8003 SmallString<16> CastBuf; 8004 llvm::raw_svector_ostream CastFix(CastBuf); 8005 CastFix << "("; 8006 IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); 8007 CastFix << ")"; 8008 8009 SmallVector<FixItHint,4> Hints; 8010 if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly) 8011 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); 8012 8013 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { 8014 // If there's already a cast present, just replace it. 8015 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); 8016 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); 8017 8018 } else if (!requiresParensToAddCast(E)) { 8019 // If the expression has high enough precedence, 8020 // just write the C-style cast. 8021 Hints.push_back( 8022 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 8023 } else { 8024 // Otherwise, add parens around the expression as well as the cast. 8025 CastFix << "("; 8026 Hints.push_back( 8027 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 8028 8029 SourceLocation After = S.getLocForEndOfToken(E->getEndLoc()); 8030 Hints.push_back(FixItHint::CreateInsertion(After, ")")); 8031 } 8032 8033 if (ShouldNotPrintDirectly) { 8034 // The expression has a type that should not be printed directly. 8035 // We extract the name from the typedef because we don't want to show 8036 // the underlying type in the diagnostic. 8037 StringRef Name; 8038 if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy)) 8039 Name = TypedefTy->getDecl()->getName(); 8040 else 8041 Name = CastTyName; 8042 unsigned Diag = Pedantic 8043 ? diag::warn_format_argument_needs_cast_pedantic 8044 : diag::warn_format_argument_needs_cast; 8045 EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum 8046 << E->getSourceRange(), 8047 E->getBeginLoc(), /*IsStringLocation=*/false, 8048 SpecRange, Hints); 8049 } else { 8050 // In this case, the expression could be printed using a different 8051 // specifier, but we've decided that the specifier is probably correct 8052 // and we should cast instead. Just use the normal warning message. 8053 EmitFormatDiagnostic( 8054 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 8055 << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum 8056 << E->getSourceRange(), 8057 E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints); 8058 } 8059 } 8060 } else { 8061 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, 8062 SpecifierLen); 8063 // Since the warning for passing non-POD types to variadic functions 8064 // was deferred until now, we emit a warning for non-POD 8065 // arguments here. 8066 switch (S.isValidVarArgType(ExprTy)) { 8067 case Sema::VAK_Valid: 8068 case Sema::VAK_ValidInCXX11: { 8069 unsigned Diag = 8070 Pedantic 8071 ? diag::warn_format_conversion_argument_type_mismatch_pedantic 8072 : diag::warn_format_conversion_argument_type_mismatch; 8073 8074 EmitFormatDiagnostic( 8075 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 8076 << IsEnum << CSR << E->getSourceRange(), 8077 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 8078 break; 8079 } 8080 case Sema::VAK_Undefined: 8081 case Sema::VAK_MSVCUndefined: 8082 EmitFormatDiagnostic(S.PDiag(diag::warn_non_pod_vararg_with_format_string) 8083 << S.getLangOpts().CPlusPlus11 << ExprTy 8084 << CallType 8085 << AT.getRepresentativeTypeName(S.Context) << CSR 8086 << E->getSourceRange(), 8087 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 8088 checkForCStrMembers(AT, E); 8089 break; 8090 8091 case Sema::VAK_Invalid: 8092 if (ExprTy->isObjCObjectType()) 8093 EmitFormatDiagnostic( 8094 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) 8095 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 8096 << AT.getRepresentativeTypeName(S.Context) << CSR 8097 << E->getSourceRange(), 8098 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 8099 else 8100 // FIXME: If this is an initializer list, suggest removing the braces 8101 // or inserting a cast to the target type. 8102 S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format) 8103 << isa<InitListExpr>(E) << ExprTy << CallType 8104 << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange(); 8105 break; 8106 } 8107 8108 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && 8109 "format string specifier index out of range"); 8110 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; 8111 } 8112 8113 return true; 8114 } 8115 8116 //===--- CHECK: Scanf format string checking ------------------------------===// 8117 8118 namespace { 8119 8120 class CheckScanfHandler : public CheckFormatHandler { 8121 public: 8122 CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr, 8123 const Expr *origFormatExpr, Sema::FormatStringType type, 8124 unsigned firstDataArg, unsigned numDataArgs, 8125 const char *beg, bool hasVAListArg, 8126 ArrayRef<const Expr *> Args, unsigned formatIdx, 8127 bool inFunctionCall, Sema::VariadicCallType CallType, 8128 llvm::SmallBitVector &CheckedVarArgs, 8129 UncoveredArgHandler &UncoveredArg) 8130 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 8131 numDataArgs, beg, hasVAListArg, Args, formatIdx, 8132 inFunctionCall, CallType, CheckedVarArgs, 8133 UncoveredArg) {} 8134 8135 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 8136 const char *startSpecifier, 8137 unsigned specifierLen) override; 8138 8139 bool HandleInvalidScanfConversionSpecifier( 8140 const analyze_scanf::ScanfSpecifier &FS, 8141 const char *startSpecifier, 8142 unsigned specifierLen) override; 8143 8144 void HandleIncompleteScanList(const char *start, const char *end) override; 8145 }; 8146 8147 } // namespace 8148 8149 void CheckScanfHandler::HandleIncompleteScanList(const char *start, 8150 const char *end) { 8151 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), 8152 getLocationOfByte(end), /*IsStringLocation*/true, 8153 getSpecifierRange(start, end - start)); 8154 } 8155 8156 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( 8157 const analyze_scanf::ScanfSpecifier &FS, 8158 const char *startSpecifier, 8159 unsigned specifierLen) { 8160 const analyze_scanf::ScanfConversionSpecifier &CS = 8161 FS.getConversionSpecifier(); 8162 8163 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 8164 getLocationOfByte(CS.getStart()), 8165 startSpecifier, specifierLen, 8166 CS.getStart(), CS.getLength()); 8167 } 8168 8169 bool CheckScanfHandler::HandleScanfSpecifier( 8170 const analyze_scanf::ScanfSpecifier &FS, 8171 const char *startSpecifier, 8172 unsigned specifierLen) { 8173 using namespace analyze_scanf; 8174 using namespace analyze_format_string; 8175 8176 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); 8177 8178 // Handle case where '%' and '*' don't consume an argument. These shouldn't 8179 // be used to decide if we are using positional arguments consistently. 8180 if (FS.consumesDataArgument()) { 8181 if (atFirstArg) { 8182 atFirstArg = false; 8183 usesPositionalArgs = FS.usesPositionalArg(); 8184 } 8185 else if (usesPositionalArgs != FS.usesPositionalArg()) { 8186 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 8187 startSpecifier, specifierLen); 8188 return false; 8189 } 8190 } 8191 8192 // Check if the field with is non-zero. 8193 const OptionalAmount &Amt = FS.getFieldWidth(); 8194 if (Amt.getHowSpecified() == OptionalAmount::Constant) { 8195 if (Amt.getConstantAmount() == 0) { 8196 const CharSourceRange &R = getSpecifierRange(Amt.getStart(), 8197 Amt.getConstantLength()); 8198 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), 8199 getLocationOfByte(Amt.getStart()), 8200 /*IsStringLocation*/true, R, 8201 FixItHint::CreateRemoval(R)); 8202 } 8203 } 8204 8205 if (!FS.consumesDataArgument()) { 8206 // FIXME: Technically specifying a precision or field width here 8207 // makes no sense. Worth issuing a warning at some point. 8208 return true; 8209 } 8210 8211 // Consume the argument. 8212 unsigned argIndex = FS.getArgIndex(); 8213 if (argIndex < NumDataArgs) { 8214 // The check to see if the argIndex is valid will come later. 8215 // We set the bit here because we may exit early from this 8216 // function if we encounter some other error. 8217 CoveredArgs.set(argIndex); 8218 } 8219 8220 // Check the length modifier is valid with the given conversion specifier. 8221 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 8222 S.getLangOpts())) 8223 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 8224 diag::warn_format_nonsensical_length); 8225 else if (!FS.hasStandardLengthModifier()) 8226 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 8227 else if (!FS.hasStandardLengthConversionCombination()) 8228 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 8229 diag::warn_format_non_standard_conversion_spec); 8230 8231 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 8232 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 8233 8234 // The remaining checks depend on the data arguments. 8235 if (HasVAListArg) 8236 return true; 8237 8238 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 8239 return false; 8240 8241 // Check that the argument type matches the format specifier. 8242 const Expr *Ex = getDataArg(argIndex); 8243 if (!Ex) 8244 return true; 8245 8246 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); 8247 8248 if (!AT.isValid()) { 8249 return true; 8250 } 8251 8252 analyze_format_string::ArgType::MatchKind Match = 8253 AT.matchesType(S.Context, Ex->getType()); 8254 bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic; 8255 if (Match == analyze_format_string::ArgType::Match) 8256 return true; 8257 8258 ScanfSpecifier fixedFS = FS; 8259 bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), 8260 S.getLangOpts(), S.Context); 8261 8262 unsigned Diag = 8263 Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic 8264 : diag::warn_format_conversion_argument_type_mismatch; 8265 8266 if (Success) { 8267 // Get the fix string from the fixed format specifier. 8268 SmallString<128> buf; 8269 llvm::raw_svector_ostream os(buf); 8270 fixedFS.toString(os); 8271 8272 EmitFormatDiagnostic( 8273 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) 8274 << Ex->getType() << false << Ex->getSourceRange(), 8275 Ex->getBeginLoc(), 8276 /*IsStringLocation*/ false, 8277 getSpecifierRange(startSpecifier, specifierLen), 8278 FixItHint::CreateReplacement( 8279 getSpecifierRange(startSpecifier, specifierLen), os.str())); 8280 } else { 8281 EmitFormatDiagnostic(S.PDiag(Diag) 8282 << AT.getRepresentativeTypeName(S.Context) 8283 << Ex->getType() << false << Ex->getSourceRange(), 8284 Ex->getBeginLoc(), 8285 /*IsStringLocation*/ false, 8286 getSpecifierRange(startSpecifier, specifierLen)); 8287 } 8288 8289 return true; 8290 } 8291 8292 static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr, 8293 const Expr *OrigFormatExpr, 8294 ArrayRef<const Expr *> Args, 8295 bool HasVAListArg, unsigned format_idx, 8296 unsigned firstDataArg, 8297 Sema::FormatStringType Type, 8298 bool inFunctionCall, 8299 Sema::VariadicCallType CallType, 8300 llvm::SmallBitVector &CheckedVarArgs, 8301 UncoveredArgHandler &UncoveredArg) { 8302 // CHECK: is the format string a wide literal? 8303 if (!FExpr->isAscii() && !FExpr->isUTF8()) { 8304 CheckFormatHandler::EmitFormatDiagnostic( 8305 S, inFunctionCall, Args[format_idx], 8306 S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(), 8307 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 8308 return; 8309 } 8310 8311 // Str - The format string. NOTE: this is NOT null-terminated! 8312 StringRef StrRef = FExpr->getString(); 8313 const char *Str = StrRef.data(); 8314 // Account for cases where the string literal is truncated in a declaration. 8315 const ConstantArrayType *T = 8316 S.Context.getAsConstantArrayType(FExpr->getType()); 8317 assert(T && "String literal not of constant array type!"); 8318 size_t TypeSize = T->getSize().getZExtValue(); 8319 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 8320 const unsigned numDataArgs = Args.size() - firstDataArg; 8321 8322 // Emit a warning if the string literal is truncated and does not contain an 8323 // embedded null character. 8324 if (TypeSize <= StrRef.size() && 8325 StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) { 8326 CheckFormatHandler::EmitFormatDiagnostic( 8327 S, inFunctionCall, Args[format_idx], 8328 S.PDiag(diag::warn_printf_format_string_not_null_terminated), 8329 FExpr->getBeginLoc(), 8330 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); 8331 return; 8332 } 8333 8334 // CHECK: empty format string? 8335 if (StrLen == 0 && numDataArgs > 0) { 8336 CheckFormatHandler::EmitFormatDiagnostic( 8337 S, inFunctionCall, Args[format_idx], 8338 S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(), 8339 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 8340 return; 8341 } 8342 8343 if (Type == Sema::FST_Printf || Type == Sema::FST_NSString || 8344 Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog || 8345 Type == Sema::FST_OSTrace) { 8346 CheckPrintfHandler H( 8347 S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs, 8348 (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, 8349 HasVAListArg, Args, format_idx, inFunctionCall, CallType, 8350 CheckedVarArgs, UncoveredArg); 8351 8352 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, 8353 S.getLangOpts(), 8354 S.Context.getTargetInfo(), 8355 Type == Sema::FST_FreeBSDKPrintf)) 8356 H.DoneProcessing(); 8357 } else if (Type == Sema::FST_Scanf) { 8358 CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg, 8359 numDataArgs, Str, HasVAListArg, Args, format_idx, 8360 inFunctionCall, CallType, CheckedVarArgs, UncoveredArg); 8361 8362 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, 8363 S.getLangOpts(), 8364 S.Context.getTargetInfo())) 8365 H.DoneProcessing(); 8366 } // TODO: handle other formats 8367 } 8368 8369 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { 8370 // Str - The format string. NOTE: this is NOT null-terminated! 8371 StringRef StrRef = FExpr->getString(); 8372 const char *Str = StrRef.data(); 8373 // Account for cases where the string literal is truncated in a declaration. 8374 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); 8375 assert(T && "String literal not of constant array type!"); 8376 size_t TypeSize = T->getSize().getZExtValue(); 8377 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 8378 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, 8379 getLangOpts(), 8380 Context.getTargetInfo()); 8381 } 8382 8383 //===--- CHECK: Warn on use of wrong absolute value function. -------------===// 8384 8385 // Returns the related absolute value function that is larger, of 0 if one 8386 // does not exist. 8387 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { 8388 switch (AbsFunction) { 8389 default: 8390 return 0; 8391 8392 case Builtin::BI__builtin_abs: 8393 return Builtin::BI__builtin_labs; 8394 case Builtin::BI__builtin_labs: 8395 return Builtin::BI__builtin_llabs; 8396 case Builtin::BI__builtin_llabs: 8397 return 0; 8398 8399 case Builtin::BI__builtin_fabsf: 8400 return Builtin::BI__builtin_fabs; 8401 case Builtin::BI__builtin_fabs: 8402 return Builtin::BI__builtin_fabsl; 8403 case Builtin::BI__builtin_fabsl: 8404 return 0; 8405 8406 case Builtin::BI__builtin_cabsf: 8407 return Builtin::BI__builtin_cabs; 8408 case Builtin::BI__builtin_cabs: 8409 return Builtin::BI__builtin_cabsl; 8410 case Builtin::BI__builtin_cabsl: 8411 return 0; 8412 8413 case Builtin::BIabs: 8414 return Builtin::BIlabs; 8415 case Builtin::BIlabs: 8416 return Builtin::BIllabs; 8417 case Builtin::BIllabs: 8418 return 0; 8419 8420 case Builtin::BIfabsf: 8421 return Builtin::BIfabs; 8422 case Builtin::BIfabs: 8423 return Builtin::BIfabsl; 8424 case Builtin::BIfabsl: 8425 return 0; 8426 8427 case Builtin::BIcabsf: 8428 return Builtin::BIcabs; 8429 case Builtin::BIcabs: 8430 return Builtin::BIcabsl; 8431 case Builtin::BIcabsl: 8432 return 0; 8433 } 8434 } 8435 8436 // Returns the argument type of the absolute value function. 8437 static QualType getAbsoluteValueArgumentType(ASTContext &Context, 8438 unsigned AbsType) { 8439 if (AbsType == 0) 8440 return QualType(); 8441 8442 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; 8443 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); 8444 if (Error != ASTContext::GE_None) 8445 return QualType(); 8446 8447 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); 8448 if (!FT) 8449 return QualType(); 8450 8451 if (FT->getNumParams() != 1) 8452 return QualType(); 8453 8454 return FT->getParamType(0); 8455 } 8456 8457 // Returns the best absolute value function, or zero, based on type and 8458 // current absolute value function. 8459 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, 8460 unsigned AbsFunctionKind) { 8461 unsigned BestKind = 0; 8462 uint64_t ArgSize = Context.getTypeSize(ArgType); 8463 for (unsigned Kind = AbsFunctionKind; Kind != 0; 8464 Kind = getLargerAbsoluteValueFunction(Kind)) { 8465 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); 8466 if (Context.getTypeSize(ParamType) >= ArgSize) { 8467 if (BestKind == 0) 8468 BestKind = Kind; 8469 else if (Context.hasSameType(ParamType, ArgType)) { 8470 BestKind = Kind; 8471 break; 8472 } 8473 } 8474 } 8475 return BestKind; 8476 } 8477 8478 enum AbsoluteValueKind { 8479 AVK_Integer, 8480 AVK_Floating, 8481 AVK_Complex 8482 }; 8483 8484 static AbsoluteValueKind getAbsoluteValueKind(QualType T) { 8485 if (T->isIntegralOrEnumerationType()) 8486 return AVK_Integer; 8487 if (T->isRealFloatingType()) 8488 return AVK_Floating; 8489 if (T->isAnyComplexType()) 8490 return AVK_Complex; 8491 8492 llvm_unreachable("Type not integer, floating, or complex"); 8493 } 8494 8495 // Changes the absolute value function to a different type. Preserves whether 8496 // the function is a builtin. 8497 static unsigned changeAbsFunction(unsigned AbsKind, 8498 AbsoluteValueKind ValueKind) { 8499 switch (ValueKind) { 8500 case AVK_Integer: 8501 switch (AbsKind) { 8502 default: 8503 return 0; 8504 case Builtin::BI__builtin_fabsf: 8505 case Builtin::BI__builtin_fabs: 8506 case Builtin::BI__builtin_fabsl: 8507 case Builtin::BI__builtin_cabsf: 8508 case Builtin::BI__builtin_cabs: 8509 case Builtin::BI__builtin_cabsl: 8510 return Builtin::BI__builtin_abs; 8511 case Builtin::BIfabsf: 8512 case Builtin::BIfabs: 8513 case Builtin::BIfabsl: 8514 case Builtin::BIcabsf: 8515 case Builtin::BIcabs: 8516 case Builtin::BIcabsl: 8517 return Builtin::BIabs; 8518 } 8519 case AVK_Floating: 8520 switch (AbsKind) { 8521 default: 8522 return 0; 8523 case Builtin::BI__builtin_abs: 8524 case Builtin::BI__builtin_labs: 8525 case Builtin::BI__builtin_llabs: 8526 case Builtin::BI__builtin_cabsf: 8527 case Builtin::BI__builtin_cabs: 8528 case Builtin::BI__builtin_cabsl: 8529 return Builtin::BI__builtin_fabsf; 8530 case Builtin::BIabs: 8531 case Builtin::BIlabs: 8532 case Builtin::BIllabs: 8533 case Builtin::BIcabsf: 8534 case Builtin::BIcabs: 8535 case Builtin::BIcabsl: 8536 return Builtin::BIfabsf; 8537 } 8538 case AVK_Complex: 8539 switch (AbsKind) { 8540 default: 8541 return 0; 8542 case Builtin::BI__builtin_abs: 8543 case Builtin::BI__builtin_labs: 8544 case Builtin::BI__builtin_llabs: 8545 case Builtin::BI__builtin_fabsf: 8546 case Builtin::BI__builtin_fabs: 8547 case Builtin::BI__builtin_fabsl: 8548 return Builtin::BI__builtin_cabsf; 8549 case Builtin::BIabs: 8550 case Builtin::BIlabs: 8551 case Builtin::BIllabs: 8552 case Builtin::BIfabsf: 8553 case Builtin::BIfabs: 8554 case Builtin::BIfabsl: 8555 return Builtin::BIcabsf; 8556 } 8557 } 8558 llvm_unreachable("Unable to convert function"); 8559 } 8560 8561 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { 8562 const IdentifierInfo *FnInfo = FDecl->getIdentifier(); 8563 if (!FnInfo) 8564 return 0; 8565 8566 switch (FDecl->getBuiltinID()) { 8567 default: 8568 return 0; 8569 case Builtin::BI__builtin_abs: 8570 case Builtin::BI__builtin_fabs: 8571 case Builtin::BI__builtin_fabsf: 8572 case Builtin::BI__builtin_fabsl: 8573 case Builtin::BI__builtin_labs: 8574 case Builtin::BI__builtin_llabs: 8575 case Builtin::BI__builtin_cabs: 8576 case Builtin::BI__builtin_cabsf: 8577 case Builtin::BI__builtin_cabsl: 8578 case Builtin::BIabs: 8579 case Builtin::BIlabs: 8580 case Builtin::BIllabs: 8581 case Builtin::BIfabs: 8582 case Builtin::BIfabsf: 8583 case Builtin::BIfabsl: 8584 case Builtin::BIcabs: 8585 case Builtin::BIcabsf: 8586 case Builtin::BIcabsl: 8587 return FDecl->getBuiltinID(); 8588 } 8589 llvm_unreachable("Unknown Builtin type"); 8590 } 8591 8592 // If the replacement is valid, emit a note with replacement function. 8593 // Additionally, suggest including the proper header if not already included. 8594 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, 8595 unsigned AbsKind, QualType ArgType) { 8596 bool EmitHeaderHint = true; 8597 const char *HeaderName = nullptr; 8598 const char *FunctionName = nullptr; 8599 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { 8600 FunctionName = "std::abs"; 8601 if (ArgType->isIntegralOrEnumerationType()) { 8602 HeaderName = "cstdlib"; 8603 } else if (ArgType->isRealFloatingType()) { 8604 HeaderName = "cmath"; 8605 } else { 8606 llvm_unreachable("Invalid Type"); 8607 } 8608 8609 // Lookup all std::abs 8610 if (NamespaceDecl *Std = S.getStdNamespace()) { 8611 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); 8612 R.suppressDiagnostics(); 8613 S.LookupQualifiedName(R, Std); 8614 8615 for (const auto *I : R) { 8616 const FunctionDecl *FDecl = nullptr; 8617 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { 8618 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); 8619 } else { 8620 FDecl = dyn_cast<FunctionDecl>(I); 8621 } 8622 if (!FDecl) 8623 continue; 8624 8625 // Found std::abs(), check that they are the right ones. 8626 if (FDecl->getNumParams() != 1) 8627 continue; 8628 8629 // Check that the parameter type can handle the argument. 8630 QualType ParamType = FDecl->getParamDecl(0)->getType(); 8631 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && 8632 S.Context.getTypeSize(ArgType) <= 8633 S.Context.getTypeSize(ParamType)) { 8634 // Found a function, don't need the header hint. 8635 EmitHeaderHint = false; 8636 break; 8637 } 8638 } 8639 } 8640 } else { 8641 FunctionName = S.Context.BuiltinInfo.getName(AbsKind); 8642 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); 8643 8644 if (HeaderName) { 8645 DeclarationName DN(&S.Context.Idents.get(FunctionName)); 8646 LookupResult R(S, DN, Loc, Sema::LookupAnyName); 8647 R.suppressDiagnostics(); 8648 S.LookupName(R, S.getCurScope()); 8649 8650 if (R.isSingleResult()) { 8651 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); 8652 if (FD && FD->getBuiltinID() == AbsKind) { 8653 EmitHeaderHint = false; 8654 } else { 8655 return; 8656 } 8657 } else if (!R.empty()) { 8658 return; 8659 } 8660 } 8661 } 8662 8663 S.Diag(Loc, diag::note_replace_abs_function) 8664 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); 8665 8666 if (!HeaderName) 8667 return; 8668 8669 if (!EmitHeaderHint) 8670 return; 8671 8672 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName 8673 << FunctionName; 8674 } 8675 8676 template <std::size_t StrLen> 8677 static bool IsStdFunction(const FunctionDecl *FDecl, 8678 const char (&Str)[StrLen]) { 8679 if (!FDecl) 8680 return false; 8681 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str)) 8682 return false; 8683 if (!FDecl->isInStdNamespace()) 8684 return false; 8685 8686 return true; 8687 } 8688 8689 // Warn when using the wrong abs() function. 8690 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, 8691 const FunctionDecl *FDecl) { 8692 if (Call->getNumArgs() != 1) 8693 return; 8694 8695 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); 8696 bool IsStdAbs = IsStdFunction(FDecl, "abs"); 8697 if (AbsKind == 0 && !IsStdAbs) 8698 return; 8699 8700 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8701 QualType ParamType = Call->getArg(0)->getType(); 8702 8703 // Unsigned types cannot be negative. Suggest removing the absolute value 8704 // function call. 8705 if (ArgType->isUnsignedIntegerType()) { 8706 const char *FunctionName = 8707 IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind); 8708 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; 8709 Diag(Call->getExprLoc(), diag::note_remove_abs) 8710 << FunctionName 8711 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); 8712 return; 8713 } 8714 8715 // Taking the absolute value of a pointer is very suspicious, they probably 8716 // wanted to index into an array, dereference a pointer, call a function, etc. 8717 if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) { 8718 unsigned DiagType = 0; 8719 if (ArgType->isFunctionType()) 8720 DiagType = 1; 8721 else if (ArgType->isArrayType()) 8722 DiagType = 2; 8723 8724 Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType; 8725 return; 8726 } 8727 8728 // std::abs has overloads which prevent most of the absolute value problems 8729 // from occurring. 8730 if (IsStdAbs) 8731 return; 8732 8733 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); 8734 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); 8735 8736 // The argument and parameter are the same kind. Check if they are the right 8737 // size. 8738 if (ArgValueKind == ParamValueKind) { 8739 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) 8740 return; 8741 8742 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); 8743 Diag(Call->getExprLoc(), diag::warn_abs_too_small) 8744 << FDecl << ArgType << ParamType; 8745 8746 if (NewAbsKind == 0) 8747 return; 8748 8749 emitReplacement(*this, Call->getExprLoc(), 8750 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8751 return; 8752 } 8753 8754 // ArgValueKind != ParamValueKind 8755 // The wrong type of absolute value function was used. Attempt to find the 8756 // proper one. 8757 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); 8758 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); 8759 if (NewAbsKind == 0) 8760 return; 8761 8762 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) 8763 << FDecl << ParamValueKind << ArgValueKind; 8764 8765 emitReplacement(*this, Call->getExprLoc(), 8766 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8767 } 8768 8769 //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===// 8770 void Sema::CheckMaxUnsignedZero(const CallExpr *Call, 8771 const FunctionDecl *FDecl) { 8772 if (!Call || !FDecl) return; 8773 8774 // Ignore template specializations and macros. 8775 if (inTemplateInstantiation()) return; 8776 if (Call->getExprLoc().isMacroID()) return; 8777 8778 // Only care about the one template argument, two function parameter std::max 8779 if (Call->getNumArgs() != 2) return; 8780 if (!IsStdFunction(FDecl, "max")) return; 8781 const auto * ArgList = FDecl->getTemplateSpecializationArgs(); 8782 if (!ArgList) return; 8783 if (ArgList->size() != 1) return; 8784 8785 // Check that template type argument is unsigned integer. 8786 const auto& TA = ArgList->get(0); 8787 if (TA.getKind() != TemplateArgument::Type) return; 8788 QualType ArgType = TA.getAsType(); 8789 if (!ArgType->isUnsignedIntegerType()) return; 8790 8791 // See if either argument is a literal zero. 8792 auto IsLiteralZeroArg = [](const Expr* E) -> bool { 8793 const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E); 8794 if (!MTE) return false; 8795 const auto *Num = dyn_cast<IntegerLiteral>(MTE->GetTemporaryExpr()); 8796 if (!Num) return false; 8797 if (Num->getValue() != 0) return false; 8798 return true; 8799 }; 8800 8801 const Expr *FirstArg = Call->getArg(0); 8802 const Expr *SecondArg = Call->getArg(1); 8803 const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg); 8804 const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg); 8805 8806 // Only warn when exactly one argument is zero. 8807 if (IsFirstArgZero == IsSecondArgZero) return; 8808 8809 SourceRange FirstRange = FirstArg->getSourceRange(); 8810 SourceRange SecondRange = SecondArg->getSourceRange(); 8811 8812 SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange; 8813 8814 Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero) 8815 << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange; 8816 8817 // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)". 8818 SourceRange RemovalRange; 8819 if (IsFirstArgZero) { 8820 RemovalRange = SourceRange(FirstRange.getBegin(), 8821 SecondRange.getBegin().getLocWithOffset(-1)); 8822 } else { 8823 RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()), 8824 SecondRange.getEnd()); 8825 } 8826 8827 Diag(Call->getExprLoc(), diag::note_remove_max_call) 8828 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()) 8829 << FixItHint::CreateRemoval(RemovalRange); 8830 } 8831 8832 //===--- CHECK: Standard memory functions ---------------------------------===// 8833 8834 /// Takes the expression passed to the size_t parameter of functions 8835 /// such as memcmp, strncat, etc and warns if it's a comparison. 8836 /// 8837 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. 8838 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, 8839 IdentifierInfo *FnName, 8840 SourceLocation FnLoc, 8841 SourceLocation RParenLoc) { 8842 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); 8843 if (!Size) 8844 return false; 8845 8846 // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||: 8847 if (!Size->isComparisonOp() && !Size->isLogicalOp()) 8848 return false; 8849 8850 SourceRange SizeRange = Size->getSourceRange(); 8851 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) 8852 << SizeRange << FnName; 8853 S.Diag(FnLoc, diag::note_memsize_comparison_paren) 8854 << FnName 8855 << FixItHint::CreateInsertion( 8856 S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")") 8857 << FixItHint::CreateRemoval(RParenLoc); 8858 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) 8859 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") 8860 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), 8861 ")"); 8862 8863 return true; 8864 } 8865 8866 /// Determine whether the given type is or contains a dynamic class type 8867 /// (e.g., whether it has a vtable). 8868 static const CXXRecordDecl *getContainedDynamicClass(QualType T, 8869 bool &IsContained) { 8870 // Look through array types while ignoring qualifiers. 8871 const Type *Ty = T->getBaseElementTypeUnsafe(); 8872 IsContained = false; 8873 8874 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 8875 RD = RD ? RD->getDefinition() : nullptr; 8876 if (!RD || RD->isInvalidDecl()) 8877 return nullptr; 8878 8879 if (RD->isDynamicClass()) 8880 return RD; 8881 8882 // Check all the fields. If any bases were dynamic, the class is dynamic. 8883 // It's impossible for a class to transitively contain itself by value, so 8884 // infinite recursion is impossible. 8885 for (auto *FD : RD->fields()) { 8886 bool SubContained; 8887 if (const CXXRecordDecl *ContainedRD = 8888 getContainedDynamicClass(FD->getType(), SubContained)) { 8889 IsContained = true; 8890 return ContainedRD; 8891 } 8892 } 8893 8894 return nullptr; 8895 } 8896 8897 static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) { 8898 if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 8899 if (Unary->getKind() == UETT_SizeOf) 8900 return Unary; 8901 return nullptr; 8902 } 8903 8904 /// If E is a sizeof expression, returns its argument expression, 8905 /// otherwise returns NULL. 8906 static const Expr *getSizeOfExprArg(const Expr *E) { 8907 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8908 if (!SizeOf->isArgumentType()) 8909 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); 8910 return nullptr; 8911 } 8912 8913 /// If E is a sizeof expression, returns its argument type. 8914 static QualType getSizeOfArgType(const Expr *E) { 8915 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8916 return SizeOf->getTypeOfArgument(); 8917 return QualType(); 8918 } 8919 8920 namespace { 8921 8922 struct SearchNonTrivialToInitializeField 8923 : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> { 8924 using Super = 8925 DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>; 8926 8927 SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {} 8928 8929 void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT, 8930 SourceLocation SL) { 8931 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8932 asDerived().visitArray(PDIK, AT, SL); 8933 return; 8934 } 8935 8936 Super::visitWithKind(PDIK, FT, SL); 8937 } 8938 8939 void visitARCStrong(QualType FT, SourceLocation SL) { 8940 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8941 } 8942 void visitARCWeak(QualType FT, SourceLocation SL) { 8943 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8944 } 8945 void visitStruct(QualType FT, SourceLocation SL) { 8946 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8947 visit(FD->getType(), FD->getLocation()); 8948 } 8949 void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK, 8950 const ArrayType *AT, SourceLocation SL) { 8951 visit(getContext().getBaseElementType(AT), SL); 8952 } 8953 void visitTrivial(QualType FT, SourceLocation SL) {} 8954 8955 static void diag(QualType RT, const Expr *E, Sema &S) { 8956 SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation()); 8957 } 8958 8959 ASTContext &getContext() { return S.getASTContext(); } 8960 8961 const Expr *E; 8962 Sema &S; 8963 }; 8964 8965 struct SearchNonTrivialToCopyField 8966 : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> { 8967 using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>; 8968 8969 SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {} 8970 8971 void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT, 8972 SourceLocation SL) { 8973 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8974 asDerived().visitArray(PCK, AT, SL); 8975 return; 8976 } 8977 8978 Super::visitWithKind(PCK, FT, SL); 8979 } 8980 8981 void visitARCStrong(QualType FT, SourceLocation SL) { 8982 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8983 } 8984 void visitARCWeak(QualType FT, SourceLocation SL) { 8985 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8986 } 8987 void visitStruct(QualType FT, SourceLocation SL) { 8988 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8989 visit(FD->getType(), FD->getLocation()); 8990 } 8991 void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT, 8992 SourceLocation SL) { 8993 visit(getContext().getBaseElementType(AT), SL); 8994 } 8995 void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT, 8996 SourceLocation SL) {} 8997 void visitTrivial(QualType FT, SourceLocation SL) {} 8998 void visitVolatileTrivial(QualType FT, SourceLocation SL) {} 8999 9000 static void diag(QualType RT, const Expr *E, Sema &S) { 9001 SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation()); 9002 } 9003 9004 ASTContext &getContext() { return S.getASTContext(); } 9005 9006 const Expr *E; 9007 Sema &S; 9008 }; 9009 9010 } 9011 9012 /// Detect if \c SizeofExpr is likely to calculate the sizeof an object. 9013 static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) { 9014 SizeofExpr = SizeofExpr->IgnoreParenImpCasts(); 9015 9016 if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) { 9017 if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add) 9018 return false; 9019 9020 return doesExprLikelyComputeSize(BO->getLHS()) || 9021 doesExprLikelyComputeSize(BO->getRHS()); 9022 } 9023 9024 return getAsSizeOfExpr(SizeofExpr) != nullptr; 9025 } 9026 9027 /// Check if the ArgLoc originated from a macro passed to the call at CallLoc. 9028 /// 9029 /// \code 9030 /// #define MACRO 0 9031 /// foo(MACRO); 9032 /// foo(0); 9033 /// \endcode 9034 /// 9035 /// This should return true for the first call to foo, but not for the second 9036 /// (regardless of whether foo is a macro or function). 9037 static bool isArgumentExpandedFromMacro(SourceManager &SM, 9038 SourceLocation CallLoc, 9039 SourceLocation ArgLoc) { 9040 if (!CallLoc.isMacroID()) 9041 return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc); 9042 9043 return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) != 9044 SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc)); 9045 } 9046 9047 /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the 9048 /// last two arguments transposed. 9049 static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) { 9050 if (BId != Builtin::BImemset && BId != Builtin::BIbzero) 9051 return; 9052 9053 const Expr *SizeArg = 9054 Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts(); 9055 9056 auto isLiteralZero = [](const Expr *E) { 9057 return isa<IntegerLiteral>(E) && cast<IntegerLiteral>(E)->getValue() == 0; 9058 }; 9059 9060 // If we're memsetting or bzeroing 0 bytes, then this is likely an error. 9061 SourceLocation CallLoc = Call->getRParenLoc(); 9062 SourceManager &SM = S.getSourceManager(); 9063 if (isLiteralZero(SizeArg) && 9064 !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) { 9065 9066 SourceLocation DiagLoc = SizeArg->getExprLoc(); 9067 9068 // Some platforms #define bzero to __builtin_memset. See if this is the 9069 // case, and if so, emit a better diagnostic. 9070 if (BId == Builtin::BIbzero || 9071 (CallLoc.isMacroID() && Lexer::getImmediateMacroName( 9072 CallLoc, SM, S.getLangOpts()) == "bzero")) { 9073 S.Diag(DiagLoc, diag::warn_suspicious_bzero_size); 9074 S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence); 9075 } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) { 9076 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0; 9077 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0; 9078 } 9079 return; 9080 } 9081 9082 // If the second argument to a memset is a sizeof expression and the third 9083 // isn't, this is also likely an error. This should catch 9084 // 'memset(buf, sizeof(buf), 0xff)'. 9085 if (BId == Builtin::BImemset && 9086 doesExprLikelyComputeSize(Call->getArg(1)) && 9087 !doesExprLikelyComputeSize(Call->getArg(2))) { 9088 SourceLocation DiagLoc = Call->getArg(1)->getExprLoc(); 9089 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1; 9090 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1; 9091 return; 9092 } 9093 } 9094 9095 /// Check for dangerous or invalid arguments to memset(). 9096 /// 9097 /// This issues warnings on known problematic, dangerous or unspecified 9098 /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' 9099 /// function calls. 9100 /// 9101 /// \param Call The call expression to diagnose. 9102 void Sema::CheckMemaccessArguments(const CallExpr *Call, 9103 unsigned BId, 9104 IdentifierInfo *FnName) { 9105 assert(BId != 0); 9106 9107 // It is possible to have a non-standard definition of memset. Validate 9108 // we have enough arguments, and if not, abort further checking. 9109 unsigned ExpectedNumArgs = 9110 (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3); 9111 if (Call->getNumArgs() < ExpectedNumArgs) 9112 return; 9113 9114 unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero || 9115 BId == Builtin::BIstrndup ? 1 : 2); 9116 unsigned LenArg = 9117 (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2); 9118 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); 9119 9120 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, 9121 Call->getBeginLoc(), Call->getRParenLoc())) 9122 return; 9123 9124 // Catch cases like 'memset(buf, sizeof(buf), 0)'. 9125 CheckMemaccessSize(*this, BId, Call); 9126 9127 // We have special checking when the length is a sizeof expression. 9128 QualType SizeOfArgTy = getSizeOfArgType(LenExpr); 9129 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); 9130 llvm::FoldingSetNodeID SizeOfArgID; 9131 9132 // Although widely used, 'bzero' is not a standard function. Be more strict 9133 // with the argument types before allowing diagnostics and only allow the 9134 // form bzero(ptr, sizeof(...)). 9135 QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 9136 if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>()) 9137 return; 9138 9139 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { 9140 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); 9141 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); 9142 9143 QualType DestTy = Dest->getType(); 9144 QualType PointeeTy; 9145 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { 9146 PointeeTy = DestPtrTy->getPointeeType(); 9147 9148 // Never warn about void type pointers. This can be used to suppress 9149 // false positives. 9150 if (PointeeTy->isVoidType()) 9151 continue; 9152 9153 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by 9154 // actually comparing the expressions for equality. Because computing the 9155 // expression IDs can be expensive, we only do this if the diagnostic is 9156 // enabled. 9157 if (SizeOfArg && 9158 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, 9159 SizeOfArg->getExprLoc())) { 9160 // We only compute IDs for expressions if the warning is enabled, and 9161 // cache the sizeof arg's ID. 9162 if (SizeOfArgID == llvm::FoldingSetNodeID()) 9163 SizeOfArg->Profile(SizeOfArgID, Context, true); 9164 llvm::FoldingSetNodeID DestID; 9165 Dest->Profile(DestID, Context, true); 9166 if (DestID == SizeOfArgID) { 9167 // TODO: For strncpy() and friends, this could suggest sizeof(dst) 9168 // over sizeof(src) as well. 9169 unsigned ActionIdx = 0; // Default is to suggest dereferencing. 9170 StringRef ReadableName = FnName->getName(); 9171 9172 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) 9173 if (UnaryOp->getOpcode() == UO_AddrOf) 9174 ActionIdx = 1; // If its an address-of operator, just remove it. 9175 if (!PointeeTy->isIncompleteType() && 9176 (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) 9177 ActionIdx = 2; // If the pointee's size is sizeof(char), 9178 // suggest an explicit length. 9179 9180 // If the function is defined as a builtin macro, do not show macro 9181 // expansion. 9182 SourceLocation SL = SizeOfArg->getExprLoc(); 9183 SourceRange DSR = Dest->getSourceRange(); 9184 SourceRange SSR = SizeOfArg->getSourceRange(); 9185 SourceManager &SM = getSourceManager(); 9186 9187 if (SM.isMacroArgExpansion(SL)) { 9188 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); 9189 SL = SM.getSpellingLoc(SL); 9190 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), 9191 SM.getSpellingLoc(DSR.getEnd())); 9192 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), 9193 SM.getSpellingLoc(SSR.getEnd())); 9194 } 9195 9196 DiagRuntimeBehavior(SL, SizeOfArg, 9197 PDiag(diag::warn_sizeof_pointer_expr_memaccess) 9198 << ReadableName 9199 << PointeeTy 9200 << DestTy 9201 << DSR 9202 << SSR); 9203 DiagRuntimeBehavior(SL, SizeOfArg, 9204 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) 9205 << ActionIdx 9206 << SSR); 9207 9208 break; 9209 } 9210 } 9211 9212 // Also check for cases where the sizeof argument is the exact same 9213 // type as the memory argument, and where it points to a user-defined 9214 // record type. 9215 if (SizeOfArgTy != QualType()) { 9216 if (PointeeTy->isRecordType() && 9217 Context.typesAreCompatible(SizeOfArgTy, DestTy)) { 9218 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, 9219 PDiag(diag::warn_sizeof_pointer_type_memaccess) 9220 << FnName << SizeOfArgTy << ArgIdx 9221 << PointeeTy << Dest->getSourceRange() 9222 << LenExpr->getSourceRange()); 9223 break; 9224 } 9225 } 9226 } else if (DestTy->isArrayType()) { 9227 PointeeTy = DestTy; 9228 } 9229 9230 if (PointeeTy == QualType()) 9231 continue; 9232 9233 // Always complain about dynamic classes. 9234 bool IsContained; 9235 if (const CXXRecordDecl *ContainedRD = 9236 getContainedDynamicClass(PointeeTy, IsContained)) { 9237 9238 unsigned OperationType = 0; 9239 const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp; 9240 // "overwritten" if we're warning about the destination for any call 9241 // but memcmp; otherwise a verb appropriate to the call. 9242 if (ArgIdx != 0 || IsCmp) { 9243 if (BId == Builtin::BImemcpy) 9244 OperationType = 1; 9245 else if(BId == Builtin::BImemmove) 9246 OperationType = 2; 9247 else if (IsCmp) 9248 OperationType = 3; 9249 } 9250 9251 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9252 PDiag(diag::warn_dyn_class_memaccess) 9253 << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName 9254 << IsContained << ContainedRD << OperationType 9255 << Call->getCallee()->getSourceRange()); 9256 } else if (PointeeTy.hasNonTrivialObjCLifetime() && 9257 BId != Builtin::BImemset) 9258 DiagRuntimeBehavior( 9259 Dest->getExprLoc(), Dest, 9260 PDiag(diag::warn_arc_object_memaccess) 9261 << ArgIdx << FnName << PointeeTy 9262 << Call->getCallee()->getSourceRange()); 9263 else if (const auto *RT = PointeeTy->getAs<RecordType>()) { 9264 if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 9265 RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) { 9266 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9267 PDiag(diag::warn_cstruct_memaccess) 9268 << ArgIdx << FnName << PointeeTy << 0); 9269 SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this); 9270 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 9271 RT->getDecl()->isNonTrivialToPrimitiveCopy()) { 9272 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9273 PDiag(diag::warn_cstruct_memaccess) 9274 << ArgIdx << FnName << PointeeTy << 1); 9275 SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this); 9276 } else { 9277 continue; 9278 } 9279 } else 9280 continue; 9281 9282 DiagRuntimeBehavior( 9283 Dest->getExprLoc(), Dest, 9284 PDiag(diag::note_bad_memaccess_silence) 9285 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); 9286 break; 9287 } 9288 } 9289 9290 // A little helper routine: ignore addition and subtraction of integer literals. 9291 // This intentionally does not ignore all integer constant expressions because 9292 // we don't want to remove sizeof(). 9293 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { 9294 Ex = Ex->IgnoreParenCasts(); 9295 9296 while (true) { 9297 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); 9298 if (!BO || !BO->isAdditiveOp()) 9299 break; 9300 9301 const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); 9302 const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); 9303 9304 if (isa<IntegerLiteral>(RHS)) 9305 Ex = LHS; 9306 else if (isa<IntegerLiteral>(LHS)) 9307 Ex = RHS; 9308 else 9309 break; 9310 } 9311 9312 return Ex; 9313 } 9314 9315 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, 9316 ASTContext &Context) { 9317 // Only handle constant-sized or VLAs, but not flexible members. 9318 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { 9319 // Only issue the FIXIT for arrays of size > 1. 9320 if (CAT->getSize().getSExtValue() <= 1) 9321 return false; 9322 } else if (!Ty->isVariableArrayType()) { 9323 return false; 9324 } 9325 return true; 9326 } 9327 9328 // Warn if the user has made the 'size' argument to strlcpy or strlcat 9329 // be the size of the source, instead of the destination. 9330 void Sema::CheckStrlcpycatArguments(const CallExpr *Call, 9331 IdentifierInfo *FnName) { 9332 9333 // Don't crash if the user has the wrong number of arguments 9334 unsigned NumArgs = Call->getNumArgs(); 9335 if ((NumArgs != 3) && (NumArgs != 4)) 9336 return; 9337 9338 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); 9339 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); 9340 const Expr *CompareWithSrc = nullptr; 9341 9342 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, 9343 Call->getBeginLoc(), Call->getRParenLoc())) 9344 return; 9345 9346 // Look for 'strlcpy(dst, x, sizeof(x))' 9347 if (const Expr *Ex = getSizeOfExprArg(SizeArg)) 9348 CompareWithSrc = Ex; 9349 else { 9350 // Look for 'strlcpy(dst, x, strlen(x))' 9351 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { 9352 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && 9353 SizeCall->getNumArgs() == 1) 9354 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); 9355 } 9356 } 9357 9358 if (!CompareWithSrc) 9359 return; 9360 9361 // Determine if the argument to sizeof/strlen is equal to the source 9362 // argument. In principle there's all kinds of things you could do 9363 // here, for instance creating an == expression and evaluating it with 9364 // EvaluateAsBooleanCondition, but this uses a more direct technique: 9365 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); 9366 if (!SrcArgDRE) 9367 return; 9368 9369 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); 9370 if (!CompareWithSrcDRE || 9371 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) 9372 return; 9373 9374 const Expr *OriginalSizeArg = Call->getArg(2); 9375 Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size) 9376 << OriginalSizeArg->getSourceRange() << FnName; 9377 9378 // Output a FIXIT hint if the destination is an array (rather than a 9379 // pointer to an array). This could be enhanced to handle some 9380 // pointers if we know the actual size, like if DstArg is 'array+2' 9381 // we could say 'sizeof(array)-2'. 9382 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); 9383 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) 9384 return; 9385 9386 SmallString<128> sizeString; 9387 llvm::raw_svector_ostream OS(sizeString); 9388 OS << "sizeof("; 9389 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9390 OS << ")"; 9391 9392 Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size) 9393 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), 9394 OS.str()); 9395 } 9396 9397 /// Check if two expressions refer to the same declaration. 9398 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { 9399 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) 9400 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) 9401 return D1->getDecl() == D2->getDecl(); 9402 return false; 9403 } 9404 9405 static const Expr *getStrlenExprArg(const Expr *E) { 9406 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 9407 const FunctionDecl *FD = CE->getDirectCallee(); 9408 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) 9409 return nullptr; 9410 return CE->getArg(0)->IgnoreParenCasts(); 9411 } 9412 return nullptr; 9413 } 9414 9415 // Warn on anti-patterns as the 'size' argument to strncat. 9416 // The correct size argument should look like following: 9417 // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); 9418 void Sema::CheckStrncatArguments(const CallExpr *CE, 9419 IdentifierInfo *FnName) { 9420 // Don't crash if the user has the wrong number of arguments. 9421 if (CE->getNumArgs() < 3) 9422 return; 9423 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); 9424 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); 9425 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); 9426 9427 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(), 9428 CE->getRParenLoc())) 9429 return; 9430 9431 // Identify common expressions, which are wrongly used as the size argument 9432 // to strncat and may lead to buffer overflows. 9433 unsigned PatternType = 0; 9434 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { 9435 // - sizeof(dst) 9436 if (referToTheSameDecl(SizeOfArg, DstArg)) 9437 PatternType = 1; 9438 // - sizeof(src) 9439 else if (referToTheSameDecl(SizeOfArg, SrcArg)) 9440 PatternType = 2; 9441 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { 9442 if (BE->getOpcode() == BO_Sub) { 9443 const Expr *L = BE->getLHS()->IgnoreParenCasts(); 9444 const Expr *R = BE->getRHS()->IgnoreParenCasts(); 9445 // - sizeof(dst) - strlen(dst) 9446 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && 9447 referToTheSameDecl(DstArg, getStrlenExprArg(R))) 9448 PatternType = 1; 9449 // - sizeof(src) - (anything) 9450 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) 9451 PatternType = 2; 9452 } 9453 } 9454 9455 if (PatternType == 0) 9456 return; 9457 9458 // Generate the diagnostic. 9459 SourceLocation SL = LenArg->getBeginLoc(); 9460 SourceRange SR = LenArg->getSourceRange(); 9461 SourceManager &SM = getSourceManager(); 9462 9463 // If the function is defined as a builtin macro, do not show macro expansion. 9464 if (SM.isMacroArgExpansion(SL)) { 9465 SL = SM.getSpellingLoc(SL); 9466 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), 9467 SM.getSpellingLoc(SR.getEnd())); 9468 } 9469 9470 // Check if the destination is an array (rather than a pointer to an array). 9471 QualType DstTy = DstArg->getType(); 9472 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, 9473 Context); 9474 if (!isKnownSizeArray) { 9475 if (PatternType == 1) 9476 Diag(SL, diag::warn_strncat_wrong_size) << SR; 9477 else 9478 Diag(SL, diag::warn_strncat_src_size) << SR; 9479 return; 9480 } 9481 9482 if (PatternType == 1) 9483 Diag(SL, diag::warn_strncat_large_size) << SR; 9484 else 9485 Diag(SL, diag::warn_strncat_src_size) << SR; 9486 9487 SmallString<128> sizeString; 9488 llvm::raw_svector_ostream OS(sizeString); 9489 OS << "sizeof("; 9490 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9491 OS << ") - "; 9492 OS << "strlen("; 9493 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9494 OS << ") - 1"; 9495 9496 Diag(SL, diag::note_strncat_wrong_size) 9497 << FixItHint::CreateReplacement(SR, OS.str()); 9498 } 9499 9500 void 9501 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, 9502 SourceLocation ReturnLoc, 9503 bool isObjCMethod, 9504 const AttrVec *Attrs, 9505 const FunctionDecl *FD) { 9506 // Check if the return value is null but should not be. 9507 if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || 9508 (!isObjCMethod && isNonNullType(Context, lhsType))) && 9509 CheckNonNullExpr(*this, RetValExp)) 9510 Diag(ReturnLoc, diag::warn_null_ret) 9511 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); 9512 9513 // C++11 [basic.stc.dynamic.allocation]p4: 9514 // If an allocation function declared with a non-throwing 9515 // exception-specification fails to allocate storage, it shall return 9516 // a null pointer. Any other allocation function that fails to allocate 9517 // storage shall indicate failure only by throwing an exception [...] 9518 if (FD) { 9519 OverloadedOperatorKind Op = FD->getOverloadedOperator(); 9520 if (Op == OO_New || Op == OO_Array_New) { 9521 const FunctionProtoType *Proto 9522 = FD->getType()->castAs<FunctionProtoType>(); 9523 if (!Proto->isNothrow(/*ResultIfDependent*/true) && 9524 CheckNonNullExpr(*this, RetValExp)) 9525 Diag(ReturnLoc, diag::warn_operator_new_returns_null) 9526 << FD << getLangOpts().CPlusPlus11; 9527 } 9528 } 9529 } 9530 9531 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// 9532 9533 /// Check for comparisons of floating point operands using != and ==. 9534 /// Issue a warning if these are no self-comparisons, as they are not likely 9535 /// to do what the programmer intended. 9536 void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { 9537 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); 9538 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); 9539 9540 // Special case: check for x == x (which is OK). 9541 // Do not emit warnings for such cases. 9542 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) 9543 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) 9544 if (DRL->getDecl() == DRR->getDecl()) 9545 return; 9546 9547 // Special case: check for comparisons against literals that can be exactly 9548 // represented by APFloat. In such cases, do not emit a warning. This 9549 // is a heuristic: often comparison against such literals are used to 9550 // detect if a value in a variable has not changed. This clearly can 9551 // lead to false negatives. 9552 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { 9553 if (FLL->isExact()) 9554 return; 9555 } else 9556 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) 9557 if (FLR->isExact()) 9558 return; 9559 9560 // Check for comparisons with builtin types. 9561 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) 9562 if (CL->getBuiltinCallee()) 9563 return; 9564 9565 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) 9566 if (CR->getBuiltinCallee()) 9567 return; 9568 9569 // Emit the diagnostic. 9570 Diag(Loc, diag::warn_floatingpoint_eq) 9571 << LHS->getSourceRange() << RHS->getSourceRange(); 9572 } 9573 9574 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// 9575 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// 9576 9577 namespace { 9578 9579 /// Structure recording the 'active' range of an integer-valued 9580 /// expression. 9581 struct IntRange { 9582 /// The number of bits active in the int. 9583 unsigned Width; 9584 9585 /// True if the int is known not to have negative values. 9586 bool NonNegative; 9587 9588 IntRange(unsigned Width, bool NonNegative) 9589 : Width(Width), NonNegative(NonNegative) {} 9590 9591 /// Returns the range of the bool type. 9592 static IntRange forBoolType() { 9593 return IntRange(1, true); 9594 } 9595 9596 /// Returns the range of an opaque value of the given integral type. 9597 static IntRange forValueOfType(ASTContext &C, QualType T) { 9598 return forValueOfCanonicalType(C, 9599 T->getCanonicalTypeInternal().getTypePtr()); 9600 } 9601 9602 /// Returns the range of an opaque value of a canonical integral type. 9603 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { 9604 assert(T->isCanonicalUnqualified()); 9605 9606 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9607 T = VT->getElementType().getTypePtr(); 9608 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9609 T = CT->getElementType().getTypePtr(); 9610 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9611 T = AT->getValueType().getTypePtr(); 9612 9613 if (!C.getLangOpts().CPlusPlus) { 9614 // For enum types in C code, use the underlying datatype. 9615 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9616 T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr(); 9617 } else if (const EnumType *ET = dyn_cast<EnumType>(T)) { 9618 // For enum types in C++, use the known bit width of the enumerators. 9619 EnumDecl *Enum = ET->getDecl(); 9620 // In C++11, enums can have a fixed underlying type. Use this type to 9621 // compute the range. 9622 if (Enum->isFixed()) { 9623 return IntRange(C.getIntWidth(QualType(T, 0)), 9624 !ET->isSignedIntegerOrEnumerationType()); 9625 } 9626 9627 unsigned NumPositive = Enum->getNumPositiveBits(); 9628 unsigned NumNegative = Enum->getNumNegativeBits(); 9629 9630 if (NumNegative == 0) 9631 return IntRange(NumPositive, true/*NonNegative*/); 9632 else 9633 return IntRange(std::max(NumPositive + 1, NumNegative), 9634 false/*NonNegative*/); 9635 } 9636 9637 const BuiltinType *BT = cast<BuiltinType>(T); 9638 assert(BT->isInteger()); 9639 9640 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9641 } 9642 9643 /// Returns the "target" range of a canonical integral type, i.e. 9644 /// the range of values expressible in the type. 9645 /// 9646 /// This matches forValueOfCanonicalType except that enums have the 9647 /// full range of their type, not the range of their enumerators. 9648 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { 9649 assert(T->isCanonicalUnqualified()); 9650 9651 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9652 T = VT->getElementType().getTypePtr(); 9653 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9654 T = CT->getElementType().getTypePtr(); 9655 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9656 T = AT->getValueType().getTypePtr(); 9657 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9658 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); 9659 9660 const BuiltinType *BT = cast<BuiltinType>(T); 9661 assert(BT->isInteger()); 9662 9663 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9664 } 9665 9666 /// Returns the supremum of two ranges: i.e. their conservative merge. 9667 static IntRange join(IntRange L, IntRange R) { 9668 return IntRange(std::max(L.Width, R.Width), 9669 L.NonNegative && R.NonNegative); 9670 } 9671 9672 /// Returns the infinum of two ranges: i.e. their aggressive merge. 9673 static IntRange meet(IntRange L, IntRange R) { 9674 return IntRange(std::min(L.Width, R.Width), 9675 L.NonNegative || R.NonNegative); 9676 } 9677 }; 9678 9679 } // namespace 9680 9681 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, 9682 unsigned MaxWidth) { 9683 if (value.isSigned() && value.isNegative()) 9684 return IntRange(value.getMinSignedBits(), false); 9685 9686 if (value.getBitWidth() > MaxWidth) 9687 value = value.trunc(MaxWidth); 9688 9689 // isNonNegative() just checks the sign bit without considering 9690 // signedness. 9691 return IntRange(value.getActiveBits(), true); 9692 } 9693 9694 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, 9695 unsigned MaxWidth) { 9696 if (result.isInt()) 9697 return GetValueRange(C, result.getInt(), MaxWidth); 9698 9699 if (result.isVector()) { 9700 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); 9701 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { 9702 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); 9703 R = IntRange::join(R, El); 9704 } 9705 return R; 9706 } 9707 9708 if (result.isComplexInt()) { 9709 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); 9710 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); 9711 return IntRange::join(R, I); 9712 } 9713 9714 // This can happen with lossless casts to intptr_t of "based" lvalues. 9715 // Assume it might use arbitrary bits. 9716 // FIXME: The only reason we need to pass the type in here is to get 9717 // the sign right on this one case. It would be nice if APValue 9718 // preserved this. 9719 assert(result.isLValue() || result.isAddrLabelDiff()); 9720 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); 9721 } 9722 9723 static QualType GetExprType(const Expr *E) { 9724 QualType Ty = E->getType(); 9725 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) 9726 Ty = AtomicRHS->getValueType(); 9727 return Ty; 9728 } 9729 9730 /// Pseudo-evaluate the given integer expression, estimating the 9731 /// range of values it might take. 9732 /// 9733 /// \param MaxWidth - the width to which the value will be truncated 9734 static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth) { 9735 E = E->IgnoreParens(); 9736 9737 // Try a full evaluation first. 9738 Expr::EvalResult result; 9739 if (E->EvaluateAsRValue(result, C)) 9740 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); 9741 9742 // I think we only want to look through implicit casts here; if the 9743 // user has an explicit widening cast, we should treat the value as 9744 // being of the new, wider type. 9745 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) { 9746 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) 9747 return GetExprRange(C, CE->getSubExpr(), MaxWidth); 9748 9749 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); 9750 9751 bool isIntegerCast = CE->getCastKind() == CK_IntegralCast || 9752 CE->getCastKind() == CK_BooleanToSignedIntegral; 9753 9754 // Assume that non-integer casts can span the full range of the type. 9755 if (!isIntegerCast) 9756 return OutputTypeRange; 9757 9758 IntRange SubRange 9759 = GetExprRange(C, CE->getSubExpr(), 9760 std::min(MaxWidth, OutputTypeRange.Width)); 9761 9762 // Bail out if the subexpr's range is as wide as the cast type. 9763 if (SubRange.Width >= OutputTypeRange.Width) 9764 return OutputTypeRange; 9765 9766 // Otherwise, we take the smaller width, and we're non-negative if 9767 // either the output type or the subexpr is. 9768 return IntRange(SubRange.Width, 9769 SubRange.NonNegative || OutputTypeRange.NonNegative); 9770 } 9771 9772 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 9773 // If we can fold the condition, just take that operand. 9774 bool CondResult; 9775 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) 9776 return GetExprRange(C, CondResult ? CO->getTrueExpr() 9777 : CO->getFalseExpr(), 9778 MaxWidth); 9779 9780 // Otherwise, conservatively merge. 9781 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); 9782 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); 9783 return IntRange::join(L, R); 9784 } 9785 9786 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 9787 switch (BO->getOpcode()) { 9788 case BO_Cmp: 9789 llvm_unreachable("builtin <=> should have class type"); 9790 9791 // Boolean-valued operations are single-bit and positive. 9792 case BO_LAnd: 9793 case BO_LOr: 9794 case BO_LT: 9795 case BO_GT: 9796 case BO_LE: 9797 case BO_GE: 9798 case BO_EQ: 9799 case BO_NE: 9800 return IntRange::forBoolType(); 9801 9802 // The type of the assignments is the type of the LHS, so the RHS 9803 // is not necessarily the same type. 9804 case BO_MulAssign: 9805 case BO_DivAssign: 9806 case BO_RemAssign: 9807 case BO_AddAssign: 9808 case BO_SubAssign: 9809 case BO_XorAssign: 9810 case BO_OrAssign: 9811 // TODO: bitfields? 9812 return IntRange::forValueOfType(C, GetExprType(E)); 9813 9814 // Simple assignments just pass through the RHS, which will have 9815 // been coerced to the LHS type. 9816 case BO_Assign: 9817 // TODO: bitfields? 9818 return GetExprRange(C, BO->getRHS(), MaxWidth); 9819 9820 // Operations with opaque sources are black-listed. 9821 case BO_PtrMemD: 9822 case BO_PtrMemI: 9823 return IntRange::forValueOfType(C, GetExprType(E)); 9824 9825 // Bitwise-and uses the *infinum* of the two source ranges. 9826 case BO_And: 9827 case BO_AndAssign: 9828 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), 9829 GetExprRange(C, BO->getRHS(), MaxWidth)); 9830 9831 // Left shift gets black-listed based on a judgement call. 9832 case BO_Shl: 9833 // ...except that we want to treat '1 << (blah)' as logically 9834 // positive. It's an important idiom. 9835 if (IntegerLiteral *I 9836 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { 9837 if (I->getValue() == 1) { 9838 IntRange R = IntRange::forValueOfType(C, GetExprType(E)); 9839 return IntRange(R.Width, /*NonNegative*/ true); 9840 } 9841 } 9842 LLVM_FALLTHROUGH; 9843 9844 case BO_ShlAssign: 9845 return IntRange::forValueOfType(C, GetExprType(E)); 9846 9847 // Right shift by a constant can narrow its left argument. 9848 case BO_Shr: 9849 case BO_ShrAssign: { 9850 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); 9851 9852 // If the shift amount is a positive constant, drop the width by 9853 // that much. 9854 llvm::APSInt shift; 9855 if (BO->getRHS()->isIntegerConstantExpr(shift, C) && 9856 shift.isNonNegative()) { 9857 unsigned zext = shift.getZExtValue(); 9858 if (zext >= L.Width) 9859 L.Width = (L.NonNegative ? 0 : 1); 9860 else 9861 L.Width -= zext; 9862 } 9863 9864 return L; 9865 } 9866 9867 // Comma acts as its right operand. 9868 case BO_Comma: 9869 return GetExprRange(C, BO->getRHS(), MaxWidth); 9870 9871 // Black-list pointer subtractions. 9872 case BO_Sub: 9873 if (BO->getLHS()->getType()->isPointerType()) 9874 return IntRange::forValueOfType(C, GetExprType(E)); 9875 break; 9876 9877 // The width of a division result is mostly determined by the size 9878 // of the LHS. 9879 case BO_Div: { 9880 // Don't 'pre-truncate' the operands. 9881 unsigned opWidth = C.getIntWidth(GetExprType(E)); 9882 IntRange L = GetExprRange(C, BO->getLHS(), opWidth); 9883 9884 // If the divisor is constant, use that. 9885 llvm::APSInt divisor; 9886 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { 9887 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) 9888 if (log2 >= L.Width) 9889 L.Width = (L.NonNegative ? 0 : 1); 9890 else 9891 L.Width = std::min(L.Width - log2, MaxWidth); 9892 return L; 9893 } 9894 9895 // Otherwise, just use the LHS's width. 9896 IntRange R = GetExprRange(C, BO->getRHS(), opWidth); 9897 return IntRange(L.Width, L.NonNegative && R.NonNegative); 9898 } 9899 9900 // The result of a remainder can't be larger than the result of 9901 // either side. 9902 case BO_Rem: { 9903 // Don't 'pre-truncate' the operands. 9904 unsigned opWidth = C.getIntWidth(GetExprType(E)); 9905 IntRange L = GetExprRange(C, BO->getLHS(), opWidth); 9906 IntRange R = GetExprRange(C, BO->getRHS(), opWidth); 9907 9908 IntRange meet = IntRange::meet(L, R); 9909 meet.Width = std::min(meet.Width, MaxWidth); 9910 return meet; 9911 } 9912 9913 // The default behavior is okay for these. 9914 case BO_Mul: 9915 case BO_Add: 9916 case BO_Xor: 9917 case BO_Or: 9918 break; 9919 } 9920 9921 // The default case is to treat the operation as if it were closed 9922 // on the narrowest type that encompasses both operands. 9923 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); 9924 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); 9925 return IntRange::join(L, R); 9926 } 9927 9928 if (const auto *UO = dyn_cast<UnaryOperator>(E)) { 9929 switch (UO->getOpcode()) { 9930 // Boolean-valued operations are white-listed. 9931 case UO_LNot: 9932 return IntRange::forBoolType(); 9933 9934 // Operations with opaque sources are black-listed. 9935 case UO_Deref: 9936 case UO_AddrOf: // should be impossible 9937 return IntRange::forValueOfType(C, GetExprType(E)); 9938 9939 default: 9940 return GetExprRange(C, UO->getSubExpr(), MaxWidth); 9941 } 9942 } 9943 9944 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 9945 return GetExprRange(C, OVE->getSourceExpr(), MaxWidth); 9946 9947 if (const auto *BitField = E->getSourceBitField()) 9948 return IntRange(BitField->getBitWidthValue(C), 9949 BitField->getType()->isUnsignedIntegerOrEnumerationType()); 9950 9951 return IntRange::forValueOfType(C, GetExprType(E)); 9952 } 9953 9954 static IntRange GetExprRange(ASTContext &C, const Expr *E) { 9955 return GetExprRange(C, E, C.getIntWidth(GetExprType(E))); 9956 } 9957 9958 /// Checks whether the given value, which currently has the given 9959 /// source semantics, has the same value when coerced through the 9960 /// target semantics. 9961 static bool IsSameFloatAfterCast(const llvm::APFloat &value, 9962 const llvm::fltSemantics &Src, 9963 const llvm::fltSemantics &Tgt) { 9964 llvm::APFloat truncated = value; 9965 9966 bool ignored; 9967 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); 9968 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); 9969 9970 return truncated.bitwiseIsEqual(value); 9971 } 9972 9973 /// Checks whether the given value, which currently has the given 9974 /// source semantics, has the same value when coerced through the 9975 /// target semantics. 9976 /// 9977 /// The value might be a vector of floats (or a complex number). 9978 static bool IsSameFloatAfterCast(const APValue &value, 9979 const llvm::fltSemantics &Src, 9980 const llvm::fltSemantics &Tgt) { 9981 if (value.isFloat()) 9982 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); 9983 9984 if (value.isVector()) { 9985 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) 9986 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) 9987 return false; 9988 return true; 9989 } 9990 9991 assert(value.isComplexFloat()); 9992 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && 9993 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); 9994 } 9995 9996 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); 9997 9998 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) { 9999 // Suppress cases where we are comparing against an enum constant. 10000 if (const DeclRefExpr *DR = 10001 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) 10002 if (isa<EnumConstantDecl>(DR->getDecl())) 10003 return true; 10004 10005 // Suppress cases where the '0' value is expanded from a macro. 10006 if (E->getBeginLoc().isMacroID()) 10007 return true; 10008 10009 return false; 10010 } 10011 10012 static bool isKnownToHaveUnsignedValue(Expr *E) { 10013 return E->getType()->isIntegerType() && 10014 (!E->getType()->isSignedIntegerType() || 10015 !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType()); 10016 } 10017 10018 namespace { 10019 /// The promoted range of values of a type. In general this has the 10020 /// following structure: 10021 /// 10022 /// |-----------| . . . |-----------| 10023 /// ^ ^ ^ ^ 10024 /// Min HoleMin HoleMax Max 10025 /// 10026 /// ... where there is only a hole if a signed type is promoted to unsigned 10027 /// (in which case Min and Max are the smallest and largest representable 10028 /// values). 10029 struct PromotedRange { 10030 // Min, or HoleMax if there is a hole. 10031 llvm::APSInt PromotedMin; 10032 // Max, or HoleMin if there is a hole. 10033 llvm::APSInt PromotedMax; 10034 10035 PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) { 10036 if (R.Width == 0) 10037 PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned); 10038 else if (R.Width >= BitWidth && !Unsigned) { 10039 // Promotion made the type *narrower*. This happens when promoting 10040 // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'. 10041 // Treat all values of 'signed int' as being in range for now. 10042 PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned); 10043 PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned); 10044 } else { 10045 PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative) 10046 .extOrTrunc(BitWidth); 10047 PromotedMin.setIsUnsigned(Unsigned); 10048 10049 PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative) 10050 .extOrTrunc(BitWidth); 10051 PromotedMax.setIsUnsigned(Unsigned); 10052 } 10053 } 10054 10055 // Determine whether this range is contiguous (has no hole). 10056 bool isContiguous() const { return PromotedMin <= PromotedMax; } 10057 10058 // Where a constant value is within the range. 10059 enum ComparisonResult { 10060 LT = 0x1, 10061 LE = 0x2, 10062 GT = 0x4, 10063 GE = 0x8, 10064 EQ = 0x10, 10065 NE = 0x20, 10066 InRangeFlag = 0x40, 10067 10068 Less = LE | LT | NE, 10069 Min = LE | InRangeFlag, 10070 InRange = InRangeFlag, 10071 Max = GE | InRangeFlag, 10072 Greater = GE | GT | NE, 10073 10074 OnlyValue = LE | GE | EQ | InRangeFlag, 10075 InHole = NE 10076 }; 10077 10078 ComparisonResult compare(const llvm::APSInt &Value) const { 10079 assert(Value.getBitWidth() == PromotedMin.getBitWidth() && 10080 Value.isUnsigned() == PromotedMin.isUnsigned()); 10081 if (!isContiguous()) { 10082 assert(Value.isUnsigned() && "discontiguous range for signed compare"); 10083 if (Value.isMinValue()) return Min; 10084 if (Value.isMaxValue()) return Max; 10085 if (Value >= PromotedMin) return InRange; 10086 if (Value <= PromotedMax) return InRange; 10087 return InHole; 10088 } 10089 10090 switch (llvm::APSInt::compareValues(Value, PromotedMin)) { 10091 case -1: return Less; 10092 case 0: return PromotedMin == PromotedMax ? OnlyValue : Min; 10093 case 1: 10094 switch (llvm::APSInt::compareValues(Value, PromotedMax)) { 10095 case -1: return InRange; 10096 case 0: return Max; 10097 case 1: return Greater; 10098 } 10099 } 10100 10101 llvm_unreachable("impossible compare result"); 10102 } 10103 10104 static llvm::Optional<StringRef> 10105 constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) { 10106 if (Op == BO_Cmp) { 10107 ComparisonResult LTFlag = LT, GTFlag = GT; 10108 if (ConstantOnRHS) std::swap(LTFlag, GTFlag); 10109 10110 if (R & EQ) return StringRef("'std::strong_ordering::equal'"); 10111 if (R & LTFlag) return StringRef("'std::strong_ordering::less'"); 10112 if (R & GTFlag) return StringRef("'std::strong_ordering::greater'"); 10113 return llvm::None; 10114 } 10115 10116 ComparisonResult TrueFlag, FalseFlag; 10117 if (Op == BO_EQ) { 10118 TrueFlag = EQ; 10119 FalseFlag = NE; 10120 } else if (Op == BO_NE) { 10121 TrueFlag = NE; 10122 FalseFlag = EQ; 10123 } else { 10124 if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) { 10125 TrueFlag = LT; 10126 FalseFlag = GE; 10127 } else { 10128 TrueFlag = GT; 10129 FalseFlag = LE; 10130 } 10131 if (Op == BO_GE || Op == BO_LE) 10132 std::swap(TrueFlag, FalseFlag); 10133 } 10134 if (R & TrueFlag) 10135 return StringRef("true"); 10136 if (R & FalseFlag) 10137 return StringRef("false"); 10138 return llvm::None; 10139 } 10140 }; 10141 } 10142 10143 static bool HasEnumType(Expr *E) { 10144 // Strip off implicit integral promotions. 10145 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 10146 if (ICE->getCastKind() != CK_IntegralCast && 10147 ICE->getCastKind() != CK_NoOp) 10148 break; 10149 E = ICE->getSubExpr(); 10150 } 10151 10152 return E->getType()->isEnumeralType(); 10153 } 10154 10155 static int classifyConstantValue(Expr *Constant) { 10156 // The values of this enumeration are used in the diagnostics 10157 // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare. 10158 enum ConstantValueKind { 10159 Miscellaneous = 0, 10160 LiteralTrue, 10161 LiteralFalse 10162 }; 10163 if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant)) 10164 return BL->getValue() ? ConstantValueKind::LiteralTrue 10165 : ConstantValueKind::LiteralFalse; 10166 return ConstantValueKind::Miscellaneous; 10167 } 10168 10169 static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E, 10170 Expr *Constant, Expr *Other, 10171 const llvm::APSInt &Value, 10172 bool RhsConstant) { 10173 if (S.inTemplateInstantiation()) 10174 return false; 10175 10176 Expr *OriginalOther = Other; 10177 10178 Constant = Constant->IgnoreParenImpCasts(); 10179 Other = Other->IgnoreParenImpCasts(); 10180 10181 // Suppress warnings on tautological comparisons between values of the same 10182 // enumeration type. There are only two ways we could warn on this: 10183 // - If the constant is outside the range of representable values of 10184 // the enumeration. In such a case, we should warn about the cast 10185 // to enumeration type, not about the comparison. 10186 // - If the constant is the maximum / minimum in-range value. For an 10187 // enumeratin type, such comparisons can be meaningful and useful. 10188 if (Constant->getType()->isEnumeralType() && 10189 S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType())) 10190 return false; 10191 10192 // TODO: Investigate using GetExprRange() to get tighter bounds 10193 // on the bit ranges. 10194 QualType OtherT = Other->getType(); 10195 if (const auto *AT = OtherT->getAs<AtomicType>()) 10196 OtherT = AT->getValueType(); 10197 IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); 10198 10199 // Whether we're treating Other as being a bool because of the form of 10200 // expression despite it having another type (typically 'int' in C). 10201 bool OtherIsBooleanDespiteType = 10202 !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue(); 10203 if (OtherIsBooleanDespiteType) 10204 OtherRange = IntRange::forBoolType(); 10205 10206 // Determine the promoted range of the other type and see if a comparison of 10207 // the constant against that range is tautological. 10208 PromotedRange OtherPromotedRange(OtherRange, Value.getBitWidth(), 10209 Value.isUnsigned()); 10210 auto Cmp = OtherPromotedRange.compare(Value); 10211 auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant); 10212 if (!Result) 10213 return false; 10214 10215 // Suppress the diagnostic for an in-range comparison if the constant comes 10216 // from a macro or enumerator. We don't want to diagnose 10217 // 10218 // some_long_value <= INT_MAX 10219 // 10220 // when sizeof(int) == sizeof(long). 10221 bool InRange = Cmp & PromotedRange::InRangeFlag; 10222 if (InRange && IsEnumConstOrFromMacro(S, Constant)) 10223 return false; 10224 10225 // If this is a comparison to an enum constant, include that 10226 // constant in the diagnostic. 10227 const EnumConstantDecl *ED = nullptr; 10228 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) 10229 ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); 10230 10231 // Should be enough for uint128 (39 decimal digits) 10232 SmallString<64> PrettySourceValue; 10233 llvm::raw_svector_ostream OS(PrettySourceValue); 10234 if (ED) 10235 OS << '\'' << *ED << "' (" << Value << ")"; 10236 else 10237 OS << Value; 10238 10239 // FIXME: We use a somewhat different formatting for the in-range cases and 10240 // cases involving boolean values for historical reasons. We should pick a 10241 // consistent way of presenting these diagnostics. 10242 if (!InRange || Other->isKnownToHaveBooleanValue()) { 10243 S.DiagRuntimeBehavior( 10244 E->getOperatorLoc(), E, 10245 S.PDiag(!InRange ? diag::warn_out_of_range_compare 10246 : diag::warn_tautological_bool_compare) 10247 << OS.str() << classifyConstantValue(Constant) 10248 << OtherT << OtherIsBooleanDespiteType << *Result 10249 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); 10250 } else { 10251 unsigned Diag = (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0) 10252 ? (HasEnumType(OriginalOther) 10253 ? diag::warn_unsigned_enum_always_true_comparison 10254 : diag::warn_unsigned_always_true_comparison) 10255 : diag::warn_tautological_constant_compare; 10256 10257 S.Diag(E->getOperatorLoc(), Diag) 10258 << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result 10259 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10260 } 10261 10262 return true; 10263 } 10264 10265 /// Analyze the operands of the given comparison. Implements the 10266 /// fallback case from AnalyzeComparison. 10267 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { 10268 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10269 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10270 } 10271 10272 /// Implements -Wsign-compare. 10273 /// 10274 /// \param E the binary operator to check for warnings 10275 static void AnalyzeComparison(Sema &S, BinaryOperator *E) { 10276 // The type the comparison is being performed in. 10277 QualType T = E->getLHS()->getType(); 10278 10279 // Only analyze comparison operators where both sides have been converted to 10280 // the same type. 10281 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) 10282 return AnalyzeImpConvsInComparison(S, E); 10283 10284 // Don't analyze value-dependent comparisons directly. 10285 if (E->isValueDependent()) 10286 return AnalyzeImpConvsInComparison(S, E); 10287 10288 Expr *LHS = E->getLHS(); 10289 Expr *RHS = E->getRHS(); 10290 10291 if (T->isIntegralType(S.Context)) { 10292 llvm::APSInt RHSValue; 10293 llvm::APSInt LHSValue; 10294 10295 bool IsRHSIntegralLiteral = RHS->isIntegerConstantExpr(RHSValue, S.Context); 10296 bool IsLHSIntegralLiteral = LHS->isIntegerConstantExpr(LHSValue, S.Context); 10297 10298 // We don't care about expressions whose result is a constant. 10299 if (IsRHSIntegralLiteral && IsLHSIntegralLiteral) 10300 return AnalyzeImpConvsInComparison(S, E); 10301 10302 // We only care about expressions where just one side is literal 10303 if (IsRHSIntegralLiteral ^ IsLHSIntegralLiteral) { 10304 // Is the constant on the RHS or LHS? 10305 const bool RhsConstant = IsRHSIntegralLiteral; 10306 Expr *Const = RhsConstant ? RHS : LHS; 10307 Expr *Other = RhsConstant ? LHS : RHS; 10308 const llvm::APSInt &Value = RhsConstant ? RHSValue : LHSValue; 10309 10310 // Check whether an integer constant comparison results in a value 10311 // of 'true' or 'false'. 10312 if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant)) 10313 return AnalyzeImpConvsInComparison(S, E); 10314 } 10315 } 10316 10317 if (!T->hasUnsignedIntegerRepresentation()) { 10318 // We don't do anything special if this isn't an unsigned integral 10319 // comparison: we're only interested in integral comparisons, and 10320 // signed comparisons only happen in cases we don't care to warn about. 10321 return AnalyzeImpConvsInComparison(S, E); 10322 } 10323 10324 LHS = LHS->IgnoreParenImpCasts(); 10325 RHS = RHS->IgnoreParenImpCasts(); 10326 10327 if (!S.getLangOpts().CPlusPlus) { 10328 // Avoid warning about comparison of integers with different signs when 10329 // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of 10330 // the type of `E`. 10331 if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType())) 10332 LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10333 if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType())) 10334 RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10335 } 10336 10337 // Check to see if one of the (unmodified) operands is of different 10338 // signedness. 10339 Expr *signedOperand, *unsignedOperand; 10340 if (LHS->getType()->hasSignedIntegerRepresentation()) { 10341 assert(!RHS->getType()->hasSignedIntegerRepresentation() && 10342 "unsigned comparison between two signed integer expressions?"); 10343 signedOperand = LHS; 10344 unsignedOperand = RHS; 10345 } else if (RHS->getType()->hasSignedIntegerRepresentation()) { 10346 signedOperand = RHS; 10347 unsignedOperand = LHS; 10348 } else { 10349 return AnalyzeImpConvsInComparison(S, E); 10350 } 10351 10352 // Otherwise, calculate the effective range of the signed operand. 10353 IntRange signedRange = GetExprRange(S.Context, signedOperand); 10354 10355 // Go ahead and analyze implicit conversions in the operands. Note 10356 // that we skip the implicit conversions on both sides. 10357 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); 10358 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); 10359 10360 // If the signed range is non-negative, -Wsign-compare won't fire. 10361 if (signedRange.NonNegative) 10362 return; 10363 10364 // For (in)equality comparisons, if the unsigned operand is a 10365 // constant which cannot collide with a overflowed signed operand, 10366 // then reinterpreting the signed operand as unsigned will not 10367 // change the result of the comparison. 10368 if (E->isEqualityOp()) { 10369 unsigned comparisonWidth = S.Context.getIntWidth(T); 10370 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); 10371 10372 // We should never be unable to prove that the unsigned operand is 10373 // non-negative. 10374 assert(unsignedRange.NonNegative && "unsigned range includes negative?"); 10375 10376 if (unsignedRange.Width < comparisonWidth) 10377 return; 10378 } 10379 10380 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10381 S.PDiag(diag::warn_mixed_sign_comparison) 10382 << LHS->getType() << RHS->getType() 10383 << LHS->getSourceRange() << RHS->getSourceRange()); 10384 } 10385 10386 /// Analyzes an attempt to assign the given value to a bitfield. 10387 /// 10388 /// Returns true if there was something fishy about the attempt. 10389 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, 10390 SourceLocation InitLoc) { 10391 assert(Bitfield->isBitField()); 10392 if (Bitfield->isInvalidDecl()) 10393 return false; 10394 10395 // White-list bool bitfields. 10396 QualType BitfieldType = Bitfield->getType(); 10397 if (BitfieldType->isBooleanType()) 10398 return false; 10399 10400 if (BitfieldType->isEnumeralType()) { 10401 EnumDecl *BitfieldEnumDecl = BitfieldType->getAs<EnumType>()->getDecl(); 10402 // If the underlying enum type was not explicitly specified as an unsigned 10403 // type and the enum contain only positive values, MSVC++ will cause an 10404 // inconsistency by storing this as a signed type. 10405 if (S.getLangOpts().CPlusPlus11 && 10406 !BitfieldEnumDecl->getIntegerTypeSourceInfo() && 10407 BitfieldEnumDecl->getNumPositiveBits() > 0 && 10408 BitfieldEnumDecl->getNumNegativeBits() == 0) { 10409 S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield) 10410 << BitfieldEnumDecl->getNameAsString(); 10411 } 10412 } 10413 10414 if (Bitfield->getType()->isBooleanType()) 10415 return false; 10416 10417 // Ignore value- or type-dependent expressions. 10418 if (Bitfield->getBitWidth()->isValueDependent() || 10419 Bitfield->getBitWidth()->isTypeDependent() || 10420 Init->isValueDependent() || 10421 Init->isTypeDependent()) 10422 return false; 10423 10424 Expr *OriginalInit = Init->IgnoreParenImpCasts(); 10425 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); 10426 10427 Expr::EvalResult Result; 10428 if (!OriginalInit->EvaluateAsInt(Result, S.Context, 10429 Expr::SE_AllowSideEffects)) { 10430 // The RHS is not constant. If the RHS has an enum type, make sure the 10431 // bitfield is wide enough to hold all the values of the enum without 10432 // truncation. 10433 if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) { 10434 EnumDecl *ED = EnumTy->getDecl(); 10435 bool SignedBitfield = BitfieldType->isSignedIntegerType(); 10436 10437 // Enum types are implicitly signed on Windows, so check if there are any 10438 // negative enumerators to see if the enum was intended to be signed or 10439 // not. 10440 bool SignedEnum = ED->getNumNegativeBits() > 0; 10441 10442 // Check for surprising sign changes when assigning enum values to a 10443 // bitfield of different signedness. If the bitfield is signed and we 10444 // have exactly the right number of bits to store this unsigned enum, 10445 // suggest changing the enum to an unsigned type. This typically happens 10446 // on Windows where unfixed enums always use an underlying type of 'int'. 10447 unsigned DiagID = 0; 10448 if (SignedEnum && !SignedBitfield) { 10449 DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum; 10450 } else if (SignedBitfield && !SignedEnum && 10451 ED->getNumPositiveBits() == FieldWidth) { 10452 DiagID = diag::warn_signed_bitfield_enum_conversion; 10453 } 10454 10455 if (DiagID) { 10456 S.Diag(InitLoc, DiagID) << Bitfield << ED; 10457 TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo(); 10458 SourceRange TypeRange = 10459 TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange(); 10460 S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign) 10461 << SignedEnum << TypeRange; 10462 } 10463 10464 // Compute the required bitwidth. If the enum has negative values, we need 10465 // one more bit than the normal number of positive bits to represent the 10466 // sign bit. 10467 unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1, 10468 ED->getNumNegativeBits()) 10469 : ED->getNumPositiveBits(); 10470 10471 // Check the bitwidth. 10472 if (BitsNeeded > FieldWidth) { 10473 Expr *WidthExpr = Bitfield->getBitWidth(); 10474 S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum) 10475 << Bitfield << ED; 10476 S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield) 10477 << BitsNeeded << ED << WidthExpr->getSourceRange(); 10478 } 10479 } 10480 10481 return false; 10482 } 10483 10484 llvm::APSInt Value = Result.Val.getInt(); 10485 10486 unsigned OriginalWidth = Value.getBitWidth(); 10487 10488 if (!Value.isSigned() || Value.isNegative()) 10489 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit)) 10490 if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not) 10491 OriginalWidth = Value.getMinSignedBits(); 10492 10493 if (OriginalWidth <= FieldWidth) 10494 return false; 10495 10496 // Compute the value which the bitfield will contain. 10497 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); 10498 TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType()); 10499 10500 // Check whether the stored value is equal to the original value. 10501 TruncatedValue = TruncatedValue.extend(OriginalWidth); 10502 if (llvm::APSInt::isSameValue(Value, TruncatedValue)) 10503 return false; 10504 10505 // Special-case bitfields of width 1: booleans are naturally 0/1, and 10506 // therefore don't strictly fit into a signed bitfield of width 1. 10507 if (FieldWidth == 1 && Value == 1) 10508 return false; 10509 10510 std::string PrettyValue = Value.toString(10); 10511 std::string PrettyTrunc = TruncatedValue.toString(10); 10512 10513 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) 10514 << PrettyValue << PrettyTrunc << OriginalInit->getType() 10515 << Init->getSourceRange(); 10516 10517 return true; 10518 } 10519 10520 /// Analyze the given simple or compound assignment for warning-worthy 10521 /// operations. 10522 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { 10523 // Just recurse on the LHS. 10524 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10525 10526 // We want to recurse on the RHS as normal unless we're assigning to 10527 // a bitfield. 10528 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { 10529 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), 10530 E->getOperatorLoc())) { 10531 // Recurse, ignoring any implicit conversions on the RHS. 10532 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), 10533 E->getOperatorLoc()); 10534 } 10535 } 10536 10537 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10538 10539 // Diagnose implicitly sequentially-consistent atomic assignment. 10540 if (E->getLHS()->getType()->isAtomicType()) 10541 S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 10542 } 10543 10544 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10545 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 10546 SourceLocation CContext, unsigned diag, 10547 bool pruneControlFlow = false) { 10548 if (pruneControlFlow) { 10549 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10550 S.PDiag(diag) 10551 << SourceType << T << E->getSourceRange() 10552 << SourceRange(CContext)); 10553 return; 10554 } 10555 S.Diag(E->getExprLoc(), diag) 10556 << SourceType << T << E->getSourceRange() << SourceRange(CContext); 10557 } 10558 10559 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10560 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, 10561 SourceLocation CContext, 10562 unsigned diag, bool pruneControlFlow = false) { 10563 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); 10564 } 10565 10566 /// Diagnose an implicit cast from a floating point value to an integer value. 10567 static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T, 10568 SourceLocation CContext) { 10569 const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool); 10570 const bool PruneWarnings = S.inTemplateInstantiation(); 10571 10572 Expr *InnerE = E->IgnoreParenImpCasts(); 10573 // We also want to warn on, e.g., "int i = -1.234" 10574 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) 10575 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) 10576 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); 10577 10578 const bool IsLiteral = 10579 isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE); 10580 10581 llvm::APFloat Value(0.0); 10582 bool IsConstant = 10583 E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects); 10584 if (!IsConstant) { 10585 return DiagnoseImpCast(S, E, T, CContext, 10586 diag::warn_impcast_float_integer, PruneWarnings); 10587 } 10588 10589 bool isExact = false; 10590 10591 llvm::APSInt IntegerValue(S.Context.getIntWidth(T), 10592 T->hasUnsignedIntegerRepresentation()); 10593 llvm::APFloat::opStatus Result = Value.convertToInteger( 10594 IntegerValue, llvm::APFloat::rmTowardZero, &isExact); 10595 10596 if (Result == llvm::APFloat::opOK && isExact) { 10597 if (IsLiteral) return; 10598 return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer, 10599 PruneWarnings); 10600 } 10601 10602 // Conversion of a floating-point value to a non-bool integer where the 10603 // integral part cannot be represented by the integer type is undefined. 10604 if (!IsBool && Result == llvm::APFloat::opInvalidOp) 10605 return DiagnoseImpCast( 10606 S, E, T, CContext, 10607 IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range 10608 : diag::warn_impcast_float_to_integer_out_of_range, 10609 PruneWarnings); 10610 10611 unsigned DiagID = 0; 10612 if (IsLiteral) { 10613 // Warn on floating point literal to integer. 10614 DiagID = diag::warn_impcast_literal_float_to_integer; 10615 } else if (IntegerValue == 0) { 10616 if (Value.isZero()) { // Skip -0.0 to 0 conversion. 10617 return DiagnoseImpCast(S, E, T, CContext, 10618 diag::warn_impcast_float_integer, PruneWarnings); 10619 } 10620 // Warn on non-zero to zero conversion. 10621 DiagID = diag::warn_impcast_float_to_integer_zero; 10622 } else { 10623 if (IntegerValue.isUnsigned()) { 10624 if (!IntegerValue.isMaxValue()) { 10625 return DiagnoseImpCast(S, E, T, CContext, 10626 diag::warn_impcast_float_integer, PruneWarnings); 10627 } 10628 } else { // IntegerValue.isSigned() 10629 if (!IntegerValue.isMaxSignedValue() && 10630 !IntegerValue.isMinSignedValue()) { 10631 return DiagnoseImpCast(S, E, T, CContext, 10632 diag::warn_impcast_float_integer, PruneWarnings); 10633 } 10634 } 10635 // Warn on evaluatable floating point expression to integer conversion. 10636 DiagID = diag::warn_impcast_float_to_integer; 10637 } 10638 10639 // FIXME: Force the precision of the source value down so we don't print 10640 // digits which are usually useless (we don't really care here if we 10641 // truncate a digit by accident in edge cases). Ideally, APFloat::toString 10642 // would automatically print the shortest representation, but it's a bit 10643 // tricky to implement. 10644 SmallString<16> PrettySourceValue; 10645 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); 10646 precision = (precision * 59 + 195) / 196; 10647 Value.toString(PrettySourceValue, precision); 10648 10649 SmallString<16> PrettyTargetValue; 10650 if (IsBool) 10651 PrettyTargetValue = Value.isZero() ? "false" : "true"; 10652 else 10653 IntegerValue.toString(PrettyTargetValue); 10654 10655 if (PruneWarnings) { 10656 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10657 S.PDiag(DiagID) 10658 << E->getType() << T.getUnqualifiedType() 10659 << PrettySourceValue << PrettyTargetValue 10660 << E->getSourceRange() << SourceRange(CContext)); 10661 } else { 10662 S.Diag(E->getExprLoc(), DiagID) 10663 << E->getType() << T.getUnqualifiedType() << PrettySourceValue 10664 << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext); 10665 } 10666 } 10667 10668 /// Analyze the given compound assignment for the possible losing of 10669 /// floating-point precision. 10670 static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) { 10671 assert(isa<CompoundAssignOperator>(E) && 10672 "Must be compound assignment operation"); 10673 // Recurse on the LHS and RHS in here 10674 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10675 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10676 10677 if (E->getLHS()->getType()->isAtomicType()) 10678 S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst); 10679 10680 // Now check the outermost expression 10681 const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>(); 10682 const auto *RBT = cast<CompoundAssignOperator>(E) 10683 ->getComputationResultType() 10684 ->getAs<BuiltinType>(); 10685 10686 // The below checks assume source is floating point. 10687 if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return; 10688 10689 // If source is floating point but target is an integer. 10690 if (ResultBT->isInteger()) 10691 return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(), 10692 E->getExprLoc(), diag::warn_impcast_float_integer); 10693 10694 if (!ResultBT->isFloatingPoint()) 10695 return; 10696 10697 // If both source and target are floating points, warn about losing precision. 10698 int Order = S.getASTContext().getFloatingTypeSemanticOrder( 10699 QualType(ResultBT, 0), QualType(RBT, 0)); 10700 if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc())) 10701 // warn about dropping FP rank. 10702 DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(), 10703 diag::warn_impcast_float_result_precision); 10704 } 10705 10706 static std::string PrettyPrintInRange(const llvm::APSInt &Value, 10707 IntRange Range) { 10708 if (!Range.Width) return "0"; 10709 10710 llvm::APSInt ValueInRange = Value; 10711 ValueInRange.setIsSigned(!Range.NonNegative); 10712 ValueInRange = ValueInRange.trunc(Range.Width); 10713 return ValueInRange.toString(10); 10714 } 10715 10716 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { 10717 if (!isa<ImplicitCastExpr>(Ex)) 10718 return false; 10719 10720 Expr *InnerE = Ex->IgnoreParenImpCasts(); 10721 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); 10722 const Type *Source = 10723 S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); 10724 if (Target->isDependentType()) 10725 return false; 10726 10727 const BuiltinType *FloatCandidateBT = 10728 dyn_cast<BuiltinType>(ToBool ? Source : Target); 10729 const Type *BoolCandidateType = ToBool ? Target : Source; 10730 10731 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && 10732 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); 10733 } 10734 10735 static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, 10736 SourceLocation CC) { 10737 unsigned NumArgs = TheCall->getNumArgs(); 10738 for (unsigned i = 0; i < NumArgs; ++i) { 10739 Expr *CurrA = TheCall->getArg(i); 10740 if (!IsImplicitBoolFloatConversion(S, CurrA, true)) 10741 continue; 10742 10743 bool IsSwapped = ((i > 0) && 10744 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); 10745 IsSwapped |= ((i < (NumArgs - 1)) && 10746 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); 10747 if (IsSwapped) { 10748 // Warn on this floating-point to bool conversion. 10749 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), 10750 CurrA->getType(), CC, 10751 diag::warn_impcast_floating_point_to_bool); 10752 } 10753 } 10754 } 10755 10756 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, 10757 SourceLocation CC) { 10758 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, 10759 E->getExprLoc())) 10760 return; 10761 10762 // Don't warn on functions which have return type nullptr_t. 10763 if (isa<CallExpr>(E)) 10764 return; 10765 10766 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). 10767 const Expr::NullPointerConstantKind NullKind = 10768 E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull); 10769 if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr) 10770 return; 10771 10772 // Return if target type is a safe conversion. 10773 if (T->isAnyPointerType() || T->isBlockPointerType() || 10774 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) 10775 return; 10776 10777 SourceLocation Loc = E->getSourceRange().getBegin(); 10778 10779 // Venture through the macro stacks to get to the source of macro arguments. 10780 // The new location is a better location than the complete location that was 10781 // passed in. 10782 Loc = S.SourceMgr.getTopMacroCallerLoc(Loc); 10783 CC = S.SourceMgr.getTopMacroCallerLoc(CC); 10784 10785 // __null is usually wrapped in a macro. Go up a macro if that is the case. 10786 if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) { 10787 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( 10788 Loc, S.SourceMgr, S.getLangOpts()); 10789 if (MacroName == "NULL") 10790 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin(); 10791 } 10792 10793 // Only warn if the null and context location are in the same macro expansion. 10794 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) 10795 return; 10796 10797 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) 10798 << (NullKind == Expr::NPCK_CXX11_nullptr) << T << SourceRange(CC) 10799 << FixItHint::CreateReplacement(Loc, 10800 S.getFixItZeroLiteralForType(T, Loc)); 10801 } 10802 10803 static void checkObjCArrayLiteral(Sema &S, QualType TargetType, 10804 ObjCArrayLiteral *ArrayLiteral); 10805 10806 static void 10807 checkObjCDictionaryLiteral(Sema &S, QualType TargetType, 10808 ObjCDictionaryLiteral *DictionaryLiteral); 10809 10810 /// Check a single element within a collection literal against the 10811 /// target element type. 10812 static void checkObjCCollectionLiteralElement(Sema &S, 10813 QualType TargetElementType, 10814 Expr *Element, 10815 unsigned ElementKind) { 10816 // Skip a bitcast to 'id' or qualified 'id'. 10817 if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) { 10818 if (ICE->getCastKind() == CK_BitCast && 10819 ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>()) 10820 Element = ICE->getSubExpr(); 10821 } 10822 10823 QualType ElementType = Element->getType(); 10824 ExprResult ElementResult(Element); 10825 if (ElementType->getAs<ObjCObjectPointerType>() && 10826 S.CheckSingleAssignmentConstraints(TargetElementType, 10827 ElementResult, 10828 false, false) 10829 != Sema::Compatible) { 10830 S.Diag(Element->getBeginLoc(), diag::warn_objc_collection_literal_element) 10831 << ElementType << ElementKind << TargetElementType 10832 << Element->getSourceRange(); 10833 } 10834 10835 if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element)) 10836 checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral); 10837 else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element)) 10838 checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral); 10839 } 10840 10841 /// Check an Objective-C array literal being converted to the given 10842 /// target type. 10843 static void checkObjCArrayLiteral(Sema &S, QualType TargetType, 10844 ObjCArrayLiteral *ArrayLiteral) { 10845 if (!S.NSArrayDecl) 10846 return; 10847 10848 const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>(); 10849 if (!TargetObjCPtr) 10850 return; 10851 10852 if (TargetObjCPtr->isUnspecialized() || 10853 TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl() 10854 != S.NSArrayDecl->getCanonicalDecl()) 10855 return; 10856 10857 auto TypeArgs = TargetObjCPtr->getTypeArgs(); 10858 if (TypeArgs.size() != 1) 10859 return; 10860 10861 QualType TargetElementType = TypeArgs[0]; 10862 for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) { 10863 checkObjCCollectionLiteralElement(S, TargetElementType, 10864 ArrayLiteral->getElement(I), 10865 0); 10866 } 10867 } 10868 10869 /// Check an Objective-C dictionary literal being converted to the given 10870 /// target type. 10871 static void 10872 checkObjCDictionaryLiteral(Sema &S, QualType TargetType, 10873 ObjCDictionaryLiteral *DictionaryLiteral) { 10874 if (!S.NSDictionaryDecl) 10875 return; 10876 10877 const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>(); 10878 if (!TargetObjCPtr) 10879 return; 10880 10881 if (TargetObjCPtr->isUnspecialized() || 10882 TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl() 10883 != S.NSDictionaryDecl->getCanonicalDecl()) 10884 return; 10885 10886 auto TypeArgs = TargetObjCPtr->getTypeArgs(); 10887 if (TypeArgs.size() != 2) 10888 return; 10889 10890 QualType TargetKeyType = TypeArgs[0]; 10891 QualType TargetObjectType = TypeArgs[1]; 10892 for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) { 10893 auto Element = DictionaryLiteral->getKeyValueElement(I); 10894 checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1); 10895 checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2); 10896 } 10897 } 10898 10899 // Helper function to filter out cases for constant width constant conversion. 10900 // Don't warn on char array initialization or for non-decimal values. 10901 static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, 10902 SourceLocation CC) { 10903 // If initializing from a constant, and the constant starts with '0', 10904 // then it is a binary, octal, or hexadecimal. Allow these constants 10905 // to fill all the bits, even if there is a sign change. 10906 if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) { 10907 const char FirstLiteralCharacter = 10908 S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0]; 10909 if (FirstLiteralCharacter == '0') 10910 return false; 10911 } 10912 10913 // If the CC location points to a '{', and the type is char, then assume 10914 // assume it is an array initialization. 10915 if (CC.isValid() && T->isCharType()) { 10916 const char FirstContextCharacter = 10917 S.getSourceManager().getCharacterData(CC)[0]; 10918 if (FirstContextCharacter == '{') 10919 return false; 10920 } 10921 10922 return true; 10923 } 10924 10925 static void 10926 CheckImplicitConversion(Sema &S, Expr *E, QualType T, SourceLocation CC, 10927 bool *ICContext = nullptr) { 10928 if (E->isTypeDependent() || E->isValueDependent()) return; 10929 10930 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); 10931 const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); 10932 if (Source == Target) return; 10933 if (Target->isDependentType()) return; 10934 10935 // If the conversion context location is invalid don't complain. We also 10936 // don't want to emit a warning if the issue occurs from the expansion of 10937 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we 10938 // delay this check as long as possible. Once we detect we are in that 10939 // scenario, we just return. 10940 if (CC.isInvalid()) 10941 return; 10942 10943 if (Source->isAtomicType()) 10944 S.Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst); 10945 10946 // Diagnose implicit casts to bool. 10947 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { 10948 if (isa<StringLiteral>(E)) 10949 // Warn on string literal to bool. Checks for string literals in logical 10950 // and expressions, for instance, assert(0 && "error here"), are 10951 // prevented by a check in AnalyzeImplicitConversions(). 10952 return DiagnoseImpCast(S, E, T, CC, 10953 diag::warn_impcast_string_literal_to_bool); 10954 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || 10955 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { 10956 // This covers the literal expressions that evaluate to Objective-C 10957 // objects. 10958 return DiagnoseImpCast(S, E, T, CC, 10959 diag::warn_impcast_objective_c_literal_to_bool); 10960 } 10961 if (Source->isPointerType() || Source->canDecayToPointerType()) { 10962 // Warn on pointer to bool conversion that is always true. 10963 S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, 10964 SourceRange(CC)); 10965 } 10966 } 10967 10968 // Check implicit casts from Objective-C collection literals to specialized 10969 // collection types, e.g., NSArray<NSString *> *. 10970 if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E)) 10971 checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral); 10972 else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) 10973 checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral); 10974 10975 // Strip vector types. 10976 if (isa<VectorType>(Source)) { 10977 if (!isa<VectorType>(Target)) { 10978 if (S.SourceMgr.isInSystemMacro(CC)) 10979 return; 10980 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); 10981 } 10982 10983 // If the vector cast is cast between two vectors of the same size, it is 10984 // a bitcast, not a conversion. 10985 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) 10986 return; 10987 10988 Source = cast<VectorType>(Source)->getElementType().getTypePtr(); 10989 Target = cast<VectorType>(Target)->getElementType().getTypePtr(); 10990 } 10991 if (auto VecTy = dyn_cast<VectorType>(Target)) 10992 Target = VecTy->getElementType().getTypePtr(); 10993 10994 // Strip complex types. 10995 if (isa<ComplexType>(Source)) { 10996 if (!isa<ComplexType>(Target)) { 10997 if (S.SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) 10998 return; 10999 11000 return DiagnoseImpCast(S, E, T, CC, 11001 S.getLangOpts().CPlusPlus 11002 ? diag::err_impcast_complex_scalar 11003 : diag::warn_impcast_complex_scalar); 11004 } 11005 11006 Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); 11007 Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); 11008 } 11009 11010 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); 11011 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); 11012 11013 // If the source is floating point... 11014 if (SourceBT && SourceBT->isFloatingPoint()) { 11015 // ...and the target is floating point... 11016 if (TargetBT && TargetBT->isFloatingPoint()) { 11017 // ...then warn if we're dropping FP rank. 11018 11019 int Order = S.getASTContext().getFloatingTypeSemanticOrder( 11020 QualType(SourceBT, 0), QualType(TargetBT, 0)); 11021 if (Order > 0) { 11022 // Don't warn about float constants that are precisely 11023 // representable in the target type. 11024 Expr::EvalResult result; 11025 if (E->EvaluateAsRValue(result, S.Context)) { 11026 // Value might be a float, a float vector, or a float complex. 11027 if (IsSameFloatAfterCast(result.Val, 11028 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), 11029 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) 11030 return; 11031 } 11032 11033 if (S.SourceMgr.isInSystemMacro(CC)) 11034 return; 11035 11036 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); 11037 } 11038 // ... or possibly if we're increasing rank, too 11039 else if (Order < 0) { 11040 if (S.SourceMgr.isInSystemMacro(CC)) 11041 return; 11042 11043 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_double_promotion); 11044 } 11045 return; 11046 } 11047 11048 // If the target is integral, always warn. 11049 if (TargetBT && TargetBT->isInteger()) { 11050 if (S.SourceMgr.isInSystemMacro(CC)) 11051 return; 11052 11053 DiagnoseFloatingImpCast(S, E, T, CC); 11054 } 11055 11056 // Detect the case where a call result is converted from floating-point to 11057 // to bool, and the final argument to the call is converted from bool, to 11058 // discover this typo: 11059 // 11060 // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;" 11061 // 11062 // FIXME: This is an incredibly special case; is there some more general 11063 // way to detect this class of misplaced-parentheses bug? 11064 if (Target->isBooleanType() && isa<CallExpr>(E)) { 11065 // Check last argument of function call to see if it is an 11066 // implicit cast from a type matching the type the result 11067 // is being cast to. 11068 CallExpr *CEx = cast<CallExpr>(E); 11069 if (unsigned NumArgs = CEx->getNumArgs()) { 11070 Expr *LastA = CEx->getArg(NumArgs - 1); 11071 Expr *InnerE = LastA->IgnoreParenImpCasts(); 11072 if (isa<ImplicitCastExpr>(LastA) && 11073 InnerE->getType()->isBooleanType()) { 11074 // Warn on this floating-point to bool conversion 11075 DiagnoseImpCast(S, E, T, CC, 11076 diag::warn_impcast_floating_point_to_bool); 11077 } 11078 } 11079 } 11080 return; 11081 } 11082 11083 // Valid casts involving fixed point types should be accounted for here. 11084 if (Source->isFixedPointType()) { 11085 if (Target->isUnsaturatedFixedPointType()) { 11086 Expr::EvalResult Result; 11087 if (E->EvaluateAsFixedPoint(Result, S.Context, 11088 Expr::SE_AllowSideEffects)) { 11089 APFixedPoint Value = Result.Val.getFixedPoint(); 11090 APFixedPoint MaxVal = S.Context.getFixedPointMax(T); 11091 APFixedPoint MinVal = S.Context.getFixedPointMin(T); 11092 if (Value > MaxVal || Value < MinVal) { 11093 S.DiagRuntimeBehavior(E->getExprLoc(), E, 11094 S.PDiag(diag::warn_impcast_fixed_point_range) 11095 << Value.toString() << T 11096 << E->getSourceRange() 11097 << clang::SourceRange(CC)); 11098 return; 11099 } 11100 } 11101 } else if (Target->isIntegerType()) { 11102 Expr::EvalResult Result; 11103 if (E->EvaluateAsFixedPoint(Result, S.Context, 11104 Expr::SE_AllowSideEffects)) { 11105 APFixedPoint FXResult = Result.Val.getFixedPoint(); 11106 11107 bool Overflowed; 11108 llvm::APSInt IntResult = FXResult.convertToInt( 11109 S.Context.getIntWidth(T), 11110 Target->isSignedIntegerOrEnumerationType(), &Overflowed); 11111 11112 if (Overflowed) { 11113 S.DiagRuntimeBehavior(E->getExprLoc(), E, 11114 S.PDiag(diag::warn_impcast_fixed_point_range) 11115 << FXResult.toString() << T 11116 << E->getSourceRange() 11117 << clang::SourceRange(CC)); 11118 return; 11119 } 11120 } 11121 } 11122 } else if (Target->isUnsaturatedFixedPointType()) { 11123 if (Source->isIntegerType()) { 11124 Expr::EvalResult Result; 11125 if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) { 11126 llvm::APSInt Value = Result.Val.getInt(); 11127 11128 bool Overflowed; 11129 APFixedPoint IntResult = APFixedPoint::getFromIntValue( 11130 Value, S.Context.getFixedPointSemantics(T), &Overflowed); 11131 11132 if (Overflowed) { 11133 S.DiagRuntimeBehavior(E->getExprLoc(), E, 11134 S.PDiag(diag::warn_impcast_fixed_point_range) 11135 << Value.toString(/*radix=*/10) << T 11136 << E->getSourceRange() 11137 << clang::SourceRange(CC)); 11138 return; 11139 } 11140 } 11141 } 11142 } 11143 11144 DiagnoseNullConversion(S, E, T, CC); 11145 11146 S.DiscardMisalignedMemberAddress(Target, E); 11147 11148 if (!Source->isIntegerType() || !Target->isIntegerType()) 11149 return; 11150 11151 // TODO: remove this early return once the false positives for constant->bool 11152 // in templates, macros, etc, are reduced or removed. 11153 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) 11154 return; 11155 11156 IntRange SourceRange = GetExprRange(S.Context, E); 11157 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); 11158 11159 if (SourceRange.Width > TargetRange.Width) { 11160 // If the source is a constant, use a default-on diagnostic. 11161 // TODO: this should happen for bitfield stores, too. 11162 Expr::EvalResult Result; 11163 if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) { 11164 llvm::APSInt Value(32); 11165 Value = Result.Val.getInt(); 11166 11167 if (S.SourceMgr.isInSystemMacro(CC)) 11168 return; 11169 11170 std::string PrettySourceValue = Value.toString(10); 11171 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11172 11173 S.DiagRuntimeBehavior(E->getExprLoc(), E, 11174 S.PDiag(diag::warn_impcast_integer_precision_constant) 11175 << PrettySourceValue << PrettyTargetValue 11176 << E->getType() << T << E->getSourceRange() 11177 << clang::SourceRange(CC)); 11178 return; 11179 } 11180 11181 // People want to build with -Wshorten-64-to-32 and not -Wconversion. 11182 if (S.SourceMgr.isInSystemMacro(CC)) 11183 return; 11184 11185 if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) 11186 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, 11187 /* pruneControlFlow */ true); 11188 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); 11189 } 11190 11191 if (TargetRange.Width > SourceRange.Width) { 11192 if (auto *UO = dyn_cast<UnaryOperator>(E)) 11193 if (UO->getOpcode() == UO_Minus) 11194 if (Source->isUnsignedIntegerType()) { 11195 if (Target->isUnsignedIntegerType()) 11196 return DiagnoseImpCast(S, E, T, CC, 11197 diag::warn_impcast_high_order_zero_bits); 11198 if (Target->isSignedIntegerType()) 11199 return DiagnoseImpCast(S, E, T, CC, 11200 diag::warn_impcast_nonnegative_result); 11201 } 11202 } 11203 11204 if (TargetRange.Width == SourceRange.Width && !TargetRange.NonNegative && 11205 SourceRange.NonNegative && Source->isSignedIntegerType()) { 11206 // Warn when doing a signed to signed conversion, warn if the positive 11207 // source value is exactly the width of the target type, which will 11208 // cause a negative value to be stored. 11209 11210 Expr::EvalResult Result; 11211 if (E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects) && 11212 !S.SourceMgr.isInSystemMacro(CC)) { 11213 llvm::APSInt Value = Result.Val.getInt(); 11214 if (isSameWidthConstantConversion(S, E, T, CC)) { 11215 std::string PrettySourceValue = Value.toString(10); 11216 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11217 11218 S.DiagRuntimeBehavior( 11219 E->getExprLoc(), E, 11220 S.PDiag(diag::warn_impcast_integer_precision_constant) 11221 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11222 << E->getSourceRange() << clang::SourceRange(CC)); 11223 return; 11224 } 11225 } 11226 11227 // Fall through for non-constants to give a sign conversion warning. 11228 } 11229 11230 if ((TargetRange.NonNegative && !SourceRange.NonNegative) || 11231 (!TargetRange.NonNegative && SourceRange.NonNegative && 11232 SourceRange.Width == TargetRange.Width)) { 11233 if (S.SourceMgr.isInSystemMacro(CC)) 11234 return; 11235 11236 unsigned DiagID = diag::warn_impcast_integer_sign; 11237 11238 // Traditionally, gcc has warned about this under -Wsign-compare. 11239 // We also want to warn about it in -Wconversion. 11240 // So if -Wconversion is off, use a completely identical diagnostic 11241 // in the sign-compare group. 11242 // The conditional-checking code will 11243 if (ICContext) { 11244 DiagID = diag::warn_impcast_integer_sign_conditional; 11245 *ICContext = true; 11246 } 11247 11248 return DiagnoseImpCast(S, E, T, CC, DiagID); 11249 } 11250 11251 // Diagnose conversions between different enumeration types. 11252 // In C, we pretend that the type of an EnumConstantDecl is its enumeration 11253 // type, to give us better diagnostics. 11254 QualType SourceType = E->getType(); 11255 if (!S.getLangOpts().CPlusPlus) { 11256 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 11257 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { 11258 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); 11259 SourceType = S.Context.getTypeDeclType(Enum); 11260 Source = S.Context.getCanonicalType(SourceType).getTypePtr(); 11261 } 11262 } 11263 11264 if (const EnumType *SourceEnum = Source->getAs<EnumType>()) 11265 if (const EnumType *TargetEnum = Target->getAs<EnumType>()) 11266 if (SourceEnum->getDecl()->hasNameForLinkage() && 11267 TargetEnum->getDecl()->hasNameForLinkage() && 11268 SourceEnum != TargetEnum) { 11269 if (S.SourceMgr.isInSystemMacro(CC)) 11270 return; 11271 11272 return DiagnoseImpCast(S, E, SourceType, T, CC, 11273 diag::warn_impcast_different_enum_types); 11274 } 11275 } 11276 11277 static void CheckConditionalOperator(Sema &S, ConditionalOperator *E, 11278 SourceLocation CC, QualType T); 11279 11280 static void CheckConditionalOperand(Sema &S, Expr *E, QualType T, 11281 SourceLocation CC, bool &ICContext) { 11282 E = E->IgnoreParenImpCasts(); 11283 11284 if (isa<ConditionalOperator>(E)) 11285 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); 11286 11287 AnalyzeImplicitConversions(S, E, CC); 11288 if (E->getType() != T) 11289 return CheckImplicitConversion(S, E, T, CC, &ICContext); 11290 } 11291 11292 static void CheckConditionalOperator(Sema &S, ConditionalOperator *E, 11293 SourceLocation CC, QualType T) { 11294 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); 11295 11296 bool Suspicious = false; 11297 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); 11298 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); 11299 11300 // If -Wconversion would have warned about either of the candidates 11301 // for a signedness conversion to the context type... 11302 if (!Suspicious) return; 11303 11304 // ...but it's currently ignored... 11305 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) 11306 return; 11307 11308 // ...then check whether it would have warned about either of the 11309 // candidates for a signedness conversion to the condition type. 11310 if (E->getType() == T) return; 11311 11312 Suspicious = false; 11313 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), 11314 E->getType(), CC, &Suspicious); 11315 if (!Suspicious) 11316 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), 11317 E->getType(), CC, &Suspicious); 11318 } 11319 11320 /// Check conversion of given expression to boolean. 11321 /// Input argument E is a logical expression. 11322 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { 11323 if (S.getLangOpts().Bool) 11324 return; 11325 if (E->IgnoreParenImpCasts()->getType()->isAtomicType()) 11326 return; 11327 CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); 11328 } 11329 11330 /// AnalyzeImplicitConversions - Find and report any interesting 11331 /// implicit conversions in the given expression. There are a couple 11332 /// of competing diagnostics here, -Wconversion and -Wsign-compare. 11333 static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, 11334 SourceLocation CC) { 11335 QualType T = OrigE->getType(); 11336 Expr *E = OrigE->IgnoreParenImpCasts(); 11337 11338 if (E->isTypeDependent() || E->isValueDependent()) 11339 return; 11340 11341 // For conditional operators, we analyze the arguments as if they 11342 // were being fed directly into the output. 11343 if (isa<ConditionalOperator>(E)) { 11344 ConditionalOperator *CO = cast<ConditionalOperator>(E); 11345 CheckConditionalOperator(S, CO, CC, T); 11346 return; 11347 } 11348 11349 // Check implicit argument conversions for function calls. 11350 if (CallExpr *Call = dyn_cast<CallExpr>(E)) 11351 CheckImplicitArgumentConversions(S, Call, CC); 11352 11353 // Go ahead and check any implicit conversions we might have skipped. 11354 // The non-canonical typecheck is just an optimization; 11355 // CheckImplicitConversion will filter out dead implicit conversions. 11356 if (E->getType() != T) 11357 CheckImplicitConversion(S, E, T, CC); 11358 11359 // Now continue drilling into this expression. 11360 11361 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 11362 // The bound subexpressions in a PseudoObjectExpr are not reachable 11363 // as transitive children. 11364 // FIXME: Use a more uniform representation for this. 11365 for (auto *SE : POE->semantics()) 11366 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE)) 11367 AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC); 11368 } 11369 11370 // Skip past explicit casts. 11371 if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) { 11372 E = CE->getSubExpr()->IgnoreParenImpCasts(); 11373 if (!CE->getType()->isVoidType() && E->getType()->isAtomicType()) 11374 S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 11375 return AnalyzeImplicitConversions(S, E, CC); 11376 } 11377 11378 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 11379 // Do a somewhat different check with comparison operators. 11380 if (BO->isComparisonOp()) 11381 return AnalyzeComparison(S, BO); 11382 11383 // And with simple assignments. 11384 if (BO->getOpcode() == BO_Assign) 11385 return AnalyzeAssignment(S, BO); 11386 // And with compound assignments. 11387 if (BO->isAssignmentOp()) 11388 return AnalyzeCompoundAssignment(S, BO); 11389 } 11390 11391 // These break the otherwise-useful invariant below. Fortunately, 11392 // we don't really need to recurse into them, because any internal 11393 // expressions should have been analyzed already when they were 11394 // built into statements. 11395 if (isa<StmtExpr>(E)) return; 11396 11397 // Don't descend into unevaluated contexts. 11398 if (isa<UnaryExprOrTypeTraitExpr>(E)) return; 11399 11400 // Now just recurse over the expression's children. 11401 CC = E->getExprLoc(); 11402 BinaryOperator *BO = dyn_cast<BinaryOperator>(E); 11403 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; 11404 for (Stmt *SubStmt : E->children()) { 11405 Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt); 11406 if (!ChildExpr) 11407 continue; 11408 11409 if (IsLogicalAndOperator && 11410 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) 11411 // Ignore checking string literals that are in logical and operators. 11412 // This is a common pattern for asserts. 11413 continue; 11414 AnalyzeImplicitConversions(S, ChildExpr, CC); 11415 } 11416 11417 if (BO && BO->isLogicalOp()) { 11418 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); 11419 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11420 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11421 11422 SubExpr = BO->getRHS()->IgnoreParenImpCasts(); 11423 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11424 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11425 } 11426 11427 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) { 11428 if (U->getOpcode() == UO_LNot) { 11429 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); 11430 } else if (U->getOpcode() != UO_AddrOf) { 11431 if (U->getSubExpr()->getType()->isAtomicType()) 11432 S.Diag(U->getSubExpr()->getBeginLoc(), 11433 diag::warn_atomic_implicit_seq_cst); 11434 } 11435 } 11436 } 11437 11438 /// Diagnose integer type and any valid implicit conversion to it. 11439 static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E, const QualType &IntT) { 11440 // Taking into account implicit conversions, 11441 // allow any integer. 11442 if (!E->getType()->isIntegerType()) { 11443 S.Diag(E->getBeginLoc(), 11444 diag::err_opencl_enqueue_kernel_invalid_local_size_type); 11445 return true; 11446 } 11447 // Potentially emit standard warnings for implicit conversions if enabled 11448 // using -Wconversion. 11449 CheckImplicitConversion(S, E, IntT, E->getBeginLoc()); 11450 return false; 11451 } 11452 11453 // Helper function for Sema::DiagnoseAlwaysNonNullPointer. 11454 // Returns true when emitting a warning about taking the address of a reference. 11455 static bool CheckForReference(Sema &SemaRef, const Expr *E, 11456 const PartialDiagnostic &PD) { 11457 E = E->IgnoreParenImpCasts(); 11458 11459 const FunctionDecl *FD = nullptr; 11460 11461 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 11462 if (!DRE->getDecl()->getType()->isReferenceType()) 11463 return false; 11464 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11465 if (!M->getMemberDecl()->getType()->isReferenceType()) 11466 return false; 11467 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { 11468 if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) 11469 return false; 11470 FD = Call->getDirectCallee(); 11471 } else { 11472 return false; 11473 } 11474 11475 SemaRef.Diag(E->getExprLoc(), PD); 11476 11477 // If possible, point to location of function. 11478 if (FD) { 11479 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; 11480 } 11481 11482 return true; 11483 } 11484 11485 // Returns true if the SourceLocation is expanded from any macro body. 11486 // Returns false if the SourceLocation is invalid, is from not in a macro 11487 // expansion, or is from expanded from a top-level macro argument. 11488 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { 11489 if (Loc.isInvalid()) 11490 return false; 11491 11492 while (Loc.isMacroID()) { 11493 if (SM.isMacroBodyExpansion(Loc)) 11494 return true; 11495 Loc = SM.getImmediateMacroCallerLoc(Loc); 11496 } 11497 11498 return false; 11499 } 11500 11501 /// Diagnose pointers that are always non-null. 11502 /// \param E the expression containing the pointer 11503 /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is 11504 /// compared to a null pointer 11505 /// \param IsEqual True when the comparison is equal to a null pointer 11506 /// \param Range Extra SourceRange to highlight in the diagnostic 11507 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, 11508 Expr::NullPointerConstantKind NullKind, 11509 bool IsEqual, SourceRange Range) { 11510 if (!E) 11511 return; 11512 11513 // Don't warn inside macros. 11514 if (E->getExprLoc().isMacroID()) { 11515 const SourceManager &SM = getSourceManager(); 11516 if (IsInAnyMacroBody(SM, E->getExprLoc()) || 11517 IsInAnyMacroBody(SM, Range.getBegin())) 11518 return; 11519 } 11520 E = E->IgnoreImpCasts(); 11521 11522 const bool IsCompare = NullKind != Expr::NPCK_NotNull; 11523 11524 if (isa<CXXThisExpr>(E)) { 11525 unsigned DiagID = IsCompare ? diag::warn_this_null_compare 11526 : diag::warn_this_bool_conversion; 11527 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; 11528 return; 11529 } 11530 11531 bool IsAddressOf = false; 11532 11533 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 11534 if (UO->getOpcode() != UO_AddrOf) 11535 return; 11536 IsAddressOf = true; 11537 E = UO->getSubExpr(); 11538 } 11539 11540 if (IsAddressOf) { 11541 unsigned DiagID = IsCompare 11542 ? diag::warn_address_of_reference_null_compare 11543 : diag::warn_address_of_reference_bool_conversion; 11544 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range 11545 << IsEqual; 11546 if (CheckForReference(*this, E, PD)) { 11547 return; 11548 } 11549 } 11550 11551 auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) { 11552 bool IsParam = isa<NonNullAttr>(NonnullAttr); 11553 std::string Str; 11554 llvm::raw_string_ostream S(Str); 11555 E->printPretty(S, nullptr, getPrintingPolicy()); 11556 unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare 11557 : diag::warn_cast_nonnull_to_bool; 11558 Diag(E->getExprLoc(), DiagID) << IsParam << S.str() 11559 << E->getSourceRange() << Range << IsEqual; 11560 Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam; 11561 }; 11562 11563 // If we have a CallExpr that is tagged with returns_nonnull, we can complain. 11564 if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) { 11565 if (auto *Callee = Call->getDirectCallee()) { 11566 if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) { 11567 ComplainAboutNonnullParamOrCall(A); 11568 return; 11569 } 11570 } 11571 } 11572 11573 // Expect to find a single Decl. Skip anything more complicated. 11574 ValueDecl *D = nullptr; 11575 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { 11576 D = R->getDecl(); 11577 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11578 D = M->getMemberDecl(); 11579 } 11580 11581 // Weak Decls can be null. 11582 if (!D || D->isWeak()) 11583 return; 11584 11585 // Check for parameter decl with nonnull attribute 11586 if (const auto* PV = dyn_cast<ParmVarDecl>(D)) { 11587 if (getCurFunction() && 11588 !getCurFunction()->ModifiedNonNullParams.count(PV)) { 11589 if (const Attr *A = PV->getAttr<NonNullAttr>()) { 11590 ComplainAboutNonnullParamOrCall(A); 11591 return; 11592 } 11593 11594 if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { 11595 auto ParamIter = llvm::find(FD->parameters(), PV); 11596 assert(ParamIter != FD->param_end()); 11597 unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); 11598 11599 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { 11600 if (!NonNull->args_size()) { 11601 ComplainAboutNonnullParamOrCall(NonNull); 11602 return; 11603 } 11604 11605 for (const ParamIdx &ArgNo : NonNull->args()) { 11606 if (ArgNo.getASTIndex() == ParamNo) { 11607 ComplainAboutNonnullParamOrCall(NonNull); 11608 return; 11609 } 11610 } 11611 } 11612 } 11613 } 11614 } 11615 11616 QualType T = D->getType(); 11617 const bool IsArray = T->isArrayType(); 11618 const bool IsFunction = T->isFunctionType(); 11619 11620 // Address of function is used to silence the function warning. 11621 if (IsAddressOf && IsFunction) { 11622 return; 11623 } 11624 11625 // Found nothing. 11626 if (!IsAddressOf && !IsFunction && !IsArray) 11627 return; 11628 11629 // Pretty print the expression for the diagnostic. 11630 std::string Str; 11631 llvm::raw_string_ostream S(Str); 11632 E->printPretty(S, nullptr, getPrintingPolicy()); 11633 11634 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare 11635 : diag::warn_impcast_pointer_to_bool; 11636 enum { 11637 AddressOf, 11638 FunctionPointer, 11639 ArrayPointer 11640 } DiagType; 11641 if (IsAddressOf) 11642 DiagType = AddressOf; 11643 else if (IsFunction) 11644 DiagType = FunctionPointer; 11645 else if (IsArray) 11646 DiagType = ArrayPointer; 11647 else 11648 llvm_unreachable("Could not determine diagnostic."); 11649 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() 11650 << Range << IsEqual; 11651 11652 if (!IsFunction) 11653 return; 11654 11655 // Suggest '&' to silence the function warning. 11656 Diag(E->getExprLoc(), diag::note_function_warning_silence) 11657 << FixItHint::CreateInsertion(E->getBeginLoc(), "&"); 11658 11659 // Check to see if '()' fixit should be emitted. 11660 QualType ReturnType; 11661 UnresolvedSet<4> NonTemplateOverloads; 11662 tryExprAsCall(*E, ReturnType, NonTemplateOverloads); 11663 if (ReturnType.isNull()) 11664 return; 11665 11666 if (IsCompare) { 11667 // There are two cases here. If there is null constant, the only suggest 11668 // for a pointer return type. If the null is 0, then suggest if the return 11669 // type is a pointer or an integer type. 11670 if (!ReturnType->isPointerType()) { 11671 if (NullKind == Expr::NPCK_ZeroExpression || 11672 NullKind == Expr::NPCK_ZeroLiteral) { 11673 if (!ReturnType->isIntegerType()) 11674 return; 11675 } else { 11676 return; 11677 } 11678 } 11679 } else { // !IsCompare 11680 // For function to bool, only suggest if the function pointer has bool 11681 // return type. 11682 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) 11683 return; 11684 } 11685 Diag(E->getExprLoc(), diag::note_function_to_function_call) 11686 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()"); 11687 } 11688 11689 /// Diagnoses "dangerous" implicit conversions within the given 11690 /// expression (which is a full expression). Implements -Wconversion 11691 /// and -Wsign-compare. 11692 /// 11693 /// \param CC the "context" location of the implicit conversion, i.e. 11694 /// the most location of the syntactic entity requiring the implicit 11695 /// conversion 11696 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { 11697 // Don't diagnose in unevaluated contexts. 11698 if (isUnevaluatedContext()) 11699 return; 11700 11701 // Don't diagnose for value- or type-dependent expressions. 11702 if (E->isTypeDependent() || E->isValueDependent()) 11703 return; 11704 11705 // Check for array bounds violations in cases where the check isn't triggered 11706 // elsewhere for other Expr types (like BinaryOperators), e.g. when an 11707 // ArraySubscriptExpr is on the RHS of a variable initialization. 11708 CheckArrayAccess(E); 11709 11710 // This is not the right CC for (e.g.) a variable initialization. 11711 AnalyzeImplicitConversions(*this, E, CC); 11712 } 11713 11714 /// CheckBoolLikeConversion - Check conversion of given expression to boolean. 11715 /// Input argument E is a logical expression. 11716 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { 11717 ::CheckBoolLikeConversion(*this, E, CC); 11718 } 11719 11720 /// Diagnose when expression is an integer constant expression and its evaluation 11721 /// results in integer overflow 11722 void Sema::CheckForIntOverflow (Expr *E) { 11723 // Use a work list to deal with nested struct initializers. 11724 SmallVector<Expr *, 2> Exprs(1, E); 11725 11726 do { 11727 Expr *OriginalE = Exprs.pop_back_val(); 11728 Expr *E = OriginalE->IgnoreParenCasts(); 11729 11730 if (isa<BinaryOperator>(E)) { 11731 E->EvaluateForOverflow(Context); 11732 continue; 11733 } 11734 11735 if (auto InitList = dyn_cast<InitListExpr>(OriginalE)) 11736 Exprs.append(InitList->inits().begin(), InitList->inits().end()); 11737 else if (isa<ObjCBoxedExpr>(OriginalE)) 11738 E->EvaluateForOverflow(Context); 11739 else if (auto Call = dyn_cast<CallExpr>(E)) 11740 Exprs.append(Call->arg_begin(), Call->arg_end()); 11741 else if (auto Message = dyn_cast<ObjCMessageExpr>(E)) 11742 Exprs.append(Message->arg_begin(), Message->arg_end()); 11743 } while (!Exprs.empty()); 11744 } 11745 11746 namespace { 11747 11748 /// Visitor for expressions which looks for unsequenced operations on the 11749 /// same object. 11750 class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> { 11751 using Base = EvaluatedExprVisitor<SequenceChecker>; 11752 11753 /// A tree of sequenced regions within an expression. Two regions are 11754 /// unsequenced if one is an ancestor or a descendent of the other. When we 11755 /// finish processing an expression with sequencing, such as a comma 11756 /// expression, we fold its tree nodes into its parent, since they are 11757 /// unsequenced with respect to nodes we will visit later. 11758 class SequenceTree { 11759 struct Value { 11760 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} 11761 unsigned Parent : 31; 11762 unsigned Merged : 1; 11763 }; 11764 SmallVector<Value, 8> Values; 11765 11766 public: 11767 /// A region within an expression which may be sequenced with respect 11768 /// to some other region. 11769 class Seq { 11770 friend class SequenceTree; 11771 11772 unsigned Index; 11773 11774 explicit Seq(unsigned N) : Index(N) {} 11775 11776 public: 11777 Seq() : Index(0) {} 11778 }; 11779 11780 SequenceTree() { Values.push_back(Value(0)); } 11781 Seq root() const { return Seq(0); } 11782 11783 /// Create a new sequence of operations, which is an unsequenced 11784 /// subset of \p Parent. This sequence of operations is sequenced with 11785 /// respect to other children of \p Parent. 11786 Seq allocate(Seq Parent) { 11787 Values.push_back(Value(Parent.Index)); 11788 return Seq(Values.size() - 1); 11789 } 11790 11791 /// Merge a sequence of operations into its parent. 11792 void merge(Seq S) { 11793 Values[S.Index].Merged = true; 11794 } 11795 11796 /// Determine whether two operations are unsequenced. This operation 11797 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old 11798 /// should have been merged into its parent as appropriate. 11799 bool isUnsequenced(Seq Cur, Seq Old) { 11800 unsigned C = representative(Cur.Index); 11801 unsigned Target = representative(Old.Index); 11802 while (C >= Target) { 11803 if (C == Target) 11804 return true; 11805 C = Values[C].Parent; 11806 } 11807 return false; 11808 } 11809 11810 private: 11811 /// Pick a representative for a sequence. 11812 unsigned representative(unsigned K) { 11813 if (Values[K].Merged) 11814 // Perform path compression as we go. 11815 return Values[K].Parent = representative(Values[K].Parent); 11816 return K; 11817 } 11818 }; 11819 11820 /// An object for which we can track unsequenced uses. 11821 using Object = NamedDecl *; 11822 11823 /// Different flavors of object usage which we track. We only track the 11824 /// least-sequenced usage of each kind. 11825 enum UsageKind { 11826 /// A read of an object. Multiple unsequenced reads are OK. 11827 UK_Use, 11828 11829 /// A modification of an object which is sequenced before the value 11830 /// computation of the expression, such as ++n in C++. 11831 UK_ModAsValue, 11832 11833 /// A modification of an object which is not sequenced before the value 11834 /// computation of the expression, such as n++. 11835 UK_ModAsSideEffect, 11836 11837 UK_Count = UK_ModAsSideEffect + 1 11838 }; 11839 11840 struct Usage { 11841 Expr *Use; 11842 SequenceTree::Seq Seq; 11843 11844 Usage() : Use(nullptr), Seq() {} 11845 }; 11846 11847 struct UsageInfo { 11848 Usage Uses[UK_Count]; 11849 11850 /// Have we issued a diagnostic for this variable already? 11851 bool Diagnosed; 11852 11853 UsageInfo() : Uses(), Diagnosed(false) {} 11854 }; 11855 using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>; 11856 11857 Sema &SemaRef; 11858 11859 /// Sequenced regions within the expression. 11860 SequenceTree Tree; 11861 11862 /// Declaration modifications and references which we have seen. 11863 UsageInfoMap UsageMap; 11864 11865 /// The region we are currently within. 11866 SequenceTree::Seq Region; 11867 11868 /// Filled in with declarations which were modified as a side-effect 11869 /// (that is, post-increment operations). 11870 SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr; 11871 11872 /// Expressions to check later. We defer checking these to reduce 11873 /// stack usage. 11874 SmallVectorImpl<Expr *> &WorkList; 11875 11876 /// RAII object wrapping the visitation of a sequenced subexpression of an 11877 /// expression. At the end of this process, the side-effects of the evaluation 11878 /// become sequenced with respect to the value computation of the result, so 11879 /// we downgrade any UK_ModAsSideEffect within the evaluation to 11880 /// UK_ModAsValue. 11881 struct SequencedSubexpression { 11882 SequencedSubexpression(SequenceChecker &Self) 11883 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { 11884 Self.ModAsSideEffect = &ModAsSideEffect; 11885 } 11886 11887 ~SequencedSubexpression() { 11888 for (auto &M : llvm::reverse(ModAsSideEffect)) { 11889 UsageInfo &U = Self.UsageMap[M.first]; 11890 auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect]; 11891 Self.addUsage(U, M.first, SideEffectUsage.Use, UK_ModAsValue); 11892 SideEffectUsage = M.second; 11893 } 11894 Self.ModAsSideEffect = OldModAsSideEffect; 11895 } 11896 11897 SequenceChecker &Self; 11898 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; 11899 SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect; 11900 }; 11901 11902 /// RAII object wrapping the visitation of a subexpression which we might 11903 /// choose to evaluate as a constant. If any subexpression is evaluated and 11904 /// found to be non-constant, this allows us to suppress the evaluation of 11905 /// the outer expression. 11906 class EvaluationTracker { 11907 public: 11908 EvaluationTracker(SequenceChecker &Self) 11909 : Self(Self), Prev(Self.EvalTracker) { 11910 Self.EvalTracker = this; 11911 } 11912 11913 ~EvaluationTracker() { 11914 Self.EvalTracker = Prev; 11915 if (Prev) 11916 Prev->EvalOK &= EvalOK; 11917 } 11918 11919 bool evaluate(const Expr *E, bool &Result) { 11920 if (!EvalOK || E->isValueDependent()) 11921 return false; 11922 EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context); 11923 return EvalOK; 11924 } 11925 11926 private: 11927 SequenceChecker &Self; 11928 EvaluationTracker *Prev; 11929 bool EvalOK = true; 11930 } *EvalTracker = nullptr; 11931 11932 /// Find the object which is produced by the specified expression, 11933 /// if any. 11934 Object getObject(Expr *E, bool Mod) const { 11935 E = E->IgnoreParenCasts(); 11936 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 11937 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) 11938 return getObject(UO->getSubExpr(), Mod); 11939 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 11940 if (BO->getOpcode() == BO_Comma) 11941 return getObject(BO->getRHS(), Mod); 11942 if (Mod && BO->isAssignmentOp()) 11943 return getObject(BO->getLHS(), Mod); 11944 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 11945 // FIXME: Check for more interesting cases, like "x.n = ++x.n". 11946 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) 11947 return ME->getMemberDecl(); 11948 } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 11949 // FIXME: If this is a reference, map through to its value. 11950 return DRE->getDecl(); 11951 return nullptr; 11952 } 11953 11954 /// Note that an object was modified or used by an expression. 11955 void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) { 11956 Usage &U = UI.Uses[UK]; 11957 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) { 11958 if (UK == UK_ModAsSideEffect && ModAsSideEffect) 11959 ModAsSideEffect->push_back(std::make_pair(O, U)); 11960 U.Use = Ref; 11961 U.Seq = Region; 11962 } 11963 } 11964 11965 /// Check whether a modification or use conflicts with a prior usage. 11966 void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind, 11967 bool IsModMod) { 11968 if (UI.Diagnosed) 11969 return; 11970 11971 const Usage &U = UI.Uses[OtherKind]; 11972 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) 11973 return; 11974 11975 Expr *Mod = U.Use; 11976 Expr *ModOrUse = Ref; 11977 if (OtherKind == UK_Use) 11978 std::swap(Mod, ModOrUse); 11979 11980 SemaRef.Diag(Mod->getExprLoc(), 11981 IsModMod ? diag::warn_unsequenced_mod_mod 11982 : diag::warn_unsequenced_mod_use) 11983 << O << SourceRange(ModOrUse->getExprLoc()); 11984 UI.Diagnosed = true; 11985 } 11986 11987 void notePreUse(Object O, Expr *Use) { 11988 UsageInfo &U = UsageMap[O]; 11989 // Uses conflict with other modifications. 11990 checkUsage(O, U, Use, UK_ModAsValue, false); 11991 } 11992 11993 void notePostUse(Object O, Expr *Use) { 11994 UsageInfo &U = UsageMap[O]; 11995 checkUsage(O, U, Use, UK_ModAsSideEffect, false); 11996 addUsage(U, O, Use, UK_Use); 11997 } 11998 11999 void notePreMod(Object O, Expr *Mod) { 12000 UsageInfo &U = UsageMap[O]; 12001 // Modifications conflict with other modifications and with uses. 12002 checkUsage(O, U, Mod, UK_ModAsValue, true); 12003 checkUsage(O, U, Mod, UK_Use, false); 12004 } 12005 12006 void notePostMod(Object O, Expr *Use, UsageKind UK) { 12007 UsageInfo &U = UsageMap[O]; 12008 checkUsage(O, U, Use, UK_ModAsSideEffect, true); 12009 addUsage(U, O, Use, UK); 12010 } 12011 12012 public: 12013 SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList) 12014 : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) { 12015 Visit(E); 12016 } 12017 12018 void VisitStmt(Stmt *S) { 12019 // Skip all statements which aren't expressions for now. 12020 } 12021 12022 void VisitExpr(Expr *E) { 12023 // By default, just recurse to evaluated subexpressions. 12024 Base::VisitStmt(E); 12025 } 12026 12027 void VisitCastExpr(CastExpr *E) { 12028 Object O = Object(); 12029 if (E->getCastKind() == CK_LValueToRValue) 12030 O = getObject(E->getSubExpr(), false); 12031 12032 if (O) 12033 notePreUse(O, E); 12034 VisitExpr(E); 12035 if (O) 12036 notePostUse(O, E); 12037 } 12038 12039 void VisitSequencedExpressions(Expr *SequencedBefore, Expr *SequencedAfter) { 12040 SequenceTree::Seq BeforeRegion = Tree.allocate(Region); 12041 SequenceTree::Seq AfterRegion = Tree.allocate(Region); 12042 SequenceTree::Seq OldRegion = Region; 12043 12044 { 12045 SequencedSubexpression SeqBefore(*this); 12046 Region = BeforeRegion; 12047 Visit(SequencedBefore); 12048 } 12049 12050 Region = AfterRegion; 12051 Visit(SequencedAfter); 12052 12053 Region = OldRegion; 12054 12055 Tree.merge(BeforeRegion); 12056 Tree.merge(AfterRegion); 12057 } 12058 12059 void VisitArraySubscriptExpr(ArraySubscriptExpr *ASE) { 12060 // C++17 [expr.sub]p1: 12061 // The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The 12062 // expression E1 is sequenced before the expression E2. 12063 if (SemaRef.getLangOpts().CPlusPlus17) 12064 VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS()); 12065 else 12066 Base::VisitStmt(ASE); 12067 } 12068 12069 void VisitBinComma(BinaryOperator *BO) { 12070 // C++11 [expr.comma]p1: 12071 // Every value computation and side effect associated with the left 12072 // expression is sequenced before every value computation and side 12073 // effect associated with the right expression. 12074 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12075 } 12076 12077 void VisitBinAssign(BinaryOperator *BO) { 12078 // The modification is sequenced after the value computation of the LHS 12079 // and RHS, so check it before inspecting the operands and update the 12080 // map afterwards. 12081 Object O = getObject(BO->getLHS(), true); 12082 if (!O) 12083 return VisitExpr(BO); 12084 12085 notePreMod(O, BO); 12086 12087 // C++11 [expr.ass]p7: 12088 // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated 12089 // only once. 12090 // 12091 // Therefore, for a compound assignment operator, O is considered used 12092 // everywhere except within the evaluation of E1 itself. 12093 if (isa<CompoundAssignOperator>(BO)) 12094 notePreUse(O, BO); 12095 12096 Visit(BO->getLHS()); 12097 12098 if (isa<CompoundAssignOperator>(BO)) 12099 notePostUse(O, BO); 12100 12101 Visit(BO->getRHS()); 12102 12103 // C++11 [expr.ass]p1: 12104 // the assignment is sequenced [...] before the value computation of the 12105 // assignment expression. 12106 // C11 6.5.16/3 has no such rule. 12107 notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12108 : UK_ModAsSideEffect); 12109 } 12110 12111 void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) { 12112 VisitBinAssign(CAO); 12113 } 12114 12115 void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12116 void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12117 void VisitUnaryPreIncDec(UnaryOperator *UO) { 12118 Object O = getObject(UO->getSubExpr(), true); 12119 if (!O) 12120 return VisitExpr(UO); 12121 12122 notePreMod(O, UO); 12123 Visit(UO->getSubExpr()); 12124 // C++11 [expr.pre.incr]p1: 12125 // the expression ++x is equivalent to x+=1 12126 notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12127 : UK_ModAsSideEffect); 12128 } 12129 12130 void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12131 void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12132 void VisitUnaryPostIncDec(UnaryOperator *UO) { 12133 Object O = getObject(UO->getSubExpr(), true); 12134 if (!O) 12135 return VisitExpr(UO); 12136 12137 notePreMod(O, UO); 12138 Visit(UO->getSubExpr()); 12139 notePostMod(O, UO, UK_ModAsSideEffect); 12140 } 12141 12142 /// Don't visit the RHS of '&&' or '||' if it might not be evaluated. 12143 void VisitBinLOr(BinaryOperator *BO) { 12144 // The side-effects of the LHS of an '&&' are sequenced before the 12145 // value computation of the RHS, and hence before the value computation 12146 // of the '&&' itself, unless the LHS evaluates to zero. We treat them 12147 // as if they were unconditionally sequenced. 12148 EvaluationTracker Eval(*this); 12149 { 12150 SequencedSubexpression Sequenced(*this); 12151 Visit(BO->getLHS()); 12152 } 12153 12154 bool Result; 12155 if (Eval.evaluate(BO->getLHS(), Result)) { 12156 if (!Result) 12157 Visit(BO->getRHS()); 12158 } else { 12159 // Check for unsequenced operations in the RHS, treating it as an 12160 // entirely separate evaluation. 12161 // 12162 // FIXME: If there are operations in the RHS which are unsequenced 12163 // with respect to operations outside the RHS, and those operations 12164 // are unconditionally evaluated, diagnose them. 12165 WorkList.push_back(BO->getRHS()); 12166 } 12167 } 12168 void VisitBinLAnd(BinaryOperator *BO) { 12169 EvaluationTracker Eval(*this); 12170 { 12171 SequencedSubexpression Sequenced(*this); 12172 Visit(BO->getLHS()); 12173 } 12174 12175 bool Result; 12176 if (Eval.evaluate(BO->getLHS(), Result)) { 12177 if (Result) 12178 Visit(BO->getRHS()); 12179 } else { 12180 WorkList.push_back(BO->getRHS()); 12181 } 12182 } 12183 12184 // Only visit the condition, unless we can be sure which subexpression will 12185 // be chosen. 12186 void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) { 12187 EvaluationTracker Eval(*this); 12188 { 12189 SequencedSubexpression Sequenced(*this); 12190 Visit(CO->getCond()); 12191 } 12192 12193 bool Result; 12194 if (Eval.evaluate(CO->getCond(), Result)) 12195 Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr()); 12196 else { 12197 WorkList.push_back(CO->getTrueExpr()); 12198 WorkList.push_back(CO->getFalseExpr()); 12199 } 12200 } 12201 12202 void VisitCallExpr(CallExpr *CE) { 12203 // C++11 [intro.execution]p15: 12204 // When calling a function [...], every value computation and side effect 12205 // associated with any argument expression, or with the postfix expression 12206 // designating the called function, is sequenced before execution of every 12207 // expression or statement in the body of the function [and thus before 12208 // the value computation of its result]. 12209 SequencedSubexpression Sequenced(*this); 12210 Base::VisitCallExpr(CE); 12211 12212 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. 12213 } 12214 12215 void VisitCXXConstructExpr(CXXConstructExpr *CCE) { 12216 // This is a call, so all subexpressions are sequenced before the result. 12217 SequencedSubexpression Sequenced(*this); 12218 12219 if (!CCE->isListInitialization()) 12220 return VisitExpr(CCE); 12221 12222 // In C++11, list initializations are sequenced. 12223 SmallVector<SequenceTree::Seq, 32> Elts; 12224 SequenceTree::Seq Parent = Region; 12225 for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(), 12226 E = CCE->arg_end(); 12227 I != E; ++I) { 12228 Region = Tree.allocate(Parent); 12229 Elts.push_back(Region); 12230 Visit(*I); 12231 } 12232 12233 // Forget that the initializers are sequenced. 12234 Region = Parent; 12235 for (unsigned I = 0; I < Elts.size(); ++I) 12236 Tree.merge(Elts[I]); 12237 } 12238 12239 void VisitInitListExpr(InitListExpr *ILE) { 12240 if (!SemaRef.getLangOpts().CPlusPlus11) 12241 return VisitExpr(ILE); 12242 12243 // In C++11, list initializations are sequenced. 12244 SmallVector<SequenceTree::Seq, 32> Elts; 12245 SequenceTree::Seq Parent = Region; 12246 for (unsigned I = 0; I < ILE->getNumInits(); ++I) { 12247 Expr *E = ILE->getInit(I); 12248 if (!E) continue; 12249 Region = Tree.allocate(Parent); 12250 Elts.push_back(Region); 12251 Visit(E); 12252 } 12253 12254 // Forget that the initializers are sequenced. 12255 Region = Parent; 12256 for (unsigned I = 0; I < Elts.size(); ++I) 12257 Tree.merge(Elts[I]); 12258 } 12259 }; 12260 12261 } // namespace 12262 12263 void Sema::CheckUnsequencedOperations(Expr *E) { 12264 SmallVector<Expr *, 8> WorkList; 12265 WorkList.push_back(E); 12266 while (!WorkList.empty()) { 12267 Expr *Item = WorkList.pop_back_val(); 12268 SequenceChecker(*this, Item, WorkList); 12269 } 12270 } 12271 12272 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, 12273 bool IsConstexpr) { 12274 CheckImplicitConversions(E, CheckLoc); 12275 if (!E->isInstantiationDependent()) 12276 CheckUnsequencedOperations(E); 12277 if (!IsConstexpr && !E->isValueDependent()) 12278 CheckForIntOverflow(E); 12279 DiagnoseMisalignedMembers(); 12280 } 12281 12282 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, 12283 FieldDecl *BitField, 12284 Expr *Init) { 12285 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); 12286 } 12287 12288 static void diagnoseArrayStarInParamType(Sema &S, QualType PType, 12289 SourceLocation Loc) { 12290 if (!PType->isVariablyModifiedType()) 12291 return; 12292 if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { 12293 diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); 12294 return; 12295 } 12296 if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { 12297 diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); 12298 return; 12299 } 12300 if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { 12301 diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); 12302 return; 12303 } 12304 12305 const ArrayType *AT = S.Context.getAsArrayType(PType); 12306 if (!AT) 12307 return; 12308 12309 if (AT->getSizeModifier() != ArrayType::Star) { 12310 diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); 12311 return; 12312 } 12313 12314 S.Diag(Loc, diag::err_array_star_in_function_definition); 12315 } 12316 12317 /// CheckParmsForFunctionDef - Check that the parameters of the given 12318 /// function are appropriate for the definition of a function. This 12319 /// takes care of any checks that cannot be performed on the 12320 /// declaration itself, e.g., that the types of each of the function 12321 /// parameters are complete. 12322 bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters, 12323 bool CheckParameterNames) { 12324 bool HasInvalidParm = false; 12325 for (ParmVarDecl *Param : Parameters) { 12326 // C99 6.7.5.3p4: the parameters in a parameter type list in a 12327 // function declarator that is part of a function definition of 12328 // that function shall not have incomplete type. 12329 // 12330 // This is also C++ [dcl.fct]p6. 12331 if (!Param->isInvalidDecl() && 12332 RequireCompleteType(Param->getLocation(), Param->getType(), 12333 diag::err_typecheck_decl_incomplete_type)) { 12334 Param->setInvalidDecl(); 12335 HasInvalidParm = true; 12336 } 12337 12338 // C99 6.9.1p5: If the declarator includes a parameter type list, the 12339 // declaration of each parameter shall include an identifier. 12340 if (CheckParameterNames && 12341 Param->getIdentifier() == nullptr && 12342 !Param->isImplicit() && 12343 !getLangOpts().CPlusPlus) 12344 Diag(Param->getLocation(), diag::err_parameter_name_omitted); 12345 12346 // C99 6.7.5.3p12: 12347 // If the function declarator is not part of a definition of that 12348 // function, parameters may have incomplete type and may use the [*] 12349 // notation in their sequences of declarator specifiers to specify 12350 // variable length array types. 12351 QualType PType = Param->getOriginalType(); 12352 // FIXME: This diagnostic should point the '[*]' if source-location 12353 // information is added for it. 12354 diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); 12355 12356 // If the parameter is a c++ class type and it has to be destructed in the 12357 // callee function, declare the destructor so that it can be called by the 12358 // callee function. Do not perform any direct access check on the dtor here. 12359 if (!Param->isInvalidDecl()) { 12360 if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) { 12361 if (!ClassDecl->isInvalidDecl() && 12362 !ClassDecl->hasIrrelevantDestructor() && 12363 !ClassDecl->isDependentContext() && 12364 ClassDecl->isParamDestroyedInCallee()) { 12365 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); 12366 MarkFunctionReferenced(Param->getLocation(), Destructor); 12367 DiagnoseUseOfDecl(Destructor, Param->getLocation()); 12368 } 12369 } 12370 } 12371 12372 // Parameters with the pass_object_size attribute only need to be marked 12373 // constant at function definitions. Because we lack information about 12374 // whether we're on a declaration or definition when we're instantiating the 12375 // attribute, we need to check for constness here. 12376 if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>()) 12377 if (!Param->getType().isConstQualified()) 12378 Diag(Param->getLocation(), diag::err_attribute_pointers_only) 12379 << Attr->getSpelling() << 1; 12380 12381 // Check for parameter names shadowing fields from the class. 12382 if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) { 12383 // The owning context for the parameter should be the function, but we 12384 // want to see if this function's declaration context is a record. 12385 DeclContext *DC = Param->getDeclContext(); 12386 if (DC && DC->isFunctionOrMethod()) { 12387 if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent())) 12388 CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(), 12389 RD, /*DeclIsField*/ false); 12390 } 12391 } 12392 } 12393 12394 return HasInvalidParm; 12395 } 12396 12397 /// A helper function to get the alignment of a Decl referred to by DeclRefExpr 12398 /// or MemberExpr. 12399 static CharUnits getDeclAlign(Expr *E, CharUnits TypeAlign, 12400 ASTContext &Context) { 12401 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 12402 return Context.getDeclAlign(DRE->getDecl()); 12403 12404 if (const auto *ME = dyn_cast<MemberExpr>(E)) 12405 return Context.getDeclAlign(ME->getMemberDecl()); 12406 12407 return TypeAlign; 12408 } 12409 12410 /// CheckCastAlign - Implements -Wcast-align, which warns when a 12411 /// pointer cast increases the alignment requirements. 12412 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { 12413 // This is actually a lot of work to potentially be doing on every 12414 // cast; don't do it if we're ignoring -Wcast_align (as is the default). 12415 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) 12416 return; 12417 12418 // Ignore dependent types. 12419 if (T->isDependentType() || Op->getType()->isDependentType()) 12420 return; 12421 12422 // Require that the destination be a pointer type. 12423 const PointerType *DestPtr = T->getAs<PointerType>(); 12424 if (!DestPtr) return; 12425 12426 // If the destination has alignment 1, we're done. 12427 QualType DestPointee = DestPtr->getPointeeType(); 12428 if (DestPointee->isIncompleteType()) return; 12429 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); 12430 if (DestAlign.isOne()) return; 12431 12432 // Require that the source be a pointer type. 12433 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); 12434 if (!SrcPtr) return; 12435 QualType SrcPointee = SrcPtr->getPointeeType(); 12436 12437 // Whitelist casts from cv void*. We already implicitly 12438 // whitelisted casts to cv void*, since they have alignment 1. 12439 // Also whitelist casts involving incomplete types, which implicitly 12440 // includes 'void'. 12441 if (SrcPointee->isIncompleteType()) return; 12442 12443 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); 12444 12445 if (auto *CE = dyn_cast<CastExpr>(Op)) { 12446 if (CE->getCastKind() == CK_ArrayToPointerDecay) 12447 SrcAlign = getDeclAlign(CE->getSubExpr(), SrcAlign, Context); 12448 } else if (auto *UO = dyn_cast<UnaryOperator>(Op)) { 12449 if (UO->getOpcode() == UO_AddrOf) 12450 SrcAlign = getDeclAlign(UO->getSubExpr(), SrcAlign, Context); 12451 } 12452 12453 if (SrcAlign >= DestAlign) return; 12454 12455 Diag(TRange.getBegin(), diag::warn_cast_align) 12456 << Op->getType() << T 12457 << static_cast<unsigned>(SrcAlign.getQuantity()) 12458 << static_cast<unsigned>(DestAlign.getQuantity()) 12459 << TRange << Op->getSourceRange(); 12460 } 12461 12462 /// Check whether this array fits the idiom of a size-one tail padded 12463 /// array member of a struct. 12464 /// 12465 /// We avoid emitting out-of-bounds access warnings for such arrays as they are 12466 /// commonly used to emulate flexible arrays in C89 code. 12467 static bool IsTailPaddedMemberArray(Sema &S, const llvm::APInt &Size, 12468 const NamedDecl *ND) { 12469 if (Size != 1 || !ND) return false; 12470 12471 const FieldDecl *FD = dyn_cast<FieldDecl>(ND); 12472 if (!FD) return false; 12473 12474 // Don't consider sizes resulting from macro expansions or template argument 12475 // substitution to form C89 tail-padded arrays. 12476 12477 TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); 12478 while (TInfo) { 12479 TypeLoc TL = TInfo->getTypeLoc(); 12480 // Look through typedefs. 12481 if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) { 12482 const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); 12483 TInfo = TDL->getTypeSourceInfo(); 12484 continue; 12485 } 12486 if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) { 12487 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); 12488 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) 12489 return false; 12490 } 12491 break; 12492 } 12493 12494 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); 12495 if (!RD) return false; 12496 if (RD->isUnion()) return false; 12497 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 12498 if (!CRD->isStandardLayout()) return false; 12499 } 12500 12501 // See if this is the last field decl in the record. 12502 const Decl *D = FD; 12503 while ((D = D->getNextDeclInContext())) 12504 if (isa<FieldDecl>(D)) 12505 return false; 12506 return true; 12507 } 12508 12509 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 12510 const ArraySubscriptExpr *ASE, 12511 bool AllowOnePastEnd, bool IndexNegated) { 12512 IndexExpr = IndexExpr->IgnoreParenImpCasts(); 12513 if (IndexExpr->isValueDependent()) 12514 return; 12515 12516 const Type *EffectiveType = 12517 BaseExpr->getType()->getPointeeOrArrayElementType(); 12518 BaseExpr = BaseExpr->IgnoreParenCasts(); 12519 const ConstantArrayType *ArrayTy = 12520 Context.getAsConstantArrayType(BaseExpr->getType()); 12521 12522 if (!ArrayTy) 12523 return; 12524 12525 const Type *BaseType = ArrayTy->getElementType().getTypePtr(); 12526 if (EffectiveType->isDependentType() || BaseType->isDependentType()) 12527 return; 12528 12529 Expr::EvalResult Result; 12530 if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) 12531 return; 12532 12533 llvm::APSInt index = Result.Val.getInt(); 12534 if (IndexNegated) 12535 index = -index; 12536 12537 const NamedDecl *ND = nullptr; 12538 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 12539 ND = DRE->getDecl(); 12540 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) 12541 ND = ME->getMemberDecl(); 12542 12543 if (index.isUnsigned() || !index.isNegative()) { 12544 // It is possible that the type of the base expression after 12545 // IgnoreParenCasts is incomplete, even though the type of the base 12546 // expression before IgnoreParenCasts is complete (see PR39746 for an 12547 // example). In this case we have no information about whether the array 12548 // access exceeds the array bounds. However we can still diagnose an array 12549 // access which precedes the array bounds. 12550 if (BaseType->isIncompleteType()) 12551 return; 12552 12553 llvm::APInt size = ArrayTy->getSize(); 12554 if (!size.isStrictlyPositive()) 12555 return; 12556 12557 if (BaseType != EffectiveType) { 12558 // Make sure we're comparing apples to apples when comparing index to size 12559 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); 12560 uint64_t array_typesize = Context.getTypeSize(BaseType); 12561 // Handle ptrarith_typesize being zero, such as when casting to void* 12562 if (!ptrarith_typesize) ptrarith_typesize = 1; 12563 if (ptrarith_typesize != array_typesize) { 12564 // There's a cast to a different size type involved 12565 uint64_t ratio = array_typesize / ptrarith_typesize; 12566 // TODO: Be smarter about handling cases where array_typesize is not a 12567 // multiple of ptrarith_typesize 12568 if (ptrarith_typesize * ratio == array_typesize) 12569 size *= llvm::APInt(size.getBitWidth(), ratio); 12570 } 12571 } 12572 12573 if (size.getBitWidth() > index.getBitWidth()) 12574 index = index.zext(size.getBitWidth()); 12575 else if (size.getBitWidth() < index.getBitWidth()) 12576 size = size.zext(index.getBitWidth()); 12577 12578 // For array subscripting the index must be less than size, but for pointer 12579 // arithmetic also allow the index (offset) to be equal to size since 12580 // computing the next address after the end of the array is legal and 12581 // commonly done e.g. in C++ iterators and range-based for loops. 12582 if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) 12583 return; 12584 12585 // Also don't warn for arrays of size 1 which are members of some 12586 // structure. These are often used to approximate flexible arrays in C89 12587 // code. 12588 if (IsTailPaddedMemberArray(*this, size, ND)) 12589 return; 12590 12591 // Suppress the warning if the subscript expression (as identified by the 12592 // ']' location) and the index expression are both from macro expansions 12593 // within a system header. 12594 if (ASE) { 12595 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( 12596 ASE->getRBracketLoc()); 12597 if (SourceMgr.isInSystemHeader(RBracketLoc)) { 12598 SourceLocation IndexLoc = 12599 SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc()); 12600 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) 12601 return; 12602 } 12603 } 12604 12605 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; 12606 if (ASE) 12607 DiagID = diag::warn_array_index_exceeds_bounds; 12608 12609 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 12610 PDiag(DiagID) << index.toString(10, true) 12611 << size.toString(10, true) 12612 << (unsigned)size.getLimitedValue(~0U) 12613 << IndexExpr->getSourceRange()); 12614 } else { 12615 unsigned DiagID = diag::warn_array_index_precedes_bounds; 12616 if (!ASE) { 12617 DiagID = diag::warn_ptr_arith_precedes_bounds; 12618 if (index.isNegative()) index = -index; 12619 } 12620 12621 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 12622 PDiag(DiagID) << index.toString(10, true) 12623 << IndexExpr->getSourceRange()); 12624 } 12625 12626 if (!ND) { 12627 // Try harder to find a NamedDecl to point at in the note. 12628 while (const ArraySubscriptExpr *ASE = 12629 dyn_cast<ArraySubscriptExpr>(BaseExpr)) 12630 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 12631 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 12632 ND = DRE->getDecl(); 12633 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) 12634 ND = ME->getMemberDecl(); 12635 } 12636 12637 if (ND) 12638 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 12639 PDiag(diag::note_array_index_out_of_bounds) 12640 << ND->getDeclName()); 12641 } 12642 12643 void Sema::CheckArrayAccess(const Expr *expr) { 12644 int AllowOnePastEnd = 0; 12645 while (expr) { 12646 expr = expr->IgnoreParenImpCasts(); 12647 switch (expr->getStmtClass()) { 12648 case Stmt::ArraySubscriptExprClass: { 12649 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); 12650 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, 12651 AllowOnePastEnd > 0); 12652 expr = ASE->getBase(); 12653 break; 12654 } 12655 case Stmt::MemberExprClass: { 12656 expr = cast<MemberExpr>(expr)->getBase(); 12657 break; 12658 } 12659 case Stmt::OMPArraySectionExprClass: { 12660 const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr); 12661 if (ASE->getLowerBound()) 12662 CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(), 12663 /*ASE=*/nullptr, AllowOnePastEnd > 0); 12664 return; 12665 } 12666 case Stmt::UnaryOperatorClass: { 12667 // Only unwrap the * and & unary operators 12668 const UnaryOperator *UO = cast<UnaryOperator>(expr); 12669 expr = UO->getSubExpr(); 12670 switch (UO->getOpcode()) { 12671 case UO_AddrOf: 12672 AllowOnePastEnd++; 12673 break; 12674 case UO_Deref: 12675 AllowOnePastEnd--; 12676 break; 12677 default: 12678 return; 12679 } 12680 break; 12681 } 12682 case Stmt::ConditionalOperatorClass: { 12683 const ConditionalOperator *cond = cast<ConditionalOperator>(expr); 12684 if (const Expr *lhs = cond->getLHS()) 12685 CheckArrayAccess(lhs); 12686 if (const Expr *rhs = cond->getRHS()) 12687 CheckArrayAccess(rhs); 12688 return; 12689 } 12690 case Stmt::CXXOperatorCallExprClass: { 12691 const auto *OCE = cast<CXXOperatorCallExpr>(expr); 12692 for (const auto *Arg : OCE->arguments()) 12693 CheckArrayAccess(Arg); 12694 return; 12695 } 12696 default: 12697 return; 12698 } 12699 } 12700 } 12701 12702 //===--- CHECK: Objective-C retain cycles ----------------------------------// 12703 12704 namespace { 12705 12706 struct RetainCycleOwner { 12707 VarDecl *Variable = nullptr; 12708 SourceRange Range; 12709 SourceLocation Loc; 12710 bool Indirect = false; 12711 12712 RetainCycleOwner() = default; 12713 12714 void setLocsFrom(Expr *e) { 12715 Loc = e->getExprLoc(); 12716 Range = e->getSourceRange(); 12717 } 12718 }; 12719 12720 } // namespace 12721 12722 /// Consider whether capturing the given variable can possibly lead to 12723 /// a retain cycle. 12724 static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { 12725 // In ARC, it's captured strongly iff the variable has __strong 12726 // lifetime. In MRR, it's captured strongly if the variable is 12727 // __block and has an appropriate type. 12728 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 12729 return false; 12730 12731 owner.Variable = var; 12732 if (ref) 12733 owner.setLocsFrom(ref); 12734 return true; 12735 } 12736 12737 static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { 12738 while (true) { 12739 e = e->IgnoreParens(); 12740 if (CastExpr *cast = dyn_cast<CastExpr>(e)) { 12741 switch (cast->getCastKind()) { 12742 case CK_BitCast: 12743 case CK_LValueBitCast: 12744 case CK_LValueToRValue: 12745 case CK_ARCReclaimReturnedObject: 12746 e = cast->getSubExpr(); 12747 continue; 12748 12749 default: 12750 return false; 12751 } 12752 } 12753 12754 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { 12755 ObjCIvarDecl *ivar = ref->getDecl(); 12756 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 12757 return false; 12758 12759 // Try to find a retain cycle in the base. 12760 if (!findRetainCycleOwner(S, ref->getBase(), owner)) 12761 return false; 12762 12763 if (ref->isFreeIvar()) owner.setLocsFrom(ref); 12764 owner.Indirect = true; 12765 return true; 12766 } 12767 12768 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { 12769 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); 12770 if (!var) return false; 12771 return considerVariable(var, ref, owner); 12772 } 12773 12774 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { 12775 if (member->isArrow()) return false; 12776 12777 // Don't count this as an indirect ownership. 12778 e = member->getBase(); 12779 continue; 12780 } 12781 12782 if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 12783 // Only pay attention to pseudo-objects on property references. 12784 ObjCPropertyRefExpr *pre 12785 = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() 12786 ->IgnoreParens()); 12787 if (!pre) return false; 12788 if (pre->isImplicitProperty()) return false; 12789 ObjCPropertyDecl *property = pre->getExplicitProperty(); 12790 if (!property->isRetaining() && 12791 !(property->getPropertyIvarDecl() && 12792 property->getPropertyIvarDecl()->getType() 12793 .getObjCLifetime() == Qualifiers::OCL_Strong)) 12794 return false; 12795 12796 owner.Indirect = true; 12797 if (pre->isSuperReceiver()) { 12798 owner.Variable = S.getCurMethodDecl()->getSelfDecl(); 12799 if (!owner.Variable) 12800 return false; 12801 owner.Loc = pre->getLocation(); 12802 owner.Range = pre->getSourceRange(); 12803 return true; 12804 } 12805 e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) 12806 ->getSourceExpr()); 12807 continue; 12808 } 12809 12810 // Array ivars? 12811 12812 return false; 12813 } 12814 } 12815 12816 namespace { 12817 12818 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { 12819 ASTContext &Context; 12820 VarDecl *Variable; 12821 Expr *Capturer = nullptr; 12822 bool VarWillBeReased = false; 12823 12824 FindCaptureVisitor(ASTContext &Context, VarDecl *variable) 12825 : EvaluatedExprVisitor<FindCaptureVisitor>(Context), 12826 Context(Context), Variable(variable) {} 12827 12828 void VisitDeclRefExpr(DeclRefExpr *ref) { 12829 if (ref->getDecl() == Variable && !Capturer) 12830 Capturer = ref; 12831 } 12832 12833 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { 12834 if (Capturer) return; 12835 Visit(ref->getBase()); 12836 if (Capturer && ref->isFreeIvar()) 12837 Capturer = ref; 12838 } 12839 12840 void VisitBlockExpr(BlockExpr *block) { 12841 // Look inside nested blocks 12842 if (block->getBlockDecl()->capturesVariable(Variable)) 12843 Visit(block->getBlockDecl()->getBody()); 12844 } 12845 12846 void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { 12847 if (Capturer) return; 12848 if (OVE->getSourceExpr()) 12849 Visit(OVE->getSourceExpr()); 12850 } 12851 12852 void VisitBinaryOperator(BinaryOperator *BinOp) { 12853 if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign) 12854 return; 12855 Expr *LHS = BinOp->getLHS(); 12856 if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) { 12857 if (DRE->getDecl() != Variable) 12858 return; 12859 if (Expr *RHS = BinOp->getRHS()) { 12860 RHS = RHS->IgnoreParenCasts(); 12861 llvm::APSInt Value; 12862 VarWillBeReased = 12863 (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); 12864 } 12865 } 12866 } 12867 }; 12868 12869 } // namespace 12870 12871 /// Check whether the given argument is a block which captures a 12872 /// variable. 12873 static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { 12874 assert(owner.Variable && owner.Loc.isValid()); 12875 12876 e = e->IgnoreParenCasts(); 12877 12878 // Look through [^{...} copy] and Block_copy(^{...}). 12879 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { 12880 Selector Cmd = ME->getSelector(); 12881 if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { 12882 e = ME->getInstanceReceiver(); 12883 if (!e) 12884 return nullptr; 12885 e = e->IgnoreParenCasts(); 12886 } 12887 } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { 12888 if (CE->getNumArgs() == 1) { 12889 FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); 12890 if (Fn) { 12891 const IdentifierInfo *FnI = Fn->getIdentifier(); 12892 if (FnI && FnI->isStr("_Block_copy")) { 12893 e = CE->getArg(0)->IgnoreParenCasts(); 12894 } 12895 } 12896 } 12897 } 12898 12899 BlockExpr *block = dyn_cast<BlockExpr>(e); 12900 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) 12901 return nullptr; 12902 12903 FindCaptureVisitor visitor(S.Context, owner.Variable); 12904 visitor.Visit(block->getBlockDecl()->getBody()); 12905 return visitor.VarWillBeReased ? nullptr : visitor.Capturer; 12906 } 12907 12908 static void diagnoseRetainCycle(Sema &S, Expr *capturer, 12909 RetainCycleOwner &owner) { 12910 assert(capturer); 12911 assert(owner.Variable && owner.Loc.isValid()); 12912 12913 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) 12914 << owner.Variable << capturer->getSourceRange(); 12915 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) 12916 << owner.Indirect << owner.Range; 12917 } 12918 12919 /// Check for a keyword selector that starts with the word 'add' or 12920 /// 'set'. 12921 static bool isSetterLikeSelector(Selector sel) { 12922 if (sel.isUnarySelector()) return false; 12923 12924 StringRef str = sel.getNameForSlot(0); 12925 while (!str.empty() && str.front() == '_') str = str.substr(1); 12926 if (str.startswith("set")) 12927 str = str.substr(3); 12928 else if (str.startswith("add")) { 12929 // Specially whitelist 'addOperationWithBlock:'. 12930 if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) 12931 return false; 12932 str = str.substr(3); 12933 } 12934 else 12935 return false; 12936 12937 if (str.empty()) return true; 12938 return !isLowercase(str.front()); 12939 } 12940 12941 static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S, 12942 ObjCMessageExpr *Message) { 12943 bool IsMutableArray = S.NSAPIObj->isSubclassOfNSClass( 12944 Message->getReceiverInterface(), 12945 NSAPI::ClassId_NSMutableArray); 12946 if (!IsMutableArray) { 12947 return None; 12948 } 12949 12950 Selector Sel = Message->getSelector(); 12951 12952 Optional<NSAPI::NSArrayMethodKind> MKOpt = 12953 S.NSAPIObj->getNSArrayMethodKind(Sel); 12954 if (!MKOpt) { 12955 return None; 12956 } 12957 12958 NSAPI::NSArrayMethodKind MK = *MKOpt; 12959 12960 switch (MK) { 12961 case NSAPI::NSMutableArr_addObject: 12962 case NSAPI::NSMutableArr_insertObjectAtIndex: 12963 case NSAPI::NSMutableArr_setObjectAtIndexedSubscript: 12964 return 0; 12965 case NSAPI::NSMutableArr_replaceObjectAtIndex: 12966 return 1; 12967 12968 default: 12969 return None; 12970 } 12971 12972 return None; 12973 } 12974 12975 static 12976 Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S, 12977 ObjCMessageExpr *Message) { 12978 bool IsMutableDictionary = S.NSAPIObj->isSubclassOfNSClass( 12979 Message->getReceiverInterface(), 12980 NSAPI::ClassId_NSMutableDictionary); 12981 if (!IsMutableDictionary) { 12982 return None; 12983 } 12984 12985 Selector Sel = Message->getSelector(); 12986 12987 Optional<NSAPI::NSDictionaryMethodKind> MKOpt = 12988 S.NSAPIObj->getNSDictionaryMethodKind(Sel); 12989 if (!MKOpt) { 12990 return None; 12991 } 12992 12993 NSAPI::NSDictionaryMethodKind MK = *MKOpt; 12994 12995 switch (MK) { 12996 case NSAPI::NSMutableDict_setObjectForKey: 12997 case NSAPI::NSMutableDict_setValueForKey: 12998 case NSAPI::NSMutableDict_setObjectForKeyedSubscript: 12999 return 0; 13000 13001 default: 13002 return None; 13003 } 13004 13005 return None; 13006 } 13007 13008 static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) { 13009 bool IsMutableSet = S.NSAPIObj->isSubclassOfNSClass( 13010 Message->getReceiverInterface(), 13011 NSAPI::ClassId_NSMutableSet); 13012 13013 bool IsMutableOrderedSet = S.NSAPIObj->isSubclassOfNSClass( 13014 Message->getReceiverInterface(), 13015 NSAPI::ClassId_NSMutableOrderedSet); 13016 if (!IsMutableSet && !IsMutableOrderedSet) { 13017 return None; 13018 } 13019 13020 Selector Sel = Message->getSelector(); 13021 13022 Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel); 13023 if (!MKOpt) { 13024 return None; 13025 } 13026 13027 NSAPI::NSSetMethodKind MK = *MKOpt; 13028 13029 switch (MK) { 13030 case NSAPI::NSMutableSet_addObject: 13031 case NSAPI::NSOrderedSet_setObjectAtIndex: 13032 case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript: 13033 case NSAPI::NSOrderedSet_insertObjectAtIndex: 13034 return 0; 13035 case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject: 13036 return 1; 13037 } 13038 13039 return None; 13040 } 13041 13042 void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) { 13043 if (!Message->isInstanceMessage()) { 13044 return; 13045 } 13046 13047 Optional<int> ArgOpt; 13048 13049 if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) && 13050 !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) && 13051 !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) { 13052 return; 13053 } 13054 13055 int ArgIndex = *ArgOpt; 13056 13057 Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts(); 13058 if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) { 13059 Arg = OE->getSourceExpr()->IgnoreImpCasts(); 13060 } 13061 13062 if (Message->getReceiverKind() == ObjCMessageExpr::SuperInstance) { 13063 if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { 13064 if (ArgRE->isObjCSelfExpr()) { 13065 Diag(Message->getSourceRange().getBegin(), 13066 diag::warn_objc_circular_container) 13067 << ArgRE->getDecl() << StringRef("'super'"); 13068 } 13069 } 13070 } else { 13071 Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts(); 13072 13073 if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) { 13074 Receiver = OE->getSourceExpr()->IgnoreImpCasts(); 13075 } 13076 13077 if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) { 13078 if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) { 13079 if (ReceiverRE->getDecl() == ArgRE->getDecl()) { 13080 ValueDecl *Decl = ReceiverRE->getDecl(); 13081 Diag(Message->getSourceRange().getBegin(), 13082 diag::warn_objc_circular_container) 13083 << Decl << Decl; 13084 if (!ArgRE->isObjCSelfExpr()) { 13085 Diag(Decl->getLocation(), 13086 diag::note_objc_circular_container_declared_here) 13087 << Decl; 13088 } 13089 } 13090 } 13091 } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) { 13092 if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) { 13093 if (IvarRE->getDecl() == IvarArgRE->getDecl()) { 13094 ObjCIvarDecl *Decl = IvarRE->getDecl(); 13095 Diag(Message->getSourceRange().getBegin(), 13096 diag::warn_objc_circular_container) 13097 << Decl << Decl; 13098 Diag(Decl->getLocation(), 13099 diag::note_objc_circular_container_declared_here) 13100 << Decl; 13101 } 13102 } 13103 } 13104 } 13105 } 13106 13107 /// Check a message send to see if it's likely to cause a retain cycle. 13108 void Sema::checkRetainCycles(ObjCMessageExpr *msg) { 13109 // Only check instance methods whose selector looks like a setter. 13110 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) 13111 return; 13112 13113 // Try to find a variable that the receiver is strongly owned by. 13114 RetainCycleOwner owner; 13115 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { 13116 if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) 13117 return; 13118 } else { 13119 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); 13120 owner.Variable = getCurMethodDecl()->getSelfDecl(); 13121 owner.Loc = msg->getSuperLoc(); 13122 owner.Range = msg->getSuperLoc(); 13123 } 13124 13125 // Check whether the receiver is captured by any of the arguments. 13126 const ObjCMethodDecl *MD = msg->getMethodDecl(); 13127 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) { 13128 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) { 13129 // noescape blocks should not be retained by the method. 13130 if (MD && MD->parameters()[i]->hasAttr<NoEscapeAttr>()) 13131 continue; 13132 return diagnoseRetainCycle(*this, capturer, owner); 13133 } 13134 } 13135 } 13136 13137 /// Check a property assign to see if it's likely to cause a retain cycle. 13138 void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { 13139 RetainCycleOwner owner; 13140 if (!findRetainCycleOwner(*this, receiver, owner)) 13141 return; 13142 13143 if (Expr *capturer = findCapturingExpr(*this, argument, owner)) 13144 diagnoseRetainCycle(*this, capturer, owner); 13145 } 13146 13147 void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { 13148 RetainCycleOwner Owner; 13149 if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner)) 13150 return; 13151 13152 // Because we don't have an expression for the variable, we have to set the 13153 // location explicitly here. 13154 Owner.Loc = Var->getLocation(); 13155 Owner.Range = Var->getSourceRange(); 13156 13157 if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) 13158 diagnoseRetainCycle(*this, Capturer, Owner); 13159 } 13160 13161 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, 13162 Expr *RHS, bool isProperty) { 13163 // Check if RHS is an Objective-C object literal, which also can get 13164 // immediately zapped in a weak reference. Note that we explicitly 13165 // allow ObjCStringLiterals, since those are designed to never really die. 13166 RHS = RHS->IgnoreParenImpCasts(); 13167 13168 // This enum needs to match with the 'select' in 13169 // warn_objc_arc_literal_assign (off-by-1). 13170 Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); 13171 if (Kind == Sema::LK_String || Kind == Sema::LK_None) 13172 return false; 13173 13174 S.Diag(Loc, diag::warn_arc_literal_assign) 13175 << (unsigned) Kind 13176 << (isProperty ? 0 : 1) 13177 << RHS->getSourceRange(); 13178 13179 return true; 13180 } 13181 13182 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, 13183 Qualifiers::ObjCLifetime LT, 13184 Expr *RHS, bool isProperty) { 13185 // Strip off any implicit cast added to get to the one ARC-specific. 13186 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13187 if (cast->getCastKind() == CK_ARCConsumeObject) { 13188 S.Diag(Loc, diag::warn_arc_retained_assign) 13189 << (LT == Qualifiers::OCL_ExplicitNone) 13190 << (isProperty ? 0 : 1) 13191 << RHS->getSourceRange(); 13192 return true; 13193 } 13194 RHS = cast->getSubExpr(); 13195 } 13196 13197 if (LT == Qualifiers::OCL_Weak && 13198 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) 13199 return true; 13200 13201 return false; 13202 } 13203 13204 bool Sema::checkUnsafeAssigns(SourceLocation Loc, 13205 QualType LHS, Expr *RHS) { 13206 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); 13207 13208 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) 13209 return false; 13210 13211 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) 13212 return true; 13213 13214 return false; 13215 } 13216 13217 void Sema::checkUnsafeExprAssigns(SourceLocation Loc, 13218 Expr *LHS, Expr *RHS) { 13219 QualType LHSType; 13220 // PropertyRef on LHS type need be directly obtained from 13221 // its declaration as it has a PseudoType. 13222 ObjCPropertyRefExpr *PRE 13223 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); 13224 if (PRE && !PRE->isImplicitProperty()) { 13225 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13226 if (PD) 13227 LHSType = PD->getType(); 13228 } 13229 13230 if (LHSType.isNull()) 13231 LHSType = LHS->getType(); 13232 13233 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); 13234 13235 if (LT == Qualifiers::OCL_Weak) { 13236 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) 13237 getCurFunction()->markSafeWeakUse(LHS); 13238 } 13239 13240 if (checkUnsafeAssigns(Loc, LHSType, RHS)) 13241 return; 13242 13243 // FIXME. Check for other life times. 13244 if (LT != Qualifiers::OCL_None) 13245 return; 13246 13247 if (PRE) { 13248 if (PRE->isImplicitProperty()) 13249 return; 13250 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13251 if (!PD) 13252 return; 13253 13254 unsigned Attributes = PD->getPropertyAttributes(); 13255 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { 13256 // when 'assign' attribute was not explicitly specified 13257 // by user, ignore it and rely on property type itself 13258 // for lifetime info. 13259 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); 13260 if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && 13261 LHSType->isObjCRetainableType()) 13262 return; 13263 13264 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13265 if (cast->getCastKind() == CK_ARCConsumeObject) { 13266 Diag(Loc, diag::warn_arc_retained_property_assign) 13267 << RHS->getSourceRange(); 13268 return; 13269 } 13270 RHS = cast->getSubExpr(); 13271 } 13272 } 13273 else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { 13274 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) 13275 return; 13276 } 13277 } 13278 } 13279 13280 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// 13281 13282 static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, 13283 SourceLocation StmtLoc, 13284 const NullStmt *Body) { 13285 // Do not warn if the body is a macro that expands to nothing, e.g: 13286 // 13287 // #define CALL(x) 13288 // if (condition) 13289 // CALL(0); 13290 if (Body->hasLeadingEmptyMacro()) 13291 return false; 13292 13293 // Get line numbers of statement and body. 13294 bool StmtLineInvalid; 13295 unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, 13296 &StmtLineInvalid); 13297 if (StmtLineInvalid) 13298 return false; 13299 13300 bool BodyLineInvalid; 13301 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), 13302 &BodyLineInvalid); 13303 if (BodyLineInvalid) 13304 return false; 13305 13306 // Warn if null statement and body are on the same line. 13307 if (StmtLine != BodyLine) 13308 return false; 13309 13310 return true; 13311 } 13312 13313 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, 13314 const Stmt *Body, 13315 unsigned DiagID) { 13316 // Since this is a syntactic check, don't emit diagnostic for template 13317 // instantiations, this just adds noise. 13318 if (CurrentInstantiationScope) 13319 return; 13320 13321 // The body should be a null statement. 13322 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13323 if (!NBody) 13324 return; 13325 13326 // Do the usual checks. 13327 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13328 return; 13329 13330 Diag(NBody->getSemiLoc(), DiagID); 13331 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13332 } 13333 13334 void Sema::DiagnoseEmptyLoopBody(const Stmt *S, 13335 const Stmt *PossibleBody) { 13336 assert(!CurrentInstantiationScope); // Ensured by caller 13337 13338 SourceLocation StmtLoc; 13339 const Stmt *Body; 13340 unsigned DiagID; 13341 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { 13342 StmtLoc = FS->getRParenLoc(); 13343 Body = FS->getBody(); 13344 DiagID = diag::warn_empty_for_body; 13345 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { 13346 StmtLoc = WS->getCond()->getSourceRange().getEnd(); 13347 Body = WS->getBody(); 13348 DiagID = diag::warn_empty_while_body; 13349 } else 13350 return; // Neither `for' nor `while'. 13351 13352 // The body should be a null statement. 13353 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13354 if (!NBody) 13355 return; 13356 13357 // Skip expensive checks if diagnostic is disabled. 13358 if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) 13359 return; 13360 13361 // Do the usual checks. 13362 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13363 return; 13364 13365 // `for(...);' and `while(...);' are popular idioms, so in order to keep 13366 // noise level low, emit diagnostics only if for/while is followed by a 13367 // CompoundStmt, e.g.: 13368 // for (int i = 0; i < n; i++); 13369 // { 13370 // a(i); 13371 // } 13372 // or if for/while is followed by a statement with more indentation 13373 // than for/while itself: 13374 // for (int i = 0; i < n; i++); 13375 // a(i); 13376 bool ProbableTypo = isa<CompoundStmt>(PossibleBody); 13377 if (!ProbableTypo) { 13378 bool BodyColInvalid; 13379 unsigned BodyCol = SourceMgr.getPresumedColumnNumber( 13380 PossibleBody->getBeginLoc(), &BodyColInvalid); 13381 if (BodyColInvalid) 13382 return; 13383 13384 bool StmtColInvalid; 13385 unsigned StmtCol = 13386 SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid); 13387 if (StmtColInvalid) 13388 return; 13389 13390 if (BodyCol > StmtCol) 13391 ProbableTypo = true; 13392 } 13393 13394 if (ProbableTypo) { 13395 Diag(NBody->getSemiLoc(), DiagID); 13396 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13397 } 13398 } 13399 13400 //===--- CHECK: Warn on self move with std::move. -------------------------===// 13401 13402 /// DiagnoseSelfMove - Emits a warning if a value is moved to itself. 13403 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, 13404 SourceLocation OpLoc) { 13405 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) 13406 return; 13407 13408 if (inTemplateInstantiation()) 13409 return; 13410 13411 // Strip parens and casts away. 13412 LHSExpr = LHSExpr->IgnoreParenImpCasts(); 13413 RHSExpr = RHSExpr->IgnoreParenImpCasts(); 13414 13415 // Check for a call expression 13416 const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr); 13417 if (!CE || CE->getNumArgs() != 1) 13418 return; 13419 13420 // Check for a call to std::move 13421 if (!CE->isCallToStdMove()) 13422 return; 13423 13424 // Get argument from std::move 13425 RHSExpr = CE->getArg(0); 13426 13427 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); 13428 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); 13429 13430 // Two DeclRefExpr's, check that the decls are the same. 13431 if (LHSDeclRef && RHSDeclRef) { 13432 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13433 return; 13434 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13435 RHSDeclRef->getDecl()->getCanonicalDecl()) 13436 return; 13437 13438 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() 13439 << LHSExpr->getSourceRange() 13440 << RHSExpr->getSourceRange(); 13441 return; 13442 } 13443 13444 // Member variables require a different approach to check for self moves. 13445 // MemberExpr's are the same if every nested MemberExpr refers to the same 13446 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or 13447 // the base Expr's are CXXThisExpr's. 13448 const Expr *LHSBase = LHSExpr; 13449 const Expr *RHSBase = RHSExpr; 13450 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); 13451 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); 13452 if (!LHSME || !RHSME) 13453 return; 13454 13455 while (LHSME && RHSME) { 13456 if (LHSME->getMemberDecl()->getCanonicalDecl() != 13457 RHSME->getMemberDecl()->getCanonicalDecl()) 13458 return; 13459 13460 LHSBase = LHSME->getBase(); 13461 RHSBase = RHSME->getBase(); 13462 LHSME = dyn_cast<MemberExpr>(LHSBase); 13463 RHSME = dyn_cast<MemberExpr>(RHSBase); 13464 } 13465 13466 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); 13467 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); 13468 if (LHSDeclRef && RHSDeclRef) { 13469 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13470 return; 13471 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13472 RHSDeclRef->getDecl()->getCanonicalDecl()) 13473 return; 13474 13475 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() 13476 << LHSExpr->getSourceRange() 13477 << RHSExpr->getSourceRange(); 13478 return; 13479 } 13480 13481 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) 13482 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() 13483 << LHSExpr->getSourceRange() 13484 << RHSExpr->getSourceRange(); 13485 } 13486 13487 //===--- Layout compatibility ----------------------------------------------// 13488 13489 static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); 13490 13491 /// Check if two enumeration types are layout-compatible. 13492 static bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { 13493 // C++11 [dcl.enum] p8: 13494 // Two enumeration types are layout-compatible if they have the same 13495 // underlying type. 13496 return ED1->isComplete() && ED2->isComplete() && 13497 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); 13498 } 13499 13500 /// Check if two fields are layout-compatible. 13501 static bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, 13502 FieldDecl *Field2) { 13503 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) 13504 return false; 13505 13506 if (Field1->isBitField() != Field2->isBitField()) 13507 return false; 13508 13509 if (Field1->isBitField()) { 13510 // Make sure that the bit-fields are the same length. 13511 unsigned Bits1 = Field1->getBitWidthValue(C); 13512 unsigned Bits2 = Field2->getBitWidthValue(C); 13513 13514 if (Bits1 != Bits2) 13515 return false; 13516 } 13517 13518 return true; 13519 } 13520 13521 /// Check if two standard-layout structs are layout-compatible. 13522 /// (C++11 [class.mem] p17) 13523 static bool isLayoutCompatibleStruct(ASTContext &C, RecordDecl *RD1, 13524 RecordDecl *RD2) { 13525 // If both records are C++ classes, check that base classes match. 13526 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { 13527 // If one of records is a CXXRecordDecl we are in C++ mode, 13528 // thus the other one is a CXXRecordDecl, too. 13529 const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); 13530 // Check number of base classes. 13531 if (D1CXX->getNumBases() != D2CXX->getNumBases()) 13532 return false; 13533 13534 // Check the base classes. 13535 for (CXXRecordDecl::base_class_const_iterator 13536 Base1 = D1CXX->bases_begin(), 13537 BaseEnd1 = D1CXX->bases_end(), 13538 Base2 = D2CXX->bases_begin(); 13539 Base1 != BaseEnd1; 13540 ++Base1, ++Base2) { 13541 if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) 13542 return false; 13543 } 13544 } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { 13545 // If only RD2 is a C++ class, it should have zero base classes. 13546 if (D2CXX->getNumBases() > 0) 13547 return false; 13548 } 13549 13550 // Check the fields. 13551 RecordDecl::field_iterator Field2 = RD2->field_begin(), 13552 Field2End = RD2->field_end(), 13553 Field1 = RD1->field_begin(), 13554 Field1End = RD1->field_end(); 13555 for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { 13556 if (!isLayoutCompatible(C, *Field1, *Field2)) 13557 return false; 13558 } 13559 if (Field1 != Field1End || Field2 != Field2End) 13560 return false; 13561 13562 return true; 13563 } 13564 13565 /// Check if two standard-layout unions are layout-compatible. 13566 /// (C++11 [class.mem] p18) 13567 static bool isLayoutCompatibleUnion(ASTContext &C, RecordDecl *RD1, 13568 RecordDecl *RD2) { 13569 llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; 13570 for (auto *Field2 : RD2->fields()) 13571 UnmatchedFields.insert(Field2); 13572 13573 for (auto *Field1 : RD1->fields()) { 13574 llvm::SmallPtrSet<FieldDecl *, 8>::iterator 13575 I = UnmatchedFields.begin(), 13576 E = UnmatchedFields.end(); 13577 13578 for ( ; I != E; ++I) { 13579 if (isLayoutCompatible(C, Field1, *I)) { 13580 bool Result = UnmatchedFields.erase(*I); 13581 (void) Result; 13582 assert(Result); 13583 break; 13584 } 13585 } 13586 if (I == E) 13587 return false; 13588 } 13589 13590 return UnmatchedFields.empty(); 13591 } 13592 13593 static bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, 13594 RecordDecl *RD2) { 13595 if (RD1->isUnion() != RD2->isUnion()) 13596 return false; 13597 13598 if (RD1->isUnion()) 13599 return isLayoutCompatibleUnion(C, RD1, RD2); 13600 else 13601 return isLayoutCompatibleStruct(C, RD1, RD2); 13602 } 13603 13604 /// Check if two types are layout-compatible in C++11 sense. 13605 static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { 13606 if (T1.isNull() || T2.isNull()) 13607 return false; 13608 13609 // C++11 [basic.types] p11: 13610 // If two types T1 and T2 are the same type, then T1 and T2 are 13611 // layout-compatible types. 13612 if (C.hasSameType(T1, T2)) 13613 return true; 13614 13615 T1 = T1.getCanonicalType().getUnqualifiedType(); 13616 T2 = T2.getCanonicalType().getUnqualifiedType(); 13617 13618 const Type::TypeClass TC1 = T1->getTypeClass(); 13619 const Type::TypeClass TC2 = T2->getTypeClass(); 13620 13621 if (TC1 != TC2) 13622 return false; 13623 13624 if (TC1 == Type::Enum) { 13625 return isLayoutCompatible(C, 13626 cast<EnumType>(T1)->getDecl(), 13627 cast<EnumType>(T2)->getDecl()); 13628 } else if (TC1 == Type::Record) { 13629 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) 13630 return false; 13631 13632 return isLayoutCompatible(C, 13633 cast<RecordType>(T1)->getDecl(), 13634 cast<RecordType>(T2)->getDecl()); 13635 } 13636 13637 return false; 13638 } 13639 13640 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// 13641 13642 /// Given a type tag expression find the type tag itself. 13643 /// 13644 /// \param TypeExpr Type tag expression, as it appears in user's code. 13645 /// 13646 /// \param VD Declaration of an identifier that appears in a type tag. 13647 /// 13648 /// \param MagicValue Type tag magic value. 13649 static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, 13650 const ValueDecl **VD, uint64_t *MagicValue) { 13651 while(true) { 13652 if (!TypeExpr) 13653 return false; 13654 13655 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); 13656 13657 switch (TypeExpr->getStmtClass()) { 13658 case Stmt::UnaryOperatorClass: { 13659 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); 13660 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { 13661 TypeExpr = UO->getSubExpr(); 13662 continue; 13663 } 13664 return false; 13665 } 13666 13667 case Stmt::DeclRefExprClass: { 13668 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); 13669 *VD = DRE->getDecl(); 13670 return true; 13671 } 13672 13673 case Stmt::IntegerLiteralClass: { 13674 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); 13675 llvm::APInt MagicValueAPInt = IL->getValue(); 13676 if (MagicValueAPInt.getActiveBits() <= 64) { 13677 *MagicValue = MagicValueAPInt.getZExtValue(); 13678 return true; 13679 } else 13680 return false; 13681 } 13682 13683 case Stmt::BinaryConditionalOperatorClass: 13684 case Stmt::ConditionalOperatorClass: { 13685 const AbstractConditionalOperator *ACO = 13686 cast<AbstractConditionalOperator>(TypeExpr); 13687 bool Result; 13688 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { 13689 if (Result) 13690 TypeExpr = ACO->getTrueExpr(); 13691 else 13692 TypeExpr = ACO->getFalseExpr(); 13693 continue; 13694 } 13695 return false; 13696 } 13697 13698 case Stmt::BinaryOperatorClass: { 13699 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); 13700 if (BO->getOpcode() == BO_Comma) { 13701 TypeExpr = BO->getRHS(); 13702 continue; 13703 } 13704 return false; 13705 } 13706 13707 default: 13708 return false; 13709 } 13710 } 13711 } 13712 13713 /// Retrieve the C type corresponding to type tag TypeExpr. 13714 /// 13715 /// \param TypeExpr Expression that specifies a type tag. 13716 /// 13717 /// \param MagicValues Registered magic values. 13718 /// 13719 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong 13720 /// kind. 13721 /// 13722 /// \param TypeInfo Information about the corresponding C type. 13723 /// 13724 /// \returns true if the corresponding C type was found. 13725 static bool GetMatchingCType( 13726 const IdentifierInfo *ArgumentKind, 13727 const Expr *TypeExpr, const ASTContext &Ctx, 13728 const llvm::DenseMap<Sema::TypeTagMagicValue, 13729 Sema::TypeTagData> *MagicValues, 13730 bool &FoundWrongKind, 13731 Sema::TypeTagData &TypeInfo) { 13732 FoundWrongKind = false; 13733 13734 // Variable declaration that has type_tag_for_datatype attribute. 13735 const ValueDecl *VD = nullptr; 13736 13737 uint64_t MagicValue; 13738 13739 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) 13740 return false; 13741 13742 if (VD) { 13743 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { 13744 if (I->getArgumentKind() != ArgumentKind) { 13745 FoundWrongKind = true; 13746 return false; 13747 } 13748 TypeInfo.Type = I->getMatchingCType(); 13749 TypeInfo.LayoutCompatible = I->getLayoutCompatible(); 13750 TypeInfo.MustBeNull = I->getMustBeNull(); 13751 return true; 13752 } 13753 return false; 13754 } 13755 13756 if (!MagicValues) 13757 return false; 13758 13759 llvm::DenseMap<Sema::TypeTagMagicValue, 13760 Sema::TypeTagData>::const_iterator I = 13761 MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); 13762 if (I == MagicValues->end()) 13763 return false; 13764 13765 TypeInfo = I->second; 13766 return true; 13767 } 13768 13769 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, 13770 uint64_t MagicValue, QualType Type, 13771 bool LayoutCompatible, 13772 bool MustBeNull) { 13773 if (!TypeTagForDatatypeMagicValues) 13774 TypeTagForDatatypeMagicValues.reset( 13775 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); 13776 13777 TypeTagMagicValue Magic(ArgumentKind, MagicValue); 13778 (*TypeTagForDatatypeMagicValues)[Magic] = 13779 TypeTagData(Type, LayoutCompatible, MustBeNull); 13780 } 13781 13782 static bool IsSameCharType(QualType T1, QualType T2) { 13783 const BuiltinType *BT1 = T1->getAs<BuiltinType>(); 13784 if (!BT1) 13785 return false; 13786 13787 const BuiltinType *BT2 = T2->getAs<BuiltinType>(); 13788 if (!BT2) 13789 return false; 13790 13791 BuiltinType::Kind T1Kind = BT1->getKind(); 13792 BuiltinType::Kind T2Kind = BT2->getKind(); 13793 13794 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || 13795 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || 13796 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || 13797 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); 13798 } 13799 13800 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, 13801 const ArrayRef<const Expr *> ExprArgs, 13802 SourceLocation CallSiteLoc) { 13803 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); 13804 bool IsPointerAttr = Attr->getIsPointer(); 13805 13806 // Retrieve the argument representing the 'type_tag'. 13807 unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex(); 13808 if (TypeTagIdxAST >= ExprArgs.size()) { 13809 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 13810 << 0 << Attr->getTypeTagIdx().getSourceIndex(); 13811 return; 13812 } 13813 const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST]; 13814 bool FoundWrongKind; 13815 TypeTagData TypeInfo; 13816 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, 13817 TypeTagForDatatypeMagicValues.get(), 13818 FoundWrongKind, TypeInfo)) { 13819 if (FoundWrongKind) 13820 Diag(TypeTagExpr->getExprLoc(), 13821 diag::warn_type_tag_for_datatype_wrong_kind) 13822 << TypeTagExpr->getSourceRange(); 13823 return; 13824 } 13825 13826 // Retrieve the argument representing the 'arg_idx'. 13827 unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex(); 13828 if (ArgumentIdxAST >= ExprArgs.size()) { 13829 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 13830 << 1 << Attr->getArgumentIdx().getSourceIndex(); 13831 return; 13832 } 13833 const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST]; 13834 if (IsPointerAttr) { 13835 // Skip implicit cast of pointer to `void *' (as a function argument). 13836 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) 13837 if (ICE->getType()->isVoidPointerType() && 13838 ICE->getCastKind() == CK_BitCast) 13839 ArgumentExpr = ICE->getSubExpr(); 13840 } 13841 QualType ArgumentType = ArgumentExpr->getType(); 13842 13843 // Passing a `void*' pointer shouldn't trigger a warning. 13844 if (IsPointerAttr && ArgumentType->isVoidPointerType()) 13845 return; 13846 13847 if (TypeInfo.MustBeNull) { 13848 // Type tag with matching void type requires a null pointer. 13849 if (!ArgumentExpr->isNullPointerConstant(Context, 13850 Expr::NPC_ValueDependentIsNotNull)) { 13851 Diag(ArgumentExpr->getExprLoc(), 13852 diag::warn_type_safety_null_pointer_required) 13853 << ArgumentKind->getName() 13854 << ArgumentExpr->getSourceRange() 13855 << TypeTagExpr->getSourceRange(); 13856 } 13857 return; 13858 } 13859 13860 QualType RequiredType = TypeInfo.Type; 13861 if (IsPointerAttr) 13862 RequiredType = Context.getPointerType(RequiredType); 13863 13864 bool mismatch = false; 13865 if (!TypeInfo.LayoutCompatible) { 13866 mismatch = !Context.hasSameType(ArgumentType, RequiredType); 13867 13868 // C++11 [basic.fundamental] p1: 13869 // Plain char, signed char, and unsigned char are three distinct types. 13870 // 13871 // But we treat plain `char' as equivalent to `signed char' or `unsigned 13872 // char' depending on the current char signedness mode. 13873 if (mismatch) 13874 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), 13875 RequiredType->getPointeeType())) || 13876 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) 13877 mismatch = false; 13878 } else 13879 if (IsPointerAttr) 13880 mismatch = !isLayoutCompatible(Context, 13881 ArgumentType->getPointeeType(), 13882 RequiredType->getPointeeType()); 13883 else 13884 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); 13885 13886 if (mismatch) 13887 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) 13888 << ArgumentType << ArgumentKind 13889 << TypeInfo.LayoutCompatible << RequiredType 13890 << ArgumentExpr->getSourceRange() 13891 << TypeTagExpr->getSourceRange(); 13892 } 13893 13894 void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD, 13895 CharUnits Alignment) { 13896 MisalignedMembers.emplace_back(E, RD, MD, Alignment); 13897 } 13898 13899 void Sema::DiagnoseMisalignedMembers() { 13900 for (MisalignedMember &m : MisalignedMembers) { 13901 const NamedDecl *ND = m.RD; 13902 if (ND->getName().empty()) { 13903 if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl()) 13904 ND = TD; 13905 } 13906 Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member) 13907 << m.MD << ND << m.E->getSourceRange(); 13908 } 13909 MisalignedMembers.clear(); 13910 } 13911 13912 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) { 13913 E = E->IgnoreParens(); 13914 if (!T->isPointerType() && !T->isIntegerType()) 13915 return; 13916 if (isa<UnaryOperator>(E) && 13917 cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) { 13918 auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 13919 if (isa<MemberExpr>(Op)) { 13920 auto MA = std::find(MisalignedMembers.begin(), MisalignedMembers.end(), 13921 MisalignedMember(Op)); 13922 if (MA != MisalignedMembers.end() && 13923 (T->isIntegerType() || 13924 (T->isPointerType() && (T->getPointeeType()->isIncompleteType() || 13925 Context.getTypeAlignInChars( 13926 T->getPointeeType()) <= MA->Alignment)))) 13927 MisalignedMembers.erase(MA); 13928 } 13929 } 13930 } 13931 13932 void Sema::RefersToMemberWithReducedAlignment( 13933 Expr *E, 13934 llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)> 13935 Action) { 13936 const auto *ME = dyn_cast<MemberExpr>(E); 13937 if (!ME) 13938 return; 13939 13940 // No need to check expressions with an __unaligned-qualified type. 13941 if (E->getType().getQualifiers().hasUnaligned()) 13942 return; 13943 13944 // For a chain of MemberExpr like "a.b.c.d" this list 13945 // will keep FieldDecl's like [d, c, b]. 13946 SmallVector<FieldDecl *, 4> ReverseMemberChain; 13947 const MemberExpr *TopME = nullptr; 13948 bool AnyIsPacked = false; 13949 do { 13950 QualType BaseType = ME->getBase()->getType(); 13951 if (ME->isArrow()) 13952 BaseType = BaseType->getPointeeType(); 13953 RecordDecl *RD = BaseType->getAs<RecordType>()->getDecl(); 13954 if (RD->isInvalidDecl()) 13955 return; 13956 13957 ValueDecl *MD = ME->getMemberDecl(); 13958 auto *FD = dyn_cast<FieldDecl>(MD); 13959 // We do not care about non-data members. 13960 if (!FD || FD->isInvalidDecl()) 13961 return; 13962 13963 AnyIsPacked = 13964 AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>()); 13965 ReverseMemberChain.push_back(FD); 13966 13967 TopME = ME; 13968 ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens()); 13969 } while (ME); 13970 assert(TopME && "We did not compute a topmost MemberExpr!"); 13971 13972 // Not the scope of this diagnostic. 13973 if (!AnyIsPacked) 13974 return; 13975 13976 const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts(); 13977 const auto *DRE = dyn_cast<DeclRefExpr>(TopBase); 13978 // TODO: The innermost base of the member expression may be too complicated. 13979 // For now, just disregard these cases. This is left for future 13980 // improvement. 13981 if (!DRE && !isa<CXXThisExpr>(TopBase)) 13982 return; 13983 13984 // Alignment expected by the whole expression. 13985 CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType()); 13986 13987 // No need to do anything else with this case. 13988 if (ExpectedAlignment.isOne()) 13989 return; 13990 13991 // Synthesize offset of the whole access. 13992 CharUnits Offset; 13993 for (auto I = ReverseMemberChain.rbegin(); I != ReverseMemberChain.rend(); 13994 I++) { 13995 Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(*I)); 13996 } 13997 13998 // Compute the CompleteObjectAlignment as the alignment of the whole chain. 13999 CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars( 14000 ReverseMemberChain.back()->getParent()->getTypeForDecl()); 14001 14002 // The base expression of the innermost MemberExpr may give 14003 // stronger guarantees than the class containing the member. 14004 if (DRE && !TopME->isArrow()) { 14005 const ValueDecl *VD = DRE->getDecl(); 14006 if (!VD->getType()->isReferenceType()) 14007 CompleteObjectAlignment = 14008 std::max(CompleteObjectAlignment, Context.getDeclAlign(VD)); 14009 } 14010 14011 // Check if the synthesized offset fulfills the alignment. 14012 if (Offset % ExpectedAlignment != 0 || 14013 // It may fulfill the offset it but the effective alignment may still be 14014 // lower than the expected expression alignment. 14015 CompleteObjectAlignment < ExpectedAlignment) { 14016 // If this happens, we want to determine a sensible culprit of this. 14017 // Intuitively, watching the chain of member expressions from right to 14018 // left, we start with the required alignment (as required by the field 14019 // type) but some packed attribute in that chain has reduced the alignment. 14020 // It may happen that another packed structure increases it again. But if 14021 // we are here such increase has not been enough. So pointing the first 14022 // FieldDecl that either is packed or else its RecordDecl is, 14023 // seems reasonable. 14024 FieldDecl *FD = nullptr; 14025 CharUnits Alignment; 14026 for (FieldDecl *FDI : ReverseMemberChain) { 14027 if (FDI->hasAttr<PackedAttr>() || 14028 FDI->getParent()->hasAttr<PackedAttr>()) { 14029 FD = FDI; 14030 Alignment = std::min( 14031 Context.getTypeAlignInChars(FD->getType()), 14032 Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl())); 14033 break; 14034 } 14035 } 14036 assert(FD && "We did not find a packed FieldDecl!"); 14037 Action(E, FD->getParent(), FD, Alignment); 14038 } 14039 } 14040 14041 void Sema::CheckAddressOfPackedMember(Expr *rhs) { 14042 using namespace std::placeholders; 14043 14044 RefersToMemberWithReducedAlignment( 14045 rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1, 14046 _2, _3, _4)); 14047 } 14048