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/SemaSystemZ.h" 74 #include "clang/Sema/SemaWasm.h" 75 #include "clang/Sema/SemaX86.h" 76 #include "llvm/ADT/APFloat.h" 77 #include "llvm/ADT/APInt.h" 78 #include "llvm/ADT/APSInt.h" 79 #include "llvm/ADT/ArrayRef.h" 80 #include "llvm/ADT/DenseMap.h" 81 #include "llvm/ADT/FoldingSet.h" 82 #include "llvm/ADT/STLExtras.h" 83 #include "llvm/ADT/SmallBitVector.h" 84 #include "llvm/ADT/SmallPtrSet.h" 85 #include "llvm/ADT/SmallString.h" 86 #include "llvm/ADT/SmallVector.h" 87 #include "llvm/ADT/StringExtras.h" 88 #include "llvm/ADT/StringRef.h" 89 #include "llvm/ADT/StringSet.h" 90 #include "llvm/ADT/StringSwitch.h" 91 #include "llvm/Support/AtomicOrdering.h" 92 #include "llvm/Support/Compiler.h" 93 #include "llvm/Support/ConvertUTF.h" 94 #include "llvm/Support/ErrorHandling.h" 95 #include "llvm/Support/Format.h" 96 #include "llvm/Support/Locale.h" 97 #include "llvm/Support/MathExtras.h" 98 #include "llvm/Support/SaveAndRestore.h" 99 #include "llvm/Support/raw_ostream.h" 100 #include "llvm/TargetParser/RISCVTargetParser.h" 101 #include "llvm/TargetParser/Triple.h" 102 #include <algorithm> 103 #include <cassert> 104 #include <cctype> 105 #include <cstddef> 106 #include <cstdint> 107 #include <functional> 108 #include <limits> 109 #include <optional> 110 #include <string> 111 #include <tuple> 112 #include <utility> 113 114 using namespace clang; 115 using namespace sema; 116 117 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, 118 unsigned ByteNo) const { 119 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, 120 Context.getTargetInfo()); 121 } 122 123 static constexpr unsigned short combineFAPK(Sema::FormatArgumentPassingKind A, 124 Sema::FormatArgumentPassingKind B) { 125 return (A << 8) | B; 126 } 127 128 bool Sema::checkArgCountAtLeast(CallExpr *Call, unsigned MinArgCount) { 129 unsigned ArgCount = Call->getNumArgs(); 130 if (ArgCount >= MinArgCount) 131 return false; 132 133 return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_few_args) 134 << 0 /*function call*/ << MinArgCount << ArgCount 135 << /*is non object*/ 0 << Call->getSourceRange(); 136 } 137 138 bool Sema::checkArgCountAtMost(CallExpr *Call, unsigned MaxArgCount) { 139 unsigned ArgCount = Call->getNumArgs(); 140 if (ArgCount <= MaxArgCount) 141 return false; 142 return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_many_args_at_most) 143 << 0 /*function call*/ << MaxArgCount << ArgCount 144 << /*is non object*/ 0 << Call->getSourceRange(); 145 } 146 147 bool Sema::checkArgCountRange(CallExpr *Call, unsigned MinArgCount, 148 unsigned MaxArgCount) { 149 return checkArgCountAtLeast(Call, MinArgCount) || 150 checkArgCountAtMost(Call, MaxArgCount); 151 } 152 153 bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) { 154 unsigned ArgCount = Call->getNumArgs(); 155 if (ArgCount == DesiredArgCount) 156 return false; 157 158 if (checkArgCountAtLeast(Call, DesiredArgCount)) 159 return true; 160 assert(ArgCount > DesiredArgCount && "should have diagnosed this"); 161 162 // Highlight all the excess arguments. 163 SourceRange Range(Call->getArg(DesiredArgCount)->getBeginLoc(), 164 Call->getArg(ArgCount - 1)->getEndLoc()); 165 166 return Diag(Range.getBegin(), diag::err_typecheck_call_too_many_args) 167 << 0 /*function call*/ << DesiredArgCount << ArgCount 168 << /*is non object*/ 0 << Call->getArg(1)->getSourceRange(); 169 } 170 171 static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) { 172 bool HasError = false; 173 174 for (unsigned I = 0; I < Call->getNumArgs(); ++I) { 175 Expr *Arg = Call->getArg(I); 176 177 if (Arg->isValueDependent()) 178 continue; 179 180 std::optional<std::string> ArgString = Arg->tryEvaluateString(S.Context); 181 int DiagMsgKind = -1; 182 // Arguments must be pointers to constant strings and cannot use '$'. 183 if (!ArgString.has_value()) 184 DiagMsgKind = 0; 185 else if (ArgString->find('$') != std::string::npos) 186 DiagMsgKind = 1; 187 188 if (DiagMsgKind >= 0) { 189 S.Diag(Arg->getBeginLoc(), diag::err_builtin_verbose_trap_arg) 190 << DiagMsgKind << Arg->getSourceRange(); 191 HasError = true; 192 } 193 } 194 195 return !HasError; 196 } 197 198 static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) { 199 if (Value->isTypeDependent()) 200 return false; 201 202 InitializedEntity Entity = 203 InitializedEntity::InitializeParameter(S.Context, Ty, false); 204 ExprResult Result = 205 S.PerformCopyInitialization(Entity, SourceLocation(), Value); 206 if (Result.isInvalid()) 207 return true; 208 Value = Result.get(); 209 return false; 210 } 211 212 /// Check that the first argument to __builtin_annotation is an integer 213 /// and the second argument is a non-wide string literal. 214 static bool BuiltinAnnotation(Sema &S, CallExpr *TheCall) { 215 if (S.checkArgCount(TheCall, 2)) 216 return true; 217 218 // First argument should be an integer. 219 Expr *ValArg = TheCall->getArg(0); 220 QualType Ty = ValArg->getType(); 221 if (!Ty->isIntegerType()) { 222 S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg) 223 << ValArg->getSourceRange(); 224 return true; 225 } 226 227 // Second argument should be a constant string. 228 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); 229 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); 230 if (!Literal || !Literal->isOrdinary()) { 231 S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg) 232 << StrArg->getSourceRange(); 233 return true; 234 } 235 236 TheCall->setType(Ty); 237 return false; 238 } 239 240 static bool BuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) { 241 // We need at least one argument. 242 if (TheCall->getNumArgs() < 1) { 243 S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 244 << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 245 << TheCall->getCallee()->getSourceRange(); 246 return true; 247 } 248 249 // All arguments should be wide string literals. 250 for (Expr *Arg : TheCall->arguments()) { 251 auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 252 if (!Literal || !Literal->isWide()) { 253 S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str) 254 << Arg->getSourceRange(); 255 return true; 256 } 257 } 258 259 return false; 260 } 261 262 /// Check that the argument to __builtin_addressof is a glvalue, and set the 263 /// result type to the corresponding pointer type. 264 static bool BuiltinAddressof(Sema &S, CallExpr *TheCall) { 265 if (S.checkArgCount(TheCall, 1)) 266 return true; 267 268 ExprResult Arg(TheCall->getArg(0)); 269 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc()); 270 if (ResultType.isNull()) 271 return true; 272 273 TheCall->setArg(0, Arg.get()); 274 TheCall->setType(ResultType); 275 return false; 276 } 277 278 /// Check that the argument to __builtin_function_start is a function. 279 static bool BuiltinFunctionStart(Sema &S, CallExpr *TheCall) { 280 if (S.checkArgCount(TheCall, 1)) 281 return true; 282 283 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 284 if (Arg.isInvalid()) 285 return true; 286 287 TheCall->setArg(0, Arg.get()); 288 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>( 289 Arg.get()->getAsBuiltinConstantDeclRef(S.getASTContext())); 290 291 if (!FD) { 292 S.Diag(TheCall->getBeginLoc(), diag::err_function_start_invalid_type) 293 << TheCall->getSourceRange(); 294 return true; 295 } 296 297 return !S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, 298 TheCall->getBeginLoc()); 299 } 300 301 /// Check the number of arguments and set the result type to 302 /// the argument type. 303 static bool BuiltinPreserveAI(Sema &S, CallExpr *TheCall) { 304 if (S.checkArgCount(TheCall, 1)) 305 return true; 306 307 TheCall->setType(TheCall->getArg(0)->getType()); 308 return false; 309 } 310 311 /// Check that the value argument for __builtin_is_aligned(value, alignment) and 312 /// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer 313 /// type (but not a function pointer) and that the alignment is a power-of-two. 314 static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) { 315 if (S.checkArgCount(TheCall, 2)) 316 return true; 317 318 clang::Expr *Source = TheCall->getArg(0); 319 bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned; 320 321 auto IsValidIntegerType = [](QualType Ty) { 322 return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType(); 323 }; 324 QualType SrcTy = Source->getType(); 325 // We should also be able to use it with arrays (but not functions!). 326 if (SrcTy->canDecayToPointerType() && SrcTy->isArrayType()) { 327 SrcTy = S.Context.getDecayedType(SrcTy); 328 } 329 if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) || 330 SrcTy->isFunctionPointerType()) { 331 // FIXME: this is not quite the right error message since we don't allow 332 // floating point types, or member pointers. 333 S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand) 334 << SrcTy; 335 return true; 336 } 337 338 clang::Expr *AlignOp = TheCall->getArg(1); 339 if (!IsValidIntegerType(AlignOp->getType())) { 340 S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int) 341 << AlignOp->getType(); 342 return true; 343 } 344 Expr::EvalResult AlignResult; 345 unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1; 346 // We can't check validity of alignment if it is value dependent. 347 if (!AlignOp->isValueDependent() && 348 AlignOp->EvaluateAsInt(AlignResult, S.Context, 349 Expr::SE_AllowSideEffects)) { 350 llvm::APSInt AlignValue = AlignResult.Val.getInt(); 351 llvm::APSInt MaxValue( 352 llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits)); 353 if (AlignValue < 1) { 354 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1; 355 return true; 356 } 357 if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) { 358 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big) 359 << toString(MaxValue, 10); 360 return true; 361 } 362 if (!AlignValue.isPowerOf2()) { 363 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two); 364 return true; 365 } 366 if (AlignValue == 1) { 367 S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless) 368 << IsBooleanAlignBuiltin; 369 } 370 } 371 372 ExprResult SrcArg = S.PerformCopyInitialization( 373 InitializedEntity::InitializeParameter(S.Context, SrcTy, false), 374 SourceLocation(), Source); 375 if (SrcArg.isInvalid()) 376 return true; 377 TheCall->setArg(0, SrcArg.get()); 378 ExprResult AlignArg = 379 S.PerformCopyInitialization(InitializedEntity::InitializeParameter( 380 S.Context, AlignOp->getType(), false), 381 SourceLocation(), AlignOp); 382 if (AlignArg.isInvalid()) 383 return true; 384 TheCall->setArg(1, AlignArg.get()); 385 // For align_up/align_down, the return type is the same as the (potentially 386 // decayed) argument type including qualifiers. For is_aligned(), the result 387 // is always bool. 388 TheCall->setType(IsBooleanAlignBuiltin ? S.Context.BoolTy : SrcTy); 389 return false; 390 } 391 392 static bool BuiltinOverflow(Sema &S, CallExpr *TheCall, unsigned BuiltinID) { 393 if (S.checkArgCount(TheCall, 3)) 394 return true; 395 396 std::pair<unsigned, const char *> Builtins[] = { 397 { Builtin::BI__builtin_add_overflow, "ckd_add" }, 398 { Builtin::BI__builtin_sub_overflow, "ckd_sub" }, 399 { Builtin::BI__builtin_mul_overflow, "ckd_mul" }, 400 }; 401 402 bool CkdOperation = llvm::any_of(Builtins, [&](const std::pair<unsigned, 403 const char *> &P) { 404 return BuiltinID == P.first && TheCall->getExprLoc().isMacroID() && 405 Lexer::getImmediateMacroName(TheCall->getExprLoc(), 406 S.getSourceManager(), S.getLangOpts()) == P.second; 407 }); 408 409 auto ValidCkdIntType = [](QualType QT) { 410 // A valid checked integer type is an integer type other than a plain char, 411 // bool, a bit-precise type, or an enumeration type. 412 if (const auto *BT = QT.getCanonicalType()->getAs<BuiltinType>()) 413 return (BT->getKind() >= BuiltinType::Short && 414 BT->getKind() <= BuiltinType::Int128) || ( 415 BT->getKind() >= BuiltinType::UShort && 416 BT->getKind() <= BuiltinType::UInt128) || 417 BT->getKind() == BuiltinType::UChar || 418 BT->getKind() == BuiltinType::SChar; 419 return false; 420 }; 421 422 // First two arguments should be integers. 423 for (unsigned I = 0; I < 2; ++I) { 424 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I)); 425 if (Arg.isInvalid()) return true; 426 TheCall->setArg(I, Arg.get()); 427 428 QualType Ty = Arg.get()->getType(); 429 bool IsValid = CkdOperation ? ValidCkdIntType(Ty) : Ty->isIntegerType(); 430 if (!IsValid) { 431 S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int) 432 << CkdOperation << Ty << Arg.get()->getSourceRange(); 433 return true; 434 } 435 } 436 437 // Third argument should be a pointer to a non-const integer. 438 // IRGen correctly handles volatile, restrict, and address spaces, and 439 // the other qualifiers aren't possible. 440 { 441 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2)); 442 if (Arg.isInvalid()) return true; 443 TheCall->setArg(2, Arg.get()); 444 445 QualType Ty = Arg.get()->getType(); 446 const auto *PtrTy = Ty->getAs<PointerType>(); 447 if (!PtrTy || 448 !PtrTy->getPointeeType()->isIntegerType() || 449 (!ValidCkdIntType(PtrTy->getPointeeType()) && CkdOperation) || 450 PtrTy->getPointeeType().isConstQualified()) { 451 S.Diag(Arg.get()->getBeginLoc(), 452 diag::err_overflow_builtin_must_be_ptr_int) 453 << CkdOperation << Ty << Arg.get()->getSourceRange(); 454 return true; 455 } 456 } 457 458 // Disallow signed bit-precise integer args larger than 128 bits to mul 459 // function until we improve backend support. 460 if (BuiltinID == Builtin::BI__builtin_mul_overflow) { 461 for (unsigned I = 0; I < 3; ++I) { 462 const auto Arg = TheCall->getArg(I); 463 // Third argument will be a pointer. 464 auto Ty = I < 2 ? Arg->getType() : Arg->getType()->getPointeeType(); 465 if (Ty->isBitIntType() && Ty->isSignedIntegerType() && 466 S.getASTContext().getIntWidth(Ty) > 128) 467 return S.Diag(Arg->getBeginLoc(), 468 diag::err_overflow_builtin_bit_int_max_size) 469 << 128; 470 } 471 } 472 473 return false; 474 } 475 476 namespace { 477 struct BuiltinDumpStructGenerator { 478 Sema &S; 479 CallExpr *TheCall; 480 SourceLocation Loc = TheCall->getBeginLoc(); 481 SmallVector<Expr *, 32> Actions; 482 DiagnosticErrorTrap ErrorTracker; 483 PrintingPolicy Policy; 484 485 BuiltinDumpStructGenerator(Sema &S, CallExpr *TheCall) 486 : S(S), TheCall(TheCall), ErrorTracker(S.getDiagnostics()), 487 Policy(S.Context.getPrintingPolicy()) { 488 Policy.AnonymousTagLocations = false; 489 } 490 491 Expr *makeOpaqueValueExpr(Expr *Inner) { 492 auto *OVE = new (S.Context) 493 OpaqueValueExpr(Loc, Inner->getType(), Inner->getValueKind(), 494 Inner->getObjectKind(), Inner); 495 Actions.push_back(OVE); 496 return OVE; 497 } 498 499 Expr *getStringLiteral(llvm::StringRef Str) { 500 Expr *Lit = S.Context.getPredefinedStringLiteralFromCache(Str); 501 // Wrap the literal in parentheses to attach a source location. 502 return new (S.Context) ParenExpr(Loc, Loc, Lit); 503 } 504 505 bool callPrintFunction(llvm::StringRef Format, 506 llvm::ArrayRef<Expr *> Exprs = {}) { 507 SmallVector<Expr *, 8> Args; 508 assert(TheCall->getNumArgs() >= 2); 509 Args.reserve((TheCall->getNumArgs() - 2) + /*Format*/ 1 + Exprs.size()); 510 Args.assign(TheCall->arg_begin() + 2, TheCall->arg_end()); 511 Args.push_back(getStringLiteral(Format)); 512 Args.insert(Args.end(), Exprs.begin(), Exprs.end()); 513 514 // Register a note to explain why we're performing the call. 515 Sema::CodeSynthesisContext Ctx; 516 Ctx.Kind = Sema::CodeSynthesisContext::BuildingBuiltinDumpStructCall; 517 Ctx.PointOfInstantiation = Loc; 518 Ctx.CallArgs = Args.data(); 519 Ctx.NumCallArgs = Args.size(); 520 S.pushCodeSynthesisContext(Ctx); 521 522 ExprResult RealCall = 523 S.BuildCallExpr(/*Scope=*/nullptr, TheCall->getArg(1), 524 TheCall->getBeginLoc(), Args, TheCall->getRParenLoc()); 525 526 S.popCodeSynthesisContext(); 527 if (!RealCall.isInvalid()) 528 Actions.push_back(RealCall.get()); 529 // Bail out if we've hit any errors, even if we managed to build the 530 // call. We don't want to produce more than one error. 531 return RealCall.isInvalid() || ErrorTracker.hasErrorOccurred(); 532 } 533 534 Expr *getIndentString(unsigned Depth) { 535 if (!Depth) 536 return nullptr; 537 538 llvm::SmallString<32> Indent; 539 Indent.resize(Depth * Policy.Indentation, ' '); 540 return getStringLiteral(Indent); 541 } 542 543 Expr *getTypeString(QualType T) { 544 return getStringLiteral(T.getAsString(Policy)); 545 } 546 547 bool appendFormatSpecifier(QualType T, llvm::SmallVectorImpl<char> &Str) { 548 llvm::raw_svector_ostream OS(Str); 549 550 // Format 'bool', 'char', 'signed char', 'unsigned char' as numbers, rather 551 // than trying to print a single character. 552 if (auto *BT = T->getAs<BuiltinType>()) { 553 switch (BT->getKind()) { 554 case BuiltinType::Bool: 555 OS << "%d"; 556 return true; 557 case BuiltinType::Char_U: 558 case BuiltinType::UChar: 559 OS << "%hhu"; 560 return true; 561 case BuiltinType::Char_S: 562 case BuiltinType::SChar: 563 OS << "%hhd"; 564 return true; 565 default: 566 break; 567 } 568 } 569 570 analyze_printf::PrintfSpecifier Specifier; 571 if (Specifier.fixType(T, S.getLangOpts(), S.Context, /*IsObjCLiteral=*/false)) { 572 // We were able to guess how to format this. 573 if (Specifier.getConversionSpecifier().getKind() == 574 analyze_printf::PrintfConversionSpecifier::sArg) { 575 // Wrap double-quotes around a '%s' specifier and limit its maximum 576 // length. Ideally we'd also somehow escape special characters in the 577 // contents but printf doesn't support that. 578 // FIXME: '%s' formatting is not safe in general. 579 OS << '"'; 580 Specifier.setPrecision(analyze_printf::OptionalAmount(32u)); 581 Specifier.toString(OS); 582 OS << '"'; 583 // FIXME: It would be nice to include a '...' if the string doesn't fit 584 // in the length limit. 585 } else { 586 Specifier.toString(OS); 587 } 588 return true; 589 } 590 591 if (T->isPointerType()) { 592 // Format all pointers with '%p'. 593 OS << "%p"; 594 return true; 595 } 596 597 return false; 598 } 599 600 bool dumpUnnamedRecord(const RecordDecl *RD, Expr *E, unsigned Depth) { 601 Expr *IndentLit = getIndentString(Depth); 602 Expr *TypeLit = getTypeString(S.Context.getRecordType(RD)); 603 if (IndentLit ? callPrintFunction("%s%s", {IndentLit, TypeLit}) 604 : callPrintFunction("%s", {TypeLit})) 605 return true; 606 607 return dumpRecordValue(RD, E, IndentLit, Depth); 608 } 609 610 // Dump a record value. E should be a pointer or lvalue referring to an RD. 611 bool dumpRecordValue(const RecordDecl *RD, Expr *E, Expr *RecordIndent, 612 unsigned Depth) { 613 // FIXME: Decide what to do if RD is a union. At least we should probably 614 // turn off printing `const char*` members with `%s`, because that is very 615 // likely to crash if that's not the active member. Whatever we decide, we 616 // should document it. 617 618 // Build an OpaqueValueExpr so we can refer to E more than once without 619 // triggering re-evaluation. 620 Expr *RecordArg = makeOpaqueValueExpr(E); 621 bool RecordArgIsPtr = RecordArg->getType()->isPointerType(); 622 623 if (callPrintFunction(" {\n")) 624 return true; 625 626 // Dump each base class, regardless of whether they're aggregates. 627 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 628 for (const auto &Base : CXXRD->bases()) { 629 QualType BaseType = 630 RecordArgIsPtr ? S.Context.getPointerType(Base.getType()) 631 : S.Context.getLValueReferenceType(Base.getType()); 632 ExprResult BasePtr = S.BuildCStyleCastExpr( 633 Loc, S.Context.getTrivialTypeSourceInfo(BaseType, Loc), Loc, 634 RecordArg); 635 if (BasePtr.isInvalid() || 636 dumpUnnamedRecord(Base.getType()->getAsRecordDecl(), BasePtr.get(), 637 Depth + 1)) 638 return true; 639 } 640 } 641 642 Expr *FieldIndentArg = getIndentString(Depth + 1); 643 644 // Dump each field. 645 for (auto *D : RD->decls()) { 646 auto *IFD = dyn_cast<IndirectFieldDecl>(D); 647 auto *FD = IFD ? IFD->getAnonField() : dyn_cast<FieldDecl>(D); 648 if (!FD || FD->isUnnamedBitField() || FD->isAnonymousStructOrUnion()) 649 continue; 650 651 llvm::SmallString<20> Format = llvm::StringRef("%s%s %s "); 652 llvm::SmallVector<Expr *, 5> Args = {FieldIndentArg, 653 getTypeString(FD->getType()), 654 getStringLiteral(FD->getName())}; 655 656 if (FD->isBitField()) { 657 Format += ": %zu "; 658 QualType SizeT = S.Context.getSizeType(); 659 llvm::APInt BitWidth(S.Context.getIntWidth(SizeT), 660 FD->getBitWidthValue(S.Context)); 661 Args.push_back(IntegerLiteral::Create(S.Context, BitWidth, SizeT, Loc)); 662 } 663 664 Format += "="; 665 666 ExprResult Field = 667 IFD ? S.BuildAnonymousStructUnionMemberReference( 668 CXXScopeSpec(), Loc, IFD, 669 DeclAccessPair::make(IFD, AS_public), RecordArg, Loc) 670 : S.BuildFieldReferenceExpr( 671 RecordArg, RecordArgIsPtr, Loc, CXXScopeSpec(), FD, 672 DeclAccessPair::make(FD, AS_public), 673 DeclarationNameInfo(FD->getDeclName(), Loc)); 674 if (Field.isInvalid()) 675 return true; 676 677 auto *InnerRD = FD->getType()->getAsRecordDecl(); 678 auto *InnerCXXRD = dyn_cast_or_null<CXXRecordDecl>(InnerRD); 679 if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) { 680 // Recursively print the values of members of aggregate record type. 681 if (callPrintFunction(Format, Args) || 682 dumpRecordValue(InnerRD, Field.get(), FieldIndentArg, Depth + 1)) 683 return true; 684 } else { 685 Format += " "; 686 if (appendFormatSpecifier(FD->getType(), Format)) { 687 // We know how to print this field. 688 Args.push_back(Field.get()); 689 } else { 690 // We don't know how to print this field. Print out its address 691 // with a format specifier that a smart tool will be able to 692 // recognize and treat specially. 693 Format += "*%p"; 694 ExprResult FieldAddr = 695 S.BuildUnaryOp(nullptr, Loc, UO_AddrOf, Field.get()); 696 if (FieldAddr.isInvalid()) 697 return true; 698 Args.push_back(FieldAddr.get()); 699 } 700 Format += "\n"; 701 if (callPrintFunction(Format, Args)) 702 return true; 703 } 704 } 705 706 return RecordIndent ? callPrintFunction("%s}\n", RecordIndent) 707 : callPrintFunction("}\n"); 708 } 709 710 Expr *buildWrapper() { 711 auto *Wrapper = PseudoObjectExpr::Create(S.Context, TheCall, Actions, 712 PseudoObjectExpr::NoResult); 713 TheCall->setType(Wrapper->getType()); 714 TheCall->setValueKind(Wrapper->getValueKind()); 715 return Wrapper; 716 } 717 }; 718 } // namespace 719 720 static ExprResult BuiltinDumpStruct(Sema &S, CallExpr *TheCall) { 721 if (S.checkArgCountAtLeast(TheCall, 2)) 722 return ExprError(); 723 724 ExprResult PtrArgResult = S.DefaultLvalueConversion(TheCall->getArg(0)); 725 if (PtrArgResult.isInvalid()) 726 return ExprError(); 727 TheCall->setArg(0, PtrArgResult.get()); 728 729 // First argument should be a pointer to a struct. 730 QualType PtrArgType = PtrArgResult.get()->getType(); 731 if (!PtrArgType->isPointerType() || 732 !PtrArgType->getPointeeType()->isRecordType()) { 733 S.Diag(PtrArgResult.get()->getBeginLoc(), 734 diag::err_expected_struct_pointer_argument) 735 << 1 << TheCall->getDirectCallee() << PtrArgType; 736 return ExprError(); 737 } 738 QualType Pointee = PtrArgType->getPointeeType(); 739 const RecordDecl *RD = Pointee->getAsRecordDecl(); 740 // Try to instantiate the class template as appropriate; otherwise, access to 741 // its data() may lead to a crash. 742 if (S.RequireCompleteType(PtrArgResult.get()->getBeginLoc(), Pointee, 743 diag::err_incomplete_type)) 744 return ExprError(); 745 // Second argument is a callable, but we can't fully validate it until we try 746 // calling it. 747 QualType FnArgType = TheCall->getArg(1)->getType(); 748 if (!FnArgType->isFunctionType() && !FnArgType->isFunctionPointerType() && 749 !FnArgType->isBlockPointerType() && 750 !(S.getLangOpts().CPlusPlus && FnArgType->isRecordType())) { 751 auto *BT = FnArgType->getAs<BuiltinType>(); 752 switch (BT ? BT->getKind() : BuiltinType::Void) { 753 case BuiltinType::Dependent: 754 case BuiltinType::Overload: 755 case BuiltinType::BoundMember: 756 case BuiltinType::PseudoObject: 757 case BuiltinType::UnknownAny: 758 case BuiltinType::BuiltinFn: 759 // This might be a callable. 760 break; 761 762 default: 763 S.Diag(TheCall->getArg(1)->getBeginLoc(), 764 diag::err_expected_callable_argument) 765 << 2 << TheCall->getDirectCallee() << FnArgType; 766 return ExprError(); 767 } 768 } 769 770 BuiltinDumpStructGenerator Generator(S, TheCall); 771 772 // Wrap parentheses around the given pointer. This is not necessary for 773 // correct code generation, but it means that when we pretty-print the call 774 // arguments in our diagnostics we will produce '(&s)->n' instead of the 775 // incorrect '&s->n'. 776 Expr *PtrArg = PtrArgResult.get(); 777 PtrArg = new (S.Context) 778 ParenExpr(PtrArg->getBeginLoc(), 779 S.getLocForEndOfToken(PtrArg->getEndLoc()), PtrArg); 780 if (Generator.dumpUnnamedRecord(RD, PtrArg, 0)) 781 return ExprError(); 782 783 return Generator.buildWrapper(); 784 } 785 786 static bool BuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { 787 if (S.checkArgCount(BuiltinCall, 2)) 788 return true; 789 790 SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc(); 791 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); 792 Expr *Call = BuiltinCall->getArg(0); 793 Expr *Chain = BuiltinCall->getArg(1); 794 795 if (Call->getStmtClass() != Stmt::CallExprClass) { 796 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) 797 << Call->getSourceRange(); 798 return true; 799 } 800 801 auto CE = cast<CallExpr>(Call); 802 if (CE->getCallee()->getType()->isBlockPointerType()) { 803 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) 804 << Call->getSourceRange(); 805 return true; 806 } 807 808 const Decl *TargetDecl = CE->getCalleeDecl(); 809 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) 810 if (FD->getBuiltinID()) { 811 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) 812 << Call->getSourceRange(); 813 return true; 814 } 815 816 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { 817 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) 818 << Call->getSourceRange(); 819 return true; 820 } 821 822 ExprResult ChainResult = S.UsualUnaryConversions(Chain); 823 if (ChainResult.isInvalid()) 824 return true; 825 if (!ChainResult.get()->getType()->isPointerType()) { 826 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) 827 << Chain->getSourceRange(); 828 return true; 829 } 830 831 QualType ReturnTy = CE->getCallReturnType(S.Context); 832 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; 833 QualType BuiltinTy = S.Context.getFunctionType( 834 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); 835 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); 836 837 Builtin = 838 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); 839 840 BuiltinCall->setType(CE->getType()); 841 BuiltinCall->setValueKind(CE->getValueKind()); 842 BuiltinCall->setObjectKind(CE->getObjectKind()); 843 BuiltinCall->setCallee(Builtin); 844 BuiltinCall->setArg(1, ChainResult.get()); 845 846 return false; 847 } 848 849 namespace { 850 851 class ScanfDiagnosticFormatHandler 852 : public analyze_format_string::FormatStringHandler { 853 // Accepts the argument index (relative to the first destination index) of the 854 // argument whose size we want. 855 using ComputeSizeFunction = 856 llvm::function_ref<std::optional<llvm::APSInt>(unsigned)>; 857 858 // Accepts the argument index (relative to the first destination index), the 859 // destination size, and the source size). 860 using DiagnoseFunction = 861 llvm::function_ref<void(unsigned, unsigned, unsigned)>; 862 863 ComputeSizeFunction ComputeSizeArgument; 864 DiagnoseFunction Diagnose; 865 866 public: 867 ScanfDiagnosticFormatHandler(ComputeSizeFunction ComputeSizeArgument, 868 DiagnoseFunction Diagnose) 869 : ComputeSizeArgument(ComputeSizeArgument), Diagnose(Diagnose) {} 870 871 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 872 const char *StartSpecifier, 873 unsigned specifierLen) override { 874 if (!FS.consumesDataArgument()) 875 return true; 876 877 unsigned NulByte = 0; 878 switch ((FS.getConversionSpecifier().getKind())) { 879 default: 880 return true; 881 case analyze_format_string::ConversionSpecifier::sArg: 882 case analyze_format_string::ConversionSpecifier::ScanListArg: 883 NulByte = 1; 884 break; 885 case analyze_format_string::ConversionSpecifier::cArg: 886 break; 887 } 888 889 analyze_format_string::OptionalAmount FW = FS.getFieldWidth(); 890 if (FW.getHowSpecified() != 891 analyze_format_string::OptionalAmount::HowSpecified::Constant) 892 return true; 893 894 unsigned SourceSize = FW.getConstantAmount() + NulByte; 895 896 std::optional<llvm::APSInt> DestSizeAPS = 897 ComputeSizeArgument(FS.getArgIndex()); 898 if (!DestSizeAPS) 899 return true; 900 901 unsigned DestSize = DestSizeAPS->getZExtValue(); 902 903 if (DestSize < SourceSize) 904 Diagnose(FS.getArgIndex(), DestSize, SourceSize); 905 906 return true; 907 } 908 }; 909 910 class EstimateSizeFormatHandler 911 : public analyze_format_string::FormatStringHandler { 912 size_t Size; 913 /// Whether the format string contains Linux kernel's format specifier 914 /// extension. 915 bool IsKernelCompatible = true; 916 917 public: 918 EstimateSizeFormatHandler(StringRef Format) 919 : Size(std::min(Format.find(0), Format.size()) + 920 1 /* null byte always written by sprintf */) {} 921 922 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 923 const char *, unsigned SpecifierLen, 924 const TargetInfo &) override { 925 926 const size_t FieldWidth = computeFieldWidth(FS); 927 const size_t Precision = computePrecision(FS); 928 929 // The actual format. 930 switch (FS.getConversionSpecifier().getKind()) { 931 // Just a char. 932 case analyze_format_string::ConversionSpecifier::cArg: 933 case analyze_format_string::ConversionSpecifier::CArg: 934 Size += std::max(FieldWidth, (size_t)1); 935 break; 936 // Just an integer. 937 case analyze_format_string::ConversionSpecifier::dArg: 938 case analyze_format_string::ConversionSpecifier::DArg: 939 case analyze_format_string::ConversionSpecifier::iArg: 940 case analyze_format_string::ConversionSpecifier::oArg: 941 case analyze_format_string::ConversionSpecifier::OArg: 942 case analyze_format_string::ConversionSpecifier::uArg: 943 case analyze_format_string::ConversionSpecifier::UArg: 944 case analyze_format_string::ConversionSpecifier::xArg: 945 case analyze_format_string::ConversionSpecifier::XArg: 946 Size += std::max(FieldWidth, Precision); 947 break; 948 949 // %g style conversion switches between %f or %e style dynamically. 950 // %g removes trailing zeros, and does not print decimal point if there are 951 // no digits that follow it. Thus %g can print a single digit. 952 // FIXME: If it is alternative form: 953 // For g and G conversions, trailing zeros are not removed from the result. 954 case analyze_format_string::ConversionSpecifier::gArg: 955 case analyze_format_string::ConversionSpecifier::GArg: 956 Size += 1; 957 break; 958 959 // Floating point number in the form '[+]ddd.ddd'. 960 case analyze_format_string::ConversionSpecifier::fArg: 961 case analyze_format_string::ConversionSpecifier::FArg: 962 Size += std::max(FieldWidth, 1 /* integer part */ + 963 (Precision ? 1 + Precision 964 : 0) /* period + decimal */); 965 break; 966 967 // Floating point number in the form '[-]d.ddde[+-]dd'. 968 case analyze_format_string::ConversionSpecifier::eArg: 969 case analyze_format_string::ConversionSpecifier::EArg: 970 Size += 971 std::max(FieldWidth, 972 1 /* integer part */ + 973 (Precision ? 1 + Precision : 0) /* period + decimal */ + 974 1 /* e or E letter */ + 2 /* exponent */); 975 break; 976 977 // Floating point number in the form '[-]0xh.hhhhp±dd'. 978 case analyze_format_string::ConversionSpecifier::aArg: 979 case analyze_format_string::ConversionSpecifier::AArg: 980 Size += 981 std::max(FieldWidth, 982 2 /* 0x */ + 1 /* integer part */ + 983 (Precision ? 1 + Precision : 0) /* period + decimal */ + 984 1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */); 985 break; 986 987 // Just a string. 988 case analyze_format_string::ConversionSpecifier::sArg: 989 case analyze_format_string::ConversionSpecifier::SArg: 990 Size += FieldWidth; 991 break; 992 993 // Just a pointer in the form '0xddd'. 994 case analyze_format_string::ConversionSpecifier::pArg: 995 // Linux kernel has its own extesion for `%p` specifier. 996 // Kernel Document: 997 // https://docs.kernel.org/core-api/printk-formats.html#pointer-types 998 IsKernelCompatible = false; 999 Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision); 1000 break; 1001 1002 // A plain percent. 1003 case analyze_format_string::ConversionSpecifier::PercentArg: 1004 Size += 1; 1005 break; 1006 1007 default: 1008 break; 1009 } 1010 1011 Size += FS.hasPlusPrefix() || FS.hasSpacePrefix(); 1012 1013 if (FS.hasAlternativeForm()) { 1014 switch (FS.getConversionSpecifier().getKind()) { 1015 // For o conversion, it increases the precision, if and only if necessary, 1016 // to force the first digit of the result to be a zero 1017 // (if the value and precision are both 0, a single 0 is printed) 1018 case analyze_format_string::ConversionSpecifier::oArg: 1019 // For b conversion, a nonzero result has 0b prefixed to it. 1020 case analyze_format_string::ConversionSpecifier::bArg: 1021 // For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to 1022 // it. 1023 case analyze_format_string::ConversionSpecifier::xArg: 1024 case analyze_format_string::ConversionSpecifier::XArg: 1025 // Note: even when the prefix is added, if 1026 // (prefix_width <= FieldWidth - formatted_length) holds, 1027 // the prefix does not increase the format 1028 // size. e.g.(("%#3x", 0xf) is "0xf") 1029 1030 // If the result is zero, o, b, x, X adds nothing. 1031 break; 1032 // For a, A, e, E, f, F, g, and G conversions, 1033 // the result of converting a floating-point number always contains a 1034 // decimal-point 1035 case analyze_format_string::ConversionSpecifier::aArg: 1036 case analyze_format_string::ConversionSpecifier::AArg: 1037 case analyze_format_string::ConversionSpecifier::eArg: 1038 case analyze_format_string::ConversionSpecifier::EArg: 1039 case analyze_format_string::ConversionSpecifier::fArg: 1040 case analyze_format_string::ConversionSpecifier::FArg: 1041 case analyze_format_string::ConversionSpecifier::gArg: 1042 case analyze_format_string::ConversionSpecifier::GArg: 1043 Size += (Precision ? 0 : 1); 1044 break; 1045 // For other conversions, the behavior is undefined. 1046 default: 1047 break; 1048 } 1049 } 1050 assert(SpecifierLen <= Size && "no underflow"); 1051 Size -= SpecifierLen; 1052 return true; 1053 } 1054 1055 size_t getSizeLowerBound() const { return Size; } 1056 bool isKernelCompatible() const { return IsKernelCompatible; } 1057 1058 private: 1059 static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) { 1060 const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth(); 1061 size_t FieldWidth = 0; 1062 if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant) 1063 FieldWidth = FW.getConstantAmount(); 1064 return FieldWidth; 1065 } 1066 1067 static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) { 1068 const analyze_format_string::OptionalAmount &FW = FS.getPrecision(); 1069 size_t Precision = 0; 1070 1071 // See man 3 printf for default precision value based on the specifier. 1072 switch (FW.getHowSpecified()) { 1073 case analyze_format_string::OptionalAmount::NotSpecified: 1074 switch (FS.getConversionSpecifier().getKind()) { 1075 default: 1076 break; 1077 case analyze_format_string::ConversionSpecifier::dArg: // %d 1078 case analyze_format_string::ConversionSpecifier::DArg: // %D 1079 case analyze_format_string::ConversionSpecifier::iArg: // %i 1080 Precision = 1; 1081 break; 1082 case analyze_format_string::ConversionSpecifier::oArg: // %d 1083 case analyze_format_string::ConversionSpecifier::OArg: // %D 1084 case analyze_format_string::ConversionSpecifier::uArg: // %d 1085 case analyze_format_string::ConversionSpecifier::UArg: // %D 1086 case analyze_format_string::ConversionSpecifier::xArg: // %d 1087 case analyze_format_string::ConversionSpecifier::XArg: // %D 1088 Precision = 1; 1089 break; 1090 case analyze_format_string::ConversionSpecifier::fArg: // %f 1091 case analyze_format_string::ConversionSpecifier::FArg: // %F 1092 case analyze_format_string::ConversionSpecifier::eArg: // %e 1093 case analyze_format_string::ConversionSpecifier::EArg: // %E 1094 case analyze_format_string::ConversionSpecifier::gArg: // %g 1095 case analyze_format_string::ConversionSpecifier::GArg: // %G 1096 Precision = 6; 1097 break; 1098 case analyze_format_string::ConversionSpecifier::pArg: // %d 1099 Precision = 1; 1100 break; 1101 } 1102 break; 1103 case analyze_format_string::OptionalAmount::Constant: 1104 Precision = FW.getConstantAmount(); 1105 break; 1106 default: 1107 break; 1108 } 1109 return Precision; 1110 } 1111 }; 1112 1113 } // namespace 1114 1115 static bool ProcessFormatStringLiteral(const Expr *FormatExpr, 1116 StringRef &FormatStrRef, size_t &StrLen, 1117 ASTContext &Context) { 1118 if (const auto *Format = dyn_cast<StringLiteral>(FormatExpr); 1119 Format && (Format->isOrdinary() || Format->isUTF8())) { 1120 FormatStrRef = Format->getString(); 1121 const ConstantArrayType *T = 1122 Context.getAsConstantArrayType(Format->getType()); 1123 assert(T && "String literal not of constant array type!"); 1124 size_t TypeSize = T->getZExtSize(); 1125 // In case there's a null byte somewhere. 1126 StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0)); 1127 return true; 1128 } 1129 return false; 1130 } 1131 1132 void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, 1133 CallExpr *TheCall) { 1134 if (TheCall->isValueDependent() || TheCall->isTypeDependent() || 1135 isConstantEvaluatedContext()) 1136 return; 1137 1138 bool UseDABAttr = false; 1139 const FunctionDecl *UseDecl = FD; 1140 1141 const auto *DABAttr = FD->getAttr<DiagnoseAsBuiltinAttr>(); 1142 if (DABAttr) { 1143 UseDecl = DABAttr->getFunction(); 1144 assert(UseDecl && "Missing FunctionDecl in DiagnoseAsBuiltin attribute!"); 1145 UseDABAttr = true; 1146 } 1147 1148 unsigned BuiltinID = UseDecl->getBuiltinID(/*ConsiderWrappers=*/true); 1149 1150 if (!BuiltinID) 1151 return; 1152 1153 const TargetInfo &TI = getASTContext().getTargetInfo(); 1154 unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType()); 1155 1156 auto TranslateIndex = [&](unsigned Index) -> std::optional<unsigned> { 1157 // If we refer to a diagnose_as_builtin attribute, we need to change the 1158 // argument index to refer to the arguments of the called function. Unless 1159 // the index is out of bounds, which presumably means it's a variadic 1160 // function. 1161 if (!UseDABAttr) 1162 return Index; 1163 unsigned DABIndices = DABAttr->argIndices_size(); 1164 unsigned NewIndex = Index < DABIndices 1165 ? DABAttr->argIndices_begin()[Index] 1166 : Index - DABIndices + FD->getNumParams(); 1167 if (NewIndex >= TheCall->getNumArgs()) 1168 return std::nullopt; 1169 return NewIndex; 1170 }; 1171 1172 auto ComputeExplicitObjectSizeArgument = 1173 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1174 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1175 if (!IndexOptional) 1176 return std::nullopt; 1177 unsigned NewIndex = *IndexOptional; 1178 Expr::EvalResult Result; 1179 Expr *SizeArg = TheCall->getArg(NewIndex); 1180 if (!SizeArg->EvaluateAsInt(Result, getASTContext())) 1181 return std::nullopt; 1182 llvm::APSInt Integer = Result.Val.getInt(); 1183 Integer.setIsUnsigned(true); 1184 return Integer; 1185 }; 1186 1187 auto ComputeSizeArgument = 1188 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1189 // If the parameter has a pass_object_size attribute, then we should use its 1190 // (potentially) more strict checking mode. Otherwise, conservatively assume 1191 // type 0. 1192 int BOSType = 0; 1193 // This check can fail for variadic functions. 1194 if (Index < FD->getNumParams()) { 1195 if (const auto *POS = 1196 FD->getParamDecl(Index)->getAttr<PassObjectSizeAttr>()) 1197 BOSType = POS->getType(); 1198 } 1199 1200 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1201 if (!IndexOptional) 1202 return std::nullopt; 1203 unsigned NewIndex = *IndexOptional; 1204 1205 if (NewIndex >= TheCall->getNumArgs()) 1206 return std::nullopt; 1207 1208 const Expr *ObjArg = TheCall->getArg(NewIndex); 1209 uint64_t Result; 1210 if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType)) 1211 return std::nullopt; 1212 1213 // Get the object size in the target's size_t width. 1214 return llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth); 1215 }; 1216 1217 auto ComputeStrLenArgument = 1218 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1219 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1220 if (!IndexOptional) 1221 return std::nullopt; 1222 unsigned NewIndex = *IndexOptional; 1223 1224 const Expr *ObjArg = TheCall->getArg(NewIndex); 1225 uint64_t Result; 1226 if (!ObjArg->tryEvaluateStrLen(Result, getASTContext())) 1227 return std::nullopt; 1228 // Add 1 for null byte. 1229 return llvm::APSInt::getUnsigned(Result + 1).extOrTrunc(SizeTypeWidth); 1230 }; 1231 1232 std::optional<llvm::APSInt> SourceSize; 1233 std::optional<llvm::APSInt> DestinationSize; 1234 unsigned DiagID = 0; 1235 bool IsChkVariant = false; 1236 1237 auto GetFunctionName = [&]() { 1238 StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID); 1239 // Skim off the details of whichever builtin was called to produce a better 1240 // diagnostic, as it's unlikely that the user wrote the __builtin 1241 // explicitly. 1242 if (IsChkVariant) { 1243 FunctionName = FunctionName.drop_front(std::strlen("__builtin___")); 1244 FunctionName = FunctionName.drop_back(std::strlen("_chk")); 1245 } else { 1246 FunctionName.consume_front("__builtin_"); 1247 } 1248 return FunctionName; 1249 }; 1250 1251 switch (BuiltinID) { 1252 default: 1253 return; 1254 case Builtin::BI__builtin_strcpy: 1255 case Builtin::BIstrcpy: { 1256 DiagID = diag::warn_fortify_strlen_overflow; 1257 SourceSize = ComputeStrLenArgument(1); 1258 DestinationSize = ComputeSizeArgument(0); 1259 break; 1260 } 1261 1262 case Builtin::BI__builtin___strcpy_chk: { 1263 DiagID = diag::warn_fortify_strlen_overflow; 1264 SourceSize = ComputeStrLenArgument(1); 1265 DestinationSize = ComputeExplicitObjectSizeArgument(2); 1266 IsChkVariant = true; 1267 break; 1268 } 1269 1270 case Builtin::BIscanf: 1271 case Builtin::BIfscanf: 1272 case Builtin::BIsscanf: { 1273 unsigned FormatIndex = 1; 1274 unsigned DataIndex = 2; 1275 if (BuiltinID == Builtin::BIscanf) { 1276 FormatIndex = 0; 1277 DataIndex = 1; 1278 } 1279 1280 const auto *FormatExpr = 1281 TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); 1282 1283 StringRef FormatStrRef; 1284 size_t StrLen; 1285 if (!ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) 1286 return; 1287 1288 auto Diagnose = [&](unsigned ArgIndex, unsigned DestSize, 1289 unsigned SourceSize) { 1290 DiagID = diag::warn_fortify_scanf_overflow; 1291 unsigned Index = ArgIndex + DataIndex; 1292 StringRef FunctionName = GetFunctionName(); 1293 DiagRuntimeBehavior(TheCall->getArg(Index)->getBeginLoc(), TheCall, 1294 PDiag(DiagID) << FunctionName << (Index + 1) 1295 << DestSize << SourceSize); 1296 }; 1297 1298 auto ShiftedComputeSizeArgument = [&](unsigned Index) { 1299 return ComputeSizeArgument(Index + DataIndex); 1300 }; 1301 ScanfDiagnosticFormatHandler H(ShiftedComputeSizeArgument, Diagnose); 1302 const char *FormatBytes = FormatStrRef.data(); 1303 analyze_format_string::ParseScanfString(H, FormatBytes, 1304 FormatBytes + StrLen, getLangOpts(), 1305 Context.getTargetInfo()); 1306 1307 // Unlike the other cases, in this one we have already issued the diagnostic 1308 // here, so no need to continue (because unlike the other cases, here the 1309 // diagnostic refers to the argument number). 1310 return; 1311 } 1312 1313 case Builtin::BIsprintf: 1314 case Builtin::BI__builtin___sprintf_chk: { 1315 size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3; 1316 auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); 1317 1318 StringRef FormatStrRef; 1319 size_t StrLen; 1320 if (ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { 1321 EstimateSizeFormatHandler H(FormatStrRef); 1322 const char *FormatBytes = FormatStrRef.data(); 1323 if (!analyze_format_string::ParsePrintfString( 1324 H, FormatBytes, FormatBytes + StrLen, getLangOpts(), 1325 Context.getTargetInfo(), false)) { 1326 DiagID = H.isKernelCompatible() 1327 ? diag::warn_format_overflow 1328 : diag::warn_format_overflow_non_kprintf; 1329 SourceSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) 1330 .extOrTrunc(SizeTypeWidth); 1331 if (BuiltinID == Builtin::BI__builtin___sprintf_chk) { 1332 DestinationSize = ComputeExplicitObjectSizeArgument(2); 1333 IsChkVariant = true; 1334 } else { 1335 DestinationSize = ComputeSizeArgument(0); 1336 } 1337 break; 1338 } 1339 } 1340 return; 1341 } 1342 case Builtin::BI__builtin___memcpy_chk: 1343 case Builtin::BI__builtin___memmove_chk: 1344 case Builtin::BI__builtin___memset_chk: 1345 case Builtin::BI__builtin___strlcat_chk: 1346 case Builtin::BI__builtin___strlcpy_chk: 1347 case Builtin::BI__builtin___strncat_chk: 1348 case Builtin::BI__builtin___strncpy_chk: 1349 case Builtin::BI__builtin___stpncpy_chk: 1350 case Builtin::BI__builtin___memccpy_chk: 1351 case Builtin::BI__builtin___mempcpy_chk: { 1352 DiagID = diag::warn_builtin_chk_overflow; 1353 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2); 1354 DestinationSize = 1355 ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1356 IsChkVariant = true; 1357 break; 1358 } 1359 1360 case Builtin::BI__builtin___snprintf_chk: 1361 case Builtin::BI__builtin___vsnprintf_chk: { 1362 DiagID = diag::warn_builtin_chk_overflow; 1363 SourceSize = ComputeExplicitObjectSizeArgument(1); 1364 DestinationSize = ComputeExplicitObjectSizeArgument(3); 1365 IsChkVariant = true; 1366 break; 1367 } 1368 1369 case Builtin::BIstrncat: 1370 case Builtin::BI__builtin_strncat: 1371 case Builtin::BIstrncpy: 1372 case Builtin::BI__builtin_strncpy: 1373 case Builtin::BIstpncpy: 1374 case Builtin::BI__builtin_stpncpy: { 1375 // Whether these functions overflow depends on the runtime strlen of the 1376 // string, not just the buffer size, so emitting the "always overflow" 1377 // diagnostic isn't quite right. We should still diagnose passing a buffer 1378 // size larger than the destination buffer though; this is a runtime abort 1379 // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise. 1380 DiagID = diag::warn_fortify_source_size_mismatch; 1381 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1382 DestinationSize = ComputeSizeArgument(0); 1383 break; 1384 } 1385 1386 case Builtin::BImemcpy: 1387 case Builtin::BI__builtin_memcpy: 1388 case Builtin::BImemmove: 1389 case Builtin::BI__builtin_memmove: 1390 case Builtin::BImemset: 1391 case Builtin::BI__builtin_memset: 1392 case Builtin::BImempcpy: 1393 case Builtin::BI__builtin_mempcpy: { 1394 DiagID = diag::warn_fortify_source_overflow; 1395 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1396 DestinationSize = ComputeSizeArgument(0); 1397 break; 1398 } 1399 case Builtin::BIsnprintf: 1400 case Builtin::BI__builtin_snprintf: 1401 case Builtin::BIvsnprintf: 1402 case Builtin::BI__builtin_vsnprintf: { 1403 DiagID = diag::warn_fortify_source_size_mismatch; 1404 SourceSize = ComputeExplicitObjectSizeArgument(1); 1405 const auto *FormatExpr = TheCall->getArg(2)->IgnoreParenImpCasts(); 1406 StringRef FormatStrRef; 1407 size_t StrLen; 1408 if (SourceSize && 1409 ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { 1410 EstimateSizeFormatHandler H(FormatStrRef); 1411 const char *FormatBytes = FormatStrRef.data(); 1412 if (!analyze_format_string::ParsePrintfString( 1413 H, FormatBytes, FormatBytes + StrLen, getLangOpts(), 1414 Context.getTargetInfo(), /*isFreeBSDKPrintf=*/false)) { 1415 llvm::APSInt FormatSize = 1416 llvm::APSInt::getUnsigned(H.getSizeLowerBound()) 1417 .extOrTrunc(SizeTypeWidth); 1418 if (FormatSize > *SourceSize && *SourceSize != 0) { 1419 unsigned TruncationDiagID = 1420 H.isKernelCompatible() ? diag::warn_format_truncation 1421 : diag::warn_format_truncation_non_kprintf; 1422 SmallString<16> SpecifiedSizeStr; 1423 SmallString<16> FormatSizeStr; 1424 SourceSize->toString(SpecifiedSizeStr, /*Radix=*/10); 1425 FormatSize.toString(FormatSizeStr, /*Radix=*/10); 1426 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 1427 PDiag(TruncationDiagID) 1428 << GetFunctionName() << SpecifiedSizeStr 1429 << FormatSizeStr); 1430 } 1431 } 1432 } 1433 DestinationSize = ComputeSizeArgument(0); 1434 } 1435 } 1436 1437 if (!SourceSize || !DestinationSize || 1438 llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0) 1439 return; 1440 1441 StringRef FunctionName = GetFunctionName(); 1442 1443 SmallString<16> DestinationStr; 1444 SmallString<16> SourceStr; 1445 DestinationSize->toString(DestinationStr, /*Radix=*/10); 1446 SourceSize->toString(SourceStr, /*Radix=*/10); 1447 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 1448 PDiag(DiagID) 1449 << FunctionName << DestinationStr << SourceStr); 1450 } 1451 1452 static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, 1453 Scope::ScopeFlags NeededScopeFlags, 1454 unsigned DiagID) { 1455 // Scopes aren't available during instantiation. Fortunately, builtin 1456 // functions cannot be template args so they cannot be formed through template 1457 // instantiation. Therefore checking once during the parse is sufficient. 1458 if (SemaRef.inTemplateInstantiation()) 1459 return false; 1460 1461 Scope *S = SemaRef.getCurScope(); 1462 while (S && !S->isSEHExceptScope()) 1463 S = S->getParent(); 1464 if (!S || !(S->getFlags() & NeededScopeFlags)) { 1465 auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1466 SemaRef.Diag(TheCall->getExprLoc(), DiagID) 1467 << DRE->getDecl()->getIdentifier(); 1468 return true; 1469 } 1470 1471 return false; 1472 } 1473 1474 // In OpenCL, __builtin_alloca_* should return a pointer to address space 1475 // that corresponds to the stack address space i.e private address space. 1476 static void builtinAllocaAddrSpace(Sema &S, CallExpr *TheCall) { 1477 QualType RT = TheCall->getType(); 1478 assert((RT->isPointerType() && !(RT->getPointeeType().hasAddressSpace())) && 1479 "__builtin_alloca has invalid address space"); 1480 1481 RT = RT->getPointeeType(); 1482 RT = S.Context.getAddrSpaceQualType(RT, LangAS::opencl_private); 1483 TheCall->setType(S.Context.getPointerType(RT)); 1484 } 1485 1486 namespace { 1487 enum PointerAuthOpKind { 1488 PAO_Strip, 1489 PAO_Sign, 1490 PAO_Auth, 1491 PAO_SignGeneric, 1492 PAO_Discriminator, 1493 PAO_BlendPointer, 1494 PAO_BlendInteger 1495 }; 1496 } 1497 1498 bool Sema::checkPointerAuthEnabled(SourceLocation Loc, SourceRange Range) { 1499 if (getLangOpts().PointerAuthIntrinsics) 1500 return false; 1501 1502 Diag(Loc, diag::err_ptrauth_disabled) << Range; 1503 return true; 1504 } 1505 1506 static bool checkPointerAuthEnabled(Sema &S, Expr *E) { 1507 return S.checkPointerAuthEnabled(E->getExprLoc(), E->getSourceRange()); 1508 } 1509 1510 static bool checkPointerAuthKey(Sema &S, Expr *&Arg) { 1511 // Convert it to type 'int'. 1512 if (convertArgumentToType(S, Arg, S.Context.IntTy)) 1513 return true; 1514 1515 // Value-dependent expressions are okay; wait for template instantiation. 1516 if (Arg->isValueDependent()) 1517 return false; 1518 1519 unsigned KeyValue; 1520 return S.checkConstantPointerAuthKey(Arg, KeyValue); 1521 } 1522 1523 bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) { 1524 // Attempt to constant-evaluate the expression. 1525 std::optional<llvm::APSInt> KeyValue = Arg->getIntegerConstantExpr(Context); 1526 if (!KeyValue) { 1527 Diag(Arg->getExprLoc(), diag::err_expr_not_ice) 1528 << 0 << Arg->getSourceRange(); 1529 return true; 1530 } 1531 1532 // Ask the target to validate the key parameter. 1533 if (!Context.getTargetInfo().validatePointerAuthKey(*KeyValue)) { 1534 llvm::SmallString<32> Value; 1535 { 1536 llvm::raw_svector_ostream Str(Value); 1537 Str << *KeyValue; 1538 } 1539 1540 Diag(Arg->getExprLoc(), diag::err_ptrauth_invalid_key) 1541 << Value << Arg->getSourceRange(); 1542 return true; 1543 } 1544 1545 Result = KeyValue->getZExtValue(); 1546 return false; 1547 } 1548 1549 static std::pair<const ValueDecl *, CharUnits> 1550 findConstantBaseAndOffset(Sema &S, Expr *E) { 1551 // Must evaluate as a pointer. 1552 Expr::EvalResult Result; 1553 if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue()) 1554 return {nullptr, CharUnits()}; 1555 1556 const auto *BaseDecl = 1557 Result.Val.getLValueBase().dyn_cast<const ValueDecl *>(); 1558 if (!BaseDecl) 1559 return {nullptr, CharUnits()}; 1560 1561 return {BaseDecl, Result.Val.getLValueOffset()}; 1562 } 1563 1564 static bool checkPointerAuthValue(Sema &S, Expr *&Arg, PointerAuthOpKind OpKind, 1565 bool RequireConstant = false) { 1566 if (Arg->hasPlaceholderType()) { 1567 ExprResult R = S.CheckPlaceholderExpr(Arg); 1568 if (R.isInvalid()) 1569 return true; 1570 Arg = R.get(); 1571 } 1572 1573 auto AllowsPointer = [](PointerAuthOpKind OpKind) { 1574 return OpKind != PAO_BlendInteger; 1575 }; 1576 auto AllowsInteger = [](PointerAuthOpKind OpKind) { 1577 return OpKind == PAO_Discriminator || OpKind == PAO_BlendInteger || 1578 OpKind == PAO_SignGeneric; 1579 }; 1580 1581 // Require the value to have the right range of type. 1582 QualType ExpectedTy; 1583 if (AllowsPointer(OpKind) && Arg->getType()->isPointerType()) { 1584 ExpectedTy = Arg->getType().getUnqualifiedType(); 1585 } else if (AllowsPointer(OpKind) && Arg->getType()->isNullPtrType()) { 1586 ExpectedTy = S.Context.VoidPtrTy; 1587 } else if (AllowsInteger(OpKind) && 1588 Arg->getType()->isIntegralOrUnscopedEnumerationType()) { 1589 ExpectedTy = S.Context.getUIntPtrType(); 1590 1591 } else { 1592 // Diagnose the failures. 1593 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_value_bad_type) 1594 << unsigned(OpKind == PAO_Discriminator ? 1 1595 : OpKind == PAO_BlendPointer ? 2 1596 : OpKind == PAO_BlendInteger ? 3 1597 : 0) 1598 << unsigned(AllowsInteger(OpKind) ? (AllowsPointer(OpKind) ? 2 : 1) : 0) 1599 << Arg->getType() << Arg->getSourceRange(); 1600 return true; 1601 } 1602 1603 // Convert to that type. This should just be an lvalue-to-rvalue 1604 // conversion. 1605 if (convertArgumentToType(S, Arg, ExpectedTy)) 1606 return true; 1607 1608 if (!RequireConstant) { 1609 // Warn about null pointers for non-generic sign and auth operations. 1610 if ((OpKind == PAO_Sign || OpKind == PAO_Auth) && 1611 Arg->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) { 1612 S.Diag(Arg->getExprLoc(), OpKind == PAO_Sign 1613 ? diag::warn_ptrauth_sign_null_pointer 1614 : diag::warn_ptrauth_auth_null_pointer) 1615 << Arg->getSourceRange(); 1616 } 1617 1618 return false; 1619 } 1620 1621 // Perform special checking on the arguments to ptrauth_sign_constant. 1622 1623 // The main argument. 1624 if (OpKind == PAO_Sign) { 1625 // Require the value we're signing to have a special form. 1626 auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Arg); 1627 bool Invalid; 1628 1629 // Must be rooted in a declaration reference. 1630 if (!BaseDecl) 1631 Invalid = true; 1632 1633 // If it's a function declaration, we can't have an offset. 1634 else if (isa<FunctionDecl>(BaseDecl)) 1635 Invalid = !Offset.isZero(); 1636 1637 // Otherwise we're fine. 1638 else 1639 Invalid = false; 1640 1641 if (Invalid) 1642 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer); 1643 return Invalid; 1644 } 1645 1646 // The discriminator argument. 1647 assert(OpKind == PAO_Discriminator); 1648 1649 // Must be a pointer or integer or blend thereof. 1650 Expr *Pointer = nullptr; 1651 Expr *Integer = nullptr; 1652 if (auto *Call = dyn_cast<CallExpr>(Arg->IgnoreParens())) { 1653 if (Call->getBuiltinCallee() == 1654 Builtin::BI__builtin_ptrauth_blend_discriminator) { 1655 Pointer = Call->getArg(0); 1656 Integer = Call->getArg(1); 1657 } 1658 } 1659 if (!Pointer && !Integer) { 1660 if (Arg->getType()->isPointerType()) 1661 Pointer = Arg; 1662 else 1663 Integer = Arg; 1664 } 1665 1666 // Check the pointer. 1667 bool Invalid = false; 1668 if (Pointer) { 1669 assert(Pointer->getType()->isPointerType()); 1670 1671 // TODO: if we're initializing a global, check that the address is 1672 // somehow related to what we're initializing. This probably will 1673 // never really be feasible and we'll have to catch it at link-time. 1674 auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Pointer); 1675 if (!BaseDecl || !isa<VarDecl>(BaseDecl)) 1676 Invalid = true; 1677 } 1678 1679 // Check the integer. 1680 if (Integer) { 1681 assert(Integer->getType()->isIntegerType()); 1682 if (!Integer->isEvaluatable(S.Context)) 1683 Invalid = true; 1684 } 1685 1686 if (Invalid) 1687 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator); 1688 return Invalid; 1689 } 1690 1691 static ExprResult PointerAuthStrip(Sema &S, CallExpr *Call) { 1692 if (S.checkArgCount(Call, 2)) 1693 return ExprError(); 1694 if (checkPointerAuthEnabled(S, Call)) 1695 return ExprError(); 1696 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Strip) || 1697 checkPointerAuthKey(S, Call->getArgs()[1])) 1698 return ExprError(); 1699 1700 Call->setType(Call->getArgs()[0]->getType()); 1701 return Call; 1702 } 1703 1704 static ExprResult PointerAuthBlendDiscriminator(Sema &S, CallExpr *Call) { 1705 if (S.checkArgCount(Call, 2)) 1706 return ExprError(); 1707 if (checkPointerAuthEnabled(S, Call)) 1708 return ExprError(); 1709 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_BlendPointer) || 1710 checkPointerAuthValue(S, Call->getArgs()[1], PAO_BlendInteger)) 1711 return ExprError(); 1712 1713 Call->setType(S.Context.getUIntPtrType()); 1714 return Call; 1715 } 1716 1717 static ExprResult PointerAuthSignGenericData(Sema &S, CallExpr *Call) { 1718 if (S.checkArgCount(Call, 2)) 1719 return ExprError(); 1720 if (checkPointerAuthEnabled(S, Call)) 1721 return ExprError(); 1722 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_SignGeneric) || 1723 checkPointerAuthValue(S, Call->getArgs()[1], PAO_Discriminator)) 1724 return ExprError(); 1725 1726 Call->setType(S.Context.getUIntPtrType()); 1727 return Call; 1728 } 1729 1730 static ExprResult PointerAuthSignOrAuth(Sema &S, CallExpr *Call, 1731 PointerAuthOpKind OpKind, 1732 bool RequireConstant) { 1733 if (S.checkArgCount(Call, 3)) 1734 return ExprError(); 1735 if (checkPointerAuthEnabled(S, Call)) 1736 return ExprError(); 1737 if (checkPointerAuthValue(S, Call->getArgs()[0], OpKind, RequireConstant) || 1738 checkPointerAuthKey(S, Call->getArgs()[1]) || 1739 checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator, 1740 RequireConstant)) 1741 return ExprError(); 1742 1743 Call->setType(Call->getArgs()[0]->getType()); 1744 return Call; 1745 } 1746 1747 static ExprResult PointerAuthAuthAndResign(Sema &S, CallExpr *Call) { 1748 if (S.checkArgCount(Call, 5)) 1749 return ExprError(); 1750 if (checkPointerAuthEnabled(S, Call)) 1751 return ExprError(); 1752 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Auth) || 1753 checkPointerAuthKey(S, Call->getArgs()[1]) || 1754 checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator) || 1755 checkPointerAuthKey(S, Call->getArgs()[3]) || 1756 checkPointerAuthValue(S, Call->getArgs()[4], PAO_Discriminator)) 1757 return ExprError(); 1758 1759 Call->setType(Call->getArgs()[0]->getType()); 1760 return Call; 1761 } 1762 1763 static ExprResult PointerAuthStringDiscriminator(Sema &S, CallExpr *Call) { 1764 if (checkPointerAuthEnabled(S, Call)) 1765 return ExprError(); 1766 1767 // We've already performed normal call type-checking. 1768 const Expr *Arg = Call->getArg(0)->IgnoreParenImpCasts(); 1769 1770 // Operand must be an ordinary or UTF-8 string literal. 1771 const auto *Literal = dyn_cast<StringLiteral>(Arg); 1772 if (!Literal || Literal->getCharByteWidth() != 1) { 1773 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_string_not_literal) 1774 << (Literal ? 1 : 0) << Arg->getSourceRange(); 1775 return ExprError(); 1776 } 1777 1778 return Call; 1779 } 1780 1781 static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) { 1782 if (S.checkArgCount(TheCall, 1)) 1783 return ExprError(); 1784 1785 // Compute __builtin_launder's parameter type from the argument. 1786 // The parameter type is: 1787 // * The type of the argument if it's not an array or function type, 1788 // Otherwise, 1789 // * The decayed argument type. 1790 QualType ParamTy = [&]() { 1791 QualType ArgTy = TheCall->getArg(0)->getType(); 1792 if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe()) 1793 return S.Context.getPointerType(Ty->getElementType()); 1794 if (ArgTy->isFunctionType()) { 1795 return S.Context.getPointerType(ArgTy); 1796 } 1797 return ArgTy; 1798 }(); 1799 1800 TheCall->setType(ParamTy); 1801 1802 auto DiagSelect = [&]() -> std::optional<unsigned> { 1803 if (!ParamTy->isPointerType()) 1804 return 0; 1805 if (ParamTy->isFunctionPointerType()) 1806 return 1; 1807 if (ParamTy->isVoidPointerType()) 1808 return 2; 1809 return std::optional<unsigned>{}; 1810 }(); 1811 if (DiagSelect) { 1812 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg) 1813 << *DiagSelect << TheCall->getSourceRange(); 1814 return ExprError(); 1815 } 1816 1817 // We either have an incomplete class type, or we have a class template 1818 // whose instantiation has not been forced. Example: 1819 // 1820 // template <class T> struct Foo { T value; }; 1821 // Foo<int> *p = nullptr; 1822 // auto *d = __builtin_launder(p); 1823 if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(), 1824 diag::err_incomplete_type)) 1825 return ExprError(); 1826 1827 assert(ParamTy->getPointeeType()->isObjectType() && 1828 "Unhandled non-object pointer case"); 1829 1830 InitializedEntity Entity = 1831 InitializedEntity::InitializeParameter(S.Context, ParamTy, false); 1832 ExprResult Arg = 1833 S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0)); 1834 if (Arg.isInvalid()) 1835 return ExprError(); 1836 TheCall->setArg(0, Arg.get()); 1837 1838 return TheCall; 1839 } 1840 1841 static ExprResult BuiltinIsWithinLifetime(Sema &S, CallExpr *TheCall) { 1842 if (S.checkArgCount(TheCall, 1)) 1843 return ExprError(); 1844 1845 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 1846 if (Arg.isInvalid()) 1847 return ExprError(); 1848 QualType ParamTy = Arg.get()->getType(); 1849 TheCall->setArg(0, Arg.get()); 1850 TheCall->setType(S.Context.BoolTy); 1851 1852 // Only accept pointers to objects as arguments, which should have object 1853 // pointer or void pointer types. 1854 if (const auto *PT = ParamTy->getAs<PointerType>()) { 1855 // LWG4138: Function pointer types not allowed 1856 if (PT->getPointeeType()->isFunctionType()) { 1857 S.Diag(TheCall->getArg(0)->getExprLoc(), 1858 diag::err_builtin_is_within_lifetime_invalid_arg) 1859 << 1; 1860 return ExprError(); 1861 } 1862 // Disallow VLAs too since those shouldn't be able to 1863 // be a template parameter for `std::is_within_lifetime` 1864 if (PT->getPointeeType()->isVariableArrayType()) { 1865 S.Diag(TheCall->getArg(0)->getExprLoc(), diag::err_vla_unsupported) 1866 << 1 << "__builtin_is_within_lifetime"; 1867 return ExprError(); 1868 } 1869 } else { 1870 S.Diag(TheCall->getArg(0)->getExprLoc(), 1871 diag::err_builtin_is_within_lifetime_invalid_arg) 1872 << 0; 1873 return ExprError(); 1874 } 1875 1876 return TheCall; 1877 } 1878 1879 // Emit an error and return true if the current object format type is in the 1880 // list of unsupported types. 1881 static bool CheckBuiltinTargetNotInUnsupported( 1882 Sema &S, unsigned BuiltinID, CallExpr *TheCall, 1883 ArrayRef<llvm::Triple::ObjectFormatType> UnsupportedObjectFormatTypes) { 1884 llvm::Triple::ObjectFormatType CurObjFormat = 1885 S.getASTContext().getTargetInfo().getTriple().getObjectFormat(); 1886 if (llvm::is_contained(UnsupportedObjectFormatTypes, CurObjFormat)) { 1887 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1888 << TheCall->getSourceRange(); 1889 return true; 1890 } 1891 return false; 1892 } 1893 1894 // Emit an error and return true if the current architecture is not in the list 1895 // of supported architectures. 1896 static bool 1897 CheckBuiltinTargetInSupported(Sema &S, CallExpr *TheCall, 1898 ArrayRef<llvm::Triple::ArchType> SupportedArchs) { 1899 llvm::Triple::ArchType CurArch = 1900 S.getASTContext().getTargetInfo().getTriple().getArch(); 1901 if (llvm::is_contained(SupportedArchs, CurArch)) 1902 return false; 1903 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1904 << TheCall->getSourceRange(); 1905 return true; 1906 } 1907 1908 static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr, 1909 SourceLocation CallSiteLoc); 1910 1911 bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, 1912 CallExpr *TheCall) { 1913 switch (TI.getTriple().getArch()) { 1914 default: 1915 // Some builtins don't require additional checking, so just consider these 1916 // acceptable. 1917 return false; 1918 case llvm::Triple::arm: 1919 case llvm::Triple::armeb: 1920 case llvm::Triple::thumb: 1921 case llvm::Triple::thumbeb: 1922 return ARM().CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall); 1923 case llvm::Triple::aarch64: 1924 case llvm::Triple::aarch64_32: 1925 case llvm::Triple::aarch64_be: 1926 return ARM().CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall); 1927 case llvm::Triple::bpfeb: 1928 case llvm::Triple::bpfel: 1929 return BPF().CheckBPFBuiltinFunctionCall(BuiltinID, TheCall); 1930 case llvm::Triple::hexagon: 1931 return Hexagon().CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall); 1932 case llvm::Triple::mips: 1933 case llvm::Triple::mipsel: 1934 case llvm::Triple::mips64: 1935 case llvm::Triple::mips64el: 1936 return MIPS().CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall); 1937 case llvm::Triple::systemz: 1938 return SystemZ().CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall); 1939 case llvm::Triple::x86: 1940 case llvm::Triple::x86_64: 1941 return X86().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall); 1942 case llvm::Triple::ppc: 1943 case llvm::Triple::ppcle: 1944 case llvm::Triple::ppc64: 1945 case llvm::Triple::ppc64le: 1946 return PPC().CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall); 1947 case llvm::Triple::amdgcn: 1948 return AMDGPU().CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall); 1949 case llvm::Triple::riscv32: 1950 case llvm::Triple::riscv64: 1951 return RISCV().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall); 1952 case llvm::Triple::loongarch32: 1953 case llvm::Triple::loongarch64: 1954 return LoongArch().CheckLoongArchBuiltinFunctionCall(TI, BuiltinID, 1955 TheCall); 1956 case llvm::Triple::wasm32: 1957 case llvm::Triple::wasm64: 1958 return Wasm().CheckWebAssemblyBuiltinFunctionCall(TI, BuiltinID, TheCall); 1959 case llvm::Triple::nvptx: 1960 case llvm::Triple::nvptx64: 1961 return NVPTX().CheckNVPTXBuiltinFunctionCall(TI, BuiltinID, TheCall); 1962 } 1963 } 1964 1965 // Check if \p Ty is a valid type for the elementwise math builtins. If it is 1966 // not a valid type, emit an error message and return true. Otherwise return 1967 // false. 1968 static bool checkMathBuiltinElementType(Sema &S, SourceLocation Loc, 1969 QualType ArgTy, int ArgIndex) { 1970 if (!ArgTy->getAs<VectorType>() && 1971 !ConstantMatrixType::isValidElementType(ArgTy)) { 1972 return S.Diag(Loc, diag::err_builtin_invalid_arg_type) 1973 << ArgIndex << /* vector, integer or float ty*/ 0 << ArgTy; 1974 } 1975 1976 return false; 1977 } 1978 1979 static bool checkFPMathBuiltinElementType(Sema &S, SourceLocation Loc, 1980 QualType ArgTy, int ArgIndex) { 1981 QualType EltTy = ArgTy; 1982 if (auto *VecTy = EltTy->getAs<VectorType>()) 1983 EltTy = VecTy->getElementType(); 1984 1985 if (!EltTy->isRealFloatingType()) { 1986 return S.Diag(Loc, diag::err_builtin_invalid_arg_type) 1987 << ArgIndex << /* vector or float ty*/ 5 << ArgTy; 1988 } 1989 1990 return false; 1991 } 1992 1993 /// BuiltinCpu{Supports|Is} - Handle __builtin_cpu_{supports|is}(char *). 1994 /// This checks that the target supports the builtin and that the string 1995 /// argument is constant and valid. 1996 static bool BuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall, 1997 const TargetInfo *AuxTI, unsigned BuiltinID) { 1998 assert((BuiltinID == Builtin::BI__builtin_cpu_supports || 1999 BuiltinID == Builtin::BI__builtin_cpu_is) && 2000 "Expecting __builtin_cpu_..."); 2001 2002 bool IsCPUSupports = BuiltinID == Builtin::BI__builtin_cpu_supports; 2003 const TargetInfo *TheTI = &TI; 2004 auto SupportsBI = [=](const TargetInfo *TInfo) { 2005 return TInfo && ((IsCPUSupports && TInfo->supportsCpuSupports()) || 2006 (!IsCPUSupports && TInfo->supportsCpuIs())); 2007 }; 2008 if (!SupportsBI(&TI) && SupportsBI(AuxTI)) 2009 TheTI = AuxTI; 2010 2011 if ((!IsCPUSupports && !TheTI->supportsCpuIs()) || 2012 (IsCPUSupports && !TheTI->supportsCpuSupports())) 2013 return S.Diag(TheCall->getBeginLoc(), 2014 TI.getTriple().isOSAIX() 2015 ? diag::err_builtin_aix_os_unsupported 2016 : diag::err_builtin_target_unsupported) 2017 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 2018 2019 Expr *Arg = TheCall->getArg(0)->IgnoreParenImpCasts(); 2020 // Check if the argument is a string literal. 2021 if (!isa<StringLiteral>(Arg)) 2022 return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 2023 << Arg->getSourceRange(); 2024 2025 // Check the contents of the string. 2026 StringRef Feature = cast<StringLiteral>(Arg)->getString(); 2027 if (IsCPUSupports && !TheTI->validateCpuSupports(Feature)) { 2028 S.Diag(TheCall->getBeginLoc(), diag::warn_invalid_cpu_supports) 2029 << Arg->getSourceRange(); 2030 return false; 2031 } 2032 if (!IsCPUSupports && !TheTI->validateCpuIs(Feature)) 2033 return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is) 2034 << Arg->getSourceRange(); 2035 return false; 2036 } 2037 2038 /// Checks that __builtin_popcountg was called with a single argument, which is 2039 /// an unsigned integer. 2040 static bool BuiltinPopcountg(Sema &S, CallExpr *TheCall) { 2041 if (S.checkArgCount(TheCall, 1)) 2042 return true; 2043 2044 ExprResult ArgRes = S.DefaultLvalueConversion(TheCall->getArg(0)); 2045 if (ArgRes.isInvalid()) 2046 return true; 2047 2048 Expr *Arg = ArgRes.get(); 2049 TheCall->setArg(0, Arg); 2050 2051 QualType ArgTy = Arg->getType(); 2052 2053 if (!ArgTy->isUnsignedIntegerType()) { 2054 S.Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2055 << 1 << /*unsigned integer ty*/ 7 << ArgTy; 2056 return true; 2057 } 2058 return false; 2059 } 2060 2061 /// Checks that __builtin_{clzg,ctzg} was called with a first argument, which is 2062 /// an unsigned integer, and an optional second argument, which is promoted to 2063 /// an 'int'. 2064 static bool BuiltinCountZeroBitsGeneric(Sema &S, CallExpr *TheCall) { 2065 if (S.checkArgCountRange(TheCall, 1, 2)) 2066 return true; 2067 2068 ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0)); 2069 if (Arg0Res.isInvalid()) 2070 return true; 2071 2072 Expr *Arg0 = Arg0Res.get(); 2073 TheCall->setArg(0, Arg0); 2074 2075 QualType Arg0Ty = Arg0->getType(); 2076 2077 if (!Arg0Ty->isUnsignedIntegerType()) { 2078 S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2079 << 1 << /*unsigned integer ty*/ 7 << Arg0Ty; 2080 return true; 2081 } 2082 2083 if (TheCall->getNumArgs() > 1) { 2084 ExprResult Arg1Res = S.UsualUnaryConversions(TheCall->getArg(1)); 2085 if (Arg1Res.isInvalid()) 2086 return true; 2087 2088 Expr *Arg1 = Arg1Res.get(); 2089 TheCall->setArg(1, Arg1); 2090 2091 QualType Arg1Ty = Arg1->getType(); 2092 2093 if (!Arg1Ty->isSpecificBuiltinType(BuiltinType::Int)) { 2094 S.Diag(Arg1->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2095 << 2 << /*'int' ty*/ 8 << Arg1Ty; 2096 return true; 2097 } 2098 } 2099 2100 return false; 2101 } 2102 2103 ExprResult 2104 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, 2105 CallExpr *TheCall) { 2106 ExprResult TheCallResult(TheCall); 2107 2108 // Find out if any arguments are required to be integer constant expressions. 2109 unsigned ICEArguments = 0; 2110 ASTContext::GetBuiltinTypeError Error; 2111 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); 2112 if (Error != ASTContext::GE_None) 2113 ICEArguments = 0; // Don't diagnose previously diagnosed errors. 2114 2115 // If any arguments are required to be ICE's, check and diagnose. 2116 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { 2117 // Skip arguments not required to be ICE's. 2118 if ((ICEArguments & (1 << ArgNo)) == 0) continue; 2119 2120 llvm::APSInt Result; 2121 // If we don't have enough arguments, continue so we can issue better 2122 // diagnostic in checkArgCount(...) 2123 if (ArgNo < TheCall->getNumArgs() && 2124 BuiltinConstantArg(TheCall, ArgNo, Result)) 2125 return true; 2126 ICEArguments &= ~(1 << ArgNo); 2127 } 2128 2129 FPOptions FPO; 2130 switch (BuiltinID) { 2131 case Builtin::BI__builtin_cpu_supports: 2132 case Builtin::BI__builtin_cpu_is: 2133 if (BuiltinCpu(*this, Context.getTargetInfo(), TheCall, 2134 Context.getAuxTargetInfo(), BuiltinID)) 2135 return ExprError(); 2136 break; 2137 case Builtin::BI__builtin_cpu_init: 2138 if (!Context.getTargetInfo().supportsCpuInit()) { 2139 Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 2140 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 2141 return ExprError(); 2142 } 2143 break; 2144 case Builtin::BI__builtin___CFStringMakeConstantString: 2145 // CFStringMakeConstantString is currently not implemented for GOFF (i.e., 2146 // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported 2147 if (CheckBuiltinTargetNotInUnsupported( 2148 *this, BuiltinID, TheCall, 2149 {llvm::Triple::GOFF, llvm::Triple::XCOFF})) 2150 return ExprError(); 2151 assert(TheCall->getNumArgs() == 1 && 2152 "Wrong # arguments to builtin CFStringMakeConstantString"); 2153 if (ObjC().CheckObjCString(TheCall->getArg(0))) 2154 return ExprError(); 2155 break; 2156 case Builtin::BI__builtin_ms_va_start: 2157 case Builtin::BI__builtin_stdarg_start: 2158 case Builtin::BI__builtin_va_start: 2159 if (BuiltinVAStart(BuiltinID, TheCall)) 2160 return ExprError(); 2161 break; 2162 case Builtin::BI__va_start: { 2163 switch (Context.getTargetInfo().getTriple().getArch()) { 2164 case llvm::Triple::aarch64: 2165 case llvm::Triple::arm: 2166 case llvm::Triple::thumb: 2167 if (BuiltinVAStartARMMicrosoft(TheCall)) 2168 return ExprError(); 2169 break; 2170 default: 2171 if (BuiltinVAStart(BuiltinID, TheCall)) 2172 return ExprError(); 2173 break; 2174 } 2175 break; 2176 } 2177 2178 // The acquire, release, and no fence variants are ARM and AArch64 only. 2179 case Builtin::BI_interlockedbittestandset_acq: 2180 case Builtin::BI_interlockedbittestandset_rel: 2181 case Builtin::BI_interlockedbittestandset_nf: 2182 case Builtin::BI_interlockedbittestandreset_acq: 2183 case Builtin::BI_interlockedbittestandreset_rel: 2184 case Builtin::BI_interlockedbittestandreset_nf: 2185 if (CheckBuiltinTargetInSupported( 2186 *this, TheCall, 2187 {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64})) 2188 return ExprError(); 2189 break; 2190 2191 // The 64-bit bittest variants are x64, ARM, and AArch64 only. 2192 case Builtin::BI_bittest64: 2193 case Builtin::BI_bittestandcomplement64: 2194 case Builtin::BI_bittestandreset64: 2195 case Builtin::BI_bittestandset64: 2196 case Builtin::BI_interlockedbittestandreset64: 2197 case Builtin::BI_interlockedbittestandset64: 2198 if (CheckBuiltinTargetInSupported( 2199 *this, TheCall, 2200 {llvm::Triple::x86_64, llvm::Triple::arm, llvm::Triple::thumb, 2201 llvm::Triple::aarch64, llvm::Triple::amdgcn})) 2202 return ExprError(); 2203 break; 2204 2205 case Builtin::BI__builtin_set_flt_rounds: 2206 if (CheckBuiltinTargetInSupported( 2207 *this, TheCall, 2208 {llvm::Triple::x86, llvm::Triple::x86_64, llvm::Triple::arm, 2209 llvm::Triple::thumb, llvm::Triple::aarch64, llvm::Triple::amdgcn, 2210 llvm::Triple::ppc, llvm::Triple::ppc64, llvm::Triple::ppcle, 2211 llvm::Triple::ppc64le})) 2212 return ExprError(); 2213 break; 2214 2215 case Builtin::BI__builtin_isgreater: 2216 case Builtin::BI__builtin_isgreaterequal: 2217 case Builtin::BI__builtin_isless: 2218 case Builtin::BI__builtin_islessequal: 2219 case Builtin::BI__builtin_islessgreater: 2220 case Builtin::BI__builtin_isunordered: 2221 if (BuiltinUnorderedCompare(TheCall, BuiltinID)) 2222 return ExprError(); 2223 break; 2224 case Builtin::BI__builtin_fpclassify: 2225 if (BuiltinFPClassification(TheCall, 6, BuiltinID)) 2226 return ExprError(); 2227 break; 2228 case Builtin::BI__builtin_isfpclass: 2229 if (BuiltinFPClassification(TheCall, 2, BuiltinID)) 2230 return ExprError(); 2231 break; 2232 case Builtin::BI__builtin_isfinite: 2233 case Builtin::BI__builtin_isinf: 2234 case Builtin::BI__builtin_isinf_sign: 2235 case Builtin::BI__builtin_isnan: 2236 case Builtin::BI__builtin_issignaling: 2237 case Builtin::BI__builtin_isnormal: 2238 case Builtin::BI__builtin_issubnormal: 2239 case Builtin::BI__builtin_iszero: 2240 case Builtin::BI__builtin_signbit: 2241 case Builtin::BI__builtin_signbitf: 2242 case Builtin::BI__builtin_signbitl: 2243 if (BuiltinFPClassification(TheCall, 1, BuiltinID)) 2244 return ExprError(); 2245 break; 2246 case Builtin::BI__builtin_shufflevector: 2247 return BuiltinShuffleVector(TheCall); 2248 // TheCall will be freed by the smart pointer here, but that's fine, since 2249 // BuiltinShuffleVector guts it, but then doesn't release it. 2250 case Builtin::BI__builtin_prefetch: 2251 if (BuiltinPrefetch(TheCall)) 2252 return ExprError(); 2253 break; 2254 case Builtin::BI__builtin_alloca_with_align: 2255 case Builtin::BI__builtin_alloca_with_align_uninitialized: 2256 if (BuiltinAllocaWithAlign(TheCall)) 2257 return ExprError(); 2258 [[fallthrough]]; 2259 case Builtin::BI__builtin_alloca: 2260 case Builtin::BI__builtin_alloca_uninitialized: 2261 Diag(TheCall->getBeginLoc(), diag::warn_alloca) 2262 << TheCall->getDirectCallee(); 2263 if (getLangOpts().OpenCL) { 2264 builtinAllocaAddrSpace(*this, TheCall); 2265 } 2266 break; 2267 case Builtin::BI__arithmetic_fence: 2268 if (BuiltinArithmeticFence(TheCall)) 2269 return ExprError(); 2270 break; 2271 case Builtin::BI__assume: 2272 case Builtin::BI__builtin_assume: 2273 if (BuiltinAssume(TheCall)) 2274 return ExprError(); 2275 break; 2276 case Builtin::BI__builtin_assume_aligned: 2277 if (BuiltinAssumeAligned(TheCall)) 2278 return ExprError(); 2279 break; 2280 case Builtin::BI__builtin_dynamic_object_size: 2281 case Builtin::BI__builtin_object_size: 2282 if (BuiltinConstantArgRange(TheCall, 1, 0, 3)) 2283 return ExprError(); 2284 break; 2285 case Builtin::BI__builtin_longjmp: 2286 if (BuiltinLongjmp(TheCall)) 2287 return ExprError(); 2288 break; 2289 case Builtin::BI__builtin_setjmp: 2290 if (BuiltinSetjmp(TheCall)) 2291 return ExprError(); 2292 break; 2293 case Builtin::BI__builtin_classify_type: 2294 if (checkArgCount(TheCall, 1)) 2295 return true; 2296 TheCall->setType(Context.IntTy); 2297 break; 2298 case Builtin::BI__builtin_complex: 2299 if (BuiltinComplex(TheCall)) 2300 return ExprError(); 2301 break; 2302 case Builtin::BI__builtin_constant_p: { 2303 if (checkArgCount(TheCall, 1)) 2304 return true; 2305 ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 2306 if (Arg.isInvalid()) return true; 2307 TheCall->setArg(0, Arg.get()); 2308 TheCall->setType(Context.IntTy); 2309 break; 2310 } 2311 case Builtin::BI__builtin_launder: 2312 return BuiltinLaunder(*this, TheCall); 2313 case Builtin::BI__builtin_is_within_lifetime: 2314 return BuiltinIsWithinLifetime(*this, TheCall); 2315 case Builtin::BI__sync_fetch_and_add: 2316 case Builtin::BI__sync_fetch_and_add_1: 2317 case Builtin::BI__sync_fetch_and_add_2: 2318 case Builtin::BI__sync_fetch_and_add_4: 2319 case Builtin::BI__sync_fetch_and_add_8: 2320 case Builtin::BI__sync_fetch_and_add_16: 2321 case Builtin::BI__sync_fetch_and_sub: 2322 case Builtin::BI__sync_fetch_and_sub_1: 2323 case Builtin::BI__sync_fetch_and_sub_2: 2324 case Builtin::BI__sync_fetch_and_sub_4: 2325 case Builtin::BI__sync_fetch_and_sub_8: 2326 case Builtin::BI__sync_fetch_and_sub_16: 2327 case Builtin::BI__sync_fetch_and_or: 2328 case Builtin::BI__sync_fetch_and_or_1: 2329 case Builtin::BI__sync_fetch_and_or_2: 2330 case Builtin::BI__sync_fetch_and_or_4: 2331 case Builtin::BI__sync_fetch_and_or_8: 2332 case Builtin::BI__sync_fetch_and_or_16: 2333 case Builtin::BI__sync_fetch_and_and: 2334 case Builtin::BI__sync_fetch_and_and_1: 2335 case Builtin::BI__sync_fetch_and_and_2: 2336 case Builtin::BI__sync_fetch_and_and_4: 2337 case Builtin::BI__sync_fetch_and_and_8: 2338 case Builtin::BI__sync_fetch_and_and_16: 2339 case Builtin::BI__sync_fetch_and_xor: 2340 case Builtin::BI__sync_fetch_and_xor_1: 2341 case Builtin::BI__sync_fetch_and_xor_2: 2342 case Builtin::BI__sync_fetch_and_xor_4: 2343 case Builtin::BI__sync_fetch_and_xor_8: 2344 case Builtin::BI__sync_fetch_and_xor_16: 2345 case Builtin::BI__sync_fetch_and_nand: 2346 case Builtin::BI__sync_fetch_and_nand_1: 2347 case Builtin::BI__sync_fetch_and_nand_2: 2348 case Builtin::BI__sync_fetch_and_nand_4: 2349 case Builtin::BI__sync_fetch_and_nand_8: 2350 case Builtin::BI__sync_fetch_and_nand_16: 2351 case Builtin::BI__sync_add_and_fetch: 2352 case Builtin::BI__sync_add_and_fetch_1: 2353 case Builtin::BI__sync_add_and_fetch_2: 2354 case Builtin::BI__sync_add_and_fetch_4: 2355 case Builtin::BI__sync_add_and_fetch_8: 2356 case Builtin::BI__sync_add_and_fetch_16: 2357 case Builtin::BI__sync_sub_and_fetch: 2358 case Builtin::BI__sync_sub_and_fetch_1: 2359 case Builtin::BI__sync_sub_and_fetch_2: 2360 case Builtin::BI__sync_sub_and_fetch_4: 2361 case Builtin::BI__sync_sub_and_fetch_8: 2362 case Builtin::BI__sync_sub_and_fetch_16: 2363 case Builtin::BI__sync_and_and_fetch: 2364 case Builtin::BI__sync_and_and_fetch_1: 2365 case Builtin::BI__sync_and_and_fetch_2: 2366 case Builtin::BI__sync_and_and_fetch_4: 2367 case Builtin::BI__sync_and_and_fetch_8: 2368 case Builtin::BI__sync_and_and_fetch_16: 2369 case Builtin::BI__sync_or_and_fetch: 2370 case Builtin::BI__sync_or_and_fetch_1: 2371 case Builtin::BI__sync_or_and_fetch_2: 2372 case Builtin::BI__sync_or_and_fetch_4: 2373 case Builtin::BI__sync_or_and_fetch_8: 2374 case Builtin::BI__sync_or_and_fetch_16: 2375 case Builtin::BI__sync_xor_and_fetch: 2376 case Builtin::BI__sync_xor_and_fetch_1: 2377 case Builtin::BI__sync_xor_and_fetch_2: 2378 case Builtin::BI__sync_xor_and_fetch_4: 2379 case Builtin::BI__sync_xor_and_fetch_8: 2380 case Builtin::BI__sync_xor_and_fetch_16: 2381 case Builtin::BI__sync_nand_and_fetch: 2382 case Builtin::BI__sync_nand_and_fetch_1: 2383 case Builtin::BI__sync_nand_and_fetch_2: 2384 case Builtin::BI__sync_nand_and_fetch_4: 2385 case Builtin::BI__sync_nand_and_fetch_8: 2386 case Builtin::BI__sync_nand_and_fetch_16: 2387 case Builtin::BI__sync_val_compare_and_swap: 2388 case Builtin::BI__sync_val_compare_and_swap_1: 2389 case Builtin::BI__sync_val_compare_and_swap_2: 2390 case Builtin::BI__sync_val_compare_and_swap_4: 2391 case Builtin::BI__sync_val_compare_and_swap_8: 2392 case Builtin::BI__sync_val_compare_and_swap_16: 2393 case Builtin::BI__sync_bool_compare_and_swap: 2394 case Builtin::BI__sync_bool_compare_and_swap_1: 2395 case Builtin::BI__sync_bool_compare_and_swap_2: 2396 case Builtin::BI__sync_bool_compare_and_swap_4: 2397 case Builtin::BI__sync_bool_compare_and_swap_8: 2398 case Builtin::BI__sync_bool_compare_and_swap_16: 2399 case Builtin::BI__sync_lock_test_and_set: 2400 case Builtin::BI__sync_lock_test_and_set_1: 2401 case Builtin::BI__sync_lock_test_and_set_2: 2402 case Builtin::BI__sync_lock_test_and_set_4: 2403 case Builtin::BI__sync_lock_test_and_set_8: 2404 case Builtin::BI__sync_lock_test_and_set_16: 2405 case Builtin::BI__sync_lock_release: 2406 case Builtin::BI__sync_lock_release_1: 2407 case Builtin::BI__sync_lock_release_2: 2408 case Builtin::BI__sync_lock_release_4: 2409 case Builtin::BI__sync_lock_release_8: 2410 case Builtin::BI__sync_lock_release_16: 2411 case Builtin::BI__sync_swap: 2412 case Builtin::BI__sync_swap_1: 2413 case Builtin::BI__sync_swap_2: 2414 case Builtin::BI__sync_swap_4: 2415 case Builtin::BI__sync_swap_8: 2416 case Builtin::BI__sync_swap_16: 2417 return BuiltinAtomicOverloaded(TheCallResult); 2418 case Builtin::BI__sync_synchronize: 2419 Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst) 2420 << TheCall->getCallee()->getSourceRange(); 2421 break; 2422 case Builtin::BI__builtin_nontemporal_load: 2423 case Builtin::BI__builtin_nontemporal_store: 2424 return BuiltinNontemporalOverloaded(TheCallResult); 2425 case Builtin::BI__builtin_memcpy_inline: { 2426 clang::Expr *SizeOp = TheCall->getArg(2); 2427 // We warn about copying to or from `nullptr` pointers when `size` is 2428 // greater than 0. When `size` is value dependent we cannot evaluate its 2429 // value so we bail out. 2430 if (SizeOp->isValueDependent()) 2431 break; 2432 if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) { 2433 CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); 2434 CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc()); 2435 } 2436 break; 2437 } 2438 case Builtin::BI__builtin_memset_inline: { 2439 clang::Expr *SizeOp = TheCall->getArg(2); 2440 // We warn about filling to `nullptr` pointers when `size` is greater than 2441 // 0. When `size` is value dependent we cannot evaluate its value so we bail 2442 // out. 2443 if (SizeOp->isValueDependent()) 2444 break; 2445 if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) 2446 CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); 2447 break; 2448 } 2449 #define BUILTIN(ID, TYPE, ATTRS) 2450 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 2451 case Builtin::BI##ID: \ 2452 return AtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); 2453 #include "clang/Basic/Builtins.inc" 2454 case Builtin::BI__annotation: 2455 if (BuiltinMSVCAnnotation(*this, TheCall)) 2456 return ExprError(); 2457 break; 2458 case Builtin::BI__builtin_annotation: 2459 if (BuiltinAnnotation(*this, TheCall)) 2460 return ExprError(); 2461 break; 2462 case Builtin::BI__builtin_addressof: 2463 if (BuiltinAddressof(*this, TheCall)) 2464 return ExprError(); 2465 break; 2466 case Builtin::BI__builtin_function_start: 2467 if (BuiltinFunctionStart(*this, TheCall)) 2468 return ExprError(); 2469 break; 2470 case Builtin::BI__builtin_is_aligned: 2471 case Builtin::BI__builtin_align_up: 2472 case Builtin::BI__builtin_align_down: 2473 if (BuiltinAlignment(*this, TheCall, BuiltinID)) 2474 return ExprError(); 2475 break; 2476 case Builtin::BI__builtin_add_overflow: 2477 case Builtin::BI__builtin_sub_overflow: 2478 case Builtin::BI__builtin_mul_overflow: 2479 if (BuiltinOverflow(*this, TheCall, BuiltinID)) 2480 return ExprError(); 2481 break; 2482 case Builtin::BI__builtin_operator_new: 2483 case Builtin::BI__builtin_operator_delete: { 2484 bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete; 2485 ExprResult Res = 2486 BuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete); 2487 if (Res.isInvalid()) 2488 CorrectDelayedTyposInExpr(TheCallResult.get()); 2489 return Res; 2490 } 2491 case Builtin::BI__builtin_dump_struct: 2492 return BuiltinDumpStruct(*this, TheCall); 2493 case Builtin::BI__builtin_expect_with_probability: { 2494 // We first want to ensure we are called with 3 arguments 2495 if (checkArgCount(TheCall, 3)) 2496 return ExprError(); 2497 // then check probability is constant float in range [0.0, 1.0] 2498 const Expr *ProbArg = TheCall->getArg(2); 2499 SmallVector<PartialDiagnosticAt, 8> Notes; 2500 Expr::EvalResult Eval; 2501 Eval.Diag = &Notes; 2502 if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) || 2503 !Eval.Val.isFloat()) { 2504 Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float) 2505 << ProbArg->getSourceRange(); 2506 for (const PartialDiagnosticAt &PDiag : Notes) 2507 Diag(PDiag.first, PDiag.second); 2508 return ExprError(); 2509 } 2510 llvm::APFloat Probability = Eval.Val.getFloat(); 2511 bool LoseInfo = false; 2512 Probability.convert(llvm::APFloat::IEEEdouble(), 2513 llvm::RoundingMode::Dynamic, &LoseInfo); 2514 if (!(Probability >= llvm::APFloat(0.0) && 2515 Probability <= llvm::APFloat(1.0))) { 2516 Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range) 2517 << ProbArg->getSourceRange(); 2518 return ExprError(); 2519 } 2520 break; 2521 } 2522 case Builtin::BI__builtin_preserve_access_index: 2523 if (BuiltinPreserveAI(*this, TheCall)) 2524 return ExprError(); 2525 break; 2526 case Builtin::BI__builtin_call_with_static_chain: 2527 if (BuiltinCallWithStaticChain(*this, TheCall)) 2528 return ExprError(); 2529 break; 2530 case Builtin::BI__exception_code: 2531 case Builtin::BI_exception_code: 2532 if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, 2533 diag::err_seh___except_block)) 2534 return ExprError(); 2535 break; 2536 case Builtin::BI__exception_info: 2537 case Builtin::BI_exception_info: 2538 if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, 2539 diag::err_seh___except_filter)) 2540 return ExprError(); 2541 break; 2542 case Builtin::BI__GetExceptionInfo: 2543 if (checkArgCount(TheCall, 1)) 2544 return ExprError(); 2545 2546 if (CheckCXXThrowOperand( 2547 TheCall->getBeginLoc(), 2548 Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), 2549 TheCall)) 2550 return ExprError(); 2551 2552 TheCall->setType(Context.VoidPtrTy); 2553 break; 2554 case Builtin::BIaddressof: 2555 case Builtin::BI__addressof: 2556 case Builtin::BIforward: 2557 case Builtin::BIforward_like: 2558 case Builtin::BImove: 2559 case Builtin::BImove_if_noexcept: 2560 case Builtin::BIas_const: { 2561 // These are all expected to be of the form 2562 // T &/&&/* f(U &/&&) 2563 // where T and U only differ in qualification. 2564 if (checkArgCount(TheCall, 1)) 2565 return ExprError(); 2566 QualType Param = FDecl->getParamDecl(0)->getType(); 2567 QualType Result = FDecl->getReturnType(); 2568 bool ReturnsPointer = BuiltinID == Builtin::BIaddressof || 2569 BuiltinID == Builtin::BI__addressof; 2570 if (!(Param->isReferenceType() && 2571 (ReturnsPointer ? Result->isAnyPointerType() 2572 : Result->isReferenceType()) && 2573 Context.hasSameUnqualifiedType(Param->getPointeeType(), 2574 Result->getPointeeType()))) { 2575 Diag(TheCall->getBeginLoc(), diag::err_builtin_move_forward_unsupported) 2576 << FDecl; 2577 return ExprError(); 2578 } 2579 break; 2580 } 2581 case Builtin::BI__builtin_ptrauth_strip: 2582 return PointerAuthStrip(*this, TheCall); 2583 case Builtin::BI__builtin_ptrauth_blend_discriminator: 2584 return PointerAuthBlendDiscriminator(*this, TheCall); 2585 case Builtin::BI__builtin_ptrauth_sign_constant: 2586 return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign, 2587 /*RequireConstant=*/true); 2588 case Builtin::BI__builtin_ptrauth_sign_unauthenticated: 2589 return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign, 2590 /*RequireConstant=*/false); 2591 case Builtin::BI__builtin_ptrauth_auth: 2592 return PointerAuthSignOrAuth(*this, TheCall, PAO_Auth, 2593 /*RequireConstant=*/false); 2594 case Builtin::BI__builtin_ptrauth_sign_generic_data: 2595 return PointerAuthSignGenericData(*this, TheCall); 2596 case Builtin::BI__builtin_ptrauth_auth_and_resign: 2597 return PointerAuthAuthAndResign(*this, TheCall); 2598 case Builtin::BI__builtin_ptrauth_string_discriminator: 2599 return PointerAuthStringDiscriminator(*this, TheCall); 2600 // OpenCL v2.0, s6.13.16 - Pipe functions 2601 case Builtin::BIread_pipe: 2602 case Builtin::BIwrite_pipe: 2603 // Since those two functions are declared with var args, we need a semantic 2604 // check for the argument. 2605 if (OpenCL().checkBuiltinRWPipe(TheCall)) 2606 return ExprError(); 2607 break; 2608 case Builtin::BIreserve_read_pipe: 2609 case Builtin::BIreserve_write_pipe: 2610 case Builtin::BIwork_group_reserve_read_pipe: 2611 case Builtin::BIwork_group_reserve_write_pipe: 2612 if (OpenCL().checkBuiltinReserveRWPipe(TheCall)) 2613 return ExprError(); 2614 break; 2615 case Builtin::BIsub_group_reserve_read_pipe: 2616 case Builtin::BIsub_group_reserve_write_pipe: 2617 if (OpenCL().checkSubgroupExt(TheCall) || 2618 OpenCL().checkBuiltinReserveRWPipe(TheCall)) 2619 return ExprError(); 2620 break; 2621 case Builtin::BIcommit_read_pipe: 2622 case Builtin::BIcommit_write_pipe: 2623 case Builtin::BIwork_group_commit_read_pipe: 2624 case Builtin::BIwork_group_commit_write_pipe: 2625 if (OpenCL().checkBuiltinCommitRWPipe(TheCall)) 2626 return ExprError(); 2627 break; 2628 case Builtin::BIsub_group_commit_read_pipe: 2629 case Builtin::BIsub_group_commit_write_pipe: 2630 if (OpenCL().checkSubgroupExt(TheCall) || 2631 OpenCL().checkBuiltinCommitRWPipe(TheCall)) 2632 return ExprError(); 2633 break; 2634 case Builtin::BIget_pipe_num_packets: 2635 case Builtin::BIget_pipe_max_packets: 2636 if (OpenCL().checkBuiltinPipePackets(TheCall)) 2637 return ExprError(); 2638 break; 2639 case Builtin::BIto_global: 2640 case Builtin::BIto_local: 2641 case Builtin::BIto_private: 2642 if (OpenCL().checkBuiltinToAddr(BuiltinID, TheCall)) 2643 return ExprError(); 2644 break; 2645 // OpenCL v2.0, s6.13.17 - Enqueue kernel functions. 2646 case Builtin::BIenqueue_kernel: 2647 if (OpenCL().checkBuiltinEnqueueKernel(TheCall)) 2648 return ExprError(); 2649 break; 2650 case Builtin::BIget_kernel_work_group_size: 2651 case Builtin::BIget_kernel_preferred_work_group_size_multiple: 2652 if (OpenCL().checkBuiltinKernelWorkGroupSize(TheCall)) 2653 return ExprError(); 2654 break; 2655 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange: 2656 case Builtin::BIget_kernel_sub_group_count_for_ndrange: 2657 if (OpenCL().checkBuiltinNDRangeAndBlock(TheCall)) 2658 return ExprError(); 2659 break; 2660 case Builtin::BI__builtin_os_log_format: 2661 Cleanup.setExprNeedsCleanups(true); 2662 [[fallthrough]]; 2663 case Builtin::BI__builtin_os_log_format_buffer_size: 2664 if (BuiltinOSLogFormat(TheCall)) 2665 return ExprError(); 2666 break; 2667 case Builtin::BI__builtin_frame_address: 2668 case Builtin::BI__builtin_return_address: { 2669 if (BuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF)) 2670 return ExprError(); 2671 2672 // -Wframe-address warning if non-zero passed to builtin 2673 // return/frame address. 2674 Expr::EvalResult Result; 2675 if (!TheCall->getArg(0)->isValueDependent() && 2676 TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) && 2677 Result.Val.getInt() != 0) 2678 Diag(TheCall->getBeginLoc(), diag::warn_frame_address) 2679 << ((BuiltinID == Builtin::BI__builtin_return_address) 2680 ? "__builtin_return_address" 2681 : "__builtin_frame_address") 2682 << TheCall->getSourceRange(); 2683 break; 2684 } 2685 2686 case Builtin::BI__builtin_nondeterministic_value: { 2687 if (BuiltinNonDeterministicValue(TheCall)) 2688 return ExprError(); 2689 break; 2690 } 2691 2692 // __builtin_elementwise_abs restricts the element type to signed integers or 2693 // floating point types only. 2694 case Builtin::BI__builtin_elementwise_abs: { 2695 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2696 return ExprError(); 2697 2698 QualType ArgTy = TheCall->getArg(0)->getType(); 2699 QualType EltTy = ArgTy; 2700 2701 if (auto *VecTy = EltTy->getAs<VectorType>()) 2702 EltTy = VecTy->getElementType(); 2703 if (EltTy->isUnsignedIntegerType()) { 2704 Diag(TheCall->getArg(0)->getBeginLoc(), 2705 diag::err_builtin_invalid_arg_type) 2706 << 1 << /* signed integer or float ty*/ 3 << ArgTy; 2707 return ExprError(); 2708 } 2709 break; 2710 } 2711 2712 // These builtins restrict the element type to floating point 2713 // types only. 2714 case Builtin::BI__builtin_elementwise_acos: 2715 case Builtin::BI__builtin_elementwise_asin: 2716 case Builtin::BI__builtin_elementwise_atan: 2717 case Builtin::BI__builtin_elementwise_ceil: 2718 case Builtin::BI__builtin_elementwise_cos: 2719 case Builtin::BI__builtin_elementwise_cosh: 2720 case Builtin::BI__builtin_elementwise_exp: 2721 case Builtin::BI__builtin_elementwise_exp2: 2722 case Builtin::BI__builtin_elementwise_floor: 2723 case Builtin::BI__builtin_elementwise_log: 2724 case Builtin::BI__builtin_elementwise_log2: 2725 case Builtin::BI__builtin_elementwise_log10: 2726 case Builtin::BI__builtin_elementwise_roundeven: 2727 case Builtin::BI__builtin_elementwise_round: 2728 case Builtin::BI__builtin_elementwise_rint: 2729 case Builtin::BI__builtin_elementwise_nearbyint: 2730 case Builtin::BI__builtin_elementwise_sin: 2731 case Builtin::BI__builtin_elementwise_sinh: 2732 case Builtin::BI__builtin_elementwise_sqrt: 2733 case Builtin::BI__builtin_elementwise_tan: 2734 case Builtin::BI__builtin_elementwise_tanh: 2735 case Builtin::BI__builtin_elementwise_trunc: 2736 case Builtin::BI__builtin_elementwise_canonicalize: { 2737 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2738 return ExprError(); 2739 2740 QualType ArgTy = TheCall->getArg(0)->getType(); 2741 if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), 2742 ArgTy, 1)) 2743 return ExprError(); 2744 break; 2745 } 2746 case Builtin::BI__builtin_elementwise_fma: { 2747 if (BuiltinElementwiseTernaryMath(TheCall)) 2748 return ExprError(); 2749 break; 2750 } 2751 2752 // These builtins restrict the element type to floating point 2753 // types only, and take in two arguments. 2754 case Builtin::BI__builtin_elementwise_minimum: 2755 case Builtin::BI__builtin_elementwise_maximum: 2756 case Builtin::BI__builtin_elementwise_atan2: 2757 case Builtin::BI__builtin_elementwise_fmod: 2758 case Builtin::BI__builtin_elementwise_pow: { 2759 if (BuiltinElementwiseMath(TheCall, /*FPOnly=*/true)) 2760 return ExprError(); 2761 break; 2762 } 2763 2764 // These builtins restrict the element type to integer 2765 // types only. 2766 case Builtin::BI__builtin_elementwise_add_sat: 2767 case Builtin::BI__builtin_elementwise_sub_sat: { 2768 if (BuiltinElementwiseMath(TheCall)) 2769 return ExprError(); 2770 2771 const Expr *Arg = TheCall->getArg(0); 2772 QualType ArgTy = Arg->getType(); 2773 QualType EltTy = ArgTy; 2774 2775 if (auto *VecTy = EltTy->getAs<VectorType>()) 2776 EltTy = VecTy->getElementType(); 2777 2778 if (!EltTy->isIntegerType()) { 2779 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2780 << 1 << /* integer ty */ 6 << ArgTy; 2781 return ExprError(); 2782 } 2783 break; 2784 } 2785 2786 case Builtin::BI__builtin_elementwise_min: 2787 case Builtin::BI__builtin_elementwise_max: 2788 if (BuiltinElementwiseMath(TheCall)) 2789 return ExprError(); 2790 break; 2791 case Builtin::BI__builtin_elementwise_popcount: 2792 case Builtin::BI__builtin_elementwise_bitreverse: { 2793 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2794 return ExprError(); 2795 2796 const Expr *Arg = TheCall->getArg(0); 2797 QualType ArgTy = Arg->getType(); 2798 QualType EltTy = ArgTy; 2799 2800 if (auto *VecTy = EltTy->getAs<VectorType>()) 2801 EltTy = VecTy->getElementType(); 2802 2803 if (!EltTy->isIntegerType()) { 2804 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2805 << 1 << /* integer ty */ 6 << ArgTy; 2806 return ExprError(); 2807 } 2808 break; 2809 } 2810 2811 case Builtin::BI__builtin_elementwise_copysign: { 2812 if (checkArgCount(TheCall, 2)) 2813 return ExprError(); 2814 2815 ExprResult Magnitude = UsualUnaryConversions(TheCall->getArg(0)); 2816 ExprResult Sign = UsualUnaryConversions(TheCall->getArg(1)); 2817 if (Magnitude.isInvalid() || Sign.isInvalid()) 2818 return ExprError(); 2819 2820 QualType MagnitudeTy = Magnitude.get()->getType(); 2821 QualType SignTy = Sign.get()->getType(); 2822 if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), 2823 MagnitudeTy, 1) || 2824 checkFPMathBuiltinElementType(*this, TheCall->getArg(1)->getBeginLoc(), 2825 SignTy, 2)) { 2826 return ExprError(); 2827 } 2828 2829 if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) { 2830 return Diag(Sign.get()->getBeginLoc(), 2831 diag::err_typecheck_call_different_arg_types) 2832 << MagnitudeTy << SignTy; 2833 } 2834 2835 TheCall->setArg(0, Magnitude.get()); 2836 TheCall->setArg(1, Sign.get()); 2837 TheCall->setType(Magnitude.get()->getType()); 2838 break; 2839 } 2840 case Builtin::BI__builtin_reduce_max: 2841 case Builtin::BI__builtin_reduce_min: { 2842 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2843 return ExprError(); 2844 2845 const Expr *Arg = TheCall->getArg(0); 2846 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2847 2848 QualType ElTy; 2849 if (TyA) 2850 ElTy = TyA->getElementType(); 2851 else if (Arg->getType()->isSizelessVectorType()) 2852 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2853 2854 if (ElTy.isNull()) { 2855 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2856 << 1 << /* vector ty*/ 4 << Arg->getType(); 2857 return ExprError(); 2858 } 2859 2860 TheCall->setType(ElTy); 2861 break; 2862 } 2863 case Builtin::BI__builtin_reduce_maximum: 2864 case Builtin::BI__builtin_reduce_minimum: { 2865 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2866 return ExprError(); 2867 2868 const Expr *Arg = TheCall->getArg(0); 2869 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2870 2871 QualType ElTy; 2872 if (TyA) 2873 ElTy = TyA->getElementType(); 2874 else if (Arg->getType()->isSizelessVectorType()) 2875 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2876 2877 if (ElTy.isNull() || !ElTy->isFloatingType()) { 2878 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2879 << 1 << /* vector of floating points */ 9 << Arg->getType(); 2880 return ExprError(); 2881 } 2882 2883 TheCall->setType(ElTy); 2884 break; 2885 } 2886 2887 // These builtins support vectors of integers only. 2888 // TODO: ADD/MUL should support floating-point types. 2889 case Builtin::BI__builtin_reduce_add: 2890 case Builtin::BI__builtin_reduce_mul: 2891 case Builtin::BI__builtin_reduce_xor: 2892 case Builtin::BI__builtin_reduce_or: 2893 case Builtin::BI__builtin_reduce_and: { 2894 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2895 return ExprError(); 2896 2897 const Expr *Arg = TheCall->getArg(0); 2898 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2899 2900 QualType ElTy; 2901 if (TyA) 2902 ElTy = TyA->getElementType(); 2903 else if (Arg->getType()->isSizelessVectorType()) 2904 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2905 2906 if (ElTy.isNull() || !ElTy->isIntegerType()) { 2907 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2908 << 1 << /* vector of integers */ 6 << Arg->getType(); 2909 return ExprError(); 2910 } 2911 2912 TheCall->setType(ElTy); 2913 break; 2914 } 2915 2916 case Builtin::BI__builtin_matrix_transpose: 2917 return BuiltinMatrixTranspose(TheCall, TheCallResult); 2918 2919 case Builtin::BI__builtin_matrix_column_major_load: 2920 return BuiltinMatrixColumnMajorLoad(TheCall, TheCallResult); 2921 2922 case Builtin::BI__builtin_matrix_column_major_store: 2923 return BuiltinMatrixColumnMajorStore(TheCall, TheCallResult); 2924 2925 case Builtin::BI__builtin_verbose_trap: 2926 if (!checkBuiltinVerboseTrap(TheCall, *this)) 2927 return ExprError(); 2928 break; 2929 2930 case Builtin::BI__builtin_get_device_side_mangled_name: { 2931 auto Check = [](CallExpr *TheCall) { 2932 if (TheCall->getNumArgs() != 1) 2933 return false; 2934 auto *DRE = dyn_cast<DeclRefExpr>(TheCall->getArg(0)->IgnoreImpCasts()); 2935 if (!DRE) 2936 return false; 2937 auto *D = DRE->getDecl(); 2938 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) 2939 return false; 2940 return D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<CUDADeviceAttr>() || 2941 D->hasAttr<CUDAConstantAttr>() || D->hasAttr<HIPManagedAttr>(); 2942 }; 2943 if (!Check(TheCall)) { 2944 Diag(TheCall->getBeginLoc(), 2945 diag::err_hip_invalid_args_builtin_mangled_name); 2946 return ExprError(); 2947 } 2948 break; 2949 } 2950 case Builtin::BI__builtin_popcountg: 2951 if (BuiltinPopcountg(*this, TheCall)) 2952 return ExprError(); 2953 break; 2954 case Builtin::BI__builtin_clzg: 2955 case Builtin::BI__builtin_ctzg: 2956 if (BuiltinCountZeroBitsGeneric(*this, TheCall)) 2957 return ExprError(); 2958 break; 2959 2960 case Builtin::BI__builtin_allow_runtime_check: { 2961 Expr *Arg = TheCall->getArg(0); 2962 // Check if the argument is a string literal. 2963 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) { 2964 Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 2965 << Arg->getSourceRange(); 2966 return ExprError(); 2967 } 2968 break; 2969 } 2970 case Builtin::BI__builtin_counted_by_ref: 2971 if (BuiltinCountedByRef(TheCall)) 2972 return ExprError(); 2973 break; 2974 } 2975 2976 if (getLangOpts().HLSL && HLSL().CheckBuiltinFunctionCall(BuiltinID, TheCall)) 2977 return ExprError(); 2978 2979 // Since the target specific builtins for each arch overlap, only check those 2980 // of the arch we are compiling for. 2981 if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) { 2982 if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) { 2983 assert(Context.getAuxTargetInfo() && 2984 "Aux Target Builtin, but not an aux target?"); 2985 2986 if (CheckTSBuiltinFunctionCall( 2987 *Context.getAuxTargetInfo(), 2988 Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall)) 2989 return ExprError(); 2990 } else { 2991 if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID, 2992 TheCall)) 2993 return ExprError(); 2994 } 2995 } 2996 2997 return TheCallResult; 2998 } 2999 3000 bool Sema::ValueIsRunOfOnes(CallExpr *TheCall, unsigned ArgNum) { 3001 llvm::APSInt Result; 3002 // We can't check the value of a dependent argument. 3003 Expr *Arg = TheCall->getArg(ArgNum); 3004 if (Arg->isTypeDependent() || Arg->isValueDependent()) 3005 return false; 3006 3007 // Check constant-ness first. 3008 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 3009 return true; 3010 3011 // Check contiguous run of 1s, 0xFF0000FF is also a run of 1s. 3012 if (Result.isShiftedMask() || (~Result).isShiftedMask()) 3013 return false; 3014 3015 return Diag(TheCall->getBeginLoc(), 3016 diag::err_argument_not_contiguous_bit_field) 3017 << ArgNum << Arg->getSourceRange(); 3018 } 3019 3020 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, 3021 bool IsVariadic, FormatStringInfo *FSI) { 3022 if (Format->getFirstArg() == 0) 3023 FSI->ArgPassingKind = FAPK_VAList; 3024 else if (IsVariadic) 3025 FSI->ArgPassingKind = FAPK_Variadic; 3026 else 3027 FSI->ArgPassingKind = FAPK_Fixed; 3028 FSI->FormatIdx = Format->getFormatIdx() - 1; 3029 FSI->FirstDataArg = 3030 FSI->ArgPassingKind == FAPK_VAList ? 0 : Format->getFirstArg() - 1; 3031 3032 // The way the format attribute works in GCC, the implicit this argument 3033 // of member functions is counted. However, it doesn't appear in our own 3034 // lists, so decrement format_idx in that case. 3035 if (IsCXXMember) { 3036 if(FSI->FormatIdx == 0) 3037 return false; 3038 --FSI->FormatIdx; 3039 if (FSI->FirstDataArg != 0) 3040 --FSI->FirstDataArg; 3041 } 3042 return true; 3043 } 3044 3045 /// Checks if a the given expression evaluates to null. 3046 /// 3047 /// Returns true if the value evaluates to null. 3048 static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { 3049 // Treat (smart) pointers constructed from nullptr as null, whether we can 3050 // const-evaluate them or not. 3051 // This must happen first: the smart pointer expr might have _Nonnull type! 3052 if (isa<CXXNullPtrLiteralExpr>( 3053 IgnoreExprNodes(Expr, IgnoreImplicitAsWrittenSingleStep, 3054 IgnoreElidableImplicitConstructorSingleStep))) 3055 return true; 3056 3057 // If the expression has non-null type, it doesn't evaluate to null. 3058 if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) { 3059 if (*nullability == NullabilityKind::NonNull) 3060 return false; 3061 } 3062 3063 // As a special case, transparent unions initialized with zero are 3064 // considered null for the purposes of the nonnull attribute. 3065 if (const RecordType *UT = Expr->getType()->getAsUnionType(); 3066 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { 3067 if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(Expr)) 3068 if (const auto *ILE = dyn_cast<InitListExpr>(CLE->getInitializer())) 3069 Expr = ILE->getInit(0); 3070 } 3071 3072 bool Result; 3073 return (!Expr->isValueDependent() && 3074 Expr->EvaluateAsBooleanCondition(Result, S.Context) && 3075 !Result); 3076 } 3077 3078 static void CheckNonNullArgument(Sema &S, 3079 const Expr *ArgExpr, 3080 SourceLocation CallSiteLoc) { 3081 if (CheckNonNullExpr(S, ArgExpr)) 3082 S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr, 3083 S.PDiag(diag::warn_null_arg) 3084 << ArgExpr->getSourceRange()); 3085 } 3086 3087 /// Determine whether the given type has a non-null nullability annotation. 3088 static bool isNonNullType(QualType type) { 3089 if (auto nullability = type->getNullability()) 3090 return *nullability == NullabilityKind::NonNull; 3091 3092 return false; 3093 } 3094 3095 static void CheckNonNullArguments(Sema &S, 3096 const NamedDecl *FDecl, 3097 const FunctionProtoType *Proto, 3098 ArrayRef<const Expr *> Args, 3099 SourceLocation CallSiteLoc) { 3100 assert((FDecl || Proto) && "Need a function declaration or prototype"); 3101 3102 // Already checked by constant evaluator. 3103 if (S.isConstantEvaluatedContext()) 3104 return; 3105 // Check the attributes attached to the method/function itself. 3106 llvm::SmallBitVector NonNullArgs; 3107 if (FDecl) { 3108 // Handle the nonnull attribute on the function/method declaration itself. 3109 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { 3110 if (!NonNull->args_size()) { 3111 // Easy case: all pointer arguments are nonnull. 3112 for (const auto *Arg : Args) 3113 if (S.isValidPointerAttrType(Arg->getType())) 3114 CheckNonNullArgument(S, Arg, CallSiteLoc); 3115 return; 3116 } 3117 3118 for (const ParamIdx &Idx : NonNull->args()) { 3119 unsigned IdxAST = Idx.getASTIndex(); 3120 if (IdxAST >= Args.size()) 3121 continue; 3122 if (NonNullArgs.empty()) 3123 NonNullArgs.resize(Args.size()); 3124 NonNullArgs.set(IdxAST); 3125 } 3126 } 3127 } 3128 3129 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) { 3130 // Handle the nonnull attribute on the parameters of the 3131 // function/method. 3132 ArrayRef<ParmVarDecl*> parms; 3133 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) 3134 parms = FD->parameters(); 3135 else 3136 parms = cast<ObjCMethodDecl>(FDecl)->parameters(); 3137 3138 unsigned ParamIndex = 0; 3139 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); 3140 I != E; ++I, ++ParamIndex) { 3141 const ParmVarDecl *PVD = *I; 3142 if (PVD->hasAttr<NonNullAttr>() || isNonNullType(PVD->getType())) { 3143 if (NonNullArgs.empty()) 3144 NonNullArgs.resize(Args.size()); 3145 3146 NonNullArgs.set(ParamIndex); 3147 } 3148 } 3149 } else { 3150 // If we have a non-function, non-method declaration but no 3151 // function prototype, try to dig out the function prototype. 3152 if (!Proto) { 3153 if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) { 3154 QualType type = VD->getType().getNonReferenceType(); 3155 if (auto pointerType = type->getAs<PointerType>()) 3156 type = pointerType->getPointeeType(); 3157 else if (auto blockType = type->getAs<BlockPointerType>()) 3158 type = blockType->getPointeeType(); 3159 // FIXME: data member pointers? 3160 3161 // Dig out the function prototype, if there is one. 3162 Proto = type->getAs<FunctionProtoType>(); 3163 } 3164 } 3165 3166 // Fill in non-null argument information from the nullability 3167 // information on the parameter types (if we have them). 3168 if (Proto) { 3169 unsigned Index = 0; 3170 for (auto paramType : Proto->getParamTypes()) { 3171 if (isNonNullType(paramType)) { 3172 if (NonNullArgs.empty()) 3173 NonNullArgs.resize(Args.size()); 3174 3175 NonNullArgs.set(Index); 3176 } 3177 3178 ++Index; 3179 } 3180 } 3181 } 3182 3183 // Check for non-null arguments. 3184 for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size(); 3185 ArgIndex != ArgIndexEnd; ++ArgIndex) { 3186 if (NonNullArgs[ArgIndex]) 3187 CheckNonNullArgument(S, Args[ArgIndex], Args[ArgIndex]->getExprLoc()); 3188 } 3189 } 3190 3191 void Sema::CheckArgAlignment(SourceLocation Loc, NamedDecl *FDecl, 3192 StringRef ParamName, QualType ArgTy, 3193 QualType ParamTy) { 3194 3195 // If a function accepts a pointer or reference type 3196 if (!ParamTy->isPointerType() && !ParamTy->isReferenceType()) 3197 return; 3198 3199 // If the parameter is a pointer type, get the pointee type for the 3200 // argument too. If the parameter is a reference type, don't try to get 3201 // the pointee type for the argument. 3202 if (ParamTy->isPointerType()) 3203 ArgTy = ArgTy->getPointeeType(); 3204 3205 // Remove reference or pointer 3206 ParamTy = ParamTy->getPointeeType(); 3207 3208 // Find expected alignment, and the actual alignment of the passed object. 3209 // getTypeAlignInChars requires complete types 3210 if (ArgTy.isNull() || ParamTy->isDependentType() || 3211 ParamTy->isIncompleteType() || ArgTy->isIncompleteType() || 3212 ParamTy->isUndeducedType() || ArgTy->isUndeducedType()) 3213 return; 3214 3215 CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy); 3216 CharUnits ArgAlign = Context.getTypeAlignInChars(ArgTy); 3217 3218 // If the argument is less aligned than the parameter, there is a 3219 // potential alignment issue. 3220 if (ArgAlign < ParamAlign) 3221 Diag(Loc, diag::warn_param_mismatched_alignment) 3222 << (int)ArgAlign.getQuantity() << (int)ParamAlign.getQuantity() 3223 << ParamName << (FDecl != nullptr) << FDecl; 3224 } 3225 3226 void Sema::checkLifetimeCaptureBy(FunctionDecl *FD, bool IsMemberFunction, 3227 const Expr *ThisArg, 3228 ArrayRef<const Expr *> Args) { 3229 if (!FD || Args.empty()) 3230 return; 3231 auto GetArgAt = [&](int Idx) -> const Expr * { 3232 if (Idx == LifetimeCaptureByAttr::GLOBAL || 3233 Idx == LifetimeCaptureByAttr::UNKNOWN) 3234 return nullptr; 3235 if (IsMemberFunction && Idx == 0) 3236 return ThisArg; 3237 return Args[Idx - IsMemberFunction]; 3238 }; 3239 auto HandleCaptureByAttr = [&](const LifetimeCaptureByAttr *Attr, 3240 unsigned ArgIdx) { 3241 if (!Attr) 3242 return; 3243 3244 Expr *Captured = const_cast<Expr *>(GetArgAt(ArgIdx)); 3245 for (int CapturingParamIdx : Attr->params()) { 3246 // lifetime_capture_by(this) case is handled in the lifetimebound expr 3247 // initialization codepath. 3248 if (CapturingParamIdx == LifetimeCaptureByAttr::THIS && 3249 isa<CXXConstructorDecl>(FD)) 3250 continue; 3251 Expr *Capturing = const_cast<Expr *>(GetArgAt(CapturingParamIdx)); 3252 CapturingEntity CE{Capturing}; 3253 // Ensure that 'Captured' outlives the 'Capturing' entity. 3254 checkCaptureByLifetime(*this, CE, Captured); 3255 } 3256 }; 3257 for (unsigned I = 0; I < FD->getNumParams(); ++I) 3258 HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(), 3259 I + IsMemberFunction); 3260 // Check when the implicit object param is captured. 3261 if (IsMemberFunction) { 3262 TypeSourceInfo *TSI = FD->getTypeSourceInfo(); 3263 if (!TSI) 3264 return; 3265 AttributedTypeLoc ATL; 3266 for (TypeLoc TL = TSI->getTypeLoc(); 3267 (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); 3268 TL = ATL.getModifiedLoc()) 3269 HandleCaptureByAttr(ATL.getAttrAs<LifetimeCaptureByAttr>(), 0); 3270 } 3271 } 3272 3273 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, 3274 const Expr *ThisArg, ArrayRef<const Expr *> Args, 3275 bool IsMemberFunction, SourceLocation Loc, 3276 SourceRange Range, VariadicCallType CallType) { 3277 // FIXME: We should check as much as we can in the template definition. 3278 if (CurContext->isDependentContext()) 3279 return; 3280 3281 // Printf and scanf checking. 3282 llvm::SmallBitVector CheckedVarArgs; 3283 if (FDecl) { 3284 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 3285 // Only create vector if there are format attributes. 3286 CheckedVarArgs.resize(Args.size()); 3287 3288 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, 3289 CheckedVarArgs); 3290 } 3291 } 3292 3293 // Refuse POD arguments that weren't caught by the format string 3294 // checks above. 3295 auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl); 3296 if (CallType != VariadicDoesNotApply && 3297 (!FD || FD->getBuiltinID() != Builtin::BI__noop)) { 3298 unsigned NumParams = Proto ? Proto->getNumParams() 3299 : isa_and_nonnull<FunctionDecl>(FDecl) 3300 ? cast<FunctionDecl>(FDecl)->getNumParams() 3301 : isa_and_nonnull<ObjCMethodDecl>(FDecl) 3302 ? cast<ObjCMethodDecl>(FDecl)->param_size() 3303 : 0; 3304 3305 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { 3306 // Args[ArgIdx] can be null in malformed code. 3307 if (const Expr *Arg = Args[ArgIdx]) { 3308 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) 3309 checkVariadicArgument(Arg, CallType); 3310 } 3311 } 3312 } 3313 if (FD) 3314 checkLifetimeCaptureBy(FD, IsMemberFunction, ThisArg, Args); 3315 if (FDecl || Proto) { 3316 CheckNonNullArguments(*this, FDecl, Proto, Args, Loc); 3317 3318 // Type safety checking. 3319 if (FDecl) { 3320 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) 3321 CheckArgumentWithTypeTag(I, Args, Loc); 3322 } 3323 } 3324 3325 // Check that passed arguments match the alignment of original arguments. 3326 // Try to get the missing prototype from the declaration. 3327 if (!Proto && FDecl) { 3328 const auto *FT = FDecl->getFunctionType(); 3329 if (isa_and_nonnull<FunctionProtoType>(FT)) 3330 Proto = cast<FunctionProtoType>(FDecl->getFunctionType()); 3331 } 3332 if (Proto) { 3333 // For variadic functions, we may have more args than parameters. 3334 // For some K&R functions, we may have less args than parameters. 3335 const auto N = std::min<unsigned>(Proto->getNumParams(), Args.size()); 3336 bool IsScalableRet = Proto->getReturnType()->isSizelessVectorType(); 3337 bool IsScalableArg = false; 3338 for (unsigned ArgIdx = 0; ArgIdx < N; ++ArgIdx) { 3339 // Args[ArgIdx] can be null in malformed code. 3340 if (const Expr *Arg = Args[ArgIdx]) { 3341 if (Arg->containsErrors()) 3342 continue; 3343 3344 if (Context.getTargetInfo().getTriple().isOSAIX() && FDecl && Arg && 3345 FDecl->hasLinkage() && 3346 FDecl->getFormalLinkage() != Linkage::Internal && 3347 CallType == VariadicDoesNotApply) 3348 PPC().checkAIXMemberAlignment((Arg->getExprLoc()), Arg); 3349 3350 QualType ParamTy = Proto->getParamType(ArgIdx); 3351 if (ParamTy->isSizelessVectorType()) 3352 IsScalableArg = true; 3353 QualType ArgTy = Arg->getType(); 3354 CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1), 3355 ArgTy, ParamTy); 3356 } 3357 } 3358 3359 // If the callee has an AArch64 SME attribute to indicate that it is an 3360 // __arm_streaming function, then the caller requires SME to be available. 3361 FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); 3362 if (ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask) { 3363 if (auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) { 3364 llvm::StringMap<bool> CallerFeatureMap; 3365 Context.getFunctionFeatureMap(CallerFeatureMap, CallerFD); 3366 if (!CallerFeatureMap.contains("sme")) 3367 Diag(Loc, diag::err_sme_call_in_non_sme_target); 3368 } else if (!Context.getTargetInfo().hasFeature("sme")) { 3369 Diag(Loc, diag::err_sme_call_in_non_sme_target); 3370 } 3371 } 3372 3373 // If the call requires a streaming-mode change and has scalable vector 3374 // arguments or return values, then warn the user that the streaming and 3375 // non-streaming vector lengths may be different. 3376 const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext); 3377 if (CallerFD && (!FD || !FD->getBuiltinID()) && 3378 (IsScalableArg || IsScalableRet)) { 3379 bool IsCalleeStreaming = 3380 ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask; 3381 bool IsCalleeStreamingCompatible = 3382 ExtInfo.AArch64SMEAttributes & 3383 FunctionType::SME_PStateSMCompatibleMask; 3384 SemaARM::ArmStreamingType CallerFnType = getArmStreamingFnType(CallerFD); 3385 if (!IsCalleeStreamingCompatible && 3386 (CallerFnType == SemaARM::ArmStreamingCompatible || 3387 ((CallerFnType == SemaARM::ArmStreaming) ^ IsCalleeStreaming))) { 3388 if (IsScalableArg) 3389 Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming) 3390 << /*IsArg=*/true; 3391 if (IsScalableRet) 3392 Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming) 3393 << /*IsArg=*/false; 3394 } 3395 } 3396 3397 FunctionType::ArmStateValue CalleeArmZAState = 3398 FunctionType::getArmZAState(ExtInfo.AArch64SMEAttributes); 3399 FunctionType::ArmStateValue CalleeArmZT0State = 3400 FunctionType::getArmZT0State(ExtInfo.AArch64SMEAttributes); 3401 if (CalleeArmZAState != FunctionType::ARM_None || 3402 CalleeArmZT0State != FunctionType::ARM_None) { 3403 bool CallerHasZAState = false; 3404 bool CallerHasZT0State = false; 3405 if (CallerFD) { 3406 auto *Attr = CallerFD->getAttr<ArmNewAttr>(); 3407 if (Attr && Attr->isNewZA()) 3408 CallerHasZAState = true; 3409 if (Attr && Attr->isNewZT0()) 3410 CallerHasZT0State = true; 3411 if (const auto *FPT = CallerFD->getType()->getAs<FunctionProtoType>()) { 3412 CallerHasZAState |= 3413 FunctionType::getArmZAState( 3414 FPT->getExtProtoInfo().AArch64SMEAttributes) != 3415 FunctionType::ARM_None; 3416 CallerHasZT0State |= 3417 FunctionType::getArmZT0State( 3418 FPT->getExtProtoInfo().AArch64SMEAttributes) != 3419 FunctionType::ARM_None; 3420 } 3421 } 3422 3423 if (CalleeArmZAState != FunctionType::ARM_None && !CallerHasZAState) 3424 Diag(Loc, diag::err_sme_za_call_no_za_state); 3425 3426 if (CalleeArmZT0State != FunctionType::ARM_None && !CallerHasZT0State) 3427 Diag(Loc, diag::err_sme_zt0_call_no_zt0_state); 3428 3429 if (CallerHasZAState && CalleeArmZAState == FunctionType::ARM_None && 3430 CalleeArmZT0State != FunctionType::ARM_None) { 3431 Diag(Loc, diag::err_sme_unimplemented_za_save_restore); 3432 Diag(Loc, diag::note_sme_use_preserves_za); 3433 } 3434 } 3435 } 3436 3437 if (FDecl && FDecl->hasAttr<AllocAlignAttr>()) { 3438 auto *AA = FDecl->getAttr<AllocAlignAttr>(); 3439 const Expr *Arg = Args[AA->getParamIndex().getASTIndex()]; 3440 if (!Arg->isValueDependent()) { 3441 Expr::EvalResult Align; 3442 if (Arg->EvaluateAsInt(Align, Context)) { 3443 const llvm::APSInt &I = Align.Val.getInt(); 3444 if (!I.isPowerOf2()) 3445 Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two) 3446 << Arg->getSourceRange(); 3447 3448 if (I > Sema::MaximumAlignment) 3449 Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great) 3450 << Arg->getSourceRange() << Sema::MaximumAlignment; 3451 } 3452 } 3453 } 3454 3455 if (FD) 3456 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc); 3457 } 3458 3459 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) { 3460 if (ConceptDecl *Decl = AutoT->getTypeConstraintConcept()) { 3461 DiagnoseUseOfDecl(Decl, Loc); 3462 } 3463 } 3464 3465 void Sema::CheckConstructorCall(FunctionDecl *FDecl, QualType ThisType, 3466 ArrayRef<const Expr *> Args, 3467 const FunctionProtoType *Proto, 3468 SourceLocation Loc) { 3469 VariadicCallType CallType = 3470 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; 3471 3472 auto *Ctor = cast<CXXConstructorDecl>(FDecl); 3473 CheckArgAlignment( 3474 Loc, FDecl, "'this'", Context.getPointerType(ThisType), 3475 Context.getPointerType(Ctor->getFunctionObjectParameterType())); 3476 3477 checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true, 3478 Loc, SourceRange(), CallType); 3479 } 3480 3481 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, 3482 const FunctionProtoType *Proto) { 3483 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && 3484 isa<CXXMethodDecl>(FDecl); 3485 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || 3486 IsMemberOperatorCall; 3487 VariadicCallType CallType = getVariadicCallType(FDecl, Proto, 3488 TheCall->getCallee()); 3489 Expr** Args = TheCall->getArgs(); 3490 unsigned NumArgs = TheCall->getNumArgs(); 3491 3492 Expr *ImplicitThis = nullptr; 3493 if (IsMemberOperatorCall && !FDecl->hasCXXExplicitFunctionObjectParameter()) { 3494 // If this is a call to a member operator, hide the first 3495 // argument from checkCall. 3496 // FIXME: Our choice of AST representation here is less than ideal. 3497 ImplicitThis = Args[0]; 3498 ++Args; 3499 --NumArgs; 3500 } else if (IsMemberFunction && !FDecl->isStatic() && 3501 !FDecl->hasCXXExplicitFunctionObjectParameter()) 3502 ImplicitThis = 3503 cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument(); 3504 3505 if (ImplicitThis) { 3506 // ImplicitThis may or may not be a pointer, depending on whether . or -> is 3507 // used. 3508 QualType ThisType = ImplicitThis->getType(); 3509 if (!ThisType->isPointerType()) { 3510 assert(!ThisType->isReferenceType()); 3511 ThisType = Context.getPointerType(ThisType); 3512 } 3513 3514 QualType ThisTypeFromDecl = Context.getPointerType( 3515 cast<CXXMethodDecl>(FDecl)->getFunctionObjectParameterType()); 3516 3517 CheckArgAlignment(TheCall->getRParenLoc(), FDecl, "'this'", ThisType, 3518 ThisTypeFromDecl); 3519 } 3520 3521 checkCall(FDecl, Proto, ImplicitThis, llvm::ArrayRef(Args, NumArgs), 3522 IsMemberFunction, TheCall->getRParenLoc(), 3523 TheCall->getCallee()->getSourceRange(), CallType); 3524 3525 IdentifierInfo *FnInfo = FDecl->getIdentifier(); 3526 // None of the checks below are needed for functions that don't have 3527 // simple names (e.g., C++ conversion functions). 3528 if (!FnInfo) 3529 return false; 3530 3531 // Enforce TCB except for builtin calls, which are always allowed. 3532 if (FDecl->getBuiltinID() == 0) 3533 CheckTCBEnforcement(TheCall->getExprLoc(), FDecl); 3534 3535 CheckAbsoluteValueFunction(TheCall, FDecl); 3536 CheckMaxUnsignedZero(TheCall, FDecl); 3537 CheckInfNaNFunction(TheCall, FDecl); 3538 3539 if (getLangOpts().ObjC) 3540 ObjC().DiagnoseCStringFormatDirectiveInCFAPI(FDecl, Args, NumArgs); 3541 3542 unsigned CMId = FDecl->getMemoryFunctionKind(); 3543 3544 // Handle memory setting and copying functions. 3545 switch (CMId) { 3546 case 0: 3547 return false; 3548 case Builtin::BIstrlcpy: // fallthrough 3549 case Builtin::BIstrlcat: 3550 CheckStrlcpycatArguments(TheCall, FnInfo); 3551 break; 3552 case Builtin::BIstrncat: 3553 CheckStrncatArguments(TheCall, FnInfo); 3554 break; 3555 case Builtin::BIfree: 3556 CheckFreeArguments(TheCall); 3557 break; 3558 default: 3559 CheckMemaccessArguments(TheCall, CMId, FnInfo); 3560 } 3561 3562 return false; 3563 } 3564 3565 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, 3566 const FunctionProtoType *Proto) { 3567 QualType Ty; 3568 if (const auto *V = dyn_cast<VarDecl>(NDecl)) 3569 Ty = V->getType().getNonReferenceType(); 3570 else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) 3571 Ty = F->getType().getNonReferenceType(); 3572 else 3573 return false; 3574 3575 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() && 3576 !Ty->isFunctionProtoType()) 3577 return false; 3578 3579 VariadicCallType CallType; 3580 if (!Proto || !Proto->isVariadic()) { 3581 CallType = VariadicDoesNotApply; 3582 } else if (Ty->isBlockPointerType()) { 3583 CallType = VariadicBlock; 3584 } else { // Ty->isFunctionPointerType() 3585 CallType = VariadicFunction; 3586 } 3587 3588 checkCall(NDecl, Proto, /*ThisArg=*/nullptr, 3589 llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 3590 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 3591 TheCall->getCallee()->getSourceRange(), CallType); 3592 3593 return false; 3594 } 3595 3596 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { 3597 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, 3598 TheCall->getCallee()); 3599 checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr, 3600 llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 3601 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 3602 TheCall->getCallee()->getSourceRange(), CallType); 3603 3604 return false; 3605 } 3606 3607 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { 3608 if (!llvm::isValidAtomicOrderingCABI(Ordering)) 3609 return false; 3610 3611 auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering; 3612 switch (Op) { 3613 case AtomicExpr::AO__c11_atomic_init: 3614 case AtomicExpr::AO__opencl_atomic_init: 3615 llvm_unreachable("There is no ordering argument for an init"); 3616 3617 case AtomicExpr::AO__c11_atomic_load: 3618 case AtomicExpr::AO__opencl_atomic_load: 3619 case AtomicExpr::AO__hip_atomic_load: 3620 case AtomicExpr::AO__atomic_load_n: 3621 case AtomicExpr::AO__atomic_load: 3622 case AtomicExpr::AO__scoped_atomic_load_n: 3623 case AtomicExpr::AO__scoped_atomic_load: 3624 return OrderingCABI != llvm::AtomicOrderingCABI::release && 3625 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 3626 3627 case AtomicExpr::AO__c11_atomic_store: 3628 case AtomicExpr::AO__opencl_atomic_store: 3629 case AtomicExpr::AO__hip_atomic_store: 3630 case AtomicExpr::AO__atomic_store: 3631 case AtomicExpr::AO__atomic_store_n: 3632 case AtomicExpr::AO__scoped_atomic_store: 3633 case AtomicExpr::AO__scoped_atomic_store_n: 3634 return OrderingCABI != llvm::AtomicOrderingCABI::consume && 3635 OrderingCABI != llvm::AtomicOrderingCABI::acquire && 3636 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 3637 3638 default: 3639 return true; 3640 } 3641 } 3642 3643 ExprResult Sema::AtomicOpsOverloaded(ExprResult TheCallResult, 3644 AtomicExpr::AtomicOp Op) { 3645 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); 3646 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 3647 MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()}; 3648 return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()}, 3649 DRE->getSourceRange(), TheCall->getRParenLoc(), Args, 3650 Op); 3651 } 3652 3653 ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, 3654 SourceLocation RParenLoc, MultiExprArg Args, 3655 AtomicExpr::AtomicOp Op, 3656 AtomicArgumentOrder ArgOrder) { 3657 // All the non-OpenCL operations take one of the following forms. 3658 // The OpenCL operations take the __c11 forms with one extra argument for 3659 // synchronization scope. 3660 enum { 3661 // C __c11_atomic_init(A *, C) 3662 Init, 3663 3664 // C __c11_atomic_load(A *, int) 3665 Load, 3666 3667 // void __atomic_load(A *, CP, int) 3668 LoadCopy, 3669 3670 // void __atomic_store(A *, CP, int) 3671 Copy, 3672 3673 // C __c11_atomic_add(A *, M, int) 3674 Arithmetic, 3675 3676 // C __atomic_exchange_n(A *, CP, int) 3677 Xchg, 3678 3679 // void __atomic_exchange(A *, C *, CP, int) 3680 GNUXchg, 3681 3682 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) 3683 C11CmpXchg, 3684 3685 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) 3686 GNUCmpXchg 3687 } Form = Init; 3688 3689 const unsigned NumForm = GNUCmpXchg + 1; 3690 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 }; 3691 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 }; 3692 // where: 3693 // C is an appropriate type, 3694 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, 3695 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, 3696 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and 3697 // the int parameters are for orderings. 3698 3699 static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm 3700 && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm, 3701 "need to update code for modified forms"); 3702 static_assert(AtomicExpr::AO__atomic_add_fetch == 0 && 3703 AtomicExpr::AO__atomic_xor_fetch + 1 == 3704 AtomicExpr::AO__c11_atomic_compare_exchange_strong, 3705 "need to update code for modified C11 atomics"); 3706 bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_compare_exchange_strong && 3707 Op <= AtomicExpr::AO__opencl_atomic_store; 3708 bool IsHIP = Op >= AtomicExpr::AO__hip_atomic_compare_exchange_strong && 3709 Op <= AtomicExpr::AO__hip_atomic_store; 3710 bool IsScoped = Op >= AtomicExpr::AO__scoped_atomic_add_fetch && 3711 Op <= AtomicExpr::AO__scoped_atomic_xor_fetch; 3712 bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_compare_exchange_strong && 3713 Op <= AtomicExpr::AO__c11_atomic_store) || 3714 IsOpenCL; 3715 bool IsN = Op == AtomicExpr::AO__atomic_load_n || 3716 Op == AtomicExpr::AO__atomic_store_n || 3717 Op == AtomicExpr::AO__atomic_exchange_n || 3718 Op == AtomicExpr::AO__atomic_compare_exchange_n || 3719 Op == AtomicExpr::AO__scoped_atomic_load_n || 3720 Op == AtomicExpr::AO__scoped_atomic_store_n || 3721 Op == AtomicExpr::AO__scoped_atomic_exchange_n || 3722 Op == AtomicExpr::AO__scoped_atomic_compare_exchange_n; 3723 // Bit mask for extra allowed value types other than integers for atomic 3724 // arithmetic operations. Add/sub allow pointer and floating point. Min/max 3725 // allow floating point. 3726 enum ArithOpExtraValueType { 3727 AOEVT_None = 0, 3728 AOEVT_Pointer = 1, 3729 AOEVT_FP = 2, 3730 }; 3731 unsigned ArithAllows = AOEVT_None; 3732 3733 switch (Op) { 3734 case AtomicExpr::AO__c11_atomic_init: 3735 case AtomicExpr::AO__opencl_atomic_init: 3736 Form = Init; 3737 break; 3738 3739 case AtomicExpr::AO__c11_atomic_load: 3740 case AtomicExpr::AO__opencl_atomic_load: 3741 case AtomicExpr::AO__hip_atomic_load: 3742 case AtomicExpr::AO__atomic_load_n: 3743 case AtomicExpr::AO__scoped_atomic_load_n: 3744 Form = Load; 3745 break; 3746 3747 case AtomicExpr::AO__atomic_load: 3748 case AtomicExpr::AO__scoped_atomic_load: 3749 Form = LoadCopy; 3750 break; 3751 3752 case AtomicExpr::AO__c11_atomic_store: 3753 case AtomicExpr::AO__opencl_atomic_store: 3754 case AtomicExpr::AO__hip_atomic_store: 3755 case AtomicExpr::AO__atomic_store: 3756 case AtomicExpr::AO__atomic_store_n: 3757 case AtomicExpr::AO__scoped_atomic_store: 3758 case AtomicExpr::AO__scoped_atomic_store_n: 3759 Form = Copy; 3760 break; 3761 case AtomicExpr::AO__atomic_fetch_add: 3762 case AtomicExpr::AO__atomic_fetch_sub: 3763 case AtomicExpr::AO__atomic_add_fetch: 3764 case AtomicExpr::AO__atomic_sub_fetch: 3765 case AtomicExpr::AO__scoped_atomic_fetch_add: 3766 case AtomicExpr::AO__scoped_atomic_fetch_sub: 3767 case AtomicExpr::AO__scoped_atomic_add_fetch: 3768 case AtomicExpr::AO__scoped_atomic_sub_fetch: 3769 case AtomicExpr::AO__c11_atomic_fetch_add: 3770 case AtomicExpr::AO__c11_atomic_fetch_sub: 3771 case AtomicExpr::AO__opencl_atomic_fetch_add: 3772 case AtomicExpr::AO__opencl_atomic_fetch_sub: 3773 case AtomicExpr::AO__hip_atomic_fetch_add: 3774 case AtomicExpr::AO__hip_atomic_fetch_sub: 3775 ArithAllows = AOEVT_Pointer | AOEVT_FP; 3776 Form = Arithmetic; 3777 break; 3778 case AtomicExpr::AO__atomic_fetch_max: 3779 case AtomicExpr::AO__atomic_fetch_min: 3780 case AtomicExpr::AO__atomic_max_fetch: 3781 case AtomicExpr::AO__atomic_min_fetch: 3782 case AtomicExpr::AO__scoped_atomic_fetch_max: 3783 case AtomicExpr::AO__scoped_atomic_fetch_min: 3784 case AtomicExpr::AO__scoped_atomic_max_fetch: 3785 case AtomicExpr::AO__scoped_atomic_min_fetch: 3786 case AtomicExpr::AO__c11_atomic_fetch_max: 3787 case AtomicExpr::AO__c11_atomic_fetch_min: 3788 case AtomicExpr::AO__opencl_atomic_fetch_max: 3789 case AtomicExpr::AO__opencl_atomic_fetch_min: 3790 case AtomicExpr::AO__hip_atomic_fetch_max: 3791 case AtomicExpr::AO__hip_atomic_fetch_min: 3792 ArithAllows = AOEVT_FP; 3793 Form = Arithmetic; 3794 break; 3795 case AtomicExpr::AO__c11_atomic_fetch_and: 3796 case AtomicExpr::AO__c11_atomic_fetch_or: 3797 case AtomicExpr::AO__c11_atomic_fetch_xor: 3798 case AtomicExpr::AO__hip_atomic_fetch_and: 3799 case AtomicExpr::AO__hip_atomic_fetch_or: 3800 case AtomicExpr::AO__hip_atomic_fetch_xor: 3801 case AtomicExpr::AO__c11_atomic_fetch_nand: 3802 case AtomicExpr::AO__opencl_atomic_fetch_and: 3803 case AtomicExpr::AO__opencl_atomic_fetch_or: 3804 case AtomicExpr::AO__opencl_atomic_fetch_xor: 3805 case AtomicExpr::AO__atomic_fetch_and: 3806 case AtomicExpr::AO__atomic_fetch_or: 3807 case AtomicExpr::AO__atomic_fetch_xor: 3808 case AtomicExpr::AO__atomic_fetch_nand: 3809 case AtomicExpr::AO__atomic_and_fetch: 3810 case AtomicExpr::AO__atomic_or_fetch: 3811 case AtomicExpr::AO__atomic_xor_fetch: 3812 case AtomicExpr::AO__atomic_nand_fetch: 3813 case AtomicExpr::AO__scoped_atomic_fetch_and: 3814 case AtomicExpr::AO__scoped_atomic_fetch_or: 3815 case AtomicExpr::AO__scoped_atomic_fetch_xor: 3816 case AtomicExpr::AO__scoped_atomic_fetch_nand: 3817 case AtomicExpr::AO__scoped_atomic_and_fetch: 3818 case AtomicExpr::AO__scoped_atomic_or_fetch: 3819 case AtomicExpr::AO__scoped_atomic_xor_fetch: 3820 case AtomicExpr::AO__scoped_atomic_nand_fetch: 3821 Form = Arithmetic; 3822 break; 3823 3824 case AtomicExpr::AO__c11_atomic_exchange: 3825 case AtomicExpr::AO__hip_atomic_exchange: 3826 case AtomicExpr::AO__opencl_atomic_exchange: 3827 case AtomicExpr::AO__atomic_exchange_n: 3828 case AtomicExpr::AO__scoped_atomic_exchange_n: 3829 Form = Xchg; 3830 break; 3831 3832 case AtomicExpr::AO__atomic_exchange: 3833 case AtomicExpr::AO__scoped_atomic_exchange: 3834 Form = GNUXchg; 3835 break; 3836 3837 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 3838 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 3839 case AtomicExpr::AO__hip_atomic_compare_exchange_strong: 3840 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong: 3841 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak: 3842 case AtomicExpr::AO__hip_atomic_compare_exchange_weak: 3843 Form = C11CmpXchg; 3844 break; 3845 3846 case AtomicExpr::AO__atomic_compare_exchange: 3847 case AtomicExpr::AO__atomic_compare_exchange_n: 3848 case AtomicExpr::AO__scoped_atomic_compare_exchange: 3849 case AtomicExpr::AO__scoped_atomic_compare_exchange_n: 3850 Form = GNUCmpXchg; 3851 break; 3852 } 3853 3854 unsigned AdjustedNumArgs = NumArgs[Form]; 3855 if ((IsOpenCL || IsHIP || IsScoped) && 3856 Op != AtomicExpr::AO__opencl_atomic_init) 3857 ++AdjustedNumArgs; 3858 // Check we have the right number of arguments. 3859 if (Args.size() < AdjustedNumArgs) { 3860 Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args) 3861 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) 3862 << /*is non object*/ 0 << ExprRange; 3863 return ExprError(); 3864 } else if (Args.size() > AdjustedNumArgs) { 3865 Diag(Args[AdjustedNumArgs]->getBeginLoc(), 3866 diag::err_typecheck_call_too_many_args) 3867 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) 3868 << /*is non object*/ 0 << ExprRange; 3869 return ExprError(); 3870 } 3871 3872 // Inspect the first argument of the atomic operation. 3873 Expr *Ptr = Args[0]; 3874 ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr); 3875 if (ConvertedPtr.isInvalid()) 3876 return ExprError(); 3877 3878 Ptr = ConvertedPtr.get(); 3879 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); 3880 if (!pointerType) { 3881 Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) 3882 << Ptr->getType() << 0 << Ptr->getSourceRange(); 3883 return ExprError(); 3884 } 3885 3886 // For a __c11 builtin, this should be a pointer to an _Atomic type. 3887 QualType AtomTy = pointerType->getPointeeType(); // 'A' 3888 QualType ValType = AtomTy; // 'C' 3889 if (IsC11) { 3890 if (!AtomTy->isAtomicType()) { 3891 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic) 3892 << Ptr->getType() << Ptr->getSourceRange(); 3893 return ExprError(); 3894 } 3895 if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) || 3896 AtomTy.getAddressSpace() == LangAS::opencl_constant) { 3897 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic) 3898 << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType() 3899 << Ptr->getSourceRange(); 3900 return ExprError(); 3901 } 3902 ValType = AtomTy->castAs<AtomicType>()->getValueType(); 3903 } else if (Form != Load && Form != LoadCopy) { 3904 if (ValType.isConstQualified()) { 3905 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer) 3906 << Ptr->getType() << Ptr->getSourceRange(); 3907 return ExprError(); 3908 } 3909 } 3910 3911 // Pointer to object of size zero is not allowed. 3912 if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy, 3913 diag::err_incomplete_type)) 3914 return ExprError(); 3915 if (Context.getTypeInfoInChars(AtomTy).Width.isZero()) { 3916 Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) 3917 << Ptr->getType() << 1 << Ptr->getSourceRange(); 3918 return ExprError(); 3919 } 3920 3921 // For an arithmetic operation, the implied arithmetic must be well-formed. 3922 if (Form == Arithmetic) { 3923 // GCC does not enforce these rules for GNU atomics, but we do to help catch 3924 // trivial type errors. 3925 auto IsAllowedValueType = [&](QualType ValType, 3926 unsigned AllowedType) -> bool { 3927 if (ValType->isIntegerType()) 3928 return true; 3929 if (ValType->isPointerType()) 3930 return AllowedType & AOEVT_Pointer; 3931 if (!(ValType->isFloatingType() && (AllowedType & AOEVT_FP))) 3932 return false; 3933 // LLVM Parser does not allow atomicrmw with x86_fp80 type. 3934 if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) && 3935 &Context.getTargetInfo().getLongDoubleFormat() == 3936 &llvm::APFloat::x87DoubleExtended()) 3937 return false; 3938 return true; 3939 }; 3940 if (!IsAllowedValueType(ValType, ArithAllows)) { 3941 auto DID = ArithAllows & AOEVT_FP 3942 ? (ArithAllows & AOEVT_Pointer 3943 ? diag::err_atomic_op_needs_atomic_int_ptr_or_fp 3944 : diag::err_atomic_op_needs_atomic_int_or_fp) 3945 : diag::err_atomic_op_needs_atomic_int; 3946 Diag(ExprRange.getBegin(), DID) 3947 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 3948 return ExprError(); 3949 } 3950 if (IsC11 && ValType->isPointerType() && 3951 RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(), 3952 diag::err_incomplete_type)) { 3953 return ExprError(); 3954 } 3955 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { 3956 // For __atomic_*_n operations, the value type must be a scalar integral or 3957 // pointer type which is 1, 2, 4, 8 or 16 bytes in length. 3958 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr) 3959 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 3960 return ExprError(); 3961 } 3962 3963 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && 3964 !AtomTy->isScalarType()) { 3965 // For GNU atomics, require a trivially-copyable type. This is not part of 3966 // the GNU atomics specification but we enforce it for consistency with 3967 // other atomics which generally all require a trivially-copyable type. This 3968 // is because atomics just copy bits. 3969 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy) 3970 << Ptr->getType() << Ptr->getSourceRange(); 3971 return ExprError(); 3972 } 3973 3974 switch (ValType.getObjCLifetime()) { 3975 case Qualifiers::OCL_None: 3976 case Qualifiers::OCL_ExplicitNone: 3977 // okay 3978 break; 3979 3980 case Qualifiers::OCL_Weak: 3981 case Qualifiers::OCL_Strong: 3982 case Qualifiers::OCL_Autoreleasing: 3983 // FIXME: Can this happen? By this point, ValType should be known 3984 // to be trivially copyable. 3985 Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership) 3986 << ValType << Ptr->getSourceRange(); 3987 return ExprError(); 3988 } 3989 3990 // All atomic operations have an overload which takes a pointer to a volatile 3991 // 'A'. We shouldn't let the volatile-ness of the pointee-type inject itself 3992 // into the result or the other operands. Similarly atomic_load takes a 3993 // pointer to a const 'A'. 3994 ValType.removeLocalVolatile(); 3995 ValType.removeLocalConst(); 3996 QualType ResultType = ValType; 3997 if (Form == Copy || Form == LoadCopy || Form == GNUXchg || 3998 Form == Init) 3999 ResultType = Context.VoidTy; 4000 else if (Form == C11CmpXchg || Form == GNUCmpXchg) 4001 ResultType = Context.BoolTy; 4002 4003 // The type of a parameter passed 'by value'. In the GNU atomics, such 4004 // arguments are actually passed as pointers. 4005 QualType ByValType = ValType; // 'CP' 4006 bool IsPassedByAddress = false; 4007 if (!IsC11 && !IsHIP && !IsN) { 4008 ByValType = Ptr->getType(); 4009 IsPassedByAddress = true; 4010 } 4011 4012 SmallVector<Expr *, 5> APIOrderedArgs; 4013 if (ArgOrder == Sema::AtomicArgumentOrder::AST) { 4014 APIOrderedArgs.push_back(Args[0]); 4015 switch (Form) { 4016 case Init: 4017 case Load: 4018 APIOrderedArgs.push_back(Args[1]); // Val1/Order 4019 break; 4020 case LoadCopy: 4021 case Copy: 4022 case Arithmetic: 4023 case Xchg: 4024 APIOrderedArgs.push_back(Args[2]); // Val1 4025 APIOrderedArgs.push_back(Args[1]); // Order 4026 break; 4027 case GNUXchg: 4028 APIOrderedArgs.push_back(Args[2]); // Val1 4029 APIOrderedArgs.push_back(Args[3]); // Val2 4030 APIOrderedArgs.push_back(Args[1]); // Order 4031 break; 4032 case C11CmpXchg: 4033 APIOrderedArgs.push_back(Args[2]); // Val1 4034 APIOrderedArgs.push_back(Args[4]); // Val2 4035 APIOrderedArgs.push_back(Args[1]); // Order 4036 APIOrderedArgs.push_back(Args[3]); // OrderFail 4037 break; 4038 case GNUCmpXchg: 4039 APIOrderedArgs.push_back(Args[2]); // Val1 4040 APIOrderedArgs.push_back(Args[4]); // Val2 4041 APIOrderedArgs.push_back(Args[5]); // Weak 4042 APIOrderedArgs.push_back(Args[1]); // Order 4043 APIOrderedArgs.push_back(Args[3]); // OrderFail 4044 break; 4045 } 4046 } else 4047 APIOrderedArgs.append(Args.begin(), Args.end()); 4048 4049 // The first argument's non-CV pointer type is used to deduce the type of 4050 // subsequent arguments, except for: 4051 // - weak flag (always converted to bool) 4052 // - memory order (always converted to int) 4053 // - scope (always converted to int) 4054 for (unsigned i = 0; i != APIOrderedArgs.size(); ++i) { 4055 QualType Ty; 4056 if (i < NumVals[Form] + 1) { 4057 switch (i) { 4058 case 0: 4059 // The first argument is always a pointer. It has a fixed type. 4060 // It is always dereferenced, a nullptr is undefined. 4061 CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); 4062 // Nothing else to do: we already know all we want about this pointer. 4063 continue; 4064 case 1: 4065 // The second argument is the non-atomic operand. For arithmetic, this 4066 // is always passed by value, and for a compare_exchange it is always 4067 // passed by address. For the rest, GNU uses by-address and C11 uses 4068 // by-value. 4069 assert(Form != Load); 4070 if (Form == Arithmetic && ValType->isPointerType()) 4071 Ty = Context.getPointerDiffType(); 4072 else if (Form == Init || Form == Arithmetic) 4073 Ty = ValType; 4074 else if (Form == Copy || Form == Xchg) { 4075 if (IsPassedByAddress) { 4076 // The value pointer is always dereferenced, a nullptr is undefined. 4077 CheckNonNullArgument(*this, APIOrderedArgs[i], 4078 ExprRange.getBegin()); 4079 } 4080 Ty = ByValType; 4081 } else { 4082 Expr *ValArg = APIOrderedArgs[i]; 4083 // The value pointer is always dereferenced, a nullptr is undefined. 4084 CheckNonNullArgument(*this, ValArg, ExprRange.getBegin()); 4085 LangAS AS = LangAS::Default; 4086 // Keep address space of non-atomic pointer type. 4087 if (const PointerType *PtrTy = 4088 ValArg->getType()->getAs<PointerType>()) { 4089 AS = PtrTy->getPointeeType().getAddressSpace(); 4090 } 4091 Ty = Context.getPointerType( 4092 Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); 4093 } 4094 break; 4095 case 2: 4096 // The third argument to compare_exchange / GNU exchange is the desired 4097 // value, either by-value (for the C11 and *_n variant) or as a pointer. 4098 if (IsPassedByAddress) 4099 CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); 4100 Ty = ByValType; 4101 break; 4102 case 3: 4103 // The fourth argument to GNU compare_exchange is a 'weak' flag. 4104 Ty = Context.BoolTy; 4105 break; 4106 } 4107 } else { 4108 // The order(s) and scope are always converted to int. 4109 Ty = Context.IntTy; 4110 } 4111 4112 InitializedEntity Entity = 4113 InitializedEntity::InitializeParameter(Context, Ty, false); 4114 ExprResult Arg = APIOrderedArgs[i]; 4115 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4116 if (Arg.isInvalid()) 4117 return true; 4118 APIOrderedArgs[i] = Arg.get(); 4119 } 4120 4121 // Permute the arguments into a 'consistent' order. 4122 SmallVector<Expr*, 5> SubExprs; 4123 SubExprs.push_back(Ptr); 4124 switch (Form) { 4125 case Init: 4126 // Note, AtomicExpr::getVal1() has a special case for this atomic. 4127 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4128 break; 4129 case Load: 4130 SubExprs.push_back(APIOrderedArgs[1]); // Order 4131 break; 4132 case LoadCopy: 4133 case Copy: 4134 case Arithmetic: 4135 case Xchg: 4136 SubExprs.push_back(APIOrderedArgs[2]); // Order 4137 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4138 break; 4139 case GNUXchg: 4140 // Note, AtomicExpr::getVal2() has a special case for this atomic. 4141 SubExprs.push_back(APIOrderedArgs[3]); // Order 4142 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4143 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4144 break; 4145 case C11CmpXchg: 4146 SubExprs.push_back(APIOrderedArgs[3]); // Order 4147 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4148 SubExprs.push_back(APIOrderedArgs[4]); // OrderFail 4149 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4150 break; 4151 case GNUCmpXchg: 4152 SubExprs.push_back(APIOrderedArgs[4]); // Order 4153 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4154 SubExprs.push_back(APIOrderedArgs[5]); // OrderFail 4155 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4156 SubExprs.push_back(APIOrderedArgs[3]); // Weak 4157 break; 4158 } 4159 4160 // If the memory orders are constants, check they are valid. 4161 if (SubExprs.size() >= 2 && Form != Init) { 4162 std::optional<llvm::APSInt> Success = 4163 SubExprs[1]->getIntegerConstantExpr(Context); 4164 if (Success && !isValidOrderingForOp(Success->getSExtValue(), Op)) { 4165 Diag(SubExprs[1]->getBeginLoc(), 4166 diag::warn_atomic_op_has_invalid_memory_order) 4167 << /*success=*/(Form == C11CmpXchg || Form == GNUCmpXchg) 4168 << SubExprs[1]->getSourceRange(); 4169 } 4170 if (SubExprs.size() >= 5) { 4171 if (std::optional<llvm::APSInt> Failure = 4172 SubExprs[3]->getIntegerConstantExpr(Context)) { 4173 if (!llvm::is_contained( 4174 {llvm::AtomicOrderingCABI::relaxed, 4175 llvm::AtomicOrderingCABI::consume, 4176 llvm::AtomicOrderingCABI::acquire, 4177 llvm::AtomicOrderingCABI::seq_cst}, 4178 (llvm::AtomicOrderingCABI)Failure->getSExtValue())) { 4179 Diag(SubExprs[3]->getBeginLoc(), 4180 diag::warn_atomic_op_has_invalid_memory_order) 4181 << /*failure=*/2 << SubExprs[3]->getSourceRange(); 4182 } 4183 } 4184 } 4185 } 4186 4187 if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { 4188 auto *Scope = Args[Args.size() - 1]; 4189 if (std::optional<llvm::APSInt> Result = 4190 Scope->getIntegerConstantExpr(Context)) { 4191 if (!ScopeModel->isValid(Result->getZExtValue())) 4192 Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) 4193 << Scope->getSourceRange(); 4194 } 4195 SubExprs.push_back(Scope); 4196 } 4197 4198 AtomicExpr *AE = new (Context) 4199 AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc); 4200 4201 if ((Op == AtomicExpr::AO__c11_atomic_load || 4202 Op == AtomicExpr::AO__c11_atomic_store || 4203 Op == AtomicExpr::AO__opencl_atomic_load || 4204 Op == AtomicExpr::AO__hip_atomic_load || 4205 Op == AtomicExpr::AO__opencl_atomic_store || 4206 Op == AtomicExpr::AO__hip_atomic_store) && 4207 Context.AtomicUsesUnsupportedLibcall(AE)) 4208 Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib) 4209 << ((Op == AtomicExpr::AO__c11_atomic_load || 4210 Op == AtomicExpr::AO__opencl_atomic_load || 4211 Op == AtomicExpr::AO__hip_atomic_load) 4212 ? 0 4213 : 1); 4214 4215 if (ValType->isBitIntType()) { 4216 Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_bit_int_prohibit); 4217 return ExprError(); 4218 } 4219 4220 return AE; 4221 } 4222 4223 /// checkBuiltinArgument - Given a call to a builtin function, perform 4224 /// normal type-checking on the given argument, updating the call in 4225 /// place. This is useful when a builtin function requires custom 4226 /// type-checking for some of its arguments but not necessarily all of 4227 /// them. 4228 /// 4229 /// Returns true on error. 4230 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { 4231 FunctionDecl *Fn = E->getDirectCallee(); 4232 assert(Fn && "builtin call without direct callee!"); 4233 4234 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); 4235 InitializedEntity Entity = 4236 InitializedEntity::InitializeParameter(S.Context, Param); 4237 4238 ExprResult Arg = E->getArg(ArgIndex); 4239 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 4240 if (Arg.isInvalid()) 4241 return true; 4242 4243 E->setArg(ArgIndex, Arg.get()); 4244 return false; 4245 } 4246 4247 ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) { 4248 CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get()); 4249 Expr *Callee = TheCall->getCallee(); 4250 DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts()); 4251 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4252 4253 // Ensure that we have at least one argument to do type inference from. 4254 if (TheCall->getNumArgs() < 1) { 4255 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4256 << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 4257 << Callee->getSourceRange(); 4258 return ExprError(); 4259 } 4260 4261 // Inspect the first argument of the atomic builtin. This should always be 4262 // a pointer type, whose element is an integral scalar or pointer type. 4263 // Because it is a pointer type, we don't have to worry about any implicit 4264 // casts here. 4265 // FIXME: We don't allow floating point scalars as input. 4266 Expr *FirstArg = TheCall->getArg(0); 4267 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); 4268 if (FirstArgResult.isInvalid()) 4269 return ExprError(); 4270 FirstArg = FirstArgResult.get(); 4271 TheCall->setArg(0, FirstArg); 4272 4273 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); 4274 if (!pointerType) { 4275 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) 4276 << FirstArg->getType() << 0 << FirstArg->getSourceRange(); 4277 return ExprError(); 4278 } 4279 4280 QualType ValType = pointerType->getPointeeType(); 4281 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4282 !ValType->isBlockPointerType()) { 4283 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr) 4284 << FirstArg->getType() << 0 << FirstArg->getSourceRange(); 4285 return ExprError(); 4286 } 4287 4288 if (ValType.isConstQualified()) { 4289 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const) 4290 << FirstArg->getType() << FirstArg->getSourceRange(); 4291 return ExprError(); 4292 } 4293 4294 switch (ValType.getObjCLifetime()) { 4295 case Qualifiers::OCL_None: 4296 case Qualifiers::OCL_ExplicitNone: 4297 // okay 4298 break; 4299 4300 case Qualifiers::OCL_Weak: 4301 case Qualifiers::OCL_Strong: 4302 case Qualifiers::OCL_Autoreleasing: 4303 Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) 4304 << ValType << FirstArg->getSourceRange(); 4305 return ExprError(); 4306 } 4307 4308 // Strip any qualifiers off ValType. 4309 ValType = ValType.getUnqualifiedType(); 4310 4311 // The majority of builtins return a value, but a few have special return 4312 // types, so allow them to override appropriately below. 4313 QualType ResultType = ValType; 4314 4315 // We need to figure out which concrete builtin this maps onto. For example, 4316 // __sync_fetch_and_add with a 2 byte object turns into 4317 // __sync_fetch_and_add_2. 4318 #define BUILTIN_ROW(x) \ 4319 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ 4320 Builtin::BI##x##_8, Builtin::BI##x##_16 } 4321 4322 static const unsigned BuiltinIndices[][5] = { 4323 BUILTIN_ROW(__sync_fetch_and_add), 4324 BUILTIN_ROW(__sync_fetch_and_sub), 4325 BUILTIN_ROW(__sync_fetch_and_or), 4326 BUILTIN_ROW(__sync_fetch_and_and), 4327 BUILTIN_ROW(__sync_fetch_and_xor), 4328 BUILTIN_ROW(__sync_fetch_and_nand), 4329 4330 BUILTIN_ROW(__sync_add_and_fetch), 4331 BUILTIN_ROW(__sync_sub_and_fetch), 4332 BUILTIN_ROW(__sync_and_and_fetch), 4333 BUILTIN_ROW(__sync_or_and_fetch), 4334 BUILTIN_ROW(__sync_xor_and_fetch), 4335 BUILTIN_ROW(__sync_nand_and_fetch), 4336 4337 BUILTIN_ROW(__sync_val_compare_and_swap), 4338 BUILTIN_ROW(__sync_bool_compare_and_swap), 4339 BUILTIN_ROW(__sync_lock_test_and_set), 4340 BUILTIN_ROW(__sync_lock_release), 4341 BUILTIN_ROW(__sync_swap) 4342 }; 4343 #undef BUILTIN_ROW 4344 4345 // Determine the index of the size. 4346 unsigned SizeIndex; 4347 switch (Context.getTypeSizeInChars(ValType).getQuantity()) { 4348 case 1: SizeIndex = 0; break; 4349 case 2: SizeIndex = 1; break; 4350 case 4: SizeIndex = 2; break; 4351 case 8: SizeIndex = 3; break; 4352 case 16: SizeIndex = 4; break; 4353 default: 4354 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size) 4355 << FirstArg->getType() << FirstArg->getSourceRange(); 4356 return ExprError(); 4357 } 4358 4359 // Each of these builtins has one pointer argument, followed by some number of 4360 // values (0, 1 or 2) followed by a potentially empty varags list of stuff 4361 // that we ignore. Find out which row of BuiltinIndices to read from as well 4362 // as the number of fixed args. 4363 unsigned BuiltinID = FDecl->getBuiltinID(); 4364 unsigned BuiltinIndex, NumFixed = 1; 4365 bool WarnAboutSemanticsChange = false; 4366 switch (BuiltinID) { 4367 default: llvm_unreachable("Unknown overloaded atomic builtin!"); 4368 case Builtin::BI__sync_fetch_and_add: 4369 case Builtin::BI__sync_fetch_and_add_1: 4370 case Builtin::BI__sync_fetch_and_add_2: 4371 case Builtin::BI__sync_fetch_and_add_4: 4372 case Builtin::BI__sync_fetch_and_add_8: 4373 case Builtin::BI__sync_fetch_and_add_16: 4374 BuiltinIndex = 0; 4375 break; 4376 4377 case Builtin::BI__sync_fetch_and_sub: 4378 case Builtin::BI__sync_fetch_and_sub_1: 4379 case Builtin::BI__sync_fetch_and_sub_2: 4380 case Builtin::BI__sync_fetch_and_sub_4: 4381 case Builtin::BI__sync_fetch_and_sub_8: 4382 case Builtin::BI__sync_fetch_and_sub_16: 4383 BuiltinIndex = 1; 4384 break; 4385 4386 case Builtin::BI__sync_fetch_and_or: 4387 case Builtin::BI__sync_fetch_and_or_1: 4388 case Builtin::BI__sync_fetch_and_or_2: 4389 case Builtin::BI__sync_fetch_and_or_4: 4390 case Builtin::BI__sync_fetch_and_or_8: 4391 case Builtin::BI__sync_fetch_and_or_16: 4392 BuiltinIndex = 2; 4393 break; 4394 4395 case Builtin::BI__sync_fetch_and_and: 4396 case Builtin::BI__sync_fetch_and_and_1: 4397 case Builtin::BI__sync_fetch_and_and_2: 4398 case Builtin::BI__sync_fetch_and_and_4: 4399 case Builtin::BI__sync_fetch_and_and_8: 4400 case Builtin::BI__sync_fetch_and_and_16: 4401 BuiltinIndex = 3; 4402 break; 4403 4404 case Builtin::BI__sync_fetch_and_xor: 4405 case Builtin::BI__sync_fetch_and_xor_1: 4406 case Builtin::BI__sync_fetch_and_xor_2: 4407 case Builtin::BI__sync_fetch_and_xor_4: 4408 case Builtin::BI__sync_fetch_and_xor_8: 4409 case Builtin::BI__sync_fetch_and_xor_16: 4410 BuiltinIndex = 4; 4411 break; 4412 4413 case Builtin::BI__sync_fetch_and_nand: 4414 case Builtin::BI__sync_fetch_and_nand_1: 4415 case Builtin::BI__sync_fetch_and_nand_2: 4416 case Builtin::BI__sync_fetch_and_nand_4: 4417 case Builtin::BI__sync_fetch_and_nand_8: 4418 case Builtin::BI__sync_fetch_and_nand_16: 4419 BuiltinIndex = 5; 4420 WarnAboutSemanticsChange = true; 4421 break; 4422 4423 case Builtin::BI__sync_add_and_fetch: 4424 case Builtin::BI__sync_add_and_fetch_1: 4425 case Builtin::BI__sync_add_and_fetch_2: 4426 case Builtin::BI__sync_add_and_fetch_4: 4427 case Builtin::BI__sync_add_and_fetch_8: 4428 case Builtin::BI__sync_add_and_fetch_16: 4429 BuiltinIndex = 6; 4430 break; 4431 4432 case Builtin::BI__sync_sub_and_fetch: 4433 case Builtin::BI__sync_sub_and_fetch_1: 4434 case Builtin::BI__sync_sub_and_fetch_2: 4435 case Builtin::BI__sync_sub_and_fetch_4: 4436 case Builtin::BI__sync_sub_and_fetch_8: 4437 case Builtin::BI__sync_sub_and_fetch_16: 4438 BuiltinIndex = 7; 4439 break; 4440 4441 case Builtin::BI__sync_and_and_fetch: 4442 case Builtin::BI__sync_and_and_fetch_1: 4443 case Builtin::BI__sync_and_and_fetch_2: 4444 case Builtin::BI__sync_and_and_fetch_4: 4445 case Builtin::BI__sync_and_and_fetch_8: 4446 case Builtin::BI__sync_and_and_fetch_16: 4447 BuiltinIndex = 8; 4448 break; 4449 4450 case Builtin::BI__sync_or_and_fetch: 4451 case Builtin::BI__sync_or_and_fetch_1: 4452 case Builtin::BI__sync_or_and_fetch_2: 4453 case Builtin::BI__sync_or_and_fetch_4: 4454 case Builtin::BI__sync_or_and_fetch_8: 4455 case Builtin::BI__sync_or_and_fetch_16: 4456 BuiltinIndex = 9; 4457 break; 4458 4459 case Builtin::BI__sync_xor_and_fetch: 4460 case Builtin::BI__sync_xor_and_fetch_1: 4461 case Builtin::BI__sync_xor_and_fetch_2: 4462 case Builtin::BI__sync_xor_and_fetch_4: 4463 case Builtin::BI__sync_xor_and_fetch_8: 4464 case Builtin::BI__sync_xor_and_fetch_16: 4465 BuiltinIndex = 10; 4466 break; 4467 4468 case Builtin::BI__sync_nand_and_fetch: 4469 case Builtin::BI__sync_nand_and_fetch_1: 4470 case Builtin::BI__sync_nand_and_fetch_2: 4471 case Builtin::BI__sync_nand_and_fetch_4: 4472 case Builtin::BI__sync_nand_and_fetch_8: 4473 case Builtin::BI__sync_nand_and_fetch_16: 4474 BuiltinIndex = 11; 4475 WarnAboutSemanticsChange = true; 4476 break; 4477 4478 case Builtin::BI__sync_val_compare_and_swap: 4479 case Builtin::BI__sync_val_compare_and_swap_1: 4480 case Builtin::BI__sync_val_compare_and_swap_2: 4481 case Builtin::BI__sync_val_compare_and_swap_4: 4482 case Builtin::BI__sync_val_compare_and_swap_8: 4483 case Builtin::BI__sync_val_compare_and_swap_16: 4484 BuiltinIndex = 12; 4485 NumFixed = 2; 4486 break; 4487 4488 case Builtin::BI__sync_bool_compare_and_swap: 4489 case Builtin::BI__sync_bool_compare_and_swap_1: 4490 case Builtin::BI__sync_bool_compare_and_swap_2: 4491 case Builtin::BI__sync_bool_compare_and_swap_4: 4492 case Builtin::BI__sync_bool_compare_and_swap_8: 4493 case Builtin::BI__sync_bool_compare_and_swap_16: 4494 BuiltinIndex = 13; 4495 NumFixed = 2; 4496 ResultType = Context.BoolTy; 4497 break; 4498 4499 case Builtin::BI__sync_lock_test_and_set: 4500 case Builtin::BI__sync_lock_test_and_set_1: 4501 case Builtin::BI__sync_lock_test_and_set_2: 4502 case Builtin::BI__sync_lock_test_and_set_4: 4503 case Builtin::BI__sync_lock_test_and_set_8: 4504 case Builtin::BI__sync_lock_test_and_set_16: 4505 BuiltinIndex = 14; 4506 break; 4507 4508 case Builtin::BI__sync_lock_release: 4509 case Builtin::BI__sync_lock_release_1: 4510 case Builtin::BI__sync_lock_release_2: 4511 case Builtin::BI__sync_lock_release_4: 4512 case Builtin::BI__sync_lock_release_8: 4513 case Builtin::BI__sync_lock_release_16: 4514 BuiltinIndex = 15; 4515 NumFixed = 0; 4516 ResultType = Context.VoidTy; 4517 break; 4518 4519 case Builtin::BI__sync_swap: 4520 case Builtin::BI__sync_swap_1: 4521 case Builtin::BI__sync_swap_2: 4522 case Builtin::BI__sync_swap_4: 4523 case Builtin::BI__sync_swap_8: 4524 case Builtin::BI__sync_swap_16: 4525 BuiltinIndex = 16; 4526 break; 4527 } 4528 4529 // Now that we know how many fixed arguments we expect, first check that we 4530 // have at least that many. 4531 if (TheCall->getNumArgs() < 1+NumFixed) { 4532 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4533 << 0 << 1 + NumFixed << TheCall->getNumArgs() << /*is non object*/ 0 4534 << Callee->getSourceRange(); 4535 return ExprError(); 4536 } 4537 4538 Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst) 4539 << Callee->getSourceRange(); 4540 4541 if (WarnAboutSemanticsChange) { 4542 Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change) 4543 << Callee->getSourceRange(); 4544 } 4545 4546 // Get the decl for the concrete builtin from this, we can tell what the 4547 // concrete integer type we should convert to is. 4548 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; 4549 StringRef NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID); 4550 FunctionDecl *NewBuiltinDecl; 4551 if (NewBuiltinID == BuiltinID) 4552 NewBuiltinDecl = FDecl; 4553 else { 4554 // Perform builtin lookup to avoid redeclaring it. 4555 DeclarationName DN(&Context.Idents.get(NewBuiltinName)); 4556 LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName); 4557 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); 4558 assert(Res.getFoundDecl()); 4559 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); 4560 if (!NewBuiltinDecl) 4561 return ExprError(); 4562 } 4563 4564 // The first argument --- the pointer --- has a fixed type; we 4565 // deduce the types of the rest of the arguments accordingly. Walk 4566 // the remaining arguments, converting them to the deduced value type. 4567 for (unsigned i = 0; i != NumFixed; ++i) { 4568 ExprResult Arg = TheCall->getArg(i+1); 4569 4570 // GCC does an implicit conversion to the pointer or integer ValType. This 4571 // can fail in some cases (1i -> int**), check for this error case now. 4572 // Initialize the argument. 4573 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 4574 ValType, /*consume*/ false); 4575 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4576 if (Arg.isInvalid()) 4577 return ExprError(); 4578 4579 // Okay, we have something that *can* be converted to the right type. Check 4580 // to see if there is a potentially weird extension going on here. This can 4581 // happen when you do an atomic operation on something like an char* and 4582 // pass in 42. The 42 gets converted to char. This is even more strange 4583 // for things like 45.123 -> char, etc. 4584 // FIXME: Do this check. 4585 TheCall->setArg(i+1, Arg.get()); 4586 } 4587 4588 // Create a new DeclRefExpr to refer to the new decl. 4589 DeclRefExpr *NewDRE = DeclRefExpr::Create( 4590 Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl, 4591 /*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy, 4592 DRE->getValueKind(), nullptr, nullptr, DRE->isNonOdrUse()); 4593 4594 // Set the callee in the CallExpr. 4595 // FIXME: This loses syntactic information. 4596 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); 4597 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, 4598 CK_BuiltinFnToFnPtr); 4599 TheCall->setCallee(PromotedCall.get()); 4600 4601 // Change the result type of the call to match the original value type. This 4602 // is arbitrary, but the codegen for these builtins ins design to handle it 4603 // gracefully. 4604 TheCall->setType(ResultType); 4605 4606 // Prohibit problematic uses of bit-precise integer types with atomic 4607 // builtins. The arguments would have already been converted to the first 4608 // argument's type, so only need to check the first argument. 4609 const auto *BitIntValType = ValType->getAs<BitIntType>(); 4610 if (BitIntValType && !llvm::isPowerOf2_64(BitIntValType->getNumBits())) { 4611 Diag(FirstArg->getExprLoc(), diag::err_atomic_builtin_ext_int_size); 4612 return ExprError(); 4613 } 4614 4615 return TheCallResult; 4616 } 4617 4618 ExprResult Sema::BuiltinNontemporalOverloaded(ExprResult TheCallResult) { 4619 CallExpr *TheCall = (CallExpr *)TheCallResult.get(); 4620 DeclRefExpr *DRE = 4621 cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 4622 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4623 unsigned BuiltinID = FDecl->getBuiltinID(); 4624 assert((BuiltinID == Builtin::BI__builtin_nontemporal_store || 4625 BuiltinID == Builtin::BI__builtin_nontemporal_load) && 4626 "Unexpected nontemporal load/store builtin!"); 4627 bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store; 4628 unsigned numArgs = isStore ? 2 : 1; 4629 4630 // Ensure that we have the proper number of arguments. 4631 if (checkArgCount(TheCall, numArgs)) 4632 return ExprError(); 4633 4634 // Inspect the last argument of the nontemporal builtin. This should always 4635 // be a pointer type, from which we imply the type of the memory access. 4636 // Because it is a pointer type, we don't have to worry about any implicit 4637 // casts here. 4638 Expr *PointerArg = TheCall->getArg(numArgs - 1); 4639 ExprResult PointerArgResult = 4640 DefaultFunctionArrayLvalueConversion(PointerArg); 4641 4642 if (PointerArgResult.isInvalid()) 4643 return ExprError(); 4644 PointerArg = PointerArgResult.get(); 4645 TheCall->setArg(numArgs - 1, PointerArg); 4646 4647 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); 4648 if (!pointerType) { 4649 Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer) 4650 << PointerArg->getType() << PointerArg->getSourceRange(); 4651 return ExprError(); 4652 } 4653 4654 QualType ValType = pointerType->getPointeeType(); 4655 4656 // Strip any qualifiers off ValType. 4657 ValType = ValType.getUnqualifiedType(); 4658 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4659 !ValType->isBlockPointerType() && !ValType->isFloatingType() && 4660 !ValType->isVectorType()) { 4661 Diag(DRE->getBeginLoc(), 4662 diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) 4663 << PointerArg->getType() << PointerArg->getSourceRange(); 4664 return ExprError(); 4665 } 4666 4667 if (!isStore) { 4668 TheCall->setType(ValType); 4669 return TheCallResult; 4670 } 4671 4672 ExprResult ValArg = TheCall->getArg(0); 4673 InitializedEntity Entity = InitializedEntity::InitializeParameter( 4674 Context, ValType, /*consume*/ false); 4675 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); 4676 if (ValArg.isInvalid()) 4677 return ExprError(); 4678 4679 TheCall->setArg(0, ValArg.get()); 4680 TheCall->setType(Context.VoidTy); 4681 return TheCallResult; 4682 } 4683 4684 /// CheckObjCString - Checks that the format string argument to the os_log() 4685 /// and os_trace() functions is correct, and converts it to const char *. 4686 ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) { 4687 Arg = Arg->IgnoreParenCasts(); 4688 auto *Literal = dyn_cast<StringLiteral>(Arg); 4689 if (!Literal) { 4690 if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) { 4691 Literal = ObjcLiteral->getString(); 4692 } 4693 } 4694 4695 if (!Literal || (!Literal->isOrdinary() && !Literal->isUTF8())) { 4696 return ExprError( 4697 Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant) 4698 << Arg->getSourceRange()); 4699 } 4700 4701 ExprResult Result(Literal); 4702 QualType ResultTy = Context.getPointerType(Context.CharTy.withConst()); 4703 InitializedEntity Entity = 4704 InitializedEntity::InitializeParameter(Context, ResultTy, false); 4705 Result = PerformCopyInitialization(Entity, SourceLocation(), Result); 4706 return Result; 4707 } 4708 4709 /// Check that the user is calling the appropriate va_start builtin for the 4710 /// target and calling convention. 4711 static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) { 4712 const llvm::Triple &TT = S.Context.getTargetInfo().getTriple(); 4713 bool IsX64 = TT.getArch() == llvm::Triple::x86_64; 4714 bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 || 4715 TT.getArch() == llvm::Triple::aarch64_32); 4716 bool IsWindows = TT.isOSWindows(); 4717 bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start; 4718 if (IsX64 || IsAArch64) { 4719 CallingConv CC = CC_C; 4720 if (const FunctionDecl *FD = S.getCurFunctionDecl()) 4721 CC = FD->getType()->castAs<FunctionType>()->getCallConv(); 4722 if (IsMSVAStart) { 4723 // Don't allow this in System V ABI functions. 4724 if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64)) 4725 return S.Diag(Fn->getBeginLoc(), 4726 diag::err_ms_va_start_used_in_sysv_function); 4727 } else { 4728 // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions. 4729 // On x64 Windows, don't allow this in System V ABI functions. 4730 // (Yes, that means there's no corresponding way to support variadic 4731 // System V ABI functions on Windows.) 4732 if ((IsWindows && CC == CC_X86_64SysV) || 4733 (!IsWindows && CC == CC_Win64)) 4734 return S.Diag(Fn->getBeginLoc(), 4735 diag::err_va_start_used_in_wrong_abi_function) 4736 << !IsWindows; 4737 } 4738 return false; 4739 } 4740 4741 if (IsMSVAStart) 4742 return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only); 4743 return false; 4744 } 4745 4746 static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn, 4747 ParmVarDecl **LastParam = nullptr) { 4748 // Determine whether the current function, block, or obj-c method is variadic 4749 // and get its parameter list. 4750 bool IsVariadic = false; 4751 ArrayRef<ParmVarDecl *> Params; 4752 DeclContext *Caller = S.CurContext; 4753 if (auto *Block = dyn_cast<BlockDecl>(Caller)) { 4754 IsVariadic = Block->isVariadic(); 4755 Params = Block->parameters(); 4756 } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) { 4757 IsVariadic = FD->isVariadic(); 4758 Params = FD->parameters(); 4759 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) { 4760 IsVariadic = MD->isVariadic(); 4761 // FIXME: This isn't correct for methods (results in bogus warning). 4762 Params = MD->parameters(); 4763 } else if (isa<CapturedDecl>(Caller)) { 4764 // We don't support va_start in a CapturedDecl. 4765 S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt); 4766 return true; 4767 } else { 4768 // This must be some other declcontext that parses exprs. 4769 S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function); 4770 return true; 4771 } 4772 4773 if (!IsVariadic) { 4774 S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function); 4775 return true; 4776 } 4777 4778 if (LastParam) 4779 *LastParam = Params.empty() ? nullptr : Params.back(); 4780 4781 return false; 4782 } 4783 4784 bool Sema::BuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) { 4785 Expr *Fn = TheCall->getCallee(); 4786 4787 if (checkVAStartABI(*this, BuiltinID, Fn)) 4788 return true; 4789 4790 // In C23 mode, va_start only needs one argument. However, the builtin still 4791 // requires two arguments (which matches the behavior of the GCC builtin), 4792 // <stdarg.h> passes `0` as the second argument in C23 mode. 4793 if (checkArgCount(TheCall, 2)) 4794 return true; 4795 4796 // Type-check the first argument normally. 4797 if (checkBuiltinArgument(*this, TheCall, 0)) 4798 return true; 4799 4800 // Check that the current function is variadic, and get its last parameter. 4801 ParmVarDecl *LastParam; 4802 if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam)) 4803 return true; 4804 4805 // Verify that the second argument to the builtin is the last argument of the 4806 // current function or method. In C23 mode, if the second argument is an 4807 // integer constant expression with value 0, then we don't bother with this 4808 // check. 4809 bool SecondArgIsLastNamedArgument = false; 4810 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); 4811 if (std::optional<llvm::APSInt> Val = 4812 TheCall->getArg(1)->getIntegerConstantExpr(Context); 4813 Val && LangOpts.C23 && *Val == 0) 4814 return false; 4815 4816 // These are valid if SecondArgIsLastNamedArgument is false after the next 4817 // block. 4818 QualType Type; 4819 SourceLocation ParamLoc; 4820 bool IsCRegister = false; 4821 4822 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { 4823 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { 4824 SecondArgIsLastNamedArgument = PV == LastParam; 4825 4826 Type = PV->getType(); 4827 ParamLoc = PV->getLocation(); 4828 IsCRegister = 4829 PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus; 4830 } 4831 } 4832 4833 if (!SecondArgIsLastNamedArgument) 4834 Diag(TheCall->getArg(1)->getBeginLoc(), 4835 diag::warn_second_arg_of_va_start_not_last_named_param); 4836 else if (IsCRegister || Type->isReferenceType() || 4837 Type->isSpecificBuiltinType(BuiltinType::Float) || [=] { 4838 // Promotable integers are UB, but enumerations need a bit of 4839 // extra checking to see what their promotable type actually is. 4840 if (!Context.isPromotableIntegerType(Type)) 4841 return false; 4842 if (!Type->isEnumeralType()) 4843 return true; 4844 const EnumDecl *ED = Type->castAs<EnumType>()->getDecl(); 4845 return !(ED && 4846 Context.typesAreCompatible(ED->getPromotionType(), Type)); 4847 }()) { 4848 unsigned Reason = 0; 4849 if (Type->isReferenceType()) Reason = 1; 4850 else if (IsCRegister) Reason = 2; 4851 Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason; 4852 Diag(ParamLoc, diag::note_parameter_type) << Type; 4853 } 4854 4855 return false; 4856 } 4857 4858 bool Sema::BuiltinVAStartARMMicrosoft(CallExpr *Call) { 4859 auto IsSuitablyTypedFormatArgument = [this](const Expr *Arg) -> bool { 4860 const LangOptions &LO = getLangOpts(); 4861 4862 if (LO.CPlusPlus) 4863 return Arg->getType() 4864 .getCanonicalType() 4865 .getTypePtr() 4866 ->getPointeeType() 4867 .withoutLocalFastQualifiers() == Context.CharTy; 4868 4869 // In C, allow aliasing through `char *`, this is required for AArch64 at 4870 // least. 4871 return true; 4872 }; 4873 4874 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, 4875 // const char *named_addr); 4876 4877 Expr *Func = Call->getCallee(); 4878 4879 if (Call->getNumArgs() < 3) 4880 return Diag(Call->getEndLoc(), 4881 diag::err_typecheck_call_too_few_args_at_least) 4882 << 0 /*function call*/ << 3 << Call->getNumArgs() 4883 << /*is non object*/ 0; 4884 4885 // Type-check the first argument normally. 4886 if (checkBuiltinArgument(*this, Call, 0)) 4887 return true; 4888 4889 // Check that the current function is variadic. 4890 if (checkVAStartIsInVariadicFunction(*this, Func)) 4891 return true; 4892 4893 // __va_start on Windows does not validate the parameter qualifiers 4894 4895 const Expr *Arg1 = Call->getArg(1)->IgnoreParens(); 4896 const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr(); 4897 4898 const Expr *Arg2 = Call->getArg(2)->IgnoreParens(); 4899 const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr(); 4900 4901 const QualType &ConstCharPtrTy = 4902 Context.getPointerType(Context.CharTy.withConst()); 4903 if (!Arg1Ty->isPointerType() || !IsSuitablyTypedFormatArgument(Arg1)) 4904 Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible) 4905 << Arg1->getType() << ConstCharPtrTy << 1 /* different class */ 4906 << 0 /* qualifier difference */ 4907 << 3 /* parameter mismatch */ 4908 << 2 << Arg1->getType() << ConstCharPtrTy; 4909 4910 const QualType SizeTy = Context.getSizeType(); 4911 if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy) 4912 Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible) 4913 << Arg2->getType() << SizeTy << 1 /* different class */ 4914 << 0 /* qualifier difference */ 4915 << 3 /* parameter mismatch */ 4916 << 3 << Arg2->getType() << SizeTy; 4917 4918 return false; 4919 } 4920 4921 bool Sema::BuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID) { 4922 if (checkArgCount(TheCall, 2)) 4923 return true; 4924 4925 if (BuiltinID == Builtin::BI__builtin_isunordered && 4926 TheCall->getFPFeaturesInEffect(getLangOpts()).getNoHonorNaNs()) 4927 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4928 << 1 << 0 << TheCall->getSourceRange(); 4929 4930 ExprResult OrigArg0 = TheCall->getArg(0); 4931 ExprResult OrigArg1 = TheCall->getArg(1); 4932 4933 // Do standard promotions between the two arguments, returning their common 4934 // type. 4935 QualType Res = UsualArithmeticConversions( 4936 OrigArg0, OrigArg1, TheCall->getExprLoc(), ACK_Comparison); 4937 if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) 4938 return true; 4939 4940 // Make sure any conversions are pushed back into the call; this is 4941 // type safe since unordered compare builtins are declared as "_Bool 4942 // foo(...)". 4943 TheCall->setArg(0, OrigArg0.get()); 4944 TheCall->setArg(1, OrigArg1.get()); 4945 4946 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) 4947 return false; 4948 4949 // If the common type isn't a real floating type, then the arguments were 4950 // invalid for this operation. 4951 if (Res.isNull() || !Res->isRealFloatingType()) 4952 return Diag(OrigArg0.get()->getBeginLoc(), 4953 diag::err_typecheck_call_invalid_ordered_compare) 4954 << OrigArg0.get()->getType() << OrigArg1.get()->getType() 4955 << SourceRange(OrigArg0.get()->getBeginLoc(), 4956 OrigArg1.get()->getEndLoc()); 4957 4958 return false; 4959 } 4960 4961 bool Sema::BuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs, 4962 unsigned BuiltinID) { 4963 if (checkArgCount(TheCall, NumArgs)) 4964 return true; 4965 4966 FPOptions FPO = TheCall->getFPFeaturesInEffect(getLangOpts()); 4967 if (FPO.getNoHonorInfs() && (BuiltinID == Builtin::BI__builtin_isfinite || 4968 BuiltinID == Builtin::BI__builtin_isinf || 4969 BuiltinID == Builtin::BI__builtin_isinf_sign)) 4970 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4971 << 0 << 0 << TheCall->getSourceRange(); 4972 4973 if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan || 4974 BuiltinID == Builtin::BI__builtin_isunordered)) 4975 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4976 << 1 << 0 << TheCall->getSourceRange(); 4977 4978 bool IsFPClass = NumArgs == 2; 4979 4980 // Find out position of floating-point argument. 4981 unsigned FPArgNo = IsFPClass ? 0 : NumArgs - 1; 4982 4983 // We can count on all parameters preceding the floating-point just being int. 4984 // Try all of those. 4985 for (unsigned i = 0; i < FPArgNo; ++i) { 4986 Expr *Arg = TheCall->getArg(i); 4987 4988 if (Arg->isTypeDependent()) 4989 return false; 4990 4991 ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy, 4992 AssignmentAction::Passing); 4993 4994 if (Res.isInvalid()) 4995 return true; 4996 TheCall->setArg(i, Res.get()); 4997 } 4998 4999 Expr *OrigArg = TheCall->getArg(FPArgNo); 5000 5001 if (OrigArg->isTypeDependent()) 5002 return false; 5003 5004 // Usual Unary Conversions will convert half to float, which we want for 5005 // machines that use fp16 conversion intrinsics. Else, we wnat to leave the 5006 // type how it is, but do normal L->Rvalue conversions. 5007 if (Context.getTargetInfo().useFP16ConversionIntrinsics()) { 5008 ExprResult Res = UsualUnaryConversions(OrigArg); 5009 5010 if (!Res.isUsable()) 5011 return true; 5012 OrigArg = Res.get(); 5013 } else { 5014 ExprResult Res = DefaultFunctionArrayLvalueConversion(OrigArg); 5015 5016 if (!Res.isUsable()) 5017 return true; 5018 OrigArg = Res.get(); 5019 } 5020 TheCall->setArg(FPArgNo, OrigArg); 5021 5022 QualType VectorResultTy; 5023 QualType ElementTy = OrigArg->getType(); 5024 // TODO: When all classification function are implemented with is_fpclass, 5025 // vector argument can be supported in all of them. 5026 if (ElementTy->isVectorType() && IsFPClass) { 5027 VectorResultTy = GetSignedVectorType(ElementTy); 5028 ElementTy = ElementTy->castAs<VectorType>()->getElementType(); 5029 } 5030 5031 // This operation requires a non-_Complex floating-point number. 5032 if (!ElementTy->isRealFloatingType()) 5033 return Diag(OrigArg->getBeginLoc(), 5034 diag::err_typecheck_call_invalid_unary_fp) 5035 << OrigArg->getType() << OrigArg->getSourceRange(); 5036 5037 // __builtin_isfpclass has integer parameter that specify test mask. It is 5038 // passed in (...), so it should be analyzed completely here. 5039 if (IsFPClass) 5040 if (BuiltinConstantArgRange(TheCall, 1, 0, llvm::fcAllFlags)) 5041 return true; 5042 5043 // TODO: enable this code to all classification functions. 5044 if (IsFPClass) { 5045 QualType ResultTy; 5046 if (!VectorResultTy.isNull()) 5047 ResultTy = VectorResultTy; 5048 else 5049 ResultTy = Context.IntTy; 5050 TheCall->setType(ResultTy); 5051 } 5052 5053 return false; 5054 } 5055 5056 bool Sema::BuiltinComplex(CallExpr *TheCall) { 5057 if (checkArgCount(TheCall, 2)) 5058 return true; 5059 5060 bool Dependent = false; 5061 for (unsigned I = 0; I != 2; ++I) { 5062 Expr *Arg = TheCall->getArg(I); 5063 QualType T = Arg->getType(); 5064 if (T->isDependentType()) { 5065 Dependent = true; 5066 continue; 5067 } 5068 5069 // Despite supporting _Complex int, GCC requires a real floating point type 5070 // for the operands of __builtin_complex. 5071 if (!T->isRealFloatingType()) { 5072 return Diag(Arg->getBeginLoc(), diag::err_typecheck_call_requires_real_fp) 5073 << Arg->getType() << Arg->getSourceRange(); 5074 } 5075 5076 ExprResult Converted = DefaultLvalueConversion(Arg); 5077 if (Converted.isInvalid()) 5078 return true; 5079 TheCall->setArg(I, Converted.get()); 5080 } 5081 5082 if (Dependent) { 5083 TheCall->setType(Context.DependentTy); 5084 return false; 5085 } 5086 5087 Expr *Real = TheCall->getArg(0); 5088 Expr *Imag = TheCall->getArg(1); 5089 if (!Context.hasSameType(Real->getType(), Imag->getType())) { 5090 return Diag(Real->getBeginLoc(), 5091 diag::err_typecheck_call_different_arg_types) 5092 << Real->getType() << Imag->getType() 5093 << Real->getSourceRange() << Imag->getSourceRange(); 5094 } 5095 5096 // We don't allow _Complex _Float16 nor _Complex __fp16 as type specifiers; 5097 // don't allow this builtin to form those types either. 5098 // FIXME: Should we allow these types? 5099 if (Real->getType()->isFloat16Type()) 5100 return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) 5101 << "_Float16"; 5102 if (Real->getType()->isHalfType()) 5103 return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) 5104 << "half"; 5105 5106 TheCall->setType(Context.getComplexType(Real->getType())); 5107 return false; 5108 } 5109 5110 /// BuiltinShuffleVector - Handle __builtin_shufflevector. 5111 // This is declared to take (...), so we have to check everything. 5112 ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) { 5113 if (TheCall->getNumArgs() < 2) 5114 return ExprError(Diag(TheCall->getEndLoc(), 5115 diag::err_typecheck_call_too_few_args_at_least) 5116 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 5117 << /*is non object*/ 0 << TheCall->getSourceRange()); 5118 5119 // Determine which of the following types of shufflevector we're checking: 5120 // 1) unary, vector mask: (lhs, mask) 5121 // 2) binary, scalar mask: (lhs, rhs, index, ..., index) 5122 QualType resType = TheCall->getArg(0)->getType(); 5123 unsigned numElements = 0; 5124 5125 if (!TheCall->getArg(0)->isTypeDependent() && 5126 !TheCall->getArg(1)->isTypeDependent()) { 5127 QualType LHSType = TheCall->getArg(0)->getType(); 5128 QualType RHSType = TheCall->getArg(1)->getType(); 5129 5130 if (!LHSType->isVectorType() || !RHSType->isVectorType()) 5131 return ExprError( 5132 Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector) 5133 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false 5134 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5135 TheCall->getArg(1)->getEndLoc())); 5136 5137 numElements = LHSType->castAs<VectorType>()->getNumElements(); 5138 unsigned numResElements = TheCall->getNumArgs() - 2; 5139 5140 // Check to see if we have a call with 2 vector arguments, the unary shuffle 5141 // with mask. If so, verify that RHS is an integer vector type with the 5142 // same number of elts as lhs. 5143 if (TheCall->getNumArgs() == 2) { 5144 if (!RHSType->hasIntegerRepresentation() || 5145 RHSType->castAs<VectorType>()->getNumElements() != numElements) 5146 return ExprError(Diag(TheCall->getBeginLoc(), 5147 diag::err_vec_builtin_incompatible_vector) 5148 << TheCall->getDirectCallee() 5149 << /*isMorethantwoArgs*/ false 5150 << SourceRange(TheCall->getArg(1)->getBeginLoc(), 5151 TheCall->getArg(1)->getEndLoc())); 5152 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { 5153 return ExprError(Diag(TheCall->getBeginLoc(), 5154 diag::err_vec_builtin_incompatible_vector) 5155 << TheCall->getDirectCallee() 5156 << /*isMorethantwoArgs*/ false 5157 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5158 TheCall->getArg(1)->getEndLoc())); 5159 } else if (numElements != numResElements) { 5160 QualType eltType = LHSType->castAs<VectorType>()->getElementType(); 5161 resType = 5162 Context.getVectorType(eltType, numResElements, VectorKind::Generic); 5163 } 5164 } 5165 5166 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { 5167 if (TheCall->getArg(i)->isTypeDependent() || 5168 TheCall->getArg(i)->isValueDependent()) 5169 continue; 5170 5171 std::optional<llvm::APSInt> Result; 5172 if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(Context))) 5173 return ExprError(Diag(TheCall->getBeginLoc(), 5174 diag::err_shufflevector_nonconstant_argument) 5175 << TheCall->getArg(i)->getSourceRange()); 5176 5177 // Allow -1 which will be translated to undef in the IR. 5178 if (Result->isSigned() && Result->isAllOnes()) 5179 continue; 5180 5181 if (Result->getActiveBits() > 64 || 5182 Result->getZExtValue() >= numElements * 2) 5183 return ExprError(Diag(TheCall->getBeginLoc(), 5184 diag::err_shufflevector_argument_too_large) 5185 << TheCall->getArg(i)->getSourceRange()); 5186 } 5187 5188 SmallVector<Expr*, 32> exprs; 5189 5190 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { 5191 exprs.push_back(TheCall->getArg(i)); 5192 TheCall->setArg(i, nullptr); 5193 } 5194 5195 return new (Context) ShuffleVectorExpr(Context, exprs, resType, 5196 TheCall->getCallee()->getBeginLoc(), 5197 TheCall->getRParenLoc()); 5198 } 5199 5200 ExprResult Sema::ConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, 5201 SourceLocation BuiltinLoc, 5202 SourceLocation RParenLoc) { 5203 ExprValueKind VK = VK_PRValue; 5204 ExprObjectKind OK = OK_Ordinary; 5205 QualType DstTy = TInfo->getType(); 5206 QualType SrcTy = E->getType(); 5207 5208 if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) 5209 return ExprError(Diag(BuiltinLoc, 5210 diag::err_convertvector_non_vector) 5211 << E->getSourceRange()); 5212 if (!DstTy->isVectorType() && !DstTy->isDependentType()) 5213 return ExprError(Diag(BuiltinLoc, diag::err_builtin_non_vector_type) 5214 << "second" 5215 << "__builtin_convertvector"); 5216 5217 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { 5218 unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements(); 5219 unsigned DstElts = DstTy->castAs<VectorType>()->getNumElements(); 5220 if (SrcElts != DstElts) 5221 return ExprError(Diag(BuiltinLoc, 5222 diag::err_convertvector_incompatible_vector) 5223 << E->getSourceRange()); 5224 } 5225 5226 return new (Context) class ConvertVectorExpr(E, TInfo, DstTy, VK, OK, 5227 BuiltinLoc, RParenLoc); 5228 } 5229 5230 bool Sema::BuiltinPrefetch(CallExpr *TheCall) { 5231 unsigned NumArgs = TheCall->getNumArgs(); 5232 5233 if (NumArgs > 3) 5234 return Diag(TheCall->getEndLoc(), 5235 diag::err_typecheck_call_too_many_args_at_most) 5236 << 0 /*function call*/ << 3 << NumArgs << /*is non object*/ 0 5237 << TheCall->getSourceRange(); 5238 5239 // Argument 0 is checked for us and the remaining arguments must be 5240 // constant integers. 5241 for (unsigned i = 1; i != NumArgs; ++i) 5242 if (BuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) 5243 return true; 5244 5245 return false; 5246 } 5247 5248 bool Sema::BuiltinArithmeticFence(CallExpr *TheCall) { 5249 if (!Context.getTargetInfo().checkArithmeticFenceSupported()) 5250 return Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 5251 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5252 if (checkArgCount(TheCall, 1)) 5253 return true; 5254 Expr *Arg = TheCall->getArg(0); 5255 if (Arg->isInstantiationDependent()) 5256 return false; 5257 5258 QualType ArgTy = Arg->getType(); 5259 if (!ArgTy->hasFloatingRepresentation()) 5260 return Diag(TheCall->getEndLoc(), diag::err_typecheck_expect_flt_or_vector) 5261 << ArgTy; 5262 if (Arg->isLValue()) { 5263 ExprResult FirstArg = DefaultLvalueConversion(Arg); 5264 TheCall->setArg(0, FirstArg.get()); 5265 } 5266 TheCall->setType(TheCall->getArg(0)->getType()); 5267 return false; 5268 } 5269 5270 bool Sema::BuiltinAssume(CallExpr *TheCall) { 5271 Expr *Arg = TheCall->getArg(0); 5272 if (Arg->isInstantiationDependent()) return false; 5273 5274 if (Arg->HasSideEffects(Context)) 5275 Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects) 5276 << Arg->getSourceRange() 5277 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); 5278 5279 return false; 5280 } 5281 5282 bool Sema::BuiltinAllocaWithAlign(CallExpr *TheCall) { 5283 // The alignment must be a constant integer. 5284 Expr *Arg = TheCall->getArg(1); 5285 5286 // We can't check the value of a dependent argument. 5287 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 5288 if (const auto *UE = 5289 dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts())) 5290 if (UE->getKind() == UETT_AlignOf || 5291 UE->getKind() == UETT_PreferredAlignOf) 5292 Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof) 5293 << Arg->getSourceRange(); 5294 5295 llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context); 5296 5297 if (!Result.isPowerOf2()) 5298 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5299 << Arg->getSourceRange(); 5300 5301 if (Result < Context.getCharWidth()) 5302 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small) 5303 << (unsigned)Context.getCharWidth() << Arg->getSourceRange(); 5304 5305 if (Result > std::numeric_limits<int32_t>::max()) 5306 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big) 5307 << std::numeric_limits<int32_t>::max() << Arg->getSourceRange(); 5308 } 5309 5310 return false; 5311 } 5312 5313 bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) { 5314 if (checkArgCountRange(TheCall, 2, 3)) 5315 return true; 5316 5317 unsigned NumArgs = TheCall->getNumArgs(); 5318 Expr *FirstArg = TheCall->getArg(0); 5319 5320 { 5321 ExprResult FirstArgResult = 5322 DefaultFunctionArrayLvalueConversion(FirstArg); 5323 if (checkBuiltinArgument(*this, TheCall, 0)) 5324 return true; 5325 /// In-place updation of FirstArg by checkBuiltinArgument is ignored. 5326 TheCall->setArg(0, FirstArgResult.get()); 5327 } 5328 5329 // The alignment must be a constant integer. 5330 Expr *SecondArg = TheCall->getArg(1); 5331 5332 // We can't check the value of a dependent argument. 5333 if (!SecondArg->isValueDependent()) { 5334 llvm::APSInt Result; 5335 if (BuiltinConstantArg(TheCall, 1, Result)) 5336 return true; 5337 5338 if (!Result.isPowerOf2()) 5339 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5340 << SecondArg->getSourceRange(); 5341 5342 if (Result > Sema::MaximumAlignment) 5343 Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great) 5344 << SecondArg->getSourceRange() << Sema::MaximumAlignment; 5345 } 5346 5347 if (NumArgs > 2) { 5348 Expr *ThirdArg = TheCall->getArg(2); 5349 if (convertArgumentToType(*this, ThirdArg, Context.getSizeType())) 5350 return true; 5351 TheCall->setArg(2, ThirdArg); 5352 } 5353 5354 return false; 5355 } 5356 5357 bool Sema::BuiltinOSLogFormat(CallExpr *TheCall) { 5358 unsigned BuiltinID = 5359 cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID(); 5360 bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size; 5361 5362 unsigned NumArgs = TheCall->getNumArgs(); 5363 unsigned NumRequiredArgs = IsSizeCall ? 1 : 2; 5364 if (NumArgs < NumRequiredArgs) { 5365 return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 5366 << 0 /* function call */ << NumRequiredArgs << NumArgs 5367 << /*is non object*/ 0 << TheCall->getSourceRange(); 5368 } 5369 if (NumArgs >= NumRequiredArgs + 0x100) { 5370 return Diag(TheCall->getEndLoc(), 5371 diag::err_typecheck_call_too_many_args_at_most) 5372 << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs 5373 << /*is non object*/ 0 << TheCall->getSourceRange(); 5374 } 5375 unsigned i = 0; 5376 5377 // For formatting call, check buffer arg. 5378 if (!IsSizeCall) { 5379 ExprResult Arg(TheCall->getArg(i)); 5380 InitializedEntity Entity = InitializedEntity::InitializeParameter( 5381 Context, Context.VoidPtrTy, false); 5382 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 5383 if (Arg.isInvalid()) 5384 return true; 5385 TheCall->setArg(i, Arg.get()); 5386 i++; 5387 } 5388 5389 // Check string literal arg. 5390 unsigned FormatIdx = i; 5391 { 5392 ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i)); 5393 if (Arg.isInvalid()) 5394 return true; 5395 TheCall->setArg(i, Arg.get()); 5396 i++; 5397 } 5398 5399 // Make sure variadic args are scalar. 5400 unsigned FirstDataArg = i; 5401 while (i < NumArgs) { 5402 ExprResult Arg = DefaultVariadicArgumentPromotion( 5403 TheCall->getArg(i), VariadicFunction, nullptr); 5404 if (Arg.isInvalid()) 5405 return true; 5406 CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType()); 5407 if (ArgSize.getQuantity() >= 0x100) { 5408 return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big) 5409 << i << (int)ArgSize.getQuantity() << 0xff 5410 << TheCall->getSourceRange(); 5411 } 5412 TheCall->setArg(i, Arg.get()); 5413 i++; 5414 } 5415 5416 // Check formatting specifiers. NOTE: We're only doing this for the non-size 5417 // call to avoid duplicate diagnostics. 5418 if (!IsSizeCall) { 5419 llvm::SmallBitVector CheckedVarArgs(NumArgs, false); 5420 ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs()); 5421 bool Success = CheckFormatArguments( 5422 Args, FAPK_Variadic, FormatIdx, FirstDataArg, FST_OSLog, 5423 VariadicFunction, TheCall->getBeginLoc(), SourceRange(), 5424 CheckedVarArgs); 5425 if (!Success) 5426 return true; 5427 } 5428 5429 if (IsSizeCall) { 5430 TheCall->setType(Context.getSizeType()); 5431 } else { 5432 TheCall->setType(Context.VoidPtrTy); 5433 } 5434 return false; 5435 } 5436 5437 bool Sema::BuiltinConstantArg(CallExpr *TheCall, int ArgNum, 5438 llvm::APSInt &Result) { 5439 Expr *Arg = TheCall->getArg(ArgNum); 5440 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 5441 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 5442 5443 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; 5444 5445 std::optional<llvm::APSInt> R; 5446 if (!(R = Arg->getIntegerConstantExpr(Context))) 5447 return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) 5448 << FDecl->getDeclName() << Arg->getSourceRange(); 5449 Result = *R; 5450 return false; 5451 } 5452 5453 bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low, 5454 int High, bool RangeIsError) { 5455 if (isConstantEvaluatedContext()) 5456 return false; 5457 llvm::APSInt Result; 5458 5459 // We can't check the value of a dependent argument. 5460 Expr *Arg = TheCall->getArg(ArgNum); 5461 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5462 return false; 5463 5464 // Check constant-ness first. 5465 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5466 return true; 5467 5468 if (Result.getSExtValue() < Low || Result.getSExtValue() > High) { 5469 if (RangeIsError) 5470 return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range) 5471 << toString(Result, 10) << Low << High << Arg->getSourceRange(); 5472 else 5473 // Defer the warning until we know if the code will be emitted so that 5474 // dead code can ignore this. 5475 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 5476 PDiag(diag::warn_argument_invalid_range) 5477 << toString(Result, 10) << Low << High 5478 << Arg->getSourceRange()); 5479 } 5480 5481 return false; 5482 } 5483 5484 bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum, 5485 unsigned Num) { 5486 llvm::APSInt Result; 5487 5488 // We can't check the value of a dependent argument. 5489 Expr *Arg = TheCall->getArg(ArgNum); 5490 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5491 return false; 5492 5493 // Check constant-ness first. 5494 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5495 return true; 5496 5497 if (Result.getSExtValue() % Num != 0) 5498 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple) 5499 << Num << Arg->getSourceRange(); 5500 5501 return false; 5502 } 5503 5504 bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) { 5505 llvm::APSInt Result; 5506 5507 // We can't check the value of a dependent argument. 5508 Expr *Arg = TheCall->getArg(ArgNum); 5509 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5510 return false; 5511 5512 // Check constant-ness first. 5513 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5514 return true; 5515 5516 // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if 5517 // and only if x is a power of 2. 5518 if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0) 5519 return false; 5520 5521 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2) 5522 << Arg->getSourceRange(); 5523 } 5524 5525 static bool IsShiftedByte(llvm::APSInt Value) { 5526 if (Value.isNegative()) 5527 return false; 5528 5529 // Check if it's a shifted byte, by shifting it down 5530 while (true) { 5531 // If the value fits in the bottom byte, the check passes. 5532 if (Value < 0x100) 5533 return true; 5534 5535 // Otherwise, if the value has _any_ bits in the bottom byte, the check 5536 // fails. 5537 if ((Value & 0xFF) != 0) 5538 return false; 5539 5540 // If the bottom 8 bits are all 0, but something above that is nonzero, 5541 // then shifting the value right by 8 bits won't affect whether it's a 5542 // shifted byte or not. So do that, and go round again. 5543 Value >>= 8; 5544 } 5545 } 5546 5547 bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum, 5548 unsigned ArgBits) { 5549 llvm::APSInt Result; 5550 5551 // We can't check the value of a dependent argument. 5552 Expr *Arg = TheCall->getArg(ArgNum); 5553 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5554 return false; 5555 5556 // Check constant-ness first. 5557 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5558 return true; 5559 5560 // Truncate to the given size. 5561 Result = Result.getLoBits(ArgBits); 5562 Result.setIsUnsigned(true); 5563 5564 if (IsShiftedByte(Result)) 5565 return false; 5566 5567 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_shifted_byte) 5568 << Arg->getSourceRange(); 5569 } 5570 5571 bool Sema::BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum, 5572 unsigned ArgBits) { 5573 llvm::APSInt Result; 5574 5575 // We can't check the value of a dependent argument. 5576 Expr *Arg = TheCall->getArg(ArgNum); 5577 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5578 return false; 5579 5580 // Check constant-ness first. 5581 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5582 return true; 5583 5584 // Truncate to the given size. 5585 Result = Result.getLoBits(ArgBits); 5586 Result.setIsUnsigned(true); 5587 5588 // Check to see if it's in either of the required forms. 5589 if (IsShiftedByte(Result) || 5590 (Result > 0 && Result < 0x10000 && (Result & 0xFF) == 0xFF)) 5591 return false; 5592 5593 return Diag(TheCall->getBeginLoc(), 5594 diag::err_argument_not_shifted_byte_or_xxff) 5595 << Arg->getSourceRange(); 5596 } 5597 5598 bool Sema::BuiltinLongjmp(CallExpr *TheCall) { 5599 if (!Context.getTargetInfo().hasSjLjLowering()) 5600 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported) 5601 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5602 5603 Expr *Arg = TheCall->getArg(1); 5604 llvm::APSInt Result; 5605 5606 // TODO: This is less than ideal. Overload this to take a value. 5607 if (BuiltinConstantArg(TheCall, 1, Result)) 5608 return true; 5609 5610 if (Result != 1) 5611 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val) 5612 << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc()); 5613 5614 return false; 5615 } 5616 5617 bool Sema::BuiltinSetjmp(CallExpr *TheCall) { 5618 if (!Context.getTargetInfo().hasSjLjLowering()) 5619 return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported) 5620 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5621 return false; 5622 } 5623 5624 bool Sema::BuiltinCountedByRef(CallExpr *TheCall) { 5625 if (checkArgCount(TheCall, 1)) 5626 return true; 5627 5628 ExprResult ArgRes = UsualUnaryConversions(TheCall->getArg(0)); 5629 if (ArgRes.isInvalid()) 5630 return true; 5631 5632 // For simplicity, we support only limited expressions for the argument. 5633 // Specifically a pointer to a flexible array member:'ptr->array'. This 5634 // allows us to reject arguments with complex casting, which really shouldn't 5635 // be a huge problem. 5636 const Expr *Arg = ArgRes.get()->IgnoreParenImpCasts(); 5637 if (!isa<PointerType>(Arg->getType()) && !Arg->getType()->isArrayType()) 5638 return Diag(Arg->getBeginLoc(), 5639 diag::err_builtin_counted_by_ref_must_be_flex_array_member) 5640 << Arg->getSourceRange(); 5641 5642 if (Arg->HasSideEffects(Context)) 5643 return Diag(Arg->getBeginLoc(), 5644 diag::err_builtin_counted_by_ref_has_side_effects) 5645 << Arg->getSourceRange(); 5646 5647 if (const auto *ME = dyn_cast<MemberExpr>(Arg)) { 5648 if (!ME->isFlexibleArrayMemberLike( 5649 Context, getLangOpts().getStrictFlexArraysLevel())) 5650 return Diag(Arg->getBeginLoc(), 5651 diag::err_builtin_counted_by_ref_must_be_flex_array_member) 5652 << Arg->getSourceRange(); 5653 5654 if (auto *CATy = 5655 ME->getMemberDecl()->getType()->getAs<CountAttributedType>(); 5656 CATy && CATy->getKind() == CountAttributedType::CountedBy) { 5657 const auto *FAMDecl = cast<FieldDecl>(ME->getMemberDecl()); 5658 if (const FieldDecl *CountFD = FAMDecl->findCountedByField()) { 5659 TheCall->setType(Context.getPointerType(CountFD->getType())); 5660 return false; 5661 } 5662 } 5663 } else { 5664 return Diag(Arg->getBeginLoc(), 5665 diag::err_builtin_counted_by_ref_must_be_flex_array_member) 5666 << Arg->getSourceRange(); 5667 } 5668 5669 TheCall->setType(Context.getPointerType(Context.VoidTy)); 5670 return false; 5671 } 5672 5673 /// The result of __builtin_counted_by_ref cannot be assigned to a variable. 5674 /// It allows leaking and modification of bounds safety information. 5675 bool Sema::CheckInvalidBuiltinCountedByRef(const Expr *E, 5676 BuiltinCountedByRefKind K) { 5677 const CallExpr *CE = 5678 E ? dyn_cast<CallExpr>(E->IgnoreParenImpCasts()) : nullptr; 5679 if (!CE || CE->getBuiltinCallee() != Builtin::BI__builtin_counted_by_ref) 5680 return false; 5681 5682 switch (K) { 5683 case AssignmentKind: 5684 case InitializerKind: 5685 Diag(E->getExprLoc(), 5686 diag::err_builtin_counted_by_ref_cannot_leak_reference) 5687 << 0 << E->getSourceRange(); 5688 break; 5689 case FunctionArgKind: 5690 Diag(E->getExprLoc(), 5691 diag::err_builtin_counted_by_ref_cannot_leak_reference) 5692 << 1 << E->getSourceRange(); 5693 break; 5694 case ReturnArgKind: 5695 Diag(E->getExprLoc(), 5696 diag::err_builtin_counted_by_ref_cannot_leak_reference) 5697 << 2 << E->getSourceRange(); 5698 break; 5699 case ArraySubscriptKind: 5700 Diag(E->getExprLoc(), diag::err_builtin_counted_by_ref_invalid_use) 5701 << 0 << E->getSourceRange(); 5702 break; 5703 case BinaryExprKind: 5704 Diag(E->getExprLoc(), diag::err_builtin_counted_by_ref_invalid_use) 5705 << 1 << E->getSourceRange(); 5706 break; 5707 } 5708 5709 return true; 5710 } 5711 5712 namespace { 5713 5714 class UncoveredArgHandler { 5715 enum { Unknown = -1, AllCovered = -2 }; 5716 5717 signed FirstUncoveredArg = Unknown; 5718 SmallVector<const Expr *, 4> DiagnosticExprs; 5719 5720 public: 5721 UncoveredArgHandler() = default; 5722 5723 bool hasUncoveredArg() const { 5724 return (FirstUncoveredArg >= 0); 5725 } 5726 5727 unsigned getUncoveredArg() const { 5728 assert(hasUncoveredArg() && "no uncovered argument"); 5729 return FirstUncoveredArg; 5730 } 5731 5732 void setAllCovered() { 5733 // A string has been found with all arguments covered, so clear out 5734 // the diagnostics. 5735 DiagnosticExprs.clear(); 5736 FirstUncoveredArg = AllCovered; 5737 } 5738 5739 void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) { 5740 assert(NewFirstUncoveredArg >= 0 && "Outside range"); 5741 5742 // Don't update if a previous string covers all arguments. 5743 if (FirstUncoveredArg == AllCovered) 5744 return; 5745 5746 // UncoveredArgHandler tracks the highest uncovered argument index 5747 // and with it all the strings that match this index. 5748 if (NewFirstUncoveredArg == FirstUncoveredArg) 5749 DiagnosticExprs.push_back(StrExpr); 5750 else if (NewFirstUncoveredArg > FirstUncoveredArg) { 5751 DiagnosticExprs.clear(); 5752 DiagnosticExprs.push_back(StrExpr); 5753 FirstUncoveredArg = NewFirstUncoveredArg; 5754 } 5755 } 5756 5757 void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr); 5758 }; 5759 5760 enum StringLiteralCheckType { 5761 SLCT_NotALiteral, 5762 SLCT_UncheckedLiteral, 5763 SLCT_CheckedLiteral 5764 }; 5765 5766 } // namespace 5767 5768 static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend, 5769 BinaryOperatorKind BinOpKind, 5770 bool AddendIsRight) { 5771 unsigned BitWidth = Offset.getBitWidth(); 5772 unsigned AddendBitWidth = Addend.getBitWidth(); 5773 // There might be negative interim results. 5774 if (Addend.isUnsigned()) { 5775 Addend = Addend.zext(++AddendBitWidth); 5776 Addend.setIsSigned(true); 5777 } 5778 // Adjust the bit width of the APSInts. 5779 if (AddendBitWidth > BitWidth) { 5780 Offset = Offset.sext(AddendBitWidth); 5781 BitWidth = AddendBitWidth; 5782 } else if (BitWidth > AddendBitWidth) { 5783 Addend = Addend.sext(BitWidth); 5784 } 5785 5786 bool Ov = false; 5787 llvm::APSInt ResOffset = Offset; 5788 if (BinOpKind == BO_Add) 5789 ResOffset = Offset.sadd_ov(Addend, Ov); 5790 else { 5791 assert(AddendIsRight && BinOpKind == BO_Sub && 5792 "operator must be add or sub with addend on the right"); 5793 ResOffset = Offset.ssub_ov(Addend, Ov); 5794 } 5795 5796 // We add an offset to a pointer here so we should support an offset as big as 5797 // possible. 5798 if (Ov) { 5799 assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 && 5800 "index (intermediate) result too big"); 5801 Offset = Offset.sext(2 * BitWidth); 5802 sumOffsets(Offset, Addend, BinOpKind, AddendIsRight); 5803 return; 5804 } 5805 5806 Offset = ResOffset; 5807 } 5808 5809 namespace { 5810 5811 // This is a wrapper class around StringLiteral to support offsetted string 5812 // literals as format strings. It takes the offset into account when returning 5813 // the string and its length or the source locations to display notes correctly. 5814 class FormatStringLiteral { 5815 const StringLiteral *FExpr; 5816 int64_t Offset; 5817 5818 public: 5819 FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0) 5820 : FExpr(fexpr), Offset(Offset) {} 5821 5822 StringRef getString() const { 5823 return FExpr->getString().drop_front(Offset); 5824 } 5825 5826 unsigned getByteLength() const { 5827 return FExpr->getByteLength() - getCharByteWidth() * Offset; 5828 } 5829 5830 unsigned getLength() const { return FExpr->getLength() - Offset; } 5831 unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); } 5832 5833 StringLiteralKind getKind() const { return FExpr->getKind(); } 5834 5835 QualType getType() const { return FExpr->getType(); } 5836 5837 bool isAscii() const { return FExpr->isOrdinary(); } 5838 bool isWide() const { return FExpr->isWide(); } 5839 bool isUTF8() const { return FExpr->isUTF8(); } 5840 bool isUTF16() const { return FExpr->isUTF16(); } 5841 bool isUTF32() const { return FExpr->isUTF32(); } 5842 bool isPascal() const { return FExpr->isPascal(); } 5843 5844 SourceLocation getLocationOfByte( 5845 unsigned ByteNo, const SourceManager &SM, const LangOptions &Features, 5846 const TargetInfo &Target, unsigned *StartToken = nullptr, 5847 unsigned *StartTokenByteOffset = nullptr) const { 5848 return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target, 5849 StartToken, StartTokenByteOffset); 5850 } 5851 5852 SourceLocation getBeginLoc() const LLVM_READONLY { 5853 return FExpr->getBeginLoc().getLocWithOffset(Offset); 5854 } 5855 5856 SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); } 5857 }; 5858 5859 } // namespace 5860 5861 static void CheckFormatString( 5862 Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, 5863 ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, 5864 unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, 5865 bool inFunctionCall, Sema::VariadicCallType CallType, 5866 llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, 5867 bool IgnoreStringsWithoutSpecifiers); 5868 5869 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, 5870 const Expr *E); 5871 5872 // Determine if an expression is a string literal or constant string. 5873 // If this function returns false on the arguments to a function expecting a 5874 // format string, we will usually need to emit a warning. 5875 // True string literals are then checked by CheckFormatString. 5876 static StringLiteralCheckType 5877 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, 5878 Sema::FormatArgumentPassingKind APK, unsigned format_idx, 5879 unsigned firstDataArg, Sema::FormatStringType Type, 5880 Sema::VariadicCallType CallType, bool InFunctionCall, 5881 llvm::SmallBitVector &CheckedVarArgs, 5882 UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset, 5883 bool IgnoreStringsWithoutSpecifiers = false) { 5884 if (S.isConstantEvaluatedContext()) 5885 return SLCT_NotALiteral; 5886 tryAgain: 5887 assert(Offset.isSigned() && "invalid offset"); 5888 5889 if (E->isTypeDependent() || E->isValueDependent()) 5890 return SLCT_NotALiteral; 5891 5892 E = E->IgnoreParenCasts(); 5893 5894 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) 5895 // Technically -Wformat-nonliteral does not warn about this case. 5896 // The behavior of printf and friends in this case is implementation 5897 // dependent. Ideally if the format string cannot be null then 5898 // it should have a 'nonnull' attribute in the function prototype. 5899 return SLCT_UncheckedLiteral; 5900 5901 switch (E->getStmtClass()) { 5902 case Stmt::InitListExprClass: 5903 // Handle expressions like {"foobar"}. 5904 if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) { 5905 return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, 5906 Type, CallType, /*InFunctionCall*/ false, 5907 CheckedVarArgs, UncoveredArg, Offset, 5908 IgnoreStringsWithoutSpecifiers); 5909 } 5910 return SLCT_NotALiteral; 5911 case Stmt::BinaryConditionalOperatorClass: 5912 case Stmt::ConditionalOperatorClass: { 5913 // The expression is a literal if both sub-expressions were, and it was 5914 // completely checked only if both sub-expressions were checked. 5915 const AbstractConditionalOperator *C = 5916 cast<AbstractConditionalOperator>(E); 5917 5918 // Determine whether it is necessary to check both sub-expressions, for 5919 // example, because the condition expression is a constant that can be 5920 // evaluated at compile time. 5921 bool CheckLeft = true, CheckRight = true; 5922 5923 bool Cond; 5924 if (C->getCond()->EvaluateAsBooleanCondition( 5925 Cond, S.getASTContext(), S.isConstantEvaluatedContext())) { 5926 if (Cond) 5927 CheckRight = false; 5928 else 5929 CheckLeft = false; 5930 } 5931 5932 // We need to maintain the offsets for the right and the left hand side 5933 // separately to check if every possible indexed expression is a valid 5934 // string literal. They might have different offsets for different string 5935 // literals in the end. 5936 StringLiteralCheckType Left; 5937 if (!CheckLeft) 5938 Left = SLCT_UncheckedLiteral; 5939 else { 5940 Left = checkFormatStringExpr(S, C->getTrueExpr(), Args, APK, format_idx, 5941 firstDataArg, Type, CallType, InFunctionCall, 5942 CheckedVarArgs, UncoveredArg, Offset, 5943 IgnoreStringsWithoutSpecifiers); 5944 if (Left == SLCT_NotALiteral || !CheckRight) { 5945 return Left; 5946 } 5947 } 5948 5949 StringLiteralCheckType Right = checkFormatStringExpr( 5950 S, C->getFalseExpr(), Args, APK, format_idx, firstDataArg, Type, 5951 CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 5952 IgnoreStringsWithoutSpecifiers); 5953 5954 return (CheckLeft && Left < Right) ? Left : Right; 5955 } 5956 5957 case Stmt::ImplicitCastExprClass: 5958 E = cast<ImplicitCastExpr>(E)->getSubExpr(); 5959 goto tryAgain; 5960 5961 case Stmt::OpaqueValueExprClass: 5962 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { 5963 E = src; 5964 goto tryAgain; 5965 } 5966 return SLCT_NotALiteral; 5967 5968 case Stmt::PredefinedExprClass: 5969 // While __func__, etc., are technically not string literals, they 5970 // cannot contain format specifiers and thus are not a security 5971 // liability. 5972 return SLCT_UncheckedLiteral; 5973 5974 case Stmt::DeclRefExprClass: { 5975 const DeclRefExpr *DR = cast<DeclRefExpr>(E); 5976 5977 // As an exception, do not flag errors for variables binding to 5978 // const string literals. 5979 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 5980 bool isConstant = false; 5981 QualType T = DR->getType(); 5982 5983 if (const ArrayType *AT = S.Context.getAsArrayType(T)) { 5984 isConstant = AT->getElementType().isConstant(S.Context); 5985 } else if (const PointerType *PT = T->getAs<PointerType>()) { 5986 isConstant = T.isConstant(S.Context) && 5987 PT->getPointeeType().isConstant(S.Context); 5988 } else if (T->isObjCObjectPointerType()) { 5989 // In ObjC, there is usually no "const ObjectPointer" type, 5990 // so don't check if the pointee type is constant. 5991 isConstant = T.isConstant(S.Context); 5992 } 5993 5994 if (isConstant) { 5995 if (const Expr *Init = VD->getAnyInitializer()) { 5996 // Look through initializers like const char c[] = { "foo" } 5997 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { 5998 if (InitList->isStringLiteralInit()) 5999 Init = InitList->getInit(0)->IgnoreParenImpCasts(); 6000 } 6001 return checkFormatStringExpr( 6002 S, Init, Args, APK, format_idx, firstDataArg, Type, CallType, 6003 /*InFunctionCall*/ false, CheckedVarArgs, UncoveredArg, Offset); 6004 } 6005 } 6006 6007 // When the format argument is an argument of this function, and this 6008 // function also has the format attribute, there are several interactions 6009 // for which there shouldn't be a warning. For instance, when calling 6010 // v*printf from a function that has the printf format attribute, we 6011 // should not emit a warning about using `fmt`, even though it's not 6012 // constant, because the arguments have already been checked for the 6013 // caller of `logmessage`: 6014 // 6015 // __attribute__((format(printf, 1, 2))) 6016 // void logmessage(char const *fmt, ...) { 6017 // va_list ap; 6018 // va_start(ap, fmt); 6019 // vprintf(fmt, ap); /* do not emit a warning about "fmt" */ 6020 // ... 6021 // } 6022 // 6023 // Another interaction that we need to support is calling a variadic 6024 // format function from a format function that has fixed arguments. For 6025 // instance: 6026 // 6027 // __attribute__((format(printf, 1, 2))) 6028 // void logstring(char const *fmt, char const *str) { 6029 // printf(fmt, str); /* do not emit a warning about "fmt" */ 6030 // } 6031 // 6032 // Same (and perhaps more relatably) for the variadic template case: 6033 // 6034 // template<typename... Args> 6035 // __attribute__((format(printf, 1, 2))) 6036 // void log(const char *fmt, Args&&... args) { 6037 // printf(fmt, forward<Args>(args)...); 6038 // /* do not emit a warning about "fmt" */ 6039 // } 6040 // 6041 // Due to implementation difficulty, we only check the format, not the 6042 // format arguments, in all cases. 6043 // 6044 if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) { 6045 if (const auto *D = dyn_cast<Decl>(PV->getDeclContext())) { 6046 for (const auto *PVFormat : D->specific_attrs<FormatAttr>()) { 6047 bool IsCXXMember = false; 6048 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) 6049 IsCXXMember = MD->isInstance(); 6050 6051 bool IsVariadic = false; 6052 if (const FunctionType *FnTy = D->getFunctionType()) 6053 IsVariadic = cast<FunctionProtoType>(FnTy)->isVariadic(); 6054 else if (const auto *BD = dyn_cast<BlockDecl>(D)) 6055 IsVariadic = BD->isVariadic(); 6056 else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) 6057 IsVariadic = OMD->isVariadic(); 6058 6059 Sema::FormatStringInfo CallerFSI; 6060 if (Sema::getFormatStringInfo(PVFormat, IsCXXMember, IsVariadic, 6061 &CallerFSI)) { 6062 // We also check if the formats are compatible. 6063 // We can't pass a 'scanf' string to a 'printf' function. 6064 if (PV->getFunctionScopeIndex() == CallerFSI.FormatIdx && 6065 Type == S.GetFormatStringType(PVFormat)) { 6066 // Lastly, check that argument passing kinds transition in a 6067 // way that makes sense: 6068 // from a caller with FAPK_VAList, allow FAPK_VAList 6069 // from a caller with FAPK_Fixed, allow FAPK_Fixed 6070 // from a caller with FAPK_Fixed, allow FAPK_Variadic 6071 // from a caller with FAPK_Variadic, allow FAPK_VAList 6072 switch (combineFAPK(CallerFSI.ArgPassingKind, APK)) { 6073 case combineFAPK(Sema::FAPK_VAList, Sema::FAPK_VAList): 6074 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Fixed): 6075 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Variadic): 6076 case combineFAPK(Sema::FAPK_Variadic, Sema::FAPK_VAList): 6077 return SLCT_UncheckedLiteral; 6078 } 6079 } 6080 } 6081 } 6082 } 6083 } 6084 } 6085 6086 return SLCT_NotALiteral; 6087 } 6088 6089 case Stmt::CallExprClass: 6090 case Stmt::CXXMemberCallExprClass: { 6091 const CallExpr *CE = cast<CallExpr>(E); 6092 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { 6093 bool IsFirst = true; 6094 StringLiteralCheckType CommonResult; 6095 for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) { 6096 const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex()); 6097 StringLiteralCheckType Result = checkFormatStringExpr( 6098 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6099 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6100 IgnoreStringsWithoutSpecifiers); 6101 if (IsFirst) { 6102 CommonResult = Result; 6103 IsFirst = false; 6104 } 6105 } 6106 if (!IsFirst) 6107 return CommonResult; 6108 6109 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { 6110 unsigned BuiltinID = FD->getBuiltinID(); 6111 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || 6112 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { 6113 const Expr *Arg = CE->getArg(0); 6114 return checkFormatStringExpr( 6115 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6116 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6117 IgnoreStringsWithoutSpecifiers); 6118 } 6119 } 6120 } 6121 if (const Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) 6122 return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, 6123 Type, CallType, /*InFunctionCall*/ false, 6124 CheckedVarArgs, UncoveredArg, Offset, 6125 IgnoreStringsWithoutSpecifiers); 6126 return SLCT_NotALiteral; 6127 } 6128 case Stmt::ObjCMessageExprClass: { 6129 const auto *ME = cast<ObjCMessageExpr>(E); 6130 if (const auto *MD = ME->getMethodDecl()) { 6131 if (const auto *FA = MD->getAttr<FormatArgAttr>()) { 6132 // As a special case heuristic, if we're using the method -[NSBundle 6133 // localizedStringForKey:value:table:], ignore any key strings that lack 6134 // format specifiers. The idea is that if the key doesn't have any 6135 // format specifiers then its probably just a key to map to the 6136 // localized strings. If it does have format specifiers though, then its 6137 // likely that the text of the key is the format string in the 6138 // programmer's language, and should be checked. 6139 const ObjCInterfaceDecl *IFace; 6140 if (MD->isInstanceMethod() && (IFace = MD->getClassInterface()) && 6141 IFace->getIdentifier()->isStr("NSBundle") && 6142 MD->getSelector().isKeywordSelector( 6143 {"localizedStringForKey", "value", "table"})) { 6144 IgnoreStringsWithoutSpecifiers = true; 6145 } 6146 6147 const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex()); 6148 return checkFormatStringExpr( 6149 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6150 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6151 IgnoreStringsWithoutSpecifiers); 6152 } 6153 } 6154 6155 return SLCT_NotALiteral; 6156 } 6157 case Stmt::ObjCStringLiteralClass: 6158 case Stmt::StringLiteralClass: { 6159 const StringLiteral *StrE = nullptr; 6160 6161 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) 6162 StrE = ObjCFExpr->getString(); 6163 else 6164 StrE = cast<StringLiteral>(E); 6165 6166 if (StrE) { 6167 if (Offset.isNegative() || Offset > StrE->getLength()) { 6168 // TODO: It would be better to have an explicit warning for out of 6169 // bounds literals. 6170 return SLCT_NotALiteral; 6171 } 6172 FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue()); 6173 CheckFormatString(S, &FStr, E, Args, APK, format_idx, firstDataArg, Type, 6174 InFunctionCall, CallType, CheckedVarArgs, UncoveredArg, 6175 IgnoreStringsWithoutSpecifiers); 6176 return SLCT_CheckedLiteral; 6177 } 6178 6179 return SLCT_NotALiteral; 6180 } 6181 case Stmt::BinaryOperatorClass: { 6182 const BinaryOperator *BinOp = cast<BinaryOperator>(E); 6183 6184 // A string literal + an int offset is still a string literal. 6185 if (BinOp->isAdditiveOp()) { 6186 Expr::EvalResult LResult, RResult; 6187 6188 bool LIsInt = BinOp->getLHS()->EvaluateAsInt( 6189 LResult, S.Context, Expr::SE_NoSideEffects, 6190 S.isConstantEvaluatedContext()); 6191 bool RIsInt = BinOp->getRHS()->EvaluateAsInt( 6192 RResult, S.Context, Expr::SE_NoSideEffects, 6193 S.isConstantEvaluatedContext()); 6194 6195 if (LIsInt != RIsInt) { 6196 BinaryOperatorKind BinOpKind = BinOp->getOpcode(); 6197 6198 if (LIsInt) { 6199 if (BinOpKind == BO_Add) { 6200 sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt); 6201 E = BinOp->getRHS(); 6202 goto tryAgain; 6203 } 6204 } else { 6205 sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt); 6206 E = BinOp->getLHS(); 6207 goto tryAgain; 6208 } 6209 } 6210 } 6211 6212 return SLCT_NotALiteral; 6213 } 6214 case Stmt::UnaryOperatorClass: { 6215 const UnaryOperator *UnaOp = cast<UnaryOperator>(E); 6216 auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr()); 6217 if (UnaOp->getOpcode() == UO_AddrOf && ASE) { 6218 Expr::EvalResult IndexResult; 6219 if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context, 6220 Expr::SE_NoSideEffects, 6221 S.isConstantEvaluatedContext())) { 6222 sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add, 6223 /*RHS is int*/ true); 6224 E = ASE->getBase(); 6225 goto tryAgain; 6226 } 6227 } 6228 6229 return SLCT_NotALiteral; 6230 } 6231 6232 default: 6233 return SLCT_NotALiteral; 6234 } 6235 } 6236 6237 // If this expression can be evaluated at compile-time, 6238 // check if the result is a StringLiteral and return it 6239 // otherwise return nullptr 6240 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, 6241 const Expr *E) { 6242 Expr::EvalResult Result; 6243 if (E->EvaluateAsRValue(Result, Context) && Result.Val.isLValue()) { 6244 const auto *LVE = Result.Val.getLValueBase().dyn_cast<const Expr *>(); 6245 if (isa_and_nonnull<StringLiteral>(LVE)) 6246 return LVE; 6247 } 6248 return nullptr; 6249 } 6250 6251 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { 6252 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) 6253 .Case("scanf", FST_Scanf) 6254 .Cases("printf", "printf0", "syslog", FST_Printf) 6255 .Cases("NSString", "CFString", FST_NSString) 6256 .Case("strftime", FST_Strftime) 6257 .Case("strfmon", FST_Strfmon) 6258 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) 6259 .Case("freebsd_kprintf", FST_FreeBSDKPrintf) 6260 .Case("os_trace", FST_OSLog) 6261 .Case("os_log", FST_OSLog) 6262 .Default(FST_Unknown); 6263 } 6264 6265 bool Sema::CheckFormatArguments(const FormatAttr *Format, 6266 ArrayRef<const Expr *> Args, bool IsCXXMember, 6267 VariadicCallType CallType, SourceLocation Loc, 6268 SourceRange Range, 6269 llvm::SmallBitVector &CheckedVarArgs) { 6270 FormatStringInfo FSI; 6271 if (getFormatStringInfo(Format, IsCXXMember, CallType != VariadicDoesNotApply, 6272 &FSI)) 6273 return CheckFormatArguments(Args, FSI.ArgPassingKind, FSI.FormatIdx, 6274 FSI.FirstDataArg, GetFormatStringType(Format), 6275 CallType, Loc, Range, CheckedVarArgs); 6276 return false; 6277 } 6278 6279 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, 6280 Sema::FormatArgumentPassingKind APK, 6281 unsigned format_idx, unsigned firstDataArg, 6282 FormatStringType Type, 6283 VariadicCallType CallType, SourceLocation Loc, 6284 SourceRange Range, 6285 llvm::SmallBitVector &CheckedVarArgs) { 6286 // CHECK: printf/scanf-like function is called with no format string. 6287 if (format_idx >= Args.size()) { 6288 Diag(Loc, diag::warn_missing_format_string) << Range; 6289 return false; 6290 } 6291 6292 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); 6293 6294 // CHECK: format string is not a string literal. 6295 // 6296 // Dynamically generated format strings are difficult to 6297 // automatically vet at compile time. Requiring that format strings 6298 // are string literals: (1) permits the checking of format strings by 6299 // the compiler and thereby (2) can practically remove the source of 6300 // many format string exploits. 6301 6302 // Format string can be either ObjC string (e.g. @"%d") or 6303 // C string (e.g. "%d") 6304 // ObjC string uses the same format specifiers as C string, so we can use 6305 // the same format string checking logic for both ObjC and C strings. 6306 UncoveredArgHandler UncoveredArg; 6307 StringLiteralCheckType CT = checkFormatStringExpr( 6308 *this, OrigFormatExpr, Args, APK, format_idx, firstDataArg, Type, 6309 CallType, 6310 /*IsFunctionCall*/ true, CheckedVarArgs, UncoveredArg, 6311 /*no string offset*/ llvm::APSInt(64, false) = 0); 6312 6313 // Generate a diagnostic where an uncovered argument is detected. 6314 if (UncoveredArg.hasUncoveredArg()) { 6315 unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg; 6316 assert(ArgIdx < Args.size() && "ArgIdx outside bounds"); 6317 UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]); 6318 } 6319 6320 if (CT != SLCT_NotALiteral) 6321 // Literal format string found, check done! 6322 return CT == SLCT_CheckedLiteral; 6323 6324 // Strftime is particular as it always uses a single 'time' argument, 6325 // so it is safe to pass a non-literal string. 6326 if (Type == FST_Strftime) 6327 return false; 6328 6329 // Do not emit diag when the string param is a macro expansion and the 6330 // format is either NSString or CFString. This is a hack to prevent 6331 // diag when using the NSLocalizedString and CFCopyLocalizedString macros 6332 // which are usually used in place of NS and CF string literals. 6333 SourceLocation FormatLoc = Args[format_idx]->getBeginLoc(); 6334 if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc)) 6335 return false; 6336 6337 // If there are no arguments specified, warn with -Wformat-security, otherwise 6338 // warn only with -Wformat-nonliteral. 6339 if (Args.size() == firstDataArg) { 6340 Diag(FormatLoc, diag::warn_format_nonliteral_noargs) 6341 << OrigFormatExpr->getSourceRange(); 6342 switch (Type) { 6343 default: 6344 break; 6345 case FST_Kprintf: 6346 case FST_FreeBSDKPrintf: 6347 case FST_Printf: 6348 case FST_Syslog: 6349 Diag(FormatLoc, diag::note_format_security_fixit) 6350 << FixItHint::CreateInsertion(FormatLoc, "\"%s\", "); 6351 break; 6352 case FST_NSString: 6353 Diag(FormatLoc, diag::note_format_security_fixit) 6354 << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", "); 6355 break; 6356 } 6357 } else { 6358 Diag(FormatLoc, diag::warn_format_nonliteral) 6359 << OrigFormatExpr->getSourceRange(); 6360 } 6361 return false; 6362 } 6363 6364 namespace { 6365 6366 class CheckFormatHandler : public analyze_format_string::FormatStringHandler { 6367 protected: 6368 Sema &S; 6369 const FormatStringLiteral *FExpr; 6370 const Expr *OrigFormatExpr; 6371 const Sema::FormatStringType FSType; 6372 const unsigned FirstDataArg; 6373 const unsigned NumDataArgs; 6374 const char *Beg; // Start of format string. 6375 const Sema::FormatArgumentPassingKind ArgPassingKind; 6376 ArrayRef<const Expr *> Args; 6377 unsigned FormatIdx; 6378 llvm::SmallBitVector CoveredArgs; 6379 bool usesPositionalArgs = false; 6380 bool atFirstArg = true; 6381 bool inFunctionCall; 6382 Sema::VariadicCallType CallType; 6383 llvm::SmallBitVector &CheckedVarArgs; 6384 UncoveredArgHandler &UncoveredArg; 6385 6386 public: 6387 CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr, 6388 const Expr *origFormatExpr, 6389 const Sema::FormatStringType type, unsigned firstDataArg, 6390 unsigned numDataArgs, const char *beg, 6391 Sema::FormatArgumentPassingKind APK, 6392 ArrayRef<const Expr *> Args, unsigned formatIdx, 6393 bool inFunctionCall, Sema::VariadicCallType callType, 6394 llvm::SmallBitVector &CheckedVarArgs, 6395 UncoveredArgHandler &UncoveredArg) 6396 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type), 6397 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg), 6398 ArgPassingKind(APK), Args(Args), FormatIdx(formatIdx), 6399 inFunctionCall(inFunctionCall), CallType(callType), 6400 CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) { 6401 CoveredArgs.resize(numDataArgs); 6402 CoveredArgs.reset(); 6403 } 6404 6405 void DoneProcessing(); 6406 6407 void HandleIncompleteSpecifier(const char *startSpecifier, 6408 unsigned specifierLen) override; 6409 6410 void HandleInvalidLengthModifier( 6411 const analyze_format_string::FormatSpecifier &FS, 6412 const analyze_format_string::ConversionSpecifier &CS, 6413 const char *startSpecifier, unsigned specifierLen, 6414 unsigned DiagID); 6415 6416 void HandleNonStandardLengthModifier( 6417 const analyze_format_string::FormatSpecifier &FS, 6418 const char *startSpecifier, unsigned specifierLen); 6419 6420 void HandleNonStandardConversionSpecifier( 6421 const analyze_format_string::ConversionSpecifier &CS, 6422 const char *startSpecifier, unsigned specifierLen); 6423 6424 void HandlePosition(const char *startPos, unsigned posLen) override; 6425 6426 void HandleInvalidPosition(const char *startSpecifier, 6427 unsigned specifierLen, 6428 analyze_format_string::PositionContext p) override; 6429 6430 void HandleZeroPosition(const char *startPos, unsigned posLen) override; 6431 6432 void HandleNullChar(const char *nullCharacter) override; 6433 6434 template <typename Range> 6435 static void 6436 EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr, 6437 const PartialDiagnostic &PDiag, SourceLocation StringLoc, 6438 bool IsStringLocation, Range StringRange, 6439 ArrayRef<FixItHint> Fixit = {}); 6440 6441 protected: 6442 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, 6443 const char *startSpec, 6444 unsigned specifierLen, 6445 const char *csStart, unsigned csLen); 6446 6447 void HandlePositionalNonpositionalArgs(SourceLocation Loc, 6448 const char *startSpec, 6449 unsigned specifierLen); 6450 6451 SourceRange getFormatStringRange(); 6452 CharSourceRange getSpecifierRange(const char *startSpecifier, 6453 unsigned specifierLen); 6454 SourceLocation getLocationOfByte(const char *x); 6455 6456 const Expr *getDataArg(unsigned i) const; 6457 6458 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, 6459 const analyze_format_string::ConversionSpecifier &CS, 6460 const char *startSpecifier, unsigned specifierLen, 6461 unsigned argIndex); 6462 6463 template <typename Range> 6464 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, 6465 bool IsStringLocation, Range StringRange, 6466 ArrayRef<FixItHint> Fixit = {}); 6467 }; 6468 6469 } // namespace 6470 6471 SourceRange CheckFormatHandler::getFormatStringRange() { 6472 return OrigFormatExpr->getSourceRange(); 6473 } 6474 6475 CharSourceRange CheckFormatHandler:: 6476 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { 6477 SourceLocation Start = getLocationOfByte(startSpecifier); 6478 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); 6479 6480 // Advance the end SourceLocation by one due to half-open ranges. 6481 End = End.getLocWithOffset(1); 6482 6483 return CharSourceRange::getCharRange(Start, End); 6484 } 6485 6486 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { 6487 return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(), 6488 S.getLangOpts(), S.Context.getTargetInfo()); 6489 } 6490 6491 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, 6492 unsigned specifierLen){ 6493 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), 6494 getLocationOfByte(startSpecifier), 6495 /*IsStringLocation*/true, 6496 getSpecifierRange(startSpecifier, specifierLen)); 6497 } 6498 6499 void CheckFormatHandler::HandleInvalidLengthModifier( 6500 const analyze_format_string::FormatSpecifier &FS, 6501 const analyze_format_string::ConversionSpecifier &CS, 6502 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { 6503 using namespace analyze_format_string; 6504 6505 const LengthModifier &LM = FS.getLengthModifier(); 6506 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6507 6508 // See if we know how to fix this length modifier. 6509 std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6510 if (FixedLM) { 6511 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6512 getLocationOfByte(LM.getStart()), 6513 /*IsStringLocation*/true, 6514 getSpecifierRange(startSpecifier, specifierLen)); 6515 6516 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6517 << FixedLM->toString() 6518 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6519 6520 } else { 6521 FixItHint Hint; 6522 if (DiagID == diag::warn_format_nonsensical_length) 6523 Hint = FixItHint::CreateRemoval(LMRange); 6524 6525 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6526 getLocationOfByte(LM.getStart()), 6527 /*IsStringLocation*/true, 6528 getSpecifierRange(startSpecifier, specifierLen), 6529 Hint); 6530 } 6531 } 6532 6533 void CheckFormatHandler::HandleNonStandardLengthModifier( 6534 const analyze_format_string::FormatSpecifier &FS, 6535 const char *startSpecifier, unsigned specifierLen) { 6536 using namespace analyze_format_string; 6537 6538 const LengthModifier &LM = FS.getLengthModifier(); 6539 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6540 6541 // See if we know how to fix this length modifier. 6542 std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6543 if (FixedLM) { 6544 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6545 << LM.toString() << 0, 6546 getLocationOfByte(LM.getStart()), 6547 /*IsStringLocation*/true, 6548 getSpecifierRange(startSpecifier, specifierLen)); 6549 6550 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6551 << FixedLM->toString() 6552 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6553 6554 } else { 6555 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6556 << LM.toString() << 0, 6557 getLocationOfByte(LM.getStart()), 6558 /*IsStringLocation*/true, 6559 getSpecifierRange(startSpecifier, specifierLen)); 6560 } 6561 } 6562 6563 void CheckFormatHandler::HandleNonStandardConversionSpecifier( 6564 const analyze_format_string::ConversionSpecifier &CS, 6565 const char *startSpecifier, unsigned specifierLen) { 6566 using namespace analyze_format_string; 6567 6568 // See if we know how to fix this conversion specifier. 6569 std::optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); 6570 if (FixedCS) { 6571 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6572 << CS.toString() << /*conversion specifier*/1, 6573 getLocationOfByte(CS.getStart()), 6574 /*IsStringLocation*/true, 6575 getSpecifierRange(startSpecifier, specifierLen)); 6576 6577 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); 6578 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) 6579 << FixedCS->toString() 6580 << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); 6581 } else { 6582 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6583 << CS.toString() << /*conversion specifier*/1, 6584 getLocationOfByte(CS.getStart()), 6585 /*IsStringLocation*/true, 6586 getSpecifierRange(startSpecifier, specifierLen)); 6587 } 6588 } 6589 6590 void CheckFormatHandler::HandlePosition(const char *startPos, 6591 unsigned posLen) { 6592 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), 6593 getLocationOfByte(startPos), 6594 /*IsStringLocation*/true, 6595 getSpecifierRange(startPos, posLen)); 6596 } 6597 6598 void CheckFormatHandler::HandleInvalidPosition( 6599 const char *startSpecifier, unsigned specifierLen, 6600 analyze_format_string::PositionContext p) { 6601 EmitFormatDiagnostic( 6602 S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p, 6603 getLocationOfByte(startSpecifier), /*IsStringLocation*/ true, 6604 getSpecifierRange(startSpecifier, specifierLen)); 6605 } 6606 6607 void CheckFormatHandler::HandleZeroPosition(const char *startPos, 6608 unsigned posLen) { 6609 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), 6610 getLocationOfByte(startPos), 6611 /*IsStringLocation*/true, 6612 getSpecifierRange(startPos, posLen)); 6613 } 6614 6615 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { 6616 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { 6617 // The presence of a null character is likely an error. 6618 EmitFormatDiagnostic( 6619 S.PDiag(diag::warn_printf_format_string_contains_null_char), 6620 getLocationOfByte(nullCharacter), /*IsStringLocation*/true, 6621 getFormatStringRange()); 6622 } 6623 } 6624 6625 // Note that this may return NULL if there was an error parsing or building 6626 // one of the argument expressions. 6627 const Expr *CheckFormatHandler::getDataArg(unsigned i) const { 6628 return Args[FirstDataArg + i]; 6629 } 6630 6631 void CheckFormatHandler::DoneProcessing() { 6632 // Does the number of data arguments exceed the number of 6633 // format conversions in the format string? 6634 if (ArgPassingKind != Sema::FAPK_VAList) { 6635 // Find any arguments that weren't covered. 6636 CoveredArgs.flip(); 6637 signed notCoveredArg = CoveredArgs.find_first(); 6638 if (notCoveredArg >= 0) { 6639 assert((unsigned)notCoveredArg < NumDataArgs); 6640 UncoveredArg.Update(notCoveredArg, OrigFormatExpr); 6641 } else { 6642 UncoveredArg.setAllCovered(); 6643 } 6644 } 6645 } 6646 6647 void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall, 6648 const Expr *ArgExpr) { 6649 assert(hasUncoveredArg() && !DiagnosticExprs.empty() && 6650 "Invalid state"); 6651 6652 if (!ArgExpr) 6653 return; 6654 6655 SourceLocation Loc = ArgExpr->getBeginLoc(); 6656 6657 if (S.getSourceManager().isInSystemMacro(Loc)) 6658 return; 6659 6660 PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used); 6661 for (auto E : DiagnosticExprs) 6662 PDiag << E->getSourceRange(); 6663 6664 CheckFormatHandler::EmitFormatDiagnostic( 6665 S, IsFunctionCall, DiagnosticExprs[0], 6666 PDiag, Loc, /*IsStringLocation*/false, 6667 DiagnosticExprs[0]->getSourceRange()); 6668 } 6669 6670 bool 6671 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, 6672 SourceLocation Loc, 6673 const char *startSpec, 6674 unsigned specifierLen, 6675 const char *csStart, 6676 unsigned csLen) { 6677 bool keepGoing = true; 6678 if (argIndex < NumDataArgs) { 6679 // Consider the argument coverered, even though the specifier doesn't 6680 // make sense. 6681 CoveredArgs.set(argIndex); 6682 } 6683 else { 6684 // If argIndex exceeds the number of data arguments we 6685 // don't issue a warning because that is just a cascade of warnings (and 6686 // they may have intended '%%' anyway). We don't want to continue processing 6687 // the format string after this point, however, as we will like just get 6688 // gibberish when trying to match arguments. 6689 keepGoing = false; 6690 } 6691 6692 StringRef Specifier(csStart, csLen); 6693 6694 // If the specifier in non-printable, it could be the first byte of a UTF-8 6695 // sequence. In that case, print the UTF-8 code point. If not, print the byte 6696 // hex value. 6697 std::string CodePointStr; 6698 if (!llvm::sys::locale::isPrint(*csStart)) { 6699 llvm::UTF32 CodePoint; 6700 const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart); 6701 const llvm::UTF8 *E = 6702 reinterpret_cast<const llvm::UTF8 *>(csStart + csLen); 6703 llvm::ConversionResult Result = 6704 llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion); 6705 6706 if (Result != llvm::conversionOK) { 6707 unsigned char FirstChar = *csStart; 6708 CodePoint = (llvm::UTF32)FirstChar; 6709 } 6710 6711 llvm::raw_string_ostream OS(CodePointStr); 6712 if (CodePoint < 256) 6713 OS << "\\x" << llvm::format("%02x", CodePoint); 6714 else if (CodePoint <= 0xFFFF) 6715 OS << "\\u" << llvm::format("%04x", CodePoint); 6716 else 6717 OS << "\\U" << llvm::format("%08x", CodePoint); 6718 Specifier = CodePointStr; 6719 } 6720 6721 EmitFormatDiagnostic( 6722 S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc, 6723 /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen)); 6724 6725 return keepGoing; 6726 } 6727 6728 void 6729 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, 6730 const char *startSpec, 6731 unsigned specifierLen) { 6732 EmitFormatDiagnostic( 6733 S.PDiag(diag::warn_format_mix_positional_nonpositional_args), 6734 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); 6735 } 6736 6737 bool 6738 CheckFormatHandler::CheckNumArgs( 6739 const analyze_format_string::FormatSpecifier &FS, 6740 const analyze_format_string::ConversionSpecifier &CS, 6741 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { 6742 6743 if (argIndex >= NumDataArgs) { 6744 PartialDiagnostic PDiag = FS.usesPositionalArg() 6745 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) 6746 << (argIndex+1) << NumDataArgs) 6747 : S.PDiag(diag::warn_printf_insufficient_data_args); 6748 EmitFormatDiagnostic( 6749 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, 6750 getSpecifierRange(startSpecifier, specifierLen)); 6751 6752 // Since more arguments than conversion tokens are given, by extension 6753 // all arguments are covered, so mark this as so. 6754 UncoveredArg.setAllCovered(); 6755 return false; 6756 } 6757 return true; 6758 } 6759 6760 template<typename Range> 6761 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, 6762 SourceLocation Loc, 6763 bool IsStringLocation, 6764 Range StringRange, 6765 ArrayRef<FixItHint> FixIt) { 6766 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, 6767 Loc, IsStringLocation, StringRange, FixIt); 6768 } 6769 6770 /// If the format string is not within the function call, emit a note 6771 /// so that the function call and string are in diagnostic messages. 6772 /// 6773 /// \param InFunctionCall if true, the format string is within the function 6774 /// call and only one diagnostic message will be produced. Otherwise, an 6775 /// extra note will be emitted pointing to location of the format string. 6776 /// 6777 /// \param ArgumentExpr the expression that is passed as the format string 6778 /// argument in the function call. Used for getting locations when two 6779 /// diagnostics are emitted. 6780 /// 6781 /// \param PDiag the callee should already have provided any strings for the 6782 /// diagnostic message. This function only adds locations and fixits 6783 /// to diagnostics. 6784 /// 6785 /// \param Loc primary location for diagnostic. If two diagnostics are 6786 /// required, one will be at Loc and a new SourceLocation will be created for 6787 /// the other one. 6788 /// 6789 /// \param IsStringLocation if true, Loc points to the format string should be 6790 /// used for the note. Otherwise, Loc points to the argument list and will 6791 /// be used with PDiag. 6792 /// 6793 /// \param StringRange some or all of the string to highlight. This is 6794 /// templated so it can accept either a CharSourceRange or a SourceRange. 6795 /// 6796 /// \param FixIt optional fix it hint for the format string. 6797 template <typename Range> 6798 void CheckFormatHandler::EmitFormatDiagnostic( 6799 Sema &S, bool InFunctionCall, const Expr *ArgumentExpr, 6800 const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation, 6801 Range StringRange, ArrayRef<FixItHint> FixIt) { 6802 if (InFunctionCall) { 6803 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); 6804 D << StringRange; 6805 D << FixIt; 6806 } else { 6807 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) 6808 << ArgumentExpr->getSourceRange(); 6809 6810 const Sema::SemaDiagnosticBuilder &Note = 6811 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), 6812 diag::note_format_string_defined); 6813 6814 Note << StringRange; 6815 Note << FixIt; 6816 } 6817 } 6818 6819 //===--- CHECK: Printf format string checking -----------------------------===// 6820 6821 namespace { 6822 6823 class CheckPrintfHandler : public CheckFormatHandler { 6824 public: 6825 CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr, 6826 const Expr *origFormatExpr, 6827 const Sema::FormatStringType type, unsigned firstDataArg, 6828 unsigned numDataArgs, bool isObjC, const char *beg, 6829 Sema::FormatArgumentPassingKind APK, 6830 ArrayRef<const Expr *> Args, unsigned formatIdx, 6831 bool inFunctionCall, Sema::VariadicCallType CallType, 6832 llvm::SmallBitVector &CheckedVarArgs, 6833 UncoveredArgHandler &UncoveredArg) 6834 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 6835 numDataArgs, beg, APK, Args, formatIdx, 6836 inFunctionCall, CallType, CheckedVarArgs, 6837 UncoveredArg) {} 6838 6839 bool isObjCContext() const { return FSType == Sema::FST_NSString; } 6840 6841 /// Returns true if '%@' specifiers are allowed in the format string. 6842 bool allowsObjCArg() const { 6843 return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog || 6844 FSType == Sema::FST_OSTrace; 6845 } 6846 6847 bool HandleInvalidPrintfConversionSpecifier( 6848 const analyze_printf::PrintfSpecifier &FS, 6849 const char *startSpecifier, 6850 unsigned specifierLen) override; 6851 6852 void handleInvalidMaskType(StringRef MaskType) override; 6853 6854 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 6855 const char *startSpecifier, unsigned specifierLen, 6856 const TargetInfo &Target) override; 6857 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 6858 const char *StartSpecifier, 6859 unsigned SpecifierLen, 6860 const Expr *E); 6861 6862 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, 6863 const char *startSpecifier, unsigned specifierLen); 6864 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, 6865 const analyze_printf::OptionalAmount &Amt, 6866 unsigned type, 6867 const char *startSpecifier, unsigned specifierLen); 6868 void HandleFlag(const analyze_printf::PrintfSpecifier &FS, 6869 const analyze_printf::OptionalFlag &flag, 6870 const char *startSpecifier, unsigned specifierLen); 6871 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, 6872 const analyze_printf::OptionalFlag &ignoredFlag, 6873 const analyze_printf::OptionalFlag &flag, 6874 const char *startSpecifier, unsigned specifierLen); 6875 bool checkForCStrMembers(const analyze_printf::ArgType &AT, 6876 const Expr *E); 6877 6878 void HandleEmptyObjCModifierFlag(const char *startFlag, 6879 unsigned flagLen) override; 6880 6881 void HandleInvalidObjCModifierFlag(const char *startFlag, 6882 unsigned flagLen) override; 6883 6884 void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, 6885 const char *flagsEnd, 6886 const char *conversionPosition) 6887 override; 6888 }; 6889 6890 } // namespace 6891 6892 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( 6893 const analyze_printf::PrintfSpecifier &FS, 6894 const char *startSpecifier, 6895 unsigned specifierLen) { 6896 const analyze_printf::PrintfConversionSpecifier &CS = 6897 FS.getConversionSpecifier(); 6898 6899 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 6900 getLocationOfByte(CS.getStart()), 6901 startSpecifier, specifierLen, 6902 CS.getStart(), CS.getLength()); 6903 } 6904 6905 void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) { 6906 S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size); 6907 } 6908 6909 bool CheckPrintfHandler::HandleAmount( 6910 const analyze_format_string::OptionalAmount &Amt, unsigned k, 6911 const char *startSpecifier, unsigned specifierLen) { 6912 if (Amt.hasDataArgument()) { 6913 if (ArgPassingKind != Sema::FAPK_VAList) { 6914 unsigned argIndex = Amt.getArgIndex(); 6915 if (argIndex >= NumDataArgs) { 6916 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) 6917 << k, 6918 getLocationOfByte(Amt.getStart()), 6919 /*IsStringLocation*/ true, 6920 getSpecifierRange(startSpecifier, specifierLen)); 6921 // Don't do any more checking. We will just emit 6922 // spurious errors. 6923 return false; 6924 } 6925 6926 // Type check the data argument. It should be an 'int'. 6927 // Although not in conformance with C99, we also allow the argument to be 6928 // an 'unsigned int' as that is a reasonably safe case. GCC also 6929 // doesn't emit a warning for that case. 6930 CoveredArgs.set(argIndex); 6931 const Expr *Arg = getDataArg(argIndex); 6932 if (!Arg) 6933 return false; 6934 6935 QualType T = Arg->getType(); 6936 6937 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); 6938 assert(AT.isValid()); 6939 6940 if (!AT.matchesType(S.Context, T)) { 6941 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) 6942 << k << AT.getRepresentativeTypeName(S.Context) 6943 << T << Arg->getSourceRange(), 6944 getLocationOfByte(Amt.getStart()), 6945 /*IsStringLocation*/true, 6946 getSpecifierRange(startSpecifier, specifierLen)); 6947 // Don't do any more checking. We will just emit 6948 // spurious errors. 6949 return false; 6950 } 6951 } 6952 } 6953 return true; 6954 } 6955 6956 void CheckPrintfHandler::HandleInvalidAmount( 6957 const analyze_printf::PrintfSpecifier &FS, 6958 const analyze_printf::OptionalAmount &Amt, 6959 unsigned type, 6960 const char *startSpecifier, 6961 unsigned specifierLen) { 6962 const analyze_printf::PrintfConversionSpecifier &CS = 6963 FS.getConversionSpecifier(); 6964 6965 FixItHint fixit = 6966 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant 6967 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), 6968 Amt.getConstantLength())) 6969 : FixItHint(); 6970 6971 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) 6972 << type << CS.toString(), 6973 getLocationOfByte(Amt.getStart()), 6974 /*IsStringLocation*/true, 6975 getSpecifierRange(startSpecifier, specifierLen), 6976 fixit); 6977 } 6978 6979 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, 6980 const analyze_printf::OptionalFlag &flag, 6981 const char *startSpecifier, 6982 unsigned specifierLen) { 6983 // Warn about pointless flag with a fixit removal. 6984 const analyze_printf::PrintfConversionSpecifier &CS = 6985 FS.getConversionSpecifier(); 6986 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) 6987 << flag.toString() << CS.toString(), 6988 getLocationOfByte(flag.getPosition()), 6989 /*IsStringLocation*/true, 6990 getSpecifierRange(startSpecifier, specifierLen), 6991 FixItHint::CreateRemoval( 6992 getSpecifierRange(flag.getPosition(), 1))); 6993 } 6994 6995 void CheckPrintfHandler::HandleIgnoredFlag( 6996 const analyze_printf::PrintfSpecifier &FS, 6997 const analyze_printf::OptionalFlag &ignoredFlag, 6998 const analyze_printf::OptionalFlag &flag, 6999 const char *startSpecifier, 7000 unsigned specifierLen) { 7001 // Warn about ignored flag with a fixit removal. 7002 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) 7003 << ignoredFlag.toString() << flag.toString(), 7004 getLocationOfByte(ignoredFlag.getPosition()), 7005 /*IsStringLocation*/true, 7006 getSpecifierRange(startSpecifier, specifierLen), 7007 FixItHint::CreateRemoval( 7008 getSpecifierRange(ignoredFlag.getPosition(), 1))); 7009 } 7010 7011 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag, 7012 unsigned flagLen) { 7013 // Warn about an empty flag. 7014 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag), 7015 getLocationOfByte(startFlag), 7016 /*IsStringLocation*/true, 7017 getSpecifierRange(startFlag, flagLen)); 7018 } 7019 7020 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag, 7021 unsigned flagLen) { 7022 // Warn about an invalid flag. 7023 auto Range = getSpecifierRange(startFlag, flagLen); 7024 StringRef flag(startFlag, flagLen); 7025 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag, 7026 getLocationOfByte(startFlag), 7027 /*IsStringLocation*/true, 7028 Range, FixItHint::CreateRemoval(Range)); 7029 } 7030 7031 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion( 7032 const char *flagsStart, const char *flagsEnd, const char *conversionPosition) { 7033 // Warn about using '[...]' without a '@' conversion. 7034 auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1); 7035 auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion; 7036 EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1), 7037 getLocationOfByte(conversionPosition), 7038 /*IsStringLocation*/true, 7039 Range, FixItHint::CreateRemoval(Range)); 7040 } 7041 7042 // Determines if the specified is a C++ class or struct containing 7043 // a member with the specified name and kind (e.g. a CXXMethodDecl named 7044 // "c_str()"). 7045 template<typename MemberKind> 7046 static llvm::SmallPtrSet<MemberKind*, 1> 7047 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { 7048 const RecordType *RT = Ty->getAs<RecordType>(); 7049 llvm::SmallPtrSet<MemberKind*, 1> Results; 7050 7051 if (!RT) 7052 return Results; 7053 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 7054 if (!RD || !RD->getDefinition()) 7055 return Results; 7056 7057 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), 7058 Sema::LookupMemberName); 7059 R.suppressDiagnostics(); 7060 7061 // We just need to include all members of the right kind turned up by the 7062 // filter, at this point. 7063 if (S.LookupQualifiedName(R, RT->getDecl())) 7064 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 7065 NamedDecl *decl = (*I)->getUnderlyingDecl(); 7066 if (MemberKind *FK = dyn_cast<MemberKind>(decl)) 7067 Results.insert(FK); 7068 } 7069 return Results; 7070 } 7071 7072 /// Check if we could call '.c_str()' on an object. 7073 /// 7074 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't 7075 /// allow the call, or if it would be ambiguous). 7076 bool Sema::hasCStrMethod(const Expr *E) { 7077 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 7078 7079 MethodSet Results = 7080 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); 7081 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 7082 MI != ME; ++MI) 7083 if ((*MI)->getMinRequiredArguments() == 0) 7084 return true; 7085 return false; 7086 } 7087 7088 // Check if a (w)string was passed when a (w)char* was needed, and offer a 7089 // better diagnostic if so. AT is assumed to be valid. 7090 // Returns true when a c_str() conversion method is found. 7091 bool CheckPrintfHandler::checkForCStrMembers( 7092 const analyze_printf::ArgType &AT, const Expr *E) { 7093 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 7094 7095 MethodSet Results = 7096 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); 7097 7098 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 7099 MI != ME; ++MI) { 7100 const CXXMethodDecl *Method = *MI; 7101 if (Method->getMinRequiredArguments() == 0 && 7102 AT.matchesType(S.Context, Method->getReturnType())) { 7103 // FIXME: Suggest parens if the expression needs them. 7104 SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc()); 7105 S.Diag(E->getBeginLoc(), diag::note_printf_c_str) 7106 << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()"); 7107 return true; 7108 } 7109 } 7110 7111 return false; 7112 } 7113 7114 bool CheckPrintfHandler::HandlePrintfSpecifier( 7115 const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier, 7116 unsigned specifierLen, const TargetInfo &Target) { 7117 using namespace analyze_format_string; 7118 using namespace analyze_printf; 7119 7120 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); 7121 7122 if (FS.consumesDataArgument()) { 7123 if (atFirstArg) { 7124 atFirstArg = false; 7125 usesPositionalArgs = FS.usesPositionalArg(); 7126 } 7127 else if (usesPositionalArgs != FS.usesPositionalArg()) { 7128 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 7129 startSpecifier, specifierLen); 7130 return false; 7131 } 7132 } 7133 7134 // First check if the field width, precision, and conversion specifier 7135 // have matching data arguments. 7136 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, 7137 startSpecifier, specifierLen)) { 7138 return false; 7139 } 7140 7141 if (!HandleAmount(FS.getPrecision(), /* precision */ 1, 7142 startSpecifier, specifierLen)) { 7143 return false; 7144 } 7145 7146 if (!CS.consumesDataArgument()) { 7147 // FIXME: Technically specifying a precision or field width here 7148 // makes no sense. Worth issuing a warning at some point. 7149 return true; 7150 } 7151 7152 // Consume the argument. 7153 unsigned argIndex = FS.getArgIndex(); 7154 if (argIndex < NumDataArgs) { 7155 // The check to see if the argIndex is valid will come later. 7156 // We set the bit here because we may exit early from this 7157 // function if we encounter some other error. 7158 CoveredArgs.set(argIndex); 7159 } 7160 7161 // FreeBSD kernel extensions. 7162 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || 7163 CS.getKind() == ConversionSpecifier::FreeBSDDArg) { 7164 // We need at least two arguments. 7165 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) 7166 return false; 7167 7168 // Claim the second argument. 7169 CoveredArgs.set(argIndex + 1); 7170 7171 // Type check the first argument (int for %b, pointer for %D) 7172 const Expr *Ex = getDataArg(argIndex); 7173 const analyze_printf::ArgType &AT = 7174 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? 7175 ArgType(S.Context.IntTy) : ArgType::CPointerTy; 7176 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) 7177 EmitFormatDiagnostic( 7178 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7179 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() 7180 << false << Ex->getSourceRange(), 7181 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7182 getSpecifierRange(startSpecifier, specifierLen)); 7183 7184 // Type check the second argument (char * for both %b and %D) 7185 Ex = getDataArg(argIndex + 1); 7186 const analyze_printf::ArgType &AT2 = ArgType::CStrTy; 7187 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) 7188 EmitFormatDiagnostic( 7189 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7190 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() 7191 << false << Ex->getSourceRange(), 7192 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7193 getSpecifierRange(startSpecifier, specifierLen)); 7194 7195 return true; 7196 } 7197 7198 // Check for using an Objective-C specific conversion specifier 7199 // in a non-ObjC literal. 7200 if (!allowsObjCArg() && CS.isObjCArg()) { 7201 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7202 specifierLen); 7203 } 7204 7205 // %P can only be used with os_log. 7206 if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) { 7207 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7208 specifierLen); 7209 } 7210 7211 // %n is not allowed with os_log. 7212 if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) { 7213 EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg), 7214 getLocationOfByte(CS.getStart()), 7215 /*IsStringLocation*/ false, 7216 getSpecifierRange(startSpecifier, specifierLen)); 7217 7218 return true; 7219 } 7220 7221 // Only scalars are allowed for os_trace. 7222 if (FSType == Sema::FST_OSTrace && 7223 (CS.getKind() == ConversionSpecifier::PArg || 7224 CS.getKind() == ConversionSpecifier::sArg || 7225 CS.getKind() == ConversionSpecifier::ObjCObjArg)) { 7226 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7227 specifierLen); 7228 } 7229 7230 // Check for use of public/private annotation outside of os_log(). 7231 if (FSType != Sema::FST_OSLog) { 7232 if (FS.isPublic().isSet()) { 7233 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7234 << "public", 7235 getLocationOfByte(FS.isPublic().getPosition()), 7236 /*IsStringLocation*/ false, 7237 getSpecifierRange(startSpecifier, specifierLen)); 7238 } 7239 if (FS.isPrivate().isSet()) { 7240 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7241 << "private", 7242 getLocationOfByte(FS.isPrivate().getPosition()), 7243 /*IsStringLocation*/ false, 7244 getSpecifierRange(startSpecifier, specifierLen)); 7245 } 7246 } 7247 7248 const llvm::Triple &Triple = Target.getTriple(); 7249 if (CS.getKind() == ConversionSpecifier::nArg && 7250 (Triple.isAndroid() || Triple.isOSFuchsia())) { 7251 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_narg_not_supported), 7252 getLocationOfByte(CS.getStart()), 7253 /*IsStringLocation*/ false, 7254 getSpecifierRange(startSpecifier, specifierLen)); 7255 } 7256 7257 // Check for invalid use of field width 7258 if (!FS.hasValidFieldWidth()) { 7259 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, 7260 startSpecifier, specifierLen); 7261 } 7262 7263 // Check for invalid use of precision 7264 if (!FS.hasValidPrecision()) { 7265 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, 7266 startSpecifier, specifierLen); 7267 } 7268 7269 // Precision is mandatory for %P specifier. 7270 if (CS.getKind() == ConversionSpecifier::PArg && 7271 FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) { 7272 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision), 7273 getLocationOfByte(startSpecifier), 7274 /*IsStringLocation*/ false, 7275 getSpecifierRange(startSpecifier, specifierLen)); 7276 } 7277 7278 // Check each flag does not conflict with any other component. 7279 if (!FS.hasValidThousandsGroupingPrefix()) 7280 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); 7281 if (!FS.hasValidLeadingZeros()) 7282 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); 7283 if (!FS.hasValidPlusPrefix()) 7284 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); 7285 if (!FS.hasValidSpacePrefix()) 7286 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); 7287 if (!FS.hasValidAlternativeForm()) 7288 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); 7289 if (!FS.hasValidLeftJustified()) 7290 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); 7291 7292 // Check that flags are not ignored by another flag 7293 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' 7294 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), 7295 startSpecifier, specifierLen); 7296 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' 7297 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), 7298 startSpecifier, specifierLen); 7299 7300 // Check the length modifier is valid with the given conversion specifier. 7301 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7302 S.getLangOpts())) 7303 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7304 diag::warn_format_nonsensical_length); 7305 else if (!FS.hasStandardLengthModifier()) 7306 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7307 else if (!FS.hasStandardLengthConversionCombination()) 7308 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7309 diag::warn_format_non_standard_conversion_spec); 7310 7311 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7312 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7313 7314 // The remaining checks depend on the data arguments. 7315 if (ArgPassingKind == Sema::FAPK_VAList) 7316 return true; 7317 7318 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7319 return false; 7320 7321 const Expr *Arg = getDataArg(argIndex); 7322 if (!Arg) 7323 return true; 7324 7325 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); 7326 } 7327 7328 static bool requiresParensToAddCast(const Expr *E) { 7329 // FIXME: We should have a general way to reason about operator 7330 // precedence and whether parens are actually needed here. 7331 // Take care of a few common cases where they aren't. 7332 const Expr *Inside = E->IgnoreImpCasts(); 7333 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) 7334 Inside = POE->getSyntacticForm()->IgnoreImpCasts(); 7335 7336 switch (Inside->getStmtClass()) { 7337 case Stmt::ArraySubscriptExprClass: 7338 case Stmt::CallExprClass: 7339 case Stmt::CharacterLiteralClass: 7340 case Stmt::CXXBoolLiteralExprClass: 7341 case Stmt::DeclRefExprClass: 7342 case Stmt::FloatingLiteralClass: 7343 case Stmt::IntegerLiteralClass: 7344 case Stmt::MemberExprClass: 7345 case Stmt::ObjCArrayLiteralClass: 7346 case Stmt::ObjCBoolLiteralExprClass: 7347 case Stmt::ObjCBoxedExprClass: 7348 case Stmt::ObjCDictionaryLiteralClass: 7349 case Stmt::ObjCEncodeExprClass: 7350 case Stmt::ObjCIvarRefExprClass: 7351 case Stmt::ObjCMessageExprClass: 7352 case Stmt::ObjCPropertyRefExprClass: 7353 case Stmt::ObjCStringLiteralClass: 7354 case Stmt::ObjCSubscriptRefExprClass: 7355 case Stmt::ParenExprClass: 7356 case Stmt::StringLiteralClass: 7357 case Stmt::UnaryOperatorClass: 7358 return false; 7359 default: 7360 return true; 7361 } 7362 } 7363 7364 static std::pair<QualType, StringRef> 7365 shouldNotPrintDirectly(const ASTContext &Context, 7366 QualType IntendedTy, 7367 const Expr *E) { 7368 // Use a 'while' to peel off layers of typedefs. 7369 QualType TyTy = IntendedTy; 7370 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { 7371 StringRef Name = UserTy->getDecl()->getName(); 7372 QualType CastTy = llvm::StringSwitch<QualType>(Name) 7373 .Case("CFIndex", Context.getNSIntegerType()) 7374 .Case("NSInteger", Context.getNSIntegerType()) 7375 .Case("NSUInteger", Context.getNSUIntegerType()) 7376 .Case("SInt32", Context.IntTy) 7377 .Case("UInt32", Context.UnsignedIntTy) 7378 .Default(QualType()); 7379 7380 if (!CastTy.isNull()) 7381 return std::make_pair(CastTy, Name); 7382 7383 TyTy = UserTy->desugar(); 7384 } 7385 7386 // Strip parens if necessary. 7387 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 7388 return shouldNotPrintDirectly(Context, 7389 PE->getSubExpr()->getType(), 7390 PE->getSubExpr()); 7391 7392 // If this is a conditional expression, then its result type is constructed 7393 // via usual arithmetic conversions and thus there might be no necessary 7394 // typedef sugar there. Recurse to operands to check for NSInteger & 7395 // Co. usage condition. 7396 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 7397 QualType TrueTy, FalseTy; 7398 StringRef TrueName, FalseName; 7399 7400 std::tie(TrueTy, TrueName) = 7401 shouldNotPrintDirectly(Context, 7402 CO->getTrueExpr()->getType(), 7403 CO->getTrueExpr()); 7404 std::tie(FalseTy, FalseName) = 7405 shouldNotPrintDirectly(Context, 7406 CO->getFalseExpr()->getType(), 7407 CO->getFalseExpr()); 7408 7409 if (TrueTy == FalseTy) 7410 return std::make_pair(TrueTy, TrueName); 7411 else if (TrueTy.isNull()) 7412 return std::make_pair(FalseTy, FalseName); 7413 else if (FalseTy.isNull()) 7414 return std::make_pair(TrueTy, TrueName); 7415 } 7416 7417 return std::make_pair(QualType(), StringRef()); 7418 } 7419 7420 /// Return true if \p ICE is an implicit argument promotion of an arithmetic 7421 /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked 7422 /// type do not count. 7423 static bool 7424 isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) { 7425 QualType From = ICE->getSubExpr()->getType(); 7426 QualType To = ICE->getType(); 7427 // It's an integer promotion if the destination type is the promoted 7428 // source type. 7429 if (ICE->getCastKind() == CK_IntegralCast && 7430 S.Context.isPromotableIntegerType(From) && 7431 S.Context.getPromotedIntegerType(From) == To) 7432 return true; 7433 // Look through vector types, since we do default argument promotion for 7434 // those in OpenCL. 7435 if (const auto *VecTy = From->getAs<ExtVectorType>()) 7436 From = VecTy->getElementType(); 7437 if (const auto *VecTy = To->getAs<ExtVectorType>()) 7438 To = VecTy->getElementType(); 7439 // It's a floating promotion if the source type is a lower rank. 7440 return ICE->getCastKind() == CK_FloatingCast && 7441 S.Context.getFloatingTypeOrder(From, To) < 0; 7442 } 7443 7444 static analyze_format_string::ArgType::MatchKind 7445 handleFormatSignedness(analyze_format_string::ArgType::MatchKind Match, 7446 DiagnosticsEngine &Diags, SourceLocation Loc) { 7447 if (Match == analyze_format_string::ArgType::NoMatchSignedness) { 7448 Match = 7449 Diags.isIgnored( 7450 diag::warn_format_conversion_argument_type_mismatch_signedness, Loc) 7451 ? analyze_format_string::ArgType::Match 7452 : analyze_format_string::ArgType::NoMatch; 7453 } 7454 return Match; 7455 } 7456 7457 bool 7458 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 7459 const char *StartSpecifier, 7460 unsigned SpecifierLen, 7461 const Expr *E) { 7462 using namespace analyze_format_string; 7463 using namespace analyze_printf; 7464 7465 // Now type check the data expression that matches the 7466 // format specifier. 7467 const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext()); 7468 if (!AT.isValid()) 7469 return true; 7470 7471 QualType ExprTy = E->getType(); 7472 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { 7473 ExprTy = TET->getUnderlyingExpr()->getType(); 7474 } 7475 7476 // When using the format attribute in C++, you can receive a function or an 7477 // array that will necessarily decay to a pointer when passed to the final 7478 // format consumer. Apply decay before type comparison. 7479 if (ExprTy->canDecayToPointerType()) 7480 ExprTy = S.Context.getDecayedType(ExprTy); 7481 7482 // Diagnose attempts to print a boolean value as a character. Unlike other 7483 // -Wformat diagnostics, this is fine from a type perspective, but it still 7484 // doesn't make sense. 7485 if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::cArg && 7486 E->isKnownToHaveBooleanValue()) { 7487 const CharSourceRange &CSR = 7488 getSpecifierRange(StartSpecifier, SpecifierLen); 7489 SmallString<4> FSString; 7490 llvm::raw_svector_ostream os(FSString); 7491 FS.toString(os); 7492 EmitFormatDiagnostic(S.PDiag(diag::warn_format_bool_as_character) 7493 << FSString, 7494 E->getExprLoc(), false, CSR); 7495 return true; 7496 } 7497 7498 // Diagnose attempts to use '%P' with ObjC object types, which will result in 7499 // dumping raw class data (like is-a pointer), not actual data. 7500 if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::PArg && 7501 ExprTy->isObjCObjectPointerType()) { 7502 const CharSourceRange &CSR = 7503 getSpecifierRange(StartSpecifier, SpecifierLen); 7504 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_with_objc_pointer), 7505 E->getExprLoc(), false, CSR); 7506 return true; 7507 } 7508 7509 ArgType::MatchKind ImplicitMatch = ArgType::NoMatch; 7510 ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy); 7511 ArgType::MatchKind OrigMatch = Match; 7512 7513 Match = handleFormatSignedness(Match, S.getDiagnostics(), E->getExprLoc()); 7514 if (Match == ArgType::Match) 7515 return true; 7516 7517 // NoMatchPromotionTypeConfusion should be only returned in ImplictCastExpr 7518 assert(Match != ArgType::NoMatchPromotionTypeConfusion); 7519 7520 // Look through argument promotions for our error message's reported type. 7521 // This includes the integral and floating promotions, but excludes array 7522 // and function pointer decay (seeing that an argument intended to be a 7523 // string has type 'char [6]' is probably more confusing than 'char *') and 7524 // certain bitfield promotions (bitfields can be 'demoted' to a lesser type). 7525 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 7526 if (isArithmeticArgumentPromotion(S, ICE)) { 7527 E = ICE->getSubExpr(); 7528 ExprTy = E->getType(); 7529 7530 // Check if we didn't match because of an implicit cast from a 'char' 7531 // or 'short' to an 'int'. This is done because printf is a varargs 7532 // function. 7533 if (ICE->getType() == S.Context.IntTy || 7534 ICE->getType() == S.Context.UnsignedIntTy) { 7535 // All further checking is done on the subexpression 7536 ImplicitMatch = AT.matchesType(S.Context, ExprTy); 7537 if (OrigMatch == ArgType::NoMatchSignedness && 7538 ImplicitMatch != ArgType::NoMatchSignedness) 7539 // If the original match was a signedness match this match on the 7540 // implicit cast type also need to be signedness match otherwise we 7541 // might introduce new unexpected warnings from -Wformat-signedness. 7542 return true; 7543 ImplicitMatch = handleFormatSignedness( 7544 ImplicitMatch, S.getDiagnostics(), E->getExprLoc()); 7545 if (ImplicitMatch == ArgType::Match) 7546 return true; 7547 } 7548 } 7549 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { 7550 // Special case for 'a', which has type 'int' in C. 7551 // Note, however, that we do /not/ want to treat multibyte constants like 7552 // 'MooV' as characters! This form is deprecated but still exists. In 7553 // addition, don't treat expressions as of type 'char' if one byte length 7554 // modifier is provided. 7555 if (ExprTy == S.Context.IntTy && 7556 FS.getLengthModifier().getKind() != LengthModifier::AsChar) 7557 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) { 7558 ExprTy = S.Context.CharTy; 7559 // To improve check results, we consider a character literal in C 7560 // to be a 'char' rather than an 'int'. 'printf("%hd", 'a');' is 7561 // more likely a type confusion situation, so we will suggest to 7562 // use '%hhd' instead by discarding the MatchPromotion. 7563 if (Match == ArgType::MatchPromotion) 7564 Match = ArgType::NoMatch; 7565 } 7566 } 7567 if (Match == ArgType::MatchPromotion) { 7568 // WG14 N2562 only clarified promotions in *printf 7569 // For NSLog in ObjC, just preserve -Wformat behavior 7570 if (!S.getLangOpts().ObjC && 7571 ImplicitMatch != ArgType::NoMatchPromotionTypeConfusion && 7572 ImplicitMatch != ArgType::NoMatchTypeConfusion) 7573 return true; 7574 Match = ArgType::NoMatch; 7575 } 7576 if (ImplicitMatch == ArgType::NoMatchPedantic || 7577 ImplicitMatch == ArgType::NoMatchTypeConfusion) 7578 Match = ImplicitMatch; 7579 assert(Match != ArgType::MatchPromotion); 7580 7581 // Look through unscoped enums to their underlying type. 7582 bool IsEnum = false; 7583 bool IsScopedEnum = false; 7584 QualType IntendedTy = ExprTy; 7585 if (auto EnumTy = ExprTy->getAs<EnumType>()) { 7586 IntendedTy = EnumTy->getDecl()->getIntegerType(); 7587 if (EnumTy->isUnscopedEnumerationType()) { 7588 ExprTy = IntendedTy; 7589 // This controls whether we're talking about the underlying type or not, 7590 // which we only want to do when it's an unscoped enum. 7591 IsEnum = true; 7592 } else { 7593 IsScopedEnum = true; 7594 } 7595 } 7596 7597 // %C in an Objective-C context prints a unichar, not a wchar_t. 7598 // If the argument is an integer of some kind, believe the %C and suggest 7599 // a cast instead of changing the conversion specifier. 7600 if (isObjCContext() && 7601 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { 7602 if (ExprTy->isIntegralOrUnscopedEnumerationType() && 7603 !ExprTy->isCharType()) { 7604 // 'unichar' is defined as a typedef of unsigned short, but we should 7605 // prefer using the typedef if it is visible. 7606 IntendedTy = S.Context.UnsignedShortTy; 7607 7608 // While we are here, check if the value is an IntegerLiteral that happens 7609 // to be within the valid range. 7610 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { 7611 const llvm::APInt &V = IL->getValue(); 7612 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) 7613 return true; 7614 } 7615 7616 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(), 7617 Sema::LookupOrdinaryName); 7618 if (S.LookupName(Result, S.getCurScope())) { 7619 NamedDecl *ND = Result.getFoundDecl(); 7620 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) 7621 if (TD->getUnderlyingType() == IntendedTy) 7622 IntendedTy = S.Context.getTypedefType(TD); 7623 } 7624 } 7625 } 7626 7627 // Special-case some of Darwin's platform-independence types by suggesting 7628 // casts to primitive types that are known to be large enough. 7629 bool ShouldNotPrintDirectly = false; StringRef CastTyName; 7630 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { 7631 QualType CastTy; 7632 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); 7633 if (!CastTy.isNull()) { 7634 // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int 7635 // (long in ASTContext). Only complain to pedants or when they're the 7636 // underlying type of a scoped enum (which always needs a cast). 7637 if (!IsScopedEnum && 7638 (CastTyName == "NSInteger" || CastTyName == "NSUInteger") && 7639 (AT.isSizeT() || AT.isPtrdiffT()) && 7640 AT.matchesType(S.Context, CastTy)) 7641 Match = ArgType::NoMatchPedantic; 7642 IntendedTy = CastTy; 7643 ShouldNotPrintDirectly = true; 7644 } 7645 } 7646 7647 // We may be able to offer a FixItHint if it is a supported type. 7648 PrintfSpecifier fixedFS = FS; 7649 bool Success = 7650 fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext()); 7651 7652 if (Success) { 7653 // Get the fix string from the fixed format specifier 7654 SmallString<16> buf; 7655 llvm::raw_svector_ostream os(buf); 7656 fixedFS.toString(os); 7657 7658 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); 7659 7660 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) { 7661 unsigned Diag; 7662 switch (Match) { 7663 case ArgType::Match: 7664 case ArgType::MatchPromotion: 7665 case ArgType::NoMatchPromotionTypeConfusion: 7666 case ArgType::NoMatchSignedness: 7667 llvm_unreachable("expected non-matching"); 7668 case ArgType::NoMatchPedantic: 7669 Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; 7670 break; 7671 case ArgType::NoMatchTypeConfusion: 7672 Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; 7673 break; 7674 case ArgType::NoMatch: 7675 Diag = diag::warn_format_conversion_argument_type_mismatch; 7676 break; 7677 } 7678 7679 // In this case, the specifier is wrong and should be changed to match 7680 // the argument. 7681 EmitFormatDiagnostic(S.PDiag(Diag) 7682 << AT.getRepresentativeTypeName(S.Context) 7683 << IntendedTy << IsEnum << E->getSourceRange(), 7684 E->getBeginLoc(), 7685 /*IsStringLocation*/ false, SpecRange, 7686 FixItHint::CreateReplacement(SpecRange, os.str())); 7687 } else { 7688 // The canonical type for formatting this value is different from the 7689 // actual type of the expression. (This occurs, for example, with Darwin's 7690 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but 7691 // should be printed as 'long' for 64-bit compatibility.) 7692 // Rather than emitting a normal format/argument mismatch, we want to 7693 // add a cast to the recommended type (and correct the format string 7694 // if necessary). We should also do so for scoped enumerations. 7695 SmallString<16> CastBuf; 7696 llvm::raw_svector_ostream CastFix(CastBuf); 7697 CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); 7698 IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); 7699 CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); 7700 7701 SmallVector<FixItHint,4> Hints; 7702 ArgType::MatchKind IntendedMatch = AT.matchesType(S.Context, IntendedTy); 7703 IntendedMatch = handleFormatSignedness(IntendedMatch, S.getDiagnostics(), 7704 E->getExprLoc()); 7705 if ((IntendedMatch != ArgType::Match) || ShouldNotPrintDirectly) 7706 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); 7707 7708 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { 7709 // If there's already a cast present, just replace it. 7710 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); 7711 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); 7712 7713 } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) { 7714 // If the expression has high enough precedence, 7715 // just write the C-style cast. 7716 Hints.push_back( 7717 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 7718 } else { 7719 // Otherwise, add parens around the expression as well as the cast. 7720 CastFix << "("; 7721 Hints.push_back( 7722 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 7723 7724 // We don't use getLocForEndOfToken because it returns invalid source 7725 // locations for macro expansions (by design). 7726 SourceLocation EndLoc = S.SourceMgr.getSpellingLoc(E->getEndLoc()); 7727 SourceLocation After = EndLoc.getLocWithOffset( 7728 Lexer::MeasureTokenLength(EndLoc, S.SourceMgr, S.LangOpts)); 7729 Hints.push_back(FixItHint::CreateInsertion(After, ")")); 7730 } 7731 7732 if (ShouldNotPrintDirectly && !IsScopedEnum) { 7733 // The expression has a type that should not be printed directly. 7734 // We extract the name from the typedef because we don't want to show 7735 // the underlying type in the diagnostic. 7736 StringRef Name; 7737 if (const auto *TypedefTy = ExprTy->getAs<TypedefType>()) 7738 Name = TypedefTy->getDecl()->getName(); 7739 else 7740 Name = CastTyName; 7741 unsigned Diag = Match == ArgType::NoMatchPedantic 7742 ? diag::warn_format_argument_needs_cast_pedantic 7743 : diag::warn_format_argument_needs_cast; 7744 EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum 7745 << E->getSourceRange(), 7746 E->getBeginLoc(), /*IsStringLocation=*/false, 7747 SpecRange, Hints); 7748 } else { 7749 // In this case, the expression could be printed using a different 7750 // specifier, but we've decided that the specifier is probably correct 7751 // and we should cast instead. Just use the normal warning message. 7752 7753 unsigned Diag = 7754 IsScopedEnum 7755 ? diag::warn_format_conversion_argument_type_mismatch_pedantic 7756 : diag::warn_format_conversion_argument_type_mismatch; 7757 7758 EmitFormatDiagnostic( 7759 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 7760 << IsEnum << E->getSourceRange(), 7761 E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints); 7762 } 7763 } 7764 } else { 7765 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, 7766 SpecifierLen); 7767 // Since the warning for passing non-POD types to variadic functions 7768 // was deferred until now, we emit a warning for non-POD 7769 // arguments here. 7770 bool EmitTypeMismatch = false; 7771 switch (S.isValidVarArgType(ExprTy)) { 7772 case Sema::VAK_Valid: 7773 case Sema::VAK_ValidInCXX11: { 7774 unsigned Diag; 7775 switch (Match) { 7776 case ArgType::Match: 7777 case ArgType::MatchPromotion: 7778 case ArgType::NoMatchPromotionTypeConfusion: 7779 case ArgType::NoMatchSignedness: 7780 llvm_unreachable("expected non-matching"); 7781 case ArgType::NoMatchPedantic: 7782 Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; 7783 break; 7784 case ArgType::NoMatchTypeConfusion: 7785 Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; 7786 break; 7787 case ArgType::NoMatch: 7788 Diag = diag::warn_format_conversion_argument_type_mismatch; 7789 break; 7790 } 7791 7792 EmitFormatDiagnostic( 7793 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 7794 << IsEnum << CSR << E->getSourceRange(), 7795 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7796 break; 7797 } 7798 case Sema::VAK_Undefined: 7799 case Sema::VAK_MSVCUndefined: 7800 if (CallType == Sema::VariadicDoesNotApply) { 7801 EmitTypeMismatch = true; 7802 } else { 7803 EmitFormatDiagnostic( 7804 S.PDiag(diag::warn_non_pod_vararg_with_format_string) 7805 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 7806 << AT.getRepresentativeTypeName(S.Context) << CSR 7807 << E->getSourceRange(), 7808 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7809 checkForCStrMembers(AT, E); 7810 } 7811 break; 7812 7813 case Sema::VAK_Invalid: 7814 if (CallType == Sema::VariadicDoesNotApply) 7815 EmitTypeMismatch = true; 7816 else if (ExprTy->isObjCObjectType()) 7817 EmitFormatDiagnostic( 7818 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) 7819 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 7820 << AT.getRepresentativeTypeName(S.Context) << CSR 7821 << E->getSourceRange(), 7822 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7823 else 7824 // FIXME: If this is an initializer list, suggest removing the braces 7825 // or inserting a cast to the target type. 7826 S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format) 7827 << isa<InitListExpr>(E) << ExprTy << CallType 7828 << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange(); 7829 break; 7830 } 7831 7832 if (EmitTypeMismatch) { 7833 // The function is not variadic, so we do not generate warnings about 7834 // being allowed to pass that object as a variadic argument. Instead, 7835 // since there are inherently no printf specifiers for types which cannot 7836 // be passed as variadic arguments, emit a plain old specifier mismatch 7837 // argument. 7838 EmitFormatDiagnostic( 7839 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7840 << AT.getRepresentativeTypeName(S.Context) << ExprTy << false 7841 << E->getSourceRange(), 7842 E->getBeginLoc(), false, CSR); 7843 } 7844 7845 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && 7846 "format string specifier index out of range"); 7847 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; 7848 } 7849 7850 return true; 7851 } 7852 7853 //===--- CHECK: Scanf format string checking ------------------------------===// 7854 7855 namespace { 7856 7857 class CheckScanfHandler : public CheckFormatHandler { 7858 public: 7859 CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr, 7860 const Expr *origFormatExpr, Sema::FormatStringType type, 7861 unsigned firstDataArg, unsigned numDataArgs, 7862 const char *beg, Sema::FormatArgumentPassingKind APK, 7863 ArrayRef<const Expr *> Args, unsigned formatIdx, 7864 bool inFunctionCall, Sema::VariadicCallType CallType, 7865 llvm::SmallBitVector &CheckedVarArgs, 7866 UncoveredArgHandler &UncoveredArg) 7867 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 7868 numDataArgs, beg, APK, Args, formatIdx, 7869 inFunctionCall, CallType, CheckedVarArgs, 7870 UncoveredArg) {} 7871 7872 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 7873 const char *startSpecifier, 7874 unsigned specifierLen) override; 7875 7876 bool HandleInvalidScanfConversionSpecifier( 7877 const analyze_scanf::ScanfSpecifier &FS, 7878 const char *startSpecifier, 7879 unsigned specifierLen) override; 7880 7881 void HandleIncompleteScanList(const char *start, const char *end) override; 7882 }; 7883 7884 } // namespace 7885 7886 void CheckScanfHandler::HandleIncompleteScanList(const char *start, 7887 const char *end) { 7888 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), 7889 getLocationOfByte(end), /*IsStringLocation*/true, 7890 getSpecifierRange(start, end - start)); 7891 } 7892 7893 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( 7894 const analyze_scanf::ScanfSpecifier &FS, 7895 const char *startSpecifier, 7896 unsigned specifierLen) { 7897 const analyze_scanf::ScanfConversionSpecifier &CS = 7898 FS.getConversionSpecifier(); 7899 7900 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 7901 getLocationOfByte(CS.getStart()), 7902 startSpecifier, specifierLen, 7903 CS.getStart(), CS.getLength()); 7904 } 7905 7906 bool CheckScanfHandler::HandleScanfSpecifier( 7907 const analyze_scanf::ScanfSpecifier &FS, 7908 const char *startSpecifier, 7909 unsigned specifierLen) { 7910 using namespace analyze_scanf; 7911 using namespace analyze_format_string; 7912 7913 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); 7914 7915 // Handle case where '%' and '*' don't consume an argument. These shouldn't 7916 // be used to decide if we are using positional arguments consistently. 7917 if (FS.consumesDataArgument()) { 7918 if (atFirstArg) { 7919 atFirstArg = false; 7920 usesPositionalArgs = FS.usesPositionalArg(); 7921 } 7922 else if (usesPositionalArgs != FS.usesPositionalArg()) { 7923 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 7924 startSpecifier, specifierLen); 7925 return false; 7926 } 7927 } 7928 7929 // Check if the field with is non-zero. 7930 const OptionalAmount &Amt = FS.getFieldWidth(); 7931 if (Amt.getHowSpecified() == OptionalAmount::Constant) { 7932 if (Amt.getConstantAmount() == 0) { 7933 const CharSourceRange &R = getSpecifierRange(Amt.getStart(), 7934 Amt.getConstantLength()); 7935 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), 7936 getLocationOfByte(Amt.getStart()), 7937 /*IsStringLocation*/true, R, 7938 FixItHint::CreateRemoval(R)); 7939 } 7940 } 7941 7942 if (!FS.consumesDataArgument()) { 7943 // FIXME: Technically specifying a precision or field width here 7944 // makes no sense. Worth issuing a warning at some point. 7945 return true; 7946 } 7947 7948 // Consume the argument. 7949 unsigned argIndex = FS.getArgIndex(); 7950 if (argIndex < NumDataArgs) { 7951 // The check to see if the argIndex is valid will come later. 7952 // We set the bit here because we may exit early from this 7953 // function if we encounter some other error. 7954 CoveredArgs.set(argIndex); 7955 } 7956 7957 // Check the length modifier is valid with the given conversion specifier. 7958 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7959 S.getLangOpts())) 7960 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7961 diag::warn_format_nonsensical_length); 7962 else if (!FS.hasStandardLengthModifier()) 7963 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7964 else if (!FS.hasStandardLengthConversionCombination()) 7965 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7966 diag::warn_format_non_standard_conversion_spec); 7967 7968 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7969 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7970 7971 // The remaining checks depend on the data arguments. 7972 if (ArgPassingKind == Sema::FAPK_VAList) 7973 return true; 7974 7975 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7976 return false; 7977 7978 // Check that the argument type matches the format specifier. 7979 const Expr *Ex = getDataArg(argIndex); 7980 if (!Ex) 7981 return true; 7982 7983 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); 7984 7985 if (!AT.isValid()) { 7986 return true; 7987 } 7988 7989 analyze_format_string::ArgType::MatchKind Match = 7990 AT.matchesType(S.Context, Ex->getType()); 7991 Match = handleFormatSignedness(Match, S.getDiagnostics(), Ex->getExprLoc()); 7992 bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic; 7993 if (Match == analyze_format_string::ArgType::Match) 7994 return true; 7995 7996 ScanfSpecifier fixedFS = FS; 7997 bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), 7998 S.getLangOpts(), S.Context); 7999 8000 unsigned Diag = 8001 Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic 8002 : diag::warn_format_conversion_argument_type_mismatch; 8003 8004 if (Success) { 8005 // Get the fix string from the fixed format specifier. 8006 SmallString<128> buf; 8007 llvm::raw_svector_ostream os(buf); 8008 fixedFS.toString(os); 8009 8010 EmitFormatDiagnostic( 8011 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) 8012 << Ex->getType() << false << Ex->getSourceRange(), 8013 Ex->getBeginLoc(), 8014 /*IsStringLocation*/ false, 8015 getSpecifierRange(startSpecifier, specifierLen), 8016 FixItHint::CreateReplacement( 8017 getSpecifierRange(startSpecifier, specifierLen), os.str())); 8018 } else { 8019 EmitFormatDiagnostic(S.PDiag(Diag) 8020 << AT.getRepresentativeTypeName(S.Context) 8021 << Ex->getType() << false << Ex->getSourceRange(), 8022 Ex->getBeginLoc(), 8023 /*IsStringLocation*/ false, 8024 getSpecifierRange(startSpecifier, specifierLen)); 8025 } 8026 8027 return true; 8028 } 8029 8030 static void CheckFormatString( 8031 Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, 8032 ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, 8033 unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, 8034 bool inFunctionCall, Sema::VariadicCallType CallType, 8035 llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, 8036 bool IgnoreStringsWithoutSpecifiers) { 8037 // CHECK: is the format string a wide literal? 8038 if (!FExpr->isAscii() && !FExpr->isUTF8()) { 8039 CheckFormatHandler::EmitFormatDiagnostic( 8040 S, inFunctionCall, Args[format_idx], 8041 S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(), 8042 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 8043 return; 8044 } 8045 8046 // Str - The format string. NOTE: this is NOT null-terminated! 8047 StringRef StrRef = FExpr->getString(); 8048 const char *Str = StrRef.data(); 8049 // Account for cases where the string literal is truncated in a declaration. 8050 const ConstantArrayType *T = 8051 S.Context.getAsConstantArrayType(FExpr->getType()); 8052 assert(T && "String literal not of constant array type!"); 8053 size_t TypeSize = T->getZExtSize(); 8054 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 8055 const unsigned numDataArgs = Args.size() - firstDataArg; 8056 8057 if (IgnoreStringsWithoutSpecifiers && 8058 !analyze_format_string::parseFormatStringHasFormattingSpecifiers( 8059 Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) 8060 return; 8061 8062 // Emit a warning if the string literal is truncated and does not contain an 8063 // embedded null character. 8064 if (TypeSize <= StrRef.size() && !StrRef.substr(0, TypeSize).contains('\0')) { 8065 CheckFormatHandler::EmitFormatDiagnostic( 8066 S, inFunctionCall, Args[format_idx], 8067 S.PDiag(diag::warn_printf_format_string_not_null_terminated), 8068 FExpr->getBeginLoc(), 8069 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); 8070 return; 8071 } 8072 8073 // CHECK: empty format string? 8074 if (StrLen == 0 && numDataArgs > 0) { 8075 CheckFormatHandler::EmitFormatDiagnostic( 8076 S, inFunctionCall, Args[format_idx], 8077 S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(), 8078 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 8079 return; 8080 } 8081 8082 if (Type == Sema::FST_Printf || Type == Sema::FST_NSString || 8083 Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog || 8084 Type == Sema::FST_OSTrace || Type == Sema::FST_Syslog) { 8085 CheckPrintfHandler H( 8086 S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs, 8087 (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, APK, 8088 Args, format_idx, inFunctionCall, CallType, CheckedVarArgs, 8089 UncoveredArg); 8090 8091 if (!analyze_format_string::ParsePrintfString( 8092 H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo(), 8093 Type == Sema::FST_FreeBSDKPrintf)) 8094 H.DoneProcessing(); 8095 } else if (Type == Sema::FST_Scanf) { 8096 CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg, 8097 numDataArgs, Str, APK, Args, format_idx, inFunctionCall, 8098 CallType, CheckedVarArgs, UncoveredArg); 8099 8100 if (!analyze_format_string::ParseScanfString( 8101 H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) 8102 H.DoneProcessing(); 8103 } // TODO: handle other formats 8104 } 8105 8106 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { 8107 // Str - The format string. NOTE: this is NOT null-terminated! 8108 StringRef StrRef = FExpr->getString(); 8109 const char *Str = StrRef.data(); 8110 // Account for cases where the string literal is truncated in a declaration. 8111 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); 8112 assert(T && "String literal not of constant array type!"); 8113 size_t TypeSize = T->getZExtSize(); 8114 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 8115 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, 8116 getLangOpts(), 8117 Context.getTargetInfo()); 8118 } 8119 8120 //===--- CHECK: Warn on use of wrong absolute value function. -------------===// 8121 8122 // Returns the related absolute value function that is larger, of 0 if one 8123 // does not exist. 8124 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { 8125 switch (AbsFunction) { 8126 default: 8127 return 0; 8128 8129 case Builtin::BI__builtin_abs: 8130 return Builtin::BI__builtin_labs; 8131 case Builtin::BI__builtin_labs: 8132 return Builtin::BI__builtin_llabs; 8133 case Builtin::BI__builtin_llabs: 8134 return 0; 8135 8136 case Builtin::BI__builtin_fabsf: 8137 return Builtin::BI__builtin_fabs; 8138 case Builtin::BI__builtin_fabs: 8139 return Builtin::BI__builtin_fabsl; 8140 case Builtin::BI__builtin_fabsl: 8141 return 0; 8142 8143 case Builtin::BI__builtin_cabsf: 8144 return Builtin::BI__builtin_cabs; 8145 case Builtin::BI__builtin_cabs: 8146 return Builtin::BI__builtin_cabsl; 8147 case Builtin::BI__builtin_cabsl: 8148 return 0; 8149 8150 case Builtin::BIabs: 8151 return Builtin::BIlabs; 8152 case Builtin::BIlabs: 8153 return Builtin::BIllabs; 8154 case Builtin::BIllabs: 8155 return 0; 8156 8157 case Builtin::BIfabsf: 8158 return Builtin::BIfabs; 8159 case Builtin::BIfabs: 8160 return Builtin::BIfabsl; 8161 case Builtin::BIfabsl: 8162 return 0; 8163 8164 case Builtin::BIcabsf: 8165 return Builtin::BIcabs; 8166 case Builtin::BIcabs: 8167 return Builtin::BIcabsl; 8168 case Builtin::BIcabsl: 8169 return 0; 8170 } 8171 } 8172 8173 // Returns the argument type of the absolute value function. 8174 static QualType getAbsoluteValueArgumentType(ASTContext &Context, 8175 unsigned AbsType) { 8176 if (AbsType == 0) 8177 return QualType(); 8178 8179 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; 8180 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); 8181 if (Error != ASTContext::GE_None) 8182 return QualType(); 8183 8184 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); 8185 if (!FT) 8186 return QualType(); 8187 8188 if (FT->getNumParams() != 1) 8189 return QualType(); 8190 8191 return FT->getParamType(0); 8192 } 8193 8194 // Returns the best absolute value function, or zero, based on type and 8195 // current absolute value function. 8196 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, 8197 unsigned AbsFunctionKind) { 8198 unsigned BestKind = 0; 8199 uint64_t ArgSize = Context.getTypeSize(ArgType); 8200 for (unsigned Kind = AbsFunctionKind; Kind != 0; 8201 Kind = getLargerAbsoluteValueFunction(Kind)) { 8202 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); 8203 if (Context.getTypeSize(ParamType) >= ArgSize) { 8204 if (BestKind == 0) 8205 BestKind = Kind; 8206 else if (Context.hasSameType(ParamType, ArgType)) { 8207 BestKind = Kind; 8208 break; 8209 } 8210 } 8211 } 8212 return BestKind; 8213 } 8214 8215 enum AbsoluteValueKind { 8216 AVK_Integer, 8217 AVK_Floating, 8218 AVK_Complex 8219 }; 8220 8221 static AbsoluteValueKind getAbsoluteValueKind(QualType T) { 8222 if (T->isIntegralOrEnumerationType()) 8223 return AVK_Integer; 8224 if (T->isRealFloatingType()) 8225 return AVK_Floating; 8226 if (T->isAnyComplexType()) 8227 return AVK_Complex; 8228 8229 llvm_unreachable("Type not integer, floating, or complex"); 8230 } 8231 8232 // Changes the absolute value function to a different type. Preserves whether 8233 // the function is a builtin. 8234 static unsigned changeAbsFunction(unsigned AbsKind, 8235 AbsoluteValueKind ValueKind) { 8236 switch (ValueKind) { 8237 case AVK_Integer: 8238 switch (AbsKind) { 8239 default: 8240 return 0; 8241 case Builtin::BI__builtin_fabsf: 8242 case Builtin::BI__builtin_fabs: 8243 case Builtin::BI__builtin_fabsl: 8244 case Builtin::BI__builtin_cabsf: 8245 case Builtin::BI__builtin_cabs: 8246 case Builtin::BI__builtin_cabsl: 8247 return Builtin::BI__builtin_abs; 8248 case Builtin::BIfabsf: 8249 case Builtin::BIfabs: 8250 case Builtin::BIfabsl: 8251 case Builtin::BIcabsf: 8252 case Builtin::BIcabs: 8253 case Builtin::BIcabsl: 8254 return Builtin::BIabs; 8255 } 8256 case AVK_Floating: 8257 switch (AbsKind) { 8258 default: 8259 return 0; 8260 case Builtin::BI__builtin_abs: 8261 case Builtin::BI__builtin_labs: 8262 case Builtin::BI__builtin_llabs: 8263 case Builtin::BI__builtin_cabsf: 8264 case Builtin::BI__builtin_cabs: 8265 case Builtin::BI__builtin_cabsl: 8266 return Builtin::BI__builtin_fabsf; 8267 case Builtin::BIabs: 8268 case Builtin::BIlabs: 8269 case Builtin::BIllabs: 8270 case Builtin::BIcabsf: 8271 case Builtin::BIcabs: 8272 case Builtin::BIcabsl: 8273 return Builtin::BIfabsf; 8274 } 8275 case AVK_Complex: 8276 switch (AbsKind) { 8277 default: 8278 return 0; 8279 case Builtin::BI__builtin_abs: 8280 case Builtin::BI__builtin_labs: 8281 case Builtin::BI__builtin_llabs: 8282 case Builtin::BI__builtin_fabsf: 8283 case Builtin::BI__builtin_fabs: 8284 case Builtin::BI__builtin_fabsl: 8285 return Builtin::BI__builtin_cabsf; 8286 case Builtin::BIabs: 8287 case Builtin::BIlabs: 8288 case Builtin::BIllabs: 8289 case Builtin::BIfabsf: 8290 case Builtin::BIfabs: 8291 case Builtin::BIfabsl: 8292 return Builtin::BIcabsf; 8293 } 8294 } 8295 llvm_unreachable("Unable to convert function"); 8296 } 8297 8298 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { 8299 const IdentifierInfo *FnInfo = FDecl->getIdentifier(); 8300 if (!FnInfo) 8301 return 0; 8302 8303 switch (FDecl->getBuiltinID()) { 8304 default: 8305 return 0; 8306 case Builtin::BI__builtin_abs: 8307 case Builtin::BI__builtin_fabs: 8308 case Builtin::BI__builtin_fabsf: 8309 case Builtin::BI__builtin_fabsl: 8310 case Builtin::BI__builtin_labs: 8311 case Builtin::BI__builtin_llabs: 8312 case Builtin::BI__builtin_cabs: 8313 case Builtin::BI__builtin_cabsf: 8314 case Builtin::BI__builtin_cabsl: 8315 case Builtin::BIabs: 8316 case Builtin::BIlabs: 8317 case Builtin::BIllabs: 8318 case Builtin::BIfabs: 8319 case Builtin::BIfabsf: 8320 case Builtin::BIfabsl: 8321 case Builtin::BIcabs: 8322 case Builtin::BIcabsf: 8323 case Builtin::BIcabsl: 8324 return FDecl->getBuiltinID(); 8325 } 8326 llvm_unreachable("Unknown Builtin type"); 8327 } 8328 8329 // If the replacement is valid, emit a note with replacement function. 8330 // Additionally, suggest including the proper header if not already included. 8331 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, 8332 unsigned AbsKind, QualType ArgType) { 8333 bool EmitHeaderHint = true; 8334 const char *HeaderName = nullptr; 8335 StringRef FunctionName; 8336 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { 8337 FunctionName = "std::abs"; 8338 if (ArgType->isIntegralOrEnumerationType()) { 8339 HeaderName = "cstdlib"; 8340 } else if (ArgType->isRealFloatingType()) { 8341 HeaderName = "cmath"; 8342 } else { 8343 llvm_unreachable("Invalid Type"); 8344 } 8345 8346 // Lookup all std::abs 8347 if (NamespaceDecl *Std = S.getStdNamespace()) { 8348 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); 8349 R.suppressDiagnostics(); 8350 S.LookupQualifiedName(R, Std); 8351 8352 for (const auto *I : R) { 8353 const FunctionDecl *FDecl = nullptr; 8354 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { 8355 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); 8356 } else { 8357 FDecl = dyn_cast<FunctionDecl>(I); 8358 } 8359 if (!FDecl) 8360 continue; 8361 8362 // Found std::abs(), check that they are the right ones. 8363 if (FDecl->getNumParams() != 1) 8364 continue; 8365 8366 // Check that the parameter type can handle the argument. 8367 QualType ParamType = FDecl->getParamDecl(0)->getType(); 8368 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && 8369 S.Context.getTypeSize(ArgType) <= 8370 S.Context.getTypeSize(ParamType)) { 8371 // Found a function, don't need the header hint. 8372 EmitHeaderHint = false; 8373 break; 8374 } 8375 } 8376 } 8377 } else { 8378 FunctionName = S.Context.BuiltinInfo.getName(AbsKind); 8379 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); 8380 8381 if (HeaderName) { 8382 DeclarationName DN(&S.Context.Idents.get(FunctionName)); 8383 LookupResult R(S, DN, Loc, Sema::LookupAnyName); 8384 R.suppressDiagnostics(); 8385 S.LookupName(R, S.getCurScope()); 8386 8387 if (R.isSingleResult()) { 8388 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); 8389 if (FD && FD->getBuiltinID() == AbsKind) { 8390 EmitHeaderHint = false; 8391 } else { 8392 return; 8393 } 8394 } else if (!R.empty()) { 8395 return; 8396 } 8397 } 8398 } 8399 8400 S.Diag(Loc, diag::note_replace_abs_function) 8401 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); 8402 8403 if (!HeaderName) 8404 return; 8405 8406 if (!EmitHeaderHint) 8407 return; 8408 8409 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName 8410 << FunctionName; 8411 } 8412 8413 template <std::size_t StrLen> 8414 static bool IsStdFunction(const FunctionDecl *FDecl, 8415 const char (&Str)[StrLen]) { 8416 if (!FDecl) 8417 return false; 8418 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str)) 8419 return false; 8420 if (!FDecl->isInStdNamespace()) 8421 return false; 8422 8423 return true; 8424 } 8425 8426 enum class MathCheck { NaN, Inf }; 8427 static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { 8428 auto MatchesAny = [&](std::initializer_list<llvm::StringRef> names) { 8429 return std::any_of(names.begin(), names.end(), [&](llvm::StringRef name) { 8430 return calleeName == name; 8431 }); 8432 }; 8433 8434 switch (Check) { 8435 case MathCheck::NaN: 8436 return MatchesAny({"__builtin_nan", "__builtin_nanf", "__builtin_nanl", 8437 "__builtin_nanf16", "__builtin_nanf128"}); 8438 case MathCheck::Inf: 8439 return MatchesAny({"__builtin_inf", "__builtin_inff", "__builtin_infl", 8440 "__builtin_inff16", "__builtin_inff128"}); 8441 } 8442 llvm_unreachable("unknown MathCheck"); 8443 } 8444 8445 void Sema::CheckInfNaNFunction(const CallExpr *Call, 8446 const FunctionDecl *FDecl) { 8447 FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); 8448 bool HasIdentifier = FDecl->getIdentifier() != nullptr; 8449 bool IsNaNOrIsUnordered = 8450 IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); 8451 bool IsSpecialNaN = 8452 HasIdentifier && IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); 8453 if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { 8454 Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 8455 << 1 << 0 << Call->getSourceRange(); 8456 } else { 8457 bool IsInfOrIsFinite = 8458 IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); 8459 bool IsInfinityOrIsSpecialInf = 8460 HasIdentifier && ((FDecl->getName() == "infinity") || 8461 IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); 8462 if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) 8463 Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 8464 << 0 << 0 << Call->getSourceRange(); 8465 } 8466 } 8467 8468 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, 8469 const FunctionDecl *FDecl) { 8470 if (Call->getNumArgs() != 1) 8471 return; 8472 8473 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); 8474 bool IsStdAbs = IsStdFunction(FDecl, "abs"); 8475 if (AbsKind == 0 && !IsStdAbs) 8476 return; 8477 8478 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8479 QualType ParamType = Call->getArg(0)->getType(); 8480 8481 // Unsigned types cannot be negative. Suggest removing the absolute value 8482 // function call. 8483 if (ArgType->isUnsignedIntegerType()) { 8484 StringRef FunctionName = 8485 IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind); 8486 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; 8487 Diag(Call->getExprLoc(), diag::note_remove_abs) 8488 << FunctionName 8489 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); 8490 return; 8491 } 8492 8493 // Taking the absolute value of a pointer is very suspicious, they probably 8494 // wanted to index into an array, dereference a pointer, call a function, etc. 8495 if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) { 8496 unsigned DiagType = 0; 8497 if (ArgType->isFunctionType()) 8498 DiagType = 1; 8499 else if (ArgType->isArrayType()) 8500 DiagType = 2; 8501 8502 Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType; 8503 return; 8504 } 8505 8506 // std::abs has overloads which prevent most of the absolute value problems 8507 // from occurring. 8508 if (IsStdAbs) 8509 return; 8510 8511 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); 8512 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); 8513 8514 // The argument and parameter are the same kind. Check if they are the right 8515 // size. 8516 if (ArgValueKind == ParamValueKind) { 8517 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) 8518 return; 8519 8520 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); 8521 Diag(Call->getExprLoc(), diag::warn_abs_too_small) 8522 << FDecl << ArgType << ParamType; 8523 8524 if (NewAbsKind == 0) 8525 return; 8526 8527 emitReplacement(*this, Call->getExprLoc(), 8528 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8529 return; 8530 } 8531 8532 // ArgValueKind != ParamValueKind 8533 // The wrong type of absolute value function was used. Attempt to find the 8534 // proper one. 8535 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); 8536 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); 8537 if (NewAbsKind == 0) 8538 return; 8539 8540 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) 8541 << FDecl << ParamValueKind << ArgValueKind; 8542 8543 emitReplacement(*this, Call->getExprLoc(), 8544 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8545 } 8546 8547 //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===// 8548 void Sema::CheckMaxUnsignedZero(const CallExpr *Call, 8549 const FunctionDecl *FDecl) { 8550 if (!Call || !FDecl) return; 8551 8552 // Ignore template specializations and macros. 8553 if (inTemplateInstantiation()) return; 8554 if (Call->getExprLoc().isMacroID()) return; 8555 8556 // Only care about the one template argument, two function parameter std::max 8557 if (Call->getNumArgs() != 2) return; 8558 if (!IsStdFunction(FDecl, "max")) return; 8559 const auto * ArgList = FDecl->getTemplateSpecializationArgs(); 8560 if (!ArgList) return; 8561 if (ArgList->size() != 1) return; 8562 8563 // Check that template type argument is unsigned integer. 8564 const auto& TA = ArgList->get(0); 8565 if (TA.getKind() != TemplateArgument::Type) return; 8566 QualType ArgType = TA.getAsType(); 8567 if (!ArgType->isUnsignedIntegerType()) return; 8568 8569 // See if either argument is a literal zero. 8570 auto IsLiteralZeroArg = [](const Expr* E) -> bool { 8571 const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E); 8572 if (!MTE) return false; 8573 const auto *Num = dyn_cast<IntegerLiteral>(MTE->getSubExpr()); 8574 if (!Num) return false; 8575 if (Num->getValue() != 0) return false; 8576 return true; 8577 }; 8578 8579 const Expr *FirstArg = Call->getArg(0); 8580 const Expr *SecondArg = Call->getArg(1); 8581 const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg); 8582 const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg); 8583 8584 // Only warn when exactly one argument is zero. 8585 if (IsFirstArgZero == IsSecondArgZero) return; 8586 8587 SourceRange FirstRange = FirstArg->getSourceRange(); 8588 SourceRange SecondRange = SecondArg->getSourceRange(); 8589 8590 SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange; 8591 8592 Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero) 8593 << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange; 8594 8595 // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)". 8596 SourceRange RemovalRange; 8597 if (IsFirstArgZero) { 8598 RemovalRange = SourceRange(FirstRange.getBegin(), 8599 SecondRange.getBegin().getLocWithOffset(-1)); 8600 } else { 8601 RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()), 8602 SecondRange.getEnd()); 8603 } 8604 8605 Diag(Call->getExprLoc(), diag::note_remove_max_call) 8606 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()) 8607 << FixItHint::CreateRemoval(RemovalRange); 8608 } 8609 8610 //===--- CHECK: Standard memory functions ---------------------------------===// 8611 8612 /// Takes the expression passed to the size_t parameter of functions 8613 /// such as memcmp, strncat, etc and warns if it's a comparison. 8614 /// 8615 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. 8616 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, 8617 IdentifierInfo *FnName, 8618 SourceLocation FnLoc, 8619 SourceLocation RParenLoc) { 8620 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); 8621 if (!Size) 8622 return false; 8623 8624 // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||: 8625 if (!Size->isComparisonOp() && !Size->isLogicalOp()) 8626 return false; 8627 8628 SourceRange SizeRange = Size->getSourceRange(); 8629 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) 8630 << SizeRange << FnName; 8631 S.Diag(FnLoc, diag::note_memsize_comparison_paren) 8632 << FnName 8633 << FixItHint::CreateInsertion( 8634 S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")") 8635 << FixItHint::CreateRemoval(RParenLoc); 8636 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) 8637 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") 8638 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), 8639 ")"); 8640 8641 return true; 8642 } 8643 8644 /// Determine whether the given type is or contains a dynamic class type 8645 /// (e.g., whether it has a vtable). 8646 static const CXXRecordDecl *getContainedDynamicClass(QualType T, 8647 bool &IsContained) { 8648 // Look through array types while ignoring qualifiers. 8649 const Type *Ty = T->getBaseElementTypeUnsafe(); 8650 IsContained = false; 8651 8652 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 8653 RD = RD ? RD->getDefinition() : nullptr; 8654 if (!RD || RD->isInvalidDecl()) 8655 return nullptr; 8656 8657 if (RD->isDynamicClass()) 8658 return RD; 8659 8660 // Check all the fields. If any bases were dynamic, the class is dynamic. 8661 // It's impossible for a class to transitively contain itself by value, so 8662 // infinite recursion is impossible. 8663 for (auto *FD : RD->fields()) { 8664 bool SubContained; 8665 if (const CXXRecordDecl *ContainedRD = 8666 getContainedDynamicClass(FD->getType(), SubContained)) { 8667 IsContained = true; 8668 return ContainedRD; 8669 } 8670 } 8671 8672 return nullptr; 8673 } 8674 8675 static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) { 8676 if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 8677 if (Unary->getKind() == UETT_SizeOf) 8678 return Unary; 8679 return nullptr; 8680 } 8681 8682 /// If E is a sizeof expression, returns its argument expression, 8683 /// otherwise returns NULL. 8684 static const Expr *getSizeOfExprArg(const Expr *E) { 8685 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8686 if (!SizeOf->isArgumentType()) 8687 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); 8688 return nullptr; 8689 } 8690 8691 /// If E is a sizeof expression, returns its argument type. 8692 static QualType getSizeOfArgType(const Expr *E) { 8693 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8694 return SizeOf->getTypeOfArgument(); 8695 return QualType(); 8696 } 8697 8698 namespace { 8699 8700 struct SearchNonTrivialToInitializeField 8701 : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> { 8702 using Super = 8703 DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>; 8704 8705 SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {} 8706 8707 void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT, 8708 SourceLocation SL) { 8709 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8710 asDerived().visitArray(PDIK, AT, SL); 8711 return; 8712 } 8713 8714 Super::visitWithKind(PDIK, FT, SL); 8715 } 8716 8717 void visitARCStrong(QualType FT, SourceLocation SL) { 8718 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8719 } 8720 void visitARCWeak(QualType FT, SourceLocation SL) { 8721 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8722 } 8723 void visitStruct(QualType FT, SourceLocation SL) { 8724 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8725 visit(FD->getType(), FD->getLocation()); 8726 } 8727 void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK, 8728 const ArrayType *AT, SourceLocation SL) { 8729 visit(getContext().getBaseElementType(AT), SL); 8730 } 8731 void visitTrivial(QualType FT, SourceLocation SL) {} 8732 8733 static void diag(QualType RT, const Expr *E, Sema &S) { 8734 SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation()); 8735 } 8736 8737 ASTContext &getContext() { return S.getASTContext(); } 8738 8739 const Expr *E; 8740 Sema &S; 8741 }; 8742 8743 struct SearchNonTrivialToCopyField 8744 : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> { 8745 using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>; 8746 8747 SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {} 8748 8749 void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT, 8750 SourceLocation SL) { 8751 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8752 asDerived().visitArray(PCK, AT, SL); 8753 return; 8754 } 8755 8756 Super::visitWithKind(PCK, FT, SL); 8757 } 8758 8759 void visitARCStrong(QualType FT, SourceLocation SL) { 8760 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8761 } 8762 void visitARCWeak(QualType FT, SourceLocation SL) { 8763 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8764 } 8765 void visitStruct(QualType FT, SourceLocation SL) { 8766 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8767 visit(FD->getType(), FD->getLocation()); 8768 } 8769 void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT, 8770 SourceLocation SL) { 8771 visit(getContext().getBaseElementType(AT), SL); 8772 } 8773 void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT, 8774 SourceLocation SL) {} 8775 void visitTrivial(QualType FT, SourceLocation SL) {} 8776 void visitVolatileTrivial(QualType FT, SourceLocation SL) {} 8777 8778 static void diag(QualType RT, const Expr *E, Sema &S) { 8779 SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation()); 8780 } 8781 8782 ASTContext &getContext() { return S.getASTContext(); } 8783 8784 const Expr *E; 8785 Sema &S; 8786 }; 8787 8788 } 8789 8790 /// Detect if \c SizeofExpr is likely to calculate the sizeof an object. 8791 static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) { 8792 SizeofExpr = SizeofExpr->IgnoreParenImpCasts(); 8793 8794 if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) { 8795 if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add) 8796 return false; 8797 8798 return doesExprLikelyComputeSize(BO->getLHS()) || 8799 doesExprLikelyComputeSize(BO->getRHS()); 8800 } 8801 8802 return getAsSizeOfExpr(SizeofExpr) != nullptr; 8803 } 8804 8805 /// Check if the ArgLoc originated from a macro passed to the call at CallLoc. 8806 /// 8807 /// \code 8808 /// #define MACRO 0 8809 /// foo(MACRO); 8810 /// foo(0); 8811 /// \endcode 8812 /// 8813 /// This should return true for the first call to foo, but not for the second 8814 /// (regardless of whether foo is a macro or function). 8815 static bool isArgumentExpandedFromMacro(SourceManager &SM, 8816 SourceLocation CallLoc, 8817 SourceLocation ArgLoc) { 8818 if (!CallLoc.isMacroID()) 8819 return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc); 8820 8821 return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) != 8822 SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc)); 8823 } 8824 8825 /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the 8826 /// last two arguments transposed. 8827 static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) { 8828 if (BId != Builtin::BImemset && BId != Builtin::BIbzero) 8829 return; 8830 8831 const Expr *SizeArg = 8832 Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts(); 8833 8834 auto isLiteralZero = [](const Expr *E) { 8835 return (isa<IntegerLiteral>(E) && 8836 cast<IntegerLiteral>(E)->getValue() == 0) || 8837 (isa<CharacterLiteral>(E) && 8838 cast<CharacterLiteral>(E)->getValue() == 0); 8839 }; 8840 8841 // If we're memsetting or bzeroing 0 bytes, then this is likely an error. 8842 SourceLocation CallLoc = Call->getRParenLoc(); 8843 SourceManager &SM = S.getSourceManager(); 8844 if (isLiteralZero(SizeArg) && 8845 !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) { 8846 8847 SourceLocation DiagLoc = SizeArg->getExprLoc(); 8848 8849 // Some platforms #define bzero to __builtin_memset. See if this is the 8850 // case, and if so, emit a better diagnostic. 8851 if (BId == Builtin::BIbzero || 8852 (CallLoc.isMacroID() && Lexer::getImmediateMacroName( 8853 CallLoc, SM, S.getLangOpts()) == "bzero")) { 8854 S.Diag(DiagLoc, diag::warn_suspicious_bzero_size); 8855 S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence); 8856 } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) { 8857 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0; 8858 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0; 8859 } 8860 return; 8861 } 8862 8863 // If the second argument to a memset is a sizeof expression and the third 8864 // isn't, this is also likely an error. This should catch 8865 // 'memset(buf, sizeof(buf), 0xff)'. 8866 if (BId == Builtin::BImemset && 8867 doesExprLikelyComputeSize(Call->getArg(1)) && 8868 !doesExprLikelyComputeSize(Call->getArg(2))) { 8869 SourceLocation DiagLoc = Call->getArg(1)->getExprLoc(); 8870 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1; 8871 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1; 8872 return; 8873 } 8874 } 8875 8876 void Sema::CheckMemaccessArguments(const CallExpr *Call, 8877 unsigned BId, 8878 IdentifierInfo *FnName) { 8879 assert(BId != 0); 8880 8881 // It is possible to have a non-standard definition of memset. Validate 8882 // we have enough arguments, and if not, abort further checking. 8883 unsigned ExpectedNumArgs = 8884 (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3); 8885 if (Call->getNumArgs() < ExpectedNumArgs) 8886 return; 8887 8888 unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero || 8889 BId == Builtin::BIstrndup ? 1 : 2); 8890 unsigned LenArg = 8891 (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2); 8892 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); 8893 8894 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, 8895 Call->getBeginLoc(), Call->getRParenLoc())) 8896 return; 8897 8898 // Catch cases like 'memset(buf, sizeof(buf), 0)'. 8899 CheckMemaccessSize(*this, BId, Call); 8900 8901 // We have special checking when the length is a sizeof expression. 8902 QualType SizeOfArgTy = getSizeOfArgType(LenExpr); 8903 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); 8904 llvm::FoldingSetNodeID SizeOfArgID; 8905 8906 // Although widely used, 'bzero' is not a standard function. Be more strict 8907 // with the argument types before allowing diagnostics and only allow the 8908 // form bzero(ptr, sizeof(...)). 8909 QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8910 if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>()) 8911 return; 8912 8913 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { 8914 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); 8915 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); 8916 8917 QualType DestTy = Dest->getType(); 8918 QualType PointeeTy; 8919 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { 8920 PointeeTy = DestPtrTy->getPointeeType(); 8921 8922 // Never warn about void type pointers. This can be used to suppress 8923 // false positives. 8924 if (PointeeTy->isVoidType()) 8925 continue; 8926 8927 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by 8928 // actually comparing the expressions for equality. Because computing the 8929 // expression IDs can be expensive, we only do this if the diagnostic is 8930 // enabled. 8931 if (SizeOfArg && 8932 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, 8933 SizeOfArg->getExprLoc())) { 8934 // We only compute IDs for expressions if the warning is enabled, and 8935 // cache the sizeof arg's ID. 8936 if (SizeOfArgID == llvm::FoldingSetNodeID()) 8937 SizeOfArg->Profile(SizeOfArgID, Context, true); 8938 llvm::FoldingSetNodeID DestID; 8939 Dest->Profile(DestID, Context, true); 8940 if (DestID == SizeOfArgID) { 8941 // TODO: For strncpy() and friends, this could suggest sizeof(dst) 8942 // over sizeof(src) as well. 8943 unsigned ActionIdx = 0; // Default is to suggest dereferencing. 8944 StringRef ReadableName = FnName->getName(); 8945 8946 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) 8947 if (UnaryOp->getOpcode() == UO_AddrOf) 8948 ActionIdx = 1; // If its an address-of operator, just remove it. 8949 if (!PointeeTy->isIncompleteType() && 8950 (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) 8951 ActionIdx = 2; // If the pointee's size is sizeof(char), 8952 // suggest an explicit length. 8953 8954 // If the function is defined as a builtin macro, do not show macro 8955 // expansion. 8956 SourceLocation SL = SizeOfArg->getExprLoc(); 8957 SourceRange DSR = Dest->getSourceRange(); 8958 SourceRange SSR = SizeOfArg->getSourceRange(); 8959 SourceManager &SM = getSourceManager(); 8960 8961 if (SM.isMacroArgExpansion(SL)) { 8962 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); 8963 SL = SM.getSpellingLoc(SL); 8964 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), 8965 SM.getSpellingLoc(DSR.getEnd())); 8966 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), 8967 SM.getSpellingLoc(SSR.getEnd())); 8968 } 8969 8970 DiagRuntimeBehavior(SL, SizeOfArg, 8971 PDiag(diag::warn_sizeof_pointer_expr_memaccess) 8972 << ReadableName 8973 << PointeeTy 8974 << DestTy 8975 << DSR 8976 << SSR); 8977 DiagRuntimeBehavior(SL, SizeOfArg, 8978 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) 8979 << ActionIdx 8980 << SSR); 8981 8982 break; 8983 } 8984 } 8985 8986 // Also check for cases where the sizeof argument is the exact same 8987 // type as the memory argument, and where it points to a user-defined 8988 // record type. 8989 if (SizeOfArgTy != QualType()) { 8990 if (PointeeTy->isRecordType() && 8991 Context.typesAreCompatible(SizeOfArgTy, DestTy)) { 8992 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, 8993 PDiag(diag::warn_sizeof_pointer_type_memaccess) 8994 << FnName << SizeOfArgTy << ArgIdx 8995 << PointeeTy << Dest->getSourceRange() 8996 << LenExpr->getSourceRange()); 8997 break; 8998 } 8999 } 9000 } else if (DestTy->isArrayType()) { 9001 PointeeTy = DestTy; 9002 } 9003 9004 if (PointeeTy == QualType()) 9005 continue; 9006 9007 // Always complain about dynamic classes. 9008 bool IsContained; 9009 if (const CXXRecordDecl *ContainedRD = 9010 getContainedDynamicClass(PointeeTy, IsContained)) { 9011 9012 unsigned OperationType = 0; 9013 const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp; 9014 // "overwritten" if we're warning about the destination for any call 9015 // but memcmp; otherwise a verb appropriate to the call. 9016 if (ArgIdx != 0 || IsCmp) { 9017 if (BId == Builtin::BImemcpy) 9018 OperationType = 1; 9019 else if(BId == Builtin::BImemmove) 9020 OperationType = 2; 9021 else if (IsCmp) 9022 OperationType = 3; 9023 } 9024 9025 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9026 PDiag(diag::warn_dyn_class_memaccess) 9027 << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName 9028 << IsContained << ContainedRD << OperationType 9029 << Call->getCallee()->getSourceRange()); 9030 } else if (PointeeTy.hasNonTrivialObjCLifetime() && 9031 BId != Builtin::BImemset) 9032 DiagRuntimeBehavior( 9033 Dest->getExprLoc(), Dest, 9034 PDiag(diag::warn_arc_object_memaccess) 9035 << ArgIdx << FnName << PointeeTy 9036 << Call->getCallee()->getSourceRange()); 9037 else if (const auto *RT = PointeeTy->getAs<RecordType>()) { 9038 9039 // FIXME: Do not consider incomplete types even though they may be 9040 // completed later. GCC does not diagnose such code, but we may want to 9041 // consider diagnosing it in the future, perhaps under a different, but 9042 // related, diagnostic group. 9043 bool MayBeTriviallyCopyableCXXRecord = 9044 RT->isIncompleteType() || 9045 RT->desugar().isTriviallyCopyableType(Context); 9046 9047 if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 9048 RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) { 9049 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9050 PDiag(diag::warn_cstruct_memaccess) 9051 << ArgIdx << FnName << PointeeTy << 0); 9052 SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this); 9053 } else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 9054 !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) { 9055 // FIXME: Limiting this warning to dest argument until we decide 9056 // whether it's valid for source argument too. 9057 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9058 PDiag(diag::warn_cxxstruct_memaccess) 9059 << FnName << PointeeTy); 9060 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 9061 RT->getDecl()->isNonTrivialToPrimitiveCopy()) { 9062 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9063 PDiag(diag::warn_cstruct_memaccess) 9064 << ArgIdx << FnName << PointeeTy << 1); 9065 SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this); 9066 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 9067 !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) { 9068 // FIXME: Limiting this warning to dest argument until we decide 9069 // whether it's valid for source argument too. 9070 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 9071 PDiag(diag::warn_cxxstruct_memaccess) 9072 << FnName << PointeeTy); 9073 } else { 9074 continue; 9075 } 9076 } else 9077 continue; 9078 9079 DiagRuntimeBehavior( 9080 Dest->getExprLoc(), Dest, 9081 PDiag(diag::note_bad_memaccess_silence) 9082 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); 9083 break; 9084 } 9085 } 9086 9087 // A little helper routine: ignore addition and subtraction of integer literals. 9088 // This intentionally does not ignore all integer constant expressions because 9089 // we don't want to remove sizeof(). 9090 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { 9091 Ex = Ex->IgnoreParenCasts(); 9092 9093 while (true) { 9094 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); 9095 if (!BO || !BO->isAdditiveOp()) 9096 break; 9097 9098 const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); 9099 const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); 9100 9101 if (isa<IntegerLiteral>(RHS)) 9102 Ex = LHS; 9103 else if (isa<IntegerLiteral>(LHS)) 9104 Ex = RHS; 9105 else 9106 break; 9107 } 9108 9109 return Ex; 9110 } 9111 9112 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, 9113 ASTContext &Context) { 9114 // Only handle constant-sized or VLAs, but not flexible members. 9115 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { 9116 // Only issue the FIXIT for arrays of size > 1. 9117 if (CAT->getZExtSize() <= 1) 9118 return false; 9119 } else if (!Ty->isVariableArrayType()) { 9120 return false; 9121 } 9122 return true; 9123 } 9124 9125 void Sema::CheckStrlcpycatArguments(const CallExpr *Call, 9126 IdentifierInfo *FnName) { 9127 9128 // Don't crash if the user has the wrong number of arguments 9129 unsigned NumArgs = Call->getNumArgs(); 9130 if ((NumArgs != 3) && (NumArgs != 4)) 9131 return; 9132 9133 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); 9134 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); 9135 const Expr *CompareWithSrc = nullptr; 9136 9137 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, 9138 Call->getBeginLoc(), Call->getRParenLoc())) 9139 return; 9140 9141 // Look for 'strlcpy(dst, x, sizeof(x))' 9142 if (const Expr *Ex = getSizeOfExprArg(SizeArg)) 9143 CompareWithSrc = Ex; 9144 else { 9145 // Look for 'strlcpy(dst, x, strlen(x))' 9146 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { 9147 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && 9148 SizeCall->getNumArgs() == 1) 9149 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); 9150 } 9151 } 9152 9153 if (!CompareWithSrc) 9154 return; 9155 9156 // Determine if the argument to sizeof/strlen is equal to the source 9157 // argument. In principle there's all kinds of things you could do 9158 // here, for instance creating an == expression and evaluating it with 9159 // EvaluateAsBooleanCondition, but this uses a more direct technique: 9160 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); 9161 if (!SrcArgDRE) 9162 return; 9163 9164 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); 9165 if (!CompareWithSrcDRE || 9166 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) 9167 return; 9168 9169 const Expr *OriginalSizeArg = Call->getArg(2); 9170 Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size) 9171 << OriginalSizeArg->getSourceRange() << FnName; 9172 9173 // Output a FIXIT hint if the destination is an array (rather than a 9174 // pointer to an array). This could be enhanced to handle some 9175 // pointers if we know the actual size, like if DstArg is 'array+2' 9176 // we could say 'sizeof(array)-2'. 9177 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); 9178 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) 9179 return; 9180 9181 SmallString<128> sizeString; 9182 llvm::raw_svector_ostream OS(sizeString); 9183 OS << "sizeof("; 9184 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9185 OS << ")"; 9186 9187 Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size) 9188 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), 9189 OS.str()); 9190 } 9191 9192 /// Check if two expressions refer to the same declaration. 9193 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { 9194 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) 9195 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) 9196 return D1->getDecl() == D2->getDecl(); 9197 return false; 9198 } 9199 9200 static const Expr *getStrlenExprArg(const Expr *E) { 9201 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 9202 const FunctionDecl *FD = CE->getDirectCallee(); 9203 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) 9204 return nullptr; 9205 return CE->getArg(0)->IgnoreParenCasts(); 9206 } 9207 return nullptr; 9208 } 9209 9210 void Sema::CheckStrncatArguments(const CallExpr *CE, 9211 IdentifierInfo *FnName) { 9212 // Don't crash if the user has the wrong number of arguments. 9213 if (CE->getNumArgs() < 3) 9214 return; 9215 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); 9216 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); 9217 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); 9218 9219 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(), 9220 CE->getRParenLoc())) 9221 return; 9222 9223 // Identify common expressions, which are wrongly used as the size argument 9224 // to strncat and may lead to buffer overflows. 9225 unsigned PatternType = 0; 9226 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { 9227 // - sizeof(dst) 9228 if (referToTheSameDecl(SizeOfArg, DstArg)) 9229 PatternType = 1; 9230 // - sizeof(src) 9231 else if (referToTheSameDecl(SizeOfArg, SrcArg)) 9232 PatternType = 2; 9233 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { 9234 if (BE->getOpcode() == BO_Sub) { 9235 const Expr *L = BE->getLHS()->IgnoreParenCasts(); 9236 const Expr *R = BE->getRHS()->IgnoreParenCasts(); 9237 // - sizeof(dst) - strlen(dst) 9238 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && 9239 referToTheSameDecl(DstArg, getStrlenExprArg(R))) 9240 PatternType = 1; 9241 // - sizeof(src) - (anything) 9242 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) 9243 PatternType = 2; 9244 } 9245 } 9246 9247 if (PatternType == 0) 9248 return; 9249 9250 // Generate the diagnostic. 9251 SourceLocation SL = LenArg->getBeginLoc(); 9252 SourceRange SR = LenArg->getSourceRange(); 9253 SourceManager &SM = getSourceManager(); 9254 9255 // If the function is defined as a builtin macro, do not show macro expansion. 9256 if (SM.isMacroArgExpansion(SL)) { 9257 SL = SM.getSpellingLoc(SL); 9258 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), 9259 SM.getSpellingLoc(SR.getEnd())); 9260 } 9261 9262 // Check if the destination is an array (rather than a pointer to an array). 9263 QualType DstTy = DstArg->getType(); 9264 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, 9265 Context); 9266 if (!isKnownSizeArray) { 9267 if (PatternType == 1) 9268 Diag(SL, diag::warn_strncat_wrong_size) << SR; 9269 else 9270 Diag(SL, diag::warn_strncat_src_size) << SR; 9271 return; 9272 } 9273 9274 if (PatternType == 1) 9275 Diag(SL, diag::warn_strncat_large_size) << SR; 9276 else 9277 Diag(SL, diag::warn_strncat_src_size) << SR; 9278 9279 SmallString<128> sizeString; 9280 llvm::raw_svector_ostream OS(sizeString); 9281 OS << "sizeof("; 9282 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9283 OS << ") - "; 9284 OS << "strlen("; 9285 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9286 OS << ") - 1"; 9287 9288 Diag(SL, diag::note_strncat_wrong_size) 9289 << FixItHint::CreateReplacement(SR, OS.str()); 9290 } 9291 9292 namespace { 9293 void CheckFreeArgumentsOnLvalue(Sema &S, const std::string &CalleeName, 9294 const UnaryOperator *UnaryExpr, const Decl *D) { 9295 if (isa<FieldDecl, FunctionDecl, VarDecl>(D)) { 9296 S.Diag(UnaryExpr->getBeginLoc(), diag::warn_free_nonheap_object) 9297 << CalleeName << 0 /*object: */ << cast<NamedDecl>(D); 9298 return; 9299 } 9300 } 9301 9302 void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName, 9303 const UnaryOperator *UnaryExpr) { 9304 if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) { 9305 const Decl *D = Lvalue->getDecl(); 9306 if (isa<DeclaratorDecl>(D)) 9307 if (!dyn_cast<DeclaratorDecl>(D)->getType()->isReferenceType()) 9308 return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D); 9309 } 9310 9311 if (const auto *Lvalue = dyn_cast<MemberExpr>(UnaryExpr->getSubExpr())) 9312 return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, 9313 Lvalue->getMemberDecl()); 9314 } 9315 9316 void CheckFreeArgumentsPlus(Sema &S, const std::string &CalleeName, 9317 const UnaryOperator *UnaryExpr) { 9318 const auto *Lambda = dyn_cast<LambdaExpr>( 9319 UnaryExpr->getSubExpr()->IgnoreImplicitAsWritten()->IgnoreParens()); 9320 if (!Lambda) 9321 return; 9322 9323 S.Diag(Lambda->getBeginLoc(), diag::warn_free_nonheap_object) 9324 << CalleeName << 2 /*object: lambda expression*/; 9325 } 9326 9327 void CheckFreeArgumentsStackArray(Sema &S, const std::string &CalleeName, 9328 const DeclRefExpr *Lvalue) { 9329 const auto *Var = dyn_cast<VarDecl>(Lvalue->getDecl()); 9330 if (Var == nullptr) 9331 return; 9332 9333 S.Diag(Lvalue->getBeginLoc(), diag::warn_free_nonheap_object) 9334 << CalleeName << 0 /*object: */ << Var; 9335 } 9336 9337 void CheckFreeArgumentsCast(Sema &S, const std::string &CalleeName, 9338 const CastExpr *Cast) { 9339 SmallString<128> SizeString; 9340 llvm::raw_svector_ostream OS(SizeString); 9341 9342 clang::CastKind Kind = Cast->getCastKind(); 9343 if (Kind == clang::CK_BitCast && 9344 !Cast->getSubExpr()->getType()->isFunctionPointerType()) 9345 return; 9346 if (Kind == clang::CK_IntegralToPointer && 9347 !isa<IntegerLiteral>( 9348 Cast->getSubExpr()->IgnoreParenImpCasts()->IgnoreParens())) 9349 return; 9350 9351 switch (Cast->getCastKind()) { 9352 case clang::CK_BitCast: 9353 case clang::CK_IntegralToPointer: 9354 case clang::CK_FunctionToPointerDecay: 9355 OS << '\''; 9356 Cast->printPretty(OS, nullptr, S.getPrintingPolicy()); 9357 OS << '\''; 9358 break; 9359 default: 9360 return; 9361 } 9362 9363 S.Diag(Cast->getBeginLoc(), diag::warn_free_nonheap_object) 9364 << CalleeName << 0 /*object: */ << OS.str(); 9365 } 9366 } // namespace 9367 9368 void Sema::CheckFreeArguments(const CallExpr *E) { 9369 const std::string CalleeName = 9370 cast<FunctionDecl>(E->getCalleeDecl())->getQualifiedNameAsString(); 9371 9372 { // Prefer something that doesn't involve a cast to make things simpler. 9373 const Expr *Arg = E->getArg(0)->IgnoreParenCasts(); 9374 if (const auto *UnaryExpr = dyn_cast<UnaryOperator>(Arg)) 9375 switch (UnaryExpr->getOpcode()) { 9376 case UnaryOperator::Opcode::UO_AddrOf: 9377 return CheckFreeArgumentsAddressof(*this, CalleeName, UnaryExpr); 9378 case UnaryOperator::Opcode::UO_Plus: 9379 return CheckFreeArgumentsPlus(*this, CalleeName, UnaryExpr); 9380 default: 9381 break; 9382 } 9383 9384 if (const auto *Lvalue = dyn_cast<DeclRefExpr>(Arg)) 9385 if (Lvalue->getType()->isArrayType()) 9386 return CheckFreeArgumentsStackArray(*this, CalleeName, Lvalue); 9387 9388 if (const auto *Label = dyn_cast<AddrLabelExpr>(Arg)) { 9389 Diag(Label->getBeginLoc(), diag::warn_free_nonheap_object) 9390 << CalleeName << 0 /*object: */ << Label->getLabel()->getIdentifier(); 9391 return; 9392 } 9393 9394 if (isa<BlockExpr>(Arg)) { 9395 Diag(Arg->getBeginLoc(), diag::warn_free_nonheap_object) 9396 << CalleeName << 1 /*object: block*/; 9397 return; 9398 } 9399 } 9400 // Maybe the cast was important, check after the other cases. 9401 if (const auto *Cast = dyn_cast<CastExpr>(E->getArg(0))) 9402 return CheckFreeArgumentsCast(*this, CalleeName, Cast); 9403 } 9404 9405 void 9406 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, 9407 SourceLocation ReturnLoc, 9408 bool isObjCMethod, 9409 const AttrVec *Attrs, 9410 const FunctionDecl *FD) { 9411 // Check if the return value is null but should not be. 9412 if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || 9413 (!isObjCMethod && isNonNullType(lhsType))) && 9414 CheckNonNullExpr(*this, RetValExp)) 9415 Diag(ReturnLoc, diag::warn_null_ret) 9416 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); 9417 9418 // C++11 [basic.stc.dynamic.allocation]p4: 9419 // If an allocation function declared with a non-throwing 9420 // exception-specification fails to allocate storage, it shall return 9421 // a null pointer. Any other allocation function that fails to allocate 9422 // storage shall indicate failure only by throwing an exception [...] 9423 if (FD) { 9424 OverloadedOperatorKind Op = FD->getOverloadedOperator(); 9425 if (Op == OO_New || Op == OO_Array_New) { 9426 const FunctionProtoType *Proto 9427 = FD->getType()->castAs<FunctionProtoType>(); 9428 if (!Proto->isNothrow(/*ResultIfDependent*/true) && 9429 CheckNonNullExpr(*this, RetValExp)) 9430 Diag(ReturnLoc, diag::warn_operator_new_returns_null) 9431 << FD << getLangOpts().CPlusPlus11; 9432 } 9433 } 9434 9435 if (RetValExp && RetValExp->getType()->isWebAssemblyTableType()) { 9436 Diag(ReturnLoc, diag::err_wasm_table_art) << 1; 9437 } 9438 9439 // PPC MMA non-pointer types are not allowed as return type. Checking the type 9440 // here prevent the user from using a PPC MMA type as trailing return type. 9441 if (Context.getTargetInfo().getTriple().isPPC64()) 9442 PPC().CheckPPCMMAType(RetValExp->getType(), ReturnLoc); 9443 } 9444 9445 void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS, 9446 BinaryOperatorKind Opcode) { 9447 if (!BinaryOperator::isEqualityOp(Opcode)) 9448 return; 9449 9450 // Match and capture subexpressions such as "(float) X == 0.1". 9451 FloatingLiteral *FPLiteral; 9452 CastExpr *FPCast; 9453 auto getCastAndLiteral = [&FPLiteral, &FPCast](Expr *L, Expr *R) { 9454 FPLiteral = dyn_cast<FloatingLiteral>(L->IgnoreParens()); 9455 FPCast = dyn_cast<CastExpr>(R->IgnoreParens()); 9456 return FPLiteral && FPCast; 9457 }; 9458 9459 if (getCastAndLiteral(LHS, RHS) || getCastAndLiteral(RHS, LHS)) { 9460 auto *SourceTy = FPCast->getSubExpr()->getType()->getAs<BuiltinType>(); 9461 auto *TargetTy = FPLiteral->getType()->getAs<BuiltinType>(); 9462 if (SourceTy && TargetTy && SourceTy->isFloatingPoint() && 9463 TargetTy->isFloatingPoint()) { 9464 bool Lossy; 9465 llvm::APFloat TargetC = FPLiteral->getValue(); 9466 TargetC.convert(Context.getFloatTypeSemantics(QualType(SourceTy, 0)), 9467 llvm::APFloat::rmNearestTiesToEven, &Lossy); 9468 if (Lossy) { 9469 // If the literal cannot be represented in the source type, then a 9470 // check for == is always false and check for != is always true. 9471 Diag(Loc, diag::warn_float_compare_literal) 9472 << (Opcode == BO_EQ) << QualType(SourceTy, 0) 9473 << LHS->getSourceRange() << RHS->getSourceRange(); 9474 return; 9475 } 9476 } 9477 } 9478 9479 // Match a more general floating-point equality comparison (-Wfloat-equal). 9480 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); 9481 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); 9482 9483 // Special case: check for x == x (which is OK). 9484 // Do not emit warnings for such cases. 9485 if (auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) 9486 if (auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) 9487 if (DRL->getDecl() == DRR->getDecl()) 9488 return; 9489 9490 // Special case: check for comparisons against literals that can be exactly 9491 // represented by APFloat. In such cases, do not emit a warning. This 9492 // is a heuristic: often comparison against such literals are used to 9493 // detect if a value in a variable has not changed. This clearly can 9494 // lead to false negatives. 9495 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { 9496 if (FLL->isExact()) 9497 return; 9498 } else 9499 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) 9500 if (FLR->isExact()) 9501 return; 9502 9503 // Check for comparisons with builtin types. 9504 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) 9505 if (CL->getBuiltinCallee()) 9506 return; 9507 9508 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) 9509 if (CR->getBuiltinCallee()) 9510 return; 9511 9512 // Emit the diagnostic. 9513 Diag(Loc, diag::warn_floatingpoint_eq) 9514 << LHS->getSourceRange() << RHS->getSourceRange(); 9515 } 9516 9517 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// 9518 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// 9519 9520 namespace { 9521 9522 /// Structure recording the 'active' range of an integer-valued 9523 /// expression. 9524 struct IntRange { 9525 /// The number of bits active in the int. Note that this includes exactly one 9526 /// sign bit if !NonNegative. 9527 unsigned Width; 9528 9529 /// True if the int is known not to have negative values. If so, all leading 9530 /// bits before Width are known zero, otherwise they are known to be the 9531 /// same as the MSB within Width. 9532 bool NonNegative; 9533 9534 IntRange(unsigned Width, bool NonNegative) 9535 : Width(Width), NonNegative(NonNegative) {} 9536 9537 /// Number of bits excluding the sign bit. 9538 unsigned valueBits() const { 9539 return NonNegative ? Width : Width - 1; 9540 } 9541 9542 /// Returns the range of the bool type. 9543 static IntRange forBoolType() { 9544 return IntRange(1, true); 9545 } 9546 9547 /// Returns the range of an opaque value of the given integral type. 9548 static IntRange forValueOfType(ASTContext &C, QualType T) { 9549 return forValueOfCanonicalType(C, 9550 T->getCanonicalTypeInternal().getTypePtr()); 9551 } 9552 9553 /// Returns the range of an opaque value of a canonical integral type. 9554 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { 9555 assert(T->isCanonicalUnqualified()); 9556 9557 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9558 T = VT->getElementType().getTypePtr(); 9559 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9560 T = CT->getElementType().getTypePtr(); 9561 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9562 T = AT->getValueType().getTypePtr(); 9563 9564 if (!C.getLangOpts().CPlusPlus) { 9565 // For enum types in C code, use the underlying datatype. 9566 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9567 T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr(); 9568 } else if (const EnumType *ET = dyn_cast<EnumType>(T)) { 9569 // For enum types in C++, use the known bit width of the enumerators. 9570 EnumDecl *Enum = ET->getDecl(); 9571 // In C++11, enums can have a fixed underlying type. Use this type to 9572 // compute the range. 9573 if (Enum->isFixed()) { 9574 return IntRange(C.getIntWidth(QualType(T, 0)), 9575 !ET->isSignedIntegerOrEnumerationType()); 9576 } 9577 9578 unsigned NumPositive = Enum->getNumPositiveBits(); 9579 unsigned NumNegative = Enum->getNumNegativeBits(); 9580 9581 if (NumNegative == 0) 9582 return IntRange(NumPositive, true/*NonNegative*/); 9583 else 9584 return IntRange(std::max(NumPositive + 1, NumNegative), 9585 false/*NonNegative*/); 9586 } 9587 9588 if (const auto *EIT = dyn_cast<BitIntType>(T)) 9589 return IntRange(EIT->getNumBits(), EIT->isUnsigned()); 9590 9591 const BuiltinType *BT = cast<BuiltinType>(T); 9592 assert(BT->isInteger()); 9593 9594 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9595 } 9596 9597 /// Returns the "target" range of a canonical integral type, i.e. 9598 /// the range of values expressible in the type. 9599 /// 9600 /// This matches forValueOfCanonicalType except that enums have the 9601 /// full range of their type, not the range of their enumerators. 9602 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { 9603 assert(T->isCanonicalUnqualified()); 9604 9605 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9606 T = VT->getElementType().getTypePtr(); 9607 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9608 T = CT->getElementType().getTypePtr(); 9609 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9610 T = AT->getValueType().getTypePtr(); 9611 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9612 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); 9613 9614 if (const auto *EIT = dyn_cast<BitIntType>(T)) 9615 return IntRange(EIT->getNumBits(), EIT->isUnsigned()); 9616 9617 const BuiltinType *BT = cast<BuiltinType>(T); 9618 assert(BT->isInteger()); 9619 9620 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9621 } 9622 9623 /// Returns the supremum of two ranges: i.e. their conservative merge. 9624 static IntRange join(IntRange L, IntRange R) { 9625 bool Unsigned = L.NonNegative && R.NonNegative; 9626 return IntRange(std::max(L.valueBits(), R.valueBits()) + !Unsigned, 9627 L.NonNegative && R.NonNegative); 9628 } 9629 9630 /// Return the range of a bitwise-AND of the two ranges. 9631 static IntRange bit_and(IntRange L, IntRange R) { 9632 unsigned Bits = std::max(L.Width, R.Width); 9633 bool NonNegative = false; 9634 if (L.NonNegative) { 9635 Bits = std::min(Bits, L.Width); 9636 NonNegative = true; 9637 } 9638 if (R.NonNegative) { 9639 Bits = std::min(Bits, R.Width); 9640 NonNegative = true; 9641 } 9642 return IntRange(Bits, NonNegative); 9643 } 9644 9645 /// Return the range of a sum of the two ranges. 9646 static IntRange sum(IntRange L, IntRange R) { 9647 bool Unsigned = L.NonNegative && R.NonNegative; 9648 return IntRange(std::max(L.valueBits(), R.valueBits()) + 1 + !Unsigned, 9649 Unsigned); 9650 } 9651 9652 /// Return the range of a difference of the two ranges. 9653 static IntRange difference(IntRange L, IntRange R) { 9654 // We need a 1-bit-wider range if: 9655 // 1) LHS can be negative: least value can be reduced. 9656 // 2) RHS can be negative: greatest value can be increased. 9657 bool CanWiden = !L.NonNegative || !R.NonNegative; 9658 bool Unsigned = L.NonNegative && R.Width == 0; 9659 return IntRange(std::max(L.valueBits(), R.valueBits()) + CanWiden + 9660 !Unsigned, 9661 Unsigned); 9662 } 9663 9664 /// Return the range of a product of the two ranges. 9665 static IntRange product(IntRange L, IntRange R) { 9666 // If both LHS and RHS can be negative, we can form 9667 // -2^L * -2^R = 2^(L + R) 9668 // which requires L + R + 1 value bits to represent. 9669 bool CanWiden = !L.NonNegative && !R.NonNegative; 9670 bool Unsigned = L.NonNegative && R.NonNegative; 9671 return IntRange(L.valueBits() + R.valueBits() + CanWiden + !Unsigned, 9672 Unsigned); 9673 } 9674 9675 /// Return the range of a remainder operation between the two ranges. 9676 static IntRange rem(IntRange L, IntRange R) { 9677 // The result of a remainder can't be larger than the result of 9678 // either side. The sign of the result is the sign of the LHS. 9679 bool Unsigned = L.NonNegative; 9680 return IntRange(std::min(L.valueBits(), R.valueBits()) + !Unsigned, 9681 Unsigned); 9682 } 9683 }; 9684 9685 } // namespace 9686 9687 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, 9688 unsigned MaxWidth) { 9689 if (value.isSigned() && value.isNegative()) 9690 return IntRange(value.getSignificantBits(), false); 9691 9692 if (value.getBitWidth() > MaxWidth) 9693 value = value.trunc(MaxWidth); 9694 9695 // isNonNegative() just checks the sign bit without considering 9696 // signedness. 9697 return IntRange(value.getActiveBits(), true); 9698 } 9699 9700 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, 9701 unsigned MaxWidth) { 9702 if (result.isInt()) 9703 return GetValueRange(C, result.getInt(), MaxWidth); 9704 9705 if (result.isVector()) { 9706 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); 9707 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { 9708 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); 9709 R = IntRange::join(R, El); 9710 } 9711 return R; 9712 } 9713 9714 if (result.isComplexInt()) { 9715 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); 9716 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); 9717 return IntRange::join(R, I); 9718 } 9719 9720 // This can happen with lossless casts to intptr_t of "based" lvalues. 9721 // Assume it might use arbitrary bits. 9722 // FIXME: The only reason we need to pass the type in here is to get 9723 // the sign right on this one case. It would be nice if APValue 9724 // preserved this. 9725 assert(result.isLValue() || result.isAddrLabelDiff()); 9726 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); 9727 } 9728 9729 static QualType GetExprType(const Expr *E) { 9730 QualType Ty = E->getType(); 9731 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) 9732 Ty = AtomicRHS->getValueType(); 9733 return Ty; 9734 } 9735 9736 /// Attempts to estimate an approximate range for the given integer expression. 9737 /// Returns a range if successful, otherwise it returns \c std::nullopt if a 9738 /// reliable estimation cannot be determined. 9739 /// 9740 /// \param MaxWidth The width to which the value will be truncated. 9741 /// \param InConstantContext If \c true, interpret the expression within a 9742 /// constant context. 9743 /// \param Approximate If \c true, provide a likely range of values by assuming 9744 /// that arithmetic on narrower types remains within those types. 9745 /// If \c false, return a range that includes all possible values 9746 /// resulting from the expression. 9747 /// \returns A range of values that the expression might take, or 9748 /// std::nullopt if a reliable estimation cannot be determined. 9749 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E, 9750 unsigned MaxWidth, 9751 bool InConstantContext, 9752 bool Approximate) { 9753 E = E->IgnoreParens(); 9754 9755 // Try a full evaluation first. 9756 Expr::EvalResult result; 9757 if (E->EvaluateAsRValue(result, C, InConstantContext)) 9758 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); 9759 9760 // I think we only want to look through implicit casts here; if the 9761 // user has an explicit widening cast, we should treat the value as 9762 // being of the new, wider type. 9763 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) { 9764 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) 9765 return TryGetExprRange(C, CE->getSubExpr(), MaxWidth, InConstantContext, 9766 Approximate); 9767 9768 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); 9769 9770 bool isIntegerCast = CE->getCastKind() == CK_IntegralCast || 9771 CE->getCastKind() == CK_BooleanToSignedIntegral; 9772 9773 // Assume that non-integer casts can span the full range of the type. 9774 if (!isIntegerCast) 9775 return OutputTypeRange; 9776 9777 std::optional<IntRange> SubRange = TryGetExprRange( 9778 C, CE->getSubExpr(), std::min(MaxWidth, OutputTypeRange.Width), 9779 InConstantContext, Approximate); 9780 if (!SubRange) 9781 return std::nullopt; 9782 9783 // Bail out if the subexpr's range is as wide as the cast type. 9784 if (SubRange->Width >= OutputTypeRange.Width) 9785 return OutputTypeRange; 9786 9787 // Otherwise, we take the smaller width, and we're non-negative if 9788 // either the output type or the subexpr is. 9789 return IntRange(SubRange->Width, 9790 SubRange->NonNegative || OutputTypeRange.NonNegative); 9791 } 9792 9793 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 9794 // If we can fold the condition, just take that operand. 9795 bool CondResult; 9796 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) 9797 return TryGetExprRange( 9798 C, CondResult ? CO->getTrueExpr() : CO->getFalseExpr(), MaxWidth, 9799 InConstantContext, Approximate); 9800 9801 // Otherwise, conservatively merge. 9802 // TryGetExprRange requires an integer expression, but a throw expression 9803 // results in a void type. 9804 Expr *TrueExpr = CO->getTrueExpr(); 9805 if (TrueExpr->getType()->isVoidType()) 9806 return std::nullopt; 9807 9808 std::optional<IntRange> L = 9809 TryGetExprRange(C, TrueExpr, MaxWidth, InConstantContext, Approximate); 9810 if (!L) 9811 return std::nullopt; 9812 9813 Expr *FalseExpr = CO->getFalseExpr(); 9814 if (FalseExpr->getType()->isVoidType()) 9815 return std::nullopt; 9816 9817 std::optional<IntRange> R = 9818 TryGetExprRange(C, FalseExpr, MaxWidth, InConstantContext, Approximate); 9819 if (!R) 9820 return std::nullopt; 9821 9822 return IntRange::join(*L, *R); 9823 } 9824 9825 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 9826 IntRange (*Combine)(IntRange, IntRange) = IntRange::join; 9827 9828 switch (BO->getOpcode()) { 9829 case BO_Cmp: 9830 llvm_unreachable("builtin <=> should have class type"); 9831 9832 // Boolean-valued operations are single-bit and positive. 9833 case BO_LAnd: 9834 case BO_LOr: 9835 case BO_LT: 9836 case BO_GT: 9837 case BO_LE: 9838 case BO_GE: 9839 case BO_EQ: 9840 case BO_NE: 9841 return IntRange::forBoolType(); 9842 9843 // The type of the assignments is the type of the LHS, so the RHS 9844 // is not necessarily the same type. 9845 case BO_MulAssign: 9846 case BO_DivAssign: 9847 case BO_RemAssign: 9848 case BO_AddAssign: 9849 case BO_SubAssign: 9850 case BO_XorAssign: 9851 case BO_OrAssign: 9852 // TODO: bitfields? 9853 return IntRange::forValueOfType(C, GetExprType(E)); 9854 9855 // Simple assignments just pass through the RHS, which will have 9856 // been coerced to the LHS type. 9857 case BO_Assign: 9858 // TODO: bitfields? 9859 return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, 9860 Approximate); 9861 9862 // Operations with opaque sources are black-listed. 9863 case BO_PtrMemD: 9864 case BO_PtrMemI: 9865 return IntRange::forValueOfType(C, GetExprType(E)); 9866 9867 // Bitwise-and uses the *infinum* of the two source ranges. 9868 case BO_And: 9869 case BO_AndAssign: 9870 Combine = IntRange::bit_and; 9871 break; 9872 9873 // Left shift gets black-listed based on a judgement call. 9874 case BO_Shl: 9875 // ...except that we want to treat '1 << (blah)' as logically 9876 // positive. It's an important idiom. 9877 if (IntegerLiteral *I 9878 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { 9879 if (I->getValue() == 1) { 9880 IntRange R = IntRange::forValueOfType(C, GetExprType(E)); 9881 return IntRange(R.Width, /*NonNegative*/ true); 9882 } 9883 } 9884 [[fallthrough]]; 9885 9886 case BO_ShlAssign: 9887 return IntRange::forValueOfType(C, GetExprType(E)); 9888 9889 // Right shift by a constant can narrow its left argument. 9890 case BO_Shr: 9891 case BO_ShrAssign: { 9892 std::optional<IntRange> L = TryGetExprRange( 9893 C, BO->getLHS(), MaxWidth, InConstantContext, Approximate); 9894 if (!L) 9895 return std::nullopt; 9896 9897 // If the shift amount is a positive constant, drop the width by 9898 // that much. 9899 if (std::optional<llvm::APSInt> shift = 9900 BO->getRHS()->getIntegerConstantExpr(C)) { 9901 if (shift->isNonNegative()) { 9902 if (shift->uge(L->Width)) 9903 L->Width = (L->NonNegative ? 0 : 1); 9904 else 9905 L->Width -= shift->getZExtValue(); 9906 } 9907 } 9908 9909 return L; 9910 } 9911 9912 // Comma acts as its right operand. 9913 case BO_Comma: 9914 return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, 9915 Approximate); 9916 9917 case BO_Add: 9918 if (!Approximate) 9919 Combine = IntRange::sum; 9920 break; 9921 9922 case BO_Sub: 9923 if (BO->getLHS()->getType()->isPointerType()) 9924 return IntRange::forValueOfType(C, GetExprType(E)); 9925 if (!Approximate) 9926 Combine = IntRange::difference; 9927 break; 9928 9929 case BO_Mul: 9930 if (!Approximate) 9931 Combine = IntRange::product; 9932 break; 9933 9934 // The width of a division result is mostly determined by the size 9935 // of the LHS. 9936 case BO_Div: { 9937 // Don't 'pre-truncate' the operands. 9938 unsigned opWidth = C.getIntWidth(GetExprType(E)); 9939 std::optional<IntRange> L = TryGetExprRange( 9940 C, BO->getLHS(), opWidth, InConstantContext, Approximate); 9941 if (!L) 9942 return std::nullopt; 9943 9944 // If the divisor is constant, use that. 9945 if (std::optional<llvm::APSInt> divisor = 9946 BO->getRHS()->getIntegerConstantExpr(C)) { 9947 unsigned log2 = divisor->logBase2(); // floor(log_2(divisor)) 9948 if (log2 >= L->Width) 9949 L->Width = (L->NonNegative ? 0 : 1); 9950 else 9951 L->Width = std::min(L->Width - log2, MaxWidth); 9952 return L; 9953 } 9954 9955 // Otherwise, just use the LHS's width. 9956 // FIXME: This is wrong if the LHS could be its minimal value and the RHS 9957 // could be -1. 9958 std::optional<IntRange> R = TryGetExprRange( 9959 C, BO->getRHS(), opWidth, InConstantContext, Approximate); 9960 if (!R) 9961 return std::nullopt; 9962 9963 return IntRange(L->Width, L->NonNegative && R->NonNegative); 9964 } 9965 9966 case BO_Rem: 9967 Combine = IntRange::rem; 9968 break; 9969 9970 // The default behavior is okay for these. 9971 case BO_Xor: 9972 case BO_Or: 9973 break; 9974 } 9975 9976 // Combine the two ranges, but limit the result to the type in which we 9977 // performed the computation. 9978 QualType T = GetExprType(E); 9979 unsigned opWidth = C.getIntWidth(T); 9980 std::optional<IntRange> L = TryGetExprRange(C, BO->getLHS(), opWidth, 9981 InConstantContext, Approximate); 9982 if (!L) 9983 return std::nullopt; 9984 9985 std::optional<IntRange> R = TryGetExprRange(C, BO->getRHS(), opWidth, 9986 InConstantContext, Approximate); 9987 if (!R) 9988 return std::nullopt; 9989 9990 IntRange C = Combine(*L, *R); 9991 C.NonNegative |= T->isUnsignedIntegerOrEnumerationType(); 9992 C.Width = std::min(C.Width, MaxWidth); 9993 return C; 9994 } 9995 9996 if (const auto *UO = dyn_cast<UnaryOperator>(E)) { 9997 switch (UO->getOpcode()) { 9998 // Boolean-valued operations are white-listed. 9999 case UO_LNot: 10000 return IntRange::forBoolType(); 10001 10002 // Operations with opaque sources are black-listed. 10003 case UO_Deref: 10004 case UO_AddrOf: // should be impossible 10005 return IntRange::forValueOfType(C, GetExprType(E)); 10006 10007 default: 10008 return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext, 10009 Approximate); 10010 } 10011 } 10012 10013 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 10014 return TryGetExprRange(C, OVE->getSourceExpr(), MaxWidth, InConstantContext, 10015 Approximate); 10016 10017 if (const auto *BitField = E->getSourceBitField()) 10018 return IntRange(BitField->getBitWidthValue(C), 10019 BitField->getType()->isUnsignedIntegerOrEnumerationType()); 10020 10021 if (GetExprType(E)->isVoidType()) 10022 return std::nullopt; 10023 10024 return IntRange::forValueOfType(C, GetExprType(E)); 10025 } 10026 10027 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E, 10028 bool InConstantContext, 10029 bool Approximate) { 10030 return TryGetExprRange(C, E, C.getIntWidth(GetExprType(E)), InConstantContext, 10031 Approximate); 10032 } 10033 10034 /// Checks whether the given value, which currently has the given 10035 /// source semantics, has the same value when coerced through the 10036 /// target semantics. 10037 static bool IsSameFloatAfterCast(const llvm::APFloat &value, 10038 const llvm::fltSemantics &Src, 10039 const llvm::fltSemantics &Tgt) { 10040 llvm::APFloat truncated = value; 10041 10042 bool ignored; 10043 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); 10044 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); 10045 10046 return truncated.bitwiseIsEqual(value); 10047 } 10048 10049 /// Checks whether the given value, which currently has the given 10050 /// source semantics, has the same value when coerced through the 10051 /// target semantics. 10052 /// 10053 /// The value might be a vector of floats (or a complex number). 10054 static bool IsSameFloatAfterCast(const APValue &value, 10055 const llvm::fltSemantics &Src, 10056 const llvm::fltSemantics &Tgt) { 10057 if (value.isFloat()) 10058 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); 10059 10060 if (value.isVector()) { 10061 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) 10062 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) 10063 return false; 10064 return true; 10065 } 10066 10067 assert(value.isComplexFloat()); 10068 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && 10069 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); 10070 } 10071 10072 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC, 10073 bool IsListInit = false); 10074 10075 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) { 10076 // Suppress cases where we are comparing against an enum constant. 10077 if (const DeclRefExpr *DR = 10078 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) 10079 if (isa<EnumConstantDecl>(DR->getDecl())) 10080 return true; 10081 10082 // Suppress cases where the value is expanded from a macro, unless that macro 10083 // is how a language represents a boolean literal. This is the case in both C 10084 // and Objective-C. 10085 SourceLocation BeginLoc = E->getBeginLoc(); 10086 if (BeginLoc.isMacroID()) { 10087 StringRef MacroName = Lexer::getImmediateMacroName( 10088 BeginLoc, S.getSourceManager(), S.getLangOpts()); 10089 return MacroName != "YES" && MacroName != "NO" && 10090 MacroName != "true" && MacroName != "false"; 10091 } 10092 10093 return false; 10094 } 10095 10096 static bool isKnownToHaveUnsignedValue(Expr *E) { 10097 return E->getType()->isIntegerType() && 10098 (!E->getType()->isSignedIntegerType() || 10099 !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType()); 10100 } 10101 10102 namespace { 10103 /// The promoted range of values of a type. In general this has the 10104 /// following structure: 10105 /// 10106 /// |-----------| . . . |-----------| 10107 /// ^ ^ ^ ^ 10108 /// Min HoleMin HoleMax Max 10109 /// 10110 /// ... where there is only a hole if a signed type is promoted to unsigned 10111 /// (in which case Min and Max are the smallest and largest representable 10112 /// values). 10113 struct PromotedRange { 10114 // Min, or HoleMax if there is a hole. 10115 llvm::APSInt PromotedMin; 10116 // Max, or HoleMin if there is a hole. 10117 llvm::APSInt PromotedMax; 10118 10119 PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) { 10120 if (R.Width == 0) 10121 PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned); 10122 else if (R.Width >= BitWidth && !Unsigned) { 10123 // Promotion made the type *narrower*. This happens when promoting 10124 // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'. 10125 // Treat all values of 'signed int' as being in range for now. 10126 PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned); 10127 PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned); 10128 } else { 10129 PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative) 10130 .extOrTrunc(BitWidth); 10131 PromotedMin.setIsUnsigned(Unsigned); 10132 10133 PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative) 10134 .extOrTrunc(BitWidth); 10135 PromotedMax.setIsUnsigned(Unsigned); 10136 } 10137 } 10138 10139 // Determine whether this range is contiguous (has no hole). 10140 bool isContiguous() const { return PromotedMin <= PromotedMax; } 10141 10142 // Where a constant value is within the range. 10143 enum ComparisonResult { 10144 LT = 0x1, 10145 LE = 0x2, 10146 GT = 0x4, 10147 GE = 0x8, 10148 EQ = 0x10, 10149 NE = 0x20, 10150 InRangeFlag = 0x40, 10151 10152 Less = LE | LT | NE, 10153 Min = LE | InRangeFlag, 10154 InRange = InRangeFlag, 10155 Max = GE | InRangeFlag, 10156 Greater = GE | GT | NE, 10157 10158 OnlyValue = LE | GE | EQ | InRangeFlag, 10159 InHole = NE 10160 }; 10161 10162 ComparisonResult compare(const llvm::APSInt &Value) const { 10163 assert(Value.getBitWidth() == PromotedMin.getBitWidth() && 10164 Value.isUnsigned() == PromotedMin.isUnsigned()); 10165 if (!isContiguous()) { 10166 assert(Value.isUnsigned() && "discontiguous range for signed compare"); 10167 if (Value.isMinValue()) return Min; 10168 if (Value.isMaxValue()) return Max; 10169 if (Value >= PromotedMin) return InRange; 10170 if (Value <= PromotedMax) return InRange; 10171 return InHole; 10172 } 10173 10174 switch (llvm::APSInt::compareValues(Value, PromotedMin)) { 10175 case -1: return Less; 10176 case 0: return PromotedMin == PromotedMax ? OnlyValue : Min; 10177 case 1: 10178 switch (llvm::APSInt::compareValues(Value, PromotedMax)) { 10179 case -1: return InRange; 10180 case 0: return Max; 10181 case 1: return Greater; 10182 } 10183 } 10184 10185 llvm_unreachable("impossible compare result"); 10186 } 10187 10188 static std::optional<StringRef> 10189 constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) { 10190 if (Op == BO_Cmp) { 10191 ComparisonResult LTFlag = LT, GTFlag = GT; 10192 if (ConstantOnRHS) std::swap(LTFlag, GTFlag); 10193 10194 if (R & EQ) return StringRef("'std::strong_ordering::equal'"); 10195 if (R & LTFlag) return StringRef("'std::strong_ordering::less'"); 10196 if (R & GTFlag) return StringRef("'std::strong_ordering::greater'"); 10197 return std::nullopt; 10198 } 10199 10200 ComparisonResult TrueFlag, FalseFlag; 10201 if (Op == BO_EQ) { 10202 TrueFlag = EQ; 10203 FalseFlag = NE; 10204 } else if (Op == BO_NE) { 10205 TrueFlag = NE; 10206 FalseFlag = EQ; 10207 } else { 10208 if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) { 10209 TrueFlag = LT; 10210 FalseFlag = GE; 10211 } else { 10212 TrueFlag = GT; 10213 FalseFlag = LE; 10214 } 10215 if (Op == BO_GE || Op == BO_LE) 10216 std::swap(TrueFlag, FalseFlag); 10217 } 10218 if (R & TrueFlag) 10219 return StringRef("true"); 10220 if (R & FalseFlag) 10221 return StringRef("false"); 10222 return std::nullopt; 10223 } 10224 }; 10225 } 10226 10227 static bool HasEnumType(Expr *E) { 10228 // Strip off implicit integral promotions. 10229 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 10230 if (ICE->getCastKind() != CK_IntegralCast && 10231 ICE->getCastKind() != CK_NoOp) 10232 break; 10233 E = ICE->getSubExpr(); 10234 } 10235 10236 return E->getType()->isEnumeralType(); 10237 } 10238 10239 static int classifyConstantValue(Expr *Constant) { 10240 // The values of this enumeration are used in the diagnostics 10241 // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare. 10242 enum ConstantValueKind { 10243 Miscellaneous = 0, 10244 LiteralTrue, 10245 LiteralFalse 10246 }; 10247 if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant)) 10248 return BL->getValue() ? ConstantValueKind::LiteralTrue 10249 : ConstantValueKind::LiteralFalse; 10250 return ConstantValueKind::Miscellaneous; 10251 } 10252 10253 static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E, 10254 Expr *Constant, Expr *Other, 10255 const llvm::APSInt &Value, 10256 bool RhsConstant) { 10257 if (S.inTemplateInstantiation()) 10258 return false; 10259 10260 Expr *OriginalOther = Other; 10261 10262 Constant = Constant->IgnoreParenImpCasts(); 10263 Other = Other->IgnoreParenImpCasts(); 10264 10265 // Suppress warnings on tautological comparisons between values of the same 10266 // enumeration type. There are only two ways we could warn on this: 10267 // - If the constant is outside the range of representable values of 10268 // the enumeration. In such a case, we should warn about the cast 10269 // to enumeration type, not about the comparison. 10270 // - If the constant is the maximum / minimum in-range value. For an 10271 // enumeratin type, such comparisons can be meaningful and useful. 10272 if (Constant->getType()->isEnumeralType() && 10273 S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType())) 10274 return false; 10275 10276 std::optional<IntRange> OtherValueRange = TryGetExprRange( 10277 S.Context, Other, S.isConstantEvaluatedContext(), /*Approximate=*/false); 10278 if (!OtherValueRange) 10279 return false; 10280 10281 QualType OtherT = Other->getType(); 10282 if (const auto *AT = OtherT->getAs<AtomicType>()) 10283 OtherT = AT->getValueType(); 10284 IntRange OtherTypeRange = IntRange::forValueOfType(S.Context, OtherT); 10285 10286 // Special case for ObjC BOOL on targets where its a typedef for a signed char 10287 // (Namely, macOS). FIXME: IntRange::forValueOfType should do this. 10288 bool IsObjCSignedCharBool = S.getLangOpts().ObjC && 10289 S.ObjC().NSAPIObj->isObjCBOOLType(OtherT) && 10290 OtherT->isSpecificBuiltinType(BuiltinType::SChar); 10291 10292 // Whether we're treating Other as being a bool because of the form of 10293 // expression despite it having another type (typically 'int' in C). 10294 bool OtherIsBooleanDespiteType = 10295 !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue(); 10296 if (OtherIsBooleanDespiteType || IsObjCSignedCharBool) 10297 OtherTypeRange = *OtherValueRange = IntRange::forBoolType(); 10298 10299 // Check if all values in the range of possible values of this expression 10300 // lead to the same comparison outcome. 10301 PromotedRange OtherPromotedValueRange(*OtherValueRange, Value.getBitWidth(), 10302 Value.isUnsigned()); 10303 auto Cmp = OtherPromotedValueRange.compare(Value); 10304 auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant); 10305 if (!Result) 10306 return false; 10307 10308 // Also consider the range determined by the type alone. This allows us to 10309 // classify the warning under the proper diagnostic group. 10310 bool TautologicalTypeCompare = false; 10311 { 10312 PromotedRange OtherPromotedTypeRange(OtherTypeRange, Value.getBitWidth(), 10313 Value.isUnsigned()); 10314 auto TypeCmp = OtherPromotedTypeRange.compare(Value); 10315 if (auto TypeResult = PromotedRange::constantValue(E->getOpcode(), TypeCmp, 10316 RhsConstant)) { 10317 TautologicalTypeCompare = true; 10318 Cmp = TypeCmp; 10319 Result = TypeResult; 10320 } 10321 } 10322 10323 // Don't warn if the non-constant operand actually always evaluates to the 10324 // same value. 10325 if (!TautologicalTypeCompare && OtherValueRange->Width == 0) 10326 return false; 10327 10328 // Suppress the diagnostic for an in-range comparison if the constant comes 10329 // from a macro or enumerator. We don't want to diagnose 10330 // 10331 // some_long_value <= INT_MAX 10332 // 10333 // when sizeof(int) == sizeof(long). 10334 bool InRange = Cmp & PromotedRange::InRangeFlag; 10335 if (InRange && IsEnumConstOrFromMacro(S, Constant)) 10336 return false; 10337 10338 // A comparison of an unsigned bit-field against 0 is really a type problem, 10339 // even though at the type level the bit-field might promote to 'signed int'. 10340 if (Other->refersToBitField() && InRange && Value == 0 && 10341 Other->getType()->isUnsignedIntegerOrEnumerationType()) 10342 TautologicalTypeCompare = true; 10343 10344 // If this is a comparison to an enum constant, include that 10345 // constant in the diagnostic. 10346 const EnumConstantDecl *ED = nullptr; 10347 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) 10348 ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); 10349 10350 // Should be enough for uint128 (39 decimal digits) 10351 SmallString<64> PrettySourceValue; 10352 llvm::raw_svector_ostream OS(PrettySourceValue); 10353 if (ED) { 10354 OS << '\'' << *ED << "' (" << Value << ")"; 10355 } else if (auto *BL = dyn_cast<ObjCBoolLiteralExpr>( 10356 Constant->IgnoreParenImpCasts())) { 10357 OS << (BL->getValue() ? "YES" : "NO"); 10358 } else { 10359 OS << Value; 10360 } 10361 10362 if (!TautologicalTypeCompare) { 10363 S.Diag(E->getOperatorLoc(), diag::warn_tautological_compare_value_range) 10364 << RhsConstant << OtherValueRange->Width << OtherValueRange->NonNegative 10365 << E->getOpcodeStr() << OS.str() << *Result 10366 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10367 return true; 10368 } 10369 10370 if (IsObjCSignedCharBool) { 10371 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10372 S.PDiag(diag::warn_tautological_compare_objc_bool) 10373 << OS.str() << *Result); 10374 return true; 10375 } 10376 10377 // FIXME: We use a somewhat different formatting for the in-range cases and 10378 // cases involving boolean values for historical reasons. We should pick a 10379 // consistent way of presenting these diagnostics. 10380 if (!InRange || Other->isKnownToHaveBooleanValue()) { 10381 10382 S.DiagRuntimeBehavior( 10383 E->getOperatorLoc(), E, 10384 S.PDiag(!InRange ? diag::warn_out_of_range_compare 10385 : diag::warn_tautological_bool_compare) 10386 << OS.str() << classifyConstantValue(Constant) << OtherT 10387 << OtherIsBooleanDespiteType << *Result 10388 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); 10389 } else { 10390 bool IsCharTy = OtherT.withoutLocalFastQualifiers() == S.Context.CharTy; 10391 unsigned Diag = 10392 (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0) 10393 ? (HasEnumType(OriginalOther) 10394 ? diag::warn_unsigned_enum_always_true_comparison 10395 : IsCharTy ? diag::warn_unsigned_char_always_true_comparison 10396 : diag::warn_unsigned_always_true_comparison) 10397 : diag::warn_tautological_constant_compare; 10398 10399 S.Diag(E->getOperatorLoc(), Diag) 10400 << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result 10401 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10402 } 10403 10404 return true; 10405 } 10406 10407 /// Analyze the operands of the given comparison. Implements the 10408 /// fallback case from AnalyzeComparison. 10409 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { 10410 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10411 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10412 } 10413 10414 /// Implements -Wsign-compare. 10415 /// 10416 /// \param E the binary operator to check for warnings 10417 static void AnalyzeComparison(Sema &S, BinaryOperator *E) { 10418 // The type the comparison is being performed in. 10419 QualType T = E->getLHS()->getType(); 10420 10421 // Only analyze comparison operators where both sides have been converted to 10422 // the same type. 10423 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) 10424 return AnalyzeImpConvsInComparison(S, E); 10425 10426 // Don't analyze value-dependent comparisons directly. 10427 if (E->isValueDependent()) 10428 return AnalyzeImpConvsInComparison(S, E); 10429 10430 Expr *LHS = E->getLHS(); 10431 Expr *RHS = E->getRHS(); 10432 10433 if (T->isIntegralType(S.Context)) { 10434 std::optional<llvm::APSInt> RHSValue = 10435 RHS->getIntegerConstantExpr(S.Context); 10436 std::optional<llvm::APSInt> LHSValue = 10437 LHS->getIntegerConstantExpr(S.Context); 10438 10439 // We don't care about expressions whose result is a constant. 10440 if (RHSValue && LHSValue) 10441 return AnalyzeImpConvsInComparison(S, E); 10442 10443 // We only care about expressions where just one side is literal 10444 if ((bool)RHSValue ^ (bool)LHSValue) { 10445 // Is the constant on the RHS or LHS? 10446 const bool RhsConstant = (bool)RHSValue; 10447 Expr *Const = RhsConstant ? RHS : LHS; 10448 Expr *Other = RhsConstant ? LHS : RHS; 10449 const llvm::APSInt &Value = RhsConstant ? *RHSValue : *LHSValue; 10450 10451 // Check whether an integer constant comparison results in a value 10452 // of 'true' or 'false'. 10453 if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant)) 10454 return AnalyzeImpConvsInComparison(S, E); 10455 } 10456 } 10457 10458 if (!T->hasUnsignedIntegerRepresentation()) { 10459 // We don't do anything special if this isn't an unsigned integral 10460 // comparison: we're only interested in integral comparisons, and 10461 // signed comparisons only happen in cases we don't care to warn about. 10462 return AnalyzeImpConvsInComparison(S, E); 10463 } 10464 10465 LHS = LHS->IgnoreParenImpCasts(); 10466 RHS = RHS->IgnoreParenImpCasts(); 10467 10468 if (!S.getLangOpts().CPlusPlus) { 10469 // Avoid warning about comparison of integers with different signs when 10470 // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of 10471 // the type of `E`. 10472 if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType())) 10473 LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10474 if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType())) 10475 RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10476 } 10477 10478 // Check to see if one of the (unmodified) operands is of different 10479 // signedness. 10480 Expr *signedOperand, *unsignedOperand; 10481 if (LHS->getType()->hasSignedIntegerRepresentation()) { 10482 assert(!RHS->getType()->hasSignedIntegerRepresentation() && 10483 "unsigned comparison between two signed integer expressions?"); 10484 signedOperand = LHS; 10485 unsignedOperand = RHS; 10486 } else if (RHS->getType()->hasSignedIntegerRepresentation()) { 10487 signedOperand = RHS; 10488 unsignedOperand = LHS; 10489 } else { 10490 return AnalyzeImpConvsInComparison(S, E); 10491 } 10492 10493 // Otherwise, calculate the effective range of the signed operand. 10494 std::optional<IntRange> signedRange = 10495 TryGetExprRange(S.Context, signedOperand, S.isConstantEvaluatedContext(), 10496 /*Approximate=*/true); 10497 if (!signedRange) 10498 return; 10499 10500 // Go ahead and analyze implicit conversions in the operands. Note 10501 // that we skip the implicit conversions on both sides. 10502 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); 10503 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); 10504 10505 // If the signed range is non-negative, -Wsign-compare won't fire. 10506 if (signedRange->NonNegative) 10507 return; 10508 10509 // For (in)equality comparisons, if the unsigned operand is a 10510 // constant which cannot collide with a overflowed signed operand, 10511 // then reinterpreting the signed operand as unsigned will not 10512 // change the result of the comparison. 10513 if (E->isEqualityOp()) { 10514 unsigned comparisonWidth = S.Context.getIntWidth(T); 10515 std::optional<IntRange> unsignedRange = TryGetExprRange( 10516 S.Context, unsignedOperand, S.isConstantEvaluatedContext(), 10517 /*Approximate=*/true); 10518 if (!unsignedRange) 10519 return; 10520 10521 // We should never be unable to prove that the unsigned operand is 10522 // non-negative. 10523 assert(unsignedRange->NonNegative && "unsigned range includes negative?"); 10524 10525 if (unsignedRange->Width < comparisonWidth) 10526 return; 10527 } 10528 10529 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10530 S.PDiag(diag::warn_mixed_sign_comparison) 10531 << LHS->getType() << RHS->getType() 10532 << LHS->getSourceRange() << RHS->getSourceRange()); 10533 } 10534 10535 /// Analyzes an attempt to assign the given value to a bitfield. 10536 /// 10537 /// Returns true if there was something fishy about the attempt. 10538 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, 10539 SourceLocation InitLoc) { 10540 assert(Bitfield->isBitField()); 10541 if (Bitfield->isInvalidDecl()) 10542 return false; 10543 10544 // White-list bool bitfields. 10545 QualType BitfieldType = Bitfield->getType(); 10546 if (BitfieldType->isBooleanType()) 10547 return false; 10548 10549 if (BitfieldType->isEnumeralType()) { 10550 EnumDecl *BitfieldEnumDecl = BitfieldType->castAs<EnumType>()->getDecl(); 10551 // If the underlying enum type was not explicitly specified as an unsigned 10552 // type and the enum contain only positive values, MSVC++ will cause an 10553 // inconsistency by storing this as a signed type. 10554 if (S.getLangOpts().CPlusPlus11 && 10555 !BitfieldEnumDecl->getIntegerTypeSourceInfo() && 10556 BitfieldEnumDecl->getNumPositiveBits() > 0 && 10557 BitfieldEnumDecl->getNumNegativeBits() == 0) { 10558 S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield) 10559 << BitfieldEnumDecl; 10560 } 10561 } 10562 10563 // Ignore value- or type-dependent expressions. 10564 if (Bitfield->getBitWidth()->isValueDependent() || 10565 Bitfield->getBitWidth()->isTypeDependent() || 10566 Init->isValueDependent() || 10567 Init->isTypeDependent()) 10568 return false; 10569 10570 Expr *OriginalInit = Init->IgnoreParenImpCasts(); 10571 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); 10572 10573 Expr::EvalResult Result; 10574 if (!OriginalInit->EvaluateAsInt(Result, S.Context, 10575 Expr::SE_AllowSideEffects)) { 10576 // The RHS is not constant. If the RHS has an enum type, make sure the 10577 // bitfield is wide enough to hold all the values of the enum without 10578 // truncation. 10579 if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) { 10580 EnumDecl *ED = EnumTy->getDecl(); 10581 bool SignedBitfield = BitfieldType->isSignedIntegerType(); 10582 10583 // Enum types are implicitly signed on Windows, so check if there are any 10584 // negative enumerators to see if the enum was intended to be signed or 10585 // not. 10586 bool SignedEnum = ED->getNumNegativeBits() > 0; 10587 10588 // Check for surprising sign changes when assigning enum values to a 10589 // bitfield of different signedness. If the bitfield is signed and we 10590 // have exactly the right number of bits to store this unsigned enum, 10591 // suggest changing the enum to an unsigned type. This typically happens 10592 // on Windows where unfixed enums always use an underlying type of 'int'. 10593 unsigned DiagID = 0; 10594 if (SignedEnum && !SignedBitfield) { 10595 DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum; 10596 } else if (SignedBitfield && !SignedEnum && 10597 ED->getNumPositiveBits() == FieldWidth) { 10598 DiagID = diag::warn_signed_bitfield_enum_conversion; 10599 } 10600 10601 if (DiagID) { 10602 S.Diag(InitLoc, DiagID) << Bitfield << ED; 10603 TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo(); 10604 SourceRange TypeRange = 10605 TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange(); 10606 S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign) 10607 << SignedEnum << TypeRange; 10608 } 10609 10610 // Compute the required bitwidth. If the enum has negative values, we need 10611 // one more bit than the normal number of positive bits to represent the 10612 // sign bit. 10613 unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1, 10614 ED->getNumNegativeBits()) 10615 : ED->getNumPositiveBits(); 10616 10617 // Check the bitwidth. 10618 if (BitsNeeded > FieldWidth) { 10619 Expr *WidthExpr = Bitfield->getBitWidth(); 10620 S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum) 10621 << Bitfield << ED; 10622 S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield) 10623 << BitsNeeded << ED << WidthExpr->getSourceRange(); 10624 } 10625 } 10626 10627 return false; 10628 } 10629 10630 llvm::APSInt Value = Result.Val.getInt(); 10631 10632 unsigned OriginalWidth = Value.getBitWidth(); 10633 10634 // In C, the macro 'true' from stdbool.h will evaluate to '1'; To reduce 10635 // false positives where the user is demonstrating they intend to use the 10636 // bit-field as a Boolean, check to see if the value is 1 and we're assigning 10637 // to a one-bit bit-field to see if the value came from a macro named 'true'. 10638 bool OneAssignedToOneBitBitfield = FieldWidth == 1 && Value == 1; 10639 if (OneAssignedToOneBitBitfield && !S.LangOpts.CPlusPlus) { 10640 SourceLocation MaybeMacroLoc = OriginalInit->getBeginLoc(); 10641 if (S.SourceMgr.isInSystemMacro(MaybeMacroLoc) && 10642 S.findMacroSpelling(MaybeMacroLoc, "true")) 10643 return false; 10644 } 10645 10646 if (!Value.isSigned() || Value.isNegative()) 10647 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit)) 10648 if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not) 10649 OriginalWidth = Value.getSignificantBits(); 10650 10651 if (OriginalWidth <= FieldWidth) 10652 return false; 10653 10654 // Compute the value which the bitfield will contain. 10655 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); 10656 TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType()); 10657 10658 // Check whether the stored value is equal to the original value. 10659 TruncatedValue = TruncatedValue.extend(OriginalWidth); 10660 if (llvm::APSInt::isSameValue(Value, TruncatedValue)) 10661 return false; 10662 10663 std::string PrettyValue = toString(Value, 10); 10664 std::string PrettyTrunc = toString(TruncatedValue, 10); 10665 10666 S.Diag(InitLoc, OneAssignedToOneBitBitfield 10667 ? diag::warn_impcast_single_bit_bitield_precision_constant 10668 : diag::warn_impcast_bitfield_precision_constant) 10669 << PrettyValue << PrettyTrunc << OriginalInit->getType() 10670 << Init->getSourceRange(); 10671 10672 return true; 10673 } 10674 10675 /// Analyze the given simple or compound assignment for warning-worthy 10676 /// operations. 10677 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { 10678 // Just recurse on the LHS. 10679 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10680 10681 // We want to recurse on the RHS as normal unless we're assigning to 10682 // a bitfield. 10683 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { 10684 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), 10685 E->getOperatorLoc())) { 10686 // Recurse, ignoring any implicit conversions on the RHS. 10687 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), 10688 E->getOperatorLoc()); 10689 } 10690 } 10691 10692 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10693 10694 // Diagnose implicitly sequentially-consistent atomic assignment. 10695 if (E->getLHS()->getType()->isAtomicType()) 10696 S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 10697 } 10698 10699 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10700 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 10701 SourceLocation CContext, unsigned diag, 10702 bool pruneControlFlow = false) { 10703 if (pruneControlFlow) { 10704 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10705 S.PDiag(diag) 10706 << SourceType << T << E->getSourceRange() 10707 << SourceRange(CContext)); 10708 return; 10709 } 10710 S.Diag(E->getExprLoc(), diag) 10711 << SourceType << T << E->getSourceRange() << SourceRange(CContext); 10712 } 10713 10714 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10715 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, 10716 SourceLocation CContext, 10717 unsigned diag, bool pruneControlFlow = false) { 10718 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); 10719 } 10720 10721 /// Diagnose an implicit cast from a floating point value to an integer value. 10722 static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T, 10723 SourceLocation CContext) { 10724 const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool); 10725 const bool PruneWarnings = S.inTemplateInstantiation(); 10726 10727 Expr *InnerE = E->IgnoreParenImpCasts(); 10728 // We also want to warn on, e.g., "int i = -1.234" 10729 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) 10730 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) 10731 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); 10732 10733 const bool IsLiteral = 10734 isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE); 10735 10736 llvm::APFloat Value(0.0); 10737 bool IsConstant = 10738 E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects); 10739 if (!IsConstant) { 10740 if (S.ObjC().isSignedCharBool(T)) { 10741 return S.ObjC().adornBoolConversionDiagWithTernaryFixit( 10742 E, S.Diag(CContext, diag::warn_impcast_float_to_objc_signed_char_bool) 10743 << E->getType()); 10744 } 10745 10746 return DiagnoseImpCast(S, E, T, CContext, 10747 diag::warn_impcast_float_integer, PruneWarnings); 10748 } 10749 10750 bool isExact = false; 10751 10752 llvm::APSInt IntegerValue(S.Context.getIntWidth(T), 10753 T->hasUnsignedIntegerRepresentation()); 10754 llvm::APFloat::opStatus Result = Value.convertToInteger( 10755 IntegerValue, llvm::APFloat::rmTowardZero, &isExact); 10756 10757 // FIXME: Force the precision of the source value down so we don't print 10758 // digits which are usually useless (we don't really care here if we 10759 // truncate a digit by accident in edge cases). Ideally, APFloat::toString 10760 // would automatically print the shortest representation, but it's a bit 10761 // tricky to implement. 10762 SmallString<16> PrettySourceValue; 10763 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); 10764 precision = (precision * 59 + 195) / 196; 10765 Value.toString(PrettySourceValue, precision); 10766 10767 if (S.ObjC().isSignedCharBool(T) && IntegerValue != 0 && IntegerValue != 1) { 10768 return S.ObjC().adornBoolConversionDiagWithTernaryFixit( 10769 E, S.Diag(CContext, diag::warn_impcast_constant_value_to_objc_bool) 10770 << PrettySourceValue); 10771 } 10772 10773 if (Result == llvm::APFloat::opOK && isExact) { 10774 if (IsLiteral) return; 10775 return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer, 10776 PruneWarnings); 10777 } 10778 10779 // Conversion of a floating-point value to a non-bool integer where the 10780 // integral part cannot be represented by the integer type is undefined. 10781 if (!IsBool && Result == llvm::APFloat::opInvalidOp) 10782 return DiagnoseImpCast( 10783 S, E, T, CContext, 10784 IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range 10785 : diag::warn_impcast_float_to_integer_out_of_range, 10786 PruneWarnings); 10787 10788 unsigned DiagID = 0; 10789 if (IsLiteral) { 10790 // Warn on floating point literal to integer. 10791 DiagID = diag::warn_impcast_literal_float_to_integer; 10792 } else if (IntegerValue == 0) { 10793 if (Value.isZero()) { // Skip -0.0 to 0 conversion. 10794 return DiagnoseImpCast(S, E, T, CContext, 10795 diag::warn_impcast_float_integer, PruneWarnings); 10796 } 10797 // Warn on non-zero to zero conversion. 10798 DiagID = diag::warn_impcast_float_to_integer_zero; 10799 } else { 10800 if (IntegerValue.isUnsigned()) { 10801 if (!IntegerValue.isMaxValue()) { 10802 return DiagnoseImpCast(S, E, T, CContext, 10803 diag::warn_impcast_float_integer, PruneWarnings); 10804 } 10805 } else { // IntegerValue.isSigned() 10806 if (!IntegerValue.isMaxSignedValue() && 10807 !IntegerValue.isMinSignedValue()) { 10808 return DiagnoseImpCast(S, E, T, CContext, 10809 diag::warn_impcast_float_integer, PruneWarnings); 10810 } 10811 } 10812 // Warn on evaluatable floating point expression to integer conversion. 10813 DiagID = diag::warn_impcast_float_to_integer; 10814 } 10815 10816 SmallString<16> PrettyTargetValue; 10817 if (IsBool) 10818 PrettyTargetValue = Value.isZero() ? "false" : "true"; 10819 else 10820 IntegerValue.toString(PrettyTargetValue); 10821 10822 if (PruneWarnings) { 10823 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10824 S.PDiag(DiagID) 10825 << E->getType() << T.getUnqualifiedType() 10826 << PrettySourceValue << PrettyTargetValue 10827 << E->getSourceRange() << SourceRange(CContext)); 10828 } else { 10829 S.Diag(E->getExprLoc(), DiagID) 10830 << E->getType() << T.getUnqualifiedType() << PrettySourceValue 10831 << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext); 10832 } 10833 } 10834 10835 /// Analyze the given compound assignment for the possible losing of 10836 /// floating-point precision. 10837 static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) { 10838 assert(isa<CompoundAssignOperator>(E) && 10839 "Must be compound assignment operation"); 10840 // Recurse on the LHS and RHS in here 10841 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10842 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10843 10844 if (E->getLHS()->getType()->isAtomicType()) 10845 S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst); 10846 10847 // Now check the outermost expression 10848 const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>(); 10849 const auto *RBT = cast<CompoundAssignOperator>(E) 10850 ->getComputationResultType() 10851 ->getAs<BuiltinType>(); 10852 10853 // The below checks assume source is floating point. 10854 if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return; 10855 10856 // If source is floating point but target is an integer. 10857 if (ResultBT->isInteger()) 10858 return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(), 10859 E->getExprLoc(), diag::warn_impcast_float_integer); 10860 10861 if (!ResultBT->isFloatingPoint()) 10862 return; 10863 10864 // If both source and target are floating points, warn about losing precision. 10865 int Order = S.getASTContext().getFloatingTypeSemanticOrder( 10866 QualType(ResultBT, 0), QualType(RBT, 0)); 10867 if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc())) 10868 // warn about dropping FP rank. 10869 DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(), 10870 diag::warn_impcast_float_result_precision); 10871 } 10872 10873 static std::string PrettyPrintInRange(const llvm::APSInt &Value, 10874 IntRange Range) { 10875 if (!Range.Width) return "0"; 10876 10877 llvm::APSInt ValueInRange = Value; 10878 ValueInRange.setIsSigned(!Range.NonNegative); 10879 ValueInRange = ValueInRange.trunc(Range.Width); 10880 return toString(ValueInRange, 10); 10881 } 10882 10883 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { 10884 if (!isa<ImplicitCastExpr>(Ex)) 10885 return false; 10886 10887 Expr *InnerE = Ex->IgnoreParenImpCasts(); 10888 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); 10889 const Type *Source = 10890 S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); 10891 if (Target->isDependentType()) 10892 return false; 10893 10894 const BuiltinType *FloatCandidateBT = 10895 dyn_cast<BuiltinType>(ToBool ? Source : Target); 10896 const Type *BoolCandidateType = ToBool ? Target : Source; 10897 10898 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && 10899 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); 10900 } 10901 10902 static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, 10903 SourceLocation CC) { 10904 unsigned NumArgs = TheCall->getNumArgs(); 10905 for (unsigned i = 0; i < NumArgs; ++i) { 10906 Expr *CurrA = TheCall->getArg(i); 10907 if (!IsImplicitBoolFloatConversion(S, CurrA, true)) 10908 continue; 10909 10910 bool IsSwapped = ((i > 0) && 10911 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); 10912 IsSwapped |= ((i < (NumArgs - 1)) && 10913 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); 10914 if (IsSwapped) { 10915 // Warn on this floating-point to bool conversion. 10916 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), 10917 CurrA->getType(), CC, 10918 diag::warn_impcast_floating_point_to_bool); 10919 } 10920 } 10921 } 10922 10923 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, 10924 SourceLocation CC) { 10925 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, 10926 E->getExprLoc())) 10927 return; 10928 10929 // Don't warn on functions which have return type nullptr_t. 10930 if (isa<CallExpr>(E)) 10931 return; 10932 10933 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). 10934 const Expr *NewE = E->IgnoreParenImpCasts(); 10935 bool IsGNUNullExpr = isa<GNUNullExpr>(NewE); 10936 bool HasNullPtrType = NewE->getType()->isNullPtrType(); 10937 if (!IsGNUNullExpr && !HasNullPtrType) 10938 return; 10939 10940 // Return if target type is a safe conversion. 10941 if (T->isAnyPointerType() || T->isBlockPointerType() || 10942 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) 10943 return; 10944 10945 SourceLocation Loc = E->getSourceRange().getBegin(); 10946 10947 // Venture through the macro stacks to get to the source of macro arguments. 10948 // The new location is a better location than the complete location that was 10949 // passed in. 10950 Loc = S.SourceMgr.getTopMacroCallerLoc(Loc); 10951 CC = S.SourceMgr.getTopMacroCallerLoc(CC); 10952 10953 // __null is usually wrapped in a macro. Go up a macro if that is the case. 10954 if (IsGNUNullExpr && Loc.isMacroID()) { 10955 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( 10956 Loc, S.SourceMgr, S.getLangOpts()); 10957 if (MacroName == "NULL") 10958 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin(); 10959 } 10960 10961 // Only warn if the null and context location are in the same macro expansion. 10962 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) 10963 return; 10964 10965 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) 10966 << HasNullPtrType << T << SourceRange(CC) 10967 << FixItHint::CreateReplacement(Loc, 10968 S.getFixItZeroLiteralForType(T, Loc)); 10969 } 10970 10971 // Helper function to filter out cases for constant width constant conversion. 10972 // Don't warn on char array initialization or for non-decimal values. 10973 static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, 10974 SourceLocation CC) { 10975 // If initializing from a constant, and the constant starts with '0', 10976 // then it is a binary, octal, or hexadecimal. Allow these constants 10977 // to fill all the bits, even if there is a sign change. 10978 if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) { 10979 const char FirstLiteralCharacter = 10980 S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0]; 10981 if (FirstLiteralCharacter == '0') 10982 return false; 10983 } 10984 10985 // If the CC location points to a '{', and the type is char, then assume 10986 // assume it is an array initialization. 10987 if (CC.isValid() && T->isCharType()) { 10988 const char FirstContextCharacter = 10989 S.getSourceManager().getCharacterData(CC)[0]; 10990 if (FirstContextCharacter == '{') 10991 return false; 10992 } 10993 10994 return true; 10995 } 10996 10997 static const IntegerLiteral *getIntegerLiteral(Expr *E) { 10998 const auto *IL = dyn_cast<IntegerLiteral>(E); 10999 if (!IL) { 11000 if (auto *UO = dyn_cast<UnaryOperator>(E)) { 11001 if (UO->getOpcode() == UO_Minus) 11002 return dyn_cast<IntegerLiteral>(UO->getSubExpr()); 11003 } 11004 } 11005 11006 return IL; 11007 } 11008 11009 static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { 11010 E = E->IgnoreParenImpCasts(); 11011 SourceLocation ExprLoc = E->getExprLoc(); 11012 11013 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 11014 BinaryOperator::Opcode Opc = BO->getOpcode(); 11015 Expr::EvalResult Result; 11016 // Do not diagnose unsigned shifts. 11017 if (Opc == BO_Shl) { 11018 const auto *LHS = getIntegerLiteral(BO->getLHS()); 11019 const auto *RHS = getIntegerLiteral(BO->getRHS()); 11020 if (LHS && LHS->getValue() == 0) 11021 S.Diag(ExprLoc, diag::warn_left_shift_always) << 0; 11022 else if (!E->isValueDependent() && LHS && RHS && 11023 RHS->getValue().isNonNegative() && 11024 E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) 11025 S.Diag(ExprLoc, diag::warn_left_shift_always) 11026 << (Result.Val.getInt() != 0); 11027 else if (E->getType()->isSignedIntegerType()) 11028 S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; 11029 } 11030 } 11031 11032 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 11033 const auto *LHS = getIntegerLiteral(CO->getTrueExpr()); 11034 const auto *RHS = getIntegerLiteral(CO->getFalseExpr()); 11035 if (!LHS || !RHS) 11036 return; 11037 if ((LHS->getValue() == 0 || LHS->getValue() == 1) && 11038 (RHS->getValue() == 0 || RHS->getValue() == 1)) 11039 // Do not diagnose common idioms. 11040 return; 11041 if (LHS->getValue() != 0 && RHS->getValue() != 0) 11042 S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true); 11043 } 11044 } 11045 11046 void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, 11047 bool *ICContext, bool IsListInit) { 11048 if (E->isTypeDependent() || E->isValueDependent()) return; 11049 11050 const Type *Source = Context.getCanonicalType(E->getType()).getTypePtr(); 11051 const Type *Target = Context.getCanonicalType(T).getTypePtr(); 11052 if (Source == Target) return; 11053 if (Target->isDependentType()) return; 11054 11055 // If the conversion context location is invalid don't complain. We also 11056 // don't want to emit a warning if the issue occurs from the expansion of 11057 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we 11058 // delay this check as long as possible. Once we detect we are in that 11059 // scenario, we just return. 11060 if (CC.isInvalid()) 11061 return; 11062 11063 if (Source->isAtomicType()) 11064 Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst); 11065 11066 // Diagnose implicit casts to bool. 11067 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { 11068 if (isa<StringLiteral>(E)) 11069 // Warn on string literal to bool. Checks for string literals in logical 11070 // and expressions, for instance, assert(0 && "error here"), are 11071 // prevented by a check in AnalyzeImplicitConversions(). 11072 return DiagnoseImpCast(*this, E, T, CC, 11073 diag::warn_impcast_string_literal_to_bool); 11074 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || 11075 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { 11076 // This covers the literal expressions that evaluate to Objective-C 11077 // objects. 11078 return DiagnoseImpCast(*this, E, T, CC, 11079 diag::warn_impcast_objective_c_literal_to_bool); 11080 } 11081 if (Source->isPointerType() || Source->canDecayToPointerType()) { 11082 // Warn on pointer to bool conversion that is always true. 11083 DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, 11084 SourceRange(CC)); 11085 } 11086 } 11087 11088 // If the we're converting a constant to an ObjC BOOL on a platform where BOOL 11089 // is a typedef for signed char (macOS), then that constant value has to be 1 11090 // or 0. 11091 if (ObjC().isSignedCharBool(T) && Source->isIntegralType(Context)) { 11092 Expr::EvalResult Result; 11093 if (E->EvaluateAsInt(Result, getASTContext(), Expr::SE_AllowSideEffects)) { 11094 if (Result.Val.getInt() != 1 && Result.Val.getInt() != 0) { 11095 ObjC().adornBoolConversionDiagWithTernaryFixit( 11096 E, Diag(CC, diag::warn_impcast_constant_value_to_objc_bool) 11097 << toString(Result.Val.getInt(), 10)); 11098 } 11099 return; 11100 } 11101 } 11102 11103 // Check implicit casts from Objective-C collection literals to specialized 11104 // collection types, e.g., NSArray<NSString *> *. 11105 if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E)) 11106 ObjC().checkArrayLiteral(QualType(Target, 0), ArrayLiteral); 11107 else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) 11108 ObjC().checkDictionaryLiteral(QualType(Target, 0), DictionaryLiteral); 11109 11110 // Strip vector types. 11111 if (isa<VectorType>(Source)) { 11112 if (Target->isSveVLSBuiltinType() && 11113 (Context.areCompatibleSveTypes(QualType(Target, 0), 11114 QualType(Source, 0)) || 11115 Context.areLaxCompatibleSveTypes(QualType(Target, 0), 11116 QualType(Source, 0)))) 11117 return; 11118 11119 if (Target->isRVVVLSBuiltinType() && 11120 (Context.areCompatibleRVVTypes(QualType(Target, 0), 11121 QualType(Source, 0)) || 11122 Context.areLaxCompatibleRVVTypes(QualType(Target, 0), 11123 QualType(Source, 0)))) 11124 return; 11125 11126 if (!isa<VectorType>(Target)) { 11127 if (SourceMgr.isInSystemMacro(CC)) 11128 return; 11129 return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_vector_scalar); 11130 } else if (getLangOpts().HLSL && 11131 Target->castAs<VectorType>()->getNumElements() < 11132 Source->castAs<VectorType>()->getNumElements()) { 11133 // Diagnose vector truncation but don't return. We may also want to 11134 // diagnose an element conversion. 11135 DiagnoseImpCast(*this, E, T, CC, 11136 diag::warn_hlsl_impcast_vector_truncation); 11137 } 11138 11139 // If the vector cast is cast between two vectors of the same size, it is 11140 // a bitcast, not a conversion, except under HLSL where it is a conversion. 11141 if (!getLangOpts().HLSL && 11142 Context.getTypeSize(Source) == Context.getTypeSize(Target)) 11143 return; 11144 11145 Source = cast<VectorType>(Source)->getElementType().getTypePtr(); 11146 Target = cast<VectorType>(Target)->getElementType().getTypePtr(); 11147 } 11148 if (auto VecTy = dyn_cast<VectorType>(Target)) 11149 Target = VecTy->getElementType().getTypePtr(); 11150 11151 // Strip complex types. 11152 if (isa<ComplexType>(Source)) { 11153 if (!isa<ComplexType>(Target)) { 11154 if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) 11155 return; 11156 11157 return DiagnoseImpCast(*this, E, T, CC, 11158 getLangOpts().CPlusPlus 11159 ? diag::err_impcast_complex_scalar 11160 : diag::warn_impcast_complex_scalar); 11161 } 11162 11163 Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); 11164 Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); 11165 } 11166 11167 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); 11168 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); 11169 11170 // Strip SVE vector types 11171 if (SourceBT && SourceBT->isSveVLSBuiltinType()) { 11172 // Need the original target type for vector type checks 11173 const Type *OriginalTarget = Context.getCanonicalType(T).getTypePtr(); 11174 // Handle conversion from scalable to fixed when msve-vector-bits is 11175 // specified 11176 if (Context.areCompatibleSveTypes(QualType(OriginalTarget, 0), 11177 QualType(Source, 0)) || 11178 Context.areLaxCompatibleSveTypes(QualType(OriginalTarget, 0), 11179 QualType(Source, 0))) 11180 return; 11181 11182 // If the vector cast is cast between two vectors of the same size, it is 11183 // a bitcast, not a conversion. 11184 if (Context.getTypeSize(Source) == Context.getTypeSize(Target)) 11185 return; 11186 11187 Source = SourceBT->getSveEltType(Context).getTypePtr(); 11188 } 11189 11190 if (TargetBT && TargetBT->isSveVLSBuiltinType()) 11191 Target = TargetBT->getSveEltType(Context).getTypePtr(); 11192 11193 // If the source is floating point... 11194 if (SourceBT && SourceBT->isFloatingPoint()) { 11195 // ...and the target is floating point... 11196 if (TargetBT && TargetBT->isFloatingPoint()) { 11197 // ...then warn if we're dropping FP rank. 11198 11199 int Order = getASTContext().getFloatingTypeSemanticOrder( 11200 QualType(SourceBT, 0), QualType(TargetBT, 0)); 11201 if (Order > 0) { 11202 // Don't warn about float constants that are precisely 11203 // representable in the target type. 11204 Expr::EvalResult result; 11205 if (E->EvaluateAsRValue(result, Context)) { 11206 // Value might be a float, a float vector, or a float complex. 11207 if (IsSameFloatAfterCast( 11208 result.Val, 11209 Context.getFloatTypeSemantics(QualType(TargetBT, 0)), 11210 Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) 11211 return; 11212 } 11213 11214 if (SourceMgr.isInSystemMacro(CC)) 11215 return; 11216 11217 DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_float_precision); 11218 } 11219 // ... or possibly if we're increasing rank, too 11220 else if (Order < 0) { 11221 if (SourceMgr.isInSystemMacro(CC)) 11222 return; 11223 11224 DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_double_promotion); 11225 } 11226 return; 11227 } 11228 11229 // If the target is integral, always warn. 11230 if (TargetBT && TargetBT->isInteger()) { 11231 if (SourceMgr.isInSystemMacro(CC)) 11232 return; 11233 11234 DiagnoseFloatingImpCast(*this, E, T, CC); 11235 } 11236 11237 // Detect the case where a call result is converted from floating-point to 11238 // to bool, and the final argument to the call is converted from bool, to 11239 // discover this typo: 11240 // 11241 // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;" 11242 // 11243 // FIXME: This is an incredibly special case; is there some more general 11244 // way to detect this class of misplaced-parentheses bug? 11245 if (Target->isBooleanType() && isa<CallExpr>(E)) { 11246 // Check last argument of function call to see if it is an 11247 // implicit cast from a type matching the type the result 11248 // is being cast to. 11249 CallExpr *CEx = cast<CallExpr>(E); 11250 if (unsigned NumArgs = CEx->getNumArgs()) { 11251 Expr *LastA = CEx->getArg(NumArgs - 1); 11252 Expr *InnerE = LastA->IgnoreParenImpCasts(); 11253 if (isa<ImplicitCastExpr>(LastA) && 11254 InnerE->getType()->isBooleanType()) { 11255 // Warn on this floating-point to bool conversion 11256 DiagnoseImpCast(*this, E, T, CC, 11257 diag::warn_impcast_floating_point_to_bool); 11258 } 11259 } 11260 } 11261 return; 11262 } 11263 11264 // Valid casts involving fixed point types should be accounted for here. 11265 if (Source->isFixedPointType()) { 11266 if (Target->isUnsaturatedFixedPointType()) { 11267 Expr::EvalResult Result; 11268 if (E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects, 11269 isConstantEvaluatedContext())) { 11270 llvm::APFixedPoint Value = Result.Val.getFixedPoint(); 11271 llvm::APFixedPoint MaxVal = Context.getFixedPointMax(T); 11272 llvm::APFixedPoint MinVal = Context.getFixedPointMin(T); 11273 if (Value > MaxVal || Value < MinVal) { 11274 DiagRuntimeBehavior(E->getExprLoc(), E, 11275 PDiag(diag::warn_impcast_fixed_point_range) 11276 << Value.toString() << T 11277 << E->getSourceRange() 11278 << clang::SourceRange(CC)); 11279 return; 11280 } 11281 } 11282 } else if (Target->isIntegerType()) { 11283 Expr::EvalResult Result; 11284 if (!isConstantEvaluatedContext() && 11285 E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects)) { 11286 llvm::APFixedPoint FXResult = Result.Val.getFixedPoint(); 11287 11288 bool Overflowed; 11289 llvm::APSInt IntResult = FXResult.convertToInt( 11290 Context.getIntWidth(T), Target->isSignedIntegerOrEnumerationType(), 11291 &Overflowed); 11292 11293 if (Overflowed) { 11294 DiagRuntimeBehavior(E->getExprLoc(), E, 11295 PDiag(diag::warn_impcast_fixed_point_range) 11296 << FXResult.toString() << T 11297 << E->getSourceRange() 11298 << clang::SourceRange(CC)); 11299 return; 11300 } 11301 } 11302 } 11303 } else if (Target->isUnsaturatedFixedPointType()) { 11304 if (Source->isIntegerType()) { 11305 Expr::EvalResult Result; 11306 if (!isConstantEvaluatedContext() && 11307 E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) { 11308 llvm::APSInt Value = Result.Val.getInt(); 11309 11310 bool Overflowed; 11311 llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue( 11312 Value, Context.getFixedPointSemantics(T), &Overflowed); 11313 11314 if (Overflowed) { 11315 DiagRuntimeBehavior(E->getExprLoc(), E, 11316 PDiag(diag::warn_impcast_fixed_point_range) 11317 << toString(Value, /*Radix=*/10) << T 11318 << E->getSourceRange() 11319 << clang::SourceRange(CC)); 11320 return; 11321 } 11322 } 11323 } 11324 } 11325 11326 // If we are casting an integer type to a floating point type without 11327 // initialization-list syntax, we might lose accuracy if the floating 11328 // point type has a narrower significand than the integer type. 11329 if (SourceBT && TargetBT && SourceBT->isIntegerType() && 11330 TargetBT->isFloatingType() && !IsListInit) { 11331 // Determine the number of precision bits in the source integer type. 11332 std::optional<IntRange> SourceRange = 11333 TryGetExprRange(Context, E, isConstantEvaluatedContext(), 11334 /*Approximate=*/true); 11335 if (!SourceRange) 11336 return; 11337 unsigned int SourcePrecision = SourceRange->Width; 11338 11339 // Determine the number of precision bits in the 11340 // target floating point type. 11341 unsigned int TargetPrecision = llvm::APFloatBase::semanticsPrecision( 11342 Context.getFloatTypeSemantics(QualType(TargetBT, 0))); 11343 11344 if (SourcePrecision > 0 && TargetPrecision > 0 && 11345 SourcePrecision > TargetPrecision) { 11346 11347 if (std::optional<llvm::APSInt> SourceInt = 11348 E->getIntegerConstantExpr(Context)) { 11349 // If the source integer is a constant, convert it to the target 11350 // floating point type. Issue a warning if the value changes 11351 // during the whole conversion. 11352 llvm::APFloat TargetFloatValue( 11353 Context.getFloatTypeSemantics(QualType(TargetBT, 0))); 11354 llvm::APFloat::opStatus ConversionStatus = 11355 TargetFloatValue.convertFromAPInt( 11356 *SourceInt, SourceBT->isSignedInteger(), 11357 llvm::APFloat::rmNearestTiesToEven); 11358 11359 if (ConversionStatus != llvm::APFloat::opOK) { 11360 SmallString<32> PrettySourceValue; 11361 SourceInt->toString(PrettySourceValue, 10); 11362 SmallString<32> PrettyTargetValue; 11363 TargetFloatValue.toString(PrettyTargetValue, TargetPrecision); 11364 11365 DiagRuntimeBehavior( 11366 E->getExprLoc(), E, 11367 PDiag(diag::warn_impcast_integer_float_precision_constant) 11368 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11369 << E->getSourceRange() << clang::SourceRange(CC)); 11370 } 11371 } else { 11372 // Otherwise, the implicit conversion may lose precision. 11373 DiagnoseImpCast(*this, E, T, CC, 11374 diag::warn_impcast_integer_float_precision); 11375 } 11376 } 11377 } 11378 11379 DiagnoseNullConversion(*this, E, T, CC); 11380 11381 DiscardMisalignedMemberAddress(Target, E); 11382 11383 if (Target->isBooleanType()) 11384 DiagnoseIntInBoolContext(*this, E); 11385 11386 if (!Source->isIntegerType() || !Target->isIntegerType()) 11387 return; 11388 11389 // TODO: remove this early return once the false positives for constant->bool 11390 // in templates, macros, etc, are reduced or removed. 11391 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) 11392 return; 11393 11394 if (ObjC().isSignedCharBool(T) && !Source->isCharType() && 11395 !E->isKnownToHaveBooleanValue(/*Semantic=*/false)) { 11396 return ObjC().adornBoolConversionDiagWithTernaryFixit( 11397 E, Diag(CC, diag::warn_impcast_int_to_objc_signed_char_bool) 11398 << E->getType()); 11399 } 11400 std::optional<IntRange> LikelySourceRange = TryGetExprRange( 11401 Context, E, isConstantEvaluatedContext(), /*Approximate=*/true); 11402 if (!LikelySourceRange) 11403 return; 11404 11405 IntRange SourceTypeRange = 11406 IntRange::forTargetOfCanonicalType(Context, Source); 11407 IntRange TargetRange = IntRange::forTargetOfCanonicalType(Context, Target); 11408 11409 if (LikelySourceRange->Width > TargetRange.Width) { 11410 // If the source is a constant, use a default-on diagnostic. 11411 // TODO: this should happen for bitfield stores, too. 11412 Expr::EvalResult Result; 11413 if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects, 11414 isConstantEvaluatedContext())) { 11415 llvm::APSInt Value(32); 11416 Value = Result.Val.getInt(); 11417 11418 if (SourceMgr.isInSystemMacro(CC)) 11419 return; 11420 11421 std::string PrettySourceValue = toString(Value, 10); 11422 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11423 11424 DiagRuntimeBehavior(E->getExprLoc(), E, 11425 PDiag(diag::warn_impcast_integer_precision_constant) 11426 << PrettySourceValue << PrettyTargetValue 11427 << E->getType() << T << E->getSourceRange() 11428 << SourceRange(CC)); 11429 return; 11430 } 11431 11432 // People want to build with -Wshorten-64-to-32 and not -Wconversion. 11433 if (SourceMgr.isInSystemMacro(CC)) 11434 return; 11435 11436 if (TargetRange.Width == 32 && Context.getIntWidth(E->getType()) == 64) 11437 return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_integer_64_32, 11438 /* pruneControlFlow */ true); 11439 return DiagnoseImpCast(*this, E, T, CC, 11440 diag::warn_impcast_integer_precision); 11441 } 11442 11443 if (TargetRange.Width > SourceTypeRange.Width) { 11444 if (auto *UO = dyn_cast<UnaryOperator>(E)) 11445 if (UO->getOpcode() == UO_Minus) 11446 if (Source->isUnsignedIntegerType()) { 11447 if (Target->isUnsignedIntegerType()) 11448 return DiagnoseImpCast(*this, E, T, CC, 11449 diag::warn_impcast_high_order_zero_bits); 11450 if (Target->isSignedIntegerType()) 11451 return DiagnoseImpCast(*this, E, T, CC, 11452 diag::warn_impcast_nonnegative_result); 11453 } 11454 } 11455 11456 if (TargetRange.Width == LikelySourceRange->Width && 11457 !TargetRange.NonNegative && LikelySourceRange->NonNegative && 11458 Source->isSignedIntegerType()) { 11459 // Warn when doing a signed to signed conversion, warn if the positive 11460 // source value is exactly the width of the target type, which will 11461 // cause a negative value to be stored. 11462 11463 Expr::EvalResult Result; 11464 if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects) && 11465 !SourceMgr.isInSystemMacro(CC)) { 11466 llvm::APSInt Value = Result.Val.getInt(); 11467 if (isSameWidthConstantConversion(*this, E, T, CC)) { 11468 std::string PrettySourceValue = toString(Value, 10); 11469 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11470 11471 Diag(E->getExprLoc(), 11472 PDiag(diag::warn_impcast_integer_precision_constant) 11473 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11474 << E->getSourceRange() << SourceRange(CC)); 11475 return; 11476 } 11477 } 11478 11479 // Fall through for non-constants to give a sign conversion warning. 11480 } 11481 11482 if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) && 11483 ((TargetRange.NonNegative && !LikelySourceRange->NonNegative) || 11484 (!TargetRange.NonNegative && LikelySourceRange->NonNegative && 11485 LikelySourceRange->Width == TargetRange.Width))) { 11486 if (SourceMgr.isInSystemMacro(CC)) 11487 return; 11488 11489 if (SourceBT && SourceBT->isInteger() && TargetBT && 11490 TargetBT->isInteger() && 11491 Source->isSignedIntegerType() == Target->isSignedIntegerType()) { 11492 return; 11493 } 11494 11495 unsigned DiagID = diag::warn_impcast_integer_sign; 11496 11497 // Traditionally, gcc has warned about this under -Wsign-compare. 11498 // We also want to warn about it in -Wconversion. 11499 // So if -Wconversion is off, use a completely identical diagnostic 11500 // in the sign-compare group. 11501 // The conditional-checking code will 11502 if (ICContext) { 11503 DiagID = diag::warn_impcast_integer_sign_conditional; 11504 *ICContext = true; 11505 } 11506 11507 return DiagnoseImpCast(*this, E, T, CC, DiagID); 11508 } 11509 11510 // Diagnose conversions between different enumeration types. 11511 // In C, we pretend that the type of an EnumConstantDecl is its enumeration 11512 // type, to give us better diagnostics. 11513 QualType SourceType = E->getEnumCoercedType(Context); 11514 Source = Context.getCanonicalType(SourceType).getTypePtr(); 11515 11516 if (const EnumType *SourceEnum = Source->getAs<EnumType>()) 11517 if (const EnumType *TargetEnum = Target->getAs<EnumType>()) 11518 if (SourceEnum->getDecl()->hasNameForLinkage() && 11519 TargetEnum->getDecl()->hasNameForLinkage() && 11520 SourceEnum != TargetEnum) { 11521 if (SourceMgr.isInSystemMacro(CC)) 11522 return; 11523 11524 return DiagnoseImpCast(*this, E, SourceType, T, CC, 11525 diag::warn_impcast_different_enum_types); 11526 } 11527 } 11528 11529 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, 11530 SourceLocation CC, QualType T); 11531 11532 static void CheckConditionalOperand(Sema &S, Expr *E, QualType T, 11533 SourceLocation CC, bool &ICContext) { 11534 E = E->IgnoreParenImpCasts(); 11535 // Diagnose incomplete type for second or third operand in C. 11536 if (!S.getLangOpts().CPlusPlus && E->getType()->isRecordType()) 11537 S.RequireCompleteExprType(E, diag::err_incomplete_type); 11538 11539 if (auto *CO = dyn_cast<AbstractConditionalOperator>(E)) 11540 return CheckConditionalOperator(S, CO, CC, T); 11541 11542 AnalyzeImplicitConversions(S, E, CC); 11543 if (E->getType() != T) 11544 return S.CheckImplicitConversion(E, T, CC, &ICContext); 11545 } 11546 11547 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, 11548 SourceLocation CC, QualType T) { 11549 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); 11550 11551 Expr *TrueExpr = E->getTrueExpr(); 11552 if (auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) 11553 TrueExpr = BCO->getCommon(); 11554 11555 bool Suspicious = false; 11556 CheckConditionalOperand(S, TrueExpr, T, CC, Suspicious); 11557 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); 11558 11559 if (T->isBooleanType()) 11560 DiagnoseIntInBoolContext(S, E); 11561 11562 // If -Wconversion would have warned about either of the candidates 11563 // for a signedness conversion to the context type... 11564 if (!Suspicious) return; 11565 11566 // ...but it's currently ignored... 11567 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) 11568 return; 11569 11570 // ...then check whether it would have warned about either of the 11571 // candidates for a signedness conversion to the condition type. 11572 if (E->getType() == T) return; 11573 11574 Suspicious = false; 11575 S.CheckImplicitConversion(TrueExpr->IgnoreParenImpCasts(), E->getType(), CC, 11576 &Suspicious); 11577 if (!Suspicious) 11578 S.CheckImplicitConversion(E->getFalseExpr()->IgnoreParenImpCasts(), 11579 E->getType(), CC, &Suspicious); 11580 } 11581 11582 /// Check conversion of given expression to boolean. 11583 /// Input argument E is a logical expression. 11584 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { 11585 // Run the bool-like conversion checks only for C since there bools are 11586 // still not used as the return type from "boolean" operators or as the input 11587 // type for conditional operators. 11588 if (S.getLangOpts().CPlusPlus) 11589 return; 11590 if (E->IgnoreParenImpCasts()->getType()->isAtomicType()) 11591 return; 11592 S.CheckImplicitConversion(E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); 11593 } 11594 11595 namespace { 11596 struct AnalyzeImplicitConversionsWorkItem { 11597 Expr *E; 11598 SourceLocation CC; 11599 bool IsListInit; 11600 }; 11601 } 11602 11603 /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions 11604 /// that should be visited are added to WorkList. 11605 static void AnalyzeImplicitConversions( 11606 Sema &S, AnalyzeImplicitConversionsWorkItem Item, 11607 llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) { 11608 Expr *OrigE = Item.E; 11609 SourceLocation CC = Item.CC; 11610 11611 QualType T = OrigE->getType(); 11612 Expr *E = OrigE->IgnoreParenImpCasts(); 11613 11614 // Propagate whether we are in a C++ list initialization expression. 11615 // If so, we do not issue warnings for implicit int-float conversion 11616 // precision loss, because C++11 narrowing already handles it. 11617 bool IsListInit = Item.IsListInit || 11618 (isa<InitListExpr>(OrigE) && S.getLangOpts().CPlusPlus); 11619 11620 if (E->isTypeDependent() || E->isValueDependent()) 11621 return; 11622 11623 Expr *SourceExpr = E; 11624 // Examine, but don't traverse into the source expression of an 11625 // OpaqueValueExpr, since it may have multiple parents and we don't want to 11626 // emit duplicate diagnostics. Its fine to examine the form or attempt to 11627 // evaluate it in the context of checking the specific conversion to T though. 11628 if (auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 11629 if (auto *Src = OVE->getSourceExpr()) 11630 SourceExpr = Src; 11631 11632 if (const auto *UO = dyn_cast<UnaryOperator>(SourceExpr)) 11633 if (UO->getOpcode() == UO_Not && 11634 UO->getSubExpr()->isKnownToHaveBooleanValue()) 11635 S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool) 11636 << OrigE->getSourceRange() << T->isBooleanType() 11637 << FixItHint::CreateReplacement(UO->getBeginLoc(), "!"); 11638 11639 if (const auto *BO = dyn_cast<BinaryOperator>(SourceExpr)) 11640 if ((BO->getOpcode() == BO_And || BO->getOpcode() == BO_Or) && 11641 BO->getLHS()->isKnownToHaveBooleanValue() && 11642 BO->getRHS()->isKnownToHaveBooleanValue() && 11643 BO->getLHS()->HasSideEffects(S.Context) && 11644 BO->getRHS()->HasSideEffects(S.Context)) { 11645 SourceManager &SM = S.getSourceManager(); 11646 const LangOptions &LO = S.getLangOpts(); 11647 SourceLocation BLoc = BO->getOperatorLoc(); 11648 SourceLocation ELoc = Lexer::getLocForEndOfToken(BLoc, 0, SM, LO); 11649 StringRef SR = clang::Lexer::getSourceText( 11650 clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO); 11651 // To reduce false positives, only issue the diagnostic if the operator 11652 // is explicitly spelled as a punctuator. This suppresses the diagnostic 11653 // when using 'bitand' or 'bitor' either as keywords in C++ or as macros 11654 // in C, along with other macro spellings the user might invent. 11655 if (SR.str() == "&" || SR.str() == "|") { 11656 11657 S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical) 11658 << (BO->getOpcode() == BO_And ? "&" : "|") 11659 << OrigE->getSourceRange() 11660 << FixItHint::CreateReplacement( 11661 BO->getOperatorLoc(), 11662 (BO->getOpcode() == BO_And ? "&&" : "||")); 11663 S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int); 11664 } 11665 } 11666 11667 // For conditional operators, we analyze the arguments as if they 11668 // were being fed directly into the output. 11669 if (auto *CO = dyn_cast<AbstractConditionalOperator>(SourceExpr)) { 11670 CheckConditionalOperator(S, CO, CC, T); 11671 return; 11672 } 11673 11674 // Check implicit argument conversions for function calls. 11675 if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr)) 11676 CheckImplicitArgumentConversions(S, Call, CC); 11677 11678 // Go ahead and check any implicit conversions we might have skipped. 11679 // The non-canonical typecheck is just an optimization; 11680 // CheckImplicitConversion will filter out dead implicit conversions. 11681 if (SourceExpr->getType() != T) 11682 S.CheckImplicitConversion(SourceExpr, T, CC, nullptr, IsListInit); 11683 11684 // Now continue drilling into this expression. 11685 11686 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 11687 // The bound subexpressions in a PseudoObjectExpr are not reachable 11688 // as transitive children. 11689 // FIXME: Use a more uniform representation for this. 11690 for (auto *SE : POE->semantics()) 11691 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE)) 11692 WorkList.push_back({OVE->getSourceExpr(), CC, IsListInit}); 11693 } 11694 11695 // Skip past explicit casts. 11696 if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) { 11697 E = CE->getSubExpr()->IgnoreParenImpCasts(); 11698 if (!CE->getType()->isVoidType() && E->getType()->isAtomicType()) 11699 S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 11700 WorkList.push_back({E, CC, IsListInit}); 11701 return; 11702 } 11703 11704 if (auto *OutArgE = dyn_cast<HLSLOutArgExpr>(E)) { 11705 WorkList.push_back({OutArgE->getArgLValue(), CC, IsListInit}); 11706 // The base expression is only used to initialize the parameter for 11707 // arguments to `inout` parameters, so we only traverse down the base 11708 // expression for `inout` cases. 11709 if (OutArgE->isInOut()) 11710 WorkList.push_back( 11711 {OutArgE->getCastedTemporary()->getSourceExpr(), CC, IsListInit}); 11712 WorkList.push_back({OutArgE->getWritebackCast(), CC, IsListInit}); 11713 return; 11714 } 11715 11716 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 11717 // Do a somewhat different check with comparison operators. 11718 if (BO->isComparisonOp()) 11719 return AnalyzeComparison(S, BO); 11720 11721 // And with simple assignments. 11722 if (BO->getOpcode() == BO_Assign) 11723 return AnalyzeAssignment(S, BO); 11724 // And with compound assignments. 11725 if (BO->isAssignmentOp()) 11726 return AnalyzeCompoundAssignment(S, BO); 11727 } 11728 11729 // These break the otherwise-useful invariant below. Fortunately, 11730 // we don't really need to recurse into them, because any internal 11731 // expressions should have been analyzed already when they were 11732 // built into statements. 11733 if (isa<StmtExpr>(E)) return; 11734 11735 // Don't descend into unevaluated contexts. 11736 if (isa<UnaryExprOrTypeTraitExpr>(E)) return; 11737 11738 // Now just recurse over the expression's children. 11739 CC = E->getExprLoc(); 11740 BinaryOperator *BO = dyn_cast<BinaryOperator>(E); 11741 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; 11742 for (Stmt *SubStmt : E->children()) { 11743 Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt); 11744 if (!ChildExpr) 11745 continue; 11746 11747 if (auto *CSE = dyn_cast<CoroutineSuspendExpr>(E)) 11748 if (ChildExpr == CSE->getOperand()) 11749 // Do not recurse over a CoroutineSuspendExpr's operand. 11750 // The operand is also a subexpression of getCommonExpr(), and 11751 // recursing into it directly would produce duplicate diagnostics. 11752 continue; 11753 11754 if (IsLogicalAndOperator && 11755 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) 11756 // Ignore checking string literals that are in logical and operators. 11757 // This is a common pattern for asserts. 11758 continue; 11759 WorkList.push_back({ChildExpr, CC, IsListInit}); 11760 } 11761 11762 if (BO && BO->isLogicalOp()) { 11763 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); 11764 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11765 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11766 11767 SubExpr = BO->getRHS()->IgnoreParenImpCasts(); 11768 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11769 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11770 } 11771 11772 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) { 11773 if (U->getOpcode() == UO_LNot) { 11774 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); 11775 } else if (U->getOpcode() != UO_AddrOf) { 11776 if (U->getSubExpr()->getType()->isAtomicType()) 11777 S.Diag(U->getSubExpr()->getBeginLoc(), 11778 diag::warn_atomic_implicit_seq_cst); 11779 } 11780 } 11781 } 11782 11783 /// AnalyzeImplicitConversions - Find and report any interesting 11784 /// implicit conversions in the given expression. There are a couple 11785 /// of competing diagnostics here, -Wconversion and -Wsign-compare. 11786 static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC, 11787 bool IsListInit/*= false*/) { 11788 llvm::SmallVector<AnalyzeImplicitConversionsWorkItem, 16> WorkList; 11789 WorkList.push_back({OrigE, CC, IsListInit}); 11790 while (!WorkList.empty()) 11791 AnalyzeImplicitConversions(S, WorkList.pop_back_val(), WorkList); 11792 } 11793 11794 // Helper function for Sema::DiagnoseAlwaysNonNullPointer. 11795 // Returns true when emitting a warning about taking the address of a reference. 11796 static bool CheckForReference(Sema &SemaRef, const Expr *E, 11797 const PartialDiagnostic &PD) { 11798 E = E->IgnoreParenImpCasts(); 11799 11800 const FunctionDecl *FD = nullptr; 11801 11802 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 11803 if (!DRE->getDecl()->getType()->isReferenceType()) 11804 return false; 11805 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11806 if (!M->getMemberDecl()->getType()->isReferenceType()) 11807 return false; 11808 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { 11809 if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) 11810 return false; 11811 FD = Call->getDirectCallee(); 11812 } else { 11813 return false; 11814 } 11815 11816 SemaRef.Diag(E->getExprLoc(), PD); 11817 11818 // If possible, point to location of function. 11819 if (FD) { 11820 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; 11821 } 11822 11823 return true; 11824 } 11825 11826 // Returns true if the SourceLocation is expanded from any macro body. 11827 // Returns false if the SourceLocation is invalid, is from not in a macro 11828 // expansion, or is from expanded from a top-level macro argument. 11829 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { 11830 if (Loc.isInvalid()) 11831 return false; 11832 11833 while (Loc.isMacroID()) { 11834 if (SM.isMacroBodyExpansion(Loc)) 11835 return true; 11836 Loc = SM.getImmediateMacroCallerLoc(Loc); 11837 } 11838 11839 return false; 11840 } 11841 11842 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, 11843 Expr::NullPointerConstantKind NullKind, 11844 bool IsEqual, SourceRange Range) { 11845 if (!E) 11846 return; 11847 11848 // Don't warn inside macros. 11849 if (E->getExprLoc().isMacroID()) { 11850 const SourceManager &SM = getSourceManager(); 11851 if (IsInAnyMacroBody(SM, E->getExprLoc()) || 11852 IsInAnyMacroBody(SM, Range.getBegin())) 11853 return; 11854 } 11855 E = E->IgnoreImpCasts(); 11856 11857 const bool IsCompare = NullKind != Expr::NPCK_NotNull; 11858 11859 if (isa<CXXThisExpr>(E)) { 11860 unsigned DiagID = IsCompare ? diag::warn_this_null_compare 11861 : diag::warn_this_bool_conversion; 11862 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; 11863 return; 11864 } 11865 11866 bool IsAddressOf = false; 11867 11868 if (auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { 11869 if (UO->getOpcode() != UO_AddrOf) 11870 return; 11871 IsAddressOf = true; 11872 E = UO->getSubExpr(); 11873 } 11874 11875 if (IsAddressOf) { 11876 unsigned DiagID = IsCompare 11877 ? diag::warn_address_of_reference_null_compare 11878 : diag::warn_address_of_reference_bool_conversion; 11879 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range 11880 << IsEqual; 11881 if (CheckForReference(*this, E, PD)) { 11882 return; 11883 } 11884 } 11885 11886 auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) { 11887 bool IsParam = isa<NonNullAttr>(NonnullAttr); 11888 std::string Str; 11889 llvm::raw_string_ostream S(Str); 11890 E->printPretty(S, nullptr, getPrintingPolicy()); 11891 unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare 11892 : diag::warn_cast_nonnull_to_bool; 11893 Diag(E->getExprLoc(), DiagID) << IsParam << S.str() 11894 << E->getSourceRange() << Range << IsEqual; 11895 Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam; 11896 }; 11897 11898 // If we have a CallExpr that is tagged with returns_nonnull, we can complain. 11899 if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) { 11900 if (auto *Callee = Call->getDirectCallee()) { 11901 if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) { 11902 ComplainAboutNonnullParamOrCall(A); 11903 return; 11904 } 11905 } 11906 } 11907 11908 // Complain if we are converting a lambda expression to a boolean value 11909 // outside of instantiation. 11910 if (!inTemplateInstantiation()) { 11911 if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) { 11912 if (const auto *MRecordDecl = MCallExpr->getRecordDecl(); 11913 MRecordDecl && MRecordDecl->isLambda()) { 11914 Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool) 11915 << /*LambdaPointerConversionOperatorType=*/3 11916 << MRecordDecl->getSourceRange() << Range << IsEqual; 11917 return; 11918 } 11919 } 11920 } 11921 11922 // Expect to find a single Decl. Skip anything more complicated. 11923 ValueDecl *D = nullptr; 11924 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { 11925 D = R->getDecl(); 11926 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11927 D = M->getMemberDecl(); 11928 } 11929 11930 // Weak Decls can be null. 11931 if (!D || D->isWeak()) 11932 return; 11933 11934 // Check for parameter decl with nonnull attribute 11935 if (const auto* PV = dyn_cast<ParmVarDecl>(D)) { 11936 if (getCurFunction() && 11937 !getCurFunction()->ModifiedNonNullParams.count(PV)) { 11938 if (const Attr *A = PV->getAttr<NonNullAttr>()) { 11939 ComplainAboutNonnullParamOrCall(A); 11940 return; 11941 } 11942 11943 if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { 11944 // Skip function template not specialized yet. 11945 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) 11946 return; 11947 auto ParamIter = llvm::find(FD->parameters(), PV); 11948 assert(ParamIter != FD->param_end()); 11949 unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); 11950 11951 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { 11952 if (!NonNull->args_size()) { 11953 ComplainAboutNonnullParamOrCall(NonNull); 11954 return; 11955 } 11956 11957 for (const ParamIdx &ArgNo : NonNull->args()) { 11958 if (ArgNo.getASTIndex() == ParamNo) { 11959 ComplainAboutNonnullParamOrCall(NonNull); 11960 return; 11961 } 11962 } 11963 } 11964 } 11965 } 11966 } 11967 11968 QualType T = D->getType(); 11969 const bool IsArray = T->isArrayType(); 11970 const bool IsFunction = T->isFunctionType(); 11971 11972 // Address of function is used to silence the function warning. 11973 if (IsAddressOf && IsFunction) { 11974 return; 11975 } 11976 11977 // Found nothing. 11978 if (!IsAddressOf && !IsFunction && !IsArray) 11979 return; 11980 11981 // Pretty print the expression for the diagnostic. 11982 std::string Str; 11983 llvm::raw_string_ostream S(Str); 11984 E->printPretty(S, nullptr, getPrintingPolicy()); 11985 11986 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare 11987 : diag::warn_impcast_pointer_to_bool; 11988 enum { 11989 AddressOf, 11990 FunctionPointer, 11991 ArrayPointer 11992 } DiagType; 11993 if (IsAddressOf) 11994 DiagType = AddressOf; 11995 else if (IsFunction) 11996 DiagType = FunctionPointer; 11997 else if (IsArray) 11998 DiagType = ArrayPointer; 11999 else 12000 llvm_unreachable("Could not determine diagnostic."); 12001 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() 12002 << Range << IsEqual; 12003 12004 if (!IsFunction) 12005 return; 12006 12007 // Suggest '&' to silence the function warning. 12008 Diag(E->getExprLoc(), diag::note_function_warning_silence) 12009 << FixItHint::CreateInsertion(E->getBeginLoc(), "&"); 12010 12011 // Check to see if '()' fixit should be emitted. 12012 QualType ReturnType; 12013 UnresolvedSet<4> NonTemplateOverloads; 12014 tryExprAsCall(*E, ReturnType, NonTemplateOverloads); 12015 if (ReturnType.isNull()) 12016 return; 12017 12018 if (IsCompare) { 12019 // There are two cases here. If there is null constant, the only suggest 12020 // for a pointer return type. If the null is 0, then suggest if the return 12021 // type is a pointer or an integer type. 12022 if (!ReturnType->isPointerType()) { 12023 if (NullKind == Expr::NPCK_ZeroExpression || 12024 NullKind == Expr::NPCK_ZeroLiteral) { 12025 if (!ReturnType->isIntegerType()) 12026 return; 12027 } else { 12028 return; 12029 } 12030 } 12031 } else { // !IsCompare 12032 // For function to bool, only suggest if the function pointer has bool 12033 // return type. 12034 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) 12035 return; 12036 } 12037 Diag(E->getExprLoc(), diag::note_function_to_function_call) 12038 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()"); 12039 } 12040 12041 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { 12042 // Don't diagnose in unevaluated contexts. 12043 if (isUnevaluatedContext()) 12044 return; 12045 12046 // Don't diagnose for value- or type-dependent expressions. 12047 if (E->isTypeDependent() || E->isValueDependent()) 12048 return; 12049 12050 // Check for array bounds violations in cases where the check isn't triggered 12051 // elsewhere for other Expr types (like BinaryOperators), e.g. when an 12052 // ArraySubscriptExpr is on the RHS of a variable initialization. 12053 CheckArrayAccess(E); 12054 12055 // This is not the right CC for (e.g.) a variable initialization. 12056 AnalyzeImplicitConversions(*this, E, CC); 12057 } 12058 12059 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { 12060 ::CheckBoolLikeConversion(*this, E, CC); 12061 } 12062 12063 void Sema::CheckForIntOverflow (const Expr *E) { 12064 // Use a work list to deal with nested struct initializers. 12065 SmallVector<const Expr *, 2> Exprs(1, E); 12066 12067 do { 12068 const Expr *OriginalE = Exprs.pop_back_val(); 12069 const Expr *E = OriginalE->IgnoreParenCasts(); 12070 12071 if (isa<BinaryOperator, UnaryOperator>(E)) { 12072 E->EvaluateForOverflow(Context); 12073 continue; 12074 } 12075 12076 if (const auto *InitList = dyn_cast<InitListExpr>(OriginalE)) 12077 Exprs.append(InitList->inits().begin(), InitList->inits().end()); 12078 else if (isa<ObjCBoxedExpr>(OriginalE)) 12079 E->EvaluateForOverflow(Context); 12080 else if (const auto *Call = dyn_cast<CallExpr>(E)) 12081 Exprs.append(Call->arg_begin(), Call->arg_end()); 12082 else if (const auto *Message = dyn_cast<ObjCMessageExpr>(E)) 12083 Exprs.append(Message->arg_begin(), Message->arg_end()); 12084 else if (const auto *Construct = dyn_cast<CXXConstructExpr>(E)) 12085 Exprs.append(Construct->arg_begin(), Construct->arg_end()); 12086 else if (const auto *Temporary = dyn_cast<CXXBindTemporaryExpr>(E)) 12087 Exprs.push_back(Temporary->getSubExpr()); 12088 else if (const auto *Array = dyn_cast<ArraySubscriptExpr>(E)) 12089 Exprs.push_back(Array->getIdx()); 12090 else if (const auto *Compound = dyn_cast<CompoundLiteralExpr>(E)) 12091 Exprs.push_back(Compound->getInitializer()); 12092 else if (const auto *New = dyn_cast<CXXNewExpr>(E); 12093 New && New->isArray()) { 12094 if (auto ArraySize = New->getArraySize()) 12095 Exprs.push_back(*ArraySize); 12096 } else if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(OriginalE)) 12097 Exprs.push_back(MTE->getSubExpr()); 12098 } while (!Exprs.empty()); 12099 } 12100 12101 namespace { 12102 12103 /// Visitor for expressions which looks for unsequenced operations on the 12104 /// same object. 12105 class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> { 12106 using Base = ConstEvaluatedExprVisitor<SequenceChecker>; 12107 12108 /// A tree of sequenced regions within an expression. Two regions are 12109 /// unsequenced if one is an ancestor or a descendent of the other. When we 12110 /// finish processing an expression with sequencing, such as a comma 12111 /// expression, we fold its tree nodes into its parent, since they are 12112 /// unsequenced with respect to nodes we will visit later. 12113 class SequenceTree { 12114 struct Value { 12115 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} 12116 unsigned Parent : 31; 12117 LLVM_PREFERRED_TYPE(bool) 12118 unsigned Merged : 1; 12119 }; 12120 SmallVector<Value, 8> Values; 12121 12122 public: 12123 /// A region within an expression which may be sequenced with respect 12124 /// to some other region. 12125 class Seq { 12126 friend class SequenceTree; 12127 12128 unsigned Index; 12129 12130 explicit Seq(unsigned N) : Index(N) {} 12131 12132 public: 12133 Seq() : Index(0) {} 12134 }; 12135 12136 SequenceTree() { Values.push_back(Value(0)); } 12137 Seq root() const { return Seq(0); } 12138 12139 /// Create a new sequence of operations, which is an unsequenced 12140 /// subset of \p Parent. This sequence of operations is sequenced with 12141 /// respect to other children of \p Parent. 12142 Seq allocate(Seq Parent) { 12143 Values.push_back(Value(Parent.Index)); 12144 return Seq(Values.size() - 1); 12145 } 12146 12147 /// Merge a sequence of operations into its parent. 12148 void merge(Seq S) { 12149 Values[S.Index].Merged = true; 12150 } 12151 12152 /// Determine whether two operations are unsequenced. This operation 12153 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old 12154 /// should have been merged into its parent as appropriate. 12155 bool isUnsequenced(Seq Cur, Seq Old) { 12156 unsigned C = representative(Cur.Index); 12157 unsigned Target = representative(Old.Index); 12158 while (C >= Target) { 12159 if (C == Target) 12160 return true; 12161 C = Values[C].Parent; 12162 } 12163 return false; 12164 } 12165 12166 private: 12167 /// Pick a representative for a sequence. 12168 unsigned representative(unsigned K) { 12169 if (Values[K].Merged) 12170 // Perform path compression as we go. 12171 return Values[K].Parent = representative(Values[K].Parent); 12172 return K; 12173 } 12174 }; 12175 12176 /// An object for which we can track unsequenced uses. 12177 using Object = const NamedDecl *; 12178 12179 /// Different flavors of object usage which we track. We only track the 12180 /// least-sequenced usage of each kind. 12181 enum UsageKind { 12182 /// A read of an object. Multiple unsequenced reads are OK. 12183 UK_Use, 12184 12185 /// A modification of an object which is sequenced before the value 12186 /// computation of the expression, such as ++n in C++. 12187 UK_ModAsValue, 12188 12189 /// A modification of an object which is not sequenced before the value 12190 /// computation of the expression, such as n++. 12191 UK_ModAsSideEffect, 12192 12193 UK_Count = UK_ModAsSideEffect + 1 12194 }; 12195 12196 /// Bundle together a sequencing region and the expression corresponding 12197 /// to a specific usage. One Usage is stored for each usage kind in UsageInfo. 12198 struct Usage { 12199 const Expr *UsageExpr = nullptr; 12200 SequenceTree::Seq Seq; 12201 12202 Usage() = default; 12203 }; 12204 12205 struct UsageInfo { 12206 Usage Uses[UK_Count]; 12207 12208 /// Have we issued a diagnostic for this object already? 12209 bool Diagnosed = false; 12210 12211 UsageInfo(); 12212 }; 12213 using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>; 12214 12215 Sema &SemaRef; 12216 12217 /// Sequenced regions within the expression. 12218 SequenceTree Tree; 12219 12220 /// Declaration modifications and references which we have seen. 12221 UsageInfoMap UsageMap; 12222 12223 /// The region we are currently within. 12224 SequenceTree::Seq Region; 12225 12226 /// Filled in with declarations which were modified as a side-effect 12227 /// (that is, post-increment operations). 12228 SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr; 12229 12230 /// Expressions to check later. We defer checking these to reduce 12231 /// stack usage. 12232 SmallVectorImpl<const Expr *> &WorkList; 12233 12234 /// RAII object wrapping the visitation of a sequenced subexpression of an 12235 /// expression. At the end of this process, the side-effects of the evaluation 12236 /// become sequenced with respect to the value computation of the result, so 12237 /// we downgrade any UK_ModAsSideEffect within the evaluation to 12238 /// UK_ModAsValue. 12239 struct SequencedSubexpression { 12240 SequencedSubexpression(SequenceChecker &Self) 12241 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { 12242 Self.ModAsSideEffect = &ModAsSideEffect; 12243 } 12244 12245 ~SequencedSubexpression() { 12246 for (const std::pair<Object, Usage> &M : llvm::reverse(ModAsSideEffect)) { 12247 // Add a new usage with usage kind UK_ModAsValue, and then restore 12248 // the previous usage with UK_ModAsSideEffect (thus clearing it if 12249 // the previous one was empty). 12250 UsageInfo &UI = Self.UsageMap[M.first]; 12251 auto &SideEffectUsage = UI.Uses[UK_ModAsSideEffect]; 12252 Self.addUsage(M.first, UI, SideEffectUsage.UsageExpr, UK_ModAsValue); 12253 SideEffectUsage = M.second; 12254 } 12255 Self.ModAsSideEffect = OldModAsSideEffect; 12256 } 12257 12258 SequenceChecker &Self; 12259 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; 12260 SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect; 12261 }; 12262 12263 /// RAII object wrapping the visitation of a subexpression which we might 12264 /// choose to evaluate as a constant. If any subexpression is evaluated and 12265 /// found to be non-constant, this allows us to suppress the evaluation of 12266 /// the outer expression. 12267 class EvaluationTracker { 12268 public: 12269 EvaluationTracker(SequenceChecker &Self) 12270 : Self(Self), Prev(Self.EvalTracker) { 12271 Self.EvalTracker = this; 12272 } 12273 12274 ~EvaluationTracker() { 12275 Self.EvalTracker = Prev; 12276 if (Prev) 12277 Prev->EvalOK &= EvalOK; 12278 } 12279 12280 bool evaluate(const Expr *E, bool &Result) { 12281 if (!EvalOK || E->isValueDependent()) 12282 return false; 12283 EvalOK = E->EvaluateAsBooleanCondition( 12284 Result, Self.SemaRef.Context, 12285 Self.SemaRef.isConstantEvaluatedContext()); 12286 return EvalOK; 12287 } 12288 12289 private: 12290 SequenceChecker &Self; 12291 EvaluationTracker *Prev; 12292 bool EvalOK = true; 12293 } *EvalTracker = nullptr; 12294 12295 /// Find the object which is produced by the specified expression, 12296 /// if any. 12297 Object getObject(const Expr *E, bool Mod) const { 12298 E = E->IgnoreParenCasts(); 12299 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 12300 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) 12301 return getObject(UO->getSubExpr(), Mod); 12302 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 12303 if (BO->getOpcode() == BO_Comma) 12304 return getObject(BO->getRHS(), Mod); 12305 if (Mod && BO->isAssignmentOp()) 12306 return getObject(BO->getLHS(), Mod); 12307 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 12308 // FIXME: Check for more interesting cases, like "x.n = ++x.n". 12309 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) 12310 return ME->getMemberDecl(); 12311 } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 12312 // FIXME: If this is a reference, map through to its value. 12313 return DRE->getDecl(); 12314 return nullptr; 12315 } 12316 12317 /// Note that an object \p O was modified or used by an expression 12318 /// \p UsageExpr with usage kind \p UK. \p UI is the \p UsageInfo for 12319 /// the object \p O as obtained via the \p UsageMap. 12320 void addUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, UsageKind UK) { 12321 // Get the old usage for the given object and usage kind. 12322 Usage &U = UI.Uses[UK]; 12323 if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) { 12324 // If we have a modification as side effect and are in a sequenced 12325 // subexpression, save the old Usage so that we can restore it later 12326 // in SequencedSubexpression::~SequencedSubexpression. 12327 if (UK == UK_ModAsSideEffect && ModAsSideEffect) 12328 ModAsSideEffect->push_back(std::make_pair(O, U)); 12329 // Then record the new usage with the current sequencing region. 12330 U.UsageExpr = UsageExpr; 12331 U.Seq = Region; 12332 } 12333 } 12334 12335 /// Check whether a modification or use of an object \p O in an expression 12336 /// \p UsageExpr conflicts with a prior usage of kind \p OtherKind. \p UI is 12337 /// the \p UsageInfo for the object \p O as obtained via the \p UsageMap. 12338 /// \p IsModMod is true when we are checking for a mod-mod unsequenced 12339 /// usage and false we are checking for a mod-use unsequenced usage. 12340 void checkUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, 12341 UsageKind OtherKind, bool IsModMod) { 12342 if (UI.Diagnosed) 12343 return; 12344 12345 const Usage &U = UI.Uses[OtherKind]; 12346 if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) 12347 return; 12348 12349 const Expr *Mod = U.UsageExpr; 12350 const Expr *ModOrUse = UsageExpr; 12351 if (OtherKind == UK_Use) 12352 std::swap(Mod, ModOrUse); 12353 12354 SemaRef.DiagRuntimeBehavior( 12355 Mod->getExprLoc(), {Mod, ModOrUse}, 12356 SemaRef.PDiag(IsModMod ? diag::warn_unsequenced_mod_mod 12357 : diag::warn_unsequenced_mod_use) 12358 << O << SourceRange(ModOrUse->getExprLoc())); 12359 UI.Diagnosed = true; 12360 } 12361 12362 // A note on note{Pre, Post}{Use, Mod}: 12363 // 12364 // (It helps to follow the algorithm with an expression such as 12365 // "((++k)++, k) = k" or "k = (k++, k++)". Both contain unsequenced 12366 // operations before C++17 and both are well-defined in C++17). 12367 // 12368 // When visiting a node which uses/modify an object we first call notePreUse 12369 // or notePreMod before visiting its sub-expression(s). At this point the 12370 // children of the current node have not yet been visited and so the eventual 12371 // uses/modifications resulting from the children of the current node have not 12372 // been recorded yet. 12373 // 12374 // We then visit the children of the current node. After that notePostUse or 12375 // notePostMod is called. These will 1) detect an unsequenced modification 12376 // as side effect (as in "k++ + k") and 2) add a new usage with the 12377 // appropriate usage kind. 12378 // 12379 // We also have to be careful that some operation sequences modification as 12380 // side effect as well (for example: || or ,). To account for this we wrap 12381 // the visitation of such a sub-expression (for example: the LHS of || or ,) 12382 // with SequencedSubexpression. SequencedSubexpression is an RAII object 12383 // which record usages which are modifications as side effect, and then 12384 // downgrade them (or more accurately restore the previous usage which was a 12385 // modification as side effect) when exiting the scope of the sequenced 12386 // subexpression. 12387 12388 void notePreUse(Object O, const Expr *UseExpr) { 12389 UsageInfo &UI = UsageMap[O]; 12390 // Uses conflict with other modifications. 12391 checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/false); 12392 } 12393 12394 void notePostUse(Object O, const Expr *UseExpr) { 12395 UsageInfo &UI = UsageMap[O]; 12396 checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsSideEffect, 12397 /*IsModMod=*/false); 12398 addUsage(O, UI, UseExpr, /*UsageKind=*/UK_Use); 12399 } 12400 12401 void notePreMod(Object O, const Expr *ModExpr) { 12402 UsageInfo &UI = UsageMap[O]; 12403 // Modifications conflict with other modifications and with uses. 12404 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/true); 12405 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_Use, /*IsModMod=*/false); 12406 } 12407 12408 void notePostMod(Object O, const Expr *ModExpr, UsageKind UK) { 12409 UsageInfo &UI = UsageMap[O]; 12410 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsSideEffect, 12411 /*IsModMod=*/true); 12412 addUsage(O, UI, ModExpr, /*UsageKind=*/UK); 12413 } 12414 12415 public: 12416 SequenceChecker(Sema &S, const Expr *E, 12417 SmallVectorImpl<const Expr *> &WorkList) 12418 : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) { 12419 Visit(E); 12420 // Silence a -Wunused-private-field since WorkList is now unused. 12421 // TODO: Evaluate if it can be used, and if not remove it. 12422 (void)this->WorkList; 12423 } 12424 12425 void VisitStmt(const Stmt *S) { 12426 // Skip all statements which aren't expressions for now. 12427 } 12428 12429 void VisitExpr(const Expr *E) { 12430 // By default, just recurse to evaluated subexpressions. 12431 Base::VisitStmt(E); 12432 } 12433 12434 void VisitCoroutineSuspendExpr(const CoroutineSuspendExpr *CSE) { 12435 for (auto *Sub : CSE->children()) { 12436 const Expr *ChildExpr = dyn_cast_or_null<Expr>(Sub); 12437 if (!ChildExpr) 12438 continue; 12439 12440 if (ChildExpr == CSE->getOperand()) 12441 // Do not recurse over a CoroutineSuspendExpr's operand. 12442 // The operand is also a subexpression of getCommonExpr(), and 12443 // recursing into it directly could confuse object management 12444 // for the sake of sequence tracking. 12445 continue; 12446 12447 Visit(Sub); 12448 } 12449 } 12450 12451 void VisitCastExpr(const CastExpr *E) { 12452 Object O = Object(); 12453 if (E->getCastKind() == CK_LValueToRValue) 12454 O = getObject(E->getSubExpr(), false); 12455 12456 if (O) 12457 notePreUse(O, E); 12458 VisitExpr(E); 12459 if (O) 12460 notePostUse(O, E); 12461 } 12462 12463 void VisitSequencedExpressions(const Expr *SequencedBefore, 12464 const Expr *SequencedAfter) { 12465 SequenceTree::Seq BeforeRegion = Tree.allocate(Region); 12466 SequenceTree::Seq AfterRegion = Tree.allocate(Region); 12467 SequenceTree::Seq OldRegion = Region; 12468 12469 { 12470 SequencedSubexpression SeqBefore(*this); 12471 Region = BeforeRegion; 12472 Visit(SequencedBefore); 12473 } 12474 12475 Region = AfterRegion; 12476 Visit(SequencedAfter); 12477 12478 Region = OldRegion; 12479 12480 Tree.merge(BeforeRegion); 12481 Tree.merge(AfterRegion); 12482 } 12483 12484 void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) { 12485 // C++17 [expr.sub]p1: 12486 // The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The 12487 // expression E1 is sequenced before the expression E2. 12488 if (SemaRef.getLangOpts().CPlusPlus17) 12489 VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS()); 12490 else { 12491 Visit(ASE->getLHS()); 12492 Visit(ASE->getRHS()); 12493 } 12494 } 12495 12496 void VisitBinPtrMemD(const BinaryOperator *BO) { VisitBinPtrMem(BO); } 12497 void VisitBinPtrMemI(const BinaryOperator *BO) { VisitBinPtrMem(BO); } 12498 void VisitBinPtrMem(const BinaryOperator *BO) { 12499 // C++17 [expr.mptr.oper]p4: 12500 // Abbreviating pm-expression.*cast-expression as E1.*E2, [...] 12501 // the expression E1 is sequenced before the expression E2. 12502 if (SemaRef.getLangOpts().CPlusPlus17) 12503 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12504 else { 12505 Visit(BO->getLHS()); 12506 Visit(BO->getRHS()); 12507 } 12508 } 12509 12510 void VisitBinShl(const BinaryOperator *BO) { VisitBinShlShr(BO); } 12511 void VisitBinShr(const BinaryOperator *BO) { VisitBinShlShr(BO); } 12512 void VisitBinShlShr(const BinaryOperator *BO) { 12513 // C++17 [expr.shift]p4: 12514 // The expression E1 is sequenced before the expression E2. 12515 if (SemaRef.getLangOpts().CPlusPlus17) 12516 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12517 else { 12518 Visit(BO->getLHS()); 12519 Visit(BO->getRHS()); 12520 } 12521 } 12522 12523 void VisitBinComma(const BinaryOperator *BO) { 12524 // C++11 [expr.comma]p1: 12525 // Every value computation and side effect associated with the left 12526 // expression is sequenced before every value computation and side 12527 // effect associated with the right expression. 12528 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12529 } 12530 12531 void VisitBinAssign(const BinaryOperator *BO) { 12532 SequenceTree::Seq RHSRegion; 12533 SequenceTree::Seq LHSRegion; 12534 if (SemaRef.getLangOpts().CPlusPlus17) { 12535 RHSRegion = Tree.allocate(Region); 12536 LHSRegion = Tree.allocate(Region); 12537 } else { 12538 RHSRegion = Region; 12539 LHSRegion = Region; 12540 } 12541 SequenceTree::Seq OldRegion = Region; 12542 12543 // C++11 [expr.ass]p1: 12544 // [...] the assignment is sequenced after the value computation 12545 // of the right and left operands, [...] 12546 // 12547 // so check it before inspecting the operands and update the 12548 // map afterwards. 12549 Object O = getObject(BO->getLHS(), /*Mod=*/true); 12550 if (O) 12551 notePreMod(O, BO); 12552 12553 if (SemaRef.getLangOpts().CPlusPlus17) { 12554 // C++17 [expr.ass]p1: 12555 // [...] The right operand is sequenced before the left operand. [...] 12556 { 12557 SequencedSubexpression SeqBefore(*this); 12558 Region = RHSRegion; 12559 Visit(BO->getRHS()); 12560 } 12561 12562 Region = LHSRegion; 12563 Visit(BO->getLHS()); 12564 12565 if (O && isa<CompoundAssignOperator>(BO)) 12566 notePostUse(O, BO); 12567 12568 } else { 12569 // C++11 does not specify any sequencing between the LHS and RHS. 12570 Region = LHSRegion; 12571 Visit(BO->getLHS()); 12572 12573 if (O && isa<CompoundAssignOperator>(BO)) 12574 notePostUse(O, BO); 12575 12576 Region = RHSRegion; 12577 Visit(BO->getRHS()); 12578 } 12579 12580 // C++11 [expr.ass]p1: 12581 // the assignment is sequenced [...] before the value computation of the 12582 // assignment expression. 12583 // C11 6.5.16/3 has no such rule. 12584 Region = OldRegion; 12585 if (O) 12586 notePostMod(O, BO, 12587 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12588 : UK_ModAsSideEffect); 12589 if (SemaRef.getLangOpts().CPlusPlus17) { 12590 Tree.merge(RHSRegion); 12591 Tree.merge(LHSRegion); 12592 } 12593 } 12594 12595 void VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) { 12596 VisitBinAssign(CAO); 12597 } 12598 12599 void VisitUnaryPreInc(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12600 void VisitUnaryPreDec(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12601 void VisitUnaryPreIncDec(const UnaryOperator *UO) { 12602 Object O = getObject(UO->getSubExpr(), true); 12603 if (!O) 12604 return VisitExpr(UO); 12605 12606 notePreMod(O, UO); 12607 Visit(UO->getSubExpr()); 12608 // C++11 [expr.pre.incr]p1: 12609 // the expression ++x is equivalent to x+=1 12610 notePostMod(O, UO, 12611 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12612 : UK_ModAsSideEffect); 12613 } 12614 12615 void VisitUnaryPostInc(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12616 void VisitUnaryPostDec(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12617 void VisitUnaryPostIncDec(const UnaryOperator *UO) { 12618 Object O = getObject(UO->getSubExpr(), true); 12619 if (!O) 12620 return VisitExpr(UO); 12621 12622 notePreMod(O, UO); 12623 Visit(UO->getSubExpr()); 12624 notePostMod(O, UO, UK_ModAsSideEffect); 12625 } 12626 12627 void VisitBinLOr(const BinaryOperator *BO) { 12628 // C++11 [expr.log.or]p2: 12629 // If the second expression is evaluated, every value computation and 12630 // side effect associated with the first expression is sequenced before 12631 // every value computation and side effect associated with the 12632 // second expression. 12633 SequenceTree::Seq LHSRegion = Tree.allocate(Region); 12634 SequenceTree::Seq RHSRegion = Tree.allocate(Region); 12635 SequenceTree::Seq OldRegion = Region; 12636 12637 EvaluationTracker Eval(*this); 12638 { 12639 SequencedSubexpression Sequenced(*this); 12640 Region = LHSRegion; 12641 Visit(BO->getLHS()); 12642 } 12643 12644 // C++11 [expr.log.or]p1: 12645 // [...] the second operand is not evaluated if the first operand 12646 // evaluates to true. 12647 bool EvalResult = false; 12648 bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); 12649 bool ShouldVisitRHS = !EvalOK || !EvalResult; 12650 if (ShouldVisitRHS) { 12651 Region = RHSRegion; 12652 Visit(BO->getRHS()); 12653 } 12654 12655 Region = OldRegion; 12656 Tree.merge(LHSRegion); 12657 Tree.merge(RHSRegion); 12658 } 12659 12660 void VisitBinLAnd(const BinaryOperator *BO) { 12661 // C++11 [expr.log.and]p2: 12662 // If the second expression is evaluated, every value computation and 12663 // side effect associated with the first expression is sequenced before 12664 // every value computation and side effect associated with the 12665 // second expression. 12666 SequenceTree::Seq LHSRegion = Tree.allocate(Region); 12667 SequenceTree::Seq RHSRegion = Tree.allocate(Region); 12668 SequenceTree::Seq OldRegion = Region; 12669 12670 EvaluationTracker Eval(*this); 12671 { 12672 SequencedSubexpression Sequenced(*this); 12673 Region = LHSRegion; 12674 Visit(BO->getLHS()); 12675 } 12676 12677 // C++11 [expr.log.and]p1: 12678 // [...] the second operand is not evaluated if the first operand is false. 12679 bool EvalResult = false; 12680 bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); 12681 bool ShouldVisitRHS = !EvalOK || EvalResult; 12682 if (ShouldVisitRHS) { 12683 Region = RHSRegion; 12684 Visit(BO->getRHS()); 12685 } 12686 12687 Region = OldRegion; 12688 Tree.merge(LHSRegion); 12689 Tree.merge(RHSRegion); 12690 } 12691 12692 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO) { 12693 // C++11 [expr.cond]p1: 12694 // [...] Every value computation and side effect associated with the first 12695 // expression is sequenced before every value computation and side effect 12696 // associated with the second or third expression. 12697 SequenceTree::Seq ConditionRegion = Tree.allocate(Region); 12698 12699 // No sequencing is specified between the true and false expression. 12700 // However since exactly one of both is going to be evaluated we can 12701 // consider them to be sequenced. This is needed to avoid warning on 12702 // something like "x ? y+= 1 : y += 2;" in the case where we will visit 12703 // both the true and false expressions because we can't evaluate x. 12704 // This will still allow us to detect an expression like (pre C++17) 12705 // "(x ? y += 1 : y += 2) = y". 12706 // 12707 // We don't wrap the visitation of the true and false expression with 12708 // SequencedSubexpression because we don't want to downgrade modifications 12709 // as side effect in the true and false expressions after the visition 12710 // is done. (for example in the expression "(x ? y++ : y++) + y" we should 12711 // not warn between the two "y++", but we should warn between the "y++" 12712 // and the "y". 12713 SequenceTree::Seq TrueRegion = Tree.allocate(Region); 12714 SequenceTree::Seq FalseRegion = Tree.allocate(Region); 12715 SequenceTree::Seq OldRegion = Region; 12716 12717 EvaluationTracker Eval(*this); 12718 { 12719 SequencedSubexpression Sequenced(*this); 12720 Region = ConditionRegion; 12721 Visit(CO->getCond()); 12722 } 12723 12724 // C++11 [expr.cond]p1: 12725 // [...] The first expression is contextually converted to bool (Clause 4). 12726 // It is evaluated and if it is true, the result of the conditional 12727 // expression is the value of the second expression, otherwise that of the 12728 // third expression. Only one of the second and third expressions is 12729 // evaluated. [...] 12730 bool EvalResult = false; 12731 bool EvalOK = Eval.evaluate(CO->getCond(), EvalResult); 12732 bool ShouldVisitTrueExpr = !EvalOK || EvalResult; 12733 bool ShouldVisitFalseExpr = !EvalOK || !EvalResult; 12734 if (ShouldVisitTrueExpr) { 12735 Region = TrueRegion; 12736 Visit(CO->getTrueExpr()); 12737 } 12738 if (ShouldVisitFalseExpr) { 12739 Region = FalseRegion; 12740 Visit(CO->getFalseExpr()); 12741 } 12742 12743 Region = OldRegion; 12744 Tree.merge(ConditionRegion); 12745 Tree.merge(TrueRegion); 12746 Tree.merge(FalseRegion); 12747 } 12748 12749 void VisitCallExpr(const CallExpr *CE) { 12750 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. 12751 12752 if (CE->isUnevaluatedBuiltinCall(Context)) 12753 return; 12754 12755 // C++11 [intro.execution]p15: 12756 // When calling a function [...], every value computation and side effect 12757 // associated with any argument expression, or with the postfix expression 12758 // designating the called function, is sequenced before execution of every 12759 // expression or statement in the body of the function [and thus before 12760 // the value computation of its result]. 12761 SequencedSubexpression Sequenced(*this); 12762 SemaRef.runWithSufficientStackSpace(CE->getExprLoc(), [&] { 12763 // C++17 [expr.call]p5 12764 // The postfix-expression is sequenced before each expression in the 12765 // expression-list and any default argument. [...] 12766 SequenceTree::Seq CalleeRegion; 12767 SequenceTree::Seq OtherRegion; 12768 if (SemaRef.getLangOpts().CPlusPlus17) { 12769 CalleeRegion = Tree.allocate(Region); 12770 OtherRegion = Tree.allocate(Region); 12771 } else { 12772 CalleeRegion = Region; 12773 OtherRegion = Region; 12774 } 12775 SequenceTree::Seq OldRegion = Region; 12776 12777 // Visit the callee expression first. 12778 Region = CalleeRegion; 12779 if (SemaRef.getLangOpts().CPlusPlus17) { 12780 SequencedSubexpression Sequenced(*this); 12781 Visit(CE->getCallee()); 12782 } else { 12783 Visit(CE->getCallee()); 12784 } 12785 12786 // Then visit the argument expressions. 12787 Region = OtherRegion; 12788 for (const Expr *Argument : CE->arguments()) 12789 Visit(Argument); 12790 12791 Region = OldRegion; 12792 if (SemaRef.getLangOpts().CPlusPlus17) { 12793 Tree.merge(CalleeRegion); 12794 Tree.merge(OtherRegion); 12795 } 12796 }); 12797 } 12798 12799 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CXXOCE) { 12800 // C++17 [over.match.oper]p2: 12801 // [...] the operator notation is first transformed to the equivalent 12802 // function-call notation as summarized in Table 12 (where @ denotes one 12803 // of the operators covered in the specified subclause). However, the 12804 // operands are sequenced in the order prescribed for the built-in 12805 // operator (Clause 8). 12806 // 12807 // From the above only overloaded binary operators and overloaded call 12808 // operators have sequencing rules in C++17 that we need to handle 12809 // separately. 12810 if (!SemaRef.getLangOpts().CPlusPlus17 || 12811 (CXXOCE->getNumArgs() != 2 && CXXOCE->getOperator() != OO_Call)) 12812 return VisitCallExpr(CXXOCE); 12813 12814 enum { 12815 NoSequencing, 12816 LHSBeforeRHS, 12817 RHSBeforeLHS, 12818 LHSBeforeRest 12819 } SequencingKind; 12820 switch (CXXOCE->getOperator()) { 12821 case OO_Equal: 12822 case OO_PlusEqual: 12823 case OO_MinusEqual: 12824 case OO_StarEqual: 12825 case OO_SlashEqual: 12826 case OO_PercentEqual: 12827 case OO_CaretEqual: 12828 case OO_AmpEqual: 12829 case OO_PipeEqual: 12830 case OO_LessLessEqual: 12831 case OO_GreaterGreaterEqual: 12832 SequencingKind = RHSBeforeLHS; 12833 break; 12834 12835 case OO_LessLess: 12836 case OO_GreaterGreater: 12837 case OO_AmpAmp: 12838 case OO_PipePipe: 12839 case OO_Comma: 12840 case OO_ArrowStar: 12841 case OO_Subscript: 12842 SequencingKind = LHSBeforeRHS; 12843 break; 12844 12845 case OO_Call: 12846 SequencingKind = LHSBeforeRest; 12847 break; 12848 12849 default: 12850 SequencingKind = NoSequencing; 12851 break; 12852 } 12853 12854 if (SequencingKind == NoSequencing) 12855 return VisitCallExpr(CXXOCE); 12856 12857 // This is a call, so all subexpressions are sequenced before the result. 12858 SequencedSubexpression Sequenced(*this); 12859 12860 SemaRef.runWithSufficientStackSpace(CXXOCE->getExprLoc(), [&] { 12861 assert(SemaRef.getLangOpts().CPlusPlus17 && 12862 "Should only get there with C++17 and above!"); 12863 assert((CXXOCE->getNumArgs() == 2 || CXXOCE->getOperator() == OO_Call) && 12864 "Should only get there with an overloaded binary operator" 12865 " or an overloaded call operator!"); 12866 12867 if (SequencingKind == LHSBeforeRest) { 12868 assert(CXXOCE->getOperator() == OO_Call && 12869 "We should only have an overloaded call operator here!"); 12870 12871 // This is very similar to VisitCallExpr, except that we only have the 12872 // C++17 case. The postfix-expression is the first argument of the 12873 // CXXOperatorCallExpr. The expressions in the expression-list, if any, 12874 // are in the following arguments. 12875 // 12876 // Note that we intentionally do not visit the callee expression since 12877 // it is just a decayed reference to a function. 12878 SequenceTree::Seq PostfixExprRegion = Tree.allocate(Region); 12879 SequenceTree::Seq ArgsRegion = Tree.allocate(Region); 12880 SequenceTree::Seq OldRegion = Region; 12881 12882 assert(CXXOCE->getNumArgs() >= 1 && 12883 "An overloaded call operator must have at least one argument" 12884 " for the postfix-expression!"); 12885 const Expr *PostfixExpr = CXXOCE->getArgs()[0]; 12886 llvm::ArrayRef<const Expr *> Args(CXXOCE->getArgs() + 1, 12887 CXXOCE->getNumArgs() - 1); 12888 12889 // Visit the postfix-expression first. 12890 { 12891 Region = PostfixExprRegion; 12892 SequencedSubexpression Sequenced(*this); 12893 Visit(PostfixExpr); 12894 } 12895 12896 // Then visit the argument expressions. 12897 Region = ArgsRegion; 12898 for (const Expr *Arg : Args) 12899 Visit(Arg); 12900 12901 Region = OldRegion; 12902 Tree.merge(PostfixExprRegion); 12903 Tree.merge(ArgsRegion); 12904 } else { 12905 assert(CXXOCE->getNumArgs() == 2 && 12906 "Should only have two arguments here!"); 12907 assert((SequencingKind == LHSBeforeRHS || 12908 SequencingKind == RHSBeforeLHS) && 12909 "Unexpected sequencing kind!"); 12910 12911 // We do not visit the callee expression since it is just a decayed 12912 // reference to a function. 12913 const Expr *E1 = CXXOCE->getArg(0); 12914 const Expr *E2 = CXXOCE->getArg(1); 12915 if (SequencingKind == RHSBeforeLHS) 12916 std::swap(E1, E2); 12917 12918 return VisitSequencedExpressions(E1, E2); 12919 } 12920 }); 12921 } 12922 12923 void VisitCXXConstructExpr(const CXXConstructExpr *CCE) { 12924 // This is a call, so all subexpressions are sequenced before the result. 12925 SequencedSubexpression Sequenced(*this); 12926 12927 if (!CCE->isListInitialization()) 12928 return VisitExpr(CCE); 12929 12930 // In C++11, list initializations are sequenced. 12931 SequenceExpressionsInOrder( 12932 llvm::ArrayRef(CCE->getArgs(), CCE->getNumArgs())); 12933 } 12934 12935 void VisitInitListExpr(const InitListExpr *ILE) { 12936 if (!SemaRef.getLangOpts().CPlusPlus11) 12937 return VisitExpr(ILE); 12938 12939 // In C++11, list initializations are sequenced. 12940 SequenceExpressionsInOrder(ILE->inits()); 12941 } 12942 12943 void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) { 12944 // C++20 parenthesized list initializations are sequenced. See C++20 12945 // [decl.init.general]p16.5 and [decl.init.general]p16.6.2.2. 12946 SequenceExpressionsInOrder(PLIE->getInitExprs()); 12947 } 12948 12949 private: 12950 void SequenceExpressionsInOrder(ArrayRef<const Expr *> ExpressionList) { 12951 SmallVector<SequenceTree::Seq, 32> Elts; 12952 SequenceTree::Seq Parent = Region; 12953 for (const Expr *E : ExpressionList) { 12954 if (!E) 12955 continue; 12956 Region = Tree.allocate(Parent); 12957 Elts.push_back(Region); 12958 Visit(E); 12959 } 12960 12961 // Forget that the initializers are sequenced. 12962 Region = Parent; 12963 for (unsigned I = 0; I < Elts.size(); ++I) 12964 Tree.merge(Elts[I]); 12965 } 12966 }; 12967 12968 SequenceChecker::UsageInfo::UsageInfo() = default; 12969 12970 } // namespace 12971 12972 void Sema::CheckUnsequencedOperations(const Expr *E) { 12973 SmallVector<const Expr *, 8> WorkList; 12974 WorkList.push_back(E); 12975 while (!WorkList.empty()) { 12976 const Expr *Item = WorkList.pop_back_val(); 12977 SequenceChecker(*this, Item, WorkList); 12978 } 12979 } 12980 12981 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, 12982 bool IsConstexpr) { 12983 llvm::SaveAndRestore ConstantContext(isConstantEvaluatedOverride, 12984 IsConstexpr || isa<ConstantExpr>(E)); 12985 CheckImplicitConversions(E, CheckLoc); 12986 if (!E->isInstantiationDependent()) 12987 CheckUnsequencedOperations(E); 12988 if (!IsConstexpr && !E->isValueDependent()) 12989 CheckForIntOverflow(E); 12990 DiagnoseMisalignedMembers(); 12991 } 12992 12993 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, 12994 FieldDecl *BitField, 12995 Expr *Init) { 12996 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); 12997 } 12998 12999 static void diagnoseArrayStarInParamType(Sema &S, QualType PType, 13000 SourceLocation Loc) { 13001 if (!PType->isVariablyModifiedType()) 13002 return; 13003 if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { 13004 diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); 13005 return; 13006 } 13007 if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { 13008 diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); 13009 return; 13010 } 13011 if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { 13012 diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); 13013 return; 13014 } 13015 13016 const ArrayType *AT = S.Context.getAsArrayType(PType); 13017 if (!AT) 13018 return; 13019 13020 if (AT->getSizeModifier() != ArraySizeModifier::Star) { 13021 diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); 13022 return; 13023 } 13024 13025 S.Diag(Loc, diag::err_array_star_in_function_definition); 13026 } 13027 13028 bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters, 13029 bool CheckParameterNames) { 13030 bool HasInvalidParm = false; 13031 for (ParmVarDecl *Param : Parameters) { 13032 assert(Param && "null in a parameter list"); 13033 // C99 6.7.5.3p4: the parameters in a parameter type list in a 13034 // function declarator that is part of a function definition of 13035 // that function shall not have incomplete type. 13036 // 13037 // C++23 [dcl.fct.def.general]/p2 13038 // The type of a parameter [...] for a function definition 13039 // shall not be a (possibly cv-qualified) class type that is incomplete 13040 // or abstract within the function body unless the function is deleted. 13041 if (!Param->isInvalidDecl() && 13042 (RequireCompleteType(Param->getLocation(), Param->getType(), 13043 diag::err_typecheck_decl_incomplete_type) || 13044 RequireNonAbstractType(Param->getBeginLoc(), Param->getOriginalType(), 13045 diag::err_abstract_type_in_decl, 13046 AbstractParamType))) { 13047 Param->setInvalidDecl(); 13048 HasInvalidParm = true; 13049 } 13050 13051 // C99 6.9.1p5: If the declarator includes a parameter type list, the 13052 // declaration of each parameter shall include an identifier. 13053 if (CheckParameterNames && Param->getIdentifier() == nullptr && 13054 !Param->isImplicit() && !getLangOpts().CPlusPlus) { 13055 // Diagnose this as an extension in C17 and earlier. 13056 if (!getLangOpts().C23) 13057 Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c23); 13058 } 13059 13060 // C99 6.7.5.3p12: 13061 // If the function declarator is not part of a definition of that 13062 // function, parameters may have incomplete type and may use the [*] 13063 // notation in their sequences of declarator specifiers to specify 13064 // variable length array types. 13065 QualType PType = Param->getOriginalType(); 13066 // FIXME: This diagnostic should point the '[*]' if source-location 13067 // information is added for it. 13068 diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); 13069 13070 // If the parameter is a c++ class type and it has to be destructed in the 13071 // callee function, declare the destructor so that it can be called by the 13072 // callee function. Do not perform any direct access check on the dtor here. 13073 if (!Param->isInvalidDecl()) { 13074 if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) { 13075 if (!ClassDecl->isInvalidDecl() && 13076 !ClassDecl->hasIrrelevantDestructor() && 13077 !ClassDecl->isDependentContext() && 13078 ClassDecl->isParamDestroyedInCallee()) { 13079 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); 13080 MarkFunctionReferenced(Param->getLocation(), Destructor); 13081 DiagnoseUseOfDecl(Destructor, Param->getLocation()); 13082 } 13083 } 13084 } 13085 13086 // Parameters with the pass_object_size attribute only need to be marked 13087 // constant at function definitions. Because we lack information about 13088 // whether we're on a declaration or definition when we're instantiating the 13089 // attribute, we need to check for constness here. 13090 if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>()) 13091 if (!Param->getType().isConstQualified()) 13092 Diag(Param->getLocation(), diag::err_attribute_pointers_only) 13093 << Attr->getSpelling() << 1; 13094 13095 // Check for parameter names shadowing fields from the class. 13096 if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) { 13097 // The owning context for the parameter should be the function, but we 13098 // want to see if this function's declaration context is a record. 13099 DeclContext *DC = Param->getDeclContext(); 13100 if (DC && DC->isFunctionOrMethod()) { 13101 if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent())) 13102 CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(), 13103 RD, /*DeclIsField*/ false); 13104 } 13105 } 13106 13107 if (!Param->isInvalidDecl() && 13108 Param->getOriginalType()->isWebAssemblyTableType()) { 13109 Param->setInvalidDecl(); 13110 HasInvalidParm = true; 13111 Diag(Param->getLocation(), diag::err_wasm_table_as_function_parameter); 13112 } 13113 } 13114 13115 return HasInvalidParm; 13116 } 13117 13118 std::optional<std::pair< 13119 CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr 13120 *E, 13121 ASTContext 13122 &Ctx); 13123 13124 /// Compute the alignment and offset of the base class object given the 13125 /// derived-to-base cast expression and the alignment and offset of the derived 13126 /// class object. 13127 static std::pair<CharUnits, CharUnits> 13128 getDerivedToBaseAlignmentAndOffset(const CastExpr *CE, QualType DerivedType, 13129 CharUnits BaseAlignment, CharUnits Offset, 13130 ASTContext &Ctx) { 13131 for (auto PathI = CE->path_begin(), PathE = CE->path_end(); PathI != PathE; 13132 ++PathI) { 13133 const CXXBaseSpecifier *Base = *PathI; 13134 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); 13135 if (Base->isVirtual()) { 13136 // The complete object may have a lower alignment than the non-virtual 13137 // alignment of the base, in which case the base may be misaligned. Choose 13138 // the smaller of the non-virtual alignment and BaseAlignment, which is a 13139 // conservative lower bound of the complete object alignment. 13140 CharUnits NonVirtualAlignment = 13141 Ctx.getASTRecordLayout(BaseDecl).getNonVirtualAlignment(); 13142 BaseAlignment = std::min(BaseAlignment, NonVirtualAlignment); 13143 Offset = CharUnits::Zero(); 13144 } else { 13145 const ASTRecordLayout &RL = 13146 Ctx.getASTRecordLayout(DerivedType->getAsCXXRecordDecl()); 13147 Offset += RL.getBaseClassOffset(BaseDecl); 13148 } 13149 DerivedType = Base->getType(); 13150 } 13151 13152 return std::make_pair(BaseAlignment, Offset); 13153 } 13154 13155 /// Compute the alignment and offset of a binary additive operator. 13156 static std::optional<std::pair<CharUnits, CharUnits>> 13157 getAlignmentAndOffsetFromBinAddOrSub(const Expr *PtrE, const Expr *IntE, 13158 bool IsSub, ASTContext &Ctx) { 13159 QualType PointeeType = PtrE->getType()->getPointeeType(); 13160 13161 if (!PointeeType->isConstantSizeType()) 13162 return std::nullopt; 13163 13164 auto P = getBaseAlignmentAndOffsetFromPtr(PtrE, Ctx); 13165 13166 if (!P) 13167 return std::nullopt; 13168 13169 CharUnits EltSize = Ctx.getTypeSizeInChars(PointeeType); 13170 if (std::optional<llvm::APSInt> IdxRes = IntE->getIntegerConstantExpr(Ctx)) { 13171 CharUnits Offset = EltSize * IdxRes->getExtValue(); 13172 if (IsSub) 13173 Offset = -Offset; 13174 return std::make_pair(P->first, P->second + Offset); 13175 } 13176 13177 // If the integer expression isn't a constant expression, compute the lower 13178 // bound of the alignment using the alignment and offset of the pointer 13179 // expression and the element size. 13180 return std::make_pair( 13181 P->first.alignmentAtOffset(P->second).alignmentAtOffset(EltSize), 13182 CharUnits::Zero()); 13183 } 13184 13185 /// This helper function takes an lvalue expression and returns the alignment of 13186 /// a VarDecl and a constant offset from the VarDecl. 13187 std::optional<std::pair< 13188 CharUnits, 13189 CharUnits>> static getBaseAlignmentAndOffsetFromLValue(const Expr *E, 13190 ASTContext &Ctx) { 13191 E = E->IgnoreParens(); 13192 switch (E->getStmtClass()) { 13193 default: 13194 break; 13195 case Stmt::CStyleCastExprClass: 13196 case Stmt::CXXStaticCastExprClass: 13197 case Stmt::ImplicitCastExprClass: { 13198 auto *CE = cast<CastExpr>(E); 13199 const Expr *From = CE->getSubExpr(); 13200 switch (CE->getCastKind()) { 13201 default: 13202 break; 13203 case CK_NoOp: 13204 return getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13205 case CK_UncheckedDerivedToBase: 13206 case CK_DerivedToBase: { 13207 auto P = getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13208 if (!P) 13209 break; 13210 return getDerivedToBaseAlignmentAndOffset(CE, From->getType(), P->first, 13211 P->second, Ctx); 13212 } 13213 } 13214 break; 13215 } 13216 case Stmt::ArraySubscriptExprClass: { 13217 auto *ASE = cast<ArraySubscriptExpr>(E); 13218 return getAlignmentAndOffsetFromBinAddOrSub(ASE->getBase(), ASE->getIdx(), 13219 false, Ctx); 13220 } 13221 case Stmt::DeclRefExprClass: { 13222 if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) { 13223 // FIXME: If VD is captured by copy or is an escaping __block variable, 13224 // use the alignment of VD's type. 13225 if (!VD->getType()->isReferenceType()) { 13226 // Dependent alignment cannot be resolved -> bail out. 13227 if (VD->hasDependentAlignment()) 13228 break; 13229 return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); 13230 } 13231 if (VD->hasInit()) 13232 return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); 13233 } 13234 break; 13235 } 13236 case Stmt::MemberExprClass: { 13237 auto *ME = cast<MemberExpr>(E); 13238 auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); 13239 if (!FD || FD->getType()->isReferenceType() || 13240 FD->getParent()->isInvalidDecl()) 13241 break; 13242 std::optional<std::pair<CharUnits, CharUnits>> P; 13243 if (ME->isArrow()) 13244 P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx); 13245 else 13246 P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx); 13247 if (!P) 13248 break; 13249 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent()); 13250 uint64_t Offset = Layout.getFieldOffset(FD->getFieldIndex()); 13251 return std::make_pair(P->first, 13252 P->second + CharUnits::fromQuantity(Offset)); 13253 } 13254 case Stmt::UnaryOperatorClass: { 13255 auto *UO = cast<UnaryOperator>(E); 13256 switch (UO->getOpcode()) { 13257 default: 13258 break; 13259 case UO_Deref: 13260 return getBaseAlignmentAndOffsetFromPtr(UO->getSubExpr(), Ctx); 13261 } 13262 break; 13263 } 13264 case Stmt::BinaryOperatorClass: { 13265 auto *BO = cast<BinaryOperator>(E); 13266 auto Opcode = BO->getOpcode(); 13267 switch (Opcode) { 13268 default: 13269 break; 13270 case BO_Comma: 13271 return getBaseAlignmentAndOffsetFromLValue(BO->getRHS(), Ctx); 13272 } 13273 break; 13274 } 13275 } 13276 return std::nullopt; 13277 } 13278 13279 /// This helper function takes a pointer expression and returns the alignment of 13280 /// a VarDecl and a constant offset from the VarDecl. 13281 std::optional<std::pair< 13282 CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr 13283 *E, 13284 ASTContext 13285 &Ctx) { 13286 E = E->IgnoreParens(); 13287 switch (E->getStmtClass()) { 13288 default: 13289 break; 13290 case Stmt::CStyleCastExprClass: 13291 case Stmt::CXXStaticCastExprClass: 13292 case Stmt::ImplicitCastExprClass: { 13293 auto *CE = cast<CastExpr>(E); 13294 const Expr *From = CE->getSubExpr(); 13295 switch (CE->getCastKind()) { 13296 default: 13297 break; 13298 case CK_NoOp: 13299 return getBaseAlignmentAndOffsetFromPtr(From, Ctx); 13300 case CK_ArrayToPointerDecay: 13301 return getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13302 case CK_UncheckedDerivedToBase: 13303 case CK_DerivedToBase: { 13304 auto P = getBaseAlignmentAndOffsetFromPtr(From, Ctx); 13305 if (!P) 13306 break; 13307 return getDerivedToBaseAlignmentAndOffset( 13308 CE, From->getType()->getPointeeType(), P->first, P->second, Ctx); 13309 } 13310 } 13311 break; 13312 } 13313 case Stmt::CXXThisExprClass: { 13314 auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl(); 13315 CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment(); 13316 return std::make_pair(Alignment, CharUnits::Zero()); 13317 } 13318 case Stmt::UnaryOperatorClass: { 13319 auto *UO = cast<UnaryOperator>(E); 13320 if (UO->getOpcode() == UO_AddrOf) 13321 return getBaseAlignmentAndOffsetFromLValue(UO->getSubExpr(), Ctx); 13322 break; 13323 } 13324 case Stmt::BinaryOperatorClass: { 13325 auto *BO = cast<BinaryOperator>(E); 13326 auto Opcode = BO->getOpcode(); 13327 switch (Opcode) { 13328 default: 13329 break; 13330 case BO_Add: 13331 case BO_Sub: { 13332 const Expr *LHS = BO->getLHS(), *RHS = BO->getRHS(); 13333 if (Opcode == BO_Add && !RHS->getType()->isIntegralOrEnumerationType()) 13334 std::swap(LHS, RHS); 13335 return getAlignmentAndOffsetFromBinAddOrSub(LHS, RHS, Opcode == BO_Sub, 13336 Ctx); 13337 } 13338 case BO_Comma: 13339 return getBaseAlignmentAndOffsetFromPtr(BO->getRHS(), Ctx); 13340 } 13341 break; 13342 } 13343 } 13344 return std::nullopt; 13345 } 13346 13347 static CharUnits getPresumedAlignmentOfPointer(const Expr *E, Sema &S) { 13348 // See if we can compute the alignment of a VarDecl and an offset from it. 13349 std::optional<std::pair<CharUnits, CharUnits>> P = 13350 getBaseAlignmentAndOffsetFromPtr(E, S.Context); 13351 13352 if (P) 13353 return P->first.alignmentAtOffset(P->second); 13354 13355 // If that failed, return the type's alignment. 13356 return S.Context.getTypeAlignInChars(E->getType()->getPointeeType()); 13357 } 13358 13359 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { 13360 // This is actually a lot of work to potentially be doing on every 13361 // cast; don't do it if we're ignoring -Wcast_align (as is the default). 13362 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) 13363 return; 13364 13365 // Ignore dependent types. 13366 if (T->isDependentType() || Op->getType()->isDependentType()) 13367 return; 13368 13369 // Require that the destination be a pointer type. 13370 const PointerType *DestPtr = T->getAs<PointerType>(); 13371 if (!DestPtr) return; 13372 13373 // If the destination has alignment 1, we're done. 13374 QualType DestPointee = DestPtr->getPointeeType(); 13375 if (DestPointee->isIncompleteType()) return; 13376 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); 13377 if (DestAlign.isOne()) return; 13378 13379 // Require that the source be a pointer type. 13380 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); 13381 if (!SrcPtr) return; 13382 QualType SrcPointee = SrcPtr->getPointeeType(); 13383 13384 // Explicitly allow casts from cv void*. We already implicitly 13385 // allowed casts to cv void*, since they have alignment 1. 13386 // Also allow casts involving incomplete types, which implicitly 13387 // includes 'void'. 13388 if (SrcPointee->isIncompleteType()) return; 13389 13390 CharUnits SrcAlign = getPresumedAlignmentOfPointer(Op, *this); 13391 13392 if (SrcAlign >= DestAlign) return; 13393 13394 Diag(TRange.getBegin(), diag::warn_cast_align) 13395 << Op->getType() << T 13396 << static_cast<unsigned>(SrcAlign.getQuantity()) 13397 << static_cast<unsigned>(DestAlign.getQuantity()) 13398 << TRange << Op->getSourceRange(); 13399 } 13400 13401 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 13402 const ArraySubscriptExpr *ASE, 13403 bool AllowOnePastEnd, bool IndexNegated) { 13404 // Already diagnosed by the constant evaluator. 13405 if (isConstantEvaluatedContext()) 13406 return; 13407 13408 IndexExpr = IndexExpr->IgnoreParenImpCasts(); 13409 if (IndexExpr->isValueDependent()) 13410 return; 13411 13412 const Type *EffectiveType = 13413 BaseExpr->getType()->getPointeeOrArrayElementType(); 13414 BaseExpr = BaseExpr->IgnoreParenCasts(); 13415 const ConstantArrayType *ArrayTy = 13416 Context.getAsConstantArrayType(BaseExpr->getType()); 13417 13418 LangOptions::StrictFlexArraysLevelKind 13419 StrictFlexArraysLevel = getLangOpts().getStrictFlexArraysLevel(); 13420 13421 const Type *BaseType = 13422 ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr(); 13423 bool IsUnboundedArray = 13424 BaseType == nullptr || BaseExpr->isFlexibleArrayMemberLike( 13425 Context, StrictFlexArraysLevel, 13426 /*IgnoreTemplateOrMacroSubstitution=*/true); 13427 if (EffectiveType->isDependentType() || 13428 (!IsUnboundedArray && BaseType->isDependentType())) 13429 return; 13430 13431 Expr::EvalResult Result; 13432 if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) 13433 return; 13434 13435 llvm::APSInt index = Result.Val.getInt(); 13436 if (IndexNegated) { 13437 index.setIsUnsigned(false); 13438 index = -index; 13439 } 13440 13441 if (IsUnboundedArray) { 13442 if (EffectiveType->isFunctionType()) 13443 return; 13444 if (index.isUnsigned() || !index.isNegative()) { 13445 const auto &ASTC = getASTContext(); 13446 unsigned AddrBits = ASTC.getTargetInfo().getPointerWidth( 13447 EffectiveType->getCanonicalTypeInternal().getAddressSpace()); 13448 if (index.getBitWidth() < AddrBits) 13449 index = index.zext(AddrBits); 13450 std::optional<CharUnits> ElemCharUnits = 13451 ASTC.getTypeSizeInCharsIfKnown(EffectiveType); 13452 // PR50741 - If EffectiveType has unknown size (e.g., if it's a void 13453 // pointer) bounds-checking isn't meaningful. 13454 if (!ElemCharUnits || ElemCharUnits->isZero()) 13455 return; 13456 llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity()); 13457 // If index has more active bits than address space, we already know 13458 // we have a bounds violation to warn about. Otherwise, compute 13459 // address of (index + 1)th element, and warn about bounds violation 13460 // only if that address exceeds address space. 13461 if (index.getActiveBits() <= AddrBits) { 13462 bool Overflow; 13463 llvm::APInt Product(index); 13464 Product += 1; 13465 Product = Product.umul_ov(ElemBytes, Overflow); 13466 if (!Overflow && Product.getActiveBits() <= AddrBits) 13467 return; 13468 } 13469 13470 // Need to compute max possible elements in address space, since that 13471 // is included in diag message. 13472 llvm::APInt MaxElems = llvm::APInt::getMaxValue(AddrBits); 13473 MaxElems = MaxElems.zext(std::max(AddrBits + 1, ElemBytes.getBitWidth())); 13474 MaxElems += 1; 13475 ElemBytes = ElemBytes.zextOrTrunc(MaxElems.getBitWidth()); 13476 MaxElems = MaxElems.udiv(ElemBytes); 13477 13478 unsigned DiagID = 13479 ASE ? diag::warn_array_index_exceeds_max_addressable_bounds 13480 : diag::warn_ptr_arith_exceeds_max_addressable_bounds; 13481 13482 // Diag message shows element size in bits and in "bytes" (platform- 13483 // dependent CharUnits) 13484 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 13485 PDiag(DiagID) 13486 << toString(index, 10, true) << AddrBits 13487 << (unsigned)ASTC.toBits(*ElemCharUnits) 13488 << toString(ElemBytes, 10, false) 13489 << toString(MaxElems, 10, false) 13490 << (unsigned)MaxElems.getLimitedValue(~0U) 13491 << IndexExpr->getSourceRange()); 13492 13493 const NamedDecl *ND = nullptr; 13494 // Try harder to find a NamedDecl to point at in the note. 13495 while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) 13496 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 13497 if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 13498 ND = DRE->getDecl(); 13499 if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) 13500 ND = ME->getMemberDecl(); 13501 13502 if (ND) 13503 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 13504 PDiag(diag::note_array_declared_here) << ND); 13505 } 13506 return; 13507 } 13508 13509 if (index.isUnsigned() || !index.isNegative()) { 13510 // It is possible that the type of the base expression after 13511 // IgnoreParenCasts is incomplete, even though the type of the base 13512 // expression before IgnoreParenCasts is complete (see PR39746 for an 13513 // example). In this case we have no information about whether the array 13514 // access exceeds the array bounds. However we can still diagnose an array 13515 // access which precedes the array bounds. 13516 if (BaseType->isIncompleteType()) 13517 return; 13518 13519 llvm::APInt size = ArrayTy->getSize(); 13520 13521 if (BaseType != EffectiveType) { 13522 // Make sure we're comparing apples to apples when comparing index to 13523 // size. 13524 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); 13525 uint64_t array_typesize = Context.getTypeSize(BaseType); 13526 13527 // Handle ptrarith_typesize being zero, such as when casting to void*. 13528 // Use the size in bits (what "getTypeSize()" returns) rather than bytes. 13529 if (!ptrarith_typesize) 13530 ptrarith_typesize = Context.getCharWidth(); 13531 13532 if (ptrarith_typesize != array_typesize) { 13533 // There's a cast to a different size type involved. 13534 uint64_t ratio = array_typesize / ptrarith_typesize; 13535 13536 // TODO: Be smarter about handling cases where array_typesize is not a 13537 // multiple of ptrarith_typesize. 13538 if (ptrarith_typesize * ratio == array_typesize) 13539 size *= llvm::APInt(size.getBitWidth(), ratio); 13540 } 13541 } 13542 13543 if (size.getBitWidth() > index.getBitWidth()) 13544 index = index.zext(size.getBitWidth()); 13545 else if (size.getBitWidth() < index.getBitWidth()) 13546 size = size.zext(index.getBitWidth()); 13547 13548 // For array subscripting the index must be less than size, but for pointer 13549 // arithmetic also allow the index (offset) to be equal to size since 13550 // computing the next address after the end of the array is legal and 13551 // commonly done e.g. in C++ iterators and range-based for loops. 13552 if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) 13553 return; 13554 13555 // Suppress the warning if the subscript expression (as identified by the 13556 // ']' location) and the index expression are both from macro expansions 13557 // within a system header. 13558 if (ASE) { 13559 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( 13560 ASE->getRBracketLoc()); 13561 if (SourceMgr.isInSystemHeader(RBracketLoc)) { 13562 SourceLocation IndexLoc = 13563 SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc()); 13564 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) 13565 return; 13566 } 13567 } 13568 13569 unsigned DiagID = ASE ? diag::warn_array_index_exceeds_bounds 13570 : diag::warn_ptr_arith_exceeds_bounds; 13571 unsigned CastMsg = (!ASE || BaseType == EffectiveType) ? 0 : 1; 13572 QualType CastMsgTy = ASE ? ASE->getLHS()->getType() : QualType(); 13573 13574 DiagRuntimeBehavior( 13575 BaseExpr->getBeginLoc(), BaseExpr, 13576 PDiag(DiagID) << toString(index, 10, true) << ArrayTy->desugar() 13577 << CastMsg << CastMsgTy << IndexExpr->getSourceRange()); 13578 } else { 13579 unsigned DiagID = diag::warn_array_index_precedes_bounds; 13580 if (!ASE) { 13581 DiagID = diag::warn_ptr_arith_precedes_bounds; 13582 if (index.isNegative()) index = -index; 13583 } 13584 13585 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 13586 PDiag(DiagID) << toString(index, 10, true) 13587 << IndexExpr->getSourceRange()); 13588 } 13589 13590 const NamedDecl *ND = nullptr; 13591 // Try harder to find a NamedDecl to point at in the note. 13592 while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) 13593 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 13594 if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 13595 ND = DRE->getDecl(); 13596 if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) 13597 ND = ME->getMemberDecl(); 13598 13599 if (ND) 13600 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 13601 PDiag(diag::note_array_declared_here) << ND); 13602 } 13603 13604 void Sema::CheckArrayAccess(const Expr *expr) { 13605 int AllowOnePastEnd = 0; 13606 while (expr) { 13607 expr = expr->IgnoreParenImpCasts(); 13608 switch (expr->getStmtClass()) { 13609 case Stmt::ArraySubscriptExprClass: { 13610 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); 13611 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, 13612 AllowOnePastEnd > 0); 13613 expr = ASE->getBase(); 13614 break; 13615 } 13616 case Stmt::MemberExprClass: { 13617 expr = cast<MemberExpr>(expr)->getBase(); 13618 break; 13619 } 13620 case Stmt::ArraySectionExprClass: { 13621 const ArraySectionExpr *ASE = cast<ArraySectionExpr>(expr); 13622 // FIXME: We should probably be checking all of the elements to the 13623 // 'length' here as well. 13624 if (ASE->getLowerBound()) 13625 CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(), 13626 /*ASE=*/nullptr, AllowOnePastEnd > 0); 13627 return; 13628 } 13629 case Stmt::UnaryOperatorClass: { 13630 // Only unwrap the * and & unary operators 13631 const UnaryOperator *UO = cast<UnaryOperator>(expr); 13632 expr = UO->getSubExpr(); 13633 switch (UO->getOpcode()) { 13634 case UO_AddrOf: 13635 AllowOnePastEnd++; 13636 break; 13637 case UO_Deref: 13638 AllowOnePastEnd--; 13639 break; 13640 default: 13641 return; 13642 } 13643 break; 13644 } 13645 case Stmt::ConditionalOperatorClass: { 13646 const ConditionalOperator *cond = cast<ConditionalOperator>(expr); 13647 if (const Expr *lhs = cond->getLHS()) 13648 CheckArrayAccess(lhs); 13649 if (const Expr *rhs = cond->getRHS()) 13650 CheckArrayAccess(rhs); 13651 return; 13652 } 13653 case Stmt::CXXOperatorCallExprClass: { 13654 const auto *OCE = cast<CXXOperatorCallExpr>(expr); 13655 for (const auto *Arg : OCE->arguments()) 13656 CheckArrayAccess(Arg); 13657 return; 13658 } 13659 default: 13660 return; 13661 } 13662 } 13663 } 13664 13665 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, 13666 Expr *RHS, bool isProperty) { 13667 // Check if RHS is an Objective-C object literal, which also can get 13668 // immediately zapped in a weak reference. Note that we explicitly 13669 // allow ObjCStringLiterals, since those are designed to never really die. 13670 RHS = RHS->IgnoreParenImpCasts(); 13671 13672 // This enum needs to match with the 'select' in 13673 // warn_objc_arc_literal_assign (off-by-1). 13674 SemaObjC::ObjCLiteralKind Kind = S.ObjC().CheckLiteralKind(RHS); 13675 if (Kind == SemaObjC::LK_String || Kind == SemaObjC::LK_None) 13676 return false; 13677 13678 S.Diag(Loc, diag::warn_arc_literal_assign) 13679 << (unsigned) Kind 13680 << (isProperty ? 0 : 1) 13681 << RHS->getSourceRange(); 13682 13683 return true; 13684 } 13685 13686 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, 13687 Qualifiers::ObjCLifetime LT, 13688 Expr *RHS, bool isProperty) { 13689 // Strip off any implicit cast added to get to the one ARC-specific. 13690 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13691 if (cast->getCastKind() == CK_ARCConsumeObject) { 13692 S.Diag(Loc, diag::warn_arc_retained_assign) 13693 << (LT == Qualifiers::OCL_ExplicitNone) 13694 << (isProperty ? 0 : 1) 13695 << RHS->getSourceRange(); 13696 return true; 13697 } 13698 RHS = cast->getSubExpr(); 13699 } 13700 13701 if (LT == Qualifiers::OCL_Weak && 13702 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) 13703 return true; 13704 13705 return false; 13706 } 13707 13708 bool Sema::checkUnsafeAssigns(SourceLocation Loc, 13709 QualType LHS, Expr *RHS) { 13710 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); 13711 13712 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) 13713 return false; 13714 13715 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) 13716 return true; 13717 13718 return false; 13719 } 13720 13721 void Sema::checkUnsafeExprAssigns(SourceLocation Loc, 13722 Expr *LHS, Expr *RHS) { 13723 QualType LHSType; 13724 // PropertyRef on LHS type need be directly obtained from 13725 // its declaration as it has a PseudoType. 13726 ObjCPropertyRefExpr *PRE 13727 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); 13728 if (PRE && !PRE->isImplicitProperty()) { 13729 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13730 if (PD) 13731 LHSType = PD->getType(); 13732 } 13733 13734 if (LHSType.isNull()) 13735 LHSType = LHS->getType(); 13736 13737 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); 13738 13739 if (LT == Qualifiers::OCL_Weak) { 13740 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) 13741 getCurFunction()->markSafeWeakUse(LHS); 13742 } 13743 13744 if (checkUnsafeAssigns(Loc, LHSType, RHS)) 13745 return; 13746 13747 // FIXME. Check for other life times. 13748 if (LT != Qualifiers::OCL_None) 13749 return; 13750 13751 if (PRE) { 13752 if (PRE->isImplicitProperty()) 13753 return; 13754 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13755 if (!PD) 13756 return; 13757 13758 unsigned Attributes = PD->getPropertyAttributes(); 13759 if (Attributes & ObjCPropertyAttribute::kind_assign) { 13760 // when 'assign' attribute was not explicitly specified 13761 // by user, ignore it and rely on property type itself 13762 // for lifetime info. 13763 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); 13764 if (!(AsWrittenAttr & ObjCPropertyAttribute::kind_assign) && 13765 LHSType->isObjCRetainableType()) 13766 return; 13767 13768 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13769 if (cast->getCastKind() == CK_ARCConsumeObject) { 13770 Diag(Loc, diag::warn_arc_retained_property_assign) 13771 << RHS->getSourceRange(); 13772 return; 13773 } 13774 RHS = cast->getSubExpr(); 13775 } 13776 } else if (Attributes & ObjCPropertyAttribute::kind_weak) { 13777 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) 13778 return; 13779 } 13780 } 13781 } 13782 13783 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// 13784 13785 static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, 13786 SourceLocation StmtLoc, 13787 const NullStmt *Body) { 13788 // Do not warn if the body is a macro that expands to nothing, e.g: 13789 // 13790 // #define CALL(x) 13791 // if (condition) 13792 // CALL(0); 13793 if (Body->hasLeadingEmptyMacro()) 13794 return false; 13795 13796 // Get line numbers of statement and body. 13797 bool StmtLineInvalid; 13798 unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, 13799 &StmtLineInvalid); 13800 if (StmtLineInvalid) 13801 return false; 13802 13803 bool BodyLineInvalid; 13804 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), 13805 &BodyLineInvalid); 13806 if (BodyLineInvalid) 13807 return false; 13808 13809 // Warn if null statement and body are on the same line. 13810 if (StmtLine != BodyLine) 13811 return false; 13812 13813 return true; 13814 } 13815 13816 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, 13817 const Stmt *Body, 13818 unsigned DiagID) { 13819 // Since this is a syntactic check, don't emit diagnostic for template 13820 // instantiations, this just adds noise. 13821 if (CurrentInstantiationScope) 13822 return; 13823 13824 // The body should be a null statement. 13825 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13826 if (!NBody) 13827 return; 13828 13829 // Do the usual checks. 13830 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13831 return; 13832 13833 Diag(NBody->getSemiLoc(), DiagID); 13834 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13835 } 13836 13837 void Sema::DiagnoseEmptyLoopBody(const Stmt *S, 13838 const Stmt *PossibleBody) { 13839 assert(!CurrentInstantiationScope); // Ensured by caller 13840 13841 SourceLocation StmtLoc; 13842 const Stmt *Body; 13843 unsigned DiagID; 13844 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { 13845 StmtLoc = FS->getRParenLoc(); 13846 Body = FS->getBody(); 13847 DiagID = diag::warn_empty_for_body; 13848 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { 13849 StmtLoc = WS->getRParenLoc(); 13850 Body = WS->getBody(); 13851 DiagID = diag::warn_empty_while_body; 13852 } else 13853 return; // Neither `for' nor `while'. 13854 13855 // The body should be a null statement. 13856 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13857 if (!NBody) 13858 return; 13859 13860 // Skip expensive checks if diagnostic is disabled. 13861 if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) 13862 return; 13863 13864 // Do the usual checks. 13865 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13866 return; 13867 13868 // `for(...);' and `while(...);' are popular idioms, so in order to keep 13869 // noise level low, emit diagnostics only if for/while is followed by a 13870 // CompoundStmt, e.g.: 13871 // for (int i = 0; i < n; i++); 13872 // { 13873 // a(i); 13874 // } 13875 // or if for/while is followed by a statement with more indentation 13876 // than for/while itself: 13877 // for (int i = 0; i < n; i++); 13878 // a(i); 13879 bool ProbableTypo = isa<CompoundStmt>(PossibleBody); 13880 if (!ProbableTypo) { 13881 bool BodyColInvalid; 13882 unsigned BodyCol = SourceMgr.getPresumedColumnNumber( 13883 PossibleBody->getBeginLoc(), &BodyColInvalid); 13884 if (BodyColInvalid) 13885 return; 13886 13887 bool StmtColInvalid; 13888 unsigned StmtCol = 13889 SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid); 13890 if (StmtColInvalid) 13891 return; 13892 13893 if (BodyCol > StmtCol) 13894 ProbableTypo = true; 13895 } 13896 13897 if (ProbableTypo) { 13898 Diag(NBody->getSemiLoc(), DiagID); 13899 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13900 } 13901 } 13902 13903 //===--- CHECK: Warn on self move with std::move. -------------------------===// 13904 13905 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, 13906 SourceLocation OpLoc) { 13907 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) 13908 return; 13909 13910 if (inTemplateInstantiation()) 13911 return; 13912 13913 // Strip parens and casts away. 13914 LHSExpr = LHSExpr->IgnoreParenImpCasts(); 13915 RHSExpr = RHSExpr->IgnoreParenImpCasts(); 13916 13917 // Check for a call to std::move or for a static_cast<T&&>(..) to an xvalue 13918 // which we can treat as an inlined std::move 13919 if (const auto *CE = dyn_cast<CallExpr>(RHSExpr); 13920 CE && CE->getNumArgs() == 1 && CE->isCallToStdMove()) 13921 RHSExpr = CE->getArg(0); 13922 else if (const auto *CXXSCE = dyn_cast<CXXStaticCastExpr>(RHSExpr); 13923 CXXSCE && CXXSCE->isXValue()) 13924 RHSExpr = CXXSCE->getSubExpr(); 13925 else 13926 return; 13927 13928 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); 13929 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); 13930 13931 // Two DeclRefExpr's, check that the decls are the same. 13932 if (LHSDeclRef && RHSDeclRef) { 13933 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13934 return; 13935 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13936 RHSDeclRef->getDecl()->getCanonicalDecl()) 13937 return; 13938 13939 auto D = Diag(OpLoc, diag::warn_self_move) 13940 << LHSExpr->getType() << LHSExpr->getSourceRange() 13941 << RHSExpr->getSourceRange(); 13942 if (const FieldDecl *F = 13943 getSelfAssignmentClassMemberCandidate(RHSDeclRef->getDecl())) 13944 D << 1 << F 13945 << FixItHint::CreateInsertion(LHSDeclRef->getBeginLoc(), "this->"); 13946 else 13947 D << 0; 13948 return; 13949 } 13950 13951 // Member variables require a different approach to check for self moves. 13952 // MemberExpr's are the same if every nested MemberExpr refers to the same 13953 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or 13954 // the base Expr's are CXXThisExpr's. 13955 const Expr *LHSBase = LHSExpr; 13956 const Expr *RHSBase = RHSExpr; 13957 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); 13958 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); 13959 if (!LHSME || !RHSME) 13960 return; 13961 13962 while (LHSME && RHSME) { 13963 if (LHSME->getMemberDecl()->getCanonicalDecl() != 13964 RHSME->getMemberDecl()->getCanonicalDecl()) 13965 return; 13966 13967 LHSBase = LHSME->getBase(); 13968 RHSBase = RHSME->getBase(); 13969 LHSME = dyn_cast<MemberExpr>(LHSBase); 13970 RHSME = dyn_cast<MemberExpr>(RHSBase); 13971 } 13972 13973 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); 13974 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); 13975 if (LHSDeclRef && RHSDeclRef) { 13976 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13977 return; 13978 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13979 RHSDeclRef->getDecl()->getCanonicalDecl()) 13980 return; 13981 13982 Diag(OpLoc, diag::warn_self_move) 13983 << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() 13984 << RHSExpr->getSourceRange(); 13985 return; 13986 } 13987 13988 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) 13989 Diag(OpLoc, diag::warn_self_move) 13990 << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() 13991 << RHSExpr->getSourceRange(); 13992 } 13993 13994 //===--- Layout compatibility ----------------------------------------------// 13995 13996 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2); 13997 13998 /// Check if two enumeration types are layout-compatible. 13999 static bool isLayoutCompatible(const ASTContext &C, const EnumDecl *ED1, 14000 const EnumDecl *ED2) { 14001 // C++11 [dcl.enum] p8: 14002 // Two enumeration types are layout-compatible if they have the same 14003 // underlying type. 14004 return ED1->isComplete() && ED2->isComplete() && 14005 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); 14006 } 14007 14008 /// Check if two fields are layout-compatible. 14009 /// Can be used on union members, which are exempt from alignment requirement 14010 /// of common initial sequence. 14011 static bool isLayoutCompatible(const ASTContext &C, const FieldDecl *Field1, 14012 const FieldDecl *Field2, 14013 bool AreUnionMembers = false) { 14014 [[maybe_unused]] const Type *Field1Parent = 14015 Field1->getParent()->getTypeForDecl(); 14016 [[maybe_unused]] const Type *Field2Parent = 14017 Field2->getParent()->getTypeForDecl(); 14018 assert(((Field1Parent->isStructureOrClassType() && 14019 Field2Parent->isStructureOrClassType()) || 14020 (Field1Parent->isUnionType() && Field2Parent->isUnionType())) && 14021 "Can't evaluate layout compatibility between a struct field and a " 14022 "union field."); 14023 assert(((!AreUnionMembers && Field1Parent->isStructureOrClassType()) || 14024 (AreUnionMembers && Field1Parent->isUnionType())) && 14025 "AreUnionMembers should be 'true' for union fields (only)."); 14026 14027 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) 14028 return false; 14029 14030 if (Field1->isBitField() != Field2->isBitField()) 14031 return false; 14032 14033 if (Field1->isBitField()) { 14034 // Make sure that the bit-fields are the same length. 14035 unsigned Bits1 = Field1->getBitWidthValue(C); 14036 unsigned Bits2 = Field2->getBitWidthValue(C); 14037 14038 if (Bits1 != Bits2) 14039 return false; 14040 } 14041 14042 if (Field1->hasAttr<clang::NoUniqueAddressAttr>() || 14043 Field2->hasAttr<clang::NoUniqueAddressAttr>()) 14044 return false; 14045 14046 if (!AreUnionMembers && 14047 Field1->getMaxAlignment() != Field2->getMaxAlignment()) 14048 return false; 14049 14050 return true; 14051 } 14052 14053 /// Check if two standard-layout structs are layout-compatible. 14054 /// (C++11 [class.mem] p17) 14055 static bool isLayoutCompatibleStruct(const ASTContext &C, const RecordDecl *RD1, 14056 const RecordDecl *RD2) { 14057 // Get to the class where the fields are declared 14058 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) 14059 RD1 = D1CXX->getStandardLayoutBaseWithFields(); 14060 14061 if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) 14062 RD2 = D2CXX->getStandardLayoutBaseWithFields(); 14063 14064 // Check the fields. 14065 return llvm::equal(RD1->fields(), RD2->fields(), 14066 [&C](const FieldDecl *F1, const FieldDecl *F2) -> bool { 14067 return isLayoutCompatible(C, F1, F2); 14068 }); 14069 } 14070 14071 /// Check if two standard-layout unions are layout-compatible. 14072 /// (C++11 [class.mem] p18) 14073 static bool isLayoutCompatibleUnion(const ASTContext &C, const RecordDecl *RD1, 14074 const RecordDecl *RD2) { 14075 llvm::SmallPtrSet<const FieldDecl *, 8> UnmatchedFields; 14076 for (auto *Field2 : RD2->fields()) 14077 UnmatchedFields.insert(Field2); 14078 14079 for (auto *Field1 : RD1->fields()) { 14080 auto I = UnmatchedFields.begin(); 14081 auto E = UnmatchedFields.end(); 14082 14083 for ( ; I != E; ++I) { 14084 if (isLayoutCompatible(C, Field1, *I, /*IsUnionMember=*/true)) { 14085 bool Result = UnmatchedFields.erase(*I); 14086 (void) Result; 14087 assert(Result); 14088 break; 14089 } 14090 } 14091 if (I == E) 14092 return false; 14093 } 14094 14095 return UnmatchedFields.empty(); 14096 } 14097 14098 static bool isLayoutCompatible(const ASTContext &C, const RecordDecl *RD1, 14099 const RecordDecl *RD2) { 14100 if (RD1->isUnion() != RD2->isUnion()) 14101 return false; 14102 14103 if (RD1->isUnion()) 14104 return isLayoutCompatibleUnion(C, RD1, RD2); 14105 else 14106 return isLayoutCompatibleStruct(C, RD1, RD2); 14107 } 14108 14109 /// Check if two types are layout-compatible in C++11 sense. 14110 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2) { 14111 if (T1.isNull() || T2.isNull()) 14112 return false; 14113 14114 // C++20 [basic.types] p11: 14115 // Two types cv1 T1 and cv2 T2 are layout-compatible types 14116 // if T1 and T2 are the same type, layout-compatible enumerations (9.7.1), 14117 // or layout-compatible standard-layout class types (11.4). 14118 T1 = T1.getCanonicalType().getUnqualifiedType(); 14119 T2 = T2.getCanonicalType().getUnqualifiedType(); 14120 14121 if (C.hasSameType(T1, T2)) 14122 return true; 14123 14124 const Type::TypeClass TC1 = T1->getTypeClass(); 14125 const Type::TypeClass TC2 = T2->getTypeClass(); 14126 14127 if (TC1 != TC2) 14128 return false; 14129 14130 if (TC1 == Type::Enum) { 14131 return isLayoutCompatible(C, 14132 cast<EnumType>(T1)->getDecl(), 14133 cast<EnumType>(T2)->getDecl()); 14134 } else if (TC1 == Type::Record) { 14135 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) 14136 return false; 14137 14138 return isLayoutCompatible(C, 14139 cast<RecordType>(T1)->getDecl(), 14140 cast<RecordType>(T2)->getDecl()); 14141 } 14142 14143 return false; 14144 } 14145 14146 bool Sema::IsLayoutCompatible(QualType T1, QualType T2) const { 14147 return isLayoutCompatible(getASTContext(), T1, T2); 14148 } 14149 14150 //===-------------- Pointer interconvertibility ----------------------------// 14151 14152 bool Sema::IsPointerInterconvertibleBaseOf(const TypeSourceInfo *Base, 14153 const TypeSourceInfo *Derived) { 14154 QualType BaseT = Base->getType()->getCanonicalTypeUnqualified(); 14155 QualType DerivedT = Derived->getType()->getCanonicalTypeUnqualified(); 14156 14157 if (BaseT->isStructureOrClassType() && DerivedT->isStructureOrClassType() && 14158 getASTContext().hasSameType(BaseT, DerivedT)) 14159 return true; 14160 14161 if (!IsDerivedFrom(Derived->getTypeLoc().getBeginLoc(), DerivedT, BaseT)) 14162 return false; 14163 14164 // Per [basic.compound]/4.3, containing object has to be standard-layout. 14165 if (DerivedT->getAsCXXRecordDecl()->isStandardLayout()) 14166 return true; 14167 14168 return false; 14169 } 14170 14171 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// 14172 14173 /// Given a type tag expression find the type tag itself. 14174 /// 14175 /// \param TypeExpr Type tag expression, as it appears in user's code. 14176 /// 14177 /// \param VD Declaration of an identifier that appears in a type tag. 14178 /// 14179 /// \param MagicValue Type tag magic value. 14180 /// 14181 /// \param isConstantEvaluated whether the evalaution should be performed in 14182 14183 /// constant context. 14184 static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, 14185 const ValueDecl **VD, uint64_t *MagicValue, 14186 bool isConstantEvaluated) { 14187 while(true) { 14188 if (!TypeExpr) 14189 return false; 14190 14191 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); 14192 14193 switch (TypeExpr->getStmtClass()) { 14194 case Stmt::UnaryOperatorClass: { 14195 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); 14196 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { 14197 TypeExpr = UO->getSubExpr(); 14198 continue; 14199 } 14200 return false; 14201 } 14202 14203 case Stmt::DeclRefExprClass: { 14204 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); 14205 *VD = DRE->getDecl(); 14206 return true; 14207 } 14208 14209 case Stmt::IntegerLiteralClass: { 14210 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); 14211 llvm::APInt MagicValueAPInt = IL->getValue(); 14212 if (MagicValueAPInt.getActiveBits() <= 64) { 14213 *MagicValue = MagicValueAPInt.getZExtValue(); 14214 return true; 14215 } else 14216 return false; 14217 } 14218 14219 case Stmt::BinaryConditionalOperatorClass: 14220 case Stmt::ConditionalOperatorClass: { 14221 const AbstractConditionalOperator *ACO = 14222 cast<AbstractConditionalOperator>(TypeExpr); 14223 bool Result; 14224 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx, 14225 isConstantEvaluated)) { 14226 if (Result) 14227 TypeExpr = ACO->getTrueExpr(); 14228 else 14229 TypeExpr = ACO->getFalseExpr(); 14230 continue; 14231 } 14232 return false; 14233 } 14234 14235 case Stmt::BinaryOperatorClass: { 14236 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); 14237 if (BO->getOpcode() == BO_Comma) { 14238 TypeExpr = BO->getRHS(); 14239 continue; 14240 } 14241 return false; 14242 } 14243 14244 default: 14245 return false; 14246 } 14247 } 14248 } 14249 14250 /// Retrieve the C type corresponding to type tag TypeExpr. 14251 /// 14252 /// \param TypeExpr Expression that specifies a type tag. 14253 /// 14254 /// \param MagicValues Registered magic values. 14255 /// 14256 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong 14257 /// kind. 14258 /// 14259 /// \param TypeInfo Information about the corresponding C type. 14260 /// 14261 /// \param isConstantEvaluated whether the evalaution should be performed in 14262 /// constant context. 14263 /// 14264 /// \returns true if the corresponding C type was found. 14265 static bool GetMatchingCType( 14266 const IdentifierInfo *ArgumentKind, const Expr *TypeExpr, 14267 const ASTContext &Ctx, 14268 const llvm::DenseMap<Sema::TypeTagMagicValue, Sema::TypeTagData> 14269 *MagicValues, 14270 bool &FoundWrongKind, Sema::TypeTagData &TypeInfo, 14271 bool isConstantEvaluated) { 14272 FoundWrongKind = false; 14273 14274 // Variable declaration that has type_tag_for_datatype attribute. 14275 const ValueDecl *VD = nullptr; 14276 14277 uint64_t MagicValue; 14278 14279 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue, isConstantEvaluated)) 14280 return false; 14281 14282 if (VD) { 14283 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { 14284 if (I->getArgumentKind() != ArgumentKind) { 14285 FoundWrongKind = true; 14286 return false; 14287 } 14288 TypeInfo.Type = I->getMatchingCType(); 14289 TypeInfo.LayoutCompatible = I->getLayoutCompatible(); 14290 TypeInfo.MustBeNull = I->getMustBeNull(); 14291 return true; 14292 } 14293 return false; 14294 } 14295 14296 if (!MagicValues) 14297 return false; 14298 14299 llvm::DenseMap<Sema::TypeTagMagicValue, 14300 Sema::TypeTagData>::const_iterator I = 14301 MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); 14302 if (I == MagicValues->end()) 14303 return false; 14304 14305 TypeInfo = I->second; 14306 return true; 14307 } 14308 14309 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, 14310 uint64_t MagicValue, QualType Type, 14311 bool LayoutCompatible, 14312 bool MustBeNull) { 14313 if (!TypeTagForDatatypeMagicValues) 14314 TypeTagForDatatypeMagicValues.reset( 14315 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); 14316 14317 TypeTagMagicValue Magic(ArgumentKind, MagicValue); 14318 (*TypeTagForDatatypeMagicValues)[Magic] = 14319 TypeTagData(Type, LayoutCompatible, MustBeNull); 14320 } 14321 14322 static bool IsSameCharType(QualType T1, QualType T2) { 14323 const BuiltinType *BT1 = T1->getAs<BuiltinType>(); 14324 if (!BT1) 14325 return false; 14326 14327 const BuiltinType *BT2 = T2->getAs<BuiltinType>(); 14328 if (!BT2) 14329 return false; 14330 14331 BuiltinType::Kind T1Kind = BT1->getKind(); 14332 BuiltinType::Kind T2Kind = BT2->getKind(); 14333 14334 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || 14335 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || 14336 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || 14337 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); 14338 } 14339 14340 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, 14341 const ArrayRef<const Expr *> ExprArgs, 14342 SourceLocation CallSiteLoc) { 14343 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); 14344 bool IsPointerAttr = Attr->getIsPointer(); 14345 14346 // Retrieve the argument representing the 'type_tag'. 14347 unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex(); 14348 if (TypeTagIdxAST >= ExprArgs.size()) { 14349 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 14350 << 0 << Attr->getTypeTagIdx().getSourceIndex(); 14351 return; 14352 } 14353 const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST]; 14354 bool FoundWrongKind; 14355 TypeTagData TypeInfo; 14356 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, 14357 TypeTagForDatatypeMagicValues.get(), FoundWrongKind, 14358 TypeInfo, isConstantEvaluatedContext())) { 14359 if (FoundWrongKind) 14360 Diag(TypeTagExpr->getExprLoc(), 14361 diag::warn_type_tag_for_datatype_wrong_kind) 14362 << TypeTagExpr->getSourceRange(); 14363 return; 14364 } 14365 14366 // Retrieve the argument representing the 'arg_idx'. 14367 unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex(); 14368 if (ArgumentIdxAST >= ExprArgs.size()) { 14369 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 14370 << 1 << Attr->getArgumentIdx().getSourceIndex(); 14371 return; 14372 } 14373 const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST]; 14374 if (IsPointerAttr) { 14375 // Skip implicit cast of pointer to `void *' (as a function argument). 14376 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) 14377 if (ICE->getType()->isVoidPointerType() && 14378 ICE->getCastKind() == CK_BitCast) 14379 ArgumentExpr = ICE->getSubExpr(); 14380 } 14381 QualType ArgumentType = ArgumentExpr->getType(); 14382 14383 // Passing a `void*' pointer shouldn't trigger a warning. 14384 if (IsPointerAttr && ArgumentType->isVoidPointerType()) 14385 return; 14386 14387 if (TypeInfo.MustBeNull) { 14388 // Type tag with matching void type requires a null pointer. 14389 if (!ArgumentExpr->isNullPointerConstant(Context, 14390 Expr::NPC_ValueDependentIsNotNull)) { 14391 Diag(ArgumentExpr->getExprLoc(), 14392 diag::warn_type_safety_null_pointer_required) 14393 << ArgumentKind->getName() 14394 << ArgumentExpr->getSourceRange() 14395 << TypeTagExpr->getSourceRange(); 14396 } 14397 return; 14398 } 14399 14400 QualType RequiredType = TypeInfo.Type; 14401 if (IsPointerAttr) 14402 RequiredType = Context.getPointerType(RequiredType); 14403 14404 bool mismatch = false; 14405 if (!TypeInfo.LayoutCompatible) { 14406 mismatch = !Context.hasSameType(ArgumentType, RequiredType); 14407 14408 // C++11 [basic.fundamental] p1: 14409 // Plain char, signed char, and unsigned char are three distinct types. 14410 // 14411 // But we treat plain `char' as equivalent to `signed char' or `unsigned 14412 // char' depending on the current char signedness mode. 14413 if (mismatch) 14414 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), 14415 RequiredType->getPointeeType())) || 14416 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) 14417 mismatch = false; 14418 } else 14419 if (IsPointerAttr) 14420 mismatch = !isLayoutCompatible(Context, 14421 ArgumentType->getPointeeType(), 14422 RequiredType->getPointeeType()); 14423 else 14424 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); 14425 14426 if (mismatch) 14427 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) 14428 << ArgumentType << ArgumentKind 14429 << TypeInfo.LayoutCompatible << RequiredType 14430 << ArgumentExpr->getSourceRange() 14431 << TypeTagExpr->getSourceRange(); 14432 } 14433 14434 void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD, 14435 CharUnits Alignment) { 14436 MisalignedMembers.emplace_back(E, RD, MD, Alignment); 14437 } 14438 14439 void Sema::DiagnoseMisalignedMembers() { 14440 for (MisalignedMember &m : MisalignedMembers) { 14441 const NamedDecl *ND = m.RD; 14442 if (ND->getName().empty()) { 14443 if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl()) 14444 ND = TD; 14445 } 14446 Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member) 14447 << m.MD << ND << m.E->getSourceRange(); 14448 } 14449 MisalignedMembers.clear(); 14450 } 14451 14452 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) { 14453 E = E->IgnoreParens(); 14454 if (!T->isPointerType() && !T->isIntegerType() && !T->isDependentType()) 14455 return; 14456 if (isa<UnaryOperator>(E) && 14457 cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) { 14458 auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 14459 if (isa<MemberExpr>(Op)) { 14460 auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op)); 14461 if (MA != MisalignedMembers.end() && 14462 (T->isDependentType() || T->isIntegerType() || 14463 (T->isPointerType() && (T->getPointeeType()->isIncompleteType() || 14464 Context.getTypeAlignInChars( 14465 T->getPointeeType()) <= MA->Alignment)))) 14466 MisalignedMembers.erase(MA); 14467 } 14468 } 14469 } 14470 14471 void Sema::RefersToMemberWithReducedAlignment( 14472 Expr *E, 14473 llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)> 14474 Action) { 14475 const auto *ME = dyn_cast<MemberExpr>(E); 14476 if (!ME) 14477 return; 14478 14479 // No need to check expressions with an __unaligned-qualified type. 14480 if (E->getType().getQualifiers().hasUnaligned()) 14481 return; 14482 14483 // For a chain of MemberExpr like "a.b.c.d" this list 14484 // will keep FieldDecl's like [d, c, b]. 14485 SmallVector<FieldDecl *, 4> ReverseMemberChain; 14486 const MemberExpr *TopME = nullptr; 14487 bool AnyIsPacked = false; 14488 do { 14489 QualType BaseType = ME->getBase()->getType(); 14490 if (BaseType->isDependentType()) 14491 return; 14492 if (ME->isArrow()) 14493 BaseType = BaseType->getPointeeType(); 14494 RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl(); 14495 if (RD->isInvalidDecl()) 14496 return; 14497 14498 ValueDecl *MD = ME->getMemberDecl(); 14499 auto *FD = dyn_cast<FieldDecl>(MD); 14500 // We do not care about non-data members. 14501 if (!FD || FD->isInvalidDecl()) 14502 return; 14503 14504 AnyIsPacked = 14505 AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>()); 14506 ReverseMemberChain.push_back(FD); 14507 14508 TopME = ME; 14509 ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens()); 14510 } while (ME); 14511 assert(TopME && "We did not compute a topmost MemberExpr!"); 14512 14513 // Not the scope of this diagnostic. 14514 if (!AnyIsPacked) 14515 return; 14516 14517 const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts(); 14518 const auto *DRE = dyn_cast<DeclRefExpr>(TopBase); 14519 // TODO: The innermost base of the member expression may be too complicated. 14520 // For now, just disregard these cases. This is left for future 14521 // improvement. 14522 if (!DRE && !isa<CXXThisExpr>(TopBase)) 14523 return; 14524 14525 // Alignment expected by the whole expression. 14526 CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType()); 14527 14528 // No need to do anything else with this case. 14529 if (ExpectedAlignment.isOne()) 14530 return; 14531 14532 // Synthesize offset of the whole access. 14533 CharUnits Offset; 14534 for (const FieldDecl *FD : llvm::reverse(ReverseMemberChain)) 14535 Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(FD)); 14536 14537 // Compute the CompleteObjectAlignment as the alignment of the whole chain. 14538 CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars( 14539 ReverseMemberChain.back()->getParent()->getTypeForDecl()); 14540 14541 // The base expression of the innermost MemberExpr may give 14542 // stronger guarantees than the class containing the member. 14543 if (DRE && !TopME->isArrow()) { 14544 const ValueDecl *VD = DRE->getDecl(); 14545 if (!VD->getType()->isReferenceType()) 14546 CompleteObjectAlignment = 14547 std::max(CompleteObjectAlignment, Context.getDeclAlign(VD)); 14548 } 14549 14550 // Check if the synthesized offset fulfills the alignment. 14551 if (Offset % ExpectedAlignment != 0 || 14552 // It may fulfill the offset it but the effective alignment may still be 14553 // lower than the expected expression alignment. 14554 CompleteObjectAlignment < ExpectedAlignment) { 14555 // If this happens, we want to determine a sensible culprit of this. 14556 // Intuitively, watching the chain of member expressions from right to 14557 // left, we start with the required alignment (as required by the field 14558 // type) but some packed attribute in that chain has reduced the alignment. 14559 // It may happen that another packed structure increases it again. But if 14560 // we are here such increase has not been enough. So pointing the first 14561 // FieldDecl that either is packed or else its RecordDecl is, 14562 // seems reasonable. 14563 FieldDecl *FD = nullptr; 14564 CharUnits Alignment; 14565 for (FieldDecl *FDI : ReverseMemberChain) { 14566 if (FDI->hasAttr<PackedAttr>() || 14567 FDI->getParent()->hasAttr<PackedAttr>()) { 14568 FD = FDI; 14569 Alignment = std::min( 14570 Context.getTypeAlignInChars(FD->getType()), 14571 Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl())); 14572 break; 14573 } 14574 } 14575 assert(FD && "We did not find a packed FieldDecl!"); 14576 Action(E, FD->getParent(), FD, Alignment); 14577 } 14578 } 14579 14580 void Sema::CheckAddressOfPackedMember(Expr *rhs) { 14581 using namespace std::placeholders; 14582 14583 RefersToMemberWithReducedAlignment( 14584 rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1, 14585 _2, _3, _4)); 14586 } 14587 14588 bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) { 14589 if (checkArgCount(TheCall, 1)) 14590 return true; 14591 14592 ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); 14593 if (A.isInvalid()) 14594 return true; 14595 14596 TheCall->setArg(0, A.get()); 14597 QualType TyA = A.get()->getType(); 14598 14599 if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14600 return true; 14601 14602 TheCall->setType(TyA); 14603 return false; 14604 } 14605 14606 bool Sema::BuiltinElementwiseMath(CallExpr *TheCall, bool FPOnly) { 14607 QualType Res; 14608 if (BuiltinVectorMath(TheCall, Res, FPOnly)) 14609 return true; 14610 TheCall->setType(Res); 14611 return false; 14612 } 14613 14614 bool Sema::BuiltinVectorToScalarMath(CallExpr *TheCall) { 14615 QualType Res; 14616 if (BuiltinVectorMath(TheCall, Res)) 14617 return true; 14618 14619 if (auto *VecTy0 = Res->getAs<VectorType>()) 14620 TheCall->setType(VecTy0->getElementType()); 14621 else 14622 TheCall->setType(Res); 14623 14624 return false; 14625 } 14626 14627 bool Sema::BuiltinVectorMath(CallExpr *TheCall, QualType &Res, bool FPOnly) { 14628 if (checkArgCount(TheCall, 2)) 14629 return true; 14630 14631 ExprResult A = TheCall->getArg(0); 14632 ExprResult B = TheCall->getArg(1); 14633 // Do standard promotions between the two arguments, returning their common 14634 // type. 14635 Res = UsualArithmeticConversions(A, B, TheCall->getExprLoc(), ACK_Comparison); 14636 if (A.isInvalid() || B.isInvalid()) 14637 return true; 14638 14639 QualType TyA = A.get()->getType(); 14640 QualType TyB = B.get()->getType(); 14641 14642 if (Res.isNull() || TyA.getCanonicalType() != TyB.getCanonicalType()) 14643 return Diag(A.get()->getBeginLoc(), 14644 diag::err_typecheck_call_different_arg_types) 14645 << TyA << TyB; 14646 14647 if (FPOnly) { 14648 if (checkFPMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14649 return true; 14650 } else { 14651 if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14652 return true; 14653 } 14654 14655 TheCall->setArg(0, A.get()); 14656 TheCall->setArg(1, B.get()); 14657 return false; 14658 } 14659 14660 bool Sema::BuiltinElementwiseTernaryMath(CallExpr *TheCall, 14661 bool CheckForFloatArgs) { 14662 if (checkArgCount(TheCall, 3)) 14663 return true; 14664 14665 Expr *Args[3]; 14666 for (int I = 0; I < 3; ++I) { 14667 ExprResult Converted = UsualUnaryConversions(TheCall->getArg(I)); 14668 if (Converted.isInvalid()) 14669 return true; 14670 Args[I] = Converted.get(); 14671 } 14672 14673 if (CheckForFloatArgs) { 14674 int ArgOrdinal = 1; 14675 for (Expr *Arg : Args) { 14676 if (checkFPMathBuiltinElementType(*this, Arg->getBeginLoc(), 14677 Arg->getType(), ArgOrdinal++)) 14678 return true; 14679 } 14680 } else { 14681 int ArgOrdinal = 1; 14682 for (Expr *Arg : Args) { 14683 if (checkMathBuiltinElementType(*this, Arg->getBeginLoc(), Arg->getType(), 14684 ArgOrdinal++)) 14685 return true; 14686 } 14687 } 14688 14689 for (int I = 1; I < 3; ++I) { 14690 if (Args[0]->getType().getCanonicalType() != 14691 Args[I]->getType().getCanonicalType()) { 14692 return Diag(Args[0]->getBeginLoc(), 14693 diag::err_typecheck_call_different_arg_types) 14694 << Args[0]->getType() << Args[I]->getType(); 14695 } 14696 14697 TheCall->setArg(I, Args[I]); 14698 } 14699 14700 TheCall->setType(Args[0]->getType()); 14701 return false; 14702 } 14703 14704 bool Sema::PrepareBuiltinReduceMathOneArgCall(CallExpr *TheCall) { 14705 if (checkArgCount(TheCall, 1)) 14706 return true; 14707 14708 ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); 14709 if (A.isInvalid()) 14710 return true; 14711 14712 TheCall->setArg(0, A.get()); 14713 return false; 14714 } 14715 14716 bool Sema::BuiltinNonDeterministicValue(CallExpr *TheCall) { 14717 if (checkArgCount(TheCall, 1)) 14718 return true; 14719 14720 ExprResult Arg = TheCall->getArg(0); 14721 QualType TyArg = Arg.get()->getType(); 14722 14723 if (!TyArg->isBuiltinType() && !TyArg->isVectorType()) 14724 return Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14725 << 1 << /*vector, integer or floating point ty*/ 0 << TyArg; 14726 14727 TheCall->setType(TyArg); 14728 return false; 14729 } 14730 14731 ExprResult Sema::BuiltinMatrixTranspose(CallExpr *TheCall, 14732 ExprResult CallResult) { 14733 if (checkArgCount(TheCall, 1)) 14734 return ExprError(); 14735 14736 ExprResult MatrixArg = DefaultLvalueConversion(TheCall->getArg(0)); 14737 if (MatrixArg.isInvalid()) 14738 return MatrixArg; 14739 Expr *Matrix = MatrixArg.get(); 14740 14741 auto *MType = Matrix->getType()->getAs<ConstantMatrixType>(); 14742 if (!MType) { 14743 Diag(Matrix->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14744 << 1 << /* matrix ty*/ 1 << Matrix->getType(); 14745 return ExprError(); 14746 } 14747 14748 // Create returned matrix type by swapping rows and columns of the argument 14749 // matrix type. 14750 QualType ResultType = Context.getConstantMatrixType( 14751 MType->getElementType(), MType->getNumColumns(), MType->getNumRows()); 14752 14753 // Change the return type to the type of the returned matrix. 14754 TheCall->setType(ResultType); 14755 14756 // Update call argument to use the possibly converted matrix argument. 14757 TheCall->setArg(0, Matrix); 14758 return CallResult; 14759 } 14760 14761 // Get and verify the matrix dimensions. 14762 static std::optional<unsigned> 14763 getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) { 14764 SourceLocation ErrorPos; 14765 std::optional<llvm::APSInt> Value = 14766 Expr->getIntegerConstantExpr(S.Context, &ErrorPos); 14767 if (!Value) { 14768 S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_scalar_unsigned_arg) 14769 << Name; 14770 return {}; 14771 } 14772 uint64_t Dim = Value->getZExtValue(); 14773 if (!ConstantMatrixType::isDimensionValid(Dim)) { 14774 S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension) 14775 << Name << ConstantMatrixType::getMaxElementsPerDimension(); 14776 return {}; 14777 } 14778 return Dim; 14779 } 14780 14781 ExprResult Sema::BuiltinMatrixColumnMajorLoad(CallExpr *TheCall, 14782 ExprResult CallResult) { 14783 if (!getLangOpts().MatrixTypes) { 14784 Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled); 14785 return ExprError(); 14786 } 14787 14788 if (checkArgCount(TheCall, 4)) 14789 return ExprError(); 14790 14791 unsigned PtrArgIdx = 0; 14792 Expr *PtrExpr = TheCall->getArg(PtrArgIdx); 14793 Expr *RowsExpr = TheCall->getArg(1); 14794 Expr *ColumnsExpr = TheCall->getArg(2); 14795 Expr *StrideExpr = TheCall->getArg(3); 14796 14797 bool ArgError = false; 14798 14799 // Check pointer argument. 14800 { 14801 ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); 14802 if (PtrConv.isInvalid()) 14803 return PtrConv; 14804 PtrExpr = PtrConv.get(); 14805 TheCall->setArg(0, PtrExpr); 14806 if (PtrExpr->isTypeDependent()) { 14807 TheCall->setType(Context.DependentTy); 14808 return TheCall; 14809 } 14810 } 14811 14812 auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); 14813 QualType ElementTy; 14814 if (!PtrTy) { 14815 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14816 << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); 14817 ArgError = true; 14818 } else { 14819 ElementTy = PtrTy->getPointeeType().getUnqualifiedType(); 14820 14821 if (!ConstantMatrixType::isValidElementType(ElementTy)) { 14822 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14823 << PtrArgIdx + 1 << /* pointer to element ty*/ 2 14824 << PtrExpr->getType(); 14825 ArgError = true; 14826 } 14827 } 14828 14829 // Apply default Lvalue conversions and convert the expression to size_t. 14830 auto ApplyArgumentConversions = [this](Expr *E) { 14831 ExprResult Conv = DefaultLvalueConversion(E); 14832 if (Conv.isInvalid()) 14833 return Conv; 14834 14835 return tryConvertExprToType(Conv.get(), Context.getSizeType()); 14836 }; 14837 14838 // Apply conversion to row and column expressions. 14839 ExprResult RowsConv = ApplyArgumentConversions(RowsExpr); 14840 if (!RowsConv.isInvalid()) { 14841 RowsExpr = RowsConv.get(); 14842 TheCall->setArg(1, RowsExpr); 14843 } else 14844 RowsExpr = nullptr; 14845 14846 ExprResult ColumnsConv = ApplyArgumentConversions(ColumnsExpr); 14847 if (!ColumnsConv.isInvalid()) { 14848 ColumnsExpr = ColumnsConv.get(); 14849 TheCall->setArg(2, ColumnsExpr); 14850 } else 14851 ColumnsExpr = nullptr; 14852 14853 // If any part of the result matrix type is still pending, just use 14854 // Context.DependentTy, until all parts are resolved. 14855 if ((RowsExpr && RowsExpr->isTypeDependent()) || 14856 (ColumnsExpr && ColumnsExpr->isTypeDependent())) { 14857 TheCall->setType(Context.DependentTy); 14858 return CallResult; 14859 } 14860 14861 // Check row and column dimensions. 14862 std::optional<unsigned> MaybeRows; 14863 if (RowsExpr) 14864 MaybeRows = getAndVerifyMatrixDimension(RowsExpr, "row", *this); 14865 14866 std::optional<unsigned> MaybeColumns; 14867 if (ColumnsExpr) 14868 MaybeColumns = getAndVerifyMatrixDimension(ColumnsExpr, "column", *this); 14869 14870 // Check stride argument. 14871 ExprResult StrideConv = ApplyArgumentConversions(StrideExpr); 14872 if (StrideConv.isInvalid()) 14873 return ExprError(); 14874 StrideExpr = StrideConv.get(); 14875 TheCall->setArg(3, StrideExpr); 14876 14877 if (MaybeRows) { 14878 if (std::optional<llvm::APSInt> Value = 14879 StrideExpr->getIntegerConstantExpr(Context)) { 14880 uint64_t Stride = Value->getZExtValue(); 14881 if (Stride < *MaybeRows) { 14882 Diag(StrideExpr->getBeginLoc(), 14883 diag::err_builtin_matrix_stride_too_small); 14884 ArgError = true; 14885 } 14886 } 14887 } 14888 14889 if (ArgError || !MaybeRows || !MaybeColumns) 14890 return ExprError(); 14891 14892 TheCall->setType( 14893 Context.getConstantMatrixType(ElementTy, *MaybeRows, *MaybeColumns)); 14894 return CallResult; 14895 } 14896 14897 ExprResult Sema::BuiltinMatrixColumnMajorStore(CallExpr *TheCall, 14898 ExprResult CallResult) { 14899 if (checkArgCount(TheCall, 3)) 14900 return ExprError(); 14901 14902 unsigned PtrArgIdx = 1; 14903 Expr *MatrixExpr = TheCall->getArg(0); 14904 Expr *PtrExpr = TheCall->getArg(PtrArgIdx); 14905 Expr *StrideExpr = TheCall->getArg(2); 14906 14907 bool ArgError = false; 14908 14909 { 14910 ExprResult MatrixConv = DefaultLvalueConversion(MatrixExpr); 14911 if (MatrixConv.isInvalid()) 14912 return MatrixConv; 14913 MatrixExpr = MatrixConv.get(); 14914 TheCall->setArg(0, MatrixExpr); 14915 } 14916 if (MatrixExpr->isTypeDependent()) { 14917 TheCall->setType(Context.DependentTy); 14918 return TheCall; 14919 } 14920 14921 auto *MatrixTy = MatrixExpr->getType()->getAs<ConstantMatrixType>(); 14922 if (!MatrixTy) { 14923 Diag(MatrixExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14924 << 1 << /*matrix ty */ 1 << MatrixExpr->getType(); 14925 ArgError = true; 14926 } 14927 14928 { 14929 ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); 14930 if (PtrConv.isInvalid()) 14931 return PtrConv; 14932 PtrExpr = PtrConv.get(); 14933 TheCall->setArg(1, PtrExpr); 14934 if (PtrExpr->isTypeDependent()) { 14935 TheCall->setType(Context.DependentTy); 14936 return TheCall; 14937 } 14938 } 14939 14940 // Check pointer argument. 14941 auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); 14942 if (!PtrTy) { 14943 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14944 << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); 14945 ArgError = true; 14946 } else { 14947 QualType ElementTy = PtrTy->getPointeeType(); 14948 if (ElementTy.isConstQualified()) { 14949 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_store_to_const); 14950 ArgError = true; 14951 } 14952 ElementTy = ElementTy.getUnqualifiedType().getCanonicalType(); 14953 if (MatrixTy && 14954 !Context.hasSameType(ElementTy, MatrixTy->getElementType())) { 14955 Diag(PtrExpr->getBeginLoc(), 14956 diag::err_builtin_matrix_pointer_arg_mismatch) 14957 << ElementTy << MatrixTy->getElementType(); 14958 ArgError = true; 14959 } 14960 } 14961 14962 // Apply default Lvalue conversions and convert the stride expression to 14963 // size_t. 14964 { 14965 ExprResult StrideConv = DefaultLvalueConversion(StrideExpr); 14966 if (StrideConv.isInvalid()) 14967 return StrideConv; 14968 14969 StrideConv = tryConvertExprToType(StrideConv.get(), Context.getSizeType()); 14970 if (StrideConv.isInvalid()) 14971 return StrideConv; 14972 StrideExpr = StrideConv.get(); 14973 TheCall->setArg(2, StrideExpr); 14974 } 14975 14976 // Check stride argument. 14977 if (MatrixTy) { 14978 if (std::optional<llvm::APSInt> Value = 14979 StrideExpr->getIntegerConstantExpr(Context)) { 14980 uint64_t Stride = Value->getZExtValue(); 14981 if (Stride < MatrixTy->getNumRows()) { 14982 Diag(StrideExpr->getBeginLoc(), 14983 diag::err_builtin_matrix_stride_too_small); 14984 ArgError = true; 14985 } 14986 } 14987 } 14988 14989 if (ArgError) 14990 return ExprError(); 14991 14992 return CallResult; 14993 } 14994 14995 void Sema::CheckTCBEnforcement(const SourceLocation CallExprLoc, 14996 const NamedDecl *Callee) { 14997 // This warning does not make sense in code that has no runtime behavior. 14998 if (isUnevaluatedContext()) 14999 return; 15000 15001 const NamedDecl *Caller = getCurFunctionOrMethodDecl(); 15002 15003 if (!Caller || !Caller->hasAttr<EnforceTCBAttr>()) 15004 return; 15005 15006 // Search through the enforce_tcb and enforce_tcb_leaf attributes to find 15007 // all TCBs the callee is a part of. 15008 llvm::StringSet<> CalleeTCBs; 15009 for (const auto *A : Callee->specific_attrs<EnforceTCBAttr>()) 15010 CalleeTCBs.insert(A->getTCBName()); 15011 for (const auto *A : Callee->specific_attrs<EnforceTCBLeafAttr>()) 15012 CalleeTCBs.insert(A->getTCBName()); 15013 15014 // Go through the TCBs the caller is a part of and emit warnings if Caller 15015 // is in a TCB that the Callee is not. 15016 for (const auto *A : Caller->specific_attrs<EnforceTCBAttr>()) { 15017 StringRef CallerTCB = A->getTCBName(); 15018 if (CalleeTCBs.count(CallerTCB) == 0) { 15019 this->Diag(CallExprLoc, diag::warn_tcb_enforcement_violation) 15020 << Callee << CallerTCB; 15021 } 15022 } 15023 } 15024