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 "CheckExprLifetime.h" 15 #include "clang/AST/APValue.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/Attr.h" 18 #include "clang/AST/AttrIterator.h" 19 #include "clang/AST/CharUnits.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclBase.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclObjC.h" 24 #include "clang/AST/DeclarationName.h" 25 #include "clang/AST/EvaluatedExprVisitor.h" 26 #include "clang/AST/Expr.h" 27 #include "clang/AST/ExprCXX.h" 28 #include "clang/AST/ExprObjC.h" 29 #include "clang/AST/FormatString.h" 30 #include "clang/AST/IgnoreExpr.h" 31 #include "clang/AST/NSAPI.h" 32 #include "clang/AST/NonTrivialTypeVisitor.h" 33 #include "clang/AST/OperationKinds.h" 34 #include "clang/AST/RecordLayout.h" 35 #include "clang/AST/Stmt.h" 36 #include "clang/AST/TemplateBase.h" 37 #include "clang/AST/Type.h" 38 #include "clang/AST/TypeLoc.h" 39 #include "clang/AST/UnresolvedSet.h" 40 #include "clang/Basic/AddressSpaces.h" 41 #include "clang/Basic/Diagnostic.h" 42 #include "clang/Basic/IdentifierTable.h" 43 #include "clang/Basic/LLVM.h" 44 #include "clang/Basic/LangOptions.h" 45 #include "clang/Basic/OpenCLOptions.h" 46 #include "clang/Basic/OperatorKinds.h" 47 #include "clang/Basic/PartialDiagnostic.h" 48 #include "clang/Basic/SourceLocation.h" 49 #include "clang/Basic/SourceManager.h" 50 #include "clang/Basic/Specifiers.h" 51 #include "clang/Basic/SyncScope.h" 52 #include "clang/Basic/TargetInfo.h" 53 #include "clang/Basic/TypeTraits.h" 54 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. 55 #include "clang/Sema/Initialization.h" 56 #include "clang/Sema/Lookup.h" 57 #include "clang/Sema/Ownership.h" 58 #include "clang/Sema/Scope.h" 59 #include "clang/Sema/ScopeInfo.h" 60 #include "clang/Sema/Sema.h" 61 #include "clang/Sema/SemaAMDGPU.h" 62 #include "clang/Sema/SemaARM.h" 63 #include "clang/Sema/SemaBPF.h" 64 #include "clang/Sema/SemaHLSL.h" 65 #include "clang/Sema/SemaHexagon.h" 66 #include "clang/Sema/SemaLoongArch.h" 67 #include "clang/Sema/SemaMIPS.h" 68 #include "clang/Sema/SemaNVPTX.h" 69 #include "clang/Sema/SemaObjC.h" 70 #include "clang/Sema/SemaOpenCL.h" 71 #include "clang/Sema/SemaPPC.h" 72 #include "clang/Sema/SemaRISCV.h" 73 #include "clang/Sema/SemaSPIRV.h" 74 #include "clang/Sema/SemaSystemZ.h" 75 #include "clang/Sema/SemaWasm.h" 76 #include "clang/Sema/SemaX86.h" 77 #include "llvm/ADT/APFloat.h" 78 #include "llvm/ADT/APInt.h" 79 #include "llvm/ADT/APSInt.h" 80 #include "llvm/ADT/ArrayRef.h" 81 #include "llvm/ADT/DenseMap.h" 82 #include "llvm/ADT/FoldingSet.h" 83 #include "llvm/ADT/STLExtras.h" 84 #include "llvm/ADT/SmallBitVector.h" 85 #include "llvm/ADT/SmallPtrSet.h" 86 #include "llvm/ADT/SmallString.h" 87 #include "llvm/ADT/SmallVector.h" 88 #include "llvm/ADT/StringExtras.h" 89 #include "llvm/ADT/StringRef.h" 90 #include "llvm/ADT/StringSet.h" 91 #include "llvm/ADT/StringSwitch.h" 92 #include "llvm/Support/AtomicOrdering.h" 93 #include "llvm/Support/Compiler.h" 94 #include "llvm/Support/ConvertUTF.h" 95 #include "llvm/Support/ErrorHandling.h" 96 #include "llvm/Support/Format.h" 97 #include "llvm/Support/Locale.h" 98 #include "llvm/Support/MathExtras.h" 99 #include "llvm/Support/SaveAndRestore.h" 100 #include "llvm/Support/raw_ostream.h" 101 #include "llvm/TargetParser/RISCVTargetParser.h" 102 #include "llvm/TargetParser/Triple.h" 103 #include <algorithm> 104 #include <cassert> 105 #include <cctype> 106 #include <cstddef> 107 #include <cstdint> 108 #include <functional> 109 #include <limits> 110 #include <optional> 111 #include <string> 112 #include <tuple> 113 #include <utility> 114 115 using namespace clang; 116 using namespace sema; 117 118 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, 119 unsigned ByteNo) const { 120 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, 121 Context.getTargetInfo()); 122 } 123 124 static constexpr unsigned short combineFAPK(Sema::FormatArgumentPassingKind A, 125 Sema::FormatArgumentPassingKind B) { 126 return (A << 8) | B; 127 } 128 129 bool Sema::checkArgCountAtLeast(CallExpr *Call, unsigned MinArgCount) { 130 unsigned ArgCount = Call->getNumArgs(); 131 if (ArgCount >= MinArgCount) 132 return false; 133 134 return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_few_args) 135 << 0 /*function call*/ << MinArgCount << ArgCount 136 << /*is non object*/ 0 << Call->getSourceRange(); 137 } 138 139 bool Sema::checkArgCountAtMost(CallExpr *Call, unsigned MaxArgCount) { 140 unsigned ArgCount = Call->getNumArgs(); 141 if (ArgCount <= MaxArgCount) 142 return false; 143 return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_many_args_at_most) 144 << 0 /*function call*/ << MaxArgCount << ArgCount 145 << /*is non object*/ 0 << Call->getSourceRange(); 146 } 147 148 bool Sema::checkArgCountRange(CallExpr *Call, unsigned MinArgCount, 149 unsigned MaxArgCount) { 150 return checkArgCountAtLeast(Call, MinArgCount) || 151 checkArgCountAtMost(Call, MaxArgCount); 152 } 153 154 bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) { 155 unsigned ArgCount = Call->getNumArgs(); 156 if (ArgCount == DesiredArgCount) 157 return false; 158 159 if (checkArgCountAtLeast(Call, DesiredArgCount)) 160 return true; 161 assert(ArgCount > DesiredArgCount && "should have diagnosed this"); 162 163 // Highlight all the excess arguments. 164 SourceRange Range(Call->getArg(DesiredArgCount)->getBeginLoc(), 165 Call->getArg(ArgCount - 1)->getEndLoc()); 166 167 return Diag(Range.getBegin(), diag::err_typecheck_call_too_many_args) 168 << 0 /*function call*/ << DesiredArgCount << ArgCount 169 << /*is non object*/ 0 << Call->getArg(1)->getSourceRange(); 170 } 171 172 static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) { 173 bool HasError = false; 174 175 for (unsigned I = 0; I < Call->getNumArgs(); ++I) { 176 Expr *Arg = Call->getArg(I); 177 178 if (Arg->isValueDependent()) 179 continue; 180 181 std::optional<std::string> ArgString = Arg->tryEvaluateString(S.Context); 182 int DiagMsgKind = -1; 183 // Arguments must be pointers to constant strings and cannot use '$'. 184 if (!ArgString.has_value()) 185 DiagMsgKind = 0; 186 else if (ArgString->find('$') != std::string::npos) 187 DiagMsgKind = 1; 188 189 if (DiagMsgKind >= 0) { 190 S.Diag(Arg->getBeginLoc(), diag::err_builtin_verbose_trap_arg) 191 << DiagMsgKind << Arg->getSourceRange(); 192 HasError = true; 193 } 194 } 195 196 return !HasError; 197 } 198 199 static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) { 200 if (Value->isTypeDependent()) 201 return false; 202 203 InitializedEntity Entity = 204 InitializedEntity::InitializeParameter(S.Context, Ty, false); 205 ExprResult Result = 206 S.PerformCopyInitialization(Entity, SourceLocation(), Value); 207 if (Result.isInvalid()) 208 return true; 209 Value = Result.get(); 210 return false; 211 } 212 213 /// Check that the first argument to __builtin_annotation is an integer 214 /// and the second argument is a non-wide string literal. 215 static bool BuiltinAnnotation(Sema &S, CallExpr *TheCall) { 216 if (S.checkArgCount(TheCall, 2)) 217 return true; 218 219 // First argument should be an integer. 220 Expr *ValArg = TheCall->getArg(0); 221 QualType Ty = ValArg->getType(); 222 if (!Ty->isIntegerType()) { 223 S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg) 224 << ValArg->getSourceRange(); 225 return true; 226 } 227 228 // Second argument should be a constant string. 229 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); 230 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); 231 if (!Literal || !Literal->isOrdinary()) { 232 S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg) 233 << StrArg->getSourceRange(); 234 return true; 235 } 236 237 TheCall->setType(Ty); 238 return false; 239 } 240 241 static bool BuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) { 242 // We need at least one argument. 243 if (TheCall->getNumArgs() < 1) { 244 S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 245 << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 246 << TheCall->getCallee()->getSourceRange(); 247 return true; 248 } 249 250 // All arguments should be wide string literals. 251 for (Expr *Arg : TheCall->arguments()) { 252 auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 253 if (!Literal || !Literal->isWide()) { 254 S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str) 255 << Arg->getSourceRange(); 256 return true; 257 } 258 } 259 260 return false; 261 } 262 263 /// Check that the argument to __builtin_addressof is a glvalue, and set the 264 /// result type to the corresponding pointer type. 265 static bool BuiltinAddressof(Sema &S, CallExpr *TheCall) { 266 if (S.checkArgCount(TheCall, 1)) 267 return true; 268 269 ExprResult Arg(TheCall->getArg(0)); 270 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc()); 271 if (ResultType.isNull()) 272 return true; 273 274 TheCall->setArg(0, Arg.get()); 275 TheCall->setType(ResultType); 276 return false; 277 } 278 279 /// Check that the argument to __builtin_function_start is a function. 280 static bool BuiltinFunctionStart(Sema &S, CallExpr *TheCall) { 281 if (S.checkArgCount(TheCall, 1)) 282 return true; 283 284 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 285 if (Arg.isInvalid()) 286 return true; 287 288 TheCall->setArg(0, Arg.get()); 289 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>( 290 Arg.get()->getAsBuiltinConstantDeclRef(S.getASTContext())); 291 292 if (!FD) { 293 S.Diag(TheCall->getBeginLoc(), diag::err_function_start_invalid_type) 294 << TheCall->getSourceRange(); 295 return true; 296 } 297 298 return !S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, 299 TheCall->getBeginLoc()); 300 } 301 302 /// Check the number of arguments and set the result type to 303 /// the argument type. 304 static bool BuiltinPreserveAI(Sema &S, CallExpr *TheCall) { 305 if (S.checkArgCount(TheCall, 1)) 306 return true; 307 308 TheCall->setType(TheCall->getArg(0)->getType()); 309 return false; 310 } 311 312 /// Check that the value argument for __builtin_is_aligned(value, alignment) and 313 /// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer 314 /// type (but not a function pointer) and that the alignment is a power-of-two. 315 static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) { 316 if (S.checkArgCount(TheCall, 2)) 317 return true; 318 319 clang::Expr *Source = TheCall->getArg(0); 320 bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned; 321 322 auto IsValidIntegerType = [](QualType Ty) { 323 return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType(); 324 }; 325 QualType SrcTy = Source->getType(); 326 // We should also be able to use it with arrays (but not functions!). 327 if (SrcTy->canDecayToPointerType() && SrcTy->isArrayType()) { 328 SrcTy = S.Context.getDecayedType(SrcTy); 329 } 330 if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) || 331 SrcTy->isFunctionPointerType()) { 332 // FIXME: this is not quite the right error message since we don't allow 333 // floating point types, or member pointers. 334 S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand) 335 << SrcTy; 336 return true; 337 } 338 339 clang::Expr *AlignOp = TheCall->getArg(1); 340 if (!IsValidIntegerType(AlignOp->getType())) { 341 S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int) 342 << AlignOp->getType(); 343 return true; 344 } 345 Expr::EvalResult AlignResult; 346 unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1; 347 // We can't check validity of alignment if it is value dependent. 348 if (!AlignOp->isValueDependent() && 349 AlignOp->EvaluateAsInt(AlignResult, S.Context, 350 Expr::SE_AllowSideEffects)) { 351 llvm::APSInt AlignValue = AlignResult.Val.getInt(); 352 llvm::APSInt MaxValue( 353 llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits)); 354 if (AlignValue < 1) { 355 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1; 356 return true; 357 } 358 if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) { 359 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big) 360 << toString(MaxValue, 10); 361 return true; 362 } 363 if (!AlignValue.isPowerOf2()) { 364 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two); 365 return true; 366 } 367 if (AlignValue == 1) { 368 S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless) 369 << IsBooleanAlignBuiltin; 370 } 371 } 372 373 ExprResult SrcArg = S.PerformCopyInitialization( 374 InitializedEntity::InitializeParameter(S.Context, SrcTy, false), 375 SourceLocation(), Source); 376 if (SrcArg.isInvalid()) 377 return true; 378 TheCall->setArg(0, SrcArg.get()); 379 ExprResult AlignArg = 380 S.PerformCopyInitialization(InitializedEntity::InitializeParameter( 381 S.Context, AlignOp->getType(), false), 382 SourceLocation(), AlignOp); 383 if (AlignArg.isInvalid()) 384 return true; 385 TheCall->setArg(1, AlignArg.get()); 386 // For align_up/align_down, the return type is the same as the (potentially 387 // decayed) argument type including qualifiers. For is_aligned(), the result 388 // is always bool. 389 TheCall->setType(IsBooleanAlignBuiltin ? S.Context.BoolTy : SrcTy); 390 return false; 391 } 392 393 static bool BuiltinOverflow(Sema &S, CallExpr *TheCall, unsigned BuiltinID) { 394 if (S.checkArgCount(TheCall, 3)) 395 return true; 396 397 std::pair<unsigned, const char *> Builtins[] = { 398 { Builtin::BI__builtin_add_overflow, "ckd_add" }, 399 { Builtin::BI__builtin_sub_overflow, "ckd_sub" }, 400 { Builtin::BI__builtin_mul_overflow, "ckd_mul" }, 401 }; 402 403 bool CkdOperation = llvm::any_of(Builtins, [&](const std::pair<unsigned, 404 const char *> &P) { 405 return BuiltinID == P.first && TheCall->getExprLoc().isMacroID() && 406 Lexer::getImmediateMacroName(TheCall->getExprLoc(), 407 S.getSourceManager(), S.getLangOpts()) == P.second; 408 }); 409 410 auto ValidCkdIntType = [](QualType QT) { 411 // A valid checked integer type is an integer type other than a plain char, 412 // bool, a bit-precise type, or an enumeration type. 413 if (const auto *BT = QT.getCanonicalType()->getAs<BuiltinType>()) 414 return (BT->getKind() >= BuiltinType::Short && 415 BT->getKind() <= BuiltinType::Int128) || ( 416 BT->getKind() >= BuiltinType::UShort && 417 BT->getKind() <= BuiltinType::UInt128) || 418 BT->getKind() == BuiltinType::UChar || 419 BT->getKind() == BuiltinType::SChar; 420 return false; 421 }; 422 423 // First two arguments should be integers. 424 for (unsigned I = 0; I < 2; ++I) { 425 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I)); 426 if (Arg.isInvalid()) return true; 427 TheCall->setArg(I, Arg.get()); 428 429 QualType Ty = Arg.get()->getType(); 430 bool IsValid = CkdOperation ? ValidCkdIntType(Ty) : Ty->isIntegerType(); 431 if (!IsValid) { 432 S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int) 433 << CkdOperation << Ty << Arg.get()->getSourceRange(); 434 return true; 435 } 436 } 437 438 // Third argument should be a pointer to a non-const integer. 439 // IRGen correctly handles volatile, restrict, and address spaces, and 440 // the other qualifiers aren't possible. 441 { 442 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2)); 443 if (Arg.isInvalid()) return true; 444 TheCall->setArg(2, Arg.get()); 445 446 QualType Ty = Arg.get()->getType(); 447 const auto *PtrTy = Ty->getAs<PointerType>(); 448 if (!PtrTy || 449 !PtrTy->getPointeeType()->isIntegerType() || 450 (!ValidCkdIntType(PtrTy->getPointeeType()) && CkdOperation) || 451 PtrTy->getPointeeType().isConstQualified()) { 452 S.Diag(Arg.get()->getBeginLoc(), 453 diag::err_overflow_builtin_must_be_ptr_int) 454 << CkdOperation << Ty << Arg.get()->getSourceRange(); 455 return true; 456 } 457 } 458 459 // Disallow signed bit-precise integer args larger than 128 bits to mul 460 // function until we improve backend support. 461 if (BuiltinID == Builtin::BI__builtin_mul_overflow) { 462 for (unsigned I = 0; I < 3; ++I) { 463 const auto Arg = TheCall->getArg(I); 464 // Third argument will be a pointer. 465 auto Ty = I < 2 ? Arg->getType() : Arg->getType()->getPointeeType(); 466 if (Ty->isBitIntType() && Ty->isSignedIntegerType() && 467 S.getASTContext().getIntWidth(Ty) > 128) 468 return S.Diag(Arg->getBeginLoc(), 469 diag::err_overflow_builtin_bit_int_max_size) 470 << 128; 471 } 472 } 473 474 return false; 475 } 476 477 namespace { 478 struct BuiltinDumpStructGenerator { 479 Sema &S; 480 CallExpr *TheCall; 481 SourceLocation Loc = TheCall->getBeginLoc(); 482 SmallVector<Expr *, 32> Actions; 483 DiagnosticErrorTrap ErrorTracker; 484 PrintingPolicy Policy; 485 486 BuiltinDumpStructGenerator(Sema &S, CallExpr *TheCall) 487 : S(S), TheCall(TheCall), ErrorTracker(S.getDiagnostics()), 488 Policy(S.Context.getPrintingPolicy()) { 489 Policy.AnonymousTagLocations = false; 490 } 491 492 Expr *makeOpaqueValueExpr(Expr *Inner) { 493 auto *OVE = new (S.Context) 494 OpaqueValueExpr(Loc, Inner->getType(), Inner->getValueKind(), 495 Inner->getObjectKind(), Inner); 496 Actions.push_back(OVE); 497 return OVE; 498 } 499 500 Expr *getStringLiteral(llvm::StringRef Str) { 501 Expr *Lit = S.Context.getPredefinedStringLiteralFromCache(Str); 502 // Wrap the literal in parentheses to attach a source location. 503 return new (S.Context) ParenExpr(Loc, Loc, Lit); 504 } 505 506 bool callPrintFunction(llvm::StringRef Format, 507 llvm::ArrayRef<Expr *> Exprs = {}) { 508 SmallVector<Expr *, 8> Args; 509 assert(TheCall->getNumArgs() >= 2); 510 Args.reserve((TheCall->getNumArgs() - 2) + /*Format*/ 1 + Exprs.size()); 511 Args.assign(TheCall->arg_begin() + 2, TheCall->arg_end()); 512 Args.push_back(getStringLiteral(Format)); 513 Args.insert(Args.end(), Exprs.begin(), Exprs.end()); 514 515 // Register a note to explain why we're performing the call. 516 Sema::CodeSynthesisContext Ctx; 517 Ctx.Kind = Sema::CodeSynthesisContext::BuildingBuiltinDumpStructCall; 518 Ctx.PointOfInstantiation = Loc; 519 Ctx.CallArgs = Args.data(); 520 Ctx.NumCallArgs = Args.size(); 521 S.pushCodeSynthesisContext(Ctx); 522 523 ExprResult RealCall = 524 S.BuildCallExpr(/*Scope=*/nullptr, TheCall->getArg(1), 525 TheCall->getBeginLoc(), Args, TheCall->getRParenLoc()); 526 527 S.popCodeSynthesisContext(); 528 if (!RealCall.isInvalid()) 529 Actions.push_back(RealCall.get()); 530 // Bail out if we've hit any errors, even if we managed to build the 531 // call. We don't want to produce more than one error. 532 return RealCall.isInvalid() || ErrorTracker.hasErrorOccurred(); 533 } 534 535 Expr *getIndentString(unsigned Depth) { 536 if (!Depth) 537 return nullptr; 538 539 llvm::SmallString<32> Indent; 540 Indent.resize(Depth * Policy.Indentation, ' '); 541 return getStringLiteral(Indent); 542 } 543 544 Expr *getTypeString(QualType T) { 545 return getStringLiteral(T.getAsString(Policy)); 546 } 547 548 bool appendFormatSpecifier(QualType T, llvm::SmallVectorImpl<char> &Str) { 549 llvm::raw_svector_ostream OS(Str); 550 551 // Format 'bool', 'char', 'signed char', 'unsigned char' as numbers, rather 552 // than trying to print a single character. 553 if (auto *BT = T->getAs<BuiltinType>()) { 554 switch (BT->getKind()) { 555 case BuiltinType::Bool: 556 OS << "%d"; 557 return true; 558 case BuiltinType::Char_U: 559 case BuiltinType::UChar: 560 OS << "%hhu"; 561 return true; 562 case BuiltinType::Char_S: 563 case BuiltinType::SChar: 564 OS << "%hhd"; 565 return true; 566 default: 567 break; 568 } 569 } 570 571 analyze_printf::PrintfSpecifier Specifier; 572 if (Specifier.fixType(T, S.getLangOpts(), S.Context, /*IsObjCLiteral=*/false)) { 573 // We were able to guess how to format this. 574 if (Specifier.getConversionSpecifier().getKind() == 575 analyze_printf::PrintfConversionSpecifier::sArg) { 576 // Wrap double-quotes around a '%s' specifier and limit its maximum 577 // length. Ideally we'd also somehow escape special characters in the 578 // contents but printf doesn't support that. 579 // FIXME: '%s' formatting is not safe in general. 580 OS << '"'; 581 Specifier.setPrecision(analyze_printf::OptionalAmount(32u)); 582 Specifier.toString(OS); 583 OS << '"'; 584 // FIXME: It would be nice to include a '...' if the string doesn't fit 585 // in the length limit. 586 } else { 587 Specifier.toString(OS); 588 } 589 return true; 590 } 591 592 if (T->isPointerType()) { 593 // Format all pointers with '%p'. 594 OS << "%p"; 595 return true; 596 } 597 598 return false; 599 } 600 601 bool dumpUnnamedRecord(const RecordDecl *RD, Expr *E, unsigned Depth) { 602 Expr *IndentLit = getIndentString(Depth); 603 Expr *TypeLit = getTypeString(S.Context.getRecordType(RD)); 604 if (IndentLit ? callPrintFunction("%s%s", {IndentLit, TypeLit}) 605 : callPrintFunction("%s", {TypeLit})) 606 return true; 607 608 return dumpRecordValue(RD, E, IndentLit, Depth); 609 } 610 611 // Dump a record value. E should be a pointer or lvalue referring to an RD. 612 bool dumpRecordValue(const RecordDecl *RD, Expr *E, Expr *RecordIndent, 613 unsigned Depth) { 614 // FIXME: Decide what to do if RD is a union. At least we should probably 615 // turn off printing `const char*` members with `%s`, because that is very 616 // likely to crash if that's not the active member. Whatever we decide, we 617 // should document it. 618 619 // Build an OpaqueValueExpr so we can refer to E more than once without 620 // triggering re-evaluation. 621 Expr *RecordArg = makeOpaqueValueExpr(E); 622 bool RecordArgIsPtr = RecordArg->getType()->isPointerType(); 623 624 if (callPrintFunction(" {\n")) 625 return true; 626 627 // Dump each base class, regardless of whether they're aggregates. 628 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 629 for (const auto &Base : CXXRD->bases()) { 630 QualType BaseType = 631 RecordArgIsPtr ? S.Context.getPointerType(Base.getType()) 632 : S.Context.getLValueReferenceType(Base.getType()); 633 ExprResult BasePtr = S.BuildCStyleCastExpr( 634 Loc, S.Context.getTrivialTypeSourceInfo(BaseType, Loc), Loc, 635 RecordArg); 636 if (BasePtr.isInvalid() || 637 dumpUnnamedRecord(Base.getType()->getAsRecordDecl(), BasePtr.get(), 638 Depth + 1)) 639 return true; 640 } 641 } 642 643 Expr *FieldIndentArg = getIndentString(Depth + 1); 644 645 // Dump each field. 646 for (auto *D : RD->decls()) { 647 auto *IFD = dyn_cast<IndirectFieldDecl>(D); 648 auto *FD = IFD ? IFD->getAnonField() : dyn_cast<FieldDecl>(D); 649 if (!FD || FD->isUnnamedBitField() || FD->isAnonymousStructOrUnion()) 650 continue; 651 652 llvm::SmallString<20> Format = llvm::StringRef("%s%s %s "); 653 llvm::SmallVector<Expr *, 5> Args = {FieldIndentArg, 654 getTypeString(FD->getType()), 655 getStringLiteral(FD->getName())}; 656 657 if (FD->isBitField()) { 658 Format += ": %zu "; 659 QualType SizeT = S.Context.getSizeType(); 660 llvm::APInt BitWidth(S.Context.getIntWidth(SizeT), 661 FD->getBitWidthValue(S.Context)); 662 Args.push_back(IntegerLiteral::Create(S.Context, BitWidth, SizeT, Loc)); 663 } 664 665 Format += "="; 666 667 ExprResult Field = 668 IFD ? S.BuildAnonymousStructUnionMemberReference( 669 CXXScopeSpec(), Loc, IFD, 670 DeclAccessPair::make(IFD, AS_public), RecordArg, Loc) 671 : S.BuildFieldReferenceExpr( 672 RecordArg, RecordArgIsPtr, Loc, CXXScopeSpec(), FD, 673 DeclAccessPair::make(FD, AS_public), 674 DeclarationNameInfo(FD->getDeclName(), Loc)); 675 if (Field.isInvalid()) 676 return true; 677 678 auto *InnerRD = FD->getType()->getAsRecordDecl(); 679 auto *InnerCXXRD = dyn_cast_or_null<CXXRecordDecl>(InnerRD); 680 if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) { 681 // Recursively print the values of members of aggregate record type. 682 if (callPrintFunction(Format, Args) || 683 dumpRecordValue(InnerRD, Field.get(), FieldIndentArg, Depth + 1)) 684 return true; 685 } else { 686 Format += " "; 687 if (appendFormatSpecifier(FD->getType(), Format)) { 688 // We know how to print this field. 689 Args.push_back(Field.get()); 690 } else { 691 // We don't know how to print this field. Print out its address 692 // with a format specifier that a smart tool will be able to 693 // recognize and treat specially. 694 Format += "*%p"; 695 ExprResult FieldAddr = 696 S.BuildUnaryOp(nullptr, Loc, UO_AddrOf, Field.get()); 697 if (FieldAddr.isInvalid()) 698 return true; 699 Args.push_back(FieldAddr.get()); 700 } 701 Format += "\n"; 702 if (callPrintFunction(Format, Args)) 703 return true; 704 } 705 } 706 707 return RecordIndent ? callPrintFunction("%s}\n", RecordIndent) 708 : callPrintFunction("}\n"); 709 } 710 711 Expr *buildWrapper() { 712 auto *Wrapper = PseudoObjectExpr::Create(S.Context, TheCall, Actions, 713 PseudoObjectExpr::NoResult); 714 TheCall->setType(Wrapper->getType()); 715 TheCall->setValueKind(Wrapper->getValueKind()); 716 return Wrapper; 717 } 718 }; 719 } // namespace 720 721 static ExprResult BuiltinDumpStruct(Sema &S, CallExpr *TheCall) { 722 if (S.checkArgCountAtLeast(TheCall, 2)) 723 return ExprError(); 724 725 ExprResult PtrArgResult = S.DefaultLvalueConversion(TheCall->getArg(0)); 726 if (PtrArgResult.isInvalid()) 727 return ExprError(); 728 TheCall->setArg(0, PtrArgResult.get()); 729 730 // First argument should be a pointer to a struct. 731 QualType PtrArgType = PtrArgResult.get()->getType(); 732 if (!PtrArgType->isPointerType() || 733 !PtrArgType->getPointeeType()->isRecordType()) { 734 S.Diag(PtrArgResult.get()->getBeginLoc(), 735 diag::err_expected_struct_pointer_argument) 736 << 1 << TheCall->getDirectCallee() << PtrArgType; 737 return ExprError(); 738 } 739 QualType Pointee = PtrArgType->getPointeeType(); 740 const RecordDecl *RD = Pointee->getAsRecordDecl(); 741 // Try to instantiate the class template as appropriate; otherwise, access to 742 // its data() may lead to a crash. 743 if (S.RequireCompleteType(PtrArgResult.get()->getBeginLoc(), Pointee, 744 diag::err_incomplete_type)) 745 return ExprError(); 746 // Second argument is a callable, but we can't fully validate it until we try 747 // calling it. 748 QualType FnArgType = TheCall->getArg(1)->getType(); 749 if (!FnArgType->isFunctionType() && !FnArgType->isFunctionPointerType() && 750 !FnArgType->isBlockPointerType() && 751 !(S.getLangOpts().CPlusPlus && FnArgType->isRecordType())) { 752 auto *BT = FnArgType->getAs<BuiltinType>(); 753 switch (BT ? BT->getKind() : BuiltinType::Void) { 754 case BuiltinType::Dependent: 755 case BuiltinType::Overload: 756 case BuiltinType::BoundMember: 757 case BuiltinType::PseudoObject: 758 case BuiltinType::UnknownAny: 759 case BuiltinType::BuiltinFn: 760 // This might be a callable. 761 break; 762 763 default: 764 S.Diag(TheCall->getArg(1)->getBeginLoc(), 765 diag::err_expected_callable_argument) 766 << 2 << TheCall->getDirectCallee() << FnArgType; 767 return ExprError(); 768 } 769 } 770 771 BuiltinDumpStructGenerator Generator(S, TheCall); 772 773 // Wrap parentheses around the given pointer. This is not necessary for 774 // correct code generation, but it means that when we pretty-print the call 775 // arguments in our diagnostics we will produce '(&s)->n' instead of the 776 // incorrect '&s->n'. 777 Expr *PtrArg = PtrArgResult.get(); 778 PtrArg = new (S.Context) 779 ParenExpr(PtrArg->getBeginLoc(), 780 S.getLocForEndOfToken(PtrArg->getEndLoc()), PtrArg); 781 if (Generator.dumpUnnamedRecord(RD, PtrArg, 0)) 782 return ExprError(); 783 784 return Generator.buildWrapper(); 785 } 786 787 static bool BuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { 788 if (S.checkArgCount(BuiltinCall, 2)) 789 return true; 790 791 SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc(); 792 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); 793 Expr *Call = BuiltinCall->getArg(0); 794 Expr *Chain = BuiltinCall->getArg(1); 795 796 if (Call->getStmtClass() != Stmt::CallExprClass) { 797 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) 798 << Call->getSourceRange(); 799 return true; 800 } 801 802 auto CE = cast<CallExpr>(Call); 803 if (CE->getCallee()->getType()->isBlockPointerType()) { 804 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) 805 << Call->getSourceRange(); 806 return true; 807 } 808 809 const Decl *TargetDecl = CE->getCalleeDecl(); 810 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) 811 if (FD->getBuiltinID()) { 812 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) 813 << Call->getSourceRange(); 814 return true; 815 } 816 817 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { 818 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) 819 << Call->getSourceRange(); 820 return true; 821 } 822 823 ExprResult ChainResult = S.UsualUnaryConversions(Chain); 824 if (ChainResult.isInvalid()) 825 return true; 826 if (!ChainResult.get()->getType()->isPointerType()) { 827 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) 828 << Chain->getSourceRange(); 829 return true; 830 } 831 832 QualType ReturnTy = CE->getCallReturnType(S.Context); 833 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; 834 QualType BuiltinTy = S.Context.getFunctionType( 835 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); 836 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); 837 838 Builtin = 839 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); 840 841 BuiltinCall->setType(CE->getType()); 842 BuiltinCall->setValueKind(CE->getValueKind()); 843 BuiltinCall->setObjectKind(CE->getObjectKind()); 844 BuiltinCall->setCallee(Builtin); 845 BuiltinCall->setArg(1, ChainResult.get()); 846 847 return false; 848 } 849 850 namespace { 851 852 class ScanfDiagnosticFormatHandler 853 : public analyze_format_string::FormatStringHandler { 854 // Accepts the argument index (relative to the first destination index) of the 855 // argument whose size we want. 856 using ComputeSizeFunction = 857 llvm::function_ref<std::optional<llvm::APSInt>(unsigned)>; 858 859 // Accepts the argument index (relative to the first destination index), the 860 // destination size, and the source size). 861 using DiagnoseFunction = 862 llvm::function_ref<void(unsigned, unsigned, unsigned)>; 863 864 ComputeSizeFunction ComputeSizeArgument; 865 DiagnoseFunction Diagnose; 866 867 public: 868 ScanfDiagnosticFormatHandler(ComputeSizeFunction ComputeSizeArgument, 869 DiagnoseFunction Diagnose) 870 : ComputeSizeArgument(ComputeSizeArgument), Diagnose(Diagnose) {} 871 872 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 873 const char *StartSpecifier, 874 unsigned specifierLen) override { 875 if (!FS.consumesDataArgument()) 876 return true; 877 878 unsigned NulByte = 0; 879 switch ((FS.getConversionSpecifier().getKind())) { 880 default: 881 return true; 882 case analyze_format_string::ConversionSpecifier::sArg: 883 case analyze_format_string::ConversionSpecifier::ScanListArg: 884 NulByte = 1; 885 break; 886 case analyze_format_string::ConversionSpecifier::cArg: 887 break; 888 } 889 890 analyze_format_string::OptionalAmount FW = FS.getFieldWidth(); 891 if (FW.getHowSpecified() != 892 analyze_format_string::OptionalAmount::HowSpecified::Constant) 893 return true; 894 895 unsigned SourceSize = FW.getConstantAmount() + NulByte; 896 897 std::optional<llvm::APSInt> DestSizeAPS = 898 ComputeSizeArgument(FS.getArgIndex()); 899 if (!DestSizeAPS) 900 return true; 901 902 unsigned DestSize = DestSizeAPS->getZExtValue(); 903 904 if (DestSize < SourceSize) 905 Diagnose(FS.getArgIndex(), DestSize, SourceSize); 906 907 return true; 908 } 909 }; 910 911 class EstimateSizeFormatHandler 912 : public analyze_format_string::FormatStringHandler { 913 size_t Size; 914 /// Whether the format string contains Linux kernel's format specifier 915 /// extension. 916 bool IsKernelCompatible = true; 917 918 public: 919 EstimateSizeFormatHandler(StringRef Format) 920 : Size(std::min(Format.find(0), Format.size()) + 921 1 /* null byte always written by sprintf */) {} 922 923 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 924 const char *, unsigned SpecifierLen, 925 const TargetInfo &) override { 926 927 const size_t FieldWidth = computeFieldWidth(FS); 928 const size_t Precision = computePrecision(FS); 929 930 // The actual format. 931 switch (FS.getConversionSpecifier().getKind()) { 932 // Just a char. 933 case analyze_format_string::ConversionSpecifier::cArg: 934 case analyze_format_string::ConversionSpecifier::CArg: 935 Size += std::max(FieldWidth, (size_t)1); 936 break; 937 // Just an integer. 938 case analyze_format_string::ConversionSpecifier::dArg: 939 case analyze_format_string::ConversionSpecifier::DArg: 940 case analyze_format_string::ConversionSpecifier::iArg: 941 case analyze_format_string::ConversionSpecifier::oArg: 942 case analyze_format_string::ConversionSpecifier::OArg: 943 case analyze_format_string::ConversionSpecifier::uArg: 944 case analyze_format_string::ConversionSpecifier::UArg: 945 case analyze_format_string::ConversionSpecifier::xArg: 946 case analyze_format_string::ConversionSpecifier::XArg: 947 Size += std::max(FieldWidth, Precision); 948 break; 949 950 // %g style conversion switches between %f or %e style dynamically. 951 // %g removes trailing zeros, and does not print decimal point if there are 952 // no digits that follow it. Thus %g can print a single digit. 953 // FIXME: If it is alternative form: 954 // For g and G conversions, trailing zeros are not removed from the result. 955 case analyze_format_string::ConversionSpecifier::gArg: 956 case analyze_format_string::ConversionSpecifier::GArg: 957 Size += 1; 958 break; 959 960 // Floating point number in the form '[+]ddd.ddd'. 961 case analyze_format_string::ConversionSpecifier::fArg: 962 case analyze_format_string::ConversionSpecifier::FArg: 963 Size += std::max(FieldWidth, 1 /* integer part */ + 964 (Precision ? 1 + Precision 965 : 0) /* period + decimal */); 966 break; 967 968 // Floating point number in the form '[-]d.ddde[+-]dd'. 969 case analyze_format_string::ConversionSpecifier::eArg: 970 case analyze_format_string::ConversionSpecifier::EArg: 971 Size += 972 std::max(FieldWidth, 973 1 /* integer part */ + 974 (Precision ? 1 + Precision : 0) /* period + decimal */ + 975 1 /* e or E letter */ + 2 /* exponent */); 976 break; 977 978 // Floating point number in the form '[-]0xh.hhhhp±dd'. 979 case analyze_format_string::ConversionSpecifier::aArg: 980 case analyze_format_string::ConversionSpecifier::AArg: 981 Size += 982 std::max(FieldWidth, 983 2 /* 0x */ + 1 /* integer part */ + 984 (Precision ? 1 + Precision : 0) /* period + decimal */ + 985 1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */); 986 break; 987 988 // Just a string. 989 case analyze_format_string::ConversionSpecifier::sArg: 990 case analyze_format_string::ConversionSpecifier::SArg: 991 Size += FieldWidth; 992 break; 993 994 // Just a pointer in the form '0xddd'. 995 case analyze_format_string::ConversionSpecifier::pArg: 996 // Linux kernel has its own extesion for `%p` specifier. 997 // Kernel Document: 998 // https://docs.kernel.org/core-api/printk-formats.html#pointer-types 999 IsKernelCompatible = false; 1000 Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision); 1001 break; 1002 1003 // A plain percent. 1004 case analyze_format_string::ConversionSpecifier::PercentArg: 1005 Size += 1; 1006 break; 1007 1008 default: 1009 break; 1010 } 1011 1012 Size += FS.hasPlusPrefix() || FS.hasSpacePrefix(); 1013 1014 if (FS.hasAlternativeForm()) { 1015 switch (FS.getConversionSpecifier().getKind()) { 1016 // For o conversion, it increases the precision, if and only if necessary, 1017 // to force the first digit of the result to be a zero 1018 // (if the value and precision are both 0, a single 0 is printed) 1019 case analyze_format_string::ConversionSpecifier::oArg: 1020 // For b conversion, a nonzero result has 0b prefixed to it. 1021 case analyze_format_string::ConversionSpecifier::bArg: 1022 // For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to 1023 // it. 1024 case analyze_format_string::ConversionSpecifier::xArg: 1025 case analyze_format_string::ConversionSpecifier::XArg: 1026 // Note: even when the prefix is added, if 1027 // (prefix_width <= FieldWidth - formatted_length) holds, 1028 // the prefix does not increase the format 1029 // size. e.g.(("%#3x", 0xf) is "0xf") 1030 1031 // If the result is zero, o, b, x, X adds nothing. 1032 break; 1033 // For a, A, e, E, f, F, g, and G conversions, 1034 // the result of converting a floating-point number always contains a 1035 // decimal-point 1036 case analyze_format_string::ConversionSpecifier::aArg: 1037 case analyze_format_string::ConversionSpecifier::AArg: 1038 case analyze_format_string::ConversionSpecifier::eArg: 1039 case analyze_format_string::ConversionSpecifier::EArg: 1040 case analyze_format_string::ConversionSpecifier::fArg: 1041 case analyze_format_string::ConversionSpecifier::FArg: 1042 case analyze_format_string::ConversionSpecifier::gArg: 1043 case analyze_format_string::ConversionSpecifier::GArg: 1044 Size += (Precision ? 0 : 1); 1045 break; 1046 // For other conversions, the behavior is undefined. 1047 default: 1048 break; 1049 } 1050 } 1051 assert(SpecifierLen <= Size && "no underflow"); 1052 Size -= SpecifierLen; 1053 return true; 1054 } 1055 1056 size_t getSizeLowerBound() const { return Size; } 1057 bool isKernelCompatible() const { return IsKernelCompatible; } 1058 1059 private: 1060 static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) { 1061 const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth(); 1062 size_t FieldWidth = 0; 1063 if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant) 1064 FieldWidth = FW.getConstantAmount(); 1065 return FieldWidth; 1066 } 1067 1068 static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) { 1069 const analyze_format_string::OptionalAmount &FW = FS.getPrecision(); 1070 size_t Precision = 0; 1071 1072 // See man 3 printf for default precision value based on the specifier. 1073 switch (FW.getHowSpecified()) { 1074 case analyze_format_string::OptionalAmount::NotSpecified: 1075 switch (FS.getConversionSpecifier().getKind()) { 1076 default: 1077 break; 1078 case analyze_format_string::ConversionSpecifier::dArg: // %d 1079 case analyze_format_string::ConversionSpecifier::DArg: // %D 1080 case analyze_format_string::ConversionSpecifier::iArg: // %i 1081 Precision = 1; 1082 break; 1083 case analyze_format_string::ConversionSpecifier::oArg: // %d 1084 case analyze_format_string::ConversionSpecifier::OArg: // %D 1085 case analyze_format_string::ConversionSpecifier::uArg: // %d 1086 case analyze_format_string::ConversionSpecifier::UArg: // %D 1087 case analyze_format_string::ConversionSpecifier::xArg: // %d 1088 case analyze_format_string::ConversionSpecifier::XArg: // %D 1089 Precision = 1; 1090 break; 1091 case analyze_format_string::ConversionSpecifier::fArg: // %f 1092 case analyze_format_string::ConversionSpecifier::FArg: // %F 1093 case analyze_format_string::ConversionSpecifier::eArg: // %e 1094 case analyze_format_string::ConversionSpecifier::EArg: // %E 1095 case analyze_format_string::ConversionSpecifier::gArg: // %g 1096 case analyze_format_string::ConversionSpecifier::GArg: // %G 1097 Precision = 6; 1098 break; 1099 case analyze_format_string::ConversionSpecifier::pArg: // %d 1100 Precision = 1; 1101 break; 1102 } 1103 break; 1104 case analyze_format_string::OptionalAmount::Constant: 1105 Precision = FW.getConstantAmount(); 1106 break; 1107 default: 1108 break; 1109 } 1110 return Precision; 1111 } 1112 }; 1113 1114 } // namespace 1115 1116 static bool ProcessFormatStringLiteral(const Expr *FormatExpr, 1117 StringRef &FormatStrRef, size_t &StrLen, 1118 ASTContext &Context) { 1119 if (const auto *Format = dyn_cast<StringLiteral>(FormatExpr); 1120 Format && (Format->isOrdinary() || Format->isUTF8())) { 1121 FormatStrRef = Format->getString(); 1122 const ConstantArrayType *T = 1123 Context.getAsConstantArrayType(Format->getType()); 1124 assert(T && "String literal not of constant array type!"); 1125 size_t TypeSize = T->getZExtSize(); 1126 // In case there's a null byte somewhere. 1127 StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0)); 1128 return true; 1129 } 1130 return false; 1131 } 1132 1133 void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, 1134 CallExpr *TheCall) { 1135 if (TheCall->isValueDependent() || TheCall->isTypeDependent() || 1136 isConstantEvaluatedContext()) 1137 return; 1138 1139 bool UseDABAttr = false; 1140 const FunctionDecl *UseDecl = FD; 1141 1142 const auto *DABAttr = FD->getAttr<DiagnoseAsBuiltinAttr>(); 1143 if (DABAttr) { 1144 UseDecl = DABAttr->getFunction(); 1145 assert(UseDecl && "Missing FunctionDecl in DiagnoseAsBuiltin attribute!"); 1146 UseDABAttr = true; 1147 } 1148 1149 unsigned BuiltinID = UseDecl->getBuiltinID(/*ConsiderWrappers=*/true); 1150 1151 if (!BuiltinID) 1152 return; 1153 1154 const TargetInfo &TI = getASTContext().getTargetInfo(); 1155 unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType()); 1156 1157 auto TranslateIndex = [&](unsigned Index) -> std::optional<unsigned> { 1158 // If we refer to a diagnose_as_builtin attribute, we need to change the 1159 // argument index to refer to the arguments of the called function. Unless 1160 // the index is out of bounds, which presumably means it's a variadic 1161 // function. 1162 if (!UseDABAttr) 1163 return Index; 1164 unsigned DABIndices = DABAttr->argIndices_size(); 1165 unsigned NewIndex = Index < DABIndices 1166 ? DABAttr->argIndices_begin()[Index] 1167 : Index - DABIndices + FD->getNumParams(); 1168 if (NewIndex >= TheCall->getNumArgs()) 1169 return std::nullopt; 1170 return NewIndex; 1171 }; 1172 1173 auto ComputeExplicitObjectSizeArgument = 1174 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1175 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1176 if (!IndexOptional) 1177 return std::nullopt; 1178 unsigned NewIndex = *IndexOptional; 1179 Expr::EvalResult Result; 1180 Expr *SizeArg = TheCall->getArg(NewIndex); 1181 if (!SizeArg->EvaluateAsInt(Result, getASTContext())) 1182 return std::nullopt; 1183 llvm::APSInt Integer = Result.Val.getInt(); 1184 Integer.setIsUnsigned(true); 1185 return Integer; 1186 }; 1187 1188 auto ComputeSizeArgument = 1189 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1190 // If the parameter has a pass_object_size attribute, then we should use its 1191 // (potentially) more strict checking mode. Otherwise, conservatively assume 1192 // type 0. 1193 int BOSType = 0; 1194 // This check can fail for variadic functions. 1195 if (Index < FD->getNumParams()) { 1196 if (const auto *POS = 1197 FD->getParamDecl(Index)->getAttr<PassObjectSizeAttr>()) 1198 BOSType = POS->getType(); 1199 } 1200 1201 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1202 if (!IndexOptional) 1203 return std::nullopt; 1204 unsigned NewIndex = *IndexOptional; 1205 1206 if (NewIndex >= TheCall->getNumArgs()) 1207 return std::nullopt; 1208 1209 const Expr *ObjArg = TheCall->getArg(NewIndex); 1210 uint64_t Result; 1211 if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType)) 1212 return std::nullopt; 1213 1214 // Get the object size in the target's size_t width. 1215 return llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth); 1216 }; 1217 1218 auto ComputeStrLenArgument = 1219 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1220 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1221 if (!IndexOptional) 1222 return std::nullopt; 1223 unsigned NewIndex = *IndexOptional; 1224 1225 const Expr *ObjArg = TheCall->getArg(NewIndex); 1226 uint64_t Result; 1227 if (!ObjArg->tryEvaluateStrLen(Result, getASTContext())) 1228 return std::nullopt; 1229 // Add 1 for null byte. 1230 return llvm::APSInt::getUnsigned(Result + 1).extOrTrunc(SizeTypeWidth); 1231 }; 1232 1233 std::optional<llvm::APSInt> SourceSize; 1234 std::optional<llvm::APSInt> DestinationSize; 1235 unsigned DiagID = 0; 1236 bool IsChkVariant = false; 1237 1238 auto GetFunctionName = [&]() { 1239 StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID); 1240 // Skim off the details of whichever builtin was called to produce a better 1241 // diagnostic, as it's unlikely that the user wrote the __builtin 1242 // explicitly. 1243 if (IsChkVariant) { 1244 FunctionName = FunctionName.drop_front(std::strlen("__builtin___")); 1245 FunctionName = FunctionName.drop_back(std::strlen("_chk")); 1246 } else { 1247 FunctionName.consume_front("__builtin_"); 1248 } 1249 return FunctionName; 1250 }; 1251 1252 switch (BuiltinID) { 1253 default: 1254 return; 1255 case Builtin::BI__builtin_strcpy: 1256 case Builtin::BIstrcpy: { 1257 DiagID = diag::warn_fortify_strlen_overflow; 1258 SourceSize = ComputeStrLenArgument(1); 1259 DestinationSize = ComputeSizeArgument(0); 1260 break; 1261 } 1262 1263 case Builtin::BI__builtin___strcpy_chk: { 1264 DiagID = diag::warn_fortify_strlen_overflow; 1265 SourceSize = ComputeStrLenArgument(1); 1266 DestinationSize = ComputeExplicitObjectSizeArgument(2); 1267 IsChkVariant = true; 1268 break; 1269 } 1270 1271 case Builtin::BIscanf: 1272 case Builtin::BIfscanf: 1273 case Builtin::BIsscanf: { 1274 unsigned FormatIndex = 1; 1275 unsigned DataIndex = 2; 1276 if (BuiltinID == Builtin::BIscanf) { 1277 FormatIndex = 0; 1278 DataIndex = 1; 1279 } 1280 1281 const auto *FormatExpr = 1282 TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); 1283 1284 StringRef FormatStrRef; 1285 size_t StrLen; 1286 if (!ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) 1287 return; 1288 1289 auto Diagnose = [&](unsigned ArgIndex, unsigned DestSize, 1290 unsigned SourceSize) { 1291 DiagID = diag::warn_fortify_scanf_overflow; 1292 unsigned Index = ArgIndex + DataIndex; 1293 StringRef FunctionName = GetFunctionName(); 1294 DiagRuntimeBehavior(TheCall->getArg(Index)->getBeginLoc(), TheCall, 1295 PDiag(DiagID) << FunctionName << (Index + 1) 1296 << DestSize << SourceSize); 1297 }; 1298 1299 auto ShiftedComputeSizeArgument = [&](unsigned Index) { 1300 return ComputeSizeArgument(Index + DataIndex); 1301 }; 1302 ScanfDiagnosticFormatHandler H(ShiftedComputeSizeArgument, Diagnose); 1303 const char *FormatBytes = FormatStrRef.data(); 1304 analyze_format_string::ParseScanfString(H, FormatBytes, 1305 FormatBytes + StrLen, getLangOpts(), 1306 Context.getTargetInfo()); 1307 1308 // Unlike the other cases, in this one we have already issued the diagnostic 1309 // here, so no need to continue (because unlike the other cases, here the 1310 // diagnostic refers to the argument number). 1311 return; 1312 } 1313 1314 case Builtin::BIsprintf: 1315 case Builtin::BI__builtin___sprintf_chk: { 1316 size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3; 1317 auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); 1318 1319 StringRef FormatStrRef; 1320 size_t StrLen; 1321 if (ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { 1322 EstimateSizeFormatHandler H(FormatStrRef); 1323 const char *FormatBytes = FormatStrRef.data(); 1324 if (!analyze_format_string::ParsePrintfString( 1325 H, FormatBytes, FormatBytes + StrLen, getLangOpts(), 1326 Context.getTargetInfo(), false)) { 1327 DiagID = H.isKernelCompatible() 1328 ? diag::warn_format_overflow 1329 : diag::warn_format_overflow_non_kprintf; 1330 SourceSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) 1331 .extOrTrunc(SizeTypeWidth); 1332 if (BuiltinID == Builtin::BI__builtin___sprintf_chk) { 1333 DestinationSize = ComputeExplicitObjectSizeArgument(2); 1334 IsChkVariant = true; 1335 } else { 1336 DestinationSize = ComputeSizeArgument(0); 1337 } 1338 break; 1339 } 1340 } 1341 return; 1342 } 1343 case Builtin::BI__builtin___memcpy_chk: 1344 case Builtin::BI__builtin___memmove_chk: 1345 case Builtin::BI__builtin___memset_chk: 1346 case Builtin::BI__builtin___strlcat_chk: 1347 case Builtin::BI__builtin___strlcpy_chk: 1348 case Builtin::BI__builtin___strncat_chk: 1349 case Builtin::BI__builtin___strncpy_chk: 1350 case Builtin::BI__builtin___stpncpy_chk: 1351 case Builtin::BI__builtin___memccpy_chk: 1352 case Builtin::BI__builtin___mempcpy_chk: { 1353 DiagID = diag::warn_builtin_chk_overflow; 1354 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2); 1355 DestinationSize = 1356 ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1357 IsChkVariant = true; 1358 break; 1359 } 1360 1361 case Builtin::BI__builtin___snprintf_chk: 1362 case Builtin::BI__builtin___vsnprintf_chk: { 1363 DiagID = diag::warn_builtin_chk_overflow; 1364 SourceSize = ComputeExplicitObjectSizeArgument(1); 1365 DestinationSize = ComputeExplicitObjectSizeArgument(3); 1366 IsChkVariant = true; 1367 break; 1368 } 1369 1370 case Builtin::BIstrncat: 1371 case Builtin::BI__builtin_strncat: 1372 case Builtin::BIstrncpy: 1373 case Builtin::BI__builtin_strncpy: 1374 case Builtin::BIstpncpy: 1375 case Builtin::BI__builtin_stpncpy: { 1376 // Whether these functions overflow depends on the runtime strlen of the 1377 // string, not just the buffer size, so emitting the "always overflow" 1378 // diagnostic isn't quite right. We should still diagnose passing a buffer 1379 // size larger than the destination buffer though; this is a runtime abort 1380 // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise. 1381 DiagID = diag::warn_fortify_source_size_mismatch; 1382 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1383 DestinationSize = ComputeSizeArgument(0); 1384 break; 1385 } 1386 1387 case Builtin::BImemcpy: 1388 case Builtin::BI__builtin_memcpy: 1389 case Builtin::BImemmove: 1390 case Builtin::BI__builtin_memmove: 1391 case Builtin::BImemset: 1392 case Builtin::BI__builtin_memset: 1393 case Builtin::BImempcpy: 1394 case Builtin::BI__builtin_mempcpy: { 1395 DiagID = diag::warn_fortify_source_overflow; 1396 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1397 DestinationSize = ComputeSizeArgument(0); 1398 break; 1399 } 1400 case Builtin::BIsnprintf: 1401 case Builtin::BI__builtin_snprintf: 1402 case Builtin::BIvsnprintf: 1403 case Builtin::BI__builtin_vsnprintf: { 1404 DiagID = diag::warn_fortify_source_size_mismatch; 1405 SourceSize = ComputeExplicitObjectSizeArgument(1); 1406 const auto *FormatExpr = TheCall->getArg(2)->IgnoreParenImpCasts(); 1407 StringRef FormatStrRef; 1408 size_t StrLen; 1409 if (SourceSize && 1410 ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { 1411 EstimateSizeFormatHandler H(FormatStrRef); 1412 const char *FormatBytes = FormatStrRef.data(); 1413 if (!analyze_format_string::ParsePrintfString( 1414 H, FormatBytes, FormatBytes + StrLen, getLangOpts(), 1415 Context.getTargetInfo(), /*isFreeBSDKPrintf=*/false)) { 1416 llvm::APSInt FormatSize = 1417 llvm::APSInt::getUnsigned(H.getSizeLowerBound()) 1418 .extOrTrunc(SizeTypeWidth); 1419 if (FormatSize > *SourceSize && *SourceSize != 0) { 1420 unsigned TruncationDiagID = 1421 H.isKernelCompatible() ? diag::warn_format_truncation 1422 : diag::warn_format_truncation_non_kprintf; 1423 SmallString<16> SpecifiedSizeStr; 1424 SmallString<16> FormatSizeStr; 1425 SourceSize->toString(SpecifiedSizeStr, /*Radix=*/10); 1426 FormatSize.toString(FormatSizeStr, /*Radix=*/10); 1427 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 1428 PDiag(TruncationDiagID) 1429 << GetFunctionName() << SpecifiedSizeStr 1430 << FormatSizeStr); 1431 } 1432 } 1433 } 1434 DestinationSize = ComputeSizeArgument(0); 1435 } 1436 } 1437 1438 if (!SourceSize || !DestinationSize || 1439 llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0) 1440 return; 1441 1442 StringRef FunctionName = GetFunctionName(); 1443 1444 SmallString<16> DestinationStr; 1445 SmallString<16> SourceStr; 1446 DestinationSize->toString(DestinationStr, /*Radix=*/10); 1447 SourceSize->toString(SourceStr, /*Radix=*/10); 1448 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 1449 PDiag(DiagID) 1450 << FunctionName << DestinationStr << SourceStr); 1451 } 1452 1453 static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, 1454 Scope::ScopeFlags NeededScopeFlags, 1455 unsigned DiagID) { 1456 // Scopes aren't available during instantiation. Fortunately, builtin 1457 // functions cannot be template args so they cannot be formed through template 1458 // instantiation. Therefore checking once during the parse is sufficient. 1459 if (SemaRef.inTemplateInstantiation()) 1460 return false; 1461 1462 Scope *S = SemaRef.getCurScope(); 1463 while (S && !S->isSEHExceptScope()) 1464 S = S->getParent(); 1465 if (!S || !(S->getFlags() & NeededScopeFlags)) { 1466 auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1467 SemaRef.Diag(TheCall->getExprLoc(), DiagID) 1468 << DRE->getDecl()->getIdentifier(); 1469 return true; 1470 } 1471 1472 return false; 1473 } 1474 1475 // In OpenCL, __builtin_alloca_* should return a pointer to address space 1476 // that corresponds to the stack address space i.e private address space. 1477 static void builtinAllocaAddrSpace(Sema &S, CallExpr *TheCall) { 1478 QualType RT = TheCall->getType(); 1479 assert((RT->isPointerType() && !(RT->getPointeeType().hasAddressSpace())) && 1480 "__builtin_alloca has invalid address space"); 1481 1482 RT = RT->getPointeeType(); 1483 RT = S.Context.getAddrSpaceQualType(RT, LangAS::opencl_private); 1484 TheCall->setType(S.Context.getPointerType(RT)); 1485 } 1486 1487 namespace { 1488 enum PointerAuthOpKind { 1489 PAO_Strip, 1490 PAO_Sign, 1491 PAO_Auth, 1492 PAO_SignGeneric, 1493 PAO_Discriminator, 1494 PAO_BlendPointer, 1495 PAO_BlendInteger 1496 }; 1497 } 1498 1499 bool Sema::checkPointerAuthEnabled(SourceLocation Loc, SourceRange Range) { 1500 if (getLangOpts().PointerAuthIntrinsics) 1501 return false; 1502 1503 Diag(Loc, diag::err_ptrauth_disabled) << Range; 1504 return true; 1505 } 1506 1507 static bool checkPointerAuthEnabled(Sema &S, Expr *E) { 1508 return S.checkPointerAuthEnabled(E->getExprLoc(), E->getSourceRange()); 1509 } 1510 1511 static bool checkPointerAuthKey(Sema &S, Expr *&Arg) { 1512 // Convert it to type 'int'. 1513 if (convertArgumentToType(S, Arg, S.Context.IntTy)) 1514 return true; 1515 1516 // Value-dependent expressions are okay; wait for template instantiation. 1517 if (Arg->isValueDependent()) 1518 return false; 1519 1520 unsigned KeyValue; 1521 return S.checkConstantPointerAuthKey(Arg, KeyValue); 1522 } 1523 1524 bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) { 1525 // Attempt to constant-evaluate the expression. 1526 std::optional<llvm::APSInt> KeyValue = Arg->getIntegerConstantExpr(Context); 1527 if (!KeyValue) { 1528 Diag(Arg->getExprLoc(), diag::err_expr_not_ice) 1529 << 0 << Arg->getSourceRange(); 1530 return true; 1531 } 1532 1533 // Ask the target to validate the key parameter. 1534 if (!Context.getTargetInfo().validatePointerAuthKey(*KeyValue)) { 1535 llvm::SmallString<32> Value; 1536 { 1537 llvm::raw_svector_ostream Str(Value); 1538 Str << *KeyValue; 1539 } 1540 1541 Diag(Arg->getExprLoc(), diag::err_ptrauth_invalid_key) 1542 << Value << Arg->getSourceRange(); 1543 return true; 1544 } 1545 1546 Result = KeyValue->getZExtValue(); 1547 return false; 1548 } 1549 1550 static std::pair<const ValueDecl *, CharUnits> 1551 findConstantBaseAndOffset(Sema &S, Expr *E) { 1552 // Must evaluate as a pointer. 1553 Expr::EvalResult Result; 1554 if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue()) 1555 return {nullptr, CharUnits()}; 1556 1557 const auto *BaseDecl = 1558 Result.Val.getLValueBase().dyn_cast<const ValueDecl *>(); 1559 if (!BaseDecl) 1560 return {nullptr, CharUnits()}; 1561 1562 return {BaseDecl, Result.Val.getLValueOffset()}; 1563 } 1564 1565 static bool checkPointerAuthValue(Sema &S, Expr *&Arg, PointerAuthOpKind OpKind, 1566 bool RequireConstant = false) { 1567 if (Arg->hasPlaceholderType()) { 1568 ExprResult R = S.CheckPlaceholderExpr(Arg); 1569 if (R.isInvalid()) 1570 return true; 1571 Arg = R.get(); 1572 } 1573 1574 auto AllowsPointer = [](PointerAuthOpKind OpKind) { 1575 return OpKind != PAO_BlendInteger; 1576 }; 1577 auto AllowsInteger = [](PointerAuthOpKind OpKind) { 1578 return OpKind == PAO_Discriminator || OpKind == PAO_BlendInteger || 1579 OpKind == PAO_SignGeneric; 1580 }; 1581 1582 // Require the value to have the right range of type. 1583 QualType ExpectedTy; 1584 if (AllowsPointer(OpKind) && Arg->getType()->isPointerType()) { 1585 ExpectedTy = Arg->getType().getUnqualifiedType(); 1586 } else if (AllowsPointer(OpKind) && Arg->getType()->isNullPtrType()) { 1587 ExpectedTy = S.Context.VoidPtrTy; 1588 } else if (AllowsInteger(OpKind) && 1589 Arg->getType()->isIntegralOrUnscopedEnumerationType()) { 1590 ExpectedTy = S.Context.getUIntPtrType(); 1591 1592 } else { 1593 // Diagnose the failures. 1594 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_value_bad_type) 1595 << unsigned(OpKind == PAO_Discriminator ? 1 1596 : OpKind == PAO_BlendPointer ? 2 1597 : OpKind == PAO_BlendInteger ? 3 1598 : 0) 1599 << unsigned(AllowsInteger(OpKind) ? (AllowsPointer(OpKind) ? 2 : 1) : 0) 1600 << Arg->getType() << Arg->getSourceRange(); 1601 return true; 1602 } 1603 1604 // Convert to that type. This should just be an lvalue-to-rvalue 1605 // conversion. 1606 if (convertArgumentToType(S, Arg, ExpectedTy)) 1607 return true; 1608 1609 if (!RequireConstant) { 1610 // Warn about null pointers for non-generic sign and auth operations. 1611 if ((OpKind == PAO_Sign || OpKind == PAO_Auth) && 1612 Arg->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) { 1613 S.Diag(Arg->getExprLoc(), OpKind == PAO_Sign 1614 ? diag::warn_ptrauth_sign_null_pointer 1615 : diag::warn_ptrauth_auth_null_pointer) 1616 << Arg->getSourceRange(); 1617 } 1618 1619 return false; 1620 } 1621 1622 // Perform special checking on the arguments to ptrauth_sign_constant. 1623 1624 // The main argument. 1625 if (OpKind == PAO_Sign) { 1626 // Require the value we're signing to have a special form. 1627 auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Arg); 1628 bool Invalid; 1629 1630 // Must be rooted in a declaration reference. 1631 if (!BaseDecl) 1632 Invalid = true; 1633 1634 // If it's a function declaration, we can't have an offset. 1635 else if (isa<FunctionDecl>(BaseDecl)) 1636 Invalid = !Offset.isZero(); 1637 1638 // Otherwise we're fine. 1639 else 1640 Invalid = false; 1641 1642 if (Invalid) 1643 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer); 1644 return Invalid; 1645 } 1646 1647 // The discriminator argument. 1648 assert(OpKind == PAO_Discriminator); 1649 1650 // Must be a pointer or integer or blend thereof. 1651 Expr *Pointer = nullptr; 1652 Expr *Integer = nullptr; 1653 if (auto *Call = dyn_cast<CallExpr>(Arg->IgnoreParens())) { 1654 if (Call->getBuiltinCallee() == 1655 Builtin::BI__builtin_ptrauth_blend_discriminator) { 1656 Pointer = Call->getArg(0); 1657 Integer = Call->getArg(1); 1658 } 1659 } 1660 if (!Pointer && !Integer) { 1661 if (Arg->getType()->isPointerType()) 1662 Pointer = Arg; 1663 else 1664 Integer = Arg; 1665 } 1666 1667 // Check the pointer. 1668 bool Invalid = false; 1669 if (Pointer) { 1670 assert(Pointer->getType()->isPointerType()); 1671 1672 // TODO: if we're initializing a global, check that the address is 1673 // somehow related to what we're initializing. This probably will 1674 // never really be feasible and we'll have to catch it at link-time. 1675 auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Pointer); 1676 if (!BaseDecl || !isa<VarDecl>(BaseDecl)) 1677 Invalid = true; 1678 } 1679 1680 // Check the integer. 1681 if (Integer) { 1682 assert(Integer->getType()->isIntegerType()); 1683 if (!Integer->isEvaluatable(S.Context)) 1684 Invalid = true; 1685 } 1686 1687 if (Invalid) 1688 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator); 1689 return Invalid; 1690 } 1691 1692 static ExprResult PointerAuthStrip(Sema &S, CallExpr *Call) { 1693 if (S.checkArgCount(Call, 2)) 1694 return ExprError(); 1695 if (checkPointerAuthEnabled(S, Call)) 1696 return ExprError(); 1697 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Strip) || 1698 checkPointerAuthKey(S, Call->getArgs()[1])) 1699 return ExprError(); 1700 1701 Call->setType(Call->getArgs()[0]->getType()); 1702 return Call; 1703 } 1704 1705 static ExprResult PointerAuthBlendDiscriminator(Sema &S, CallExpr *Call) { 1706 if (S.checkArgCount(Call, 2)) 1707 return ExprError(); 1708 if (checkPointerAuthEnabled(S, Call)) 1709 return ExprError(); 1710 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_BlendPointer) || 1711 checkPointerAuthValue(S, Call->getArgs()[1], PAO_BlendInteger)) 1712 return ExprError(); 1713 1714 Call->setType(S.Context.getUIntPtrType()); 1715 return Call; 1716 } 1717 1718 static ExprResult PointerAuthSignGenericData(Sema &S, CallExpr *Call) { 1719 if (S.checkArgCount(Call, 2)) 1720 return ExprError(); 1721 if (checkPointerAuthEnabled(S, Call)) 1722 return ExprError(); 1723 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_SignGeneric) || 1724 checkPointerAuthValue(S, Call->getArgs()[1], PAO_Discriminator)) 1725 return ExprError(); 1726 1727 Call->setType(S.Context.getUIntPtrType()); 1728 return Call; 1729 } 1730 1731 static ExprResult PointerAuthSignOrAuth(Sema &S, CallExpr *Call, 1732 PointerAuthOpKind OpKind, 1733 bool RequireConstant) { 1734 if (S.checkArgCount(Call, 3)) 1735 return ExprError(); 1736 if (checkPointerAuthEnabled(S, Call)) 1737 return ExprError(); 1738 if (checkPointerAuthValue(S, Call->getArgs()[0], OpKind, RequireConstant) || 1739 checkPointerAuthKey(S, Call->getArgs()[1]) || 1740 checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator, 1741 RequireConstant)) 1742 return ExprError(); 1743 1744 Call->setType(Call->getArgs()[0]->getType()); 1745 return Call; 1746 } 1747 1748 static ExprResult PointerAuthAuthAndResign(Sema &S, CallExpr *Call) { 1749 if (S.checkArgCount(Call, 5)) 1750 return ExprError(); 1751 if (checkPointerAuthEnabled(S, Call)) 1752 return ExprError(); 1753 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Auth) || 1754 checkPointerAuthKey(S, Call->getArgs()[1]) || 1755 checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator) || 1756 checkPointerAuthKey(S, Call->getArgs()[3]) || 1757 checkPointerAuthValue(S, Call->getArgs()[4], PAO_Discriminator)) 1758 return ExprError(); 1759 1760 Call->setType(Call->getArgs()[0]->getType()); 1761 return Call; 1762 } 1763 1764 static ExprResult PointerAuthStringDiscriminator(Sema &S, CallExpr *Call) { 1765 if (checkPointerAuthEnabled(S, Call)) 1766 return ExprError(); 1767 1768 // We've already performed normal call type-checking. 1769 const Expr *Arg = Call->getArg(0)->IgnoreParenImpCasts(); 1770 1771 // Operand must be an ordinary or UTF-8 string literal. 1772 const auto *Literal = dyn_cast<StringLiteral>(Arg); 1773 if (!Literal || Literal->getCharByteWidth() != 1) { 1774 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_string_not_literal) 1775 << (Literal ? 1 : 0) << Arg->getSourceRange(); 1776 return ExprError(); 1777 } 1778 1779 return Call; 1780 } 1781 1782 static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) { 1783 if (S.checkArgCount(TheCall, 1)) 1784 return ExprError(); 1785 1786 // Compute __builtin_launder's parameter type from the argument. 1787 // The parameter type is: 1788 // * The type of the argument if it's not an array or function type, 1789 // Otherwise, 1790 // * The decayed argument type. 1791 QualType ParamTy = [&]() { 1792 QualType ArgTy = TheCall->getArg(0)->getType(); 1793 if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe()) 1794 return S.Context.getPointerType(Ty->getElementType()); 1795 if (ArgTy->isFunctionType()) { 1796 return S.Context.getPointerType(ArgTy); 1797 } 1798 return ArgTy; 1799 }(); 1800 1801 TheCall->setType(ParamTy); 1802 1803 auto DiagSelect = [&]() -> std::optional<unsigned> { 1804 if (!ParamTy->isPointerType()) 1805 return 0; 1806 if (ParamTy->isFunctionPointerType()) 1807 return 1; 1808 if (ParamTy->isVoidPointerType()) 1809 return 2; 1810 return std::optional<unsigned>{}; 1811 }(); 1812 if (DiagSelect) { 1813 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg) 1814 << *DiagSelect << TheCall->getSourceRange(); 1815 return ExprError(); 1816 } 1817 1818 // We either have an incomplete class type, or we have a class template 1819 // whose instantiation has not been forced. Example: 1820 // 1821 // template <class T> struct Foo { T value; }; 1822 // Foo<int> *p = nullptr; 1823 // auto *d = __builtin_launder(p); 1824 if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(), 1825 diag::err_incomplete_type)) 1826 return ExprError(); 1827 1828 assert(ParamTy->getPointeeType()->isObjectType() && 1829 "Unhandled non-object pointer case"); 1830 1831 InitializedEntity Entity = 1832 InitializedEntity::InitializeParameter(S.Context, ParamTy, false); 1833 ExprResult Arg = 1834 S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0)); 1835 if (Arg.isInvalid()) 1836 return ExprError(); 1837 TheCall->setArg(0, Arg.get()); 1838 1839 return TheCall; 1840 } 1841 1842 static ExprResult BuiltinIsWithinLifetime(Sema &S, CallExpr *TheCall) { 1843 if (S.checkArgCount(TheCall, 1)) 1844 return ExprError(); 1845 1846 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 1847 if (Arg.isInvalid()) 1848 return ExprError(); 1849 QualType ParamTy = Arg.get()->getType(); 1850 TheCall->setArg(0, Arg.get()); 1851 TheCall->setType(S.Context.BoolTy); 1852 1853 // Only accept pointers to objects as arguments, which should have object 1854 // pointer or void pointer types. 1855 if (const auto *PT = ParamTy->getAs<PointerType>()) { 1856 // LWG4138: Function pointer types not allowed 1857 if (PT->getPointeeType()->isFunctionType()) { 1858 S.Diag(TheCall->getArg(0)->getExprLoc(), 1859 diag::err_builtin_is_within_lifetime_invalid_arg) 1860 << 1; 1861 return ExprError(); 1862 } 1863 // Disallow VLAs too since those shouldn't be able to 1864 // be a template parameter for `std::is_within_lifetime` 1865 if (PT->getPointeeType()->isVariableArrayType()) { 1866 S.Diag(TheCall->getArg(0)->getExprLoc(), diag::err_vla_unsupported) 1867 << 1 << "__builtin_is_within_lifetime"; 1868 return ExprError(); 1869 } 1870 } else { 1871 S.Diag(TheCall->getArg(0)->getExprLoc(), 1872 diag::err_builtin_is_within_lifetime_invalid_arg) 1873 << 0; 1874 return ExprError(); 1875 } 1876 1877 return TheCall; 1878 } 1879 1880 // Emit an error and return true if the current object format type is in the 1881 // list of unsupported types. 1882 static bool CheckBuiltinTargetNotInUnsupported( 1883 Sema &S, unsigned BuiltinID, CallExpr *TheCall, 1884 ArrayRef<llvm::Triple::ObjectFormatType> UnsupportedObjectFormatTypes) { 1885 llvm::Triple::ObjectFormatType CurObjFormat = 1886 S.getASTContext().getTargetInfo().getTriple().getObjectFormat(); 1887 if (llvm::is_contained(UnsupportedObjectFormatTypes, CurObjFormat)) { 1888 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1889 << TheCall->getSourceRange(); 1890 return true; 1891 } 1892 return false; 1893 } 1894 1895 // Emit an error and return true if the current architecture is not in the list 1896 // of supported architectures. 1897 static bool 1898 CheckBuiltinTargetInSupported(Sema &S, CallExpr *TheCall, 1899 ArrayRef<llvm::Triple::ArchType> SupportedArchs) { 1900 llvm::Triple::ArchType CurArch = 1901 S.getASTContext().getTargetInfo().getTriple().getArch(); 1902 if (llvm::is_contained(SupportedArchs, CurArch)) 1903 return false; 1904 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1905 << TheCall->getSourceRange(); 1906 return true; 1907 } 1908 1909 static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr, 1910 SourceLocation CallSiteLoc); 1911 1912 bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, 1913 CallExpr *TheCall) { 1914 switch (TI.getTriple().getArch()) { 1915 default: 1916 // Some builtins don't require additional checking, so just consider these 1917 // acceptable. 1918 return false; 1919 case llvm::Triple::arm: 1920 case llvm::Triple::armeb: 1921 case llvm::Triple::thumb: 1922 case llvm::Triple::thumbeb: 1923 return ARM().CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall); 1924 case llvm::Triple::aarch64: 1925 case llvm::Triple::aarch64_32: 1926 case llvm::Triple::aarch64_be: 1927 return ARM().CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall); 1928 case llvm::Triple::bpfeb: 1929 case llvm::Triple::bpfel: 1930 return BPF().CheckBPFBuiltinFunctionCall(BuiltinID, TheCall); 1931 case llvm::Triple::hexagon: 1932 return Hexagon().CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall); 1933 case llvm::Triple::mips: 1934 case llvm::Triple::mipsel: 1935 case llvm::Triple::mips64: 1936 case llvm::Triple::mips64el: 1937 return MIPS().CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall); 1938 case llvm::Triple::spirv: 1939 return SPIRV().CheckSPIRVBuiltinFunctionCall(BuiltinID, TheCall); 1940 case llvm::Triple::systemz: 1941 return SystemZ().CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall); 1942 case llvm::Triple::x86: 1943 case llvm::Triple::x86_64: 1944 return X86().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall); 1945 case llvm::Triple::ppc: 1946 case llvm::Triple::ppcle: 1947 case llvm::Triple::ppc64: 1948 case llvm::Triple::ppc64le: 1949 return PPC().CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall); 1950 case llvm::Triple::amdgcn: 1951 return AMDGPU().CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall); 1952 case llvm::Triple::riscv32: 1953 case llvm::Triple::riscv64: 1954 return RISCV().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall); 1955 case llvm::Triple::loongarch32: 1956 case llvm::Triple::loongarch64: 1957 return LoongArch().CheckLoongArchBuiltinFunctionCall(TI, BuiltinID, 1958 TheCall); 1959 case llvm::Triple::wasm32: 1960 case llvm::Triple::wasm64: 1961 return Wasm().CheckWebAssemblyBuiltinFunctionCall(TI, BuiltinID, TheCall); 1962 case llvm::Triple::nvptx: 1963 case llvm::Triple::nvptx64: 1964 return NVPTX().CheckNVPTXBuiltinFunctionCall(TI, BuiltinID, TheCall); 1965 } 1966 } 1967 1968 // Check if \p Ty is a valid type for the elementwise math builtins. If it is 1969 // not a valid type, emit an error message and return true. Otherwise return 1970 // false. 1971 static bool checkMathBuiltinElementType(Sema &S, SourceLocation Loc, 1972 QualType ArgTy, int ArgIndex) { 1973 if (!ArgTy->getAs<VectorType>() && 1974 !ConstantMatrixType::isValidElementType(ArgTy)) { 1975 return S.Diag(Loc, diag::err_builtin_invalid_arg_type) 1976 << ArgIndex << /* vector, integer or float ty*/ 0 << ArgTy; 1977 } 1978 1979 return false; 1980 } 1981 1982 static bool checkFPMathBuiltinElementType(Sema &S, SourceLocation Loc, 1983 QualType ArgTy, int ArgIndex) { 1984 QualType EltTy = ArgTy; 1985 if (auto *VecTy = EltTy->getAs<VectorType>()) 1986 EltTy = VecTy->getElementType(); 1987 1988 if (!EltTy->isRealFloatingType()) { 1989 return S.Diag(Loc, diag::err_builtin_invalid_arg_type) 1990 << ArgIndex << /* vector or float ty*/ 5 << ArgTy; 1991 } 1992 1993 return false; 1994 } 1995 1996 /// BuiltinCpu{Supports|Is} - Handle __builtin_cpu_{supports|is}(char *). 1997 /// This checks that the target supports the builtin and that the string 1998 /// argument is constant and valid. 1999 static bool BuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall, 2000 const TargetInfo *AuxTI, unsigned BuiltinID) { 2001 assert((BuiltinID == Builtin::BI__builtin_cpu_supports || 2002 BuiltinID == Builtin::BI__builtin_cpu_is) && 2003 "Expecting __builtin_cpu_..."); 2004 2005 bool IsCPUSupports = BuiltinID == Builtin::BI__builtin_cpu_supports; 2006 const TargetInfo *TheTI = &TI; 2007 auto SupportsBI = [=](const TargetInfo *TInfo) { 2008 return TInfo && ((IsCPUSupports && TInfo->supportsCpuSupports()) || 2009 (!IsCPUSupports && TInfo->supportsCpuIs())); 2010 }; 2011 if (!SupportsBI(&TI) && SupportsBI(AuxTI)) 2012 TheTI = AuxTI; 2013 2014 if ((!IsCPUSupports && !TheTI->supportsCpuIs()) || 2015 (IsCPUSupports && !TheTI->supportsCpuSupports())) 2016 return S.Diag(TheCall->getBeginLoc(), 2017 TI.getTriple().isOSAIX() 2018 ? diag::err_builtin_aix_os_unsupported 2019 : diag::err_builtin_target_unsupported) 2020 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 2021 2022 Expr *Arg = TheCall->getArg(0)->IgnoreParenImpCasts(); 2023 // Check if the argument is a string literal. 2024 if (!isa<StringLiteral>(Arg)) 2025 return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 2026 << Arg->getSourceRange(); 2027 2028 // Check the contents of the string. 2029 StringRef Feature = cast<StringLiteral>(Arg)->getString(); 2030 if (IsCPUSupports && !TheTI->validateCpuSupports(Feature)) { 2031 S.Diag(TheCall->getBeginLoc(), diag::warn_invalid_cpu_supports) 2032 << Arg->getSourceRange(); 2033 return false; 2034 } 2035 if (!IsCPUSupports && !TheTI->validateCpuIs(Feature)) 2036 return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is) 2037 << Arg->getSourceRange(); 2038 return false; 2039 } 2040 2041 /// Checks that __builtin_popcountg was called with a single argument, which is 2042 /// an unsigned integer. 2043 static bool BuiltinPopcountg(Sema &S, CallExpr *TheCall) { 2044 if (S.checkArgCount(TheCall, 1)) 2045 return true; 2046 2047 ExprResult ArgRes = S.DefaultLvalueConversion(TheCall->getArg(0)); 2048 if (ArgRes.isInvalid()) 2049 return true; 2050 2051 Expr *Arg = ArgRes.get(); 2052 TheCall->setArg(0, Arg); 2053 2054 QualType ArgTy = Arg->getType(); 2055 2056 if (!ArgTy->isUnsignedIntegerType()) { 2057 S.Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2058 << 1 << /*unsigned integer ty*/ 7 << ArgTy; 2059 return true; 2060 } 2061 return false; 2062 } 2063 2064 /// Checks that __builtin_{clzg,ctzg} was called with a first argument, which is 2065 /// an unsigned integer, and an optional second argument, which is promoted to 2066 /// an 'int'. 2067 static bool BuiltinCountZeroBitsGeneric(Sema &S, CallExpr *TheCall) { 2068 if (S.checkArgCountRange(TheCall, 1, 2)) 2069 return true; 2070 2071 ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0)); 2072 if (Arg0Res.isInvalid()) 2073 return true; 2074 2075 Expr *Arg0 = Arg0Res.get(); 2076 TheCall->setArg(0, Arg0); 2077 2078 QualType Arg0Ty = Arg0->getType(); 2079 2080 if (!Arg0Ty->isUnsignedIntegerType()) { 2081 S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2082 << 1 << /*unsigned integer ty*/ 7 << Arg0Ty; 2083 return true; 2084 } 2085 2086 if (TheCall->getNumArgs() > 1) { 2087 ExprResult Arg1Res = S.UsualUnaryConversions(TheCall->getArg(1)); 2088 if (Arg1Res.isInvalid()) 2089 return true; 2090 2091 Expr *Arg1 = Arg1Res.get(); 2092 TheCall->setArg(1, Arg1); 2093 2094 QualType Arg1Ty = Arg1->getType(); 2095 2096 if (!Arg1Ty->isSpecificBuiltinType(BuiltinType::Int)) { 2097 S.Diag(Arg1->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2098 << 2 << /*'int' ty*/ 8 << Arg1Ty; 2099 return true; 2100 } 2101 } 2102 2103 return false; 2104 } 2105 2106 ExprResult 2107 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, 2108 CallExpr *TheCall) { 2109 ExprResult TheCallResult(TheCall); 2110 2111 // Find out if any arguments are required to be integer constant expressions. 2112 unsigned ICEArguments = 0; 2113 ASTContext::GetBuiltinTypeError Error; 2114 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); 2115 if (Error != ASTContext::GE_None) 2116 ICEArguments = 0; // Don't diagnose previously diagnosed errors. 2117 2118 // If any arguments are required to be ICE's, check and diagnose. 2119 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { 2120 // Skip arguments not required to be ICE's. 2121 if ((ICEArguments & (1 << ArgNo)) == 0) continue; 2122 2123 llvm::APSInt Result; 2124 // If we don't have enough arguments, continue so we can issue better 2125 // diagnostic in checkArgCount(...) 2126 if (ArgNo < TheCall->getNumArgs() && 2127 BuiltinConstantArg(TheCall, ArgNo, Result)) 2128 return true; 2129 ICEArguments &= ~(1 << ArgNo); 2130 } 2131 2132 FPOptions FPO; 2133 switch (BuiltinID) { 2134 case Builtin::BI__builtin_cpu_supports: 2135 case Builtin::BI__builtin_cpu_is: 2136 if (BuiltinCpu(*this, Context.getTargetInfo(), TheCall, 2137 Context.getAuxTargetInfo(), BuiltinID)) 2138 return ExprError(); 2139 break; 2140 case Builtin::BI__builtin_cpu_init: 2141 if (!Context.getTargetInfo().supportsCpuInit()) { 2142 Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 2143 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 2144 return ExprError(); 2145 } 2146 break; 2147 case Builtin::BI__builtin___CFStringMakeConstantString: 2148 // CFStringMakeConstantString is currently not implemented for GOFF (i.e., 2149 // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported 2150 if (CheckBuiltinTargetNotInUnsupported( 2151 *this, BuiltinID, TheCall, 2152 {llvm::Triple::GOFF, llvm::Triple::XCOFF})) 2153 return ExprError(); 2154 assert(TheCall->getNumArgs() == 1 && 2155 "Wrong # arguments to builtin CFStringMakeConstantString"); 2156 if (ObjC().CheckObjCString(TheCall->getArg(0))) 2157 return ExprError(); 2158 break; 2159 case Builtin::BI__builtin_ms_va_start: 2160 case Builtin::BI__builtin_stdarg_start: 2161 case Builtin::BI__builtin_va_start: 2162 if (BuiltinVAStart(BuiltinID, TheCall)) 2163 return ExprError(); 2164 break; 2165 case Builtin::BI__va_start: { 2166 switch (Context.getTargetInfo().getTriple().getArch()) { 2167 case llvm::Triple::aarch64: 2168 case llvm::Triple::arm: 2169 case llvm::Triple::thumb: 2170 if (BuiltinVAStartARMMicrosoft(TheCall)) 2171 return ExprError(); 2172 break; 2173 default: 2174 if (BuiltinVAStart(BuiltinID, TheCall)) 2175 return ExprError(); 2176 break; 2177 } 2178 break; 2179 } 2180 2181 // The acquire, release, and no fence variants are ARM and AArch64 only. 2182 case Builtin::BI_interlockedbittestandset_acq: 2183 case Builtin::BI_interlockedbittestandset_rel: 2184 case Builtin::BI_interlockedbittestandset_nf: 2185 case Builtin::BI_interlockedbittestandreset_acq: 2186 case Builtin::BI_interlockedbittestandreset_rel: 2187 case Builtin::BI_interlockedbittestandreset_nf: 2188 if (CheckBuiltinTargetInSupported( 2189 *this, TheCall, 2190 {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64})) 2191 return ExprError(); 2192 break; 2193 2194 // The 64-bit bittest variants are x64, ARM, and AArch64 only. 2195 case Builtin::BI_bittest64: 2196 case Builtin::BI_bittestandcomplement64: 2197 case Builtin::BI_bittestandreset64: 2198 case Builtin::BI_bittestandset64: 2199 case Builtin::BI_interlockedbittestandreset64: 2200 case Builtin::BI_interlockedbittestandset64: 2201 if (CheckBuiltinTargetInSupported( 2202 *this, TheCall, 2203 {llvm::Triple::x86_64, llvm::Triple::arm, llvm::Triple::thumb, 2204 llvm::Triple::aarch64, llvm::Triple::amdgcn})) 2205 return ExprError(); 2206 break; 2207 2208 case Builtin::BI__builtin_set_flt_rounds: 2209 if (CheckBuiltinTargetInSupported( 2210 *this, TheCall, 2211 {llvm::Triple::x86, llvm::Triple::x86_64, llvm::Triple::arm, 2212 llvm::Triple::thumb, llvm::Triple::aarch64, llvm::Triple::amdgcn, 2213 llvm::Triple::ppc, llvm::Triple::ppc64, llvm::Triple::ppcle, 2214 llvm::Triple::ppc64le})) 2215 return ExprError(); 2216 break; 2217 2218 case Builtin::BI__builtin_isgreater: 2219 case Builtin::BI__builtin_isgreaterequal: 2220 case Builtin::BI__builtin_isless: 2221 case Builtin::BI__builtin_islessequal: 2222 case Builtin::BI__builtin_islessgreater: 2223 case Builtin::BI__builtin_isunordered: 2224 if (BuiltinUnorderedCompare(TheCall, BuiltinID)) 2225 return ExprError(); 2226 break; 2227 case Builtin::BI__builtin_fpclassify: 2228 if (BuiltinFPClassification(TheCall, 6, BuiltinID)) 2229 return ExprError(); 2230 break; 2231 case Builtin::BI__builtin_isfpclass: 2232 if (BuiltinFPClassification(TheCall, 2, BuiltinID)) 2233 return ExprError(); 2234 break; 2235 case Builtin::BI__builtin_isfinite: 2236 case Builtin::BI__builtin_isinf: 2237 case Builtin::BI__builtin_isinf_sign: 2238 case Builtin::BI__builtin_isnan: 2239 case Builtin::BI__builtin_issignaling: 2240 case Builtin::BI__builtin_isnormal: 2241 case Builtin::BI__builtin_issubnormal: 2242 case Builtin::BI__builtin_iszero: 2243 case Builtin::BI__builtin_signbit: 2244 case Builtin::BI__builtin_signbitf: 2245 case Builtin::BI__builtin_signbitl: 2246 if (BuiltinFPClassification(TheCall, 1, BuiltinID)) 2247 return ExprError(); 2248 break; 2249 case Builtin::BI__builtin_shufflevector: 2250 return BuiltinShuffleVector(TheCall); 2251 // TheCall will be freed by the smart pointer here, but that's fine, since 2252 // BuiltinShuffleVector guts it, but then doesn't release it. 2253 case Builtin::BI__builtin_prefetch: 2254 if (BuiltinPrefetch(TheCall)) 2255 return ExprError(); 2256 break; 2257 case Builtin::BI__builtin_alloca_with_align: 2258 case Builtin::BI__builtin_alloca_with_align_uninitialized: 2259 if (BuiltinAllocaWithAlign(TheCall)) 2260 return ExprError(); 2261 [[fallthrough]]; 2262 case Builtin::BI__builtin_alloca: 2263 case Builtin::BI__builtin_alloca_uninitialized: 2264 Diag(TheCall->getBeginLoc(), diag::warn_alloca) 2265 << TheCall->getDirectCallee(); 2266 if (getLangOpts().OpenCL) { 2267 builtinAllocaAddrSpace(*this, TheCall); 2268 } 2269 break; 2270 case Builtin::BI__arithmetic_fence: 2271 if (BuiltinArithmeticFence(TheCall)) 2272 return ExprError(); 2273 break; 2274 case Builtin::BI__assume: 2275 case Builtin::BI__builtin_assume: 2276 if (BuiltinAssume(TheCall)) 2277 return ExprError(); 2278 break; 2279 case Builtin::BI__builtin_assume_aligned: 2280 if (BuiltinAssumeAligned(TheCall)) 2281 return ExprError(); 2282 break; 2283 case Builtin::BI__builtin_dynamic_object_size: 2284 case Builtin::BI__builtin_object_size: 2285 if (BuiltinConstantArgRange(TheCall, 1, 0, 3)) 2286 return ExprError(); 2287 break; 2288 case Builtin::BI__builtin_longjmp: 2289 if (BuiltinLongjmp(TheCall)) 2290 return ExprError(); 2291 break; 2292 case Builtin::BI__builtin_setjmp: 2293 if (BuiltinSetjmp(TheCall)) 2294 return ExprError(); 2295 break; 2296 case Builtin::BI__builtin_classify_type: 2297 if (checkArgCount(TheCall, 1)) 2298 return true; 2299 TheCall->setType(Context.IntTy); 2300 break; 2301 case Builtin::BI__builtin_complex: 2302 if (BuiltinComplex(TheCall)) 2303 return ExprError(); 2304 break; 2305 case Builtin::BI__builtin_constant_p: { 2306 if (checkArgCount(TheCall, 1)) 2307 return true; 2308 ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 2309 if (Arg.isInvalid()) return true; 2310 TheCall->setArg(0, Arg.get()); 2311 TheCall->setType(Context.IntTy); 2312 break; 2313 } 2314 case Builtin::BI__builtin_launder: 2315 return BuiltinLaunder(*this, TheCall); 2316 case Builtin::BI__builtin_is_within_lifetime: 2317 return BuiltinIsWithinLifetime(*this, TheCall); 2318 case Builtin::BI__sync_fetch_and_add: 2319 case Builtin::BI__sync_fetch_and_add_1: 2320 case Builtin::BI__sync_fetch_and_add_2: 2321 case Builtin::BI__sync_fetch_and_add_4: 2322 case Builtin::BI__sync_fetch_and_add_8: 2323 case Builtin::BI__sync_fetch_and_add_16: 2324 case Builtin::BI__sync_fetch_and_sub: 2325 case Builtin::BI__sync_fetch_and_sub_1: 2326 case Builtin::BI__sync_fetch_and_sub_2: 2327 case Builtin::BI__sync_fetch_and_sub_4: 2328 case Builtin::BI__sync_fetch_and_sub_8: 2329 case Builtin::BI__sync_fetch_and_sub_16: 2330 case Builtin::BI__sync_fetch_and_or: 2331 case Builtin::BI__sync_fetch_and_or_1: 2332 case Builtin::BI__sync_fetch_and_or_2: 2333 case Builtin::BI__sync_fetch_and_or_4: 2334 case Builtin::BI__sync_fetch_and_or_8: 2335 case Builtin::BI__sync_fetch_and_or_16: 2336 case Builtin::BI__sync_fetch_and_and: 2337 case Builtin::BI__sync_fetch_and_and_1: 2338 case Builtin::BI__sync_fetch_and_and_2: 2339 case Builtin::BI__sync_fetch_and_and_4: 2340 case Builtin::BI__sync_fetch_and_and_8: 2341 case Builtin::BI__sync_fetch_and_and_16: 2342 case Builtin::BI__sync_fetch_and_xor: 2343 case Builtin::BI__sync_fetch_and_xor_1: 2344 case Builtin::BI__sync_fetch_and_xor_2: 2345 case Builtin::BI__sync_fetch_and_xor_4: 2346 case Builtin::BI__sync_fetch_and_xor_8: 2347 case Builtin::BI__sync_fetch_and_xor_16: 2348 case Builtin::BI__sync_fetch_and_nand: 2349 case Builtin::BI__sync_fetch_and_nand_1: 2350 case Builtin::BI__sync_fetch_and_nand_2: 2351 case Builtin::BI__sync_fetch_and_nand_4: 2352 case Builtin::BI__sync_fetch_and_nand_8: 2353 case Builtin::BI__sync_fetch_and_nand_16: 2354 case Builtin::BI__sync_add_and_fetch: 2355 case Builtin::BI__sync_add_and_fetch_1: 2356 case Builtin::BI__sync_add_and_fetch_2: 2357 case Builtin::BI__sync_add_and_fetch_4: 2358 case Builtin::BI__sync_add_and_fetch_8: 2359 case Builtin::BI__sync_add_and_fetch_16: 2360 case Builtin::BI__sync_sub_and_fetch: 2361 case Builtin::BI__sync_sub_and_fetch_1: 2362 case Builtin::BI__sync_sub_and_fetch_2: 2363 case Builtin::BI__sync_sub_and_fetch_4: 2364 case Builtin::BI__sync_sub_and_fetch_8: 2365 case Builtin::BI__sync_sub_and_fetch_16: 2366 case Builtin::BI__sync_and_and_fetch: 2367 case Builtin::BI__sync_and_and_fetch_1: 2368 case Builtin::BI__sync_and_and_fetch_2: 2369 case Builtin::BI__sync_and_and_fetch_4: 2370 case Builtin::BI__sync_and_and_fetch_8: 2371 case Builtin::BI__sync_and_and_fetch_16: 2372 case Builtin::BI__sync_or_and_fetch: 2373 case Builtin::BI__sync_or_and_fetch_1: 2374 case Builtin::BI__sync_or_and_fetch_2: 2375 case Builtin::BI__sync_or_and_fetch_4: 2376 case Builtin::BI__sync_or_and_fetch_8: 2377 case Builtin::BI__sync_or_and_fetch_16: 2378 case Builtin::BI__sync_xor_and_fetch: 2379 case Builtin::BI__sync_xor_and_fetch_1: 2380 case Builtin::BI__sync_xor_and_fetch_2: 2381 case Builtin::BI__sync_xor_and_fetch_4: 2382 case Builtin::BI__sync_xor_and_fetch_8: 2383 case Builtin::BI__sync_xor_and_fetch_16: 2384 case Builtin::BI__sync_nand_and_fetch: 2385 case Builtin::BI__sync_nand_and_fetch_1: 2386 case Builtin::BI__sync_nand_and_fetch_2: 2387 case Builtin::BI__sync_nand_and_fetch_4: 2388 case Builtin::BI__sync_nand_and_fetch_8: 2389 case Builtin::BI__sync_nand_and_fetch_16: 2390 case Builtin::BI__sync_val_compare_and_swap: 2391 case Builtin::BI__sync_val_compare_and_swap_1: 2392 case Builtin::BI__sync_val_compare_and_swap_2: 2393 case Builtin::BI__sync_val_compare_and_swap_4: 2394 case Builtin::BI__sync_val_compare_and_swap_8: 2395 case Builtin::BI__sync_val_compare_and_swap_16: 2396 case Builtin::BI__sync_bool_compare_and_swap: 2397 case Builtin::BI__sync_bool_compare_and_swap_1: 2398 case Builtin::BI__sync_bool_compare_and_swap_2: 2399 case Builtin::BI__sync_bool_compare_and_swap_4: 2400 case Builtin::BI__sync_bool_compare_and_swap_8: 2401 case Builtin::BI__sync_bool_compare_and_swap_16: 2402 case Builtin::BI__sync_lock_test_and_set: 2403 case Builtin::BI__sync_lock_test_and_set_1: 2404 case Builtin::BI__sync_lock_test_and_set_2: 2405 case Builtin::BI__sync_lock_test_and_set_4: 2406 case Builtin::BI__sync_lock_test_and_set_8: 2407 case Builtin::BI__sync_lock_test_and_set_16: 2408 case Builtin::BI__sync_lock_release: 2409 case Builtin::BI__sync_lock_release_1: 2410 case Builtin::BI__sync_lock_release_2: 2411 case Builtin::BI__sync_lock_release_4: 2412 case Builtin::BI__sync_lock_release_8: 2413 case Builtin::BI__sync_lock_release_16: 2414 case Builtin::BI__sync_swap: 2415 case Builtin::BI__sync_swap_1: 2416 case Builtin::BI__sync_swap_2: 2417 case Builtin::BI__sync_swap_4: 2418 case Builtin::BI__sync_swap_8: 2419 case Builtin::BI__sync_swap_16: 2420 return BuiltinAtomicOverloaded(TheCallResult); 2421 case Builtin::BI__sync_synchronize: 2422 Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst) 2423 << TheCall->getCallee()->getSourceRange(); 2424 break; 2425 case Builtin::BI__builtin_nontemporal_load: 2426 case Builtin::BI__builtin_nontemporal_store: 2427 return BuiltinNontemporalOverloaded(TheCallResult); 2428 case Builtin::BI__builtin_memcpy_inline: { 2429 clang::Expr *SizeOp = TheCall->getArg(2); 2430 // We warn about copying to or from `nullptr` pointers when `size` is 2431 // greater than 0. When `size` is value dependent we cannot evaluate its 2432 // value so we bail out. 2433 if (SizeOp->isValueDependent()) 2434 break; 2435 if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) { 2436 CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); 2437 CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc()); 2438 } 2439 break; 2440 } 2441 case Builtin::BI__builtin_memset_inline: { 2442 clang::Expr *SizeOp = TheCall->getArg(2); 2443 // We warn about filling to `nullptr` pointers when `size` is greater than 2444 // 0. When `size` is value dependent we cannot evaluate its value so we bail 2445 // out. 2446 if (SizeOp->isValueDependent()) 2447 break; 2448 if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) 2449 CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); 2450 break; 2451 } 2452 #define BUILTIN(ID, TYPE, ATTRS) 2453 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 2454 case Builtin::BI##ID: \ 2455 return AtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); 2456 #include "clang/Basic/Builtins.inc" 2457 case Builtin::BI__annotation: 2458 if (BuiltinMSVCAnnotation(*this, TheCall)) 2459 return ExprError(); 2460 break; 2461 case Builtin::BI__builtin_annotation: 2462 if (BuiltinAnnotation(*this, TheCall)) 2463 return ExprError(); 2464 break; 2465 case Builtin::BI__builtin_addressof: 2466 if (BuiltinAddressof(*this, TheCall)) 2467 return ExprError(); 2468 break; 2469 case Builtin::BI__builtin_function_start: 2470 if (BuiltinFunctionStart(*this, TheCall)) 2471 return ExprError(); 2472 break; 2473 case Builtin::BI__builtin_is_aligned: 2474 case Builtin::BI__builtin_align_up: 2475 case Builtin::BI__builtin_align_down: 2476 if (BuiltinAlignment(*this, TheCall, BuiltinID)) 2477 return ExprError(); 2478 break; 2479 case Builtin::BI__builtin_add_overflow: 2480 case Builtin::BI__builtin_sub_overflow: 2481 case Builtin::BI__builtin_mul_overflow: 2482 if (BuiltinOverflow(*this, TheCall, BuiltinID)) 2483 return ExprError(); 2484 break; 2485 case Builtin::BI__builtin_operator_new: 2486 case Builtin::BI__builtin_operator_delete: { 2487 bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete; 2488 ExprResult Res = 2489 BuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete); 2490 if (Res.isInvalid()) 2491 CorrectDelayedTyposInExpr(TheCallResult.get()); 2492 return Res; 2493 } 2494 case Builtin::BI__builtin_dump_struct: 2495 return BuiltinDumpStruct(*this, TheCall); 2496 case Builtin::BI__builtin_expect_with_probability: { 2497 // We first want to ensure we are called with 3 arguments 2498 if (checkArgCount(TheCall, 3)) 2499 return ExprError(); 2500 // then check probability is constant float in range [0.0, 1.0] 2501 const Expr *ProbArg = TheCall->getArg(2); 2502 SmallVector<PartialDiagnosticAt, 8> Notes; 2503 Expr::EvalResult Eval; 2504 Eval.Diag = &Notes; 2505 if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) || 2506 !Eval.Val.isFloat()) { 2507 Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float) 2508 << ProbArg->getSourceRange(); 2509 for (const PartialDiagnosticAt &PDiag : Notes) 2510 Diag(PDiag.first, PDiag.second); 2511 return ExprError(); 2512 } 2513 llvm::APFloat Probability = Eval.Val.getFloat(); 2514 bool LoseInfo = false; 2515 Probability.convert(llvm::APFloat::IEEEdouble(), 2516 llvm::RoundingMode::Dynamic, &LoseInfo); 2517 if (!(Probability >= llvm::APFloat(0.0) && 2518 Probability <= llvm::APFloat(1.0))) { 2519 Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range) 2520 << ProbArg->getSourceRange(); 2521 return ExprError(); 2522 } 2523 break; 2524 } 2525 case Builtin::BI__builtin_preserve_access_index: 2526 if (BuiltinPreserveAI(*this, TheCall)) 2527 return ExprError(); 2528 break; 2529 case Builtin::BI__builtin_call_with_static_chain: 2530 if (BuiltinCallWithStaticChain(*this, TheCall)) 2531 return ExprError(); 2532 break; 2533 case Builtin::BI__exception_code: 2534 case Builtin::BI_exception_code: 2535 if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, 2536 diag::err_seh___except_block)) 2537 return ExprError(); 2538 break; 2539 case Builtin::BI__exception_info: 2540 case Builtin::BI_exception_info: 2541 if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, 2542 diag::err_seh___except_filter)) 2543 return ExprError(); 2544 break; 2545 case Builtin::BI__GetExceptionInfo: 2546 if (checkArgCount(TheCall, 1)) 2547 return ExprError(); 2548 2549 if (CheckCXXThrowOperand( 2550 TheCall->getBeginLoc(), 2551 Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), 2552 TheCall)) 2553 return ExprError(); 2554 2555 TheCall->setType(Context.VoidPtrTy); 2556 break; 2557 case Builtin::BIaddressof: 2558 case Builtin::BI__addressof: 2559 case Builtin::BIforward: 2560 case Builtin::BIforward_like: 2561 case Builtin::BImove: 2562 case Builtin::BImove_if_noexcept: 2563 case Builtin::BIas_const: { 2564 // These are all expected to be of the form 2565 // T &/&&/* f(U &/&&) 2566 // where T and U only differ in qualification. 2567 if (checkArgCount(TheCall, 1)) 2568 return ExprError(); 2569 QualType Param = FDecl->getParamDecl(0)->getType(); 2570 QualType Result = FDecl->getReturnType(); 2571 bool ReturnsPointer = BuiltinID == Builtin::BIaddressof || 2572 BuiltinID == Builtin::BI__addressof; 2573 if (!(Param->isReferenceType() && 2574 (ReturnsPointer ? Result->isAnyPointerType() 2575 : Result->isReferenceType()) && 2576 Context.hasSameUnqualifiedType(Param->getPointeeType(), 2577 Result->getPointeeType()))) { 2578 Diag(TheCall->getBeginLoc(), diag::err_builtin_move_forward_unsupported) 2579 << FDecl; 2580 return ExprError(); 2581 } 2582 break; 2583 } 2584 case Builtin::BI__builtin_ptrauth_strip: 2585 return PointerAuthStrip(*this, TheCall); 2586 case Builtin::BI__builtin_ptrauth_blend_discriminator: 2587 return PointerAuthBlendDiscriminator(*this, TheCall); 2588 case Builtin::BI__builtin_ptrauth_sign_constant: 2589 return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign, 2590 /*RequireConstant=*/true); 2591 case Builtin::BI__builtin_ptrauth_sign_unauthenticated: 2592 return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign, 2593 /*RequireConstant=*/false); 2594 case Builtin::BI__builtin_ptrauth_auth: 2595 return PointerAuthSignOrAuth(*this, TheCall, PAO_Auth, 2596 /*RequireConstant=*/false); 2597 case Builtin::BI__builtin_ptrauth_sign_generic_data: 2598 return PointerAuthSignGenericData(*this, TheCall); 2599 case Builtin::BI__builtin_ptrauth_auth_and_resign: 2600 return PointerAuthAuthAndResign(*this, TheCall); 2601 case Builtin::BI__builtin_ptrauth_string_discriminator: 2602 return PointerAuthStringDiscriminator(*this, TheCall); 2603 // OpenCL v2.0, s6.13.16 - Pipe functions 2604 case Builtin::BIread_pipe: 2605 case Builtin::BIwrite_pipe: 2606 // Since those two functions are declared with var args, we need a semantic 2607 // check for the argument. 2608 if (OpenCL().checkBuiltinRWPipe(TheCall)) 2609 return ExprError(); 2610 break; 2611 case Builtin::BIreserve_read_pipe: 2612 case Builtin::BIreserve_write_pipe: 2613 case Builtin::BIwork_group_reserve_read_pipe: 2614 case Builtin::BIwork_group_reserve_write_pipe: 2615 if (OpenCL().checkBuiltinReserveRWPipe(TheCall)) 2616 return ExprError(); 2617 break; 2618 case Builtin::BIsub_group_reserve_read_pipe: 2619 case Builtin::BIsub_group_reserve_write_pipe: 2620 if (OpenCL().checkSubgroupExt(TheCall) || 2621 OpenCL().checkBuiltinReserveRWPipe(TheCall)) 2622 return ExprError(); 2623 break; 2624 case Builtin::BIcommit_read_pipe: 2625 case Builtin::BIcommit_write_pipe: 2626 case Builtin::BIwork_group_commit_read_pipe: 2627 case Builtin::BIwork_group_commit_write_pipe: 2628 if (OpenCL().checkBuiltinCommitRWPipe(TheCall)) 2629 return ExprError(); 2630 break; 2631 case Builtin::BIsub_group_commit_read_pipe: 2632 case Builtin::BIsub_group_commit_write_pipe: 2633 if (OpenCL().checkSubgroupExt(TheCall) || 2634 OpenCL().checkBuiltinCommitRWPipe(TheCall)) 2635 return ExprError(); 2636 break; 2637 case Builtin::BIget_pipe_num_packets: 2638 case Builtin::BIget_pipe_max_packets: 2639 if (OpenCL().checkBuiltinPipePackets(TheCall)) 2640 return ExprError(); 2641 break; 2642 case Builtin::BIto_global: 2643 case Builtin::BIto_local: 2644 case Builtin::BIto_private: 2645 if (OpenCL().checkBuiltinToAddr(BuiltinID, TheCall)) 2646 return ExprError(); 2647 break; 2648 // OpenCL v2.0, s6.13.17 - Enqueue kernel functions. 2649 case Builtin::BIenqueue_kernel: 2650 if (OpenCL().checkBuiltinEnqueueKernel(TheCall)) 2651 return ExprError(); 2652 break; 2653 case Builtin::BIget_kernel_work_group_size: 2654 case Builtin::BIget_kernel_preferred_work_group_size_multiple: 2655 if (OpenCL().checkBuiltinKernelWorkGroupSize(TheCall)) 2656 return ExprError(); 2657 break; 2658 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange: 2659 case Builtin::BIget_kernel_sub_group_count_for_ndrange: 2660 if (OpenCL().checkBuiltinNDRangeAndBlock(TheCall)) 2661 return ExprError(); 2662 break; 2663 case Builtin::BI__builtin_os_log_format: 2664 Cleanup.setExprNeedsCleanups(true); 2665 [[fallthrough]]; 2666 case Builtin::BI__builtin_os_log_format_buffer_size: 2667 if (BuiltinOSLogFormat(TheCall)) 2668 return ExprError(); 2669 break; 2670 case Builtin::BI__builtin_frame_address: 2671 case Builtin::BI__builtin_return_address: { 2672 if (BuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF)) 2673 return ExprError(); 2674 2675 // -Wframe-address warning if non-zero passed to builtin 2676 // return/frame address. 2677 Expr::EvalResult Result; 2678 if (!TheCall->getArg(0)->isValueDependent() && 2679 TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) && 2680 Result.Val.getInt() != 0) 2681 Diag(TheCall->getBeginLoc(), diag::warn_frame_address) 2682 << ((BuiltinID == Builtin::BI__builtin_return_address) 2683 ? "__builtin_return_address" 2684 : "__builtin_frame_address") 2685 << TheCall->getSourceRange(); 2686 break; 2687 } 2688 2689 case Builtin::BI__builtin_nondeterministic_value: { 2690 if (BuiltinNonDeterministicValue(TheCall)) 2691 return ExprError(); 2692 break; 2693 } 2694 2695 // __builtin_elementwise_abs restricts the element type to signed integers or 2696 // floating point types only. 2697 case Builtin::BI__builtin_elementwise_abs: { 2698 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2699 return ExprError(); 2700 2701 QualType ArgTy = TheCall->getArg(0)->getType(); 2702 QualType EltTy = ArgTy; 2703 2704 if (auto *VecTy = EltTy->getAs<VectorType>()) 2705 EltTy = VecTy->getElementType(); 2706 if (EltTy->isUnsignedIntegerType()) { 2707 Diag(TheCall->getArg(0)->getBeginLoc(), 2708 diag::err_builtin_invalid_arg_type) 2709 << 1 << /* signed integer or float ty*/ 3 << ArgTy; 2710 return ExprError(); 2711 } 2712 break; 2713 } 2714 2715 // These builtins restrict the element type to floating point 2716 // types only. 2717 case Builtin::BI__builtin_elementwise_acos: 2718 case Builtin::BI__builtin_elementwise_asin: 2719 case Builtin::BI__builtin_elementwise_atan: 2720 case Builtin::BI__builtin_elementwise_ceil: 2721 case Builtin::BI__builtin_elementwise_cos: 2722 case Builtin::BI__builtin_elementwise_cosh: 2723 case Builtin::BI__builtin_elementwise_exp: 2724 case Builtin::BI__builtin_elementwise_exp2: 2725 case Builtin::BI__builtin_elementwise_floor: 2726 case Builtin::BI__builtin_elementwise_log: 2727 case Builtin::BI__builtin_elementwise_log2: 2728 case Builtin::BI__builtin_elementwise_log10: 2729 case Builtin::BI__builtin_elementwise_roundeven: 2730 case Builtin::BI__builtin_elementwise_round: 2731 case Builtin::BI__builtin_elementwise_rint: 2732 case Builtin::BI__builtin_elementwise_nearbyint: 2733 case Builtin::BI__builtin_elementwise_sin: 2734 case Builtin::BI__builtin_elementwise_sinh: 2735 case Builtin::BI__builtin_elementwise_sqrt: 2736 case Builtin::BI__builtin_elementwise_tan: 2737 case Builtin::BI__builtin_elementwise_tanh: 2738 case Builtin::BI__builtin_elementwise_trunc: 2739 case Builtin::BI__builtin_elementwise_canonicalize: { 2740 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2741 return ExprError(); 2742 2743 QualType ArgTy = TheCall->getArg(0)->getType(); 2744 if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), 2745 ArgTy, 1)) 2746 return ExprError(); 2747 break; 2748 } 2749 case Builtin::BI__builtin_elementwise_fma: { 2750 if (BuiltinElementwiseTernaryMath(TheCall)) 2751 return ExprError(); 2752 break; 2753 } 2754 2755 // These builtins restrict the element type to floating point 2756 // types only, and take in two arguments. 2757 case Builtin::BI__builtin_elementwise_minimum: 2758 case Builtin::BI__builtin_elementwise_maximum: 2759 case Builtin::BI__builtin_elementwise_atan2: 2760 case Builtin::BI__builtin_elementwise_fmod: 2761 case Builtin::BI__builtin_elementwise_pow: { 2762 if (BuiltinElementwiseMath(TheCall, /*FPOnly=*/true)) 2763 return ExprError(); 2764 break; 2765 } 2766 2767 // These builtins restrict the element type to integer 2768 // types only. 2769 case Builtin::BI__builtin_elementwise_add_sat: 2770 case Builtin::BI__builtin_elementwise_sub_sat: { 2771 if (BuiltinElementwiseMath(TheCall)) 2772 return ExprError(); 2773 2774 const Expr *Arg = TheCall->getArg(0); 2775 QualType ArgTy = Arg->getType(); 2776 QualType EltTy = ArgTy; 2777 2778 if (auto *VecTy = EltTy->getAs<VectorType>()) 2779 EltTy = VecTy->getElementType(); 2780 2781 if (!EltTy->isIntegerType()) { 2782 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2783 << 1 << /* integer ty */ 6 << ArgTy; 2784 return ExprError(); 2785 } 2786 break; 2787 } 2788 2789 case Builtin::BI__builtin_elementwise_min: 2790 case Builtin::BI__builtin_elementwise_max: 2791 if (BuiltinElementwiseMath(TheCall)) 2792 return ExprError(); 2793 break; 2794 case Builtin::BI__builtin_elementwise_popcount: 2795 case Builtin::BI__builtin_elementwise_bitreverse: { 2796 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2797 return ExprError(); 2798 2799 const Expr *Arg = TheCall->getArg(0); 2800 QualType ArgTy = Arg->getType(); 2801 QualType EltTy = ArgTy; 2802 2803 if (auto *VecTy = EltTy->getAs<VectorType>()) 2804 EltTy = VecTy->getElementType(); 2805 2806 if (!EltTy->isIntegerType()) { 2807 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2808 << 1 << /* integer ty */ 6 << ArgTy; 2809 return ExprError(); 2810 } 2811 break; 2812 } 2813 2814 case Builtin::BI__builtin_elementwise_copysign: { 2815 if (checkArgCount(TheCall, 2)) 2816 return ExprError(); 2817 2818 ExprResult Magnitude = UsualUnaryConversions(TheCall->getArg(0)); 2819 ExprResult Sign = UsualUnaryConversions(TheCall->getArg(1)); 2820 if (Magnitude.isInvalid() || Sign.isInvalid()) 2821 return ExprError(); 2822 2823 QualType MagnitudeTy = Magnitude.get()->getType(); 2824 QualType SignTy = Sign.get()->getType(); 2825 if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), 2826 MagnitudeTy, 1) || 2827 checkFPMathBuiltinElementType(*this, TheCall->getArg(1)->getBeginLoc(), 2828 SignTy, 2)) { 2829 return ExprError(); 2830 } 2831 2832 if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) { 2833 return Diag(Sign.get()->getBeginLoc(), 2834 diag::err_typecheck_call_different_arg_types) 2835 << MagnitudeTy << SignTy; 2836 } 2837 2838 TheCall->setArg(0, Magnitude.get()); 2839 TheCall->setArg(1, Sign.get()); 2840 TheCall->setType(Magnitude.get()->getType()); 2841 break; 2842 } 2843 case Builtin::BI__builtin_reduce_max: 2844 case Builtin::BI__builtin_reduce_min: { 2845 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2846 return ExprError(); 2847 2848 const Expr *Arg = TheCall->getArg(0); 2849 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2850 2851 QualType ElTy; 2852 if (TyA) 2853 ElTy = TyA->getElementType(); 2854 else if (Arg->getType()->isSizelessVectorType()) 2855 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2856 2857 if (ElTy.isNull()) { 2858 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2859 << 1 << /* vector ty*/ 4 << Arg->getType(); 2860 return ExprError(); 2861 } 2862 2863 TheCall->setType(ElTy); 2864 break; 2865 } 2866 case Builtin::BI__builtin_reduce_maximum: 2867 case Builtin::BI__builtin_reduce_minimum: { 2868 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2869 return ExprError(); 2870 2871 const Expr *Arg = TheCall->getArg(0); 2872 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2873 2874 QualType ElTy; 2875 if (TyA) 2876 ElTy = TyA->getElementType(); 2877 else if (Arg->getType()->isSizelessVectorType()) 2878 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2879 2880 if (ElTy.isNull() || !ElTy->isFloatingType()) { 2881 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2882 << 1 << /* vector of floating points */ 9 << Arg->getType(); 2883 return ExprError(); 2884 } 2885 2886 TheCall->setType(ElTy); 2887 break; 2888 } 2889 2890 // These builtins support vectors of integers only. 2891 // TODO: ADD/MUL should support floating-point types. 2892 case Builtin::BI__builtin_reduce_add: 2893 case Builtin::BI__builtin_reduce_mul: 2894 case Builtin::BI__builtin_reduce_xor: 2895 case Builtin::BI__builtin_reduce_or: 2896 case Builtin::BI__builtin_reduce_and: { 2897 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2898 return ExprError(); 2899 2900 const Expr *Arg = TheCall->getArg(0); 2901 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2902 2903 QualType ElTy; 2904 if (TyA) 2905 ElTy = TyA->getElementType(); 2906 else if (Arg->getType()->isSizelessVectorType()) 2907 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2908 2909 if (ElTy.isNull() || !ElTy->isIntegerType()) { 2910 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2911 << 1 << /* vector of integers */ 6 << Arg->getType(); 2912 return ExprError(); 2913 } 2914 2915 TheCall->setType(ElTy); 2916 break; 2917 } 2918 2919 case Builtin::BI__builtin_matrix_transpose: 2920 return BuiltinMatrixTranspose(TheCall, TheCallResult); 2921 2922 case Builtin::BI__builtin_matrix_column_major_load: 2923 return BuiltinMatrixColumnMajorLoad(TheCall, TheCallResult); 2924 2925 case Builtin::BI__builtin_matrix_column_major_store: 2926 return BuiltinMatrixColumnMajorStore(TheCall, TheCallResult); 2927 2928 case Builtin::BI__builtin_verbose_trap: 2929 if (!checkBuiltinVerboseTrap(TheCall, *this)) 2930 return ExprError(); 2931 break; 2932 2933 case Builtin::BI__builtin_get_device_side_mangled_name: { 2934 auto Check = [](CallExpr *TheCall) { 2935 if (TheCall->getNumArgs() != 1) 2936 return false; 2937 auto *DRE = dyn_cast<DeclRefExpr>(TheCall->getArg(0)->IgnoreImpCasts()); 2938 if (!DRE) 2939 return false; 2940 auto *D = DRE->getDecl(); 2941 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) 2942 return false; 2943 return D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<CUDADeviceAttr>() || 2944 D->hasAttr<CUDAConstantAttr>() || D->hasAttr<HIPManagedAttr>(); 2945 }; 2946 if (!Check(TheCall)) { 2947 Diag(TheCall->getBeginLoc(), 2948 diag::err_hip_invalid_args_builtin_mangled_name); 2949 return ExprError(); 2950 } 2951 break; 2952 } 2953 case Builtin::BI__builtin_popcountg: 2954 if (BuiltinPopcountg(*this, TheCall)) 2955 return ExprError(); 2956 break; 2957 case Builtin::BI__builtin_clzg: 2958 case Builtin::BI__builtin_ctzg: 2959 if (BuiltinCountZeroBitsGeneric(*this, TheCall)) 2960 return ExprError(); 2961 break; 2962 2963 case Builtin::BI__builtin_allow_runtime_check: { 2964 Expr *Arg = TheCall->getArg(0); 2965 // Check if the argument is a string literal. 2966 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) { 2967 Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 2968 << Arg->getSourceRange(); 2969 return ExprError(); 2970 } 2971 break; 2972 } 2973 case Builtin::BI__builtin_counted_by_ref: 2974 if (BuiltinCountedByRef(TheCall)) 2975 return ExprError(); 2976 break; 2977 } 2978 2979 if (getLangOpts().HLSL && HLSL().CheckBuiltinFunctionCall(BuiltinID, TheCall)) 2980 return ExprError(); 2981 2982 // Since the target specific builtins for each arch overlap, only check those 2983 // of the arch we are compiling for. 2984 if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) { 2985 if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) { 2986 assert(Context.getAuxTargetInfo() && 2987 "Aux Target Builtin, but not an aux target?"); 2988 2989 if (CheckTSBuiltinFunctionCall( 2990 *Context.getAuxTargetInfo(), 2991 Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall)) 2992 return ExprError(); 2993 } else { 2994 if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID, 2995 TheCall)) 2996 return ExprError(); 2997 } 2998 } 2999 3000 return TheCallResult; 3001 } 3002 3003 bool Sema::ValueIsRunOfOnes(CallExpr *TheCall, unsigned ArgNum) { 3004 llvm::APSInt Result; 3005 // We can't check the value of a dependent argument. 3006 Expr *Arg = TheCall->getArg(ArgNum); 3007 if (Arg->isTypeDependent() || Arg->isValueDependent()) 3008 return false; 3009 3010 // Check constant-ness first. 3011 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 3012 return true; 3013 3014 // Check contiguous run of 1s, 0xFF0000FF is also a run of 1s. 3015 if (Result.isShiftedMask() || (~Result).isShiftedMask()) 3016 return false; 3017 3018 return Diag(TheCall->getBeginLoc(), 3019 diag::err_argument_not_contiguous_bit_field) 3020 << ArgNum << Arg->getSourceRange(); 3021 } 3022 3023 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, 3024 bool IsVariadic, FormatStringInfo *FSI) { 3025 if (Format->getFirstArg() == 0) 3026 FSI->ArgPassingKind = FAPK_VAList; 3027 else if (IsVariadic) 3028 FSI->ArgPassingKind = FAPK_Variadic; 3029 else 3030 FSI->ArgPassingKind = FAPK_Fixed; 3031 FSI->FormatIdx = Format->getFormatIdx() - 1; 3032 FSI->FirstDataArg = 3033 FSI->ArgPassingKind == FAPK_VAList ? 0 : Format->getFirstArg() - 1; 3034 3035 // The way the format attribute works in GCC, the implicit this argument 3036 // of member functions is counted. However, it doesn't appear in our own 3037 // lists, so decrement format_idx in that case. 3038 if (IsCXXMember) { 3039 if(FSI->FormatIdx == 0) 3040 return false; 3041 --FSI->FormatIdx; 3042 if (FSI->FirstDataArg != 0) 3043 --FSI->FirstDataArg; 3044 } 3045 return true; 3046 } 3047 3048 /// Checks if a the given expression evaluates to null. 3049 /// 3050 /// Returns true if the value evaluates to null. 3051 static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { 3052 // Treat (smart) pointers constructed from nullptr as null, whether we can 3053 // const-evaluate them or not. 3054 // This must happen first: the smart pointer expr might have _Nonnull type! 3055 if (isa<CXXNullPtrLiteralExpr>( 3056 IgnoreExprNodes(Expr, IgnoreImplicitAsWrittenSingleStep, 3057 IgnoreElidableImplicitConstructorSingleStep))) 3058 return true; 3059 3060 // If the expression has non-null type, it doesn't evaluate to null. 3061 if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) { 3062 if (*nullability == NullabilityKind::NonNull) 3063 return false; 3064 } 3065 3066 // As a special case, transparent unions initialized with zero are 3067 // considered null for the purposes of the nonnull attribute. 3068 if (const RecordType *UT = Expr->getType()->getAsUnionType(); 3069 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { 3070 if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(Expr)) 3071 if (const auto *ILE = dyn_cast<InitListExpr>(CLE->getInitializer())) 3072 Expr = ILE->getInit(0); 3073 } 3074 3075 bool Result; 3076 return (!Expr->isValueDependent() && 3077 Expr->EvaluateAsBooleanCondition(Result, S.Context) && 3078 !Result); 3079 } 3080 3081 static void CheckNonNullArgument(Sema &S, 3082 const Expr *ArgExpr, 3083 SourceLocation CallSiteLoc) { 3084 if (CheckNonNullExpr(S, ArgExpr)) 3085 S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr, 3086 S.PDiag(diag::warn_null_arg) 3087 << ArgExpr->getSourceRange()); 3088 } 3089 3090 /// Determine whether the given type has a non-null nullability annotation. 3091 static bool isNonNullType(QualType type) { 3092 if (auto nullability = type->getNullability()) 3093 return *nullability == NullabilityKind::NonNull; 3094 3095 return false; 3096 } 3097 3098 static void CheckNonNullArguments(Sema &S, 3099 const NamedDecl *FDecl, 3100 const FunctionProtoType *Proto, 3101 ArrayRef<const Expr *> Args, 3102 SourceLocation CallSiteLoc) { 3103 assert((FDecl || Proto) && "Need a function declaration or prototype"); 3104 3105 // Already checked by constant evaluator. 3106 if (S.isConstantEvaluatedContext()) 3107 return; 3108 // Check the attributes attached to the method/function itself. 3109 llvm::SmallBitVector NonNullArgs; 3110 if (FDecl) { 3111 // Handle the nonnull attribute on the function/method declaration itself. 3112 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { 3113 if (!NonNull->args_size()) { 3114 // Easy case: all pointer arguments are nonnull. 3115 for (const auto *Arg : Args) 3116 if (S.isValidPointerAttrType(Arg->getType())) 3117 CheckNonNullArgument(S, Arg, CallSiteLoc); 3118 return; 3119 } 3120 3121 for (const ParamIdx &Idx : NonNull->args()) { 3122 unsigned IdxAST = Idx.getASTIndex(); 3123 if (IdxAST >= Args.size()) 3124 continue; 3125 if (NonNullArgs.empty()) 3126 NonNullArgs.resize(Args.size()); 3127 NonNullArgs.set(IdxAST); 3128 } 3129 } 3130 } 3131 3132 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) { 3133 // Handle the nonnull attribute on the parameters of the 3134 // function/method. 3135 ArrayRef<ParmVarDecl*> parms; 3136 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) 3137 parms = FD->parameters(); 3138 else 3139 parms = cast<ObjCMethodDecl>(FDecl)->parameters(); 3140 3141 unsigned ParamIndex = 0; 3142 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); 3143 I != E; ++I, ++ParamIndex) { 3144 const ParmVarDecl *PVD = *I; 3145 if (PVD->hasAttr<NonNullAttr>() || isNonNullType(PVD->getType())) { 3146 if (NonNullArgs.empty()) 3147 NonNullArgs.resize(Args.size()); 3148 3149 NonNullArgs.set(ParamIndex); 3150 } 3151 } 3152 } else { 3153 // If we have a non-function, non-method declaration but no 3154 // function prototype, try to dig out the function prototype. 3155 if (!Proto) { 3156 if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) { 3157 QualType type = VD->getType().getNonReferenceType(); 3158 if (auto pointerType = type->getAs<PointerType>()) 3159 type = pointerType->getPointeeType(); 3160 else if (auto blockType = type->getAs<BlockPointerType>()) 3161 type = blockType->getPointeeType(); 3162 // FIXME: data member pointers? 3163 3164 // Dig out the function prototype, if there is one. 3165 Proto = type->getAs<FunctionProtoType>(); 3166 } 3167 } 3168 3169 // Fill in non-null argument information from the nullability 3170 // information on the parameter types (if we have them). 3171 if (Proto) { 3172 unsigned Index = 0; 3173 for (auto paramType : Proto->getParamTypes()) { 3174 if (isNonNullType(paramType)) { 3175 if (NonNullArgs.empty()) 3176 NonNullArgs.resize(Args.size()); 3177 3178 NonNullArgs.set(Index); 3179 } 3180 3181 ++Index; 3182 } 3183 } 3184 } 3185 3186 // Check for non-null arguments. 3187 for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size(); 3188 ArgIndex != ArgIndexEnd; ++ArgIndex) { 3189 if (NonNullArgs[ArgIndex]) 3190 CheckNonNullArgument(S, Args[ArgIndex], Args[ArgIndex]->getExprLoc()); 3191 } 3192 } 3193 3194 void Sema::CheckArgAlignment(SourceLocation Loc, NamedDecl *FDecl, 3195 StringRef ParamName, QualType ArgTy, 3196 QualType ParamTy) { 3197 3198 // If a function accepts a pointer or reference type 3199 if (!ParamTy->isPointerType() && !ParamTy->isReferenceType()) 3200 return; 3201 3202 // If the parameter is a pointer type, get the pointee type for the 3203 // argument too. If the parameter is a reference type, don't try to get 3204 // the pointee type for the argument. 3205 if (ParamTy->isPointerType()) 3206 ArgTy = ArgTy->getPointeeType(); 3207 3208 // Remove reference or pointer 3209 ParamTy = ParamTy->getPointeeType(); 3210 3211 // Find expected alignment, and the actual alignment of the passed object. 3212 // getTypeAlignInChars requires complete types 3213 if (ArgTy.isNull() || ParamTy->isDependentType() || 3214 ParamTy->isIncompleteType() || ArgTy->isIncompleteType() || 3215 ParamTy->isUndeducedType() || ArgTy->isUndeducedType()) 3216 return; 3217 3218 CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy); 3219 CharUnits ArgAlign = Context.getTypeAlignInChars(ArgTy); 3220 3221 // If the argument is less aligned than the parameter, there is a 3222 // potential alignment issue. 3223 if (ArgAlign < ParamAlign) 3224 Diag(Loc, diag::warn_param_mismatched_alignment) 3225 << (int)ArgAlign.getQuantity() << (int)ParamAlign.getQuantity() 3226 << ParamName << (FDecl != nullptr) << FDecl; 3227 } 3228 3229 void Sema::checkLifetimeCaptureBy(FunctionDecl *FD, bool IsMemberFunction, 3230 const Expr *ThisArg, 3231 ArrayRef<const Expr *> Args) { 3232 if (!FD || Args.empty()) 3233 return; 3234 auto GetArgAt = [&](int Idx) -> const Expr * { 3235 if (Idx == LifetimeCaptureByAttr::GLOBAL || 3236 Idx == LifetimeCaptureByAttr::UNKNOWN) 3237 return nullptr; 3238 if (IsMemberFunction && Idx == 0) 3239 return ThisArg; 3240 return Args[Idx - IsMemberFunction]; 3241 }; 3242 auto HandleCaptureByAttr = [&](const LifetimeCaptureByAttr *Attr, 3243 unsigned ArgIdx) { 3244 if (!Attr) 3245 return; 3246 3247 Expr *Captured = const_cast<Expr *>(GetArgAt(ArgIdx)); 3248 for (int CapturingParamIdx : Attr->params()) { 3249 // lifetime_capture_by(this) case is handled in the lifetimebound expr 3250 // initialization codepath. 3251 if (CapturingParamIdx == LifetimeCaptureByAttr::THIS && 3252 isa<CXXConstructorDecl>(FD)) 3253 continue; 3254 Expr *Capturing = const_cast<Expr *>(GetArgAt(CapturingParamIdx)); 3255 CapturingEntity CE{Capturing}; 3256 // Ensure that 'Captured' outlives the 'Capturing' entity. 3257 checkCaptureByLifetime(*this, CE, Captured); 3258 } 3259 }; 3260 for (unsigned I = 0; I < FD->getNumParams(); ++I) 3261 HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(), 3262 I + IsMemberFunction); 3263 // Check when the implicit object param is captured. 3264 if (IsMemberFunction) { 3265 TypeSourceInfo *TSI = FD->getTypeSourceInfo(); 3266 if (!TSI) 3267 return; 3268 AttributedTypeLoc ATL; 3269 for (TypeLoc TL = TSI->getTypeLoc(); 3270 (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); 3271 TL = ATL.getModifiedLoc()) 3272 HandleCaptureByAttr(ATL.getAttrAs<LifetimeCaptureByAttr>(), 0); 3273 } 3274 } 3275 3276 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, 3277 const Expr *ThisArg, ArrayRef<const Expr *> Args, 3278 bool IsMemberFunction, SourceLocation Loc, 3279 SourceRange Range, VariadicCallType CallType) { 3280 // FIXME: We should check as much as we can in the template definition. 3281 if (CurContext->isDependentContext()) 3282 return; 3283 3284 // Printf and scanf checking. 3285 llvm::SmallBitVector CheckedVarArgs; 3286 if (FDecl) { 3287 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 3288 // Only create vector if there are format attributes. 3289 CheckedVarArgs.resize(Args.size()); 3290 3291 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, 3292 CheckedVarArgs); 3293 } 3294 } 3295 3296 // Refuse POD arguments that weren't caught by the format string 3297 // checks above. 3298 auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl); 3299 if (CallType != VariadicDoesNotApply && 3300 (!FD || FD->getBuiltinID() != Builtin::BI__noop)) { 3301 unsigned NumParams = Proto ? Proto->getNumParams() 3302 : isa_and_nonnull<FunctionDecl>(FDecl) 3303 ? cast<FunctionDecl>(FDecl)->getNumParams() 3304 : isa_and_nonnull<ObjCMethodDecl>(FDecl) 3305 ? cast<ObjCMethodDecl>(FDecl)->param_size() 3306 : 0; 3307 3308 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { 3309 // Args[ArgIdx] can be null in malformed code. 3310 if (const Expr *Arg = Args[ArgIdx]) { 3311 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) 3312 checkVariadicArgument(Arg, CallType); 3313 } 3314 } 3315 } 3316 if (FD) 3317 checkLifetimeCaptureBy(FD, IsMemberFunction, ThisArg, Args); 3318 if (FDecl || Proto) { 3319 CheckNonNullArguments(*this, FDecl, Proto, Args, Loc); 3320 3321 // Type safety checking. 3322 if (FDecl) { 3323 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) 3324 CheckArgumentWithTypeTag(I, Args, Loc); 3325 } 3326 } 3327 3328 // Check that passed arguments match the alignment of original arguments. 3329 // Try to get the missing prototype from the declaration. 3330 if (!Proto && FDecl) { 3331 const auto *FT = FDecl->getFunctionType(); 3332 if (isa_and_nonnull<FunctionProtoType>(FT)) 3333 Proto = cast<FunctionProtoType>(FDecl->getFunctionType()); 3334 } 3335 if (Proto) { 3336 // For variadic functions, we may have more args than parameters. 3337 // For some K&R functions, we may have less args than parameters. 3338 const auto N = std::min<unsigned>(Proto->getNumParams(), Args.size()); 3339 bool IsScalableRet = Proto->getReturnType()->isSizelessVectorType(); 3340 bool IsScalableArg = false; 3341 for (unsigned ArgIdx = 0; ArgIdx < N; ++ArgIdx) { 3342 // Args[ArgIdx] can be null in malformed code. 3343 if (const Expr *Arg = Args[ArgIdx]) { 3344 if (Arg->containsErrors()) 3345 continue; 3346 3347 if (Context.getTargetInfo().getTriple().isOSAIX() && FDecl && Arg && 3348 FDecl->hasLinkage() && 3349 FDecl->getFormalLinkage() != Linkage::Internal && 3350 CallType == VariadicDoesNotApply) 3351 PPC().checkAIXMemberAlignment((Arg->getExprLoc()), Arg); 3352 3353 QualType ParamTy = Proto->getParamType(ArgIdx); 3354 if (ParamTy->isSizelessVectorType()) 3355 IsScalableArg = true; 3356 QualType ArgTy = Arg->getType(); 3357 CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1), 3358 ArgTy, ParamTy); 3359 } 3360 } 3361 3362 // If the callee has an AArch64 SME attribute to indicate that it is an 3363 // __arm_streaming function, then the caller requires SME to be available. 3364 FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); 3365 if (ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask) { 3366 if (auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) { 3367 llvm::StringMap<bool> CallerFeatureMap; 3368 Context.getFunctionFeatureMap(CallerFeatureMap, CallerFD); 3369 if (!CallerFeatureMap.contains("sme")) 3370 Diag(Loc, diag::err_sme_call_in_non_sme_target); 3371 } else if (!Context.getTargetInfo().hasFeature("sme")) { 3372 Diag(Loc, diag::err_sme_call_in_non_sme_target); 3373 } 3374 } 3375 3376 // If the call requires a streaming-mode change and has scalable vector 3377 // arguments or return values, then warn the user that the streaming and 3378 // non-streaming vector lengths may be different. 3379 const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext); 3380 if (CallerFD && (!FD || !FD->getBuiltinID()) && 3381 (IsScalableArg || IsScalableRet)) { 3382 bool IsCalleeStreaming = 3383 ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask; 3384 bool IsCalleeStreamingCompatible = 3385 ExtInfo.AArch64SMEAttributes & 3386 FunctionType::SME_PStateSMCompatibleMask; 3387 SemaARM::ArmStreamingType CallerFnType = getArmStreamingFnType(CallerFD); 3388 if (!IsCalleeStreamingCompatible && 3389 (CallerFnType == SemaARM::ArmStreamingCompatible || 3390 ((CallerFnType == SemaARM::ArmStreaming) ^ IsCalleeStreaming))) { 3391 if (IsScalableArg) 3392 Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming) 3393 << /*IsArg=*/true; 3394 if (IsScalableRet) 3395 Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming) 3396 << /*IsArg=*/false; 3397 } 3398 } 3399 3400 FunctionType::ArmStateValue CalleeArmZAState = 3401 FunctionType::getArmZAState(ExtInfo.AArch64SMEAttributes); 3402 FunctionType::ArmStateValue CalleeArmZT0State = 3403 FunctionType::getArmZT0State(ExtInfo.AArch64SMEAttributes); 3404 if (CalleeArmZAState != FunctionType::ARM_None || 3405 CalleeArmZT0State != FunctionType::ARM_None) { 3406 bool CallerHasZAState = false; 3407 bool CallerHasZT0State = false; 3408 if (CallerFD) { 3409 auto *Attr = CallerFD->getAttr<ArmNewAttr>(); 3410 if (Attr && Attr->isNewZA()) 3411 CallerHasZAState = true; 3412 if (Attr && Attr->isNewZT0()) 3413 CallerHasZT0State = true; 3414 if (const auto *FPT = CallerFD->getType()->getAs<FunctionProtoType>()) { 3415 CallerHasZAState |= 3416 FunctionType::getArmZAState( 3417 FPT->getExtProtoInfo().AArch64SMEAttributes) != 3418 FunctionType::ARM_None; 3419 CallerHasZT0State |= 3420 FunctionType::getArmZT0State( 3421 FPT->getExtProtoInfo().AArch64SMEAttributes) != 3422 FunctionType::ARM_None; 3423 } 3424 } 3425 3426 if (CalleeArmZAState != FunctionType::ARM_None && !CallerHasZAState) 3427 Diag(Loc, diag::err_sme_za_call_no_za_state); 3428 3429 if (CalleeArmZT0State != FunctionType::ARM_None && !CallerHasZT0State) 3430 Diag(Loc, diag::err_sme_zt0_call_no_zt0_state); 3431 3432 if (CallerHasZAState && CalleeArmZAState == FunctionType::ARM_None && 3433 CalleeArmZT0State != FunctionType::ARM_None) { 3434 Diag(Loc, diag::err_sme_unimplemented_za_save_restore); 3435 Diag(Loc, diag::note_sme_use_preserves_za); 3436 } 3437 } 3438 } 3439 3440 if (FDecl && FDecl->hasAttr<AllocAlignAttr>()) { 3441 auto *AA = FDecl->getAttr<AllocAlignAttr>(); 3442 const Expr *Arg = Args[AA->getParamIndex().getASTIndex()]; 3443 if (!Arg->isValueDependent()) { 3444 Expr::EvalResult Align; 3445 if (Arg->EvaluateAsInt(Align, Context)) { 3446 const llvm::APSInt &I = Align.Val.getInt(); 3447 if (!I.isPowerOf2()) 3448 Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two) 3449 << Arg->getSourceRange(); 3450 3451 if (I > Sema::MaximumAlignment) 3452 Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great) 3453 << Arg->getSourceRange() << Sema::MaximumAlignment; 3454 } 3455 } 3456 } 3457 3458 if (FD) 3459 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc); 3460 } 3461 3462 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) { 3463 if (ConceptDecl *Decl = AutoT->getTypeConstraintConcept()) { 3464 DiagnoseUseOfDecl(Decl, Loc); 3465 } 3466 } 3467 3468 void Sema::CheckConstructorCall(FunctionDecl *FDecl, QualType ThisType, 3469 ArrayRef<const Expr *> Args, 3470 const FunctionProtoType *Proto, 3471 SourceLocation Loc) { 3472 VariadicCallType CallType = 3473 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; 3474 3475 auto *Ctor = cast<CXXConstructorDecl>(FDecl); 3476 CheckArgAlignment( 3477 Loc, FDecl, "'this'", Context.getPointerType(ThisType), 3478 Context.getPointerType(Ctor->getFunctionObjectParameterType())); 3479 3480 checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true, 3481 Loc, SourceRange(), CallType); 3482 } 3483 3484 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, 3485 const FunctionProtoType *Proto) { 3486 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && 3487 isa<CXXMethodDecl>(FDecl); 3488 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || 3489 IsMemberOperatorCall; 3490 VariadicCallType CallType = getVariadicCallType(FDecl, Proto, 3491 TheCall->getCallee()); 3492 Expr** Args = TheCall->getArgs(); 3493 unsigned NumArgs = TheCall->getNumArgs(); 3494 3495 Expr *ImplicitThis = nullptr; 3496 if (IsMemberOperatorCall && !FDecl->hasCXXExplicitFunctionObjectParameter()) { 3497 // If this is a call to a member operator, hide the first 3498 // argument from checkCall. 3499 // FIXME: Our choice of AST representation here is less than ideal. 3500 ImplicitThis = Args[0]; 3501 ++Args; 3502 --NumArgs; 3503 } else if (IsMemberFunction && !FDecl->isStatic() && 3504 !FDecl->hasCXXExplicitFunctionObjectParameter()) 3505 ImplicitThis = 3506 cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument(); 3507 3508 if (ImplicitThis) { 3509 // ImplicitThis may or may not be a pointer, depending on whether . or -> is 3510 // used. 3511 QualType ThisType = ImplicitThis->getType(); 3512 if (!ThisType->isPointerType()) { 3513 assert(!ThisType->isReferenceType()); 3514 ThisType = Context.getPointerType(ThisType); 3515 } 3516 3517 QualType ThisTypeFromDecl = Context.getPointerType( 3518 cast<CXXMethodDecl>(FDecl)->getFunctionObjectParameterType()); 3519 3520 CheckArgAlignment(TheCall->getRParenLoc(), FDecl, "'this'", ThisType, 3521 ThisTypeFromDecl); 3522 } 3523 3524 checkCall(FDecl, Proto, ImplicitThis, llvm::ArrayRef(Args, NumArgs), 3525 IsMemberFunction, TheCall->getRParenLoc(), 3526 TheCall->getCallee()->getSourceRange(), CallType); 3527 3528 IdentifierInfo *FnInfo = FDecl->getIdentifier(); 3529 // None of the checks below are needed for functions that don't have 3530 // simple names (e.g., C++ conversion functions). 3531 if (!FnInfo) 3532 return false; 3533 3534 // Enforce TCB except for builtin calls, which are always allowed. 3535 if (FDecl->getBuiltinID() == 0) 3536 CheckTCBEnforcement(TheCall->getExprLoc(), FDecl); 3537 3538 CheckAbsoluteValueFunction(TheCall, FDecl); 3539 CheckMaxUnsignedZero(TheCall, FDecl); 3540 CheckInfNaNFunction(TheCall, FDecl); 3541 3542 if (getLangOpts().ObjC) 3543 ObjC().DiagnoseCStringFormatDirectiveInCFAPI(FDecl, Args, NumArgs); 3544 3545 unsigned CMId = FDecl->getMemoryFunctionKind(); 3546 3547 // Handle memory setting and copying functions. 3548 switch (CMId) { 3549 case 0: 3550 return false; 3551 case Builtin::BIstrlcpy: // fallthrough 3552 case Builtin::BIstrlcat: 3553 CheckStrlcpycatArguments(TheCall, FnInfo); 3554 break; 3555 case Builtin::BIstrncat: 3556 CheckStrncatArguments(TheCall, FnInfo); 3557 break; 3558 case Builtin::BIfree: 3559 CheckFreeArguments(TheCall); 3560 break; 3561 default: 3562 CheckMemaccessArguments(TheCall, CMId, FnInfo); 3563 } 3564 3565 return false; 3566 } 3567 3568 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, 3569 const FunctionProtoType *Proto) { 3570 QualType Ty; 3571 if (const auto *V = dyn_cast<VarDecl>(NDecl)) 3572 Ty = V->getType().getNonReferenceType(); 3573 else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) 3574 Ty = F->getType().getNonReferenceType(); 3575 else 3576 return false; 3577 3578 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() && 3579 !Ty->isFunctionProtoType()) 3580 return false; 3581 3582 VariadicCallType CallType; 3583 if (!Proto || !Proto->isVariadic()) { 3584 CallType = VariadicDoesNotApply; 3585 } else if (Ty->isBlockPointerType()) { 3586 CallType = VariadicBlock; 3587 } else { // Ty->isFunctionPointerType() 3588 CallType = VariadicFunction; 3589 } 3590 3591 checkCall(NDecl, Proto, /*ThisArg=*/nullptr, 3592 llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 3593 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 3594 TheCall->getCallee()->getSourceRange(), CallType); 3595 3596 return false; 3597 } 3598 3599 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { 3600 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, 3601 TheCall->getCallee()); 3602 checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr, 3603 llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 3604 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 3605 TheCall->getCallee()->getSourceRange(), CallType); 3606 3607 return false; 3608 } 3609 3610 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { 3611 if (!llvm::isValidAtomicOrderingCABI(Ordering)) 3612 return false; 3613 3614 auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering; 3615 switch (Op) { 3616 case AtomicExpr::AO__c11_atomic_init: 3617 case AtomicExpr::AO__opencl_atomic_init: 3618 llvm_unreachable("There is no ordering argument for an init"); 3619 3620 case AtomicExpr::AO__c11_atomic_load: 3621 case AtomicExpr::AO__opencl_atomic_load: 3622 case AtomicExpr::AO__hip_atomic_load: 3623 case AtomicExpr::AO__atomic_load_n: 3624 case AtomicExpr::AO__atomic_load: 3625 case AtomicExpr::AO__scoped_atomic_load_n: 3626 case AtomicExpr::AO__scoped_atomic_load: 3627 return OrderingCABI != llvm::AtomicOrderingCABI::release && 3628 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 3629 3630 case AtomicExpr::AO__c11_atomic_store: 3631 case AtomicExpr::AO__opencl_atomic_store: 3632 case AtomicExpr::AO__hip_atomic_store: 3633 case AtomicExpr::AO__atomic_store: 3634 case AtomicExpr::AO__atomic_store_n: 3635 case AtomicExpr::AO__scoped_atomic_store: 3636 case AtomicExpr::AO__scoped_atomic_store_n: 3637 return OrderingCABI != llvm::AtomicOrderingCABI::consume && 3638 OrderingCABI != llvm::AtomicOrderingCABI::acquire && 3639 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 3640 3641 default: 3642 return true; 3643 } 3644 } 3645 3646 ExprResult Sema::AtomicOpsOverloaded(ExprResult TheCallResult, 3647 AtomicExpr::AtomicOp Op) { 3648 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); 3649 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 3650 MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()}; 3651 return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()}, 3652 DRE->getSourceRange(), TheCall->getRParenLoc(), Args, 3653 Op); 3654 } 3655 3656 ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, 3657 SourceLocation RParenLoc, MultiExprArg Args, 3658 AtomicExpr::AtomicOp Op, 3659 AtomicArgumentOrder ArgOrder) { 3660 // All the non-OpenCL operations take one of the following forms. 3661 // The OpenCL operations take the __c11 forms with one extra argument for 3662 // synchronization scope. 3663 enum { 3664 // C __c11_atomic_init(A *, C) 3665 Init, 3666 3667 // C __c11_atomic_load(A *, int) 3668 Load, 3669 3670 // void __atomic_load(A *, CP, int) 3671 LoadCopy, 3672 3673 // void __atomic_store(A *, CP, int) 3674 Copy, 3675 3676 // C __c11_atomic_add(A *, M, int) 3677 Arithmetic, 3678 3679 // C __atomic_exchange_n(A *, CP, int) 3680 Xchg, 3681 3682 // void __atomic_exchange(A *, C *, CP, int) 3683 GNUXchg, 3684 3685 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) 3686 C11CmpXchg, 3687 3688 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) 3689 GNUCmpXchg 3690 } Form = Init; 3691 3692 const unsigned NumForm = GNUCmpXchg + 1; 3693 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 }; 3694 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 }; 3695 // where: 3696 // C is an appropriate type, 3697 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, 3698 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, 3699 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and 3700 // the int parameters are for orderings. 3701 3702 static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm 3703 && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm, 3704 "need to update code for modified forms"); 3705 static_assert(AtomicExpr::AO__atomic_add_fetch == 0 && 3706 AtomicExpr::AO__atomic_xor_fetch + 1 == 3707 AtomicExpr::AO__c11_atomic_compare_exchange_strong, 3708 "need to update code for modified C11 atomics"); 3709 bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_compare_exchange_strong && 3710 Op <= AtomicExpr::AO__opencl_atomic_store; 3711 bool IsHIP = Op >= AtomicExpr::AO__hip_atomic_compare_exchange_strong && 3712 Op <= AtomicExpr::AO__hip_atomic_store; 3713 bool IsScoped = Op >= AtomicExpr::AO__scoped_atomic_add_fetch && 3714 Op <= AtomicExpr::AO__scoped_atomic_xor_fetch; 3715 bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_compare_exchange_strong && 3716 Op <= AtomicExpr::AO__c11_atomic_store) || 3717 IsOpenCL; 3718 bool IsN = Op == AtomicExpr::AO__atomic_load_n || 3719 Op == AtomicExpr::AO__atomic_store_n || 3720 Op == AtomicExpr::AO__atomic_exchange_n || 3721 Op == AtomicExpr::AO__atomic_compare_exchange_n || 3722 Op == AtomicExpr::AO__scoped_atomic_load_n || 3723 Op == AtomicExpr::AO__scoped_atomic_store_n || 3724 Op == AtomicExpr::AO__scoped_atomic_exchange_n || 3725 Op == AtomicExpr::AO__scoped_atomic_compare_exchange_n; 3726 // Bit mask for extra allowed value types other than integers for atomic 3727 // arithmetic operations. Add/sub allow pointer and floating point. Min/max 3728 // allow floating point. 3729 enum ArithOpExtraValueType { 3730 AOEVT_None = 0, 3731 AOEVT_Pointer = 1, 3732 AOEVT_FP = 2, 3733 }; 3734 unsigned ArithAllows = AOEVT_None; 3735 3736 switch (Op) { 3737 case AtomicExpr::AO__c11_atomic_init: 3738 case AtomicExpr::AO__opencl_atomic_init: 3739 Form = Init; 3740 break; 3741 3742 case AtomicExpr::AO__c11_atomic_load: 3743 case AtomicExpr::AO__opencl_atomic_load: 3744 case AtomicExpr::AO__hip_atomic_load: 3745 case AtomicExpr::AO__atomic_load_n: 3746 case AtomicExpr::AO__scoped_atomic_load_n: 3747 Form = Load; 3748 break; 3749 3750 case AtomicExpr::AO__atomic_load: 3751 case AtomicExpr::AO__scoped_atomic_load: 3752 Form = LoadCopy; 3753 break; 3754 3755 case AtomicExpr::AO__c11_atomic_store: 3756 case AtomicExpr::AO__opencl_atomic_store: 3757 case AtomicExpr::AO__hip_atomic_store: 3758 case AtomicExpr::AO__atomic_store: 3759 case AtomicExpr::AO__atomic_store_n: 3760 case AtomicExpr::AO__scoped_atomic_store: 3761 case AtomicExpr::AO__scoped_atomic_store_n: 3762 Form = Copy; 3763 break; 3764 case AtomicExpr::AO__atomic_fetch_add: 3765 case AtomicExpr::AO__atomic_fetch_sub: 3766 case AtomicExpr::AO__atomic_add_fetch: 3767 case AtomicExpr::AO__atomic_sub_fetch: 3768 case AtomicExpr::AO__scoped_atomic_fetch_add: 3769 case AtomicExpr::AO__scoped_atomic_fetch_sub: 3770 case AtomicExpr::AO__scoped_atomic_add_fetch: 3771 case AtomicExpr::AO__scoped_atomic_sub_fetch: 3772 case AtomicExpr::AO__c11_atomic_fetch_add: 3773 case AtomicExpr::AO__c11_atomic_fetch_sub: 3774 case AtomicExpr::AO__opencl_atomic_fetch_add: 3775 case AtomicExpr::AO__opencl_atomic_fetch_sub: 3776 case AtomicExpr::AO__hip_atomic_fetch_add: 3777 case AtomicExpr::AO__hip_atomic_fetch_sub: 3778 ArithAllows = AOEVT_Pointer | AOEVT_FP; 3779 Form = Arithmetic; 3780 break; 3781 case AtomicExpr::AO__atomic_fetch_max: 3782 case AtomicExpr::AO__atomic_fetch_min: 3783 case AtomicExpr::AO__atomic_max_fetch: 3784 case AtomicExpr::AO__atomic_min_fetch: 3785 case AtomicExpr::AO__scoped_atomic_fetch_max: 3786 case AtomicExpr::AO__scoped_atomic_fetch_min: 3787 case AtomicExpr::AO__scoped_atomic_max_fetch: 3788 case AtomicExpr::AO__scoped_atomic_min_fetch: 3789 case AtomicExpr::AO__c11_atomic_fetch_max: 3790 case AtomicExpr::AO__c11_atomic_fetch_min: 3791 case AtomicExpr::AO__opencl_atomic_fetch_max: 3792 case AtomicExpr::AO__opencl_atomic_fetch_min: 3793 case AtomicExpr::AO__hip_atomic_fetch_max: 3794 case AtomicExpr::AO__hip_atomic_fetch_min: 3795 ArithAllows = AOEVT_FP; 3796 Form = Arithmetic; 3797 break; 3798 case AtomicExpr::AO__c11_atomic_fetch_and: 3799 case AtomicExpr::AO__c11_atomic_fetch_or: 3800 case AtomicExpr::AO__c11_atomic_fetch_xor: 3801 case AtomicExpr::AO__hip_atomic_fetch_and: 3802 case AtomicExpr::AO__hip_atomic_fetch_or: 3803 case AtomicExpr::AO__hip_atomic_fetch_xor: 3804 case AtomicExpr::AO__c11_atomic_fetch_nand: 3805 case AtomicExpr::AO__opencl_atomic_fetch_and: 3806 case AtomicExpr::AO__opencl_atomic_fetch_or: 3807 case AtomicExpr::AO__opencl_atomic_fetch_xor: 3808 case AtomicExpr::AO__atomic_fetch_and: 3809 case AtomicExpr::AO__atomic_fetch_or: 3810 case AtomicExpr::AO__atomic_fetch_xor: 3811 case AtomicExpr::AO__atomic_fetch_nand: 3812 case AtomicExpr::AO__atomic_and_fetch: 3813 case AtomicExpr::AO__atomic_or_fetch: 3814 case AtomicExpr::AO__atomic_xor_fetch: 3815 case AtomicExpr::AO__atomic_nand_fetch: 3816 case AtomicExpr::AO__scoped_atomic_fetch_and: 3817 case AtomicExpr::AO__scoped_atomic_fetch_or: 3818 case AtomicExpr::AO__scoped_atomic_fetch_xor: 3819 case AtomicExpr::AO__scoped_atomic_fetch_nand: 3820 case AtomicExpr::AO__scoped_atomic_and_fetch: 3821 case AtomicExpr::AO__scoped_atomic_or_fetch: 3822 case AtomicExpr::AO__scoped_atomic_xor_fetch: 3823 case AtomicExpr::AO__scoped_atomic_nand_fetch: 3824 Form = Arithmetic; 3825 break; 3826 3827 case AtomicExpr::AO__c11_atomic_exchange: 3828 case AtomicExpr::AO__hip_atomic_exchange: 3829 case AtomicExpr::AO__opencl_atomic_exchange: 3830 case AtomicExpr::AO__atomic_exchange_n: 3831 case AtomicExpr::AO__scoped_atomic_exchange_n: 3832 Form = Xchg; 3833 break; 3834 3835 case AtomicExpr::AO__atomic_exchange: 3836 case AtomicExpr::AO__scoped_atomic_exchange: 3837 Form = GNUXchg; 3838 break; 3839 3840 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 3841 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 3842 case AtomicExpr::AO__hip_atomic_compare_exchange_strong: 3843 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong: 3844 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak: 3845 case AtomicExpr::AO__hip_atomic_compare_exchange_weak: 3846 Form = C11CmpXchg; 3847 break; 3848 3849 case AtomicExpr::AO__atomic_compare_exchange: 3850 case AtomicExpr::AO__atomic_compare_exchange_n: 3851 case AtomicExpr::AO__scoped_atomic_compare_exchange: 3852 case AtomicExpr::AO__scoped_atomic_compare_exchange_n: 3853 Form = GNUCmpXchg; 3854 break; 3855 } 3856 3857 unsigned AdjustedNumArgs = NumArgs[Form]; 3858 if ((IsOpenCL || IsHIP || IsScoped) && 3859 Op != AtomicExpr::AO__opencl_atomic_init) 3860 ++AdjustedNumArgs; 3861 // Check we have the right number of arguments. 3862 if (Args.size() < AdjustedNumArgs) { 3863 Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args) 3864 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) 3865 << /*is non object*/ 0 << ExprRange; 3866 return ExprError(); 3867 } else if (Args.size() > AdjustedNumArgs) { 3868 Diag(Args[AdjustedNumArgs]->getBeginLoc(), 3869 diag::err_typecheck_call_too_many_args) 3870 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) 3871 << /*is non object*/ 0 << ExprRange; 3872 return ExprError(); 3873 } 3874 3875 // Inspect the first argument of the atomic operation. 3876 Expr *Ptr = Args[0]; 3877 ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr); 3878 if (ConvertedPtr.isInvalid()) 3879 return ExprError(); 3880 3881 Ptr = ConvertedPtr.get(); 3882 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); 3883 if (!pointerType) { 3884 Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) 3885 << Ptr->getType() << 0 << Ptr->getSourceRange(); 3886 return ExprError(); 3887 } 3888 3889 // For a __c11 builtin, this should be a pointer to an _Atomic type. 3890 QualType AtomTy = pointerType->getPointeeType(); // 'A' 3891 QualType ValType = AtomTy; // 'C' 3892 if (IsC11) { 3893 if (!AtomTy->isAtomicType()) { 3894 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic) 3895 << Ptr->getType() << Ptr->getSourceRange(); 3896 return ExprError(); 3897 } 3898 if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) || 3899 AtomTy.getAddressSpace() == LangAS::opencl_constant) { 3900 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic) 3901 << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType() 3902 << Ptr->getSourceRange(); 3903 return ExprError(); 3904 } 3905 ValType = AtomTy->castAs<AtomicType>()->getValueType(); 3906 } else if (Form != Load && Form != LoadCopy) { 3907 if (ValType.isConstQualified()) { 3908 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer) 3909 << Ptr->getType() << Ptr->getSourceRange(); 3910 return ExprError(); 3911 } 3912 } 3913 3914 // Pointer to object of size zero is not allowed. 3915 if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy, 3916 diag::err_incomplete_type)) 3917 return ExprError(); 3918 if (Context.getTypeInfoInChars(AtomTy).Width.isZero()) { 3919 Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) 3920 << Ptr->getType() << 1 << Ptr->getSourceRange(); 3921 return ExprError(); 3922 } 3923 3924 // For an arithmetic operation, the implied arithmetic must be well-formed. 3925 if (Form == Arithmetic) { 3926 // GCC does not enforce these rules for GNU atomics, but we do to help catch 3927 // trivial type errors. 3928 auto IsAllowedValueType = [&](QualType ValType, 3929 unsigned AllowedType) -> bool { 3930 if (ValType->isIntegerType()) 3931 return true; 3932 if (ValType->isPointerType()) 3933 return AllowedType & AOEVT_Pointer; 3934 if (!(ValType->isFloatingType() && (AllowedType & AOEVT_FP))) 3935 return false; 3936 // LLVM Parser does not allow atomicrmw with x86_fp80 type. 3937 if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) && 3938 &Context.getTargetInfo().getLongDoubleFormat() == 3939 &llvm::APFloat::x87DoubleExtended()) 3940 return false; 3941 return true; 3942 }; 3943 if (!IsAllowedValueType(ValType, ArithAllows)) { 3944 auto DID = ArithAllows & AOEVT_FP 3945 ? (ArithAllows & AOEVT_Pointer 3946 ? diag::err_atomic_op_needs_atomic_int_ptr_or_fp 3947 : diag::err_atomic_op_needs_atomic_int_or_fp) 3948 : diag::err_atomic_op_needs_atomic_int; 3949 Diag(ExprRange.getBegin(), DID) 3950 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 3951 return ExprError(); 3952 } 3953 if (IsC11 && ValType->isPointerType() && 3954 RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(), 3955 diag::err_incomplete_type)) { 3956 return ExprError(); 3957 } 3958 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { 3959 // For __atomic_*_n operations, the value type must be a scalar integral or 3960 // pointer type which is 1, 2, 4, 8 or 16 bytes in length. 3961 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr) 3962 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 3963 return ExprError(); 3964 } 3965 3966 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && 3967 !AtomTy->isScalarType()) { 3968 // For GNU atomics, require a trivially-copyable type. This is not part of 3969 // the GNU atomics specification but we enforce it for consistency with 3970 // other atomics which generally all require a trivially-copyable type. This 3971 // is because atomics just copy bits. 3972 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy) 3973 << Ptr->getType() << Ptr->getSourceRange(); 3974 return ExprError(); 3975 } 3976 3977 switch (ValType.getObjCLifetime()) { 3978 case Qualifiers::OCL_None: 3979 case Qualifiers::OCL_ExplicitNone: 3980 // okay 3981 break; 3982 3983 case Qualifiers::OCL_Weak: 3984 case Qualifiers::OCL_Strong: 3985 case Qualifiers::OCL_Autoreleasing: 3986 // FIXME: Can this happen? By this point, ValType should be known 3987 // to be trivially copyable. 3988 Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership) 3989 << ValType << Ptr->getSourceRange(); 3990 return ExprError(); 3991 } 3992 3993 // All atomic operations have an overload which takes a pointer to a volatile 3994 // 'A'. We shouldn't let the volatile-ness of the pointee-type inject itself 3995 // into the result or the other operands. Similarly atomic_load takes a 3996 // pointer to a const 'A'. 3997 ValType.removeLocalVolatile(); 3998 ValType.removeLocalConst(); 3999 QualType ResultType = ValType; 4000 if (Form == Copy || Form == LoadCopy || Form == GNUXchg || 4001 Form == Init) 4002 ResultType = Context.VoidTy; 4003 else if (Form == C11CmpXchg || Form == GNUCmpXchg) 4004 ResultType = Context.BoolTy; 4005 4006 // The type of a parameter passed 'by value'. In the GNU atomics, such 4007 // arguments are actually passed as pointers. 4008 QualType ByValType = ValType; // 'CP' 4009 bool IsPassedByAddress = false; 4010 if (!IsC11 && !IsHIP && !IsN) { 4011 ByValType = Ptr->getType(); 4012 IsPassedByAddress = true; 4013 } 4014 4015 SmallVector<Expr *, 5> APIOrderedArgs; 4016 if (ArgOrder == Sema::AtomicArgumentOrder::AST) { 4017 APIOrderedArgs.push_back(Args[0]); 4018 switch (Form) { 4019 case Init: 4020 case Load: 4021 APIOrderedArgs.push_back(Args[1]); // Val1/Order 4022 break; 4023 case LoadCopy: 4024 case Copy: 4025 case Arithmetic: 4026 case Xchg: 4027 APIOrderedArgs.push_back(Args[2]); // Val1 4028 APIOrderedArgs.push_back(Args[1]); // Order 4029 break; 4030 case GNUXchg: 4031 APIOrderedArgs.push_back(Args[2]); // Val1 4032 APIOrderedArgs.push_back(Args[3]); // Val2 4033 APIOrderedArgs.push_back(Args[1]); // Order 4034 break; 4035 case C11CmpXchg: 4036 APIOrderedArgs.push_back(Args[2]); // Val1 4037 APIOrderedArgs.push_back(Args[4]); // Val2 4038 APIOrderedArgs.push_back(Args[1]); // Order 4039 APIOrderedArgs.push_back(Args[3]); // OrderFail 4040 break; 4041 case GNUCmpXchg: 4042 APIOrderedArgs.push_back(Args[2]); // Val1 4043 APIOrderedArgs.push_back(Args[4]); // Val2 4044 APIOrderedArgs.push_back(Args[5]); // Weak 4045 APIOrderedArgs.push_back(Args[1]); // Order 4046 APIOrderedArgs.push_back(Args[3]); // OrderFail 4047 break; 4048 } 4049 } else 4050 APIOrderedArgs.append(Args.begin(), Args.end()); 4051 4052 // The first argument's non-CV pointer type is used to deduce the type of 4053 // subsequent arguments, except for: 4054 // - weak flag (always converted to bool) 4055 // - memory order (always converted to int) 4056 // - scope (always converted to int) 4057 for (unsigned i = 0; i != APIOrderedArgs.size(); ++i) { 4058 QualType Ty; 4059 if (i < NumVals[Form] + 1) { 4060 switch (i) { 4061 case 0: 4062 // The first argument is always a pointer. It has a fixed type. 4063 // It is always dereferenced, a nullptr is undefined. 4064 CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); 4065 // Nothing else to do: we already know all we want about this pointer. 4066 continue; 4067 case 1: 4068 // The second argument is the non-atomic operand. For arithmetic, this 4069 // is always passed by value, and for a compare_exchange it is always 4070 // passed by address. For the rest, GNU uses by-address and C11 uses 4071 // by-value. 4072 assert(Form != Load); 4073 if (Form == Arithmetic && ValType->isPointerType()) 4074 Ty = Context.getPointerDiffType(); 4075 else if (Form == Init || Form == Arithmetic) 4076 Ty = ValType; 4077 else if (Form == Copy || Form == Xchg) { 4078 if (IsPassedByAddress) { 4079 // The value pointer is always dereferenced, a nullptr is undefined. 4080 CheckNonNullArgument(*this, APIOrderedArgs[i], 4081 ExprRange.getBegin()); 4082 } 4083 Ty = ByValType; 4084 } else { 4085 Expr *ValArg = APIOrderedArgs[i]; 4086 // The value pointer is always dereferenced, a nullptr is undefined. 4087 CheckNonNullArgument(*this, ValArg, ExprRange.getBegin()); 4088 LangAS AS = LangAS::Default; 4089 // Keep address space of non-atomic pointer type. 4090 if (const PointerType *PtrTy = 4091 ValArg->getType()->getAs<PointerType>()) { 4092 AS = PtrTy->getPointeeType().getAddressSpace(); 4093 } 4094 Ty = Context.getPointerType( 4095 Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); 4096 } 4097 break; 4098 case 2: 4099 // The third argument to compare_exchange / GNU exchange is the desired 4100 // value, either by-value (for the C11 and *_n variant) or as a pointer. 4101 if (IsPassedByAddress) 4102 CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); 4103 Ty = ByValType; 4104 break; 4105 case 3: 4106 // The fourth argument to GNU compare_exchange is a 'weak' flag. 4107 Ty = Context.BoolTy; 4108 break; 4109 } 4110 } else { 4111 // The order(s) and scope are always converted to int. 4112 Ty = Context.IntTy; 4113 } 4114 4115 InitializedEntity Entity = 4116 InitializedEntity::InitializeParameter(Context, Ty, false); 4117 ExprResult Arg = APIOrderedArgs[i]; 4118 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4119 if (Arg.isInvalid()) 4120 return true; 4121 APIOrderedArgs[i] = Arg.get(); 4122 } 4123 4124 // Permute the arguments into a 'consistent' order. 4125 SmallVector<Expr*, 5> SubExprs; 4126 SubExprs.push_back(Ptr); 4127 switch (Form) { 4128 case Init: 4129 // Note, AtomicExpr::getVal1() has a special case for this atomic. 4130 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4131 break; 4132 case Load: 4133 SubExprs.push_back(APIOrderedArgs[1]); // Order 4134 break; 4135 case LoadCopy: 4136 case Copy: 4137 case Arithmetic: 4138 case Xchg: 4139 SubExprs.push_back(APIOrderedArgs[2]); // Order 4140 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4141 break; 4142 case GNUXchg: 4143 // Note, AtomicExpr::getVal2() has a special case for this atomic. 4144 SubExprs.push_back(APIOrderedArgs[3]); // Order 4145 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4146 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4147 break; 4148 case C11CmpXchg: 4149 SubExprs.push_back(APIOrderedArgs[3]); // Order 4150 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4151 SubExprs.push_back(APIOrderedArgs[4]); // OrderFail 4152 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4153 break; 4154 case GNUCmpXchg: 4155 SubExprs.push_back(APIOrderedArgs[4]); // Order 4156 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4157 SubExprs.push_back(APIOrderedArgs[5]); // OrderFail 4158 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4159 SubExprs.push_back(APIOrderedArgs[3]); // Weak 4160 break; 4161 } 4162 4163 // If the memory orders are constants, check they are valid. 4164 if (SubExprs.size() >= 2 && Form != Init) { 4165 std::optional<llvm::APSInt> Success = 4166 SubExprs[1]->getIntegerConstantExpr(Context); 4167 if (Success && !isValidOrderingForOp(Success->getSExtValue(), Op)) { 4168 Diag(SubExprs[1]->getBeginLoc(), 4169 diag::warn_atomic_op_has_invalid_memory_order) 4170 << /*success=*/(Form == C11CmpXchg || Form == GNUCmpXchg) 4171 << SubExprs[1]->getSourceRange(); 4172 } 4173 if (SubExprs.size() >= 5) { 4174 if (std::optional<llvm::APSInt> Failure = 4175 SubExprs[3]->getIntegerConstantExpr(Context)) { 4176 if (!llvm::is_contained( 4177 {llvm::AtomicOrderingCABI::relaxed, 4178 llvm::AtomicOrderingCABI::consume, 4179 llvm::AtomicOrderingCABI::acquire, 4180 llvm::AtomicOrderingCABI::seq_cst}, 4181 (llvm::AtomicOrderingCABI)Failure->getSExtValue())) { 4182 Diag(SubExprs[3]->getBeginLoc(), 4183 diag::warn_atomic_op_has_invalid_memory_order) 4184 << /*failure=*/2 << SubExprs[3]->getSourceRange(); 4185 } 4186 } 4187 } 4188 } 4189 4190 if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { 4191 auto *Scope = Args[Args.size() - 1]; 4192 if (std::optional<llvm::APSInt> Result = 4193 Scope->getIntegerConstantExpr(Context)) { 4194 if (!ScopeModel->isValid(Result->getZExtValue())) 4195 Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) 4196 << Scope->getSourceRange(); 4197 } 4198 SubExprs.push_back(Scope); 4199 } 4200 4201 AtomicExpr *AE = new (Context) 4202 AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc); 4203 4204 if ((Op == AtomicExpr::AO__c11_atomic_load || 4205 Op == AtomicExpr::AO__c11_atomic_store || 4206 Op == AtomicExpr::AO__opencl_atomic_load || 4207 Op == AtomicExpr::AO__hip_atomic_load || 4208 Op == AtomicExpr::AO__opencl_atomic_store || 4209 Op == AtomicExpr::AO__hip_atomic_store) && 4210 Context.AtomicUsesUnsupportedLibcall(AE)) 4211 Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib) 4212 << ((Op == AtomicExpr::AO__c11_atomic_load || 4213 Op == AtomicExpr::AO__opencl_atomic_load || 4214 Op == AtomicExpr::AO__hip_atomic_load) 4215 ? 0 4216 : 1); 4217 4218 if (ValType->isBitIntType()) { 4219 Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_bit_int_prohibit); 4220 return ExprError(); 4221 } 4222 4223 return AE; 4224 } 4225 4226 /// checkBuiltinArgument - Given a call to a builtin function, perform 4227 /// normal type-checking on the given argument, updating the call in 4228 /// place. This is useful when a builtin function requires custom 4229 /// type-checking for some of its arguments but not necessarily all of 4230 /// them. 4231 /// 4232 /// Returns true on error. 4233 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { 4234 FunctionDecl *Fn = E->getDirectCallee(); 4235 assert(Fn && "builtin call without direct callee!"); 4236 4237 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); 4238 InitializedEntity Entity = 4239 InitializedEntity::InitializeParameter(S.Context, Param); 4240 4241 ExprResult Arg = E->getArg(ArgIndex); 4242 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 4243 if (Arg.isInvalid()) 4244 return true; 4245 4246 E->setArg(ArgIndex, Arg.get()); 4247 return false; 4248 } 4249 4250 ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) { 4251 CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get()); 4252 Expr *Callee = TheCall->getCallee(); 4253 DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts()); 4254 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4255 4256 // Ensure that we have at least one argument to do type inference from. 4257 if (TheCall->getNumArgs() < 1) { 4258 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4259 << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 4260 << Callee->getSourceRange(); 4261 return ExprError(); 4262 } 4263 4264 // Inspect the first argument of the atomic builtin. This should always be 4265 // a pointer type, whose element is an integral scalar or pointer type. 4266 // Because it is a pointer type, we don't have to worry about any implicit 4267 // casts here. 4268 // FIXME: We don't allow floating point scalars as input. 4269 Expr *FirstArg = TheCall->getArg(0); 4270 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); 4271 if (FirstArgResult.isInvalid()) 4272 return ExprError(); 4273 FirstArg = FirstArgResult.get(); 4274 TheCall->setArg(0, FirstArg); 4275 4276 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); 4277 if (!pointerType) { 4278 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) 4279 << FirstArg->getType() << 0 << FirstArg->getSourceRange(); 4280 return ExprError(); 4281 } 4282 4283 QualType ValType = pointerType->getPointeeType(); 4284 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4285 !ValType->isBlockPointerType()) { 4286 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr) 4287 << FirstArg->getType() << 0 << FirstArg->getSourceRange(); 4288 return ExprError(); 4289 } 4290 4291 if (ValType.isConstQualified()) { 4292 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const) 4293 << FirstArg->getType() << FirstArg->getSourceRange(); 4294 return ExprError(); 4295 } 4296 4297 switch (ValType.getObjCLifetime()) { 4298 case Qualifiers::OCL_None: 4299 case Qualifiers::OCL_ExplicitNone: 4300 // okay 4301 break; 4302 4303 case Qualifiers::OCL_Weak: 4304 case Qualifiers::OCL_Strong: 4305 case Qualifiers::OCL_Autoreleasing: 4306 Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) 4307 << ValType << FirstArg->getSourceRange(); 4308 return ExprError(); 4309 } 4310 4311 // Strip any qualifiers off ValType. 4312 ValType = ValType.getUnqualifiedType(); 4313 4314 // The majority of builtins return a value, but a few have special return 4315 // types, so allow them to override appropriately below. 4316 QualType ResultType = ValType; 4317 4318 // We need to figure out which concrete builtin this maps onto. For example, 4319 // __sync_fetch_and_add with a 2 byte object turns into 4320 // __sync_fetch_and_add_2. 4321 #define BUILTIN_ROW(x) \ 4322 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ 4323 Builtin::BI##x##_8, Builtin::BI##x##_16 } 4324 4325 static const unsigned BuiltinIndices[][5] = { 4326 BUILTIN_ROW(__sync_fetch_and_add), 4327 BUILTIN_ROW(__sync_fetch_and_sub), 4328 BUILTIN_ROW(__sync_fetch_and_or), 4329 BUILTIN_ROW(__sync_fetch_and_and), 4330 BUILTIN_ROW(__sync_fetch_and_xor), 4331 BUILTIN_ROW(__sync_fetch_and_nand), 4332 4333 BUILTIN_ROW(__sync_add_and_fetch), 4334 BUILTIN_ROW(__sync_sub_and_fetch), 4335 BUILTIN_ROW(__sync_and_and_fetch), 4336 BUILTIN_ROW(__sync_or_and_fetch), 4337 BUILTIN_ROW(__sync_xor_and_fetch), 4338 BUILTIN_ROW(__sync_nand_and_fetch), 4339 4340 BUILTIN_ROW(__sync_val_compare_and_swap), 4341 BUILTIN_ROW(__sync_bool_compare_and_swap), 4342 BUILTIN_ROW(__sync_lock_test_and_set), 4343 BUILTIN_ROW(__sync_lock_release), 4344 BUILTIN_ROW(__sync_swap) 4345 }; 4346 #undef BUILTIN_ROW 4347 4348 // Determine the index of the size. 4349 unsigned SizeIndex; 4350 switch (Context.getTypeSizeInChars(ValType).getQuantity()) { 4351 case 1: SizeIndex = 0; break; 4352 case 2: SizeIndex = 1; break; 4353 case 4: SizeIndex = 2; break; 4354 case 8: SizeIndex = 3; break; 4355 case 16: SizeIndex = 4; break; 4356 default: 4357 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size) 4358 << FirstArg->getType() << FirstArg->getSourceRange(); 4359 return ExprError(); 4360 } 4361 4362 // Each of these builtins has one pointer argument, followed by some number of 4363 // values (0, 1 or 2) followed by a potentially empty varags list of stuff 4364 // that we ignore. Find out which row of BuiltinIndices to read from as well 4365 // as the number of fixed args. 4366 unsigned BuiltinID = FDecl->getBuiltinID(); 4367 unsigned BuiltinIndex, NumFixed = 1; 4368 bool WarnAboutSemanticsChange = false; 4369 switch (BuiltinID) { 4370 default: llvm_unreachable("Unknown overloaded atomic builtin!"); 4371 case Builtin::BI__sync_fetch_and_add: 4372 case Builtin::BI__sync_fetch_and_add_1: 4373 case Builtin::BI__sync_fetch_and_add_2: 4374 case Builtin::BI__sync_fetch_and_add_4: 4375 case Builtin::BI__sync_fetch_and_add_8: 4376 case Builtin::BI__sync_fetch_and_add_16: 4377 BuiltinIndex = 0; 4378 break; 4379 4380 case Builtin::BI__sync_fetch_and_sub: 4381 case Builtin::BI__sync_fetch_and_sub_1: 4382 case Builtin::BI__sync_fetch_and_sub_2: 4383 case Builtin::BI__sync_fetch_and_sub_4: 4384 case Builtin::BI__sync_fetch_and_sub_8: 4385 case Builtin::BI__sync_fetch_and_sub_16: 4386 BuiltinIndex = 1; 4387 break; 4388 4389 case Builtin::BI__sync_fetch_and_or: 4390 case Builtin::BI__sync_fetch_and_or_1: 4391 case Builtin::BI__sync_fetch_and_or_2: 4392 case Builtin::BI__sync_fetch_and_or_4: 4393 case Builtin::BI__sync_fetch_and_or_8: 4394 case Builtin::BI__sync_fetch_and_or_16: 4395 BuiltinIndex = 2; 4396 break; 4397 4398 case Builtin::BI__sync_fetch_and_and: 4399 case Builtin::BI__sync_fetch_and_and_1: 4400 case Builtin::BI__sync_fetch_and_and_2: 4401 case Builtin::BI__sync_fetch_and_and_4: 4402 case Builtin::BI__sync_fetch_and_and_8: 4403 case Builtin::BI__sync_fetch_and_and_16: 4404 BuiltinIndex = 3; 4405 break; 4406 4407 case Builtin::BI__sync_fetch_and_xor: 4408 case Builtin::BI__sync_fetch_and_xor_1: 4409 case Builtin::BI__sync_fetch_and_xor_2: 4410 case Builtin::BI__sync_fetch_and_xor_4: 4411 case Builtin::BI__sync_fetch_and_xor_8: 4412 case Builtin::BI__sync_fetch_and_xor_16: 4413 BuiltinIndex = 4; 4414 break; 4415 4416 case Builtin::BI__sync_fetch_and_nand: 4417 case Builtin::BI__sync_fetch_and_nand_1: 4418 case Builtin::BI__sync_fetch_and_nand_2: 4419 case Builtin::BI__sync_fetch_and_nand_4: 4420 case Builtin::BI__sync_fetch_and_nand_8: 4421 case Builtin::BI__sync_fetch_and_nand_16: 4422 BuiltinIndex = 5; 4423 WarnAboutSemanticsChange = true; 4424 break; 4425 4426 case Builtin::BI__sync_add_and_fetch: 4427 case Builtin::BI__sync_add_and_fetch_1: 4428 case Builtin::BI__sync_add_and_fetch_2: 4429 case Builtin::BI__sync_add_and_fetch_4: 4430 case Builtin::BI__sync_add_and_fetch_8: 4431 case Builtin::BI__sync_add_and_fetch_16: 4432 BuiltinIndex = 6; 4433 break; 4434 4435 case Builtin::BI__sync_sub_and_fetch: 4436 case Builtin::BI__sync_sub_and_fetch_1: 4437 case Builtin::BI__sync_sub_and_fetch_2: 4438 case Builtin::BI__sync_sub_and_fetch_4: 4439 case Builtin::BI__sync_sub_and_fetch_8: 4440 case Builtin::BI__sync_sub_and_fetch_16: 4441 BuiltinIndex = 7; 4442 break; 4443 4444 case Builtin::BI__sync_and_and_fetch: 4445 case Builtin::BI__sync_and_and_fetch_1: 4446 case Builtin::BI__sync_and_and_fetch_2: 4447 case Builtin::BI__sync_and_and_fetch_4: 4448 case Builtin::BI__sync_and_and_fetch_8: 4449 case Builtin::BI__sync_and_and_fetch_16: 4450 BuiltinIndex = 8; 4451 break; 4452 4453 case Builtin::BI__sync_or_and_fetch: 4454 case Builtin::BI__sync_or_and_fetch_1: 4455 case Builtin::BI__sync_or_and_fetch_2: 4456 case Builtin::BI__sync_or_and_fetch_4: 4457 case Builtin::BI__sync_or_and_fetch_8: 4458 case Builtin::BI__sync_or_and_fetch_16: 4459 BuiltinIndex = 9; 4460 break; 4461 4462 case Builtin::BI__sync_xor_and_fetch: 4463 case Builtin::BI__sync_xor_and_fetch_1: 4464 case Builtin::BI__sync_xor_and_fetch_2: 4465 case Builtin::BI__sync_xor_and_fetch_4: 4466 case Builtin::BI__sync_xor_and_fetch_8: 4467 case Builtin::BI__sync_xor_and_fetch_16: 4468 BuiltinIndex = 10; 4469 break; 4470 4471 case Builtin::BI__sync_nand_and_fetch: 4472 case Builtin::BI__sync_nand_and_fetch_1: 4473 case Builtin::BI__sync_nand_and_fetch_2: 4474 case Builtin::BI__sync_nand_and_fetch_4: 4475 case Builtin::BI__sync_nand_and_fetch_8: 4476 case Builtin::BI__sync_nand_and_fetch_16: 4477 BuiltinIndex = 11; 4478 WarnAboutSemanticsChange = true; 4479 break; 4480 4481 case Builtin::BI__sync_val_compare_and_swap: 4482 case Builtin::BI__sync_val_compare_and_swap_1: 4483 case Builtin::BI__sync_val_compare_and_swap_2: 4484 case Builtin::BI__sync_val_compare_and_swap_4: 4485 case Builtin::BI__sync_val_compare_and_swap_8: 4486 case Builtin::BI__sync_val_compare_and_swap_16: 4487 BuiltinIndex = 12; 4488 NumFixed = 2; 4489 break; 4490 4491 case Builtin::BI__sync_bool_compare_and_swap: 4492 case Builtin::BI__sync_bool_compare_and_swap_1: 4493 case Builtin::BI__sync_bool_compare_and_swap_2: 4494 case Builtin::BI__sync_bool_compare_and_swap_4: 4495 case Builtin::BI__sync_bool_compare_and_swap_8: 4496 case Builtin::BI__sync_bool_compare_and_swap_16: 4497 BuiltinIndex = 13; 4498 NumFixed = 2; 4499 ResultType = Context.BoolTy; 4500 break; 4501 4502 case Builtin::BI__sync_lock_test_and_set: 4503 case Builtin::BI__sync_lock_test_and_set_1: 4504 case Builtin::BI__sync_lock_test_and_set_2: 4505 case Builtin::BI__sync_lock_test_and_set_4: 4506 case Builtin::BI__sync_lock_test_and_set_8: 4507 case Builtin::BI__sync_lock_test_and_set_16: 4508 BuiltinIndex = 14; 4509 break; 4510 4511 case Builtin::BI__sync_lock_release: 4512 case Builtin::BI__sync_lock_release_1: 4513 case Builtin::BI__sync_lock_release_2: 4514 case Builtin::BI__sync_lock_release_4: 4515 case Builtin::BI__sync_lock_release_8: 4516 case Builtin::BI__sync_lock_release_16: 4517 BuiltinIndex = 15; 4518 NumFixed = 0; 4519 ResultType = Context.VoidTy; 4520 break; 4521 4522 case Builtin::BI__sync_swap: 4523 case Builtin::BI__sync_swap_1: 4524 case Builtin::BI__sync_swap_2: 4525 case Builtin::BI__sync_swap_4: 4526 case Builtin::BI__sync_swap_8: 4527 case Builtin::BI__sync_swap_16: 4528 BuiltinIndex = 16; 4529 break; 4530 } 4531 4532 // Now that we know how many fixed arguments we expect, first check that we 4533 // have at least that many. 4534 if (TheCall->getNumArgs() < 1+NumFixed) { 4535 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4536 << 0 << 1 + NumFixed << TheCall->getNumArgs() << /*is non object*/ 0 4537 << Callee->getSourceRange(); 4538 return ExprError(); 4539 } 4540 4541 Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst) 4542 << Callee->getSourceRange(); 4543 4544 if (WarnAboutSemanticsChange) { 4545 Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change) 4546 << Callee->getSourceRange(); 4547 } 4548 4549 // Get the decl for the concrete builtin from this, we can tell what the 4550 // concrete integer type we should convert to is. 4551 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; 4552 StringRef NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID); 4553 FunctionDecl *NewBuiltinDecl; 4554 if (NewBuiltinID == BuiltinID) 4555 NewBuiltinDecl = FDecl; 4556 else { 4557 // Perform builtin lookup to avoid redeclaring it. 4558 DeclarationName DN(&Context.Idents.get(NewBuiltinName)); 4559 LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName); 4560 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); 4561 assert(Res.getFoundDecl()); 4562 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); 4563 if (!NewBuiltinDecl) 4564 return ExprError(); 4565 } 4566 4567 // The first argument --- the pointer --- has a fixed type; we 4568 // deduce the types of the rest of the arguments accordingly. Walk 4569 // the remaining arguments, converting them to the deduced value type. 4570 for (unsigned i = 0; i != NumFixed; ++i) { 4571 ExprResult Arg = TheCall->getArg(i+1); 4572 4573 // GCC does an implicit conversion to the pointer or integer ValType. This 4574 // can fail in some cases (1i -> int**), check for this error case now. 4575 // Initialize the argument. 4576 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 4577 ValType, /*consume*/ false); 4578 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4579 if (Arg.isInvalid()) 4580 return ExprError(); 4581 4582 // Okay, we have something that *can* be converted to the right type. Check 4583 // to see if there is a potentially weird extension going on here. This can 4584 // happen when you do an atomic operation on something like an char* and 4585 // pass in 42. The 42 gets converted to char. This is even more strange 4586 // for things like 45.123 -> char, etc. 4587 // FIXME: Do this check. 4588 TheCall->setArg(i+1, Arg.get()); 4589 } 4590 4591 // Create a new DeclRefExpr to refer to the new decl. 4592 DeclRefExpr *NewDRE = DeclRefExpr::Create( 4593 Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl, 4594 /*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy, 4595 DRE->getValueKind(), nullptr, nullptr, DRE->isNonOdrUse()); 4596 4597 // Set the callee in the CallExpr. 4598 // FIXME: This loses syntactic information. 4599 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); 4600 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, 4601 CK_BuiltinFnToFnPtr); 4602 TheCall->setCallee(PromotedCall.get()); 4603 4604 // Change the result type of the call to match the original value type. This 4605 // is arbitrary, but the codegen for these builtins ins design to handle it 4606 // gracefully. 4607 TheCall->setType(ResultType); 4608 4609 // Prohibit problematic uses of bit-precise integer types with atomic 4610 // builtins. The arguments would have already been converted to the first 4611 // argument's type, so only need to check the first argument. 4612 const auto *BitIntValType = ValType->getAs<BitIntType>(); 4613 if (BitIntValType && !llvm::isPowerOf2_64(BitIntValType->getNumBits())) { 4614 Diag(FirstArg->getExprLoc(), diag::err_atomic_builtin_ext_int_size); 4615 return ExprError(); 4616 } 4617 4618 return TheCallResult; 4619 } 4620 4621 ExprResult Sema::BuiltinNontemporalOverloaded(ExprResult TheCallResult) { 4622 CallExpr *TheCall = (CallExpr *)TheCallResult.get(); 4623 DeclRefExpr *DRE = 4624 cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 4625 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4626 unsigned BuiltinID = FDecl->getBuiltinID(); 4627 assert((BuiltinID == Builtin::BI__builtin_nontemporal_store || 4628 BuiltinID == Builtin::BI__builtin_nontemporal_load) && 4629 "Unexpected nontemporal load/store builtin!"); 4630 bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store; 4631 unsigned numArgs = isStore ? 2 : 1; 4632 4633 // Ensure that we have the proper number of arguments. 4634 if (checkArgCount(TheCall, numArgs)) 4635 return ExprError(); 4636 4637 // Inspect the last argument of the nontemporal builtin. This should always 4638 // be a pointer type, from which we imply the type of the memory access. 4639 // Because it is a pointer type, we don't have to worry about any implicit 4640 // casts here. 4641 Expr *PointerArg = TheCall->getArg(numArgs - 1); 4642 ExprResult PointerArgResult = 4643 DefaultFunctionArrayLvalueConversion(PointerArg); 4644 4645 if (PointerArgResult.isInvalid()) 4646 return ExprError(); 4647 PointerArg = PointerArgResult.get(); 4648 TheCall->setArg(numArgs - 1, PointerArg); 4649 4650 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); 4651 if (!pointerType) { 4652 Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer) 4653 << PointerArg->getType() << PointerArg->getSourceRange(); 4654 return ExprError(); 4655 } 4656 4657 QualType ValType = pointerType->getPointeeType(); 4658 4659 // Strip any qualifiers off ValType. 4660 ValType = ValType.getUnqualifiedType(); 4661 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4662 !ValType->isBlockPointerType() && !ValType->isFloatingType() && 4663 !ValType->isVectorType()) { 4664 Diag(DRE->getBeginLoc(), 4665 diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) 4666 << PointerArg->getType() << PointerArg->getSourceRange(); 4667 return ExprError(); 4668 } 4669 4670 if (!isStore) { 4671 TheCall->setType(ValType); 4672 return TheCallResult; 4673 } 4674 4675 ExprResult ValArg = TheCall->getArg(0); 4676 InitializedEntity Entity = InitializedEntity::InitializeParameter( 4677 Context, ValType, /*consume*/ false); 4678 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); 4679 if (ValArg.isInvalid()) 4680 return ExprError(); 4681 4682 TheCall->setArg(0, ValArg.get()); 4683 TheCall->setType(Context.VoidTy); 4684 return TheCallResult; 4685 } 4686 4687 /// CheckObjCString - Checks that the format string argument to the os_log() 4688 /// and os_trace() functions is correct, and converts it to const char *. 4689 ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) { 4690 Arg = Arg->IgnoreParenCasts(); 4691 auto *Literal = dyn_cast<StringLiteral>(Arg); 4692 if (!Literal) { 4693 if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) { 4694 Literal = ObjcLiteral->getString(); 4695 } 4696 } 4697 4698 if (!Literal || (!Literal->isOrdinary() && !Literal->isUTF8())) { 4699 return ExprError( 4700 Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant) 4701 << Arg->getSourceRange()); 4702 } 4703 4704 ExprResult Result(Literal); 4705 QualType ResultTy = Context.getPointerType(Context.CharTy.withConst()); 4706 InitializedEntity Entity = 4707 InitializedEntity::InitializeParameter(Context, ResultTy, false); 4708 Result = PerformCopyInitialization(Entity, SourceLocation(), Result); 4709 return Result; 4710 } 4711 4712 /// Check that the user is calling the appropriate va_start builtin for the 4713 /// target and calling convention. 4714 static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) { 4715 const llvm::Triple &TT = S.Context.getTargetInfo().getTriple(); 4716 bool IsX64 = TT.getArch() == llvm::Triple::x86_64; 4717 bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 || 4718 TT.getArch() == llvm::Triple::aarch64_32); 4719 bool IsWindows = TT.isOSWindows(); 4720 bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start; 4721 if (IsX64 || IsAArch64) { 4722 CallingConv CC = CC_C; 4723 if (const FunctionDecl *FD = S.getCurFunctionDecl()) 4724 CC = FD->getType()->castAs<FunctionType>()->getCallConv(); 4725 if (IsMSVAStart) { 4726 // Don't allow this in System V ABI functions. 4727 if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64)) 4728 return S.Diag(Fn->getBeginLoc(), 4729 diag::err_ms_va_start_used_in_sysv_function); 4730 } else { 4731 // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions. 4732 // On x64 Windows, don't allow this in System V ABI functions. 4733 // (Yes, that means there's no corresponding way to support variadic 4734 // System V ABI functions on Windows.) 4735 if ((IsWindows && CC == CC_X86_64SysV) || 4736 (!IsWindows && CC == CC_Win64)) 4737 return S.Diag(Fn->getBeginLoc(), 4738 diag::err_va_start_used_in_wrong_abi_function) 4739 << !IsWindows; 4740 } 4741 return false; 4742 } 4743 4744 if (IsMSVAStart) 4745 return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only); 4746 return false; 4747 } 4748 4749 static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn, 4750 ParmVarDecl **LastParam = nullptr) { 4751 // Determine whether the current function, block, or obj-c method is variadic 4752 // and get its parameter list. 4753 bool IsVariadic = false; 4754 ArrayRef<ParmVarDecl *> Params; 4755 DeclContext *Caller = S.CurContext; 4756 if (auto *Block = dyn_cast<BlockDecl>(Caller)) { 4757 IsVariadic = Block->isVariadic(); 4758 Params = Block->parameters(); 4759 } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) { 4760 IsVariadic = FD->isVariadic(); 4761 Params = FD->parameters(); 4762 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) { 4763 IsVariadic = MD->isVariadic(); 4764 // FIXME: This isn't correct for methods (results in bogus warning). 4765 Params = MD->parameters(); 4766 } else if (isa<CapturedDecl>(Caller)) { 4767 // We don't support va_start in a CapturedDecl. 4768 S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt); 4769 return true; 4770 } else { 4771 // This must be some other declcontext that parses exprs. 4772 S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function); 4773 return true; 4774 } 4775 4776 if (!IsVariadic) { 4777 S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function); 4778 return true; 4779 } 4780 4781 if (LastParam) 4782 *LastParam = Params.empty() ? nullptr : Params.back(); 4783 4784 return false; 4785 } 4786 4787 bool Sema::BuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) { 4788 Expr *Fn = TheCall->getCallee(); 4789 4790 if (checkVAStartABI(*this, BuiltinID, Fn)) 4791 return true; 4792 4793 // In C23 mode, va_start only needs one argument. However, the builtin still 4794 // requires two arguments (which matches the behavior of the GCC builtin), 4795 // <stdarg.h> passes `0` as the second argument in C23 mode. 4796 if (checkArgCount(TheCall, 2)) 4797 return true; 4798 4799 // Type-check the first argument normally. 4800 if (checkBuiltinArgument(*this, TheCall, 0)) 4801 return true; 4802 4803 // Check that the current function is variadic, and get its last parameter. 4804 ParmVarDecl *LastParam; 4805 if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam)) 4806 return true; 4807 4808 // Verify that the second argument to the builtin is the last argument of the 4809 // current function or method. In C23 mode, if the second argument is an 4810 // integer constant expression with value 0, then we don't bother with this 4811 // check. 4812 bool SecondArgIsLastNamedArgument = false; 4813 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); 4814 if (std::optional<llvm::APSInt> Val = 4815 TheCall->getArg(1)->getIntegerConstantExpr(Context); 4816 Val && LangOpts.C23 && *Val == 0) 4817 return false; 4818 4819 // These are valid if SecondArgIsLastNamedArgument is false after the next 4820 // block. 4821 QualType Type; 4822 SourceLocation ParamLoc; 4823 bool IsCRegister = false; 4824 4825 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { 4826 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { 4827 SecondArgIsLastNamedArgument = PV == LastParam; 4828 4829 Type = PV->getType(); 4830 ParamLoc = PV->getLocation(); 4831 IsCRegister = 4832 PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus; 4833 } 4834 } 4835 4836 if (!SecondArgIsLastNamedArgument) 4837 Diag(TheCall->getArg(1)->getBeginLoc(), 4838 diag::warn_second_arg_of_va_start_not_last_named_param); 4839 else if (IsCRegister || Type->isReferenceType() || 4840 Type->isSpecificBuiltinType(BuiltinType::Float) || [=] { 4841 // Promotable integers are UB, but enumerations need a bit of 4842 // extra checking to see what their promotable type actually is. 4843 if (!Context.isPromotableIntegerType(Type)) 4844 return false; 4845 if (!Type->isEnumeralType()) 4846 return true; 4847 const EnumDecl *ED = Type->castAs<EnumType>()->getDecl(); 4848 return !(ED && 4849 Context.typesAreCompatible(ED->getPromotionType(), Type)); 4850 }()) { 4851 unsigned Reason = 0; 4852 if (Type->isReferenceType()) Reason = 1; 4853 else if (IsCRegister) Reason = 2; 4854 Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason; 4855 Diag(ParamLoc, diag::note_parameter_type) << Type; 4856 } 4857 4858 return false; 4859 } 4860 4861 bool Sema::BuiltinVAStartARMMicrosoft(CallExpr *Call) { 4862 auto IsSuitablyTypedFormatArgument = [this](const Expr *Arg) -> bool { 4863 const LangOptions &LO = getLangOpts(); 4864 4865 if (LO.CPlusPlus) 4866 return Arg->getType() 4867 .getCanonicalType() 4868 .getTypePtr() 4869 ->getPointeeType() 4870 .withoutLocalFastQualifiers() == Context.CharTy; 4871 4872 // In C, allow aliasing through `char *`, this is required for AArch64 at 4873 // least. 4874 return true; 4875 }; 4876 4877 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, 4878 // const char *named_addr); 4879 4880 Expr *Func = Call->getCallee(); 4881 4882 if (Call->getNumArgs() < 3) 4883 return Diag(Call->getEndLoc(), 4884 diag::err_typecheck_call_too_few_args_at_least) 4885 << 0 /*function call*/ << 3 << Call->getNumArgs() 4886 << /*is non object*/ 0; 4887 4888 // Type-check the first argument normally. 4889 if (checkBuiltinArgument(*this, Call, 0)) 4890 return true; 4891 4892 // Check that the current function is variadic. 4893 if (checkVAStartIsInVariadicFunction(*this, Func)) 4894 return true; 4895 4896 // __va_start on Windows does not validate the parameter qualifiers 4897 4898 const Expr *Arg1 = Call->getArg(1)->IgnoreParens(); 4899 const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr(); 4900 4901 const Expr *Arg2 = Call->getArg(2)->IgnoreParens(); 4902 const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr(); 4903 4904 const QualType &ConstCharPtrTy = 4905 Context.getPointerType(Context.CharTy.withConst()); 4906 if (!Arg1Ty->isPointerType() || !IsSuitablyTypedFormatArgument(Arg1)) 4907 Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible) 4908 << Arg1->getType() << ConstCharPtrTy << 1 /* different class */ 4909 << 0 /* qualifier difference */ 4910 << 3 /* parameter mismatch */ 4911 << 2 << Arg1->getType() << ConstCharPtrTy; 4912 4913 const QualType SizeTy = Context.getSizeType(); 4914 if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy) 4915 Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible) 4916 << Arg2->getType() << SizeTy << 1 /* different class */ 4917 << 0 /* qualifier difference */ 4918 << 3 /* parameter mismatch */ 4919 << 3 << Arg2->getType() << SizeTy; 4920 4921 return false; 4922 } 4923 4924 bool Sema::BuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID) { 4925 if (checkArgCount(TheCall, 2)) 4926 return true; 4927 4928 if (BuiltinID == Builtin::BI__builtin_isunordered && 4929 TheCall->getFPFeaturesInEffect(getLangOpts()).getNoHonorNaNs()) 4930 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4931 << 1 << 0 << TheCall->getSourceRange(); 4932 4933 ExprResult OrigArg0 = TheCall->getArg(0); 4934 ExprResult OrigArg1 = TheCall->getArg(1); 4935 4936 // Do standard promotions between the two arguments, returning their common 4937 // type. 4938 QualType Res = UsualArithmeticConversions( 4939 OrigArg0, OrigArg1, TheCall->getExprLoc(), ACK_Comparison); 4940 if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) 4941 return true; 4942 4943 // Make sure any conversions are pushed back into the call; this is 4944 // type safe since unordered compare builtins are declared as "_Bool 4945 // foo(...)". 4946 TheCall->setArg(0, OrigArg0.get()); 4947 TheCall->setArg(1, OrigArg1.get()); 4948 4949 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) 4950 return false; 4951 4952 // If the common type isn't a real floating type, then the arguments were 4953 // invalid for this operation. 4954 if (Res.isNull() || !Res->isRealFloatingType()) 4955 return Diag(OrigArg0.get()->getBeginLoc(), 4956 diag::err_typecheck_call_invalid_ordered_compare) 4957 << OrigArg0.get()->getType() << OrigArg1.get()->getType() 4958 << SourceRange(OrigArg0.get()->getBeginLoc(), 4959 OrigArg1.get()->getEndLoc()); 4960 4961 return false; 4962 } 4963 4964 bool Sema::BuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs, 4965 unsigned BuiltinID) { 4966 if (checkArgCount(TheCall, NumArgs)) 4967 return true; 4968 4969 FPOptions FPO = TheCall->getFPFeaturesInEffect(getLangOpts()); 4970 if (FPO.getNoHonorInfs() && (BuiltinID == Builtin::BI__builtin_isfinite || 4971 BuiltinID == Builtin::BI__builtin_isinf || 4972 BuiltinID == Builtin::BI__builtin_isinf_sign)) 4973 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4974 << 0 << 0 << TheCall->getSourceRange(); 4975 4976 if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan || 4977 BuiltinID == Builtin::BI__builtin_isunordered)) 4978 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4979 << 1 << 0 << TheCall->getSourceRange(); 4980 4981 bool IsFPClass = NumArgs == 2; 4982 4983 // Find out position of floating-point argument. 4984 unsigned FPArgNo = IsFPClass ? 0 : NumArgs - 1; 4985 4986 // We can count on all parameters preceding the floating-point just being int. 4987 // Try all of those. 4988 for (unsigned i = 0; i < FPArgNo; ++i) { 4989 Expr *Arg = TheCall->getArg(i); 4990 4991 if (Arg->isTypeDependent()) 4992 return false; 4993 4994 ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy, 4995 AssignmentAction::Passing); 4996 4997 if (Res.isInvalid()) 4998 return true; 4999 TheCall->setArg(i, Res.get()); 5000 } 5001 5002 Expr *OrigArg = TheCall->getArg(FPArgNo); 5003 5004 if (OrigArg->isTypeDependent()) 5005 return false; 5006 5007 // Usual Unary Conversions will convert half to float, which we want for 5008 // machines that use fp16 conversion intrinsics. Else, we wnat to leave the 5009 // type how it is, but do normal L->Rvalue conversions. 5010 if (Context.getTargetInfo().useFP16ConversionIntrinsics()) { 5011 ExprResult Res = UsualUnaryConversions(OrigArg); 5012 5013 if (!Res.isUsable()) 5014 return true; 5015 OrigArg = Res.get(); 5016 } else { 5017 ExprResult Res = DefaultFunctionArrayLvalueConversion(OrigArg); 5018 5019 if (!Res.isUsable()) 5020 return true; 5021 OrigArg = Res.get(); 5022 } 5023 TheCall->setArg(FPArgNo, OrigArg); 5024 5025 QualType VectorResultTy; 5026 QualType ElementTy = OrigArg->getType(); 5027 // TODO: When all classification function are implemented with is_fpclass, 5028 // vector argument can be supported in all of them. 5029 if (ElementTy->isVectorType() && IsFPClass) { 5030 VectorResultTy = GetSignedVectorType(ElementTy); 5031 ElementTy = ElementTy->castAs<VectorType>()->getElementType(); 5032 } 5033 5034 // This operation requires a non-_Complex floating-point number. 5035 if (!ElementTy->isRealFloatingType()) 5036 return Diag(OrigArg->getBeginLoc(), 5037 diag::err_typecheck_call_invalid_unary_fp) 5038 << OrigArg->getType() << OrigArg->getSourceRange(); 5039 5040 // __builtin_isfpclass has integer parameter that specify test mask. It is 5041 // passed in (...), so it should be analyzed completely here. 5042 if (IsFPClass) 5043 if (BuiltinConstantArgRange(TheCall, 1, 0, llvm::fcAllFlags)) 5044 return true; 5045 5046 // TODO: enable this code to all classification functions. 5047 if (IsFPClass) { 5048 QualType ResultTy; 5049 if (!VectorResultTy.isNull()) 5050 ResultTy = VectorResultTy; 5051 else 5052 ResultTy = Context.IntTy; 5053 TheCall->setType(ResultTy); 5054 } 5055 5056 return false; 5057 } 5058 5059 bool Sema::BuiltinComplex(CallExpr *TheCall) { 5060 if (checkArgCount(TheCall, 2)) 5061 return true; 5062 5063 bool Dependent = false; 5064 for (unsigned I = 0; I != 2; ++I) { 5065 Expr *Arg = TheCall->getArg(I); 5066 QualType T = Arg->getType(); 5067 if (T->isDependentType()) { 5068 Dependent = true; 5069 continue; 5070 } 5071 5072 // Despite supporting _Complex int, GCC requires a real floating point type 5073 // for the operands of __builtin_complex. 5074 if (!T->isRealFloatingType()) { 5075 return Diag(Arg->getBeginLoc(), diag::err_typecheck_call_requires_real_fp) 5076 << Arg->getType() << Arg->getSourceRange(); 5077 } 5078 5079 ExprResult Converted = DefaultLvalueConversion(Arg); 5080 if (Converted.isInvalid()) 5081 return true; 5082 TheCall->setArg(I, Converted.get()); 5083 } 5084 5085 if (Dependent) { 5086 TheCall->setType(Context.DependentTy); 5087 return false; 5088 } 5089 5090 Expr *Real = TheCall->getArg(0); 5091 Expr *Imag = TheCall->getArg(1); 5092 if (!Context.hasSameType(Real->getType(), Imag->getType())) { 5093 return Diag(Real->getBeginLoc(), 5094 diag::err_typecheck_call_different_arg_types) 5095 << Real->getType() << Imag->getType() 5096 << Real->getSourceRange() << Imag->getSourceRange(); 5097 } 5098 5099 // We don't allow _Complex _Float16 nor _Complex __fp16 as type specifiers; 5100 // don't allow this builtin to form those types either. 5101 // FIXME: Should we allow these types? 5102 if (Real->getType()->isFloat16Type()) 5103 return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) 5104 << "_Float16"; 5105 if (Real->getType()->isHalfType()) 5106 return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) 5107 << "half"; 5108 5109 TheCall->setType(Context.getComplexType(Real->getType())); 5110 return false; 5111 } 5112 5113 /// BuiltinShuffleVector - Handle __builtin_shufflevector. 5114 // This is declared to take (...), so we have to check everything. 5115 ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) { 5116 if (TheCall->getNumArgs() < 2) 5117 return ExprError(Diag(TheCall->getEndLoc(), 5118 diag::err_typecheck_call_too_few_args_at_least) 5119 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 5120 << /*is non object*/ 0 << TheCall->getSourceRange()); 5121 5122 // Determine which of the following types of shufflevector we're checking: 5123 // 1) unary, vector mask: (lhs, mask) 5124 // 2) binary, scalar mask: (lhs, rhs, index, ..., index) 5125 QualType resType = TheCall->getArg(0)->getType(); 5126 unsigned numElements = 0; 5127 5128 if (!TheCall->getArg(0)->isTypeDependent() && 5129 !TheCall->getArg(1)->isTypeDependent()) { 5130 QualType LHSType = TheCall->getArg(0)->getType(); 5131 QualType RHSType = TheCall->getArg(1)->getType(); 5132 5133 if (!LHSType->isVectorType() || !RHSType->isVectorType()) 5134 return ExprError( 5135 Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector) 5136 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false 5137 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5138 TheCall->getArg(1)->getEndLoc())); 5139 5140 numElements = LHSType->castAs<VectorType>()->getNumElements(); 5141 unsigned numResElements = TheCall->getNumArgs() - 2; 5142 5143 // Check to see if we have a call with 2 vector arguments, the unary shuffle 5144 // with mask. If so, verify that RHS is an integer vector type with the 5145 // same number of elts as lhs. 5146 if (TheCall->getNumArgs() == 2) { 5147 if (!RHSType->hasIntegerRepresentation() || 5148 RHSType->castAs<VectorType>()->getNumElements() != numElements) 5149 return ExprError(Diag(TheCall->getBeginLoc(), 5150 diag::err_vec_builtin_incompatible_vector) 5151 << TheCall->getDirectCallee() 5152 << /*isMorethantwoArgs*/ false 5153 << SourceRange(TheCall->getArg(1)->getBeginLoc(), 5154 TheCall->getArg(1)->getEndLoc())); 5155 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { 5156 return ExprError(Diag(TheCall->getBeginLoc(), 5157 diag::err_vec_builtin_incompatible_vector) 5158 << TheCall->getDirectCallee() 5159 << /*isMorethantwoArgs*/ false 5160 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5161 TheCall->getArg(1)->getEndLoc())); 5162 } else if (numElements != numResElements) { 5163 QualType eltType = LHSType->castAs<VectorType>()->getElementType(); 5164 resType = 5165 Context.getVectorType(eltType, numResElements, VectorKind::Generic); 5166 } 5167 } 5168 5169 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { 5170 if (TheCall->getArg(i)->isTypeDependent() || 5171 TheCall->getArg(i)->isValueDependent()) 5172 continue; 5173 5174 std::optional<llvm::APSInt> Result; 5175 if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(Context))) 5176 return ExprError(Diag(TheCall->getBeginLoc(), 5177 diag::err_shufflevector_nonconstant_argument) 5178 << TheCall->getArg(i)->getSourceRange()); 5179 5180 // Allow -1 which will be translated to undef in the IR. 5181 if (Result->isSigned() && Result->isAllOnes()) 5182 continue; 5183 5184 if (Result->getActiveBits() > 64 || 5185 Result->getZExtValue() >= numElements * 2) 5186 return ExprError(Diag(TheCall->getBeginLoc(), 5187 diag::err_shufflevector_argument_too_large) 5188 << TheCall->getArg(i)->getSourceRange()); 5189 } 5190 5191 SmallVector<Expr*, 32> exprs; 5192 5193 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { 5194 exprs.push_back(TheCall->getArg(i)); 5195 TheCall->setArg(i, nullptr); 5196 } 5197 5198 return new (Context) ShuffleVectorExpr(Context, exprs, resType, 5199 TheCall->getCallee()->getBeginLoc(), 5200 TheCall->getRParenLoc()); 5201 } 5202 5203 ExprResult Sema::ConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, 5204 SourceLocation BuiltinLoc, 5205 SourceLocation RParenLoc) { 5206 ExprValueKind VK = VK_PRValue; 5207 ExprObjectKind OK = OK_Ordinary; 5208 QualType DstTy = TInfo->getType(); 5209 QualType SrcTy = E->getType(); 5210 5211 if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) 5212 return ExprError(Diag(BuiltinLoc, 5213 diag::err_convertvector_non_vector) 5214 << E->getSourceRange()); 5215 if (!DstTy->isVectorType() && !DstTy->isDependentType()) 5216 return ExprError(Diag(BuiltinLoc, diag::err_builtin_non_vector_type) 5217 << "second" 5218 << "__builtin_convertvector"); 5219 5220 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { 5221 unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements(); 5222 unsigned DstElts = DstTy->castAs<VectorType>()->getNumElements(); 5223 if (SrcElts != DstElts) 5224 return ExprError(Diag(BuiltinLoc, 5225 diag::err_convertvector_incompatible_vector) 5226 << E->getSourceRange()); 5227 } 5228 5229 return new (Context) class ConvertVectorExpr(E, TInfo, DstTy, VK, OK, 5230 BuiltinLoc, RParenLoc); 5231 } 5232 5233 bool Sema::BuiltinPrefetch(CallExpr *TheCall) { 5234 unsigned NumArgs = TheCall->getNumArgs(); 5235 5236 if (NumArgs > 3) 5237 return Diag(TheCall->getEndLoc(), 5238 diag::err_typecheck_call_too_many_args_at_most) 5239 << 0 /*function call*/ << 3 << NumArgs << /*is non object*/ 0 5240 << TheCall->getSourceRange(); 5241 5242 // Argument 0 is checked for us and the remaining arguments must be 5243 // constant integers. 5244 for (unsigned i = 1; i != NumArgs; ++i) 5245 if (BuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) 5246 return true; 5247 5248 return false; 5249 } 5250 5251 bool Sema::BuiltinArithmeticFence(CallExpr *TheCall) { 5252 if (!Context.getTargetInfo().checkArithmeticFenceSupported()) 5253 return Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 5254 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5255 if (checkArgCount(TheCall, 1)) 5256 return true; 5257 Expr *Arg = TheCall->getArg(0); 5258 if (Arg->isInstantiationDependent()) 5259 return false; 5260 5261 QualType ArgTy = Arg->getType(); 5262 if (!ArgTy->hasFloatingRepresentation()) 5263 return Diag(TheCall->getEndLoc(), diag::err_typecheck_expect_flt_or_vector) 5264 << ArgTy; 5265 if (Arg->isLValue()) { 5266 ExprResult FirstArg = DefaultLvalueConversion(Arg); 5267 TheCall->setArg(0, FirstArg.get()); 5268 } 5269 TheCall->setType(TheCall->getArg(0)->getType()); 5270 return false; 5271 } 5272 5273 bool Sema::BuiltinAssume(CallExpr *TheCall) { 5274 Expr *Arg = TheCall->getArg(0); 5275 if (Arg->isInstantiationDependent()) return false; 5276 5277 if (Arg->HasSideEffects(Context)) 5278 Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects) 5279 << Arg->getSourceRange() 5280 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); 5281 5282 return false; 5283 } 5284 5285 bool Sema::BuiltinAllocaWithAlign(CallExpr *TheCall) { 5286 // The alignment must be a constant integer. 5287 Expr *Arg = TheCall->getArg(1); 5288 5289 // We can't check the value of a dependent argument. 5290 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 5291 if (const auto *UE = 5292 dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts())) 5293 if (UE->getKind() == UETT_AlignOf || 5294 UE->getKind() == UETT_PreferredAlignOf) 5295 Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof) 5296 << Arg->getSourceRange(); 5297 5298 llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context); 5299 5300 if (!Result.isPowerOf2()) 5301 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5302 << Arg->getSourceRange(); 5303 5304 if (Result < Context.getCharWidth()) 5305 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small) 5306 << (unsigned)Context.getCharWidth() << Arg->getSourceRange(); 5307 5308 if (Result > std::numeric_limits<int32_t>::max()) 5309 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big) 5310 << std::numeric_limits<int32_t>::max() << Arg->getSourceRange(); 5311 } 5312 5313 return false; 5314 } 5315 5316 bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) { 5317 if (checkArgCountRange(TheCall, 2, 3)) 5318 return true; 5319 5320 unsigned NumArgs = TheCall->getNumArgs(); 5321 Expr *FirstArg = TheCall->getArg(0); 5322 5323 { 5324 ExprResult FirstArgResult = 5325 DefaultFunctionArrayLvalueConversion(FirstArg); 5326 if (!FirstArgResult.get()->getType()->isPointerType()) { 5327 Diag(TheCall->getBeginLoc(), diag::err_builtin_assume_aligned_invalid_arg) 5328 << TheCall->getSourceRange(); 5329 return true; 5330 } 5331 TheCall->setArg(0, FirstArgResult.get()); 5332 } 5333 5334 // The alignment must be a constant integer. 5335 Expr *SecondArg = TheCall->getArg(1); 5336 5337 // We can't check the value of a dependent argument. 5338 if (!SecondArg->isValueDependent()) { 5339 llvm::APSInt Result; 5340 if (BuiltinConstantArg(TheCall, 1, Result)) 5341 return true; 5342 5343 if (!Result.isPowerOf2()) 5344 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5345 << SecondArg->getSourceRange(); 5346 5347 if (Result > Sema::MaximumAlignment) 5348 Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great) 5349 << SecondArg->getSourceRange() << Sema::MaximumAlignment; 5350 } 5351 5352 if (NumArgs > 2) { 5353 Expr *ThirdArg = TheCall->getArg(2); 5354 if (convertArgumentToType(*this, ThirdArg, Context.getSizeType())) 5355 return true; 5356 TheCall->setArg(2, ThirdArg); 5357 } 5358 5359 return false; 5360 } 5361 5362 bool Sema::BuiltinOSLogFormat(CallExpr *TheCall) { 5363 unsigned BuiltinID = 5364 cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID(); 5365 bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size; 5366 5367 unsigned NumArgs = TheCall->getNumArgs(); 5368 unsigned NumRequiredArgs = IsSizeCall ? 1 : 2; 5369 if (NumArgs < NumRequiredArgs) { 5370 return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 5371 << 0 /* function call */ << NumRequiredArgs << NumArgs 5372 << /*is non object*/ 0 << TheCall->getSourceRange(); 5373 } 5374 if (NumArgs >= NumRequiredArgs + 0x100) { 5375 return Diag(TheCall->getEndLoc(), 5376 diag::err_typecheck_call_too_many_args_at_most) 5377 << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs 5378 << /*is non object*/ 0 << TheCall->getSourceRange(); 5379 } 5380 unsigned i = 0; 5381 5382 // For formatting call, check buffer arg. 5383 if (!IsSizeCall) { 5384 ExprResult Arg(TheCall->getArg(i)); 5385 InitializedEntity Entity = InitializedEntity::InitializeParameter( 5386 Context, Context.VoidPtrTy, false); 5387 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 5388 if (Arg.isInvalid()) 5389 return true; 5390 TheCall->setArg(i, Arg.get()); 5391 i++; 5392 } 5393 5394 // Check string literal arg. 5395 unsigned FormatIdx = i; 5396 { 5397 ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i)); 5398 if (Arg.isInvalid()) 5399 return true; 5400 TheCall->setArg(i, Arg.get()); 5401 i++; 5402 } 5403 5404 // Make sure variadic args are scalar. 5405 unsigned FirstDataArg = i; 5406 while (i < NumArgs) { 5407 ExprResult Arg = DefaultVariadicArgumentPromotion( 5408 TheCall->getArg(i), VariadicFunction, nullptr); 5409 if (Arg.isInvalid()) 5410 return true; 5411 CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType()); 5412 if (ArgSize.getQuantity() >= 0x100) { 5413 return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big) 5414 << i << (int)ArgSize.getQuantity() << 0xff 5415 << TheCall->getSourceRange(); 5416 } 5417 TheCall->setArg(i, Arg.get()); 5418 i++; 5419 } 5420 5421 // Check formatting specifiers. NOTE: We're only doing this for the non-size 5422 // call to avoid duplicate diagnostics. 5423 if (!IsSizeCall) { 5424 llvm::SmallBitVector CheckedVarArgs(NumArgs, false); 5425 ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs()); 5426 bool Success = CheckFormatArguments( 5427 Args, FAPK_Variadic, FormatIdx, FirstDataArg, FST_OSLog, 5428 VariadicFunction, TheCall->getBeginLoc(), SourceRange(), 5429 CheckedVarArgs); 5430 if (!Success) 5431 return true; 5432 } 5433 5434 if (IsSizeCall) { 5435 TheCall->setType(Context.getSizeType()); 5436 } else { 5437 TheCall->setType(Context.VoidPtrTy); 5438 } 5439 return false; 5440 } 5441 5442 bool Sema::BuiltinConstantArg(CallExpr *TheCall, int ArgNum, 5443 llvm::APSInt &Result) { 5444 Expr *Arg = TheCall->getArg(ArgNum); 5445 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 5446 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 5447 5448 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; 5449 5450 std::optional<llvm::APSInt> R; 5451 if (!(R = Arg->getIntegerConstantExpr(Context))) 5452 return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) 5453 << FDecl->getDeclName() << Arg->getSourceRange(); 5454 Result = *R; 5455 return false; 5456 } 5457 5458 bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low, 5459 int High, bool RangeIsError) { 5460 if (isConstantEvaluatedContext()) 5461 return false; 5462 llvm::APSInt Result; 5463 5464 // We can't check the value of a dependent argument. 5465 Expr *Arg = TheCall->getArg(ArgNum); 5466 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5467 return false; 5468 5469 // Check constant-ness first. 5470 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5471 return true; 5472 5473 if (Result.getSExtValue() < Low || Result.getSExtValue() > High) { 5474 if (RangeIsError) 5475 return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range) 5476 << toString(Result, 10) << Low << High << Arg->getSourceRange(); 5477 else 5478 // Defer the warning until we know if the code will be emitted so that 5479 // dead code can ignore this. 5480 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 5481 PDiag(diag::warn_argument_invalid_range) 5482 << toString(Result, 10) << Low << High 5483 << Arg->getSourceRange()); 5484 } 5485 5486 return false; 5487 } 5488 5489 bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum, 5490 unsigned Num) { 5491 llvm::APSInt Result; 5492 5493 // We can't check the value of a dependent argument. 5494 Expr *Arg = TheCall->getArg(ArgNum); 5495 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5496 return false; 5497 5498 // Check constant-ness first. 5499 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5500 return true; 5501 5502 if (Result.getSExtValue() % Num != 0) 5503 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple) 5504 << Num << Arg->getSourceRange(); 5505 5506 return false; 5507 } 5508 5509 bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) { 5510 llvm::APSInt Result; 5511 5512 // We can't check the value of a dependent argument. 5513 Expr *Arg = TheCall->getArg(ArgNum); 5514 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5515 return false; 5516 5517 // Check constant-ness first. 5518 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5519 return true; 5520 5521 // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if 5522 // and only if x is a power of 2. 5523 if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0) 5524 return false; 5525 5526 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2) 5527 << Arg->getSourceRange(); 5528 } 5529 5530 static bool IsShiftedByte(llvm::APSInt Value) { 5531 if (Value.isNegative()) 5532 return false; 5533 5534 // Check if it's a shifted byte, by shifting it down 5535 while (true) { 5536 // If the value fits in the bottom byte, the check passes. 5537 if (Value < 0x100) 5538 return true; 5539 5540 // Otherwise, if the value has _any_ bits in the bottom byte, the check 5541 // fails. 5542 if ((Value & 0xFF) != 0) 5543 return false; 5544 5545 // If the bottom 8 bits are all 0, but something above that is nonzero, 5546 // then shifting the value right by 8 bits won't affect whether it's a 5547 // shifted byte or not. So do that, and go round again. 5548 Value >>= 8; 5549 } 5550 } 5551 5552 bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum, 5553 unsigned ArgBits) { 5554 llvm::APSInt Result; 5555 5556 // We can't check the value of a dependent argument. 5557 Expr *Arg = TheCall->getArg(ArgNum); 5558 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5559 return false; 5560 5561 // Check constant-ness first. 5562 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5563 return true; 5564 5565 // Truncate to the given size. 5566 Result = Result.getLoBits(ArgBits); 5567 Result.setIsUnsigned(true); 5568 5569 if (IsShiftedByte(Result)) 5570 return false; 5571 5572 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_shifted_byte) 5573 << Arg->getSourceRange(); 5574 } 5575 5576 bool Sema::BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum, 5577 unsigned ArgBits) { 5578 llvm::APSInt Result; 5579 5580 // We can't check the value of a dependent argument. 5581 Expr *Arg = TheCall->getArg(ArgNum); 5582 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5583 return false; 5584 5585 // Check constant-ness first. 5586 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5587 return true; 5588 5589 // Truncate to the given size. 5590 Result = Result.getLoBits(ArgBits); 5591 Result.setIsUnsigned(true); 5592 5593 // Check to see if it's in either of the required forms. 5594 if (IsShiftedByte(Result) || 5595 (Result > 0 && Result < 0x10000 && (Result & 0xFF) == 0xFF)) 5596 return false; 5597 5598 return Diag(TheCall->getBeginLoc(), 5599 diag::err_argument_not_shifted_byte_or_xxff) 5600 << Arg->getSourceRange(); 5601 } 5602 5603 bool Sema::BuiltinLongjmp(CallExpr *TheCall) { 5604 if (!Context.getTargetInfo().hasSjLjLowering()) 5605 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported) 5606 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5607 5608 Expr *Arg = TheCall->getArg(1); 5609 llvm::APSInt Result; 5610 5611 // TODO: This is less than ideal. Overload this to take a value. 5612 if (BuiltinConstantArg(TheCall, 1, Result)) 5613 return true; 5614 5615 if (Result != 1) 5616 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val) 5617 << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc()); 5618 5619 return false; 5620 } 5621 5622 bool Sema::BuiltinSetjmp(CallExpr *TheCall) { 5623 if (!Context.getTargetInfo().hasSjLjLowering()) 5624 return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported) 5625 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5626 return false; 5627 } 5628 5629 bool Sema::BuiltinCountedByRef(CallExpr *TheCall) { 5630 if (checkArgCount(TheCall, 1)) 5631 return true; 5632 5633 ExprResult ArgRes = UsualUnaryConversions(TheCall->getArg(0)); 5634 if (ArgRes.isInvalid()) 5635 return true; 5636 5637 // For simplicity, we support only limited expressions for the argument. 5638 // Specifically a pointer to a flexible array member:'ptr->array'. This 5639 // allows us to reject arguments with complex casting, which really shouldn't 5640 // be a huge problem. 5641 const Expr *Arg = ArgRes.get()->IgnoreParenImpCasts(); 5642 if (!isa<PointerType>(Arg->getType()) && !Arg->getType()->isArrayType()) 5643 return Diag(Arg->getBeginLoc(), 5644 diag::err_builtin_counted_by_ref_must_be_flex_array_member) 5645 << Arg->getSourceRange(); 5646 5647 if (Arg->HasSideEffects(Context)) 5648 return Diag(Arg->getBeginLoc(), 5649 diag::err_builtin_counted_by_ref_has_side_effects) 5650 << Arg->getSourceRange(); 5651 5652 if (const auto *ME = dyn_cast<MemberExpr>(Arg)) { 5653 if (!ME->isFlexibleArrayMemberLike( 5654 Context, getLangOpts().getStrictFlexArraysLevel())) 5655 return Diag(Arg->getBeginLoc(), 5656 diag::err_builtin_counted_by_ref_must_be_flex_array_member) 5657 << Arg->getSourceRange(); 5658 5659 if (auto *CATy = 5660 ME->getMemberDecl()->getType()->getAs<CountAttributedType>(); 5661 CATy && CATy->getKind() == CountAttributedType::CountedBy) { 5662 const auto *FAMDecl = cast<FieldDecl>(ME->getMemberDecl()); 5663 if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) { 5664 TheCall->setType(Context.getPointerType(CountFD->getType())); 5665 return false; 5666 } 5667 } 5668 } else { 5669 return Diag(Arg->getBeginLoc(), 5670 diag::err_builtin_counted_by_ref_must_be_flex_array_member) 5671 << Arg->getSourceRange(); 5672 } 5673 5674 TheCall->setType(Context.getPointerType(Context.VoidTy)); 5675 return false; 5676 } 5677 5678 /// The result of __builtin_counted_by_ref cannot be assigned to a variable. 5679 /// It allows leaking and modification of bounds safety information. 5680 bool Sema::CheckInvalidBuiltinCountedByRef(const Expr *E, 5681 BuiltinCountedByRefKind K) { 5682 const CallExpr *CE = 5683 E ? dyn_cast<CallExpr>(E->IgnoreParenImpCasts()) : nullptr; 5684 if (!CE || CE->getBuiltinCallee() != Builtin::BI__builtin_counted_by_ref) 5685 return false; 5686 5687 switch (K) { 5688 case AssignmentKind: 5689 case InitializerKind: 5690 Diag(E->getExprLoc(), 5691 diag::err_builtin_counted_by_ref_cannot_leak_reference) 5692 << 0 << E->getSourceRange(); 5693 break; 5694 case FunctionArgKind: 5695 Diag(E->getExprLoc(), 5696 diag::err_builtin_counted_by_ref_cannot_leak_reference) 5697 << 1 << E->getSourceRange(); 5698 break; 5699 case ReturnArgKind: 5700 Diag(E->getExprLoc(), 5701 diag::err_builtin_counted_by_ref_cannot_leak_reference) 5702 << 2 << E->getSourceRange(); 5703 break; 5704 case ArraySubscriptKind: 5705 Diag(E->getExprLoc(), diag::err_builtin_counted_by_ref_invalid_use) 5706 << 0 << E->getSourceRange(); 5707 break; 5708 case BinaryExprKind: 5709 Diag(E->getExprLoc(), diag::err_builtin_counted_by_ref_invalid_use) 5710 << 1 << E->getSourceRange(); 5711 break; 5712 } 5713 5714 return true; 5715 } 5716 5717 namespace { 5718 5719 class UncoveredArgHandler { 5720 enum { Unknown = -1, AllCovered = -2 }; 5721 5722 signed FirstUncoveredArg = Unknown; 5723 SmallVector<const Expr *, 4> DiagnosticExprs; 5724 5725 public: 5726 UncoveredArgHandler() = default; 5727 5728 bool hasUncoveredArg() const { 5729 return (FirstUncoveredArg >= 0); 5730 } 5731 5732 unsigned getUncoveredArg() const { 5733 assert(hasUncoveredArg() && "no uncovered argument"); 5734 return FirstUncoveredArg; 5735 } 5736 5737 void setAllCovered() { 5738 // A string has been found with all arguments covered, so clear out 5739 // the diagnostics. 5740 DiagnosticExprs.clear(); 5741 FirstUncoveredArg = AllCovered; 5742 } 5743 5744 void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) { 5745 assert(NewFirstUncoveredArg >= 0 && "Outside range"); 5746 5747 // Don't update if a previous string covers all arguments. 5748 if (FirstUncoveredArg == AllCovered) 5749 return; 5750 5751 // UncoveredArgHandler tracks the highest uncovered argument index 5752 // and with it all the strings that match this index. 5753 if (NewFirstUncoveredArg == FirstUncoveredArg) 5754 DiagnosticExprs.push_back(StrExpr); 5755 else if (NewFirstUncoveredArg > FirstUncoveredArg) { 5756 DiagnosticExprs.clear(); 5757 DiagnosticExprs.push_back(StrExpr); 5758 FirstUncoveredArg = NewFirstUncoveredArg; 5759 } 5760 } 5761 5762 void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr); 5763 }; 5764 5765 enum StringLiteralCheckType { 5766 SLCT_NotALiteral, 5767 SLCT_UncheckedLiteral, 5768 SLCT_CheckedLiteral 5769 }; 5770 5771 } // namespace 5772 5773 static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend, 5774 BinaryOperatorKind BinOpKind, 5775 bool AddendIsRight) { 5776 unsigned BitWidth = Offset.getBitWidth(); 5777 unsigned AddendBitWidth = Addend.getBitWidth(); 5778 // There might be negative interim results. 5779 if (Addend.isUnsigned()) { 5780 Addend = Addend.zext(++AddendBitWidth); 5781 Addend.setIsSigned(true); 5782 } 5783 // Adjust the bit width of the APSInts. 5784 if (AddendBitWidth > BitWidth) { 5785 Offset = Offset.sext(AddendBitWidth); 5786 BitWidth = AddendBitWidth; 5787 } else if (BitWidth > AddendBitWidth) { 5788 Addend = Addend.sext(BitWidth); 5789 } 5790 5791 bool Ov = false; 5792 llvm::APSInt ResOffset = Offset; 5793 if (BinOpKind == BO_Add) 5794 ResOffset = Offset.sadd_ov(Addend, Ov); 5795 else { 5796 assert(AddendIsRight && BinOpKind == BO_Sub && 5797 "operator must be add or sub with addend on the right"); 5798 ResOffset = Offset.ssub_ov(Addend, Ov); 5799 } 5800 5801 // We add an offset to a pointer here so we should support an offset as big as 5802 // possible. 5803 if (Ov) { 5804 assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 && 5805 "index (intermediate) result too big"); 5806 Offset = Offset.sext(2 * BitWidth); 5807 sumOffsets(Offset, Addend, BinOpKind, AddendIsRight); 5808 return; 5809 } 5810 5811 Offset = ResOffset; 5812 } 5813 5814 namespace { 5815 5816 // This is a wrapper class around StringLiteral to support offsetted string 5817 // literals as format strings. It takes the offset into account when returning 5818 // the string and its length or the source locations to display notes correctly. 5819 class FormatStringLiteral { 5820 const StringLiteral *FExpr; 5821 int64_t Offset; 5822 5823 public: 5824 FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0) 5825 : FExpr(fexpr), Offset(Offset) {} 5826 5827 StringRef getString() const { 5828 return FExpr->getString().drop_front(Offset); 5829 } 5830 5831 unsigned getByteLength() const { 5832 return FExpr->getByteLength() - getCharByteWidth() * Offset; 5833 } 5834 5835 unsigned getLength() const { return FExpr->getLength() - Offset; } 5836 unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); } 5837 5838 StringLiteralKind getKind() const { return FExpr->getKind(); } 5839 5840 QualType getType() const { return FExpr->getType(); } 5841 5842 bool isAscii() const { return FExpr->isOrdinary(); } 5843 bool isWide() const { return FExpr->isWide(); } 5844 bool isUTF8() const { return FExpr->isUTF8(); } 5845 bool isUTF16() const { return FExpr->isUTF16(); } 5846 bool isUTF32() const { return FExpr->isUTF32(); } 5847 bool isPascal() const { return FExpr->isPascal(); } 5848 5849 SourceLocation getLocationOfByte( 5850 unsigned ByteNo, const SourceManager &SM, const LangOptions &Features, 5851 const TargetInfo &Target, unsigned *StartToken = nullptr, 5852 unsigned *StartTokenByteOffset = nullptr) const { 5853 return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target, 5854 StartToken, StartTokenByteOffset); 5855 } 5856 5857 SourceLocation getBeginLoc() const LLVM_READONLY { 5858 return FExpr->getBeginLoc().getLocWithOffset(Offset); 5859 } 5860 5861 SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); } 5862 }; 5863 5864 } // namespace 5865 5866 static void CheckFormatString( 5867 Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, 5868 ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, 5869 unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, 5870 bool inFunctionCall, Sema::VariadicCallType CallType, 5871 llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, 5872 bool IgnoreStringsWithoutSpecifiers); 5873 5874 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, 5875 const Expr *E); 5876 5877 // Determine if an expression is a string literal or constant string. 5878 // If this function returns false on the arguments to a function expecting a 5879 // format string, we will usually need to emit a warning. 5880 // True string literals are then checked by CheckFormatString. 5881 static StringLiteralCheckType 5882 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, 5883 Sema::FormatArgumentPassingKind APK, unsigned format_idx, 5884 unsigned firstDataArg, Sema::FormatStringType Type, 5885 Sema::VariadicCallType CallType, bool InFunctionCall, 5886 llvm::SmallBitVector &CheckedVarArgs, 5887 UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset, 5888 bool IgnoreStringsWithoutSpecifiers = false) { 5889 if (S.isConstantEvaluatedContext()) 5890 return SLCT_NotALiteral; 5891 tryAgain: 5892 assert(Offset.isSigned() && "invalid offset"); 5893 5894 if (E->isTypeDependent() || E->isValueDependent()) 5895 return SLCT_NotALiteral; 5896 5897 E = E->IgnoreParenCasts(); 5898 5899 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) 5900 // Technically -Wformat-nonliteral does not warn about this case. 5901 // The behavior of printf and friends in this case is implementation 5902 // dependent. Ideally if the format string cannot be null then 5903 // it should have a 'nonnull' attribute in the function prototype. 5904 return SLCT_UncheckedLiteral; 5905 5906 switch (E->getStmtClass()) { 5907 case Stmt::InitListExprClass: 5908 // Handle expressions like {"foobar"}. 5909 if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) { 5910 return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, 5911 Type, CallType, /*InFunctionCall*/ false, 5912 CheckedVarArgs, UncoveredArg, Offset, 5913 IgnoreStringsWithoutSpecifiers); 5914 } 5915 return SLCT_NotALiteral; 5916 case Stmt::BinaryConditionalOperatorClass: 5917 case Stmt::ConditionalOperatorClass: { 5918 // The expression is a literal if both sub-expressions were, and it was 5919 // completely checked only if both sub-expressions were checked. 5920 const AbstractConditionalOperator *C = 5921 cast<AbstractConditionalOperator>(E); 5922 5923 // Determine whether it is necessary to check both sub-expressions, for 5924 // example, because the condition expression is a constant that can be 5925 // evaluated at compile time. 5926 bool CheckLeft = true, CheckRight = true; 5927 5928 bool Cond; 5929 if (C->getCond()->EvaluateAsBooleanCondition( 5930 Cond, S.getASTContext(), S.isConstantEvaluatedContext())) { 5931 if (Cond) 5932 CheckRight = false; 5933 else 5934 CheckLeft = false; 5935 } 5936 5937 // We need to maintain the offsets for the right and the left hand side 5938 // separately to check if every possible indexed expression is a valid 5939 // string literal. They might have different offsets for different string 5940 // literals in the end. 5941 StringLiteralCheckType Left; 5942 if (!CheckLeft) 5943 Left = SLCT_UncheckedLiteral; 5944 else { 5945 Left = checkFormatStringExpr(S, C->getTrueExpr(), Args, APK, format_idx, 5946 firstDataArg, Type, CallType, InFunctionCall, 5947 CheckedVarArgs, UncoveredArg, Offset, 5948 IgnoreStringsWithoutSpecifiers); 5949 if (Left == SLCT_NotALiteral || !CheckRight) { 5950 return Left; 5951 } 5952 } 5953 5954 StringLiteralCheckType Right = checkFormatStringExpr( 5955 S, C->getFalseExpr(), Args, APK, format_idx, firstDataArg, Type, 5956 CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 5957 IgnoreStringsWithoutSpecifiers); 5958 5959 return (CheckLeft && Left < Right) ? Left : Right; 5960 } 5961 5962 case Stmt::ImplicitCastExprClass: 5963 E = cast<ImplicitCastExpr>(E)->getSubExpr(); 5964 goto tryAgain; 5965 5966 case Stmt::OpaqueValueExprClass: 5967 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { 5968 E = src; 5969 goto tryAgain; 5970 } 5971 return SLCT_NotALiteral; 5972 5973 case Stmt::PredefinedExprClass: 5974 // While __func__, etc., are technically not string literals, they 5975 // cannot contain format specifiers and thus are not a security 5976 // liability. 5977 return SLCT_UncheckedLiteral; 5978 5979 case Stmt::DeclRefExprClass: { 5980 const DeclRefExpr *DR = cast<DeclRefExpr>(E); 5981 5982 // As an exception, do not flag errors for variables binding to 5983 // const string literals. 5984 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 5985 bool isConstant = false; 5986 QualType T = DR->getType(); 5987 5988 if (const ArrayType *AT = S.Context.getAsArrayType(T)) { 5989 isConstant = AT->getElementType().isConstant(S.Context); 5990 } else if (const PointerType *PT = T->getAs<PointerType>()) { 5991 isConstant = T.isConstant(S.Context) && 5992 PT->getPointeeType().isConstant(S.Context); 5993 } else if (T->isObjCObjectPointerType()) { 5994 // In ObjC, there is usually no "const ObjectPointer" type, 5995 // so don't check if the pointee type is constant. 5996 isConstant = T.isConstant(S.Context); 5997 } 5998 5999 if (isConstant) { 6000 if (const Expr *Init = VD->getAnyInitializer()) { 6001 // Look through initializers like const char c[] = { "foo" } 6002 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { 6003 if (InitList->isStringLiteralInit()) 6004 Init = InitList->getInit(0)->IgnoreParenImpCasts(); 6005 } 6006 return checkFormatStringExpr( 6007 S, Init, Args, APK, format_idx, firstDataArg, Type, CallType, 6008 /*InFunctionCall*/ false, CheckedVarArgs, UncoveredArg, Offset); 6009 } 6010 } 6011 6012 // When the format argument is an argument of this function, and this 6013 // function also has the format attribute, there are several interactions 6014 // for which there shouldn't be a warning. For instance, when calling 6015 // v*printf from a function that has the printf format attribute, we 6016 // should not emit a warning about using `fmt`, even though it's not 6017 // constant, because the arguments have already been checked for the 6018 // caller of `logmessage`: 6019 // 6020 // __attribute__((format(printf, 1, 2))) 6021 // void logmessage(char const *fmt, ...) { 6022 // va_list ap; 6023 // va_start(ap, fmt); 6024 // vprintf(fmt, ap); /* do not emit a warning about "fmt" */ 6025 // ... 6026 // } 6027 // 6028 // Another interaction that we need to support is calling a variadic 6029 // format function from a format function that has fixed arguments. For 6030 // instance: 6031 // 6032 // __attribute__((format(printf, 1, 2))) 6033 // void logstring(char const *fmt, char const *str) { 6034 // printf(fmt, str); /* do not emit a warning about "fmt" */ 6035 // } 6036 // 6037 // Same (and perhaps more relatably) for the variadic template case: 6038 // 6039 // template<typename... Args> 6040 // __attribute__((format(printf, 1, 2))) 6041 // void log(const char *fmt, Args&&... args) { 6042 // printf(fmt, forward<Args>(args)...); 6043 // /* do not emit a warning about "fmt" */ 6044 // } 6045 // 6046 // Due to implementation difficulty, we only check the format, not the 6047 // format arguments, in all cases. 6048 // 6049 if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) { 6050 if (const auto *D = dyn_cast<Decl>(PV->getDeclContext())) { 6051 for (const auto *PVFormat : D->specific_attrs<FormatAttr>()) { 6052 bool IsCXXMember = false; 6053 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) 6054 IsCXXMember = MD->isInstance(); 6055 6056 bool IsVariadic = false; 6057 if (const FunctionType *FnTy = D->getFunctionType()) 6058 IsVariadic = cast<FunctionProtoType>(FnTy)->isVariadic(); 6059 else if (const auto *BD = dyn_cast<BlockDecl>(D)) 6060 IsVariadic = BD->isVariadic(); 6061 else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) 6062 IsVariadic = OMD->isVariadic(); 6063 6064 Sema::FormatStringInfo CallerFSI; 6065 if (Sema::getFormatStringInfo(PVFormat, IsCXXMember, IsVariadic, 6066 &CallerFSI)) { 6067 // We also check if the formats are compatible. 6068 // We can't pass a 'scanf' string to a 'printf' function. 6069 if (PV->getFunctionScopeIndex() == CallerFSI.FormatIdx && 6070 Type == S.GetFormatStringType(PVFormat)) { 6071 // Lastly, check that argument passing kinds transition in a 6072 // way that makes sense: 6073 // from a caller with FAPK_VAList, allow FAPK_VAList 6074 // from a caller with FAPK_Fixed, allow FAPK_Fixed 6075 // from a caller with FAPK_Fixed, allow FAPK_Variadic 6076 // from a caller with FAPK_Variadic, allow FAPK_VAList 6077 switch (combineFAPK(CallerFSI.ArgPassingKind, APK)) { 6078 case combineFAPK(Sema::FAPK_VAList, Sema::FAPK_VAList): 6079 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Fixed): 6080 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Variadic): 6081 case combineFAPK(Sema::FAPK_Variadic, Sema::FAPK_VAList): 6082 return SLCT_UncheckedLiteral; 6083 } 6084 } 6085 } 6086 } 6087 } 6088 } 6089 } 6090 6091 return SLCT_NotALiteral; 6092 } 6093 6094 case Stmt::CallExprClass: 6095 case Stmt::CXXMemberCallExprClass: { 6096 const CallExpr *CE = cast<CallExpr>(E); 6097 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { 6098 bool IsFirst = true; 6099 StringLiteralCheckType CommonResult; 6100 for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) { 6101 const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex()); 6102 StringLiteralCheckType Result = checkFormatStringExpr( 6103 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6104 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6105 IgnoreStringsWithoutSpecifiers); 6106 if (IsFirst) { 6107 CommonResult = Result; 6108 IsFirst = false; 6109 } 6110 } 6111 if (!IsFirst) 6112 return CommonResult; 6113 6114 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { 6115 unsigned BuiltinID = FD->getBuiltinID(); 6116 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || 6117 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { 6118 const Expr *Arg = CE->getArg(0); 6119 return checkFormatStringExpr( 6120 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6121 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6122 IgnoreStringsWithoutSpecifiers); 6123 } 6124 } 6125 } 6126 if (const Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) 6127 return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, 6128 Type, CallType, /*InFunctionCall*/ false, 6129 CheckedVarArgs, UncoveredArg, Offset, 6130 IgnoreStringsWithoutSpecifiers); 6131 return SLCT_NotALiteral; 6132 } 6133 case Stmt::ObjCMessageExprClass: { 6134 const auto *ME = cast<ObjCMessageExpr>(E); 6135 if (const auto *MD = ME->getMethodDecl()) { 6136 if (const auto *FA = MD->getAttr<FormatArgAttr>()) { 6137 // As a special case heuristic, if we're using the method -[NSBundle 6138 // localizedStringForKey:value:table:], ignore any key strings that lack 6139 // format specifiers. The idea is that if the key doesn't have any 6140 // format specifiers then its probably just a key to map to the 6141 // localized strings. If it does have format specifiers though, then its 6142 // likely that the text of the key is the format string in the 6143 // programmer's language, and should be checked. 6144 const ObjCInterfaceDecl *IFace; 6145 if (MD->isInstanceMethod() && (IFace = MD->getClassInterface()) && 6146 IFace->getIdentifier()->isStr("NSBundle") && 6147 MD->getSelector().isKeywordSelector( 6148 {"localizedStringForKey", "value", "table"})) { 6149 IgnoreStringsWithoutSpecifiers = true; 6150 } 6151 6152 const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex()); 6153 return checkFormatStringExpr( 6154 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6155 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6156 IgnoreStringsWithoutSpecifiers); 6157 } 6158 } 6159 6160 return SLCT_NotALiteral; 6161 } 6162 case Stmt::ObjCStringLiteralClass: 6163 case Stmt::StringLiteralClass: { 6164 const StringLiteral *StrE = nullptr; 6165 6166 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) 6167 StrE = ObjCFExpr->getString(); 6168 else 6169 StrE = cast<StringLiteral>(E); 6170 6171 if (StrE) { 6172 if (Offset.isNegative() || Offset > StrE->getLength()) { 6173 // TODO: It would be better to have an explicit warning for out of 6174 // bounds literals. 6175 return SLCT_NotALiteral; 6176 } 6177 FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue()); 6178 CheckFormatString(S, &FStr, E, Args, APK, format_idx, firstDataArg, Type, 6179 InFunctionCall, CallType, CheckedVarArgs, UncoveredArg, 6180 IgnoreStringsWithoutSpecifiers); 6181 return SLCT_CheckedLiteral; 6182 } 6183 6184 return SLCT_NotALiteral; 6185 } 6186 case Stmt::BinaryOperatorClass: { 6187 const BinaryOperator *BinOp = cast<BinaryOperator>(E); 6188 6189 // A string literal + an int offset is still a string literal. 6190 if (BinOp->isAdditiveOp()) { 6191 Expr::EvalResult LResult, RResult; 6192 6193 bool LIsInt = BinOp->getLHS()->EvaluateAsInt( 6194 LResult, S.Context, Expr::SE_NoSideEffects, 6195 S.isConstantEvaluatedContext()); 6196 bool RIsInt = BinOp->getRHS()->EvaluateAsInt( 6197 RResult, S.Context, Expr::SE_NoSideEffects, 6198 S.isConstantEvaluatedContext()); 6199 6200 if (LIsInt != RIsInt) { 6201 BinaryOperatorKind BinOpKind = BinOp->getOpcode(); 6202 6203 if (LIsInt) { 6204 if (BinOpKind == BO_Add) { 6205 sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt); 6206 E = BinOp->getRHS(); 6207 goto tryAgain; 6208 } 6209 } else { 6210 sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt); 6211 E = BinOp->getLHS(); 6212 goto tryAgain; 6213 } 6214 } 6215 } 6216 6217 return SLCT_NotALiteral; 6218 } 6219 case Stmt::UnaryOperatorClass: { 6220 const UnaryOperator *UnaOp = cast<UnaryOperator>(E); 6221 auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr()); 6222 if (UnaOp->getOpcode() == UO_AddrOf && ASE) { 6223 Expr::EvalResult IndexResult; 6224 if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context, 6225 Expr::SE_NoSideEffects, 6226 S.isConstantEvaluatedContext())) { 6227 sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add, 6228 /*RHS is int*/ true); 6229 E = ASE->getBase(); 6230 goto tryAgain; 6231 } 6232 } 6233 6234 return SLCT_NotALiteral; 6235 } 6236 6237 default: 6238 return SLCT_NotALiteral; 6239 } 6240 } 6241 6242 // If this expression can be evaluated at compile-time, 6243 // check if the result is a StringLiteral and return it 6244 // otherwise return nullptr 6245 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, 6246 const Expr *E) { 6247 Expr::EvalResult Result; 6248 if (E->EvaluateAsRValue(Result, Context) && Result.Val.isLValue()) { 6249 const auto *LVE = Result.Val.getLValueBase().dyn_cast<const Expr *>(); 6250 if (isa_and_nonnull<StringLiteral>(LVE)) 6251 return LVE; 6252 } 6253 return nullptr; 6254 } 6255 6256 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { 6257 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) 6258 .Case("scanf", FST_Scanf) 6259 .Cases("printf", "printf0", "syslog", FST_Printf) 6260 .Cases("NSString", "CFString", FST_NSString) 6261 .Case("strftime", FST_Strftime) 6262 .Case("strfmon", FST_Strfmon) 6263 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) 6264 .Case("freebsd_kprintf", FST_FreeBSDKPrintf) 6265 .Case("os_trace", FST_OSLog) 6266 .Case("os_log", FST_OSLog) 6267 .Default(FST_Unknown); 6268 } 6269 6270 bool Sema::CheckFormatArguments(const FormatAttr *Format, 6271 ArrayRef<const Expr *> Args, bool IsCXXMember, 6272 VariadicCallType CallType, SourceLocation Loc, 6273 SourceRange Range, 6274 llvm::SmallBitVector &CheckedVarArgs) { 6275 FormatStringInfo FSI; 6276 if (getFormatStringInfo(Format, IsCXXMember, CallType != VariadicDoesNotApply, 6277 &FSI)) 6278 return CheckFormatArguments(Args, FSI.ArgPassingKind, FSI.FormatIdx, 6279 FSI.FirstDataArg, GetFormatStringType(Format), 6280 CallType, Loc, Range, CheckedVarArgs); 6281 return false; 6282 } 6283 6284 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, 6285 Sema::FormatArgumentPassingKind APK, 6286 unsigned format_idx, unsigned firstDataArg, 6287 FormatStringType Type, 6288 VariadicCallType CallType, SourceLocation Loc, 6289 SourceRange Range, 6290 llvm::SmallBitVector &CheckedVarArgs) { 6291 // CHECK: printf/scanf-like function is called with no format string. 6292 if (format_idx >= Args.size()) { 6293 Diag(Loc, diag::warn_missing_format_string) << Range; 6294 return false; 6295 } 6296 6297 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); 6298 6299 // CHECK: format string is not a string literal. 6300 // 6301 // Dynamically generated format strings are difficult to 6302 // automatically vet at compile time. Requiring that format strings 6303 // are string literals: (1) permits the checking of format strings by 6304 // the compiler and thereby (2) can practically remove the source of 6305 // many format string exploits. 6306 6307 // Format string can be either ObjC string (e.g. @"%d") or 6308 // C string (e.g. "%d") 6309 // ObjC string uses the same format specifiers as C string, so we can use 6310 // the same format string checking logic for both ObjC and C strings. 6311 UncoveredArgHandler UncoveredArg; 6312 StringLiteralCheckType CT = checkFormatStringExpr( 6313 *this, OrigFormatExpr, Args, APK, format_idx, firstDataArg, Type, 6314 CallType, 6315 /*IsFunctionCall*/ true, CheckedVarArgs, UncoveredArg, 6316 /*no string offset*/ llvm::APSInt(64, false) = 0); 6317 6318 // Generate a diagnostic where an uncovered argument is detected. 6319 if (UncoveredArg.hasUncoveredArg()) { 6320 unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg; 6321 assert(ArgIdx < Args.size() && "ArgIdx outside bounds"); 6322 UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]); 6323 } 6324 6325 if (CT != SLCT_NotALiteral) 6326 // Literal format string found, check done! 6327 return CT == SLCT_CheckedLiteral; 6328 6329 // Strftime is particular as it always uses a single 'time' argument, 6330 // so it is safe to pass a non-literal string. 6331 if (Type == FST_Strftime) 6332 return false; 6333 6334 // Do not emit diag when the string param is a macro expansion and the 6335 // format is either NSString or CFString. This is a hack to prevent 6336 // diag when using the NSLocalizedString and CFCopyLocalizedString macros 6337 // which are usually used in place of NS and CF string literals. 6338 SourceLocation FormatLoc = Args[format_idx]->getBeginLoc(); 6339 if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc)) 6340 return false; 6341 6342 // If there are no arguments specified, warn with -Wformat-security, otherwise 6343 // warn only with -Wformat-nonliteral. 6344 if (Args.size() == firstDataArg) { 6345 Diag(FormatLoc, diag::warn_format_nonliteral_noargs) 6346 << OrigFormatExpr->getSourceRange(); 6347 switch (Type) { 6348 default: 6349 break; 6350 case FST_Kprintf: 6351 case FST_FreeBSDKPrintf: 6352 case FST_Printf: 6353 case FST_Syslog: 6354 Diag(FormatLoc, diag::note_format_security_fixit) 6355 << FixItHint::CreateInsertion(FormatLoc, "\"%s\", "); 6356 break; 6357 case FST_NSString: 6358 Diag(FormatLoc, diag::note_format_security_fixit) 6359 << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", "); 6360 break; 6361 } 6362 } else { 6363 Diag(FormatLoc, diag::warn_format_nonliteral) 6364 << OrigFormatExpr->getSourceRange(); 6365 } 6366 return false; 6367 } 6368 6369 namespace { 6370 6371 class CheckFormatHandler : public analyze_format_string::FormatStringHandler { 6372 protected: 6373 Sema &S; 6374 const FormatStringLiteral *FExpr; 6375 const Expr *OrigFormatExpr; 6376 const Sema::FormatStringType FSType; 6377 const unsigned FirstDataArg; 6378 const unsigned NumDataArgs; 6379 const char *Beg; // Start of format string. 6380 const Sema::FormatArgumentPassingKind ArgPassingKind; 6381 ArrayRef<const Expr *> Args; 6382 unsigned FormatIdx; 6383 llvm::SmallBitVector CoveredArgs; 6384 bool usesPositionalArgs = false; 6385 bool atFirstArg = true; 6386 bool inFunctionCall; 6387 Sema::VariadicCallType CallType; 6388 llvm::SmallBitVector &CheckedVarArgs; 6389 UncoveredArgHandler &UncoveredArg; 6390 6391 public: 6392 CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr, 6393 const Expr *origFormatExpr, 6394 const Sema::FormatStringType type, unsigned firstDataArg, 6395 unsigned numDataArgs, const char *beg, 6396 Sema::FormatArgumentPassingKind APK, 6397 ArrayRef<const Expr *> Args, unsigned formatIdx, 6398 bool inFunctionCall, Sema::VariadicCallType callType, 6399 llvm::SmallBitVector &CheckedVarArgs, 6400 UncoveredArgHandler &UncoveredArg) 6401 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type), 6402 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg), 6403 ArgPassingKind(APK), Args(Args), FormatIdx(formatIdx), 6404 inFunctionCall(inFunctionCall), CallType(callType), 6405 CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) { 6406 CoveredArgs.resize(numDataArgs); 6407 CoveredArgs.reset(); 6408 } 6409 6410 void DoneProcessing(); 6411 6412 void HandleIncompleteSpecifier(const char *startSpecifier, 6413 unsigned specifierLen) override; 6414 6415 void HandleInvalidLengthModifier( 6416 const analyze_format_string::FormatSpecifier &FS, 6417 const analyze_format_string::ConversionSpecifier &CS, 6418 const char *startSpecifier, unsigned specifierLen, 6419 unsigned DiagID); 6420 6421 void HandleNonStandardLengthModifier( 6422 const analyze_format_string::FormatSpecifier &FS, 6423 const char *startSpecifier, unsigned specifierLen); 6424 6425 void HandleNonStandardConversionSpecifier( 6426 const analyze_format_string::ConversionSpecifier &CS, 6427 const char *startSpecifier, unsigned specifierLen); 6428 6429 void HandlePosition(const char *startPos, unsigned posLen) override; 6430 6431 void HandleInvalidPosition(const char *startSpecifier, 6432 unsigned specifierLen, 6433 analyze_format_string::PositionContext p) override; 6434 6435 void HandleZeroPosition(const char *startPos, unsigned posLen) override; 6436 6437 void HandleNullChar(const char *nullCharacter) override; 6438 6439 template <typename Range> 6440 static void 6441 EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr, 6442 const PartialDiagnostic &PDiag, SourceLocation StringLoc, 6443 bool IsStringLocation, Range StringRange, 6444 ArrayRef<FixItHint> Fixit = {}); 6445 6446 protected: 6447 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, 6448 const char *startSpec, 6449 unsigned specifierLen, 6450 const char *csStart, unsigned csLen); 6451 6452 void HandlePositionalNonpositionalArgs(SourceLocation Loc, 6453 const char *startSpec, 6454 unsigned specifierLen); 6455 6456 SourceRange getFormatStringRange(); 6457 CharSourceRange getSpecifierRange(const char *startSpecifier, 6458 unsigned specifierLen); 6459 SourceLocation getLocationOfByte(const char *x); 6460 6461 const Expr *getDataArg(unsigned i) const; 6462 6463 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, 6464 const analyze_format_string::ConversionSpecifier &CS, 6465 const char *startSpecifier, unsigned specifierLen, 6466 unsigned argIndex); 6467 6468 template <typename Range> 6469 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, 6470 bool IsStringLocation, Range StringRange, 6471 ArrayRef<FixItHint> Fixit = {}); 6472 }; 6473 6474 } // namespace 6475 6476 SourceRange CheckFormatHandler::getFormatStringRange() { 6477 return OrigFormatExpr->getSourceRange(); 6478 } 6479 6480 CharSourceRange CheckFormatHandler:: 6481 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { 6482 SourceLocation Start = getLocationOfByte(startSpecifier); 6483 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); 6484 6485 // Advance the end SourceLocation by one due to half-open ranges. 6486 End = End.getLocWithOffset(1); 6487 6488 return CharSourceRange::getCharRange(Start, End); 6489 } 6490 6491 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { 6492 return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(), 6493 S.getLangOpts(), S.Context.getTargetInfo()); 6494 } 6495 6496 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, 6497 unsigned specifierLen){ 6498 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), 6499 getLocationOfByte(startSpecifier), 6500 /*IsStringLocation*/true, 6501 getSpecifierRange(startSpecifier, specifierLen)); 6502 } 6503 6504 void CheckFormatHandler::HandleInvalidLengthModifier( 6505 const analyze_format_string::FormatSpecifier &FS, 6506 const analyze_format_string::ConversionSpecifier &CS, 6507 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { 6508 using namespace analyze_format_string; 6509 6510 const LengthModifier &LM = FS.getLengthModifier(); 6511 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6512 6513 // See if we know how to fix this length modifier. 6514 std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6515 if (FixedLM) { 6516 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6517 getLocationOfByte(LM.getStart()), 6518 /*IsStringLocation*/true, 6519 getSpecifierRange(startSpecifier, specifierLen)); 6520 6521 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6522 << FixedLM->toString() 6523 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6524 6525 } else { 6526 FixItHint Hint; 6527 if (DiagID == diag::warn_format_nonsensical_length) 6528 Hint = FixItHint::CreateRemoval(LMRange); 6529 6530 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6531 getLocationOfByte(LM.getStart()), 6532 /*IsStringLocation*/true, 6533 getSpecifierRange(startSpecifier, specifierLen), 6534 Hint); 6535 } 6536 } 6537 6538 void CheckFormatHandler::HandleNonStandardLengthModifier( 6539 const analyze_format_string::FormatSpecifier &FS, 6540 const char *startSpecifier, unsigned specifierLen) { 6541 using namespace analyze_format_string; 6542 6543 const LengthModifier &LM = FS.getLengthModifier(); 6544 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6545 6546 // See if we know how to fix this length modifier. 6547 std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6548 if (FixedLM) { 6549 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6550 << LM.toString() << 0, 6551 getLocationOfByte(LM.getStart()), 6552 /*IsStringLocation*/true, 6553 getSpecifierRange(startSpecifier, specifierLen)); 6554 6555 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6556 << FixedLM->toString() 6557 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6558 6559 } else { 6560 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6561 << LM.toString() << 0, 6562 getLocationOfByte(LM.getStart()), 6563 /*IsStringLocation*/true, 6564 getSpecifierRange(startSpecifier, specifierLen)); 6565 } 6566 } 6567 6568 void CheckFormatHandler::HandleNonStandardConversionSpecifier( 6569 const analyze_format_string::ConversionSpecifier &CS, 6570 const char *startSpecifier, unsigned specifierLen) { 6571 using namespace analyze_format_string; 6572 6573 // See if we know how to fix this conversion specifier. 6574 std::optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); 6575 if (FixedCS) { 6576 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6577 << CS.toString() << /*conversion specifier*/1, 6578 getLocationOfByte(CS.getStart()), 6579 /*IsStringLocation*/true, 6580 getSpecifierRange(startSpecifier, specifierLen)); 6581 6582 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); 6583 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) 6584 << FixedCS->toString() 6585 << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); 6586 } else { 6587 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6588 << CS.toString() << /*conversion specifier*/1, 6589 getLocationOfByte(CS.getStart()), 6590 /*IsStringLocation*/true, 6591 getSpecifierRange(startSpecifier, specifierLen)); 6592 } 6593 } 6594 6595 void CheckFormatHandler::HandlePosition(const char *startPos, 6596 unsigned posLen) { 6597 if (!S.getDiagnostics().isIgnored( 6598 diag::warn_format_non_standard_positional_arg, SourceLocation())) 6599 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), 6600 getLocationOfByte(startPos), 6601 /*IsStringLocation*/ true, 6602 getSpecifierRange(startPos, posLen)); 6603 } 6604 6605 void CheckFormatHandler::HandleInvalidPosition( 6606 const char *startSpecifier, unsigned specifierLen, 6607 analyze_format_string::PositionContext p) { 6608 if (!S.getDiagnostics().isIgnored( 6609 diag::warn_format_invalid_positional_specifier, SourceLocation())) 6610 EmitFormatDiagnostic( 6611 S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p, 6612 getLocationOfByte(startSpecifier), /*IsStringLocation*/ true, 6613 getSpecifierRange(startSpecifier, specifierLen)); 6614 } 6615 6616 void CheckFormatHandler::HandleZeroPosition(const char *startPos, 6617 unsigned posLen) { 6618 if (!S.getDiagnostics().isIgnored(diag::warn_format_zero_positional_specifier, 6619 SourceLocation())) 6620 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), 6621 getLocationOfByte(startPos), 6622 /*IsStringLocation*/ true, 6623 getSpecifierRange(startPos, posLen)); 6624 } 6625 6626 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { 6627 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { 6628 // The presence of a null character is likely an error. 6629 EmitFormatDiagnostic( 6630 S.PDiag(diag::warn_printf_format_string_contains_null_char), 6631 getLocationOfByte(nullCharacter), /*IsStringLocation*/true, 6632 getFormatStringRange()); 6633 } 6634 } 6635 6636 // Note that this may return NULL if there was an error parsing or building 6637 // one of the argument expressions. 6638 const Expr *CheckFormatHandler::getDataArg(unsigned i) const { 6639 return Args[FirstDataArg + i]; 6640 } 6641 6642 void CheckFormatHandler::DoneProcessing() { 6643 // Does the number of data arguments exceed the number of 6644 // format conversions in the format string? 6645 if (ArgPassingKind != Sema::FAPK_VAList) { 6646 // Find any arguments that weren't covered. 6647 CoveredArgs.flip(); 6648 signed notCoveredArg = CoveredArgs.find_first(); 6649 if (notCoveredArg >= 0) { 6650 assert((unsigned)notCoveredArg < NumDataArgs); 6651 UncoveredArg.Update(notCoveredArg, OrigFormatExpr); 6652 } else { 6653 UncoveredArg.setAllCovered(); 6654 } 6655 } 6656 } 6657 6658 void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall, 6659 const Expr *ArgExpr) { 6660 assert(hasUncoveredArg() && !DiagnosticExprs.empty() && 6661 "Invalid state"); 6662 6663 if (!ArgExpr) 6664 return; 6665 6666 SourceLocation Loc = ArgExpr->getBeginLoc(); 6667 6668 if (S.getSourceManager().isInSystemMacro(Loc)) 6669 return; 6670 6671 PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used); 6672 for (auto E : DiagnosticExprs) 6673 PDiag << E->getSourceRange(); 6674 6675 CheckFormatHandler::EmitFormatDiagnostic( 6676 S, IsFunctionCall, DiagnosticExprs[0], 6677 PDiag, Loc, /*IsStringLocation*/false, 6678 DiagnosticExprs[0]->getSourceRange()); 6679 } 6680 6681 bool 6682 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, 6683 SourceLocation Loc, 6684 const char *startSpec, 6685 unsigned specifierLen, 6686 const char *csStart, 6687 unsigned csLen) { 6688 bool keepGoing = true; 6689 if (argIndex < NumDataArgs) { 6690 // Consider the argument coverered, even though the specifier doesn't 6691 // make sense. 6692 CoveredArgs.set(argIndex); 6693 } 6694 else { 6695 // If argIndex exceeds the number of data arguments we 6696 // don't issue a warning because that is just a cascade of warnings (and 6697 // they may have intended '%%' anyway). We don't want to continue processing 6698 // the format string after this point, however, as we will like just get 6699 // gibberish when trying to match arguments. 6700 keepGoing = false; 6701 } 6702 6703 StringRef Specifier(csStart, csLen); 6704 6705 // If the specifier in non-printable, it could be the first byte of a UTF-8 6706 // sequence. In that case, print the UTF-8 code point. If not, print the byte 6707 // hex value. 6708 std::string CodePointStr; 6709 if (!llvm::sys::locale::isPrint(*csStart)) { 6710 llvm::UTF32 CodePoint; 6711 const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart); 6712 const llvm::UTF8 *E = 6713 reinterpret_cast<const llvm::UTF8 *>(csStart + csLen); 6714 llvm::ConversionResult Result = 6715 llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion); 6716 6717 if (Result != llvm::conversionOK) { 6718 unsigned char FirstChar = *csStart; 6719 CodePoint = (llvm::UTF32)FirstChar; 6720 } 6721 6722 llvm::raw_string_ostream OS(CodePointStr); 6723 if (CodePoint < 256) 6724 OS << "\\x" << llvm::format("%02x", CodePoint); 6725 else if (CodePoint <= 0xFFFF) 6726 OS << "\\u" << llvm::format("%04x", CodePoint); 6727 else 6728 OS << "\\U" << llvm::format("%08x", CodePoint); 6729 Specifier = CodePointStr; 6730 } 6731 6732 EmitFormatDiagnostic( 6733 S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc, 6734 /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen)); 6735 6736 return keepGoing; 6737 } 6738 6739 void 6740 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, 6741 const char *startSpec, 6742 unsigned specifierLen) { 6743 EmitFormatDiagnostic( 6744 S.PDiag(diag::warn_format_mix_positional_nonpositional_args), 6745 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); 6746 } 6747 6748 bool 6749 CheckFormatHandler::CheckNumArgs( 6750 const analyze_format_string::FormatSpecifier &FS, 6751 const analyze_format_string::ConversionSpecifier &CS, 6752 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { 6753 6754 if (argIndex >= NumDataArgs) { 6755 PartialDiagnostic PDiag = FS.usesPositionalArg() 6756 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) 6757 << (argIndex+1) << NumDataArgs) 6758 : S.PDiag(diag::warn_printf_insufficient_data_args); 6759 EmitFormatDiagnostic( 6760 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, 6761 getSpecifierRange(startSpecifier, specifierLen)); 6762 6763 // Since more arguments than conversion tokens are given, by extension 6764 // all arguments are covered, so mark this as so. 6765 UncoveredArg.setAllCovered(); 6766 return false; 6767 } 6768 return true; 6769 } 6770 6771 template<typename Range> 6772 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, 6773 SourceLocation Loc, 6774 bool IsStringLocation, 6775 Range StringRange, 6776 ArrayRef<FixItHint> FixIt) { 6777 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, 6778 Loc, IsStringLocation, StringRange, FixIt); 6779 } 6780 6781 /// If the format string is not within the function call, emit a note 6782 /// so that the function call and string are in diagnostic messages. 6783 /// 6784 /// \param InFunctionCall if true, the format string is within the function 6785 /// call and only one diagnostic message will be produced. Otherwise, an 6786 /// extra note will be emitted pointing to location of the format string. 6787 /// 6788 /// \param ArgumentExpr the expression that is passed as the format string 6789 /// argument in the function call. Used for getting locations when two 6790 /// diagnostics are emitted. 6791 /// 6792 /// \param PDiag the callee should already have provided any strings for the 6793 /// diagnostic message. This function only adds locations and fixits 6794 /// to diagnostics. 6795 /// 6796 /// \param Loc primary location for diagnostic. If two diagnostics are 6797 /// required, one will be at Loc and a new SourceLocation will be created for 6798 /// the other one. 6799 /// 6800 /// \param IsStringLocation if true, Loc points to the format string should be 6801 /// used for the note. Otherwise, Loc points to the argument list and will 6802 /// be used with PDiag. 6803 /// 6804 /// \param StringRange some or all of the string to highlight. This is 6805 /// templated so it can accept either a CharSourceRange or a SourceRange. 6806 /// 6807 /// \param FixIt optional fix it hint for the format string. 6808 template <typename Range> 6809 void CheckFormatHandler::EmitFormatDiagnostic( 6810 Sema &S, bool InFunctionCall, const Expr *ArgumentExpr, 6811 const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation, 6812 Range StringRange, ArrayRef<FixItHint> FixIt) { 6813 if (InFunctionCall) { 6814 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); 6815 D << StringRange; 6816 D << FixIt; 6817 } else { 6818 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) 6819 << ArgumentExpr->getSourceRange(); 6820 6821 const Sema::SemaDiagnosticBuilder &Note = 6822 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), 6823 diag::note_format_string_defined); 6824 6825 Note << StringRange; 6826 Note << FixIt; 6827 } 6828 } 6829 6830 //===--- CHECK: Printf format string checking -----------------------------===// 6831 6832 namespace { 6833 6834 class CheckPrintfHandler : public CheckFormatHandler { 6835 public: 6836 CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr, 6837 const Expr *origFormatExpr, 6838 const Sema::FormatStringType type, unsigned firstDataArg, 6839 unsigned numDataArgs, bool isObjC, const char *beg, 6840 Sema::FormatArgumentPassingKind APK, 6841 ArrayRef<const Expr *> Args, unsigned formatIdx, 6842 bool inFunctionCall, Sema::VariadicCallType CallType, 6843 llvm::SmallBitVector &CheckedVarArgs, 6844 UncoveredArgHandler &UncoveredArg) 6845 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 6846 numDataArgs, beg, APK, Args, formatIdx, 6847 inFunctionCall, CallType, CheckedVarArgs, 6848 UncoveredArg) {} 6849 6850 bool isObjCContext() const { return FSType == Sema::FST_NSString; } 6851 6852 /// Returns true if '%@' specifiers are allowed in the format string. 6853 bool allowsObjCArg() const { 6854 return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog || 6855 FSType == Sema::FST_OSTrace; 6856 } 6857 6858 bool HandleInvalidPrintfConversionSpecifier( 6859 const analyze_printf::PrintfSpecifier &FS, 6860 const char *startSpecifier, 6861 unsigned specifierLen) override; 6862 6863 void handleInvalidMaskType(StringRef MaskType) override; 6864 6865 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 6866 const char *startSpecifier, unsigned specifierLen, 6867 const TargetInfo &Target) override; 6868 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 6869 const char *StartSpecifier, 6870 unsigned SpecifierLen, 6871 const Expr *E); 6872 6873 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, 6874 const char *startSpecifier, unsigned specifierLen); 6875 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, 6876 const analyze_printf::OptionalAmount &Amt, 6877 unsigned type, 6878 const char *startSpecifier, unsigned specifierLen); 6879 void HandleFlag(const analyze_printf::PrintfSpecifier &FS, 6880 const analyze_printf::OptionalFlag &flag, 6881 const char *startSpecifier, unsigned specifierLen); 6882 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, 6883 const analyze_printf::OptionalFlag &ignoredFlag, 6884 const analyze_printf::OptionalFlag &flag, 6885 const char *startSpecifier, unsigned specifierLen); 6886 bool checkForCStrMembers(const analyze_printf::ArgType &AT, 6887 const Expr *E); 6888 6889 void HandleEmptyObjCModifierFlag(const char *startFlag, 6890 unsigned flagLen) override; 6891 6892 void HandleInvalidObjCModifierFlag(const char *startFlag, 6893 unsigned flagLen) override; 6894 6895 void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, 6896 const char *flagsEnd, 6897 const char *conversionPosition) 6898 override; 6899 }; 6900 6901 } // namespace 6902 6903 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( 6904 const analyze_printf::PrintfSpecifier &FS, 6905 const char *startSpecifier, 6906 unsigned specifierLen) { 6907 const analyze_printf::PrintfConversionSpecifier &CS = 6908 FS.getConversionSpecifier(); 6909 6910 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 6911 getLocationOfByte(CS.getStart()), 6912 startSpecifier, specifierLen, 6913 CS.getStart(), CS.getLength()); 6914 } 6915 6916 void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) { 6917 S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size); 6918 } 6919 6920 bool CheckPrintfHandler::HandleAmount( 6921 const analyze_format_string::OptionalAmount &Amt, unsigned k, 6922 const char *startSpecifier, unsigned specifierLen) { 6923 if (Amt.hasDataArgument()) { 6924 if (ArgPassingKind != Sema::FAPK_VAList) { 6925 unsigned argIndex = Amt.getArgIndex(); 6926 if (argIndex >= NumDataArgs) { 6927 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) 6928 << k, 6929 getLocationOfByte(Amt.getStart()), 6930 /*IsStringLocation*/ true, 6931 getSpecifierRange(startSpecifier, specifierLen)); 6932 // Don't do any more checking. We will just emit 6933 // spurious errors. 6934 return false; 6935 } 6936 6937 // Type check the data argument. It should be an 'int'. 6938 // Although not in conformance with C99, we also allow the argument to be 6939 // an 'unsigned int' as that is a reasonably safe case. GCC also 6940 // doesn't emit a warning for that case. 6941 CoveredArgs.set(argIndex); 6942 const Expr *Arg = getDataArg(argIndex); 6943 if (!Arg) 6944 return false; 6945 6946 QualType T = Arg->getType(); 6947 6948 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); 6949 assert(AT.isValid()); 6950 6951 if (!AT.matchesType(S.Context, T)) { 6952 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) 6953 << k << AT.getRepresentativeTypeName(S.Context) 6954 << T << Arg->getSourceRange(), 6955 getLocationOfByte(Amt.getStart()), 6956 /*IsStringLocation*/true, 6957 getSpecifierRange(startSpecifier, specifierLen)); 6958 // Don't do any more checking. We will just emit 6959 // spurious errors. 6960 return false; 6961 } 6962 } 6963 } 6964 return true; 6965 } 6966 6967 void CheckPrintfHandler::HandleInvalidAmount( 6968 const analyze_printf::PrintfSpecifier &FS, 6969 const analyze_printf::OptionalAmount &Amt, 6970 unsigned type, 6971 const char *startSpecifier, 6972 unsigned specifierLen) { 6973 const analyze_printf::PrintfConversionSpecifier &CS = 6974 FS.getConversionSpecifier(); 6975 6976 FixItHint fixit = 6977 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant 6978 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), 6979 Amt.getConstantLength())) 6980 : FixItHint(); 6981 6982 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) 6983 << type << CS.toString(), 6984 getLocationOfByte(Amt.getStart()), 6985 /*IsStringLocation*/true, 6986 getSpecifierRange(startSpecifier, specifierLen), 6987 fixit); 6988 } 6989 6990 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, 6991 const analyze_printf::OptionalFlag &flag, 6992 const char *startSpecifier, 6993 unsigned specifierLen) { 6994 // Warn about pointless flag with a fixit removal. 6995 const analyze_printf::PrintfConversionSpecifier &CS = 6996 FS.getConversionSpecifier(); 6997 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) 6998 << flag.toString() << CS.toString(), 6999 getLocationOfByte(flag.getPosition()), 7000 /*IsStringLocation*/true, 7001 getSpecifierRange(startSpecifier, specifierLen), 7002 FixItHint::CreateRemoval( 7003 getSpecifierRange(flag.getPosition(), 1))); 7004 } 7005 7006 void CheckPrintfHandler::HandleIgnoredFlag( 7007 const analyze_printf::PrintfSpecifier &FS, 7008 const analyze_printf::OptionalFlag &ignoredFlag, 7009 const analyze_printf::OptionalFlag &flag, 7010 const char *startSpecifier, 7011 unsigned specifierLen) { 7012 // Warn about ignored flag with a fixit removal. 7013 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) 7014 << ignoredFlag.toString() << flag.toString(), 7015 getLocationOfByte(ignoredFlag.getPosition()), 7016 /*IsStringLocation*/true, 7017 getSpecifierRange(startSpecifier, specifierLen), 7018 FixItHint::CreateRemoval( 7019 getSpecifierRange(ignoredFlag.getPosition(), 1))); 7020 } 7021 7022 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag, 7023 unsigned flagLen) { 7024 // Warn about an empty flag. 7025 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag), 7026 getLocationOfByte(startFlag), 7027 /*IsStringLocation*/true, 7028 getSpecifierRange(startFlag, flagLen)); 7029 } 7030 7031 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag, 7032 unsigned flagLen) { 7033 // Warn about an invalid flag. 7034 auto Range = getSpecifierRange(startFlag, flagLen); 7035 StringRef flag(startFlag, flagLen); 7036 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag, 7037 getLocationOfByte(startFlag), 7038 /*IsStringLocation*/true, 7039 Range, FixItHint::CreateRemoval(Range)); 7040 } 7041 7042 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion( 7043 const char *flagsStart, const char *flagsEnd, const char *conversionPosition) { 7044 // Warn about using '[...]' without a '@' conversion. 7045 auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1); 7046 auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion; 7047 EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1), 7048 getLocationOfByte(conversionPosition), 7049 /*IsStringLocation*/true, 7050 Range, FixItHint::CreateRemoval(Range)); 7051 } 7052 7053 // Determines if the specified is a C++ class or struct containing 7054 // a member with the specified name and kind (e.g. a CXXMethodDecl named 7055 // "c_str()"). 7056 template<typename MemberKind> 7057 static llvm::SmallPtrSet<MemberKind*, 1> 7058 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { 7059 const RecordType *RT = Ty->getAs<RecordType>(); 7060 llvm::SmallPtrSet<MemberKind*, 1> Results; 7061 7062 if (!RT) 7063 return Results; 7064 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 7065 if (!RD || !RD->getDefinition()) 7066 return Results; 7067 7068 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), 7069 Sema::LookupMemberName); 7070 R.suppressDiagnostics(); 7071 7072 // We just need to include all members of the right kind turned up by the 7073 // filter, at this point. 7074 if (S.LookupQualifiedName(R, RT->getDecl())) 7075 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 7076 NamedDecl *decl = (*I)->getUnderlyingDecl(); 7077 if (MemberKind *FK = dyn_cast<MemberKind>(decl)) 7078 Results.insert(FK); 7079 } 7080 return Results; 7081 } 7082 7083 /// Check if we could call '.c_str()' on an object. 7084 /// 7085 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't 7086 /// allow the call, or if it would be ambiguous). 7087 bool Sema::hasCStrMethod(const Expr *E) { 7088 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 7089 7090 MethodSet Results = 7091 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); 7092 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 7093 MI != ME; ++MI) 7094 if ((*MI)->getMinRequiredArguments() == 0) 7095 return true; 7096 return false; 7097 } 7098 7099 // Check if a (w)string was passed when a (w)char* was needed, and offer a 7100 // better diagnostic if so. AT is assumed to be valid. 7101 // Returns true when a c_str() conversion method is found. 7102 bool CheckPrintfHandler::checkForCStrMembers( 7103 const analyze_printf::ArgType &AT, const Expr *E) { 7104 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 7105 7106 MethodSet Results = 7107 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); 7108 7109 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 7110 MI != ME; ++MI) { 7111 const CXXMethodDecl *Method = *MI; 7112 if (Method->getMinRequiredArguments() == 0 && 7113 AT.matchesType(S.Context, Method->getReturnType())) { 7114 // FIXME: Suggest parens if the expression needs them. 7115 SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc()); 7116 S.Diag(E->getBeginLoc(), diag::note_printf_c_str) 7117 << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()"); 7118 return true; 7119 } 7120 } 7121 7122 return false; 7123 } 7124 7125 bool CheckPrintfHandler::HandlePrintfSpecifier( 7126 const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier, 7127 unsigned specifierLen, const TargetInfo &Target) { 7128 using namespace analyze_format_string; 7129 using namespace analyze_printf; 7130 7131 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); 7132 7133 if (FS.consumesDataArgument()) { 7134 if (atFirstArg) { 7135 atFirstArg = false; 7136 usesPositionalArgs = FS.usesPositionalArg(); 7137 } 7138 else if (usesPositionalArgs != FS.usesPositionalArg()) { 7139 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 7140 startSpecifier, specifierLen); 7141 return false; 7142 } 7143 } 7144 7145 // First check if the field width, precision, and conversion specifier 7146 // have matching data arguments. 7147 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, 7148 startSpecifier, specifierLen)) { 7149 return false; 7150 } 7151 7152 if (!HandleAmount(FS.getPrecision(), /* precision */ 1, 7153 startSpecifier, specifierLen)) { 7154 return false; 7155 } 7156 7157 if (!CS.consumesDataArgument()) { 7158 // FIXME: Technically specifying a precision or field width here 7159 // makes no sense. Worth issuing a warning at some point. 7160 return true; 7161 } 7162 7163 // Consume the argument. 7164 unsigned argIndex = FS.getArgIndex(); 7165 if (argIndex < NumDataArgs) { 7166 // The check to see if the argIndex is valid will come later. 7167 // We set the bit here because we may exit early from this 7168 // function if we encounter some other error. 7169 CoveredArgs.set(argIndex); 7170 } 7171 7172 // FreeBSD kernel extensions. 7173 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || 7174 CS.getKind() == ConversionSpecifier::FreeBSDDArg) { 7175 // We need at least two arguments. 7176 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) 7177 return false; 7178 7179 // Claim the second argument. 7180 CoveredArgs.set(argIndex + 1); 7181 7182 // Type check the first argument (int for %b, pointer for %D) 7183 const Expr *Ex = getDataArg(argIndex); 7184 const analyze_printf::ArgType &AT = 7185 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? 7186 ArgType(S.Context.IntTy) : ArgType::CPointerTy; 7187 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) 7188 EmitFormatDiagnostic( 7189 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7190 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() 7191 << false << Ex->getSourceRange(), 7192 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7193 getSpecifierRange(startSpecifier, specifierLen)); 7194 7195 // Type check the second argument (char * for both %b and %D) 7196 Ex = getDataArg(argIndex + 1); 7197 const analyze_printf::ArgType &AT2 = ArgType::CStrTy; 7198 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) 7199 EmitFormatDiagnostic( 7200 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7201 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() 7202 << false << Ex->getSourceRange(), 7203 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7204 getSpecifierRange(startSpecifier, specifierLen)); 7205 7206 return true; 7207 } 7208 7209 // Check for using an Objective-C specific conversion specifier 7210 // in a non-ObjC literal. 7211 if (!allowsObjCArg() && CS.isObjCArg()) { 7212 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7213 specifierLen); 7214 } 7215 7216 // %P can only be used with os_log. 7217 if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) { 7218 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7219 specifierLen); 7220 } 7221 7222 // %n is not allowed with os_log. 7223 if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) { 7224 EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg), 7225 getLocationOfByte(CS.getStart()), 7226 /*IsStringLocation*/ false, 7227 getSpecifierRange(startSpecifier, specifierLen)); 7228 7229 return true; 7230 } 7231 7232 // Only scalars are allowed for os_trace. 7233 if (FSType == Sema::FST_OSTrace && 7234 (CS.getKind() == ConversionSpecifier::PArg || 7235 CS.getKind() == ConversionSpecifier::sArg || 7236 CS.getKind() == ConversionSpecifier::ObjCObjArg)) { 7237 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7238 specifierLen); 7239 } 7240 7241 // Check for use of public/private annotation outside of os_log(). 7242 if (FSType != Sema::FST_OSLog) { 7243 if (FS.isPublic().isSet()) { 7244 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7245 << "public", 7246 getLocationOfByte(FS.isPublic().getPosition()), 7247 /*IsStringLocation*/ false, 7248 getSpecifierRange(startSpecifier, specifierLen)); 7249 } 7250 if (FS.isPrivate().isSet()) { 7251 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7252 << "private", 7253 getLocationOfByte(FS.isPrivate().getPosition()), 7254 /*IsStringLocation*/ false, 7255 getSpecifierRange(startSpecifier, specifierLen)); 7256 } 7257 } 7258 7259 const llvm::Triple &Triple = Target.getTriple(); 7260 if (CS.getKind() == ConversionSpecifier::nArg && 7261 (Triple.isAndroid() || Triple.isOSFuchsia())) { 7262 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_narg_not_supported), 7263 getLocationOfByte(CS.getStart()), 7264 /*IsStringLocation*/ false, 7265 getSpecifierRange(startSpecifier, specifierLen)); 7266 } 7267 7268 // Check for invalid use of field width 7269 if (!FS.hasValidFieldWidth()) { 7270 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, 7271 startSpecifier, specifierLen); 7272 } 7273 7274 // Check for invalid use of precision 7275 if (!FS.hasValidPrecision()) { 7276 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, 7277 startSpecifier, specifierLen); 7278 } 7279 7280 // Precision is mandatory for %P specifier. 7281 if (CS.getKind() == ConversionSpecifier::PArg && 7282 FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) { 7283 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision), 7284 getLocationOfByte(startSpecifier), 7285 /*IsStringLocation*/ false, 7286 getSpecifierRange(startSpecifier, specifierLen)); 7287 } 7288 7289 // Check each flag does not conflict with any other component. 7290 if (!FS.hasValidThousandsGroupingPrefix()) 7291 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); 7292 if (!FS.hasValidLeadingZeros()) 7293 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); 7294 if (!FS.hasValidPlusPrefix()) 7295 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); 7296 if (!FS.hasValidSpacePrefix()) 7297 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); 7298 if (!FS.hasValidAlternativeForm()) 7299 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); 7300 if (!FS.hasValidLeftJustified()) 7301 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); 7302 7303 // Check that flags are not ignored by another flag 7304 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' 7305 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), 7306 startSpecifier, specifierLen); 7307 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' 7308 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), 7309 startSpecifier, specifierLen); 7310 7311 // Check the length modifier is valid with the given conversion specifier. 7312 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7313 S.getLangOpts())) 7314 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7315 diag::warn_format_nonsensical_length); 7316 else if (!FS.hasStandardLengthModifier()) 7317 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7318 else if (!FS.hasStandardLengthConversionCombination()) 7319 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7320 diag::warn_format_non_standard_conversion_spec); 7321 7322 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7323 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7324 7325 // The remaining checks depend on the data arguments. 7326 if (ArgPassingKind == Sema::FAPK_VAList) 7327 return true; 7328 7329 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7330 return false; 7331 7332 const Expr *Arg = getDataArg(argIndex); 7333 if (!Arg) 7334 return true; 7335 7336 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); 7337 } 7338 7339 static bool requiresParensToAddCast(const Expr *E) { 7340 // FIXME: We should have a general way to reason about operator 7341 // precedence and whether parens are actually needed here. 7342 // Take care of a few common cases where they aren't. 7343 const Expr *Inside = E->IgnoreImpCasts(); 7344 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) 7345 Inside = POE->getSyntacticForm()->IgnoreImpCasts(); 7346 7347 switch (Inside->getStmtClass()) { 7348 case Stmt::ArraySubscriptExprClass: 7349 case Stmt::CallExprClass: 7350 case Stmt::CharacterLiteralClass: 7351 case Stmt::CXXBoolLiteralExprClass: 7352 case Stmt::DeclRefExprClass: 7353 case Stmt::FloatingLiteralClass: 7354 case Stmt::IntegerLiteralClass: 7355 case Stmt::MemberExprClass: 7356 case Stmt::ObjCArrayLiteralClass: 7357 case Stmt::ObjCBoolLiteralExprClass: 7358 case Stmt::ObjCBoxedExprClass: 7359 case Stmt::ObjCDictionaryLiteralClass: 7360 case Stmt::ObjCEncodeExprClass: 7361 case Stmt::ObjCIvarRefExprClass: 7362 case Stmt::ObjCMessageExprClass: 7363 case Stmt::ObjCPropertyRefExprClass: 7364 case Stmt::ObjCStringLiteralClass: 7365 case Stmt::ObjCSubscriptRefExprClass: 7366 case Stmt::ParenExprClass: 7367 case Stmt::StringLiteralClass: 7368 case Stmt::UnaryOperatorClass: 7369 return false; 7370 default: 7371 return true; 7372 } 7373 } 7374 7375 static std::pair<QualType, StringRef> 7376 shouldNotPrintDirectly(const ASTContext &Context, 7377 QualType IntendedTy, 7378 const Expr *E) { 7379 // Use a 'while' to peel off layers of typedefs. 7380 QualType TyTy = IntendedTy; 7381 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { 7382 StringRef Name = UserTy->getDecl()->getName(); 7383 QualType CastTy = llvm::StringSwitch<QualType>(Name) 7384 .Case("CFIndex", Context.getNSIntegerType()) 7385 .Case("NSInteger", Context.getNSIntegerType()) 7386 .Case("NSUInteger", Context.getNSUIntegerType()) 7387 .Case("SInt32", Context.IntTy) 7388 .Case("UInt32", Context.UnsignedIntTy) 7389 .Default(QualType()); 7390 7391 if (!CastTy.isNull()) 7392 return std::make_pair(CastTy, Name); 7393 7394 TyTy = UserTy->desugar(); 7395 } 7396 7397 // Strip parens if necessary. 7398 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 7399 return shouldNotPrintDirectly(Context, 7400 PE->getSubExpr()->getType(), 7401 PE->getSubExpr()); 7402 7403 // If this is a conditional expression, then its result type is constructed 7404 // via usual arithmetic conversions and thus there might be no necessary 7405 // typedef sugar there. Recurse to operands to check for NSInteger & 7406 // Co. usage condition. 7407 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 7408 QualType TrueTy, FalseTy; 7409 StringRef TrueName, FalseName; 7410 7411 std::tie(TrueTy, TrueName) = 7412 shouldNotPrintDirectly(Context, 7413 CO->getTrueExpr()->getType(), 7414 CO->getTrueExpr()); 7415 std::tie(FalseTy, FalseName) = 7416 shouldNotPrintDirectly(Context, 7417 CO->getFalseExpr()->getType(), 7418 CO->getFalseExpr()); 7419 7420 if (TrueTy == FalseTy) 7421 return std::make_pair(TrueTy, TrueName); 7422 else if (TrueTy.isNull()) 7423 return std::make_pair(FalseTy, FalseName); 7424 else if (FalseTy.isNull()) 7425 return std::make_pair(TrueTy, TrueName); 7426 } 7427 7428 return std::make_pair(QualType(), StringRef()); 7429 } 7430 7431 /// Return true if \p ICE is an implicit argument promotion of an arithmetic 7432 /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked 7433 /// type do not count. 7434 static bool 7435 isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) { 7436 QualType From = ICE->getSubExpr()->getType(); 7437 QualType To = ICE->getType(); 7438 // It's an integer promotion if the destination type is the promoted 7439 // source type. 7440 if (ICE->getCastKind() == CK_IntegralCast && 7441 S.Context.isPromotableIntegerType(From) && 7442 S.Context.getPromotedIntegerType(From) == To) 7443 return true; 7444 // Look through vector types, since we do default argument promotion for 7445 // those in OpenCL. 7446 if (const auto *VecTy = From->getAs<ExtVectorType>()) 7447 From = VecTy->getElementType(); 7448 if (const auto *VecTy = To->getAs<ExtVectorType>()) 7449 To = VecTy->getElementType(); 7450 // It's a floating promotion if the source type is a lower rank. 7451 return ICE->getCastKind() == CK_FloatingCast && 7452 S.Context.getFloatingTypeOrder(From, To) < 0; 7453 } 7454 7455 static analyze_format_string::ArgType::MatchKind 7456 handleFormatSignedness(analyze_format_string::ArgType::MatchKind Match, 7457 DiagnosticsEngine &Diags, SourceLocation Loc) { 7458 if (Match == analyze_format_string::ArgType::NoMatchSignedness) { 7459 Match = 7460 Diags.isIgnored( 7461 diag::warn_format_conversion_argument_type_mismatch_signedness, Loc) 7462 ? analyze_format_string::ArgType::Match 7463 : analyze_format_string::ArgType::NoMatch; 7464 } 7465 return Match; 7466 } 7467 7468 bool 7469 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 7470 const char *StartSpecifier, 7471 unsigned SpecifierLen, 7472 const Expr *E) { 7473 using namespace analyze_format_string; 7474 using namespace analyze_printf; 7475 7476 // Now type check the data expression that matches the 7477 // format specifier. 7478 const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext()); 7479 if (!AT.isValid()) 7480 return true; 7481 7482 QualType ExprTy = E->getType(); 7483 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { 7484 ExprTy = TET->getUnderlyingExpr()->getType(); 7485 } 7486 7487 // When using the format attribute in C++, you can receive a function or an 7488 // array that will necessarily decay to a pointer when passed to the final 7489 // format consumer. Apply decay before type comparison. 7490 if (ExprTy->canDecayToPointerType()) 7491 ExprTy = S.Context.getDecayedType(ExprTy); 7492 7493 // Diagnose attempts to print a boolean value as a character. Unlike other 7494 // -Wformat diagnostics, this is fine from a type perspective, but it still 7495 // doesn't make sense. 7496 if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::cArg && 7497 E->isKnownToHaveBooleanValue()) { 7498 const CharSourceRange &CSR = 7499 getSpecifierRange(StartSpecifier, SpecifierLen); 7500 SmallString<4> FSString; 7501 llvm::raw_svector_ostream os(FSString); 7502 FS.toString(os); 7503 EmitFormatDiagnostic(S.PDiag(diag::warn_format_bool_as_character) 7504 << FSString, 7505 E->getExprLoc(), false, CSR); 7506 return true; 7507 } 7508 7509 // Diagnose attempts to use '%P' with ObjC object types, which will result in 7510 // dumping raw class data (like is-a pointer), not actual data. 7511 if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::PArg && 7512 ExprTy->isObjCObjectPointerType()) { 7513 const CharSourceRange &CSR = 7514 getSpecifierRange(StartSpecifier, SpecifierLen); 7515 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_with_objc_pointer), 7516 E->getExprLoc(), false, CSR); 7517 return true; 7518 } 7519 7520 ArgType::MatchKind ImplicitMatch = ArgType::NoMatch; 7521 ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy); 7522 ArgType::MatchKind OrigMatch = Match; 7523 7524 Match = handleFormatSignedness(Match, S.getDiagnostics(), E->getExprLoc()); 7525 if (Match == ArgType::Match) 7526 return true; 7527 7528 // NoMatchPromotionTypeConfusion should be only returned in ImplictCastExpr 7529 assert(Match != ArgType::NoMatchPromotionTypeConfusion); 7530 7531 // Look through argument promotions for our error message's reported type. 7532 // This includes the integral and floating promotions, but excludes array 7533 // and function pointer decay (seeing that an argument intended to be a 7534 // string has type 'char [6]' is probably more confusing than 'char *') and 7535 // certain bitfield promotions (bitfields can be 'demoted' to a lesser type). 7536 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 7537 if (isArithmeticArgumentPromotion(S, ICE)) { 7538 E = ICE->getSubExpr(); 7539 ExprTy = E->getType(); 7540 7541 // Check if we didn't match because of an implicit cast from a 'char' 7542 // or 'short' to an 'int'. This is done because printf is a varargs 7543 // function. 7544 if (ICE->getType() == S.Context.IntTy || 7545 ICE->getType() == S.Context.UnsignedIntTy) { 7546 // All further checking is done on the subexpression 7547 ImplicitMatch = AT.matchesType(S.Context, ExprTy); 7548 if (OrigMatch == ArgType::NoMatchSignedness && 7549 ImplicitMatch != ArgType::NoMatchSignedness) 7550 // If the original match was a signedness match this match on the 7551 // implicit cast type also need to be signedness match otherwise we 7552 // might introduce new unexpected warnings from -Wformat-signedness. 7553 return true; 7554 ImplicitMatch = handleFormatSignedness( 7555 ImplicitMatch, S.getDiagnostics(), E->getExprLoc()); 7556 if (ImplicitMatch == ArgType::Match) 7557 return true; 7558 } 7559 } 7560 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { 7561 // Special case for 'a', which has type 'int' in C. 7562 // Note, however, that we do /not/ want to treat multibyte constants like 7563 // 'MooV' as characters! This form is deprecated but still exists. In 7564 // addition, don't treat expressions as of type 'char' if one byte length 7565 // modifier is provided. 7566 if (ExprTy == S.Context.IntTy && 7567 FS.getLengthModifier().getKind() != LengthModifier::AsChar) 7568 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) { 7569 ExprTy = S.Context.CharTy; 7570 // To improve check results, we consider a character literal in C 7571 // to be a 'char' rather than an 'int'. 'printf("%hd", 'a');' is 7572 // more likely a type confusion situation, so we will suggest to 7573 // use '%hhd' instead by discarding the MatchPromotion. 7574 if (Match == ArgType::MatchPromotion) 7575 Match = ArgType::NoMatch; 7576 } 7577 } 7578 if (Match == ArgType::MatchPromotion) { 7579 // WG14 N2562 only clarified promotions in *printf 7580 // For NSLog in ObjC, just preserve -Wformat behavior 7581 if (!S.getLangOpts().ObjC && 7582 ImplicitMatch != ArgType::NoMatchPromotionTypeConfusion && 7583 ImplicitMatch != ArgType::NoMatchTypeConfusion) 7584 return true; 7585 Match = ArgType::NoMatch; 7586 } 7587 if (ImplicitMatch == ArgType::NoMatchPedantic || 7588 ImplicitMatch == ArgType::NoMatchTypeConfusion) 7589 Match = ImplicitMatch; 7590 assert(Match != ArgType::MatchPromotion); 7591 7592 // Look through unscoped enums to their underlying type. 7593 bool IsEnum = false; 7594 bool IsScopedEnum = false; 7595 QualType IntendedTy = ExprTy; 7596 if (auto EnumTy = ExprTy->getAs<EnumType>()) { 7597 IntendedTy = EnumTy->getDecl()->getIntegerType(); 7598 if (EnumTy->isUnscopedEnumerationType()) { 7599 ExprTy = IntendedTy; 7600 // This controls whether we're talking about the underlying type or not, 7601 // which we only want to do when it's an unscoped enum. 7602 IsEnum = true; 7603 } else { 7604 IsScopedEnum = true; 7605 } 7606 } 7607 7608 // %C in an Objective-C context prints a unichar, not a wchar_t. 7609 // If the argument is an integer of some kind, believe the %C and suggest 7610 // a cast instead of changing the conversion specifier. 7611 if (isObjCContext() && 7612 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { 7613 if (ExprTy->isIntegralOrUnscopedEnumerationType() && 7614 !ExprTy->isCharType()) { 7615 // 'unichar' is defined as a typedef of unsigned short, but we should 7616 // prefer using the typedef if it is visible. 7617 IntendedTy = S.Context.UnsignedShortTy; 7618 7619 // While we are here, check if the value is an IntegerLiteral that happens 7620 // to be within the valid range. 7621 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { 7622 const llvm::APInt &V = IL->getValue(); 7623 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) 7624 return true; 7625 } 7626 7627 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(), 7628 Sema::LookupOrdinaryName); 7629 if (S.LookupName(Result, S.getCurScope())) { 7630 NamedDecl *ND = Result.getFoundDecl(); 7631 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) 7632 if (TD->getUnderlyingType() == IntendedTy) 7633 IntendedTy = S.Context.getTypedefType(TD); 7634 } 7635 } 7636 } 7637 7638 // Special-case some of Darwin's platform-independence types by suggesting 7639 // casts to primitive types that are known to be large enough. 7640 bool ShouldNotPrintDirectly = false; StringRef CastTyName; 7641 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { 7642 QualType CastTy; 7643 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); 7644 if (!CastTy.isNull()) { 7645 // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int 7646 // (long in ASTContext). Only complain to pedants or when they're the 7647 // underlying type of a scoped enum (which always needs a cast). 7648 if (!IsScopedEnum && 7649 (CastTyName == "NSInteger" || CastTyName == "NSUInteger") && 7650 (AT.isSizeT() || AT.isPtrdiffT()) && 7651 AT.matchesType(S.Context, CastTy)) 7652 Match = ArgType::NoMatchPedantic; 7653 IntendedTy = CastTy; 7654 ShouldNotPrintDirectly = true; 7655 } 7656 } 7657 7658 // We may be able to offer a FixItHint if it is a supported type. 7659 PrintfSpecifier fixedFS = FS; 7660 bool Success = 7661 fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext()); 7662 7663 if (Success) { 7664 // Get the fix string from the fixed format specifier 7665 SmallString<16> buf; 7666 llvm::raw_svector_ostream os(buf); 7667 fixedFS.toString(os); 7668 7669 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); 7670 7671 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) { 7672 unsigned Diag; 7673 switch (Match) { 7674 case ArgType::Match: 7675 case ArgType::MatchPromotion: 7676 case ArgType::NoMatchPromotionTypeConfusion: 7677 case ArgType::NoMatchSignedness: 7678 llvm_unreachable("expected non-matching"); 7679 case ArgType::NoMatchPedantic: 7680 Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; 7681 break; 7682 case ArgType::NoMatchTypeConfusion: 7683 Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; 7684 break; 7685 case ArgType::NoMatch: 7686 Diag = diag::warn_format_conversion_argument_type_mismatch; 7687 break; 7688 } 7689 7690 // In this case, the specifier is wrong and should be changed to match 7691 // the argument. 7692 EmitFormatDiagnostic(S.PDiag(Diag) 7693 << AT.getRepresentativeTypeName(S.Context) 7694 << IntendedTy << IsEnum << E->getSourceRange(), 7695 E->getBeginLoc(), 7696 /*IsStringLocation*/ false, SpecRange, 7697 FixItHint::CreateReplacement(SpecRange, os.str())); 7698 } else { 7699 // The canonical type for formatting this value is different from the 7700 // actual type of the expression. (This occurs, for example, with Darwin's 7701 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but 7702 // should be printed as 'long' for 64-bit compatibility.) 7703 // Rather than emitting a normal format/argument mismatch, we want to 7704 // add a cast to the recommended type (and correct the format string 7705 // if necessary). We should also do so for scoped enumerations. 7706 SmallString<16> CastBuf; 7707 llvm::raw_svector_ostream CastFix(CastBuf); 7708 CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); 7709 IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); 7710 CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); 7711 7712 SmallVector<FixItHint,4> Hints; 7713 ArgType::MatchKind IntendedMatch = AT.matchesType(S.Context, IntendedTy); 7714 IntendedMatch = handleFormatSignedness(IntendedMatch, S.getDiagnostics(), 7715 E->getExprLoc()); 7716 if ((IntendedMatch != ArgType::Match) || ShouldNotPrintDirectly) 7717 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); 7718 7719 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { 7720 // If there's already a cast present, just replace it. 7721 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); 7722 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); 7723 7724 } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) { 7725 // If the expression has high enough precedence, 7726 // just write the C-style cast. 7727 Hints.push_back( 7728 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 7729 } else { 7730 // Otherwise, add parens around the expression as well as the cast. 7731 CastFix << "("; 7732 Hints.push_back( 7733 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 7734 7735 // We don't use getLocForEndOfToken because it returns invalid source 7736 // locations for macro expansions (by design). 7737 SourceLocation EndLoc = S.SourceMgr.getSpellingLoc(E->getEndLoc()); 7738 SourceLocation After = EndLoc.getLocWithOffset( 7739 Lexer::MeasureTokenLength(EndLoc, S.SourceMgr, S.LangOpts)); 7740 Hints.push_back(FixItHint::CreateInsertion(After, ")")); 7741 } 7742 7743 if (ShouldNotPrintDirectly && !IsScopedEnum) { 7744 // The expression has a type that should not be printed directly. 7745 // We extract the name from the typedef because we don't want to show 7746 // the underlying type in the diagnostic. 7747 StringRef Name; 7748 if (const auto *TypedefTy = ExprTy->getAs<TypedefType>()) 7749 Name = TypedefTy->getDecl()->getName(); 7750 else 7751 Name = CastTyName; 7752 unsigned Diag = Match == ArgType::NoMatchPedantic 7753 ? diag::warn_format_argument_needs_cast_pedantic 7754 : diag::warn_format_argument_needs_cast; 7755 EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum 7756 << E->getSourceRange(), 7757 E->getBeginLoc(), /*IsStringLocation=*/false, 7758 SpecRange, Hints); 7759 } else { 7760 // In this case, the expression could be printed using a different 7761 // specifier, but we've decided that the specifier is probably correct 7762 // and we should cast instead. Just use the normal warning message. 7763 7764 unsigned Diag = 7765 IsScopedEnum 7766 ? diag::warn_format_conversion_argument_type_mismatch_pedantic 7767 : diag::warn_format_conversion_argument_type_mismatch; 7768 7769 EmitFormatDiagnostic( 7770 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 7771 << IsEnum << E->getSourceRange(), 7772 E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints); 7773 } 7774 } 7775 } else { 7776 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, 7777 SpecifierLen); 7778 // Since the warning for passing non-POD types to variadic functions 7779 // was deferred until now, we emit a warning for non-POD 7780 // arguments here. 7781 bool EmitTypeMismatch = false; 7782 switch (S.isValidVarArgType(ExprTy)) { 7783 case Sema::VAK_Valid: 7784 case Sema::VAK_ValidInCXX11: { 7785 unsigned Diag; 7786 switch (Match) { 7787 case ArgType::Match: 7788 case ArgType::MatchPromotion: 7789 case ArgType::NoMatchPromotionTypeConfusion: 7790 case ArgType::NoMatchSignedness: 7791 llvm_unreachable("expected non-matching"); 7792 case ArgType::NoMatchPedantic: 7793 Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; 7794 break; 7795 case ArgType::NoMatchTypeConfusion: 7796 Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; 7797 break; 7798 case ArgType::NoMatch: 7799 Diag = diag::warn_format_conversion_argument_type_mismatch; 7800 break; 7801 } 7802 7803 EmitFormatDiagnostic( 7804 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 7805 << IsEnum << CSR << E->getSourceRange(), 7806 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7807 break; 7808 } 7809 case Sema::VAK_Undefined: 7810 case Sema::VAK_MSVCUndefined: 7811 if (CallType == Sema::VariadicDoesNotApply) { 7812 EmitTypeMismatch = true; 7813 } else { 7814 EmitFormatDiagnostic( 7815 S.PDiag(diag::warn_non_pod_vararg_with_format_string) 7816 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 7817 << AT.getRepresentativeTypeName(S.Context) << CSR 7818 << E->getSourceRange(), 7819 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7820 checkForCStrMembers(AT, E); 7821 } 7822 break; 7823 7824 case Sema::VAK_Invalid: 7825 if (CallType == Sema::VariadicDoesNotApply) 7826 EmitTypeMismatch = true; 7827 else if (ExprTy->isObjCObjectType()) 7828 EmitFormatDiagnostic( 7829 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) 7830 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 7831 << AT.getRepresentativeTypeName(S.Context) << CSR 7832 << E->getSourceRange(), 7833 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7834 else 7835 // FIXME: If this is an initializer list, suggest removing the braces 7836 // or inserting a cast to the target type. 7837 S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format) 7838 << isa<InitListExpr>(E) << ExprTy << CallType 7839 << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange(); 7840 break; 7841 } 7842 7843 if (EmitTypeMismatch) { 7844 // The function is not variadic, so we do not generate warnings about 7845 // being allowed to pass that object as a variadic argument. Instead, 7846 // since there are inherently no printf specifiers for types which cannot 7847 // be passed as variadic arguments, emit a plain old specifier mismatch 7848 // argument. 7849 EmitFormatDiagnostic( 7850 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7851 << AT.getRepresentativeTypeName(S.Context) << ExprTy << false 7852 << E->getSourceRange(), 7853 E->getBeginLoc(), false, CSR); 7854 } 7855 7856 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && 7857 "format string specifier index out of range"); 7858 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; 7859 } 7860 7861 return true; 7862 } 7863 7864 //===--- CHECK: Scanf format string checking ------------------------------===// 7865 7866 namespace { 7867 7868 class CheckScanfHandler : public CheckFormatHandler { 7869 public: 7870 CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr, 7871 const Expr *origFormatExpr, Sema::FormatStringType type, 7872 unsigned firstDataArg, unsigned numDataArgs, 7873 const char *beg, Sema::FormatArgumentPassingKind APK, 7874 ArrayRef<const Expr *> Args, unsigned formatIdx, 7875 bool inFunctionCall, Sema::VariadicCallType CallType, 7876 llvm::SmallBitVector &CheckedVarArgs, 7877 UncoveredArgHandler &UncoveredArg) 7878 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 7879 numDataArgs, beg, APK, Args, formatIdx, 7880 inFunctionCall, CallType, CheckedVarArgs, 7881 UncoveredArg) {} 7882 7883 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 7884 const char *startSpecifier, 7885 unsigned specifierLen) override; 7886 7887 bool HandleInvalidScanfConversionSpecifier( 7888 const analyze_scanf::ScanfSpecifier &FS, 7889 const char *startSpecifier, 7890 unsigned specifierLen) override; 7891 7892 void HandleIncompleteScanList(const char *start, const char *end) override; 7893 }; 7894 7895 } // namespace 7896 7897 void CheckScanfHandler::HandleIncompleteScanList(const char *start, 7898 const char *end) { 7899 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), 7900 getLocationOfByte(end), /*IsStringLocation*/true, 7901 getSpecifierRange(start, end - start)); 7902 } 7903 7904 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( 7905 const analyze_scanf::ScanfSpecifier &FS, 7906 const char *startSpecifier, 7907 unsigned specifierLen) { 7908 const analyze_scanf::ScanfConversionSpecifier &CS = 7909 FS.getConversionSpecifier(); 7910 7911 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 7912 getLocationOfByte(CS.getStart()), 7913 startSpecifier, specifierLen, 7914 CS.getStart(), CS.getLength()); 7915 } 7916 7917 bool CheckScanfHandler::HandleScanfSpecifier( 7918 const analyze_scanf::ScanfSpecifier &FS, 7919 const char *startSpecifier, 7920 unsigned specifierLen) { 7921 using namespace analyze_scanf; 7922 using namespace analyze_format_string; 7923 7924 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); 7925 7926 // Handle case where '%' and '*' don't consume an argument. These shouldn't 7927 // be used to decide if we are using positional arguments consistently. 7928 if (FS.consumesDataArgument()) { 7929 if (atFirstArg) { 7930 atFirstArg = false; 7931 usesPositionalArgs = FS.usesPositionalArg(); 7932 } 7933 else if (usesPositionalArgs != FS.usesPositionalArg()) { 7934 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 7935 startSpecifier, specifierLen); 7936 return false; 7937 } 7938 } 7939 7940 // Check if the field with is non-zero. 7941 const OptionalAmount &Amt = FS.getFieldWidth(); 7942 if (Amt.getHowSpecified() == OptionalAmount::Constant) { 7943 if (Amt.getConstantAmount() == 0) { 7944 const CharSourceRange &R = getSpecifierRange(Amt.getStart(), 7945 Amt.getConstantLength()); 7946 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), 7947 getLocationOfByte(Amt.getStart()), 7948 /*IsStringLocation*/true, R, 7949 FixItHint::CreateRemoval(R)); 7950 } 7951 } 7952 7953 if (!FS.consumesDataArgument()) { 7954 // FIXME: Technically specifying a precision or field width here 7955 // makes no sense. Worth issuing a warning at some point. 7956 return true; 7957 } 7958 7959 // Consume the argument. 7960 unsigned argIndex = FS.getArgIndex(); 7961 if (argIndex < NumDataArgs) { 7962 // The check to see if the argIndex is valid will come later. 7963 // We set the bit here because we may exit early from this 7964 // function if we encounter some other error. 7965 CoveredArgs.set(argIndex); 7966 } 7967 7968 // Check the length modifier is valid with the given conversion specifier. 7969 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7970 S.getLangOpts())) 7971 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7972 diag::warn_format_nonsensical_length); 7973 else if (!FS.hasStandardLengthModifier()) 7974 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7975 else if (!FS.hasStandardLengthConversionCombination()) 7976 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7977 diag::warn_format_non_standard_conversion_spec); 7978 7979 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7980 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7981 7982 // The remaining checks depend on the data arguments. 7983 if (ArgPassingKind == Sema::FAPK_VAList) 7984 return true; 7985 7986 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7987 return false; 7988 7989 // Check that the argument type matches the format specifier. 7990 const Expr *Ex = getDataArg(argIndex); 7991 if (!Ex) 7992 return true; 7993 7994 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); 7995 7996 if (!AT.isValid()) { 7997 return true; 7998 } 7999 8000 analyze_format_string::ArgType::MatchKind Match = 8001 AT.matchesType(S.Context, Ex->getType()); 8002 Match = handleFormatSignedness(Match, S.getDiagnostics(), Ex->getExprLoc()); 8003 bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic; 8004 if (Match == analyze_format_string::ArgType::Match) 8005 return true; 8006 8007 ScanfSpecifier fixedFS = FS; 8008 bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), 8009 S.getLangOpts(), S.Context); 8010 8011 unsigned Diag = 8012 Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic 8013 : diag::warn_format_conversion_argument_type_mismatch; 8014 8015 if (Success) { 8016 // Get the fix string from the fixed format specifier. 8017 SmallString<128> buf; 8018 llvm::raw_svector_ostream os(buf); 8019 fixedFS.toString(os); 8020 8021 EmitFormatDiagnostic( 8022 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) 8023 << Ex->getType() << false << Ex->getSourceRange(), 8024 Ex->getBeginLoc(), 8025 /*IsStringLocation*/ false, 8026 getSpecifierRange(startSpecifier, specifierLen), 8027 FixItHint::CreateReplacement( 8028 getSpecifierRange(startSpecifier, specifierLen), os.str())); 8029 } else { 8030 EmitFormatDiagnostic(S.PDiag(Diag) 8031 << AT.getRepresentativeTypeName(S.Context) 8032 << Ex->getType() << false << Ex->getSourceRange(), 8033 Ex->getBeginLoc(), 8034 /*IsStringLocation*/ false, 8035 getSpecifierRange(startSpecifier, specifierLen)); 8036 } 8037 8038 return true; 8039 } 8040 8041 static void CheckFormatString( 8042 Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, 8043 ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, 8044 unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, 8045 bool inFunctionCall, Sema::VariadicCallType CallType, 8046 llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, 8047 bool IgnoreStringsWithoutSpecifiers) { 8048 // CHECK: is the format string a wide literal? 8049 if (!FExpr->isAscii() && !FExpr->isUTF8()) { 8050 CheckFormatHandler::EmitFormatDiagnostic( 8051 S, inFunctionCall, Args[format_idx], 8052 S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(), 8053 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 8054 return; 8055 } 8056 8057 // Str - The format string. NOTE: this is NOT null-terminated! 8058 StringRef StrRef = FExpr->getString(); 8059 const char *Str = StrRef.data(); 8060 // Account for cases where the string literal is truncated in a declaration. 8061 const ConstantArrayType *T = 8062 S.Context.getAsConstantArrayType(FExpr->getType()); 8063 assert(T && "String literal not of constant array type!"); 8064 size_t TypeSize = T->getZExtSize(); 8065 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 8066 const unsigned numDataArgs = Args.size() - firstDataArg; 8067 8068 if (IgnoreStringsWithoutSpecifiers && 8069 !analyze_format_string::parseFormatStringHasFormattingSpecifiers( 8070 Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) 8071 return; 8072 8073 // Emit a warning if the string literal is truncated and does not contain an 8074 // embedded null character. 8075 if (TypeSize <= StrRef.size() && !StrRef.substr(0, TypeSize).contains('\0')) { 8076 CheckFormatHandler::EmitFormatDiagnostic( 8077 S, inFunctionCall, Args[format_idx], 8078 S.PDiag(diag::warn_printf_format_string_not_null_terminated), 8079 FExpr->getBeginLoc(), 8080 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); 8081 return; 8082 } 8083 8084 // CHECK: empty format string? 8085 if (StrLen == 0 && numDataArgs > 0) { 8086 CheckFormatHandler::EmitFormatDiagnostic( 8087 S, inFunctionCall, Args[format_idx], 8088 S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(), 8089 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 8090 return; 8091 } 8092 8093 if (Type == Sema::FST_Printf || Type == Sema::FST_NSString || 8094 Type == Sema::FST_Kprintf || Type == Sema::FST_FreeBSDKPrintf || 8095 Type == Sema::FST_OSLog || Type == Sema::FST_OSTrace || 8096 Type == Sema::FST_Syslog) { 8097 CheckPrintfHandler H( 8098 S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs, 8099 (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, APK, 8100 Args, format_idx, inFunctionCall, CallType, CheckedVarArgs, 8101 UncoveredArg); 8102 8103 if (!analyze_format_string::ParsePrintfString( 8104 H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo(), 8105 Type == Sema::FST_Kprintf || Type == Sema::FST_FreeBSDKPrintf)) 8106 H.DoneProcessing(); 8107 } else if (Type == Sema::FST_Scanf) { 8108 CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg, 8109 numDataArgs, Str, APK, Args, format_idx, inFunctionCall, 8110 CallType, CheckedVarArgs, UncoveredArg); 8111 8112 if (!analyze_format_string::ParseScanfString( 8113 H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) 8114 H.DoneProcessing(); 8115 } // TODO: handle other formats 8116 } 8117 8118 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { 8119 // Str - The format string. NOTE: this is NOT null-terminated! 8120 StringRef StrRef = FExpr->getString(); 8121 const char *Str = StrRef.data(); 8122 // Account for cases where the string literal is truncated in a declaration. 8123 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); 8124 assert(T && "String literal not of constant array type!"); 8125 size_t TypeSize = T->getZExtSize(); 8126 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 8127 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, 8128 getLangOpts(), 8129 Context.getTargetInfo()); 8130 } 8131 8132 //===--- CHECK: Warn on use of wrong absolute value function. -------------===// 8133 8134 // Returns the related absolute value function that is larger, of 0 if one 8135 // does not exist. 8136 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { 8137 switch (AbsFunction) { 8138 default: 8139 return 0; 8140 8141 case Builtin::BI__builtin_abs: 8142 return Builtin::BI__builtin_labs; 8143 case Builtin::BI__builtin_labs: 8144 return Builtin::BI__builtin_llabs; 8145 case Builtin::BI__builtin_llabs: 8146 return 0; 8147 8148 case Builtin::BI__builtin_fabsf: 8149 return Builtin::BI__builtin_fabs; 8150 case Builtin::BI__builtin_fabs: 8151 return Builtin::BI__builtin_fabsl; 8152 case Builtin::BI__builtin_fabsl: 8153 return 0; 8154 8155 case Builtin::BI__builtin_cabsf: 8156 return Builtin::BI__builtin_cabs; 8157 case Builtin::BI__builtin_cabs: 8158 return Builtin::BI__builtin_cabsl; 8159 case Builtin::BI__builtin_cabsl: 8160 return 0; 8161 8162 case Builtin::BIabs: 8163 return Builtin::BIlabs; 8164 case Builtin::BIlabs: 8165 return Builtin::BIllabs; 8166 case Builtin::BIllabs: 8167 return 0; 8168 8169 case Builtin::BIfabsf: 8170 return Builtin::BIfabs; 8171 case Builtin::BIfabs: 8172 return Builtin::BIfabsl; 8173 case Builtin::BIfabsl: 8174 return 0; 8175 8176 case Builtin::BIcabsf: 8177 return Builtin::BIcabs; 8178 case Builtin::BIcabs: 8179 return Builtin::BIcabsl; 8180 case Builtin::BIcabsl: 8181 return 0; 8182 } 8183 } 8184 8185 // Returns the argument type of the absolute value function. 8186 static QualType getAbsoluteValueArgumentType(ASTContext &Context, 8187 unsigned AbsType) { 8188 if (AbsType == 0) 8189 return QualType(); 8190 8191 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; 8192 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); 8193 if (Error != ASTContext::GE_None) 8194 return QualType(); 8195 8196 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); 8197 if (!FT) 8198 return QualType(); 8199 8200 if (FT->getNumParams() != 1) 8201 return QualType(); 8202 8203 return FT->getParamType(0); 8204 } 8205 8206 // Returns the best absolute value function, or zero, based on type and 8207 // current absolute value function. 8208 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, 8209 unsigned AbsFunctionKind) { 8210 unsigned BestKind = 0; 8211 uint64_t ArgSize = Context.getTypeSize(ArgType); 8212 for (unsigned Kind = AbsFunctionKind; Kind != 0; 8213 Kind = getLargerAbsoluteValueFunction(Kind)) { 8214 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); 8215 if (Context.getTypeSize(ParamType) >= ArgSize) { 8216 if (BestKind == 0) 8217 BestKind = Kind; 8218 else if (Context.hasSameType(ParamType, ArgType)) { 8219 BestKind = Kind; 8220 break; 8221 } 8222 } 8223 } 8224 return BestKind; 8225 } 8226 8227 enum AbsoluteValueKind { 8228 AVK_Integer, 8229 AVK_Floating, 8230 AVK_Complex 8231 }; 8232 8233 static AbsoluteValueKind getAbsoluteValueKind(QualType T) { 8234 if (T->isIntegralOrEnumerationType()) 8235 return AVK_Integer; 8236 if (T->isRealFloatingType()) 8237 return AVK_Floating; 8238 if (T->isAnyComplexType()) 8239 return AVK_Complex; 8240 8241 llvm_unreachable("Type not integer, floating, or complex"); 8242 } 8243 8244 // Changes the absolute value function to a different type. Preserves whether 8245 // the function is a builtin. 8246 static unsigned changeAbsFunction(unsigned AbsKind, 8247 AbsoluteValueKind ValueKind) { 8248 switch (ValueKind) { 8249 case AVK_Integer: 8250 switch (AbsKind) { 8251 default: 8252 return 0; 8253 case Builtin::BI__builtin_fabsf: 8254 case Builtin::BI__builtin_fabs: 8255 case Builtin::BI__builtin_fabsl: 8256 case Builtin::BI__builtin_cabsf: 8257 case Builtin::BI__builtin_cabs: 8258 case Builtin::BI__builtin_cabsl: 8259 return Builtin::BI__builtin_abs; 8260 case Builtin::BIfabsf: 8261 case Builtin::BIfabs: 8262 case Builtin::BIfabsl: 8263 case Builtin::BIcabsf: 8264 case Builtin::BIcabs: 8265 case Builtin::BIcabsl: 8266 return Builtin::BIabs; 8267 } 8268 case AVK_Floating: 8269 switch (AbsKind) { 8270 default: 8271 return 0; 8272 case Builtin::BI__builtin_abs: 8273 case Builtin::BI__builtin_labs: 8274 case Builtin::BI__builtin_llabs: 8275 case Builtin::BI__builtin_cabsf: 8276 case Builtin::BI__builtin_cabs: 8277 case Builtin::BI__builtin_cabsl: 8278 return Builtin::BI__builtin_fabsf; 8279 case Builtin::BIabs: 8280 case Builtin::BIlabs: 8281 case Builtin::BIllabs: 8282 case Builtin::BIcabsf: 8283 case Builtin::BIcabs: 8284 case Builtin::BIcabsl: 8285 return Builtin::BIfabsf; 8286 } 8287 case AVK_Complex: 8288 switch (AbsKind) { 8289 default: 8290 return 0; 8291 case Builtin::BI__builtin_abs: 8292 case Builtin::BI__builtin_labs: 8293 case Builtin::BI__builtin_llabs: 8294 case Builtin::BI__builtin_fabsf: 8295 case Builtin::BI__builtin_fabs: 8296 case Builtin::BI__builtin_fabsl: 8297 return Builtin::BI__builtin_cabsf; 8298 case Builtin::BIabs: 8299 case Builtin::BIlabs: 8300 case Builtin::BIllabs: 8301 case Builtin::BIfabsf: 8302 case Builtin::BIfabs: 8303 case Builtin::BIfabsl: 8304 return Builtin::BIcabsf; 8305 } 8306 } 8307 llvm_unreachable("Unable to convert function"); 8308 } 8309 8310 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { 8311 const IdentifierInfo *FnInfo = FDecl->getIdentifier(); 8312 if (!FnInfo) 8313 return 0; 8314 8315 switch (FDecl->getBuiltinID()) { 8316 default: 8317 return 0; 8318 case Builtin::BI__builtin_abs: 8319 case Builtin::BI__builtin_fabs: 8320 case Builtin::BI__builtin_fabsf: 8321 case Builtin::BI__builtin_fabsl: 8322 case Builtin::BI__builtin_labs: 8323 case Builtin::BI__builtin_llabs: 8324 case Builtin::BI__builtin_cabs: 8325 case Builtin::BI__builtin_cabsf: 8326 case Builtin::BI__builtin_cabsl: 8327 case Builtin::BIabs: 8328 case Builtin::BIlabs: 8329 case Builtin::BIllabs: 8330 case Builtin::BIfabs: 8331 case Builtin::BIfabsf: 8332 case Builtin::BIfabsl: 8333 case Builtin::BIcabs: 8334 case Builtin::BIcabsf: 8335 case Builtin::BIcabsl: 8336 return FDecl->getBuiltinID(); 8337 } 8338 llvm_unreachable("Unknown Builtin type"); 8339 } 8340 8341 // If the replacement is valid, emit a note with replacement function. 8342 // Additionally, suggest including the proper header if not already included. 8343 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, 8344 unsigned AbsKind, QualType ArgType) { 8345 bool EmitHeaderHint = true; 8346 const char *HeaderName = nullptr; 8347 StringRef FunctionName; 8348 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { 8349 FunctionName = "std::abs"; 8350 if (ArgType->isIntegralOrEnumerationType()) { 8351 HeaderName = "cstdlib"; 8352 } else if (ArgType->isRealFloatingType()) { 8353 HeaderName = "cmath"; 8354 } else { 8355 llvm_unreachable("Invalid Type"); 8356 } 8357 8358 // Lookup all std::abs 8359 if (NamespaceDecl *Std = S.getStdNamespace()) { 8360 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); 8361 R.suppressDiagnostics(); 8362 S.LookupQualifiedName(R, Std); 8363 8364 for (const auto *I : R) { 8365 const FunctionDecl *FDecl = nullptr; 8366 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { 8367 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); 8368 } else { 8369 FDecl = dyn_cast<FunctionDecl>(I); 8370 } 8371 if (!FDecl) 8372 continue; 8373 8374 // Found std::abs(), check that they are the right ones. 8375 if (FDecl->getNumParams() != 1) 8376 continue; 8377 8378 // Check that the parameter type can handle the argument. 8379 QualType ParamType = FDecl->getParamDecl(0)->getType(); 8380 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && 8381 S.Context.getTypeSize(ArgType) <= 8382 S.Context.getTypeSize(ParamType)) { 8383 // Found a function, don't need the header hint. 8384 EmitHeaderHint = false; 8385 break; 8386 } 8387 } 8388 } 8389 } else { 8390 FunctionName = S.Context.BuiltinInfo.getName(AbsKind); 8391 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); 8392 8393 if (HeaderName) { 8394 DeclarationName DN(&S.Context.Idents.get(FunctionName)); 8395 LookupResult R(S, DN, Loc, Sema::LookupAnyName); 8396 R.suppressDiagnostics(); 8397 S.LookupName(R, S.getCurScope()); 8398 8399 if (R.isSingleResult()) { 8400 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); 8401 if (FD && FD->getBuiltinID() == AbsKind) { 8402 EmitHeaderHint = false; 8403 } else { 8404 return; 8405 } 8406 } else if (!R.empty()) { 8407 return; 8408 } 8409 } 8410 } 8411 8412 S.Diag(Loc, diag::note_replace_abs_function) 8413 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); 8414 8415 if (!HeaderName) 8416 return; 8417 8418 if (!EmitHeaderHint) 8419 return; 8420 8421 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName 8422 << FunctionName; 8423 } 8424 8425 template <std::size_t StrLen> 8426 static bool IsStdFunction(const FunctionDecl *FDecl, 8427 const char (&Str)[StrLen]) { 8428 if (!FDecl) 8429 return false; 8430 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str)) 8431 return false; 8432 if (!FDecl->isInStdNamespace()) 8433 return false; 8434 8435 return true; 8436 } 8437 8438 enum class MathCheck { NaN, Inf }; 8439 static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { 8440 auto MatchesAny = [&](std::initializer_list<llvm::StringRef> names) { 8441 return std::any_of(names.begin(), names.end(), [&](llvm::StringRef name) { 8442 return calleeName == name; 8443 }); 8444 }; 8445 8446 switch (Check) { 8447 case MathCheck::NaN: 8448 return MatchesAny({"__builtin_nan", "__builtin_nanf", "__builtin_nanl", 8449 "__builtin_nanf16", "__builtin_nanf128"}); 8450 case MathCheck::Inf: 8451 return MatchesAny({"__builtin_inf", "__builtin_inff", "__builtin_infl", 8452 "__builtin_inff16", "__builtin_inff128"}); 8453 } 8454 llvm_unreachable("unknown MathCheck"); 8455 } 8456 8457 void Sema::CheckInfNaNFunction(const CallExpr *Call, 8458 const FunctionDecl *FDecl) { 8459 FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); 8460 bool HasIdentifier = FDecl->getIdentifier() != nullptr; 8461 bool IsNaNOrIsUnordered = 8462 IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); 8463 bool IsSpecialNaN = 8464 HasIdentifier && IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); 8465 if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { 8466 Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 8467 << 1 << 0 << Call->getSourceRange(); 8468 } else { 8469 bool IsInfOrIsFinite = 8470 IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); 8471 bool IsInfinityOrIsSpecialInf = 8472 HasIdentifier && ((FDecl->getName() == "infinity") || 8473 IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); 8474 if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) 8475 Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 8476 << 0 << 0 << Call->getSourceRange(); 8477 } 8478 } 8479 8480 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, 8481 const FunctionDecl *FDecl) { 8482 if (Call->getNumArgs() != 1) 8483 return; 8484 8485 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); 8486 bool IsStdAbs = IsStdFunction(FDecl, "abs"); 8487 if (AbsKind == 0 && !IsStdAbs) 8488 return; 8489 8490 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8491 QualType ParamType = Call->getArg(0)->getType(); 8492 8493 // Unsigned types cannot be negative. Suggest removing the absolute value 8494 // function call. 8495 if (ArgType->isUnsignedIntegerType()) { 8496 StringRef FunctionName = 8497 IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind); 8498 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; 8499 Diag(Call->getExprLoc(), diag::note_remove_abs) 8500 << FunctionName 8501 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); 8502 return; 8503 } 8504 8505 // Taking the absolute value of a pointer is very suspicious, they probably 8506 // wanted to index into an array, dereference a pointer, call a function, etc. 8507 if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) { 8508 unsigned DiagType = 0; 8509 if (ArgType->isFunctionType()) 8510 DiagType = 1; 8511 else if (ArgType->isArrayType()) 8512 DiagType = 2; 8513 8514 Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType; 8515 return; 8516 } 8517 8518 // std::abs has overloads which prevent most of the absolute value problems 8519 // from occurring. 8520 if (IsStdAbs) 8521 return; 8522 8523 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); 8524 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); 8525 8526 // The argument and parameter are the same kind. Check if they are the right 8527 // size. 8528 if (ArgValueKind == ParamValueKind) { 8529 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) 8530 return; 8531 8532 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); 8533 Diag(Call->getExprLoc(), diag::warn_abs_too_small) 8534 << FDecl << ArgType << ParamType; 8535 8536 if (NewAbsKind == 0) 8537 return; 8538 8539 emitReplacement(*this, Call->getExprLoc(), 8540 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8541 return; 8542 } 8543 8544 // ArgValueKind != ParamValueKind 8545 // The wrong type of absolute value function was used. Attempt to find the 8546 // proper one. 8547 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); 8548 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); 8549 if (NewAbsKind == 0) 8550 return; 8551 8552 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) 8553 << FDecl << ParamValueKind << ArgValueKind; 8554 8555 emitReplacement(*this, Call->getExprLoc(), 8556 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8557 } 8558 8559 //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===// 8560 void Sema::CheckMaxUnsignedZero(const CallExpr *Call, 8561 const FunctionDecl *FDecl) { 8562 if (!Call || !FDecl) return; 8563 8564 // Ignore template specializations and macros. 8565 if (inTemplateInstantiation()) return; 8566 if (Call->getExprLoc().isMacroID()) return; 8567 8568 // Only care about the one template argument, two function parameter std::max 8569 if (Call->getNumArgs() != 2) return; 8570 if (!IsStdFunction(FDecl, "max")) return; 8571 const auto * ArgList = FDecl->getTemplateSpecializationArgs(); 8572 if (!ArgList) return; 8573 if (ArgList->size() != 1) return; 8574 8575 // Check that template type argument is unsigned integer. 8576 const auto& TA = ArgList->get(0); 8577 if (TA.getKind() != TemplateArgument::Type) return; 8578 QualType ArgType = TA.getAsType(); 8579 if (!ArgType->isUnsignedIntegerType()) return; 8580 8581 // See if either argument is a literal zero. 8582 auto IsLiteralZeroArg = [](const Expr* E) -> bool { 8583 const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E); 8584 if (!MTE) return false; 8585 const auto *Num = dyn_cast<IntegerLiteral>(MTE->getSubExpr()); 8586 if (!Num) return false; 8587 if (Num->getValue() != 0) return false; 8588 return true; 8589 }; 8590 8591 const Expr *FirstArg = Call->getArg(0); 8592 const Expr *SecondArg = Call->getArg(1); 8593 const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg); 8594 const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg); 8595 8596 // Only warn when exactly one argument is zero. 8597 if (IsFirstArgZero == IsSecondArgZero) return; 8598 8599 SourceRange FirstRange = FirstArg->getSourceRange(); 8600 SourceRange SecondRange = SecondArg->getSourceRange(); 8601 8602 SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange; 8603 8604 Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero) 8605 << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange; 8606 8607 // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)". 8608 SourceRange RemovalRange; 8609 if (IsFirstArgZero) { 8610 RemovalRange = SourceRange(FirstRange.getBegin(), 8611 SecondRange.getBegin().getLocWithOffset(-1)); 8612 } else { 8613 RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()), 8614 SecondRange.getEnd()); 8615 } 8616 8617 Diag(Call->getExprLoc(), diag::note_remove_max_call) 8618 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()) 8619 << FixItHint::CreateRemoval(RemovalRange); 8620 } 8621 8622 //===--- CHECK: Standard memory functions ---------------------------------===// 8623 8624 /// Takes the expression passed to the size_t parameter of functions 8625 /// such as memcmp, strncat, etc and warns if it's a comparison. 8626 /// 8627 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. 8628 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, 8629 IdentifierInfo *FnName, 8630 SourceLocation FnLoc, 8631 SourceLocation RParenLoc) { 8632 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); 8633 if (!Size) 8634 return false; 8635 8636 // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||: 8637 if (!Size->isComparisonOp() && !Size->isLogicalOp()) 8638 return false; 8639 8640 SourceRange SizeRange = Size->getSourceRange(); 8641 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) 8642 << SizeRange << FnName; 8643 S.Diag(FnLoc, diag::note_memsize_comparison_paren) 8644 << FnName 8645 << FixItHint::CreateInsertion( 8646 S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")") 8647 << FixItHint::CreateRemoval(RParenLoc); 8648 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) 8649 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") 8650 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), 8651 ")"); 8652 8653 return true; 8654 } 8655 8656 /// Determine whether the given type is or contains a dynamic class type 8657 /// (e.g., whether it has a vtable). 8658 static const CXXRecordDecl *getContainedDynamicClass(QualType T, 8659 bool &IsContained) { 8660 // Look through array types while ignoring qualifiers. 8661 const Type *Ty = T->getBaseElementTypeUnsafe(); 8662 IsContained = false; 8663 8664 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 8665 RD = RD ? RD->getDefinition() : nullptr; 8666 if (!RD || RD->isInvalidDecl()) 8667 return nullptr; 8668 8669 if (RD->isDynamicClass()) 8670 return RD; 8671 8672 // Check all the fields. If any bases were dynamic, the class is dynamic. 8673 // It's impossible for a class to transitively contain itself by value, so 8674 // infinite recursion is impossible. 8675 for (auto *FD : RD->fields()) { 8676 bool SubContained; 8677 if (const CXXRecordDecl *ContainedRD = 8678 getContainedDynamicClass(FD->getType(), SubContained)) { 8679 IsContained = true; 8680 return ContainedRD; 8681 } 8682 } 8683 8684 return nullptr; 8685 } 8686 8687 static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) { 8688 if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 8689 if (Unary->getKind() == UETT_SizeOf) 8690 return Unary; 8691 return nullptr; 8692 } 8693 8694 /// If E is a sizeof expression, returns its argument expression, 8695 /// otherwise returns NULL. 8696 static const Expr *getSizeOfExprArg(const Expr *E) { 8697 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8698 if (!SizeOf->isArgumentType()) 8699 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); 8700 return nullptr; 8701 } 8702 8703 /// If E is a sizeof expression, returns its argument type. 8704 static QualType getSizeOfArgType(const Expr *E) { 8705 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8706 return SizeOf->getTypeOfArgument(); 8707 return QualType(); 8708 } 8709 8710 namespace { 8711 8712 struct SearchNonTrivialToInitializeField 8713 : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> { 8714 using Super = 8715 DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>; 8716 8717 SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {} 8718 8719 void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT, 8720 SourceLocation SL) { 8721 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8722 asDerived().visitArray(PDIK, AT, SL); 8723 return; 8724 } 8725 8726 Super::visitWithKind(PDIK, FT, SL); 8727 } 8728 8729 void visitARCStrong(QualType FT, SourceLocation SL) { 8730 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8731 } 8732 void visitARCWeak(QualType FT, SourceLocation SL) { 8733 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8734 } 8735 void visitStruct(QualType FT, SourceLocation SL) { 8736 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8737 visit(FD->getType(), FD->getLocation()); 8738 } 8739 void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK, 8740 const ArrayType *AT, SourceLocation SL) { 8741 visit(getContext().getBaseElementType(AT), SL); 8742 } 8743 void visitTrivial(QualType FT, SourceLocation SL) {} 8744 8745 static void diag(QualType RT, const Expr *E, Sema &S) { 8746 SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation()); 8747 } 8748 8749 ASTContext &getContext() { return S.getASTContext(); } 8750 8751 const Expr *E; 8752 Sema &S; 8753 }; 8754 8755 struct SearchNonTrivialToCopyField 8756 : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> { 8757 using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>; 8758 8759 SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {} 8760 8761 void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT, 8762 SourceLocation SL) { 8763 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8764 asDerived().visitArray(PCK, AT, SL); 8765 return; 8766 } 8767 8768 Super::visitWithKind(PCK, FT, SL); 8769 } 8770 8771 void visitARCStrong(QualType FT, SourceLocation SL) { 8772 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8773 } 8774 void visitARCWeak(QualType FT, SourceLocation SL) { 8775 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8776 } 8777 void visitStruct(QualType FT, SourceLocation SL) { 8778 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8779 visit(FD->getType(), FD->getLocation()); 8780 } 8781 void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT, 8782 SourceLocation SL) { 8783 visit(getContext().getBaseElementType(AT), SL); 8784 } 8785 void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT, 8786 SourceLocation SL) {} 8787 void visitTrivial(QualType FT, SourceLocation SL) {} 8788 void visitVolatileTrivial(QualType FT, SourceLocation SL) {} 8789 8790 static void diag(QualType RT, const Expr *E, Sema &S) { 8791 SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation()); 8792 } 8793 8794 ASTContext &getContext() { return S.getASTContext(); } 8795 8796 const Expr *E; 8797 Sema &S; 8798 }; 8799 8800 } 8801 8802 /// Detect if \c SizeofExpr is likely to calculate the sizeof an object. 8803 static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) { 8804 SizeofExpr = SizeofExpr->IgnoreParenImpCasts(); 8805 8806 if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) { 8807 if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add) 8808 return false; 8809 8810 return doesExprLikelyComputeSize(BO->getLHS()) || 8811 doesExprLikelyComputeSize(BO->getRHS()); 8812 } 8813 8814 return getAsSizeOfExpr(SizeofExpr) != nullptr; 8815 } 8816 8817 /// Check if the ArgLoc originated from a macro passed to the call at CallLoc. 8818 /// 8819 /// \code 8820 /// #define MACRO 0 8821 /// foo(MACRO); 8822 /// foo(0); 8823 /// \endcode 8824 /// 8825 /// This should return true for the first call to foo, but not for the second 8826 /// (regardless of whether foo is a macro or function). 8827 static bool isArgumentExpandedFromMacro(SourceManager &SM, 8828 SourceLocation CallLoc, 8829 SourceLocation ArgLoc) { 8830 if (!CallLoc.isMacroID()) 8831 return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc); 8832 8833 return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) != 8834 SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc)); 8835 } 8836 8837 /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the 8838 /// last two arguments transposed. 8839 static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) { 8840 if (BId != Builtin::BImemset && BId != Builtin::BIbzero) 8841 return; 8842 8843 const Expr *SizeArg = 8844 Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts(); 8845 8846 auto isLiteralZero = [](const Expr *E) { 8847 return (isa<IntegerLiteral>(E) && 8848 cast<IntegerLiteral>(E)->getValue() == 0) || 8849 (isa<CharacterLiteral>(E) && 8850 cast<CharacterLiteral>(E)->getValue() == 0); 8851 }; 8852 8853 // If we're memsetting or bzeroing 0 bytes, then this is likely an error. 8854 SourceLocation CallLoc = Call->getRParenLoc(); 8855 SourceManager &SM = S.getSourceManager(); 8856 if (isLiteralZero(SizeArg) && 8857 !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) { 8858 8859 SourceLocation DiagLoc = SizeArg->getExprLoc(); 8860 8861 // Some platforms #define bzero to __builtin_memset. See if this is the 8862 // case, and if so, emit a better diagnostic. 8863 if (BId == Builtin::BIbzero || 8864 (CallLoc.isMacroID() && Lexer::getImmediateMacroName( 8865 CallLoc, SM, S.getLangOpts()) == "bzero")) { 8866 S.Diag(DiagLoc, diag::warn_suspicious_bzero_size); 8867 S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence); 8868 } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) { 8869 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0; 8870 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0; 8871 } 8872 return; 8873 } 8874 8875 // If the second argument to a memset is a sizeof expression and the third 8876 // isn't, this is also likely an error. This should catch 8877 // 'memset(buf, sizeof(buf), 0xff)'. 8878 if (BId == Builtin::BImemset && 8879 doesExprLikelyComputeSize(Call->getArg(1)) && 8880 !doesExprLikelyComputeSize(Call->getArg(2))) { 8881 SourceLocation DiagLoc = Call->getArg(1)->getExprLoc(); 8882 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1; 8883 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1; 8884 return; 8885 } 8886 } 8887 8888 void Sema::CheckMemaccessArguments(const CallExpr *Call, 8889 unsigned BId, 8890 IdentifierInfo *FnName) { 8891 assert(BId != 0); 8892 8893 // It is possible to have a non-standard definition of memset. Validate 8894 // we have enough arguments, and if not, abort further checking. 8895 unsigned ExpectedNumArgs = 8896 (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3); 8897 if (Call->getNumArgs() < ExpectedNumArgs) 8898 return; 8899 8900 unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero || 8901 BId == Builtin::BIstrndup ? 1 : 2); 8902 unsigned LenArg = 8903 (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2); 8904 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); 8905 8906 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, 8907 Call->getBeginLoc(), Call->getRParenLoc())) 8908 return; 8909 8910 // Catch cases like 'memset(buf, sizeof(buf), 0)'. 8911 CheckMemaccessSize(*this, BId, Call); 8912 8913 // We have special checking when the length is a sizeof expression. 8914 QualType SizeOfArgTy = getSizeOfArgType(LenExpr); 8915 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); 8916 llvm::FoldingSetNodeID SizeOfArgID; 8917 8918 // Although widely used, 'bzero' is not a standard function. Be more strict 8919 // with the argument types before allowing diagnostics and only allow the 8920 // form bzero(ptr, sizeof(...)). 8921 QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8922 if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>()) 8923 return; 8924 8925 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { 8926 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); 8927 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); 8928 8929 QualType DestTy = Dest->getType(); 8930 QualType PointeeTy; 8931 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { 8932 PointeeTy = DestPtrTy->getPointeeType(); 8933 8934 // Never warn about void type pointers. This can be used to suppress 8935 // false positives. 8936 if (PointeeTy->isVoidType()) 8937 continue; 8938 8939 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by 8940 // actually comparing the expressions for equality. Because computing the 8941 // expression IDs can be expensive, we only do this if the diagnostic is 8942 // enabled. 8943 if (SizeOfArg && 8944 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, 8945 SizeOfArg->getExprLoc())) { 8946 // We only compute IDs for expressions if the warning is enabled, and 8947 // cache the sizeof arg's ID. 8948 if (SizeOfArgID == llvm::FoldingSetNodeID()) 8949 SizeOfArg->Profile(SizeOfArgID, Context, true); 8950 llvm::FoldingSetNodeID DestID; 8951 Dest->Profile(DestID, Context, true); 8952 if (DestID == SizeOfArgID) { 8953 // TODO: For strncpy() and friends, this could suggest sizeof(dst) 8954 // over sizeof(src) as well. 8955 unsigned ActionIdx = 0; // Default is to suggest dereferencing. 8956 StringRef ReadableName = FnName->getName(); 8957 8958 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) 8959 if (UnaryOp->getOpcode() == UO_AddrOf) 8960 ActionIdx = 1; // If its an address-of operator, just remove it. 8961 if (!PointeeTy->isIncompleteType() && 8962 (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) 8963 ActionIdx = 2; // If the pointee's size is sizeof(char), 8964 // suggest an explicit length. 8965 8966 // If the function is defined as a builtin macro, do not show macro 8967 // expansion. 8968 SourceLocation SL = SizeOfArg->getExprLoc(); 8969 SourceRange DSR = Dest->getSourceRange(); 8970 SourceRange SSR = SizeOfArg->getSourceRange(); 8971 SourceManager &SM = getSourceManager(); 8972 8973 if (SM.isMacroArgExpansion(SL)) { 8974 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); 8975 SL = SM.getSpellingLoc(SL); 8976 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), 8977 SM.getSpellingLoc(DSR.getEnd())); 8978 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), 8979 SM.getSpellingLoc(SSR.getEnd())); 8980 } 8981 8982 DiagRuntimeBehavior(SL, SizeOfArg, 8983 PDiag(diag::warn_sizeof_pointer_expr_memaccess) 8984 << ReadableName 8985 << PointeeTy 8986 << DestTy 8987 << DSR 8988 << SSR); 8989 DiagRuntimeBehavior(SL, SizeOfArg, 8990 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) 8991 << ActionIdx 8992 << SSR); 8993 8994 break; 8995 } 8996 } 8997 8998 // Also check for cases where the sizeof argument is the exact same 8999 // type as the memory argument, and where it points to a user-defined 9000 // record type. 9001 if (SizeOfArgTy != QualType()) { 9002 if (PointeeTy->isRecordType() && 9003 Context.typesAreCompatible(SizeOfArgTy, DestTy)) { 9004 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, 9005 PDiag(diag::warn_sizeof_pointer_type_memaccess) 9006 << FnName << SizeOfArgTy << ArgIdx 9007 << PointeeTy << Dest->getSourceRange() 9008 << LenExpr->getSourceRange()); 9009 break; 9010 } 9011 } 9012 } else if (DestTy->isArrayType()) { 9013 PointeeTy = DestTy; 9014 } 9015 9016 if (PointeeTy == QualType()) 9017 continue; 9018 9019 // Always complain about dynamic classes. 9020 bool IsContained; 9021 if (const CXXRecordDecl *ContainedRD = 9022 getContainedDynamicClass(PointeeTy, IsContained)) { 9023 9024 unsigned OperationType = 0; 9025 const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp; 9026 // "overwritten" if we're warning about the destination for any call 9027 // but memcmp; otherwise a verb appropriate to the call. 9028 if (ArgIdx != 0 || IsCmp) { 9029 if (BId == Builtin::BImemcpy) 9030 OperationType = 1; 9031 else if(BId == Builtin::BImemmove) 9032 OperationType = 2; 9033 else if (IsCmp) 9034 OperationType = 3; 9035 } 9036 9037 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9038 PDiag(diag::warn_dyn_class_memaccess) 9039 << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName 9040 << IsContained << ContainedRD << OperationType 9041 << Call->getCallee()->getSourceRange()); 9042 } else if (PointeeTy.hasNonTrivialObjCLifetime() && 9043 BId != Builtin::BImemset) 9044 DiagRuntimeBehavior( 9045 Dest->getExprLoc(), Dest, 9046 PDiag(diag::warn_arc_object_memaccess) 9047 << ArgIdx << FnName << PointeeTy 9048 << Call->getCallee()->getSourceRange()); 9049 else if (const auto *RT = PointeeTy->getAs<RecordType>()) { 9050 9051 // FIXME: Do not consider incomplete types even though they may be 9052 // completed later. GCC does not diagnose such code, but we may want to 9053 // consider diagnosing it in the future, perhaps under a different, but 9054 // related, diagnostic group. 9055 bool MayBeTriviallyCopyableCXXRecord = 9056 RT->isIncompleteType() || 9057 RT->desugar().isTriviallyCopyableType(Context); 9058 9059 if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 9060 RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) { 9061 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9062 PDiag(diag::warn_cstruct_memaccess) 9063 << ArgIdx << FnName << PointeeTy << 0); 9064 SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this); 9065 } else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 9066 !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) { 9067 // FIXME: Limiting this warning to dest argument until we decide 9068 // whether it's valid for source argument too. 9069 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9070 PDiag(diag::warn_cxxstruct_memaccess) 9071 << FnName << PointeeTy); 9072 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 9073 RT->getDecl()->isNonTrivialToPrimitiveCopy()) { 9074 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9075 PDiag(diag::warn_cstruct_memaccess) 9076 << ArgIdx << FnName << PointeeTy << 1); 9077 SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this); 9078 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 9079 !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) { 9080 // FIXME: Limiting this warning to dest argument until we decide 9081 // whether it's valid for source argument too. 9082 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9083 PDiag(diag::warn_cxxstruct_memaccess) 9084 << FnName << PointeeTy); 9085 } else { 9086 continue; 9087 } 9088 } else 9089 continue; 9090 9091 DiagRuntimeBehavior( 9092 Dest->getExprLoc(), Dest, 9093 PDiag(diag::note_bad_memaccess_silence) 9094 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); 9095 break; 9096 } 9097 } 9098 9099 // A little helper routine: ignore addition and subtraction of integer literals. 9100 // This intentionally does not ignore all integer constant expressions because 9101 // we don't want to remove sizeof(). 9102 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { 9103 Ex = Ex->IgnoreParenCasts(); 9104 9105 while (true) { 9106 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); 9107 if (!BO || !BO->isAdditiveOp()) 9108 break; 9109 9110 const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); 9111 const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); 9112 9113 if (isa<IntegerLiteral>(RHS)) 9114 Ex = LHS; 9115 else if (isa<IntegerLiteral>(LHS)) 9116 Ex = RHS; 9117 else 9118 break; 9119 } 9120 9121 return Ex; 9122 } 9123 9124 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, 9125 ASTContext &Context) { 9126 // Only handle constant-sized or VLAs, but not flexible members. 9127 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { 9128 // Only issue the FIXIT for arrays of size > 1. 9129 if (CAT->getZExtSize() <= 1) 9130 return false; 9131 } else if (!Ty->isVariableArrayType()) { 9132 return false; 9133 } 9134 return true; 9135 } 9136 9137 void Sema::CheckStrlcpycatArguments(const CallExpr *Call, 9138 IdentifierInfo *FnName) { 9139 9140 // Don't crash if the user has the wrong number of arguments 9141 unsigned NumArgs = Call->getNumArgs(); 9142 if ((NumArgs != 3) && (NumArgs != 4)) 9143 return; 9144 9145 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); 9146 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); 9147 const Expr *CompareWithSrc = nullptr; 9148 9149 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, 9150 Call->getBeginLoc(), Call->getRParenLoc())) 9151 return; 9152 9153 // Look for 'strlcpy(dst, x, sizeof(x))' 9154 if (const Expr *Ex = getSizeOfExprArg(SizeArg)) 9155 CompareWithSrc = Ex; 9156 else { 9157 // Look for 'strlcpy(dst, x, strlen(x))' 9158 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { 9159 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && 9160 SizeCall->getNumArgs() == 1) 9161 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); 9162 } 9163 } 9164 9165 if (!CompareWithSrc) 9166 return; 9167 9168 // Determine if the argument to sizeof/strlen is equal to the source 9169 // argument. In principle there's all kinds of things you could do 9170 // here, for instance creating an == expression and evaluating it with 9171 // EvaluateAsBooleanCondition, but this uses a more direct technique: 9172 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); 9173 if (!SrcArgDRE) 9174 return; 9175 9176 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); 9177 if (!CompareWithSrcDRE || 9178 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) 9179 return; 9180 9181 const Expr *OriginalSizeArg = Call->getArg(2); 9182 Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size) 9183 << OriginalSizeArg->getSourceRange() << FnName; 9184 9185 // Output a FIXIT hint if the destination is an array (rather than a 9186 // pointer to an array). This could be enhanced to handle some 9187 // pointers if we know the actual size, like if DstArg is 'array+2' 9188 // we could say 'sizeof(array)-2'. 9189 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); 9190 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) 9191 return; 9192 9193 SmallString<128> sizeString; 9194 llvm::raw_svector_ostream OS(sizeString); 9195 OS << "sizeof("; 9196 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9197 OS << ")"; 9198 9199 Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size) 9200 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), 9201 OS.str()); 9202 } 9203 9204 /// Check if two expressions refer to the same declaration. 9205 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { 9206 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) 9207 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) 9208 return D1->getDecl() == D2->getDecl(); 9209 return false; 9210 } 9211 9212 static const Expr *getStrlenExprArg(const Expr *E) { 9213 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 9214 const FunctionDecl *FD = CE->getDirectCallee(); 9215 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) 9216 return nullptr; 9217 return CE->getArg(0)->IgnoreParenCasts(); 9218 } 9219 return nullptr; 9220 } 9221 9222 void Sema::CheckStrncatArguments(const CallExpr *CE, 9223 IdentifierInfo *FnName) { 9224 // Don't crash if the user has the wrong number of arguments. 9225 if (CE->getNumArgs() < 3) 9226 return; 9227 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); 9228 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); 9229 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); 9230 9231 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(), 9232 CE->getRParenLoc())) 9233 return; 9234 9235 // Identify common expressions, which are wrongly used as the size argument 9236 // to strncat and may lead to buffer overflows. 9237 unsigned PatternType = 0; 9238 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { 9239 // - sizeof(dst) 9240 if (referToTheSameDecl(SizeOfArg, DstArg)) 9241 PatternType = 1; 9242 // - sizeof(src) 9243 else if (referToTheSameDecl(SizeOfArg, SrcArg)) 9244 PatternType = 2; 9245 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { 9246 if (BE->getOpcode() == BO_Sub) { 9247 const Expr *L = BE->getLHS()->IgnoreParenCasts(); 9248 const Expr *R = BE->getRHS()->IgnoreParenCasts(); 9249 // - sizeof(dst) - strlen(dst) 9250 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && 9251 referToTheSameDecl(DstArg, getStrlenExprArg(R))) 9252 PatternType = 1; 9253 // - sizeof(src) - (anything) 9254 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) 9255 PatternType = 2; 9256 } 9257 } 9258 9259 if (PatternType == 0) 9260 return; 9261 9262 // Generate the diagnostic. 9263 SourceLocation SL = LenArg->getBeginLoc(); 9264 SourceRange SR = LenArg->getSourceRange(); 9265 SourceManager &SM = getSourceManager(); 9266 9267 // If the function is defined as a builtin macro, do not show macro expansion. 9268 if (SM.isMacroArgExpansion(SL)) { 9269 SL = SM.getSpellingLoc(SL); 9270 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), 9271 SM.getSpellingLoc(SR.getEnd())); 9272 } 9273 9274 // Check if the destination is an array (rather than a pointer to an array). 9275 QualType DstTy = DstArg->getType(); 9276 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, 9277 Context); 9278 if (!isKnownSizeArray) { 9279 if (PatternType == 1) 9280 Diag(SL, diag::warn_strncat_wrong_size) << SR; 9281 else 9282 Diag(SL, diag::warn_strncat_src_size) << SR; 9283 return; 9284 } 9285 9286 if (PatternType == 1) 9287 Diag(SL, diag::warn_strncat_large_size) << SR; 9288 else 9289 Diag(SL, diag::warn_strncat_src_size) << SR; 9290 9291 SmallString<128> sizeString; 9292 llvm::raw_svector_ostream OS(sizeString); 9293 OS << "sizeof("; 9294 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9295 OS << ") - "; 9296 OS << "strlen("; 9297 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9298 OS << ") - 1"; 9299 9300 Diag(SL, diag::note_strncat_wrong_size) 9301 << FixItHint::CreateReplacement(SR, OS.str()); 9302 } 9303 9304 namespace { 9305 void CheckFreeArgumentsOnLvalue(Sema &S, const std::string &CalleeName, 9306 const UnaryOperator *UnaryExpr, const Decl *D) { 9307 if (isa<FieldDecl, FunctionDecl, VarDecl>(D)) { 9308 S.Diag(UnaryExpr->getBeginLoc(), diag::warn_free_nonheap_object) 9309 << CalleeName << 0 /*object: */ << cast<NamedDecl>(D); 9310 return; 9311 } 9312 } 9313 9314 void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName, 9315 const UnaryOperator *UnaryExpr) { 9316 if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) { 9317 const Decl *D = Lvalue->getDecl(); 9318 if (isa<DeclaratorDecl>(D)) 9319 if (!dyn_cast<DeclaratorDecl>(D)->getType()->isReferenceType()) 9320 return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D); 9321 } 9322 9323 if (const auto *Lvalue = dyn_cast<MemberExpr>(UnaryExpr->getSubExpr())) 9324 return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, 9325 Lvalue->getMemberDecl()); 9326 } 9327 9328 void CheckFreeArgumentsPlus(Sema &S, const std::string &CalleeName, 9329 const UnaryOperator *UnaryExpr) { 9330 const auto *Lambda = dyn_cast<LambdaExpr>( 9331 UnaryExpr->getSubExpr()->IgnoreImplicitAsWritten()->IgnoreParens()); 9332 if (!Lambda) 9333 return; 9334 9335 S.Diag(Lambda->getBeginLoc(), diag::warn_free_nonheap_object) 9336 << CalleeName << 2 /*object: lambda expression*/; 9337 } 9338 9339 void CheckFreeArgumentsStackArray(Sema &S, const std::string &CalleeName, 9340 const DeclRefExpr *Lvalue) { 9341 const auto *Var = dyn_cast<VarDecl>(Lvalue->getDecl()); 9342 if (Var == nullptr) 9343 return; 9344 9345 S.Diag(Lvalue->getBeginLoc(), diag::warn_free_nonheap_object) 9346 << CalleeName << 0 /*object: */ << Var; 9347 } 9348 9349 void CheckFreeArgumentsCast(Sema &S, const std::string &CalleeName, 9350 const CastExpr *Cast) { 9351 SmallString<128> SizeString; 9352 llvm::raw_svector_ostream OS(SizeString); 9353 9354 clang::CastKind Kind = Cast->getCastKind(); 9355 if (Kind == clang::CK_BitCast && 9356 !Cast->getSubExpr()->getType()->isFunctionPointerType()) 9357 return; 9358 if (Kind == clang::CK_IntegralToPointer && 9359 !isa<IntegerLiteral>( 9360 Cast->getSubExpr()->IgnoreParenImpCasts()->IgnoreParens())) 9361 return; 9362 9363 switch (Cast->getCastKind()) { 9364 case clang::CK_BitCast: 9365 case clang::CK_IntegralToPointer: 9366 case clang::CK_FunctionToPointerDecay: 9367 OS << '\''; 9368 Cast->printPretty(OS, nullptr, S.getPrintingPolicy()); 9369 OS << '\''; 9370 break; 9371 default: 9372 return; 9373 } 9374 9375 S.Diag(Cast->getBeginLoc(), diag::warn_free_nonheap_object) 9376 << CalleeName << 0 /*object: */ << OS.str(); 9377 } 9378 } // namespace 9379 9380 void Sema::CheckFreeArguments(const CallExpr *E) { 9381 const std::string CalleeName = 9382 cast<FunctionDecl>(E->getCalleeDecl())->getQualifiedNameAsString(); 9383 9384 { // Prefer something that doesn't involve a cast to make things simpler. 9385 const Expr *Arg = E->getArg(0)->IgnoreParenCasts(); 9386 if (const auto *UnaryExpr = dyn_cast<UnaryOperator>(Arg)) 9387 switch (UnaryExpr->getOpcode()) { 9388 case UnaryOperator::Opcode::UO_AddrOf: 9389 return CheckFreeArgumentsAddressof(*this, CalleeName, UnaryExpr); 9390 case UnaryOperator::Opcode::UO_Plus: 9391 return CheckFreeArgumentsPlus(*this, CalleeName, UnaryExpr); 9392 default: 9393 break; 9394 } 9395 9396 if (const auto *Lvalue = dyn_cast<DeclRefExpr>(Arg)) 9397 if (Lvalue->getType()->isArrayType()) 9398 return CheckFreeArgumentsStackArray(*this, CalleeName, Lvalue); 9399 9400 if (const auto *Label = dyn_cast<AddrLabelExpr>(Arg)) { 9401 Diag(Label->getBeginLoc(), diag::warn_free_nonheap_object) 9402 << CalleeName << 0 /*object: */ << Label->getLabel()->getIdentifier(); 9403 return; 9404 } 9405 9406 if (isa<BlockExpr>(Arg)) { 9407 Diag(Arg->getBeginLoc(), diag::warn_free_nonheap_object) 9408 << CalleeName << 1 /*object: block*/; 9409 return; 9410 } 9411 } 9412 // Maybe the cast was important, check after the other cases. 9413 if (const auto *Cast = dyn_cast<CastExpr>(E->getArg(0))) 9414 return CheckFreeArgumentsCast(*this, CalleeName, Cast); 9415 } 9416 9417 void 9418 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, 9419 SourceLocation ReturnLoc, 9420 bool isObjCMethod, 9421 const AttrVec *Attrs, 9422 const FunctionDecl *FD) { 9423 // Check if the return value is null but should not be. 9424 if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || 9425 (!isObjCMethod && isNonNullType(lhsType))) && 9426 CheckNonNullExpr(*this, RetValExp)) 9427 Diag(ReturnLoc, diag::warn_null_ret) 9428 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); 9429 9430 // C++11 [basic.stc.dynamic.allocation]p4: 9431 // If an allocation function declared with a non-throwing 9432 // exception-specification fails to allocate storage, it shall return 9433 // a null pointer. Any other allocation function that fails to allocate 9434 // storage shall indicate failure only by throwing an exception [...] 9435 if (FD) { 9436 OverloadedOperatorKind Op = FD->getOverloadedOperator(); 9437 if (Op == OO_New || Op == OO_Array_New) { 9438 const FunctionProtoType *Proto 9439 = FD->getType()->castAs<FunctionProtoType>(); 9440 if (!Proto->isNothrow(/*ResultIfDependent*/true) && 9441 CheckNonNullExpr(*this, RetValExp)) 9442 Diag(ReturnLoc, diag::warn_operator_new_returns_null) 9443 << FD << getLangOpts().CPlusPlus11; 9444 } 9445 } 9446 9447 if (RetValExp && RetValExp->getType()->isWebAssemblyTableType()) { 9448 Diag(ReturnLoc, diag::err_wasm_table_art) << 1; 9449 } 9450 9451 // PPC MMA non-pointer types are not allowed as return type. Checking the type 9452 // here prevent the user from using a PPC MMA type as trailing return type. 9453 if (Context.getTargetInfo().getTriple().isPPC64()) 9454 PPC().CheckPPCMMAType(RetValExp->getType(), ReturnLoc); 9455 } 9456 9457 void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS, 9458 BinaryOperatorKind Opcode) { 9459 if (!BinaryOperator::isEqualityOp(Opcode)) 9460 return; 9461 9462 // Match and capture subexpressions such as "(float) X == 0.1". 9463 FloatingLiteral *FPLiteral; 9464 CastExpr *FPCast; 9465 auto getCastAndLiteral = [&FPLiteral, &FPCast](Expr *L, Expr *R) { 9466 FPLiteral = dyn_cast<FloatingLiteral>(L->IgnoreParens()); 9467 FPCast = dyn_cast<CastExpr>(R->IgnoreParens()); 9468 return FPLiteral && FPCast; 9469 }; 9470 9471 if (getCastAndLiteral(LHS, RHS) || getCastAndLiteral(RHS, LHS)) { 9472 auto *SourceTy = FPCast->getSubExpr()->getType()->getAs<BuiltinType>(); 9473 auto *TargetTy = FPLiteral->getType()->getAs<BuiltinType>(); 9474 if (SourceTy && TargetTy && SourceTy->isFloatingPoint() && 9475 TargetTy->isFloatingPoint()) { 9476 bool Lossy; 9477 llvm::APFloat TargetC = FPLiteral->getValue(); 9478 TargetC.convert(Context.getFloatTypeSemantics(QualType(SourceTy, 0)), 9479 llvm::APFloat::rmNearestTiesToEven, &Lossy); 9480 if (Lossy) { 9481 // If the literal cannot be represented in the source type, then a 9482 // check for == is always false and check for != is always true. 9483 Diag(Loc, diag::warn_float_compare_literal) 9484 << (Opcode == BO_EQ) << QualType(SourceTy, 0) 9485 << LHS->getSourceRange() << RHS->getSourceRange(); 9486 return; 9487 } 9488 } 9489 } 9490 9491 // Match a more general floating-point equality comparison (-Wfloat-equal). 9492 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); 9493 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); 9494 9495 // Special case: check for x == x (which is OK). 9496 // Do not emit warnings for such cases. 9497 if (auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) 9498 if (auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) 9499 if (DRL->getDecl() == DRR->getDecl()) 9500 return; 9501 9502 // Special case: check for comparisons against literals that can be exactly 9503 // represented by APFloat. In such cases, do not emit a warning. This 9504 // is a heuristic: often comparison against such literals are used to 9505 // detect if a value in a variable has not changed. This clearly can 9506 // lead to false negatives. 9507 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { 9508 if (FLL->isExact()) 9509 return; 9510 } else 9511 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) 9512 if (FLR->isExact()) 9513 return; 9514 9515 // Check for comparisons with builtin types. 9516 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) 9517 if (CL->getBuiltinCallee()) 9518 return; 9519 9520 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) 9521 if (CR->getBuiltinCallee()) 9522 return; 9523 9524 // Emit the diagnostic. 9525 Diag(Loc, diag::warn_floatingpoint_eq) 9526 << LHS->getSourceRange() << RHS->getSourceRange(); 9527 } 9528 9529 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// 9530 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// 9531 9532 namespace { 9533 9534 /// Structure recording the 'active' range of an integer-valued 9535 /// expression. 9536 struct IntRange { 9537 /// The number of bits active in the int. Note that this includes exactly one 9538 /// sign bit if !NonNegative. 9539 unsigned Width; 9540 9541 /// True if the int is known not to have negative values. If so, all leading 9542 /// bits before Width are known zero, otherwise they are known to be the 9543 /// same as the MSB within Width. 9544 bool NonNegative; 9545 9546 IntRange(unsigned Width, bool NonNegative) 9547 : Width(Width), NonNegative(NonNegative) {} 9548 9549 /// Number of bits excluding the sign bit. 9550 unsigned valueBits() const { 9551 return NonNegative ? Width : Width - 1; 9552 } 9553 9554 /// Returns the range of the bool type. 9555 static IntRange forBoolType() { 9556 return IntRange(1, true); 9557 } 9558 9559 /// Returns the range of an opaque value of the given integral type. 9560 static IntRange forValueOfType(ASTContext &C, QualType T) { 9561 return forValueOfCanonicalType(C, 9562 T->getCanonicalTypeInternal().getTypePtr()); 9563 } 9564 9565 /// Returns the range of an opaque value of a canonical integral type. 9566 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { 9567 assert(T->isCanonicalUnqualified()); 9568 9569 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9570 T = VT->getElementType().getTypePtr(); 9571 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9572 T = CT->getElementType().getTypePtr(); 9573 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9574 T = AT->getValueType().getTypePtr(); 9575 9576 if (!C.getLangOpts().CPlusPlus) { 9577 // For enum types in C code, use the underlying datatype. 9578 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9579 T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr(); 9580 } else if (const EnumType *ET = dyn_cast<EnumType>(T)) { 9581 // For enum types in C++, use the known bit width of the enumerators. 9582 EnumDecl *Enum = ET->getDecl(); 9583 // In C++11, enums can have a fixed underlying type. Use this type to 9584 // compute the range. 9585 if (Enum->isFixed()) { 9586 return IntRange(C.getIntWidth(QualType(T, 0)), 9587 !ET->isSignedIntegerOrEnumerationType()); 9588 } 9589 9590 unsigned NumPositive = Enum->getNumPositiveBits(); 9591 unsigned NumNegative = Enum->getNumNegativeBits(); 9592 9593 if (NumNegative == 0) 9594 return IntRange(NumPositive, true/*NonNegative*/); 9595 else 9596 return IntRange(std::max(NumPositive + 1, NumNegative), 9597 false/*NonNegative*/); 9598 } 9599 9600 if (const auto *EIT = dyn_cast<BitIntType>(T)) 9601 return IntRange(EIT->getNumBits(), EIT->isUnsigned()); 9602 9603 const BuiltinType *BT = cast<BuiltinType>(T); 9604 assert(BT->isInteger()); 9605 9606 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9607 } 9608 9609 /// Returns the "target" range of a canonical integral type, i.e. 9610 /// the range of values expressible in the type. 9611 /// 9612 /// This matches forValueOfCanonicalType except that enums have the 9613 /// full range of their type, not the range of their enumerators. 9614 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { 9615 assert(T->isCanonicalUnqualified()); 9616 9617 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9618 T = VT->getElementType().getTypePtr(); 9619 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9620 T = CT->getElementType().getTypePtr(); 9621 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9622 T = AT->getValueType().getTypePtr(); 9623 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9624 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); 9625 9626 if (const auto *EIT = dyn_cast<BitIntType>(T)) 9627 return IntRange(EIT->getNumBits(), EIT->isUnsigned()); 9628 9629 const BuiltinType *BT = cast<BuiltinType>(T); 9630 assert(BT->isInteger()); 9631 9632 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9633 } 9634 9635 /// Returns the supremum of two ranges: i.e. their conservative merge. 9636 static IntRange join(IntRange L, IntRange R) { 9637 bool Unsigned = L.NonNegative && R.NonNegative; 9638 return IntRange(std::max(L.valueBits(), R.valueBits()) + !Unsigned, 9639 L.NonNegative && R.NonNegative); 9640 } 9641 9642 /// Return the range of a bitwise-AND of the two ranges. 9643 static IntRange bit_and(IntRange L, IntRange R) { 9644 unsigned Bits = std::max(L.Width, R.Width); 9645 bool NonNegative = false; 9646 if (L.NonNegative) { 9647 Bits = std::min(Bits, L.Width); 9648 NonNegative = true; 9649 } 9650 if (R.NonNegative) { 9651 Bits = std::min(Bits, R.Width); 9652 NonNegative = true; 9653 } 9654 return IntRange(Bits, NonNegative); 9655 } 9656 9657 /// Return the range of a sum of the two ranges. 9658 static IntRange sum(IntRange L, IntRange R) { 9659 bool Unsigned = L.NonNegative && R.NonNegative; 9660 return IntRange(std::max(L.valueBits(), R.valueBits()) + 1 + !Unsigned, 9661 Unsigned); 9662 } 9663 9664 /// Return the range of a difference of the two ranges. 9665 static IntRange difference(IntRange L, IntRange R) { 9666 // We need a 1-bit-wider range if: 9667 // 1) LHS can be negative: least value can be reduced. 9668 // 2) RHS can be negative: greatest value can be increased. 9669 bool CanWiden = !L.NonNegative || !R.NonNegative; 9670 bool Unsigned = L.NonNegative && R.Width == 0; 9671 return IntRange(std::max(L.valueBits(), R.valueBits()) + CanWiden + 9672 !Unsigned, 9673 Unsigned); 9674 } 9675 9676 /// Return the range of a product of the two ranges. 9677 static IntRange product(IntRange L, IntRange R) { 9678 // If both LHS and RHS can be negative, we can form 9679 // -2^L * -2^R = 2^(L + R) 9680 // which requires L + R + 1 value bits to represent. 9681 bool CanWiden = !L.NonNegative && !R.NonNegative; 9682 bool Unsigned = L.NonNegative && R.NonNegative; 9683 return IntRange(L.valueBits() + R.valueBits() + CanWiden + !Unsigned, 9684 Unsigned); 9685 } 9686 9687 /// Return the range of a remainder operation between the two ranges. 9688 static IntRange rem(IntRange L, IntRange R) { 9689 // The result of a remainder can't be larger than the result of 9690 // either side. The sign of the result is the sign of the LHS. 9691 bool Unsigned = L.NonNegative; 9692 return IntRange(std::min(L.valueBits(), R.valueBits()) + !Unsigned, 9693 Unsigned); 9694 } 9695 }; 9696 9697 } // namespace 9698 9699 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, 9700 unsigned MaxWidth) { 9701 if (value.isSigned() && value.isNegative()) 9702 return IntRange(value.getSignificantBits(), false); 9703 9704 if (value.getBitWidth() > MaxWidth) 9705 value = value.trunc(MaxWidth); 9706 9707 // isNonNegative() just checks the sign bit without considering 9708 // signedness. 9709 return IntRange(value.getActiveBits(), true); 9710 } 9711 9712 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, 9713 unsigned MaxWidth) { 9714 if (result.isInt()) 9715 return GetValueRange(C, result.getInt(), MaxWidth); 9716 9717 if (result.isVector()) { 9718 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); 9719 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { 9720 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); 9721 R = IntRange::join(R, El); 9722 } 9723 return R; 9724 } 9725 9726 if (result.isComplexInt()) { 9727 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); 9728 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); 9729 return IntRange::join(R, I); 9730 } 9731 9732 // This can happen with lossless casts to intptr_t of "based" lvalues. 9733 // Assume it might use arbitrary bits. 9734 // FIXME: The only reason we need to pass the type in here is to get 9735 // the sign right on this one case. It would be nice if APValue 9736 // preserved this. 9737 assert(result.isLValue() || result.isAddrLabelDiff()); 9738 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); 9739 } 9740 9741 static QualType GetExprType(const Expr *E) { 9742 QualType Ty = E->getType(); 9743 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) 9744 Ty = AtomicRHS->getValueType(); 9745 return Ty; 9746 } 9747 9748 /// Attempts to estimate an approximate range for the given integer expression. 9749 /// Returns a range if successful, otherwise it returns \c std::nullopt if a 9750 /// reliable estimation cannot be determined. 9751 /// 9752 /// \param MaxWidth The width to which the value will be truncated. 9753 /// \param InConstantContext If \c true, interpret the expression within a 9754 /// constant context. 9755 /// \param Approximate If \c true, provide a likely range of values by assuming 9756 /// that arithmetic on narrower types remains within those types. 9757 /// If \c false, return a range that includes all possible values 9758 /// resulting from the expression. 9759 /// \returns A range of values that the expression might take, or 9760 /// std::nullopt if a reliable estimation cannot be determined. 9761 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E, 9762 unsigned MaxWidth, 9763 bool InConstantContext, 9764 bool Approximate) { 9765 E = E->IgnoreParens(); 9766 9767 // Try a full evaluation first. 9768 Expr::EvalResult result; 9769 if (E->EvaluateAsRValue(result, C, InConstantContext)) 9770 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); 9771 9772 // I think we only want to look through implicit casts here; if the 9773 // user has an explicit widening cast, we should treat the value as 9774 // being of the new, wider type. 9775 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) { 9776 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) 9777 return TryGetExprRange(C, CE->getSubExpr(), MaxWidth, InConstantContext, 9778 Approximate); 9779 9780 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); 9781 9782 bool isIntegerCast = CE->getCastKind() == CK_IntegralCast || 9783 CE->getCastKind() == CK_BooleanToSignedIntegral; 9784 9785 // Assume that non-integer casts can span the full range of the type. 9786 if (!isIntegerCast) 9787 return OutputTypeRange; 9788 9789 std::optional<IntRange> SubRange = TryGetExprRange( 9790 C, CE->getSubExpr(), std::min(MaxWidth, OutputTypeRange.Width), 9791 InConstantContext, Approximate); 9792 if (!SubRange) 9793 return std::nullopt; 9794 9795 // Bail out if the subexpr's range is as wide as the cast type. 9796 if (SubRange->Width >= OutputTypeRange.Width) 9797 return OutputTypeRange; 9798 9799 // Otherwise, we take the smaller width, and we're non-negative if 9800 // either the output type or the subexpr is. 9801 return IntRange(SubRange->Width, 9802 SubRange->NonNegative || OutputTypeRange.NonNegative); 9803 } 9804 9805 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 9806 // If we can fold the condition, just take that operand. 9807 bool CondResult; 9808 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) 9809 return TryGetExprRange( 9810 C, CondResult ? CO->getTrueExpr() : CO->getFalseExpr(), MaxWidth, 9811 InConstantContext, Approximate); 9812 9813 // Otherwise, conservatively merge. 9814 // TryGetExprRange requires an integer expression, but a throw expression 9815 // results in a void type. 9816 Expr *TrueExpr = CO->getTrueExpr(); 9817 if (TrueExpr->getType()->isVoidType()) 9818 return std::nullopt; 9819 9820 std::optional<IntRange> L = 9821 TryGetExprRange(C, TrueExpr, MaxWidth, InConstantContext, Approximate); 9822 if (!L) 9823 return std::nullopt; 9824 9825 Expr *FalseExpr = CO->getFalseExpr(); 9826 if (FalseExpr->getType()->isVoidType()) 9827 return std::nullopt; 9828 9829 std::optional<IntRange> R = 9830 TryGetExprRange(C, FalseExpr, MaxWidth, InConstantContext, Approximate); 9831 if (!R) 9832 return std::nullopt; 9833 9834 return IntRange::join(*L, *R); 9835 } 9836 9837 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 9838 IntRange (*Combine)(IntRange, IntRange) = IntRange::join; 9839 9840 switch (BO->getOpcode()) { 9841 case BO_Cmp: 9842 llvm_unreachable("builtin <=> should have class type"); 9843 9844 // Boolean-valued operations are single-bit and positive. 9845 case BO_LAnd: 9846 case BO_LOr: 9847 case BO_LT: 9848 case BO_GT: 9849 case BO_LE: 9850 case BO_GE: 9851 case BO_EQ: 9852 case BO_NE: 9853 return IntRange::forBoolType(); 9854 9855 // The type of the assignments is the type of the LHS, so the RHS 9856 // is not necessarily the same type. 9857 case BO_MulAssign: 9858 case BO_DivAssign: 9859 case BO_RemAssign: 9860 case BO_AddAssign: 9861 case BO_SubAssign: 9862 case BO_XorAssign: 9863 case BO_OrAssign: 9864 // TODO: bitfields? 9865 return IntRange::forValueOfType(C, GetExprType(E)); 9866 9867 // Simple assignments just pass through the RHS, which will have 9868 // been coerced to the LHS type. 9869 case BO_Assign: 9870 // TODO: bitfields? 9871 return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, 9872 Approximate); 9873 9874 // Operations with opaque sources are black-listed. 9875 case BO_PtrMemD: 9876 case BO_PtrMemI: 9877 return IntRange::forValueOfType(C, GetExprType(E)); 9878 9879 // Bitwise-and uses the *infinum* of the two source ranges. 9880 case BO_And: 9881 case BO_AndAssign: 9882 Combine = IntRange::bit_and; 9883 break; 9884 9885 // Left shift gets black-listed based on a judgement call. 9886 case BO_Shl: 9887 // ...except that we want to treat '1 << (blah)' as logically 9888 // positive. It's an important idiom. 9889 if (IntegerLiteral *I 9890 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { 9891 if (I->getValue() == 1) { 9892 IntRange R = IntRange::forValueOfType(C, GetExprType(E)); 9893 return IntRange(R.Width, /*NonNegative*/ true); 9894 } 9895 } 9896 [[fallthrough]]; 9897 9898 case BO_ShlAssign: 9899 return IntRange::forValueOfType(C, GetExprType(E)); 9900 9901 // Right shift by a constant can narrow its left argument. 9902 case BO_Shr: 9903 case BO_ShrAssign: { 9904 std::optional<IntRange> L = TryGetExprRange( 9905 C, BO->getLHS(), MaxWidth, InConstantContext, Approximate); 9906 if (!L) 9907 return std::nullopt; 9908 9909 // If the shift amount is a positive constant, drop the width by 9910 // that much. 9911 if (std::optional<llvm::APSInt> shift = 9912 BO->getRHS()->getIntegerConstantExpr(C)) { 9913 if (shift->isNonNegative()) { 9914 if (shift->uge(L->Width)) 9915 L->Width = (L->NonNegative ? 0 : 1); 9916 else 9917 L->Width -= shift->getZExtValue(); 9918 } 9919 } 9920 9921 return L; 9922 } 9923 9924 // Comma acts as its right operand. 9925 case BO_Comma: 9926 return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, 9927 Approximate); 9928 9929 case BO_Add: 9930 if (!Approximate) 9931 Combine = IntRange::sum; 9932 break; 9933 9934 case BO_Sub: 9935 if (BO->getLHS()->getType()->isPointerType()) 9936 return IntRange::forValueOfType(C, GetExprType(E)); 9937 if (!Approximate) 9938 Combine = IntRange::difference; 9939 break; 9940 9941 case BO_Mul: 9942 if (!Approximate) 9943 Combine = IntRange::product; 9944 break; 9945 9946 // The width of a division result is mostly determined by the size 9947 // of the LHS. 9948 case BO_Div: { 9949 // Don't 'pre-truncate' the operands. 9950 unsigned opWidth = C.getIntWidth(GetExprType(E)); 9951 std::optional<IntRange> L = TryGetExprRange( 9952 C, BO->getLHS(), opWidth, InConstantContext, Approximate); 9953 if (!L) 9954 return std::nullopt; 9955 9956 // If the divisor is constant, use that. 9957 if (std::optional<llvm::APSInt> divisor = 9958 BO->getRHS()->getIntegerConstantExpr(C)) { 9959 unsigned log2 = divisor->logBase2(); // floor(log_2(divisor)) 9960 if (log2 >= L->Width) 9961 L->Width = (L->NonNegative ? 0 : 1); 9962 else 9963 L->Width = std::min(L->Width - log2, MaxWidth); 9964 return L; 9965 } 9966 9967 // Otherwise, just use the LHS's width. 9968 // FIXME: This is wrong if the LHS could be its minimal value and the RHS 9969 // could be -1. 9970 std::optional<IntRange> R = TryGetExprRange( 9971 C, BO->getRHS(), opWidth, InConstantContext, Approximate); 9972 if (!R) 9973 return std::nullopt; 9974 9975 return IntRange(L->Width, L->NonNegative && R->NonNegative); 9976 } 9977 9978 case BO_Rem: 9979 Combine = IntRange::rem; 9980 break; 9981 9982 // The default behavior is okay for these. 9983 case BO_Xor: 9984 case BO_Or: 9985 break; 9986 } 9987 9988 // Combine the two ranges, but limit the result to the type in which we 9989 // performed the computation. 9990 QualType T = GetExprType(E); 9991 unsigned opWidth = C.getIntWidth(T); 9992 std::optional<IntRange> L = TryGetExprRange(C, BO->getLHS(), opWidth, 9993 InConstantContext, Approximate); 9994 if (!L) 9995 return std::nullopt; 9996 9997 std::optional<IntRange> R = TryGetExprRange(C, BO->getRHS(), opWidth, 9998 InConstantContext, Approximate); 9999 if (!R) 10000 return std::nullopt; 10001 10002 IntRange C = Combine(*L, *R); 10003 C.NonNegative |= T->isUnsignedIntegerOrEnumerationType(); 10004 C.Width = std::min(C.Width, MaxWidth); 10005 return C; 10006 } 10007 10008 if (const auto *UO = dyn_cast<UnaryOperator>(E)) { 10009 switch (UO->getOpcode()) { 10010 // Boolean-valued operations are white-listed. 10011 case UO_LNot: 10012 return IntRange::forBoolType(); 10013 10014 // Operations with opaque sources are black-listed. 10015 case UO_Deref: 10016 case UO_AddrOf: // should be impossible 10017 return IntRange::forValueOfType(C, GetExprType(E)); 10018 10019 default: 10020 return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext, 10021 Approximate); 10022 } 10023 } 10024 10025 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 10026 return TryGetExprRange(C, OVE->getSourceExpr(), MaxWidth, InConstantContext, 10027 Approximate); 10028 10029 if (const auto *BitField = E->getSourceBitField()) 10030 return IntRange(BitField->getBitWidthValue(C), 10031 BitField->getType()->isUnsignedIntegerOrEnumerationType()); 10032 10033 if (GetExprType(E)->isVoidType()) 10034 return std::nullopt; 10035 10036 return IntRange::forValueOfType(C, GetExprType(E)); 10037 } 10038 10039 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E, 10040 bool InConstantContext, 10041 bool Approximate) { 10042 return TryGetExprRange(C, E, C.getIntWidth(GetExprType(E)), InConstantContext, 10043 Approximate); 10044 } 10045 10046 /// Checks whether the given value, which currently has the given 10047 /// source semantics, has the same value when coerced through the 10048 /// target semantics. 10049 static bool IsSameFloatAfterCast(const llvm::APFloat &value, 10050 const llvm::fltSemantics &Src, 10051 const llvm::fltSemantics &Tgt) { 10052 llvm::APFloat truncated = value; 10053 10054 bool ignored; 10055 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); 10056 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); 10057 10058 return truncated.bitwiseIsEqual(value); 10059 } 10060 10061 /// Checks whether the given value, which currently has the given 10062 /// source semantics, has the same value when coerced through the 10063 /// target semantics. 10064 /// 10065 /// The value might be a vector of floats (or a complex number). 10066 static bool IsSameFloatAfterCast(const APValue &value, 10067 const llvm::fltSemantics &Src, 10068 const llvm::fltSemantics &Tgt) { 10069 if (value.isFloat()) 10070 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); 10071 10072 if (value.isVector()) { 10073 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) 10074 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) 10075 return false; 10076 return true; 10077 } 10078 10079 assert(value.isComplexFloat()); 10080 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && 10081 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); 10082 } 10083 10084 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC, 10085 bool IsListInit = false); 10086 10087 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) { 10088 // Suppress cases where we are comparing against an enum constant. 10089 if (const DeclRefExpr *DR = 10090 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) 10091 if (isa<EnumConstantDecl>(DR->getDecl())) 10092 return true; 10093 10094 // Suppress cases where the value is expanded from a macro, unless that macro 10095 // is how a language represents a boolean literal. This is the case in both C 10096 // and Objective-C. 10097 SourceLocation BeginLoc = E->getBeginLoc(); 10098 if (BeginLoc.isMacroID()) { 10099 StringRef MacroName = Lexer::getImmediateMacroName( 10100 BeginLoc, S.getSourceManager(), S.getLangOpts()); 10101 return MacroName != "YES" && MacroName != "NO" && 10102 MacroName != "true" && MacroName != "false"; 10103 } 10104 10105 return false; 10106 } 10107 10108 static bool isKnownToHaveUnsignedValue(Expr *E) { 10109 return E->getType()->isIntegerType() && 10110 (!E->getType()->isSignedIntegerType() || 10111 !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType()); 10112 } 10113 10114 namespace { 10115 /// The promoted range of values of a type. In general this has the 10116 /// following structure: 10117 /// 10118 /// |-----------| . . . |-----------| 10119 /// ^ ^ ^ ^ 10120 /// Min HoleMin HoleMax Max 10121 /// 10122 /// ... where there is only a hole if a signed type is promoted to unsigned 10123 /// (in which case Min and Max are the smallest and largest representable 10124 /// values). 10125 struct PromotedRange { 10126 // Min, or HoleMax if there is a hole. 10127 llvm::APSInt PromotedMin; 10128 // Max, or HoleMin if there is a hole. 10129 llvm::APSInt PromotedMax; 10130 10131 PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) { 10132 if (R.Width == 0) 10133 PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned); 10134 else if (R.Width >= BitWidth && !Unsigned) { 10135 // Promotion made the type *narrower*. This happens when promoting 10136 // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'. 10137 // Treat all values of 'signed int' as being in range for now. 10138 PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned); 10139 PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned); 10140 } else { 10141 PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative) 10142 .extOrTrunc(BitWidth); 10143 PromotedMin.setIsUnsigned(Unsigned); 10144 10145 PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative) 10146 .extOrTrunc(BitWidth); 10147 PromotedMax.setIsUnsigned(Unsigned); 10148 } 10149 } 10150 10151 // Determine whether this range is contiguous (has no hole). 10152 bool isContiguous() const { return PromotedMin <= PromotedMax; } 10153 10154 // Where a constant value is within the range. 10155 enum ComparisonResult { 10156 LT = 0x1, 10157 LE = 0x2, 10158 GT = 0x4, 10159 GE = 0x8, 10160 EQ = 0x10, 10161 NE = 0x20, 10162 InRangeFlag = 0x40, 10163 10164 Less = LE | LT | NE, 10165 Min = LE | InRangeFlag, 10166 InRange = InRangeFlag, 10167 Max = GE | InRangeFlag, 10168 Greater = GE | GT | NE, 10169 10170 OnlyValue = LE | GE | EQ | InRangeFlag, 10171 InHole = NE 10172 }; 10173 10174 ComparisonResult compare(const llvm::APSInt &Value) const { 10175 assert(Value.getBitWidth() == PromotedMin.getBitWidth() && 10176 Value.isUnsigned() == PromotedMin.isUnsigned()); 10177 if (!isContiguous()) { 10178 assert(Value.isUnsigned() && "discontiguous range for signed compare"); 10179 if (Value.isMinValue()) return Min; 10180 if (Value.isMaxValue()) return Max; 10181 if (Value >= PromotedMin) return InRange; 10182 if (Value <= PromotedMax) return InRange; 10183 return InHole; 10184 } 10185 10186 switch (llvm::APSInt::compareValues(Value, PromotedMin)) { 10187 case -1: return Less; 10188 case 0: return PromotedMin == PromotedMax ? OnlyValue : Min; 10189 case 1: 10190 switch (llvm::APSInt::compareValues(Value, PromotedMax)) { 10191 case -1: return InRange; 10192 case 0: return Max; 10193 case 1: return Greater; 10194 } 10195 } 10196 10197 llvm_unreachable("impossible compare result"); 10198 } 10199 10200 static std::optional<StringRef> 10201 constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) { 10202 if (Op == BO_Cmp) { 10203 ComparisonResult LTFlag = LT, GTFlag = GT; 10204 if (ConstantOnRHS) std::swap(LTFlag, GTFlag); 10205 10206 if (R & EQ) return StringRef("'std::strong_ordering::equal'"); 10207 if (R & LTFlag) return StringRef("'std::strong_ordering::less'"); 10208 if (R & GTFlag) return StringRef("'std::strong_ordering::greater'"); 10209 return std::nullopt; 10210 } 10211 10212 ComparisonResult TrueFlag, FalseFlag; 10213 if (Op == BO_EQ) { 10214 TrueFlag = EQ; 10215 FalseFlag = NE; 10216 } else if (Op == BO_NE) { 10217 TrueFlag = NE; 10218 FalseFlag = EQ; 10219 } else { 10220 if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) { 10221 TrueFlag = LT; 10222 FalseFlag = GE; 10223 } else { 10224 TrueFlag = GT; 10225 FalseFlag = LE; 10226 } 10227 if (Op == BO_GE || Op == BO_LE) 10228 std::swap(TrueFlag, FalseFlag); 10229 } 10230 if (R & TrueFlag) 10231 return StringRef("true"); 10232 if (R & FalseFlag) 10233 return StringRef("false"); 10234 return std::nullopt; 10235 } 10236 }; 10237 } 10238 10239 static bool HasEnumType(Expr *E) { 10240 // Strip off implicit integral promotions. 10241 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 10242 if (ICE->getCastKind() != CK_IntegralCast && 10243 ICE->getCastKind() != CK_NoOp) 10244 break; 10245 E = ICE->getSubExpr(); 10246 } 10247 10248 return E->getType()->isEnumeralType(); 10249 } 10250 10251 static int classifyConstantValue(Expr *Constant) { 10252 // The values of this enumeration are used in the diagnostics 10253 // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare. 10254 enum ConstantValueKind { 10255 Miscellaneous = 0, 10256 LiteralTrue, 10257 LiteralFalse 10258 }; 10259 if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant)) 10260 return BL->getValue() ? ConstantValueKind::LiteralTrue 10261 : ConstantValueKind::LiteralFalse; 10262 return ConstantValueKind::Miscellaneous; 10263 } 10264 10265 static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E, 10266 Expr *Constant, Expr *Other, 10267 const llvm::APSInt &Value, 10268 bool RhsConstant) { 10269 if (S.inTemplateInstantiation()) 10270 return false; 10271 10272 Expr *OriginalOther = Other; 10273 10274 Constant = Constant->IgnoreParenImpCasts(); 10275 Other = Other->IgnoreParenImpCasts(); 10276 10277 // Suppress warnings on tautological comparisons between values of the same 10278 // enumeration type. There are only two ways we could warn on this: 10279 // - If the constant is outside the range of representable values of 10280 // the enumeration. In such a case, we should warn about the cast 10281 // to enumeration type, not about the comparison. 10282 // - If the constant is the maximum / minimum in-range value. For an 10283 // enumeratin type, such comparisons can be meaningful and useful. 10284 if (Constant->getType()->isEnumeralType() && 10285 S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType())) 10286 return false; 10287 10288 std::optional<IntRange> OtherValueRange = TryGetExprRange( 10289 S.Context, Other, S.isConstantEvaluatedContext(), /*Approximate=*/false); 10290 if (!OtherValueRange) 10291 return false; 10292 10293 QualType OtherT = Other->getType(); 10294 if (const auto *AT = OtherT->getAs<AtomicType>()) 10295 OtherT = AT->getValueType(); 10296 IntRange OtherTypeRange = IntRange::forValueOfType(S.Context, OtherT); 10297 10298 // Special case for ObjC BOOL on targets where its a typedef for a signed char 10299 // (Namely, macOS). FIXME: IntRange::forValueOfType should do this. 10300 bool IsObjCSignedCharBool = S.getLangOpts().ObjC && 10301 S.ObjC().NSAPIObj->isObjCBOOLType(OtherT) && 10302 OtherT->isSpecificBuiltinType(BuiltinType::SChar); 10303 10304 // Whether we're treating Other as being a bool because of the form of 10305 // expression despite it having another type (typically 'int' in C). 10306 bool OtherIsBooleanDespiteType = 10307 !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue(); 10308 if (OtherIsBooleanDespiteType || IsObjCSignedCharBool) 10309 OtherTypeRange = *OtherValueRange = IntRange::forBoolType(); 10310 10311 // Check if all values in the range of possible values of this expression 10312 // lead to the same comparison outcome. 10313 PromotedRange OtherPromotedValueRange(*OtherValueRange, Value.getBitWidth(), 10314 Value.isUnsigned()); 10315 auto Cmp = OtherPromotedValueRange.compare(Value); 10316 auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant); 10317 if (!Result) 10318 return false; 10319 10320 // Also consider the range determined by the type alone. This allows us to 10321 // classify the warning under the proper diagnostic group. 10322 bool TautologicalTypeCompare = false; 10323 { 10324 PromotedRange OtherPromotedTypeRange(OtherTypeRange, Value.getBitWidth(), 10325 Value.isUnsigned()); 10326 auto TypeCmp = OtherPromotedTypeRange.compare(Value); 10327 if (auto TypeResult = PromotedRange::constantValue(E->getOpcode(), TypeCmp, 10328 RhsConstant)) { 10329 TautologicalTypeCompare = true; 10330 Cmp = TypeCmp; 10331 Result = TypeResult; 10332 } 10333 } 10334 10335 // Don't warn if the non-constant operand actually always evaluates to the 10336 // same value. 10337 if (!TautologicalTypeCompare && OtherValueRange->Width == 0) 10338 return false; 10339 10340 // Suppress the diagnostic for an in-range comparison if the constant comes 10341 // from a macro or enumerator. We don't want to diagnose 10342 // 10343 // some_long_value <= INT_MAX 10344 // 10345 // when sizeof(int) == sizeof(long). 10346 bool InRange = Cmp & PromotedRange::InRangeFlag; 10347 if (InRange && IsEnumConstOrFromMacro(S, Constant)) 10348 return false; 10349 10350 // A comparison of an unsigned bit-field against 0 is really a type problem, 10351 // even though at the type level the bit-field might promote to 'signed int'. 10352 if (Other->refersToBitField() && InRange && Value == 0 && 10353 Other->getType()->isUnsignedIntegerOrEnumerationType()) 10354 TautologicalTypeCompare = true; 10355 10356 // If this is a comparison to an enum constant, include that 10357 // constant in the diagnostic. 10358 const EnumConstantDecl *ED = nullptr; 10359 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) 10360 ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); 10361 10362 // Should be enough for uint128 (39 decimal digits) 10363 SmallString<64> PrettySourceValue; 10364 llvm::raw_svector_ostream OS(PrettySourceValue); 10365 if (ED) { 10366 OS << '\'' << *ED << "' (" << Value << ")"; 10367 } else if (auto *BL = dyn_cast<ObjCBoolLiteralExpr>( 10368 Constant->IgnoreParenImpCasts())) { 10369 OS << (BL->getValue() ? "YES" : "NO"); 10370 } else { 10371 OS << Value; 10372 } 10373 10374 if (!TautologicalTypeCompare) { 10375 S.Diag(E->getOperatorLoc(), diag::warn_tautological_compare_value_range) 10376 << RhsConstant << OtherValueRange->Width << OtherValueRange->NonNegative 10377 << E->getOpcodeStr() << OS.str() << *Result 10378 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10379 return true; 10380 } 10381 10382 if (IsObjCSignedCharBool) { 10383 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10384 S.PDiag(diag::warn_tautological_compare_objc_bool) 10385 << OS.str() << *Result); 10386 return true; 10387 } 10388 10389 // FIXME: We use a somewhat different formatting for the in-range cases and 10390 // cases involving boolean values for historical reasons. We should pick a 10391 // consistent way of presenting these diagnostics. 10392 if (!InRange || Other->isKnownToHaveBooleanValue()) { 10393 10394 S.DiagRuntimeBehavior( 10395 E->getOperatorLoc(), E, 10396 S.PDiag(!InRange ? diag::warn_out_of_range_compare 10397 : diag::warn_tautological_bool_compare) 10398 << OS.str() << classifyConstantValue(Constant) << OtherT 10399 << OtherIsBooleanDespiteType << *Result 10400 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); 10401 } else { 10402 bool IsCharTy = OtherT.withoutLocalFastQualifiers() == S.Context.CharTy; 10403 unsigned Diag = 10404 (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0) 10405 ? (HasEnumType(OriginalOther) 10406 ? diag::warn_unsigned_enum_always_true_comparison 10407 : IsCharTy ? diag::warn_unsigned_char_always_true_comparison 10408 : diag::warn_unsigned_always_true_comparison) 10409 : diag::warn_tautological_constant_compare; 10410 10411 S.Diag(E->getOperatorLoc(), Diag) 10412 << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result 10413 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10414 } 10415 10416 return true; 10417 } 10418 10419 /// Analyze the operands of the given comparison. Implements the 10420 /// fallback case from AnalyzeComparison. 10421 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { 10422 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10423 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10424 } 10425 10426 /// Implements -Wsign-compare. 10427 /// 10428 /// \param E the binary operator to check for warnings 10429 static void AnalyzeComparison(Sema &S, BinaryOperator *E) { 10430 // The type the comparison is being performed in. 10431 QualType T = E->getLHS()->getType(); 10432 10433 // Only analyze comparison operators where both sides have been converted to 10434 // the same type. 10435 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) 10436 return AnalyzeImpConvsInComparison(S, E); 10437 10438 // Don't analyze value-dependent comparisons directly. 10439 if (E->isValueDependent()) 10440 return AnalyzeImpConvsInComparison(S, E); 10441 10442 Expr *LHS = E->getLHS(); 10443 Expr *RHS = E->getRHS(); 10444 10445 if (T->isIntegralType(S.Context)) { 10446 std::optional<llvm::APSInt> RHSValue = 10447 RHS->getIntegerConstantExpr(S.Context); 10448 std::optional<llvm::APSInt> LHSValue = 10449 LHS->getIntegerConstantExpr(S.Context); 10450 10451 // We don't care about expressions whose result is a constant. 10452 if (RHSValue && LHSValue) 10453 return AnalyzeImpConvsInComparison(S, E); 10454 10455 // We only care about expressions where just one side is literal 10456 if ((bool)RHSValue ^ (bool)LHSValue) { 10457 // Is the constant on the RHS or LHS? 10458 const bool RhsConstant = (bool)RHSValue; 10459 Expr *Const = RhsConstant ? RHS : LHS; 10460 Expr *Other = RhsConstant ? LHS : RHS; 10461 const llvm::APSInt &Value = RhsConstant ? *RHSValue : *LHSValue; 10462 10463 // Check whether an integer constant comparison results in a value 10464 // of 'true' or 'false'. 10465 if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant)) 10466 return AnalyzeImpConvsInComparison(S, E); 10467 } 10468 } 10469 10470 if (!T->hasUnsignedIntegerRepresentation()) { 10471 // We don't do anything special if this isn't an unsigned integral 10472 // comparison: we're only interested in integral comparisons, and 10473 // signed comparisons only happen in cases we don't care to warn about. 10474 return AnalyzeImpConvsInComparison(S, E); 10475 } 10476 10477 LHS = LHS->IgnoreParenImpCasts(); 10478 RHS = RHS->IgnoreParenImpCasts(); 10479 10480 if (!S.getLangOpts().CPlusPlus) { 10481 // Avoid warning about comparison of integers with different signs when 10482 // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of 10483 // the type of `E`. 10484 if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType())) 10485 LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10486 if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType())) 10487 RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10488 } 10489 10490 // Check to see if one of the (unmodified) operands is of different 10491 // signedness. 10492 Expr *signedOperand, *unsignedOperand; 10493 if (LHS->getType()->hasSignedIntegerRepresentation()) { 10494 assert(!RHS->getType()->hasSignedIntegerRepresentation() && 10495 "unsigned comparison between two signed integer expressions?"); 10496 signedOperand = LHS; 10497 unsignedOperand = RHS; 10498 } else if (RHS->getType()->hasSignedIntegerRepresentation()) { 10499 signedOperand = RHS; 10500 unsignedOperand = LHS; 10501 } else { 10502 return AnalyzeImpConvsInComparison(S, E); 10503 } 10504 10505 // Otherwise, calculate the effective range of the signed operand. 10506 std::optional<IntRange> signedRange = 10507 TryGetExprRange(S.Context, signedOperand, S.isConstantEvaluatedContext(), 10508 /*Approximate=*/true); 10509 if (!signedRange) 10510 return; 10511 10512 // Go ahead and analyze implicit conversions in the operands. Note 10513 // that we skip the implicit conversions on both sides. 10514 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); 10515 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); 10516 10517 // If the signed range is non-negative, -Wsign-compare won't fire. 10518 if (signedRange->NonNegative) 10519 return; 10520 10521 // For (in)equality comparisons, if the unsigned operand is a 10522 // constant which cannot collide with a overflowed signed operand, 10523 // then reinterpreting the signed operand as unsigned will not 10524 // change the result of the comparison. 10525 if (E->isEqualityOp()) { 10526 unsigned comparisonWidth = S.Context.getIntWidth(T); 10527 std::optional<IntRange> unsignedRange = TryGetExprRange( 10528 S.Context, unsignedOperand, S.isConstantEvaluatedContext(), 10529 /*Approximate=*/true); 10530 if (!unsignedRange) 10531 return; 10532 10533 // We should never be unable to prove that the unsigned operand is 10534 // non-negative. 10535 assert(unsignedRange->NonNegative && "unsigned range includes negative?"); 10536 10537 if (unsignedRange->Width < comparisonWidth) 10538 return; 10539 } 10540 10541 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10542 S.PDiag(diag::warn_mixed_sign_comparison) 10543 << LHS->getType() << RHS->getType() 10544 << LHS->getSourceRange() << RHS->getSourceRange()); 10545 } 10546 10547 /// Analyzes an attempt to assign the given value to a bitfield. 10548 /// 10549 /// Returns true if there was something fishy about the attempt. 10550 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, 10551 SourceLocation InitLoc) { 10552 assert(Bitfield->isBitField()); 10553 if (Bitfield->isInvalidDecl()) 10554 return false; 10555 10556 // White-list bool bitfields. 10557 QualType BitfieldType = Bitfield->getType(); 10558 if (BitfieldType->isBooleanType()) 10559 return false; 10560 10561 if (BitfieldType->isEnumeralType()) { 10562 EnumDecl *BitfieldEnumDecl = BitfieldType->castAs<EnumType>()->getDecl(); 10563 // If the underlying enum type was not explicitly specified as an unsigned 10564 // type and the enum contain only positive values, MSVC++ will cause an 10565 // inconsistency by storing this as a signed type. 10566 if (S.getLangOpts().CPlusPlus11 && 10567 !BitfieldEnumDecl->getIntegerTypeSourceInfo() && 10568 BitfieldEnumDecl->getNumPositiveBits() > 0 && 10569 BitfieldEnumDecl->getNumNegativeBits() == 0) { 10570 S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield) 10571 << BitfieldEnumDecl; 10572 } 10573 } 10574 10575 // Ignore value- or type-dependent expressions. 10576 if (Bitfield->getBitWidth()->isValueDependent() || 10577 Bitfield->getBitWidth()->isTypeDependent() || 10578 Init->isValueDependent() || 10579 Init->isTypeDependent()) 10580 return false; 10581 10582 Expr *OriginalInit = Init->IgnoreParenImpCasts(); 10583 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); 10584 10585 Expr::EvalResult Result; 10586 if (!OriginalInit->EvaluateAsInt(Result, S.Context, 10587 Expr::SE_AllowSideEffects)) { 10588 // The RHS is not constant. If the RHS has an enum type, make sure the 10589 // bitfield is wide enough to hold all the values of the enum without 10590 // truncation. 10591 if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) { 10592 EnumDecl *ED = EnumTy->getDecl(); 10593 bool SignedBitfield = BitfieldType->isSignedIntegerType(); 10594 10595 // Enum types are implicitly signed on Windows, so check if there are any 10596 // negative enumerators to see if the enum was intended to be signed or 10597 // not. 10598 bool SignedEnum = ED->getNumNegativeBits() > 0; 10599 10600 // Check for surprising sign changes when assigning enum values to a 10601 // bitfield of different signedness. If the bitfield is signed and we 10602 // have exactly the right number of bits to store this unsigned enum, 10603 // suggest changing the enum to an unsigned type. This typically happens 10604 // on Windows where unfixed enums always use an underlying type of 'int'. 10605 unsigned DiagID = 0; 10606 if (SignedEnum && !SignedBitfield) { 10607 DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum; 10608 } else if (SignedBitfield && !SignedEnum && 10609 ED->getNumPositiveBits() == FieldWidth) { 10610 DiagID = diag::warn_signed_bitfield_enum_conversion; 10611 } 10612 10613 if (DiagID) { 10614 S.Diag(InitLoc, DiagID) << Bitfield << ED; 10615 TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo(); 10616 SourceRange TypeRange = 10617 TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange(); 10618 S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign) 10619 << SignedEnum << TypeRange; 10620 } 10621 10622 // Compute the required bitwidth. If the enum has negative values, we need 10623 // one more bit than the normal number of positive bits to represent the 10624 // sign bit. 10625 unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1, 10626 ED->getNumNegativeBits()) 10627 : ED->getNumPositiveBits(); 10628 10629 // Check the bitwidth. 10630 if (BitsNeeded > FieldWidth) { 10631 Expr *WidthExpr = Bitfield->getBitWidth(); 10632 S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum) 10633 << Bitfield << ED; 10634 S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield) 10635 << BitsNeeded << ED << WidthExpr->getSourceRange(); 10636 } 10637 } 10638 10639 return false; 10640 } 10641 10642 llvm::APSInt Value = Result.Val.getInt(); 10643 10644 unsigned OriginalWidth = Value.getBitWidth(); 10645 10646 // In C, the macro 'true' from stdbool.h will evaluate to '1'; To reduce 10647 // false positives where the user is demonstrating they intend to use the 10648 // bit-field as a Boolean, check to see if the value is 1 and we're assigning 10649 // to a one-bit bit-field to see if the value came from a macro named 'true'. 10650 bool OneAssignedToOneBitBitfield = FieldWidth == 1 && Value == 1; 10651 if (OneAssignedToOneBitBitfield && !S.LangOpts.CPlusPlus) { 10652 SourceLocation MaybeMacroLoc = OriginalInit->getBeginLoc(); 10653 if (S.SourceMgr.isInSystemMacro(MaybeMacroLoc) && 10654 S.findMacroSpelling(MaybeMacroLoc, "true")) 10655 return false; 10656 } 10657 10658 if (!Value.isSigned() || Value.isNegative()) 10659 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit)) 10660 if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not) 10661 OriginalWidth = Value.getSignificantBits(); 10662 10663 if (OriginalWidth <= FieldWidth) 10664 return false; 10665 10666 // Compute the value which the bitfield will contain. 10667 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); 10668 TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType()); 10669 10670 // Check whether the stored value is equal to the original value. 10671 TruncatedValue = TruncatedValue.extend(OriginalWidth); 10672 if (llvm::APSInt::isSameValue(Value, TruncatedValue)) 10673 return false; 10674 10675 std::string PrettyValue = toString(Value, 10); 10676 std::string PrettyTrunc = toString(TruncatedValue, 10); 10677 10678 S.Diag(InitLoc, OneAssignedToOneBitBitfield 10679 ? diag::warn_impcast_single_bit_bitield_precision_constant 10680 : diag::warn_impcast_bitfield_precision_constant) 10681 << PrettyValue << PrettyTrunc << OriginalInit->getType() 10682 << Init->getSourceRange(); 10683 10684 return true; 10685 } 10686 10687 /// Analyze the given simple or compound assignment for warning-worthy 10688 /// operations. 10689 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { 10690 // Just recurse on the LHS. 10691 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10692 10693 // We want to recurse on the RHS as normal unless we're assigning to 10694 // a bitfield. 10695 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { 10696 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), 10697 E->getOperatorLoc())) { 10698 // Recurse, ignoring any implicit conversions on the RHS. 10699 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), 10700 E->getOperatorLoc()); 10701 } 10702 } 10703 10704 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10705 10706 // Diagnose implicitly sequentially-consistent atomic assignment. 10707 if (E->getLHS()->getType()->isAtomicType()) 10708 S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 10709 } 10710 10711 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10712 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 10713 SourceLocation CContext, unsigned diag, 10714 bool pruneControlFlow = false) { 10715 if (pruneControlFlow) { 10716 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10717 S.PDiag(diag) 10718 << SourceType << T << E->getSourceRange() 10719 << SourceRange(CContext)); 10720 return; 10721 } 10722 S.Diag(E->getExprLoc(), diag) 10723 << SourceType << T << E->getSourceRange() << SourceRange(CContext); 10724 } 10725 10726 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10727 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, 10728 SourceLocation CContext, 10729 unsigned diag, bool pruneControlFlow = false) { 10730 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); 10731 } 10732 10733 /// Diagnose an implicit cast from a floating point value to an integer value. 10734 static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T, 10735 SourceLocation CContext) { 10736 const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool); 10737 const bool PruneWarnings = S.inTemplateInstantiation(); 10738 10739 Expr *InnerE = E->IgnoreParenImpCasts(); 10740 // We also want to warn on, e.g., "int i = -1.234" 10741 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) 10742 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) 10743 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); 10744 10745 const bool IsLiteral = 10746 isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE); 10747 10748 llvm::APFloat Value(0.0); 10749 bool IsConstant = 10750 E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects); 10751 if (!IsConstant) { 10752 if (S.ObjC().isSignedCharBool(T)) { 10753 return S.ObjC().adornBoolConversionDiagWithTernaryFixit( 10754 E, S.Diag(CContext, diag::warn_impcast_float_to_objc_signed_char_bool) 10755 << E->getType()); 10756 } 10757 10758 return DiagnoseImpCast(S, E, T, CContext, 10759 diag::warn_impcast_float_integer, PruneWarnings); 10760 } 10761 10762 bool isExact = false; 10763 10764 llvm::APSInt IntegerValue(S.Context.getIntWidth(T), 10765 T->hasUnsignedIntegerRepresentation()); 10766 llvm::APFloat::opStatus Result = Value.convertToInteger( 10767 IntegerValue, llvm::APFloat::rmTowardZero, &isExact); 10768 10769 // FIXME: Force the precision of the source value down so we don't print 10770 // digits which are usually useless (we don't really care here if we 10771 // truncate a digit by accident in edge cases). Ideally, APFloat::toString 10772 // would automatically print the shortest representation, but it's a bit 10773 // tricky to implement. 10774 SmallString<16> PrettySourceValue; 10775 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); 10776 precision = (precision * 59 + 195) / 196; 10777 Value.toString(PrettySourceValue, precision); 10778 10779 if (S.ObjC().isSignedCharBool(T) && IntegerValue != 0 && IntegerValue != 1) { 10780 return S.ObjC().adornBoolConversionDiagWithTernaryFixit( 10781 E, S.Diag(CContext, diag::warn_impcast_constant_value_to_objc_bool) 10782 << PrettySourceValue); 10783 } 10784 10785 if (Result == llvm::APFloat::opOK && isExact) { 10786 if (IsLiteral) return; 10787 return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer, 10788 PruneWarnings); 10789 } 10790 10791 // Conversion of a floating-point value to a non-bool integer where the 10792 // integral part cannot be represented by the integer type is undefined. 10793 if (!IsBool && Result == llvm::APFloat::opInvalidOp) 10794 return DiagnoseImpCast( 10795 S, E, T, CContext, 10796 IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range 10797 : diag::warn_impcast_float_to_integer_out_of_range, 10798 PruneWarnings); 10799 10800 unsigned DiagID = 0; 10801 if (IsLiteral) { 10802 // Warn on floating point literal to integer. 10803 DiagID = diag::warn_impcast_literal_float_to_integer; 10804 } else if (IntegerValue == 0) { 10805 if (Value.isZero()) { // Skip -0.0 to 0 conversion. 10806 return DiagnoseImpCast(S, E, T, CContext, 10807 diag::warn_impcast_float_integer, PruneWarnings); 10808 } 10809 // Warn on non-zero to zero conversion. 10810 DiagID = diag::warn_impcast_float_to_integer_zero; 10811 } else { 10812 if (IntegerValue.isUnsigned()) { 10813 if (!IntegerValue.isMaxValue()) { 10814 return DiagnoseImpCast(S, E, T, CContext, 10815 diag::warn_impcast_float_integer, PruneWarnings); 10816 } 10817 } else { // IntegerValue.isSigned() 10818 if (!IntegerValue.isMaxSignedValue() && 10819 !IntegerValue.isMinSignedValue()) { 10820 return DiagnoseImpCast(S, E, T, CContext, 10821 diag::warn_impcast_float_integer, PruneWarnings); 10822 } 10823 } 10824 // Warn on evaluatable floating point expression to integer conversion. 10825 DiagID = diag::warn_impcast_float_to_integer; 10826 } 10827 10828 SmallString<16> PrettyTargetValue; 10829 if (IsBool) 10830 PrettyTargetValue = Value.isZero() ? "false" : "true"; 10831 else 10832 IntegerValue.toString(PrettyTargetValue); 10833 10834 if (PruneWarnings) { 10835 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10836 S.PDiag(DiagID) 10837 << E->getType() << T.getUnqualifiedType() 10838 << PrettySourceValue << PrettyTargetValue 10839 << E->getSourceRange() << SourceRange(CContext)); 10840 } else { 10841 S.Diag(E->getExprLoc(), DiagID) 10842 << E->getType() << T.getUnqualifiedType() << PrettySourceValue 10843 << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext); 10844 } 10845 } 10846 10847 /// Analyze the given compound assignment for the possible losing of 10848 /// floating-point precision. 10849 static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) { 10850 assert(isa<CompoundAssignOperator>(E) && 10851 "Must be compound assignment operation"); 10852 // Recurse on the LHS and RHS in here 10853 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10854 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10855 10856 if (E->getLHS()->getType()->isAtomicType()) 10857 S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst); 10858 10859 // Now check the outermost expression 10860 const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>(); 10861 const auto *RBT = cast<CompoundAssignOperator>(E) 10862 ->getComputationResultType() 10863 ->getAs<BuiltinType>(); 10864 10865 // The below checks assume source is floating point. 10866 if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return; 10867 10868 // If source is floating point but target is an integer. 10869 if (ResultBT->isInteger()) 10870 return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(), 10871 E->getExprLoc(), diag::warn_impcast_float_integer); 10872 10873 if (!ResultBT->isFloatingPoint()) 10874 return; 10875 10876 // If both source and target are floating points, warn about losing precision. 10877 int Order = S.getASTContext().getFloatingTypeSemanticOrder( 10878 QualType(ResultBT, 0), QualType(RBT, 0)); 10879 if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc())) 10880 // warn about dropping FP rank. 10881 DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(), 10882 diag::warn_impcast_float_result_precision); 10883 } 10884 10885 static std::string PrettyPrintInRange(const llvm::APSInt &Value, 10886 IntRange Range) { 10887 if (!Range.Width) return "0"; 10888 10889 llvm::APSInt ValueInRange = Value; 10890 ValueInRange.setIsSigned(!Range.NonNegative); 10891 ValueInRange = ValueInRange.trunc(Range.Width); 10892 return toString(ValueInRange, 10); 10893 } 10894 10895 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { 10896 if (!isa<ImplicitCastExpr>(Ex)) 10897 return false; 10898 10899 Expr *InnerE = Ex->IgnoreParenImpCasts(); 10900 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); 10901 const Type *Source = 10902 S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); 10903 if (Target->isDependentType()) 10904 return false; 10905 10906 const BuiltinType *FloatCandidateBT = 10907 dyn_cast<BuiltinType>(ToBool ? Source : Target); 10908 const Type *BoolCandidateType = ToBool ? Target : Source; 10909 10910 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && 10911 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); 10912 } 10913 10914 static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, 10915 SourceLocation CC) { 10916 unsigned NumArgs = TheCall->getNumArgs(); 10917 for (unsigned i = 0; i < NumArgs; ++i) { 10918 Expr *CurrA = TheCall->getArg(i); 10919 if (!IsImplicitBoolFloatConversion(S, CurrA, true)) 10920 continue; 10921 10922 bool IsSwapped = ((i > 0) && 10923 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); 10924 IsSwapped |= ((i < (NumArgs - 1)) && 10925 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); 10926 if (IsSwapped) { 10927 // Warn on this floating-point to bool conversion. 10928 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), 10929 CurrA->getType(), CC, 10930 diag::warn_impcast_floating_point_to_bool); 10931 } 10932 } 10933 } 10934 10935 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, 10936 SourceLocation CC) { 10937 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, 10938 E->getExprLoc())) 10939 return; 10940 10941 // Don't warn on functions which have return type nullptr_t. 10942 if (isa<CallExpr>(E)) 10943 return; 10944 10945 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). 10946 const Expr *NewE = E->IgnoreParenImpCasts(); 10947 bool IsGNUNullExpr = isa<GNUNullExpr>(NewE); 10948 bool HasNullPtrType = NewE->getType()->isNullPtrType(); 10949 if (!IsGNUNullExpr && !HasNullPtrType) 10950 return; 10951 10952 // Return if target type is a safe conversion. 10953 if (T->isAnyPointerType() || T->isBlockPointerType() || 10954 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) 10955 return; 10956 10957 SourceLocation Loc = E->getSourceRange().getBegin(); 10958 10959 // Venture through the macro stacks to get to the source of macro arguments. 10960 // The new location is a better location than the complete location that was 10961 // passed in. 10962 Loc = S.SourceMgr.getTopMacroCallerLoc(Loc); 10963 CC = S.SourceMgr.getTopMacroCallerLoc(CC); 10964 10965 // __null is usually wrapped in a macro. Go up a macro if that is the case. 10966 if (IsGNUNullExpr && Loc.isMacroID()) { 10967 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( 10968 Loc, S.SourceMgr, S.getLangOpts()); 10969 if (MacroName == "NULL") 10970 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin(); 10971 } 10972 10973 // Only warn if the null and context location are in the same macro expansion. 10974 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) 10975 return; 10976 10977 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) 10978 << HasNullPtrType << T << SourceRange(CC) 10979 << FixItHint::CreateReplacement(Loc, 10980 S.getFixItZeroLiteralForType(T, Loc)); 10981 } 10982 10983 // Helper function to filter out cases for constant width constant conversion. 10984 // Don't warn on char array initialization or for non-decimal values. 10985 static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, 10986 SourceLocation CC) { 10987 // If initializing from a constant, and the constant starts with '0', 10988 // then it is a binary, octal, or hexadecimal. Allow these constants 10989 // to fill all the bits, even if there is a sign change. 10990 if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) { 10991 const char FirstLiteralCharacter = 10992 S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0]; 10993 if (FirstLiteralCharacter == '0') 10994 return false; 10995 } 10996 10997 // If the CC location points to a '{', and the type is char, then assume 10998 // assume it is an array initialization. 10999 if (CC.isValid() && T->isCharType()) { 11000 const char FirstContextCharacter = 11001 S.getSourceManager().getCharacterData(CC)[0]; 11002 if (FirstContextCharacter == '{') 11003 return false; 11004 } 11005 11006 return true; 11007 } 11008 11009 static const IntegerLiteral *getIntegerLiteral(Expr *E) { 11010 const auto *IL = dyn_cast<IntegerLiteral>(E); 11011 if (!IL) { 11012 if (auto *UO = dyn_cast<UnaryOperator>(E)) { 11013 if (UO->getOpcode() == UO_Minus) 11014 return dyn_cast<IntegerLiteral>(UO->getSubExpr()); 11015 } 11016 } 11017 11018 return IL; 11019 } 11020 11021 static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { 11022 E = E->IgnoreParenImpCasts(); 11023 SourceLocation ExprLoc = E->getExprLoc(); 11024 11025 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 11026 BinaryOperator::Opcode Opc = BO->getOpcode(); 11027 Expr::EvalResult Result; 11028 // Do not diagnose unsigned shifts. 11029 if (Opc == BO_Shl) { 11030 const auto *LHS = getIntegerLiteral(BO->getLHS()); 11031 const auto *RHS = getIntegerLiteral(BO->getRHS()); 11032 if (LHS && LHS->getValue() == 0) 11033 S.Diag(ExprLoc, diag::warn_left_shift_always) << 0; 11034 else if (!E->isValueDependent() && LHS && RHS && 11035 RHS->getValue().isNonNegative() && 11036 E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) 11037 S.Diag(ExprLoc, diag::warn_left_shift_always) 11038 << (Result.Val.getInt() != 0); 11039 else if (E->getType()->isSignedIntegerType()) 11040 S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; 11041 } 11042 } 11043 11044 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 11045 const auto *LHS = getIntegerLiteral(CO->getTrueExpr()); 11046 const auto *RHS = getIntegerLiteral(CO->getFalseExpr()); 11047 if (!LHS || !RHS) 11048 return; 11049 if ((LHS->getValue() == 0 || LHS->getValue() == 1) && 11050 (RHS->getValue() == 0 || RHS->getValue() == 1)) 11051 // Do not diagnose common idioms. 11052 return; 11053 if (LHS->getValue() != 0 && RHS->getValue() != 0) 11054 S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true); 11055 } 11056 } 11057 11058 void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, 11059 bool *ICContext, bool IsListInit) { 11060 if (E->isTypeDependent() || E->isValueDependent()) return; 11061 11062 const Type *Source = Context.getCanonicalType(E->getType()).getTypePtr(); 11063 const Type *Target = Context.getCanonicalType(T).getTypePtr(); 11064 if (Source == Target) return; 11065 if (Target->isDependentType()) return; 11066 11067 // If the conversion context location is invalid don't complain. We also 11068 // don't want to emit a warning if the issue occurs from the expansion of 11069 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we 11070 // delay this check as long as possible. Once we detect we are in that 11071 // scenario, we just return. 11072 if (CC.isInvalid()) 11073 return; 11074 11075 if (Source->isAtomicType()) 11076 Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst); 11077 11078 // Diagnose implicit casts to bool. 11079 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { 11080 if (isa<StringLiteral>(E)) 11081 // Warn on string literal to bool. Checks for string literals in logical 11082 // and expressions, for instance, assert(0 && "error here"), are 11083 // prevented by a check in AnalyzeImplicitConversions(). 11084 return DiagnoseImpCast(*this, E, T, CC, 11085 diag::warn_impcast_string_literal_to_bool); 11086 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || 11087 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { 11088 // This covers the literal expressions that evaluate to Objective-C 11089 // objects. 11090 return DiagnoseImpCast(*this, E, T, CC, 11091 diag::warn_impcast_objective_c_literal_to_bool); 11092 } 11093 if (Source->isPointerType() || Source->canDecayToPointerType()) { 11094 // Warn on pointer to bool conversion that is always true. 11095 DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, 11096 SourceRange(CC)); 11097 } 11098 } 11099 11100 // If the we're converting a constant to an ObjC BOOL on a platform where BOOL 11101 // is a typedef for signed char (macOS), then that constant value has to be 1 11102 // or 0. 11103 if (ObjC().isSignedCharBool(T) && Source->isIntegralType(Context)) { 11104 Expr::EvalResult Result; 11105 if (E->EvaluateAsInt(Result, getASTContext(), Expr::SE_AllowSideEffects)) { 11106 if (Result.Val.getInt() != 1 && Result.Val.getInt() != 0) { 11107 ObjC().adornBoolConversionDiagWithTernaryFixit( 11108 E, Diag(CC, diag::warn_impcast_constant_value_to_objc_bool) 11109 << toString(Result.Val.getInt(), 10)); 11110 } 11111 return; 11112 } 11113 } 11114 11115 // Check implicit casts from Objective-C collection literals to specialized 11116 // collection types, e.g., NSArray<NSString *> *. 11117 if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E)) 11118 ObjC().checkArrayLiteral(QualType(Target, 0), ArrayLiteral); 11119 else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) 11120 ObjC().checkDictionaryLiteral(QualType(Target, 0), DictionaryLiteral); 11121 11122 // Strip vector types. 11123 if (isa<VectorType>(Source)) { 11124 if (Target->isSveVLSBuiltinType() && 11125 (Context.areCompatibleSveTypes(QualType(Target, 0), 11126 QualType(Source, 0)) || 11127 Context.areLaxCompatibleSveTypes(QualType(Target, 0), 11128 QualType(Source, 0)))) 11129 return; 11130 11131 if (Target->isRVVVLSBuiltinType() && 11132 (Context.areCompatibleRVVTypes(QualType(Target, 0), 11133 QualType(Source, 0)) || 11134 Context.areLaxCompatibleRVVTypes(QualType(Target, 0), 11135 QualType(Source, 0)))) 11136 return; 11137 11138 if (!isa<VectorType>(Target)) { 11139 if (SourceMgr.isInSystemMacro(CC)) 11140 return; 11141 return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_vector_scalar); 11142 } else if (getLangOpts().HLSL && 11143 Target->castAs<VectorType>()->getNumElements() < 11144 Source->castAs<VectorType>()->getNumElements()) { 11145 // Diagnose vector truncation but don't return. We may also want to 11146 // diagnose an element conversion. 11147 DiagnoseImpCast(*this, E, T, CC, 11148 diag::warn_hlsl_impcast_vector_truncation); 11149 } 11150 11151 // If the vector cast is cast between two vectors of the same size, it is 11152 // a bitcast, not a conversion, except under HLSL where it is a conversion. 11153 if (!getLangOpts().HLSL && 11154 Context.getTypeSize(Source) == Context.getTypeSize(Target)) 11155 return; 11156 11157 Source = cast<VectorType>(Source)->getElementType().getTypePtr(); 11158 Target = cast<VectorType>(Target)->getElementType().getTypePtr(); 11159 } 11160 if (auto VecTy = dyn_cast<VectorType>(Target)) 11161 Target = VecTy->getElementType().getTypePtr(); 11162 11163 // Strip complex types. 11164 if (isa<ComplexType>(Source)) { 11165 if (!isa<ComplexType>(Target)) { 11166 if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) 11167 return; 11168 11169 return DiagnoseImpCast(*this, E, T, CC, 11170 getLangOpts().CPlusPlus 11171 ? diag::err_impcast_complex_scalar 11172 : diag::warn_impcast_complex_scalar); 11173 } 11174 11175 Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); 11176 Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); 11177 } 11178 11179 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); 11180 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); 11181 11182 // Strip SVE vector types 11183 if (SourceBT && SourceBT->isSveVLSBuiltinType()) { 11184 // Need the original target type for vector type checks 11185 const Type *OriginalTarget = Context.getCanonicalType(T).getTypePtr(); 11186 // Handle conversion from scalable to fixed when msve-vector-bits is 11187 // specified 11188 if (Context.areCompatibleSveTypes(QualType(OriginalTarget, 0), 11189 QualType(Source, 0)) || 11190 Context.areLaxCompatibleSveTypes(QualType(OriginalTarget, 0), 11191 QualType(Source, 0))) 11192 return; 11193 11194 // If the vector cast is cast between two vectors of the same size, it is 11195 // a bitcast, not a conversion. 11196 if (Context.getTypeSize(Source) == Context.getTypeSize(Target)) 11197 return; 11198 11199 Source = SourceBT->getSveEltType(Context).getTypePtr(); 11200 } 11201 11202 if (TargetBT && TargetBT->isSveVLSBuiltinType()) 11203 Target = TargetBT->getSveEltType(Context).getTypePtr(); 11204 11205 // If the source is floating point... 11206 if (SourceBT && SourceBT->isFloatingPoint()) { 11207 // ...and the target is floating point... 11208 if (TargetBT && TargetBT->isFloatingPoint()) { 11209 // ...then warn if we're dropping FP rank. 11210 11211 int Order = getASTContext().getFloatingTypeSemanticOrder( 11212 QualType(SourceBT, 0), QualType(TargetBT, 0)); 11213 if (Order > 0) { 11214 // Don't warn about float constants that are precisely 11215 // representable in the target type. 11216 Expr::EvalResult result; 11217 if (E->EvaluateAsRValue(result, Context)) { 11218 // Value might be a float, a float vector, or a float complex. 11219 if (IsSameFloatAfterCast( 11220 result.Val, 11221 Context.getFloatTypeSemantics(QualType(TargetBT, 0)), 11222 Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) 11223 return; 11224 } 11225 11226 if (SourceMgr.isInSystemMacro(CC)) 11227 return; 11228 11229 DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_float_precision); 11230 } 11231 // ... or possibly if we're increasing rank, too 11232 else if (Order < 0) { 11233 if (SourceMgr.isInSystemMacro(CC)) 11234 return; 11235 11236 DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_double_promotion); 11237 } 11238 return; 11239 } 11240 11241 // If the target is integral, always warn. 11242 if (TargetBT && TargetBT->isInteger()) { 11243 if (SourceMgr.isInSystemMacro(CC)) 11244 return; 11245 11246 DiagnoseFloatingImpCast(*this, E, T, CC); 11247 } 11248 11249 // Detect the case where a call result is converted from floating-point to 11250 // to bool, and the final argument to the call is converted from bool, to 11251 // discover this typo: 11252 // 11253 // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;" 11254 // 11255 // FIXME: This is an incredibly special case; is there some more general 11256 // way to detect this class of misplaced-parentheses bug? 11257 if (Target->isBooleanType() && isa<CallExpr>(E)) { 11258 // Check last argument of function call to see if it is an 11259 // implicit cast from a type matching the type the result 11260 // is being cast to. 11261 CallExpr *CEx = cast<CallExpr>(E); 11262 if (unsigned NumArgs = CEx->getNumArgs()) { 11263 Expr *LastA = CEx->getArg(NumArgs - 1); 11264 Expr *InnerE = LastA->IgnoreParenImpCasts(); 11265 if (isa<ImplicitCastExpr>(LastA) && 11266 InnerE->getType()->isBooleanType()) { 11267 // Warn on this floating-point to bool conversion 11268 DiagnoseImpCast(*this, E, T, CC, 11269 diag::warn_impcast_floating_point_to_bool); 11270 } 11271 } 11272 } 11273 return; 11274 } 11275 11276 // Valid casts involving fixed point types should be accounted for here. 11277 if (Source->isFixedPointType()) { 11278 if (Target->isUnsaturatedFixedPointType()) { 11279 Expr::EvalResult Result; 11280 if (E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects, 11281 isConstantEvaluatedContext())) { 11282 llvm::APFixedPoint Value = Result.Val.getFixedPoint(); 11283 llvm::APFixedPoint MaxVal = Context.getFixedPointMax(T); 11284 llvm::APFixedPoint MinVal = Context.getFixedPointMin(T); 11285 if (Value > MaxVal || Value < MinVal) { 11286 DiagRuntimeBehavior(E->getExprLoc(), E, 11287 PDiag(diag::warn_impcast_fixed_point_range) 11288 << Value.toString() << T 11289 << E->getSourceRange() 11290 << clang::SourceRange(CC)); 11291 return; 11292 } 11293 } 11294 } else if (Target->isIntegerType()) { 11295 Expr::EvalResult Result; 11296 if (!isConstantEvaluatedContext() && 11297 E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects)) { 11298 llvm::APFixedPoint FXResult = Result.Val.getFixedPoint(); 11299 11300 bool Overflowed; 11301 llvm::APSInt IntResult = FXResult.convertToInt( 11302 Context.getIntWidth(T), Target->isSignedIntegerOrEnumerationType(), 11303 &Overflowed); 11304 11305 if (Overflowed) { 11306 DiagRuntimeBehavior(E->getExprLoc(), E, 11307 PDiag(diag::warn_impcast_fixed_point_range) 11308 << FXResult.toString() << T 11309 << E->getSourceRange() 11310 << clang::SourceRange(CC)); 11311 return; 11312 } 11313 } 11314 } 11315 } else if (Target->isUnsaturatedFixedPointType()) { 11316 if (Source->isIntegerType()) { 11317 Expr::EvalResult Result; 11318 if (!isConstantEvaluatedContext() && 11319 E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) { 11320 llvm::APSInt Value = Result.Val.getInt(); 11321 11322 bool Overflowed; 11323 llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue( 11324 Value, Context.getFixedPointSemantics(T), &Overflowed); 11325 11326 if (Overflowed) { 11327 DiagRuntimeBehavior(E->getExprLoc(), E, 11328 PDiag(diag::warn_impcast_fixed_point_range) 11329 << toString(Value, /*Radix=*/10) << T 11330 << E->getSourceRange() 11331 << clang::SourceRange(CC)); 11332 return; 11333 } 11334 } 11335 } 11336 } 11337 11338 // If we are casting an integer type to a floating point type without 11339 // initialization-list syntax, we might lose accuracy if the floating 11340 // point type has a narrower significand than the integer type. 11341 if (SourceBT && TargetBT && SourceBT->isIntegerType() && 11342 TargetBT->isFloatingType() && !IsListInit) { 11343 // Determine the number of precision bits in the source integer type. 11344 std::optional<IntRange> SourceRange = 11345 TryGetExprRange(Context, E, isConstantEvaluatedContext(), 11346 /*Approximate=*/true); 11347 if (!SourceRange) 11348 return; 11349 unsigned int SourcePrecision = SourceRange->Width; 11350 11351 // Determine the number of precision bits in the 11352 // target floating point type. 11353 unsigned int TargetPrecision = llvm::APFloatBase::semanticsPrecision( 11354 Context.getFloatTypeSemantics(QualType(TargetBT, 0))); 11355 11356 if (SourcePrecision > 0 && TargetPrecision > 0 && 11357 SourcePrecision > TargetPrecision) { 11358 11359 if (std::optional<llvm::APSInt> SourceInt = 11360 E->getIntegerConstantExpr(Context)) { 11361 // If the source integer is a constant, convert it to the target 11362 // floating point type. Issue a warning if the value changes 11363 // during the whole conversion. 11364 llvm::APFloat TargetFloatValue( 11365 Context.getFloatTypeSemantics(QualType(TargetBT, 0))); 11366 llvm::APFloat::opStatus ConversionStatus = 11367 TargetFloatValue.convertFromAPInt( 11368 *SourceInt, SourceBT->isSignedInteger(), 11369 llvm::APFloat::rmNearestTiesToEven); 11370 11371 if (ConversionStatus != llvm::APFloat::opOK) { 11372 SmallString<32> PrettySourceValue; 11373 SourceInt->toString(PrettySourceValue, 10); 11374 SmallString<32> PrettyTargetValue; 11375 TargetFloatValue.toString(PrettyTargetValue, TargetPrecision); 11376 11377 DiagRuntimeBehavior( 11378 E->getExprLoc(), E, 11379 PDiag(diag::warn_impcast_integer_float_precision_constant) 11380 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11381 << E->getSourceRange() << clang::SourceRange(CC)); 11382 } 11383 } else { 11384 // Otherwise, the implicit conversion may lose precision. 11385 DiagnoseImpCast(*this, E, T, CC, 11386 diag::warn_impcast_integer_float_precision); 11387 } 11388 } 11389 } 11390 11391 DiagnoseNullConversion(*this, E, T, CC); 11392 11393 DiscardMisalignedMemberAddress(Target, E); 11394 11395 if (Target->isBooleanType()) 11396 DiagnoseIntInBoolContext(*this, E); 11397 11398 if (!Source->isIntegerType() || !Target->isIntegerType()) 11399 return; 11400 11401 // TODO: remove this early return once the false positives for constant->bool 11402 // in templates, macros, etc, are reduced or removed. 11403 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) 11404 return; 11405 11406 if (ObjC().isSignedCharBool(T) && !Source->isCharType() && 11407 !E->isKnownToHaveBooleanValue(/*Semantic=*/false)) { 11408 return ObjC().adornBoolConversionDiagWithTernaryFixit( 11409 E, Diag(CC, diag::warn_impcast_int_to_objc_signed_char_bool) 11410 << E->getType()); 11411 } 11412 std::optional<IntRange> LikelySourceRange = TryGetExprRange( 11413 Context, E, isConstantEvaluatedContext(), /*Approximate=*/true); 11414 if (!LikelySourceRange) 11415 return; 11416 11417 IntRange SourceTypeRange = 11418 IntRange::forTargetOfCanonicalType(Context, Source); 11419 IntRange TargetRange = IntRange::forTargetOfCanonicalType(Context, Target); 11420 11421 if (LikelySourceRange->Width > TargetRange.Width) { 11422 // If the source is a constant, use a default-on diagnostic. 11423 // TODO: this should happen for bitfield stores, too. 11424 Expr::EvalResult Result; 11425 if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects, 11426 isConstantEvaluatedContext())) { 11427 llvm::APSInt Value(32); 11428 Value = Result.Val.getInt(); 11429 11430 if (SourceMgr.isInSystemMacro(CC)) 11431 return; 11432 11433 std::string PrettySourceValue = toString(Value, 10); 11434 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11435 11436 DiagRuntimeBehavior(E->getExprLoc(), E, 11437 PDiag(diag::warn_impcast_integer_precision_constant) 11438 << PrettySourceValue << PrettyTargetValue 11439 << E->getType() << T << E->getSourceRange() 11440 << SourceRange(CC)); 11441 return; 11442 } 11443 11444 // People want to build with -Wshorten-64-to-32 and not -Wconversion. 11445 if (SourceMgr.isInSystemMacro(CC)) 11446 return; 11447 11448 if (TargetRange.Width == 32 && Context.getIntWidth(E->getType()) == 64) 11449 return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_integer_64_32, 11450 /* pruneControlFlow */ true); 11451 return DiagnoseImpCast(*this, E, T, CC, 11452 diag::warn_impcast_integer_precision); 11453 } 11454 11455 if (TargetRange.Width > SourceTypeRange.Width) { 11456 if (auto *UO = dyn_cast<UnaryOperator>(E)) 11457 if (UO->getOpcode() == UO_Minus) 11458 if (Source->isUnsignedIntegerType()) { 11459 if (Target->isUnsignedIntegerType()) 11460 return DiagnoseImpCast(*this, E, T, CC, 11461 diag::warn_impcast_high_order_zero_bits); 11462 if (Target->isSignedIntegerType()) 11463 return DiagnoseImpCast(*this, E, T, CC, 11464 diag::warn_impcast_nonnegative_result); 11465 } 11466 } 11467 11468 if (TargetRange.Width == LikelySourceRange->Width && 11469 !TargetRange.NonNegative && LikelySourceRange->NonNegative && 11470 Source->isSignedIntegerType()) { 11471 // Warn when doing a signed to signed conversion, warn if the positive 11472 // source value is exactly the width of the target type, which will 11473 // cause a negative value to be stored. 11474 11475 Expr::EvalResult Result; 11476 if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects) && 11477 !SourceMgr.isInSystemMacro(CC)) { 11478 llvm::APSInt Value = Result.Val.getInt(); 11479 if (isSameWidthConstantConversion(*this, E, T, CC)) { 11480 std::string PrettySourceValue = toString(Value, 10); 11481 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11482 11483 Diag(E->getExprLoc(), 11484 PDiag(diag::warn_impcast_integer_precision_constant) 11485 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11486 << E->getSourceRange() << SourceRange(CC)); 11487 return; 11488 } 11489 } 11490 11491 // Fall through for non-constants to give a sign conversion warning. 11492 } 11493 11494 if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) && 11495 ((TargetRange.NonNegative && !LikelySourceRange->NonNegative) || 11496 (!TargetRange.NonNegative && LikelySourceRange->NonNegative && 11497 LikelySourceRange->Width == TargetRange.Width))) { 11498 if (SourceMgr.isInSystemMacro(CC)) 11499 return; 11500 11501 if (SourceBT && SourceBT->isInteger() && TargetBT && 11502 TargetBT->isInteger() && 11503 Source->isSignedIntegerType() == Target->isSignedIntegerType()) { 11504 return; 11505 } 11506 11507 unsigned DiagID = diag::warn_impcast_integer_sign; 11508 11509 // Traditionally, gcc has warned about this under -Wsign-compare. 11510 // We also want to warn about it in -Wconversion. 11511 // So if -Wconversion is off, use a completely identical diagnostic 11512 // in the sign-compare group. 11513 // The conditional-checking code will 11514 if (ICContext) { 11515 DiagID = diag::warn_impcast_integer_sign_conditional; 11516 *ICContext = true; 11517 } 11518 11519 return DiagnoseImpCast(*this, E, T, CC, DiagID); 11520 } 11521 11522 // Diagnose conversions between different enumeration types. 11523 // In C, we pretend that the type of an EnumConstantDecl is its enumeration 11524 // type, to give us better diagnostics. 11525 QualType SourceType = E->getEnumCoercedType(Context); 11526 Source = Context.getCanonicalType(SourceType).getTypePtr(); 11527 11528 if (const EnumType *SourceEnum = Source->getAs<EnumType>()) 11529 if (const EnumType *TargetEnum = Target->getAs<EnumType>()) 11530 if (SourceEnum->getDecl()->hasNameForLinkage() && 11531 TargetEnum->getDecl()->hasNameForLinkage() && 11532 SourceEnum != TargetEnum) { 11533 if (SourceMgr.isInSystemMacro(CC)) 11534 return; 11535 11536 return DiagnoseImpCast(*this, E, SourceType, T, CC, 11537 diag::warn_impcast_different_enum_types); 11538 } 11539 } 11540 11541 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, 11542 SourceLocation CC, QualType T); 11543 11544 static void CheckConditionalOperand(Sema &S, Expr *E, QualType T, 11545 SourceLocation CC, bool &ICContext) { 11546 E = E->IgnoreParenImpCasts(); 11547 // Diagnose incomplete type for second or third operand in C. 11548 if (!S.getLangOpts().CPlusPlus && E->getType()->isRecordType()) 11549 S.RequireCompleteExprType(E, diag::err_incomplete_type); 11550 11551 if (auto *CO = dyn_cast<AbstractConditionalOperator>(E)) 11552 return CheckConditionalOperator(S, CO, CC, T); 11553 11554 AnalyzeImplicitConversions(S, E, CC); 11555 if (E->getType() != T) 11556 return S.CheckImplicitConversion(E, T, CC, &ICContext); 11557 } 11558 11559 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, 11560 SourceLocation CC, QualType T) { 11561 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); 11562 11563 Expr *TrueExpr = E->getTrueExpr(); 11564 if (auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) 11565 TrueExpr = BCO->getCommon(); 11566 11567 bool Suspicious = false; 11568 CheckConditionalOperand(S, TrueExpr, T, CC, Suspicious); 11569 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); 11570 11571 if (T->isBooleanType()) 11572 DiagnoseIntInBoolContext(S, E); 11573 11574 // If -Wconversion would have warned about either of the candidates 11575 // for a signedness conversion to the context type... 11576 if (!Suspicious) return; 11577 11578 // ...but it's currently ignored... 11579 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) 11580 return; 11581 11582 // ...then check whether it would have warned about either of the 11583 // candidates for a signedness conversion to the condition type. 11584 if (E->getType() == T) return; 11585 11586 Suspicious = false; 11587 S.CheckImplicitConversion(TrueExpr->IgnoreParenImpCasts(), E->getType(), CC, 11588 &Suspicious); 11589 if (!Suspicious) 11590 S.CheckImplicitConversion(E->getFalseExpr()->IgnoreParenImpCasts(), 11591 E->getType(), CC, &Suspicious); 11592 } 11593 11594 /// Check conversion of given expression to boolean. 11595 /// Input argument E is a logical expression. 11596 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { 11597 // Run the bool-like conversion checks only for C since there bools are 11598 // still not used as the return type from "boolean" operators or as the input 11599 // type for conditional operators. 11600 if (S.getLangOpts().CPlusPlus) 11601 return; 11602 if (E->IgnoreParenImpCasts()->getType()->isAtomicType()) 11603 return; 11604 S.CheckImplicitConversion(E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); 11605 } 11606 11607 namespace { 11608 struct AnalyzeImplicitConversionsWorkItem { 11609 Expr *E; 11610 SourceLocation CC; 11611 bool IsListInit; 11612 }; 11613 } 11614 11615 /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions 11616 /// that should be visited are added to WorkList. 11617 static void AnalyzeImplicitConversions( 11618 Sema &S, AnalyzeImplicitConversionsWorkItem Item, 11619 llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) { 11620 Expr *OrigE = Item.E; 11621 SourceLocation CC = Item.CC; 11622 11623 QualType T = OrigE->getType(); 11624 Expr *E = OrigE->IgnoreParenImpCasts(); 11625 11626 // Propagate whether we are in a C++ list initialization expression. 11627 // If so, we do not issue warnings for implicit int-float conversion 11628 // precision loss, because C++11 narrowing already handles it. 11629 bool IsListInit = Item.IsListInit || 11630 (isa<InitListExpr>(OrigE) && S.getLangOpts().CPlusPlus); 11631 11632 if (E->isTypeDependent() || E->isValueDependent()) 11633 return; 11634 11635 Expr *SourceExpr = E; 11636 // Examine, but don't traverse into the source expression of an 11637 // OpaqueValueExpr, since it may have multiple parents and we don't want to 11638 // emit duplicate diagnostics. Its fine to examine the form or attempt to 11639 // evaluate it in the context of checking the specific conversion to T though. 11640 if (auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 11641 if (auto *Src = OVE->getSourceExpr()) 11642 SourceExpr = Src; 11643 11644 if (const auto *UO = dyn_cast<UnaryOperator>(SourceExpr)) 11645 if (UO->getOpcode() == UO_Not && 11646 UO->getSubExpr()->isKnownToHaveBooleanValue()) 11647 S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool) 11648 << OrigE->getSourceRange() << T->isBooleanType() 11649 << FixItHint::CreateReplacement(UO->getBeginLoc(), "!"); 11650 11651 if (const auto *BO = dyn_cast<BinaryOperator>(SourceExpr)) 11652 if ((BO->getOpcode() == BO_And || BO->getOpcode() == BO_Or) && 11653 BO->getLHS()->isKnownToHaveBooleanValue() && 11654 BO->getRHS()->isKnownToHaveBooleanValue() && 11655 BO->getLHS()->HasSideEffects(S.Context) && 11656 BO->getRHS()->HasSideEffects(S.Context)) { 11657 SourceManager &SM = S.getSourceManager(); 11658 const LangOptions &LO = S.getLangOpts(); 11659 SourceLocation BLoc = BO->getOperatorLoc(); 11660 SourceLocation ELoc = Lexer::getLocForEndOfToken(BLoc, 0, SM, LO); 11661 StringRef SR = clang::Lexer::getSourceText( 11662 clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO); 11663 // To reduce false positives, only issue the diagnostic if the operator 11664 // is explicitly spelled as a punctuator. This suppresses the diagnostic 11665 // when using 'bitand' or 'bitor' either as keywords in C++ or as macros 11666 // in C, along with other macro spellings the user might invent. 11667 if (SR.str() == "&" || SR.str() == "|") { 11668 11669 S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical) 11670 << (BO->getOpcode() == BO_And ? "&" : "|") 11671 << OrigE->getSourceRange() 11672 << FixItHint::CreateReplacement( 11673 BO->getOperatorLoc(), 11674 (BO->getOpcode() == BO_And ? "&&" : "||")); 11675 S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int); 11676 } 11677 } 11678 11679 // For conditional operators, we analyze the arguments as if they 11680 // were being fed directly into the output. 11681 if (auto *CO = dyn_cast<AbstractConditionalOperator>(SourceExpr)) { 11682 CheckConditionalOperator(S, CO, CC, T); 11683 return; 11684 } 11685 11686 // Check implicit argument conversions for function calls. 11687 if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr)) 11688 CheckImplicitArgumentConversions(S, Call, CC); 11689 11690 // Go ahead and check any implicit conversions we might have skipped. 11691 // The non-canonical typecheck is just an optimization; 11692 // CheckImplicitConversion will filter out dead implicit conversions. 11693 if (SourceExpr->getType() != T) 11694 S.CheckImplicitConversion(SourceExpr, T, CC, nullptr, IsListInit); 11695 11696 // Now continue drilling into this expression. 11697 11698 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 11699 // The bound subexpressions in a PseudoObjectExpr are not reachable 11700 // as transitive children. 11701 // FIXME: Use a more uniform representation for this. 11702 for (auto *SE : POE->semantics()) 11703 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE)) 11704 WorkList.push_back({OVE->getSourceExpr(), CC, IsListInit}); 11705 } 11706 11707 // Skip past explicit casts. 11708 if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) { 11709 E = CE->getSubExpr()->IgnoreParenImpCasts(); 11710 if (!CE->getType()->isVoidType() && E->getType()->isAtomicType()) 11711 S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 11712 WorkList.push_back({E, CC, IsListInit}); 11713 return; 11714 } 11715 11716 if (auto *OutArgE = dyn_cast<HLSLOutArgExpr>(E)) { 11717 WorkList.push_back({OutArgE->getArgLValue(), CC, IsListInit}); 11718 // The base expression is only used to initialize the parameter for 11719 // arguments to `inout` parameters, so we only traverse down the base 11720 // expression for `inout` cases. 11721 if (OutArgE->isInOut()) 11722 WorkList.push_back( 11723 {OutArgE->getCastedTemporary()->getSourceExpr(), CC, IsListInit}); 11724 WorkList.push_back({OutArgE->getWritebackCast(), CC, IsListInit}); 11725 return; 11726 } 11727 11728 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 11729 // Do a somewhat different check with comparison operators. 11730 if (BO->isComparisonOp()) 11731 return AnalyzeComparison(S, BO); 11732 11733 // And with simple assignments. 11734 if (BO->getOpcode() == BO_Assign) 11735 return AnalyzeAssignment(S, BO); 11736 // And with compound assignments. 11737 if (BO->isAssignmentOp()) 11738 return AnalyzeCompoundAssignment(S, BO); 11739 } 11740 11741 // These break the otherwise-useful invariant below. Fortunately, 11742 // we don't really need to recurse into them, because any internal 11743 // expressions should have been analyzed already when they were 11744 // built into statements. 11745 if (isa<StmtExpr>(E)) return; 11746 11747 // Don't descend into unevaluated contexts. 11748 if (isa<UnaryExprOrTypeTraitExpr>(E)) return; 11749 11750 // Now just recurse over the expression's children. 11751 CC = E->getExprLoc(); 11752 BinaryOperator *BO = dyn_cast<BinaryOperator>(E); 11753 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; 11754 for (Stmt *SubStmt : E->children()) { 11755 Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt); 11756 if (!ChildExpr) 11757 continue; 11758 11759 if (auto *CSE = dyn_cast<CoroutineSuspendExpr>(E)) 11760 if (ChildExpr == CSE->getOperand()) 11761 // Do not recurse over a CoroutineSuspendExpr's operand. 11762 // The operand is also a subexpression of getCommonExpr(), and 11763 // recursing into it directly would produce duplicate diagnostics. 11764 continue; 11765 11766 if (IsLogicalAndOperator && 11767 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) 11768 // Ignore checking string literals that are in logical and operators. 11769 // This is a common pattern for asserts. 11770 continue; 11771 WorkList.push_back({ChildExpr, CC, IsListInit}); 11772 } 11773 11774 if (BO && BO->isLogicalOp()) { 11775 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); 11776 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11777 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11778 11779 SubExpr = BO->getRHS()->IgnoreParenImpCasts(); 11780 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11781 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11782 } 11783 11784 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) { 11785 if (U->getOpcode() == UO_LNot) { 11786 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); 11787 } else if (U->getOpcode() != UO_AddrOf) { 11788 if (U->getSubExpr()->getType()->isAtomicType()) 11789 S.Diag(U->getSubExpr()->getBeginLoc(), 11790 diag::warn_atomic_implicit_seq_cst); 11791 } 11792 } 11793 } 11794 11795 /// AnalyzeImplicitConversions - Find and report any interesting 11796 /// implicit conversions in the given expression. There are a couple 11797 /// of competing diagnostics here, -Wconversion and -Wsign-compare. 11798 static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC, 11799 bool IsListInit/*= false*/) { 11800 llvm::SmallVector<AnalyzeImplicitConversionsWorkItem, 16> WorkList; 11801 WorkList.push_back({OrigE, CC, IsListInit}); 11802 while (!WorkList.empty()) 11803 AnalyzeImplicitConversions(S, WorkList.pop_back_val(), WorkList); 11804 } 11805 11806 // Helper function for Sema::DiagnoseAlwaysNonNullPointer. 11807 // Returns true when emitting a warning about taking the address of a reference. 11808 static bool CheckForReference(Sema &SemaRef, const Expr *E, 11809 const PartialDiagnostic &PD) { 11810 E = E->IgnoreParenImpCasts(); 11811 11812 const FunctionDecl *FD = nullptr; 11813 11814 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 11815 if (!DRE->getDecl()->getType()->isReferenceType()) 11816 return false; 11817 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11818 if (!M->getMemberDecl()->getType()->isReferenceType()) 11819 return false; 11820 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { 11821 if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) 11822 return false; 11823 FD = Call->getDirectCallee(); 11824 } else { 11825 return false; 11826 } 11827 11828 SemaRef.Diag(E->getExprLoc(), PD); 11829 11830 // If possible, point to location of function. 11831 if (FD) { 11832 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; 11833 } 11834 11835 return true; 11836 } 11837 11838 // Returns true if the SourceLocation is expanded from any macro body. 11839 // Returns false if the SourceLocation is invalid, is from not in a macro 11840 // expansion, or is from expanded from a top-level macro argument. 11841 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { 11842 if (Loc.isInvalid()) 11843 return false; 11844 11845 while (Loc.isMacroID()) { 11846 if (SM.isMacroBodyExpansion(Loc)) 11847 return true; 11848 Loc = SM.getImmediateMacroCallerLoc(Loc); 11849 } 11850 11851 return false; 11852 } 11853 11854 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, 11855 Expr::NullPointerConstantKind NullKind, 11856 bool IsEqual, SourceRange Range) { 11857 if (!E) 11858 return; 11859 11860 // Don't warn inside macros. 11861 if (E->getExprLoc().isMacroID()) { 11862 const SourceManager &SM = getSourceManager(); 11863 if (IsInAnyMacroBody(SM, E->getExprLoc()) || 11864 IsInAnyMacroBody(SM, Range.getBegin())) 11865 return; 11866 } 11867 E = E->IgnoreImpCasts(); 11868 11869 const bool IsCompare = NullKind != Expr::NPCK_NotNull; 11870 11871 if (isa<CXXThisExpr>(E)) { 11872 unsigned DiagID = IsCompare ? diag::warn_this_null_compare 11873 : diag::warn_this_bool_conversion; 11874 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; 11875 return; 11876 } 11877 11878 bool IsAddressOf = false; 11879 11880 if (auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { 11881 if (UO->getOpcode() != UO_AddrOf) 11882 return; 11883 IsAddressOf = true; 11884 E = UO->getSubExpr(); 11885 } 11886 11887 if (IsAddressOf) { 11888 unsigned DiagID = IsCompare 11889 ? diag::warn_address_of_reference_null_compare 11890 : diag::warn_address_of_reference_bool_conversion; 11891 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range 11892 << IsEqual; 11893 if (CheckForReference(*this, E, PD)) { 11894 return; 11895 } 11896 } 11897 11898 auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) { 11899 bool IsParam = isa<NonNullAttr>(NonnullAttr); 11900 std::string Str; 11901 llvm::raw_string_ostream S(Str); 11902 E->printPretty(S, nullptr, getPrintingPolicy()); 11903 unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare 11904 : diag::warn_cast_nonnull_to_bool; 11905 Diag(E->getExprLoc(), DiagID) << IsParam << S.str() 11906 << E->getSourceRange() << Range << IsEqual; 11907 Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam; 11908 }; 11909 11910 // If we have a CallExpr that is tagged with returns_nonnull, we can complain. 11911 if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) { 11912 if (auto *Callee = Call->getDirectCallee()) { 11913 if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) { 11914 ComplainAboutNonnullParamOrCall(A); 11915 return; 11916 } 11917 } 11918 } 11919 11920 // Complain if we are converting a lambda expression to a boolean value 11921 // outside of instantiation. 11922 if (!inTemplateInstantiation()) { 11923 if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) { 11924 if (const auto *MRecordDecl = MCallExpr->getRecordDecl(); 11925 MRecordDecl && MRecordDecl->isLambda()) { 11926 Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool) 11927 << /*LambdaPointerConversionOperatorType=*/3 11928 << MRecordDecl->getSourceRange() << Range << IsEqual; 11929 return; 11930 } 11931 } 11932 } 11933 11934 // Expect to find a single Decl. Skip anything more complicated. 11935 ValueDecl *D = nullptr; 11936 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { 11937 D = R->getDecl(); 11938 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11939 D = M->getMemberDecl(); 11940 } 11941 11942 // Weak Decls can be null. 11943 if (!D || D->isWeak()) 11944 return; 11945 11946 // Check for parameter decl with nonnull attribute 11947 if (const auto* PV = dyn_cast<ParmVarDecl>(D)) { 11948 if (getCurFunction() && 11949 !getCurFunction()->ModifiedNonNullParams.count(PV)) { 11950 if (const Attr *A = PV->getAttr<NonNullAttr>()) { 11951 ComplainAboutNonnullParamOrCall(A); 11952 return; 11953 } 11954 11955 if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { 11956 // Skip function template not specialized yet. 11957 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) 11958 return; 11959 auto ParamIter = llvm::find(FD->parameters(), PV); 11960 assert(ParamIter != FD->param_end()); 11961 unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); 11962 11963 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { 11964 if (!NonNull->args_size()) { 11965 ComplainAboutNonnullParamOrCall(NonNull); 11966 return; 11967 } 11968 11969 for (const ParamIdx &ArgNo : NonNull->args()) { 11970 if (ArgNo.getASTIndex() == ParamNo) { 11971 ComplainAboutNonnullParamOrCall(NonNull); 11972 return; 11973 } 11974 } 11975 } 11976 } 11977 } 11978 } 11979 11980 QualType T = D->getType(); 11981 const bool IsArray = T->isArrayType(); 11982 const bool IsFunction = T->isFunctionType(); 11983 11984 // Address of function is used to silence the function warning. 11985 if (IsAddressOf && IsFunction) { 11986 return; 11987 } 11988 11989 // Found nothing. 11990 if (!IsAddressOf && !IsFunction && !IsArray) 11991 return; 11992 11993 // Pretty print the expression for the diagnostic. 11994 std::string Str; 11995 llvm::raw_string_ostream S(Str); 11996 E->printPretty(S, nullptr, getPrintingPolicy()); 11997 11998 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare 11999 : diag::warn_impcast_pointer_to_bool; 12000 enum { 12001 AddressOf, 12002 FunctionPointer, 12003 ArrayPointer 12004 } DiagType; 12005 if (IsAddressOf) 12006 DiagType = AddressOf; 12007 else if (IsFunction) 12008 DiagType = FunctionPointer; 12009 else if (IsArray) 12010 DiagType = ArrayPointer; 12011 else 12012 llvm_unreachable("Could not determine diagnostic."); 12013 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() 12014 << Range << IsEqual; 12015 12016 if (!IsFunction) 12017 return; 12018 12019 // Suggest '&' to silence the function warning. 12020 Diag(E->getExprLoc(), diag::note_function_warning_silence) 12021 << FixItHint::CreateInsertion(E->getBeginLoc(), "&"); 12022 12023 // Check to see if '()' fixit should be emitted. 12024 QualType ReturnType; 12025 UnresolvedSet<4> NonTemplateOverloads; 12026 tryExprAsCall(*E, ReturnType, NonTemplateOverloads); 12027 if (ReturnType.isNull()) 12028 return; 12029 12030 if (IsCompare) { 12031 // There are two cases here. If there is null constant, the only suggest 12032 // for a pointer return type. If the null is 0, then suggest if the return 12033 // type is a pointer or an integer type. 12034 if (!ReturnType->isPointerType()) { 12035 if (NullKind == Expr::NPCK_ZeroExpression || 12036 NullKind == Expr::NPCK_ZeroLiteral) { 12037 if (!ReturnType->isIntegerType()) 12038 return; 12039 } else { 12040 return; 12041 } 12042 } 12043 } else { // !IsCompare 12044 // For function to bool, only suggest if the function pointer has bool 12045 // return type. 12046 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) 12047 return; 12048 } 12049 Diag(E->getExprLoc(), diag::note_function_to_function_call) 12050 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()"); 12051 } 12052 12053 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { 12054 // Don't diagnose in unevaluated contexts. 12055 if (isUnevaluatedContext()) 12056 return; 12057 12058 // Don't diagnose for value- or type-dependent expressions. 12059 if (E->isTypeDependent() || E->isValueDependent()) 12060 return; 12061 12062 // Check for array bounds violations in cases where the check isn't triggered 12063 // elsewhere for other Expr types (like BinaryOperators), e.g. when an 12064 // ArraySubscriptExpr is on the RHS of a variable initialization. 12065 CheckArrayAccess(E); 12066 12067 // This is not the right CC for (e.g.) a variable initialization. 12068 AnalyzeImplicitConversions(*this, E, CC); 12069 } 12070 12071 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { 12072 ::CheckBoolLikeConversion(*this, E, CC); 12073 } 12074 12075 void Sema::CheckForIntOverflow (const Expr *E) { 12076 // Use a work list to deal with nested struct initializers. 12077 SmallVector<const Expr *, 2> Exprs(1, E); 12078 12079 do { 12080 const Expr *OriginalE = Exprs.pop_back_val(); 12081 const Expr *E = OriginalE->IgnoreParenCasts(); 12082 12083 if (isa<BinaryOperator, UnaryOperator>(E)) { 12084 E->EvaluateForOverflow(Context); 12085 continue; 12086 } 12087 12088 if (const auto *InitList = dyn_cast<InitListExpr>(OriginalE)) 12089 Exprs.append(InitList->inits().begin(), InitList->inits().end()); 12090 else if (isa<ObjCBoxedExpr>(OriginalE)) 12091 E->EvaluateForOverflow(Context); 12092 else if (const auto *Call = dyn_cast<CallExpr>(E)) 12093 Exprs.append(Call->arg_begin(), Call->arg_end()); 12094 else if (const auto *Message = dyn_cast<ObjCMessageExpr>(E)) 12095 Exprs.append(Message->arg_begin(), Message->arg_end()); 12096 else if (const auto *Construct = dyn_cast<CXXConstructExpr>(E)) 12097 Exprs.append(Construct->arg_begin(), Construct->arg_end()); 12098 else if (const auto *Temporary = dyn_cast<CXXBindTemporaryExpr>(E)) 12099 Exprs.push_back(Temporary->getSubExpr()); 12100 else if (const auto *Array = dyn_cast<ArraySubscriptExpr>(E)) 12101 Exprs.push_back(Array->getIdx()); 12102 else if (const auto *Compound = dyn_cast<CompoundLiteralExpr>(E)) 12103 Exprs.push_back(Compound->getInitializer()); 12104 else if (const auto *New = dyn_cast<CXXNewExpr>(E); 12105 New && New->isArray()) { 12106 if (auto ArraySize = New->getArraySize()) 12107 Exprs.push_back(*ArraySize); 12108 } else if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(OriginalE)) 12109 Exprs.push_back(MTE->getSubExpr()); 12110 } while (!Exprs.empty()); 12111 } 12112 12113 namespace { 12114 12115 /// Visitor for expressions which looks for unsequenced operations on the 12116 /// same object. 12117 class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> { 12118 using Base = ConstEvaluatedExprVisitor<SequenceChecker>; 12119 12120 /// A tree of sequenced regions within an expression. Two regions are 12121 /// unsequenced if one is an ancestor or a descendent of the other. When we 12122 /// finish processing an expression with sequencing, such as a comma 12123 /// expression, we fold its tree nodes into its parent, since they are 12124 /// unsequenced with respect to nodes we will visit later. 12125 class SequenceTree { 12126 struct Value { 12127 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} 12128 unsigned Parent : 31; 12129 LLVM_PREFERRED_TYPE(bool) 12130 unsigned Merged : 1; 12131 }; 12132 SmallVector<Value, 8> Values; 12133 12134 public: 12135 /// A region within an expression which may be sequenced with respect 12136 /// to some other region. 12137 class Seq { 12138 friend class SequenceTree; 12139 12140 unsigned Index; 12141 12142 explicit Seq(unsigned N) : Index(N) {} 12143 12144 public: 12145 Seq() : Index(0) {} 12146 }; 12147 12148 SequenceTree() { Values.push_back(Value(0)); } 12149 Seq root() const { return Seq(0); } 12150 12151 /// Create a new sequence of operations, which is an unsequenced 12152 /// subset of \p Parent. This sequence of operations is sequenced with 12153 /// respect to other children of \p Parent. 12154 Seq allocate(Seq Parent) { 12155 Values.push_back(Value(Parent.Index)); 12156 return Seq(Values.size() - 1); 12157 } 12158 12159 /// Merge a sequence of operations into its parent. 12160 void merge(Seq S) { 12161 Values[S.Index].Merged = true; 12162 } 12163 12164 /// Determine whether two operations are unsequenced. This operation 12165 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old 12166 /// should have been merged into its parent as appropriate. 12167 bool isUnsequenced(Seq Cur, Seq Old) { 12168 unsigned C = representative(Cur.Index); 12169 unsigned Target = representative(Old.Index); 12170 while (C >= Target) { 12171 if (C == Target) 12172 return true; 12173 C = Values[C].Parent; 12174 } 12175 return false; 12176 } 12177 12178 private: 12179 /// Pick a representative for a sequence. 12180 unsigned representative(unsigned K) { 12181 if (Values[K].Merged) 12182 // Perform path compression as we go. 12183 return Values[K].Parent = representative(Values[K].Parent); 12184 return K; 12185 } 12186 }; 12187 12188 /// An object for which we can track unsequenced uses. 12189 using Object = const NamedDecl *; 12190 12191 /// Different flavors of object usage which we track. We only track the 12192 /// least-sequenced usage of each kind. 12193 enum UsageKind { 12194 /// A read of an object. Multiple unsequenced reads are OK. 12195 UK_Use, 12196 12197 /// A modification of an object which is sequenced before the value 12198 /// computation of the expression, such as ++n in C++. 12199 UK_ModAsValue, 12200 12201 /// A modification of an object which is not sequenced before the value 12202 /// computation of the expression, such as n++. 12203 UK_ModAsSideEffect, 12204 12205 UK_Count = UK_ModAsSideEffect + 1 12206 }; 12207 12208 /// Bundle together a sequencing region and the expression corresponding 12209 /// to a specific usage. One Usage is stored for each usage kind in UsageInfo. 12210 struct Usage { 12211 const Expr *UsageExpr = nullptr; 12212 SequenceTree::Seq Seq; 12213 12214 Usage() = default; 12215 }; 12216 12217 struct UsageInfo { 12218 Usage Uses[UK_Count]; 12219 12220 /// Have we issued a diagnostic for this object already? 12221 bool Diagnosed = false; 12222 12223 UsageInfo(); 12224 }; 12225 using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>; 12226 12227 Sema &SemaRef; 12228 12229 /// Sequenced regions within the expression. 12230 SequenceTree Tree; 12231 12232 /// Declaration modifications and references which we have seen. 12233 UsageInfoMap UsageMap; 12234 12235 /// The region we are currently within. 12236 SequenceTree::Seq Region; 12237 12238 /// Filled in with declarations which were modified as a side-effect 12239 /// (that is, post-increment operations). 12240 SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr; 12241 12242 /// Expressions to check later. We defer checking these to reduce 12243 /// stack usage. 12244 SmallVectorImpl<const Expr *> &WorkList; 12245 12246 /// RAII object wrapping the visitation of a sequenced subexpression of an 12247 /// expression. At the end of this process, the side-effects of the evaluation 12248 /// become sequenced with respect to the value computation of the result, so 12249 /// we downgrade any UK_ModAsSideEffect within the evaluation to 12250 /// UK_ModAsValue. 12251 struct SequencedSubexpression { 12252 SequencedSubexpression(SequenceChecker &Self) 12253 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { 12254 Self.ModAsSideEffect = &ModAsSideEffect; 12255 } 12256 12257 ~SequencedSubexpression() { 12258 for (const std::pair<Object, Usage> &M : llvm::reverse(ModAsSideEffect)) { 12259 // Add a new usage with usage kind UK_ModAsValue, and then restore 12260 // the previous usage with UK_ModAsSideEffect (thus clearing it if 12261 // the previous one was empty). 12262 UsageInfo &UI = Self.UsageMap[M.first]; 12263 auto &SideEffectUsage = UI.Uses[UK_ModAsSideEffect]; 12264 Self.addUsage(M.first, UI, SideEffectUsage.UsageExpr, UK_ModAsValue); 12265 SideEffectUsage = M.second; 12266 } 12267 Self.ModAsSideEffect = OldModAsSideEffect; 12268 } 12269 12270 SequenceChecker &Self; 12271 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; 12272 SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect; 12273 }; 12274 12275 /// RAII object wrapping the visitation of a subexpression which we might 12276 /// choose to evaluate as a constant. If any subexpression is evaluated and 12277 /// found to be non-constant, this allows us to suppress the evaluation of 12278 /// the outer expression. 12279 class EvaluationTracker { 12280 public: 12281 EvaluationTracker(SequenceChecker &Self) 12282 : Self(Self), Prev(Self.EvalTracker) { 12283 Self.EvalTracker = this; 12284 } 12285 12286 ~EvaluationTracker() { 12287 Self.EvalTracker = Prev; 12288 if (Prev) 12289 Prev->EvalOK &= EvalOK; 12290 } 12291 12292 bool evaluate(const Expr *E, bool &Result) { 12293 if (!EvalOK || E->isValueDependent()) 12294 return false; 12295 EvalOK = E->EvaluateAsBooleanCondition( 12296 Result, Self.SemaRef.Context, 12297 Self.SemaRef.isConstantEvaluatedContext()); 12298 return EvalOK; 12299 } 12300 12301 private: 12302 SequenceChecker &Self; 12303 EvaluationTracker *Prev; 12304 bool EvalOK = true; 12305 } *EvalTracker = nullptr; 12306 12307 /// Find the object which is produced by the specified expression, 12308 /// if any. 12309 Object getObject(const Expr *E, bool Mod) const { 12310 E = E->IgnoreParenCasts(); 12311 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 12312 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) 12313 return getObject(UO->getSubExpr(), Mod); 12314 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 12315 if (BO->getOpcode() == BO_Comma) 12316 return getObject(BO->getRHS(), Mod); 12317 if (Mod && BO->isAssignmentOp()) 12318 return getObject(BO->getLHS(), Mod); 12319 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 12320 // FIXME: Check for more interesting cases, like "x.n = ++x.n". 12321 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) 12322 return ME->getMemberDecl(); 12323 } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 12324 // FIXME: If this is a reference, map through to its value. 12325 return DRE->getDecl(); 12326 return nullptr; 12327 } 12328 12329 /// Note that an object \p O was modified or used by an expression 12330 /// \p UsageExpr with usage kind \p UK. \p UI is the \p UsageInfo for 12331 /// the object \p O as obtained via the \p UsageMap. 12332 void addUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, UsageKind UK) { 12333 // Get the old usage for the given object and usage kind. 12334 Usage &U = UI.Uses[UK]; 12335 if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) { 12336 // If we have a modification as side effect and are in a sequenced 12337 // subexpression, save the old Usage so that we can restore it later 12338 // in SequencedSubexpression::~SequencedSubexpression. 12339 if (UK == UK_ModAsSideEffect && ModAsSideEffect) 12340 ModAsSideEffect->push_back(std::make_pair(O, U)); 12341 // Then record the new usage with the current sequencing region. 12342 U.UsageExpr = UsageExpr; 12343 U.Seq = Region; 12344 } 12345 } 12346 12347 /// Check whether a modification or use of an object \p O in an expression 12348 /// \p UsageExpr conflicts with a prior usage of kind \p OtherKind. \p UI is 12349 /// the \p UsageInfo for the object \p O as obtained via the \p UsageMap. 12350 /// \p IsModMod is true when we are checking for a mod-mod unsequenced 12351 /// usage and false we are checking for a mod-use unsequenced usage. 12352 void checkUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, 12353 UsageKind OtherKind, bool IsModMod) { 12354 if (UI.Diagnosed) 12355 return; 12356 12357 const Usage &U = UI.Uses[OtherKind]; 12358 if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) 12359 return; 12360 12361 const Expr *Mod = U.UsageExpr; 12362 const Expr *ModOrUse = UsageExpr; 12363 if (OtherKind == UK_Use) 12364 std::swap(Mod, ModOrUse); 12365 12366 SemaRef.DiagRuntimeBehavior( 12367 Mod->getExprLoc(), {Mod, ModOrUse}, 12368 SemaRef.PDiag(IsModMod ? diag::warn_unsequenced_mod_mod 12369 : diag::warn_unsequenced_mod_use) 12370 << O << SourceRange(ModOrUse->getExprLoc())); 12371 UI.Diagnosed = true; 12372 } 12373 12374 // A note on note{Pre, Post}{Use, Mod}: 12375 // 12376 // (It helps to follow the algorithm with an expression such as 12377 // "((++k)++, k) = k" or "k = (k++, k++)". Both contain unsequenced 12378 // operations before C++17 and both are well-defined in C++17). 12379 // 12380 // When visiting a node which uses/modify an object we first call notePreUse 12381 // or notePreMod before visiting its sub-expression(s). At this point the 12382 // children of the current node have not yet been visited and so the eventual 12383 // uses/modifications resulting from the children of the current node have not 12384 // been recorded yet. 12385 // 12386 // We then visit the children of the current node. After that notePostUse or 12387 // notePostMod is called. These will 1) detect an unsequenced modification 12388 // as side effect (as in "k++ + k") and 2) add a new usage with the 12389 // appropriate usage kind. 12390 // 12391 // We also have to be careful that some operation sequences modification as 12392 // side effect as well (for example: || or ,). To account for this we wrap 12393 // the visitation of such a sub-expression (for example: the LHS of || or ,) 12394 // with SequencedSubexpression. SequencedSubexpression is an RAII object 12395 // which record usages which are modifications as side effect, and then 12396 // downgrade them (or more accurately restore the previous usage which was a 12397 // modification as side effect) when exiting the scope of the sequenced 12398 // subexpression. 12399 12400 void notePreUse(Object O, const Expr *UseExpr) { 12401 UsageInfo &UI = UsageMap[O]; 12402 // Uses conflict with other modifications. 12403 checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/false); 12404 } 12405 12406 void notePostUse(Object O, const Expr *UseExpr) { 12407 UsageInfo &UI = UsageMap[O]; 12408 checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsSideEffect, 12409 /*IsModMod=*/false); 12410 addUsage(O, UI, UseExpr, /*UsageKind=*/UK_Use); 12411 } 12412 12413 void notePreMod(Object O, const Expr *ModExpr) { 12414 UsageInfo &UI = UsageMap[O]; 12415 // Modifications conflict with other modifications and with uses. 12416 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/true); 12417 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_Use, /*IsModMod=*/false); 12418 } 12419 12420 void notePostMod(Object O, const Expr *ModExpr, UsageKind UK) { 12421 UsageInfo &UI = UsageMap[O]; 12422 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsSideEffect, 12423 /*IsModMod=*/true); 12424 addUsage(O, UI, ModExpr, /*UsageKind=*/UK); 12425 } 12426 12427 public: 12428 SequenceChecker(Sema &S, const Expr *E, 12429 SmallVectorImpl<const Expr *> &WorkList) 12430 : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) { 12431 Visit(E); 12432 // Silence a -Wunused-private-field since WorkList is now unused. 12433 // TODO: Evaluate if it can be used, and if not remove it. 12434 (void)this->WorkList; 12435 } 12436 12437 void VisitStmt(const Stmt *S) { 12438 // Skip all statements which aren't expressions for now. 12439 } 12440 12441 void VisitExpr(const Expr *E) { 12442 // By default, just recurse to evaluated subexpressions. 12443 Base::VisitStmt(E); 12444 } 12445 12446 void VisitCoroutineSuspendExpr(const CoroutineSuspendExpr *CSE) { 12447 for (auto *Sub : CSE->children()) { 12448 const Expr *ChildExpr = dyn_cast_or_null<Expr>(Sub); 12449 if (!ChildExpr) 12450 continue; 12451 12452 if (ChildExpr == CSE->getOperand()) 12453 // Do not recurse over a CoroutineSuspendExpr's operand. 12454 // The operand is also a subexpression of getCommonExpr(), and 12455 // recursing into it directly could confuse object management 12456 // for the sake of sequence tracking. 12457 continue; 12458 12459 Visit(Sub); 12460 } 12461 } 12462 12463 void VisitCastExpr(const CastExpr *E) { 12464 Object O = Object(); 12465 if (E->getCastKind() == CK_LValueToRValue) 12466 O = getObject(E->getSubExpr(), false); 12467 12468 if (O) 12469 notePreUse(O, E); 12470 VisitExpr(E); 12471 if (O) 12472 notePostUse(O, E); 12473 } 12474 12475 void VisitSequencedExpressions(const Expr *SequencedBefore, 12476 const Expr *SequencedAfter) { 12477 SequenceTree::Seq BeforeRegion = Tree.allocate(Region); 12478 SequenceTree::Seq AfterRegion = Tree.allocate(Region); 12479 SequenceTree::Seq OldRegion = Region; 12480 12481 { 12482 SequencedSubexpression SeqBefore(*this); 12483 Region = BeforeRegion; 12484 Visit(SequencedBefore); 12485 } 12486 12487 Region = AfterRegion; 12488 Visit(SequencedAfter); 12489 12490 Region = OldRegion; 12491 12492 Tree.merge(BeforeRegion); 12493 Tree.merge(AfterRegion); 12494 } 12495 12496 void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) { 12497 // C++17 [expr.sub]p1: 12498 // The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The 12499 // expression E1 is sequenced before the expression E2. 12500 if (SemaRef.getLangOpts().CPlusPlus17) 12501 VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS()); 12502 else { 12503 Visit(ASE->getLHS()); 12504 Visit(ASE->getRHS()); 12505 } 12506 } 12507 12508 void VisitBinPtrMemD(const BinaryOperator *BO) { VisitBinPtrMem(BO); } 12509 void VisitBinPtrMemI(const BinaryOperator *BO) { VisitBinPtrMem(BO); } 12510 void VisitBinPtrMem(const BinaryOperator *BO) { 12511 // C++17 [expr.mptr.oper]p4: 12512 // Abbreviating pm-expression.*cast-expression as E1.*E2, [...] 12513 // the expression E1 is sequenced before the expression E2. 12514 if (SemaRef.getLangOpts().CPlusPlus17) 12515 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12516 else { 12517 Visit(BO->getLHS()); 12518 Visit(BO->getRHS()); 12519 } 12520 } 12521 12522 void VisitBinShl(const BinaryOperator *BO) { VisitBinShlShr(BO); } 12523 void VisitBinShr(const BinaryOperator *BO) { VisitBinShlShr(BO); } 12524 void VisitBinShlShr(const BinaryOperator *BO) { 12525 // C++17 [expr.shift]p4: 12526 // The expression E1 is sequenced before the expression E2. 12527 if (SemaRef.getLangOpts().CPlusPlus17) 12528 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12529 else { 12530 Visit(BO->getLHS()); 12531 Visit(BO->getRHS()); 12532 } 12533 } 12534 12535 void VisitBinComma(const BinaryOperator *BO) { 12536 // C++11 [expr.comma]p1: 12537 // Every value computation and side effect associated with the left 12538 // expression is sequenced before every value computation and side 12539 // effect associated with the right expression. 12540 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12541 } 12542 12543 void VisitBinAssign(const BinaryOperator *BO) { 12544 SequenceTree::Seq RHSRegion; 12545 SequenceTree::Seq LHSRegion; 12546 if (SemaRef.getLangOpts().CPlusPlus17) { 12547 RHSRegion = Tree.allocate(Region); 12548 LHSRegion = Tree.allocate(Region); 12549 } else { 12550 RHSRegion = Region; 12551 LHSRegion = Region; 12552 } 12553 SequenceTree::Seq OldRegion = Region; 12554 12555 // C++11 [expr.ass]p1: 12556 // [...] the assignment is sequenced after the value computation 12557 // of the right and left operands, [...] 12558 // 12559 // so check it before inspecting the operands and update the 12560 // map afterwards. 12561 Object O = getObject(BO->getLHS(), /*Mod=*/true); 12562 if (O) 12563 notePreMod(O, BO); 12564 12565 if (SemaRef.getLangOpts().CPlusPlus17) { 12566 // C++17 [expr.ass]p1: 12567 // [...] The right operand is sequenced before the left operand. [...] 12568 { 12569 SequencedSubexpression SeqBefore(*this); 12570 Region = RHSRegion; 12571 Visit(BO->getRHS()); 12572 } 12573 12574 Region = LHSRegion; 12575 Visit(BO->getLHS()); 12576 12577 if (O && isa<CompoundAssignOperator>(BO)) 12578 notePostUse(O, BO); 12579 12580 } else { 12581 // C++11 does not specify any sequencing between the LHS and RHS. 12582 Region = LHSRegion; 12583 Visit(BO->getLHS()); 12584 12585 if (O && isa<CompoundAssignOperator>(BO)) 12586 notePostUse(O, BO); 12587 12588 Region = RHSRegion; 12589 Visit(BO->getRHS()); 12590 } 12591 12592 // C++11 [expr.ass]p1: 12593 // the assignment is sequenced [...] before the value computation of the 12594 // assignment expression. 12595 // C11 6.5.16/3 has no such rule. 12596 Region = OldRegion; 12597 if (O) 12598 notePostMod(O, BO, 12599 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12600 : UK_ModAsSideEffect); 12601 if (SemaRef.getLangOpts().CPlusPlus17) { 12602 Tree.merge(RHSRegion); 12603 Tree.merge(LHSRegion); 12604 } 12605 } 12606 12607 void VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) { 12608 VisitBinAssign(CAO); 12609 } 12610 12611 void VisitUnaryPreInc(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12612 void VisitUnaryPreDec(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12613 void VisitUnaryPreIncDec(const UnaryOperator *UO) { 12614 Object O = getObject(UO->getSubExpr(), true); 12615 if (!O) 12616 return VisitExpr(UO); 12617 12618 notePreMod(O, UO); 12619 Visit(UO->getSubExpr()); 12620 // C++11 [expr.pre.incr]p1: 12621 // the expression ++x is equivalent to x+=1 12622 notePostMod(O, UO, 12623 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12624 : UK_ModAsSideEffect); 12625 } 12626 12627 void VisitUnaryPostInc(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12628 void VisitUnaryPostDec(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12629 void VisitUnaryPostIncDec(const UnaryOperator *UO) { 12630 Object O = getObject(UO->getSubExpr(), true); 12631 if (!O) 12632 return VisitExpr(UO); 12633 12634 notePreMod(O, UO); 12635 Visit(UO->getSubExpr()); 12636 notePostMod(O, UO, UK_ModAsSideEffect); 12637 } 12638 12639 void VisitBinLOr(const BinaryOperator *BO) { 12640 // C++11 [expr.log.or]p2: 12641 // If the second expression is evaluated, every value computation and 12642 // side effect associated with the first expression is sequenced before 12643 // every value computation and side effect associated with the 12644 // second expression. 12645 SequenceTree::Seq LHSRegion = Tree.allocate(Region); 12646 SequenceTree::Seq RHSRegion = Tree.allocate(Region); 12647 SequenceTree::Seq OldRegion = Region; 12648 12649 EvaluationTracker Eval(*this); 12650 { 12651 SequencedSubexpression Sequenced(*this); 12652 Region = LHSRegion; 12653 Visit(BO->getLHS()); 12654 } 12655 12656 // C++11 [expr.log.or]p1: 12657 // [...] the second operand is not evaluated if the first operand 12658 // evaluates to true. 12659 bool EvalResult = false; 12660 bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); 12661 bool ShouldVisitRHS = !EvalOK || !EvalResult; 12662 if (ShouldVisitRHS) { 12663 Region = RHSRegion; 12664 Visit(BO->getRHS()); 12665 } 12666 12667 Region = OldRegion; 12668 Tree.merge(LHSRegion); 12669 Tree.merge(RHSRegion); 12670 } 12671 12672 void VisitBinLAnd(const BinaryOperator *BO) { 12673 // C++11 [expr.log.and]p2: 12674 // If the second expression is evaluated, every value computation and 12675 // side effect associated with the first expression is sequenced before 12676 // every value computation and side effect associated with the 12677 // second expression. 12678 SequenceTree::Seq LHSRegion = Tree.allocate(Region); 12679 SequenceTree::Seq RHSRegion = Tree.allocate(Region); 12680 SequenceTree::Seq OldRegion = Region; 12681 12682 EvaluationTracker Eval(*this); 12683 { 12684 SequencedSubexpression Sequenced(*this); 12685 Region = LHSRegion; 12686 Visit(BO->getLHS()); 12687 } 12688 12689 // C++11 [expr.log.and]p1: 12690 // [...] the second operand is not evaluated if the first operand is false. 12691 bool EvalResult = false; 12692 bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); 12693 bool ShouldVisitRHS = !EvalOK || EvalResult; 12694 if (ShouldVisitRHS) { 12695 Region = RHSRegion; 12696 Visit(BO->getRHS()); 12697 } 12698 12699 Region = OldRegion; 12700 Tree.merge(LHSRegion); 12701 Tree.merge(RHSRegion); 12702 } 12703 12704 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO) { 12705 // C++11 [expr.cond]p1: 12706 // [...] Every value computation and side effect associated with the first 12707 // expression is sequenced before every value computation and side effect 12708 // associated with the second or third expression. 12709 SequenceTree::Seq ConditionRegion = Tree.allocate(Region); 12710 12711 // No sequencing is specified between the true and false expression. 12712 // However since exactly one of both is going to be evaluated we can 12713 // consider them to be sequenced. This is needed to avoid warning on 12714 // something like "x ? y+= 1 : y += 2;" in the case where we will visit 12715 // both the true and false expressions because we can't evaluate x. 12716 // This will still allow us to detect an expression like (pre C++17) 12717 // "(x ? y += 1 : y += 2) = y". 12718 // 12719 // We don't wrap the visitation of the true and false expression with 12720 // SequencedSubexpression because we don't want to downgrade modifications 12721 // as side effect in the true and false expressions after the visition 12722 // is done. (for example in the expression "(x ? y++ : y++) + y" we should 12723 // not warn between the two "y++", but we should warn between the "y++" 12724 // and the "y". 12725 SequenceTree::Seq TrueRegion = Tree.allocate(Region); 12726 SequenceTree::Seq FalseRegion = Tree.allocate(Region); 12727 SequenceTree::Seq OldRegion = Region; 12728 12729 EvaluationTracker Eval(*this); 12730 { 12731 SequencedSubexpression Sequenced(*this); 12732 Region = ConditionRegion; 12733 Visit(CO->getCond()); 12734 } 12735 12736 // C++11 [expr.cond]p1: 12737 // [...] The first expression is contextually converted to bool (Clause 4). 12738 // It is evaluated and if it is true, the result of the conditional 12739 // expression is the value of the second expression, otherwise that of the 12740 // third expression. Only one of the second and third expressions is 12741 // evaluated. [...] 12742 bool EvalResult = false; 12743 bool EvalOK = Eval.evaluate(CO->getCond(), EvalResult); 12744 bool ShouldVisitTrueExpr = !EvalOK || EvalResult; 12745 bool ShouldVisitFalseExpr = !EvalOK || !EvalResult; 12746 if (ShouldVisitTrueExpr) { 12747 Region = TrueRegion; 12748 Visit(CO->getTrueExpr()); 12749 } 12750 if (ShouldVisitFalseExpr) { 12751 Region = FalseRegion; 12752 Visit(CO->getFalseExpr()); 12753 } 12754 12755 Region = OldRegion; 12756 Tree.merge(ConditionRegion); 12757 Tree.merge(TrueRegion); 12758 Tree.merge(FalseRegion); 12759 } 12760 12761 void VisitCallExpr(const CallExpr *CE) { 12762 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. 12763 12764 if (CE->isUnevaluatedBuiltinCall(Context)) 12765 return; 12766 12767 // C++11 [intro.execution]p15: 12768 // When calling a function [...], every value computation and side effect 12769 // associated with any argument expression, or with the postfix expression 12770 // designating the called function, is sequenced before execution of every 12771 // expression or statement in the body of the function [and thus before 12772 // the value computation of its result]. 12773 SequencedSubexpression Sequenced(*this); 12774 SemaRef.runWithSufficientStackSpace(CE->getExprLoc(), [&] { 12775 // C++17 [expr.call]p5 12776 // The postfix-expression is sequenced before each expression in the 12777 // expression-list and any default argument. [...] 12778 SequenceTree::Seq CalleeRegion; 12779 SequenceTree::Seq OtherRegion; 12780 if (SemaRef.getLangOpts().CPlusPlus17) { 12781 CalleeRegion = Tree.allocate(Region); 12782 OtherRegion = Tree.allocate(Region); 12783 } else { 12784 CalleeRegion = Region; 12785 OtherRegion = Region; 12786 } 12787 SequenceTree::Seq OldRegion = Region; 12788 12789 // Visit the callee expression first. 12790 Region = CalleeRegion; 12791 if (SemaRef.getLangOpts().CPlusPlus17) { 12792 SequencedSubexpression Sequenced(*this); 12793 Visit(CE->getCallee()); 12794 } else { 12795 Visit(CE->getCallee()); 12796 } 12797 12798 // Then visit the argument expressions. 12799 Region = OtherRegion; 12800 for (const Expr *Argument : CE->arguments()) 12801 Visit(Argument); 12802 12803 Region = OldRegion; 12804 if (SemaRef.getLangOpts().CPlusPlus17) { 12805 Tree.merge(CalleeRegion); 12806 Tree.merge(OtherRegion); 12807 } 12808 }); 12809 } 12810 12811 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CXXOCE) { 12812 // C++17 [over.match.oper]p2: 12813 // [...] the operator notation is first transformed to the equivalent 12814 // function-call notation as summarized in Table 12 (where @ denotes one 12815 // of the operators covered in the specified subclause). However, the 12816 // operands are sequenced in the order prescribed for the built-in 12817 // operator (Clause 8). 12818 // 12819 // From the above only overloaded binary operators and overloaded call 12820 // operators have sequencing rules in C++17 that we need to handle 12821 // separately. 12822 if (!SemaRef.getLangOpts().CPlusPlus17 || 12823 (CXXOCE->getNumArgs() != 2 && CXXOCE->getOperator() != OO_Call)) 12824 return VisitCallExpr(CXXOCE); 12825 12826 enum { 12827 NoSequencing, 12828 LHSBeforeRHS, 12829 RHSBeforeLHS, 12830 LHSBeforeRest 12831 } SequencingKind; 12832 switch (CXXOCE->getOperator()) { 12833 case OO_Equal: 12834 case OO_PlusEqual: 12835 case OO_MinusEqual: 12836 case OO_StarEqual: 12837 case OO_SlashEqual: 12838 case OO_PercentEqual: 12839 case OO_CaretEqual: 12840 case OO_AmpEqual: 12841 case OO_PipeEqual: 12842 case OO_LessLessEqual: 12843 case OO_GreaterGreaterEqual: 12844 SequencingKind = RHSBeforeLHS; 12845 break; 12846 12847 case OO_LessLess: 12848 case OO_GreaterGreater: 12849 case OO_AmpAmp: 12850 case OO_PipePipe: 12851 case OO_Comma: 12852 case OO_ArrowStar: 12853 case OO_Subscript: 12854 SequencingKind = LHSBeforeRHS; 12855 break; 12856 12857 case OO_Call: 12858 SequencingKind = LHSBeforeRest; 12859 break; 12860 12861 default: 12862 SequencingKind = NoSequencing; 12863 break; 12864 } 12865 12866 if (SequencingKind == NoSequencing) 12867 return VisitCallExpr(CXXOCE); 12868 12869 // This is a call, so all subexpressions are sequenced before the result. 12870 SequencedSubexpression Sequenced(*this); 12871 12872 SemaRef.runWithSufficientStackSpace(CXXOCE->getExprLoc(), [&] { 12873 assert(SemaRef.getLangOpts().CPlusPlus17 && 12874 "Should only get there with C++17 and above!"); 12875 assert((CXXOCE->getNumArgs() == 2 || CXXOCE->getOperator() == OO_Call) && 12876 "Should only get there with an overloaded binary operator" 12877 " or an overloaded call operator!"); 12878 12879 if (SequencingKind == LHSBeforeRest) { 12880 assert(CXXOCE->getOperator() == OO_Call && 12881 "We should only have an overloaded call operator here!"); 12882 12883 // This is very similar to VisitCallExpr, except that we only have the 12884 // C++17 case. The postfix-expression is the first argument of the 12885 // CXXOperatorCallExpr. The expressions in the expression-list, if any, 12886 // are in the following arguments. 12887 // 12888 // Note that we intentionally do not visit the callee expression since 12889 // it is just a decayed reference to a function. 12890 SequenceTree::Seq PostfixExprRegion = Tree.allocate(Region); 12891 SequenceTree::Seq ArgsRegion = Tree.allocate(Region); 12892 SequenceTree::Seq OldRegion = Region; 12893 12894 assert(CXXOCE->getNumArgs() >= 1 && 12895 "An overloaded call operator must have at least one argument" 12896 " for the postfix-expression!"); 12897 const Expr *PostfixExpr = CXXOCE->getArgs()[0]; 12898 llvm::ArrayRef<const Expr *> Args(CXXOCE->getArgs() + 1, 12899 CXXOCE->getNumArgs() - 1); 12900 12901 // Visit the postfix-expression first. 12902 { 12903 Region = PostfixExprRegion; 12904 SequencedSubexpression Sequenced(*this); 12905 Visit(PostfixExpr); 12906 } 12907 12908 // Then visit the argument expressions. 12909 Region = ArgsRegion; 12910 for (const Expr *Arg : Args) 12911 Visit(Arg); 12912 12913 Region = OldRegion; 12914 Tree.merge(PostfixExprRegion); 12915 Tree.merge(ArgsRegion); 12916 } else { 12917 assert(CXXOCE->getNumArgs() == 2 && 12918 "Should only have two arguments here!"); 12919 assert((SequencingKind == LHSBeforeRHS || 12920 SequencingKind == RHSBeforeLHS) && 12921 "Unexpected sequencing kind!"); 12922 12923 // We do not visit the callee expression since it is just a decayed 12924 // reference to a function. 12925 const Expr *E1 = CXXOCE->getArg(0); 12926 const Expr *E2 = CXXOCE->getArg(1); 12927 if (SequencingKind == RHSBeforeLHS) 12928 std::swap(E1, E2); 12929 12930 return VisitSequencedExpressions(E1, E2); 12931 } 12932 }); 12933 } 12934 12935 void VisitCXXConstructExpr(const CXXConstructExpr *CCE) { 12936 // This is a call, so all subexpressions are sequenced before the result. 12937 SequencedSubexpression Sequenced(*this); 12938 12939 if (!CCE->isListInitialization()) 12940 return VisitExpr(CCE); 12941 12942 // In C++11, list initializations are sequenced. 12943 SequenceExpressionsInOrder( 12944 llvm::ArrayRef(CCE->getArgs(), CCE->getNumArgs())); 12945 } 12946 12947 void VisitInitListExpr(const InitListExpr *ILE) { 12948 if (!SemaRef.getLangOpts().CPlusPlus11) 12949 return VisitExpr(ILE); 12950 12951 // In C++11, list initializations are sequenced. 12952 SequenceExpressionsInOrder(ILE->inits()); 12953 } 12954 12955 void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) { 12956 // C++20 parenthesized list initializations are sequenced. See C++20 12957 // [decl.init.general]p16.5 and [decl.init.general]p16.6.2.2. 12958 SequenceExpressionsInOrder(PLIE->getInitExprs()); 12959 } 12960 12961 private: 12962 void SequenceExpressionsInOrder(ArrayRef<const Expr *> ExpressionList) { 12963 SmallVector<SequenceTree::Seq, 32> Elts; 12964 SequenceTree::Seq Parent = Region; 12965 for (const Expr *E : ExpressionList) { 12966 if (!E) 12967 continue; 12968 Region = Tree.allocate(Parent); 12969 Elts.push_back(Region); 12970 Visit(E); 12971 } 12972 12973 // Forget that the initializers are sequenced. 12974 Region = Parent; 12975 for (unsigned I = 0; I < Elts.size(); ++I) 12976 Tree.merge(Elts[I]); 12977 } 12978 }; 12979 12980 SequenceChecker::UsageInfo::UsageInfo() = default; 12981 12982 } // namespace 12983 12984 void Sema::CheckUnsequencedOperations(const Expr *E) { 12985 SmallVector<const Expr *, 8> WorkList; 12986 WorkList.push_back(E); 12987 while (!WorkList.empty()) { 12988 const Expr *Item = WorkList.pop_back_val(); 12989 SequenceChecker(*this, Item, WorkList); 12990 } 12991 } 12992 12993 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, 12994 bool IsConstexpr) { 12995 llvm::SaveAndRestore ConstantContext(isConstantEvaluatedOverride, 12996 IsConstexpr || isa<ConstantExpr>(E)); 12997 CheckImplicitConversions(E, CheckLoc); 12998 if (!E->isInstantiationDependent()) 12999 CheckUnsequencedOperations(E); 13000 if (!IsConstexpr && !E->isValueDependent()) 13001 CheckForIntOverflow(E); 13002 DiagnoseMisalignedMembers(); 13003 } 13004 13005 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, 13006 FieldDecl *BitField, 13007 Expr *Init) { 13008 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); 13009 } 13010 13011 static void diagnoseArrayStarInParamType(Sema &S, QualType PType, 13012 SourceLocation Loc) { 13013 if (!PType->isVariablyModifiedType()) 13014 return; 13015 if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { 13016 diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); 13017 return; 13018 } 13019 if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { 13020 diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); 13021 return; 13022 } 13023 if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { 13024 diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); 13025 return; 13026 } 13027 13028 const ArrayType *AT = S.Context.getAsArrayType(PType); 13029 if (!AT) 13030 return; 13031 13032 if (AT->getSizeModifier() != ArraySizeModifier::Star) { 13033 diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); 13034 return; 13035 } 13036 13037 S.Diag(Loc, diag::err_array_star_in_function_definition); 13038 } 13039 13040 bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters, 13041 bool CheckParameterNames) { 13042 bool HasInvalidParm = false; 13043 for (ParmVarDecl *Param : Parameters) { 13044 assert(Param && "null in a parameter list"); 13045 // C99 6.7.5.3p4: the parameters in a parameter type list in a 13046 // function declarator that is part of a function definition of 13047 // that function shall not have incomplete type. 13048 // 13049 // C++23 [dcl.fct.def.general]/p2 13050 // The type of a parameter [...] for a function definition 13051 // shall not be a (possibly cv-qualified) class type that is incomplete 13052 // or abstract within the function body unless the function is deleted. 13053 if (!Param->isInvalidDecl() && 13054 (RequireCompleteType(Param->getLocation(), Param->getType(), 13055 diag::err_typecheck_decl_incomplete_type) || 13056 RequireNonAbstractType(Param->getBeginLoc(), Param->getOriginalType(), 13057 diag::err_abstract_type_in_decl, 13058 AbstractParamType))) { 13059 Param->setInvalidDecl(); 13060 HasInvalidParm = true; 13061 } 13062 13063 // C99 6.9.1p5: If the declarator includes a parameter type list, the 13064 // declaration of each parameter shall include an identifier. 13065 if (CheckParameterNames && Param->getIdentifier() == nullptr && 13066 !Param->isImplicit() && !getLangOpts().CPlusPlus) { 13067 // Diagnose this as an extension in C17 and earlier. 13068 if (!getLangOpts().C23) 13069 Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c23); 13070 } 13071 13072 // C99 6.7.5.3p12: 13073 // If the function declarator is not part of a definition of that 13074 // function, parameters may have incomplete type and may use the [*] 13075 // notation in their sequences of declarator specifiers to specify 13076 // variable length array types. 13077 QualType PType = Param->getOriginalType(); 13078 // FIXME: This diagnostic should point the '[*]' if source-location 13079 // information is added for it. 13080 diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); 13081 13082 // If the parameter is a c++ class type and it has to be destructed in the 13083 // callee function, declare the destructor so that it can be called by the 13084 // callee function. Do not perform any direct access check on the dtor here. 13085 if (!Param->isInvalidDecl()) { 13086 if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) { 13087 if (!ClassDecl->isInvalidDecl() && 13088 !ClassDecl->hasIrrelevantDestructor() && 13089 !ClassDecl->isDependentContext() && 13090 ClassDecl->isParamDestroyedInCallee()) { 13091 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); 13092 MarkFunctionReferenced(Param->getLocation(), Destructor); 13093 DiagnoseUseOfDecl(Destructor, Param->getLocation()); 13094 } 13095 } 13096 } 13097 13098 // Parameters with the pass_object_size attribute only need to be marked 13099 // constant at function definitions. Because we lack information about 13100 // whether we're on a declaration or definition when we're instantiating the 13101 // attribute, we need to check for constness here. 13102 if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>()) 13103 if (!Param->getType().isConstQualified()) 13104 Diag(Param->getLocation(), diag::err_attribute_pointers_only) 13105 << Attr->getSpelling() << 1; 13106 13107 // Check for parameter names shadowing fields from the class. 13108 if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) { 13109 // The owning context for the parameter should be the function, but we 13110 // want to see if this function's declaration context is a record. 13111 DeclContext *DC = Param->getDeclContext(); 13112 if (DC && DC->isFunctionOrMethod()) { 13113 if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent())) 13114 CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(), 13115 RD, /*DeclIsField*/ false); 13116 } 13117 } 13118 13119 if (!Param->isInvalidDecl() && 13120 Param->getOriginalType()->isWebAssemblyTableType()) { 13121 Param->setInvalidDecl(); 13122 HasInvalidParm = true; 13123 Diag(Param->getLocation(), diag::err_wasm_table_as_function_parameter); 13124 } 13125 } 13126 13127 return HasInvalidParm; 13128 } 13129 13130 std::optional<std::pair< 13131 CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr 13132 *E, 13133 ASTContext 13134 &Ctx); 13135 13136 /// Compute the alignment and offset of the base class object given the 13137 /// derived-to-base cast expression and the alignment and offset of the derived 13138 /// class object. 13139 static std::pair<CharUnits, CharUnits> 13140 getDerivedToBaseAlignmentAndOffset(const CastExpr *CE, QualType DerivedType, 13141 CharUnits BaseAlignment, CharUnits Offset, 13142 ASTContext &Ctx) { 13143 for (auto PathI = CE->path_begin(), PathE = CE->path_end(); PathI != PathE; 13144 ++PathI) { 13145 const CXXBaseSpecifier *Base = *PathI; 13146 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); 13147 if (Base->isVirtual()) { 13148 // The complete object may have a lower alignment than the non-virtual 13149 // alignment of the base, in which case the base may be misaligned. Choose 13150 // the smaller of the non-virtual alignment and BaseAlignment, which is a 13151 // conservative lower bound of the complete object alignment. 13152 CharUnits NonVirtualAlignment = 13153 Ctx.getASTRecordLayout(BaseDecl).getNonVirtualAlignment(); 13154 BaseAlignment = std::min(BaseAlignment, NonVirtualAlignment); 13155 Offset = CharUnits::Zero(); 13156 } else { 13157 const ASTRecordLayout &RL = 13158 Ctx.getASTRecordLayout(DerivedType->getAsCXXRecordDecl()); 13159 Offset += RL.getBaseClassOffset(BaseDecl); 13160 } 13161 DerivedType = Base->getType(); 13162 } 13163 13164 return std::make_pair(BaseAlignment, Offset); 13165 } 13166 13167 /// Compute the alignment and offset of a binary additive operator. 13168 static std::optional<std::pair<CharUnits, CharUnits>> 13169 getAlignmentAndOffsetFromBinAddOrSub(const Expr *PtrE, const Expr *IntE, 13170 bool IsSub, ASTContext &Ctx) { 13171 QualType PointeeType = PtrE->getType()->getPointeeType(); 13172 13173 if (!PointeeType->isConstantSizeType()) 13174 return std::nullopt; 13175 13176 auto P = getBaseAlignmentAndOffsetFromPtr(PtrE, Ctx); 13177 13178 if (!P) 13179 return std::nullopt; 13180 13181 CharUnits EltSize = Ctx.getTypeSizeInChars(PointeeType); 13182 if (std::optional<llvm::APSInt> IdxRes = IntE->getIntegerConstantExpr(Ctx)) { 13183 CharUnits Offset = EltSize * IdxRes->getExtValue(); 13184 if (IsSub) 13185 Offset = -Offset; 13186 return std::make_pair(P->first, P->second + Offset); 13187 } 13188 13189 // If the integer expression isn't a constant expression, compute the lower 13190 // bound of the alignment using the alignment and offset of the pointer 13191 // expression and the element size. 13192 return std::make_pair( 13193 P->first.alignmentAtOffset(P->second).alignmentAtOffset(EltSize), 13194 CharUnits::Zero()); 13195 } 13196 13197 /// This helper function takes an lvalue expression and returns the alignment of 13198 /// a VarDecl and a constant offset from the VarDecl. 13199 std::optional<std::pair< 13200 CharUnits, 13201 CharUnits>> static getBaseAlignmentAndOffsetFromLValue(const Expr *E, 13202 ASTContext &Ctx) { 13203 E = E->IgnoreParens(); 13204 switch (E->getStmtClass()) { 13205 default: 13206 break; 13207 case Stmt::CStyleCastExprClass: 13208 case Stmt::CXXStaticCastExprClass: 13209 case Stmt::ImplicitCastExprClass: { 13210 auto *CE = cast<CastExpr>(E); 13211 const Expr *From = CE->getSubExpr(); 13212 switch (CE->getCastKind()) { 13213 default: 13214 break; 13215 case CK_NoOp: 13216 return getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13217 case CK_UncheckedDerivedToBase: 13218 case CK_DerivedToBase: { 13219 auto P = getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13220 if (!P) 13221 break; 13222 return getDerivedToBaseAlignmentAndOffset(CE, From->getType(), P->first, 13223 P->second, Ctx); 13224 } 13225 } 13226 break; 13227 } 13228 case Stmt::ArraySubscriptExprClass: { 13229 auto *ASE = cast<ArraySubscriptExpr>(E); 13230 return getAlignmentAndOffsetFromBinAddOrSub(ASE->getBase(), ASE->getIdx(), 13231 false, Ctx); 13232 } 13233 case Stmt::DeclRefExprClass: { 13234 if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) { 13235 // FIXME: If VD is captured by copy or is an escaping __block variable, 13236 // use the alignment of VD's type. 13237 if (!VD->getType()->isReferenceType()) { 13238 // Dependent alignment cannot be resolved -> bail out. 13239 if (VD->hasDependentAlignment()) 13240 break; 13241 return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); 13242 } 13243 if (VD->hasInit()) 13244 return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); 13245 } 13246 break; 13247 } 13248 case Stmt::MemberExprClass: { 13249 auto *ME = cast<MemberExpr>(E); 13250 auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); 13251 if (!FD || FD->getType()->isReferenceType() || 13252 FD->getParent()->isInvalidDecl()) 13253 break; 13254 std::optional<std::pair<CharUnits, CharUnits>> P; 13255 if (ME->isArrow()) 13256 P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx); 13257 else 13258 P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx); 13259 if (!P) 13260 break; 13261 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent()); 13262 uint64_t Offset = Layout.getFieldOffset(FD->getFieldIndex()); 13263 return std::make_pair(P->first, 13264 P->second + CharUnits::fromQuantity(Offset)); 13265 } 13266 case Stmt::UnaryOperatorClass: { 13267 auto *UO = cast<UnaryOperator>(E); 13268 switch (UO->getOpcode()) { 13269 default: 13270 break; 13271 case UO_Deref: 13272 return getBaseAlignmentAndOffsetFromPtr(UO->getSubExpr(), Ctx); 13273 } 13274 break; 13275 } 13276 case Stmt::BinaryOperatorClass: { 13277 auto *BO = cast<BinaryOperator>(E); 13278 auto Opcode = BO->getOpcode(); 13279 switch (Opcode) { 13280 default: 13281 break; 13282 case BO_Comma: 13283 return getBaseAlignmentAndOffsetFromLValue(BO->getRHS(), Ctx); 13284 } 13285 break; 13286 } 13287 } 13288 return std::nullopt; 13289 } 13290 13291 /// This helper function takes a pointer expression and returns the alignment of 13292 /// a VarDecl and a constant offset from the VarDecl. 13293 std::optional<std::pair< 13294 CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr 13295 *E, 13296 ASTContext 13297 &Ctx) { 13298 E = E->IgnoreParens(); 13299 switch (E->getStmtClass()) { 13300 default: 13301 break; 13302 case Stmt::CStyleCastExprClass: 13303 case Stmt::CXXStaticCastExprClass: 13304 case Stmt::ImplicitCastExprClass: { 13305 auto *CE = cast<CastExpr>(E); 13306 const Expr *From = CE->getSubExpr(); 13307 switch (CE->getCastKind()) { 13308 default: 13309 break; 13310 case CK_NoOp: 13311 return getBaseAlignmentAndOffsetFromPtr(From, Ctx); 13312 case CK_ArrayToPointerDecay: 13313 return getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13314 case CK_UncheckedDerivedToBase: 13315 case CK_DerivedToBase: { 13316 auto P = getBaseAlignmentAndOffsetFromPtr(From, Ctx); 13317 if (!P) 13318 break; 13319 return getDerivedToBaseAlignmentAndOffset( 13320 CE, From->getType()->getPointeeType(), P->first, P->second, Ctx); 13321 } 13322 } 13323 break; 13324 } 13325 case Stmt::CXXThisExprClass: { 13326 auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl(); 13327 CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment(); 13328 return std::make_pair(Alignment, CharUnits::Zero()); 13329 } 13330 case Stmt::UnaryOperatorClass: { 13331 auto *UO = cast<UnaryOperator>(E); 13332 if (UO->getOpcode() == UO_AddrOf) 13333 return getBaseAlignmentAndOffsetFromLValue(UO->getSubExpr(), Ctx); 13334 break; 13335 } 13336 case Stmt::BinaryOperatorClass: { 13337 auto *BO = cast<BinaryOperator>(E); 13338 auto Opcode = BO->getOpcode(); 13339 switch (Opcode) { 13340 default: 13341 break; 13342 case BO_Add: 13343 case BO_Sub: { 13344 const Expr *LHS = BO->getLHS(), *RHS = BO->getRHS(); 13345 if (Opcode == BO_Add && !RHS->getType()->isIntegralOrEnumerationType()) 13346 std::swap(LHS, RHS); 13347 return getAlignmentAndOffsetFromBinAddOrSub(LHS, RHS, Opcode == BO_Sub, 13348 Ctx); 13349 } 13350 case BO_Comma: 13351 return getBaseAlignmentAndOffsetFromPtr(BO->getRHS(), Ctx); 13352 } 13353 break; 13354 } 13355 } 13356 return std::nullopt; 13357 } 13358 13359 static CharUnits getPresumedAlignmentOfPointer(const Expr *E, Sema &S) { 13360 // See if we can compute the alignment of a VarDecl and an offset from it. 13361 std::optional<std::pair<CharUnits, CharUnits>> P = 13362 getBaseAlignmentAndOffsetFromPtr(E, S.Context); 13363 13364 if (P) 13365 return P->first.alignmentAtOffset(P->second); 13366 13367 // If that failed, return the type's alignment. 13368 return S.Context.getTypeAlignInChars(E->getType()->getPointeeType()); 13369 } 13370 13371 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { 13372 // This is actually a lot of work to potentially be doing on every 13373 // cast; don't do it if we're ignoring -Wcast_align (as is the default). 13374 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) 13375 return; 13376 13377 // Ignore dependent types. 13378 if (T->isDependentType() || Op->getType()->isDependentType()) 13379 return; 13380 13381 // Require that the destination be a pointer type. 13382 const PointerType *DestPtr = T->getAs<PointerType>(); 13383 if (!DestPtr) return; 13384 13385 // If the destination has alignment 1, we're done. 13386 QualType DestPointee = DestPtr->getPointeeType(); 13387 if (DestPointee->isIncompleteType()) return; 13388 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); 13389 if (DestAlign.isOne()) return; 13390 13391 // Require that the source be a pointer type. 13392 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); 13393 if (!SrcPtr) return; 13394 QualType SrcPointee = SrcPtr->getPointeeType(); 13395 13396 // Explicitly allow casts from cv void*. We already implicitly 13397 // allowed casts to cv void*, since they have alignment 1. 13398 // Also allow casts involving incomplete types, which implicitly 13399 // includes 'void'. 13400 if (SrcPointee->isIncompleteType()) return; 13401 13402 CharUnits SrcAlign = getPresumedAlignmentOfPointer(Op, *this); 13403 13404 if (SrcAlign >= DestAlign) return; 13405 13406 Diag(TRange.getBegin(), diag::warn_cast_align) 13407 << Op->getType() << T 13408 << static_cast<unsigned>(SrcAlign.getQuantity()) 13409 << static_cast<unsigned>(DestAlign.getQuantity()) 13410 << TRange << Op->getSourceRange(); 13411 } 13412 13413 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 13414 const ArraySubscriptExpr *ASE, 13415 bool AllowOnePastEnd, bool IndexNegated) { 13416 // Already diagnosed by the constant evaluator. 13417 if (isConstantEvaluatedContext()) 13418 return; 13419 13420 IndexExpr = IndexExpr->IgnoreParenImpCasts(); 13421 if (IndexExpr->isValueDependent()) 13422 return; 13423 13424 const Type *EffectiveType = 13425 BaseExpr->getType()->getPointeeOrArrayElementType(); 13426 BaseExpr = BaseExpr->IgnoreParenCasts(); 13427 const ConstantArrayType *ArrayTy = 13428 Context.getAsConstantArrayType(BaseExpr->getType()); 13429 13430 LangOptions::StrictFlexArraysLevelKind 13431 StrictFlexArraysLevel = getLangOpts().getStrictFlexArraysLevel(); 13432 13433 const Type *BaseType = 13434 ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr(); 13435 bool IsUnboundedArray = 13436 BaseType == nullptr || BaseExpr->isFlexibleArrayMemberLike( 13437 Context, StrictFlexArraysLevel, 13438 /*IgnoreTemplateOrMacroSubstitution=*/true); 13439 if (EffectiveType->isDependentType() || 13440 (!IsUnboundedArray && BaseType->isDependentType())) 13441 return; 13442 13443 Expr::EvalResult Result; 13444 if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) 13445 return; 13446 13447 llvm::APSInt index = Result.Val.getInt(); 13448 if (IndexNegated) { 13449 index.setIsUnsigned(false); 13450 index = -index; 13451 } 13452 13453 if (IsUnboundedArray) { 13454 if (EffectiveType->isFunctionType()) 13455 return; 13456 if (index.isUnsigned() || !index.isNegative()) { 13457 const auto &ASTC = getASTContext(); 13458 unsigned AddrBits = ASTC.getTargetInfo().getPointerWidth( 13459 EffectiveType->getCanonicalTypeInternal().getAddressSpace()); 13460 if (index.getBitWidth() < AddrBits) 13461 index = index.zext(AddrBits); 13462 std::optional<CharUnits> ElemCharUnits = 13463 ASTC.getTypeSizeInCharsIfKnown(EffectiveType); 13464 // PR50741 - If EffectiveType has unknown size (e.g., if it's a void 13465 // pointer) bounds-checking isn't meaningful. 13466 if (!ElemCharUnits || ElemCharUnits->isZero()) 13467 return; 13468 llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity()); 13469 // If index has more active bits than address space, we already know 13470 // we have a bounds violation to warn about. Otherwise, compute 13471 // address of (index + 1)th element, and warn about bounds violation 13472 // only if that address exceeds address space. 13473 if (index.getActiveBits() <= AddrBits) { 13474 bool Overflow; 13475 llvm::APInt Product(index); 13476 Product += 1; 13477 Product = Product.umul_ov(ElemBytes, Overflow); 13478 if (!Overflow && Product.getActiveBits() <= AddrBits) 13479 return; 13480 } 13481 13482 // Need to compute max possible elements in address space, since that 13483 // is included in diag message. 13484 llvm::APInt MaxElems = llvm::APInt::getMaxValue(AddrBits); 13485 MaxElems = MaxElems.zext(std::max(AddrBits + 1, ElemBytes.getBitWidth())); 13486 MaxElems += 1; 13487 ElemBytes = ElemBytes.zextOrTrunc(MaxElems.getBitWidth()); 13488 MaxElems = MaxElems.udiv(ElemBytes); 13489 13490 unsigned DiagID = 13491 ASE ? diag::warn_array_index_exceeds_max_addressable_bounds 13492 : diag::warn_ptr_arith_exceeds_max_addressable_bounds; 13493 13494 // Diag message shows element size in bits and in "bytes" (platform- 13495 // dependent CharUnits) 13496 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 13497 PDiag(DiagID) 13498 << toString(index, 10, true) << AddrBits 13499 << (unsigned)ASTC.toBits(*ElemCharUnits) 13500 << toString(ElemBytes, 10, false) 13501 << toString(MaxElems, 10, false) 13502 << (unsigned)MaxElems.getLimitedValue(~0U) 13503 << IndexExpr->getSourceRange()); 13504 13505 const NamedDecl *ND = nullptr; 13506 // Try harder to find a NamedDecl to point at in the note. 13507 while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) 13508 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 13509 if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 13510 ND = DRE->getDecl(); 13511 if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) 13512 ND = ME->getMemberDecl(); 13513 13514 if (ND) 13515 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 13516 PDiag(diag::note_array_declared_here) << ND); 13517 } 13518 return; 13519 } 13520 13521 if (index.isUnsigned() || !index.isNegative()) { 13522 // It is possible that the type of the base expression after 13523 // IgnoreParenCasts is incomplete, even though the type of the base 13524 // expression before IgnoreParenCasts is complete (see PR39746 for an 13525 // example). In this case we have no information about whether the array 13526 // access exceeds the array bounds. However we can still diagnose an array 13527 // access which precedes the array bounds. 13528 if (BaseType->isIncompleteType()) 13529 return; 13530 13531 llvm::APInt size = ArrayTy->getSize(); 13532 13533 if (BaseType != EffectiveType) { 13534 // Make sure we're comparing apples to apples when comparing index to 13535 // size. 13536 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); 13537 uint64_t array_typesize = Context.getTypeSize(BaseType); 13538 13539 // Handle ptrarith_typesize being zero, such as when casting to void*. 13540 // Use the size in bits (what "getTypeSize()" returns) rather than bytes. 13541 if (!ptrarith_typesize) 13542 ptrarith_typesize = Context.getCharWidth(); 13543 13544 if (ptrarith_typesize != array_typesize) { 13545 // There's a cast to a different size type involved. 13546 uint64_t ratio = array_typesize / ptrarith_typesize; 13547 13548 // TODO: Be smarter about handling cases where array_typesize is not a 13549 // multiple of ptrarith_typesize. 13550 if (ptrarith_typesize * ratio == array_typesize) 13551 size *= llvm::APInt(size.getBitWidth(), ratio); 13552 } 13553 } 13554 13555 if (size.getBitWidth() > index.getBitWidth()) 13556 index = index.zext(size.getBitWidth()); 13557 else if (size.getBitWidth() < index.getBitWidth()) 13558 size = size.zext(index.getBitWidth()); 13559 13560 // For array subscripting the index must be less than size, but for pointer 13561 // arithmetic also allow the index (offset) to be equal to size since 13562 // computing the next address after the end of the array is legal and 13563 // commonly done e.g. in C++ iterators and range-based for loops. 13564 if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) 13565 return; 13566 13567 // Suppress the warning if the subscript expression (as identified by the 13568 // ']' location) and the index expression are both from macro expansions 13569 // within a system header. 13570 if (ASE) { 13571 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( 13572 ASE->getRBracketLoc()); 13573 if (SourceMgr.isInSystemHeader(RBracketLoc)) { 13574 SourceLocation IndexLoc = 13575 SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc()); 13576 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) 13577 return; 13578 } 13579 } 13580 13581 unsigned DiagID = ASE ? diag::warn_array_index_exceeds_bounds 13582 : diag::warn_ptr_arith_exceeds_bounds; 13583 unsigned CastMsg = (!ASE || BaseType == EffectiveType) ? 0 : 1; 13584 QualType CastMsgTy = ASE ? ASE->getLHS()->getType() : QualType(); 13585 13586 DiagRuntimeBehavior( 13587 BaseExpr->getBeginLoc(), BaseExpr, 13588 PDiag(DiagID) << toString(index, 10, true) << ArrayTy->desugar() 13589 << CastMsg << CastMsgTy << IndexExpr->getSourceRange()); 13590 } else { 13591 unsigned DiagID = diag::warn_array_index_precedes_bounds; 13592 if (!ASE) { 13593 DiagID = diag::warn_ptr_arith_precedes_bounds; 13594 if (index.isNegative()) index = -index; 13595 } 13596 13597 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 13598 PDiag(DiagID) << toString(index, 10, true) 13599 << IndexExpr->getSourceRange()); 13600 } 13601 13602 const NamedDecl *ND = nullptr; 13603 // Try harder to find a NamedDecl to point at in the note. 13604 while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) 13605 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 13606 if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 13607 ND = DRE->getDecl(); 13608 if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) 13609 ND = ME->getMemberDecl(); 13610 13611 if (ND) 13612 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 13613 PDiag(diag::note_array_declared_here) << ND); 13614 } 13615 13616 void Sema::CheckArrayAccess(const Expr *expr) { 13617 int AllowOnePastEnd = 0; 13618 while (expr) { 13619 expr = expr->IgnoreParenImpCasts(); 13620 switch (expr->getStmtClass()) { 13621 case Stmt::ArraySubscriptExprClass: { 13622 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); 13623 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, 13624 AllowOnePastEnd > 0); 13625 expr = ASE->getBase(); 13626 break; 13627 } 13628 case Stmt::MemberExprClass: { 13629 expr = cast<MemberExpr>(expr)->getBase(); 13630 break; 13631 } 13632 case Stmt::ArraySectionExprClass: { 13633 const ArraySectionExpr *ASE = cast<ArraySectionExpr>(expr); 13634 // FIXME: We should probably be checking all of the elements to the 13635 // 'length' here as well. 13636 if (ASE->getLowerBound()) 13637 CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(), 13638 /*ASE=*/nullptr, AllowOnePastEnd > 0); 13639 return; 13640 } 13641 case Stmt::UnaryOperatorClass: { 13642 // Only unwrap the * and & unary operators 13643 const UnaryOperator *UO = cast<UnaryOperator>(expr); 13644 expr = UO->getSubExpr(); 13645 switch (UO->getOpcode()) { 13646 case UO_AddrOf: 13647 AllowOnePastEnd++; 13648 break; 13649 case UO_Deref: 13650 AllowOnePastEnd--; 13651 break; 13652 default: 13653 return; 13654 } 13655 break; 13656 } 13657 case Stmt::ConditionalOperatorClass: { 13658 const ConditionalOperator *cond = cast<ConditionalOperator>(expr); 13659 if (const Expr *lhs = cond->getLHS()) 13660 CheckArrayAccess(lhs); 13661 if (const Expr *rhs = cond->getRHS()) 13662 CheckArrayAccess(rhs); 13663 return; 13664 } 13665 case Stmt::CXXOperatorCallExprClass: { 13666 const auto *OCE = cast<CXXOperatorCallExpr>(expr); 13667 for (const auto *Arg : OCE->arguments()) 13668 CheckArrayAccess(Arg); 13669 return; 13670 } 13671 default: 13672 return; 13673 } 13674 } 13675 } 13676 13677 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, 13678 Expr *RHS, bool isProperty) { 13679 // Check if RHS is an Objective-C object literal, which also can get 13680 // immediately zapped in a weak reference. Note that we explicitly 13681 // allow ObjCStringLiterals, since those are designed to never really die. 13682 RHS = RHS->IgnoreParenImpCasts(); 13683 13684 // This enum needs to match with the 'select' in 13685 // warn_objc_arc_literal_assign (off-by-1). 13686 SemaObjC::ObjCLiteralKind Kind = S.ObjC().CheckLiteralKind(RHS); 13687 if (Kind == SemaObjC::LK_String || Kind == SemaObjC::LK_None) 13688 return false; 13689 13690 S.Diag(Loc, diag::warn_arc_literal_assign) 13691 << (unsigned) Kind 13692 << (isProperty ? 0 : 1) 13693 << RHS->getSourceRange(); 13694 13695 return true; 13696 } 13697 13698 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, 13699 Qualifiers::ObjCLifetime LT, 13700 Expr *RHS, bool isProperty) { 13701 // Strip off any implicit cast added to get to the one ARC-specific. 13702 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13703 if (cast->getCastKind() == CK_ARCConsumeObject) { 13704 S.Diag(Loc, diag::warn_arc_retained_assign) 13705 << (LT == Qualifiers::OCL_ExplicitNone) 13706 << (isProperty ? 0 : 1) 13707 << RHS->getSourceRange(); 13708 return true; 13709 } 13710 RHS = cast->getSubExpr(); 13711 } 13712 13713 if (LT == Qualifiers::OCL_Weak && 13714 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) 13715 return true; 13716 13717 return false; 13718 } 13719 13720 bool Sema::checkUnsafeAssigns(SourceLocation Loc, 13721 QualType LHS, Expr *RHS) { 13722 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); 13723 13724 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) 13725 return false; 13726 13727 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) 13728 return true; 13729 13730 return false; 13731 } 13732 13733 void Sema::checkUnsafeExprAssigns(SourceLocation Loc, 13734 Expr *LHS, Expr *RHS) { 13735 QualType LHSType; 13736 // PropertyRef on LHS type need be directly obtained from 13737 // its declaration as it has a PseudoType. 13738 ObjCPropertyRefExpr *PRE 13739 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); 13740 if (PRE && !PRE->isImplicitProperty()) { 13741 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13742 if (PD) 13743 LHSType = PD->getType(); 13744 } 13745 13746 if (LHSType.isNull()) 13747 LHSType = LHS->getType(); 13748 13749 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); 13750 13751 if (LT == Qualifiers::OCL_Weak) { 13752 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) 13753 getCurFunction()->markSafeWeakUse(LHS); 13754 } 13755 13756 if (checkUnsafeAssigns(Loc, LHSType, RHS)) 13757 return; 13758 13759 // FIXME. Check for other life times. 13760 if (LT != Qualifiers::OCL_None) 13761 return; 13762 13763 if (PRE) { 13764 if (PRE->isImplicitProperty()) 13765 return; 13766 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13767 if (!PD) 13768 return; 13769 13770 unsigned Attributes = PD->getPropertyAttributes(); 13771 if (Attributes & ObjCPropertyAttribute::kind_assign) { 13772 // when 'assign' attribute was not explicitly specified 13773 // by user, ignore it and rely on property type itself 13774 // for lifetime info. 13775 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); 13776 if (!(AsWrittenAttr & ObjCPropertyAttribute::kind_assign) && 13777 LHSType->isObjCRetainableType()) 13778 return; 13779 13780 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13781 if (cast->getCastKind() == CK_ARCConsumeObject) { 13782 Diag(Loc, diag::warn_arc_retained_property_assign) 13783 << RHS->getSourceRange(); 13784 return; 13785 } 13786 RHS = cast->getSubExpr(); 13787 } 13788 } else if (Attributes & ObjCPropertyAttribute::kind_weak) { 13789 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) 13790 return; 13791 } 13792 } 13793 } 13794 13795 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// 13796 13797 static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, 13798 SourceLocation StmtLoc, 13799 const NullStmt *Body) { 13800 // Do not warn if the body is a macro that expands to nothing, e.g: 13801 // 13802 // #define CALL(x) 13803 // if (condition) 13804 // CALL(0); 13805 if (Body->hasLeadingEmptyMacro()) 13806 return false; 13807 13808 // Get line numbers of statement and body. 13809 bool StmtLineInvalid; 13810 unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, 13811 &StmtLineInvalid); 13812 if (StmtLineInvalid) 13813 return false; 13814 13815 bool BodyLineInvalid; 13816 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), 13817 &BodyLineInvalid); 13818 if (BodyLineInvalid) 13819 return false; 13820 13821 // Warn if null statement and body are on the same line. 13822 if (StmtLine != BodyLine) 13823 return false; 13824 13825 return true; 13826 } 13827 13828 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, 13829 const Stmt *Body, 13830 unsigned DiagID) { 13831 // Since this is a syntactic check, don't emit diagnostic for template 13832 // instantiations, this just adds noise. 13833 if (CurrentInstantiationScope) 13834 return; 13835 13836 // The body should be a null statement. 13837 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13838 if (!NBody) 13839 return; 13840 13841 // Do the usual checks. 13842 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13843 return; 13844 13845 Diag(NBody->getSemiLoc(), DiagID); 13846 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13847 } 13848 13849 void Sema::DiagnoseEmptyLoopBody(const Stmt *S, 13850 const Stmt *PossibleBody) { 13851 assert(!CurrentInstantiationScope); // Ensured by caller 13852 13853 SourceLocation StmtLoc; 13854 const Stmt *Body; 13855 unsigned DiagID; 13856 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { 13857 StmtLoc = FS->getRParenLoc(); 13858 Body = FS->getBody(); 13859 DiagID = diag::warn_empty_for_body; 13860 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { 13861 StmtLoc = WS->getRParenLoc(); 13862 Body = WS->getBody(); 13863 DiagID = diag::warn_empty_while_body; 13864 } else 13865 return; // Neither `for' nor `while'. 13866 13867 // The body should be a null statement. 13868 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13869 if (!NBody) 13870 return; 13871 13872 // Skip expensive checks if diagnostic is disabled. 13873 if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) 13874 return; 13875 13876 // Do the usual checks. 13877 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13878 return; 13879 13880 // `for(...);' and `while(...);' are popular idioms, so in order to keep 13881 // noise level low, emit diagnostics only if for/while is followed by a 13882 // CompoundStmt, e.g.: 13883 // for (int i = 0; i < n; i++); 13884 // { 13885 // a(i); 13886 // } 13887 // or if for/while is followed by a statement with more indentation 13888 // than for/while itself: 13889 // for (int i = 0; i < n; i++); 13890 // a(i); 13891 bool ProbableTypo = isa<CompoundStmt>(PossibleBody); 13892 if (!ProbableTypo) { 13893 bool BodyColInvalid; 13894 unsigned BodyCol = SourceMgr.getPresumedColumnNumber( 13895 PossibleBody->getBeginLoc(), &BodyColInvalid); 13896 if (BodyColInvalid) 13897 return; 13898 13899 bool StmtColInvalid; 13900 unsigned StmtCol = 13901 SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid); 13902 if (StmtColInvalid) 13903 return; 13904 13905 if (BodyCol > StmtCol) 13906 ProbableTypo = true; 13907 } 13908 13909 if (ProbableTypo) { 13910 Diag(NBody->getSemiLoc(), DiagID); 13911 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13912 } 13913 } 13914 13915 //===--- CHECK: Warn on self move with std::move. -------------------------===// 13916 13917 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, 13918 SourceLocation OpLoc) { 13919 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) 13920 return; 13921 13922 if (inTemplateInstantiation()) 13923 return; 13924 13925 // Strip parens and casts away. 13926 LHSExpr = LHSExpr->IgnoreParenImpCasts(); 13927 RHSExpr = RHSExpr->IgnoreParenImpCasts(); 13928 13929 // Check for a call to std::move or for a static_cast<T&&>(..) to an xvalue 13930 // which we can treat as an inlined std::move 13931 if (const auto *CE = dyn_cast<CallExpr>(RHSExpr); 13932 CE && CE->getNumArgs() == 1 && CE->isCallToStdMove()) 13933 RHSExpr = CE->getArg(0); 13934 else if (const auto *CXXSCE = dyn_cast<CXXStaticCastExpr>(RHSExpr); 13935 CXXSCE && CXXSCE->isXValue()) 13936 RHSExpr = CXXSCE->getSubExpr(); 13937 else 13938 return; 13939 13940 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); 13941 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); 13942 13943 // Two DeclRefExpr's, check that the decls are the same. 13944 if (LHSDeclRef && RHSDeclRef) { 13945 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13946 return; 13947 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13948 RHSDeclRef->getDecl()->getCanonicalDecl()) 13949 return; 13950 13951 auto D = Diag(OpLoc, diag::warn_self_move) 13952 << LHSExpr->getType() << LHSExpr->getSourceRange() 13953 << RHSExpr->getSourceRange(); 13954 if (const FieldDecl *F = 13955 getSelfAssignmentClassMemberCandidate(RHSDeclRef->getDecl())) 13956 D << 1 << F 13957 << FixItHint::CreateInsertion(LHSDeclRef->getBeginLoc(), "this->"); 13958 else 13959 D << 0; 13960 return; 13961 } 13962 13963 // Member variables require a different approach to check for self moves. 13964 // MemberExpr's are the same if every nested MemberExpr refers to the same 13965 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or 13966 // the base Expr's are CXXThisExpr's. 13967 const Expr *LHSBase = LHSExpr; 13968 const Expr *RHSBase = RHSExpr; 13969 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); 13970 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); 13971 if (!LHSME || !RHSME) 13972 return; 13973 13974 while (LHSME && RHSME) { 13975 if (LHSME->getMemberDecl()->getCanonicalDecl() != 13976 RHSME->getMemberDecl()->getCanonicalDecl()) 13977 return; 13978 13979 LHSBase = LHSME->getBase(); 13980 RHSBase = RHSME->getBase(); 13981 LHSME = dyn_cast<MemberExpr>(LHSBase); 13982 RHSME = dyn_cast<MemberExpr>(RHSBase); 13983 } 13984 13985 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); 13986 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); 13987 if (LHSDeclRef && RHSDeclRef) { 13988 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13989 return; 13990 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13991 RHSDeclRef->getDecl()->getCanonicalDecl()) 13992 return; 13993 13994 Diag(OpLoc, diag::warn_self_move) 13995 << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() 13996 << RHSExpr->getSourceRange(); 13997 return; 13998 } 13999 14000 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) 14001 Diag(OpLoc, diag::warn_self_move) 14002 << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() 14003 << RHSExpr->getSourceRange(); 14004 } 14005 14006 //===--- Layout compatibility ----------------------------------------------// 14007 14008 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2); 14009 14010 /// Check if two enumeration types are layout-compatible. 14011 static bool isLayoutCompatible(const ASTContext &C, const EnumDecl *ED1, 14012 const EnumDecl *ED2) { 14013 // C++11 [dcl.enum] p8: 14014 // Two enumeration types are layout-compatible if they have the same 14015 // underlying type. 14016 return ED1->isComplete() && ED2->isComplete() && 14017 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); 14018 } 14019 14020 /// Check if two fields are layout-compatible. 14021 /// Can be used on union members, which are exempt from alignment requirement 14022 /// of common initial sequence. 14023 static bool isLayoutCompatible(const ASTContext &C, const FieldDecl *Field1, 14024 const FieldDecl *Field2, 14025 bool AreUnionMembers = false) { 14026 [[maybe_unused]] const Type *Field1Parent = 14027 Field1->getParent()->getTypeForDecl(); 14028 [[maybe_unused]] const Type *Field2Parent = 14029 Field2->getParent()->getTypeForDecl(); 14030 assert(((Field1Parent->isStructureOrClassType() && 14031 Field2Parent->isStructureOrClassType()) || 14032 (Field1Parent->isUnionType() && Field2Parent->isUnionType())) && 14033 "Can't evaluate layout compatibility between a struct field and a " 14034 "union field."); 14035 assert(((!AreUnionMembers && Field1Parent->isStructureOrClassType()) || 14036 (AreUnionMembers && Field1Parent->isUnionType())) && 14037 "AreUnionMembers should be 'true' for union fields (only)."); 14038 14039 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) 14040 return false; 14041 14042 if (Field1->isBitField() != Field2->isBitField()) 14043 return false; 14044 14045 if (Field1->isBitField()) { 14046 // Make sure that the bit-fields are the same length. 14047 unsigned Bits1 = Field1->getBitWidthValue(C); 14048 unsigned Bits2 = Field2->getBitWidthValue(C); 14049 14050 if (Bits1 != Bits2) 14051 return false; 14052 } 14053 14054 if (Field1->hasAttr<clang::NoUniqueAddressAttr>() || 14055 Field2->hasAttr<clang::NoUniqueAddressAttr>()) 14056 return false; 14057 14058 if (!AreUnionMembers && 14059 Field1->getMaxAlignment() != Field2->getMaxAlignment()) 14060 return false; 14061 14062 return true; 14063 } 14064 14065 /// Check if two standard-layout structs are layout-compatible. 14066 /// (C++11 [class.mem] p17) 14067 static bool isLayoutCompatibleStruct(const ASTContext &C, const RecordDecl *RD1, 14068 const RecordDecl *RD2) { 14069 // Get to the class where the fields are declared 14070 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) 14071 RD1 = D1CXX->getStandardLayoutBaseWithFields(); 14072 14073 if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) 14074 RD2 = D2CXX->getStandardLayoutBaseWithFields(); 14075 14076 // Check the fields. 14077 return llvm::equal(RD1->fields(), RD2->fields(), 14078 [&C](const FieldDecl *F1, const FieldDecl *F2) -> bool { 14079 return isLayoutCompatible(C, F1, F2); 14080 }); 14081 } 14082 14083 /// Check if two standard-layout unions are layout-compatible. 14084 /// (C++11 [class.mem] p18) 14085 static bool isLayoutCompatibleUnion(const ASTContext &C, const RecordDecl *RD1, 14086 const RecordDecl *RD2) { 14087 llvm::SmallPtrSet<const FieldDecl *, 8> UnmatchedFields; 14088 for (auto *Field2 : RD2->fields()) 14089 UnmatchedFields.insert(Field2); 14090 14091 for (auto *Field1 : RD1->fields()) { 14092 auto I = UnmatchedFields.begin(); 14093 auto E = UnmatchedFields.end(); 14094 14095 for ( ; I != E; ++I) { 14096 if (isLayoutCompatible(C, Field1, *I, /*IsUnionMember=*/true)) { 14097 bool Result = UnmatchedFields.erase(*I); 14098 (void) Result; 14099 assert(Result); 14100 break; 14101 } 14102 } 14103 if (I == E) 14104 return false; 14105 } 14106 14107 return UnmatchedFields.empty(); 14108 } 14109 14110 static bool isLayoutCompatible(const ASTContext &C, const RecordDecl *RD1, 14111 const RecordDecl *RD2) { 14112 if (RD1->isUnion() != RD2->isUnion()) 14113 return false; 14114 14115 if (RD1->isUnion()) 14116 return isLayoutCompatibleUnion(C, RD1, RD2); 14117 else 14118 return isLayoutCompatibleStruct(C, RD1, RD2); 14119 } 14120 14121 /// Check if two types are layout-compatible in C++11 sense. 14122 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2) { 14123 if (T1.isNull() || T2.isNull()) 14124 return false; 14125 14126 // C++20 [basic.types] p11: 14127 // Two types cv1 T1 and cv2 T2 are layout-compatible types 14128 // if T1 and T2 are the same type, layout-compatible enumerations (9.7.1), 14129 // or layout-compatible standard-layout class types (11.4). 14130 T1 = T1.getCanonicalType().getUnqualifiedType(); 14131 T2 = T2.getCanonicalType().getUnqualifiedType(); 14132 14133 if (C.hasSameType(T1, T2)) 14134 return true; 14135 14136 const Type::TypeClass TC1 = T1->getTypeClass(); 14137 const Type::TypeClass TC2 = T2->getTypeClass(); 14138 14139 if (TC1 != TC2) 14140 return false; 14141 14142 if (TC1 == Type::Enum) { 14143 return isLayoutCompatible(C, 14144 cast<EnumType>(T1)->getDecl(), 14145 cast<EnumType>(T2)->getDecl()); 14146 } else if (TC1 == Type::Record) { 14147 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) 14148 return false; 14149 14150 return isLayoutCompatible(C, 14151 cast<RecordType>(T1)->getDecl(), 14152 cast<RecordType>(T2)->getDecl()); 14153 } 14154 14155 return false; 14156 } 14157 14158 bool Sema::IsLayoutCompatible(QualType T1, QualType T2) const { 14159 return isLayoutCompatible(getASTContext(), T1, T2); 14160 } 14161 14162 //===-------------- Pointer interconvertibility ----------------------------// 14163 14164 bool Sema::IsPointerInterconvertibleBaseOf(const TypeSourceInfo *Base, 14165 const TypeSourceInfo *Derived) { 14166 QualType BaseT = Base->getType()->getCanonicalTypeUnqualified(); 14167 QualType DerivedT = Derived->getType()->getCanonicalTypeUnqualified(); 14168 14169 if (BaseT->isStructureOrClassType() && DerivedT->isStructureOrClassType() && 14170 getASTContext().hasSameType(BaseT, DerivedT)) 14171 return true; 14172 14173 if (!IsDerivedFrom(Derived->getTypeLoc().getBeginLoc(), DerivedT, BaseT)) 14174 return false; 14175 14176 // Per [basic.compound]/4.3, containing object has to be standard-layout. 14177 if (DerivedT->getAsCXXRecordDecl()->isStandardLayout()) 14178 return true; 14179 14180 return false; 14181 } 14182 14183 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// 14184 14185 /// Given a type tag expression find the type tag itself. 14186 /// 14187 /// \param TypeExpr Type tag expression, as it appears in user's code. 14188 /// 14189 /// \param VD Declaration of an identifier that appears in a type tag. 14190 /// 14191 /// \param MagicValue Type tag magic value. 14192 /// 14193 /// \param isConstantEvaluated whether the evalaution should be performed in 14194 14195 /// constant context. 14196 static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, 14197 const ValueDecl **VD, uint64_t *MagicValue, 14198 bool isConstantEvaluated) { 14199 while(true) { 14200 if (!TypeExpr) 14201 return false; 14202 14203 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); 14204 14205 switch (TypeExpr->getStmtClass()) { 14206 case Stmt::UnaryOperatorClass: { 14207 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); 14208 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { 14209 TypeExpr = UO->getSubExpr(); 14210 continue; 14211 } 14212 return false; 14213 } 14214 14215 case Stmt::DeclRefExprClass: { 14216 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); 14217 *VD = DRE->getDecl(); 14218 return true; 14219 } 14220 14221 case Stmt::IntegerLiteralClass: { 14222 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); 14223 llvm::APInt MagicValueAPInt = IL->getValue(); 14224 if (MagicValueAPInt.getActiveBits() <= 64) { 14225 *MagicValue = MagicValueAPInt.getZExtValue(); 14226 return true; 14227 } else 14228 return false; 14229 } 14230 14231 case Stmt::BinaryConditionalOperatorClass: 14232 case Stmt::ConditionalOperatorClass: { 14233 const AbstractConditionalOperator *ACO = 14234 cast<AbstractConditionalOperator>(TypeExpr); 14235 bool Result; 14236 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx, 14237 isConstantEvaluated)) { 14238 if (Result) 14239 TypeExpr = ACO->getTrueExpr(); 14240 else 14241 TypeExpr = ACO->getFalseExpr(); 14242 continue; 14243 } 14244 return false; 14245 } 14246 14247 case Stmt::BinaryOperatorClass: { 14248 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); 14249 if (BO->getOpcode() == BO_Comma) { 14250 TypeExpr = BO->getRHS(); 14251 continue; 14252 } 14253 return false; 14254 } 14255 14256 default: 14257 return false; 14258 } 14259 } 14260 } 14261 14262 /// Retrieve the C type corresponding to type tag TypeExpr. 14263 /// 14264 /// \param TypeExpr Expression that specifies a type tag. 14265 /// 14266 /// \param MagicValues Registered magic values. 14267 /// 14268 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong 14269 /// kind. 14270 /// 14271 /// \param TypeInfo Information about the corresponding C type. 14272 /// 14273 /// \param isConstantEvaluated whether the evalaution should be performed in 14274 /// constant context. 14275 /// 14276 /// \returns true if the corresponding C type was found. 14277 static bool GetMatchingCType( 14278 const IdentifierInfo *ArgumentKind, const Expr *TypeExpr, 14279 const ASTContext &Ctx, 14280 const llvm::DenseMap<Sema::TypeTagMagicValue, Sema::TypeTagData> 14281 *MagicValues, 14282 bool &FoundWrongKind, Sema::TypeTagData &TypeInfo, 14283 bool isConstantEvaluated) { 14284 FoundWrongKind = false; 14285 14286 // Variable declaration that has type_tag_for_datatype attribute. 14287 const ValueDecl *VD = nullptr; 14288 14289 uint64_t MagicValue; 14290 14291 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue, isConstantEvaluated)) 14292 return false; 14293 14294 if (VD) { 14295 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { 14296 if (I->getArgumentKind() != ArgumentKind) { 14297 FoundWrongKind = true; 14298 return false; 14299 } 14300 TypeInfo.Type = I->getMatchingCType(); 14301 TypeInfo.LayoutCompatible = I->getLayoutCompatible(); 14302 TypeInfo.MustBeNull = I->getMustBeNull(); 14303 return true; 14304 } 14305 return false; 14306 } 14307 14308 if (!MagicValues) 14309 return false; 14310 14311 llvm::DenseMap<Sema::TypeTagMagicValue, 14312 Sema::TypeTagData>::const_iterator I = 14313 MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); 14314 if (I == MagicValues->end()) 14315 return false; 14316 14317 TypeInfo = I->second; 14318 return true; 14319 } 14320 14321 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, 14322 uint64_t MagicValue, QualType Type, 14323 bool LayoutCompatible, 14324 bool MustBeNull) { 14325 if (!TypeTagForDatatypeMagicValues) 14326 TypeTagForDatatypeMagicValues.reset( 14327 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); 14328 14329 TypeTagMagicValue Magic(ArgumentKind, MagicValue); 14330 (*TypeTagForDatatypeMagicValues)[Magic] = 14331 TypeTagData(Type, LayoutCompatible, MustBeNull); 14332 } 14333 14334 static bool IsSameCharType(QualType T1, QualType T2) { 14335 const BuiltinType *BT1 = T1->getAs<BuiltinType>(); 14336 if (!BT1) 14337 return false; 14338 14339 const BuiltinType *BT2 = T2->getAs<BuiltinType>(); 14340 if (!BT2) 14341 return false; 14342 14343 BuiltinType::Kind T1Kind = BT1->getKind(); 14344 BuiltinType::Kind T2Kind = BT2->getKind(); 14345 14346 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || 14347 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || 14348 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || 14349 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); 14350 } 14351 14352 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, 14353 const ArrayRef<const Expr *> ExprArgs, 14354 SourceLocation CallSiteLoc) { 14355 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); 14356 bool IsPointerAttr = Attr->getIsPointer(); 14357 14358 // Retrieve the argument representing the 'type_tag'. 14359 unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex(); 14360 if (TypeTagIdxAST >= ExprArgs.size()) { 14361 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 14362 << 0 << Attr->getTypeTagIdx().getSourceIndex(); 14363 return; 14364 } 14365 const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST]; 14366 bool FoundWrongKind; 14367 TypeTagData TypeInfo; 14368 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, 14369 TypeTagForDatatypeMagicValues.get(), FoundWrongKind, 14370 TypeInfo, isConstantEvaluatedContext())) { 14371 if (FoundWrongKind) 14372 Diag(TypeTagExpr->getExprLoc(), 14373 diag::warn_type_tag_for_datatype_wrong_kind) 14374 << TypeTagExpr->getSourceRange(); 14375 return; 14376 } 14377 14378 // Retrieve the argument representing the 'arg_idx'. 14379 unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex(); 14380 if (ArgumentIdxAST >= ExprArgs.size()) { 14381 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 14382 << 1 << Attr->getArgumentIdx().getSourceIndex(); 14383 return; 14384 } 14385 const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST]; 14386 if (IsPointerAttr) { 14387 // Skip implicit cast of pointer to `void *' (as a function argument). 14388 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) 14389 if (ICE->getType()->isVoidPointerType() && 14390 ICE->getCastKind() == CK_BitCast) 14391 ArgumentExpr = ICE->getSubExpr(); 14392 } 14393 QualType ArgumentType = ArgumentExpr->getType(); 14394 14395 // Passing a `void*' pointer shouldn't trigger a warning. 14396 if (IsPointerAttr && ArgumentType->isVoidPointerType()) 14397 return; 14398 14399 if (TypeInfo.MustBeNull) { 14400 // Type tag with matching void type requires a null pointer. 14401 if (!ArgumentExpr->isNullPointerConstant(Context, 14402 Expr::NPC_ValueDependentIsNotNull)) { 14403 Diag(ArgumentExpr->getExprLoc(), 14404 diag::warn_type_safety_null_pointer_required) 14405 << ArgumentKind->getName() 14406 << ArgumentExpr->getSourceRange() 14407 << TypeTagExpr->getSourceRange(); 14408 } 14409 return; 14410 } 14411 14412 QualType RequiredType = TypeInfo.Type; 14413 if (IsPointerAttr) 14414 RequiredType = Context.getPointerType(RequiredType); 14415 14416 bool mismatch = false; 14417 if (!TypeInfo.LayoutCompatible) { 14418 mismatch = !Context.hasSameType(ArgumentType, RequiredType); 14419 14420 // C++11 [basic.fundamental] p1: 14421 // Plain char, signed char, and unsigned char are three distinct types. 14422 // 14423 // But we treat plain `char' as equivalent to `signed char' or `unsigned 14424 // char' depending on the current char signedness mode. 14425 if (mismatch) 14426 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), 14427 RequiredType->getPointeeType())) || 14428 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) 14429 mismatch = false; 14430 } else 14431 if (IsPointerAttr) 14432 mismatch = !isLayoutCompatible(Context, 14433 ArgumentType->getPointeeType(), 14434 RequiredType->getPointeeType()); 14435 else 14436 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); 14437 14438 if (mismatch) 14439 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) 14440 << ArgumentType << ArgumentKind 14441 << TypeInfo.LayoutCompatible << RequiredType 14442 << ArgumentExpr->getSourceRange() 14443 << TypeTagExpr->getSourceRange(); 14444 } 14445 14446 void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD, 14447 CharUnits Alignment) { 14448 MisalignedMembers.emplace_back(E, RD, MD, Alignment); 14449 } 14450 14451 void Sema::DiagnoseMisalignedMembers() { 14452 for (MisalignedMember &m : MisalignedMembers) { 14453 const NamedDecl *ND = m.RD; 14454 if (ND->getName().empty()) { 14455 if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl()) 14456 ND = TD; 14457 } 14458 Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member) 14459 << m.MD << ND << m.E->getSourceRange(); 14460 } 14461 MisalignedMembers.clear(); 14462 } 14463 14464 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) { 14465 E = E->IgnoreParens(); 14466 if (!T->isPointerType() && !T->isIntegerType() && !T->isDependentType()) 14467 return; 14468 if (isa<UnaryOperator>(E) && 14469 cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) { 14470 auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 14471 if (isa<MemberExpr>(Op)) { 14472 auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op)); 14473 if (MA != MisalignedMembers.end() && 14474 (T->isDependentType() || T->isIntegerType() || 14475 (T->isPointerType() && (T->getPointeeType()->isIncompleteType() || 14476 Context.getTypeAlignInChars( 14477 T->getPointeeType()) <= MA->Alignment)))) 14478 MisalignedMembers.erase(MA); 14479 } 14480 } 14481 } 14482 14483 void Sema::RefersToMemberWithReducedAlignment( 14484 Expr *E, 14485 llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)> 14486 Action) { 14487 const auto *ME = dyn_cast<MemberExpr>(E); 14488 if (!ME) 14489 return; 14490 14491 // No need to check expressions with an __unaligned-qualified type. 14492 if (E->getType().getQualifiers().hasUnaligned()) 14493 return; 14494 14495 // For a chain of MemberExpr like "a.b.c.d" this list 14496 // will keep FieldDecl's like [d, c, b]. 14497 SmallVector<FieldDecl *, 4> ReverseMemberChain; 14498 const MemberExpr *TopME = nullptr; 14499 bool AnyIsPacked = false; 14500 do { 14501 QualType BaseType = ME->getBase()->getType(); 14502 if (BaseType->isDependentType()) 14503 return; 14504 if (ME->isArrow()) 14505 BaseType = BaseType->getPointeeType(); 14506 RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl(); 14507 if (RD->isInvalidDecl()) 14508 return; 14509 14510 ValueDecl *MD = ME->getMemberDecl(); 14511 auto *FD = dyn_cast<FieldDecl>(MD); 14512 // We do not care about non-data members. 14513 if (!FD || FD->isInvalidDecl()) 14514 return; 14515 14516 AnyIsPacked = 14517 AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>()); 14518 ReverseMemberChain.push_back(FD); 14519 14520 TopME = ME; 14521 ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens()); 14522 } while (ME); 14523 assert(TopME && "We did not compute a topmost MemberExpr!"); 14524 14525 // Not the scope of this diagnostic. 14526 if (!AnyIsPacked) 14527 return; 14528 14529 const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts(); 14530 const auto *DRE = dyn_cast<DeclRefExpr>(TopBase); 14531 // TODO: The innermost base of the member expression may be too complicated. 14532 // For now, just disregard these cases. This is left for future 14533 // improvement. 14534 if (!DRE && !isa<CXXThisExpr>(TopBase)) 14535 return; 14536 14537 // Alignment expected by the whole expression. 14538 CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType()); 14539 14540 // No need to do anything else with this case. 14541 if (ExpectedAlignment.isOne()) 14542 return; 14543 14544 // Synthesize offset of the whole access. 14545 CharUnits Offset; 14546 for (const FieldDecl *FD : llvm::reverse(ReverseMemberChain)) 14547 Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(FD)); 14548 14549 // Compute the CompleteObjectAlignment as the alignment of the whole chain. 14550 CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars( 14551 ReverseMemberChain.back()->getParent()->getTypeForDecl()); 14552 14553 // The base expression of the innermost MemberExpr may give 14554 // stronger guarantees than the class containing the member. 14555 if (DRE && !TopME->isArrow()) { 14556 const ValueDecl *VD = DRE->getDecl(); 14557 if (!VD->getType()->isReferenceType()) 14558 CompleteObjectAlignment = 14559 std::max(CompleteObjectAlignment, Context.getDeclAlign(VD)); 14560 } 14561 14562 // Check if the synthesized offset fulfills the alignment. 14563 if (Offset % ExpectedAlignment != 0 || 14564 // It may fulfill the offset it but the effective alignment may still be 14565 // lower than the expected expression alignment. 14566 CompleteObjectAlignment < ExpectedAlignment) { 14567 // If this happens, we want to determine a sensible culprit of this. 14568 // Intuitively, watching the chain of member expressions from right to 14569 // left, we start with the required alignment (as required by the field 14570 // type) but some packed attribute in that chain has reduced the alignment. 14571 // It may happen that another packed structure increases it again. But if 14572 // we are here such increase has not been enough. So pointing the first 14573 // FieldDecl that either is packed or else its RecordDecl is, 14574 // seems reasonable. 14575 FieldDecl *FD = nullptr; 14576 CharUnits Alignment; 14577 for (FieldDecl *FDI : ReverseMemberChain) { 14578 if (FDI->hasAttr<PackedAttr>() || 14579 FDI->getParent()->hasAttr<PackedAttr>()) { 14580 FD = FDI; 14581 Alignment = std::min( 14582 Context.getTypeAlignInChars(FD->getType()), 14583 Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl())); 14584 break; 14585 } 14586 } 14587 assert(FD && "We did not find a packed FieldDecl!"); 14588 Action(E, FD->getParent(), FD, Alignment); 14589 } 14590 } 14591 14592 void Sema::CheckAddressOfPackedMember(Expr *rhs) { 14593 using namespace std::placeholders; 14594 14595 RefersToMemberWithReducedAlignment( 14596 rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1, 14597 _2, _3, _4)); 14598 } 14599 14600 bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) { 14601 if (checkArgCount(TheCall, 1)) 14602 return true; 14603 14604 ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); 14605 if (A.isInvalid()) 14606 return true; 14607 14608 TheCall->setArg(0, A.get()); 14609 QualType TyA = A.get()->getType(); 14610 14611 if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14612 return true; 14613 14614 TheCall->setType(TyA); 14615 return false; 14616 } 14617 14618 bool Sema::BuiltinElementwiseMath(CallExpr *TheCall, bool FPOnly) { 14619 QualType Res; 14620 if (BuiltinVectorMath(TheCall, Res, FPOnly)) 14621 return true; 14622 TheCall->setType(Res); 14623 return false; 14624 } 14625 14626 bool Sema::BuiltinVectorToScalarMath(CallExpr *TheCall) { 14627 QualType Res; 14628 if (BuiltinVectorMath(TheCall, Res)) 14629 return true; 14630 14631 if (auto *VecTy0 = Res->getAs<VectorType>()) 14632 TheCall->setType(VecTy0->getElementType()); 14633 else 14634 TheCall->setType(Res); 14635 14636 return false; 14637 } 14638 14639 bool Sema::BuiltinVectorMath(CallExpr *TheCall, QualType &Res, bool FPOnly) { 14640 if (checkArgCount(TheCall, 2)) 14641 return true; 14642 14643 ExprResult A = TheCall->getArg(0); 14644 ExprResult B = TheCall->getArg(1); 14645 // Do standard promotions between the two arguments, returning their common 14646 // type. 14647 Res = UsualArithmeticConversions(A, B, TheCall->getExprLoc(), ACK_Comparison); 14648 if (A.isInvalid() || B.isInvalid()) 14649 return true; 14650 14651 QualType TyA = A.get()->getType(); 14652 QualType TyB = B.get()->getType(); 14653 14654 if (Res.isNull() || TyA.getCanonicalType() != TyB.getCanonicalType()) 14655 return Diag(A.get()->getBeginLoc(), 14656 diag::err_typecheck_call_different_arg_types) 14657 << TyA << TyB; 14658 14659 if (FPOnly) { 14660 if (checkFPMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14661 return true; 14662 } else { 14663 if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14664 return true; 14665 } 14666 14667 TheCall->setArg(0, A.get()); 14668 TheCall->setArg(1, B.get()); 14669 return false; 14670 } 14671 14672 bool Sema::BuiltinElementwiseTernaryMath(CallExpr *TheCall, 14673 bool CheckForFloatArgs) { 14674 if (checkArgCount(TheCall, 3)) 14675 return true; 14676 14677 Expr *Args[3]; 14678 for (int I = 0; I < 3; ++I) { 14679 ExprResult Converted = UsualUnaryConversions(TheCall->getArg(I)); 14680 if (Converted.isInvalid()) 14681 return true; 14682 Args[I] = Converted.get(); 14683 } 14684 14685 if (CheckForFloatArgs) { 14686 int ArgOrdinal = 1; 14687 for (Expr *Arg : Args) { 14688 if (checkFPMathBuiltinElementType(*this, Arg->getBeginLoc(), 14689 Arg->getType(), ArgOrdinal++)) 14690 return true; 14691 } 14692 } else { 14693 int ArgOrdinal = 1; 14694 for (Expr *Arg : Args) { 14695 if (checkMathBuiltinElementType(*this, Arg->getBeginLoc(), Arg->getType(), 14696 ArgOrdinal++)) 14697 return true; 14698 } 14699 } 14700 14701 for (int I = 1; I < 3; ++I) { 14702 if (Args[0]->getType().getCanonicalType() != 14703 Args[I]->getType().getCanonicalType()) { 14704 return Diag(Args[0]->getBeginLoc(), 14705 diag::err_typecheck_call_different_arg_types) 14706 << Args[0]->getType() << Args[I]->getType(); 14707 } 14708 14709 TheCall->setArg(I, Args[I]); 14710 } 14711 14712 TheCall->setType(Args[0]->getType()); 14713 return false; 14714 } 14715 14716 bool Sema::PrepareBuiltinReduceMathOneArgCall(CallExpr *TheCall) { 14717 if (checkArgCount(TheCall, 1)) 14718 return true; 14719 14720 ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); 14721 if (A.isInvalid()) 14722 return true; 14723 14724 TheCall->setArg(0, A.get()); 14725 return false; 14726 } 14727 14728 bool Sema::BuiltinNonDeterministicValue(CallExpr *TheCall) { 14729 if (checkArgCount(TheCall, 1)) 14730 return true; 14731 14732 ExprResult Arg = TheCall->getArg(0); 14733 QualType TyArg = Arg.get()->getType(); 14734 14735 if (!TyArg->isBuiltinType() && !TyArg->isVectorType()) 14736 return Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14737 << 1 << /*vector, integer or floating point ty*/ 0 << TyArg; 14738 14739 TheCall->setType(TyArg); 14740 return false; 14741 } 14742 14743 ExprResult Sema::BuiltinMatrixTranspose(CallExpr *TheCall, 14744 ExprResult CallResult) { 14745 if (checkArgCount(TheCall, 1)) 14746 return ExprError(); 14747 14748 ExprResult MatrixArg = DefaultLvalueConversion(TheCall->getArg(0)); 14749 if (MatrixArg.isInvalid()) 14750 return MatrixArg; 14751 Expr *Matrix = MatrixArg.get(); 14752 14753 auto *MType = Matrix->getType()->getAs<ConstantMatrixType>(); 14754 if (!MType) { 14755 Diag(Matrix->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14756 << 1 << /* matrix ty*/ 1 << Matrix->getType(); 14757 return ExprError(); 14758 } 14759 14760 // Create returned matrix type by swapping rows and columns of the argument 14761 // matrix type. 14762 QualType ResultType = Context.getConstantMatrixType( 14763 MType->getElementType(), MType->getNumColumns(), MType->getNumRows()); 14764 14765 // Change the return type to the type of the returned matrix. 14766 TheCall->setType(ResultType); 14767 14768 // Update call argument to use the possibly converted matrix argument. 14769 TheCall->setArg(0, Matrix); 14770 return CallResult; 14771 } 14772 14773 // Get and verify the matrix dimensions. 14774 static std::optional<unsigned> 14775 getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) { 14776 SourceLocation ErrorPos; 14777 std::optional<llvm::APSInt> Value = 14778 Expr->getIntegerConstantExpr(S.Context, &ErrorPos); 14779 if (!Value) { 14780 S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_scalar_unsigned_arg) 14781 << Name; 14782 return {}; 14783 } 14784 uint64_t Dim = Value->getZExtValue(); 14785 if (!ConstantMatrixType::isDimensionValid(Dim)) { 14786 S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension) 14787 << Name << ConstantMatrixType::getMaxElementsPerDimension(); 14788 return {}; 14789 } 14790 return Dim; 14791 } 14792 14793 ExprResult Sema::BuiltinMatrixColumnMajorLoad(CallExpr *TheCall, 14794 ExprResult CallResult) { 14795 if (!getLangOpts().MatrixTypes) { 14796 Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled); 14797 return ExprError(); 14798 } 14799 14800 if (checkArgCount(TheCall, 4)) 14801 return ExprError(); 14802 14803 unsigned PtrArgIdx = 0; 14804 Expr *PtrExpr = TheCall->getArg(PtrArgIdx); 14805 Expr *RowsExpr = TheCall->getArg(1); 14806 Expr *ColumnsExpr = TheCall->getArg(2); 14807 Expr *StrideExpr = TheCall->getArg(3); 14808 14809 bool ArgError = false; 14810 14811 // Check pointer argument. 14812 { 14813 ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); 14814 if (PtrConv.isInvalid()) 14815 return PtrConv; 14816 PtrExpr = PtrConv.get(); 14817 TheCall->setArg(0, PtrExpr); 14818 if (PtrExpr->isTypeDependent()) { 14819 TheCall->setType(Context.DependentTy); 14820 return TheCall; 14821 } 14822 } 14823 14824 auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); 14825 QualType ElementTy; 14826 if (!PtrTy) { 14827 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14828 << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); 14829 ArgError = true; 14830 } else { 14831 ElementTy = PtrTy->getPointeeType().getUnqualifiedType(); 14832 14833 if (!ConstantMatrixType::isValidElementType(ElementTy)) { 14834 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14835 << PtrArgIdx + 1 << /* pointer to element ty*/ 2 14836 << PtrExpr->getType(); 14837 ArgError = true; 14838 } 14839 } 14840 14841 // Apply default Lvalue conversions and convert the expression to size_t. 14842 auto ApplyArgumentConversions = [this](Expr *E) { 14843 ExprResult Conv = DefaultLvalueConversion(E); 14844 if (Conv.isInvalid()) 14845 return Conv; 14846 14847 return tryConvertExprToType(Conv.get(), Context.getSizeType()); 14848 }; 14849 14850 // Apply conversion to row and column expressions. 14851 ExprResult RowsConv = ApplyArgumentConversions(RowsExpr); 14852 if (!RowsConv.isInvalid()) { 14853 RowsExpr = RowsConv.get(); 14854 TheCall->setArg(1, RowsExpr); 14855 } else 14856 RowsExpr = nullptr; 14857 14858 ExprResult ColumnsConv = ApplyArgumentConversions(ColumnsExpr); 14859 if (!ColumnsConv.isInvalid()) { 14860 ColumnsExpr = ColumnsConv.get(); 14861 TheCall->setArg(2, ColumnsExpr); 14862 } else 14863 ColumnsExpr = nullptr; 14864 14865 // If any part of the result matrix type is still pending, just use 14866 // Context.DependentTy, until all parts are resolved. 14867 if ((RowsExpr && RowsExpr->isTypeDependent()) || 14868 (ColumnsExpr && ColumnsExpr->isTypeDependent())) { 14869 TheCall->setType(Context.DependentTy); 14870 return CallResult; 14871 } 14872 14873 // Check row and column dimensions. 14874 std::optional<unsigned> MaybeRows; 14875 if (RowsExpr) 14876 MaybeRows = getAndVerifyMatrixDimension(RowsExpr, "row", *this); 14877 14878 std::optional<unsigned> MaybeColumns; 14879 if (ColumnsExpr) 14880 MaybeColumns = getAndVerifyMatrixDimension(ColumnsExpr, "column", *this); 14881 14882 // Check stride argument. 14883 ExprResult StrideConv = ApplyArgumentConversions(StrideExpr); 14884 if (StrideConv.isInvalid()) 14885 return ExprError(); 14886 StrideExpr = StrideConv.get(); 14887 TheCall->setArg(3, StrideExpr); 14888 14889 if (MaybeRows) { 14890 if (std::optional<llvm::APSInt> Value = 14891 StrideExpr->getIntegerConstantExpr(Context)) { 14892 uint64_t Stride = Value->getZExtValue(); 14893 if (Stride < *MaybeRows) { 14894 Diag(StrideExpr->getBeginLoc(), 14895 diag::err_builtin_matrix_stride_too_small); 14896 ArgError = true; 14897 } 14898 } 14899 } 14900 14901 if (ArgError || !MaybeRows || !MaybeColumns) 14902 return ExprError(); 14903 14904 TheCall->setType( 14905 Context.getConstantMatrixType(ElementTy, *MaybeRows, *MaybeColumns)); 14906 return CallResult; 14907 } 14908 14909 ExprResult Sema::BuiltinMatrixColumnMajorStore(CallExpr *TheCall, 14910 ExprResult CallResult) { 14911 if (checkArgCount(TheCall, 3)) 14912 return ExprError(); 14913 14914 unsigned PtrArgIdx = 1; 14915 Expr *MatrixExpr = TheCall->getArg(0); 14916 Expr *PtrExpr = TheCall->getArg(PtrArgIdx); 14917 Expr *StrideExpr = TheCall->getArg(2); 14918 14919 bool ArgError = false; 14920 14921 { 14922 ExprResult MatrixConv = DefaultLvalueConversion(MatrixExpr); 14923 if (MatrixConv.isInvalid()) 14924 return MatrixConv; 14925 MatrixExpr = MatrixConv.get(); 14926 TheCall->setArg(0, MatrixExpr); 14927 } 14928 if (MatrixExpr->isTypeDependent()) { 14929 TheCall->setType(Context.DependentTy); 14930 return TheCall; 14931 } 14932 14933 auto *MatrixTy = MatrixExpr->getType()->getAs<ConstantMatrixType>(); 14934 if (!MatrixTy) { 14935 Diag(MatrixExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14936 << 1 << /*matrix ty */ 1 << MatrixExpr->getType(); 14937 ArgError = true; 14938 } 14939 14940 { 14941 ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); 14942 if (PtrConv.isInvalid()) 14943 return PtrConv; 14944 PtrExpr = PtrConv.get(); 14945 TheCall->setArg(1, PtrExpr); 14946 if (PtrExpr->isTypeDependent()) { 14947 TheCall->setType(Context.DependentTy); 14948 return TheCall; 14949 } 14950 } 14951 14952 // Check pointer argument. 14953 auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); 14954 if (!PtrTy) { 14955 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14956 << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); 14957 ArgError = true; 14958 } else { 14959 QualType ElementTy = PtrTy->getPointeeType(); 14960 if (ElementTy.isConstQualified()) { 14961 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_store_to_const); 14962 ArgError = true; 14963 } 14964 ElementTy = ElementTy.getUnqualifiedType().getCanonicalType(); 14965 if (MatrixTy && 14966 !Context.hasSameType(ElementTy, MatrixTy->getElementType())) { 14967 Diag(PtrExpr->getBeginLoc(), 14968 diag::err_builtin_matrix_pointer_arg_mismatch) 14969 << ElementTy << MatrixTy->getElementType(); 14970 ArgError = true; 14971 } 14972 } 14973 14974 // Apply default Lvalue conversions and convert the stride expression to 14975 // size_t. 14976 { 14977 ExprResult StrideConv = DefaultLvalueConversion(StrideExpr); 14978 if (StrideConv.isInvalid()) 14979 return StrideConv; 14980 14981 StrideConv = tryConvertExprToType(StrideConv.get(), Context.getSizeType()); 14982 if (StrideConv.isInvalid()) 14983 return StrideConv; 14984 StrideExpr = StrideConv.get(); 14985 TheCall->setArg(2, StrideExpr); 14986 } 14987 14988 // Check stride argument. 14989 if (MatrixTy) { 14990 if (std::optional<llvm::APSInt> Value = 14991 StrideExpr->getIntegerConstantExpr(Context)) { 14992 uint64_t Stride = Value->getZExtValue(); 14993 if (Stride < MatrixTy->getNumRows()) { 14994 Diag(StrideExpr->getBeginLoc(), 14995 diag::err_builtin_matrix_stride_too_small); 14996 ArgError = true; 14997 } 14998 } 14999 } 15000 15001 if (ArgError) 15002 return ExprError(); 15003 15004 return CallResult; 15005 } 15006 15007 void Sema::CheckTCBEnforcement(const SourceLocation CallExprLoc, 15008 const NamedDecl *Callee) { 15009 // This warning does not make sense in code that has no runtime behavior. 15010 if (isUnevaluatedContext()) 15011 return; 15012 15013 const NamedDecl *Caller = getCurFunctionOrMethodDecl(); 15014 15015 if (!Caller || !Caller->hasAttr<EnforceTCBAttr>()) 15016 return; 15017 15018 // Search through the enforce_tcb and enforce_tcb_leaf attributes to find 15019 // all TCBs the callee is a part of. 15020 llvm::StringSet<> CalleeTCBs; 15021 for (const auto *A : Callee->specific_attrs<EnforceTCBAttr>()) 15022 CalleeTCBs.insert(A->getTCBName()); 15023 for (const auto *A : Callee->specific_attrs<EnforceTCBLeafAttr>()) 15024 CalleeTCBs.insert(A->getTCBName()); 15025 15026 // Go through the TCBs the caller is a part of and emit warnings if Caller 15027 // is in a TCB that the Callee is not. 15028 for (const auto *A : Caller->specific_attrs<EnforceTCBAttr>()) { 15029 StringRef CallerTCB = A->getTCBName(); 15030 if (CalleeTCBs.count(CallerTCB) == 0) { 15031 this->Diag(CallExprLoc, diag::warn_tcb_enforcement_violation) 15032 << Callee << CallerTCB; 15033 } 15034 } 15035 } 15036