1 //===- SemaChecking.cpp - Extra Semantic Checking -------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements extra semantic analysis beyond what is enforced 10 // by the C type system. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/APValue.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Attr.h" 17 #include "clang/AST/AttrIterator.h" 18 #include "clang/AST/CharUnits.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclBase.h" 21 #include "clang/AST/DeclCXX.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/DeclarationName.h" 24 #include "clang/AST/EvaluatedExprVisitor.h" 25 #include "clang/AST/Expr.h" 26 #include "clang/AST/ExprCXX.h" 27 #include "clang/AST/ExprObjC.h" 28 #include "clang/AST/ExprOpenMP.h" 29 #include "clang/AST/FormatString.h" 30 #include "clang/AST/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/CharInfo.h" 42 #include "clang/Basic/Diagnostic.h" 43 #include "clang/Basic/IdentifierTable.h" 44 #include "clang/Basic/LLVM.h" 45 #include "clang/Basic/LangOptions.h" 46 #include "clang/Basic/OpenCLOptions.h" 47 #include "clang/Basic/OperatorKinds.h" 48 #include "clang/Basic/PartialDiagnostic.h" 49 #include "clang/Basic/SourceLocation.h" 50 #include "clang/Basic/SourceManager.h" 51 #include "clang/Basic/Specifiers.h" 52 #include "clang/Basic/SyncScope.h" 53 #include "clang/Basic/TargetBuiltins.h" 54 #include "clang/Basic/TargetCXXABI.h" 55 #include "clang/Basic/TargetInfo.h" 56 #include "clang/Basic/TypeTraits.h" 57 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. 58 #include "clang/Sema/Initialization.h" 59 #include "clang/Sema/Lookup.h" 60 #include "clang/Sema/Ownership.h" 61 #include "clang/Sema/Scope.h" 62 #include "clang/Sema/ScopeInfo.h" 63 #include "clang/Sema/Sema.h" 64 #include "clang/Sema/SemaAMDGPU.h" 65 #include "clang/Sema/SemaARM.h" 66 #include "clang/Sema/SemaBPF.h" 67 #include "clang/Sema/SemaHLSL.h" 68 #include "clang/Sema/SemaHexagon.h" 69 #include "clang/Sema/SemaInternal.h" 70 #include "clang/Sema/SemaLoongArch.h" 71 #include "clang/Sema/SemaMIPS.h" 72 #include "clang/Sema/SemaNVPTX.h" 73 #include "clang/Sema/SemaObjC.h" 74 #include "clang/Sema/SemaOpenCL.h" 75 #include "clang/Sema/SemaPPC.h" 76 #include "clang/Sema/SemaRISCV.h" 77 #include "clang/Sema/SemaSystemZ.h" 78 #include "clang/Sema/SemaWasm.h" 79 #include "clang/Sema/SemaX86.h" 80 #include "llvm/ADT/APFloat.h" 81 #include "llvm/ADT/APInt.h" 82 #include "llvm/ADT/APSInt.h" 83 #include "llvm/ADT/ArrayRef.h" 84 #include "llvm/ADT/DenseMap.h" 85 #include "llvm/ADT/FoldingSet.h" 86 #include "llvm/ADT/STLExtras.h" 87 #include "llvm/ADT/SmallBitVector.h" 88 #include "llvm/ADT/SmallPtrSet.h" 89 #include "llvm/ADT/SmallString.h" 90 #include "llvm/ADT/SmallVector.h" 91 #include "llvm/ADT/StringExtras.h" 92 #include "llvm/ADT/StringRef.h" 93 #include "llvm/ADT/StringSet.h" 94 #include "llvm/ADT/StringSwitch.h" 95 #include "llvm/Support/AtomicOrdering.h" 96 #include "llvm/Support/Casting.h" 97 #include "llvm/Support/Compiler.h" 98 #include "llvm/Support/ConvertUTF.h" 99 #include "llvm/Support/ErrorHandling.h" 100 #include "llvm/Support/Format.h" 101 #include "llvm/Support/Locale.h" 102 #include "llvm/Support/MathExtras.h" 103 #include "llvm/Support/SaveAndRestore.h" 104 #include "llvm/Support/raw_ostream.h" 105 #include "llvm/TargetParser/RISCVTargetParser.h" 106 #include "llvm/TargetParser/Triple.h" 107 #include <algorithm> 108 #include <bitset> 109 #include <cassert> 110 #include <cctype> 111 #include <cstddef> 112 #include <cstdint> 113 #include <functional> 114 #include <limits> 115 #include <optional> 116 #include <string> 117 #include <tuple> 118 #include <utility> 119 120 using namespace clang; 121 using namespace sema; 122 123 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, 124 unsigned ByteNo) const { 125 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, 126 Context.getTargetInfo()); 127 } 128 129 static constexpr unsigned short combineFAPK(Sema::FormatArgumentPassingKind A, 130 Sema::FormatArgumentPassingKind B) { 131 return (A << 8) | B; 132 } 133 134 bool Sema::checkArgCountAtLeast(CallExpr *Call, unsigned MinArgCount) { 135 unsigned ArgCount = Call->getNumArgs(); 136 if (ArgCount >= MinArgCount) 137 return false; 138 139 return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_few_args) 140 << 0 /*function call*/ << MinArgCount << ArgCount 141 << /*is non object*/ 0 << Call->getSourceRange(); 142 } 143 144 bool Sema::checkArgCountAtMost(CallExpr *Call, unsigned MaxArgCount) { 145 unsigned ArgCount = Call->getNumArgs(); 146 if (ArgCount <= MaxArgCount) 147 return false; 148 return Diag(Call->getEndLoc(), diag::err_typecheck_call_too_many_args_at_most) 149 << 0 /*function call*/ << MaxArgCount << ArgCount 150 << /*is non object*/ 0 << Call->getSourceRange(); 151 } 152 153 bool Sema::checkArgCountRange(CallExpr *Call, unsigned MinArgCount, 154 unsigned MaxArgCount) { 155 return checkArgCountAtLeast(Call, MinArgCount) || 156 checkArgCountAtMost(Call, MaxArgCount); 157 } 158 159 bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) { 160 unsigned ArgCount = Call->getNumArgs(); 161 if (ArgCount == DesiredArgCount) 162 return false; 163 164 if (checkArgCountAtLeast(Call, DesiredArgCount)) 165 return true; 166 assert(ArgCount > DesiredArgCount && "should have diagnosed this"); 167 168 // Highlight all the excess arguments. 169 SourceRange Range(Call->getArg(DesiredArgCount)->getBeginLoc(), 170 Call->getArg(ArgCount - 1)->getEndLoc()); 171 172 return Diag(Range.getBegin(), diag::err_typecheck_call_too_many_args) 173 << 0 /*function call*/ << DesiredArgCount << ArgCount 174 << /*is non object*/ 0 << Call->getArg(1)->getSourceRange(); 175 } 176 177 static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) { 178 bool HasError = false; 179 180 for (unsigned I = 0; I < Call->getNumArgs(); ++I) { 181 Expr *Arg = Call->getArg(I); 182 183 if (Arg->isValueDependent()) 184 continue; 185 186 std::optional<std::string> ArgString = Arg->tryEvaluateString(S.Context); 187 int DiagMsgKind = -1; 188 // Arguments must be pointers to constant strings and cannot use '$'. 189 if (!ArgString.has_value()) 190 DiagMsgKind = 0; 191 else if (ArgString->find('$') != std::string::npos) 192 DiagMsgKind = 1; 193 194 if (DiagMsgKind >= 0) { 195 S.Diag(Arg->getBeginLoc(), diag::err_builtin_verbose_trap_arg) 196 << DiagMsgKind << Arg->getSourceRange(); 197 HasError = true; 198 } 199 } 200 201 return !HasError; 202 } 203 204 static bool convertArgumentToType(Sema &S, Expr *&Value, QualType Ty) { 205 if (Value->isTypeDependent()) 206 return false; 207 208 InitializedEntity Entity = 209 InitializedEntity::InitializeParameter(S.Context, Ty, false); 210 ExprResult Result = 211 S.PerformCopyInitialization(Entity, SourceLocation(), Value); 212 if (Result.isInvalid()) 213 return true; 214 Value = Result.get(); 215 return false; 216 } 217 218 /// Check that the first argument to __builtin_annotation is an integer 219 /// and the second argument is a non-wide string literal. 220 static bool BuiltinAnnotation(Sema &S, CallExpr *TheCall) { 221 if (S.checkArgCount(TheCall, 2)) 222 return true; 223 224 // First argument should be an integer. 225 Expr *ValArg = TheCall->getArg(0); 226 QualType Ty = ValArg->getType(); 227 if (!Ty->isIntegerType()) { 228 S.Diag(ValArg->getBeginLoc(), diag::err_builtin_annotation_first_arg) 229 << ValArg->getSourceRange(); 230 return true; 231 } 232 233 // Second argument should be a constant string. 234 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); 235 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); 236 if (!Literal || !Literal->isOrdinary()) { 237 S.Diag(StrArg->getBeginLoc(), diag::err_builtin_annotation_second_arg) 238 << StrArg->getSourceRange(); 239 return true; 240 } 241 242 TheCall->setType(Ty); 243 return false; 244 } 245 246 static bool BuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) { 247 // We need at least one argument. 248 if (TheCall->getNumArgs() < 1) { 249 S.Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 250 << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 251 << TheCall->getCallee()->getSourceRange(); 252 return true; 253 } 254 255 // All arguments should be wide string literals. 256 for (Expr *Arg : TheCall->arguments()) { 257 auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 258 if (!Literal || !Literal->isWide()) { 259 S.Diag(Arg->getBeginLoc(), diag::err_msvc_annotation_wide_str) 260 << Arg->getSourceRange(); 261 return true; 262 } 263 } 264 265 return false; 266 } 267 268 /// Check that the argument to __builtin_addressof is a glvalue, and set the 269 /// result type to the corresponding pointer type. 270 static bool BuiltinAddressof(Sema &S, CallExpr *TheCall) { 271 if (S.checkArgCount(TheCall, 1)) 272 return true; 273 274 ExprResult Arg(TheCall->getArg(0)); 275 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc()); 276 if (ResultType.isNull()) 277 return true; 278 279 TheCall->setArg(0, Arg.get()); 280 TheCall->setType(ResultType); 281 return false; 282 } 283 284 /// Check that the argument to __builtin_function_start is a function. 285 static bool BuiltinFunctionStart(Sema &S, CallExpr *TheCall) { 286 if (S.checkArgCount(TheCall, 1)) 287 return true; 288 289 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 290 if (Arg.isInvalid()) 291 return true; 292 293 TheCall->setArg(0, Arg.get()); 294 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>( 295 Arg.get()->getAsBuiltinConstantDeclRef(S.getASTContext())); 296 297 if (!FD) { 298 S.Diag(TheCall->getBeginLoc(), diag::err_function_start_invalid_type) 299 << TheCall->getSourceRange(); 300 return true; 301 } 302 303 return !S.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, 304 TheCall->getBeginLoc()); 305 } 306 307 /// Check the number of arguments and set the result type to 308 /// the argument type. 309 static bool BuiltinPreserveAI(Sema &S, CallExpr *TheCall) { 310 if (S.checkArgCount(TheCall, 1)) 311 return true; 312 313 TheCall->setType(TheCall->getArg(0)->getType()); 314 return false; 315 } 316 317 /// Check that the value argument for __builtin_is_aligned(value, alignment) and 318 /// __builtin_aligned_{up,down}(value, alignment) is an integer or a pointer 319 /// type (but not a function pointer) and that the alignment is a power-of-two. 320 static bool BuiltinAlignment(Sema &S, CallExpr *TheCall, unsigned ID) { 321 if (S.checkArgCount(TheCall, 2)) 322 return true; 323 324 clang::Expr *Source = TheCall->getArg(0); 325 bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned; 326 327 auto IsValidIntegerType = [](QualType Ty) { 328 return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType(); 329 }; 330 QualType SrcTy = Source->getType(); 331 // We should also be able to use it with arrays (but not functions!). 332 if (SrcTy->canDecayToPointerType() && SrcTy->isArrayType()) { 333 SrcTy = S.Context.getDecayedType(SrcTy); 334 } 335 if ((!SrcTy->isPointerType() && !IsValidIntegerType(SrcTy)) || 336 SrcTy->isFunctionPointerType()) { 337 // FIXME: this is not quite the right error message since we don't allow 338 // floating point types, or member pointers. 339 S.Diag(Source->getExprLoc(), diag::err_typecheck_expect_scalar_operand) 340 << SrcTy; 341 return true; 342 } 343 344 clang::Expr *AlignOp = TheCall->getArg(1); 345 if (!IsValidIntegerType(AlignOp->getType())) { 346 S.Diag(AlignOp->getExprLoc(), diag::err_typecheck_expect_int) 347 << AlignOp->getType(); 348 return true; 349 } 350 Expr::EvalResult AlignResult; 351 unsigned MaxAlignmentBits = S.Context.getIntWidth(SrcTy) - 1; 352 // We can't check validity of alignment if it is value dependent. 353 if (!AlignOp->isValueDependent() && 354 AlignOp->EvaluateAsInt(AlignResult, S.Context, 355 Expr::SE_AllowSideEffects)) { 356 llvm::APSInt AlignValue = AlignResult.Val.getInt(); 357 llvm::APSInt MaxValue( 358 llvm::APInt::getOneBitSet(MaxAlignmentBits + 1, MaxAlignmentBits)); 359 if (AlignValue < 1) { 360 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_small) << 1; 361 return true; 362 } 363 if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) { 364 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_too_big) 365 << toString(MaxValue, 10); 366 return true; 367 } 368 if (!AlignValue.isPowerOf2()) { 369 S.Diag(AlignOp->getExprLoc(), diag::err_alignment_not_power_of_two); 370 return true; 371 } 372 if (AlignValue == 1) { 373 S.Diag(AlignOp->getExprLoc(), diag::warn_alignment_builtin_useless) 374 << IsBooleanAlignBuiltin; 375 } 376 } 377 378 ExprResult SrcArg = S.PerformCopyInitialization( 379 InitializedEntity::InitializeParameter(S.Context, SrcTy, false), 380 SourceLocation(), Source); 381 if (SrcArg.isInvalid()) 382 return true; 383 TheCall->setArg(0, SrcArg.get()); 384 ExprResult AlignArg = 385 S.PerformCopyInitialization(InitializedEntity::InitializeParameter( 386 S.Context, AlignOp->getType(), false), 387 SourceLocation(), AlignOp); 388 if (AlignArg.isInvalid()) 389 return true; 390 TheCall->setArg(1, AlignArg.get()); 391 // For align_up/align_down, the return type is the same as the (potentially 392 // decayed) argument type including qualifiers. For is_aligned(), the result 393 // is always bool. 394 TheCall->setType(IsBooleanAlignBuiltin ? S.Context.BoolTy : SrcTy); 395 return false; 396 } 397 398 static bool BuiltinOverflow(Sema &S, CallExpr *TheCall, unsigned BuiltinID) { 399 if (S.checkArgCount(TheCall, 3)) 400 return true; 401 402 std::pair<unsigned, const char *> Builtins[] = { 403 { Builtin::BI__builtin_add_overflow, "ckd_add" }, 404 { Builtin::BI__builtin_sub_overflow, "ckd_sub" }, 405 { Builtin::BI__builtin_mul_overflow, "ckd_mul" }, 406 }; 407 408 bool CkdOperation = llvm::any_of(Builtins, [&](const std::pair<unsigned, 409 const char *> &P) { 410 return BuiltinID == P.first && TheCall->getExprLoc().isMacroID() && 411 Lexer::getImmediateMacroName(TheCall->getExprLoc(), 412 S.getSourceManager(), S.getLangOpts()) == P.second; 413 }); 414 415 auto ValidCkdIntType = [](QualType QT) { 416 // A valid checked integer type is an integer type other than a plain char, 417 // bool, a bit-precise type, or an enumeration type. 418 if (const auto *BT = QT.getCanonicalType()->getAs<BuiltinType>()) 419 return (BT->getKind() >= BuiltinType::Short && 420 BT->getKind() <= BuiltinType::Int128) || ( 421 BT->getKind() >= BuiltinType::UShort && 422 BT->getKind() <= BuiltinType::UInt128) || 423 BT->getKind() == BuiltinType::UChar || 424 BT->getKind() == BuiltinType::SChar; 425 return false; 426 }; 427 428 // First two arguments should be integers. 429 for (unsigned I = 0; I < 2; ++I) { 430 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(I)); 431 if (Arg.isInvalid()) return true; 432 TheCall->setArg(I, Arg.get()); 433 434 QualType Ty = Arg.get()->getType(); 435 bool IsValid = CkdOperation ? ValidCkdIntType(Ty) : Ty->isIntegerType(); 436 if (!IsValid) { 437 S.Diag(Arg.get()->getBeginLoc(), diag::err_overflow_builtin_must_be_int) 438 << CkdOperation << Ty << Arg.get()->getSourceRange(); 439 return true; 440 } 441 } 442 443 // Third argument should be a pointer to a non-const integer. 444 // IRGen correctly handles volatile, restrict, and address spaces, and 445 // the other qualifiers aren't possible. 446 { 447 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(2)); 448 if (Arg.isInvalid()) return true; 449 TheCall->setArg(2, Arg.get()); 450 451 QualType Ty = Arg.get()->getType(); 452 const auto *PtrTy = Ty->getAs<PointerType>(); 453 if (!PtrTy || 454 !PtrTy->getPointeeType()->isIntegerType() || 455 (!ValidCkdIntType(PtrTy->getPointeeType()) && CkdOperation) || 456 PtrTy->getPointeeType().isConstQualified()) { 457 S.Diag(Arg.get()->getBeginLoc(), 458 diag::err_overflow_builtin_must_be_ptr_int) 459 << CkdOperation << Ty << Arg.get()->getSourceRange(); 460 return true; 461 } 462 } 463 464 // Disallow signed bit-precise integer args larger than 128 bits to mul 465 // function until we improve backend support. 466 if (BuiltinID == Builtin::BI__builtin_mul_overflow) { 467 for (unsigned I = 0; I < 3; ++I) { 468 const auto Arg = TheCall->getArg(I); 469 // Third argument will be a pointer. 470 auto Ty = I < 2 ? Arg->getType() : Arg->getType()->getPointeeType(); 471 if (Ty->isBitIntType() && Ty->isSignedIntegerType() && 472 S.getASTContext().getIntWidth(Ty) > 128) 473 return S.Diag(Arg->getBeginLoc(), 474 diag::err_overflow_builtin_bit_int_max_size) 475 << 128; 476 } 477 } 478 479 return false; 480 } 481 482 namespace { 483 struct BuiltinDumpStructGenerator { 484 Sema &S; 485 CallExpr *TheCall; 486 SourceLocation Loc = TheCall->getBeginLoc(); 487 SmallVector<Expr *, 32> Actions; 488 DiagnosticErrorTrap ErrorTracker; 489 PrintingPolicy Policy; 490 491 BuiltinDumpStructGenerator(Sema &S, CallExpr *TheCall) 492 : S(S), TheCall(TheCall), ErrorTracker(S.getDiagnostics()), 493 Policy(S.Context.getPrintingPolicy()) { 494 Policy.AnonymousTagLocations = false; 495 } 496 497 Expr *makeOpaqueValueExpr(Expr *Inner) { 498 auto *OVE = new (S.Context) 499 OpaqueValueExpr(Loc, Inner->getType(), Inner->getValueKind(), 500 Inner->getObjectKind(), Inner); 501 Actions.push_back(OVE); 502 return OVE; 503 } 504 505 Expr *getStringLiteral(llvm::StringRef Str) { 506 Expr *Lit = S.Context.getPredefinedStringLiteralFromCache(Str); 507 // Wrap the literal in parentheses to attach a source location. 508 return new (S.Context) ParenExpr(Loc, Loc, Lit); 509 } 510 511 bool callPrintFunction(llvm::StringRef Format, 512 llvm::ArrayRef<Expr *> Exprs = {}) { 513 SmallVector<Expr *, 8> Args; 514 assert(TheCall->getNumArgs() >= 2); 515 Args.reserve((TheCall->getNumArgs() - 2) + /*Format*/ 1 + Exprs.size()); 516 Args.assign(TheCall->arg_begin() + 2, TheCall->arg_end()); 517 Args.push_back(getStringLiteral(Format)); 518 Args.insert(Args.end(), Exprs.begin(), Exprs.end()); 519 520 // Register a note to explain why we're performing the call. 521 Sema::CodeSynthesisContext Ctx; 522 Ctx.Kind = Sema::CodeSynthesisContext::BuildingBuiltinDumpStructCall; 523 Ctx.PointOfInstantiation = Loc; 524 Ctx.CallArgs = Args.data(); 525 Ctx.NumCallArgs = Args.size(); 526 S.pushCodeSynthesisContext(Ctx); 527 528 ExprResult RealCall = 529 S.BuildCallExpr(/*Scope=*/nullptr, TheCall->getArg(1), 530 TheCall->getBeginLoc(), Args, TheCall->getRParenLoc()); 531 532 S.popCodeSynthesisContext(); 533 if (!RealCall.isInvalid()) 534 Actions.push_back(RealCall.get()); 535 // Bail out if we've hit any errors, even if we managed to build the 536 // call. We don't want to produce more than one error. 537 return RealCall.isInvalid() || ErrorTracker.hasErrorOccurred(); 538 } 539 540 Expr *getIndentString(unsigned Depth) { 541 if (!Depth) 542 return nullptr; 543 544 llvm::SmallString<32> Indent; 545 Indent.resize(Depth * Policy.Indentation, ' '); 546 return getStringLiteral(Indent); 547 } 548 549 Expr *getTypeString(QualType T) { 550 return getStringLiteral(T.getAsString(Policy)); 551 } 552 553 bool appendFormatSpecifier(QualType T, llvm::SmallVectorImpl<char> &Str) { 554 llvm::raw_svector_ostream OS(Str); 555 556 // Format 'bool', 'char', 'signed char', 'unsigned char' as numbers, rather 557 // than trying to print a single character. 558 if (auto *BT = T->getAs<BuiltinType>()) { 559 switch (BT->getKind()) { 560 case BuiltinType::Bool: 561 OS << "%d"; 562 return true; 563 case BuiltinType::Char_U: 564 case BuiltinType::UChar: 565 OS << "%hhu"; 566 return true; 567 case BuiltinType::Char_S: 568 case BuiltinType::SChar: 569 OS << "%hhd"; 570 return true; 571 default: 572 break; 573 } 574 } 575 576 analyze_printf::PrintfSpecifier Specifier; 577 if (Specifier.fixType(T, S.getLangOpts(), S.Context, /*IsObjCLiteral=*/false)) { 578 // We were able to guess how to format this. 579 if (Specifier.getConversionSpecifier().getKind() == 580 analyze_printf::PrintfConversionSpecifier::sArg) { 581 // Wrap double-quotes around a '%s' specifier and limit its maximum 582 // length. Ideally we'd also somehow escape special characters in the 583 // contents but printf doesn't support that. 584 // FIXME: '%s' formatting is not safe in general. 585 OS << '"'; 586 Specifier.setPrecision(analyze_printf::OptionalAmount(32u)); 587 Specifier.toString(OS); 588 OS << '"'; 589 // FIXME: It would be nice to include a '...' if the string doesn't fit 590 // in the length limit. 591 } else { 592 Specifier.toString(OS); 593 } 594 return true; 595 } 596 597 if (T->isPointerType()) { 598 // Format all pointers with '%p'. 599 OS << "%p"; 600 return true; 601 } 602 603 return false; 604 } 605 606 bool dumpUnnamedRecord(const RecordDecl *RD, Expr *E, unsigned Depth) { 607 Expr *IndentLit = getIndentString(Depth); 608 Expr *TypeLit = getTypeString(S.Context.getRecordType(RD)); 609 if (IndentLit ? callPrintFunction("%s%s", {IndentLit, TypeLit}) 610 : callPrintFunction("%s", {TypeLit})) 611 return true; 612 613 return dumpRecordValue(RD, E, IndentLit, Depth); 614 } 615 616 // Dump a record value. E should be a pointer or lvalue referring to an RD. 617 bool dumpRecordValue(const RecordDecl *RD, Expr *E, Expr *RecordIndent, 618 unsigned Depth) { 619 // FIXME: Decide what to do if RD is a union. At least we should probably 620 // turn off printing `const char*` members with `%s`, because that is very 621 // likely to crash if that's not the active member. Whatever we decide, we 622 // should document it. 623 624 // Build an OpaqueValueExpr so we can refer to E more than once without 625 // triggering re-evaluation. 626 Expr *RecordArg = makeOpaqueValueExpr(E); 627 bool RecordArgIsPtr = RecordArg->getType()->isPointerType(); 628 629 if (callPrintFunction(" {\n")) 630 return true; 631 632 // Dump each base class, regardless of whether they're aggregates. 633 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 634 for (const auto &Base : CXXRD->bases()) { 635 QualType BaseType = 636 RecordArgIsPtr ? S.Context.getPointerType(Base.getType()) 637 : S.Context.getLValueReferenceType(Base.getType()); 638 ExprResult BasePtr = S.BuildCStyleCastExpr( 639 Loc, S.Context.getTrivialTypeSourceInfo(BaseType, Loc), Loc, 640 RecordArg); 641 if (BasePtr.isInvalid() || 642 dumpUnnamedRecord(Base.getType()->getAsRecordDecl(), BasePtr.get(), 643 Depth + 1)) 644 return true; 645 } 646 } 647 648 Expr *FieldIndentArg = getIndentString(Depth + 1); 649 650 // Dump each field. 651 for (auto *D : RD->decls()) { 652 auto *IFD = dyn_cast<IndirectFieldDecl>(D); 653 auto *FD = IFD ? IFD->getAnonField() : dyn_cast<FieldDecl>(D); 654 if (!FD || FD->isUnnamedBitField() || FD->isAnonymousStructOrUnion()) 655 continue; 656 657 llvm::SmallString<20> Format = llvm::StringRef("%s%s %s "); 658 llvm::SmallVector<Expr *, 5> Args = {FieldIndentArg, 659 getTypeString(FD->getType()), 660 getStringLiteral(FD->getName())}; 661 662 if (FD->isBitField()) { 663 Format += ": %zu "; 664 QualType SizeT = S.Context.getSizeType(); 665 llvm::APInt BitWidth(S.Context.getIntWidth(SizeT), 666 FD->getBitWidthValue(S.Context)); 667 Args.push_back(IntegerLiteral::Create(S.Context, BitWidth, SizeT, Loc)); 668 } 669 670 Format += "="; 671 672 ExprResult Field = 673 IFD ? S.BuildAnonymousStructUnionMemberReference( 674 CXXScopeSpec(), Loc, IFD, 675 DeclAccessPair::make(IFD, AS_public), RecordArg, Loc) 676 : S.BuildFieldReferenceExpr( 677 RecordArg, RecordArgIsPtr, Loc, CXXScopeSpec(), FD, 678 DeclAccessPair::make(FD, AS_public), 679 DeclarationNameInfo(FD->getDeclName(), Loc)); 680 if (Field.isInvalid()) 681 return true; 682 683 auto *InnerRD = FD->getType()->getAsRecordDecl(); 684 auto *InnerCXXRD = dyn_cast_or_null<CXXRecordDecl>(InnerRD); 685 if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) { 686 // Recursively print the values of members of aggregate record type. 687 if (callPrintFunction(Format, Args) || 688 dumpRecordValue(InnerRD, Field.get(), FieldIndentArg, Depth + 1)) 689 return true; 690 } else { 691 Format += " "; 692 if (appendFormatSpecifier(FD->getType(), Format)) { 693 // We know how to print this field. 694 Args.push_back(Field.get()); 695 } else { 696 // We don't know how to print this field. Print out its address 697 // with a format specifier that a smart tool will be able to 698 // recognize and treat specially. 699 Format += "*%p"; 700 ExprResult FieldAddr = 701 S.BuildUnaryOp(nullptr, Loc, UO_AddrOf, Field.get()); 702 if (FieldAddr.isInvalid()) 703 return true; 704 Args.push_back(FieldAddr.get()); 705 } 706 Format += "\n"; 707 if (callPrintFunction(Format, Args)) 708 return true; 709 } 710 } 711 712 return RecordIndent ? callPrintFunction("%s}\n", RecordIndent) 713 : callPrintFunction("}\n"); 714 } 715 716 Expr *buildWrapper() { 717 auto *Wrapper = PseudoObjectExpr::Create(S.Context, TheCall, Actions, 718 PseudoObjectExpr::NoResult); 719 TheCall->setType(Wrapper->getType()); 720 TheCall->setValueKind(Wrapper->getValueKind()); 721 return Wrapper; 722 } 723 }; 724 } // namespace 725 726 static ExprResult BuiltinDumpStruct(Sema &S, CallExpr *TheCall) { 727 if (S.checkArgCountAtLeast(TheCall, 2)) 728 return ExprError(); 729 730 ExprResult PtrArgResult = S.DefaultLvalueConversion(TheCall->getArg(0)); 731 if (PtrArgResult.isInvalid()) 732 return ExprError(); 733 TheCall->setArg(0, PtrArgResult.get()); 734 735 // First argument should be a pointer to a struct. 736 QualType PtrArgType = PtrArgResult.get()->getType(); 737 if (!PtrArgType->isPointerType() || 738 !PtrArgType->getPointeeType()->isRecordType()) { 739 S.Diag(PtrArgResult.get()->getBeginLoc(), 740 diag::err_expected_struct_pointer_argument) 741 << 1 << TheCall->getDirectCallee() << PtrArgType; 742 return ExprError(); 743 } 744 QualType Pointee = PtrArgType->getPointeeType(); 745 const RecordDecl *RD = Pointee->getAsRecordDecl(); 746 // Try to instantiate the class template as appropriate; otherwise, access to 747 // its data() may lead to a crash. 748 if (S.RequireCompleteType(PtrArgResult.get()->getBeginLoc(), Pointee, 749 diag::err_incomplete_type)) 750 return ExprError(); 751 // Second argument is a callable, but we can't fully validate it until we try 752 // calling it. 753 QualType FnArgType = TheCall->getArg(1)->getType(); 754 if (!FnArgType->isFunctionType() && !FnArgType->isFunctionPointerType() && 755 !FnArgType->isBlockPointerType() && 756 !(S.getLangOpts().CPlusPlus && FnArgType->isRecordType())) { 757 auto *BT = FnArgType->getAs<BuiltinType>(); 758 switch (BT ? BT->getKind() : BuiltinType::Void) { 759 case BuiltinType::Dependent: 760 case BuiltinType::Overload: 761 case BuiltinType::BoundMember: 762 case BuiltinType::PseudoObject: 763 case BuiltinType::UnknownAny: 764 case BuiltinType::BuiltinFn: 765 // This might be a callable. 766 break; 767 768 default: 769 S.Diag(TheCall->getArg(1)->getBeginLoc(), 770 diag::err_expected_callable_argument) 771 << 2 << TheCall->getDirectCallee() << FnArgType; 772 return ExprError(); 773 } 774 } 775 776 BuiltinDumpStructGenerator Generator(S, TheCall); 777 778 // Wrap parentheses around the given pointer. This is not necessary for 779 // correct code generation, but it means that when we pretty-print the call 780 // arguments in our diagnostics we will produce '(&s)->n' instead of the 781 // incorrect '&s->n'. 782 Expr *PtrArg = PtrArgResult.get(); 783 PtrArg = new (S.Context) 784 ParenExpr(PtrArg->getBeginLoc(), 785 S.getLocForEndOfToken(PtrArg->getEndLoc()), PtrArg); 786 if (Generator.dumpUnnamedRecord(RD, PtrArg, 0)) 787 return ExprError(); 788 789 return Generator.buildWrapper(); 790 } 791 792 static bool BuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { 793 if (S.checkArgCount(BuiltinCall, 2)) 794 return true; 795 796 SourceLocation BuiltinLoc = BuiltinCall->getBeginLoc(); 797 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); 798 Expr *Call = BuiltinCall->getArg(0); 799 Expr *Chain = BuiltinCall->getArg(1); 800 801 if (Call->getStmtClass() != Stmt::CallExprClass) { 802 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) 803 << Call->getSourceRange(); 804 return true; 805 } 806 807 auto CE = cast<CallExpr>(Call); 808 if (CE->getCallee()->getType()->isBlockPointerType()) { 809 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) 810 << Call->getSourceRange(); 811 return true; 812 } 813 814 const Decl *TargetDecl = CE->getCalleeDecl(); 815 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) 816 if (FD->getBuiltinID()) { 817 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) 818 << Call->getSourceRange(); 819 return true; 820 } 821 822 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { 823 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) 824 << Call->getSourceRange(); 825 return true; 826 } 827 828 ExprResult ChainResult = S.UsualUnaryConversions(Chain); 829 if (ChainResult.isInvalid()) 830 return true; 831 if (!ChainResult.get()->getType()->isPointerType()) { 832 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) 833 << Chain->getSourceRange(); 834 return true; 835 } 836 837 QualType ReturnTy = CE->getCallReturnType(S.Context); 838 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; 839 QualType BuiltinTy = S.Context.getFunctionType( 840 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); 841 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); 842 843 Builtin = 844 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); 845 846 BuiltinCall->setType(CE->getType()); 847 BuiltinCall->setValueKind(CE->getValueKind()); 848 BuiltinCall->setObjectKind(CE->getObjectKind()); 849 BuiltinCall->setCallee(Builtin); 850 BuiltinCall->setArg(1, ChainResult.get()); 851 852 return false; 853 } 854 855 namespace { 856 857 class ScanfDiagnosticFormatHandler 858 : public analyze_format_string::FormatStringHandler { 859 // Accepts the argument index (relative to the first destination index) of the 860 // argument whose size we want. 861 using ComputeSizeFunction = 862 llvm::function_ref<std::optional<llvm::APSInt>(unsigned)>; 863 864 // Accepts the argument index (relative to the first destination index), the 865 // destination size, and the source size). 866 using DiagnoseFunction = 867 llvm::function_ref<void(unsigned, unsigned, unsigned)>; 868 869 ComputeSizeFunction ComputeSizeArgument; 870 DiagnoseFunction Diagnose; 871 872 public: 873 ScanfDiagnosticFormatHandler(ComputeSizeFunction ComputeSizeArgument, 874 DiagnoseFunction Diagnose) 875 : ComputeSizeArgument(ComputeSizeArgument), Diagnose(Diagnose) {} 876 877 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 878 const char *StartSpecifier, 879 unsigned specifierLen) override { 880 if (!FS.consumesDataArgument()) 881 return true; 882 883 unsigned NulByte = 0; 884 switch ((FS.getConversionSpecifier().getKind())) { 885 default: 886 return true; 887 case analyze_format_string::ConversionSpecifier::sArg: 888 case analyze_format_string::ConversionSpecifier::ScanListArg: 889 NulByte = 1; 890 break; 891 case analyze_format_string::ConversionSpecifier::cArg: 892 break; 893 } 894 895 analyze_format_string::OptionalAmount FW = FS.getFieldWidth(); 896 if (FW.getHowSpecified() != 897 analyze_format_string::OptionalAmount::HowSpecified::Constant) 898 return true; 899 900 unsigned SourceSize = FW.getConstantAmount() + NulByte; 901 902 std::optional<llvm::APSInt> DestSizeAPS = 903 ComputeSizeArgument(FS.getArgIndex()); 904 if (!DestSizeAPS) 905 return true; 906 907 unsigned DestSize = DestSizeAPS->getZExtValue(); 908 909 if (DestSize < SourceSize) 910 Diagnose(FS.getArgIndex(), DestSize, SourceSize); 911 912 return true; 913 } 914 }; 915 916 class EstimateSizeFormatHandler 917 : public analyze_format_string::FormatStringHandler { 918 size_t Size; 919 /// Whether the format string contains Linux kernel's format specifier 920 /// extension. 921 bool IsKernelCompatible = true; 922 923 public: 924 EstimateSizeFormatHandler(StringRef Format) 925 : Size(std::min(Format.find(0), Format.size()) + 926 1 /* null byte always written by sprintf */) {} 927 928 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 929 const char *, unsigned SpecifierLen, 930 const TargetInfo &) override { 931 932 const size_t FieldWidth = computeFieldWidth(FS); 933 const size_t Precision = computePrecision(FS); 934 935 // The actual format. 936 switch (FS.getConversionSpecifier().getKind()) { 937 // Just a char. 938 case analyze_format_string::ConversionSpecifier::cArg: 939 case analyze_format_string::ConversionSpecifier::CArg: 940 Size += std::max(FieldWidth, (size_t)1); 941 break; 942 // Just an integer. 943 case analyze_format_string::ConversionSpecifier::dArg: 944 case analyze_format_string::ConversionSpecifier::DArg: 945 case analyze_format_string::ConversionSpecifier::iArg: 946 case analyze_format_string::ConversionSpecifier::oArg: 947 case analyze_format_string::ConversionSpecifier::OArg: 948 case analyze_format_string::ConversionSpecifier::uArg: 949 case analyze_format_string::ConversionSpecifier::UArg: 950 case analyze_format_string::ConversionSpecifier::xArg: 951 case analyze_format_string::ConversionSpecifier::XArg: 952 Size += std::max(FieldWidth, Precision); 953 break; 954 955 // %g style conversion switches between %f or %e style dynamically. 956 // %g removes trailing zeros, and does not print decimal point if there are 957 // no digits that follow it. Thus %g can print a single digit. 958 // FIXME: If it is alternative form: 959 // For g and G conversions, trailing zeros are not removed from the result. 960 case analyze_format_string::ConversionSpecifier::gArg: 961 case analyze_format_string::ConversionSpecifier::GArg: 962 Size += 1; 963 break; 964 965 // Floating point number in the form '[+]ddd.ddd'. 966 case analyze_format_string::ConversionSpecifier::fArg: 967 case analyze_format_string::ConversionSpecifier::FArg: 968 Size += std::max(FieldWidth, 1 /* integer part */ + 969 (Precision ? 1 + Precision 970 : 0) /* period + decimal */); 971 break; 972 973 // Floating point number in the form '[-]d.ddde[+-]dd'. 974 case analyze_format_string::ConversionSpecifier::eArg: 975 case analyze_format_string::ConversionSpecifier::EArg: 976 Size += 977 std::max(FieldWidth, 978 1 /* integer part */ + 979 (Precision ? 1 + Precision : 0) /* period + decimal */ + 980 1 /* e or E letter */ + 2 /* exponent */); 981 break; 982 983 // Floating point number in the form '[-]0xh.hhhhp±dd'. 984 case analyze_format_string::ConversionSpecifier::aArg: 985 case analyze_format_string::ConversionSpecifier::AArg: 986 Size += 987 std::max(FieldWidth, 988 2 /* 0x */ + 1 /* integer part */ + 989 (Precision ? 1 + Precision : 0) /* period + decimal */ + 990 1 /* p or P letter */ + 1 /* + or - */ + 1 /* value */); 991 break; 992 993 // Just a string. 994 case analyze_format_string::ConversionSpecifier::sArg: 995 case analyze_format_string::ConversionSpecifier::SArg: 996 Size += FieldWidth; 997 break; 998 999 // Just a pointer in the form '0xddd'. 1000 case analyze_format_string::ConversionSpecifier::pArg: 1001 // Linux kernel has its own extesion for `%p` specifier. 1002 // Kernel Document: 1003 // https://docs.kernel.org/core-api/printk-formats.html#pointer-types 1004 IsKernelCompatible = false; 1005 Size += std::max(FieldWidth, 2 /* leading 0x */ + Precision); 1006 break; 1007 1008 // A plain percent. 1009 case analyze_format_string::ConversionSpecifier::PercentArg: 1010 Size += 1; 1011 break; 1012 1013 default: 1014 break; 1015 } 1016 1017 Size += FS.hasPlusPrefix() || FS.hasSpacePrefix(); 1018 1019 if (FS.hasAlternativeForm()) { 1020 switch (FS.getConversionSpecifier().getKind()) { 1021 // For o conversion, it increases the precision, if and only if necessary, 1022 // to force the first digit of the result to be a zero 1023 // (if the value and precision are both 0, a single 0 is printed) 1024 case analyze_format_string::ConversionSpecifier::oArg: 1025 // For b conversion, a nonzero result has 0b prefixed to it. 1026 case analyze_format_string::ConversionSpecifier::bArg: 1027 // For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to 1028 // it. 1029 case analyze_format_string::ConversionSpecifier::xArg: 1030 case analyze_format_string::ConversionSpecifier::XArg: 1031 // Note: even when the prefix is added, if 1032 // (prefix_width <= FieldWidth - formatted_length) holds, 1033 // the prefix does not increase the format 1034 // size. e.g.(("%#3x", 0xf) is "0xf") 1035 1036 // If the result is zero, o, b, x, X adds nothing. 1037 break; 1038 // For a, A, e, E, f, F, g, and G conversions, 1039 // the result of converting a floating-point number always contains a 1040 // decimal-point 1041 case analyze_format_string::ConversionSpecifier::aArg: 1042 case analyze_format_string::ConversionSpecifier::AArg: 1043 case analyze_format_string::ConversionSpecifier::eArg: 1044 case analyze_format_string::ConversionSpecifier::EArg: 1045 case analyze_format_string::ConversionSpecifier::fArg: 1046 case analyze_format_string::ConversionSpecifier::FArg: 1047 case analyze_format_string::ConversionSpecifier::gArg: 1048 case analyze_format_string::ConversionSpecifier::GArg: 1049 Size += (Precision ? 0 : 1); 1050 break; 1051 // For other conversions, the behavior is undefined. 1052 default: 1053 break; 1054 } 1055 } 1056 assert(SpecifierLen <= Size && "no underflow"); 1057 Size -= SpecifierLen; 1058 return true; 1059 } 1060 1061 size_t getSizeLowerBound() const { return Size; } 1062 bool isKernelCompatible() const { return IsKernelCompatible; } 1063 1064 private: 1065 static size_t computeFieldWidth(const analyze_printf::PrintfSpecifier &FS) { 1066 const analyze_format_string::OptionalAmount &FW = FS.getFieldWidth(); 1067 size_t FieldWidth = 0; 1068 if (FW.getHowSpecified() == analyze_format_string::OptionalAmount::Constant) 1069 FieldWidth = FW.getConstantAmount(); 1070 return FieldWidth; 1071 } 1072 1073 static size_t computePrecision(const analyze_printf::PrintfSpecifier &FS) { 1074 const analyze_format_string::OptionalAmount &FW = FS.getPrecision(); 1075 size_t Precision = 0; 1076 1077 // See man 3 printf for default precision value based on the specifier. 1078 switch (FW.getHowSpecified()) { 1079 case analyze_format_string::OptionalAmount::NotSpecified: 1080 switch (FS.getConversionSpecifier().getKind()) { 1081 default: 1082 break; 1083 case analyze_format_string::ConversionSpecifier::dArg: // %d 1084 case analyze_format_string::ConversionSpecifier::DArg: // %D 1085 case analyze_format_string::ConversionSpecifier::iArg: // %i 1086 Precision = 1; 1087 break; 1088 case analyze_format_string::ConversionSpecifier::oArg: // %d 1089 case analyze_format_string::ConversionSpecifier::OArg: // %D 1090 case analyze_format_string::ConversionSpecifier::uArg: // %d 1091 case analyze_format_string::ConversionSpecifier::UArg: // %D 1092 case analyze_format_string::ConversionSpecifier::xArg: // %d 1093 case analyze_format_string::ConversionSpecifier::XArg: // %D 1094 Precision = 1; 1095 break; 1096 case analyze_format_string::ConversionSpecifier::fArg: // %f 1097 case analyze_format_string::ConversionSpecifier::FArg: // %F 1098 case analyze_format_string::ConversionSpecifier::eArg: // %e 1099 case analyze_format_string::ConversionSpecifier::EArg: // %E 1100 case analyze_format_string::ConversionSpecifier::gArg: // %g 1101 case analyze_format_string::ConversionSpecifier::GArg: // %G 1102 Precision = 6; 1103 break; 1104 case analyze_format_string::ConversionSpecifier::pArg: // %d 1105 Precision = 1; 1106 break; 1107 } 1108 break; 1109 case analyze_format_string::OptionalAmount::Constant: 1110 Precision = FW.getConstantAmount(); 1111 break; 1112 default: 1113 break; 1114 } 1115 return Precision; 1116 } 1117 }; 1118 1119 } // namespace 1120 1121 static bool ProcessFormatStringLiteral(const Expr *FormatExpr, 1122 StringRef &FormatStrRef, size_t &StrLen, 1123 ASTContext &Context) { 1124 if (const auto *Format = dyn_cast<StringLiteral>(FormatExpr); 1125 Format && (Format->isOrdinary() || Format->isUTF8())) { 1126 FormatStrRef = Format->getString(); 1127 const ConstantArrayType *T = 1128 Context.getAsConstantArrayType(Format->getType()); 1129 assert(T && "String literal not of constant array type!"); 1130 size_t TypeSize = T->getZExtSize(); 1131 // In case there's a null byte somewhere. 1132 StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, FormatStrRef.find(0)); 1133 return true; 1134 } 1135 return false; 1136 } 1137 1138 void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, 1139 CallExpr *TheCall) { 1140 if (TheCall->isValueDependent() || TheCall->isTypeDependent() || 1141 isConstantEvaluatedContext()) 1142 return; 1143 1144 bool UseDABAttr = false; 1145 const FunctionDecl *UseDecl = FD; 1146 1147 const auto *DABAttr = FD->getAttr<DiagnoseAsBuiltinAttr>(); 1148 if (DABAttr) { 1149 UseDecl = DABAttr->getFunction(); 1150 assert(UseDecl && "Missing FunctionDecl in DiagnoseAsBuiltin attribute!"); 1151 UseDABAttr = true; 1152 } 1153 1154 unsigned BuiltinID = UseDecl->getBuiltinID(/*ConsiderWrappers=*/true); 1155 1156 if (!BuiltinID) 1157 return; 1158 1159 const TargetInfo &TI = getASTContext().getTargetInfo(); 1160 unsigned SizeTypeWidth = TI.getTypeWidth(TI.getSizeType()); 1161 1162 auto TranslateIndex = [&](unsigned Index) -> std::optional<unsigned> { 1163 // If we refer to a diagnose_as_builtin attribute, we need to change the 1164 // argument index to refer to the arguments of the called function. Unless 1165 // the index is out of bounds, which presumably means it's a variadic 1166 // function. 1167 if (!UseDABAttr) 1168 return Index; 1169 unsigned DABIndices = DABAttr->argIndices_size(); 1170 unsigned NewIndex = Index < DABIndices 1171 ? DABAttr->argIndices_begin()[Index] 1172 : Index - DABIndices + FD->getNumParams(); 1173 if (NewIndex >= TheCall->getNumArgs()) 1174 return std::nullopt; 1175 return NewIndex; 1176 }; 1177 1178 auto ComputeExplicitObjectSizeArgument = 1179 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1180 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1181 if (!IndexOptional) 1182 return std::nullopt; 1183 unsigned NewIndex = *IndexOptional; 1184 Expr::EvalResult Result; 1185 Expr *SizeArg = TheCall->getArg(NewIndex); 1186 if (!SizeArg->EvaluateAsInt(Result, getASTContext())) 1187 return std::nullopt; 1188 llvm::APSInt Integer = Result.Val.getInt(); 1189 Integer.setIsUnsigned(true); 1190 return Integer; 1191 }; 1192 1193 auto ComputeSizeArgument = 1194 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1195 // If the parameter has a pass_object_size attribute, then we should use its 1196 // (potentially) more strict checking mode. Otherwise, conservatively assume 1197 // type 0. 1198 int BOSType = 0; 1199 // This check can fail for variadic functions. 1200 if (Index < FD->getNumParams()) { 1201 if (const auto *POS = 1202 FD->getParamDecl(Index)->getAttr<PassObjectSizeAttr>()) 1203 BOSType = POS->getType(); 1204 } 1205 1206 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1207 if (!IndexOptional) 1208 return std::nullopt; 1209 unsigned NewIndex = *IndexOptional; 1210 1211 if (NewIndex >= TheCall->getNumArgs()) 1212 return std::nullopt; 1213 1214 const Expr *ObjArg = TheCall->getArg(NewIndex); 1215 uint64_t Result; 1216 if (!ObjArg->tryEvaluateObjectSize(Result, getASTContext(), BOSType)) 1217 return std::nullopt; 1218 1219 // Get the object size in the target's size_t width. 1220 return llvm::APSInt::getUnsigned(Result).extOrTrunc(SizeTypeWidth); 1221 }; 1222 1223 auto ComputeStrLenArgument = 1224 [&](unsigned Index) -> std::optional<llvm::APSInt> { 1225 std::optional<unsigned> IndexOptional = TranslateIndex(Index); 1226 if (!IndexOptional) 1227 return std::nullopt; 1228 unsigned NewIndex = *IndexOptional; 1229 1230 const Expr *ObjArg = TheCall->getArg(NewIndex); 1231 uint64_t Result; 1232 if (!ObjArg->tryEvaluateStrLen(Result, getASTContext())) 1233 return std::nullopt; 1234 // Add 1 for null byte. 1235 return llvm::APSInt::getUnsigned(Result + 1).extOrTrunc(SizeTypeWidth); 1236 }; 1237 1238 std::optional<llvm::APSInt> SourceSize; 1239 std::optional<llvm::APSInt> DestinationSize; 1240 unsigned DiagID = 0; 1241 bool IsChkVariant = false; 1242 1243 auto GetFunctionName = [&]() { 1244 StringRef FunctionName = getASTContext().BuiltinInfo.getName(BuiltinID); 1245 // Skim off the details of whichever builtin was called to produce a better 1246 // diagnostic, as it's unlikely that the user wrote the __builtin 1247 // explicitly. 1248 if (IsChkVariant) { 1249 FunctionName = FunctionName.drop_front(std::strlen("__builtin___")); 1250 FunctionName = FunctionName.drop_back(std::strlen("_chk")); 1251 } else { 1252 FunctionName.consume_front("__builtin_"); 1253 } 1254 return FunctionName; 1255 }; 1256 1257 switch (BuiltinID) { 1258 default: 1259 return; 1260 case Builtin::BI__builtin_strcpy: 1261 case Builtin::BIstrcpy: { 1262 DiagID = diag::warn_fortify_strlen_overflow; 1263 SourceSize = ComputeStrLenArgument(1); 1264 DestinationSize = ComputeSizeArgument(0); 1265 break; 1266 } 1267 1268 case Builtin::BI__builtin___strcpy_chk: { 1269 DiagID = diag::warn_fortify_strlen_overflow; 1270 SourceSize = ComputeStrLenArgument(1); 1271 DestinationSize = ComputeExplicitObjectSizeArgument(2); 1272 IsChkVariant = true; 1273 break; 1274 } 1275 1276 case Builtin::BIscanf: 1277 case Builtin::BIfscanf: 1278 case Builtin::BIsscanf: { 1279 unsigned FormatIndex = 1; 1280 unsigned DataIndex = 2; 1281 if (BuiltinID == Builtin::BIscanf) { 1282 FormatIndex = 0; 1283 DataIndex = 1; 1284 } 1285 1286 const auto *FormatExpr = 1287 TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); 1288 1289 StringRef FormatStrRef; 1290 size_t StrLen; 1291 if (!ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) 1292 return; 1293 1294 auto Diagnose = [&](unsigned ArgIndex, unsigned DestSize, 1295 unsigned SourceSize) { 1296 DiagID = diag::warn_fortify_scanf_overflow; 1297 unsigned Index = ArgIndex + DataIndex; 1298 StringRef FunctionName = GetFunctionName(); 1299 DiagRuntimeBehavior(TheCall->getArg(Index)->getBeginLoc(), TheCall, 1300 PDiag(DiagID) << FunctionName << (Index + 1) 1301 << DestSize << SourceSize); 1302 }; 1303 1304 auto ShiftedComputeSizeArgument = [&](unsigned Index) { 1305 return ComputeSizeArgument(Index + DataIndex); 1306 }; 1307 ScanfDiagnosticFormatHandler H(ShiftedComputeSizeArgument, Diagnose); 1308 const char *FormatBytes = FormatStrRef.data(); 1309 analyze_format_string::ParseScanfString(H, FormatBytes, 1310 FormatBytes + StrLen, getLangOpts(), 1311 Context.getTargetInfo()); 1312 1313 // Unlike the other cases, in this one we have already issued the diagnostic 1314 // here, so no need to continue (because unlike the other cases, here the 1315 // diagnostic refers to the argument number). 1316 return; 1317 } 1318 1319 case Builtin::BIsprintf: 1320 case Builtin::BI__builtin___sprintf_chk: { 1321 size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3; 1322 auto *FormatExpr = TheCall->getArg(FormatIndex)->IgnoreParenImpCasts(); 1323 1324 StringRef FormatStrRef; 1325 size_t StrLen; 1326 if (ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { 1327 EstimateSizeFormatHandler H(FormatStrRef); 1328 const char *FormatBytes = FormatStrRef.data(); 1329 if (!analyze_format_string::ParsePrintfString( 1330 H, FormatBytes, FormatBytes + StrLen, getLangOpts(), 1331 Context.getTargetInfo(), false)) { 1332 DiagID = H.isKernelCompatible() 1333 ? diag::warn_format_overflow 1334 : diag::warn_format_overflow_non_kprintf; 1335 SourceSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) 1336 .extOrTrunc(SizeTypeWidth); 1337 if (BuiltinID == Builtin::BI__builtin___sprintf_chk) { 1338 DestinationSize = ComputeExplicitObjectSizeArgument(2); 1339 IsChkVariant = true; 1340 } else { 1341 DestinationSize = ComputeSizeArgument(0); 1342 } 1343 break; 1344 } 1345 } 1346 return; 1347 } 1348 case Builtin::BI__builtin___memcpy_chk: 1349 case Builtin::BI__builtin___memmove_chk: 1350 case Builtin::BI__builtin___memset_chk: 1351 case Builtin::BI__builtin___strlcat_chk: 1352 case Builtin::BI__builtin___strlcpy_chk: 1353 case Builtin::BI__builtin___strncat_chk: 1354 case Builtin::BI__builtin___strncpy_chk: 1355 case Builtin::BI__builtin___stpncpy_chk: 1356 case Builtin::BI__builtin___memccpy_chk: 1357 case Builtin::BI__builtin___mempcpy_chk: { 1358 DiagID = diag::warn_builtin_chk_overflow; 1359 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2); 1360 DestinationSize = 1361 ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1362 IsChkVariant = true; 1363 break; 1364 } 1365 1366 case Builtin::BI__builtin___snprintf_chk: 1367 case Builtin::BI__builtin___vsnprintf_chk: { 1368 DiagID = diag::warn_builtin_chk_overflow; 1369 SourceSize = ComputeExplicitObjectSizeArgument(1); 1370 DestinationSize = ComputeExplicitObjectSizeArgument(3); 1371 IsChkVariant = true; 1372 break; 1373 } 1374 1375 case Builtin::BIstrncat: 1376 case Builtin::BI__builtin_strncat: 1377 case Builtin::BIstrncpy: 1378 case Builtin::BI__builtin_strncpy: 1379 case Builtin::BIstpncpy: 1380 case Builtin::BI__builtin_stpncpy: { 1381 // Whether these functions overflow depends on the runtime strlen of the 1382 // string, not just the buffer size, so emitting the "always overflow" 1383 // diagnostic isn't quite right. We should still diagnose passing a buffer 1384 // size larger than the destination buffer though; this is a runtime abort 1385 // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise. 1386 DiagID = diag::warn_fortify_source_size_mismatch; 1387 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1388 DestinationSize = ComputeSizeArgument(0); 1389 break; 1390 } 1391 1392 case Builtin::BImemcpy: 1393 case Builtin::BI__builtin_memcpy: 1394 case Builtin::BImemmove: 1395 case Builtin::BI__builtin_memmove: 1396 case Builtin::BImemset: 1397 case Builtin::BI__builtin_memset: 1398 case Builtin::BImempcpy: 1399 case Builtin::BI__builtin_mempcpy: { 1400 DiagID = diag::warn_fortify_source_overflow; 1401 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); 1402 DestinationSize = ComputeSizeArgument(0); 1403 break; 1404 } 1405 case Builtin::BIsnprintf: 1406 case Builtin::BI__builtin_snprintf: 1407 case Builtin::BIvsnprintf: 1408 case Builtin::BI__builtin_vsnprintf: { 1409 DiagID = diag::warn_fortify_source_size_mismatch; 1410 SourceSize = ComputeExplicitObjectSizeArgument(1); 1411 const auto *FormatExpr = TheCall->getArg(2)->IgnoreParenImpCasts(); 1412 StringRef FormatStrRef; 1413 size_t StrLen; 1414 if (SourceSize && 1415 ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { 1416 EstimateSizeFormatHandler H(FormatStrRef); 1417 const char *FormatBytes = FormatStrRef.data(); 1418 if (!analyze_format_string::ParsePrintfString( 1419 H, FormatBytes, FormatBytes + StrLen, getLangOpts(), 1420 Context.getTargetInfo(), /*isFreeBSDKPrintf=*/false)) { 1421 llvm::APSInt FormatSize = 1422 llvm::APSInt::getUnsigned(H.getSizeLowerBound()) 1423 .extOrTrunc(SizeTypeWidth); 1424 if (FormatSize > *SourceSize && *SourceSize != 0) { 1425 unsigned TruncationDiagID = 1426 H.isKernelCompatible() ? diag::warn_format_truncation 1427 : diag::warn_format_truncation_non_kprintf; 1428 SmallString<16> SpecifiedSizeStr; 1429 SmallString<16> FormatSizeStr; 1430 SourceSize->toString(SpecifiedSizeStr, /*Radix=*/10); 1431 FormatSize.toString(FormatSizeStr, /*Radix=*/10); 1432 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 1433 PDiag(TruncationDiagID) 1434 << GetFunctionName() << SpecifiedSizeStr 1435 << FormatSizeStr); 1436 } 1437 } 1438 } 1439 DestinationSize = ComputeSizeArgument(0); 1440 } 1441 } 1442 1443 if (!SourceSize || !DestinationSize || 1444 llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0) 1445 return; 1446 1447 StringRef FunctionName = GetFunctionName(); 1448 1449 SmallString<16> DestinationStr; 1450 SmallString<16> SourceStr; 1451 DestinationSize->toString(DestinationStr, /*Radix=*/10); 1452 SourceSize->toString(SourceStr, /*Radix=*/10); 1453 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 1454 PDiag(DiagID) 1455 << FunctionName << DestinationStr << SourceStr); 1456 } 1457 1458 static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, 1459 Scope::ScopeFlags NeededScopeFlags, 1460 unsigned DiagID) { 1461 // Scopes aren't available during instantiation. Fortunately, builtin 1462 // functions cannot be template args so they cannot be formed through template 1463 // instantiation. Therefore checking once during the parse is sufficient. 1464 if (SemaRef.inTemplateInstantiation()) 1465 return false; 1466 1467 Scope *S = SemaRef.getCurScope(); 1468 while (S && !S->isSEHExceptScope()) 1469 S = S->getParent(); 1470 if (!S || !(S->getFlags() & NeededScopeFlags)) { 1471 auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1472 SemaRef.Diag(TheCall->getExprLoc(), DiagID) 1473 << DRE->getDecl()->getIdentifier(); 1474 return true; 1475 } 1476 1477 return false; 1478 } 1479 1480 // In OpenCL, __builtin_alloca_* should return a pointer to address space 1481 // that corresponds to the stack address space i.e private address space. 1482 static void builtinAllocaAddrSpace(Sema &S, CallExpr *TheCall) { 1483 QualType RT = TheCall->getType(); 1484 assert((RT->isPointerType() && !(RT->getPointeeType().hasAddressSpace())) && 1485 "__builtin_alloca has invalid address space"); 1486 1487 RT = RT->getPointeeType(); 1488 RT = S.Context.getAddrSpaceQualType(RT, LangAS::opencl_private); 1489 TheCall->setType(S.Context.getPointerType(RT)); 1490 } 1491 1492 namespace { 1493 enum PointerAuthOpKind { 1494 PAO_Strip, 1495 PAO_Sign, 1496 PAO_Auth, 1497 PAO_SignGeneric, 1498 PAO_Discriminator, 1499 PAO_BlendPointer, 1500 PAO_BlendInteger 1501 }; 1502 } 1503 1504 bool Sema::checkPointerAuthEnabled(SourceLocation Loc, SourceRange Range) { 1505 if (getLangOpts().PointerAuthIntrinsics) 1506 return false; 1507 1508 Diag(Loc, diag::err_ptrauth_disabled) << Range; 1509 return true; 1510 } 1511 1512 static bool checkPointerAuthEnabled(Sema &S, Expr *E) { 1513 return S.checkPointerAuthEnabled(E->getExprLoc(), E->getSourceRange()); 1514 } 1515 1516 static bool checkPointerAuthKey(Sema &S, Expr *&Arg) { 1517 // Convert it to type 'int'. 1518 if (convertArgumentToType(S, Arg, S.Context.IntTy)) 1519 return true; 1520 1521 // Value-dependent expressions are okay; wait for template instantiation. 1522 if (Arg->isValueDependent()) 1523 return false; 1524 1525 unsigned KeyValue; 1526 return S.checkConstantPointerAuthKey(Arg, KeyValue); 1527 } 1528 1529 bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) { 1530 // Attempt to constant-evaluate the expression. 1531 std::optional<llvm::APSInt> KeyValue = Arg->getIntegerConstantExpr(Context); 1532 if (!KeyValue) { 1533 Diag(Arg->getExprLoc(), diag::err_expr_not_ice) 1534 << 0 << Arg->getSourceRange(); 1535 return true; 1536 } 1537 1538 // Ask the target to validate the key parameter. 1539 if (!Context.getTargetInfo().validatePointerAuthKey(*KeyValue)) { 1540 llvm::SmallString<32> Value; 1541 { 1542 llvm::raw_svector_ostream Str(Value); 1543 Str << *KeyValue; 1544 } 1545 1546 Diag(Arg->getExprLoc(), diag::err_ptrauth_invalid_key) 1547 << Value << Arg->getSourceRange(); 1548 return true; 1549 } 1550 1551 Result = KeyValue->getZExtValue(); 1552 return false; 1553 } 1554 1555 static std::pair<const ValueDecl *, CharUnits> 1556 findConstantBaseAndOffset(Sema &S, Expr *E) { 1557 // Must evaluate as a pointer. 1558 Expr::EvalResult Result; 1559 if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue()) 1560 return {nullptr, CharUnits()}; 1561 1562 const auto *BaseDecl = 1563 Result.Val.getLValueBase().dyn_cast<const ValueDecl *>(); 1564 if (!BaseDecl) 1565 return {nullptr, CharUnits()}; 1566 1567 return {BaseDecl, Result.Val.getLValueOffset()}; 1568 } 1569 1570 static bool checkPointerAuthValue(Sema &S, Expr *&Arg, PointerAuthOpKind OpKind, 1571 bool RequireConstant = false) { 1572 if (Arg->hasPlaceholderType()) { 1573 ExprResult R = S.CheckPlaceholderExpr(Arg); 1574 if (R.isInvalid()) 1575 return true; 1576 Arg = R.get(); 1577 } 1578 1579 auto AllowsPointer = [](PointerAuthOpKind OpKind) { 1580 return OpKind != PAO_BlendInteger; 1581 }; 1582 auto AllowsInteger = [](PointerAuthOpKind OpKind) { 1583 return OpKind == PAO_Discriminator || OpKind == PAO_BlendInteger || 1584 OpKind == PAO_SignGeneric; 1585 }; 1586 1587 // Require the value to have the right range of type. 1588 QualType ExpectedTy; 1589 if (AllowsPointer(OpKind) && Arg->getType()->isPointerType()) { 1590 ExpectedTy = Arg->getType().getUnqualifiedType(); 1591 } else if (AllowsPointer(OpKind) && Arg->getType()->isNullPtrType()) { 1592 ExpectedTy = S.Context.VoidPtrTy; 1593 } else if (AllowsInteger(OpKind) && 1594 Arg->getType()->isIntegralOrUnscopedEnumerationType()) { 1595 ExpectedTy = S.Context.getUIntPtrType(); 1596 1597 } else { 1598 // Diagnose the failures. 1599 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_value_bad_type) 1600 << unsigned(OpKind == PAO_Discriminator ? 1 1601 : OpKind == PAO_BlendPointer ? 2 1602 : OpKind == PAO_BlendInteger ? 3 1603 : 0) 1604 << unsigned(AllowsInteger(OpKind) ? (AllowsPointer(OpKind) ? 2 : 1) : 0) 1605 << Arg->getType() << Arg->getSourceRange(); 1606 return true; 1607 } 1608 1609 // Convert to that type. This should just be an lvalue-to-rvalue 1610 // conversion. 1611 if (convertArgumentToType(S, Arg, ExpectedTy)) 1612 return true; 1613 1614 if (!RequireConstant) { 1615 // Warn about null pointers for non-generic sign and auth operations. 1616 if ((OpKind == PAO_Sign || OpKind == PAO_Auth) && 1617 Arg->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) { 1618 S.Diag(Arg->getExprLoc(), OpKind == PAO_Sign 1619 ? diag::warn_ptrauth_sign_null_pointer 1620 : diag::warn_ptrauth_auth_null_pointer) 1621 << Arg->getSourceRange(); 1622 } 1623 1624 return false; 1625 } 1626 1627 // Perform special checking on the arguments to ptrauth_sign_constant. 1628 1629 // The main argument. 1630 if (OpKind == PAO_Sign) { 1631 // Require the value we're signing to have a special form. 1632 auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Arg); 1633 bool Invalid; 1634 1635 // Must be rooted in a declaration reference. 1636 if (!BaseDecl) 1637 Invalid = true; 1638 1639 // If it's a function declaration, we can't have an offset. 1640 else if (isa<FunctionDecl>(BaseDecl)) 1641 Invalid = !Offset.isZero(); 1642 1643 // Otherwise we're fine. 1644 else 1645 Invalid = false; 1646 1647 if (Invalid) 1648 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer); 1649 return Invalid; 1650 } 1651 1652 // The discriminator argument. 1653 assert(OpKind == PAO_Discriminator); 1654 1655 // Must be a pointer or integer or blend thereof. 1656 Expr *Pointer = nullptr; 1657 Expr *Integer = nullptr; 1658 if (auto *Call = dyn_cast<CallExpr>(Arg->IgnoreParens())) { 1659 if (Call->getBuiltinCallee() == 1660 Builtin::BI__builtin_ptrauth_blend_discriminator) { 1661 Pointer = Call->getArg(0); 1662 Integer = Call->getArg(1); 1663 } 1664 } 1665 if (!Pointer && !Integer) { 1666 if (Arg->getType()->isPointerType()) 1667 Pointer = Arg; 1668 else 1669 Integer = Arg; 1670 } 1671 1672 // Check the pointer. 1673 bool Invalid = false; 1674 if (Pointer) { 1675 assert(Pointer->getType()->isPointerType()); 1676 1677 // TODO: if we're initializing a global, check that the address is 1678 // somehow related to what we're initializing. This probably will 1679 // never really be feasible and we'll have to catch it at link-time. 1680 auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Pointer); 1681 if (!BaseDecl || !isa<VarDecl>(BaseDecl)) 1682 Invalid = true; 1683 } 1684 1685 // Check the integer. 1686 if (Integer) { 1687 assert(Integer->getType()->isIntegerType()); 1688 if (!Integer->isEvaluatable(S.Context)) 1689 Invalid = true; 1690 } 1691 1692 if (Invalid) 1693 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator); 1694 return Invalid; 1695 } 1696 1697 static ExprResult PointerAuthStrip(Sema &S, CallExpr *Call) { 1698 if (S.checkArgCount(Call, 2)) 1699 return ExprError(); 1700 if (checkPointerAuthEnabled(S, Call)) 1701 return ExprError(); 1702 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Strip) || 1703 checkPointerAuthKey(S, Call->getArgs()[1])) 1704 return ExprError(); 1705 1706 Call->setType(Call->getArgs()[0]->getType()); 1707 return Call; 1708 } 1709 1710 static ExprResult PointerAuthBlendDiscriminator(Sema &S, CallExpr *Call) { 1711 if (S.checkArgCount(Call, 2)) 1712 return ExprError(); 1713 if (checkPointerAuthEnabled(S, Call)) 1714 return ExprError(); 1715 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_BlendPointer) || 1716 checkPointerAuthValue(S, Call->getArgs()[1], PAO_BlendInteger)) 1717 return ExprError(); 1718 1719 Call->setType(S.Context.getUIntPtrType()); 1720 return Call; 1721 } 1722 1723 static ExprResult PointerAuthSignGenericData(Sema &S, CallExpr *Call) { 1724 if (S.checkArgCount(Call, 2)) 1725 return ExprError(); 1726 if (checkPointerAuthEnabled(S, Call)) 1727 return ExprError(); 1728 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_SignGeneric) || 1729 checkPointerAuthValue(S, Call->getArgs()[1], PAO_Discriminator)) 1730 return ExprError(); 1731 1732 Call->setType(S.Context.getUIntPtrType()); 1733 return Call; 1734 } 1735 1736 static ExprResult PointerAuthSignOrAuth(Sema &S, CallExpr *Call, 1737 PointerAuthOpKind OpKind, 1738 bool RequireConstant) { 1739 if (S.checkArgCount(Call, 3)) 1740 return ExprError(); 1741 if (checkPointerAuthEnabled(S, Call)) 1742 return ExprError(); 1743 if (checkPointerAuthValue(S, Call->getArgs()[0], OpKind, RequireConstant) || 1744 checkPointerAuthKey(S, Call->getArgs()[1]) || 1745 checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator, 1746 RequireConstant)) 1747 return ExprError(); 1748 1749 Call->setType(Call->getArgs()[0]->getType()); 1750 return Call; 1751 } 1752 1753 static ExprResult PointerAuthAuthAndResign(Sema &S, CallExpr *Call) { 1754 if (S.checkArgCount(Call, 5)) 1755 return ExprError(); 1756 if (checkPointerAuthEnabled(S, Call)) 1757 return ExprError(); 1758 if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Auth) || 1759 checkPointerAuthKey(S, Call->getArgs()[1]) || 1760 checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator) || 1761 checkPointerAuthKey(S, Call->getArgs()[3]) || 1762 checkPointerAuthValue(S, Call->getArgs()[4], PAO_Discriminator)) 1763 return ExprError(); 1764 1765 Call->setType(Call->getArgs()[0]->getType()); 1766 return Call; 1767 } 1768 1769 static ExprResult PointerAuthStringDiscriminator(Sema &S, CallExpr *Call) { 1770 if (checkPointerAuthEnabled(S, Call)) 1771 return ExprError(); 1772 1773 // We've already performed normal call type-checking. 1774 const Expr *Arg = Call->getArg(0)->IgnoreParenImpCasts(); 1775 1776 // Operand must be an ordinary or UTF-8 string literal. 1777 const auto *Literal = dyn_cast<StringLiteral>(Arg); 1778 if (!Literal || Literal->getCharByteWidth() != 1) { 1779 S.Diag(Arg->getExprLoc(), diag::err_ptrauth_string_not_literal) 1780 << (Literal ? 1 : 0) << Arg->getSourceRange(); 1781 return ExprError(); 1782 } 1783 1784 return Call; 1785 } 1786 1787 static ExprResult BuiltinLaunder(Sema &S, CallExpr *TheCall) { 1788 if (S.checkArgCount(TheCall, 1)) 1789 return ExprError(); 1790 1791 // Compute __builtin_launder's parameter type from the argument. 1792 // The parameter type is: 1793 // * The type of the argument if it's not an array or function type, 1794 // Otherwise, 1795 // * The decayed argument type. 1796 QualType ParamTy = [&]() { 1797 QualType ArgTy = TheCall->getArg(0)->getType(); 1798 if (const ArrayType *Ty = ArgTy->getAsArrayTypeUnsafe()) 1799 return S.Context.getPointerType(Ty->getElementType()); 1800 if (ArgTy->isFunctionType()) { 1801 return S.Context.getPointerType(ArgTy); 1802 } 1803 return ArgTy; 1804 }(); 1805 1806 TheCall->setType(ParamTy); 1807 1808 auto DiagSelect = [&]() -> std::optional<unsigned> { 1809 if (!ParamTy->isPointerType()) 1810 return 0; 1811 if (ParamTy->isFunctionPointerType()) 1812 return 1; 1813 if (ParamTy->isVoidPointerType()) 1814 return 2; 1815 return std::optional<unsigned>{}; 1816 }(); 1817 if (DiagSelect) { 1818 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_launder_invalid_arg) 1819 << *DiagSelect << TheCall->getSourceRange(); 1820 return ExprError(); 1821 } 1822 1823 // We either have an incomplete class type, or we have a class template 1824 // whose instantiation has not been forced. Example: 1825 // 1826 // template <class T> struct Foo { T value; }; 1827 // Foo<int> *p = nullptr; 1828 // auto *d = __builtin_launder(p); 1829 if (S.RequireCompleteType(TheCall->getBeginLoc(), ParamTy->getPointeeType(), 1830 diag::err_incomplete_type)) 1831 return ExprError(); 1832 1833 assert(ParamTy->getPointeeType()->isObjectType() && 1834 "Unhandled non-object pointer case"); 1835 1836 InitializedEntity Entity = 1837 InitializedEntity::InitializeParameter(S.Context, ParamTy, false); 1838 ExprResult Arg = 1839 S.PerformCopyInitialization(Entity, SourceLocation(), TheCall->getArg(0)); 1840 if (Arg.isInvalid()) 1841 return ExprError(); 1842 TheCall->setArg(0, Arg.get()); 1843 1844 return TheCall; 1845 } 1846 1847 static ExprResult BuiltinIsWithinLifetime(Sema &S, CallExpr *TheCall) { 1848 if (S.checkArgCount(TheCall, 1)) 1849 return ExprError(); 1850 1851 ExprResult Arg = S.DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 1852 if (Arg.isInvalid()) 1853 return ExprError(); 1854 QualType ParamTy = Arg.get()->getType(); 1855 TheCall->setArg(0, Arg.get()); 1856 TheCall->setType(S.Context.BoolTy); 1857 1858 // Only accept pointers to objects as arguments, which should have object 1859 // pointer or void pointer types. 1860 if (const auto *PT = ParamTy->getAs<PointerType>()) { 1861 // LWG4138: Function pointer types not allowed 1862 if (PT->getPointeeType()->isFunctionType()) { 1863 S.Diag(TheCall->getArg(0)->getExprLoc(), 1864 diag::err_builtin_is_within_lifetime_invalid_arg) 1865 << 1; 1866 return ExprError(); 1867 } 1868 // Disallow VLAs too since those shouldn't be able to 1869 // be a template parameter for `std::is_within_lifetime` 1870 if (PT->getPointeeType()->isVariableArrayType()) { 1871 S.Diag(TheCall->getArg(0)->getExprLoc(), diag::err_vla_unsupported) 1872 << 1 << "__builtin_is_within_lifetime"; 1873 return ExprError(); 1874 } 1875 } else { 1876 S.Diag(TheCall->getArg(0)->getExprLoc(), 1877 diag::err_builtin_is_within_lifetime_invalid_arg) 1878 << 0; 1879 return ExprError(); 1880 } 1881 1882 return TheCall; 1883 } 1884 1885 // Emit an error and return true if the current object format type is in the 1886 // list of unsupported types. 1887 static bool CheckBuiltinTargetNotInUnsupported( 1888 Sema &S, unsigned BuiltinID, CallExpr *TheCall, 1889 ArrayRef<llvm::Triple::ObjectFormatType> UnsupportedObjectFormatTypes) { 1890 llvm::Triple::ObjectFormatType CurObjFormat = 1891 S.getASTContext().getTargetInfo().getTriple().getObjectFormat(); 1892 if (llvm::is_contained(UnsupportedObjectFormatTypes, CurObjFormat)) { 1893 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1894 << TheCall->getSourceRange(); 1895 return true; 1896 } 1897 return false; 1898 } 1899 1900 // Emit an error and return true if the current architecture is not in the list 1901 // of supported architectures. 1902 static bool 1903 CheckBuiltinTargetInSupported(Sema &S, CallExpr *TheCall, 1904 ArrayRef<llvm::Triple::ArchType> SupportedArchs) { 1905 llvm::Triple::ArchType CurArch = 1906 S.getASTContext().getTargetInfo().getTriple().getArch(); 1907 if (llvm::is_contained(SupportedArchs, CurArch)) 1908 return false; 1909 S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 1910 << TheCall->getSourceRange(); 1911 return true; 1912 } 1913 1914 static void CheckNonNullArgument(Sema &S, const Expr *ArgExpr, 1915 SourceLocation CallSiteLoc); 1916 1917 bool Sema::CheckTSBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, 1918 CallExpr *TheCall) { 1919 switch (TI.getTriple().getArch()) { 1920 default: 1921 // Some builtins don't require additional checking, so just consider these 1922 // acceptable. 1923 return false; 1924 case llvm::Triple::arm: 1925 case llvm::Triple::armeb: 1926 case llvm::Triple::thumb: 1927 case llvm::Triple::thumbeb: 1928 return ARM().CheckARMBuiltinFunctionCall(TI, BuiltinID, TheCall); 1929 case llvm::Triple::aarch64: 1930 case llvm::Triple::aarch64_32: 1931 case llvm::Triple::aarch64_be: 1932 return ARM().CheckAArch64BuiltinFunctionCall(TI, BuiltinID, TheCall); 1933 case llvm::Triple::bpfeb: 1934 case llvm::Triple::bpfel: 1935 return BPF().CheckBPFBuiltinFunctionCall(BuiltinID, TheCall); 1936 case llvm::Triple::hexagon: 1937 return Hexagon().CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall); 1938 case llvm::Triple::mips: 1939 case llvm::Triple::mipsel: 1940 case llvm::Triple::mips64: 1941 case llvm::Triple::mips64el: 1942 return MIPS().CheckMipsBuiltinFunctionCall(TI, BuiltinID, TheCall); 1943 case llvm::Triple::systemz: 1944 return SystemZ().CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall); 1945 case llvm::Triple::x86: 1946 case llvm::Triple::x86_64: 1947 return X86().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall); 1948 case llvm::Triple::ppc: 1949 case llvm::Triple::ppcle: 1950 case llvm::Triple::ppc64: 1951 case llvm::Triple::ppc64le: 1952 return PPC().CheckPPCBuiltinFunctionCall(TI, BuiltinID, TheCall); 1953 case llvm::Triple::amdgcn: 1954 return AMDGPU().CheckAMDGCNBuiltinFunctionCall(BuiltinID, TheCall); 1955 case llvm::Triple::riscv32: 1956 case llvm::Triple::riscv64: 1957 return RISCV().CheckBuiltinFunctionCall(TI, BuiltinID, TheCall); 1958 case llvm::Triple::loongarch32: 1959 case llvm::Triple::loongarch64: 1960 return LoongArch().CheckLoongArchBuiltinFunctionCall(TI, BuiltinID, 1961 TheCall); 1962 case llvm::Triple::wasm32: 1963 case llvm::Triple::wasm64: 1964 return Wasm().CheckWebAssemblyBuiltinFunctionCall(TI, BuiltinID, TheCall); 1965 case llvm::Triple::nvptx: 1966 case llvm::Triple::nvptx64: 1967 return NVPTX().CheckNVPTXBuiltinFunctionCall(TI, BuiltinID, TheCall); 1968 } 1969 } 1970 1971 // Check if \p Ty is a valid type for the elementwise math builtins. If it is 1972 // not a valid type, emit an error message and return true. Otherwise return 1973 // false. 1974 static bool checkMathBuiltinElementType(Sema &S, SourceLocation Loc, 1975 QualType ArgTy, int ArgIndex) { 1976 if (!ArgTy->getAs<VectorType>() && 1977 !ConstantMatrixType::isValidElementType(ArgTy)) { 1978 return S.Diag(Loc, diag::err_builtin_invalid_arg_type) 1979 << ArgIndex << /* vector, integer or float ty*/ 0 << ArgTy; 1980 } 1981 1982 return false; 1983 } 1984 1985 static bool checkFPMathBuiltinElementType(Sema &S, SourceLocation Loc, 1986 QualType ArgTy, int ArgIndex) { 1987 QualType EltTy = ArgTy; 1988 if (auto *VecTy = EltTy->getAs<VectorType>()) 1989 EltTy = VecTy->getElementType(); 1990 1991 if (!EltTy->isRealFloatingType()) { 1992 return S.Diag(Loc, diag::err_builtin_invalid_arg_type) 1993 << ArgIndex << /* vector or float ty*/ 5 << ArgTy; 1994 } 1995 1996 return false; 1997 } 1998 1999 /// BuiltinCpu{Supports|Is} - Handle __builtin_cpu_{supports|is}(char *). 2000 /// This checks that the target supports the builtin and that the string 2001 /// argument is constant and valid. 2002 static bool BuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall, 2003 const TargetInfo *AuxTI, unsigned BuiltinID) { 2004 assert((BuiltinID == Builtin::BI__builtin_cpu_supports || 2005 BuiltinID == Builtin::BI__builtin_cpu_is) && 2006 "Expecting __builtin_cpu_..."); 2007 2008 bool IsCPUSupports = BuiltinID == Builtin::BI__builtin_cpu_supports; 2009 const TargetInfo *TheTI = &TI; 2010 auto SupportsBI = [=](const TargetInfo *TInfo) { 2011 return TInfo && ((IsCPUSupports && TInfo->supportsCpuSupports()) || 2012 (!IsCPUSupports && TInfo->supportsCpuIs())); 2013 }; 2014 if (!SupportsBI(&TI) && SupportsBI(AuxTI)) 2015 TheTI = AuxTI; 2016 2017 if ((!IsCPUSupports && !TheTI->supportsCpuIs()) || 2018 (IsCPUSupports && !TheTI->supportsCpuSupports())) 2019 return S.Diag(TheCall->getBeginLoc(), 2020 TI.getTriple().isOSAIX() 2021 ? diag::err_builtin_aix_os_unsupported 2022 : diag::err_builtin_target_unsupported) 2023 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 2024 2025 Expr *Arg = TheCall->getArg(0)->IgnoreParenImpCasts(); 2026 // Check if the argument is a string literal. 2027 if (!isa<StringLiteral>(Arg)) 2028 return S.Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 2029 << Arg->getSourceRange(); 2030 2031 // Check the contents of the string. 2032 StringRef Feature = cast<StringLiteral>(Arg)->getString(); 2033 if (IsCPUSupports && !TheTI->validateCpuSupports(Feature)) { 2034 S.Diag(TheCall->getBeginLoc(), diag::warn_invalid_cpu_supports) 2035 << Arg->getSourceRange(); 2036 return false; 2037 } 2038 if (!IsCPUSupports && !TheTI->validateCpuIs(Feature)) 2039 return S.Diag(TheCall->getBeginLoc(), diag::err_invalid_cpu_is) 2040 << Arg->getSourceRange(); 2041 return false; 2042 } 2043 2044 /// Checks that __builtin_popcountg was called with a single argument, which is 2045 /// an unsigned integer. 2046 static bool BuiltinPopcountg(Sema &S, CallExpr *TheCall) { 2047 if (S.checkArgCount(TheCall, 1)) 2048 return true; 2049 2050 ExprResult ArgRes = S.DefaultLvalueConversion(TheCall->getArg(0)); 2051 if (ArgRes.isInvalid()) 2052 return true; 2053 2054 Expr *Arg = ArgRes.get(); 2055 TheCall->setArg(0, Arg); 2056 2057 QualType ArgTy = Arg->getType(); 2058 2059 if (!ArgTy->isUnsignedIntegerType()) { 2060 S.Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2061 << 1 << /*unsigned integer ty*/ 7 << ArgTy; 2062 return true; 2063 } 2064 return false; 2065 } 2066 2067 /// Checks that __builtin_{clzg,ctzg} was called with a first argument, which is 2068 /// an unsigned integer, and an optional second argument, which is promoted to 2069 /// an 'int'. 2070 static bool BuiltinCountZeroBitsGeneric(Sema &S, CallExpr *TheCall) { 2071 if (S.checkArgCountRange(TheCall, 1, 2)) 2072 return true; 2073 2074 ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0)); 2075 if (Arg0Res.isInvalid()) 2076 return true; 2077 2078 Expr *Arg0 = Arg0Res.get(); 2079 TheCall->setArg(0, Arg0); 2080 2081 QualType Arg0Ty = Arg0->getType(); 2082 2083 if (!Arg0Ty->isUnsignedIntegerType()) { 2084 S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2085 << 1 << /*unsigned integer ty*/ 7 << Arg0Ty; 2086 return true; 2087 } 2088 2089 if (TheCall->getNumArgs() > 1) { 2090 ExprResult Arg1Res = S.UsualUnaryConversions(TheCall->getArg(1)); 2091 if (Arg1Res.isInvalid()) 2092 return true; 2093 2094 Expr *Arg1 = Arg1Res.get(); 2095 TheCall->setArg(1, Arg1); 2096 2097 QualType Arg1Ty = Arg1->getType(); 2098 2099 if (!Arg1Ty->isSpecificBuiltinType(BuiltinType::Int)) { 2100 S.Diag(Arg1->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2101 << 2 << /*'int' ty*/ 8 << Arg1Ty; 2102 return true; 2103 } 2104 } 2105 2106 return false; 2107 } 2108 2109 ExprResult 2110 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, 2111 CallExpr *TheCall) { 2112 ExprResult TheCallResult(TheCall); 2113 2114 // Find out if any arguments are required to be integer constant expressions. 2115 unsigned ICEArguments = 0; 2116 ASTContext::GetBuiltinTypeError Error; 2117 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); 2118 if (Error != ASTContext::GE_None) 2119 ICEArguments = 0; // Don't diagnose previously diagnosed errors. 2120 2121 // If any arguments are required to be ICE's, check and diagnose. 2122 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { 2123 // Skip arguments not required to be ICE's. 2124 if ((ICEArguments & (1 << ArgNo)) == 0) continue; 2125 2126 llvm::APSInt Result; 2127 // If we don't have enough arguments, continue so we can issue better 2128 // diagnostic in checkArgCount(...) 2129 if (ArgNo < TheCall->getNumArgs() && 2130 BuiltinConstantArg(TheCall, ArgNo, Result)) 2131 return true; 2132 ICEArguments &= ~(1 << ArgNo); 2133 } 2134 2135 FPOptions FPO; 2136 switch (BuiltinID) { 2137 case Builtin::BI__builtin_cpu_supports: 2138 case Builtin::BI__builtin_cpu_is: 2139 if (BuiltinCpu(*this, Context.getTargetInfo(), TheCall, 2140 Context.getAuxTargetInfo(), BuiltinID)) 2141 return ExprError(); 2142 break; 2143 case Builtin::BI__builtin_cpu_init: 2144 if (!Context.getTargetInfo().supportsCpuInit()) { 2145 Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 2146 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 2147 return ExprError(); 2148 } 2149 break; 2150 case Builtin::BI__builtin___CFStringMakeConstantString: 2151 // CFStringMakeConstantString is currently not implemented for GOFF (i.e., 2152 // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported 2153 if (CheckBuiltinTargetNotInUnsupported( 2154 *this, BuiltinID, TheCall, 2155 {llvm::Triple::GOFF, llvm::Triple::XCOFF})) 2156 return ExprError(); 2157 assert(TheCall->getNumArgs() == 1 && 2158 "Wrong # arguments to builtin CFStringMakeConstantString"); 2159 if (ObjC().CheckObjCString(TheCall->getArg(0))) 2160 return ExprError(); 2161 break; 2162 case Builtin::BI__builtin_ms_va_start: 2163 case Builtin::BI__builtin_stdarg_start: 2164 case Builtin::BI__builtin_va_start: 2165 if (BuiltinVAStart(BuiltinID, TheCall)) 2166 return ExprError(); 2167 break; 2168 case Builtin::BI__va_start: { 2169 switch (Context.getTargetInfo().getTriple().getArch()) { 2170 case llvm::Triple::aarch64: 2171 case llvm::Triple::arm: 2172 case llvm::Triple::thumb: 2173 if (BuiltinVAStartARMMicrosoft(TheCall)) 2174 return ExprError(); 2175 break; 2176 default: 2177 if (BuiltinVAStart(BuiltinID, TheCall)) 2178 return ExprError(); 2179 break; 2180 } 2181 break; 2182 } 2183 2184 // The acquire, release, and no fence variants are ARM and AArch64 only. 2185 case Builtin::BI_interlockedbittestandset_acq: 2186 case Builtin::BI_interlockedbittestandset_rel: 2187 case Builtin::BI_interlockedbittestandset_nf: 2188 case Builtin::BI_interlockedbittestandreset_acq: 2189 case Builtin::BI_interlockedbittestandreset_rel: 2190 case Builtin::BI_interlockedbittestandreset_nf: 2191 if (CheckBuiltinTargetInSupported( 2192 *this, TheCall, 2193 {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64})) 2194 return ExprError(); 2195 break; 2196 2197 // The 64-bit bittest variants are x64, ARM, and AArch64 only. 2198 case Builtin::BI_bittest64: 2199 case Builtin::BI_bittestandcomplement64: 2200 case Builtin::BI_bittestandreset64: 2201 case Builtin::BI_bittestandset64: 2202 case Builtin::BI_interlockedbittestandreset64: 2203 case Builtin::BI_interlockedbittestandset64: 2204 if (CheckBuiltinTargetInSupported( 2205 *this, TheCall, 2206 {llvm::Triple::x86_64, llvm::Triple::arm, llvm::Triple::thumb, 2207 llvm::Triple::aarch64, llvm::Triple::amdgcn})) 2208 return ExprError(); 2209 break; 2210 2211 case Builtin::BI__builtin_set_flt_rounds: 2212 if (CheckBuiltinTargetInSupported( 2213 *this, TheCall, 2214 {llvm::Triple::x86, llvm::Triple::x86_64, llvm::Triple::arm, 2215 llvm::Triple::thumb, llvm::Triple::aarch64, llvm::Triple::amdgcn, 2216 llvm::Triple::ppc, llvm::Triple::ppc64, llvm::Triple::ppcle, 2217 llvm::Triple::ppc64le})) 2218 return ExprError(); 2219 break; 2220 2221 case Builtin::BI__builtin_isgreater: 2222 case Builtin::BI__builtin_isgreaterequal: 2223 case Builtin::BI__builtin_isless: 2224 case Builtin::BI__builtin_islessequal: 2225 case Builtin::BI__builtin_islessgreater: 2226 case Builtin::BI__builtin_isunordered: 2227 if (BuiltinUnorderedCompare(TheCall, BuiltinID)) 2228 return ExprError(); 2229 break; 2230 case Builtin::BI__builtin_fpclassify: 2231 if (BuiltinFPClassification(TheCall, 6, BuiltinID)) 2232 return ExprError(); 2233 break; 2234 case Builtin::BI__builtin_isfpclass: 2235 if (BuiltinFPClassification(TheCall, 2, BuiltinID)) 2236 return ExprError(); 2237 break; 2238 case Builtin::BI__builtin_isfinite: 2239 case Builtin::BI__builtin_isinf: 2240 case Builtin::BI__builtin_isinf_sign: 2241 case Builtin::BI__builtin_isnan: 2242 case Builtin::BI__builtin_issignaling: 2243 case Builtin::BI__builtin_isnormal: 2244 case Builtin::BI__builtin_issubnormal: 2245 case Builtin::BI__builtin_iszero: 2246 case Builtin::BI__builtin_signbit: 2247 case Builtin::BI__builtin_signbitf: 2248 case Builtin::BI__builtin_signbitl: 2249 if (BuiltinFPClassification(TheCall, 1, BuiltinID)) 2250 return ExprError(); 2251 break; 2252 case Builtin::BI__builtin_shufflevector: 2253 return BuiltinShuffleVector(TheCall); 2254 // TheCall will be freed by the smart pointer here, but that's fine, since 2255 // BuiltinShuffleVector guts it, but then doesn't release it. 2256 case Builtin::BI__builtin_prefetch: 2257 if (BuiltinPrefetch(TheCall)) 2258 return ExprError(); 2259 break; 2260 case Builtin::BI__builtin_alloca_with_align: 2261 case Builtin::BI__builtin_alloca_with_align_uninitialized: 2262 if (BuiltinAllocaWithAlign(TheCall)) 2263 return ExprError(); 2264 [[fallthrough]]; 2265 case Builtin::BI__builtin_alloca: 2266 case Builtin::BI__builtin_alloca_uninitialized: 2267 Diag(TheCall->getBeginLoc(), diag::warn_alloca) 2268 << TheCall->getDirectCallee(); 2269 if (getLangOpts().OpenCL) { 2270 builtinAllocaAddrSpace(*this, TheCall); 2271 } 2272 break; 2273 case Builtin::BI__arithmetic_fence: 2274 if (BuiltinArithmeticFence(TheCall)) 2275 return ExprError(); 2276 break; 2277 case Builtin::BI__assume: 2278 case Builtin::BI__builtin_assume: 2279 if (BuiltinAssume(TheCall)) 2280 return ExprError(); 2281 break; 2282 case Builtin::BI__builtin_assume_aligned: 2283 if (BuiltinAssumeAligned(TheCall)) 2284 return ExprError(); 2285 break; 2286 case Builtin::BI__builtin_dynamic_object_size: 2287 case Builtin::BI__builtin_object_size: 2288 if (BuiltinConstantArgRange(TheCall, 1, 0, 3)) 2289 return ExprError(); 2290 break; 2291 case Builtin::BI__builtin_longjmp: 2292 if (BuiltinLongjmp(TheCall)) 2293 return ExprError(); 2294 break; 2295 case Builtin::BI__builtin_setjmp: 2296 if (BuiltinSetjmp(TheCall)) 2297 return ExprError(); 2298 break; 2299 case Builtin::BI__builtin_classify_type: 2300 if (checkArgCount(TheCall, 1)) 2301 return true; 2302 TheCall->setType(Context.IntTy); 2303 break; 2304 case Builtin::BI__builtin_complex: 2305 if (BuiltinComplex(TheCall)) 2306 return ExprError(); 2307 break; 2308 case Builtin::BI__builtin_constant_p: { 2309 if (checkArgCount(TheCall, 1)) 2310 return true; 2311 ExprResult Arg = DefaultFunctionArrayLvalueConversion(TheCall->getArg(0)); 2312 if (Arg.isInvalid()) return true; 2313 TheCall->setArg(0, Arg.get()); 2314 TheCall->setType(Context.IntTy); 2315 break; 2316 } 2317 case Builtin::BI__builtin_launder: 2318 return BuiltinLaunder(*this, TheCall); 2319 case Builtin::BI__builtin_is_within_lifetime: 2320 return BuiltinIsWithinLifetime(*this, TheCall); 2321 case Builtin::BI__sync_fetch_and_add: 2322 case Builtin::BI__sync_fetch_and_add_1: 2323 case Builtin::BI__sync_fetch_and_add_2: 2324 case Builtin::BI__sync_fetch_and_add_4: 2325 case Builtin::BI__sync_fetch_and_add_8: 2326 case Builtin::BI__sync_fetch_and_add_16: 2327 case Builtin::BI__sync_fetch_and_sub: 2328 case Builtin::BI__sync_fetch_and_sub_1: 2329 case Builtin::BI__sync_fetch_and_sub_2: 2330 case Builtin::BI__sync_fetch_and_sub_4: 2331 case Builtin::BI__sync_fetch_and_sub_8: 2332 case Builtin::BI__sync_fetch_and_sub_16: 2333 case Builtin::BI__sync_fetch_and_or: 2334 case Builtin::BI__sync_fetch_and_or_1: 2335 case Builtin::BI__sync_fetch_and_or_2: 2336 case Builtin::BI__sync_fetch_and_or_4: 2337 case Builtin::BI__sync_fetch_and_or_8: 2338 case Builtin::BI__sync_fetch_and_or_16: 2339 case Builtin::BI__sync_fetch_and_and: 2340 case Builtin::BI__sync_fetch_and_and_1: 2341 case Builtin::BI__sync_fetch_and_and_2: 2342 case Builtin::BI__sync_fetch_and_and_4: 2343 case Builtin::BI__sync_fetch_and_and_8: 2344 case Builtin::BI__sync_fetch_and_and_16: 2345 case Builtin::BI__sync_fetch_and_xor: 2346 case Builtin::BI__sync_fetch_and_xor_1: 2347 case Builtin::BI__sync_fetch_and_xor_2: 2348 case Builtin::BI__sync_fetch_and_xor_4: 2349 case Builtin::BI__sync_fetch_and_xor_8: 2350 case Builtin::BI__sync_fetch_and_xor_16: 2351 case Builtin::BI__sync_fetch_and_nand: 2352 case Builtin::BI__sync_fetch_and_nand_1: 2353 case Builtin::BI__sync_fetch_and_nand_2: 2354 case Builtin::BI__sync_fetch_and_nand_4: 2355 case Builtin::BI__sync_fetch_and_nand_8: 2356 case Builtin::BI__sync_fetch_and_nand_16: 2357 case Builtin::BI__sync_add_and_fetch: 2358 case Builtin::BI__sync_add_and_fetch_1: 2359 case Builtin::BI__sync_add_and_fetch_2: 2360 case Builtin::BI__sync_add_and_fetch_4: 2361 case Builtin::BI__sync_add_and_fetch_8: 2362 case Builtin::BI__sync_add_and_fetch_16: 2363 case Builtin::BI__sync_sub_and_fetch: 2364 case Builtin::BI__sync_sub_and_fetch_1: 2365 case Builtin::BI__sync_sub_and_fetch_2: 2366 case Builtin::BI__sync_sub_and_fetch_4: 2367 case Builtin::BI__sync_sub_and_fetch_8: 2368 case Builtin::BI__sync_sub_and_fetch_16: 2369 case Builtin::BI__sync_and_and_fetch: 2370 case Builtin::BI__sync_and_and_fetch_1: 2371 case Builtin::BI__sync_and_and_fetch_2: 2372 case Builtin::BI__sync_and_and_fetch_4: 2373 case Builtin::BI__sync_and_and_fetch_8: 2374 case Builtin::BI__sync_and_and_fetch_16: 2375 case Builtin::BI__sync_or_and_fetch: 2376 case Builtin::BI__sync_or_and_fetch_1: 2377 case Builtin::BI__sync_or_and_fetch_2: 2378 case Builtin::BI__sync_or_and_fetch_4: 2379 case Builtin::BI__sync_or_and_fetch_8: 2380 case Builtin::BI__sync_or_and_fetch_16: 2381 case Builtin::BI__sync_xor_and_fetch: 2382 case Builtin::BI__sync_xor_and_fetch_1: 2383 case Builtin::BI__sync_xor_and_fetch_2: 2384 case Builtin::BI__sync_xor_and_fetch_4: 2385 case Builtin::BI__sync_xor_and_fetch_8: 2386 case Builtin::BI__sync_xor_and_fetch_16: 2387 case Builtin::BI__sync_nand_and_fetch: 2388 case Builtin::BI__sync_nand_and_fetch_1: 2389 case Builtin::BI__sync_nand_and_fetch_2: 2390 case Builtin::BI__sync_nand_and_fetch_4: 2391 case Builtin::BI__sync_nand_and_fetch_8: 2392 case Builtin::BI__sync_nand_and_fetch_16: 2393 case Builtin::BI__sync_val_compare_and_swap: 2394 case Builtin::BI__sync_val_compare_and_swap_1: 2395 case Builtin::BI__sync_val_compare_and_swap_2: 2396 case Builtin::BI__sync_val_compare_and_swap_4: 2397 case Builtin::BI__sync_val_compare_and_swap_8: 2398 case Builtin::BI__sync_val_compare_and_swap_16: 2399 case Builtin::BI__sync_bool_compare_and_swap: 2400 case Builtin::BI__sync_bool_compare_and_swap_1: 2401 case Builtin::BI__sync_bool_compare_and_swap_2: 2402 case Builtin::BI__sync_bool_compare_and_swap_4: 2403 case Builtin::BI__sync_bool_compare_and_swap_8: 2404 case Builtin::BI__sync_bool_compare_and_swap_16: 2405 case Builtin::BI__sync_lock_test_and_set: 2406 case Builtin::BI__sync_lock_test_and_set_1: 2407 case Builtin::BI__sync_lock_test_and_set_2: 2408 case Builtin::BI__sync_lock_test_and_set_4: 2409 case Builtin::BI__sync_lock_test_and_set_8: 2410 case Builtin::BI__sync_lock_test_and_set_16: 2411 case Builtin::BI__sync_lock_release: 2412 case Builtin::BI__sync_lock_release_1: 2413 case Builtin::BI__sync_lock_release_2: 2414 case Builtin::BI__sync_lock_release_4: 2415 case Builtin::BI__sync_lock_release_8: 2416 case Builtin::BI__sync_lock_release_16: 2417 case Builtin::BI__sync_swap: 2418 case Builtin::BI__sync_swap_1: 2419 case Builtin::BI__sync_swap_2: 2420 case Builtin::BI__sync_swap_4: 2421 case Builtin::BI__sync_swap_8: 2422 case Builtin::BI__sync_swap_16: 2423 return BuiltinAtomicOverloaded(TheCallResult); 2424 case Builtin::BI__sync_synchronize: 2425 Diag(TheCall->getBeginLoc(), diag::warn_atomic_implicit_seq_cst) 2426 << TheCall->getCallee()->getSourceRange(); 2427 break; 2428 case Builtin::BI__builtin_nontemporal_load: 2429 case Builtin::BI__builtin_nontemporal_store: 2430 return BuiltinNontemporalOverloaded(TheCallResult); 2431 case Builtin::BI__builtin_memcpy_inline: { 2432 clang::Expr *SizeOp = TheCall->getArg(2); 2433 // We warn about copying to or from `nullptr` pointers when `size` is 2434 // greater than 0. When `size` is value dependent we cannot evaluate its 2435 // value so we bail out. 2436 if (SizeOp->isValueDependent()) 2437 break; 2438 if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) { 2439 CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); 2440 CheckNonNullArgument(*this, TheCall->getArg(1), TheCall->getExprLoc()); 2441 } 2442 break; 2443 } 2444 case Builtin::BI__builtin_memset_inline: { 2445 clang::Expr *SizeOp = TheCall->getArg(2); 2446 // We warn about filling to `nullptr` pointers when `size` is greater than 2447 // 0. When `size` is value dependent we cannot evaluate its value so we bail 2448 // out. 2449 if (SizeOp->isValueDependent()) 2450 break; 2451 if (!SizeOp->EvaluateKnownConstInt(Context).isZero()) 2452 CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc()); 2453 break; 2454 } 2455 #define BUILTIN(ID, TYPE, ATTRS) 2456 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 2457 case Builtin::BI##ID: \ 2458 return AtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); 2459 #include "clang/Basic/Builtins.inc" 2460 case Builtin::BI__annotation: 2461 if (BuiltinMSVCAnnotation(*this, TheCall)) 2462 return ExprError(); 2463 break; 2464 case Builtin::BI__builtin_annotation: 2465 if (BuiltinAnnotation(*this, TheCall)) 2466 return ExprError(); 2467 break; 2468 case Builtin::BI__builtin_addressof: 2469 if (BuiltinAddressof(*this, TheCall)) 2470 return ExprError(); 2471 break; 2472 case Builtin::BI__builtin_function_start: 2473 if (BuiltinFunctionStart(*this, TheCall)) 2474 return ExprError(); 2475 break; 2476 case Builtin::BI__builtin_is_aligned: 2477 case Builtin::BI__builtin_align_up: 2478 case Builtin::BI__builtin_align_down: 2479 if (BuiltinAlignment(*this, TheCall, BuiltinID)) 2480 return ExprError(); 2481 break; 2482 case Builtin::BI__builtin_add_overflow: 2483 case Builtin::BI__builtin_sub_overflow: 2484 case Builtin::BI__builtin_mul_overflow: 2485 if (BuiltinOverflow(*this, TheCall, BuiltinID)) 2486 return ExprError(); 2487 break; 2488 case Builtin::BI__builtin_operator_new: 2489 case Builtin::BI__builtin_operator_delete: { 2490 bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete; 2491 ExprResult Res = 2492 BuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete); 2493 if (Res.isInvalid()) 2494 CorrectDelayedTyposInExpr(TheCallResult.get()); 2495 return Res; 2496 } 2497 case Builtin::BI__builtin_dump_struct: 2498 return BuiltinDumpStruct(*this, TheCall); 2499 case Builtin::BI__builtin_expect_with_probability: { 2500 // We first want to ensure we are called with 3 arguments 2501 if (checkArgCount(TheCall, 3)) 2502 return ExprError(); 2503 // then check probability is constant float in range [0.0, 1.0] 2504 const Expr *ProbArg = TheCall->getArg(2); 2505 SmallVector<PartialDiagnosticAt, 8> Notes; 2506 Expr::EvalResult Eval; 2507 Eval.Diag = &Notes; 2508 if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) || 2509 !Eval.Val.isFloat()) { 2510 Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float) 2511 << ProbArg->getSourceRange(); 2512 for (const PartialDiagnosticAt &PDiag : Notes) 2513 Diag(PDiag.first, PDiag.second); 2514 return ExprError(); 2515 } 2516 llvm::APFloat Probability = Eval.Val.getFloat(); 2517 bool LoseInfo = false; 2518 Probability.convert(llvm::APFloat::IEEEdouble(), 2519 llvm::RoundingMode::Dynamic, &LoseInfo); 2520 if (!(Probability >= llvm::APFloat(0.0) && 2521 Probability <= llvm::APFloat(1.0))) { 2522 Diag(ProbArg->getBeginLoc(), diag::err_probability_out_of_range) 2523 << ProbArg->getSourceRange(); 2524 return ExprError(); 2525 } 2526 break; 2527 } 2528 case Builtin::BI__builtin_preserve_access_index: 2529 if (BuiltinPreserveAI(*this, TheCall)) 2530 return ExprError(); 2531 break; 2532 case Builtin::BI__builtin_call_with_static_chain: 2533 if (BuiltinCallWithStaticChain(*this, TheCall)) 2534 return ExprError(); 2535 break; 2536 case Builtin::BI__exception_code: 2537 case Builtin::BI_exception_code: 2538 if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope, 2539 diag::err_seh___except_block)) 2540 return ExprError(); 2541 break; 2542 case Builtin::BI__exception_info: 2543 case Builtin::BI_exception_info: 2544 if (BuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope, 2545 diag::err_seh___except_filter)) 2546 return ExprError(); 2547 break; 2548 case Builtin::BI__GetExceptionInfo: 2549 if (checkArgCount(TheCall, 1)) 2550 return ExprError(); 2551 2552 if (CheckCXXThrowOperand( 2553 TheCall->getBeginLoc(), 2554 Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()), 2555 TheCall)) 2556 return ExprError(); 2557 2558 TheCall->setType(Context.VoidPtrTy); 2559 break; 2560 case Builtin::BIaddressof: 2561 case Builtin::BI__addressof: 2562 case Builtin::BIforward: 2563 case Builtin::BIforward_like: 2564 case Builtin::BImove: 2565 case Builtin::BImove_if_noexcept: 2566 case Builtin::BIas_const: { 2567 // These are all expected to be of the form 2568 // T &/&&/* f(U &/&&) 2569 // where T and U only differ in qualification. 2570 if (checkArgCount(TheCall, 1)) 2571 return ExprError(); 2572 QualType Param = FDecl->getParamDecl(0)->getType(); 2573 QualType Result = FDecl->getReturnType(); 2574 bool ReturnsPointer = BuiltinID == Builtin::BIaddressof || 2575 BuiltinID == Builtin::BI__addressof; 2576 if (!(Param->isReferenceType() && 2577 (ReturnsPointer ? Result->isAnyPointerType() 2578 : Result->isReferenceType()) && 2579 Context.hasSameUnqualifiedType(Param->getPointeeType(), 2580 Result->getPointeeType()))) { 2581 Diag(TheCall->getBeginLoc(), diag::err_builtin_move_forward_unsupported) 2582 << FDecl; 2583 return ExprError(); 2584 } 2585 break; 2586 } 2587 case Builtin::BI__builtin_ptrauth_strip: 2588 return PointerAuthStrip(*this, TheCall); 2589 case Builtin::BI__builtin_ptrauth_blend_discriminator: 2590 return PointerAuthBlendDiscriminator(*this, TheCall); 2591 case Builtin::BI__builtin_ptrauth_sign_constant: 2592 return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign, 2593 /*RequireConstant=*/true); 2594 case Builtin::BI__builtin_ptrauth_sign_unauthenticated: 2595 return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign, 2596 /*RequireConstant=*/false); 2597 case Builtin::BI__builtin_ptrauth_auth: 2598 return PointerAuthSignOrAuth(*this, TheCall, PAO_Auth, 2599 /*RequireConstant=*/false); 2600 case Builtin::BI__builtin_ptrauth_sign_generic_data: 2601 return PointerAuthSignGenericData(*this, TheCall); 2602 case Builtin::BI__builtin_ptrauth_auth_and_resign: 2603 return PointerAuthAuthAndResign(*this, TheCall); 2604 case Builtin::BI__builtin_ptrauth_string_discriminator: 2605 return PointerAuthStringDiscriminator(*this, TheCall); 2606 // OpenCL v2.0, s6.13.16 - Pipe functions 2607 case Builtin::BIread_pipe: 2608 case Builtin::BIwrite_pipe: 2609 // Since those two functions are declared with var args, we need a semantic 2610 // check for the argument. 2611 if (OpenCL().checkBuiltinRWPipe(TheCall)) 2612 return ExprError(); 2613 break; 2614 case Builtin::BIreserve_read_pipe: 2615 case Builtin::BIreserve_write_pipe: 2616 case Builtin::BIwork_group_reserve_read_pipe: 2617 case Builtin::BIwork_group_reserve_write_pipe: 2618 if (OpenCL().checkBuiltinReserveRWPipe(TheCall)) 2619 return ExprError(); 2620 break; 2621 case Builtin::BIsub_group_reserve_read_pipe: 2622 case Builtin::BIsub_group_reserve_write_pipe: 2623 if (OpenCL().checkSubgroupExt(TheCall) || 2624 OpenCL().checkBuiltinReserveRWPipe(TheCall)) 2625 return ExprError(); 2626 break; 2627 case Builtin::BIcommit_read_pipe: 2628 case Builtin::BIcommit_write_pipe: 2629 case Builtin::BIwork_group_commit_read_pipe: 2630 case Builtin::BIwork_group_commit_write_pipe: 2631 if (OpenCL().checkBuiltinCommitRWPipe(TheCall)) 2632 return ExprError(); 2633 break; 2634 case Builtin::BIsub_group_commit_read_pipe: 2635 case Builtin::BIsub_group_commit_write_pipe: 2636 if (OpenCL().checkSubgroupExt(TheCall) || 2637 OpenCL().checkBuiltinCommitRWPipe(TheCall)) 2638 return ExprError(); 2639 break; 2640 case Builtin::BIget_pipe_num_packets: 2641 case Builtin::BIget_pipe_max_packets: 2642 if (OpenCL().checkBuiltinPipePackets(TheCall)) 2643 return ExprError(); 2644 break; 2645 case Builtin::BIto_global: 2646 case Builtin::BIto_local: 2647 case Builtin::BIto_private: 2648 if (OpenCL().checkBuiltinToAddr(BuiltinID, TheCall)) 2649 return ExprError(); 2650 break; 2651 // OpenCL v2.0, s6.13.17 - Enqueue kernel functions. 2652 case Builtin::BIenqueue_kernel: 2653 if (OpenCL().checkBuiltinEnqueueKernel(TheCall)) 2654 return ExprError(); 2655 break; 2656 case Builtin::BIget_kernel_work_group_size: 2657 case Builtin::BIget_kernel_preferred_work_group_size_multiple: 2658 if (OpenCL().checkBuiltinKernelWorkGroupSize(TheCall)) 2659 return ExprError(); 2660 break; 2661 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange: 2662 case Builtin::BIget_kernel_sub_group_count_for_ndrange: 2663 if (OpenCL().checkBuiltinNDRangeAndBlock(TheCall)) 2664 return ExprError(); 2665 break; 2666 case Builtin::BI__builtin_os_log_format: 2667 Cleanup.setExprNeedsCleanups(true); 2668 [[fallthrough]]; 2669 case Builtin::BI__builtin_os_log_format_buffer_size: 2670 if (BuiltinOSLogFormat(TheCall)) 2671 return ExprError(); 2672 break; 2673 case Builtin::BI__builtin_frame_address: 2674 case Builtin::BI__builtin_return_address: { 2675 if (BuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF)) 2676 return ExprError(); 2677 2678 // -Wframe-address warning if non-zero passed to builtin 2679 // return/frame address. 2680 Expr::EvalResult Result; 2681 if (!TheCall->getArg(0)->isValueDependent() && 2682 TheCall->getArg(0)->EvaluateAsInt(Result, getASTContext()) && 2683 Result.Val.getInt() != 0) 2684 Diag(TheCall->getBeginLoc(), diag::warn_frame_address) 2685 << ((BuiltinID == Builtin::BI__builtin_return_address) 2686 ? "__builtin_return_address" 2687 : "__builtin_frame_address") 2688 << TheCall->getSourceRange(); 2689 break; 2690 } 2691 2692 case Builtin::BI__builtin_nondeterministic_value: { 2693 if (BuiltinNonDeterministicValue(TheCall)) 2694 return ExprError(); 2695 break; 2696 } 2697 2698 // __builtin_elementwise_abs restricts the element type to signed integers or 2699 // floating point types only. 2700 case Builtin::BI__builtin_elementwise_abs: { 2701 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2702 return ExprError(); 2703 2704 QualType ArgTy = TheCall->getArg(0)->getType(); 2705 QualType EltTy = ArgTy; 2706 2707 if (auto *VecTy = EltTy->getAs<VectorType>()) 2708 EltTy = VecTy->getElementType(); 2709 if (EltTy->isUnsignedIntegerType()) { 2710 Diag(TheCall->getArg(0)->getBeginLoc(), 2711 diag::err_builtin_invalid_arg_type) 2712 << 1 << /* signed integer or float ty*/ 3 << ArgTy; 2713 return ExprError(); 2714 } 2715 break; 2716 } 2717 2718 // These builtins restrict the element type to floating point 2719 // types only. 2720 case Builtin::BI__builtin_elementwise_acos: 2721 case Builtin::BI__builtin_elementwise_asin: 2722 case Builtin::BI__builtin_elementwise_atan: 2723 case Builtin::BI__builtin_elementwise_ceil: 2724 case Builtin::BI__builtin_elementwise_cos: 2725 case Builtin::BI__builtin_elementwise_cosh: 2726 case Builtin::BI__builtin_elementwise_exp: 2727 case Builtin::BI__builtin_elementwise_exp2: 2728 case Builtin::BI__builtin_elementwise_floor: 2729 case Builtin::BI__builtin_elementwise_log: 2730 case Builtin::BI__builtin_elementwise_log2: 2731 case Builtin::BI__builtin_elementwise_log10: 2732 case Builtin::BI__builtin_elementwise_roundeven: 2733 case Builtin::BI__builtin_elementwise_round: 2734 case Builtin::BI__builtin_elementwise_rint: 2735 case Builtin::BI__builtin_elementwise_nearbyint: 2736 case Builtin::BI__builtin_elementwise_sin: 2737 case Builtin::BI__builtin_elementwise_sinh: 2738 case Builtin::BI__builtin_elementwise_sqrt: 2739 case Builtin::BI__builtin_elementwise_tan: 2740 case Builtin::BI__builtin_elementwise_tanh: 2741 case Builtin::BI__builtin_elementwise_trunc: 2742 case Builtin::BI__builtin_elementwise_canonicalize: { 2743 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2744 return ExprError(); 2745 2746 QualType ArgTy = TheCall->getArg(0)->getType(); 2747 if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), 2748 ArgTy, 1)) 2749 return ExprError(); 2750 break; 2751 } 2752 case Builtin::BI__builtin_elementwise_fma: { 2753 if (BuiltinElementwiseTernaryMath(TheCall)) 2754 return ExprError(); 2755 break; 2756 } 2757 2758 // These builtins restrict the element type to floating point 2759 // types only, and take in two arguments. 2760 case Builtin::BI__builtin_elementwise_minimum: 2761 case Builtin::BI__builtin_elementwise_maximum: 2762 case Builtin::BI__builtin_elementwise_atan2: 2763 case Builtin::BI__builtin_elementwise_fmod: 2764 case Builtin::BI__builtin_elementwise_pow: { 2765 if (BuiltinElementwiseMath(TheCall, /*FPOnly=*/true)) 2766 return ExprError(); 2767 break; 2768 } 2769 2770 // These builtins restrict the element type to integer 2771 // types only. 2772 case Builtin::BI__builtin_elementwise_add_sat: 2773 case Builtin::BI__builtin_elementwise_sub_sat: { 2774 if (BuiltinElementwiseMath(TheCall)) 2775 return ExprError(); 2776 2777 const Expr *Arg = TheCall->getArg(0); 2778 QualType ArgTy = Arg->getType(); 2779 QualType EltTy = ArgTy; 2780 2781 if (auto *VecTy = EltTy->getAs<VectorType>()) 2782 EltTy = VecTy->getElementType(); 2783 2784 if (!EltTy->isIntegerType()) { 2785 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2786 << 1 << /* integer ty */ 6 << ArgTy; 2787 return ExprError(); 2788 } 2789 break; 2790 } 2791 2792 case Builtin::BI__builtin_elementwise_min: 2793 case Builtin::BI__builtin_elementwise_max: 2794 if (BuiltinElementwiseMath(TheCall)) 2795 return ExprError(); 2796 break; 2797 case Builtin::BI__builtin_elementwise_popcount: 2798 case Builtin::BI__builtin_elementwise_bitreverse: { 2799 if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) 2800 return ExprError(); 2801 2802 const Expr *Arg = TheCall->getArg(0); 2803 QualType ArgTy = Arg->getType(); 2804 QualType EltTy = ArgTy; 2805 2806 if (auto *VecTy = EltTy->getAs<VectorType>()) 2807 EltTy = VecTy->getElementType(); 2808 2809 if (!EltTy->isIntegerType()) { 2810 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2811 << 1 << /* integer ty */ 6 << ArgTy; 2812 return ExprError(); 2813 } 2814 break; 2815 } 2816 2817 case Builtin::BI__builtin_elementwise_copysign: { 2818 if (checkArgCount(TheCall, 2)) 2819 return ExprError(); 2820 2821 ExprResult Magnitude = UsualUnaryConversions(TheCall->getArg(0)); 2822 ExprResult Sign = UsualUnaryConversions(TheCall->getArg(1)); 2823 if (Magnitude.isInvalid() || Sign.isInvalid()) 2824 return ExprError(); 2825 2826 QualType MagnitudeTy = Magnitude.get()->getType(); 2827 QualType SignTy = Sign.get()->getType(); 2828 if (checkFPMathBuiltinElementType(*this, TheCall->getArg(0)->getBeginLoc(), 2829 MagnitudeTy, 1) || 2830 checkFPMathBuiltinElementType(*this, TheCall->getArg(1)->getBeginLoc(), 2831 SignTy, 2)) { 2832 return ExprError(); 2833 } 2834 2835 if (MagnitudeTy.getCanonicalType() != SignTy.getCanonicalType()) { 2836 return Diag(Sign.get()->getBeginLoc(), 2837 diag::err_typecheck_call_different_arg_types) 2838 << MagnitudeTy << SignTy; 2839 } 2840 2841 TheCall->setArg(0, Magnitude.get()); 2842 TheCall->setArg(1, Sign.get()); 2843 TheCall->setType(Magnitude.get()->getType()); 2844 break; 2845 } 2846 case Builtin::BI__builtin_reduce_max: 2847 case Builtin::BI__builtin_reduce_min: { 2848 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2849 return ExprError(); 2850 2851 const Expr *Arg = TheCall->getArg(0); 2852 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2853 2854 QualType ElTy; 2855 if (TyA) 2856 ElTy = TyA->getElementType(); 2857 else if (Arg->getType()->isSizelessVectorType()) 2858 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2859 2860 if (ElTy.isNull()) { 2861 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2862 << 1 << /* vector ty*/ 4 << Arg->getType(); 2863 return ExprError(); 2864 } 2865 2866 TheCall->setType(ElTy); 2867 break; 2868 } 2869 case Builtin::BI__builtin_reduce_maximum: 2870 case Builtin::BI__builtin_reduce_minimum: { 2871 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2872 return ExprError(); 2873 2874 const Expr *Arg = TheCall->getArg(0); 2875 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2876 2877 QualType ElTy; 2878 if (TyA) 2879 ElTy = TyA->getElementType(); 2880 else if (Arg->getType()->isSizelessVectorType()) 2881 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2882 2883 if (ElTy.isNull() || !ElTy->isFloatingType()) { 2884 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2885 << 1 << /* vector of floating points */ 9 << Arg->getType(); 2886 return ExprError(); 2887 } 2888 2889 TheCall->setType(ElTy); 2890 break; 2891 } 2892 2893 // These builtins support vectors of integers only. 2894 // TODO: ADD/MUL should support floating-point types. 2895 case Builtin::BI__builtin_reduce_add: 2896 case Builtin::BI__builtin_reduce_mul: 2897 case Builtin::BI__builtin_reduce_xor: 2898 case Builtin::BI__builtin_reduce_or: 2899 case Builtin::BI__builtin_reduce_and: { 2900 if (PrepareBuiltinReduceMathOneArgCall(TheCall)) 2901 return ExprError(); 2902 2903 const Expr *Arg = TheCall->getArg(0); 2904 const auto *TyA = Arg->getType()->getAs<VectorType>(); 2905 2906 QualType ElTy; 2907 if (TyA) 2908 ElTy = TyA->getElementType(); 2909 else if (Arg->getType()->isSizelessVectorType()) 2910 ElTy = Arg->getType()->getSizelessVectorEltType(Context); 2911 2912 if (ElTy.isNull() || !ElTy->isIntegerType()) { 2913 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type) 2914 << 1 << /* vector of integers */ 6 << Arg->getType(); 2915 return ExprError(); 2916 } 2917 2918 TheCall->setType(ElTy); 2919 break; 2920 } 2921 2922 case Builtin::BI__builtin_matrix_transpose: 2923 return BuiltinMatrixTranspose(TheCall, TheCallResult); 2924 2925 case Builtin::BI__builtin_matrix_column_major_load: 2926 return BuiltinMatrixColumnMajorLoad(TheCall, TheCallResult); 2927 2928 case Builtin::BI__builtin_matrix_column_major_store: 2929 return BuiltinMatrixColumnMajorStore(TheCall, TheCallResult); 2930 2931 case Builtin::BI__builtin_verbose_trap: 2932 if (!checkBuiltinVerboseTrap(TheCall, *this)) 2933 return ExprError(); 2934 break; 2935 2936 case Builtin::BI__builtin_get_device_side_mangled_name: { 2937 auto Check = [](CallExpr *TheCall) { 2938 if (TheCall->getNumArgs() != 1) 2939 return false; 2940 auto *DRE = dyn_cast<DeclRefExpr>(TheCall->getArg(0)->IgnoreImpCasts()); 2941 if (!DRE) 2942 return false; 2943 auto *D = DRE->getDecl(); 2944 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) 2945 return false; 2946 return D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<CUDADeviceAttr>() || 2947 D->hasAttr<CUDAConstantAttr>() || D->hasAttr<HIPManagedAttr>(); 2948 }; 2949 if (!Check(TheCall)) { 2950 Diag(TheCall->getBeginLoc(), 2951 diag::err_hip_invalid_args_builtin_mangled_name); 2952 return ExprError(); 2953 } 2954 break; 2955 } 2956 case Builtin::BI__builtin_popcountg: 2957 if (BuiltinPopcountg(*this, TheCall)) 2958 return ExprError(); 2959 break; 2960 case Builtin::BI__builtin_clzg: 2961 case Builtin::BI__builtin_ctzg: 2962 if (BuiltinCountZeroBitsGeneric(*this, TheCall)) 2963 return ExprError(); 2964 break; 2965 2966 case Builtin::BI__builtin_allow_runtime_check: { 2967 Expr *Arg = TheCall->getArg(0); 2968 // Check if the argument is a string literal. 2969 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts())) { 2970 Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal) 2971 << Arg->getSourceRange(); 2972 return ExprError(); 2973 } 2974 break; 2975 } 2976 } 2977 2978 if (getLangOpts().HLSL && HLSL().CheckBuiltinFunctionCall(BuiltinID, TheCall)) 2979 return ExprError(); 2980 2981 // Since the target specific builtins for each arch overlap, only check those 2982 // of the arch we are compiling for. 2983 if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) { 2984 if (Context.BuiltinInfo.isAuxBuiltinID(BuiltinID)) { 2985 assert(Context.getAuxTargetInfo() && 2986 "Aux Target Builtin, but not an aux target?"); 2987 2988 if (CheckTSBuiltinFunctionCall( 2989 *Context.getAuxTargetInfo(), 2990 Context.BuiltinInfo.getAuxBuiltinID(BuiltinID), TheCall)) 2991 return ExprError(); 2992 } else { 2993 if (CheckTSBuiltinFunctionCall(Context.getTargetInfo(), BuiltinID, 2994 TheCall)) 2995 return ExprError(); 2996 } 2997 } 2998 2999 return TheCallResult; 3000 } 3001 3002 bool Sema::ValueIsRunOfOnes(CallExpr *TheCall, unsigned ArgNum) { 3003 llvm::APSInt Result; 3004 // We can't check the value of a dependent argument. 3005 Expr *Arg = TheCall->getArg(ArgNum); 3006 if (Arg->isTypeDependent() || Arg->isValueDependent()) 3007 return false; 3008 3009 // Check constant-ness first. 3010 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 3011 return true; 3012 3013 // Check contiguous run of 1s, 0xFF0000FF is also a run of 1s. 3014 if (Result.isShiftedMask() || (~Result).isShiftedMask()) 3015 return false; 3016 3017 return Diag(TheCall->getBeginLoc(), 3018 diag::err_argument_not_contiguous_bit_field) 3019 << ArgNum << Arg->getSourceRange(); 3020 } 3021 3022 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, 3023 bool IsVariadic, FormatStringInfo *FSI) { 3024 if (Format->getFirstArg() == 0) 3025 FSI->ArgPassingKind = FAPK_VAList; 3026 else if (IsVariadic) 3027 FSI->ArgPassingKind = FAPK_Variadic; 3028 else 3029 FSI->ArgPassingKind = FAPK_Fixed; 3030 FSI->FormatIdx = Format->getFormatIdx() - 1; 3031 FSI->FirstDataArg = 3032 FSI->ArgPassingKind == FAPK_VAList ? 0 : Format->getFirstArg() - 1; 3033 3034 // The way the format attribute works in GCC, the implicit this argument 3035 // of member functions is counted. However, it doesn't appear in our own 3036 // lists, so decrement format_idx in that case. 3037 if (IsCXXMember) { 3038 if(FSI->FormatIdx == 0) 3039 return false; 3040 --FSI->FormatIdx; 3041 if (FSI->FirstDataArg != 0) 3042 --FSI->FirstDataArg; 3043 } 3044 return true; 3045 } 3046 3047 /// Checks if a the given expression evaluates to null. 3048 /// 3049 /// Returns true if the value evaluates to null. 3050 static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { 3051 // Treat (smart) pointers constructed from nullptr as null, whether we can 3052 // const-evaluate them or not. 3053 // This must happen first: the smart pointer expr might have _Nonnull type! 3054 if (isa<CXXNullPtrLiteralExpr>( 3055 IgnoreExprNodes(Expr, IgnoreImplicitAsWrittenSingleStep, 3056 IgnoreElidableImplicitConstructorSingleStep))) 3057 return true; 3058 3059 // If the expression has non-null type, it doesn't evaluate to null. 3060 if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability()) { 3061 if (*nullability == NullabilityKind::NonNull) 3062 return false; 3063 } 3064 3065 // As a special case, transparent unions initialized with zero are 3066 // considered null for the purposes of the nonnull attribute. 3067 if (const RecordType *UT = Expr->getType()->getAsUnionType(); 3068 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { 3069 if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(Expr)) 3070 if (const auto *ILE = dyn_cast<InitListExpr>(CLE->getInitializer())) 3071 Expr = ILE->getInit(0); 3072 } 3073 3074 bool Result; 3075 return (!Expr->isValueDependent() && 3076 Expr->EvaluateAsBooleanCondition(Result, S.Context) && 3077 !Result); 3078 } 3079 3080 static void CheckNonNullArgument(Sema &S, 3081 const Expr *ArgExpr, 3082 SourceLocation CallSiteLoc) { 3083 if (CheckNonNullExpr(S, ArgExpr)) 3084 S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr, 3085 S.PDiag(diag::warn_null_arg) 3086 << ArgExpr->getSourceRange()); 3087 } 3088 3089 /// Determine whether the given type has a non-null nullability annotation. 3090 static bool isNonNullType(QualType type) { 3091 if (auto nullability = type->getNullability()) 3092 return *nullability == NullabilityKind::NonNull; 3093 3094 return false; 3095 } 3096 3097 static void CheckNonNullArguments(Sema &S, 3098 const NamedDecl *FDecl, 3099 const FunctionProtoType *Proto, 3100 ArrayRef<const Expr *> Args, 3101 SourceLocation CallSiteLoc) { 3102 assert((FDecl || Proto) && "Need a function declaration or prototype"); 3103 3104 // Already checked by constant evaluator. 3105 if (S.isConstantEvaluatedContext()) 3106 return; 3107 // Check the attributes attached to the method/function itself. 3108 llvm::SmallBitVector NonNullArgs; 3109 if (FDecl) { 3110 // Handle the nonnull attribute on the function/method declaration itself. 3111 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { 3112 if (!NonNull->args_size()) { 3113 // Easy case: all pointer arguments are nonnull. 3114 for (const auto *Arg : Args) 3115 if (S.isValidPointerAttrType(Arg->getType())) 3116 CheckNonNullArgument(S, Arg, CallSiteLoc); 3117 return; 3118 } 3119 3120 for (const ParamIdx &Idx : NonNull->args()) { 3121 unsigned IdxAST = Idx.getASTIndex(); 3122 if (IdxAST >= Args.size()) 3123 continue; 3124 if (NonNullArgs.empty()) 3125 NonNullArgs.resize(Args.size()); 3126 NonNullArgs.set(IdxAST); 3127 } 3128 } 3129 } 3130 3131 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) { 3132 // Handle the nonnull attribute on the parameters of the 3133 // function/method. 3134 ArrayRef<ParmVarDecl*> parms; 3135 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) 3136 parms = FD->parameters(); 3137 else 3138 parms = cast<ObjCMethodDecl>(FDecl)->parameters(); 3139 3140 unsigned ParamIndex = 0; 3141 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); 3142 I != E; ++I, ++ParamIndex) { 3143 const ParmVarDecl *PVD = *I; 3144 if (PVD->hasAttr<NonNullAttr>() || isNonNullType(PVD->getType())) { 3145 if (NonNullArgs.empty()) 3146 NonNullArgs.resize(Args.size()); 3147 3148 NonNullArgs.set(ParamIndex); 3149 } 3150 } 3151 } else { 3152 // If we have a non-function, non-method declaration but no 3153 // function prototype, try to dig out the function prototype. 3154 if (!Proto) { 3155 if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) { 3156 QualType type = VD->getType().getNonReferenceType(); 3157 if (auto pointerType = type->getAs<PointerType>()) 3158 type = pointerType->getPointeeType(); 3159 else if (auto blockType = type->getAs<BlockPointerType>()) 3160 type = blockType->getPointeeType(); 3161 // FIXME: data member pointers? 3162 3163 // Dig out the function prototype, if there is one. 3164 Proto = type->getAs<FunctionProtoType>(); 3165 } 3166 } 3167 3168 // Fill in non-null argument information from the nullability 3169 // information on the parameter types (if we have them). 3170 if (Proto) { 3171 unsigned Index = 0; 3172 for (auto paramType : Proto->getParamTypes()) { 3173 if (isNonNullType(paramType)) { 3174 if (NonNullArgs.empty()) 3175 NonNullArgs.resize(Args.size()); 3176 3177 NonNullArgs.set(Index); 3178 } 3179 3180 ++Index; 3181 } 3182 } 3183 } 3184 3185 // Check for non-null arguments. 3186 for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size(); 3187 ArgIndex != ArgIndexEnd; ++ArgIndex) { 3188 if (NonNullArgs[ArgIndex]) 3189 CheckNonNullArgument(S, Args[ArgIndex], Args[ArgIndex]->getExprLoc()); 3190 } 3191 } 3192 3193 void Sema::CheckArgAlignment(SourceLocation Loc, NamedDecl *FDecl, 3194 StringRef ParamName, QualType ArgTy, 3195 QualType ParamTy) { 3196 3197 // If a function accepts a pointer or reference type 3198 if (!ParamTy->isPointerType() && !ParamTy->isReferenceType()) 3199 return; 3200 3201 // If the parameter is a pointer type, get the pointee type for the 3202 // argument too. If the parameter is a reference type, don't try to get 3203 // the pointee type for the argument. 3204 if (ParamTy->isPointerType()) 3205 ArgTy = ArgTy->getPointeeType(); 3206 3207 // Remove reference or pointer 3208 ParamTy = ParamTy->getPointeeType(); 3209 3210 // Find expected alignment, and the actual alignment of the passed object. 3211 // getTypeAlignInChars requires complete types 3212 if (ArgTy.isNull() || ParamTy->isDependentType() || 3213 ParamTy->isIncompleteType() || ArgTy->isIncompleteType() || 3214 ParamTy->isUndeducedType() || ArgTy->isUndeducedType()) 3215 return; 3216 3217 CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy); 3218 CharUnits ArgAlign = Context.getTypeAlignInChars(ArgTy); 3219 3220 // If the argument is less aligned than the parameter, there is a 3221 // potential alignment issue. 3222 if (ArgAlign < ParamAlign) 3223 Diag(Loc, diag::warn_param_mismatched_alignment) 3224 << (int)ArgAlign.getQuantity() << (int)ParamAlign.getQuantity() 3225 << ParamName << (FDecl != nullptr) << FDecl; 3226 } 3227 3228 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, 3229 const Expr *ThisArg, ArrayRef<const Expr *> Args, 3230 bool IsMemberFunction, SourceLocation Loc, 3231 SourceRange Range, VariadicCallType CallType) { 3232 // FIXME: We should check as much as we can in the template definition. 3233 if (CurContext->isDependentContext()) 3234 return; 3235 3236 // Printf and scanf checking. 3237 llvm::SmallBitVector CheckedVarArgs; 3238 if (FDecl) { 3239 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 3240 // Only create vector if there are format attributes. 3241 CheckedVarArgs.resize(Args.size()); 3242 3243 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, 3244 CheckedVarArgs); 3245 } 3246 } 3247 3248 // Refuse POD arguments that weren't caught by the format string 3249 // checks above. 3250 auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl); 3251 if (CallType != VariadicDoesNotApply && 3252 (!FD || FD->getBuiltinID() != Builtin::BI__noop)) { 3253 unsigned NumParams = Proto ? Proto->getNumParams() 3254 : isa_and_nonnull<FunctionDecl>(FDecl) 3255 ? cast<FunctionDecl>(FDecl)->getNumParams() 3256 : isa_and_nonnull<ObjCMethodDecl>(FDecl) 3257 ? cast<ObjCMethodDecl>(FDecl)->param_size() 3258 : 0; 3259 3260 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { 3261 // Args[ArgIdx] can be null in malformed code. 3262 if (const Expr *Arg = Args[ArgIdx]) { 3263 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) 3264 checkVariadicArgument(Arg, CallType); 3265 } 3266 } 3267 } 3268 3269 if (FDecl || Proto) { 3270 CheckNonNullArguments(*this, FDecl, Proto, Args, Loc); 3271 3272 // Type safety checking. 3273 if (FDecl) { 3274 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) 3275 CheckArgumentWithTypeTag(I, Args, Loc); 3276 } 3277 } 3278 3279 // Check that passed arguments match the alignment of original arguments. 3280 // Try to get the missing prototype from the declaration. 3281 if (!Proto && FDecl) { 3282 const auto *FT = FDecl->getFunctionType(); 3283 if (isa_and_nonnull<FunctionProtoType>(FT)) 3284 Proto = cast<FunctionProtoType>(FDecl->getFunctionType()); 3285 } 3286 if (Proto) { 3287 // For variadic functions, we may have more args than parameters. 3288 // For some K&R functions, we may have less args than parameters. 3289 const auto N = std::min<unsigned>(Proto->getNumParams(), Args.size()); 3290 bool IsScalableRet = Proto->getReturnType()->isSizelessVectorType(); 3291 bool IsScalableArg = false; 3292 for (unsigned ArgIdx = 0; ArgIdx < N; ++ArgIdx) { 3293 // Args[ArgIdx] can be null in malformed code. 3294 if (const Expr *Arg = Args[ArgIdx]) { 3295 if (Arg->containsErrors()) 3296 continue; 3297 3298 if (Context.getTargetInfo().getTriple().isOSAIX() && FDecl && Arg && 3299 FDecl->hasLinkage() && 3300 FDecl->getFormalLinkage() != Linkage::Internal && 3301 CallType == VariadicDoesNotApply) 3302 PPC().checkAIXMemberAlignment((Arg->getExprLoc()), Arg); 3303 3304 QualType ParamTy = Proto->getParamType(ArgIdx); 3305 if (ParamTy->isSizelessVectorType()) 3306 IsScalableArg = true; 3307 QualType ArgTy = Arg->getType(); 3308 CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1), 3309 ArgTy, ParamTy); 3310 } 3311 } 3312 3313 // If the callee has an AArch64 SME attribute to indicate that it is an 3314 // __arm_streaming function, then the caller requires SME to be available. 3315 FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); 3316 if (ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask) { 3317 if (auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) { 3318 llvm::StringMap<bool> CallerFeatureMap; 3319 Context.getFunctionFeatureMap(CallerFeatureMap, CallerFD); 3320 if (!CallerFeatureMap.contains("sme")) 3321 Diag(Loc, diag::err_sme_call_in_non_sme_target); 3322 } else if (!Context.getTargetInfo().hasFeature("sme")) { 3323 Diag(Loc, diag::err_sme_call_in_non_sme_target); 3324 } 3325 } 3326 3327 // If the call requires a streaming-mode change and has scalable vector 3328 // arguments or return values, then warn the user that the streaming and 3329 // non-streaming vector lengths may be different. 3330 const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext); 3331 if (CallerFD && (!FD || !FD->getBuiltinID()) && 3332 (IsScalableArg || IsScalableRet)) { 3333 bool IsCalleeStreaming = 3334 ExtInfo.AArch64SMEAttributes & FunctionType::SME_PStateSMEnabledMask; 3335 bool IsCalleeStreamingCompatible = 3336 ExtInfo.AArch64SMEAttributes & 3337 FunctionType::SME_PStateSMCompatibleMask; 3338 SemaARM::ArmStreamingType CallerFnType = getArmStreamingFnType(CallerFD); 3339 if (!IsCalleeStreamingCompatible && 3340 (CallerFnType == SemaARM::ArmStreamingCompatible || 3341 ((CallerFnType == SemaARM::ArmStreaming) ^ IsCalleeStreaming))) { 3342 if (IsScalableArg) 3343 Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming) 3344 << /*IsArg=*/true; 3345 if (IsScalableRet) 3346 Diag(Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming) 3347 << /*IsArg=*/false; 3348 } 3349 } 3350 3351 FunctionType::ArmStateValue CalleeArmZAState = 3352 FunctionType::getArmZAState(ExtInfo.AArch64SMEAttributes); 3353 FunctionType::ArmStateValue CalleeArmZT0State = 3354 FunctionType::getArmZT0State(ExtInfo.AArch64SMEAttributes); 3355 if (CalleeArmZAState != FunctionType::ARM_None || 3356 CalleeArmZT0State != FunctionType::ARM_None) { 3357 bool CallerHasZAState = false; 3358 bool CallerHasZT0State = false; 3359 if (CallerFD) { 3360 auto *Attr = CallerFD->getAttr<ArmNewAttr>(); 3361 if (Attr && Attr->isNewZA()) 3362 CallerHasZAState = true; 3363 if (Attr && Attr->isNewZT0()) 3364 CallerHasZT0State = true; 3365 if (const auto *FPT = CallerFD->getType()->getAs<FunctionProtoType>()) { 3366 CallerHasZAState |= 3367 FunctionType::getArmZAState( 3368 FPT->getExtProtoInfo().AArch64SMEAttributes) != 3369 FunctionType::ARM_None; 3370 CallerHasZT0State |= 3371 FunctionType::getArmZT0State( 3372 FPT->getExtProtoInfo().AArch64SMEAttributes) != 3373 FunctionType::ARM_None; 3374 } 3375 } 3376 3377 if (CalleeArmZAState != FunctionType::ARM_None && !CallerHasZAState) 3378 Diag(Loc, diag::err_sme_za_call_no_za_state); 3379 3380 if (CalleeArmZT0State != FunctionType::ARM_None && !CallerHasZT0State) 3381 Diag(Loc, diag::err_sme_zt0_call_no_zt0_state); 3382 3383 if (CallerHasZAState && CalleeArmZAState == FunctionType::ARM_None && 3384 CalleeArmZT0State != FunctionType::ARM_None) { 3385 Diag(Loc, diag::err_sme_unimplemented_za_save_restore); 3386 Diag(Loc, diag::note_sme_use_preserves_za); 3387 } 3388 } 3389 } 3390 3391 if (FDecl && FDecl->hasAttr<AllocAlignAttr>()) { 3392 auto *AA = FDecl->getAttr<AllocAlignAttr>(); 3393 const Expr *Arg = Args[AA->getParamIndex().getASTIndex()]; 3394 if (!Arg->isValueDependent()) { 3395 Expr::EvalResult Align; 3396 if (Arg->EvaluateAsInt(Align, Context)) { 3397 const llvm::APSInt &I = Align.Val.getInt(); 3398 if (!I.isPowerOf2()) 3399 Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two) 3400 << Arg->getSourceRange(); 3401 3402 if (I > Sema::MaximumAlignment) 3403 Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great) 3404 << Arg->getSourceRange() << Sema::MaximumAlignment; 3405 } 3406 } 3407 } 3408 3409 if (FD) 3410 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc); 3411 } 3412 3413 void Sema::CheckConstrainedAuto(const AutoType *AutoT, SourceLocation Loc) { 3414 if (ConceptDecl *Decl = AutoT->getTypeConstraintConcept()) { 3415 DiagnoseUseOfDecl(Decl, Loc); 3416 } 3417 } 3418 3419 void Sema::CheckConstructorCall(FunctionDecl *FDecl, QualType ThisType, 3420 ArrayRef<const Expr *> Args, 3421 const FunctionProtoType *Proto, 3422 SourceLocation Loc) { 3423 VariadicCallType CallType = 3424 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; 3425 3426 auto *Ctor = cast<CXXConstructorDecl>(FDecl); 3427 CheckArgAlignment( 3428 Loc, FDecl, "'this'", Context.getPointerType(ThisType), 3429 Context.getPointerType(Ctor->getFunctionObjectParameterType())); 3430 3431 checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true, 3432 Loc, SourceRange(), CallType); 3433 } 3434 3435 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, 3436 const FunctionProtoType *Proto) { 3437 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && 3438 isa<CXXMethodDecl>(FDecl); 3439 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || 3440 IsMemberOperatorCall; 3441 VariadicCallType CallType = getVariadicCallType(FDecl, Proto, 3442 TheCall->getCallee()); 3443 Expr** Args = TheCall->getArgs(); 3444 unsigned NumArgs = TheCall->getNumArgs(); 3445 3446 Expr *ImplicitThis = nullptr; 3447 if (IsMemberOperatorCall && !FDecl->hasCXXExplicitFunctionObjectParameter()) { 3448 // If this is a call to a member operator, hide the first 3449 // argument from checkCall. 3450 // FIXME: Our choice of AST representation here is less than ideal. 3451 ImplicitThis = Args[0]; 3452 ++Args; 3453 --NumArgs; 3454 } else if (IsMemberFunction && !FDecl->isStatic() && 3455 !FDecl->hasCXXExplicitFunctionObjectParameter()) 3456 ImplicitThis = 3457 cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument(); 3458 3459 if (ImplicitThis) { 3460 // ImplicitThis may or may not be a pointer, depending on whether . or -> is 3461 // used. 3462 QualType ThisType = ImplicitThis->getType(); 3463 if (!ThisType->isPointerType()) { 3464 assert(!ThisType->isReferenceType()); 3465 ThisType = Context.getPointerType(ThisType); 3466 } 3467 3468 QualType ThisTypeFromDecl = Context.getPointerType( 3469 cast<CXXMethodDecl>(FDecl)->getFunctionObjectParameterType()); 3470 3471 CheckArgAlignment(TheCall->getRParenLoc(), FDecl, "'this'", ThisType, 3472 ThisTypeFromDecl); 3473 } 3474 3475 checkCall(FDecl, Proto, ImplicitThis, llvm::ArrayRef(Args, NumArgs), 3476 IsMemberFunction, TheCall->getRParenLoc(), 3477 TheCall->getCallee()->getSourceRange(), CallType); 3478 3479 IdentifierInfo *FnInfo = FDecl->getIdentifier(); 3480 // None of the checks below are needed for functions that don't have 3481 // simple names (e.g., C++ conversion functions). 3482 if (!FnInfo) 3483 return false; 3484 3485 // Enforce TCB except for builtin calls, which are always allowed. 3486 if (FDecl->getBuiltinID() == 0) 3487 CheckTCBEnforcement(TheCall->getExprLoc(), FDecl); 3488 3489 CheckAbsoluteValueFunction(TheCall, FDecl); 3490 CheckMaxUnsignedZero(TheCall, FDecl); 3491 CheckInfNaNFunction(TheCall, FDecl); 3492 3493 if (getLangOpts().ObjC) 3494 ObjC().DiagnoseCStringFormatDirectiveInCFAPI(FDecl, Args, NumArgs); 3495 3496 unsigned CMId = FDecl->getMemoryFunctionKind(); 3497 3498 // Handle memory setting and copying functions. 3499 switch (CMId) { 3500 case 0: 3501 return false; 3502 case Builtin::BIstrlcpy: // fallthrough 3503 case Builtin::BIstrlcat: 3504 CheckStrlcpycatArguments(TheCall, FnInfo); 3505 break; 3506 case Builtin::BIstrncat: 3507 CheckStrncatArguments(TheCall, FnInfo); 3508 break; 3509 case Builtin::BIfree: 3510 CheckFreeArguments(TheCall); 3511 break; 3512 default: 3513 CheckMemaccessArguments(TheCall, CMId, FnInfo); 3514 } 3515 3516 return false; 3517 } 3518 3519 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, 3520 const FunctionProtoType *Proto) { 3521 QualType Ty; 3522 if (const auto *V = dyn_cast<VarDecl>(NDecl)) 3523 Ty = V->getType().getNonReferenceType(); 3524 else if (const auto *F = dyn_cast<FieldDecl>(NDecl)) 3525 Ty = F->getType().getNonReferenceType(); 3526 else 3527 return false; 3528 3529 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() && 3530 !Ty->isFunctionProtoType()) 3531 return false; 3532 3533 VariadicCallType CallType; 3534 if (!Proto || !Proto->isVariadic()) { 3535 CallType = VariadicDoesNotApply; 3536 } else if (Ty->isBlockPointerType()) { 3537 CallType = VariadicBlock; 3538 } else { // Ty->isFunctionPointerType() 3539 CallType = VariadicFunction; 3540 } 3541 3542 checkCall(NDecl, Proto, /*ThisArg=*/nullptr, 3543 llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 3544 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 3545 TheCall->getCallee()->getSourceRange(), CallType); 3546 3547 return false; 3548 } 3549 3550 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { 3551 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, 3552 TheCall->getCallee()); 3553 checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr, 3554 llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 3555 /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 3556 TheCall->getCallee()->getSourceRange(), CallType); 3557 3558 return false; 3559 } 3560 3561 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { 3562 if (!llvm::isValidAtomicOrderingCABI(Ordering)) 3563 return false; 3564 3565 auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering; 3566 switch (Op) { 3567 case AtomicExpr::AO__c11_atomic_init: 3568 case AtomicExpr::AO__opencl_atomic_init: 3569 llvm_unreachable("There is no ordering argument for an init"); 3570 3571 case AtomicExpr::AO__c11_atomic_load: 3572 case AtomicExpr::AO__opencl_atomic_load: 3573 case AtomicExpr::AO__hip_atomic_load: 3574 case AtomicExpr::AO__atomic_load_n: 3575 case AtomicExpr::AO__atomic_load: 3576 case AtomicExpr::AO__scoped_atomic_load_n: 3577 case AtomicExpr::AO__scoped_atomic_load: 3578 return OrderingCABI != llvm::AtomicOrderingCABI::release && 3579 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 3580 3581 case AtomicExpr::AO__c11_atomic_store: 3582 case AtomicExpr::AO__opencl_atomic_store: 3583 case AtomicExpr::AO__hip_atomic_store: 3584 case AtomicExpr::AO__atomic_store: 3585 case AtomicExpr::AO__atomic_store_n: 3586 case AtomicExpr::AO__scoped_atomic_store: 3587 case AtomicExpr::AO__scoped_atomic_store_n: 3588 return OrderingCABI != llvm::AtomicOrderingCABI::consume && 3589 OrderingCABI != llvm::AtomicOrderingCABI::acquire && 3590 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel; 3591 3592 default: 3593 return true; 3594 } 3595 } 3596 3597 ExprResult Sema::AtomicOpsOverloaded(ExprResult TheCallResult, 3598 AtomicExpr::AtomicOp Op) { 3599 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); 3600 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 3601 MultiExprArg Args{TheCall->getArgs(), TheCall->getNumArgs()}; 3602 return BuildAtomicExpr({TheCall->getBeginLoc(), TheCall->getEndLoc()}, 3603 DRE->getSourceRange(), TheCall->getRParenLoc(), Args, 3604 Op); 3605 } 3606 3607 ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, 3608 SourceLocation RParenLoc, MultiExprArg Args, 3609 AtomicExpr::AtomicOp Op, 3610 AtomicArgumentOrder ArgOrder) { 3611 // All the non-OpenCL operations take one of the following forms. 3612 // The OpenCL operations take the __c11 forms with one extra argument for 3613 // synchronization scope. 3614 enum { 3615 // C __c11_atomic_init(A *, C) 3616 Init, 3617 3618 // C __c11_atomic_load(A *, int) 3619 Load, 3620 3621 // void __atomic_load(A *, CP, int) 3622 LoadCopy, 3623 3624 // void __atomic_store(A *, CP, int) 3625 Copy, 3626 3627 // C __c11_atomic_add(A *, M, int) 3628 Arithmetic, 3629 3630 // C __atomic_exchange_n(A *, CP, int) 3631 Xchg, 3632 3633 // void __atomic_exchange(A *, C *, CP, int) 3634 GNUXchg, 3635 3636 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) 3637 C11CmpXchg, 3638 3639 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) 3640 GNUCmpXchg 3641 } Form = Init; 3642 3643 const unsigned NumForm = GNUCmpXchg + 1; 3644 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 }; 3645 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 }; 3646 // where: 3647 // C is an appropriate type, 3648 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, 3649 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, 3650 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and 3651 // the int parameters are for orderings. 3652 3653 static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm 3654 && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm, 3655 "need to update code for modified forms"); 3656 static_assert(AtomicExpr::AO__atomic_add_fetch == 0 && 3657 AtomicExpr::AO__atomic_xor_fetch + 1 == 3658 AtomicExpr::AO__c11_atomic_compare_exchange_strong, 3659 "need to update code for modified C11 atomics"); 3660 bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_compare_exchange_strong && 3661 Op <= AtomicExpr::AO__opencl_atomic_store; 3662 bool IsHIP = Op >= AtomicExpr::AO__hip_atomic_compare_exchange_strong && 3663 Op <= AtomicExpr::AO__hip_atomic_store; 3664 bool IsScoped = Op >= AtomicExpr::AO__scoped_atomic_add_fetch && 3665 Op <= AtomicExpr::AO__scoped_atomic_xor_fetch; 3666 bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_compare_exchange_strong && 3667 Op <= AtomicExpr::AO__c11_atomic_store) || 3668 IsOpenCL; 3669 bool IsN = Op == AtomicExpr::AO__atomic_load_n || 3670 Op == AtomicExpr::AO__atomic_store_n || 3671 Op == AtomicExpr::AO__atomic_exchange_n || 3672 Op == AtomicExpr::AO__atomic_compare_exchange_n || 3673 Op == AtomicExpr::AO__scoped_atomic_load_n || 3674 Op == AtomicExpr::AO__scoped_atomic_store_n || 3675 Op == AtomicExpr::AO__scoped_atomic_exchange_n || 3676 Op == AtomicExpr::AO__scoped_atomic_compare_exchange_n; 3677 // Bit mask for extra allowed value types other than integers for atomic 3678 // arithmetic operations. Add/sub allow pointer and floating point. Min/max 3679 // allow floating point. 3680 enum ArithOpExtraValueType { 3681 AOEVT_None = 0, 3682 AOEVT_Pointer = 1, 3683 AOEVT_FP = 2, 3684 }; 3685 unsigned ArithAllows = AOEVT_None; 3686 3687 switch (Op) { 3688 case AtomicExpr::AO__c11_atomic_init: 3689 case AtomicExpr::AO__opencl_atomic_init: 3690 Form = Init; 3691 break; 3692 3693 case AtomicExpr::AO__c11_atomic_load: 3694 case AtomicExpr::AO__opencl_atomic_load: 3695 case AtomicExpr::AO__hip_atomic_load: 3696 case AtomicExpr::AO__atomic_load_n: 3697 case AtomicExpr::AO__scoped_atomic_load_n: 3698 Form = Load; 3699 break; 3700 3701 case AtomicExpr::AO__atomic_load: 3702 case AtomicExpr::AO__scoped_atomic_load: 3703 Form = LoadCopy; 3704 break; 3705 3706 case AtomicExpr::AO__c11_atomic_store: 3707 case AtomicExpr::AO__opencl_atomic_store: 3708 case AtomicExpr::AO__hip_atomic_store: 3709 case AtomicExpr::AO__atomic_store: 3710 case AtomicExpr::AO__atomic_store_n: 3711 case AtomicExpr::AO__scoped_atomic_store: 3712 case AtomicExpr::AO__scoped_atomic_store_n: 3713 Form = Copy; 3714 break; 3715 case AtomicExpr::AO__atomic_fetch_add: 3716 case AtomicExpr::AO__atomic_fetch_sub: 3717 case AtomicExpr::AO__atomic_add_fetch: 3718 case AtomicExpr::AO__atomic_sub_fetch: 3719 case AtomicExpr::AO__scoped_atomic_fetch_add: 3720 case AtomicExpr::AO__scoped_atomic_fetch_sub: 3721 case AtomicExpr::AO__scoped_atomic_add_fetch: 3722 case AtomicExpr::AO__scoped_atomic_sub_fetch: 3723 case AtomicExpr::AO__c11_atomic_fetch_add: 3724 case AtomicExpr::AO__c11_atomic_fetch_sub: 3725 case AtomicExpr::AO__opencl_atomic_fetch_add: 3726 case AtomicExpr::AO__opencl_atomic_fetch_sub: 3727 case AtomicExpr::AO__hip_atomic_fetch_add: 3728 case AtomicExpr::AO__hip_atomic_fetch_sub: 3729 ArithAllows = AOEVT_Pointer | AOEVT_FP; 3730 Form = Arithmetic; 3731 break; 3732 case AtomicExpr::AO__atomic_fetch_max: 3733 case AtomicExpr::AO__atomic_fetch_min: 3734 case AtomicExpr::AO__atomic_max_fetch: 3735 case AtomicExpr::AO__atomic_min_fetch: 3736 case AtomicExpr::AO__scoped_atomic_fetch_max: 3737 case AtomicExpr::AO__scoped_atomic_fetch_min: 3738 case AtomicExpr::AO__scoped_atomic_max_fetch: 3739 case AtomicExpr::AO__scoped_atomic_min_fetch: 3740 case AtomicExpr::AO__c11_atomic_fetch_max: 3741 case AtomicExpr::AO__c11_atomic_fetch_min: 3742 case AtomicExpr::AO__opencl_atomic_fetch_max: 3743 case AtomicExpr::AO__opencl_atomic_fetch_min: 3744 case AtomicExpr::AO__hip_atomic_fetch_max: 3745 case AtomicExpr::AO__hip_atomic_fetch_min: 3746 ArithAllows = AOEVT_FP; 3747 Form = Arithmetic; 3748 break; 3749 case AtomicExpr::AO__c11_atomic_fetch_and: 3750 case AtomicExpr::AO__c11_atomic_fetch_or: 3751 case AtomicExpr::AO__c11_atomic_fetch_xor: 3752 case AtomicExpr::AO__hip_atomic_fetch_and: 3753 case AtomicExpr::AO__hip_atomic_fetch_or: 3754 case AtomicExpr::AO__hip_atomic_fetch_xor: 3755 case AtomicExpr::AO__c11_atomic_fetch_nand: 3756 case AtomicExpr::AO__opencl_atomic_fetch_and: 3757 case AtomicExpr::AO__opencl_atomic_fetch_or: 3758 case AtomicExpr::AO__opencl_atomic_fetch_xor: 3759 case AtomicExpr::AO__atomic_fetch_and: 3760 case AtomicExpr::AO__atomic_fetch_or: 3761 case AtomicExpr::AO__atomic_fetch_xor: 3762 case AtomicExpr::AO__atomic_fetch_nand: 3763 case AtomicExpr::AO__atomic_and_fetch: 3764 case AtomicExpr::AO__atomic_or_fetch: 3765 case AtomicExpr::AO__atomic_xor_fetch: 3766 case AtomicExpr::AO__atomic_nand_fetch: 3767 case AtomicExpr::AO__scoped_atomic_fetch_and: 3768 case AtomicExpr::AO__scoped_atomic_fetch_or: 3769 case AtomicExpr::AO__scoped_atomic_fetch_xor: 3770 case AtomicExpr::AO__scoped_atomic_fetch_nand: 3771 case AtomicExpr::AO__scoped_atomic_and_fetch: 3772 case AtomicExpr::AO__scoped_atomic_or_fetch: 3773 case AtomicExpr::AO__scoped_atomic_xor_fetch: 3774 case AtomicExpr::AO__scoped_atomic_nand_fetch: 3775 Form = Arithmetic; 3776 break; 3777 3778 case AtomicExpr::AO__c11_atomic_exchange: 3779 case AtomicExpr::AO__hip_atomic_exchange: 3780 case AtomicExpr::AO__opencl_atomic_exchange: 3781 case AtomicExpr::AO__atomic_exchange_n: 3782 case AtomicExpr::AO__scoped_atomic_exchange_n: 3783 Form = Xchg; 3784 break; 3785 3786 case AtomicExpr::AO__atomic_exchange: 3787 case AtomicExpr::AO__scoped_atomic_exchange: 3788 Form = GNUXchg; 3789 break; 3790 3791 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 3792 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 3793 case AtomicExpr::AO__hip_atomic_compare_exchange_strong: 3794 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong: 3795 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak: 3796 case AtomicExpr::AO__hip_atomic_compare_exchange_weak: 3797 Form = C11CmpXchg; 3798 break; 3799 3800 case AtomicExpr::AO__atomic_compare_exchange: 3801 case AtomicExpr::AO__atomic_compare_exchange_n: 3802 case AtomicExpr::AO__scoped_atomic_compare_exchange: 3803 case AtomicExpr::AO__scoped_atomic_compare_exchange_n: 3804 Form = GNUCmpXchg; 3805 break; 3806 } 3807 3808 unsigned AdjustedNumArgs = NumArgs[Form]; 3809 if ((IsOpenCL || IsHIP || IsScoped) && 3810 Op != AtomicExpr::AO__opencl_atomic_init) 3811 ++AdjustedNumArgs; 3812 // Check we have the right number of arguments. 3813 if (Args.size() < AdjustedNumArgs) { 3814 Diag(CallRange.getEnd(), diag::err_typecheck_call_too_few_args) 3815 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) 3816 << /*is non object*/ 0 << ExprRange; 3817 return ExprError(); 3818 } else if (Args.size() > AdjustedNumArgs) { 3819 Diag(Args[AdjustedNumArgs]->getBeginLoc(), 3820 diag::err_typecheck_call_too_many_args) 3821 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size()) 3822 << /*is non object*/ 0 << ExprRange; 3823 return ExprError(); 3824 } 3825 3826 // Inspect the first argument of the atomic operation. 3827 Expr *Ptr = Args[0]; 3828 ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr); 3829 if (ConvertedPtr.isInvalid()) 3830 return ExprError(); 3831 3832 Ptr = ConvertedPtr.get(); 3833 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); 3834 if (!pointerType) { 3835 Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) 3836 << Ptr->getType() << 0 << Ptr->getSourceRange(); 3837 return ExprError(); 3838 } 3839 3840 // For a __c11 builtin, this should be a pointer to an _Atomic type. 3841 QualType AtomTy = pointerType->getPointeeType(); // 'A' 3842 QualType ValType = AtomTy; // 'C' 3843 if (IsC11) { 3844 if (!AtomTy->isAtomicType()) { 3845 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic) 3846 << Ptr->getType() << Ptr->getSourceRange(); 3847 return ExprError(); 3848 } 3849 if ((Form != Load && Form != LoadCopy && AtomTy.isConstQualified()) || 3850 AtomTy.getAddressSpace() == LangAS::opencl_constant) { 3851 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_atomic) 3852 << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType() 3853 << Ptr->getSourceRange(); 3854 return ExprError(); 3855 } 3856 ValType = AtomTy->castAs<AtomicType>()->getValueType(); 3857 } else if (Form != Load && Form != LoadCopy) { 3858 if (ValType.isConstQualified()) { 3859 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_const_pointer) 3860 << Ptr->getType() << Ptr->getSourceRange(); 3861 return ExprError(); 3862 } 3863 } 3864 3865 // Pointer to object of size zero is not allowed. 3866 if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy, 3867 diag::err_incomplete_type)) 3868 return ExprError(); 3869 if (Context.getTypeInfoInChars(AtomTy).Width.isZero()) { 3870 Diag(ExprRange.getBegin(), diag::err_atomic_builtin_must_be_pointer) 3871 << Ptr->getType() << 1 << Ptr->getSourceRange(); 3872 return ExprError(); 3873 } 3874 3875 // For an arithmetic operation, the implied arithmetic must be well-formed. 3876 if (Form == Arithmetic) { 3877 // GCC does not enforce these rules for GNU atomics, but we do to help catch 3878 // trivial type errors. 3879 auto IsAllowedValueType = [&](QualType ValType, 3880 unsigned AllowedType) -> bool { 3881 if (ValType->isIntegerType()) 3882 return true; 3883 if (ValType->isPointerType()) 3884 return AllowedType & AOEVT_Pointer; 3885 if (!(ValType->isFloatingType() && (AllowedType & AOEVT_FP))) 3886 return false; 3887 // LLVM Parser does not allow atomicrmw with x86_fp80 type. 3888 if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) && 3889 &Context.getTargetInfo().getLongDoubleFormat() == 3890 &llvm::APFloat::x87DoubleExtended()) 3891 return false; 3892 return true; 3893 }; 3894 if (!IsAllowedValueType(ValType, ArithAllows)) { 3895 auto DID = ArithAllows & AOEVT_FP 3896 ? (ArithAllows & AOEVT_Pointer 3897 ? diag::err_atomic_op_needs_atomic_int_ptr_or_fp 3898 : diag::err_atomic_op_needs_atomic_int_or_fp) 3899 : diag::err_atomic_op_needs_atomic_int; 3900 Diag(ExprRange.getBegin(), DID) 3901 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 3902 return ExprError(); 3903 } 3904 if (IsC11 && ValType->isPointerType() && 3905 RequireCompleteType(Ptr->getBeginLoc(), ValType->getPointeeType(), 3906 diag::err_incomplete_type)) { 3907 return ExprError(); 3908 } 3909 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { 3910 // For __atomic_*_n operations, the value type must be a scalar integral or 3911 // pointer type which is 1, 2, 4, 8 or 16 bytes in length. 3912 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr) 3913 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 3914 return ExprError(); 3915 } 3916 3917 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && 3918 !AtomTy->isScalarType()) { 3919 // For GNU atomics, require a trivially-copyable type. This is not part of 3920 // the GNU atomics specification but we enforce it for consistency with 3921 // other atomics which generally all require a trivially-copyable type. This 3922 // is because atomics just copy bits. 3923 Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_trivial_copy) 3924 << Ptr->getType() << Ptr->getSourceRange(); 3925 return ExprError(); 3926 } 3927 3928 switch (ValType.getObjCLifetime()) { 3929 case Qualifiers::OCL_None: 3930 case Qualifiers::OCL_ExplicitNone: 3931 // okay 3932 break; 3933 3934 case Qualifiers::OCL_Weak: 3935 case Qualifiers::OCL_Strong: 3936 case Qualifiers::OCL_Autoreleasing: 3937 // FIXME: Can this happen? By this point, ValType should be known 3938 // to be trivially copyable. 3939 Diag(ExprRange.getBegin(), diag::err_arc_atomic_ownership) 3940 << ValType << Ptr->getSourceRange(); 3941 return ExprError(); 3942 } 3943 3944 // All atomic operations have an overload which takes a pointer to a volatile 3945 // 'A'. We shouldn't let the volatile-ness of the pointee-type inject itself 3946 // into the result or the other operands. Similarly atomic_load takes a 3947 // pointer to a const 'A'. 3948 ValType.removeLocalVolatile(); 3949 ValType.removeLocalConst(); 3950 QualType ResultType = ValType; 3951 if (Form == Copy || Form == LoadCopy || Form == GNUXchg || 3952 Form == Init) 3953 ResultType = Context.VoidTy; 3954 else if (Form == C11CmpXchg || Form == GNUCmpXchg) 3955 ResultType = Context.BoolTy; 3956 3957 // The type of a parameter passed 'by value'. In the GNU atomics, such 3958 // arguments are actually passed as pointers. 3959 QualType ByValType = ValType; // 'CP' 3960 bool IsPassedByAddress = false; 3961 if (!IsC11 && !IsHIP && !IsN) { 3962 ByValType = Ptr->getType(); 3963 IsPassedByAddress = true; 3964 } 3965 3966 SmallVector<Expr *, 5> APIOrderedArgs; 3967 if (ArgOrder == Sema::AtomicArgumentOrder::AST) { 3968 APIOrderedArgs.push_back(Args[0]); 3969 switch (Form) { 3970 case Init: 3971 case Load: 3972 APIOrderedArgs.push_back(Args[1]); // Val1/Order 3973 break; 3974 case LoadCopy: 3975 case Copy: 3976 case Arithmetic: 3977 case Xchg: 3978 APIOrderedArgs.push_back(Args[2]); // Val1 3979 APIOrderedArgs.push_back(Args[1]); // Order 3980 break; 3981 case GNUXchg: 3982 APIOrderedArgs.push_back(Args[2]); // Val1 3983 APIOrderedArgs.push_back(Args[3]); // Val2 3984 APIOrderedArgs.push_back(Args[1]); // Order 3985 break; 3986 case C11CmpXchg: 3987 APIOrderedArgs.push_back(Args[2]); // Val1 3988 APIOrderedArgs.push_back(Args[4]); // Val2 3989 APIOrderedArgs.push_back(Args[1]); // Order 3990 APIOrderedArgs.push_back(Args[3]); // OrderFail 3991 break; 3992 case GNUCmpXchg: 3993 APIOrderedArgs.push_back(Args[2]); // Val1 3994 APIOrderedArgs.push_back(Args[4]); // Val2 3995 APIOrderedArgs.push_back(Args[5]); // Weak 3996 APIOrderedArgs.push_back(Args[1]); // Order 3997 APIOrderedArgs.push_back(Args[3]); // OrderFail 3998 break; 3999 } 4000 } else 4001 APIOrderedArgs.append(Args.begin(), Args.end()); 4002 4003 // The first argument's non-CV pointer type is used to deduce the type of 4004 // subsequent arguments, except for: 4005 // - weak flag (always converted to bool) 4006 // - memory order (always converted to int) 4007 // - scope (always converted to int) 4008 for (unsigned i = 0; i != APIOrderedArgs.size(); ++i) { 4009 QualType Ty; 4010 if (i < NumVals[Form] + 1) { 4011 switch (i) { 4012 case 0: 4013 // The first argument is always a pointer. It has a fixed type. 4014 // It is always dereferenced, a nullptr is undefined. 4015 CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); 4016 // Nothing else to do: we already know all we want about this pointer. 4017 continue; 4018 case 1: 4019 // The second argument is the non-atomic operand. For arithmetic, this 4020 // is always passed by value, and for a compare_exchange it is always 4021 // passed by address. For the rest, GNU uses by-address and C11 uses 4022 // by-value. 4023 assert(Form != Load); 4024 if (Form == Arithmetic && ValType->isPointerType()) 4025 Ty = Context.getPointerDiffType(); 4026 else if (Form == Init || Form == Arithmetic) 4027 Ty = ValType; 4028 else if (Form == Copy || Form == Xchg) { 4029 if (IsPassedByAddress) { 4030 // The value pointer is always dereferenced, a nullptr is undefined. 4031 CheckNonNullArgument(*this, APIOrderedArgs[i], 4032 ExprRange.getBegin()); 4033 } 4034 Ty = ByValType; 4035 } else { 4036 Expr *ValArg = APIOrderedArgs[i]; 4037 // The value pointer is always dereferenced, a nullptr is undefined. 4038 CheckNonNullArgument(*this, ValArg, ExprRange.getBegin()); 4039 LangAS AS = LangAS::Default; 4040 // Keep address space of non-atomic pointer type. 4041 if (const PointerType *PtrTy = 4042 ValArg->getType()->getAs<PointerType>()) { 4043 AS = PtrTy->getPointeeType().getAddressSpace(); 4044 } 4045 Ty = Context.getPointerType( 4046 Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); 4047 } 4048 break; 4049 case 2: 4050 // The third argument to compare_exchange / GNU exchange is the desired 4051 // value, either by-value (for the C11 and *_n variant) or as a pointer. 4052 if (IsPassedByAddress) 4053 CheckNonNullArgument(*this, APIOrderedArgs[i], ExprRange.getBegin()); 4054 Ty = ByValType; 4055 break; 4056 case 3: 4057 // The fourth argument to GNU compare_exchange is a 'weak' flag. 4058 Ty = Context.BoolTy; 4059 break; 4060 } 4061 } else { 4062 // The order(s) and scope are always converted to int. 4063 Ty = Context.IntTy; 4064 } 4065 4066 InitializedEntity Entity = 4067 InitializedEntity::InitializeParameter(Context, Ty, false); 4068 ExprResult Arg = APIOrderedArgs[i]; 4069 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4070 if (Arg.isInvalid()) 4071 return true; 4072 APIOrderedArgs[i] = Arg.get(); 4073 } 4074 4075 // Permute the arguments into a 'consistent' order. 4076 SmallVector<Expr*, 5> SubExprs; 4077 SubExprs.push_back(Ptr); 4078 switch (Form) { 4079 case Init: 4080 // Note, AtomicExpr::getVal1() has a special case for this atomic. 4081 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4082 break; 4083 case Load: 4084 SubExprs.push_back(APIOrderedArgs[1]); // Order 4085 break; 4086 case LoadCopy: 4087 case Copy: 4088 case Arithmetic: 4089 case Xchg: 4090 SubExprs.push_back(APIOrderedArgs[2]); // Order 4091 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4092 break; 4093 case GNUXchg: 4094 // Note, AtomicExpr::getVal2() has a special case for this atomic. 4095 SubExprs.push_back(APIOrderedArgs[3]); // Order 4096 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4097 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4098 break; 4099 case C11CmpXchg: 4100 SubExprs.push_back(APIOrderedArgs[3]); // Order 4101 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4102 SubExprs.push_back(APIOrderedArgs[4]); // OrderFail 4103 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4104 break; 4105 case GNUCmpXchg: 4106 SubExprs.push_back(APIOrderedArgs[4]); // Order 4107 SubExprs.push_back(APIOrderedArgs[1]); // Val1 4108 SubExprs.push_back(APIOrderedArgs[5]); // OrderFail 4109 SubExprs.push_back(APIOrderedArgs[2]); // Val2 4110 SubExprs.push_back(APIOrderedArgs[3]); // Weak 4111 break; 4112 } 4113 4114 // If the memory orders are constants, check they are valid. 4115 if (SubExprs.size() >= 2 && Form != Init) { 4116 std::optional<llvm::APSInt> Success = 4117 SubExprs[1]->getIntegerConstantExpr(Context); 4118 if (Success && !isValidOrderingForOp(Success->getSExtValue(), Op)) { 4119 Diag(SubExprs[1]->getBeginLoc(), 4120 diag::warn_atomic_op_has_invalid_memory_order) 4121 << /*success=*/(Form == C11CmpXchg || Form == GNUCmpXchg) 4122 << SubExprs[1]->getSourceRange(); 4123 } 4124 if (SubExprs.size() >= 5) { 4125 if (std::optional<llvm::APSInt> Failure = 4126 SubExprs[3]->getIntegerConstantExpr(Context)) { 4127 if (!llvm::is_contained( 4128 {llvm::AtomicOrderingCABI::relaxed, 4129 llvm::AtomicOrderingCABI::consume, 4130 llvm::AtomicOrderingCABI::acquire, 4131 llvm::AtomicOrderingCABI::seq_cst}, 4132 (llvm::AtomicOrderingCABI)Failure->getSExtValue())) { 4133 Diag(SubExprs[3]->getBeginLoc(), 4134 diag::warn_atomic_op_has_invalid_memory_order) 4135 << /*failure=*/2 << SubExprs[3]->getSourceRange(); 4136 } 4137 } 4138 } 4139 } 4140 4141 if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) { 4142 auto *Scope = Args[Args.size() - 1]; 4143 if (std::optional<llvm::APSInt> Result = 4144 Scope->getIntegerConstantExpr(Context)) { 4145 if (!ScopeModel->isValid(Result->getZExtValue())) 4146 Diag(Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope) 4147 << Scope->getSourceRange(); 4148 } 4149 SubExprs.push_back(Scope); 4150 } 4151 4152 AtomicExpr *AE = new (Context) 4153 AtomicExpr(ExprRange.getBegin(), SubExprs, ResultType, Op, RParenLoc); 4154 4155 if ((Op == AtomicExpr::AO__c11_atomic_load || 4156 Op == AtomicExpr::AO__c11_atomic_store || 4157 Op == AtomicExpr::AO__opencl_atomic_load || 4158 Op == AtomicExpr::AO__hip_atomic_load || 4159 Op == AtomicExpr::AO__opencl_atomic_store || 4160 Op == AtomicExpr::AO__hip_atomic_store) && 4161 Context.AtomicUsesUnsupportedLibcall(AE)) 4162 Diag(AE->getBeginLoc(), diag::err_atomic_load_store_uses_lib) 4163 << ((Op == AtomicExpr::AO__c11_atomic_load || 4164 Op == AtomicExpr::AO__opencl_atomic_load || 4165 Op == AtomicExpr::AO__hip_atomic_load) 4166 ? 0 4167 : 1); 4168 4169 if (ValType->isBitIntType()) { 4170 Diag(Ptr->getExprLoc(), diag::err_atomic_builtin_bit_int_prohibit); 4171 return ExprError(); 4172 } 4173 4174 return AE; 4175 } 4176 4177 /// checkBuiltinArgument - Given a call to a builtin function, perform 4178 /// normal type-checking on the given argument, updating the call in 4179 /// place. This is useful when a builtin function requires custom 4180 /// type-checking for some of its arguments but not necessarily all of 4181 /// them. 4182 /// 4183 /// Returns true on error. 4184 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { 4185 FunctionDecl *Fn = E->getDirectCallee(); 4186 assert(Fn && "builtin call without direct callee!"); 4187 4188 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); 4189 InitializedEntity Entity = 4190 InitializedEntity::InitializeParameter(S.Context, Param); 4191 4192 ExprResult Arg = E->getArg(ArgIndex); 4193 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 4194 if (Arg.isInvalid()) 4195 return true; 4196 4197 E->setArg(ArgIndex, Arg.get()); 4198 return false; 4199 } 4200 4201 ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) { 4202 CallExpr *TheCall = static_cast<CallExpr *>(TheCallResult.get()); 4203 Expr *Callee = TheCall->getCallee(); 4204 DeclRefExpr *DRE = cast<DeclRefExpr>(Callee->IgnoreParenCasts()); 4205 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4206 4207 // Ensure that we have at least one argument to do type inference from. 4208 if (TheCall->getNumArgs() < 1) { 4209 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4210 << 0 << 1 << TheCall->getNumArgs() << /*is non object*/ 0 4211 << Callee->getSourceRange(); 4212 return ExprError(); 4213 } 4214 4215 // Inspect the first argument of the atomic builtin. This should always be 4216 // a pointer type, whose element is an integral scalar or pointer type. 4217 // Because it is a pointer type, we don't have to worry about any implicit 4218 // casts here. 4219 // FIXME: We don't allow floating point scalars as input. 4220 Expr *FirstArg = TheCall->getArg(0); 4221 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); 4222 if (FirstArgResult.isInvalid()) 4223 return ExprError(); 4224 FirstArg = FirstArgResult.get(); 4225 TheCall->setArg(0, FirstArg); 4226 4227 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); 4228 if (!pointerType) { 4229 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer) 4230 << FirstArg->getType() << 0 << FirstArg->getSourceRange(); 4231 return ExprError(); 4232 } 4233 4234 QualType ValType = pointerType->getPointeeType(); 4235 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4236 !ValType->isBlockPointerType()) { 4237 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr) 4238 << FirstArg->getType() << 0 << FirstArg->getSourceRange(); 4239 return ExprError(); 4240 } 4241 4242 if (ValType.isConstQualified()) { 4243 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_cannot_be_const) 4244 << FirstArg->getType() << FirstArg->getSourceRange(); 4245 return ExprError(); 4246 } 4247 4248 switch (ValType.getObjCLifetime()) { 4249 case Qualifiers::OCL_None: 4250 case Qualifiers::OCL_ExplicitNone: 4251 // okay 4252 break; 4253 4254 case Qualifiers::OCL_Weak: 4255 case Qualifiers::OCL_Strong: 4256 case Qualifiers::OCL_Autoreleasing: 4257 Diag(DRE->getBeginLoc(), diag::err_arc_atomic_ownership) 4258 << ValType << FirstArg->getSourceRange(); 4259 return ExprError(); 4260 } 4261 4262 // Strip any qualifiers off ValType. 4263 ValType = ValType.getUnqualifiedType(); 4264 4265 // The majority of builtins return a value, but a few have special return 4266 // types, so allow them to override appropriately below. 4267 QualType ResultType = ValType; 4268 4269 // We need to figure out which concrete builtin this maps onto. For example, 4270 // __sync_fetch_and_add with a 2 byte object turns into 4271 // __sync_fetch_and_add_2. 4272 #define BUILTIN_ROW(x) \ 4273 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ 4274 Builtin::BI##x##_8, Builtin::BI##x##_16 } 4275 4276 static const unsigned BuiltinIndices[][5] = { 4277 BUILTIN_ROW(__sync_fetch_and_add), 4278 BUILTIN_ROW(__sync_fetch_and_sub), 4279 BUILTIN_ROW(__sync_fetch_and_or), 4280 BUILTIN_ROW(__sync_fetch_and_and), 4281 BUILTIN_ROW(__sync_fetch_and_xor), 4282 BUILTIN_ROW(__sync_fetch_and_nand), 4283 4284 BUILTIN_ROW(__sync_add_and_fetch), 4285 BUILTIN_ROW(__sync_sub_and_fetch), 4286 BUILTIN_ROW(__sync_and_and_fetch), 4287 BUILTIN_ROW(__sync_or_and_fetch), 4288 BUILTIN_ROW(__sync_xor_and_fetch), 4289 BUILTIN_ROW(__sync_nand_and_fetch), 4290 4291 BUILTIN_ROW(__sync_val_compare_and_swap), 4292 BUILTIN_ROW(__sync_bool_compare_and_swap), 4293 BUILTIN_ROW(__sync_lock_test_and_set), 4294 BUILTIN_ROW(__sync_lock_release), 4295 BUILTIN_ROW(__sync_swap) 4296 }; 4297 #undef BUILTIN_ROW 4298 4299 // Determine the index of the size. 4300 unsigned SizeIndex; 4301 switch (Context.getTypeSizeInChars(ValType).getQuantity()) { 4302 case 1: SizeIndex = 0; break; 4303 case 2: SizeIndex = 1; break; 4304 case 4: SizeIndex = 2; break; 4305 case 8: SizeIndex = 3; break; 4306 case 16: SizeIndex = 4; break; 4307 default: 4308 Diag(DRE->getBeginLoc(), diag::err_atomic_builtin_pointer_size) 4309 << FirstArg->getType() << FirstArg->getSourceRange(); 4310 return ExprError(); 4311 } 4312 4313 // Each of these builtins has one pointer argument, followed by some number of 4314 // values (0, 1 or 2) followed by a potentially empty varags list of stuff 4315 // that we ignore. Find out which row of BuiltinIndices to read from as well 4316 // as the number of fixed args. 4317 unsigned BuiltinID = FDecl->getBuiltinID(); 4318 unsigned BuiltinIndex, NumFixed = 1; 4319 bool WarnAboutSemanticsChange = false; 4320 switch (BuiltinID) { 4321 default: llvm_unreachable("Unknown overloaded atomic builtin!"); 4322 case Builtin::BI__sync_fetch_and_add: 4323 case Builtin::BI__sync_fetch_and_add_1: 4324 case Builtin::BI__sync_fetch_and_add_2: 4325 case Builtin::BI__sync_fetch_and_add_4: 4326 case Builtin::BI__sync_fetch_and_add_8: 4327 case Builtin::BI__sync_fetch_and_add_16: 4328 BuiltinIndex = 0; 4329 break; 4330 4331 case Builtin::BI__sync_fetch_and_sub: 4332 case Builtin::BI__sync_fetch_and_sub_1: 4333 case Builtin::BI__sync_fetch_and_sub_2: 4334 case Builtin::BI__sync_fetch_and_sub_4: 4335 case Builtin::BI__sync_fetch_and_sub_8: 4336 case Builtin::BI__sync_fetch_and_sub_16: 4337 BuiltinIndex = 1; 4338 break; 4339 4340 case Builtin::BI__sync_fetch_and_or: 4341 case Builtin::BI__sync_fetch_and_or_1: 4342 case Builtin::BI__sync_fetch_and_or_2: 4343 case Builtin::BI__sync_fetch_and_or_4: 4344 case Builtin::BI__sync_fetch_and_or_8: 4345 case Builtin::BI__sync_fetch_and_or_16: 4346 BuiltinIndex = 2; 4347 break; 4348 4349 case Builtin::BI__sync_fetch_and_and: 4350 case Builtin::BI__sync_fetch_and_and_1: 4351 case Builtin::BI__sync_fetch_and_and_2: 4352 case Builtin::BI__sync_fetch_and_and_4: 4353 case Builtin::BI__sync_fetch_and_and_8: 4354 case Builtin::BI__sync_fetch_and_and_16: 4355 BuiltinIndex = 3; 4356 break; 4357 4358 case Builtin::BI__sync_fetch_and_xor: 4359 case Builtin::BI__sync_fetch_and_xor_1: 4360 case Builtin::BI__sync_fetch_and_xor_2: 4361 case Builtin::BI__sync_fetch_and_xor_4: 4362 case Builtin::BI__sync_fetch_and_xor_8: 4363 case Builtin::BI__sync_fetch_and_xor_16: 4364 BuiltinIndex = 4; 4365 break; 4366 4367 case Builtin::BI__sync_fetch_and_nand: 4368 case Builtin::BI__sync_fetch_and_nand_1: 4369 case Builtin::BI__sync_fetch_and_nand_2: 4370 case Builtin::BI__sync_fetch_and_nand_4: 4371 case Builtin::BI__sync_fetch_and_nand_8: 4372 case Builtin::BI__sync_fetch_and_nand_16: 4373 BuiltinIndex = 5; 4374 WarnAboutSemanticsChange = true; 4375 break; 4376 4377 case Builtin::BI__sync_add_and_fetch: 4378 case Builtin::BI__sync_add_and_fetch_1: 4379 case Builtin::BI__sync_add_and_fetch_2: 4380 case Builtin::BI__sync_add_and_fetch_4: 4381 case Builtin::BI__sync_add_and_fetch_8: 4382 case Builtin::BI__sync_add_and_fetch_16: 4383 BuiltinIndex = 6; 4384 break; 4385 4386 case Builtin::BI__sync_sub_and_fetch: 4387 case Builtin::BI__sync_sub_and_fetch_1: 4388 case Builtin::BI__sync_sub_and_fetch_2: 4389 case Builtin::BI__sync_sub_and_fetch_4: 4390 case Builtin::BI__sync_sub_and_fetch_8: 4391 case Builtin::BI__sync_sub_and_fetch_16: 4392 BuiltinIndex = 7; 4393 break; 4394 4395 case Builtin::BI__sync_and_and_fetch: 4396 case Builtin::BI__sync_and_and_fetch_1: 4397 case Builtin::BI__sync_and_and_fetch_2: 4398 case Builtin::BI__sync_and_and_fetch_4: 4399 case Builtin::BI__sync_and_and_fetch_8: 4400 case Builtin::BI__sync_and_and_fetch_16: 4401 BuiltinIndex = 8; 4402 break; 4403 4404 case Builtin::BI__sync_or_and_fetch: 4405 case Builtin::BI__sync_or_and_fetch_1: 4406 case Builtin::BI__sync_or_and_fetch_2: 4407 case Builtin::BI__sync_or_and_fetch_4: 4408 case Builtin::BI__sync_or_and_fetch_8: 4409 case Builtin::BI__sync_or_and_fetch_16: 4410 BuiltinIndex = 9; 4411 break; 4412 4413 case Builtin::BI__sync_xor_and_fetch: 4414 case Builtin::BI__sync_xor_and_fetch_1: 4415 case Builtin::BI__sync_xor_and_fetch_2: 4416 case Builtin::BI__sync_xor_and_fetch_4: 4417 case Builtin::BI__sync_xor_and_fetch_8: 4418 case Builtin::BI__sync_xor_and_fetch_16: 4419 BuiltinIndex = 10; 4420 break; 4421 4422 case Builtin::BI__sync_nand_and_fetch: 4423 case Builtin::BI__sync_nand_and_fetch_1: 4424 case Builtin::BI__sync_nand_and_fetch_2: 4425 case Builtin::BI__sync_nand_and_fetch_4: 4426 case Builtin::BI__sync_nand_and_fetch_8: 4427 case Builtin::BI__sync_nand_and_fetch_16: 4428 BuiltinIndex = 11; 4429 WarnAboutSemanticsChange = true; 4430 break; 4431 4432 case Builtin::BI__sync_val_compare_and_swap: 4433 case Builtin::BI__sync_val_compare_and_swap_1: 4434 case Builtin::BI__sync_val_compare_and_swap_2: 4435 case Builtin::BI__sync_val_compare_and_swap_4: 4436 case Builtin::BI__sync_val_compare_and_swap_8: 4437 case Builtin::BI__sync_val_compare_and_swap_16: 4438 BuiltinIndex = 12; 4439 NumFixed = 2; 4440 break; 4441 4442 case Builtin::BI__sync_bool_compare_and_swap: 4443 case Builtin::BI__sync_bool_compare_and_swap_1: 4444 case Builtin::BI__sync_bool_compare_and_swap_2: 4445 case Builtin::BI__sync_bool_compare_and_swap_4: 4446 case Builtin::BI__sync_bool_compare_and_swap_8: 4447 case Builtin::BI__sync_bool_compare_and_swap_16: 4448 BuiltinIndex = 13; 4449 NumFixed = 2; 4450 ResultType = Context.BoolTy; 4451 break; 4452 4453 case Builtin::BI__sync_lock_test_and_set: 4454 case Builtin::BI__sync_lock_test_and_set_1: 4455 case Builtin::BI__sync_lock_test_and_set_2: 4456 case Builtin::BI__sync_lock_test_and_set_4: 4457 case Builtin::BI__sync_lock_test_and_set_8: 4458 case Builtin::BI__sync_lock_test_and_set_16: 4459 BuiltinIndex = 14; 4460 break; 4461 4462 case Builtin::BI__sync_lock_release: 4463 case Builtin::BI__sync_lock_release_1: 4464 case Builtin::BI__sync_lock_release_2: 4465 case Builtin::BI__sync_lock_release_4: 4466 case Builtin::BI__sync_lock_release_8: 4467 case Builtin::BI__sync_lock_release_16: 4468 BuiltinIndex = 15; 4469 NumFixed = 0; 4470 ResultType = Context.VoidTy; 4471 break; 4472 4473 case Builtin::BI__sync_swap: 4474 case Builtin::BI__sync_swap_1: 4475 case Builtin::BI__sync_swap_2: 4476 case Builtin::BI__sync_swap_4: 4477 case Builtin::BI__sync_swap_8: 4478 case Builtin::BI__sync_swap_16: 4479 BuiltinIndex = 16; 4480 break; 4481 } 4482 4483 // Now that we know how many fixed arguments we expect, first check that we 4484 // have at least that many. 4485 if (TheCall->getNumArgs() < 1+NumFixed) { 4486 Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least) 4487 << 0 << 1 + NumFixed << TheCall->getNumArgs() << /*is non object*/ 0 4488 << Callee->getSourceRange(); 4489 return ExprError(); 4490 } 4491 4492 Diag(TheCall->getEndLoc(), diag::warn_atomic_implicit_seq_cst) 4493 << Callee->getSourceRange(); 4494 4495 if (WarnAboutSemanticsChange) { 4496 Diag(TheCall->getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change) 4497 << Callee->getSourceRange(); 4498 } 4499 4500 // Get the decl for the concrete builtin from this, we can tell what the 4501 // concrete integer type we should convert to is. 4502 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; 4503 StringRef NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID); 4504 FunctionDecl *NewBuiltinDecl; 4505 if (NewBuiltinID == BuiltinID) 4506 NewBuiltinDecl = FDecl; 4507 else { 4508 // Perform builtin lookup to avoid redeclaring it. 4509 DeclarationName DN(&Context.Idents.get(NewBuiltinName)); 4510 LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName); 4511 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); 4512 assert(Res.getFoundDecl()); 4513 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); 4514 if (!NewBuiltinDecl) 4515 return ExprError(); 4516 } 4517 4518 // The first argument --- the pointer --- has a fixed type; we 4519 // deduce the types of the rest of the arguments accordingly. Walk 4520 // the remaining arguments, converting them to the deduced value type. 4521 for (unsigned i = 0; i != NumFixed; ++i) { 4522 ExprResult Arg = TheCall->getArg(i+1); 4523 4524 // GCC does an implicit conversion to the pointer or integer ValType. This 4525 // can fail in some cases (1i -> int**), check for this error case now. 4526 // Initialize the argument. 4527 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 4528 ValType, /*consume*/ false); 4529 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 4530 if (Arg.isInvalid()) 4531 return ExprError(); 4532 4533 // Okay, we have something that *can* be converted to the right type. Check 4534 // to see if there is a potentially weird extension going on here. This can 4535 // happen when you do an atomic operation on something like an char* and 4536 // pass in 42. The 42 gets converted to char. This is even more strange 4537 // for things like 45.123 -> char, etc. 4538 // FIXME: Do this check. 4539 TheCall->setArg(i+1, Arg.get()); 4540 } 4541 4542 // Create a new DeclRefExpr to refer to the new decl. 4543 DeclRefExpr *NewDRE = DeclRefExpr::Create( 4544 Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl, 4545 /*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy, 4546 DRE->getValueKind(), nullptr, nullptr, DRE->isNonOdrUse()); 4547 4548 // Set the callee in the CallExpr. 4549 // FIXME: This loses syntactic information. 4550 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); 4551 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, 4552 CK_BuiltinFnToFnPtr); 4553 TheCall->setCallee(PromotedCall.get()); 4554 4555 // Change the result type of the call to match the original value type. This 4556 // is arbitrary, but the codegen for these builtins ins design to handle it 4557 // gracefully. 4558 TheCall->setType(ResultType); 4559 4560 // Prohibit problematic uses of bit-precise integer types with atomic 4561 // builtins. The arguments would have already been converted to the first 4562 // argument's type, so only need to check the first argument. 4563 const auto *BitIntValType = ValType->getAs<BitIntType>(); 4564 if (BitIntValType && !llvm::isPowerOf2_64(BitIntValType->getNumBits())) { 4565 Diag(FirstArg->getExprLoc(), diag::err_atomic_builtin_ext_int_size); 4566 return ExprError(); 4567 } 4568 4569 return TheCallResult; 4570 } 4571 4572 ExprResult Sema::BuiltinNontemporalOverloaded(ExprResult TheCallResult) { 4573 CallExpr *TheCall = (CallExpr *)TheCallResult.get(); 4574 DeclRefExpr *DRE = 4575 cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 4576 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 4577 unsigned BuiltinID = FDecl->getBuiltinID(); 4578 assert((BuiltinID == Builtin::BI__builtin_nontemporal_store || 4579 BuiltinID == Builtin::BI__builtin_nontemporal_load) && 4580 "Unexpected nontemporal load/store builtin!"); 4581 bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store; 4582 unsigned numArgs = isStore ? 2 : 1; 4583 4584 // Ensure that we have the proper number of arguments. 4585 if (checkArgCount(TheCall, numArgs)) 4586 return ExprError(); 4587 4588 // Inspect the last argument of the nontemporal builtin. This should always 4589 // be a pointer type, from which we imply the type of the memory access. 4590 // Because it is a pointer type, we don't have to worry about any implicit 4591 // casts here. 4592 Expr *PointerArg = TheCall->getArg(numArgs - 1); 4593 ExprResult PointerArgResult = 4594 DefaultFunctionArrayLvalueConversion(PointerArg); 4595 4596 if (PointerArgResult.isInvalid()) 4597 return ExprError(); 4598 PointerArg = PointerArgResult.get(); 4599 TheCall->setArg(numArgs - 1, PointerArg); 4600 4601 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); 4602 if (!pointerType) { 4603 Diag(DRE->getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer) 4604 << PointerArg->getType() << PointerArg->getSourceRange(); 4605 return ExprError(); 4606 } 4607 4608 QualType ValType = pointerType->getPointeeType(); 4609 4610 // Strip any qualifiers off ValType. 4611 ValType = ValType.getUnqualifiedType(); 4612 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 4613 !ValType->isBlockPointerType() && !ValType->isFloatingType() && 4614 !ValType->isVectorType()) { 4615 Diag(DRE->getBeginLoc(), 4616 diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector) 4617 << PointerArg->getType() << PointerArg->getSourceRange(); 4618 return ExprError(); 4619 } 4620 4621 if (!isStore) { 4622 TheCall->setType(ValType); 4623 return TheCallResult; 4624 } 4625 4626 ExprResult ValArg = TheCall->getArg(0); 4627 InitializedEntity Entity = InitializedEntity::InitializeParameter( 4628 Context, ValType, /*consume*/ false); 4629 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); 4630 if (ValArg.isInvalid()) 4631 return ExprError(); 4632 4633 TheCall->setArg(0, ValArg.get()); 4634 TheCall->setType(Context.VoidTy); 4635 return TheCallResult; 4636 } 4637 4638 /// CheckObjCString - Checks that the format string argument to the os_log() 4639 /// and os_trace() functions is correct, and converts it to const char *. 4640 ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) { 4641 Arg = Arg->IgnoreParenCasts(); 4642 auto *Literal = dyn_cast<StringLiteral>(Arg); 4643 if (!Literal) { 4644 if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) { 4645 Literal = ObjcLiteral->getString(); 4646 } 4647 } 4648 4649 if (!Literal || (!Literal->isOrdinary() && !Literal->isUTF8())) { 4650 return ExprError( 4651 Diag(Arg->getBeginLoc(), diag::err_os_log_format_not_string_constant) 4652 << Arg->getSourceRange()); 4653 } 4654 4655 ExprResult Result(Literal); 4656 QualType ResultTy = Context.getPointerType(Context.CharTy.withConst()); 4657 InitializedEntity Entity = 4658 InitializedEntity::InitializeParameter(Context, ResultTy, false); 4659 Result = PerformCopyInitialization(Entity, SourceLocation(), Result); 4660 return Result; 4661 } 4662 4663 /// Check that the user is calling the appropriate va_start builtin for the 4664 /// target and calling convention. 4665 static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) { 4666 const llvm::Triple &TT = S.Context.getTargetInfo().getTriple(); 4667 bool IsX64 = TT.getArch() == llvm::Triple::x86_64; 4668 bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 || 4669 TT.getArch() == llvm::Triple::aarch64_32); 4670 bool IsWindows = TT.isOSWindows(); 4671 bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start; 4672 if (IsX64 || IsAArch64) { 4673 CallingConv CC = CC_C; 4674 if (const FunctionDecl *FD = S.getCurFunctionDecl()) 4675 CC = FD->getType()->castAs<FunctionType>()->getCallConv(); 4676 if (IsMSVAStart) { 4677 // Don't allow this in System V ABI functions. 4678 if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64)) 4679 return S.Diag(Fn->getBeginLoc(), 4680 diag::err_ms_va_start_used_in_sysv_function); 4681 } else { 4682 // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions. 4683 // On x64 Windows, don't allow this in System V ABI functions. 4684 // (Yes, that means there's no corresponding way to support variadic 4685 // System V ABI functions on Windows.) 4686 if ((IsWindows && CC == CC_X86_64SysV) || 4687 (!IsWindows && CC == CC_Win64)) 4688 return S.Diag(Fn->getBeginLoc(), 4689 diag::err_va_start_used_in_wrong_abi_function) 4690 << !IsWindows; 4691 } 4692 return false; 4693 } 4694 4695 if (IsMSVAStart) 4696 return S.Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only); 4697 return false; 4698 } 4699 4700 static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn, 4701 ParmVarDecl **LastParam = nullptr) { 4702 // Determine whether the current function, block, or obj-c method is variadic 4703 // and get its parameter list. 4704 bool IsVariadic = false; 4705 ArrayRef<ParmVarDecl *> Params; 4706 DeclContext *Caller = S.CurContext; 4707 if (auto *Block = dyn_cast<BlockDecl>(Caller)) { 4708 IsVariadic = Block->isVariadic(); 4709 Params = Block->parameters(); 4710 } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) { 4711 IsVariadic = FD->isVariadic(); 4712 Params = FD->parameters(); 4713 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) { 4714 IsVariadic = MD->isVariadic(); 4715 // FIXME: This isn't correct for methods (results in bogus warning). 4716 Params = MD->parameters(); 4717 } else if (isa<CapturedDecl>(Caller)) { 4718 // We don't support va_start in a CapturedDecl. 4719 S.Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt); 4720 return true; 4721 } else { 4722 // This must be some other declcontext that parses exprs. 4723 S.Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function); 4724 return true; 4725 } 4726 4727 if (!IsVariadic) { 4728 S.Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function); 4729 return true; 4730 } 4731 4732 if (LastParam) 4733 *LastParam = Params.empty() ? nullptr : Params.back(); 4734 4735 return false; 4736 } 4737 4738 bool Sema::BuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) { 4739 Expr *Fn = TheCall->getCallee(); 4740 4741 if (checkVAStartABI(*this, BuiltinID, Fn)) 4742 return true; 4743 4744 // In C23 mode, va_start only needs one argument. However, the builtin still 4745 // requires two arguments (which matches the behavior of the GCC builtin), 4746 // <stdarg.h> passes `0` as the second argument in C23 mode. 4747 if (checkArgCount(TheCall, 2)) 4748 return true; 4749 4750 // Type-check the first argument normally. 4751 if (checkBuiltinArgument(*this, TheCall, 0)) 4752 return true; 4753 4754 // Check that the current function is variadic, and get its last parameter. 4755 ParmVarDecl *LastParam; 4756 if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam)) 4757 return true; 4758 4759 // Verify that the second argument to the builtin is the last argument of the 4760 // current function or method. In C23 mode, if the second argument is an 4761 // integer constant expression with value 0, then we don't bother with this 4762 // check. 4763 bool SecondArgIsLastNamedArgument = false; 4764 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); 4765 if (std::optional<llvm::APSInt> Val = 4766 TheCall->getArg(1)->getIntegerConstantExpr(Context); 4767 Val && LangOpts.C23 && *Val == 0) 4768 return false; 4769 4770 // These are valid if SecondArgIsLastNamedArgument is false after the next 4771 // block. 4772 QualType Type; 4773 SourceLocation ParamLoc; 4774 bool IsCRegister = false; 4775 4776 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { 4777 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { 4778 SecondArgIsLastNamedArgument = PV == LastParam; 4779 4780 Type = PV->getType(); 4781 ParamLoc = PV->getLocation(); 4782 IsCRegister = 4783 PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus; 4784 } 4785 } 4786 4787 if (!SecondArgIsLastNamedArgument) 4788 Diag(TheCall->getArg(1)->getBeginLoc(), 4789 diag::warn_second_arg_of_va_start_not_last_named_param); 4790 else if (IsCRegister || Type->isReferenceType() || 4791 Type->isSpecificBuiltinType(BuiltinType::Float) || [=] { 4792 // Promotable integers are UB, but enumerations need a bit of 4793 // extra checking to see what their promotable type actually is. 4794 if (!Context.isPromotableIntegerType(Type)) 4795 return false; 4796 if (!Type->isEnumeralType()) 4797 return true; 4798 const EnumDecl *ED = Type->castAs<EnumType>()->getDecl(); 4799 return !(ED && 4800 Context.typesAreCompatible(ED->getPromotionType(), Type)); 4801 }()) { 4802 unsigned Reason = 0; 4803 if (Type->isReferenceType()) Reason = 1; 4804 else if (IsCRegister) Reason = 2; 4805 Diag(Arg->getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason; 4806 Diag(ParamLoc, diag::note_parameter_type) << Type; 4807 } 4808 4809 return false; 4810 } 4811 4812 bool Sema::BuiltinVAStartARMMicrosoft(CallExpr *Call) { 4813 auto IsSuitablyTypedFormatArgument = [this](const Expr *Arg) -> bool { 4814 const LangOptions &LO = getLangOpts(); 4815 4816 if (LO.CPlusPlus) 4817 return Arg->getType() 4818 .getCanonicalType() 4819 .getTypePtr() 4820 ->getPointeeType() 4821 .withoutLocalFastQualifiers() == Context.CharTy; 4822 4823 // In C, allow aliasing through `char *`, this is required for AArch64 at 4824 // least. 4825 return true; 4826 }; 4827 4828 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, 4829 // const char *named_addr); 4830 4831 Expr *Func = Call->getCallee(); 4832 4833 if (Call->getNumArgs() < 3) 4834 return Diag(Call->getEndLoc(), 4835 diag::err_typecheck_call_too_few_args_at_least) 4836 << 0 /*function call*/ << 3 << Call->getNumArgs() 4837 << /*is non object*/ 0; 4838 4839 // Type-check the first argument normally. 4840 if (checkBuiltinArgument(*this, Call, 0)) 4841 return true; 4842 4843 // Check that the current function is variadic. 4844 if (checkVAStartIsInVariadicFunction(*this, Func)) 4845 return true; 4846 4847 // __va_start on Windows does not validate the parameter qualifiers 4848 4849 const Expr *Arg1 = Call->getArg(1)->IgnoreParens(); 4850 const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr(); 4851 4852 const Expr *Arg2 = Call->getArg(2)->IgnoreParens(); 4853 const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr(); 4854 4855 const QualType &ConstCharPtrTy = 4856 Context.getPointerType(Context.CharTy.withConst()); 4857 if (!Arg1Ty->isPointerType() || !IsSuitablyTypedFormatArgument(Arg1)) 4858 Diag(Arg1->getBeginLoc(), diag::err_typecheck_convert_incompatible) 4859 << Arg1->getType() << ConstCharPtrTy << 1 /* different class */ 4860 << 0 /* qualifier difference */ 4861 << 3 /* parameter mismatch */ 4862 << 2 << Arg1->getType() << ConstCharPtrTy; 4863 4864 const QualType SizeTy = Context.getSizeType(); 4865 if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy) 4866 Diag(Arg2->getBeginLoc(), diag::err_typecheck_convert_incompatible) 4867 << Arg2->getType() << SizeTy << 1 /* different class */ 4868 << 0 /* qualifier difference */ 4869 << 3 /* parameter mismatch */ 4870 << 3 << Arg2->getType() << SizeTy; 4871 4872 return false; 4873 } 4874 4875 bool Sema::BuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID) { 4876 if (checkArgCount(TheCall, 2)) 4877 return true; 4878 4879 if (BuiltinID == Builtin::BI__builtin_isunordered && 4880 TheCall->getFPFeaturesInEffect(getLangOpts()).getNoHonorNaNs()) 4881 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4882 << 1 << 0 << TheCall->getSourceRange(); 4883 4884 ExprResult OrigArg0 = TheCall->getArg(0); 4885 ExprResult OrigArg1 = TheCall->getArg(1); 4886 4887 // Do standard promotions between the two arguments, returning their common 4888 // type. 4889 QualType Res = UsualArithmeticConversions( 4890 OrigArg0, OrigArg1, TheCall->getExprLoc(), ACK_Comparison); 4891 if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) 4892 return true; 4893 4894 // Make sure any conversions are pushed back into the call; this is 4895 // type safe since unordered compare builtins are declared as "_Bool 4896 // foo(...)". 4897 TheCall->setArg(0, OrigArg0.get()); 4898 TheCall->setArg(1, OrigArg1.get()); 4899 4900 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) 4901 return false; 4902 4903 // If the common type isn't a real floating type, then the arguments were 4904 // invalid for this operation. 4905 if (Res.isNull() || !Res->isRealFloatingType()) 4906 return Diag(OrigArg0.get()->getBeginLoc(), 4907 diag::err_typecheck_call_invalid_ordered_compare) 4908 << OrigArg0.get()->getType() << OrigArg1.get()->getType() 4909 << SourceRange(OrigArg0.get()->getBeginLoc(), 4910 OrigArg1.get()->getEndLoc()); 4911 4912 return false; 4913 } 4914 4915 bool Sema::BuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs, 4916 unsigned BuiltinID) { 4917 if (checkArgCount(TheCall, NumArgs)) 4918 return true; 4919 4920 FPOptions FPO = TheCall->getFPFeaturesInEffect(getLangOpts()); 4921 if (FPO.getNoHonorInfs() && (BuiltinID == Builtin::BI__builtin_isfinite || 4922 BuiltinID == Builtin::BI__builtin_isinf || 4923 BuiltinID == Builtin::BI__builtin_isinf_sign)) 4924 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4925 << 0 << 0 << TheCall->getSourceRange(); 4926 4927 if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan || 4928 BuiltinID == Builtin::BI__builtin_isunordered)) 4929 Diag(TheCall->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 4930 << 1 << 0 << TheCall->getSourceRange(); 4931 4932 bool IsFPClass = NumArgs == 2; 4933 4934 // Find out position of floating-point argument. 4935 unsigned FPArgNo = IsFPClass ? 0 : NumArgs - 1; 4936 4937 // We can count on all parameters preceding the floating-point just being int. 4938 // Try all of those. 4939 for (unsigned i = 0; i < FPArgNo; ++i) { 4940 Expr *Arg = TheCall->getArg(i); 4941 4942 if (Arg->isTypeDependent()) 4943 return false; 4944 4945 ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy, 4946 AssignmentAction::Passing); 4947 4948 if (Res.isInvalid()) 4949 return true; 4950 TheCall->setArg(i, Res.get()); 4951 } 4952 4953 Expr *OrigArg = TheCall->getArg(FPArgNo); 4954 4955 if (OrigArg->isTypeDependent()) 4956 return false; 4957 4958 // Usual Unary Conversions will convert half to float, which we want for 4959 // machines that use fp16 conversion intrinsics. Else, we wnat to leave the 4960 // type how it is, but do normal L->Rvalue conversions. 4961 if (Context.getTargetInfo().useFP16ConversionIntrinsics()) { 4962 ExprResult Res = UsualUnaryConversions(OrigArg); 4963 4964 if (!Res.isUsable()) 4965 return true; 4966 OrigArg = Res.get(); 4967 } else { 4968 ExprResult Res = DefaultFunctionArrayLvalueConversion(OrigArg); 4969 4970 if (!Res.isUsable()) 4971 return true; 4972 OrigArg = Res.get(); 4973 } 4974 TheCall->setArg(FPArgNo, OrigArg); 4975 4976 QualType VectorResultTy; 4977 QualType ElementTy = OrigArg->getType(); 4978 // TODO: When all classification function are implemented with is_fpclass, 4979 // vector argument can be supported in all of them. 4980 if (ElementTy->isVectorType() && IsFPClass) { 4981 VectorResultTy = GetSignedVectorType(ElementTy); 4982 ElementTy = ElementTy->castAs<VectorType>()->getElementType(); 4983 } 4984 4985 // This operation requires a non-_Complex floating-point number. 4986 if (!ElementTy->isRealFloatingType()) 4987 return Diag(OrigArg->getBeginLoc(), 4988 diag::err_typecheck_call_invalid_unary_fp) 4989 << OrigArg->getType() << OrigArg->getSourceRange(); 4990 4991 // __builtin_isfpclass has integer parameter that specify test mask. It is 4992 // passed in (...), so it should be analyzed completely here. 4993 if (IsFPClass) 4994 if (BuiltinConstantArgRange(TheCall, 1, 0, llvm::fcAllFlags)) 4995 return true; 4996 4997 // TODO: enable this code to all classification functions. 4998 if (IsFPClass) { 4999 QualType ResultTy; 5000 if (!VectorResultTy.isNull()) 5001 ResultTy = VectorResultTy; 5002 else 5003 ResultTy = Context.IntTy; 5004 TheCall->setType(ResultTy); 5005 } 5006 5007 return false; 5008 } 5009 5010 bool Sema::BuiltinComplex(CallExpr *TheCall) { 5011 if (checkArgCount(TheCall, 2)) 5012 return true; 5013 5014 bool Dependent = false; 5015 for (unsigned I = 0; I != 2; ++I) { 5016 Expr *Arg = TheCall->getArg(I); 5017 QualType T = Arg->getType(); 5018 if (T->isDependentType()) { 5019 Dependent = true; 5020 continue; 5021 } 5022 5023 // Despite supporting _Complex int, GCC requires a real floating point type 5024 // for the operands of __builtin_complex. 5025 if (!T->isRealFloatingType()) { 5026 return Diag(Arg->getBeginLoc(), diag::err_typecheck_call_requires_real_fp) 5027 << Arg->getType() << Arg->getSourceRange(); 5028 } 5029 5030 ExprResult Converted = DefaultLvalueConversion(Arg); 5031 if (Converted.isInvalid()) 5032 return true; 5033 TheCall->setArg(I, Converted.get()); 5034 } 5035 5036 if (Dependent) { 5037 TheCall->setType(Context.DependentTy); 5038 return false; 5039 } 5040 5041 Expr *Real = TheCall->getArg(0); 5042 Expr *Imag = TheCall->getArg(1); 5043 if (!Context.hasSameType(Real->getType(), Imag->getType())) { 5044 return Diag(Real->getBeginLoc(), 5045 diag::err_typecheck_call_different_arg_types) 5046 << Real->getType() << Imag->getType() 5047 << Real->getSourceRange() << Imag->getSourceRange(); 5048 } 5049 5050 // We don't allow _Complex _Float16 nor _Complex __fp16 as type specifiers; 5051 // don't allow this builtin to form those types either. 5052 // FIXME: Should we allow these types? 5053 if (Real->getType()->isFloat16Type()) 5054 return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) 5055 << "_Float16"; 5056 if (Real->getType()->isHalfType()) 5057 return Diag(TheCall->getBeginLoc(), diag::err_invalid_complex_spec) 5058 << "half"; 5059 5060 TheCall->setType(Context.getComplexType(Real->getType())); 5061 return false; 5062 } 5063 5064 /// BuiltinShuffleVector - Handle __builtin_shufflevector. 5065 // This is declared to take (...), so we have to check everything. 5066 ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) { 5067 if (TheCall->getNumArgs() < 2) 5068 return ExprError(Diag(TheCall->getEndLoc(), 5069 diag::err_typecheck_call_too_few_args_at_least) 5070 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 5071 << /*is non object*/ 0 << TheCall->getSourceRange()); 5072 5073 // Determine which of the following types of shufflevector we're checking: 5074 // 1) unary, vector mask: (lhs, mask) 5075 // 2) binary, scalar mask: (lhs, rhs, index, ..., index) 5076 QualType resType = TheCall->getArg(0)->getType(); 5077 unsigned numElements = 0; 5078 5079 if (!TheCall->getArg(0)->isTypeDependent() && 5080 !TheCall->getArg(1)->isTypeDependent()) { 5081 QualType LHSType = TheCall->getArg(0)->getType(); 5082 QualType RHSType = TheCall->getArg(1)->getType(); 5083 5084 if (!LHSType->isVectorType() || !RHSType->isVectorType()) 5085 return ExprError( 5086 Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_non_vector) 5087 << TheCall->getDirectCallee() << /*isMorethantwoArgs*/ false 5088 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5089 TheCall->getArg(1)->getEndLoc())); 5090 5091 numElements = LHSType->castAs<VectorType>()->getNumElements(); 5092 unsigned numResElements = TheCall->getNumArgs() - 2; 5093 5094 // Check to see if we have a call with 2 vector arguments, the unary shuffle 5095 // with mask. If so, verify that RHS is an integer vector type with the 5096 // same number of elts as lhs. 5097 if (TheCall->getNumArgs() == 2) { 5098 if (!RHSType->hasIntegerRepresentation() || 5099 RHSType->castAs<VectorType>()->getNumElements() != numElements) 5100 return ExprError(Diag(TheCall->getBeginLoc(), 5101 diag::err_vec_builtin_incompatible_vector) 5102 << TheCall->getDirectCallee() 5103 << /*isMorethantwoArgs*/ false 5104 << SourceRange(TheCall->getArg(1)->getBeginLoc(), 5105 TheCall->getArg(1)->getEndLoc())); 5106 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { 5107 return ExprError(Diag(TheCall->getBeginLoc(), 5108 diag::err_vec_builtin_incompatible_vector) 5109 << TheCall->getDirectCallee() 5110 << /*isMorethantwoArgs*/ false 5111 << SourceRange(TheCall->getArg(0)->getBeginLoc(), 5112 TheCall->getArg(1)->getEndLoc())); 5113 } else if (numElements != numResElements) { 5114 QualType eltType = LHSType->castAs<VectorType>()->getElementType(); 5115 resType = 5116 Context.getVectorType(eltType, numResElements, VectorKind::Generic); 5117 } 5118 } 5119 5120 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { 5121 if (TheCall->getArg(i)->isTypeDependent() || 5122 TheCall->getArg(i)->isValueDependent()) 5123 continue; 5124 5125 std::optional<llvm::APSInt> Result; 5126 if (!(Result = TheCall->getArg(i)->getIntegerConstantExpr(Context))) 5127 return ExprError(Diag(TheCall->getBeginLoc(), 5128 diag::err_shufflevector_nonconstant_argument) 5129 << TheCall->getArg(i)->getSourceRange()); 5130 5131 // Allow -1 which will be translated to undef in the IR. 5132 if (Result->isSigned() && Result->isAllOnes()) 5133 continue; 5134 5135 if (Result->getActiveBits() > 64 || 5136 Result->getZExtValue() >= numElements * 2) 5137 return ExprError(Diag(TheCall->getBeginLoc(), 5138 diag::err_shufflevector_argument_too_large) 5139 << TheCall->getArg(i)->getSourceRange()); 5140 } 5141 5142 SmallVector<Expr*, 32> exprs; 5143 5144 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { 5145 exprs.push_back(TheCall->getArg(i)); 5146 TheCall->setArg(i, nullptr); 5147 } 5148 5149 return new (Context) ShuffleVectorExpr(Context, exprs, resType, 5150 TheCall->getCallee()->getBeginLoc(), 5151 TheCall->getRParenLoc()); 5152 } 5153 5154 ExprResult Sema::ConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, 5155 SourceLocation BuiltinLoc, 5156 SourceLocation RParenLoc) { 5157 ExprValueKind VK = VK_PRValue; 5158 ExprObjectKind OK = OK_Ordinary; 5159 QualType DstTy = TInfo->getType(); 5160 QualType SrcTy = E->getType(); 5161 5162 if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) 5163 return ExprError(Diag(BuiltinLoc, 5164 diag::err_convertvector_non_vector) 5165 << E->getSourceRange()); 5166 if (!DstTy->isVectorType() && !DstTy->isDependentType()) 5167 return ExprError(Diag(BuiltinLoc, diag::err_builtin_non_vector_type) 5168 << "second" 5169 << "__builtin_convertvector"); 5170 5171 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { 5172 unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements(); 5173 unsigned DstElts = DstTy->castAs<VectorType>()->getNumElements(); 5174 if (SrcElts != DstElts) 5175 return ExprError(Diag(BuiltinLoc, 5176 diag::err_convertvector_incompatible_vector) 5177 << E->getSourceRange()); 5178 } 5179 5180 return new (Context) class ConvertVectorExpr(E, TInfo, DstTy, VK, OK, 5181 BuiltinLoc, RParenLoc); 5182 } 5183 5184 bool Sema::BuiltinPrefetch(CallExpr *TheCall) { 5185 unsigned NumArgs = TheCall->getNumArgs(); 5186 5187 if (NumArgs > 3) 5188 return Diag(TheCall->getEndLoc(), 5189 diag::err_typecheck_call_too_many_args_at_most) 5190 << 0 /*function call*/ << 3 << NumArgs << /*is non object*/ 0 5191 << TheCall->getSourceRange(); 5192 5193 // Argument 0 is checked for us and the remaining arguments must be 5194 // constant integers. 5195 for (unsigned i = 1; i != NumArgs; ++i) 5196 if (BuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) 5197 return true; 5198 5199 return false; 5200 } 5201 5202 bool Sema::BuiltinArithmeticFence(CallExpr *TheCall) { 5203 if (!Context.getTargetInfo().checkArithmeticFenceSupported()) 5204 return Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) 5205 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5206 if (checkArgCount(TheCall, 1)) 5207 return true; 5208 Expr *Arg = TheCall->getArg(0); 5209 if (Arg->isInstantiationDependent()) 5210 return false; 5211 5212 QualType ArgTy = Arg->getType(); 5213 if (!ArgTy->hasFloatingRepresentation()) 5214 return Diag(TheCall->getEndLoc(), diag::err_typecheck_expect_flt_or_vector) 5215 << ArgTy; 5216 if (Arg->isLValue()) { 5217 ExprResult FirstArg = DefaultLvalueConversion(Arg); 5218 TheCall->setArg(0, FirstArg.get()); 5219 } 5220 TheCall->setType(TheCall->getArg(0)->getType()); 5221 return false; 5222 } 5223 5224 bool Sema::BuiltinAssume(CallExpr *TheCall) { 5225 Expr *Arg = TheCall->getArg(0); 5226 if (Arg->isInstantiationDependent()) return false; 5227 5228 if (Arg->HasSideEffects(Context)) 5229 Diag(Arg->getBeginLoc(), diag::warn_assume_side_effects) 5230 << Arg->getSourceRange() 5231 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); 5232 5233 return false; 5234 } 5235 5236 bool Sema::BuiltinAllocaWithAlign(CallExpr *TheCall) { 5237 // The alignment must be a constant integer. 5238 Expr *Arg = TheCall->getArg(1); 5239 5240 // We can't check the value of a dependent argument. 5241 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 5242 if (const auto *UE = 5243 dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts())) 5244 if (UE->getKind() == UETT_AlignOf || 5245 UE->getKind() == UETT_PreferredAlignOf) 5246 Diag(TheCall->getBeginLoc(), diag::warn_alloca_align_alignof) 5247 << Arg->getSourceRange(); 5248 5249 llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context); 5250 5251 if (!Result.isPowerOf2()) 5252 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5253 << Arg->getSourceRange(); 5254 5255 if (Result < Context.getCharWidth()) 5256 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_small) 5257 << (unsigned)Context.getCharWidth() << Arg->getSourceRange(); 5258 5259 if (Result > std::numeric_limits<int32_t>::max()) 5260 return Diag(TheCall->getBeginLoc(), diag::err_alignment_too_big) 5261 << std::numeric_limits<int32_t>::max() << Arg->getSourceRange(); 5262 } 5263 5264 return false; 5265 } 5266 5267 bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) { 5268 if (checkArgCountRange(TheCall, 2, 3)) 5269 return true; 5270 5271 unsigned NumArgs = TheCall->getNumArgs(); 5272 Expr *FirstArg = TheCall->getArg(0); 5273 5274 { 5275 ExprResult FirstArgResult = 5276 DefaultFunctionArrayLvalueConversion(FirstArg); 5277 if (checkBuiltinArgument(*this, TheCall, 0)) 5278 return true; 5279 /// In-place updation of FirstArg by checkBuiltinArgument is ignored. 5280 TheCall->setArg(0, FirstArgResult.get()); 5281 } 5282 5283 // The alignment must be a constant integer. 5284 Expr *SecondArg = TheCall->getArg(1); 5285 5286 // We can't check the value of a dependent argument. 5287 if (!SecondArg->isValueDependent()) { 5288 llvm::APSInt Result; 5289 if (BuiltinConstantArg(TheCall, 1, Result)) 5290 return true; 5291 5292 if (!Result.isPowerOf2()) 5293 return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two) 5294 << SecondArg->getSourceRange(); 5295 5296 if (Result > Sema::MaximumAlignment) 5297 Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great) 5298 << SecondArg->getSourceRange() << Sema::MaximumAlignment; 5299 } 5300 5301 if (NumArgs > 2) { 5302 Expr *ThirdArg = TheCall->getArg(2); 5303 if (convertArgumentToType(*this, ThirdArg, Context.getSizeType())) 5304 return true; 5305 TheCall->setArg(2, ThirdArg); 5306 } 5307 5308 return false; 5309 } 5310 5311 bool Sema::BuiltinOSLogFormat(CallExpr *TheCall) { 5312 unsigned BuiltinID = 5313 cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID(); 5314 bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size; 5315 5316 unsigned NumArgs = TheCall->getNumArgs(); 5317 unsigned NumRequiredArgs = IsSizeCall ? 1 : 2; 5318 if (NumArgs < NumRequiredArgs) { 5319 return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args) 5320 << 0 /* function call */ << NumRequiredArgs << NumArgs 5321 << /*is non object*/ 0 << TheCall->getSourceRange(); 5322 } 5323 if (NumArgs >= NumRequiredArgs + 0x100) { 5324 return Diag(TheCall->getEndLoc(), 5325 diag::err_typecheck_call_too_many_args_at_most) 5326 << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs 5327 << /*is non object*/ 0 << TheCall->getSourceRange(); 5328 } 5329 unsigned i = 0; 5330 5331 // For formatting call, check buffer arg. 5332 if (!IsSizeCall) { 5333 ExprResult Arg(TheCall->getArg(i)); 5334 InitializedEntity Entity = InitializedEntity::InitializeParameter( 5335 Context, Context.VoidPtrTy, false); 5336 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 5337 if (Arg.isInvalid()) 5338 return true; 5339 TheCall->setArg(i, Arg.get()); 5340 i++; 5341 } 5342 5343 // Check string literal arg. 5344 unsigned FormatIdx = i; 5345 { 5346 ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i)); 5347 if (Arg.isInvalid()) 5348 return true; 5349 TheCall->setArg(i, Arg.get()); 5350 i++; 5351 } 5352 5353 // Make sure variadic args are scalar. 5354 unsigned FirstDataArg = i; 5355 while (i < NumArgs) { 5356 ExprResult Arg = DefaultVariadicArgumentPromotion( 5357 TheCall->getArg(i), VariadicFunction, nullptr); 5358 if (Arg.isInvalid()) 5359 return true; 5360 CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType()); 5361 if (ArgSize.getQuantity() >= 0x100) { 5362 return Diag(Arg.get()->getEndLoc(), diag::err_os_log_argument_too_big) 5363 << i << (int)ArgSize.getQuantity() << 0xff 5364 << TheCall->getSourceRange(); 5365 } 5366 TheCall->setArg(i, Arg.get()); 5367 i++; 5368 } 5369 5370 // Check formatting specifiers. NOTE: We're only doing this for the non-size 5371 // call to avoid duplicate diagnostics. 5372 if (!IsSizeCall) { 5373 llvm::SmallBitVector CheckedVarArgs(NumArgs, false); 5374 ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs()); 5375 bool Success = CheckFormatArguments( 5376 Args, FAPK_Variadic, FormatIdx, FirstDataArg, FST_OSLog, 5377 VariadicFunction, TheCall->getBeginLoc(), SourceRange(), 5378 CheckedVarArgs); 5379 if (!Success) 5380 return true; 5381 } 5382 5383 if (IsSizeCall) { 5384 TheCall->setType(Context.getSizeType()); 5385 } else { 5386 TheCall->setType(Context.VoidPtrTy); 5387 } 5388 return false; 5389 } 5390 5391 bool Sema::BuiltinConstantArg(CallExpr *TheCall, int ArgNum, 5392 llvm::APSInt &Result) { 5393 Expr *Arg = TheCall->getArg(ArgNum); 5394 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 5395 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 5396 5397 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; 5398 5399 std::optional<llvm::APSInt> R; 5400 if (!(R = Arg->getIntegerConstantExpr(Context))) 5401 return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type) 5402 << FDecl->getDeclName() << Arg->getSourceRange(); 5403 Result = *R; 5404 return false; 5405 } 5406 5407 bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low, 5408 int High, bool RangeIsError) { 5409 if (isConstantEvaluatedContext()) 5410 return false; 5411 llvm::APSInt Result; 5412 5413 // We can't check the value of a dependent argument. 5414 Expr *Arg = TheCall->getArg(ArgNum); 5415 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5416 return false; 5417 5418 // Check constant-ness first. 5419 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5420 return true; 5421 5422 if (Result.getSExtValue() < Low || Result.getSExtValue() > High) { 5423 if (RangeIsError) 5424 return Diag(TheCall->getBeginLoc(), diag::err_argument_invalid_range) 5425 << toString(Result, 10) << Low << High << Arg->getSourceRange(); 5426 else 5427 // Defer the warning until we know if the code will be emitted so that 5428 // dead code can ignore this. 5429 DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, 5430 PDiag(diag::warn_argument_invalid_range) 5431 << toString(Result, 10) << Low << High 5432 << Arg->getSourceRange()); 5433 } 5434 5435 return false; 5436 } 5437 5438 bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum, 5439 unsigned Num) { 5440 llvm::APSInt Result; 5441 5442 // We can't check the value of a dependent argument. 5443 Expr *Arg = TheCall->getArg(ArgNum); 5444 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5445 return false; 5446 5447 // Check constant-ness first. 5448 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5449 return true; 5450 5451 if (Result.getSExtValue() % Num != 0) 5452 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_multiple) 5453 << Num << Arg->getSourceRange(); 5454 5455 return false; 5456 } 5457 5458 bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) { 5459 llvm::APSInt Result; 5460 5461 // We can't check the value of a dependent argument. 5462 Expr *Arg = TheCall->getArg(ArgNum); 5463 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5464 return false; 5465 5466 // Check constant-ness first. 5467 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5468 return true; 5469 5470 // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if 5471 // and only if x is a power of 2. 5472 if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0) 5473 return false; 5474 5475 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2) 5476 << Arg->getSourceRange(); 5477 } 5478 5479 static bool IsShiftedByte(llvm::APSInt Value) { 5480 if (Value.isNegative()) 5481 return false; 5482 5483 // Check if it's a shifted byte, by shifting it down 5484 while (true) { 5485 // If the value fits in the bottom byte, the check passes. 5486 if (Value < 0x100) 5487 return true; 5488 5489 // Otherwise, if the value has _any_ bits in the bottom byte, the check 5490 // fails. 5491 if ((Value & 0xFF) != 0) 5492 return false; 5493 5494 // If the bottom 8 bits are all 0, but something above that is nonzero, 5495 // then shifting the value right by 8 bits won't affect whether it's a 5496 // shifted byte or not. So do that, and go round again. 5497 Value >>= 8; 5498 } 5499 } 5500 5501 bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum, 5502 unsigned ArgBits) { 5503 llvm::APSInt Result; 5504 5505 // We can't check the value of a dependent argument. 5506 Expr *Arg = TheCall->getArg(ArgNum); 5507 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5508 return false; 5509 5510 // Check constant-ness first. 5511 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5512 return true; 5513 5514 // Truncate to the given size. 5515 Result = Result.getLoBits(ArgBits); 5516 Result.setIsUnsigned(true); 5517 5518 if (IsShiftedByte(Result)) 5519 return false; 5520 5521 return Diag(TheCall->getBeginLoc(), diag::err_argument_not_shifted_byte) 5522 << Arg->getSourceRange(); 5523 } 5524 5525 bool Sema::BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum, 5526 unsigned ArgBits) { 5527 llvm::APSInt Result; 5528 5529 // We can't check the value of a dependent argument. 5530 Expr *Arg = TheCall->getArg(ArgNum); 5531 if (Arg->isTypeDependent() || Arg->isValueDependent()) 5532 return false; 5533 5534 // Check constant-ness first. 5535 if (BuiltinConstantArg(TheCall, ArgNum, Result)) 5536 return true; 5537 5538 // Truncate to the given size. 5539 Result = Result.getLoBits(ArgBits); 5540 Result.setIsUnsigned(true); 5541 5542 // Check to see if it's in either of the required forms. 5543 if (IsShiftedByte(Result) || 5544 (Result > 0 && Result < 0x10000 && (Result & 0xFF) == 0xFF)) 5545 return false; 5546 5547 return Diag(TheCall->getBeginLoc(), 5548 diag::err_argument_not_shifted_byte_or_xxff) 5549 << Arg->getSourceRange(); 5550 } 5551 5552 bool Sema::BuiltinLongjmp(CallExpr *TheCall) { 5553 if (!Context.getTargetInfo().hasSjLjLowering()) 5554 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_unsupported) 5555 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5556 5557 Expr *Arg = TheCall->getArg(1); 5558 llvm::APSInt Result; 5559 5560 // TODO: This is less than ideal. Overload this to take a value. 5561 if (BuiltinConstantArg(TheCall, 1, Result)) 5562 return true; 5563 5564 if (Result != 1) 5565 return Diag(TheCall->getBeginLoc(), diag::err_builtin_longjmp_invalid_val) 5566 << SourceRange(Arg->getBeginLoc(), Arg->getEndLoc()); 5567 5568 return false; 5569 } 5570 5571 bool Sema::BuiltinSetjmp(CallExpr *TheCall) { 5572 if (!Context.getTargetInfo().hasSjLjLowering()) 5573 return Diag(TheCall->getBeginLoc(), diag::err_builtin_setjmp_unsupported) 5574 << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); 5575 return false; 5576 } 5577 5578 namespace { 5579 5580 class UncoveredArgHandler { 5581 enum { Unknown = -1, AllCovered = -2 }; 5582 5583 signed FirstUncoveredArg = Unknown; 5584 SmallVector<const Expr *, 4> DiagnosticExprs; 5585 5586 public: 5587 UncoveredArgHandler() = default; 5588 5589 bool hasUncoveredArg() const { 5590 return (FirstUncoveredArg >= 0); 5591 } 5592 5593 unsigned getUncoveredArg() const { 5594 assert(hasUncoveredArg() && "no uncovered argument"); 5595 return FirstUncoveredArg; 5596 } 5597 5598 void setAllCovered() { 5599 // A string has been found with all arguments covered, so clear out 5600 // the diagnostics. 5601 DiagnosticExprs.clear(); 5602 FirstUncoveredArg = AllCovered; 5603 } 5604 5605 void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) { 5606 assert(NewFirstUncoveredArg >= 0 && "Outside range"); 5607 5608 // Don't update if a previous string covers all arguments. 5609 if (FirstUncoveredArg == AllCovered) 5610 return; 5611 5612 // UncoveredArgHandler tracks the highest uncovered argument index 5613 // and with it all the strings that match this index. 5614 if (NewFirstUncoveredArg == FirstUncoveredArg) 5615 DiagnosticExprs.push_back(StrExpr); 5616 else if (NewFirstUncoveredArg > FirstUncoveredArg) { 5617 DiagnosticExprs.clear(); 5618 DiagnosticExprs.push_back(StrExpr); 5619 FirstUncoveredArg = NewFirstUncoveredArg; 5620 } 5621 } 5622 5623 void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr); 5624 }; 5625 5626 enum StringLiteralCheckType { 5627 SLCT_NotALiteral, 5628 SLCT_UncheckedLiteral, 5629 SLCT_CheckedLiteral 5630 }; 5631 5632 } // namespace 5633 5634 static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend, 5635 BinaryOperatorKind BinOpKind, 5636 bool AddendIsRight) { 5637 unsigned BitWidth = Offset.getBitWidth(); 5638 unsigned AddendBitWidth = Addend.getBitWidth(); 5639 // There might be negative interim results. 5640 if (Addend.isUnsigned()) { 5641 Addend = Addend.zext(++AddendBitWidth); 5642 Addend.setIsSigned(true); 5643 } 5644 // Adjust the bit width of the APSInts. 5645 if (AddendBitWidth > BitWidth) { 5646 Offset = Offset.sext(AddendBitWidth); 5647 BitWidth = AddendBitWidth; 5648 } else if (BitWidth > AddendBitWidth) { 5649 Addend = Addend.sext(BitWidth); 5650 } 5651 5652 bool Ov = false; 5653 llvm::APSInt ResOffset = Offset; 5654 if (BinOpKind == BO_Add) 5655 ResOffset = Offset.sadd_ov(Addend, Ov); 5656 else { 5657 assert(AddendIsRight && BinOpKind == BO_Sub && 5658 "operator must be add or sub with addend on the right"); 5659 ResOffset = Offset.ssub_ov(Addend, Ov); 5660 } 5661 5662 // We add an offset to a pointer here so we should support an offset as big as 5663 // possible. 5664 if (Ov) { 5665 assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 && 5666 "index (intermediate) result too big"); 5667 Offset = Offset.sext(2 * BitWidth); 5668 sumOffsets(Offset, Addend, BinOpKind, AddendIsRight); 5669 return; 5670 } 5671 5672 Offset = ResOffset; 5673 } 5674 5675 namespace { 5676 5677 // This is a wrapper class around StringLiteral to support offsetted string 5678 // literals as format strings. It takes the offset into account when returning 5679 // the string and its length or the source locations to display notes correctly. 5680 class FormatStringLiteral { 5681 const StringLiteral *FExpr; 5682 int64_t Offset; 5683 5684 public: 5685 FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0) 5686 : FExpr(fexpr), Offset(Offset) {} 5687 5688 StringRef getString() const { 5689 return FExpr->getString().drop_front(Offset); 5690 } 5691 5692 unsigned getByteLength() const { 5693 return FExpr->getByteLength() - getCharByteWidth() * Offset; 5694 } 5695 5696 unsigned getLength() const { return FExpr->getLength() - Offset; } 5697 unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); } 5698 5699 StringLiteralKind getKind() const { return FExpr->getKind(); } 5700 5701 QualType getType() const { return FExpr->getType(); } 5702 5703 bool isAscii() const { return FExpr->isOrdinary(); } 5704 bool isWide() const { return FExpr->isWide(); } 5705 bool isUTF8() const { return FExpr->isUTF8(); } 5706 bool isUTF16() const { return FExpr->isUTF16(); } 5707 bool isUTF32() const { return FExpr->isUTF32(); } 5708 bool isPascal() const { return FExpr->isPascal(); } 5709 5710 SourceLocation getLocationOfByte( 5711 unsigned ByteNo, const SourceManager &SM, const LangOptions &Features, 5712 const TargetInfo &Target, unsigned *StartToken = nullptr, 5713 unsigned *StartTokenByteOffset = nullptr) const { 5714 return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target, 5715 StartToken, StartTokenByteOffset); 5716 } 5717 5718 SourceLocation getBeginLoc() const LLVM_READONLY { 5719 return FExpr->getBeginLoc().getLocWithOffset(Offset); 5720 } 5721 5722 SourceLocation getEndLoc() const LLVM_READONLY { return FExpr->getEndLoc(); } 5723 }; 5724 5725 } // namespace 5726 5727 static void CheckFormatString( 5728 Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, 5729 ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, 5730 unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, 5731 bool inFunctionCall, Sema::VariadicCallType CallType, 5732 llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, 5733 bool IgnoreStringsWithoutSpecifiers); 5734 5735 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, 5736 const Expr *E); 5737 5738 // Determine if an expression is a string literal or constant string. 5739 // If this function returns false on the arguments to a function expecting a 5740 // format string, we will usually need to emit a warning. 5741 // True string literals are then checked by CheckFormatString. 5742 static StringLiteralCheckType 5743 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, 5744 Sema::FormatArgumentPassingKind APK, unsigned format_idx, 5745 unsigned firstDataArg, Sema::FormatStringType Type, 5746 Sema::VariadicCallType CallType, bool InFunctionCall, 5747 llvm::SmallBitVector &CheckedVarArgs, 5748 UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset, 5749 bool IgnoreStringsWithoutSpecifiers = false) { 5750 if (S.isConstantEvaluatedContext()) 5751 return SLCT_NotALiteral; 5752 tryAgain: 5753 assert(Offset.isSigned() && "invalid offset"); 5754 5755 if (E->isTypeDependent() || E->isValueDependent()) 5756 return SLCT_NotALiteral; 5757 5758 E = E->IgnoreParenCasts(); 5759 5760 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) 5761 // Technically -Wformat-nonliteral does not warn about this case. 5762 // The behavior of printf and friends in this case is implementation 5763 // dependent. Ideally if the format string cannot be null then 5764 // it should have a 'nonnull' attribute in the function prototype. 5765 return SLCT_UncheckedLiteral; 5766 5767 switch (E->getStmtClass()) { 5768 case Stmt::InitListExprClass: 5769 // Handle expressions like {"foobar"}. 5770 if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) { 5771 return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, 5772 Type, CallType, /*InFunctionCall*/ false, 5773 CheckedVarArgs, UncoveredArg, Offset, 5774 IgnoreStringsWithoutSpecifiers); 5775 } 5776 return SLCT_NotALiteral; 5777 case Stmt::BinaryConditionalOperatorClass: 5778 case Stmt::ConditionalOperatorClass: { 5779 // The expression is a literal if both sub-expressions were, and it was 5780 // completely checked only if both sub-expressions were checked. 5781 const AbstractConditionalOperator *C = 5782 cast<AbstractConditionalOperator>(E); 5783 5784 // Determine whether it is necessary to check both sub-expressions, for 5785 // example, because the condition expression is a constant that can be 5786 // evaluated at compile time. 5787 bool CheckLeft = true, CheckRight = true; 5788 5789 bool Cond; 5790 if (C->getCond()->EvaluateAsBooleanCondition( 5791 Cond, S.getASTContext(), S.isConstantEvaluatedContext())) { 5792 if (Cond) 5793 CheckRight = false; 5794 else 5795 CheckLeft = false; 5796 } 5797 5798 // We need to maintain the offsets for the right and the left hand side 5799 // separately to check if every possible indexed expression is a valid 5800 // string literal. They might have different offsets for different string 5801 // literals in the end. 5802 StringLiteralCheckType Left; 5803 if (!CheckLeft) 5804 Left = SLCT_UncheckedLiteral; 5805 else { 5806 Left = checkFormatStringExpr(S, C->getTrueExpr(), Args, APK, format_idx, 5807 firstDataArg, Type, CallType, InFunctionCall, 5808 CheckedVarArgs, UncoveredArg, Offset, 5809 IgnoreStringsWithoutSpecifiers); 5810 if (Left == SLCT_NotALiteral || !CheckRight) { 5811 return Left; 5812 } 5813 } 5814 5815 StringLiteralCheckType Right = checkFormatStringExpr( 5816 S, C->getFalseExpr(), Args, APK, format_idx, firstDataArg, Type, 5817 CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 5818 IgnoreStringsWithoutSpecifiers); 5819 5820 return (CheckLeft && Left < Right) ? Left : Right; 5821 } 5822 5823 case Stmt::ImplicitCastExprClass: 5824 E = cast<ImplicitCastExpr>(E)->getSubExpr(); 5825 goto tryAgain; 5826 5827 case Stmt::OpaqueValueExprClass: 5828 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { 5829 E = src; 5830 goto tryAgain; 5831 } 5832 return SLCT_NotALiteral; 5833 5834 case Stmt::PredefinedExprClass: 5835 // While __func__, etc., are technically not string literals, they 5836 // cannot contain format specifiers and thus are not a security 5837 // liability. 5838 return SLCT_UncheckedLiteral; 5839 5840 case Stmt::DeclRefExprClass: { 5841 const DeclRefExpr *DR = cast<DeclRefExpr>(E); 5842 5843 // As an exception, do not flag errors for variables binding to 5844 // const string literals. 5845 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 5846 bool isConstant = false; 5847 QualType T = DR->getType(); 5848 5849 if (const ArrayType *AT = S.Context.getAsArrayType(T)) { 5850 isConstant = AT->getElementType().isConstant(S.Context); 5851 } else if (const PointerType *PT = T->getAs<PointerType>()) { 5852 isConstant = T.isConstant(S.Context) && 5853 PT->getPointeeType().isConstant(S.Context); 5854 } else if (T->isObjCObjectPointerType()) { 5855 // In ObjC, there is usually no "const ObjectPointer" type, 5856 // so don't check if the pointee type is constant. 5857 isConstant = T.isConstant(S.Context); 5858 } 5859 5860 if (isConstant) { 5861 if (const Expr *Init = VD->getAnyInitializer()) { 5862 // Look through initializers like const char c[] = { "foo" } 5863 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { 5864 if (InitList->isStringLiteralInit()) 5865 Init = InitList->getInit(0)->IgnoreParenImpCasts(); 5866 } 5867 return checkFormatStringExpr( 5868 S, Init, Args, APK, format_idx, firstDataArg, Type, CallType, 5869 /*InFunctionCall*/ false, CheckedVarArgs, UncoveredArg, Offset); 5870 } 5871 } 5872 5873 // When the format argument is an argument of this function, and this 5874 // function also has the format attribute, there are several interactions 5875 // for which there shouldn't be a warning. For instance, when calling 5876 // v*printf from a function that has the printf format attribute, we 5877 // should not emit a warning about using `fmt`, even though it's not 5878 // constant, because the arguments have already been checked for the 5879 // caller of `logmessage`: 5880 // 5881 // __attribute__((format(printf, 1, 2))) 5882 // void logmessage(char const *fmt, ...) { 5883 // va_list ap; 5884 // va_start(ap, fmt); 5885 // vprintf(fmt, ap); /* do not emit a warning about "fmt" */ 5886 // ... 5887 // } 5888 // 5889 // Another interaction that we need to support is calling a variadic 5890 // format function from a format function that has fixed arguments. For 5891 // instance: 5892 // 5893 // __attribute__((format(printf, 1, 2))) 5894 // void logstring(char const *fmt, char const *str) { 5895 // printf(fmt, str); /* do not emit a warning about "fmt" */ 5896 // } 5897 // 5898 // Same (and perhaps more relatably) for the variadic template case: 5899 // 5900 // template<typename... Args> 5901 // __attribute__((format(printf, 1, 2))) 5902 // void log(const char *fmt, Args&&... args) { 5903 // printf(fmt, forward<Args>(args)...); 5904 // /* do not emit a warning about "fmt" */ 5905 // } 5906 // 5907 // Due to implementation difficulty, we only check the format, not the 5908 // format arguments, in all cases. 5909 // 5910 if (const auto *PV = dyn_cast<ParmVarDecl>(VD)) { 5911 if (const auto *D = dyn_cast<Decl>(PV->getDeclContext())) { 5912 for (const auto *PVFormat : D->specific_attrs<FormatAttr>()) { 5913 bool IsCXXMember = false; 5914 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) 5915 IsCXXMember = MD->isInstance(); 5916 5917 bool IsVariadic = false; 5918 if (const FunctionType *FnTy = D->getFunctionType()) 5919 IsVariadic = cast<FunctionProtoType>(FnTy)->isVariadic(); 5920 else if (const auto *BD = dyn_cast<BlockDecl>(D)) 5921 IsVariadic = BD->isVariadic(); 5922 else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) 5923 IsVariadic = OMD->isVariadic(); 5924 5925 Sema::FormatStringInfo CallerFSI; 5926 if (Sema::getFormatStringInfo(PVFormat, IsCXXMember, IsVariadic, 5927 &CallerFSI)) { 5928 // We also check if the formats are compatible. 5929 // We can't pass a 'scanf' string to a 'printf' function. 5930 if (PV->getFunctionScopeIndex() == CallerFSI.FormatIdx && 5931 Type == S.GetFormatStringType(PVFormat)) { 5932 // Lastly, check that argument passing kinds transition in a 5933 // way that makes sense: 5934 // from a caller with FAPK_VAList, allow FAPK_VAList 5935 // from a caller with FAPK_Fixed, allow FAPK_Fixed 5936 // from a caller with FAPK_Fixed, allow FAPK_Variadic 5937 // from a caller with FAPK_Variadic, allow FAPK_VAList 5938 switch (combineFAPK(CallerFSI.ArgPassingKind, APK)) { 5939 case combineFAPK(Sema::FAPK_VAList, Sema::FAPK_VAList): 5940 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Fixed): 5941 case combineFAPK(Sema::FAPK_Fixed, Sema::FAPK_Variadic): 5942 case combineFAPK(Sema::FAPK_Variadic, Sema::FAPK_VAList): 5943 return SLCT_UncheckedLiteral; 5944 } 5945 } 5946 } 5947 } 5948 } 5949 } 5950 } 5951 5952 return SLCT_NotALiteral; 5953 } 5954 5955 case Stmt::CallExprClass: 5956 case Stmt::CXXMemberCallExprClass: { 5957 const CallExpr *CE = cast<CallExpr>(E); 5958 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { 5959 bool IsFirst = true; 5960 StringLiteralCheckType CommonResult; 5961 for (const auto *FA : ND->specific_attrs<FormatArgAttr>()) { 5962 const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex()); 5963 StringLiteralCheckType Result = checkFormatStringExpr( 5964 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 5965 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 5966 IgnoreStringsWithoutSpecifiers); 5967 if (IsFirst) { 5968 CommonResult = Result; 5969 IsFirst = false; 5970 } 5971 } 5972 if (!IsFirst) 5973 return CommonResult; 5974 5975 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { 5976 unsigned BuiltinID = FD->getBuiltinID(); 5977 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || 5978 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { 5979 const Expr *Arg = CE->getArg(0); 5980 return checkFormatStringExpr( 5981 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 5982 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 5983 IgnoreStringsWithoutSpecifiers); 5984 } 5985 } 5986 } 5987 if (const Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) 5988 return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg, 5989 Type, CallType, /*InFunctionCall*/ false, 5990 CheckedVarArgs, UncoveredArg, Offset, 5991 IgnoreStringsWithoutSpecifiers); 5992 return SLCT_NotALiteral; 5993 } 5994 case Stmt::ObjCMessageExprClass: { 5995 const auto *ME = cast<ObjCMessageExpr>(E); 5996 if (const auto *MD = ME->getMethodDecl()) { 5997 if (const auto *FA = MD->getAttr<FormatArgAttr>()) { 5998 // As a special case heuristic, if we're using the method -[NSBundle 5999 // localizedStringForKey:value:table:], ignore any key strings that lack 6000 // format specifiers. The idea is that if the key doesn't have any 6001 // format specifiers then its probably just a key to map to the 6002 // localized strings. If it does have format specifiers though, then its 6003 // likely that the text of the key is the format string in the 6004 // programmer's language, and should be checked. 6005 const ObjCInterfaceDecl *IFace; 6006 if (MD->isInstanceMethod() && (IFace = MD->getClassInterface()) && 6007 IFace->getIdentifier()->isStr("NSBundle") && 6008 MD->getSelector().isKeywordSelector( 6009 {"localizedStringForKey", "value", "table"})) { 6010 IgnoreStringsWithoutSpecifiers = true; 6011 } 6012 6013 const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex()); 6014 return checkFormatStringExpr( 6015 S, Arg, Args, APK, format_idx, firstDataArg, Type, CallType, 6016 InFunctionCall, CheckedVarArgs, UncoveredArg, Offset, 6017 IgnoreStringsWithoutSpecifiers); 6018 } 6019 } 6020 6021 return SLCT_NotALiteral; 6022 } 6023 case Stmt::ObjCStringLiteralClass: 6024 case Stmt::StringLiteralClass: { 6025 const StringLiteral *StrE = nullptr; 6026 6027 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) 6028 StrE = ObjCFExpr->getString(); 6029 else 6030 StrE = cast<StringLiteral>(E); 6031 6032 if (StrE) { 6033 if (Offset.isNegative() || Offset > StrE->getLength()) { 6034 // TODO: It would be better to have an explicit warning for out of 6035 // bounds literals. 6036 return SLCT_NotALiteral; 6037 } 6038 FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue()); 6039 CheckFormatString(S, &FStr, E, Args, APK, format_idx, firstDataArg, Type, 6040 InFunctionCall, CallType, CheckedVarArgs, UncoveredArg, 6041 IgnoreStringsWithoutSpecifiers); 6042 return SLCT_CheckedLiteral; 6043 } 6044 6045 return SLCT_NotALiteral; 6046 } 6047 case Stmt::BinaryOperatorClass: { 6048 const BinaryOperator *BinOp = cast<BinaryOperator>(E); 6049 6050 // A string literal + an int offset is still a string literal. 6051 if (BinOp->isAdditiveOp()) { 6052 Expr::EvalResult LResult, RResult; 6053 6054 bool LIsInt = BinOp->getLHS()->EvaluateAsInt( 6055 LResult, S.Context, Expr::SE_NoSideEffects, 6056 S.isConstantEvaluatedContext()); 6057 bool RIsInt = BinOp->getRHS()->EvaluateAsInt( 6058 RResult, S.Context, Expr::SE_NoSideEffects, 6059 S.isConstantEvaluatedContext()); 6060 6061 if (LIsInt != RIsInt) { 6062 BinaryOperatorKind BinOpKind = BinOp->getOpcode(); 6063 6064 if (LIsInt) { 6065 if (BinOpKind == BO_Add) { 6066 sumOffsets(Offset, LResult.Val.getInt(), BinOpKind, RIsInt); 6067 E = BinOp->getRHS(); 6068 goto tryAgain; 6069 } 6070 } else { 6071 sumOffsets(Offset, RResult.Val.getInt(), BinOpKind, RIsInt); 6072 E = BinOp->getLHS(); 6073 goto tryAgain; 6074 } 6075 } 6076 } 6077 6078 return SLCT_NotALiteral; 6079 } 6080 case Stmt::UnaryOperatorClass: { 6081 const UnaryOperator *UnaOp = cast<UnaryOperator>(E); 6082 auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr()); 6083 if (UnaOp->getOpcode() == UO_AddrOf && ASE) { 6084 Expr::EvalResult IndexResult; 6085 if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context, 6086 Expr::SE_NoSideEffects, 6087 S.isConstantEvaluatedContext())) { 6088 sumOffsets(Offset, IndexResult.Val.getInt(), BO_Add, 6089 /*RHS is int*/ true); 6090 E = ASE->getBase(); 6091 goto tryAgain; 6092 } 6093 } 6094 6095 return SLCT_NotALiteral; 6096 } 6097 6098 default: 6099 return SLCT_NotALiteral; 6100 } 6101 } 6102 6103 // If this expression can be evaluated at compile-time, 6104 // check if the result is a StringLiteral and return it 6105 // otherwise return nullptr 6106 static const Expr *maybeConstEvalStringLiteral(ASTContext &Context, 6107 const Expr *E) { 6108 Expr::EvalResult Result; 6109 if (E->EvaluateAsRValue(Result, Context) && Result.Val.isLValue()) { 6110 const auto *LVE = Result.Val.getLValueBase().dyn_cast<const Expr *>(); 6111 if (isa_and_nonnull<StringLiteral>(LVE)) 6112 return LVE; 6113 } 6114 return nullptr; 6115 } 6116 6117 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { 6118 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) 6119 .Case("scanf", FST_Scanf) 6120 .Cases("printf", "printf0", "syslog", FST_Printf) 6121 .Cases("NSString", "CFString", FST_NSString) 6122 .Case("strftime", FST_Strftime) 6123 .Case("strfmon", FST_Strfmon) 6124 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) 6125 .Case("freebsd_kprintf", FST_FreeBSDKPrintf) 6126 .Case("os_trace", FST_OSLog) 6127 .Case("os_log", FST_OSLog) 6128 .Default(FST_Unknown); 6129 } 6130 6131 bool Sema::CheckFormatArguments(const FormatAttr *Format, 6132 ArrayRef<const Expr *> Args, bool IsCXXMember, 6133 VariadicCallType CallType, SourceLocation Loc, 6134 SourceRange Range, 6135 llvm::SmallBitVector &CheckedVarArgs) { 6136 FormatStringInfo FSI; 6137 if (getFormatStringInfo(Format, IsCXXMember, CallType != VariadicDoesNotApply, 6138 &FSI)) 6139 return CheckFormatArguments(Args, FSI.ArgPassingKind, FSI.FormatIdx, 6140 FSI.FirstDataArg, GetFormatStringType(Format), 6141 CallType, Loc, Range, CheckedVarArgs); 6142 return false; 6143 } 6144 6145 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, 6146 Sema::FormatArgumentPassingKind APK, 6147 unsigned format_idx, unsigned firstDataArg, 6148 FormatStringType Type, 6149 VariadicCallType CallType, SourceLocation Loc, 6150 SourceRange Range, 6151 llvm::SmallBitVector &CheckedVarArgs) { 6152 // CHECK: printf/scanf-like function is called with no format string. 6153 if (format_idx >= Args.size()) { 6154 Diag(Loc, diag::warn_missing_format_string) << Range; 6155 return false; 6156 } 6157 6158 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); 6159 6160 // CHECK: format string is not a string literal. 6161 // 6162 // Dynamically generated format strings are difficult to 6163 // automatically vet at compile time. Requiring that format strings 6164 // are string literals: (1) permits the checking of format strings by 6165 // the compiler and thereby (2) can practically remove the source of 6166 // many format string exploits. 6167 6168 // Format string can be either ObjC string (e.g. @"%d") or 6169 // C string (e.g. "%d") 6170 // ObjC string uses the same format specifiers as C string, so we can use 6171 // the same format string checking logic for both ObjC and C strings. 6172 UncoveredArgHandler UncoveredArg; 6173 StringLiteralCheckType CT = checkFormatStringExpr( 6174 *this, OrigFormatExpr, Args, APK, format_idx, firstDataArg, Type, 6175 CallType, 6176 /*IsFunctionCall*/ true, CheckedVarArgs, UncoveredArg, 6177 /*no string offset*/ llvm::APSInt(64, false) = 0); 6178 6179 // Generate a diagnostic where an uncovered argument is detected. 6180 if (UncoveredArg.hasUncoveredArg()) { 6181 unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg; 6182 assert(ArgIdx < Args.size() && "ArgIdx outside bounds"); 6183 UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]); 6184 } 6185 6186 if (CT != SLCT_NotALiteral) 6187 // Literal format string found, check done! 6188 return CT == SLCT_CheckedLiteral; 6189 6190 // Strftime is particular as it always uses a single 'time' argument, 6191 // so it is safe to pass a non-literal string. 6192 if (Type == FST_Strftime) 6193 return false; 6194 6195 // Do not emit diag when the string param is a macro expansion and the 6196 // format is either NSString or CFString. This is a hack to prevent 6197 // diag when using the NSLocalizedString and CFCopyLocalizedString macros 6198 // which are usually used in place of NS and CF string literals. 6199 SourceLocation FormatLoc = Args[format_idx]->getBeginLoc(); 6200 if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc)) 6201 return false; 6202 6203 // If there are no arguments specified, warn with -Wformat-security, otherwise 6204 // warn only with -Wformat-nonliteral. 6205 if (Args.size() == firstDataArg) { 6206 Diag(FormatLoc, diag::warn_format_nonliteral_noargs) 6207 << OrigFormatExpr->getSourceRange(); 6208 switch (Type) { 6209 default: 6210 break; 6211 case FST_Kprintf: 6212 case FST_FreeBSDKPrintf: 6213 case FST_Printf: 6214 case FST_Syslog: 6215 Diag(FormatLoc, diag::note_format_security_fixit) 6216 << FixItHint::CreateInsertion(FormatLoc, "\"%s\", "); 6217 break; 6218 case FST_NSString: 6219 Diag(FormatLoc, diag::note_format_security_fixit) 6220 << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", "); 6221 break; 6222 } 6223 } else { 6224 Diag(FormatLoc, diag::warn_format_nonliteral) 6225 << OrigFormatExpr->getSourceRange(); 6226 } 6227 return false; 6228 } 6229 6230 namespace { 6231 6232 class CheckFormatHandler : public analyze_format_string::FormatStringHandler { 6233 protected: 6234 Sema &S; 6235 const FormatStringLiteral *FExpr; 6236 const Expr *OrigFormatExpr; 6237 const Sema::FormatStringType FSType; 6238 const unsigned FirstDataArg; 6239 const unsigned NumDataArgs; 6240 const char *Beg; // Start of format string. 6241 const Sema::FormatArgumentPassingKind ArgPassingKind; 6242 ArrayRef<const Expr *> Args; 6243 unsigned FormatIdx; 6244 llvm::SmallBitVector CoveredArgs; 6245 bool usesPositionalArgs = false; 6246 bool atFirstArg = true; 6247 bool inFunctionCall; 6248 Sema::VariadicCallType CallType; 6249 llvm::SmallBitVector &CheckedVarArgs; 6250 UncoveredArgHandler &UncoveredArg; 6251 6252 public: 6253 CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr, 6254 const Expr *origFormatExpr, 6255 const Sema::FormatStringType type, unsigned firstDataArg, 6256 unsigned numDataArgs, const char *beg, 6257 Sema::FormatArgumentPassingKind APK, 6258 ArrayRef<const Expr *> Args, unsigned formatIdx, 6259 bool inFunctionCall, Sema::VariadicCallType callType, 6260 llvm::SmallBitVector &CheckedVarArgs, 6261 UncoveredArgHandler &UncoveredArg) 6262 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type), 6263 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg), 6264 ArgPassingKind(APK), Args(Args), FormatIdx(formatIdx), 6265 inFunctionCall(inFunctionCall), CallType(callType), 6266 CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) { 6267 CoveredArgs.resize(numDataArgs); 6268 CoveredArgs.reset(); 6269 } 6270 6271 void DoneProcessing(); 6272 6273 void HandleIncompleteSpecifier(const char *startSpecifier, 6274 unsigned specifierLen) override; 6275 6276 void HandleInvalidLengthModifier( 6277 const analyze_format_string::FormatSpecifier &FS, 6278 const analyze_format_string::ConversionSpecifier &CS, 6279 const char *startSpecifier, unsigned specifierLen, 6280 unsigned DiagID); 6281 6282 void HandleNonStandardLengthModifier( 6283 const analyze_format_string::FormatSpecifier &FS, 6284 const char *startSpecifier, unsigned specifierLen); 6285 6286 void HandleNonStandardConversionSpecifier( 6287 const analyze_format_string::ConversionSpecifier &CS, 6288 const char *startSpecifier, unsigned specifierLen); 6289 6290 void HandlePosition(const char *startPos, unsigned posLen) override; 6291 6292 void HandleInvalidPosition(const char *startSpecifier, 6293 unsigned specifierLen, 6294 analyze_format_string::PositionContext p) override; 6295 6296 void HandleZeroPosition(const char *startPos, unsigned posLen) override; 6297 6298 void HandleNullChar(const char *nullCharacter) override; 6299 6300 template <typename Range> 6301 static void 6302 EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr, 6303 const PartialDiagnostic &PDiag, SourceLocation StringLoc, 6304 bool IsStringLocation, Range StringRange, 6305 ArrayRef<FixItHint> Fixit = {}); 6306 6307 protected: 6308 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, 6309 const char *startSpec, 6310 unsigned specifierLen, 6311 const char *csStart, unsigned csLen); 6312 6313 void HandlePositionalNonpositionalArgs(SourceLocation Loc, 6314 const char *startSpec, 6315 unsigned specifierLen); 6316 6317 SourceRange getFormatStringRange(); 6318 CharSourceRange getSpecifierRange(const char *startSpecifier, 6319 unsigned specifierLen); 6320 SourceLocation getLocationOfByte(const char *x); 6321 6322 const Expr *getDataArg(unsigned i) const; 6323 6324 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, 6325 const analyze_format_string::ConversionSpecifier &CS, 6326 const char *startSpecifier, unsigned specifierLen, 6327 unsigned argIndex); 6328 6329 template <typename Range> 6330 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, 6331 bool IsStringLocation, Range StringRange, 6332 ArrayRef<FixItHint> Fixit = {}); 6333 }; 6334 6335 } // namespace 6336 6337 SourceRange CheckFormatHandler::getFormatStringRange() { 6338 return OrigFormatExpr->getSourceRange(); 6339 } 6340 6341 CharSourceRange CheckFormatHandler:: 6342 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { 6343 SourceLocation Start = getLocationOfByte(startSpecifier); 6344 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); 6345 6346 // Advance the end SourceLocation by one due to half-open ranges. 6347 End = End.getLocWithOffset(1); 6348 6349 return CharSourceRange::getCharRange(Start, End); 6350 } 6351 6352 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { 6353 return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(), 6354 S.getLangOpts(), S.Context.getTargetInfo()); 6355 } 6356 6357 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, 6358 unsigned specifierLen){ 6359 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), 6360 getLocationOfByte(startSpecifier), 6361 /*IsStringLocation*/true, 6362 getSpecifierRange(startSpecifier, specifierLen)); 6363 } 6364 6365 void CheckFormatHandler::HandleInvalidLengthModifier( 6366 const analyze_format_string::FormatSpecifier &FS, 6367 const analyze_format_string::ConversionSpecifier &CS, 6368 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { 6369 using namespace analyze_format_string; 6370 6371 const LengthModifier &LM = FS.getLengthModifier(); 6372 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6373 6374 // See if we know how to fix this length modifier. 6375 std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6376 if (FixedLM) { 6377 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6378 getLocationOfByte(LM.getStart()), 6379 /*IsStringLocation*/true, 6380 getSpecifierRange(startSpecifier, specifierLen)); 6381 6382 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6383 << FixedLM->toString() 6384 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6385 6386 } else { 6387 FixItHint Hint; 6388 if (DiagID == diag::warn_format_nonsensical_length) 6389 Hint = FixItHint::CreateRemoval(LMRange); 6390 6391 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 6392 getLocationOfByte(LM.getStart()), 6393 /*IsStringLocation*/true, 6394 getSpecifierRange(startSpecifier, specifierLen), 6395 Hint); 6396 } 6397 } 6398 6399 void CheckFormatHandler::HandleNonStandardLengthModifier( 6400 const analyze_format_string::FormatSpecifier &FS, 6401 const char *startSpecifier, unsigned specifierLen) { 6402 using namespace analyze_format_string; 6403 6404 const LengthModifier &LM = FS.getLengthModifier(); 6405 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 6406 6407 // See if we know how to fix this length modifier. 6408 std::optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 6409 if (FixedLM) { 6410 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6411 << LM.toString() << 0, 6412 getLocationOfByte(LM.getStart()), 6413 /*IsStringLocation*/true, 6414 getSpecifierRange(startSpecifier, specifierLen)); 6415 6416 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 6417 << FixedLM->toString() 6418 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 6419 6420 } else { 6421 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6422 << LM.toString() << 0, 6423 getLocationOfByte(LM.getStart()), 6424 /*IsStringLocation*/true, 6425 getSpecifierRange(startSpecifier, specifierLen)); 6426 } 6427 } 6428 6429 void CheckFormatHandler::HandleNonStandardConversionSpecifier( 6430 const analyze_format_string::ConversionSpecifier &CS, 6431 const char *startSpecifier, unsigned specifierLen) { 6432 using namespace analyze_format_string; 6433 6434 // See if we know how to fix this conversion specifier. 6435 std::optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); 6436 if (FixedCS) { 6437 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6438 << CS.toString() << /*conversion specifier*/1, 6439 getLocationOfByte(CS.getStart()), 6440 /*IsStringLocation*/true, 6441 getSpecifierRange(startSpecifier, specifierLen)); 6442 6443 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); 6444 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) 6445 << FixedCS->toString() 6446 << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); 6447 } else { 6448 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 6449 << CS.toString() << /*conversion specifier*/1, 6450 getLocationOfByte(CS.getStart()), 6451 /*IsStringLocation*/true, 6452 getSpecifierRange(startSpecifier, specifierLen)); 6453 } 6454 } 6455 6456 void CheckFormatHandler::HandlePosition(const char *startPos, 6457 unsigned posLen) { 6458 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), 6459 getLocationOfByte(startPos), 6460 /*IsStringLocation*/true, 6461 getSpecifierRange(startPos, posLen)); 6462 } 6463 6464 void CheckFormatHandler::HandleInvalidPosition( 6465 const char *startSpecifier, unsigned specifierLen, 6466 analyze_format_string::PositionContext p) { 6467 EmitFormatDiagnostic( 6468 S.PDiag(diag::warn_format_invalid_positional_specifier) << (unsigned)p, 6469 getLocationOfByte(startSpecifier), /*IsStringLocation*/ true, 6470 getSpecifierRange(startSpecifier, specifierLen)); 6471 } 6472 6473 void CheckFormatHandler::HandleZeroPosition(const char *startPos, 6474 unsigned posLen) { 6475 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), 6476 getLocationOfByte(startPos), 6477 /*IsStringLocation*/true, 6478 getSpecifierRange(startPos, posLen)); 6479 } 6480 6481 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { 6482 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { 6483 // The presence of a null character is likely an error. 6484 EmitFormatDiagnostic( 6485 S.PDiag(diag::warn_printf_format_string_contains_null_char), 6486 getLocationOfByte(nullCharacter), /*IsStringLocation*/true, 6487 getFormatStringRange()); 6488 } 6489 } 6490 6491 // Note that this may return NULL if there was an error parsing or building 6492 // one of the argument expressions. 6493 const Expr *CheckFormatHandler::getDataArg(unsigned i) const { 6494 return Args[FirstDataArg + i]; 6495 } 6496 6497 void CheckFormatHandler::DoneProcessing() { 6498 // Does the number of data arguments exceed the number of 6499 // format conversions in the format string? 6500 if (ArgPassingKind != Sema::FAPK_VAList) { 6501 // Find any arguments that weren't covered. 6502 CoveredArgs.flip(); 6503 signed notCoveredArg = CoveredArgs.find_first(); 6504 if (notCoveredArg >= 0) { 6505 assert((unsigned)notCoveredArg < NumDataArgs); 6506 UncoveredArg.Update(notCoveredArg, OrigFormatExpr); 6507 } else { 6508 UncoveredArg.setAllCovered(); 6509 } 6510 } 6511 } 6512 6513 void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall, 6514 const Expr *ArgExpr) { 6515 assert(hasUncoveredArg() && !DiagnosticExprs.empty() && 6516 "Invalid state"); 6517 6518 if (!ArgExpr) 6519 return; 6520 6521 SourceLocation Loc = ArgExpr->getBeginLoc(); 6522 6523 if (S.getSourceManager().isInSystemMacro(Loc)) 6524 return; 6525 6526 PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used); 6527 for (auto E : DiagnosticExprs) 6528 PDiag << E->getSourceRange(); 6529 6530 CheckFormatHandler::EmitFormatDiagnostic( 6531 S, IsFunctionCall, DiagnosticExprs[0], 6532 PDiag, Loc, /*IsStringLocation*/false, 6533 DiagnosticExprs[0]->getSourceRange()); 6534 } 6535 6536 bool 6537 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, 6538 SourceLocation Loc, 6539 const char *startSpec, 6540 unsigned specifierLen, 6541 const char *csStart, 6542 unsigned csLen) { 6543 bool keepGoing = true; 6544 if (argIndex < NumDataArgs) { 6545 // Consider the argument coverered, even though the specifier doesn't 6546 // make sense. 6547 CoveredArgs.set(argIndex); 6548 } 6549 else { 6550 // If argIndex exceeds the number of data arguments we 6551 // don't issue a warning because that is just a cascade of warnings (and 6552 // they may have intended '%%' anyway). We don't want to continue processing 6553 // the format string after this point, however, as we will like just get 6554 // gibberish when trying to match arguments. 6555 keepGoing = false; 6556 } 6557 6558 StringRef Specifier(csStart, csLen); 6559 6560 // If the specifier in non-printable, it could be the first byte of a UTF-8 6561 // sequence. In that case, print the UTF-8 code point. If not, print the byte 6562 // hex value. 6563 std::string CodePointStr; 6564 if (!llvm::sys::locale::isPrint(*csStart)) { 6565 llvm::UTF32 CodePoint; 6566 const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart); 6567 const llvm::UTF8 *E = 6568 reinterpret_cast<const llvm::UTF8 *>(csStart + csLen); 6569 llvm::ConversionResult Result = 6570 llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion); 6571 6572 if (Result != llvm::conversionOK) { 6573 unsigned char FirstChar = *csStart; 6574 CodePoint = (llvm::UTF32)FirstChar; 6575 } 6576 6577 llvm::raw_string_ostream OS(CodePointStr); 6578 if (CodePoint < 256) 6579 OS << "\\x" << llvm::format("%02x", CodePoint); 6580 else if (CodePoint <= 0xFFFF) 6581 OS << "\\u" << llvm::format("%04x", CodePoint); 6582 else 6583 OS << "\\U" << llvm::format("%08x", CodePoint); 6584 Specifier = CodePointStr; 6585 } 6586 6587 EmitFormatDiagnostic( 6588 S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc, 6589 /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen)); 6590 6591 return keepGoing; 6592 } 6593 6594 void 6595 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, 6596 const char *startSpec, 6597 unsigned specifierLen) { 6598 EmitFormatDiagnostic( 6599 S.PDiag(diag::warn_format_mix_positional_nonpositional_args), 6600 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); 6601 } 6602 6603 bool 6604 CheckFormatHandler::CheckNumArgs( 6605 const analyze_format_string::FormatSpecifier &FS, 6606 const analyze_format_string::ConversionSpecifier &CS, 6607 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { 6608 6609 if (argIndex >= NumDataArgs) { 6610 PartialDiagnostic PDiag = FS.usesPositionalArg() 6611 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) 6612 << (argIndex+1) << NumDataArgs) 6613 : S.PDiag(diag::warn_printf_insufficient_data_args); 6614 EmitFormatDiagnostic( 6615 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, 6616 getSpecifierRange(startSpecifier, specifierLen)); 6617 6618 // Since more arguments than conversion tokens are given, by extension 6619 // all arguments are covered, so mark this as so. 6620 UncoveredArg.setAllCovered(); 6621 return false; 6622 } 6623 return true; 6624 } 6625 6626 template<typename Range> 6627 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, 6628 SourceLocation Loc, 6629 bool IsStringLocation, 6630 Range StringRange, 6631 ArrayRef<FixItHint> FixIt) { 6632 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, 6633 Loc, IsStringLocation, StringRange, FixIt); 6634 } 6635 6636 /// If the format string is not within the function call, emit a note 6637 /// so that the function call and string are in diagnostic messages. 6638 /// 6639 /// \param InFunctionCall if true, the format string is within the function 6640 /// call and only one diagnostic message will be produced. Otherwise, an 6641 /// extra note will be emitted pointing to location of the format string. 6642 /// 6643 /// \param ArgumentExpr the expression that is passed as the format string 6644 /// argument in the function call. Used for getting locations when two 6645 /// diagnostics are emitted. 6646 /// 6647 /// \param PDiag the callee should already have provided any strings for the 6648 /// diagnostic message. This function only adds locations and fixits 6649 /// to diagnostics. 6650 /// 6651 /// \param Loc primary location for diagnostic. If two diagnostics are 6652 /// required, one will be at Loc and a new SourceLocation will be created for 6653 /// the other one. 6654 /// 6655 /// \param IsStringLocation if true, Loc points to the format string should be 6656 /// used for the note. Otherwise, Loc points to the argument list and will 6657 /// be used with PDiag. 6658 /// 6659 /// \param StringRange some or all of the string to highlight. This is 6660 /// templated so it can accept either a CharSourceRange or a SourceRange. 6661 /// 6662 /// \param FixIt optional fix it hint for the format string. 6663 template <typename Range> 6664 void CheckFormatHandler::EmitFormatDiagnostic( 6665 Sema &S, bool InFunctionCall, const Expr *ArgumentExpr, 6666 const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation, 6667 Range StringRange, ArrayRef<FixItHint> FixIt) { 6668 if (InFunctionCall) { 6669 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); 6670 D << StringRange; 6671 D << FixIt; 6672 } else { 6673 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) 6674 << ArgumentExpr->getSourceRange(); 6675 6676 const Sema::SemaDiagnosticBuilder &Note = 6677 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), 6678 diag::note_format_string_defined); 6679 6680 Note << StringRange; 6681 Note << FixIt; 6682 } 6683 } 6684 6685 //===--- CHECK: Printf format string checking -----------------------------===// 6686 6687 namespace { 6688 6689 class CheckPrintfHandler : public CheckFormatHandler { 6690 public: 6691 CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr, 6692 const Expr *origFormatExpr, 6693 const Sema::FormatStringType type, unsigned firstDataArg, 6694 unsigned numDataArgs, bool isObjC, const char *beg, 6695 Sema::FormatArgumentPassingKind APK, 6696 ArrayRef<const Expr *> Args, unsigned formatIdx, 6697 bool inFunctionCall, Sema::VariadicCallType CallType, 6698 llvm::SmallBitVector &CheckedVarArgs, 6699 UncoveredArgHandler &UncoveredArg) 6700 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 6701 numDataArgs, beg, APK, Args, formatIdx, 6702 inFunctionCall, CallType, CheckedVarArgs, 6703 UncoveredArg) {} 6704 6705 bool isObjCContext() const { return FSType == Sema::FST_NSString; } 6706 6707 /// Returns true if '%@' specifiers are allowed in the format string. 6708 bool allowsObjCArg() const { 6709 return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog || 6710 FSType == Sema::FST_OSTrace; 6711 } 6712 6713 bool HandleInvalidPrintfConversionSpecifier( 6714 const analyze_printf::PrintfSpecifier &FS, 6715 const char *startSpecifier, 6716 unsigned specifierLen) override; 6717 6718 void handleInvalidMaskType(StringRef MaskType) override; 6719 6720 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 6721 const char *startSpecifier, unsigned specifierLen, 6722 const TargetInfo &Target) override; 6723 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 6724 const char *StartSpecifier, 6725 unsigned SpecifierLen, 6726 const Expr *E); 6727 6728 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, 6729 const char *startSpecifier, unsigned specifierLen); 6730 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, 6731 const analyze_printf::OptionalAmount &Amt, 6732 unsigned type, 6733 const char *startSpecifier, unsigned specifierLen); 6734 void HandleFlag(const analyze_printf::PrintfSpecifier &FS, 6735 const analyze_printf::OptionalFlag &flag, 6736 const char *startSpecifier, unsigned specifierLen); 6737 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, 6738 const analyze_printf::OptionalFlag &ignoredFlag, 6739 const analyze_printf::OptionalFlag &flag, 6740 const char *startSpecifier, unsigned specifierLen); 6741 bool checkForCStrMembers(const analyze_printf::ArgType &AT, 6742 const Expr *E); 6743 6744 void HandleEmptyObjCModifierFlag(const char *startFlag, 6745 unsigned flagLen) override; 6746 6747 void HandleInvalidObjCModifierFlag(const char *startFlag, 6748 unsigned flagLen) override; 6749 6750 void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart, 6751 const char *flagsEnd, 6752 const char *conversionPosition) 6753 override; 6754 }; 6755 6756 } // namespace 6757 6758 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( 6759 const analyze_printf::PrintfSpecifier &FS, 6760 const char *startSpecifier, 6761 unsigned specifierLen) { 6762 const analyze_printf::PrintfConversionSpecifier &CS = 6763 FS.getConversionSpecifier(); 6764 6765 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 6766 getLocationOfByte(CS.getStart()), 6767 startSpecifier, specifierLen, 6768 CS.getStart(), CS.getLength()); 6769 } 6770 6771 void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) { 6772 S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size); 6773 } 6774 6775 bool CheckPrintfHandler::HandleAmount( 6776 const analyze_format_string::OptionalAmount &Amt, unsigned k, 6777 const char *startSpecifier, unsigned specifierLen) { 6778 if (Amt.hasDataArgument()) { 6779 if (ArgPassingKind != Sema::FAPK_VAList) { 6780 unsigned argIndex = Amt.getArgIndex(); 6781 if (argIndex >= NumDataArgs) { 6782 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) 6783 << k, 6784 getLocationOfByte(Amt.getStart()), 6785 /*IsStringLocation*/ true, 6786 getSpecifierRange(startSpecifier, specifierLen)); 6787 // Don't do any more checking. We will just emit 6788 // spurious errors. 6789 return false; 6790 } 6791 6792 // Type check the data argument. It should be an 'int'. 6793 // Although not in conformance with C99, we also allow the argument to be 6794 // an 'unsigned int' as that is a reasonably safe case. GCC also 6795 // doesn't emit a warning for that case. 6796 CoveredArgs.set(argIndex); 6797 const Expr *Arg = getDataArg(argIndex); 6798 if (!Arg) 6799 return false; 6800 6801 QualType T = Arg->getType(); 6802 6803 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); 6804 assert(AT.isValid()); 6805 6806 if (!AT.matchesType(S.Context, T)) { 6807 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) 6808 << k << AT.getRepresentativeTypeName(S.Context) 6809 << T << Arg->getSourceRange(), 6810 getLocationOfByte(Amt.getStart()), 6811 /*IsStringLocation*/true, 6812 getSpecifierRange(startSpecifier, specifierLen)); 6813 // Don't do any more checking. We will just emit 6814 // spurious errors. 6815 return false; 6816 } 6817 } 6818 } 6819 return true; 6820 } 6821 6822 void CheckPrintfHandler::HandleInvalidAmount( 6823 const analyze_printf::PrintfSpecifier &FS, 6824 const analyze_printf::OptionalAmount &Amt, 6825 unsigned type, 6826 const char *startSpecifier, 6827 unsigned specifierLen) { 6828 const analyze_printf::PrintfConversionSpecifier &CS = 6829 FS.getConversionSpecifier(); 6830 6831 FixItHint fixit = 6832 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant 6833 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), 6834 Amt.getConstantLength())) 6835 : FixItHint(); 6836 6837 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) 6838 << type << CS.toString(), 6839 getLocationOfByte(Amt.getStart()), 6840 /*IsStringLocation*/true, 6841 getSpecifierRange(startSpecifier, specifierLen), 6842 fixit); 6843 } 6844 6845 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, 6846 const analyze_printf::OptionalFlag &flag, 6847 const char *startSpecifier, 6848 unsigned specifierLen) { 6849 // Warn about pointless flag with a fixit removal. 6850 const analyze_printf::PrintfConversionSpecifier &CS = 6851 FS.getConversionSpecifier(); 6852 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) 6853 << flag.toString() << CS.toString(), 6854 getLocationOfByte(flag.getPosition()), 6855 /*IsStringLocation*/true, 6856 getSpecifierRange(startSpecifier, specifierLen), 6857 FixItHint::CreateRemoval( 6858 getSpecifierRange(flag.getPosition(), 1))); 6859 } 6860 6861 void CheckPrintfHandler::HandleIgnoredFlag( 6862 const analyze_printf::PrintfSpecifier &FS, 6863 const analyze_printf::OptionalFlag &ignoredFlag, 6864 const analyze_printf::OptionalFlag &flag, 6865 const char *startSpecifier, 6866 unsigned specifierLen) { 6867 // Warn about ignored flag with a fixit removal. 6868 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) 6869 << ignoredFlag.toString() << flag.toString(), 6870 getLocationOfByte(ignoredFlag.getPosition()), 6871 /*IsStringLocation*/true, 6872 getSpecifierRange(startSpecifier, specifierLen), 6873 FixItHint::CreateRemoval( 6874 getSpecifierRange(ignoredFlag.getPosition(), 1))); 6875 } 6876 6877 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag, 6878 unsigned flagLen) { 6879 // Warn about an empty flag. 6880 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag), 6881 getLocationOfByte(startFlag), 6882 /*IsStringLocation*/true, 6883 getSpecifierRange(startFlag, flagLen)); 6884 } 6885 6886 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag, 6887 unsigned flagLen) { 6888 // Warn about an invalid flag. 6889 auto Range = getSpecifierRange(startFlag, flagLen); 6890 StringRef flag(startFlag, flagLen); 6891 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag, 6892 getLocationOfByte(startFlag), 6893 /*IsStringLocation*/true, 6894 Range, FixItHint::CreateRemoval(Range)); 6895 } 6896 6897 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion( 6898 const char *flagsStart, const char *flagsEnd, const char *conversionPosition) { 6899 // Warn about using '[...]' without a '@' conversion. 6900 auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1); 6901 auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion; 6902 EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1), 6903 getLocationOfByte(conversionPosition), 6904 /*IsStringLocation*/true, 6905 Range, FixItHint::CreateRemoval(Range)); 6906 } 6907 6908 // Determines if the specified is a C++ class or struct containing 6909 // a member with the specified name and kind (e.g. a CXXMethodDecl named 6910 // "c_str()"). 6911 template<typename MemberKind> 6912 static llvm::SmallPtrSet<MemberKind*, 1> 6913 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { 6914 const RecordType *RT = Ty->getAs<RecordType>(); 6915 llvm::SmallPtrSet<MemberKind*, 1> Results; 6916 6917 if (!RT) 6918 return Results; 6919 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 6920 if (!RD || !RD->getDefinition()) 6921 return Results; 6922 6923 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), 6924 Sema::LookupMemberName); 6925 R.suppressDiagnostics(); 6926 6927 // We just need to include all members of the right kind turned up by the 6928 // filter, at this point. 6929 if (S.LookupQualifiedName(R, RT->getDecl())) 6930 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 6931 NamedDecl *decl = (*I)->getUnderlyingDecl(); 6932 if (MemberKind *FK = dyn_cast<MemberKind>(decl)) 6933 Results.insert(FK); 6934 } 6935 return Results; 6936 } 6937 6938 /// Check if we could call '.c_str()' on an object. 6939 /// 6940 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't 6941 /// allow the call, or if it would be ambiguous). 6942 bool Sema::hasCStrMethod(const Expr *E) { 6943 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 6944 6945 MethodSet Results = 6946 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); 6947 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 6948 MI != ME; ++MI) 6949 if ((*MI)->getMinRequiredArguments() == 0) 6950 return true; 6951 return false; 6952 } 6953 6954 // Check if a (w)string was passed when a (w)char* was needed, and offer a 6955 // better diagnostic if so. AT is assumed to be valid. 6956 // Returns true when a c_str() conversion method is found. 6957 bool CheckPrintfHandler::checkForCStrMembers( 6958 const analyze_printf::ArgType &AT, const Expr *E) { 6959 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>; 6960 6961 MethodSet Results = 6962 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); 6963 6964 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 6965 MI != ME; ++MI) { 6966 const CXXMethodDecl *Method = *MI; 6967 if (Method->getMinRequiredArguments() == 0 && 6968 AT.matchesType(S.Context, Method->getReturnType())) { 6969 // FIXME: Suggest parens if the expression needs them. 6970 SourceLocation EndLoc = S.getLocForEndOfToken(E->getEndLoc()); 6971 S.Diag(E->getBeginLoc(), diag::note_printf_c_str) 6972 << "c_str()" << FixItHint::CreateInsertion(EndLoc, ".c_str()"); 6973 return true; 6974 } 6975 } 6976 6977 return false; 6978 } 6979 6980 bool CheckPrintfHandler::HandlePrintfSpecifier( 6981 const analyze_printf::PrintfSpecifier &FS, const char *startSpecifier, 6982 unsigned specifierLen, const TargetInfo &Target) { 6983 using namespace analyze_format_string; 6984 using namespace analyze_printf; 6985 6986 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); 6987 6988 if (FS.consumesDataArgument()) { 6989 if (atFirstArg) { 6990 atFirstArg = false; 6991 usesPositionalArgs = FS.usesPositionalArg(); 6992 } 6993 else if (usesPositionalArgs != FS.usesPositionalArg()) { 6994 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 6995 startSpecifier, specifierLen); 6996 return false; 6997 } 6998 } 6999 7000 // First check if the field width, precision, and conversion specifier 7001 // have matching data arguments. 7002 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, 7003 startSpecifier, specifierLen)) { 7004 return false; 7005 } 7006 7007 if (!HandleAmount(FS.getPrecision(), /* precision */ 1, 7008 startSpecifier, specifierLen)) { 7009 return false; 7010 } 7011 7012 if (!CS.consumesDataArgument()) { 7013 // FIXME: Technically specifying a precision or field width here 7014 // makes no sense. Worth issuing a warning at some point. 7015 return true; 7016 } 7017 7018 // Consume the argument. 7019 unsigned argIndex = FS.getArgIndex(); 7020 if (argIndex < NumDataArgs) { 7021 // The check to see if the argIndex is valid will come later. 7022 // We set the bit here because we may exit early from this 7023 // function if we encounter some other error. 7024 CoveredArgs.set(argIndex); 7025 } 7026 7027 // FreeBSD kernel extensions. 7028 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || 7029 CS.getKind() == ConversionSpecifier::FreeBSDDArg) { 7030 // We need at least two arguments. 7031 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) 7032 return false; 7033 7034 // Claim the second argument. 7035 CoveredArgs.set(argIndex + 1); 7036 7037 // Type check the first argument (int for %b, pointer for %D) 7038 const Expr *Ex = getDataArg(argIndex); 7039 const analyze_printf::ArgType &AT = 7040 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? 7041 ArgType(S.Context.IntTy) : ArgType::CPointerTy; 7042 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) 7043 EmitFormatDiagnostic( 7044 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7045 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() 7046 << false << Ex->getSourceRange(), 7047 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7048 getSpecifierRange(startSpecifier, specifierLen)); 7049 7050 // Type check the second argument (char * for both %b and %D) 7051 Ex = getDataArg(argIndex + 1); 7052 const analyze_printf::ArgType &AT2 = ArgType::CStrTy; 7053 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) 7054 EmitFormatDiagnostic( 7055 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7056 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() 7057 << false << Ex->getSourceRange(), 7058 Ex->getBeginLoc(), /*IsStringLocation*/ false, 7059 getSpecifierRange(startSpecifier, specifierLen)); 7060 7061 return true; 7062 } 7063 7064 // Check for using an Objective-C specific conversion specifier 7065 // in a non-ObjC literal. 7066 if (!allowsObjCArg() && CS.isObjCArg()) { 7067 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7068 specifierLen); 7069 } 7070 7071 // %P can only be used with os_log. 7072 if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) { 7073 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7074 specifierLen); 7075 } 7076 7077 // %n is not allowed with os_log. 7078 if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) { 7079 EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg), 7080 getLocationOfByte(CS.getStart()), 7081 /*IsStringLocation*/ false, 7082 getSpecifierRange(startSpecifier, specifierLen)); 7083 7084 return true; 7085 } 7086 7087 // Only scalars are allowed for os_trace. 7088 if (FSType == Sema::FST_OSTrace && 7089 (CS.getKind() == ConversionSpecifier::PArg || 7090 CS.getKind() == ConversionSpecifier::sArg || 7091 CS.getKind() == ConversionSpecifier::ObjCObjArg)) { 7092 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 7093 specifierLen); 7094 } 7095 7096 // Check for use of public/private annotation outside of os_log(). 7097 if (FSType != Sema::FST_OSLog) { 7098 if (FS.isPublic().isSet()) { 7099 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7100 << "public", 7101 getLocationOfByte(FS.isPublic().getPosition()), 7102 /*IsStringLocation*/ false, 7103 getSpecifierRange(startSpecifier, specifierLen)); 7104 } 7105 if (FS.isPrivate().isSet()) { 7106 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation) 7107 << "private", 7108 getLocationOfByte(FS.isPrivate().getPosition()), 7109 /*IsStringLocation*/ false, 7110 getSpecifierRange(startSpecifier, specifierLen)); 7111 } 7112 } 7113 7114 const llvm::Triple &Triple = Target.getTriple(); 7115 if (CS.getKind() == ConversionSpecifier::nArg && 7116 (Triple.isAndroid() || Triple.isOSFuchsia())) { 7117 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_narg_not_supported), 7118 getLocationOfByte(CS.getStart()), 7119 /*IsStringLocation*/ false, 7120 getSpecifierRange(startSpecifier, specifierLen)); 7121 } 7122 7123 // Check for invalid use of field width 7124 if (!FS.hasValidFieldWidth()) { 7125 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, 7126 startSpecifier, specifierLen); 7127 } 7128 7129 // Check for invalid use of precision 7130 if (!FS.hasValidPrecision()) { 7131 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, 7132 startSpecifier, specifierLen); 7133 } 7134 7135 // Precision is mandatory for %P specifier. 7136 if (CS.getKind() == ConversionSpecifier::PArg && 7137 FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) { 7138 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision), 7139 getLocationOfByte(startSpecifier), 7140 /*IsStringLocation*/ false, 7141 getSpecifierRange(startSpecifier, specifierLen)); 7142 } 7143 7144 // Check each flag does not conflict with any other component. 7145 if (!FS.hasValidThousandsGroupingPrefix()) 7146 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); 7147 if (!FS.hasValidLeadingZeros()) 7148 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); 7149 if (!FS.hasValidPlusPrefix()) 7150 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); 7151 if (!FS.hasValidSpacePrefix()) 7152 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); 7153 if (!FS.hasValidAlternativeForm()) 7154 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); 7155 if (!FS.hasValidLeftJustified()) 7156 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); 7157 7158 // Check that flags are not ignored by another flag 7159 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' 7160 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), 7161 startSpecifier, specifierLen); 7162 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' 7163 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), 7164 startSpecifier, specifierLen); 7165 7166 // Check the length modifier is valid with the given conversion specifier. 7167 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7168 S.getLangOpts())) 7169 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7170 diag::warn_format_nonsensical_length); 7171 else if (!FS.hasStandardLengthModifier()) 7172 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7173 else if (!FS.hasStandardLengthConversionCombination()) 7174 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7175 diag::warn_format_non_standard_conversion_spec); 7176 7177 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7178 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7179 7180 // The remaining checks depend on the data arguments. 7181 if (ArgPassingKind == Sema::FAPK_VAList) 7182 return true; 7183 7184 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7185 return false; 7186 7187 const Expr *Arg = getDataArg(argIndex); 7188 if (!Arg) 7189 return true; 7190 7191 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); 7192 } 7193 7194 static bool requiresParensToAddCast(const Expr *E) { 7195 // FIXME: We should have a general way to reason about operator 7196 // precedence and whether parens are actually needed here. 7197 // Take care of a few common cases where they aren't. 7198 const Expr *Inside = E->IgnoreImpCasts(); 7199 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) 7200 Inside = POE->getSyntacticForm()->IgnoreImpCasts(); 7201 7202 switch (Inside->getStmtClass()) { 7203 case Stmt::ArraySubscriptExprClass: 7204 case Stmt::CallExprClass: 7205 case Stmt::CharacterLiteralClass: 7206 case Stmt::CXXBoolLiteralExprClass: 7207 case Stmt::DeclRefExprClass: 7208 case Stmt::FloatingLiteralClass: 7209 case Stmt::IntegerLiteralClass: 7210 case Stmt::MemberExprClass: 7211 case Stmt::ObjCArrayLiteralClass: 7212 case Stmt::ObjCBoolLiteralExprClass: 7213 case Stmt::ObjCBoxedExprClass: 7214 case Stmt::ObjCDictionaryLiteralClass: 7215 case Stmt::ObjCEncodeExprClass: 7216 case Stmt::ObjCIvarRefExprClass: 7217 case Stmt::ObjCMessageExprClass: 7218 case Stmt::ObjCPropertyRefExprClass: 7219 case Stmt::ObjCStringLiteralClass: 7220 case Stmt::ObjCSubscriptRefExprClass: 7221 case Stmt::ParenExprClass: 7222 case Stmt::StringLiteralClass: 7223 case Stmt::UnaryOperatorClass: 7224 return false; 7225 default: 7226 return true; 7227 } 7228 } 7229 7230 static std::pair<QualType, StringRef> 7231 shouldNotPrintDirectly(const ASTContext &Context, 7232 QualType IntendedTy, 7233 const Expr *E) { 7234 // Use a 'while' to peel off layers of typedefs. 7235 QualType TyTy = IntendedTy; 7236 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { 7237 StringRef Name = UserTy->getDecl()->getName(); 7238 QualType CastTy = llvm::StringSwitch<QualType>(Name) 7239 .Case("CFIndex", Context.getNSIntegerType()) 7240 .Case("NSInteger", Context.getNSIntegerType()) 7241 .Case("NSUInteger", Context.getNSUIntegerType()) 7242 .Case("SInt32", Context.IntTy) 7243 .Case("UInt32", Context.UnsignedIntTy) 7244 .Default(QualType()); 7245 7246 if (!CastTy.isNull()) 7247 return std::make_pair(CastTy, Name); 7248 7249 TyTy = UserTy->desugar(); 7250 } 7251 7252 // Strip parens if necessary. 7253 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 7254 return shouldNotPrintDirectly(Context, 7255 PE->getSubExpr()->getType(), 7256 PE->getSubExpr()); 7257 7258 // If this is a conditional expression, then its result type is constructed 7259 // via usual arithmetic conversions and thus there might be no necessary 7260 // typedef sugar there. Recurse to operands to check for NSInteger & 7261 // Co. usage condition. 7262 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 7263 QualType TrueTy, FalseTy; 7264 StringRef TrueName, FalseName; 7265 7266 std::tie(TrueTy, TrueName) = 7267 shouldNotPrintDirectly(Context, 7268 CO->getTrueExpr()->getType(), 7269 CO->getTrueExpr()); 7270 std::tie(FalseTy, FalseName) = 7271 shouldNotPrintDirectly(Context, 7272 CO->getFalseExpr()->getType(), 7273 CO->getFalseExpr()); 7274 7275 if (TrueTy == FalseTy) 7276 return std::make_pair(TrueTy, TrueName); 7277 else if (TrueTy.isNull()) 7278 return std::make_pair(FalseTy, FalseName); 7279 else if (FalseTy.isNull()) 7280 return std::make_pair(TrueTy, TrueName); 7281 } 7282 7283 return std::make_pair(QualType(), StringRef()); 7284 } 7285 7286 /// Return true if \p ICE is an implicit argument promotion of an arithmetic 7287 /// type. Bit-field 'promotions' from a higher ranked type to a lower ranked 7288 /// type do not count. 7289 static bool 7290 isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) { 7291 QualType From = ICE->getSubExpr()->getType(); 7292 QualType To = ICE->getType(); 7293 // It's an integer promotion if the destination type is the promoted 7294 // source type. 7295 if (ICE->getCastKind() == CK_IntegralCast && 7296 S.Context.isPromotableIntegerType(From) && 7297 S.Context.getPromotedIntegerType(From) == To) 7298 return true; 7299 // Look through vector types, since we do default argument promotion for 7300 // those in OpenCL. 7301 if (const auto *VecTy = From->getAs<ExtVectorType>()) 7302 From = VecTy->getElementType(); 7303 if (const auto *VecTy = To->getAs<ExtVectorType>()) 7304 To = VecTy->getElementType(); 7305 // It's a floating promotion if the source type is a lower rank. 7306 return ICE->getCastKind() == CK_FloatingCast && 7307 S.Context.getFloatingTypeOrder(From, To) < 0; 7308 } 7309 7310 static analyze_format_string::ArgType::MatchKind 7311 handleFormatSignedness(analyze_format_string::ArgType::MatchKind Match, 7312 DiagnosticsEngine &Diags, SourceLocation Loc) { 7313 if (Match == analyze_format_string::ArgType::NoMatchSignedness) { 7314 Match = 7315 Diags.isIgnored( 7316 diag::warn_format_conversion_argument_type_mismatch_signedness, Loc) 7317 ? analyze_format_string::ArgType::Match 7318 : analyze_format_string::ArgType::NoMatch; 7319 } 7320 return Match; 7321 } 7322 7323 bool 7324 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 7325 const char *StartSpecifier, 7326 unsigned SpecifierLen, 7327 const Expr *E) { 7328 using namespace analyze_format_string; 7329 using namespace analyze_printf; 7330 7331 // Now type check the data expression that matches the 7332 // format specifier. 7333 const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext()); 7334 if (!AT.isValid()) 7335 return true; 7336 7337 QualType ExprTy = E->getType(); 7338 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { 7339 ExprTy = TET->getUnderlyingExpr()->getType(); 7340 } 7341 7342 // When using the format attribute in C++, you can receive a function or an 7343 // array that will necessarily decay to a pointer when passed to the final 7344 // format consumer. Apply decay before type comparison. 7345 if (ExprTy->canDecayToPointerType()) 7346 ExprTy = S.Context.getDecayedType(ExprTy); 7347 7348 // Diagnose attempts to print a boolean value as a character. Unlike other 7349 // -Wformat diagnostics, this is fine from a type perspective, but it still 7350 // doesn't make sense. 7351 if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::cArg && 7352 E->isKnownToHaveBooleanValue()) { 7353 const CharSourceRange &CSR = 7354 getSpecifierRange(StartSpecifier, SpecifierLen); 7355 SmallString<4> FSString; 7356 llvm::raw_svector_ostream os(FSString); 7357 FS.toString(os); 7358 EmitFormatDiagnostic(S.PDiag(diag::warn_format_bool_as_character) 7359 << FSString, 7360 E->getExprLoc(), false, CSR); 7361 return true; 7362 } 7363 7364 // Diagnose attempts to use '%P' with ObjC object types, which will result in 7365 // dumping raw class data (like is-a pointer), not actual data. 7366 if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::PArg && 7367 ExprTy->isObjCObjectPointerType()) { 7368 const CharSourceRange &CSR = 7369 getSpecifierRange(StartSpecifier, SpecifierLen); 7370 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_with_objc_pointer), 7371 E->getExprLoc(), false, CSR); 7372 return true; 7373 } 7374 7375 ArgType::MatchKind ImplicitMatch = ArgType::NoMatch; 7376 ArgType::MatchKind Match = AT.matchesType(S.Context, ExprTy); 7377 ArgType::MatchKind OrigMatch = Match; 7378 7379 Match = handleFormatSignedness(Match, S.getDiagnostics(), E->getExprLoc()); 7380 if (Match == ArgType::Match) 7381 return true; 7382 7383 // NoMatchPromotionTypeConfusion should be only returned in ImplictCastExpr 7384 assert(Match != ArgType::NoMatchPromotionTypeConfusion); 7385 7386 // Look through argument promotions for our error message's reported type. 7387 // This includes the integral and floating promotions, but excludes array 7388 // and function pointer decay (seeing that an argument intended to be a 7389 // string has type 'char [6]' is probably more confusing than 'char *') and 7390 // certain bitfield promotions (bitfields can be 'demoted' to a lesser type). 7391 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 7392 if (isArithmeticArgumentPromotion(S, ICE)) { 7393 E = ICE->getSubExpr(); 7394 ExprTy = E->getType(); 7395 7396 // Check if we didn't match because of an implicit cast from a 'char' 7397 // or 'short' to an 'int'. This is done because printf is a varargs 7398 // function. 7399 if (ICE->getType() == S.Context.IntTy || 7400 ICE->getType() == S.Context.UnsignedIntTy) { 7401 // All further checking is done on the subexpression 7402 ImplicitMatch = AT.matchesType(S.Context, ExprTy); 7403 if (OrigMatch == ArgType::NoMatchSignedness && 7404 ImplicitMatch != ArgType::NoMatchSignedness) 7405 // If the original match was a signedness match this match on the 7406 // implicit cast type also need to be signedness match otherwise we 7407 // might introduce new unexpected warnings from -Wformat-signedness. 7408 return true; 7409 ImplicitMatch = handleFormatSignedness( 7410 ImplicitMatch, S.getDiagnostics(), E->getExprLoc()); 7411 if (ImplicitMatch == ArgType::Match) 7412 return true; 7413 } 7414 } 7415 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { 7416 // Special case for 'a', which has type 'int' in C. 7417 // Note, however, that we do /not/ want to treat multibyte constants like 7418 // 'MooV' as characters! This form is deprecated but still exists. In 7419 // addition, don't treat expressions as of type 'char' if one byte length 7420 // modifier is provided. 7421 if (ExprTy == S.Context.IntTy && 7422 FS.getLengthModifier().getKind() != LengthModifier::AsChar) 7423 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) { 7424 ExprTy = S.Context.CharTy; 7425 // To improve check results, we consider a character literal in C 7426 // to be a 'char' rather than an 'int'. 'printf("%hd", 'a');' is 7427 // more likely a type confusion situation, so we will suggest to 7428 // use '%hhd' instead by discarding the MatchPromotion. 7429 if (Match == ArgType::MatchPromotion) 7430 Match = ArgType::NoMatch; 7431 } 7432 } 7433 if (Match == ArgType::MatchPromotion) { 7434 // WG14 N2562 only clarified promotions in *printf 7435 // For NSLog in ObjC, just preserve -Wformat behavior 7436 if (!S.getLangOpts().ObjC && 7437 ImplicitMatch != ArgType::NoMatchPromotionTypeConfusion && 7438 ImplicitMatch != ArgType::NoMatchTypeConfusion) 7439 return true; 7440 Match = ArgType::NoMatch; 7441 } 7442 if (ImplicitMatch == ArgType::NoMatchPedantic || 7443 ImplicitMatch == ArgType::NoMatchTypeConfusion) 7444 Match = ImplicitMatch; 7445 assert(Match != ArgType::MatchPromotion); 7446 7447 // Look through unscoped enums to their underlying type. 7448 bool IsEnum = false; 7449 bool IsScopedEnum = false; 7450 QualType IntendedTy = ExprTy; 7451 if (auto EnumTy = ExprTy->getAs<EnumType>()) { 7452 IntendedTy = EnumTy->getDecl()->getIntegerType(); 7453 if (EnumTy->isUnscopedEnumerationType()) { 7454 ExprTy = IntendedTy; 7455 // This controls whether we're talking about the underlying type or not, 7456 // which we only want to do when it's an unscoped enum. 7457 IsEnum = true; 7458 } else { 7459 IsScopedEnum = true; 7460 } 7461 } 7462 7463 // %C in an Objective-C context prints a unichar, not a wchar_t. 7464 // If the argument is an integer of some kind, believe the %C and suggest 7465 // a cast instead of changing the conversion specifier. 7466 if (isObjCContext() && 7467 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { 7468 if (ExprTy->isIntegralOrUnscopedEnumerationType() && 7469 !ExprTy->isCharType()) { 7470 // 'unichar' is defined as a typedef of unsigned short, but we should 7471 // prefer using the typedef if it is visible. 7472 IntendedTy = S.Context.UnsignedShortTy; 7473 7474 // While we are here, check if the value is an IntegerLiteral that happens 7475 // to be within the valid range. 7476 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { 7477 const llvm::APInt &V = IL->getValue(); 7478 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) 7479 return true; 7480 } 7481 7482 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getBeginLoc(), 7483 Sema::LookupOrdinaryName); 7484 if (S.LookupName(Result, S.getCurScope())) { 7485 NamedDecl *ND = Result.getFoundDecl(); 7486 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) 7487 if (TD->getUnderlyingType() == IntendedTy) 7488 IntendedTy = S.Context.getTypedefType(TD); 7489 } 7490 } 7491 } 7492 7493 // Special-case some of Darwin's platform-independence types by suggesting 7494 // casts to primitive types that are known to be large enough. 7495 bool ShouldNotPrintDirectly = false; StringRef CastTyName; 7496 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { 7497 QualType CastTy; 7498 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); 7499 if (!CastTy.isNull()) { 7500 // %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int 7501 // (long in ASTContext). Only complain to pedants or when they're the 7502 // underlying type of a scoped enum (which always needs a cast). 7503 if (!IsScopedEnum && 7504 (CastTyName == "NSInteger" || CastTyName == "NSUInteger") && 7505 (AT.isSizeT() || AT.isPtrdiffT()) && 7506 AT.matchesType(S.Context, CastTy)) 7507 Match = ArgType::NoMatchPedantic; 7508 IntendedTy = CastTy; 7509 ShouldNotPrintDirectly = true; 7510 } 7511 } 7512 7513 // We may be able to offer a FixItHint if it is a supported type. 7514 PrintfSpecifier fixedFS = FS; 7515 bool Success = 7516 fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext()); 7517 7518 if (Success) { 7519 // Get the fix string from the fixed format specifier 7520 SmallString<16> buf; 7521 llvm::raw_svector_ostream os(buf); 7522 fixedFS.toString(os); 7523 7524 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); 7525 7526 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) { 7527 unsigned Diag; 7528 switch (Match) { 7529 case ArgType::Match: 7530 case ArgType::MatchPromotion: 7531 case ArgType::NoMatchPromotionTypeConfusion: 7532 case ArgType::NoMatchSignedness: 7533 llvm_unreachable("expected non-matching"); 7534 case ArgType::NoMatchPedantic: 7535 Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; 7536 break; 7537 case ArgType::NoMatchTypeConfusion: 7538 Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; 7539 break; 7540 case ArgType::NoMatch: 7541 Diag = diag::warn_format_conversion_argument_type_mismatch; 7542 break; 7543 } 7544 7545 // In this case, the specifier is wrong and should be changed to match 7546 // the argument. 7547 EmitFormatDiagnostic(S.PDiag(Diag) 7548 << AT.getRepresentativeTypeName(S.Context) 7549 << IntendedTy << IsEnum << E->getSourceRange(), 7550 E->getBeginLoc(), 7551 /*IsStringLocation*/ false, SpecRange, 7552 FixItHint::CreateReplacement(SpecRange, os.str())); 7553 } else { 7554 // The canonical type for formatting this value is different from the 7555 // actual type of the expression. (This occurs, for example, with Darwin's 7556 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but 7557 // should be printed as 'long' for 64-bit compatibility.) 7558 // Rather than emitting a normal format/argument mismatch, we want to 7559 // add a cast to the recommended type (and correct the format string 7560 // if necessary). We should also do so for scoped enumerations. 7561 SmallString<16> CastBuf; 7562 llvm::raw_svector_ostream CastFix(CastBuf); 7563 CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); 7564 IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); 7565 CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); 7566 7567 SmallVector<FixItHint,4> Hints; 7568 ArgType::MatchKind IntendedMatch = AT.matchesType(S.Context, IntendedTy); 7569 IntendedMatch = handleFormatSignedness(IntendedMatch, S.getDiagnostics(), 7570 E->getExprLoc()); 7571 if ((IntendedMatch != ArgType::Match) || ShouldNotPrintDirectly) 7572 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); 7573 7574 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { 7575 // If there's already a cast present, just replace it. 7576 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); 7577 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); 7578 7579 } else if (!requiresParensToAddCast(E) && !S.LangOpts.CPlusPlus) { 7580 // If the expression has high enough precedence, 7581 // just write the C-style cast. 7582 Hints.push_back( 7583 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 7584 } else { 7585 // Otherwise, add parens around the expression as well as the cast. 7586 CastFix << "("; 7587 Hints.push_back( 7588 FixItHint::CreateInsertion(E->getBeginLoc(), CastFix.str())); 7589 7590 // We don't use getLocForEndOfToken because it returns invalid source 7591 // locations for macro expansions (by design). 7592 SourceLocation EndLoc = S.SourceMgr.getSpellingLoc(E->getEndLoc()); 7593 SourceLocation After = EndLoc.getLocWithOffset( 7594 Lexer::MeasureTokenLength(EndLoc, S.SourceMgr, S.LangOpts)); 7595 Hints.push_back(FixItHint::CreateInsertion(After, ")")); 7596 } 7597 7598 if (ShouldNotPrintDirectly && !IsScopedEnum) { 7599 // The expression has a type that should not be printed directly. 7600 // We extract the name from the typedef because we don't want to show 7601 // the underlying type in the diagnostic. 7602 StringRef Name; 7603 if (const auto *TypedefTy = ExprTy->getAs<TypedefType>()) 7604 Name = TypedefTy->getDecl()->getName(); 7605 else 7606 Name = CastTyName; 7607 unsigned Diag = Match == ArgType::NoMatchPedantic 7608 ? diag::warn_format_argument_needs_cast_pedantic 7609 : diag::warn_format_argument_needs_cast; 7610 EmitFormatDiagnostic(S.PDiag(Diag) << Name << IntendedTy << IsEnum 7611 << E->getSourceRange(), 7612 E->getBeginLoc(), /*IsStringLocation=*/false, 7613 SpecRange, Hints); 7614 } else { 7615 // In this case, the expression could be printed using a different 7616 // specifier, but we've decided that the specifier is probably correct 7617 // and we should cast instead. Just use the normal warning message. 7618 7619 unsigned Diag = 7620 IsScopedEnum 7621 ? diag::warn_format_conversion_argument_type_mismatch_pedantic 7622 : diag::warn_format_conversion_argument_type_mismatch; 7623 7624 EmitFormatDiagnostic( 7625 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 7626 << IsEnum << E->getSourceRange(), 7627 E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints); 7628 } 7629 } 7630 } else { 7631 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, 7632 SpecifierLen); 7633 // Since the warning for passing non-POD types to variadic functions 7634 // was deferred until now, we emit a warning for non-POD 7635 // arguments here. 7636 bool EmitTypeMismatch = false; 7637 switch (S.isValidVarArgType(ExprTy)) { 7638 case Sema::VAK_Valid: 7639 case Sema::VAK_ValidInCXX11: { 7640 unsigned Diag; 7641 switch (Match) { 7642 case ArgType::Match: 7643 case ArgType::MatchPromotion: 7644 case ArgType::NoMatchPromotionTypeConfusion: 7645 case ArgType::NoMatchSignedness: 7646 llvm_unreachable("expected non-matching"); 7647 case ArgType::NoMatchPedantic: 7648 Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic; 7649 break; 7650 case ArgType::NoMatchTypeConfusion: 7651 Diag = diag::warn_format_conversion_argument_type_mismatch_confusion; 7652 break; 7653 case ArgType::NoMatch: 7654 Diag = diag::warn_format_conversion_argument_type_mismatch; 7655 break; 7656 } 7657 7658 EmitFormatDiagnostic( 7659 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy 7660 << IsEnum << CSR << E->getSourceRange(), 7661 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7662 break; 7663 } 7664 case Sema::VAK_Undefined: 7665 case Sema::VAK_MSVCUndefined: 7666 if (CallType == Sema::VariadicDoesNotApply) { 7667 EmitTypeMismatch = true; 7668 } else { 7669 EmitFormatDiagnostic( 7670 S.PDiag(diag::warn_non_pod_vararg_with_format_string) 7671 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 7672 << AT.getRepresentativeTypeName(S.Context) << CSR 7673 << E->getSourceRange(), 7674 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7675 checkForCStrMembers(AT, E); 7676 } 7677 break; 7678 7679 case Sema::VAK_Invalid: 7680 if (CallType == Sema::VariadicDoesNotApply) 7681 EmitTypeMismatch = true; 7682 else if (ExprTy->isObjCObjectType()) 7683 EmitFormatDiagnostic( 7684 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) 7685 << S.getLangOpts().CPlusPlus11 << ExprTy << CallType 7686 << AT.getRepresentativeTypeName(S.Context) << CSR 7687 << E->getSourceRange(), 7688 E->getBeginLoc(), /*IsStringLocation*/ false, CSR); 7689 else 7690 // FIXME: If this is an initializer list, suggest removing the braces 7691 // or inserting a cast to the target type. 7692 S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format) 7693 << isa<InitListExpr>(E) << ExprTy << CallType 7694 << AT.getRepresentativeTypeName(S.Context) << E->getSourceRange(); 7695 break; 7696 } 7697 7698 if (EmitTypeMismatch) { 7699 // The function is not variadic, so we do not generate warnings about 7700 // being allowed to pass that object as a variadic argument. Instead, 7701 // since there are inherently no printf specifiers for types which cannot 7702 // be passed as variadic arguments, emit a plain old specifier mismatch 7703 // argument. 7704 EmitFormatDiagnostic( 7705 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 7706 << AT.getRepresentativeTypeName(S.Context) << ExprTy << false 7707 << E->getSourceRange(), 7708 E->getBeginLoc(), false, CSR); 7709 } 7710 7711 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && 7712 "format string specifier index out of range"); 7713 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; 7714 } 7715 7716 return true; 7717 } 7718 7719 //===--- CHECK: Scanf format string checking ------------------------------===// 7720 7721 namespace { 7722 7723 class CheckScanfHandler : public CheckFormatHandler { 7724 public: 7725 CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr, 7726 const Expr *origFormatExpr, Sema::FormatStringType type, 7727 unsigned firstDataArg, unsigned numDataArgs, 7728 const char *beg, Sema::FormatArgumentPassingKind APK, 7729 ArrayRef<const Expr *> Args, unsigned formatIdx, 7730 bool inFunctionCall, Sema::VariadicCallType CallType, 7731 llvm::SmallBitVector &CheckedVarArgs, 7732 UncoveredArgHandler &UncoveredArg) 7733 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg, 7734 numDataArgs, beg, APK, Args, formatIdx, 7735 inFunctionCall, CallType, CheckedVarArgs, 7736 UncoveredArg) {} 7737 7738 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 7739 const char *startSpecifier, 7740 unsigned specifierLen) override; 7741 7742 bool HandleInvalidScanfConversionSpecifier( 7743 const analyze_scanf::ScanfSpecifier &FS, 7744 const char *startSpecifier, 7745 unsigned specifierLen) override; 7746 7747 void HandleIncompleteScanList(const char *start, const char *end) override; 7748 }; 7749 7750 } // namespace 7751 7752 void CheckScanfHandler::HandleIncompleteScanList(const char *start, 7753 const char *end) { 7754 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), 7755 getLocationOfByte(end), /*IsStringLocation*/true, 7756 getSpecifierRange(start, end - start)); 7757 } 7758 7759 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( 7760 const analyze_scanf::ScanfSpecifier &FS, 7761 const char *startSpecifier, 7762 unsigned specifierLen) { 7763 const analyze_scanf::ScanfConversionSpecifier &CS = 7764 FS.getConversionSpecifier(); 7765 7766 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 7767 getLocationOfByte(CS.getStart()), 7768 startSpecifier, specifierLen, 7769 CS.getStart(), CS.getLength()); 7770 } 7771 7772 bool CheckScanfHandler::HandleScanfSpecifier( 7773 const analyze_scanf::ScanfSpecifier &FS, 7774 const char *startSpecifier, 7775 unsigned specifierLen) { 7776 using namespace analyze_scanf; 7777 using namespace analyze_format_string; 7778 7779 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); 7780 7781 // Handle case where '%' and '*' don't consume an argument. These shouldn't 7782 // be used to decide if we are using positional arguments consistently. 7783 if (FS.consumesDataArgument()) { 7784 if (atFirstArg) { 7785 atFirstArg = false; 7786 usesPositionalArgs = FS.usesPositionalArg(); 7787 } 7788 else if (usesPositionalArgs != FS.usesPositionalArg()) { 7789 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 7790 startSpecifier, specifierLen); 7791 return false; 7792 } 7793 } 7794 7795 // Check if the field with is non-zero. 7796 const OptionalAmount &Amt = FS.getFieldWidth(); 7797 if (Amt.getHowSpecified() == OptionalAmount::Constant) { 7798 if (Amt.getConstantAmount() == 0) { 7799 const CharSourceRange &R = getSpecifierRange(Amt.getStart(), 7800 Amt.getConstantLength()); 7801 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), 7802 getLocationOfByte(Amt.getStart()), 7803 /*IsStringLocation*/true, R, 7804 FixItHint::CreateRemoval(R)); 7805 } 7806 } 7807 7808 if (!FS.consumesDataArgument()) { 7809 // FIXME: Technically specifying a precision or field width here 7810 // makes no sense. Worth issuing a warning at some point. 7811 return true; 7812 } 7813 7814 // Consume the argument. 7815 unsigned argIndex = FS.getArgIndex(); 7816 if (argIndex < NumDataArgs) { 7817 // The check to see if the argIndex is valid will come later. 7818 // We set the bit here because we may exit early from this 7819 // function if we encounter some other error. 7820 CoveredArgs.set(argIndex); 7821 } 7822 7823 // Check the length modifier is valid with the given conversion specifier. 7824 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), 7825 S.getLangOpts())) 7826 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7827 diag::warn_format_nonsensical_length); 7828 else if (!FS.hasStandardLengthModifier()) 7829 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 7830 else if (!FS.hasStandardLengthConversionCombination()) 7831 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 7832 diag::warn_format_non_standard_conversion_spec); 7833 7834 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 7835 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 7836 7837 // The remaining checks depend on the data arguments. 7838 if (ArgPassingKind == Sema::FAPK_VAList) 7839 return true; 7840 7841 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 7842 return false; 7843 7844 // Check that the argument type matches the format specifier. 7845 const Expr *Ex = getDataArg(argIndex); 7846 if (!Ex) 7847 return true; 7848 7849 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); 7850 7851 if (!AT.isValid()) { 7852 return true; 7853 } 7854 7855 analyze_format_string::ArgType::MatchKind Match = 7856 AT.matchesType(S.Context, Ex->getType()); 7857 Match = handleFormatSignedness(Match, S.getDiagnostics(), Ex->getExprLoc()); 7858 bool Pedantic = Match == analyze_format_string::ArgType::NoMatchPedantic; 7859 if (Match == analyze_format_string::ArgType::Match) 7860 return true; 7861 7862 ScanfSpecifier fixedFS = FS; 7863 bool Success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(), 7864 S.getLangOpts(), S.Context); 7865 7866 unsigned Diag = 7867 Pedantic ? diag::warn_format_conversion_argument_type_mismatch_pedantic 7868 : diag::warn_format_conversion_argument_type_mismatch; 7869 7870 if (Success) { 7871 // Get the fix string from the fixed format specifier. 7872 SmallString<128> buf; 7873 llvm::raw_svector_ostream os(buf); 7874 fixedFS.toString(os); 7875 7876 EmitFormatDiagnostic( 7877 S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) 7878 << Ex->getType() << false << Ex->getSourceRange(), 7879 Ex->getBeginLoc(), 7880 /*IsStringLocation*/ false, 7881 getSpecifierRange(startSpecifier, specifierLen), 7882 FixItHint::CreateReplacement( 7883 getSpecifierRange(startSpecifier, specifierLen), os.str())); 7884 } else { 7885 EmitFormatDiagnostic(S.PDiag(Diag) 7886 << AT.getRepresentativeTypeName(S.Context) 7887 << Ex->getType() << false << Ex->getSourceRange(), 7888 Ex->getBeginLoc(), 7889 /*IsStringLocation*/ false, 7890 getSpecifierRange(startSpecifier, specifierLen)); 7891 } 7892 7893 return true; 7894 } 7895 7896 static void CheckFormatString( 7897 Sema &S, const FormatStringLiteral *FExpr, const Expr *OrigFormatExpr, 7898 ArrayRef<const Expr *> Args, Sema::FormatArgumentPassingKind APK, 7899 unsigned format_idx, unsigned firstDataArg, Sema::FormatStringType Type, 7900 bool inFunctionCall, Sema::VariadicCallType CallType, 7901 llvm::SmallBitVector &CheckedVarArgs, UncoveredArgHandler &UncoveredArg, 7902 bool IgnoreStringsWithoutSpecifiers) { 7903 // CHECK: is the format string a wide literal? 7904 if (!FExpr->isAscii() && !FExpr->isUTF8()) { 7905 CheckFormatHandler::EmitFormatDiagnostic( 7906 S, inFunctionCall, Args[format_idx], 7907 S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getBeginLoc(), 7908 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 7909 return; 7910 } 7911 7912 // Str - The format string. NOTE: this is NOT null-terminated! 7913 StringRef StrRef = FExpr->getString(); 7914 const char *Str = StrRef.data(); 7915 // Account for cases where the string literal is truncated in a declaration. 7916 const ConstantArrayType *T = 7917 S.Context.getAsConstantArrayType(FExpr->getType()); 7918 assert(T && "String literal not of constant array type!"); 7919 size_t TypeSize = T->getZExtSize(); 7920 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 7921 const unsigned numDataArgs = Args.size() - firstDataArg; 7922 7923 if (IgnoreStringsWithoutSpecifiers && 7924 !analyze_format_string::parseFormatStringHasFormattingSpecifiers( 7925 Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) 7926 return; 7927 7928 // Emit a warning if the string literal is truncated and does not contain an 7929 // embedded null character. 7930 if (TypeSize <= StrRef.size() && !StrRef.substr(0, TypeSize).contains('\0')) { 7931 CheckFormatHandler::EmitFormatDiagnostic( 7932 S, inFunctionCall, Args[format_idx], 7933 S.PDiag(diag::warn_printf_format_string_not_null_terminated), 7934 FExpr->getBeginLoc(), 7935 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); 7936 return; 7937 } 7938 7939 // CHECK: empty format string? 7940 if (StrLen == 0 && numDataArgs > 0) { 7941 CheckFormatHandler::EmitFormatDiagnostic( 7942 S, inFunctionCall, Args[format_idx], 7943 S.PDiag(diag::warn_empty_format_string), FExpr->getBeginLoc(), 7944 /*IsStringLocation*/ true, OrigFormatExpr->getSourceRange()); 7945 return; 7946 } 7947 7948 if (Type == Sema::FST_Printf || Type == Sema::FST_NSString || 7949 Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog || 7950 Type == Sema::FST_OSTrace || Type == Sema::FST_Syslog) { 7951 CheckPrintfHandler H( 7952 S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs, 7953 (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str, APK, 7954 Args, format_idx, inFunctionCall, CallType, CheckedVarArgs, 7955 UncoveredArg); 7956 7957 if (!analyze_format_string::ParsePrintfString( 7958 H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo(), 7959 Type == Sema::FST_FreeBSDKPrintf)) 7960 H.DoneProcessing(); 7961 } else if (Type == Sema::FST_Scanf) { 7962 CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg, 7963 numDataArgs, Str, APK, Args, format_idx, inFunctionCall, 7964 CallType, CheckedVarArgs, UncoveredArg); 7965 7966 if (!analyze_format_string::ParseScanfString( 7967 H, Str, Str + StrLen, S.getLangOpts(), S.Context.getTargetInfo())) 7968 H.DoneProcessing(); 7969 } // TODO: handle other formats 7970 } 7971 7972 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { 7973 // Str - The format string. NOTE: this is NOT null-terminated! 7974 StringRef StrRef = FExpr->getString(); 7975 const char *Str = StrRef.data(); 7976 // Account for cases where the string literal is truncated in a declaration. 7977 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); 7978 assert(T && "String literal not of constant array type!"); 7979 size_t TypeSize = T->getZExtSize(); 7980 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 7981 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, 7982 getLangOpts(), 7983 Context.getTargetInfo()); 7984 } 7985 7986 //===--- CHECK: Warn on use of wrong absolute value function. -------------===// 7987 7988 // Returns the related absolute value function that is larger, of 0 if one 7989 // does not exist. 7990 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { 7991 switch (AbsFunction) { 7992 default: 7993 return 0; 7994 7995 case Builtin::BI__builtin_abs: 7996 return Builtin::BI__builtin_labs; 7997 case Builtin::BI__builtin_labs: 7998 return Builtin::BI__builtin_llabs; 7999 case Builtin::BI__builtin_llabs: 8000 return 0; 8001 8002 case Builtin::BI__builtin_fabsf: 8003 return Builtin::BI__builtin_fabs; 8004 case Builtin::BI__builtin_fabs: 8005 return Builtin::BI__builtin_fabsl; 8006 case Builtin::BI__builtin_fabsl: 8007 return 0; 8008 8009 case Builtin::BI__builtin_cabsf: 8010 return Builtin::BI__builtin_cabs; 8011 case Builtin::BI__builtin_cabs: 8012 return Builtin::BI__builtin_cabsl; 8013 case Builtin::BI__builtin_cabsl: 8014 return 0; 8015 8016 case Builtin::BIabs: 8017 return Builtin::BIlabs; 8018 case Builtin::BIlabs: 8019 return Builtin::BIllabs; 8020 case Builtin::BIllabs: 8021 return 0; 8022 8023 case Builtin::BIfabsf: 8024 return Builtin::BIfabs; 8025 case Builtin::BIfabs: 8026 return Builtin::BIfabsl; 8027 case Builtin::BIfabsl: 8028 return 0; 8029 8030 case Builtin::BIcabsf: 8031 return Builtin::BIcabs; 8032 case Builtin::BIcabs: 8033 return Builtin::BIcabsl; 8034 case Builtin::BIcabsl: 8035 return 0; 8036 } 8037 } 8038 8039 // Returns the argument type of the absolute value function. 8040 static QualType getAbsoluteValueArgumentType(ASTContext &Context, 8041 unsigned AbsType) { 8042 if (AbsType == 0) 8043 return QualType(); 8044 8045 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; 8046 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); 8047 if (Error != ASTContext::GE_None) 8048 return QualType(); 8049 8050 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); 8051 if (!FT) 8052 return QualType(); 8053 8054 if (FT->getNumParams() != 1) 8055 return QualType(); 8056 8057 return FT->getParamType(0); 8058 } 8059 8060 // Returns the best absolute value function, or zero, based on type and 8061 // current absolute value function. 8062 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, 8063 unsigned AbsFunctionKind) { 8064 unsigned BestKind = 0; 8065 uint64_t ArgSize = Context.getTypeSize(ArgType); 8066 for (unsigned Kind = AbsFunctionKind; Kind != 0; 8067 Kind = getLargerAbsoluteValueFunction(Kind)) { 8068 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); 8069 if (Context.getTypeSize(ParamType) >= ArgSize) { 8070 if (BestKind == 0) 8071 BestKind = Kind; 8072 else if (Context.hasSameType(ParamType, ArgType)) { 8073 BestKind = Kind; 8074 break; 8075 } 8076 } 8077 } 8078 return BestKind; 8079 } 8080 8081 enum AbsoluteValueKind { 8082 AVK_Integer, 8083 AVK_Floating, 8084 AVK_Complex 8085 }; 8086 8087 static AbsoluteValueKind getAbsoluteValueKind(QualType T) { 8088 if (T->isIntegralOrEnumerationType()) 8089 return AVK_Integer; 8090 if (T->isRealFloatingType()) 8091 return AVK_Floating; 8092 if (T->isAnyComplexType()) 8093 return AVK_Complex; 8094 8095 llvm_unreachable("Type not integer, floating, or complex"); 8096 } 8097 8098 // Changes the absolute value function to a different type. Preserves whether 8099 // the function is a builtin. 8100 static unsigned changeAbsFunction(unsigned AbsKind, 8101 AbsoluteValueKind ValueKind) { 8102 switch (ValueKind) { 8103 case AVK_Integer: 8104 switch (AbsKind) { 8105 default: 8106 return 0; 8107 case Builtin::BI__builtin_fabsf: 8108 case Builtin::BI__builtin_fabs: 8109 case Builtin::BI__builtin_fabsl: 8110 case Builtin::BI__builtin_cabsf: 8111 case Builtin::BI__builtin_cabs: 8112 case Builtin::BI__builtin_cabsl: 8113 return Builtin::BI__builtin_abs; 8114 case Builtin::BIfabsf: 8115 case Builtin::BIfabs: 8116 case Builtin::BIfabsl: 8117 case Builtin::BIcabsf: 8118 case Builtin::BIcabs: 8119 case Builtin::BIcabsl: 8120 return Builtin::BIabs; 8121 } 8122 case AVK_Floating: 8123 switch (AbsKind) { 8124 default: 8125 return 0; 8126 case Builtin::BI__builtin_abs: 8127 case Builtin::BI__builtin_labs: 8128 case Builtin::BI__builtin_llabs: 8129 case Builtin::BI__builtin_cabsf: 8130 case Builtin::BI__builtin_cabs: 8131 case Builtin::BI__builtin_cabsl: 8132 return Builtin::BI__builtin_fabsf; 8133 case Builtin::BIabs: 8134 case Builtin::BIlabs: 8135 case Builtin::BIllabs: 8136 case Builtin::BIcabsf: 8137 case Builtin::BIcabs: 8138 case Builtin::BIcabsl: 8139 return Builtin::BIfabsf; 8140 } 8141 case AVK_Complex: 8142 switch (AbsKind) { 8143 default: 8144 return 0; 8145 case Builtin::BI__builtin_abs: 8146 case Builtin::BI__builtin_labs: 8147 case Builtin::BI__builtin_llabs: 8148 case Builtin::BI__builtin_fabsf: 8149 case Builtin::BI__builtin_fabs: 8150 case Builtin::BI__builtin_fabsl: 8151 return Builtin::BI__builtin_cabsf; 8152 case Builtin::BIabs: 8153 case Builtin::BIlabs: 8154 case Builtin::BIllabs: 8155 case Builtin::BIfabsf: 8156 case Builtin::BIfabs: 8157 case Builtin::BIfabsl: 8158 return Builtin::BIcabsf; 8159 } 8160 } 8161 llvm_unreachable("Unable to convert function"); 8162 } 8163 8164 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { 8165 const IdentifierInfo *FnInfo = FDecl->getIdentifier(); 8166 if (!FnInfo) 8167 return 0; 8168 8169 switch (FDecl->getBuiltinID()) { 8170 default: 8171 return 0; 8172 case Builtin::BI__builtin_abs: 8173 case Builtin::BI__builtin_fabs: 8174 case Builtin::BI__builtin_fabsf: 8175 case Builtin::BI__builtin_fabsl: 8176 case Builtin::BI__builtin_labs: 8177 case Builtin::BI__builtin_llabs: 8178 case Builtin::BI__builtin_cabs: 8179 case Builtin::BI__builtin_cabsf: 8180 case Builtin::BI__builtin_cabsl: 8181 case Builtin::BIabs: 8182 case Builtin::BIlabs: 8183 case Builtin::BIllabs: 8184 case Builtin::BIfabs: 8185 case Builtin::BIfabsf: 8186 case Builtin::BIfabsl: 8187 case Builtin::BIcabs: 8188 case Builtin::BIcabsf: 8189 case Builtin::BIcabsl: 8190 return FDecl->getBuiltinID(); 8191 } 8192 llvm_unreachable("Unknown Builtin type"); 8193 } 8194 8195 // If the replacement is valid, emit a note with replacement function. 8196 // Additionally, suggest including the proper header if not already included. 8197 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, 8198 unsigned AbsKind, QualType ArgType) { 8199 bool EmitHeaderHint = true; 8200 const char *HeaderName = nullptr; 8201 StringRef FunctionName; 8202 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { 8203 FunctionName = "std::abs"; 8204 if (ArgType->isIntegralOrEnumerationType()) { 8205 HeaderName = "cstdlib"; 8206 } else if (ArgType->isRealFloatingType()) { 8207 HeaderName = "cmath"; 8208 } else { 8209 llvm_unreachable("Invalid Type"); 8210 } 8211 8212 // Lookup all std::abs 8213 if (NamespaceDecl *Std = S.getStdNamespace()) { 8214 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); 8215 R.suppressDiagnostics(); 8216 S.LookupQualifiedName(R, Std); 8217 8218 for (const auto *I : R) { 8219 const FunctionDecl *FDecl = nullptr; 8220 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { 8221 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); 8222 } else { 8223 FDecl = dyn_cast<FunctionDecl>(I); 8224 } 8225 if (!FDecl) 8226 continue; 8227 8228 // Found std::abs(), check that they are the right ones. 8229 if (FDecl->getNumParams() != 1) 8230 continue; 8231 8232 // Check that the parameter type can handle the argument. 8233 QualType ParamType = FDecl->getParamDecl(0)->getType(); 8234 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && 8235 S.Context.getTypeSize(ArgType) <= 8236 S.Context.getTypeSize(ParamType)) { 8237 // Found a function, don't need the header hint. 8238 EmitHeaderHint = false; 8239 break; 8240 } 8241 } 8242 } 8243 } else { 8244 FunctionName = S.Context.BuiltinInfo.getName(AbsKind); 8245 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); 8246 8247 if (HeaderName) { 8248 DeclarationName DN(&S.Context.Idents.get(FunctionName)); 8249 LookupResult R(S, DN, Loc, Sema::LookupAnyName); 8250 R.suppressDiagnostics(); 8251 S.LookupName(R, S.getCurScope()); 8252 8253 if (R.isSingleResult()) { 8254 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); 8255 if (FD && FD->getBuiltinID() == AbsKind) { 8256 EmitHeaderHint = false; 8257 } else { 8258 return; 8259 } 8260 } else if (!R.empty()) { 8261 return; 8262 } 8263 } 8264 } 8265 8266 S.Diag(Loc, diag::note_replace_abs_function) 8267 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); 8268 8269 if (!HeaderName) 8270 return; 8271 8272 if (!EmitHeaderHint) 8273 return; 8274 8275 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName 8276 << FunctionName; 8277 } 8278 8279 template <std::size_t StrLen> 8280 static bool IsStdFunction(const FunctionDecl *FDecl, 8281 const char (&Str)[StrLen]) { 8282 if (!FDecl) 8283 return false; 8284 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str)) 8285 return false; 8286 if (!FDecl->isInStdNamespace()) 8287 return false; 8288 8289 return true; 8290 } 8291 8292 enum class MathCheck { NaN, Inf }; 8293 static bool IsInfOrNanFunction(StringRef calleeName, MathCheck Check) { 8294 auto MatchesAny = [&](std::initializer_list<llvm::StringRef> names) { 8295 return std::any_of(names.begin(), names.end(), [&](llvm::StringRef name) { 8296 return calleeName == name; 8297 }); 8298 }; 8299 8300 switch (Check) { 8301 case MathCheck::NaN: 8302 return MatchesAny({"__builtin_nan", "__builtin_nanf", "__builtin_nanl", 8303 "__builtin_nanf16", "__builtin_nanf128"}); 8304 case MathCheck::Inf: 8305 return MatchesAny({"__builtin_inf", "__builtin_inff", "__builtin_infl", 8306 "__builtin_inff16", "__builtin_inff128"}); 8307 } 8308 llvm_unreachable("unknown MathCheck"); 8309 } 8310 8311 void Sema::CheckInfNaNFunction(const CallExpr *Call, 8312 const FunctionDecl *FDecl) { 8313 FPOptions FPO = Call->getFPFeaturesInEffect(getLangOpts()); 8314 bool HasIdentifier = FDecl->getIdentifier() != nullptr; 8315 bool IsNaNOrIsUnordered = 8316 IsStdFunction(FDecl, "isnan") || IsStdFunction(FDecl, "isunordered"); 8317 bool IsSpecialNaN = 8318 HasIdentifier && IsInfOrNanFunction(FDecl->getName(), MathCheck::NaN); 8319 if ((IsNaNOrIsUnordered || IsSpecialNaN) && FPO.getNoHonorNaNs()) { 8320 Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 8321 << 1 << 0 << Call->getSourceRange(); 8322 } else { 8323 bool IsInfOrIsFinite = 8324 IsStdFunction(FDecl, "isinf") || IsStdFunction(FDecl, "isfinite"); 8325 bool IsInfinityOrIsSpecialInf = 8326 HasIdentifier && ((FDecl->getName() == "infinity") || 8327 IsInfOrNanFunction(FDecl->getName(), MathCheck::Inf)); 8328 if ((IsInfOrIsFinite || IsInfinityOrIsSpecialInf) && FPO.getNoHonorInfs()) 8329 Diag(Call->getBeginLoc(), diag::warn_fp_nan_inf_when_disabled) 8330 << 0 << 0 << Call->getSourceRange(); 8331 } 8332 } 8333 8334 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, 8335 const FunctionDecl *FDecl) { 8336 if (Call->getNumArgs() != 1) 8337 return; 8338 8339 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); 8340 bool IsStdAbs = IsStdFunction(FDecl, "abs"); 8341 if (AbsKind == 0 && !IsStdAbs) 8342 return; 8343 8344 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8345 QualType ParamType = Call->getArg(0)->getType(); 8346 8347 // Unsigned types cannot be negative. Suggest removing the absolute value 8348 // function call. 8349 if (ArgType->isUnsignedIntegerType()) { 8350 StringRef FunctionName = 8351 IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind); 8352 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; 8353 Diag(Call->getExprLoc(), diag::note_remove_abs) 8354 << FunctionName 8355 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); 8356 return; 8357 } 8358 8359 // Taking the absolute value of a pointer is very suspicious, they probably 8360 // wanted to index into an array, dereference a pointer, call a function, etc. 8361 if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) { 8362 unsigned DiagType = 0; 8363 if (ArgType->isFunctionType()) 8364 DiagType = 1; 8365 else if (ArgType->isArrayType()) 8366 DiagType = 2; 8367 8368 Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType; 8369 return; 8370 } 8371 8372 // std::abs has overloads which prevent most of the absolute value problems 8373 // from occurring. 8374 if (IsStdAbs) 8375 return; 8376 8377 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); 8378 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); 8379 8380 // The argument and parameter are the same kind. Check if they are the right 8381 // size. 8382 if (ArgValueKind == ParamValueKind) { 8383 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) 8384 return; 8385 8386 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); 8387 Diag(Call->getExprLoc(), diag::warn_abs_too_small) 8388 << FDecl << ArgType << ParamType; 8389 8390 if (NewAbsKind == 0) 8391 return; 8392 8393 emitReplacement(*this, Call->getExprLoc(), 8394 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8395 return; 8396 } 8397 8398 // ArgValueKind != ParamValueKind 8399 // The wrong type of absolute value function was used. Attempt to find the 8400 // proper one. 8401 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); 8402 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); 8403 if (NewAbsKind == 0) 8404 return; 8405 8406 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) 8407 << FDecl << ParamValueKind << ArgValueKind; 8408 8409 emitReplacement(*this, Call->getExprLoc(), 8410 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 8411 } 8412 8413 //===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===// 8414 void Sema::CheckMaxUnsignedZero(const CallExpr *Call, 8415 const FunctionDecl *FDecl) { 8416 if (!Call || !FDecl) return; 8417 8418 // Ignore template specializations and macros. 8419 if (inTemplateInstantiation()) return; 8420 if (Call->getExprLoc().isMacroID()) return; 8421 8422 // Only care about the one template argument, two function parameter std::max 8423 if (Call->getNumArgs() != 2) return; 8424 if (!IsStdFunction(FDecl, "max")) return; 8425 const auto * ArgList = FDecl->getTemplateSpecializationArgs(); 8426 if (!ArgList) return; 8427 if (ArgList->size() != 1) return; 8428 8429 // Check that template type argument is unsigned integer. 8430 const auto& TA = ArgList->get(0); 8431 if (TA.getKind() != TemplateArgument::Type) return; 8432 QualType ArgType = TA.getAsType(); 8433 if (!ArgType->isUnsignedIntegerType()) return; 8434 8435 // See if either argument is a literal zero. 8436 auto IsLiteralZeroArg = [](const Expr* E) -> bool { 8437 const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E); 8438 if (!MTE) return false; 8439 const auto *Num = dyn_cast<IntegerLiteral>(MTE->getSubExpr()); 8440 if (!Num) return false; 8441 if (Num->getValue() != 0) return false; 8442 return true; 8443 }; 8444 8445 const Expr *FirstArg = Call->getArg(0); 8446 const Expr *SecondArg = Call->getArg(1); 8447 const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg); 8448 const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg); 8449 8450 // Only warn when exactly one argument is zero. 8451 if (IsFirstArgZero == IsSecondArgZero) return; 8452 8453 SourceRange FirstRange = FirstArg->getSourceRange(); 8454 SourceRange SecondRange = SecondArg->getSourceRange(); 8455 8456 SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange; 8457 8458 Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero) 8459 << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange; 8460 8461 // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)". 8462 SourceRange RemovalRange; 8463 if (IsFirstArgZero) { 8464 RemovalRange = SourceRange(FirstRange.getBegin(), 8465 SecondRange.getBegin().getLocWithOffset(-1)); 8466 } else { 8467 RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()), 8468 SecondRange.getEnd()); 8469 } 8470 8471 Diag(Call->getExprLoc(), diag::note_remove_max_call) 8472 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()) 8473 << FixItHint::CreateRemoval(RemovalRange); 8474 } 8475 8476 //===--- CHECK: Standard memory functions ---------------------------------===// 8477 8478 /// Takes the expression passed to the size_t parameter of functions 8479 /// such as memcmp, strncat, etc and warns if it's a comparison. 8480 /// 8481 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. 8482 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, 8483 IdentifierInfo *FnName, 8484 SourceLocation FnLoc, 8485 SourceLocation RParenLoc) { 8486 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); 8487 if (!Size) 8488 return false; 8489 8490 // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||: 8491 if (!Size->isComparisonOp() && !Size->isLogicalOp()) 8492 return false; 8493 8494 SourceRange SizeRange = Size->getSourceRange(); 8495 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) 8496 << SizeRange << FnName; 8497 S.Diag(FnLoc, diag::note_memsize_comparison_paren) 8498 << FnName 8499 << FixItHint::CreateInsertion( 8500 S.getLocForEndOfToken(Size->getLHS()->getEndLoc()), ")") 8501 << FixItHint::CreateRemoval(RParenLoc); 8502 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) 8503 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") 8504 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), 8505 ")"); 8506 8507 return true; 8508 } 8509 8510 /// Determine whether the given type is or contains a dynamic class type 8511 /// (e.g., whether it has a vtable). 8512 static const CXXRecordDecl *getContainedDynamicClass(QualType T, 8513 bool &IsContained) { 8514 // Look through array types while ignoring qualifiers. 8515 const Type *Ty = T->getBaseElementTypeUnsafe(); 8516 IsContained = false; 8517 8518 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 8519 RD = RD ? RD->getDefinition() : nullptr; 8520 if (!RD || RD->isInvalidDecl()) 8521 return nullptr; 8522 8523 if (RD->isDynamicClass()) 8524 return RD; 8525 8526 // Check all the fields. If any bases were dynamic, the class is dynamic. 8527 // It's impossible for a class to transitively contain itself by value, so 8528 // infinite recursion is impossible. 8529 for (auto *FD : RD->fields()) { 8530 bool SubContained; 8531 if (const CXXRecordDecl *ContainedRD = 8532 getContainedDynamicClass(FD->getType(), SubContained)) { 8533 IsContained = true; 8534 return ContainedRD; 8535 } 8536 } 8537 8538 return nullptr; 8539 } 8540 8541 static const UnaryExprOrTypeTraitExpr *getAsSizeOfExpr(const Expr *E) { 8542 if (const auto *Unary = dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 8543 if (Unary->getKind() == UETT_SizeOf) 8544 return Unary; 8545 return nullptr; 8546 } 8547 8548 /// If E is a sizeof expression, returns its argument expression, 8549 /// otherwise returns NULL. 8550 static const Expr *getSizeOfExprArg(const Expr *E) { 8551 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8552 if (!SizeOf->isArgumentType()) 8553 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); 8554 return nullptr; 8555 } 8556 8557 /// If E is a sizeof expression, returns its argument type. 8558 static QualType getSizeOfArgType(const Expr *E) { 8559 if (const UnaryExprOrTypeTraitExpr *SizeOf = getAsSizeOfExpr(E)) 8560 return SizeOf->getTypeOfArgument(); 8561 return QualType(); 8562 } 8563 8564 namespace { 8565 8566 struct SearchNonTrivialToInitializeField 8567 : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> { 8568 using Super = 8569 DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>; 8570 8571 SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {} 8572 8573 void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT, 8574 SourceLocation SL) { 8575 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8576 asDerived().visitArray(PDIK, AT, SL); 8577 return; 8578 } 8579 8580 Super::visitWithKind(PDIK, FT, SL); 8581 } 8582 8583 void visitARCStrong(QualType FT, SourceLocation SL) { 8584 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8585 } 8586 void visitARCWeak(QualType FT, SourceLocation SL) { 8587 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1); 8588 } 8589 void visitStruct(QualType FT, SourceLocation SL) { 8590 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8591 visit(FD->getType(), FD->getLocation()); 8592 } 8593 void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK, 8594 const ArrayType *AT, SourceLocation SL) { 8595 visit(getContext().getBaseElementType(AT), SL); 8596 } 8597 void visitTrivial(QualType FT, SourceLocation SL) {} 8598 8599 static void diag(QualType RT, const Expr *E, Sema &S) { 8600 SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation()); 8601 } 8602 8603 ASTContext &getContext() { return S.getASTContext(); } 8604 8605 const Expr *E; 8606 Sema &S; 8607 }; 8608 8609 struct SearchNonTrivialToCopyField 8610 : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> { 8611 using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>; 8612 8613 SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {} 8614 8615 void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT, 8616 SourceLocation SL) { 8617 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) { 8618 asDerived().visitArray(PCK, AT, SL); 8619 return; 8620 } 8621 8622 Super::visitWithKind(PCK, FT, SL); 8623 } 8624 8625 void visitARCStrong(QualType FT, SourceLocation SL) { 8626 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8627 } 8628 void visitARCWeak(QualType FT, SourceLocation SL) { 8629 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0); 8630 } 8631 void visitStruct(QualType FT, SourceLocation SL) { 8632 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields()) 8633 visit(FD->getType(), FD->getLocation()); 8634 } 8635 void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT, 8636 SourceLocation SL) { 8637 visit(getContext().getBaseElementType(AT), SL); 8638 } 8639 void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT, 8640 SourceLocation SL) {} 8641 void visitTrivial(QualType FT, SourceLocation SL) {} 8642 void visitVolatileTrivial(QualType FT, SourceLocation SL) {} 8643 8644 static void diag(QualType RT, const Expr *E, Sema &S) { 8645 SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation()); 8646 } 8647 8648 ASTContext &getContext() { return S.getASTContext(); } 8649 8650 const Expr *E; 8651 Sema &S; 8652 }; 8653 8654 } 8655 8656 /// Detect if \c SizeofExpr is likely to calculate the sizeof an object. 8657 static bool doesExprLikelyComputeSize(const Expr *SizeofExpr) { 8658 SizeofExpr = SizeofExpr->IgnoreParenImpCasts(); 8659 8660 if (const auto *BO = dyn_cast<BinaryOperator>(SizeofExpr)) { 8661 if (BO->getOpcode() != BO_Mul && BO->getOpcode() != BO_Add) 8662 return false; 8663 8664 return doesExprLikelyComputeSize(BO->getLHS()) || 8665 doesExprLikelyComputeSize(BO->getRHS()); 8666 } 8667 8668 return getAsSizeOfExpr(SizeofExpr) != nullptr; 8669 } 8670 8671 /// Check if the ArgLoc originated from a macro passed to the call at CallLoc. 8672 /// 8673 /// \code 8674 /// #define MACRO 0 8675 /// foo(MACRO); 8676 /// foo(0); 8677 /// \endcode 8678 /// 8679 /// This should return true for the first call to foo, but not for the second 8680 /// (regardless of whether foo is a macro or function). 8681 static bool isArgumentExpandedFromMacro(SourceManager &SM, 8682 SourceLocation CallLoc, 8683 SourceLocation ArgLoc) { 8684 if (!CallLoc.isMacroID()) 8685 return SM.getFileID(CallLoc) != SM.getFileID(ArgLoc); 8686 8687 return SM.getFileID(SM.getImmediateMacroCallerLoc(CallLoc)) != 8688 SM.getFileID(SM.getImmediateMacroCallerLoc(ArgLoc)); 8689 } 8690 8691 /// Diagnose cases like 'memset(buf, sizeof(buf), 0)', which should have the 8692 /// last two arguments transposed. 8693 static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) { 8694 if (BId != Builtin::BImemset && BId != Builtin::BIbzero) 8695 return; 8696 8697 const Expr *SizeArg = 8698 Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts(); 8699 8700 auto isLiteralZero = [](const Expr *E) { 8701 return (isa<IntegerLiteral>(E) && 8702 cast<IntegerLiteral>(E)->getValue() == 0) || 8703 (isa<CharacterLiteral>(E) && 8704 cast<CharacterLiteral>(E)->getValue() == 0); 8705 }; 8706 8707 // If we're memsetting or bzeroing 0 bytes, then this is likely an error. 8708 SourceLocation CallLoc = Call->getRParenLoc(); 8709 SourceManager &SM = S.getSourceManager(); 8710 if (isLiteralZero(SizeArg) && 8711 !isArgumentExpandedFromMacro(SM, CallLoc, SizeArg->getExprLoc())) { 8712 8713 SourceLocation DiagLoc = SizeArg->getExprLoc(); 8714 8715 // Some platforms #define bzero to __builtin_memset. See if this is the 8716 // case, and if so, emit a better diagnostic. 8717 if (BId == Builtin::BIbzero || 8718 (CallLoc.isMacroID() && Lexer::getImmediateMacroName( 8719 CallLoc, SM, S.getLangOpts()) == "bzero")) { 8720 S.Diag(DiagLoc, diag::warn_suspicious_bzero_size); 8721 S.Diag(DiagLoc, diag::note_suspicious_bzero_size_silence); 8722 } else if (!isLiteralZero(Call->getArg(1)->IgnoreImpCasts())) { 8723 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 0; 8724 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 0; 8725 } 8726 return; 8727 } 8728 8729 // If the second argument to a memset is a sizeof expression and the third 8730 // isn't, this is also likely an error. This should catch 8731 // 'memset(buf, sizeof(buf), 0xff)'. 8732 if (BId == Builtin::BImemset && 8733 doesExprLikelyComputeSize(Call->getArg(1)) && 8734 !doesExprLikelyComputeSize(Call->getArg(2))) { 8735 SourceLocation DiagLoc = Call->getArg(1)->getExprLoc(); 8736 S.Diag(DiagLoc, diag::warn_suspicious_sizeof_memset) << 1; 8737 S.Diag(DiagLoc, diag::note_suspicious_sizeof_memset_silence) << 1; 8738 return; 8739 } 8740 } 8741 8742 void Sema::CheckMemaccessArguments(const CallExpr *Call, 8743 unsigned BId, 8744 IdentifierInfo *FnName) { 8745 assert(BId != 0); 8746 8747 // It is possible to have a non-standard definition of memset. Validate 8748 // we have enough arguments, and if not, abort further checking. 8749 unsigned ExpectedNumArgs = 8750 (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3); 8751 if (Call->getNumArgs() < ExpectedNumArgs) 8752 return; 8753 8754 unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero || 8755 BId == Builtin::BIstrndup ? 1 : 2); 8756 unsigned LenArg = 8757 (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2); 8758 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); 8759 8760 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, 8761 Call->getBeginLoc(), Call->getRParenLoc())) 8762 return; 8763 8764 // Catch cases like 'memset(buf, sizeof(buf), 0)'. 8765 CheckMemaccessSize(*this, BId, Call); 8766 8767 // We have special checking when the length is a sizeof expression. 8768 QualType SizeOfArgTy = getSizeOfArgType(LenExpr); 8769 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); 8770 llvm::FoldingSetNodeID SizeOfArgID; 8771 8772 // Although widely used, 'bzero' is not a standard function. Be more strict 8773 // with the argument types before allowing diagnostics and only allow the 8774 // form bzero(ptr, sizeof(...)). 8775 QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 8776 if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>()) 8777 return; 8778 8779 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { 8780 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); 8781 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); 8782 8783 QualType DestTy = Dest->getType(); 8784 QualType PointeeTy; 8785 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { 8786 PointeeTy = DestPtrTy->getPointeeType(); 8787 8788 // Never warn about void type pointers. This can be used to suppress 8789 // false positives. 8790 if (PointeeTy->isVoidType()) 8791 continue; 8792 8793 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by 8794 // actually comparing the expressions for equality. Because computing the 8795 // expression IDs can be expensive, we only do this if the diagnostic is 8796 // enabled. 8797 if (SizeOfArg && 8798 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, 8799 SizeOfArg->getExprLoc())) { 8800 // We only compute IDs for expressions if the warning is enabled, and 8801 // cache the sizeof arg's ID. 8802 if (SizeOfArgID == llvm::FoldingSetNodeID()) 8803 SizeOfArg->Profile(SizeOfArgID, Context, true); 8804 llvm::FoldingSetNodeID DestID; 8805 Dest->Profile(DestID, Context, true); 8806 if (DestID == SizeOfArgID) { 8807 // TODO: For strncpy() and friends, this could suggest sizeof(dst) 8808 // over sizeof(src) as well. 8809 unsigned ActionIdx = 0; // Default is to suggest dereferencing. 8810 StringRef ReadableName = FnName->getName(); 8811 8812 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) 8813 if (UnaryOp->getOpcode() == UO_AddrOf) 8814 ActionIdx = 1; // If its an address-of operator, just remove it. 8815 if (!PointeeTy->isIncompleteType() && 8816 (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) 8817 ActionIdx = 2; // If the pointee's size is sizeof(char), 8818 // suggest an explicit length. 8819 8820 // If the function is defined as a builtin macro, do not show macro 8821 // expansion. 8822 SourceLocation SL = SizeOfArg->getExprLoc(); 8823 SourceRange DSR = Dest->getSourceRange(); 8824 SourceRange SSR = SizeOfArg->getSourceRange(); 8825 SourceManager &SM = getSourceManager(); 8826 8827 if (SM.isMacroArgExpansion(SL)) { 8828 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); 8829 SL = SM.getSpellingLoc(SL); 8830 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), 8831 SM.getSpellingLoc(DSR.getEnd())); 8832 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), 8833 SM.getSpellingLoc(SSR.getEnd())); 8834 } 8835 8836 DiagRuntimeBehavior(SL, SizeOfArg, 8837 PDiag(diag::warn_sizeof_pointer_expr_memaccess) 8838 << ReadableName 8839 << PointeeTy 8840 << DestTy 8841 << DSR 8842 << SSR); 8843 DiagRuntimeBehavior(SL, SizeOfArg, 8844 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) 8845 << ActionIdx 8846 << SSR); 8847 8848 break; 8849 } 8850 } 8851 8852 // Also check for cases where the sizeof argument is the exact same 8853 // type as the memory argument, and where it points to a user-defined 8854 // record type. 8855 if (SizeOfArgTy != QualType()) { 8856 if (PointeeTy->isRecordType() && 8857 Context.typesAreCompatible(SizeOfArgTy, DestTy)) { 8858 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, 8859 PDiag(diag::warn_sizeof_pointer_type_memaccess) 8860 << FnName << SizeOfArgTy << ArgIdx 8861 << PointeeTy << Dest->getSourceRange() 8862 << LenExpr->getSourceRange()); 8863 break; 8864 } 8865 } 8866 } else if (DestTy->isArrayType()) { 8867 PointeeTy = DestTy; 8868 } 8869 8870 if (PointeeTy == QualType()) 8871 continue; 8872 8873 // Always complain about dynamic classes. 8874 bool IsContained; 8875 if (const CXXRecordDecl *ContainedRD = 8876 getContainedDynamicClass(PointeeTy, IsContained)) { 8877 8878 unsigned OperationType = 0; 8879 const bool IsCmp = BId == Builtin::BImemcmp || BId == Builtin::BIbcmp; 8880 // "overwritten" if we're warning about the destination for any call 8881 // but memcmp; otherwise a verb appropriate to the call. 8882 if (ArgIdx != 0 || IsCmp) { 8883 if (BId == Builtin::BImemcpy) 8884 OperationType = 1; 8885 else if(BId == Builtin::BImemmove) 8886 OperationType = 2; 8887 else if (IsCmp) 8888 OperationType = 3; 8889 } 8890 8891 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 8892 PDiag(diag::warn_dyn_class_memaccess) 8893 << (IsCmp ? ArgIdx + 2 : ArgIdx) << FnName 8894 << IsContained << ContainedRD << OperationType 8895 << Call->getCallee()->getSourceRange()); 8896 } else if (PointeeTy.hasNonTrivialObjCLifetime() && 8897 BId != Builtin::BImemset) 8898 DiagRuntimeBehavior( 8899 Dest->getExprLoc(), Dest, 8900 PDiag(diag::warn_arc_object_memaccess) 8901 << ArgIdx << FnName << PointeeTy 8902 << Call->getCallee()->getSourceRange()); 8903 else if (const auto *RT = PointeeTy->getAs<RecordType>()) { 8904 8905 // FIXME: Do not consider incomplete types even though they may be 8906 // completed later. GCC does not diagnose such code, but we may want to 8907 // consider diagnosing it in the future, perhaps under a different, but 8908 // related, diagnostic group. 8909 bool MayBeTriviallyCopyableCXXRecord = 8910 RT->isIncompleteType() || 8911 RT->desugar().isTriviallyCopyableType(Context); 8912 8913 if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 8914 RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) { 8915 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 8916 PDiag(diag::warn_cstruct_memaccess) 8917 << ArgIdx << FnName << PointeeTy << 0); 8918 SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this); 8919 } else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) && 8920 !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) { 8921 // FIXME: Limiting this warning to dest argument until we decide 8922 // whether it's valid for source argument too. 8923 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 8924 PDiag(diag::warn_cxxstruct_memaccess) 8925 << FnName << PointeeTy); 8926 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 8927 RT->getDecl()->isNonTrivialToPrimitiveCopy()) { 8928 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 8929 PDiag(diag::warn_cstruct_memaccess) 8930 << ArgIdx << FnName << PointeeTy << 1); 8931 SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this); 8932 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) && 8933 !MayBeTriviallyCopyableCXXRecord && ArgIdx == 0) { 8934 // FIXME: Limiting this warning to dest argument until we decide 8935 // whether it's valid for source argument too. 8936 DiagRuntimeBehavior(Dest->getExprLoc(), Dest, 8937 PDiag(diag::warn_cxxstruct_memaccess) 8938 << FnName << PointeeTy); 8939 } else { 8940 continue; 8941 } 8942 } else 8943 continue; 8944 8945 DiagRuntimeBehavior( 8946 Dest->getExprLoc(), Dest, 8947 PDiag(diag::note_bad_memaccess_silence) 8948 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); 8949 break; 8950 } 8951 } 8952 8953 // A little helper routine: ignore addition and subtraction of integer literals. 8954 // This intentionally does not ignore all integer constant expressions because 8955 // we don't want to remove sizeof(). 8956 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { 8957 Ex = Ex->IgnoreParenCasts(); 8958 8959 while (true) { 8960 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); 8961 if (!BO || !BO->isAdditiveOp()) 8962 break; 8963 8964 const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); 8965 const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); 8966 8967 if (isa<IntegerLiteral>(RHS)) 8968 Ex = LHS; 8969 else if (isa<IntegerLiteral>(LHS)) 8970 Ex = RHS; 8971 else 8972 break; 8973 } 8974 8975 return Ex; 8976 } 8977 8978 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, 8979 ASTContext &Context) { 8980 // Only handle constant-sized or VLAs, but not flexible members. 8981 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { 8982 // Only issue the FIXIT for arrays of size > 1. 8983 if (CAT->getZExtSize() <= 1) 8984 return false; 8985 } else if (!Ty->isVariableArrayType()) { 8986 return false; 8987 } 8988 return true; 8989 } 8990 8991 void Sema::CheckStrlcpycatArguments(const CallExpr *Call, 8992 IdentifierInfo *FnName) { 8993 8994 // Don't crash if the user has the wrong number of arguments 8995 unsigned NumArgs = Call->getNumArgs(); 8996 if ((NumArgs != 3) && (NumArgs != 4)) 8997 return; 8998 8999 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); 9000 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); 9001 const Expr *CompareWithSrc = nullptr; 9002 9003 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, 9004 Call->getBeginLoc(), Call->getRParenLoc())) 9005 return; 9006 9007 // Look for 'strlcpy(dst, x, sizeof(x))' 9008 if (const Expr *Ex = getSizeOfExprArg(SizeArg)) 9009 CompareWithSrc = Ex; 9010 else { 9011 // Look for 'strlcpy(dst, x, strlen(x))' 9012 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { 9013 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && 9014 SizeCall->getNumArgs() == 1) 9015 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); 9016 } 9017 } 9018 9019 if (!CompareWithSrc) 9020 return; 9021 9022 // Determine if the argument to sizeof/strlen is equal to the source 9023 // argument. In principle there's all kinds of things you could do 9024 // here, for instance creating an == expression and evaluating it with 9025 // EvaluateAsBooleanCondition, but this uses a more direct technique: 9026 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); 9027 if (!SrcArgDRE) 9028 return; 9029 9030 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); 9031 if (!CompareWithSrcDRE || 9032 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) 9033 return; 9034 9035 const Expr *OriginalSizeArg = Call->getArg(2); 9036 Diag(CompareWithSrcDRE->getBeginLoc(), diag::warn_strlcpycat_wrong_size) 9037 << OriginalSizeArg->getSourceRange() << FnName; 9038 9039 // Output a FIXIT hint if the destination is an array (rather than a 9040 // pointer to an array). This could be enhanced to handle some 9041 // pointers if we know the actual size, like if DstArg is 'array+2' 9042 // we could say 'sizeof(array)-2'. 9043 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); 9044 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) 9045 return; 9046 9047 SmallString<128> sizeString; 9048 llvm::raw_svector_ostream OS(sizeString); 9049 OS << "sizeof("; 9050 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9051 OS << ")"; 9052 9053 Diag(OriginalSizeArg->getBeginLoc(), diag::note_strlcpycat_wrong_size) 9054 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), 9055 OS.str()); 9056 } 9057 9058 /// Check if two expressions refer to the same declaration. 9059 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { 9060 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) 9061 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) 9062 return D1->getDecl() == D2->getDecl(); 9063 return false; 9064 } 9065 9066 static const Expr *getStrlenExprArg(const Expr *E) { 9067 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 9068 const FunctionDecl *FD = CE->getDirectCallee(); 9069 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) 9070 return nullptr; 9071 return CE->getArg(0)->IgnoreParenCasts(); 9072 } 9073 return nullptr; 9074 } 9075 9076 void Sema::CheckStrncatArguments(const CallExpr *CE, 9077 IdentifierInfo *FnName) { 9078 // Don't crash if the user has the wrong number of arguments. 9079 if (CE->getNumArgs() < 3) 9080 return; 9081 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); 9082 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); 9083 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); 9084 9085 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getBeginLoc(), 9086 CE->getRParenLoc())) 9087 return; 9088 9089 // Identify common expressions, which are wrongly used as the size argument 9090 // to strncat and may lead to buffer overflows. 9091 unsigned PatternType = 0; 9092 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { 9093 // - sizeof(dst) 9094 if (referToTheSameDecl(SizeOfArg, DstArg)) 9095 PatternType = 1; 9096 // - sizeof(src) 9097 else if (referToTheSameDecl(SizeOfArg, SrcArg)) 9098 PatternType = 2; 9099 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { 9100 if (BE->getOpcode() == BO_Sub) { 9101 const Expr *L = BE->getLHS()->IgnoreParenCasts(); 9102 const Expr *R = BE->getRHS()->IgnoreParenCasts(); 9103 // - sizeof(dst) - strlen(dst) 9104 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && 9105 referToTheSameDecl(DstArg, getStrlenExprArg(R))) 9106 PatternType = 1; 9107 // - sizeof(src) - (anything) 9108 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) 9109 PatternType = 2; 9110 } 9111 } 9112 9113 if (PatternType == 0) 9114 return; 9115 9116 // Generate the diagnostic. 9117 SourceLocation SL = LenArg->getBeginLoc(); 9118 SourceRange SR = LenArg->getSourceRange(); 9119 SourceManager &SM = getSourceManager(); 9120 9121 // If the function is defined as a builtin macro, do not show macro expansion. 9122 if (SM.isMacroArgExpansion(SL)) { 9123 SL = SM.getSpellingLoc(SL); 9124 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), 9125 SM.getSpellingLoc(SR.getEnd())); 9126 } 9127 9128 // Check if the destination is an array (rather than a pointer to an array). 9129 QualType DstTy = DstArg->getType(); 9130 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, 9131 Context); 9132 if (!isKnownSizeArray) { 9133 if (PatternType == 1) 9134 Diag(SL, diag::warn_strncat_wrong_size) << SR; 9135 else 9136 Diag(SL, diag::warn_strncat_src_size) << SR; 9137 return; 9138 } 9139 9140 if (PatternType == 1) 9141 Diag(SL, diag::warn_strncat_large_size) << SR; 9142 else 9143 Diag(SL, diag::warn_strncat_src_size) << SR; 9144 9145 SmallString<128> sizeString; 9146 llvm::raw_svector_ostream OS(sizeString); 9147 OS << "sizeof("; 9148 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9149 OS << ") - "; 9150 OS << "strlen("; 9151 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 9152 OS << ") - 1"; 9153 9154 Diag(SL, diag::note_strncat_wrong_size) 9155 << FixItHint::CreateReplacement(SR, OS.str()); 9156 } 9157 9158 namespace { 9159 void CheckFreeArgumentsOnLvalue(Sema &S, const std::string &CalleeName, 9160 const UnaryOperator *UnaryExpr, const Decl *D) { 9161 if (isa<FieldDecl, FunctionDecl, VarDecl>(D)) { 9162 S.Diag(UnaryExpr->getBeginLoc(), diag::warn_free_nonheap_object) 9163 << CalleeName << 0 /*object: */ << cast<NamedDecl>(D); 9164 return; 9165 } 9166 } 9167 9168 void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName, 9169 const UnaryOperator *UnaryExpr) { 9170 if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) { 9171 const Decl *D = Lvalue->getDecl(); 9172 if (isa<DeclaratorDecl>(D)) 9173 if (!dyn_cast<DeclaratorDecl>(D)->getType()->isReferenceType()) 9174 return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D); 9175 } 9176 9177 if (const auto *Lvalue = dyn_cast<MemberExpr>(UnaryExpr->getSubExpr())) 9178 return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, 9179 Lvalue->getMemberDecl()); 9180 } 9181 9182 void CheckFreeArgumentsPlus(Sema &S, const std::string &CalleeName, 9183 const UnaryOperator *UnaryExpr) { 9184 const auto *Lambda = dyn_cast<LambdaExpr>( 9185 UnaryExpr->getSubExpr()->IgnoreImplicitAsWritten()->IgnoreParens()); 9186 if (!Lambda) 9187 return; 9188 9189 S.Diag(Lambda->getBeginLoc(), diag::warn_free_nonheap_object) 9190 << CalleeName << 2 /*object: lambda expression*/; 9191 } 9192 9193 void CheckFreeArgumentsStackArray(Sema &S, const std::string &CalleeName, 9194 const DeclRefExpr *Lvalue) { 9195 const auto *Var = dyn_cast<VarDecl>(Lvalue->getDecl()); 9196 if (Var == nullptr) 9197 return; 9198 9199 S.Diag(Lvalue->getBeginLoc(), diag::warn_free_nonheap_object) 9200 << CalleeName << 0 /*object: */ << Var; 9201 } 9202 9203 void CheckFreeArgumentsCast(Sema &S, const std::string &CalleeName, 9204 const CastExpr *Cast) { 9205 SmallString<128> SizeString; 9206 llvm::raw_svector_ostream OS(SizeString); 9207 9208 clang::CastKind Kind = Cast->getCastKind(); 9209 if (Kind == clang::CK_BitCast && 9210 !Cast->getSubExpr()->getType()->isFunctionPointerType()) 9211 return; 9212 if (Kind == clang::CK_IntegralToPointer && 9213 !isa<IntegerLiteral>( 9214 Cast->getSubExpr()->IgnoreParenImpCasts()->IgnoreParens())) 9215 return; 9216 9217 switch (Cast->getCastKind()) { 9218 case clang::CK_BitCast: 9219 case clang::CK_IntegralToPointer: 9220 case clang::CK_FunctionToPointerDecay: 9221 OS << '\''; 9222 Cast->printPretty(OS, nullptr, S.getPrintingPolicy()); 9223 OS << '\''; 9224 break; 9225 default: 9226 return; 9227 } 9228 9229 S.Diag(Cast->getBeginLoc(), diag::warn_free_nonheap_object) 9230 << CalleeName << 0 /*object: */ << OS.str(); 9231 } 9232 } // namespace 9233 9234 void Sema::CheckFreeArguments(const CallExpr *E) { 9235 const std::string CalleeName = 9236 cast<FunctionDecl>(E->getCalleeDecl())->getQualifiedNameAsString(); 9237 9238 { // Prefer something that doesn't involve a cast to make things simpler. 9239 const Expr *Arg = E->getArg(0)->IgnoreParenCasts(); 9240 if (const auto *UnaryExpr = dyn_cast<UnaryOperator>(Arg)) 9241 switch (UnaryExpr->getOpcode()) { 9242 case UnaryOperator::Opcode::UO_AddrOf: 9243 return CheckFreeArgumentsAddressof(*this, CalleeName, UnaryExpr); 9244 case UnaryOperator::Opcode::UO_Plus: 9245 return CheckFreeArgumentsPlus(*this, CalleeName, UnaryExpr); 9246 default: 9247 break; 9248 } 9249 9250 if (const auto *Lvalue = dyn_cast<DeclRefExpr>(Arg)) 9251 if (Lvalue->getType()->isArrayType()) 9252 return CheckFreeArgumentsStackArray(*this, CalleeName, Lvalue); 9253 9254 if (const auto *Label = dyn_cast<AddrLabelExpr>(Arg)) { 9255 Diag(Label->getBeginLoc(), diag::warn_free_nonheap_object) 9256 << CalleeName << 0 /*object: */ << Label->getLabel()->getIdentifier(); 9257 return; 9258 } 9259 9260 if (isa<BlockExpr>(Arg)) { 9261 Diag(Arg->getBeginLoc(), diag::warn_free_nonheap_object) 9262 << CalleeName << 1 /*object: block*/; 9263 return; 9264 } 9265 } 9266 // Maybe the cast was important, check after the other cases. 9267 if (const auto *Cast = dyn_cast<CastExpr>(E->getArg(0))) 9268 return CheckFreeArgumentsCast(*this, CalleeName, Cast); 9269 } 9270 9271 void 9272 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, 9273 SourceLocation ReturnLoc, 9274 bool isObjCMethod, 9275 const AttrVec *Attrs, 9276 const FunctionDecl *FD) { 9277 // Check if the return value is null but should not be. 9278 if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) || 9279 (!isObjCMethod && isNonNullType(lhsType))) && 9280 CheckNonNullExpr(*this, RetValExp)) 9281 Diag(ReturnLoc, diag::warn_null_ret) 9282 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); 9283 9284 // C++11 [basic.stc.dynamic.allocation]p4: 9285 // If an allocation function declared with a non-throwing 9286 // exception-specification fails to allocate storage, it shall return 9287 // a null pointer. Any other allocation function that fails to allocate 9288 // storage shall indicate failure only by throwing an exception [...] 9289 if (FD) { 9290 OverloadedOperatorKind Op = FD->getOverloadedOperator(); 9291 if (Op == OO_New || Op == OO_Array_New) { 9292 const FunctionProtoType *Proto 9293 = FD->getType()->castAs<FunctionProtoType>(); 9294 if (!Proto->isNothrow(/*ResultIfDependent*/true) && 9295 CheckNonNullExpr(*this, RetValExp)) 9296 Diag(ReturnLoc, diag::warn_operator_new_returns_null) 9297 << FD << getLangOpts().CPlusPlus11; 9298 } 9299 } 9300 9301 if (RetValExp && RetValExp->getType()->isWebAssemblyTableType()) { 9302 Diag(ReturnLoc, diag::err_wasm_table_art) << 1; 9303 } 9304 9305 // PPC MMA non-pointer types are not allowed as return type. Checking the type 9306 // here prevent the user from using a PPC MMA type as trailing return type. 9307 if (Context.getTargetInfo().getTriple().isPPC64()) 9308 PPC().CheckPPCMMAType(RetValExp->getType(), ReturnLoc); 9309 } 9310 9311 void Sema::CheckFloatComparison(SourceLocation Loc, Expr *LHS, Expr *RHS, 9312 BinaryOperatorKind Opcode) { 9313 if (!BinaryOperator::isEqualityOp(Opcode)) 9314 return; 9315 9316 // Match and capture subexpressions such as "(float) X == 0.1". 9317 FloatingLiteral *FPLiteral; 9318 CastExpr *FPCast; 9319 auto getCastAndLiteral = [&FPLiteral, &FPCast](Expr *L, Expr *R) { 9320 FPLiteral = dyn_cast<FloatingLiteral>(L->IgnoreParens()); 9321 FPCast = dyn_cast<CastExpr>(R->IgnoreParens()); 9322 return FPLiteral && FPCast; 9323 }; 9324 9325 if (getCastAndLiteral(LHS, RHS) || getCastAndLiteral(RHS, LHS)) { 9326 auto *SourceTy = FPCast->getSubExpr()->getType()->getAs<BuiltinType>(); 9327 auto *TargetTy = FPLiteral->getType()->getAs<BuiltinType>(); 9328 if (SourceTy && TargetTy && SourceTy->isFloatingPoint() && 9329 TargetTy->isFloatingPoint()) { 9330 bool Lossy; 9331 llvm::APFloat TargetC = FPLiteral->getValue(); 9332 TargetC.convert(Context.getFloatTypeSemantics(QualType(SourceTy, 0)), 9333 llvm::APFloat::rmNearestTiesToEven, &Lossy); 9334 if (Lossy) { 9335 // If the literal cannot be represented in the source type, then a 9336 // check for == is always false and check for != is always true. 9337 Diag(Loc, diag::warn_float_compare_literal) 9338 << (Opcode == BO_EQ) << QualType(SourceTy, 0) 9339 << LHS->getSourceRange() << RHS->getSourceRange(); 9340 return; 9341 } 9342 } 9343 } 9344 9345 // Match a more general floating-point equality comparison (-Wfloat-equal). 9346 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); 9347 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); 9348 9349 // Special case: check for x == x (which is OK). 9350 // Do not emit warnings for such cases. 9351 if (auto *DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) 9352 if (auto *DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) 9353 if (DRL->getDecl() == DRR->getDecl()) 9354 return; 9355 9356 // Special case: check for comparisons against literals that can be exactly 9357 // represented by APFloat. In such cases, do not emit a warning. This 9358 // is a heuristic: often comparison against such literals are used to 9359 // detect if a value in a variable has not changed. This clearly can 9360 // lead to false negatives. 9361 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { 9362 if (FLL->isExact()) 9363 return; 9364 } else 9365 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) 9366 if (FLR->isExact()) 9367 return; 9368 9369 // Check for comparisons with builtin types. 9370 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) 9371 if (CL->getBuiltinCallee()) 9372 return; 9373 9374 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) 9375 if (CR->getBuiltinCallee()) 9376 return; 9377 9378 // Emit the diagnostic. 9379 Diag(Loc, diag::warn_floatingpoint_eq) 9380 << LHS->getSourceRange() << RHS->getSourceRange(); 9381 } 9382 9383 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// 9384 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// 9385 9386 namespace { 9387 9388 /// Structure recording the 'active' range of an integer-valued 9389 /// expression. 9390 struct IntRange { 9391 /// The number of bits active in the int. Note that this includes exactly one 9392 /// sign bit if !NonNegative. 9393 unsigned Width; 9394 9395 /// True if the int is known not to have negative values. If so, all leading 9396 /// bits before Width are known zero, otherwise they are known to be the 9397 /// same as the MSB within Width. 9398 bool NonNegative; 9399 9400 IntRange(unsigned Width, bool NonNegative) 9401 : Width(Width), NonNegative(NonNegative) {} 9402 9403 /// Number of bits excluding the sign bit. 9404 unsigned valueBits() const { 9405 return NonNegative ? Width : Width - 1; 9406 } 9407 9408 /// Returns the range of the bool type. 9409 static IntRange forBoolType() { 9410 return IntRange(1, true); 9411 } 9412 9413 /// Returns the range of an opaque value of the given integral type. 9414 static IntRange forValueOfType(ASTContext &C, QualType T) { 9415 return forValueOfCanonicalType(C, 9416 T->getCanonicalTypeInternal().getTypePtr()); 9417 } 9418 9419 /// Returns the range of an opaque value of a canonical integral type. 9420 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { 9421 assert(T->isCanonicalUnqualified()); 9422 9423 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9424 T = VT->getElementType().getTypePtr(); 9425 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9426 T = CT->getElementType().getTypePtr(); 9427 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9428 T = AT->getValueType().getTypePtr(); 9429 9430 if (!C.getLangOpts().CPlusPlus) { 9431 // For enum types in C code, use the underlying datatype. 9432 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9433 T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr(); 9434 } else if (const EnumType *ET = dyn_cast<EnumType>(T)) { 9435 // For enum types in C++, use the known bit width of the enumerators. 9436 EnumDecl *Enum = ET->getDecl(); 9437 // In C++11, enums can have a fixed underlying type. Use this type to 9438 // compute the range. 9439 if (Enum->isFixed()) { 9440 return IntRange(C.getIntWidth(QualType(T, 0)), 9441 !ET->isSignedIntegerOrEnumerationType()); 9442 } 9443 9444 unsigned NumPositive = Enum->getNumPositiveBits(); 9445 unsigned NumNegative = Enum->getNumNegativeBits(); 9446 9447 if (NumNegative == 0) 9448 return IntRange(NumPositive, true/*NonNegative*/); 9449 else 9450 return IntRange(std::max(NumPositive + 1, NumNegative), 9451 false/*NonNegative*/); 9452 } 9453 9454 if (const auto *EIT = dyn_cast<BitIntType>(T)) 9455 return IntRange(EIT->getNumBits(), EIT->isUnsigned()); 9456 9457 const BuiltinType *BT = cast<BuiltinType>(T); 9458 assert(BT->isInteger()); 9459 9460 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9461 } 9462 9463 /// Returns the "target" range of a canonical integral type, i.e. 9464 /// the range of values expressible in the type. 9465 /// 9466 /// This matches forValueOfCanonicalType except that enums have the 9467 /// full range of their type, not the range of their enumerators. 9468 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { 9469 assert(T->isCanonicalUnqualified()); 9470 9471 if (const VectorType *VT = dyn_cast<VectorType>(T)) 9472 T = VT->getElementType().getTypePtr(); 9473 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 9474 T = CT->getElementType().getTypePtr(); 9475 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 9476 T = AT->getValueType().getTypePtr(); 9477 if (const EnumType *ET = dyn_cast<EnumType>(T)) 9478 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); 9479 9480 if (const auto *EIT = dyn_cast<BitIntType>(T)) 9481 return IntRange(EIT->getNumBits(), EIT->isUnsigned()); 9482 9483 const BuiltinType *BT = cast<BuiltinType>(T); 9484 assert(BT->isInteger()); 9485 9486 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 9487 } 9488 9489 /// Returns the supremum of two ranges: i.e. their conservative merge. 9490 static IntRange join(IntRange L, IntRange R) { 9491 bool Unsigned = L.NonNegative && R.NonNegative; 9492 return IntRange(std::max(L.valueBits(), R.valueBits()) + !Unsigned, 9493 L.NonNegative && R.NonNegative); 9494 } 9495 9496 /// Return the range of a bitwise-AND of the two ranges. 9497 static IntRange bit_and(IntRange L, IntRange R) { 9498 unsigned Bits = std::max(L.Width, R.Width); 9499 bool NonNegative = false; 9500 if (L.NonNegative) { 9501 Bits = std::min(Bits, L.Width); 9502 NonNegative = true; 9503 } 9504 if (R.NonNegative) { 9505 Bits = std::min(Bits, R.Width); 9506 NonNegative = true; 9507 } 9508 return IntRange(Bits, NonNegative); 9509 } 9510 9511 /// Return the range of a sum of the two ranges. 9512 static IntRange sum(IntRange L, IntRange R) { 9513 bool Unsigned = L.NonNegative && R.NonNegative; 9514 return IntRange(std::max(L.valueBits(), R.valueBits()) + 1 + !Unsigned, 9515 Unsigned); 9516 } 9517 9518 /// Return the range of a difference of the two ranges. 9519 static IntRange difference(IntRange L, IntRange R) { 9520 // We need a 1-bit-wider range if: 9521 // 1) LHS can be negative: least value can be reduced. 9522 // 2) RHS can be negative: greatest value can be increased. 9523 bool CanWiden = !L.NonNegative || !R.NonNegative; 9524 bool Unsigned = L.NonNegative && R.Width == 0; 9525 return IntRange(std::max(L.valueBits(), R.valueBits()) + CanWiden + 9526 !Unsigned, 9527 Unsigned); 9528 } 9529 9530 /// Return the range of a product of the two ranges. 9531 static IntRange product(IntRange L, IntRange R) { 9532 // If both LHS and RHS can be negative, we can form 9533 // -2^L * -2^R = 2^(L + R) 9534 // which requires L + R + 1 value bits to represent. 9535 bool CanWiden = !L.NonNegative && !R.NonNegative; 9536 bool Unsigned = L.NonNegative && R.NonNegative; 9537 return IntRange(L.valueBits() + R.valueBits() + CanWiden + !Unsigned, 9538 Unsigned); 9539 } 9540 9541 /// Return the range of a remainder operation between the two ranges. 9542 static IntRange rem(IntRange L, IntRange R) { 9543 // The result of a remainder can't be larger than the result of 9544 // either side. The sign of the result is the sign of the LHS. 9545 bool Unsigned = L.NonNegative; 9546 return IntRange(std::min(L.valueBits(), R.valueBits()) + !Unsigned, 9547 Unsigned); 9548 } 9549 }; 9550 9551 } // namespace 9552 9553 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, 9554 unsigned MaxWidth) { 9555 if (value.isSigned() && value.isNegative()) 9556 return IntRange(value.getSignificantBits(), false); 9557 9558 if (value.getBitWidth() > MaxWidth) 9559 value = value.trunc(MaxWidth); 9560 9561 // isNonNegative() just checks the sign bit without considering 9562 // signedness. 9563 return IntRange(value.getActiveBits(), true); 9564 } 9565 9566 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, 9567 unsigned MaxWidth) { 9568 if (result.isInt()) 9569 return GetValueRange(C, result.getInt(), MaxWidth); 9570 9571 if (result.isVector()) { 9572 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); 9573 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { 9574 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); 9575 R = IntRange::join(R, El); 9576 } 9577 return R; 9578 } 9579 9580 if (result.isComplexInt()) { 9581 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); 9582 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); 9583 return IntRange::join(R, I); 9584 } 9585 9586 // This can happen with lossless casts to intptr_t of "based" lvalues. 9587 // Assume it might use arbitrary bits. 9588 // FIXME: The only reason we need to pass the type in here is to get 9589 // the sign right on this one case. It would be nice if APValue 9590 // preserved this. 9591 assert(result.isLValue() || result.isAddrLabelDiff()); 9592 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); 9593 } 9594 9595 static QualType GetExprType(const Expr *E) { 9596 QualType Ty = E->getType(); 9597 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) 9598 Ty = AtomicRHS->getValueType(); 9599 return Ty; 9600 } 9601 9602 /// Attempts to estimate an approximate range for the given integer expression. 9603 /// Returns a range if successful, otherwise it returns \c std::nullopt if a 9604 /// reliable estimation cannot be determined. 9605 /// 9606 /// \param MaxWidth The width to which the value will be truncated. 9607 /// \param InConstantContext If \c true, interpret the expression within a 9608 /// constant context. 9609 /// \param Approximate If \c true, provide a likely range of values by assuming 9610 /// that arithmetic on narrower types remains within those types. 9611 /// If \c false, return a range that includes all possible values 9612 /// resulting from the expression. 9613 /// \returns A range of values that the expression might take, or 9614 /// std::nullopt if a reliable estimation cannot be determined. 9615 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E, 9616 unsigned MaxWidth, 9617 bool InConstantContext, 9618 bool Approximate) { 9619 E = E->IgnoreParens(); 9620 9621 // Try a full evaluation first. 9622 Expr::EvalResult result; 9623 if (E->EvaluateAsRValue(result, C, InConstantContext)) 9624 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); 9625 9626 // I think we only want to look through implicit casts here; if the 9627 // user has an explicit widening cast, we should treat the value as 9628 // being of the new, wider type. 9629 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) { 9630 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) 9631 return TryGetExprRange(C, CE->getSubExpr(), MaxWidth, InConstantContext, 9632 Approximate); 9633 9634 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); 9635 9636 bool isIntegerCast = CE->getCastKind() == CK_IntegralCast || 9637 CE->getCastKind() == CK_BooleanToSignedIntegral; 9638 9639 // Assume that non-integer casts can span the full range of the type. 9640 if (!isIntegerCast) 9641 return OutputTypeRange; 9642 9643 std::optional<IntRange> SubRange = TryGetExprRange( 9644 C, CE->getSubExpr(), std::min(MaxWidth, OutputTypeRange.Width), 9645 InConstantContext, Approximate); 9646 if (!SubRange) 9647 return std::nullopt; 9648 9649 // Bail out if the subexpr's range is as wide as the cast type. 9650 if (SubRange->Width >= OutputTypeRange.Width) 9651 return OutputTypeRange; 9652 9653 // Otherwise, we take the smaller width, and we're non-negative if 9654 // either the output type or the subexpr is. 9655 return IntRange(SubRange->Width, 9656 SubRange->NonNegative || OutputTypeRange.NonNegative); 9657 } 9658 9659 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 9660 // If we can fold the condition, just take that operand. 9661 bool CondResult; 9662 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) 9663 return TryGetExprRange( 9664 C, CondResult ? CO->getTrueExpr() : CO->getFalseExpr(), MaxWidth, 9665 InConstantContext, Approximate); 9666 9667 // Otherwise, conservatively merge. 9668 // TryGetExprRange requires an integer expression, but a throw expression 9669 // results in a void type. 9670 Expr *TrueExpr = CO->getTrueExpr(); 9671 if (TrueExpr->getType()->isVoidType()) 9672 return std::nullopt; 9673 9674 std::optional<IntRange> L = 9675 TryGetExprRange(C, TrueExpr, MaxWidth, InConstantContext, Approximate); 9676 if (!L) 9677 return std::nullopt; 9678 9679 Expr *FalseExpr = CO->getFalseExpr(); 9680 if (FalseExpr->getType()->isVoidType()) 9681 return std::nullopt; 9682 9683 std::optional<IntRange> R = 9684 TryGetExprRange(C, FalseExpr, MaxWidth, InConstantContext, Approximate); 9685 if (!R) 9686 return std::nullopt; 9687 9688 return IntRange::join(*L, *R); 9689 } 9690 9691 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 9692 IntRange (*Combine)(IntRange, IntRange) = IntRange::join; 9693 9694 switch (BO->getOpcode()) { 9695 case BO_Cmp: 9696 llvm_unreachable("builtin <=> should have class type"); 9697 9698 // Boolean-valued operations are single-bit and positive. 9699 case BO_LAnd: 9700 case BO_LOr: 9701 case BO_LT: 9702 case BO_GT: 9703 case BO_LE: 9704 case BO_GE: 9705 case BO_EQ: 9706 case BO_NE: 9707 return IntRange::forBoolType(); 9708 9709 // The type of the assignments is the type of the LHS, so the RHS 9710 // is not necessarily the same type. 9711 case BO_MulAssign: 9712 case BO_DivAssign: 9713 case BO_RemAssign: 9714 case BO_AddAssign: 9715 case BO_SubAssign: 9716 case BO_XorAssign: 9717 case BO_OrAssign: 9718 // TODO: bitfields? 9719 return IntRange::forValueOfType(C, GetExprType(E)); 9720 9721 // Simple assignments just pass through the RHS, which will have 9722 // been coerced to the LHS type. 9723 case BO_Assign: 9724 // TODO: bitfields? 9725 return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, 9726 Approximate); 9727 9728 // Operations with opaque sources are black-listed. 9729 case BO_PtrMemD: 9730 case BO_PtrMemI: 9731 return IntRange::forValueOfType(C, GetExprType(E)); 9732 9733 // Bitwise-and uses the *infinum* of the two source ranges. 9734 case BO_And: 9735 case BO_AndAssign: 9736 Combine = IntRange::bit_and; 9737 break; 9738 9739 // Left shift gets black-listed based on a judgement call. 9740 case BO_Shl: 9741 // ...except that we want to treat '1 << (blah)' as logically 9742 // positive. It's an important idiom. 9743 if (IntegerLiteral *I 9744 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { 9745 if (I->getValue() == 1) { 9746 IntRange R = IntRange::forValueOfType(C, GetExprType(E)); 9747 return IntRange(R.Width, /*NonNegative*/ true); 9748 } 9749 } 9750 [[fallthrough]]; 9751 9752 case BO_ShlAssign: 9753 return IntRange::forValueOfType(C, GetExprType(E)); 9754 9755 // Right shift by a constant can narrow its left argument. 9756 case BO_Shr: 9757 case BO_ShrAssign: { 9758 std::optional<IntRange> L = TryGetExprRange( 9759 C, BO->getLHS(), MaxWidth, InConstantContext, Approximate); 9760 if (!L) 9761 return std::nullopt; 9762 9763 // If the shift amount is a positive constant, drop the width by 9764 // that much. 9765 if (std::optional<llvm::APSInt> shift = 9766 BO->getRHS()->getIntegerConstantExpr(C)) { 9767 if (shift->isNonNegative()) { 9768 if (shift->uge(L->Width)) 9769 L->Width = (L->NonNegative ? 0 : 1); 9770 else 9771 L->Width -= shift->getZExtValue(); 9772 } 9773 } 9774 9775 return L; 9776 } 9777 9778 // Comma acts as its right operand. 9779 case BO_Comma: 9780 return TryGetExprRange(C, BO->getRHS(), MaxWidth, InConstantContext, 9781 Approximate); 9782 9783 case BO_Add: 9784 if (!Approximate) 9785 Combine = IntRange::sum; 9786 break; 9787 9788 case BO_Sub: 9789 if (BO->getLHS()->getType()->isPointerType()) 9790 return IntRange::forValueOfType(C, GetExprType(E)); 9791 if (!Approximate) 9792 Combine = IntRange::difference; 9793 break; 9794 9795 case BO_Mul: 9796 if (!Approximate) 9797 Combine = IntRange::product; 9798 break; 9799 9800 // The width of a division result is mostly determined by the size 9801 // of the LHS. 9802 case BO_Div: { 9803 // Don't 'pre-truncate' the operands. 9804 unsigned opWidth = C.getIntWidth(GetExprType(E)); 9805 std::optional<IntRange> L = TryGetExprRange( 9806 C, BO->getLHS(), opWidth, InConstantContext, Approximate); 9807 if (!L) 9808 return std::nullopt; 9809 9810 // If the divisor is constant, use that. 9811 if (std::optional<llvm::APSInt> divisor = 9812 BO->getRHS()->getIntegerConstantExpr(C)) { 9813 unsigned log2 = divisor->logBase2(); // floor(log_2(divisor)) 9814 if (log2 >= L->Width) 9815 L->Width = (L->NonNegative ? 0 : 1); 9816 else 9817 L->Width = std::min(L->Width - log2, MaxWidth); 9818 return L; 9819 } 9820 9821 // Otherwise, just use the LHS's width. 9822 // FIXME: This is wrong if the LHS could be its minimal value and the RHS 9823 // could be -1. 9824 std::optional<IntRange> R = TryGetExprRange( 9825 C, BO->getRHS(), opWidth, InConstantContext, Approximate); 9826 if (!R) 9827 return std::nullopt; 9828 9829 return IntRange(L->Width, L->NonNegative && R->NonNegative); 9830 } 9831 9832 case BO_Rem: 9833 Combine = IntRange::rem; 9834 break; 9835 9836 // The default behavior is okay for these. 9837 case BO_Xor: 9838 case BO_Or: 9839 break; 9840 } 9841 9842 // Combine the two ranges, but limit the result to the type in which we 9843 // performed the computation. 9844 QualType T = GetExprType(E); 9845 unsigned opWidth = C.getIntWidth(T); 9846 std::optional<IntRange> L = TryGetExprRange(C, BO->getLHS(), opWidth, 9847 InConstantContext, Approximate); 9848 if (!L) 9849 return std::nullopt; 9850 9851 std::optional<IntRange> R = TryGetExprRange(C, BO->getRHS(), opWidth, 9852 InConstantContext, Approximate); 9853 if (!R) 9854 return std::nullopt; 9855 9856 IntRange C = Combine(*L, *R); 9857 C.NonNegative |= T->isUnsignedIntegerOrEnumerationType(); 9858 C.Width = std::min(C.Width, MaxWidth); 9859 return C; 9860 } 9861 9862 if (const auto *UO = dyn_cast<UnaryOperator>(E)) { 9863 switch (UO->getOpcode()) { 9864 // Boolean-valued operations are white-listed. 9865 case UO_LNot: 9866 return IntRange::forBoolType(); 9867 9868 // Operations with opaque sources are black-listed. 9869 case UO_Deref: 9870 case UO_AddrOf: // should be impossible 9871 return IntRange::forValueOfType(C, GetExprType(E)); 9872 9873 default: 9874 return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext, 9875 Approximate); 9876 } 9877 } 9878 9879 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 9880 return TryGetExprRange(C, OVE->getSourceExpr(), MaxWidth, InConstantContext, 9881 Approximate); 9882 9883 if (const auto *BitField = E->getSourceBitField()) 9884 return IntRange(BitField->getBitWidthValue(C), 9885 BitField->getType()->isUnsignedIntegerOrEnumerationType()); 9886 9887 if (GetExprType(E)->isVoidType()) 9888 return std::nullopt; 9889 9890 return IntRange::forValueOfType(C, GetExprType(E)); 9891 } 9892 9893 static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E, 9894 bool InConstantContext, 9895 bool Approximate) { 9896 return TryGetExprRange(C, E, C.getIntWidth(GetExprType(E)), InConstantContext, 9897 Approximate); 9898 } 9899 9900 /// Checks whether the given value, which currently has the given 9901 /// source semantics, has the same value when coerced through the 9902 /// target semantics. 9903 static bool IsSameFloatAfterCast(const llvm::APFloat &value, 9904 const llvm::fltSemantics &Src, 9905 const llvm::fltSemantics &Tgt) { 9906 llvm::APFloat truncated = value; 9907 9908 bool ignored; 9909 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); 9910 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); 9911 9912 return truncated.bitwiseIsEqual(value); 9913 } 9914 9915 /// Checks whether the given value, which currently has the given 9916 /// source semantics, has the same value when coerced through the 9917 /// target semantics. 9918 /// 9919 /// The value might be a vector of floats (or a complex number). 9920 static bool IsSameFloatAfterCast(const APValue &value, 9921 const llvm::fltSemantics &Src, 9922 const llvm::fltSemantics &Tgt) { 9923 if (value.isFloat()) 9924 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); 9925 9926 if (value.isVector()) { 9927 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) 9928 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) 9929 return false; 9930 return true; 9931 } 9932 9933 assert(value.isComplexFloat()); 9934 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && 9935 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); 9936 } 9937 9938 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC, 9939 bool IsListInit = false); 9940 9941 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) { 9942 // Suppress cases where we are comparing against an enum constant. 9943 if (const DeclRefExpr *DR = 9944 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) 9945 if (isa<EnumConstantDecl>(DR->getDecl())) 9946 return true; 9947 9948 // Suppress cases where the value is expanded from a macro, unless that macro 9949 // is how a language represents a boolean literal. This is the case in both C 9950 // and Objective-C. 9951 SourceLocation BeginLoc = E->getBeginLoc(); 9952 if (BeginLoc.isMacroID()) { 9953 StringRef MacroName = Lexer::getImmediateMacroName( 9954 BeginLoc, S.getSourceManager(), S.getLangOpts()); 9955 return MacroName != "YES" && MacroName != "NO" && 9956 MacroName != "true" && MacroName != "false"; 9957 } 9958 9959 return false; 9960 } 9961 9962 static bool isKnownToHaveUnsignedValue(Expr *E) { 9963 return E->getType()->isIntegerType() && 9964 (!E->getType()->isSignedIntegerType() || 9965 !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType()); 9966 } 9967 9968 namespace { 9969 /// The promoted range of values of a type. In general this has the 9970 /// following structure: 9971 /// 9972 /// |-----------| . . . |-----------| 9973 /// ^ ^ ^ ^ 9974 /// Min HoleMin HoleMax Max 9975 /// 9976 /// ... where there is only a hole if a signed type is promoted to unsigned 9977 /// (in which case Min and Max are the smallest and largest representable 9978 /// values). 9979 struct PromotedRange { 9980 // Min, or HoleMax if there is a hole. 9981 llvm::APSInt PromotedMin; 9982 // Max, or HoleMin if there is a hole. 9983 llvm::APSInt PromotedMax; 9984 9985 PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) { 9986 if (R.Width == 0) 9987 PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned); 9988 else if (R.Width >= BitWidth && !Unsigned) { 9989 // Promotion made the type *narrower*. This happens when promoting 9990 // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'. 9991 // Treat all values of 'signed int' as being in range for now. 9992 PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned); 9993 PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned); 9994 } else { 9995 PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative) 9996 .extOrTrunc(BitWidth); 9997 PromotedMin.setIsUnsigned(Unsigned); 9998 9999 PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative) 10000 .extOrTrunc(BitWidth); 10001 PromotedMax.setIsUnsigned(Unsigned); 10002 } 10003 } 10004 10005 // Determine whether this range is contiguous (has no hole). 10006 bool isContiguous() const { return PromotedMin <= PromotedMax; } 10007 10008 // Where a constant value is within the range. 10009 enum ComparisonResult { 10010 LT = 0x1, 10011 LE = 0x2, 10012 GT = 0x4, 10013 GE = 0x8, 10014 EQ = 0x10, 10015 NE = 0x20, 10016 InRangeFlag = 0x40, 10017 10018 Less = LE | LT | NE, 10019 Min = LE | InRangeFlag, 10020 InRange = InRangeFlag, 10021 Max = GE | InRangeFlag, 10022 Greater = GE | GT | NE, 10023 10024 OnlyValue = LE | GE | EQ | InRangeFlag, 10025 InHole = NE 10026 }; 10027 10028 ComparisonResult compare(const llvm::APSInt &Value) const { 10029 assert(Value.getBitWidth() == PromotedMin.getBitWidth() && 10030 Value.isUnsigned() == PromotedMin.isUnsigned()); 10031 if (!isContiguous()) { 10032 assert(Value.isUnsigned() && "discontiguous range for signed compare"); 10033 if (Value.isMinValue()) return Min; 10034 if (Value.isMaxValue()) return Max; 10035 if (Value >= PromotedMin) return InRange; 10036 if (Value <= PromotedMax) return InRange; 10037 return InHole; 10038 } 10039 10040 switch (llvm::APSInt::compareValues(Value, PromotedMin)) { 10041 case -1: return Less; 10042 case 0: return PromotedMin == PromotedMax ? OnlyValue : Min; 10043 case 1: 10044 switch (llvm::APSInt::compareValues(Value, PromotedMax)) { 10045 case -1: return InRange; 10046 case 0: return Max; 10047 case 1: return Greater; 10048 } 10049 } 10050 10051 llvm_unreachable("impossible compare result"); 10052 } 10053 10054 static std::optional<StringRef> 10055 constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) { 10056 if (Op == BO_Cmp) { 10057 ComparisonResult LTFlag = LT, GTFlag = GT; 10058 if (ConstantOnRHS) std::swap(LTFlag, GTFlag); 10059 10060 if (R & EQ) return StringRef("'std::strong_ordering::equal'"); 10061 if (R & LTFlag) return StringRef("'std::strong_ordering::less'"); 10062 if (R & GTFlag) return StringRef("'std::strong_ordering::greater'"); 10063 return std::nullopt; 10064 } 10065 10066 ComparisonResult TrueFlag, FalseFlag; 10067 if (Op == BO_EQ) { 10068 TrueFlag = EQ; 10069 FalseFlag = NE; 10070 } else if (Op == BO_NE) { 10071 TrueFlag = NE; 10072 FalseFlag = EQ; 10073 } else { 10074 if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) { 10075 TrueFlag = LT; 10076 FalseFlag = GE; 10077 } else { 10078 TrueFlag = GT; 10079 FalseFlag = LE; 10080 } 10081 if (Op == BO_GE || Op == BO_LE) 10082 std::swap(TrueFlag, FalseFlag); 10083 } 10084 if (R & TrueFlag) 10085 return StringRef("true"); 10086 if (R & FalseFlag) 10087 return StringRef("false"); 10088 return std::nullopt; 10089 } 10090 }; 10091 } 10092 10093 static bool HasEnumType(Expr *E) { 10094 // Strip off implicit integral promotions. 10095 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 10096 if (ICE->getCastKind() != CK_IntegralCast && 10097 ICE->getCastKind() != CK_NoOp) 10098 break; 10099 E = ICE->getSubExpr(); 10100 } 10101 10102 return E->getType()->isEnumeralType(); 10103 } 10104 10105 static int classifyConstantValue(Expr *Constant) { 10106 // The values of this enumeration are used in the diagnostics 10107 // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare. 10108 enum ConstantValueKind { 10109 Miscellaneous = 0, 10110 LiteralTrue, 10111 LiteralFalse 10112 }; 10113 if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant)) 10114 return BL->getValue() ? ConstantValueKind::LiteralTrue 10115 : ConstantValueKind::LiteralFalse; 10116 return ConstantValueKind::Miscellaneous; 10117 } 10118 10119 static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E, 10120 Expr *Constant, Expr *Other, 10121 const llvm::APSInt &Value, 10122 bool RhsConstant) { 10123 if (S.inTemplateInstantiation()) 10124 return false; 10125 10126 Expr *OriginalOther = Other; 10127 10128 Constant = Constant->IgnoreParenImpCasts(); 10129 Other = Other->IgnoreParenImpCasts(); 10130 10131 // Suppress warnings on tautological comparisons between values of the same 10132 // enumeration type. There are only two ways we could warn on this: 10133 // - If the constant is outside the range of representable values of 10134 // the enumeration. In such a case, we should warn about the cast 10135 // to enumeration type, not about the comparison. 10136 // - If the constant is the maximum / minimum in-range value. For an 10137 // enumeratin type, such comparisons can be meaningful and useful. 10138 if (Constant->getType()->isEnumeralType() && 10139 S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType())) 10140 return false; 10141 10142 std::optional<IntRange> OtherValueRange = TryGetExprRange( 10143 S.Context, Other, S.isConstantEvaluatedContext(), /*Approximate=*/false); 10144 if (!OtherValueRange) 10145 return false; 10146 10147 QualType OtherT = Other->getType(); 10148 if (const auto *AT = OtherT->getAs<AtomicType>()) 10149 OtherT = AT->getValueType(); 10150 IntRange OtherTypeRange = IntRange::forValueOfType(S.Context, OtherT); 10151 10152 // Special case for ObjC BOOL on targets where its a typedef for a signed char 10153 // (Namely, macOS). FIXME: IntRange::forValueOfType should do this. 10154 bool IsObjCSignedCharBool = S.getLangOpts().ObjC && 10155 S.ObjC().NSAPIObj->isObjCBOOLType(OtherT) && 10156 OtherT->isSpecificBuiltinType(BuiltinType::SChar); 10157 10158 // Whether we're treating Other as being a bool because of the form of 10159 // expression despite it having another type (typically 'int' in C). 10160 bool OtherIsBooleanDespiteType = 10161 !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue(); 10162 if (OtherIsBooleanDespiteType || IsObjCSignedCharBool) 10163 OtherTypeRange = *OtherValueRange = IntRange::forBoolType(); 10164 10165 // Check if all values in the range of possible values of this expression 10166 // lead to the same comparison outcome. 10167 PromotedRange OtherPromotedValueRange(*OtherValueRange, Value.getBitWidth(), 10168 Value.isUnsigned()); 10169 auto Cmp = OtherPromotedValueRange.compare(Value); 10170 auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant); 10171 if (!Result) 10172 return false; 10173 10174 // Also consider the range determined by the type alone. This allows us to 10175 // classify the warning under the proper diagnostic group. 10176 bool TautologicalTypeCompare = false; 10177 { 10178 PromotedRange OtherPromotedTypeRange(OtherTypeRange, Value.getBitWidth(), 10179 Value.isUnsigned()); 10180 auto TypeCmp = OtherPromotedTypeRange.compare(Value); 10181 if (auto TypeResult = PromotedRange::constantValue(E->getOpcode(), TypeCmp, 10182 RhsConstant)) { 10183 TautologicalTypeCompare = true; 10184 Cmp = TypeCmp; 10185 Result = TypeResult; 10186 } 10187 } 10188 10189 // Don't warn if the non-constant operand actually always evaluates to the 10190 // same value. 10191 if (!TautologicalTypeCompare && OtherValueRange->Width == 0) 10192 return false; 10193 10194 // Suppress the diagnostic for an in-range comparison if the constant comes 10195 // from a macro or enumerator. We don't want to diagnose 10196 // 10197 // some_long_value <= INT_MAX 10198 // 10199 // when sizeof(int) == sizeof(long). 10200 bool InRange = Cmp & PromotedRange::InRangeFlag; 10201 if (InRange && IsEnumConstOrFromMacro(S, Constant)) 10202 return false; 10203 10204 // A comparison of an unsigned bit-field against 0 is really a type problem, 10205 // even though at the type level the bit-field might promote to 'signed int'. 10206 if (Other->refersToBitField() && InRange && Value == 0 && 10207 Other->getType()->isUnsignedIntegerOrEnumerationType()) 10208 TautologicalTypeCompare = true; 10209 10210 // If this is a comparison to an enum constant, include that 10211 // constant in the diagnostic. 10212 const EnumConstantDecl *ED = nullptr; 10213 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) 10214 ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); 10215 10216 // Should be enough for uint128 (39 decimal digits) 10217 SmallString<64> PrettySourceValue; 10218 llvm::raw_svector_ostream OS(PrettySourceValue); 10219 if (ED) { 10220 OS << '\'' << *ED << "' (" << Value << ")"; 10221 } else if (auto *BL = dyn_cast<ObjCBoolLiteralExpr>( 10222 Constant->IgnoreParenImpCasts())) { 10223 OS << (BL->getValue() ? "YES" : "NO"); 10224 } else { 10225 OS << Value; 10226 } 10227 10228 if (!TautologicalTypeCompare) { 10229 S.Diag(E->getOperatorLoc(), diag::warn_tautological_compare_value_range) 10230 << RhsConstant << OtherValueRange->Width << OtherValueRange->NonNegative 10231 << E->getOpcodeStr() << OS.str() << *Result 10232 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10233 return true; 10234 } 10235 10236 if (IsObjCSignedCharBool) { 10237 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10238 S.PDiag(diag::warn_tautological_compare_objc_bool) 10239 << OS.str() << *Result); 10240 return true; 10241 } 10242 10243 // FIXME: We use a somewhat different formatting for the in-range cases and 10244 // cases involving boolean values for historical reasons. We should pick a 10245 // consistent way of presenting these diagnostics. 10246 if (!InRange || Other->isKnownToHaveBooleanValue()) { 10247 10248 S.DiagRuntimeBehavior( 10249 E->getOperatorLoc(), E, 10250 S.PDiag(!InRange ? diag::warn_out_of_range_compare 10251 : diag::warn_tautological_bool_compare) 10252 << OS.str() << classifyConstantValue(Constant) << OtherT 10253 << OtherIsBooleanDespiteType << *Result 10254 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); 10255 } else { 10256 bool IsCharTy = OtherT.withoutLocalFastQualifiers() == S.Context.CharTy; 10257 unsigned Diag = 10258 (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0) 10259 ? (HasEnumType(OriginalOther) 10260 ? diag::warn_unsigned_enum_always_true_comparison 10261 : IsCharTy ? diag::warn_unsigned_char_always_true_comparison 10262 : diag::warn_unsigned_always_true_comparison) 10263 : diag::warn_tautological_constant_compare; 10264 10265 S.Diag(E->getOperatorLoc(), Diag) 10266 << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result 10267 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 10268 } 10269 10270 return true; 10271 } 10272 10273 /// Analyze the operands of the given comparison. Implements the 10274 /// fallback case from AnalyzeComparison. 10275 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { 10276 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10277 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10278 } 10279 10280 /// Implements -Wsign-compare. 10281 /// 10282 /// \param E the binary operator to check for warnings 10283 static void AnalyzeComparison(Sema &S, BinaryOperator *E) { 10284 // The type the comparison is being performed in. 10285 QualType T = E->getLHS()->getType(); 10286 10287 // Only analyze comparison operators where both sides have been converted to 10288 // the same type. 10289 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) 10290 return AnalyzeImpConvsInComparison(S, E); 10291 10292 // Don't analyze value-dependent comparisons directly. 10293 if (E->isValueDependent()) 10294 return AnalyzeImpConvsInComparison(S, E); 10295 10296 Expr *LHS = E->getLHS(); 10297 Expr *RHS = E->getRHS(); 10298 10299 if (T->isIntegralType(S.Context)) { 10300 std::optional<llvm::APSInt> RHSValue = 10301 RHS->getIntegerConstantExpr(S.Context); 10302 std::optional<llvm::APSInt> LHSValue = 10303 LHS->getIntegerConstantExpr(S.Context); 10304 10305 // We don't care about expressions whose result is a constant. 10306 if (RHSValue && LHSValue) 10307 return AnalyzeImpConvsInComparison(S, E); 10308 10309 // We only care about expressions where just one side is literal 10310 if ((bool)RHSValue ^ (bool)LHSValue) { 10311 // Is the constant on the RHS or LHS? 10312 const bool RhsConstant = (bool)RHSValue; 10313 Expr *Const = RhsConstant ? RHS : LHS; 10314 Expr *Other = RhsConstant ? LHS : RHS; 10315 const llvm::APSInt &Value = RhsConstant ? *RHSValue : *LHSValue; 10316 10317 // Check whether an integer constant comparison results in a value 10318 // of 'true' or 'false'. 10319 if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant)) 10320 return AnalyzeImpConvsInComparison(S, E); 10321 } 10322 } 10323 10324 if (!T->hasUnsignedIntegerRepresentation()) { 10325 // We don't do anything special if this isn't an unsigned integral 10326 // comparison: we're only interested in integral comparisons, and 10327 // signed comparisons only happen in cases we don't care to warn about. 10328 return AnalyzeImpConvsInComparison(S, E); 10329 } 10330 10331 LHS = LHS->IgnoreParenImpCasts(); 10332 RHS = RHS->IgnoreParenImpCasts(); 10333 10334 if (!S.getLangOpts().CPlusPlus) { 10335 // Avoid warning about comparison of integers with different signs when 10336 // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of 10337 // the type of `E`. 10338 if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType())) 10339 LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10340 if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType())) 10341 RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts(); 10342 } 10343 10344 // Check to see if one of the (unmodified) operands is of different 10345 // signedness. 10346 Expr *signedOperand, *unsignedOperand; 10347 if (LHS->getType()->hasSignedIntegerRepresentation()) { 10348 assert(!RHS->getType()->hasSignedIntegerRepresentation() && 10349 "unsigned comparison between two signed integer expressions?"); 10350 signedOperand = LHS; 10351 unsignedOperand = RHS; 10352 } else if (RHS->getType()->hasSignedIntegerRepresentation()) { 10353 signedOperand = RHS; 10354 unsignedOperand = LHS; 10355 } else { 10356 return AnalyzeImpConvsInComparison(S, E); 10357 } 10358 10359 // Otherwise, calculate the effective range of the signed operand. 10360 std::optional<IntRange> signedRange = 10361 TryGetExprRange(S.Context, signedOperand, S.isConstantEvaluatedContext(), 10362 /*Approximate=*/true); 10363 if (!signedRange) 10364 return; 10365 10366 // Go ahead and analyze implicit conversions in the operands. Note 10367 // that we skip the implicit conversions on both sides. 10368 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); 10369 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); 10370 10371 // If the signed range is non-negative, -Wsign-compare won't fire. 10372 if (signedRange->NonNegative) 10373 return; 10374 10375 // For (in)equality comparisons, if the unsigned operand is a 10376 // constant which cannot collide with a overflowed signed operand, 10377 // then reinterpreting the signed operand as unsigned will not 10378 // change the result of the comparison. 10379 if (E->isEqualityOp()) { 10380 unsigned comparisonWidth = S.Context.getIntWidth(T); 10381 std::optional<IntRange> unsignedRange = TryGetExprRange( 10382 S.Context, unsignedOperand, S.isConstantEvaluatedContext(), 10383 /*Approximate=*/true); 10384 if (!unsignedRange) 10385 return; 10386 10387 // We should never be unable to prove that the unsigned operand is 10388 // non-negative. 10389 assert(unsignedRange->NonNegative && "unsigned range includes negative?"); 10390 10391 if (unsignedRange->Width < comparisonWidth) 10392 return; 10393 } 10394 10395 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 10396 S.PDiag(diag::warn_mixed_sign_comparison) 10397 << LHS->getType() << RHS->getType() 10398 << LHS->getSourceRange() << RHS->getSourceRange()); 10399 } 10400 10401 /// Analyzes an attempt to assign the given value to a bitfield. 10402 /// 10403 /// Returns true if there was something fishy about the attempt. 10404 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, 10405 SourceLocation InitLoc) { 10406 assert(Bitfield->isBitField()); 10407 if (Bitfield->isInvalidDecl()) 10408 return false; 10409 10410 // White-list bool bitfields. 10411 QualType BitfieldType = Bitfield->getType(); 10412 if (BitfieldType->isBooleanType()) 10413 return false; 10414 10415 if (BitfieldType->isEnumeralType()) { 10416 EnumDecl *BitfieldEnumDecl = BitfieldType->castAs<EnumType>()->getDecl(); 10417 // If the underlying enum type was not explicitly specified as an unsigned 10418 // type and the enum contain only positive values, MSVC++ will cause an 10419 // inconsistency by storing this as a signed type. 10420 if (S.getLangOpts().CPlusPlus11 && 10421 !BitfieldEnumDecl->getIntegerTypeSourceInfo() && 10422 BitfieldEnumDecl->getNumPositiveBits() > 0 && 10423 BitfieldEnumDecl->getNumNegativeBits() == 0) { 10424 S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield) 10425 << BitfieldEnumDecl; 10426 } 10427 } 10428 10429 // Ignore value- or type-dependent expressions. 10430 if (Bitfield->getBitWidth()->isValueDependent() || 10431 Bitfield->getBitWidth()->isTypeDependent() || 10432 Init->isValueDependent() || 10433 Init->isTypeDependent()) 10434 return false; 10435 10436 Expr *OriginalInit = Init->IgnoreParenImpCasts(); 10437 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); 10438 10439 Expr::EvalResult Result; 10440 if (!OriginalInit->EvaluateAsInt(Result, S.Context, 10441 Expr::SE_AllowSideEffects)) { 10442 // The RHS is not constant. If the RHS has an enum type, make sure the 10443 // bitfield is wide enough to hold all the values of the enum without 10444 // truncation. 10445 if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) { 10446 EnumDecl *ED = EnumTy->getDecl(); 10447 bool SignedBitfield = BitfieldType->isSignedIntegerType(); 10448 10449 // Enum types are implicitly signed on Windows, so check if there are any 10450 // negative enumerators to see if the enum was intended to be signed or 10451 // not. 10452 bool SignedEnum = ED->getNumNegativeBits() > 0; 10453 10454 // Check for surprising sign changes when assigning enum values to a 10455 // bitfield of different signedness. If the bitfield is signed and we 10456 // have exactly the right number of bits to store this unsigned enum, 10457 // suggest changing the enum to an unsigned type. This typically happens 10458 // on Windows where unfixed enums always use an underlying type of 'int'. 10459 unsigned DiagID = 0; 10460 if (SignedEnum && !SignedBitfield) { 10461 DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum; 10462 } else if (SignedBitfield && !SignedEnum && 10463 ED->getNumPositiveBits() == FieldWidth) { 10464 DiagID = diag::warn_signed_bitfield_enum_conversion; 10465 } 10466 10467 if (DiagID) { 10468 S.Diag(InitLoc, DiagID) << Bitfield << ED; 10469 TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo(); 10470 SourceRange TypeRange = 10471 TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange(); 10472 S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign) 10473 << SignedEnum << TypeRange; 10474 } 10475 10476 // Compute the required bitwidth. If the enum has negative values, we need 10477 // one more bit than the normal number of positive bits to represent the 10478 // sign bit. 10479 unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1, 10480 ED->getNumNegativeBits()) 10481 : ED->getNumPositiveBits(); 10482 10483 // Check the bitwidth. 10484 if (BitsNeeded > FieldWidth) { 10485 Expr *WidthExpr = Bitfield->getBitWidth(); 10486 S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum) 10487 << Bitfield << ED; 10488 S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield) 10489 << BitsNeeded << ED << WidthExpr->getSourceRange(); 10490 } 10491 } 10492 10493 return false; 10494 } 10495 10496 llvm::APSInt Value = Result.Val.getInt(); 10497 10498 unsigned OriginalWidth = Value.getBitWidth(); 10499 10500 // In C, the macro 'true' from stdbool.h will evaluate to '1'; To reduce 10501 // false positives where the user is demonstrating they intend to use the 10502 // bit-field as a Boolean, check to see if the value is 1 and we're assigning 10503 // to a one-bit bit-field to see if the value came from a macro named 'true'. 10504 bool OneAssignedToOneBitBitfield = FieldWidth == 1 && Value == 1; 10505 if (OneAssignedToOneBitBitfield && !S.LangOpts.CPlusPlus) { 10506 SourceLocation MaybeMacroLoc = OriginalInit->getBeginLoc(); 10507 if (S.SourceMgr.isInSystemMacro(MaybeMacroLoc) && 10508 S.findMacroSpelling(MaybeMacroLoc, "true")) 10509 return false; 10510 } 10511 10512 if (!Value.isSigned() || Value.isNegative()) 10513 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit)) 10514 if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not) 10515 OriginalWidth = Value.getSignificantBits(); 10516 10517 if (OriginalWidth <= FieldWidth) 10518 return false; 10519 10520 // Compute the value which the bitfield will contain. 10521 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); 10522 TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType()); 10523 10524 // Check whether the stored value is equal to the original value. 10525 TruncatedValue = TruncatedValue.extend(OriginalWidth); 10526 if (llvm::APSInt::isSameValue(Value, TruncatedValue)) 10527 return false; 10528 10529 std::string PrettyValue = toString(Value, 10); 10530 std::string PrettyTrunc = toString(TruncatedValue, 10); 10531 10532 S.Diag(InitLoc, OneAssignedToOneBitBitfield 10533 ? diag::warn_impcast_single_bit_bitield_precision_constant 10534 : diag::warn_impcast_bitfield_precision_constant) 10535 << PrettyValue << PrettyTrunc << OriginalInit->getType() 10536 << Init->getSourceRange(); 10537 10538 return true; 10539 } 10540 10541 /// Analyze the given simple or compound assignment for warning-worthy 10542 /// operations. 10543 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { 10544 // Just recurse on the LHS. 10545 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10546 10547 // We want to recurse on the RHS as normal unless we're assigning to 10548 // a bitfield. 10549 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { 10550 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), 10551 E->getOperatorLoc())) { 10552 // Recurse, ignoring any implicit conversions on the RHS. 10553 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), 10554 E->getOperatorLoc()); 10555 } 10556 } 10557 10558 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10559 10560 // Diagnose implicitly sequentially-consistent atomic assignment. 10561 if (E->getLHS()->getType()->isAtomicType()) 10562 S.Diag(E->getRHS()->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 10563 } 10564 10565 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10566 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 10567 SourceLocation CContext, unsigned diag, 10568 bool pruneControlFlow = false) { 10569 if (pruneControlFlow) { 10570 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10571 S.PDiag(diag) 10572 << SourceType << T << E->getSourceRange() 10573 << SourceRange(CContext)); 10574 return; 10575 } 10576 S.Diag(E->getExprLoc(), diag) 10577 << SourceType << T << E->getSourceRange() << SourceRange(CContext); 10578 } 10579 10580 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 10581 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, 10582 SourceLocation CContext, 10583 unsigned diag, bool pruneControlFlow = false) { 10584 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); 10585 } 10586 10587 /// Diagnose an implicit cast from a floating point value to an integer value. 10588 static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T, 10589 SourceLocation CContext) { 10590 const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool); 10591 const bool PruneWarnings = S.inTemplateInstantiation(); 10592 10593 Expr *InnerE = E->IgnoreParenImpCasts(); 10594 // We also want to warn on, e.g., "int i = -1.234" 10595 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) 10596 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) 10597 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); 10598 10599 const bool IsLiteral = 10600 isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE); 10601 10602 llvm::APFloat Value(0.0); 10603 bool IsConstant = 10604 E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects); 10605 if (!IsConstant) { 10606 if (S.ObjC().isSignedCharBool(T)) { 10607 return S.ObjC().adornBoolConversionDiagWithTernaryFixit( 10608 E, S.Diag(CContext, diag::warn_impcast_float_to_objc_signed_char_bool) 10609 << E->getType()); 10610 } 10611 10612 return DiagnoseImpCast(S, E, T, CContext, 10613 diag::warn_impcast_float_integer, PruneWarnings); 10614 } 10615 10616 bool isExact = false; 10617 10618 llvm::APSInt IntegerValue(S.Context.getIntWidth(T), 10619 T->hasUnsignedIntegerRepresentation()); 10620 llvm::APFloat::opStatus Result = Value.convertToInteger( 10621 IntegerValue, llvm::APFloat::rmTowardZero, &isExact); 10622 10623 // FIXME: Force the precision of the source value down so we don't print 10624 // digits which are usually useless (we don't really care here if we 10625 // truncate a digit by accident in edge cases). Ideally, APFloat::toString 10626 // would automatically print the shortest representation, but it's a bit 10627 // tricky to implement. 10628 SmallString<16> PrettySourceValue; 10629 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); 10630 precision = (precision * 59 + 195) / 196; 10631 Value.toString(PrettySourceValue, precision); 10632 10633 if (S.ObjC().isSignedCharBool(T) && IntegerValue != 0 && IntegerValue != 1) { 10634 return S.ObjC().adornBoolConversionDiagWithTernaryFixit( 10635 E, S.Diag(CContext, diag::warn_impcast_constant_value_to_objc_bool) 10636 << PrettySourceValue); 10637 } 10638 10639 if (Result == llvm::APFloat::opOK && isExact) { 10640 if (IsLiteral) return; 10641 return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer, 10642 PruneWarnings); 10643 } 10644 10645 // Conversion of a floating-point value to a non-bool integer where the 10646 // integral part cannot be represented by the integer type is undefined. 10647 if (!IsBool && Result == llvm::APFloat::opInvalidOp) 10648 return DiagnoseImpCast( 10649 S, E, T, CContext, 10650 IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range 10651 : diag::warn_impcast_float_to_integer_out_of_range, 10652 PruneWarnings); 10653 10654 unsigned DiagID = 0; 10655 if (IsLiteral) { 10656 // Warn on floating point literal to integer. 10657 DiagID = diag::warn_impcast_literal_float_to_integer; 10658 } else if (IntegerValue == 0) { 10659 if (Value.isZero()) { // Skip -0.0 to 0 conversion. 10660 return DiagnoseImpCast(S, E, T, CContext, 10661 diag::warn_impcast_float_integer, PruneWarnings); 10662 } 10663 // Warn on non-zero to zero conversion. 10664 DiagID = diag::warn_impcast_float_to_integer_zero; 10665 } else { 10666 if (IntegerValue.isUnsigned()) { 10667 if (!IntegerValue.isMaxValue()) { 10668 return DiagnoseImpCast(S, E, T, CContext, 10669 diag::warn_impcast_float_integer, PruneWarnings); 10670 } 10671 } else { // IntegerValue.isSigned() 10672 if (!IntegerValue.isMaxSignedValue() && 10673 !IntegerValue.isMinSignedValue()) { 10674 return DiagnoseImpCast(S, E, T, CContext, 10675 diag::warn_impcast_float_integer, PruneWarnings); 10676 } 10677 } 10678 // Warn on evaluatable floating point expression to integer conversion. 10679 DiagID = diag::warn_impcast_float_to_integer; 10680 } 10681 10682 SmallString<16> PrettyTargetValue; 10683 if (IsBool) 10684 PrettyTargetValue = Value.isZero() ? "false" : "true"; 10685 else 10686 IntegerValue.toString(PrettyTargetValue); 10687 10688 if (PruneWarnings) { 10689 S.DiagRuntimeBehavior(E->getExprLoc(), E, 10690 S.PDiag(DiagID) 10691 << E->getType() << T.getUnqualifiedType() 10692 << PrettySourceValue << PrettyTargetValue 10693 << E->getSourceRange() << SourceRange(CContext)); 10694 } else { 10695 S.Diag(E->getExprLoc(), DiagID) 10696 << E->getType() << T.getUnqualifiedType() << PrettySourceValue 10697 << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext); 10698 } 10699 } 10700 10701 /// Analyze the given compound assignment for the possible losing of 10702 /// floating-point precision. 10703 static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) { 10704 assert(isa<CompoundAssignOperator>(E) && 10705 "Must be compound assignment operation"); 10706 // Recurse on the LHS and RHS in here 10707 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 10708 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 10709 10710 if (E->getLHS()->getType()->isAtomicType()) 10711 S.Diag(E->getOperatorLoc(), diag::warn_atomic_implicit_seq_cst); 10712 10713 // Now check the outermost expression 10714 const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>(); 10715 const auto *RBT = cast<CompoundAssignOperator>(E) 10716 ->getComputationResultType() 10717 ->getAs<BuiltinType>(); 10718 10719 // The below checks assume source is floating point. 10720 if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return; 10721 10722 // If source is floating point but target is an integer. 10723 if (ResultBT->isInteger()) 10724 return DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(), 10725 E->getExprLoc(), diag::warn_impcast_float_integer); 10726 10727 if (!ResultBT->isFloatingPoint()) 10728 return; 10729 10730 // If both source and target are floating points, warn about losing precision. 10731 int Order = S.getASTContext().getFloatingTypeSemanticOrder( 10732 QualType(ResultBT, 0), QualType(RBT, 0)); 10733 if (Order < 0 && !S.SourceMgr.isInSystemMacro(E->getOperatorLoc())) 10734 // warn about dropping FP rank. 10735 DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(), 10736 diag::warn_impcast_float_result_precision); 10737 } 10738 10739 static std::string PrettyPrintInRange(const llvm::APSInt &Value, 10740 IntRange Range) { 10741 if (!Range.Width) return "0"; 10742 10743 llvm::APSInt ValueInRange = Value; 10744 ValueInRange.setIsSigned(!Range.NonNegative); 10745 ValueInRange = ValueInRange.trunc(Range.Width); 10746 return toString(ValueInRange, 10); 10747 } 10748 10749 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { 10750 if (!isa<ImplicitCastExpr>(Ex)) 10751 return false; 10752 10753 Expr *InnerE = Ex->IgnoreParenImpCasts(); 10754 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); 10755 const Type *Source = 10756 S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); 10757 if (Target->isDependentType()) 10758 return false; 10759 10760 const BuiltinType *FloatCandidateBT = 10761 dyn_cast<BuiltinType>(ToBool ? Source : Target); 10762 const Type *BoolCandidateType = ToBool ? Target : Source; 10763 10764 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && 10765 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); 10766 } 10767 10768 static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, 10769 SourceLocation CC) { 10770 unsigned NumArgs = TheCall->getNumArgs(); 10771 for (unsigned i = 0; i < NumArgs; ++i) { 10772 Expr *CurrA = TheCall->getArg(i); 10773 if (!IsImplicitBoolFloatConversion(S, CurrA, true)) 10774 continue; 10775 10776 bool IsSwapped = ((i > 0) && 10777 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); 10778 IsSwapped |= ((i < (NumArgs - 1)) && 10779 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); 10780 if (IsSwapped) { 10781 // Warn on this floating-point to bool conversion. 10782 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), 10783 CurrA->getType(), CC, 10784 diag::warn_impcast_floating_point_to_bool); 10785 } 10786 } 10787 } 10788 10789 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, 10790 SourceLocation CC) { 10791 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, 10792 E->getExprLoc())) 10793 return; 10794 10795 // Don't warn on functions which have return type nullptr_t. 10796 if (isa<CallExpr>(E)) 10797 return; 10798 10799 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). 10800 const Expr *NewE = E->IgnoreParenImpCasts(); 10801 bool IsGNUNullExpr = isa<GNUNullExpr>(NewE); 10802 bool HasNullPtrType = NewE->getType()->isNullPtrType(); 10803 if (!IsGNUNullExpr && !HasNullPtrType) 10804 return; 10805 10806 // Return if target type is a safe conversion. 10807 if (T->isAnyPointerType() || T->isBlockPointerType() || 10808 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) 10809 return; 10810 10811 SourceLocation Loc = E->getSourceRange().getBegin(); 10812 10813 // Venture through the macro stacks to get to the source of macro arguments. 10814 // The new location is a better location than the complete location that was 10815 // passed in. 10816 Loc = S.SourceMgr.getTopMacroCallerLoc(Loc); 10817 CC = S.SourceMgr.getTopMacroCallerLoc(CC); 10818 10819 // __null is usually wrapped in a macro. Go up a macro if that is the case. 10820 if (IsGNUNullExpr && Loc.isMacroID()) { 10821 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics( 10822 Loc, S.SourceMgr, S.getLangOpts()); 10823 if (MacroName == "NULL") 10824 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin(); 10825 } 10826 10827 // Only warn if the null and context location are in the same macro expansion. 10828 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) 10829 return; 10830 10831 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) 10832 << HasNullPtrType << T << SourceRange(CC) 10833 << FixItHint::CreateReplacement(Loc, 10834 S.getFixItZeroLiteralForType(T, Loc)); 10835 } 10836 10837 // Helper function to filter out cases for constant width constant conversion. 10838 // Don't warn on char array initialization or for non-decimal values. 10839 static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T, 10840 SourceLocation CC) { 10841 // If initializing from a constant, and the constant starts with '0', 10842 // then it is a binary, octal, or hexadecimal. Allow these constants 10843 // to fill all the bits, even if there is a sign change. 10844 if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) { 10845 const char FirstLiteralCharacter = 10846 S.getSourceManager().getCharacterData(IntLit->getBeginLoc())[0]; 10847 if (FirstLiteralCharacter == '0') 10848 return false; 10849 } 10850 10851 // If the CC location points to a '{', and the type is char, then assume 10852 // assume it is an array initialization. 10853 if (CC.isValid() && T->isCharType()) { 10854 const char FirstContextCharacter = 10855 S.getSourceManager().getCharacterData(CC)[0]; 10856 if (FirstContextCharacter == '{') 10857 return false; 10858 } 10859 10860 return true; 10861 } 10862 10863 static const IntegerLiteral *getIntegerLiteral(Expr *E) { 10864 const auto *IL = dyn_cast<IntegerLiteral>(E); 10865 if (!IL) { 10866 if (auto *UO = dyn_cast<UnaryOperator>(E)) { 10867 if (UO->getOpcode() == UO_Minus) 10868 return dyn_cast<IntegerLiteral>(UO->getSubExpr()); 10869 } 10870 } 10871 10872 return IL; 10873 } 10874 10875 static void DiagnoseIntInBoolContext(Sema &S, Expr *E) { 10876 E = E->IgnoreParenImpCasts(); 10877 SourceLocation ExprLoc = E->getExprLoc(); 10878 10879 if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 10880 BinaryOperator::Opcode Opc = BO->getOpcode(); 10881 Expr::EvalResult Result; 10882 // Do not diagnose unsigned shifts. 10883 if (Opc == BO_Shl) { 10884 const auto *LHS = getIntegerLiteral(BO->getLHS()); 10885 const auto *RHS = getIntegerLiteral(BO->getRHS()); 10886 if (LHS && LHS->getValue() == 0) 10887 S.Diag(ExprLoc, diag::warn_left_shift_always) << 0; 10888 else if (!E->isValueDependent() && LHS && RHS && 10889 RHS->getValue().isNonNegative() && 10890 E->EvaluateAsInt(Result, S.Context, Expr::SE_AllowSideEffects)) 10891 S.Diag(ExprLoc, diag::warn_left_shift_always) 10892 << (Result.Val.getInt() != 0); 10893 else if (E->getType()->isSignedIntegerType()) 10894 S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E; 10895 } 10896 } 10897 10898 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 10899 const auto *LHS = getIntegerLiteral(CO->getTrueExpr()); 10900 const auto *RHS = getIntegerLiteral(CO->getFalseExpr()); 10901 if (!LHS || !RHS) 10902 return; 10903 if ((LHS->getValue() == 0 || LHS->getValue() == 1) && 10904 (RHS->getValue() == 0 || RHS->getValue() == 1)) 10905 // Do not diagnose common idioms. 10906 return; 10907 if (LHS->getValue() != 0 && RHS->getValue() != 0) 10908 S.Diag(ExprLoc, diag::warn_integer_constants_in_conditional_always_true); 10909 } 10910 } 10911 10912 void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, 10913 bool *ICContext, bool IsListInit) { 10914 if (E->isTypeDependent() || E->isValueDependent()) return; 10915 10916 const Type *Source = Context.getCanonicalType(E->getType()).getTypePtr(); 10917 const Type *Target = Context.getCanonicalType(T).getTypePtr(); 10918 if (Source == Target) return; 10919 if (Target->isDependentType()) return; 10920 10921 // If the conversion context location is invalid don't complain. We also 10922 // don't want to emit a warning if the issue occurs from the expansion of 10923 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we 10924 // delay this check as long as possible. Once we detect we are in that 10925 // scenario, we just return. 10926 if (CC.isInvalid()) 10927 return; 10928 10929 if (Source->isAtomicType()) 10930 Diag(E->getExprLoc(), diag::warn_atomic_implicit_seq_cst); 10931 10932 // Diagnose implicit casts to bool. 10933 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { 10934 if (isa<StringLiteral>(E)) 10935 // Warn on string literal to bool. Checks for string literals in logical 10936 // and expressions, for instance, assert(0 && "error here"), are 10937 // prevented by a check in AnalyzeImplicitConversions(). 10938 return DiagnoseImpCast(*this, E, T, CC, 10939 diag::warn_impcast_string_literal_to_bool); 10940 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || 10941 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { 10942 // This covers the literal expressions that evaluate to Objective-C 10943 // objects. 10944 return DiagnoseImpCast(*this, E, T, CC, 10945 diag::warn_impcast_objective_c_literal_to_bool); 10946 } 10947 if (Source->isPointerType() || Source->canDecayToPointerType()) { 10948 // Warn on pointer to bool conversion that is always true. 10949 DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, 10950 SourceRange(CC)); 10951 } 10952 } 10953 10954 // If the we're converting a constant to an ObjC BOOL on a platform where BOOL 10955 // is a typedef for signed char (macOS), then that constant value has to be 1 10956 // or 0. 10957 if (ObjC().isSignedCharBool(T) && Source->isIntegralType(Context)) { 10958 Expr::EvalResult Result; 10959 if (E->EvaluateAsInt(Result, getASTContext(), Expr::SE_AllowSideEffects)) { 10960 if (Result.Val.getInt() != 1 && Result.Val.getInt() != 0) { 10961 ObjC().adornBoolConversionDiagWithTernaryFixit( 10962 E, Diag(CC, diag::warn_impcast_constant_value_to_objc_bool) 10963 << toString(Result.Val.getInt(), 10)); 10964 } 10965 return; 10966 } 10967 } 10968 10969 // Check implicit casts from Objective-C collection literals to specialized 10970 // collection types, e.g., NSArray<NSString *> *. 10971 if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E)) 10972 ObjC().checkArrayLiteral(QualType(Target, 0), ArrayLiteral); 10973 else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) 10974 ObjC().checkDictionaryLiteral(QualType(Target, 0), DictionaryLiteral); 10975 10976 // Strip vector types. 10977 if (isa<VectorType>(Source)) { 10978 if (Target->isSveVLSBuiltinType() && 10979 (Context.areCompatibleSveTypes(QualType(Target, 0), 10980 QualType(Source, 0)) || 10981 Context.areLaxCompatibleSveTypes(QualType(Target, 0), 10982 QualType(Source, 0)))) 10983 return; 10984 10985 if (Target->isRVVVLSBuiltinType() && 10986 (Context.areCompatibleRVVTypes(QualType(Target, 0), 10987 QualType(Source, 0)) || 10988 Context.areLaxCompatibleRVVTypes(QualType(Target, 0), 10989 QualType(Source, 0)))) 10990 return; 10991 10992 if (!isa<VectorType>(Target)) { 10993 if (SourceMgr.isInSystemMacro(CC)) 10994 return; 10995 return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_vector_scalar); 10996 } else if (getLangOpts().HLSL && 10997 Target->castAs<VectorType>()->getNumElements() < 10998 Source->castAs<VectorType>()->getNumElements()) { 10999 // Diagnose vector truncation but don't return. We may also want to 11000 // diagnose an element conversion. 11001 DiagnoseImpCast(*this, E, T, CC, 11002 diag::warn_hlsl_impcast_vector_truncation); 11003 } 11004 11005 // If the vector cast is cast between two vectors of the same size, it is 11006 // a bitcast, not a conversion, except under HLSL where it is a conversion. 11007 if (!getLangOpts().HLSL && 11008 Context.getTypeSize(Source) == Context.getTypeSize(Target)) 11009 return; 11010 11011 Source = cast<VectorType>(Source)->getElementType().getTypePtr(); 11012 Target = cast<VectorType>(Target)->getElementType().getTypePtr(); 11013 } 11014 if (auto VecTy = dyn_cast<VectorType>(Target)) 11015 Target = VecTy->getElementType().getTypePtr(); 11016 11017 // Strip complex types. 11018 if (isa<ComplexType>(Source)) { 11019 if (!isa<ComplexType>(Target)) { 11020 if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) 11021 return; 11022 11023 return DiagnoseImpCast(*this, E, T, CC, 11024 getLangOpts().CPlusPlus 11025 ? diag::err_impcast_complex_scalar 11026 : diag::warn_impcast_complex_scalar); 11027 } 11028 11029 Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); 11030 Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); 11031 } 11032 11033 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); 11034 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); 11035 11036 // Strip SVE vector types 11037 if (SourceBT && SourceBT->isSveVLSBuiltinType()) { 11038 // Need the original target type for vector type checks 11039 const Type *OriginalTarget = Context.getCanonicalType(T).getTypePtr(); 11040 // Handle conversion from scalable to fixed when msve-vector-bits is 11041 // specified 11042 if (Context.areCompatibleSveTypes(QualType(OriginalTarget, 0), 11043 QualType(Source, 0)) || 11044 Context.areLaxCompatibleSveTypes(QualType(OriginalTarget, 0), 11045 QualType(Source, 0))) 11046 return; 11047 11048 // If the vector cast is cast between two vectors of the same size, it is 11049 // a bitcast, not a conversion. 11050 if (Context.getTypeSize(Source) == Context.getTypeSize(Target)) 11051 return; 11052 11053 Source = SourceBT->getSveEltType(Context).getTypePtr(); 11054 } 11055 11056 if (TargetBT && TargetBT->isSveVLSBuiltinType()) 11057 Target = TargetBT->getSveEltType(Context).getTypePtr(); 11058 11059 // If the source is floating point... 11060 if (SourceBT && SourceBT->isFloatingPoint()) { 11061 // ...and the target is floating point... 11062 if (TargetBT && TargetBT->isFloatingPoint()) { 11063 // ...then warn if we're dropping FP rank. 11064 11065 int Order = getASTContext().getFloatingTypeSemanticOrder( 11066 QualType(SourceBT, 0), QualType(TargetBT, 0)); 11067 if (Order > 0) { 11068 // Don't warn about float constants that are precisely 11069 // representable in the target type. 11070 Expr::EvalResult result; 11071 if (E->EvaluateAsRValue(result, Context)) { 11072 // Value might be a float, a float vector, or a float complex. 11073 if (IsSameFloatAfterCast( 11074 result.Val, 11075 Context.getFloatTypeSemantics(QualType(TargetBT, 0)), 11076 Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) 11077 return; 11078 } 11079 11080 if (SourceMgr.isInSystemMacro(CC)) 11081 return; 11082 11083 DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_float_precision); 11084 } 11085 // ... or possibly if we're increasing rank, too 11086 else if (Order < 0) { 11087 if (SourceMgr.isInSystemMacro(CC)) 11088 return; 11089 11090 DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_double_promotion); 11091 } 11092 return; 11093 } 11094 11095 // If the target is integral, always warn. 11096 if (TargetBT && TargetBT->isInteger()) { 11097 if (SourceMgr.isInSystemMacro(CC)) 11098 return; 11099 11100 DiagnoseFloatingImpCast(*this, E, T, CC); 11101 } 11102 11103 // Detect the case where a call result is converted from floating-point to 11104 // to bool, and the final argument to the call is converted from bool, to 11105 // discover this typo: 11106 // 11107 // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;" 11108 // 11109 // FIXME: This is an incredibly special case; is there some more general 11110 // way to detect this class of misplaced-parentheses bug? 11111 if (Target->isBooleanType() && isa<CallExpr>(E)) { 11112 // Check last argument of function call to see if it is an 11113 // implicit cast from a type matching the type the result 11114 // is being cast to. 11115 CallExpr *CEx = cast<CallExpr>(E); 11116 if (unsigned NumArgs = CEx->getNumArgs()) { 11117 Expr *LastA = CEx->getArg(NumArgs - 1); 11118 Expr *InnerE = LastA->IgnoreParenImpCasts(); 11119 if (isa<ImplicitCastExpr>(LastA) && 11120 InnerE->getType()->isBooleanType()) { 11121 // Warn on this floating-point to bool conversion 11122 DiagnoseImpCast(*this, E, T, CC, 11123 diag::warn_impcast_floating_point_to_bool); 11124 } 11125 } 11126 } 11127 return; 11128 } 11129 11130 // Valid casts involving fixed point types should be accounted for here. 11131 if (Source->isFixedPointType()) { 11132 if (Target->isUnsaturatedFixedPointType()) { 11133 Expr::EvalResult Result; 11134 if (E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects, 11135 isConstantEvaluatedContext())) { 11136 llvm::APFixedPoint Value = Result.Val.getFixedPoint(); 11137 llvm::APFixedPoint MaxVal = Context.getFixedPointMax(T); 11138 llvm::APFixedPoint MinVal = Context.getFixedPointMin(T); 11139 if (Value > MaxVal || Value < MinVal) { 11140 DiagRuntimeBehavior(E->getExprLoc(), E, 11141 PDiag(diag::warn_impcast_fixed_point_range) 11142 << Value.toString() << T 11143 << E->getSourceRange() 11144 << clang::SourceRange(CC)); 11145 return; 11146 } 11147 } 11148 } else if (Target->isIntegerType()) { 11149 Expr::EvalResult Result; 11150 if (!isConstantEvaluatedContext() && 11151 E->EvaluateAsFixedPoint(Result, Context, Expr::SE_AllowSideEffects)) { 11152 llvm::APFixedPoint FXResult = Result.Val.getFixedPoint(); 11153 11154 bool Overflowed; 11155 llvm::APSInt IntResult = FXResult.convertToInt( 11156 Context.getIntWidth(T), Target->isSignedIntegerOrEnumerationType(), 11157 &Overflowed); 11158 11159 if (Overflowed) { 11160 DiagRuntimeBehavior(E->getExprLoc(), E, 11161 PDiag(diag::warn_impcast_fixed_point_range) 11162 << FXResult.toString() << T 11163 << E->getSourceRange() 11164 << clang::SourceRange(CC)); 11165 return; 11166 } 11167 } 11168 } 11169 } else if (Target->isUnsaturatedFixedPointType()) { 11170 if (Source->isIntegerType()) { 11171 Expr::EvalResult Result; 11172 if (!isConstantEvaluatedContext() && 11173 E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) { 11174 llvm::APSInt Value = Result.Val.getInt(); 11175 11176 bool Overflowed; 11177 llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue( 11178 Value, Context.getFixedPointSemantics(T), &Overflowed); 11179 11180 if (Overflowed) { 11181 DiagRuntimeBehavior(E->getExprLoc(), E, 11182 PDiag(diag::warn_impcast_fixed_point_range) 11183 << toString(Value, /*Radix=*/10) << T 11184 << E->getSourceRange() 11185 << clang::SourceRange(CC)); 11186 return; 11187 } 11188 } 11189 } 11190 } 11191 11192 // If we are casting an integer type to a floating point type without 11193 // initialization-list syntax, we might lose accuracy if the floating 11194 // point type has a narrower significand than the integer type. 11195 if (SourceBT && TargetBT && SourceBT->isIntegerType() && 11196 TargetBT->isFloatingType() && !IsListInit) { 11197 // Determine the number of precision bits in the source integer type. 11198 std::optional<IntRange> SourceRange = 11199 TryGetExprRange(Context, E, isConstantEvaluatedContext(), 11200 /*Approximate=*/true); 11201 if (!SourceRange) 11202 return; 11203 unsigned int SourcePrecision = SourceRange->Width; 11204 11205 // Determine the number of precision bits in the 11206 // target floating point type. 11207 unsigned int TargetPrecision = llvm::APFloatBase::semanticsPrecision( 11208 Context.getFloatTypeSemantics(QualType(TargetBT, 0))); 11209 11210 if (SourcePrecision > 0 && TargetPrecision > 0 && 11211 SourcePrecision > TargetPrecision) { 11212 11213 if (std::optional<llvm::APSInt> SourceInt = 11214 E->getIntegerConstantExpr(Context)) { 11215 // If the source integer is a constant, convert it to the target 11216 // floating point type. Issue a warning if the value changes 11217 // during the whole conversion. 11218 llvm::APFloat TargetFloatValue( 11219 Context.getFloatTypeSemantics(QualType(TargetBT, 0))); 11220 llvm::APFloat::opStatus ConversionStatus = 11221 TargetFloatValue.convertFromAPInt( 11222 *SourceInt, SourceBT->isSignedInteger(), 11223 llvm::APFloat::rmNearestTiesToEven); 11224 11225 if (ConversionStatus != llvm::APFloat::opOK) { 11226 SmallString<32> PrettySourceValue; 11227 SourceInt->toString(PrettySourceValue, 10); 11228 SmallString<32> PrettyTargetValue; 11229 TargetFloatValue.toString(PrettyTargetValue, TargetPrecision); 11230 11231 DiagRuntimeBehavior( 11232 E->getExprLoc(), E, 11233 PDiag(diag::warn_impcast_integer_float_precision_constant) 11234 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11235 << E->getSourceRange() << clang::SourceRange(CC)); 11236 } 11237 } else { 11238 // Otherwise, the implicit conversion may lose precision. 11239 DiagnoseImpCast(*this, E, T, CC, 11240 diag::warn_impcast_integer_float_precision); 11241 } 11242 } 11243 } 11244 11245 DiagnoseNullConversion(*this, E, T, CC); 11246 11247 DiscardMisalignedMemberAddress(Target, E); 11248 11249 if (Target->isBooleanType()) 11250 DiagnoseIntInBoolContext(*this, E); 11251 11252 if (!Source->isIntegerType() || !Target->isIntegerType()) 11253 return; 11254 11255 // TODO: remove this early return once the false positives for constant->bool 11256 // in templates, macros, etc, are reduced or removed. 11257 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) 11258 return; 11259 11260 if (ObjC().isSignedCharBool(T) && !Source->isCharType() && 11261 !E->isKnownToHaveBooleanValue(/*Semantic=*/false)) { 11262 return ObjC().adornBoolConversionDiagWithTernaryFixit( 11263 E, Diag(CC, diag::warn_impcast_int_to_objc_signed_char_bool) 11264 << E->getType()); 11265 } 11266 std::optional<IntRange> LikelySourceRange = TryGetExprRange( 11267 Context, E, isConstantEvaluatedContext(), /*Approximate=*/true); 11268 if (!LikelySourceRange) 11269 return; 11270 11271 IntRange SourceTypeRange = 11272 IntRange::forTargetOfCanonicalType(Context, Source); 11273 IntRange TargetRange = IntRange::forTargetOfCanonicalType(Context, Target); 11274 11275 if (LikelySourceRange->Width > TargetRange.Width) { 11276 // If the source is a constant, use a default-on diagnostic. 11277 // TODO: this should happen for bitfield stores, too. 11278 Expr::EvalResult Result; 11279 if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects, 11280 isConstantEvaluatedContext())) { 11281 llvm::APSInt Value(32); 11282 Value = Result.Val.getInt(); 11283 11284 if (SourceMgr.isInSystemMacro(CC)) 11285 return; 11286 11287 std::string PrettySourceValue = toString(Value, 10); 11288 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11289 11290 DiagRuntimeBehavior(E->getExprLoc(), E, 11291 PDiag(diag::warn_impcast_integer_precision_constant) 11292 << PrettySourceValue << PrettyTargetValue 11293 << E->getType() << T << E->getSourceRange() 11294 << SourceRange(CC)); 11295 return; 11296 } 11297 11298 // People want to build with -Wshorten-64-to-32 and not -Wconversion. 11299 if (SourceMgr.isInSystemMacro(CC)) 11300 return; 11301 11302 if (TargetRange.Width == 32 && Context.getIntWidth(E->getType()) == 64) 11303 return DiagnoseImpCast(*this, E, T, CC, diag::warn_impcast_integer_64_32, 11304 /* pruneControlFlow */ true); 11305 return DiagnoseImpCast(*this, E, T, CC, 11306 diag::warn_impcast_integer_precision); 11307 } 11308 11309 if (TargetRange.Width > SourceTypeRange.Width) { 11310 if (auto *UO = dyn_cast<UnaryOperator>(E)) 11311 if (UO->getOpcode() == UO_Minus) 11312 if (Source->isUnsignedIntegerType()) { 11313 if (Target->isUnsignedIntegerType()) 11314 return DiagnoseImpCast(*this, E, T, CC, 11315 diag::warn_impcast_high_order_zero_bits); 11316 if (Target->isSignedIntegerType()) 11317 return DiagnoseImpCast(*this, E, T, CC, 11318 diag::warn_impcast_nonnegative_result); 11319 } 11320 } 11321 11322 if (TargetRange.Width == LikelySourceRange->Width && 11323 !TargetRange.NonNegative && LikelySourceRange->NonNegative && 11324 Source->isSignedIntegerType()) { 11325 // Warn when doing a signed to signed conversion, warn if the positive 11326 // source value is exactly the width of the target type, which will 11327 // cause a negative value to be stored. 11328 11329 Expr::EvalResult Result; 11330 if (E->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects) && 11331 !SourceMgr.isInSystemMacro(CC)) { 11332 llvm::APSInt Value = Result.Val.getInt(); 11333 if (isSameWidthConstantConversion(*this, E, T, CC)) { 11334 std::string PrettySourceValue = toString(Value, 10); 11335 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 11336 11337 Diag(E->getExprLoc(), 11338 PDiag(diag::warn_impcast_integer_precision_constant) 11339 << PrettySourceValue << PrettyTargetValue << E->getType() << T 11340 << E->getSourceRange() << SourceRange(CC)); 11341 return; 11342 } 11343 } 11344 11345 // Fall through for non-constants to give a sign conversion warning. 11346 } 11347 11348 if ((!isa<EnumType>(Target) || !isa<EnumType>(Source)) && 11349 ((TargetRange.NonNegative && !LikelySourceRange->NonNegative) || 11350 (!TargetRange.NonNegative && LikelySourceRange->NonNegative && 11351 LikelySourceRange->Width == TargetRange.Width))) { 11352 if (SourceMgr.isInSystemMacro(CC)) 11353 return; 11354 11355 if (SourceBT && SourceBT->isInteger() && TargetBT && 11356 TargetBT->isInteger() && 11357 Source->isSignedIntegerType() == Target->isSignedIntegerType()) { 11358 return; 11359 } 11360 11361 unsigned DiagID = diag::warn_impcast_integer_sign; 11362 11363 // Traditionally, gcc has warned about this under -Wsign-compare. 11364 // We also want to warn about it in -Wconversion. 11365 // So if -Wconversion is off, use a completely identical diagnostic 11366 // in the sign-compare group. 11367 // The conditional-checking code will 11368 if (ICContext) { 11369 DiagID = diag::warn_impcast_integer_sign_conditional; 11370 *ICContext = true; 11371 } 11372 11373 return DiagnoseImpCast(*this, E, T, CC, DiagID); 11374 } 11375 11376 // Diagnose conversions between different enumeration types. 11377 // In C, we pretend that the type of an EnumConstantDecl is its enumeration 11378 // type, to give us better diagnostics. 11379 QualType SourceType = E->getEnumCoercedType(Context); 11380 Source = Context.getCanonicalType(SourceType).getTypePtr(); 11381 11382 if (const EnumType *SourceEnum = Source->getAs<EnumType>()) 11383 if (const EnumType *TargetEnum = Target->getAs<EnumType>()) 11384 if (SourceEnum->getDecl()->hasNameForLinkage() && 11385 TargetEnum->getDecl()->hasNameForLinkage() && 11386 SourceEnum != TargetEnum) { 11387 if (SourceMgr.isInSystemMacro(CC)) 11388 return; 11389 11390 return DiagnoseImpCast(*this, E, SourceType, T, CC, 11391 diag::warn_impcast_different_enum_types); 11392 } 11393 } 11394 11395 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, 11396 SourceLocation CC, QualType T); 11397 11398 static void CheckConditionalOperand(Sema &S, Expr *E, QualType T, 11399 SourceLocation CC, bool &ICContext) { 11400 E = E->IgnoreParenImpCasts(); 11401 // Diagnose incomplete type for second or third operand in C. 11402 if (!S.getLangOpts().CPlusPlus && E->getType()->isRecordType()) 11403 S.RequireCompleteExprType(E, diag::err_incomplete_type); 11404 11405 if (auto *CO = dyn_cast<AbstractConditionalOperator>(E)) 11406 return CheckConditionalOperator(S, CO, CC, T); 11407 11408 AnalyzeImplicitConversions(S, E, CC); 11409 if (E->getType() != T) 11410 return S.CheckImplicitConversion(E, T, CC, &ICContext); 11411 } 11412 11413 static void CheckConditionalOperator(Sema &S, AbstractConditionalOperator *E, 11414 SourceLocation CC, QualType T) { 11415 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); 11416 11417 Expr *TrueExpr = E->getTrueExpr(); 11418 if (auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) 11419 TrueExpr = BCO->getCommon(); 11420 11421 bool Suspicious = false; 11422 CheckConditionalOperand(S, TrueExpr, T, CC, Suspicious); 11423 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); 11424 11425 if (T->isBooleanType()) 11426 DiagnoseIntInBoolContext(S, E); 11427 11428 // If -Wconversion would have warned about either of the candidates 11429 // for a signedness conversion to the context type... 11430 if (!Suspicious) return; 11431 11432 // ...but it's currently ignored... 11433 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) 11434 return; 11435 11436 // ...then check whether it would have warned about either of the 11437 // candidates for a signedness conversion to the condition type. 11438 if (E->getType() == T) return; 11439 11440 Suspicious = false; 11441 S.CheckImplicitConversion(TrueExpr->IgnoreParenImpCasts(), E->getType(), CC, 11442 &Suspicious); 11443 if (!Suspicious) 11444 S.CheckImplicitConversion(E->getFalseExpr()->IgnoreParenImpCasts(), 11445 E->getType(), CC, &Suspicious); 11446 } 11447 11448 /// Check conversion of given expression to boolean. 11449 /// Input argument E is a logical expression. 11450 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { 11451 // Run the bool-like conversion checks only for C since there bools are 11452 // still not used as the return type from "boolean" operators or as the input 11453 // type for conditional operators. 11454 if (S.getLangOpts().CPlusPlus) 11455 return; 11456 if (E->IgnoreParenImpCasts()->getType()->isAtomicType()) 11457 return; 11458 S.CheckImplicitConversion(E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); 11459 } 11460 11461 namespace { 11462 struct AnalyzeImplicitConversionsWorkItem { 11463 Expr *E; 11464 SourceLocation CC; 11465 bool IsListInit; 11466 }; 11467 } 11468 11469 /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions 11470 /// that should be visited are added to WorkList. 11471 static void AnalyzeImplicitConversions( 11472 Sema &S, AnalyzeImplicitConversionsWorkItem Item, 11473 llvm::SmallVectorImpl<AnalyzeImplicitConversionsWorkItem> &WorkList) { 11474 Expr *OrigE = Item.E; 11475 SourceLocation CC = Item.CC; 11476 11477 QualType T = OrigE->getType(); 11478 Expr *E = OrigE->IgnoreParenImpCasts(); 11479 11480 // Propagate whether we are in a C++ list initialization expression. 11481 // If so, we do not issue warnings for implicit int-float conversion 11482 // precision loss, because C++11 narrowing already handles it. 11483 bool IsListInit = Item.IsListInit || 11484 (isa<InitListExpr>(OrigE) && S.getLangOpts().CPlusPlus); 11485 11486 if (E->isTypeDependent() || E->isValueDependent()) 11487 return; 11488 11489 Expr *SourceExpr = E; 11490 // Examine, but don't traverse into the source expression of an 11491 // OpaqueValueExpr, since it may have multiple parents and we don't want to 11492 // emit duplicate diagnostics. Its fine to examine the form or attempt to 11493 // evaluate it in the context of checking the specific conversion to T though. 11494 if (auto *OVE = dyn_cast<OpaqueValueExpr>(E)) 11495 if (auto *Src = OVE->getSourceExpr()) 11496 SourceExpr = Src; 11497 11498 if (const auto *UO = dyn_cast<UnaryOperator>(SourceExpr)) 11499 if (UO->getOpcode() == UO_Not && 11500 UO->getSubExpr()->isKnownToHaveBooleanValue()) 11501 S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool) 11502 << OrigE->getSourceRange() << T->isBooleanType() 11503 << FixItHint::CreateReplacement(UO->getBeginLoc(), "!"); 11504 11505 if (const auto *BO = dyn_cast<BinaryOperator>(SourceExpr)) 11506 if ((BO->getOpcode() == BO_And || BO->getOpcode() == BO_Or) && 11507 BO->getLHS()->isKnownToHaveBooleanValue() && 11508 BO->getRHS()->isKnownToHaveBooleanValue() && 11509 BO->getLHS()->HasSideEffects(S.Context) && 11510 BO->getRHS()->HasSideEffects(S.Context)) { 11511 SourceManager &SM = S.getSourceManager(); 11512 const LangOptions &LO = S.getLangOpts(); 11513 SourceLocation BLoc = BO->getOperatorLoc(); 11514 SourceLocation ELoc = Lexer::getLocForEndOfToken(BLoc, 0, SM, LO); 11515 StringRef SR = clang::Lexer::getSourceText( 11516 clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO); 11517 // To reduce false positives, only issue the diagnostic if the operator 11518 // is explicitly spelled as a punctuator. This suppresses the diagnostic 11519 // when using 'bitand' or 'bitor' either as keywords in C++ or as macros 11520 // in C, along with other macro spellings the user might invent. 11521 if (SR.str() == "&" || SR.str() == "|") { 11522 11523 S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical) 11524 << (BO->getOpcode() == BO_And ? "&" : "|") 11525 << OrigE->getSourceRange() 11526 << FixItHint::CreateReplacement( 11527 BO->getOperatorLoc(), 11528 (BO->getOpcode() == BO_And ? "&&" : "||")); 11529 S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int); 11530 } 11531 } 11532 11533 // For conditional operators, we analyze the arguments as if they 11534 // were being fed directly into the output. 11535 if (auto *CO = dyn_cast<AbstractConditionalOperator>(SourceExpr)) { 11536 CheckConditionalOperator(S, CO, CC, T); 11537 return; 11538 } 11539 11540 // Check implicit argument conversions for function calls. 11541 if (CallExpr *Call = dyn_cast<CallExpr>(SourceExpr)) 11542 CheckImplicitArgumentConversions(S, Call, CC); 11543 11544 // Go ahead and check any implicit conversions we might have skipped. 11545 // The non-canonical typecheck is just an optimization; 11546 // CheckImplicitConversion will filter out dead implicit conversions. 11547 if (SourceExpr->getType() != T) 11548 S.CheckImplicitConversion(SourceExpr, T, CC, nullptr, IsListInit); 11549 11550 // Now continue drilling into this expression. 11551 11552 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 11553 // The bound subexpressions in a PseudoObjectExpr are not reachable 11554 // as transitive children. 11555 // FIXME: Use a more uniform representation for this. 11556 for (auto *SE : POE->semantics()) 11557 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE)) 11558 WorkList.push_back({OVE->getSourceExpr(), CC, IsListInit}); 11559 } 11560 11561 // Skip past explicit casts. 11562 if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) { 11563 E = CE->getSubExpr()->IgnoreParenImpCasts(); 11564 if (!CE->getType()->isVoidType() && E->getType()->isAtomicType()) 11565 S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst); 11566 WorkList.push_back({E, CC, IsListInit}); 11567 return; 11568 } 11569 11570 if (auto *OutArgE = dyn_cast<HLSLOutArgExpr>(E)) { 11571 WorkList.push_back({OutArgE->getArgLValue(), CC, IsListInit}); 11572 // The base expression is only used to initialize the parameter for 11573 // arguments to `inout` parameters, so we only traverse down the base 11574 // expression for `inout` cases. 11575 if (OutArgE->isInOut()) 11576 WorkList.push_back( 11577 {OutArgE->getCastedTemporary()->getSourceExpr(), CC, IsListInit}); 11578 WorkList.push_back({OutArgE->getWritebackCast(), CC, IsListInit}); 11579 return; 11580 } 11581 11582 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 11583 // Do a somewhat different check with comparison operators. 11584 if (BO->isComparisonOp()) 11585 return AnalyzeComparison(S, BO); 11586 11587 // And with simple assignments. 11588 if (BO->getOpcode() == BO_Assign) 11589 return AnalyzeAssignment(S, BO); 11590 // And with compound assignments. 11591 if (BO->isAssignmentOp()) 11592 return AnalyzeCompoundAssignment(S, BO); 11593 } 11594 11595 // These break the otherwise-useful invariant below. Fortunately, 11596 // we don't really need to recurse into them, because any internal 11597 // expressions should have been analyzed already when they were 11598 // built into statements. 11599 if (isa<StmtExpr>(E)) return; 11600 11601 // Don't descend into unevaluated contexts. 11602 if (isa<UnaryExprOrTypeTraitExpr>(E)) return; 11603 11604 // Now just recurse over the expression's children. 11605 CC = E->getExprLoc(); 11606 BinaryOperator *BO = dyn_cast<BinaryOperator>(E); 11607 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; 11608 for (Stmt *SubStmt : E->children()) { 11609 Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt); 11610 if (!ChildExpr) 11611 continue; 11612 11613 if (auto *CSE = dyn_cast<CoroutineSuspendExpr>(E)) 11614 if (ChildExpr == CSE->getOperand()) 11615 // Do not recurse over a CoroutineSuspendExpr's operand. 11616 // The operand is also a subexpression of getCommonExpr(), and 11617 // recursing into it directly would produce duplicate diagnostics. 11618 continue; 11619 11620 if (IsLogicalAndOperator && 11621 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) 11622 // Ignore checking string literals that are in logical and operators. 11623 // This is a common pattern for asserts. 11624 continue; 11625 WorkList.push_back({ChildExpr, CC, IsListInit}); 11626 } 11627 11628 if (BO && BO->isLogicalOp()) { 11629 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); 11630 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11631 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11632 11633 SubExpr = BO->getRHS()->IgnoreParenImpCasts(); 11634 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 11635 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 11636 } 11637 11638 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) { 11639 if (U->getOpcode() == UO_LNot) { 11640 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); 11641 } else if (U->getOpcode() != UO_AddrOf) { 11642 if (U->getSubExpr()->getType()->isAtomicType()) 11643 S.Diag(U->getSubExpr()->getBeginLoc(), 11644 diag::warn_atomic_implicit_seq_cst); 11645 } 11646 } 11647 } 11648 11649 /// AnalyzeImplicitConversions - Find and report any interesting 11650 /// implicit conversions in the given expression. There are a couple 11651 /// of competing diagnostics here, -Wconversion and -Wsign-compare. 11652 static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC, 11653 bool IsListInit/*= false*/) { 11654 llvm::SmallVector<AnalyzeImplicitConversionsWorkItem, 16> WorkList; 11655 WorkList.push_back({OrigE, CC, IsListInit}); 11656 while (!WorkList.empty()) 11657 AnalyzeImplicitConversions(S, WorkList.pop_back_val(), WorkList); 11658 } 11659 11660 // Helper function for Sema::DiagnoseAlwaysNonNullPointer. 11661 // Returns true when emitting a warning about taking the address of a reference. 11662 static bool CheckForReference(Sema &SemaRef, const Expr *E, 11663 const PartialDiagnostic &PD) { 11664 E = E->IgnoreParenImpCasts(); 11665 11666 const FunctionDecl *FD = nullptr; 11667 11668 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 11669 if (!DRE->getDecl()->getType()->isReferenceType()) 11670 return false; 11671 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11672 if (!M->getMemberDecl()->getType()->isReferenceType()) 11673 return false; 11674 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { 11675 if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType()) 11676 return false; 11677 FD = Call->getDirectCallee(); 11678 } else { 11679 return false; 11680 } 11681 11682 SemaRef.Diag(E->getExprLoc(), PD); 11683 11684 // If possible, point to location of function. 11685 if (FD) { 11686 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; 11687 } 11688 11689 return true; 11690 } 11691 11692 // Returns true if the SourceLocation is expanded from any macro body. 11693 // Returns false if the SourceLocation is invalid, is from not in a macro 11694 // expansion, or is from expanded from a top-level macro argument. 11695 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { 11696 if (Loc.isInvalid()) 11697 return false; 11698 11699 while (Loc.isMacroID()) { 11700 if (SM.isMacroBodyExpansion(Loc)) 11701 return true; 11702 Loc = SM.getImmediateMacroCallerLoc(Loc); 11703 } 11704 11705 return false; 11706 } 11707 11708 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, 11709 Expr::NullPointerConstantKind NullKind, 11710 bool IsEqual, SourceRange Range) { 11711 if (!E) 11712 return; 11713 11714 // Don't warn inside macros. 11715 if (E->getExprLoc().isMacroID()) { 11716 const SourceManager &SM = getSourceManager(); 11717 if (IsInAnyMacroBody(SM, E->getExprLoc()) || 11718 IsInAnyMacroBody(SM, Range.getBegin())) 11719 return; 11720 } 11721 E = E->IgnoreImpCasts(); 11722 11723 const bool IsCompare = NullKind != Expr::NPCK_NotNull; 11724 11725 if (isa<CXXThisExpr>(E)) { 11726 unsigned DiagID = IsCompare ? diag::warn_this_null_compare 11727 : diag::warn_this_bool_conversion; 11728 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; 11729 return; 11730 } 11731 11732 bool IsAddressOf = false; 11733 11734 if (auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) { 11735 if (UO->getOpcode() != UO_AddrOf) 11736 return; 11737 IsAddressOf = true; 11738 E = UO->getSubExpr(); 11739 } 11740 11741 if (IsAddressOf) { 11742 unsigned DiagID = IsCompare 11743 ? diag::warn_address_of_reference_null_compare 11744 : diag::warn_address_of_reference_bool_conversion; 11745 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range 11746 << IsEqual; 11747 if (CheckForReference(*this, E, PD)) { 11748 return; 11749 } 11750 } 11751 11752 auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) { 11753 bool IsParam = isa<NonNullAttr>(NonnullAttr); 11754 std::string Str; 11755 llvm::raw_string_ostream S(Str); 11756 E->printPretty(S, nullptr, getPrintingPolicy()); 11757 unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare 11758 : diag::warn_cast_nonnull_to_bool; 11759 Diag(E->getExprLoc(), DiagID) << IsParam << S.str() 11760 << E->getSourceRange() << Range << IsEqual; 11761 Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam; 11762 }; 11763 11764 // If we have a CallExpr that is tagged with returns_nonnull, we can complain. 11765 if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) { 11766 if (auto *Callee = Call->getDirectCallee()) { 11767 if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) { 11768 ComplainAboutNonnullParamOrCall(A); 11769 return; 11770 } 11771 } 11772 } 11773 11774 // Complain if we are converting a lambda expression to a boolean value 11775 // outside of instantiation. 11776 if (!inTemplateInstantiation()) { 11777 if (const auto *MCallExpr = dyn_cast<CXXMemberCallExpr>(E)) { 11778 if (const auto *MRecordDecl = MCallExpr->getRecordDecl(); 11779 MRecordDecl && MRecordDecl->isLambda()) { 11780 Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool) 11781 << /*LambdaPointerConversionOperatorType=*/3 11782 << MRecordDecl->getSourceRange() << Range << IsEqual; 11783 return; 11784 } 11785 } 11786 } 11787 11788 // Expect to find a single Decl. Skip anything more complicated. 11789 ValueDecl *D = nullptr; 11790 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { 11791 D = R->getDecl(); 11792 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { 11793 D = M->getMemberDecl(); 11794 } 11795 11796 // Weak Decls can be null. 11797 if (!D || D->isWeak()) 11798 return; 11799 11800 // Check for parameter decl with nonnull attribute 11801 if (const auto* PV = dyn_cast<ParmVarDecl>(D)) { 11802 if (getCurFunction() && 11803 !getCurFunction()->ModifiedNonNullParams.count(PV)) { 11804 if (const Attr *A = PV->getAttr<NonNullAttr>()) { 11805 ComplainAboutNonnullParamOrCall(A); 11806 return; 11807 } 11808 11809 if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { 11810 // Skip function template not specialized yet. 11811 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) 11812 return; 11813 auto ParamIter = llvm::find(FD->parameters(), PV); 11814 assert(ParamIter != FD->param_end()); 11815 unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); 11816 11817 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { 11818 if (!NonNull->args_size()) { 11819 ComplainAboutNonnullParamOrCall(NonNull); 11820 return; 11821 } 11822 11823 for (const ParamIdx &ArgNo : NonNull->args()) { 11824 if (ArgNo.getASTIndex() == ParamNo) { 11825 ComplainAboutNonnullParamOrCall(NonNull); 11826 return; 11827 } 11828 } 11829 } 11830 } 11831 } 11832 } 11833 11834 QualType T = D->getType(); 11835 const bool IsArray = T->isArrayType(); 11836 const bool IsFunction = T->isFunctionType(); 11837 11838 // Address of function is used to silence the function warning. 11839 if (IsAddressOf && IsFunction) { 11840 return; 11841 } 11842 11843 // Found nothing. 11844 if (!IsAddressOf && !IsFunction && !IsArray) 11845 return; 11846 11847 // Pretty print the expression for the diagnostic. 11848 std::string Str; 11849 llvm::raw_string_ostream S(Str); 11850 E->printPretty(S, nullptr, getPrintingPolicy()); 11851 11852 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare 11853 : diag::warn_impcast_pointer_to_bool; 11854 enum { 11855 AddressOf, 11856 FunctionPointer, 11857 ArrayPointer 11858 } DiagType; 11859 if (IsAddressOf) 11860 DiagType = AddressOf; 11861 else if (IsFunction) 11862 DiagType = FunctionPointer; 11863 else if (IsArray) 11864 DiagType = ArrayPointer; 11865 else 11866 llvm_unreachable("Could not determine diagnostic."); 11867 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() 11868 << Range << IsEqual; 11869 11870 if (!IsFunction) 11871 return; 11872 11873 // Suggest '&' to silence the function warning. 11874 Diag(E->getExprLoc(), diag::note_function_warning_silence) 11875 << FixItHint::CreateInsertion(E->getBeginLoc(), "&"); 11876 11877 // Check to see if '()' fixit should be emitted. 11878 QualType ReturnType; 11879 UnresolvedSet<4> NonTemplateOverloads; 11880 tryExprAsCall(*E, ReturnType, NonTemplateOverloads); 11881 if (ReturnType.isNull()) 11882 return; 11883 11884 if (IsCompare) { 11885 // There are two cases here. If there is null constant, the only suggest 11886 // for a pointer return type. If the null is 0, then suggest if the return 11887 // type is a pointer or an integer type. 11888 if (!ReturnType->isPointerType()) { 11889 if (NullKind == Expr::NPCK_ZeroExpression || 11890 NullKind == Expr::NPCK_ZeroLiteral) { 11891 if (!ReturnType->isIntegerType()) 11892 return; 11893 } else { 11894 return; 11895 } 11896 } 11897 } else { // !IsCompare 11898 // For function to bool, only suggest if the function pointer has bool 11899 // return type. 11900 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) 11901 return; 11902 } 11903 Diag(E->getExprLoc(), diag::note_function_to_function_call) 11904 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getEndLoc()), "()"); 11905 } 11906 11907 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { 11908 // Don't diagnose in unevaluated contexts. 11909 if (isUnevaluatedContext()) 11910 return; 11911 11912 // Don't diagnose for value- or type-dependent expressions. 11913 if (E->isTypeDependent() || E->isValueDependent()) 11914 return; 11915 11916 // Check for array bounds violations in cases where the check isn't triggered 11917 // elsewhere for other Expr types (like BinaryOperators), e.g. when an 11918 // ArraySubscriptExpr is on the RHS of a variable initialization. 11919 CheckArrayAccess(E); 11920 11921 // This is not the right CC for (e.g.) a variable initialization. 11922 AnalyzeImplicitConversions(*this, E, CC); 11923 } 11924 11925 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { 11926 ::CheckBoolLikeConversion(*this, E, CC); 11927 } 11928 11929 void Sema::CheckForIntOverflow (const Expr *E) { 11930 // Use a work list to deal with nested struct initializers. 11931 SmallVector<const Expr *, 2> Exprs(1, E); 11932 11933 do { 11934 const Expr *OriginalE = Exprs.pop_back_val(); 11935 const Expr *E = OriginalE->IgnoreParenCasts(); 11936 11937 if (isa<BinaryOperator, UnaryOperator>(E)) { 11938 E->EvaluateForOverflow(Context); 11939 continue; 11940 } 11941 11942 if (const auto *InitList = dyn_cast<InitListExpr>(OriginalE)) 11943 Exprs.append(InitList->inits().begin(), InitList->inits().end()); 11944 else if (isa<ObjCBoxedExpr>(OriginalE)) 11945 E->EvaluateForOverflow(Context); 11946 else if (const auto *Call = dyn_cast<CallExpr>(E)) 11947 Exprs.append(Call->arg_begin(), Call->arg_end()); 11948 else if (const auto *Message = dyn_cast<ObjCMessageExpr>(E)) 11949 Exprs.append(Message->arg_begin(), Message->arg_end()); 11950 else if (const auto *Construct = dyn_cast<CXXConstructExpr>(E)) 11951 Exprs.append(Construct->arg_begin(), Construct->arg_end()); 11952 else if (const auto *Temporary = dyn_cast<CXXBindTemporaryExpr>(E)) 11953 Exprs.push_back(Temporary->getSubExpr()); 11954 else if (const auto *Array = dyn_cast<ArraySubscriptExpr>(E)) 11955 Exprs.push_back(Array->getIdx()); 11956 else if (const auto *Compound = dyn_cast<CompoundLiteralExpr>(E)) 11957 Exprs.push_back(Compound->getInitializer()); 11958 else if (const auto *New = dyn_cast<CXXNewExpr>(E); 11959 New && New->isArray()) { 11960 if (auto ArraySize = New->getArraySize()) 11961 Exprs.push_back(*ArraySize); 11962 } 11963 } while (!Exprs.empty()); 11964 } 11965 11966 namespace { 11967 11968 /// Visitor for expressions which looks for unsequenced operations on the 11969 /// same object. 11970 class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> { 11971 using Base = ConstEvaluatedExprVisitor<SequenceChecker>; 11972 11973 /// A tree of sequenced regions within an expression. Two regions are 11974 /// unsequenced if one is an ancestor or a descendent of the other. When we 11975 /// finish processing an expression with sequencing, such as a comma 11976 /// expression, we fold its tree nodes into its parent, since they are 11977 /// unsequenced with respect to nodes we will visit later. 11978 class SequenceTree { 11979 struct Value { 11980 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} 11981 unsigned Parent : 31; 11982 LLVM_PREFERRED_TYPE(bool) 11983 unsigned Merged : 1; 11984 }; 11985 SmallVector<Value, 8> Values; 11986 11987 public: 11988 /// A region within an expression which may be sequenced with respect 11989 /// to some other region. 11990 class Seq { 11991 friend class SequenceTree; 11992 11993 unsigned Index; 11994 11995 explicit Seq(unsigned N) : Index(N) {} 11996 11997 public: 11998 Seq() : Index(0) {} 11999 }; 12000 12001 SequenceTree() { Values.push_back(Value(0)); } 12002 Seq root() const { return Seq(0); } 12003 12004 /// Create a new sequence of operations, which is an unsequenced 12005 /// subset of \p Parent. This sequence of operations is sequenced with 12006 /// respect to other children of \p Parent. 12007 Seq allocate(Seq Parent) { 12008 Values.push_back(Value(Parent.Index)); 12009 return Seq(Values.size() - 1); 12010 } 12011 12012 /// Merge a sequence of operations into its parent. 12013 void merge(Seq S) { 12014 Values[S.Index].Merged = true; 12015 } 12016 12017 /// Determine whether two operations are unsequenced. This operation 12018 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old 12019 /// should have been merged into its parent as appropriate. 12020 bool isUnsequenced(Seq Cur, Seq Old) { 12021 unsigned C = representative(Cur.Index); 12022 unsigned Target = representative(Old.Index); 12023 while (C >= Target) { 12024 if (C == Target) 12025 return true; 12026 C = Values[C].Parent; 12027 } 12028 return false; 12029 } 12030 12031 private: 12032 /// Pick a representative for a sequence. 12033 unsigned representative(unsigned K) { 12034 if (Values[K].Merged) 12035 // Perform path compression as we go. 12036 return Values[K].Parent = representative(Values[K].Parent); 12037 return K; 12038 } 12039 }; 12040 12041 /// An object for which we can track unsequenced uses. 12042 using Object = const NamedDecl *; 12043 12044 /// Different flavors of object usage which we track. We only track the 12045 /// least-sequenced usage of each kind. 12046 enum UsageKind { 12047 /// A read of an object. Multiple unsequenced reads are OK. 12048 UK_Use, 12049 12050 /// A modification of an object which is sequenced before the value 12051 /// computation of the expression, such as ++n in C++. 12052 UK_ModAsValue, 12053 12054 /// A modification of an object which is not sequenced before the value 12055 /// computation of the expression, such as n++. 12056 UK_ModAsSideEffect, 12057 12058 UK_Count = UK_ModAsSideEffect + 1 12059 }; 12060 12061 /// Bundle together a sequencing region and the expression corresponding 12062 /// to a specific usage. One Usage is stored for each usage kind in UsageInfo. 12063 struct Usage { 12064 const Expr *UsageExpr = nullptr; 12065 SequenceTree::Seq Seq; 12066 12067 Usage() = default; 12068 }; 12069 12070 struct UsageInfo { 12071 Usage Uses[UK_Count]; 12072 12073 /// Have we issued a diagnostic for this object already? 12074 bool Diagnosed = false; 12075 12076 UsageInfo(); 12077 }; 12078 using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>; 12079 12080 Sema &SemaRef; 12081 12082 /// Sequenced regions within the expression. 12083 SequenceTree Tree; 12084 12085 /// Declaration modifications and references which we have seen. 12086 UsageInfoMap UsageMap; 12087 12088 /// The region we are currently within. 12089 SequenceTree::Seq Region; 12090 12091 /// Filled in with declarations which were modified as a side-effect 12092 /// (that is, post-increment operations). 12093 SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr; 12094 12095 /// Expressions to check later. We defer checking these to reduce 12096 /// stack usage. 12097 SmallVectorImpl<const Expr *> &WorkList; 12098 12099 /// RAII object wrapping the visitation of a sequenced subexpression of an 12100 /// expression. At the end of this process, the side-effects of the evaluation 12101 /// become sequenced with respect to the value computation of the result, so 12102 /// we downgrade any UK_ModAsSideEffect within the evaluation to 12103 /// UK_ModAsValue. 12104 struct SequencedSubexpression { 12105 SequencedSubexpression(SequenceChecker &Self) 12106 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { 12107 Self.ModAsSideEffect = &ModAsSideEffect; 12108 } 12109 12110 ~SequencedSubexpression() { 12111 for (const std::pair<Object, Usage> &M : llvm::reverse(ModAsSideEffect)) { 12112 // Add a new usage with usage kind UK_ModAsValue, and then restore 12113 // the previous usage with UK_ModAsSideEffect (thus clearing it if 12114 // the previous one was empty). 12115 UsageInfo &UI = Self.UsageMap[M.first]; 12116 auto &SideEffectUsage = UI.Uses[UK_ModAsSideEffect]; 12117 Self.addUsage(M.first, UI, SideEffectUsage.UsageExpr, UK_ModAsValue); 12118 SideEffectUsage = M.second; 12119 } 12120 Self.ModAsSideEffect = OldModAsSideEffect; 12121 } 12122 12123 SequenceChecker &Self; 12124 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; 12125 SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect; 12126 }; 12127 12128 /// RAII object wrapping the visitation of a subexpression which we might 12129 /// choose to evaluate as a constant. If any subexpression is evaluated and 12130 /// found to be non-constant, this allows us to suppress the evaluation of 12131 /// the outer expression. 12132 class EvaluationTracker { 12133 public: 12134 EvaluationTracker(SequenceChecker &Self) 12135 : Self(Self), Prev(Self.EvalTracker) { 12136 Self.EvalTracker = this; 12137 } 12138 12139 ~EvaluationTracker() { 12140 Self.EvalTracker = Prev; 12141 if (Prev) 12142 Prev->EvalOK &= EvalOK; 12143 } 12144 12145 bool evaluate(const Expr *E, bool &Result) { 12146 if (!EvalOK || E->isValueDependent()) 12147 return false; 12148 EvalOK = E->EvaluateAsBooleanCondition( 12149 Result, Self.SemaRef.Context, 12150 Self.SemaRef.isConstantEvaluatedContext()); 12151 return EvalOK; 12152 } 12153 12154 private: 12155 SequenceChecker &Self; 12156 EvaluationTracker *Prev; 12157 bool EvalOK = true; 12158 } *EvalTracker = nullptr; 12159 12160 /// Find the object which is produced by the specified expression, 12161 /// if any. 12162 Object getObject(const Expr *E, bool Mod) const { 12163 E = E->IgnoreParenCasts(); 12164 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 12165 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) 12166 return getObject(UO->getSubExpr(), Mod); 12167 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 12168 if (BO->getOpcode() == BO_Comma) 12169 return getObject(BO->getRHS(), Mod); 12170 if (Mod && BO->isAssignmentOp()) 12171 return getObject(BO->getLHS(), Mod); 12172 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 12173 // FIXME: Check for more interesting cases, like "x.n = ++x.n". 12174 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) 12175 return ME->getMemberDecl(); 12176 } else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 12177 // FIXME: If this is a reference, map through to its value. 12178 return DRE->getDecl(); 12179 return nullptr; 12180 } 12181 12182 /// Note that an object \p O was modified or used by an expression 12183 /// \p UsageExpr with usage kind \p UK. \p UI is the \p UsageInfo for 12184 /// the object \p O as obtained via the \p UsageMap. 12185 void addUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, UsageKind UK) { 12186 // Get the old usage for the given object and usage kind. 12187 Usage &U = UI.Uses[UK]; 12188 if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) { 12189 // If we have a modification as side effect and are in a sequenced 12190 // subexpression, save the old Usage so that we can restore it later 12191 // in SequencedSubexpression::~SequencedSubexpression. 12192 if (UK == UK_ModAsSideEffect && ModAsSideEffect) 12193 ModAsSideEffect->push_back(std::make_pair(O, U)); 12194 // Then record the new usage with the current sequencing region. 12195 U.UsageExpr = UsageExpr; 12196 U.Seq = Region; 12197 } 12198 } 12199 12200 /// Check whether a modification or use of an object \p O in an expression 12201 /// \p UsageExpr conflicts with a prior usage of kind \p OtherKind. \p UI is 12202 /// the \p UsageInfo for the object \p O as obtained via the \p UsageMap. 12203 /// \p IsModMod is true when we are checking for a mod-mod unsequenced 12204 /// usage and false we are checking for a mod-use unsequenced usage. 12205 void checkUsage(Object O, UsageInfo &UI, const Expr *UsageExpr, 12206 UsageKind OtherKind, bool IsModMod) { 12207 if (UI.Diagnosed) 12208 return; 12209 12210 const Usage &U = UI.Uses[OtherKind]; 12211 if (!U.UsageExpr || !Tree.isUnsequenced(Region, U.Seq)) 12212 return; 12213 12214 const Expr *Mod = U.UsageExpr; 12215 const Expr *ModOrUse = UsageExpr; 12216 if (OtherKind == UK_Use) 12217 std::swap(Mod, ModOrUse); 12218 12219 SemaRef.DiagRuntimeBehavior( 12220 Mod->getExprLoc(), {Mod, ModOrUse}, 12221 SemaRef.PDiag(IsModMod ? diag::warn_unsequenced_mod_mod 12222 : diag::warn_unsequenced_mod_use) 12223 << O << SourceRange(ModOrUse->getExprLoc())); 12224 UI.Diagnosed = true; 12225 } 12226 12227 // A note on note{Pre, Post}{Use, Mod}: 12228 // 12229 // (It helps to follow the algorithm with an expression such as 12230 // "((++k)++, k) = k" or "k = (k++, k++)". Both contain unsequenced 12231 // operations before C++17 and both are well-defined in C++17). 12232 // 12233 // When visiting a node which uses/modify an object we first call notePreUse 12234 // or notePreMod before visiting its sub-expression(s). At this point the 12235 // children of the current node have not yet been visited and so the eventual 12236 // uses/modifications resulting from the children of the current node have not 12237 // been recorded yet. 12238 // 12239 // We then visit the children of the current node. After that notePostUse or 12240 // notePostMod is called. These will 1) detect an unsequenced modification 12241 // as side effect (as in "k++ + k") and 2) add a new usage with the 12242 // appropriate usage kind. 12243 // 12244 // We also have to be careful that some operation sequences modification as 12245 // side effect as well (for example: || or ,). To account for this we wrap 12246 // the visitation of such a sub-expression (for example: the LHS of || or ,) 12247 // with SequencedSubexpression. SequencedSubexpression is an RAII object 12248 // which record usages which are modifications as side effect, and then 12249 // downgrade them (or more accurately restore the previous usage which was a 12250 // modification as side effect) when exiting the scope of the sequenced 12251 // subexpression. 12252 12253 void notePreUse(Object O, const Expr *UseExpr) { 12254 UsageInfo &UI = UsageMap[O]; 12255 // Uses conflict with other modifications. 12256 checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/false); 12257 } 12258 12259 void notePostUse(Object O, const Expr *UseExpr) { 12260 UsageInfo &UI = UsageMap[O]; 12261 checkUsage(O, UI, UseExpr, /*OtherKind=*/UK_ModAsSideEffect, 12262 /*IsModMod=*/false); 12263 addUsage(O, UI, UseExpr, /*UsageKind=*/UK_Use); 12264 } 12265 12266 void notePreMod(Object O, const Expr *ModExpr) { 12267 UsageInfo &UI = UsageMap[O]; 12268 // Modifications conflict with other modifications and with uses. 12269 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsValue, /*IsModMod=*/true); 12270 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_Use, /*IsModMod=*/false); 12271 } 12272 12273 void notePostMod(Object O, const Expr *ModExpr, UsageKind UK) { 12274 UsageInfo &UI = UsageMap[O]; 12275 checkUsage(O, UI, ModExpr, /*OtherKind=*/UK_ModAsSideEffect, 12276 /*IsModMod=*/true); 12277 addUsage(O, UI, ModExpr, /*UsageKind=*/UK); 12278 } 12279 12280 public: 12281 SequenceChecker(Sema &S, const Expr *E, 12282 SmallVectorImpl<const Expr *> &WorkList) 12283 : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) { 12284 Visit(E); 12285 // Silence a -Wunused-private-field since WorkList is now unused. 12286 // TODO: Evaluate if it can be used, and if not remove it. 12287 (void)this->WorkList; 12288 } 12289 12290 void VisitStmt(const Stmt *S) { 12291 // Skip all statements which aren't expressions for now. 12292 } 12293 12294 void VisitExpr(const Expr *E) { 12295 // By default, just recurse to evaluated subexpressions. 12296 Base::VisitStmt(E); 12297 } 12298 12299 void VisitCoroutineSuspendExpr(const CoroutineSuspendExpr *CSE) { 12300 for (auto *Sub : CSE->children()) { 12301 const Expr *ChildExpr = dyn_cast_or_null<Expr>(Sub); 12302 if (!ChildExpr) 12303 continue; 12304 12305 if (ChildExpr == CSE->getOperand()) 12306 // Do not recurse over a CoroutineSuspendExpr's operand. 12307 // The operand is also a subexpression of getCommonExpr(), and 12308 // recursing into it directly could confuse object management 12309 // for the sake of sequence tracking. 12310 continue; 12311 12312 Visit(Sub); 12313 } 12314 } 12315 12316 void VisitCastExpr(const CastExpr *E) { 12317 Object O = Object(); 12318 if (E->getCastKind() == CK_LValueToRValue) 12319 O = getObject(E->getSubExpr(), false); 12320 12321 if (O) 12322 notePreUse(O, E); 12323 VisitExpr(E); 12324 if (O) 12325 notePostUse(O, E); 12326 } 12327 12328 void VisitSequencedExpressions(const Expr *SequencedBefore, 12329 const Expr *SequencedAfter) { 12330 SequenceTree::Seq BeforeRegion = Tree.allocate(Region); 12331 SequenceTree::Seq AfterRegion = Tree.allocate(Region); 12332 SequenceTree::Seq OldRegion = Region; 12333 12334 { 12335 SequencedSubexpression SeqBefore(*this); 12336 Region = BeforeRegion; 12337 Visit(SequencedBefore); 12338 } 12339 12340 Region = AfterRegion; 12341 Visit(SequencedAfter); 12342 12343 Region = OldRegion; 12344 12345 Tree.merge(BeforeRegion); 12346 Tree.merge(AfterRegion); 12347 } 12348 12349 void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) { 12350 // C++17 [expr.sub]p1: 12351 // The expression E1[E2] is identical (by definition) to *((E1)+(E2)). The 12352 // expression E1 is sequenced before the expression E2. 12353 if (SemaRef.getLangOpts().CPlusPlus17) 12354 VisitSequencedExpressions(ASE->getLHS(), ASE->getRHS()); 12355 else { 12356 Visit(ASE->getLHS()); 12357 Visit(ASE->getRHS()); 12358 } 12359 } 12360 12361 void VisitBinPtrMemD(const BinaryOperator *BO) { VisitBinPtrMem(BO); } 12362 void VisitBinPtrMemI(const BinaryOperator *BO) { VisitBinPtrMem(BO); } 12363 void VisitBinPtrMem(const BinaryOperator *BO) { 12364 // C++17 [expr.mptr.oper]p4: 12365 // Abbreviating pm-expression.*cast-expression as E1.*E2, [...] 12366 // the expression E1 is sequenced before the expression E2. 12367 if (SemaRef.getLangOpts().CPlusPlus17) 12368 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12369 else { 12370 Visit(BO->getLHS()); 12371 Visit(BO->getRHS()); 12372 } 12373 } 12374 12375 void VisitBinShl(const BinaryOperator *BO) { VisitBinShlShr(BO); } 12376 void VisitBinShr(const BinaryOperator *BO) { VisitBinShlShr(BO); } 12377 void VisitBinShlShr(const BinaryOperator *BO) { 12378 // C++17 [expr.shift]p4: 12379 // The expression E1 is sequenced before the expression E2. 12380 if (SemaRef.getLangOpts().CPlusPlus17) 12381 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12382 else { 12383 Visit(BO->getLHS()); 12384 Visit(BO->getRHS()); 12385 } 12386 } 12387 12388 void VisitBinComma(const BinaryOperator *BO) { 12389 // C++11 [expr.comma]p1: 12390 // Every value computation and side effect associated with the left 12391 // expression is sequenced before every value computation and side 12392 // effect associated with the right expression. 12393 VisitSequencedExpressions(BO->getLHS(), BO->getRHS()); 12394 } 12395 12396 void VisitBinAssign(const BinaryOperator *BO) { 12397 SequenceTree::Seq RHSRegion; 12398 SequenceTree::Seq LHSRegion; 12399 if (SemaRef.getLangOpts().CPlusPlus17) { 12400 RHSRegion = Tree.allocate(Region); 12401 LHSRegion = Tree.allocate(Region); 12402 } else { 12403 RHSRegion = Region; 12404 LHSRegion = Region; 12405 } 12406 SequenceTree::Seq OldRegion = Region; 12407 12408 // C++11 [expr.ass]p1: 12409 // [...] the assignment is sequenced after the value computation 12410 // of the right and left operands, [...] 12411 // 12412 // so check it before inspecting the operands and update the 12413 // map afterwards. 12414 Object O = getObject(BO->getLHS(), /*Mod=*/true); 12415 if (O) 12416 notePreMod(O, BO); 12417 12418 if (SemaRef.getLangOpts().CPlusPlus17) { 12419 // C++17 [expr.ass]p1: 12420 // [...] The right operand is sequenced before the left operand. [...] 12421 { 12422 SequencedSubexpression SeqBefore(*this); 12423 Region = RHSRegion; 12424 Visit(BO->getRHS()); 12425 } 12426 12427 Region = LHSRegion; 12428 Visit(BO->getLHS()); 12429 12430 if (O && isa<CompoundAssignOperator>(BO)) 12431 notePostUse(O, BO); 12432 12433 } else { 12434 // C++11 does not specify any sequencing between the LHS and RHS. 12435 Region = LHSRegion; 12436 Visit(BO->getLHS()); 12437 12438 if (O && isa<CompoundAssignOperator>(BO)) 12439 notePostUse(O, BO); 12440 12441 Region = RHSRegion; 12442 Visit(BO->getRHS()); 12443 } 12444 12445 // C++11 [expr.ass]p1: 12446 // the assignment is sequenced [...] before the value computation of the 12447 // assignment expression. 12448 // C11 6.5.16/3 has no such rule. 12449 Region = OldRegion; 12450 if (O) 12451 notePostMod(O, BO, 12452 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12453 : UK_ModAsSideEffect); 12454 if (SemaRef.getLangOpts().CPlusPlus17) { 12455 Tree.merge(RHSRegion); 12456 Tree.merge(LHSRegion); 12457 } 12458 } 12459 12460 void VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) { 12461 VisitBinAssign(CAO); 12462 } 12463 12464 void VisitUnaryPreInc(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12465 void VisitUnaryPreDec(const UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 12466 void VisitUnaryPreIncDec(const UnaryOperator *UO) { 12467 Object O = getObject(UO->getSubExpr(), true); 12468 if (!O) 12469 return VisitExpr(UO); 12470 12471 notePreMod(O, UO); 12472 Visit(UO->getSubExpr()); 12473 // C++11 [expr.pre.incr]p1: 12474 // the expression ++x is equivalent to x+=1 12475 notePostMod(O, UO, 12476 SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 12477 : UK_ModAsSideEffect); 12478 } 12479 12480 void VisitUnaryPostInc(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12481 void VisitUnaryPostDec(const UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 12482 void VisitUnaryPostIncDec(const UnaryOperator *UO) { 12483 Object O = getObject(UO->getSubExpr(), true); 12484 if (!O) 12485 return VisitExpr(UO); 12486 12487 notePreMod(O, UO); 12488 Visit(UO->getSubExpr()); 12489 notePostMod(O, UO, UK_ModAsSideEffect); 12490 } 12491 12492 void VisitBinLOr(const BinaryOperator *BO) { 12493 // C++11 [expr.log.or]p2: 12494 // If the second expression is evaluated, every value computation and 12495 // side effect associated with the first expression is sequenced before 12496 // every value computation and side effect associated with the 12497 // second expression. 12498 SequenceTree::Seq LHSRegion = Tree.allocate(Region); 12499 SequenceTree::Seq RHSRegion = Tree.allocate(Region); 12500 SequenceTree::Seq OldRegion = Region; 12501 12502 EvaluationTracker Eval(*this); 12503 { 12504 SequencedSubexpression Sequenced(*this); 12505 Region = LHSRegion; 12506 Visit(BO->getLHS()); 12507 } 12508 12509 // C++11 [expr.log.or]p1: 12510 // [...] the second operand is not evaluated if the first operand 12511 // evaluates to true. 12512 bool EvalResult = false; 12513 bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); 12514 bool ShouldVisitRHS = !EvalOK || !EvalResult; 12515 if (ShouldVisitRHS) { 12516 Region = RHSRegion; 12517 Visit(BO->getRHS()); 12518 } 12519 12520 Region = OldRegion; 12521 Tree.merge(LHSRegion); 12522 Tree.merge(RHSRegion); 12523 } 12524 12525 void VisitBinLAnd(const BinaryOperator *BO) { 12526 // C++11 [expr.log.and]p2: 12527 // If the second expression is evaluated, every value computation and 12528 // side effect associated with the first expression is sequenced before 12529 // every value computation and side effect associated with the 12530 // second expression. 12531 SequenceTree::Seq LHSRegion = Tree.allocate(Region); 12532 SequenceTree::Seq RHSRegion = Tree.allocate(Region); 12533 SequenceTree::Seq OldRegion = Region; 12534 12535 EvaluationTracker Eval(*this); 12536 { 12537 SequencedSubexpression Sequenced(*this); 12538 Region = LHSRegion; 12539 Visit(BO->getLHS()); 12540 } 12541 12542 // C++11 [expr.log.and]p1: 12543 // [...] the second operand is not evaluated if the first operand is false. 12544 bool EvalResult = false; 12545 bool EvalOK = Eval.evaluate(BO->getLHS(), EvalResult); 12546 bool ShouldVisitRHS = !EvalOK || EvalResult; 12547 if (ShouldVisitRHS) { 12548 Region = RHSRegion; 12549 Visit(BO->getRHS()); 12550 } 12551 12552 Region = OldRegion; 12553 Tree.merge(LHSRegion); 12554 Tree.merge(RHSRegion); 12555 } 12556 12557 void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO) { 12558 // C++11 [expr.cond]p1: 12559 // [...] Every value computation and side effect associated with the first 12560 // expression is sequenced before every value computation and side effect 12561 // associated with the second or third expression. 12562 SequenceTree::Seq ConditionRegion = Tree.allocate(Region); 12563 12564 // No sequencing is specified between the true and false expression. 12565 // However since exactly one of both is going to be evaluated we can 12566 // consider them to be sequenced. This is needed to avoid warning on 12567 // something like "x ? y+= 1 : y += 2;" in the case where we will visit 12568 // both the true and false expressions because we can't evaluate x. 12569 // This will still allow us to detect an expression like (pre C++17) 12570 // "(x ? y += 1 : y += 2) = y". 12571 // 12572 // We don't wrap the visitation of the true and false expression with 12573 // SequencedSubexpression because we don't want to downgrade modifications 12574 // as side effect in the true and false expressions after the visition 12575 // is done. (for example in the expression "(x ? y++ : y++) + y" we should 12576 // not warn between the two "y++", but we should warn between the "y++" 12577 // and the "y". 12578 SequenceTree::Seq TrueRegion = Tree.allocate(Region); 12579 SequenceTree::Seq FalseRegion = Tree.allocate(Region); 12580 SequenceTree::Seq OldRegion = Region; 12581 12582 EvaluationTracker Eval(*this); 12583 { 12584 SequencedSubexpression Sequenced(*this); 12585 Region = ConditionRegion; 12586 Visit(CO->getCond()); 12587 } 12588 12589 // C++11 [expr.cond]p1: 12590 // [...] The first expression is contextually converted to bool (Clause 4). 12591 // It is evaluated and if it is true, the result of the conditional 12592 // expression is the value of the second expression, otherwise that of the 12593 // third expression. Only one of the second and third expressions is 12594 // evaluated. [...] 12595 bool EvalResult = false; 12596 bool EvalOK = Eval.evaluate(CO->getCond(), EvalResult); 12597 bool ShouldVisitTrueExpr = !EvalOK || EvalResult; 12598 bool ShouldVisitFalseExpr = !EvalOK || !EvalResult; 12599 if (ShouldVisitTrueExpr) { 12600 Region = TrueRegion; 12601 Visit(CO->getTrueExpr()); 12602 } 12603 if (ShouldVisitFalseExpr) { 12604 Region = FalseRegion; 12605 Visit(CO->getFalseExpr()); 12606 } 12607 12608 Region = OldRegion; 12609 Tree.merge(ConditionRegion); 12610 Tree.merge(TrueRegion); 12611 Tree.merge(FalseRegion); 12612 } 12613 12614 void VisitCallExpr(const CallExpr *CE) { 12615 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. 12616 12617 if (CE->isUnevaluatedBuiltinCall(Context)) 12618 return; 12619 12620 // C++11 [intro.execution]p15: 12621 // When calling a function [...], every value computation and side effect 12622 // associated with any argument expression, or with the postfix expression 12623 // designating the called function, is sequenced before execution of every 12624 // expression or statement in the body of the function [and thus before 12625 // the value computation of its result]. 12626 SequencedSubexpression Sequenced(*this); 12627 SemaRef.runWithSufficientStackSpace(CE->getExprLoc(), [&] { 12628 // C++17 [expr.call]p5 12629 // The postfix-expression is sequenced before each expression in the 12630 // expression-list and any default argument. [...] 12631 SequenceTree::Seq CalleeRegion; 12632 SequenceTree::Seq OtherRegion; 12633 if (SemaRef.getLangOpts().CPlusPlus17) { 12634 CalleeRegion = Tree.allocate(Region); 12635 OtherRegion = Tree.allocate(Region); 12636 } else { 12637 CalleeRegion = Region; 12638 OtherRegion = Region; 12639 } 12640 SequenceTree::Seq OldRegion = Region; 12641 12642 // Visit the callee expression first. 12643 Region = CalleeRegion; 12644 if (SemaRef.getLangOpts().CPlusPlus17) { 12645 SequencedSubexpression Sequenced(*this); 12646 Visit(CE->getCallee()); 12647 } else { 12648 Visit(CE->getCallee()); 12649 } 12650 12651 // Then visit the argument expressions. 12652 Region = OtherRegion; 12653 for (const Expr *Argument : CE->arguments()) 12654 Visit(Argument); 12655 12656 Region = OldRegion; 12657 if (SemaRef.getLangOpts().CPlusPlus17) { 12658 Tree.merge(CalleeRegion); 12659 Tree.merge(OtherRegion); 12660 } 12661 }); 12662 } 12663 12664 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CXXOCE) { 12665 // C++17 [over.match.oper]p2: 12666 // [...] the operator notation is first transformed to the equivalent 12667 // function-call notation as summarized in Table 12 (where @ denotes one 12668 // of the operators covered in the specified subclause). However, the 12669 // operands are sequenced in the order prescribed for the built-in 12670 // operator (Clause 8). 12671 // 12672 // From the above only overloaded binary operators and overloaded call 12673 // operators have sequencing rules in C++17 that we need to handle 12674 // separately. 12675 if (!SemaRef.getLangOpts().CPlusPlus17 || 12676 (CXXOCE->getNumArgs() != 2 && CXXOCE->getOperator() != OO_Call)) 12677 return VisitCallExpr(CXXOCE); 12678 12679 enum { 12680 NoSequencing, 12681 LHSBeforeRHS, 12682 RHSBeforeLHS, 12683 LHSBeforeRest 12684 } SequencingKind; 12685 switch (CXXOCE->getOperator()) { 12686 case OO_Equal: 12687 case OO_PlusEqual: 12688 case OO_MinusEqual: 12689 case OO_StarEqual: 12690 case OO_SlashEqual: 12691 case OO_PercentEqual: 12692 case OO_CaretEqual: 12693 case OO_AmpEqual: 12694 case OO_PipeEqual: 12695 case OO_LessLessEqual: 12696 case OO_GreaterGreaterEqual: 12697 SequencingKind = RHSBeforeLHS; 12698 break; 12699 12700 case OO_LessLess: 12701 case OO_GreaterGreater: 12702 case OO_AmpAmp: 12703 case OO_PipePipe: 12704 case OO_Comma: 12705 case OO_ArrowStar: 12706 case OO_Subscript: 12707 SequencingKind = LHSBeforeRHS; 12708 break; 12709 12710 case OO_Call: 12711 SequencingKind = LHSBeforeRest; 12712 break; 12713 12714 default: 12715 SequencingKind = NoSequencing; 12716 break; 12717 } 12718 12719 if (SequencingKind == NoSequencing) 12720 return VisitCallExpr(CXXOCE); 12721 12722 // This is a call, so all subexpressions are sequenced before the result. 12723 SequencedSubexpression Sequenced(*this); 12724 12725 SemaRef.runWithSufficientStackSpace(CXXOCE->getExprLoc(), [&] { 12726 assert(SemaRef.getLangOpts().CPlusPlus17 && 12727 "Should only get there with C++17 and above!"); 12728 assert((CXXOCE->getNumArgs() == 2 || CXXOCE->getOperator() == OO_Call) && 12729 "Should only get there with an overloaded binary operator" 12730 " or an overloaded call operator!"); 12731 12732 if (SequencingKind == LHSBeforeRest) { 12733 assert(CXXOCE->getOperator() == OO_Call && 12734 "We should only have an overloaded call operator here!"); 12735 12736 // This is very similar to VisitCallExpr, except that we only have the 12737 // C++17 case. The postfix-expression is the first argument of the 12738 // CXXOperatorCallExpr. The expressions in the expression-list, if any, 12739 // are in the following arguments. 12740 // 12741 // Note that we intentionally do not visit the callee expression since 12742 // it is just a decayed reference to a function. 12743 SequenceTree::Seq PostfixExprRegion = Tree.allocate(Region); 12744 SequenceTree::Seq ArgsRegion = Tree.allocate(Region); 12745 SequenceTree::Seq OldRegion = Region; 12746 12747 assert(CXXOCE->getNumArgs() >= 1 && 12748 "An overloaded call operator must have at least one argument" 12749 " for the postfix-expression!"); 12750 const Expr *PostfixExpr = CXXOCE->getArgs()[0]; 12751 llvm::ArrayRef<const Expr *> Args(CXXOCE->getArgs() + 1, 12752 CXXOCE->getNumArgs() - 1); 12753 12754 // Visit the postfix-expression first. 12755 { 12756 Region = PostfixExprRegion; 12757 SequencedSubexpression Sequenced(*this); 12758 Visit(PostfixExpr); 12759 } 12760 12761 // Then visit the argument expressions. 12762 Region = ArgsRegion; 12763 for (const Expr *Arg : Args) 12764 Visit(Arg); 12765 12766 Region = OldRegion; 12767 Tree.merge(PostfixExprRegion); 12768 Tree.merge(ArgsRegion); 12769 } else { 12770 assert(CXXOCE->getNumArgs() == 2 && 12771 "Should only have two arguments here!"); 12772 assert((SequencingKind == LHSBeforeRHS || 12773 SequencingKind == RHSBeforeLHS) && 12774 "Unexpected sequencing kind!"); 12775 12776 // We do not visit the callee expression since it is just a decayed 12777 // reference to a function. 12778 const Expr *E1 = CXXOCE->getArg(0); 12779 const Expr *E2 = CXXOCE->getArg(1); 12780 if (SequencingKind == RHSBeforeLHS) 12781 std::swap(E1, E2); 12782 12783 return VisitSequencedExpressions(E1, E2); 12784 } 12785 }); 12786 } 12787 12788 void VisitCXXConstructExpr(const CXXConstructExpr *CCE) { 12789 // This is a call, so all subexpressions are sequenced before the result. 12790 SequencedSubexpression Sequenced(*this); 12791 12792 if (!CCE->isListInitialization()) 12793 return VisitExpr(CCE); 12794 12795 // In C++11, list initializations are sequenced. 12796 SequenceExpressionsInOrder( 12797 llvm::ArrayRef(CCE->getArgs(), CCE->getNumArgs())); 12798 } 12799 12800 void VisitInitListExpr(const InitListExpr *ILE) { 12801 if (!SemaRef.getLangOpts().CPlusPlus11) 12802 return VisitExpr(ILE); 12803 12804 // In C++11, list initializations are sequenced. 12805 SequenceExpressionsInOrder(ILE->inits()); 12806 } 12807 12808 void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) { 12809 // C++20 parenthesized list initializations are sequenced. See C++20 12810 // [decl.init.general]p16.5 and [decl.init.general]p16.6.2.2. 12811 SequenceExpressionsInOrder(PLIE->getInitExprs()); 12812 } 12813 12814 private: 12815 void SequenceExpressionsInOrder(ArrayRef<const Expr *> ExpressionList) { 12816 SmallVector<SequenceTree::Seq, 32> Elts; 12817 SequenceTree::Seq Parent = Region; 12818 for (const Expr *E : ExpressionList) { 12819 if (!E) 12820 continue; 12821 Region = Tree.allocate(Parent); 12822 Elts.push_back(Region); 12823 Visit(E); 12824 } 12825 12826 // Forget that the initializers are sequenced. 12827 Region = Parent; 12828 for (unsigned I = 0; I < Elts.size(); ++I) 12829 Tree.merge(Elts[I]); 12830 } 12831 }; 12832 12833 SequenceChecker::UsageInfo::UsageInfo() = default; 12834 12835 } // namespace 12836 12837 void Sema::CheckUnsequencedOperations(const Expr *E) { 12838 SmallVector<const Expr *, 8> WorkList; 12839 WorkList.push_back(E); 12840 while (!WorkList.empty()) { 12841 const Expr *Item = WorkList.pop_back_val(); 12842 SequenceChecker(*this, Item, WorkList); 12843 } 12844 } 12845 12846 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, 12847 bool IsConstexpr) { 12848 llvm::SaveAndRestore ConstantContext(isConstantEvaluatedOverride, 12849 IsConstexpr || isa<ConstantExpr>(E)); 12850 CheckImplicitConversions(E, CheckLoc); 12851 if (!E->isInstantiationDependent()) 12852 CheckUnsequencedOperations(E); 12853 if (!IsConstexpr && !E->isValueDependent()) 12854 CheckForIntOverflow(E); 12855 DiagnoseMisalignedMembers(); 12856 } 12857 12858 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, 12859 FieldDecl *BitField, 12860 Expr *Init) { 12861 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); 12862 } 12863 12864 static void diagnoseArrayStarInParamType(Sema &S, QualType PType, 12865 SourceLocation Loc) { 12866 if (!PType->isVariablyModifiedType()) 12867 return; 12868 if (const auto *PointerTy = dyn_cast<PointerType>(PType)) { 12869 diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc); 12870 return; 12871 } 12872 if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) { 12873 diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc); 12874 return; 12875 } 12876 if (const auto *ParenTy = dyn_cast<ParenType>(PType)) { 12877 diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc); 12878 return; 12879 } 12880 12881 const ArrayType *AT = S.Context.getAsArrayType(PType); 12882 if (!AT) 12883 return; 12884 12885 if (AT->getSizeModifier() != ArraySizeModifier::Star) { 12886 diagnoseArrayStarInParamType(S, AT->getElementType(), Loc); 12887 return; 12888 } 12889 12890 S.Diag(Loc, diag::err_array_star_in_function_definition); 12891 } 12892 12893 bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters, 12894 bool CheckParameterNames) { 12895 bool HasInvalidParm = false; 12896 for (ParmVarDecl *Param : Parameters) { 12897 assert(Param && "null in a parameter list"); 12898 // C99 6.7.5.3p4: the parameters in a parameter type list in a 12899 // function declarator that is part of a function definition of 12900 // that function shall not have incomplete type. 12901 // 12902 // C++23 [dcl.fct.def.general]/p2 12903 // The type of a parameter [...] for a function definition 12904 // shall not be a (possibly cv-qualified) class type that is incomplete 12905 // or abstract within the function body unless the function is deleted. 12906 if (!Param->isInvalidDecl() && 12907 (RequireCompleteType(Param->getLocation(), Param->getType(), 12908 diag::err_typecheck_decl_incomplete_type) || 12909 RequireNonAbstractType(Param->getBeginLoc(), Param->getOriginalType(), 12910 diag::err_abstract_type_in_decl, 12911 AbstractParamType))) { 12912 Param->setInvalidDecl(); 12913 HasInvalidParm = true; 12914 } 12915 12916 // C99 6.9.1p5: If the declarator includes a parameter type list, the 12917 // declaration of each parameter shall include an identifier. 12918 if (CheckParameterNames && Param->getIdentifier() == nullptr && 12919 !Param->isImplicit() && !getLangOpts().CPlusPlus) { 12920 // Diagnose this as an extension in C17 and earlier. 12921 if (!getLangOpts().C23) 12922 Diag(Param->getLocation(), diag::ext_parameter_name_omitted_c23); 12923 } 12924 12925 // C99 6.7.5.3p12: 12926 // If the function declarator is not part of a definition of that 12927 // function, parameters may have incomplete type and may use the [*] 12928 // notation in their sequences of declarator specifiers to specify 12929 // variable length array types. 12930 QualType PType = Param->getOriginalType(); 12931 // FIXME: This diagnostic should point the '[*]' if source-location 12932 // information is added for it. 12933 diagnoseArrayStarInParamType(*this, PType, Param->getLocation()); 12934 12935 // If the parameter is a c++ class type and it has to be destructed in the 12936 // callee function, declare the destructor so that it can be called by the 12937 // callee function. Do not perform any direct access check on the dtor here. 12938 if (!Param->isInvalidDecl()) { 12939 if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) { 12940 if (!ClassDecl->isInvalidDecl() && 12941 !ClassDecl->hasIrrelevantDestructor() && 12942 !ClassDecl->isDependentContext() && 12943 ClassDecl->isParamDestroyedInCallee()) { 12944 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); 12945 MarkFunctionReferenced(Param->getLocation(), Destructor); 12946 DiagnoseUseOfDecl(Destructor, Param->getLocation()); 12947 } 12948 } 12949 } 12950 12951 // Parameters with the pass_object_size attribute only need to be marked 12952 // constant at function definitions. Because we lack information about 12953 // whether we're on a declaration or definition when we're instantiating the 12954 // attribute, we need to check for constness here. 12955 if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>()) 12956 if (!Param->getType().isConstQualified()) 12957 Diag(Param->getLocation(), diag::err_attribute_pointers_only) 12958 << Attr->getSpelling() << 1; 12959 12960 // Check for parameter names shadowing fields from the class. 12961 if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) { 12962 // The owning context for the parameter should be the function, but we 12963 // want to see if this function's declaration context is a record. 12964 DeclContext *DC = Param->getDeclContext(); 12965 if (DC && DC->isFunctionOrMethod()) { 12966 if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent())) 12967 CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(), 12968 RD, /*DeclIsField*/ false); 12969 } 12970 } 12971 12972 if (!Param->isInvalidDecl() && 12973 Param->getOriginalType()->isWebAssemblyTableType()) { 12974 Param->setInvalidDecl(); 12975 HasInvalidParm = true; 12976 Diag(Param->getLocation(), diag::err_wasm_table_as_function_parameter); 12977 } 12978 } 12979 12980 return HasInvalidParm; 12981 } 12982 12983 std::optional<std::pair< 12984 CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr 12985 *E, 12986 ASTContext 12987 &Ctx); 12988 12989 /// Compute the alignment and offset of the base class object given the 12990 /// derived-to-base cast expression and the alignment and offset of the derived 12991 /// class object. 12992 static std::pair<CharUnits, CharUnits> 12993 getDerivedToBaseAlignmentAndOffset(const CastExpr *CE, QualType DerivedType, 12994 CharUnits BaseAlignment, CharUnits Offset, 12995 ASTContext &Ctx) { 12996 for (auto PathI = CE->path_begin(), PathE = CE->path_end(); PathI != PathE; 12997 ++PathI) { 12998 const CXXBaseSpecifier *Base = *PathI; 12999 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); 13000 if (Base->isVirtual()) { 13001 // The complete object may have a lower alignment than the non-virtual 13002 // alignment of the base, in which case the base may be misaligned. Choose 13003 // the smaller of the non-virtual alignment and BaseAlignment, which is a 13004 // conservative lower bound of the complete object alignment. 13005 CharUnits NonVirtualAlignment = 13006 Ctx.getASTRecordLayout(BaseDecl).getNonVirtualAlignment(); 13007 BaseAlignment = std::min(BaseAlignment, NonVirtualAlignment); 13008 Offset = CharUnits::Zero(); 13009 } else { 13010 const ASTRecordLayout &RL = 13011 Ctx.getASTRecordLayout(DerivedType->getAsCXXRecordDecl()); 13012 Offset += RL.getBaseClassOffset(BaseDecl); 13013 } 13014 DerivedType = Base->getType(); 13015 } 13016 13017 return std::make_pair(BaseAlignment, Offset); 13018 } 13019 13020 /// Compute the alignment and offset of a binary additive operator. 13021 static std::optional<std::pair<CharUnits, CharUnits>> 13022 getAlignmentAndOffsetFromBinAddOrSub(const Expr *PtrE, const Expr *IntE, 13023 bool IsSub, ASTContext &Ctx) { 13024 QualType PointeeType = PtrE->getType()->getPointeeType(); 13025 13026 if (!PointeeType->isConstantSizeType()) 13027 return std::nullopt; 13028 13029 auto P = getBaseAlignmentAndOffsetFromPtr(PtrE, Ctx); 13030 13031 if (!P) 13032 return std::nullopt; 13033 13034 CharUnits EltSize = Ctx.getTypeSizeInChars(PointeeType); 13035 if (std::optional<llvm::APSInt> IdxRes = IntE->getIntegerConstantExpr(Ctx)) { 13036 CharUnits Offset = EltSize * IdxRes->getExtValue(); 13037 if (IsSub) 13038 Offset = -Offset; 13039 return std::make_pair(P->first, P->second + Offset); 13040 } 13041 13042 // If the integer expression isn't a constant expression, compute the lower 13043 // bound of the alignment using the alignment and offset of the pointer 13044 // expression and the element size. 13045 return std::make_pair( 13046 P->first.alignmentAtOffset(P->second).alignmentAtOffset(EltSize), 13047 CharUnits::Zero()); 13048 } 13049 13050 /// This helper function takes an lvalue expression and returns the alignment of 13051 /// a VarDecl and a constant offset from the VarDecl. 13052 std::optional<std::pair< 13053 CharUnits, 13054 CharUnits>> static getBaseAlignmentAndOffsetFromLValue(const Expr *E, 13055 ASTContext &Ctx) { 13056 E = E->IgnoreParens(); 13057 switch (E->getStmtClass()) { 13058 default: 13059 break; 13060 case Stmt::CStyleCastExprClass: 13061 case Stmt::CXXStaticCastExprClass: 13062 case Stmt::ImplicitCastExprClass: { 13063 auto *CE = cast<CastExpr>(E); 13064 const Expr *From = CE->getSubExpr(); 13065 switch (CE->getCastKind()) { 13066 default: 13067 break; 13068 case CK_NoOp: 13069 return getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13070 case CK_UncheckedDerivedToBase: 13071 case CK_DerivedToBase: { 13072 auto P = getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13073 if (!P) 13074 break; 13075 return getDerivedToBaseAlignmentAndOffset(CE, From->getType(), P->first, 13076 P->second, Ctx); 13077 } 13078 } 13079 break; 13080 } 13081 case Stmt::ArraySubscriptExprClass: { 13082 auto *ASE = cast<ArraySubscriptExpr>(E); 13083 return getAlignmentAndOffsetFromBinAddOrSub(ASE->getBase(), ASE->getIdx(), 13084 false, Ctx); 13085 } 13086 case Stmt::DeclRefExprClass: { 13087 if (auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) { 13088 // FIXME: If VD is captured by copy or is an escaping __block variable, 13089 // use the alignment of VD's type. 13090 if (!VD->getType()->isReferenceType()) { 13091 // Dependent alignment cannot be resolved -> bail out. 13092 if (VD->hasDependentAlignment()) 13093 break; 13094 return std::make_pair(Ctx.getDeclAlign(VD), CharUnits::Zero()); 13095 } 13096 if (VD->hasInit()) 13097 return getBaseAlignmentAndOffsetFromLValue(VD->getInit(), Ctx); 13098 } 13099 break; 13100 } 13101 case Stmt::MemberExprClass: { 13102 auto *ME = cast<MemberExpr>(E); 13103 auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); 13104 if (!FD || FD->getType()->isReferenceType() || 13105 FD->getParent()->isInvalidDecl()) 13106 break; 13107 std::optional<std::pair<CharUnits, CharUnits>> P; 13108 if (ME->isArrow()) 13109 P = getBaseAlignmentAndOffsetFromPtr(ME->getBase(), Ctx); 13110 else 13111 P = getBaseAlignmentAndOffsetFromLValue(ME->getBase(), Ctx); 13112 if (!P) 13113 break; 13114 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(FD->getParent()); 13115 uint64_t Offset = Layout.getFieldOffset(FD->getFieldIndex()); 13116 return std::make_pair(P->first, 13117 P->second + CharUnits::fromQuantity(Offset)); 13118 } 13119 case Stmt::UnaryOperatorClass: { 13120 auto *UO = cast<UnaryOperator>(E); 13121 switch (UO->getOpcode()) { 13122 default: 13123 break; 13124 case UO_Deref: 13125 return getBaseAlignmentAndOffsetFromPtr(UO->getSubExpr(), Ctx); 13126 } 13127 break; 13128 } 13129 case Stmt::BinaryOperatorClass: { 13130 auto *BO = cast<BinaryOperator>(E); 13131 auto Opcode = BO->getOpcode(); 13132 switch (Opcode) { 13133 default: 13134 break; 13135 case BO_Comma: 13136 return getBaseAlignmentAndOffsetFromLValue(BO->getRHS(), Ctx); 13137 } 13138 break; 13139 } 13140 } 13141 return std::nullopt; 13142 } 13143 13144 /// This helper function takes a pointer expression and returns the alignment of 13145 /// a VarDecl and a constant offset from the VarDecl. 13146 std::optional<std::pair< 13147 CharUnits, CharUnits>> static getBaseAlignmentAndOffsetFromPtr(const Expr 13148 *E, 13149 ASTContext 13150 &Ctx) { 13151 E = E->IgnoreParens(); 13152 switch (E->getStmtClass()) { 13153 default: 13154 break; 13155 case Stmt::CStyleCastExprClass: 13156 case Stmt::CXXStaticCastExprClass: 13157 case Stmt::ImplicitCastExprClass: { 13158 auto *CE = cast<CastExpr>(E); 13159 const Expr *From = CE->getSubExpr(); 13160 switch (CE->getCastKind()) { 13161 default: 13162 break; 13163 case CK_NoOp: 13164 return getBaseAlignmentAndOffsetFromPtr(From, Ctx); 13165 case CK_ArrayToPointerDecay: 13166 return getBaseAlignmentAndOffsetFromLValue(From, Ctx); 13167 case CK_UncheckedDerivedToBase: 13168 case CK_DerivedToBase: { 13169 auto P = getBaseAlignmentAndOffsetFromPtr(From, Ctx); 13170 if (!P) 13171 break; 13172 return getDerivedToBaseAlignmentAndOffset( 13173 CE, From->getType()->getPointeeType(), P->first, P->second, Ctx); 13174 } 13175 } 13176 break; 13177 } 13178 case Stmt::CXXThisExprClass: { 13179 auto *RD = E->getType()->getPointeeType()->getAsCXXRecordDecl(); 13180 CharUnits Alignment = Ctx.getASTRecordLayout(RD).getNonVirtualAlignment(); 13181 return std::make_pair(Alignment, CharUnits::Zero()); 13182 } 13183 case Stmt::UnaryOperatorClass: { 13184 auto *UO = cast<UnaryOperator>(E); 13185 if (UO->getOpcode() == UO_AddrOf) 13186 return getBaseAlignmentAndOffsetFromLValue(UO->getSubExpr(), Ctx); 13187 break; 13188 } 13189 case Stmt::BinaryOperatorClass: { 13190 auto *BO = cast<BinaryOperator>(E); 13191 auto Opcode = BO->getOpcode(); 13192 switch (Opcode) { 13193 default: 13194 break; 13195 case BO_Add: 13196 case BO_Sub: { 13197 const Expr *LHS = BO->getLHS(), *RHS = BO->getRHS(); 13198 if (Opcode == BO_Add && !RHS->getType()->isIntegralOrEnumerationType()) 13199 std::swap(LHS, RHS); 13200 return getAlignmentAndOffsetFromBinAddOrSub(LHS, RHS, Opcode == BO_Sub, 13201 Ctx); 13202 } 13203 case BO_Comma: 13204 return getBaseAlignmentAndOffsetFromPtr(BO->getRHS(), Ctx); 13205 } 13206 break; 13207 } 13208 } 13209 return std::nullopt; 13210 } 13211 13212 static CharUnits getPresumedAlignmentOfPointer(const Expr *E, Sema &S) { 13213 // See if we can compute the alignment of a VarDecl and an offset from it. 13214 std::optional<std::pair<CharUnits, CharUnits>> P = 13215 getBaseAlignmentAndOffsetFromPtr(E, S.Context); 13216 13217 if (P) 13218 return P->first.alignmentAtOffset(P->second); 13219 13220 // If that failed, return the type's alignment. 13221 return S.Context.getTypeAlignInChars(E->getType()->getPointeeType()); 13222 } 13223 13224 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { 13225 // This is actually a lot of work to potentially be doing on every 13226 // cast; don't do it if we're ignoring -Wcast_align (as is the default). 13227 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) 13228 return; 13229 13230 // Ignore dependent types. 13231 if (T->isDependentType() || Op->getType()->isDependentType()) 13232 return; 13233 13234 // Require that the destination be a pointer type. 13235 const PointerType *DestPtr = T->getAs<PointerType>(); 13236 if (!DestPtr) return; 13237 13238 // If the destination has alignment 1, we're done. 13239 QualType DestPointee = DestPtr->getPointeeType(); 13240 if (DestPointee->isIncompleteType()) return; 13241 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); 13242 if (DestAlign.isOne()) return; 13243 13244 // Require that the source be a pointer type. 13245 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); 13246 if (!SrcPtr) return; 13247 QualType SrcPointee = SrcPtr->getPointeeType(); 13248 13249 // Explicitly allow casts from cv void*. We already implicitly 13250 // allowed casts to cv void*, since they have alignment 1. 13251 // Also allow casts involving incomplete types, which implicitly 13252 // includes 'void'. 13253 if (SrcPointee->isIncompleteType()) return; 13254 13255 CharUnits SrcAlign = getPresumedAlignmentOfPointer(Op, *this); 13256 13257 if (SrcAlign >= DestAlign) return; 13258 13259 Diag(TRange.getBegin(), diag::warn_cast_align) 13260 << Op->getType() << T 13261 << static_cast<unsigned>(SrcAlign.getQuantity()) 13262 << static_cast<unsigned>(DestAlign.getQuantity()) 13263 << TRange << Op->getSourceRange(); 13264 } 13265 13266 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 13267 const ArraySubscriptExpr *ASE, 13268 bool AllowOnePastEnd, bool IndexNegated) { 13269 // Already diagnosed by the constant evaluator. 13270 if (isConstantEvaluatedContext()) 13271 return; 13272 13273 IndexExpr = IndexExpr->IgnoreParenImpCasts(); 13274 if (IndexExpr->isValueDependent()) 13275 return; 13276 13277 const Type *EffectiveType = 13278 BaseExpr->getType()->getPointeeOrArrayElementType(); 13279 BaseExpr = BaseExpr->IgnoreParenCasts(); 13280 const ConstantArrayType *ArrayTy = 13281 Context.getAsConstantArrayType(BaseExpr->getType()); 13282 13283 LangOptions::StrictFlexArraysLevelKind 13284 StrictFlexArraysLevel = getLangOpts().getStrictFlexArraysLevel(); 13285 13286 const Type *BaseType = 13287 ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr(); 13288 bool IsUnboundedArray = 13289 BaseType == nullptr || BaseExpr->isFlexibleArrayMemberLike( 13290 Context, StrictFlexArraysLevel, 13291 /*IgnoreTemplateOrMacroSubstitution=*/true); 13292 if (EffectiveType->isDependentType() || 13293 (!IsUnboundedArray && BaseType->isDependentType())) 13294 return; 13295 13296 Expr::EvalResult Result; 13297 if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects)) 13298 return; 13299 13300 llvm::APSInt index = Result.Val.getInt(); 13301 if (IndexNegated) { 13302 index.setIsUnsigned(false); 13303 index = -index; 13304 } 13305 13306 if (IsUnboundedArray) { 13307 if (EffectiveType->isFunctionType()) 13308 return; 13309 if (index.isUnsigned() || !index.isNegative()) { 13310 const auto &ASTC = getASTContext(); 13311 unsigned AddrBits = ASTC.getTargetInfo().getPointerWidth( 13312 EffectiveType->getCanonicalTypeInternal().getAddressSpace()); 13313 if (index.getBitWidth() < AddrBits) 13314 index = index.zext(AddrBits); 13315 std::optional<CharUnits> ElemCharUnits = 13316 ASTC.getTypeSizeInCharsIfKnown(EffectiveType); 13317 // PR50741 - If EffectiveType has unknown size (e.g., if it's a void 13318 // pointer) bounds-checking isn't meaningful. 13319 if (!ElemCharUnits || ElemCharUnits->isZero()) 13320 return; 13321 llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity()); 13322 // If index has more active bits than address space, we already know 13323 // we have a bounds violation to warn about. Otherwise, compute 13324 // address of (index + 1)th element, and warn about bounds violation 13325 // only if that address exceeds address space. 13326 if (index.getActiveBits() <= AddrBits) { 13327 bool Overflow; 13328 llvm::APInt Product(index); 13329 Product += 1; 13330 Product = Product.umul_ov(ElemBytes, Overflow); 13331 if (!Overflow && Product.getActiveBits() <= AddrBits) 13332 return; 13333 } 13334 13335 // Need to compute max possible elements in address space, since that 13336 // is included in diag message. 13337 llvm::APInt MaxElems = llvm::APInt::getMaxValue(AddrBits); 13338 MaxElems = MaxElems.zext(std::max(AddrBits + 1, ElemBytes.getBitWidth())); 13339 MaxElems += 1; 13340 ElemBytes = ElemBytes.zextOrTrunc(MaxElems.getBitWidth()); 13341 MaxElems = MaxElems.udiv(ElemBytes); 13342 13343 unsigned DiagID = 13344 ASE ? diag::warn_array_index_exceeds_max_addressable_bounds 13345 : diag::warn_ptr_arith_exceeds_max_addressable_bounds; 13346 13347 // Diag message shows element size in bits and in "bytes" (platform- 13348 // dependent CharUnits) 13349 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 13350 PDiag(DiagID) 13351 << toString(index, 10, true) << AddrBits 13352 << (unsigned)ASTC.toBits(*ElemCharUnits) 13353 << toString(ElemBytes, 10, false) 13354 << toString(MaxElems, 10, false) 13355 << (unsigned)MaxElems.getLimitedValue(~0U) 13356 << IndexExpr->getSourceRange()); 13357 13358 const NamedDecl *ND = nullptr; 13359 // Try harder to find a NamedDecl to point at in the note. 13360 while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) 13361 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 13362 if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 13363 ND = DRE->getDecl(); 13364 if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) 13365 ND = ME->getMemberDecl(); 13366 13367 if (ND) 13368 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 13369 PDiag(diag::note_array_declared_here) << ND); 13370 } 13371 return; 13372 } 13373 13374 if (index.isUnsigned() || !index.isNegative()) { 13375 // It is possible that the type of the base expression after 13376 // IgnoreParenCasts is incomplete, even though the type of the base 13377 // expression before IgnoreParenCasts is complete (see PR39746 for an 13378 // example). In this case we have no information about whether the array 13379 // access exceeds the array bounds. However we can still diagnose an array 13380 // access which precedes the array bounds. 13381 if (BaseType->isIncompleteType()) 13382 return; 13383 13384 llvm::APInt size = ArrayTy->getSize(); 13385 13386 if (BaseType != EffectiveType) { 13387 // Make sure we're comparing apples to apples when comparing index to 13388 // size. 13389 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); 13390 uint64_t array_typesize = Context.getTypeSize(BaseType); 13391 13392 // Handle ptrarith_typesize being zero, such as when casting to void*. 13393 // Use the size in bits (what "getTypeSize()" returns) rather than bytes. 13394 if (!ptrarith_typesize) 13395 ptrarith_typesize = Context.getCharWidth(); 13396 13397 if (ptrarith_typesize != array_typesize) { 13398 // There's a cast to a different size type involved. 13399 uint64_t ratio = array_typesize / ptrarith_typesize; 13400 13401 // TODO: Be smarter about handling cases where array_typesize is not a 13402 // multiple of ptrarith_typesize. 13403 if (ptrarith_typesize * ratio == array_typesize) 13404 size *= llvm::APInt(size.getBitWidth(), ratio); 13405 } 13406 } 13407 13408 if (size.getBitWidth() > index.getBitWidth()) 13409 index = index.zext(size.getBitWidth()); 13410 else if (size.getBitWidth() < index.getBitWidth()) 13411 size = size.zext(index.getBitWidth()); 13412 13413 // For array subscripting the index must be less than size, but for pointer 13414 // arithmetic also allow the index (offset) to be equal to size since 13415 // computing the next address after the end of the array is legal and 13416 // commonly done e.g. in C++ iterators and range-based for loops. 13417 if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) 13418 return; 13419 13420 // Suppress the warning if the subscript expression (as identified by the 13421 // ']' location) and the index expression are both from macro expansions 13422 // within a system header. 13423 if (ASE) { 13424 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( 13425 ASE->getRBracketLoc()); 13426 if (SourceMgr.isInSystemHeader(RBracketLoc)) { 13427 SourceLocation IndexLoc = 13428 SourceMgr.getSpellingLoc(IndexExpr->getBeginLoc()); 13429 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) 13430 return; 13431 } 13432 } 13433 13434 unsigned DiagID = ASE ? diag::warn_array_index_exceeds_bounds 13435 : diag::warn_ptr_arith_exceeds_bounds; 13436 unsigned CastMsg = (!ASE || BaseType == EffectiveType) ? 0 : 1; 13437 QualType CastMsgTy = ASE ? ASE->getLHS()->getType() : QualType(); 13438 13439 DiagRuntimeBehavior( 13440 BaseExpr->getBeginLoc(), BaseExpr, 13441 PDiag(DiagID) << toString(index, 10, true) << ArrayTy->desugar() 13442 << CastMsg << CastMsgTy << IndexExpr->getSourceRange()); 13443 } else { 13444 unsigned DiagID = diag::warn_array_index_precedes_bounds; 13445 if (!ASE) { 13446 DiagID = diag::warn_ptr_arith_precedes_bounds; 13447 if (index.isNegative()) index = -index; 13448 } 13449 13450 DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr, 13451 PDiag(DiagID) << toString(index, 10, true) 13452 << IndexExpr->getSourceRange()); 13453 } 13454 13455 const NamedDecl *ND = nullptr; 13456 // Try harder to find a NamedDecl to point at in the note. 13457 while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr)) 13458 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 13459 if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 13460 ND = DRE->getDecl(); 13461 if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr)) 13462 ND = ME->getMemberDecl(); 13463 13464 if (ND) 13465 DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr, 13466 PDiag(diag::note_array_declared_here) << ND); 13467 } 13468 13469 void Sema::CheckArrayAccess(const Expr *expr) { 13470 int AllowOnePastEnd = 0; 13471 while (expr) { 13472 expr = expr->IgnoreParenImpCasts(); 13473 switch (expr->getStmtClass()) { 13474 case Stmt::ArraySubscriptExprClass: { 13475 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); 13476 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, 13477 AllowOnePastEnd > 0); 13478 expr = ASE->getBase(); 13479 break; 13480 } 13481 case Stmt::MemberExprClass: { 13482 expr = cast<MemberExpr>(expr)->getBase(); 13483 break; 13484 } 13485 case Stmt::ArraySectionExprClass: { 13486 const ArraySectionExpr *ASE = cast<ArraySectionExpr>(expr); 13487 // FIXME: We should probably be checking all of the elements to the 13488 // 'length' here as well. 13489 if (ASE->getLowerBound()) 13490 CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(), 13491 /*ASE=*/nullptr, AllowOnePastEnd > 0); 13492 return; 13493 } 13494 case Stmt::UnaryOperatorClass: { 13495 // Only unwrap the * and & unary operators 13496 const UnaryOperator *UO = cast<UnaryOperator>(expr); 13497 expr = UO->getSubExpr(); 13498 switch (UO->getOpcode()) { 13499 case UO_AddrOf: 13500 AllowOnePastEnd++; 13501 break; 13502 case UO_Deref: 13503 AllowOnePastEnd--; 13504 break; 13505 default: 13506 return; 13507 } 13508 break; 13509 } 13510 case Stmt::ConditionalOperatorClass: { 13511 const ConditionalOperator *cond = cast<ConditionalOperator>(expr); 13512 if (const Expr *lhs = cond->getLHS()) 13513 CheckArrayAccess(lhs); 13514 if (const Expr *rhs = cond->getRHS()) 13515 CheckArrayAccess(rhs); 13516 return; 13517 } 13518 case Stmt::CXXOperatorCallExprClass: { 13519 const auto *OCE = cast<CXXOperatorCallExpr>(expr); 13520 for (const auto *Arg : OCE->arguments()) 13521 CheckArrayAccess(Arg); 13522 return; 13523 } 13524 default: 13525 return; 13526 } 13527 } 13528 } 13529 13530 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, 13531 Expr *RHS, bool isProperty) { 13532 // Check if RHS is an Objective-C object literal, which also can get 13533 // immediately zapped in a weak reference. Note that we explicitly 13534 // allow ObjCStringLiterals, since those are designed to never really die. 13535 RHS = RHS->IgnoreParenImpCasts(); 13536 13537 // This enum needs to match with the 'select' in 13538 // warn_objc_arc_literal_assign (off-by-1). 13539 SemaObjC::ObjCLiteralKind Kind = S.ObjC().CheckLiteralKind(RHS); 13540 if (Kind == SemaObjC::LK_String || Kind == SemaObjC::LK_None) 13541 return false; 13542 13543 S.Diag(Loc, diag::warn_arc_literal_assign) 13544 << (unsigned) Kind 13545 << (isProperty ? 0 : 1) 13546 << RHS->getSourceRange(); 13547 13548 return true; 13549 } 13550 13551 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, 13552 Qualifiers::ObjCLifetime LT, 13553 Expr *RHS, bool isProperty) { 13554 // Strip off any implicit cast added to get to the one ARC-specific. 13555 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13556 if (cast->getCastKind() == CK_ARCConsumeObject) { 13557 S.Diag(Loc, diag::warn_arc_retained_assign) 13558 << (LT == Qualifiers::OCL_ExplicitNone) 13559 << (isProperty ? 0 : 1) 13560 << RHS->getSourceRange(); 13561 return true; 13562 } 13563 RHS = cast->getSubExpr(); 13564 } 13565 13566 if (LT == Qualifiers::OCL_Weak && 13567 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) 13568 return true; 13569 13570 return false; 13571 } 13572 13573 bool Sema::checkUnsafeAssigns(SourceLocation Loc, 13574 QualType LHS, Expr *RHS) { 13575 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); 13576 13577 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) 13578 return false; 13579 13580 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) 13581 return true; 13582 13583 return false; 13584 } 13585 13586 void Sema::checkUnsafeExprAssigns(SourceLocation Loc, 13587 Expr *LHS, Expr *RHS) { 13588 QualType LHSType; 13589 // PropertyRef on LHS type need be directly obtained from 13590 // its declaration as it has a PseudoType. 13591 ObjCPropertyRefExpr *PRE 13592 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); 13593 if (PRE && !PRE->isImplicitProperty()) { 13594 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13595 if (PD) 13596 LHSType = PD->getType(); 13597 } 13598 13599 if (LHSType.isNull()) 13600 LHSType = LHS->getType(); 13601 13602 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); 13603 13604 if (LT == Qualifiers::OCL_Weak) { 13605 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) 13606 getCurFunction()->markSafeWeakUse(LHS); 13607 } 13608 13609 if (checkUnsafeAssigns(Loc, LHSType, RHS)) 13610 return; 13611 13612 // FIXME. Check for other life times. 13613 if (LT != Qualifiers::OCL_None) 13614 return; 13615 13616 if (PRE) { 13617 if (PRE->isImplicitProperty()) 13618 return; 13619 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 13620 if (!PD) 13621 return; 13622 13623 unsigned Attributes = PD->getPropertyAttributes(); 13624 if (Attributes & ObjCPropertyAttribute::kind_assign) { 13625 // when 'assign' attribute was not explicitly specified 13626 // by user, ignore it and rely on property type itself 13627 // for lifetime info. 13628 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); 13629 if (!(AsWrittenAttr & ObjCPropertyAttribute::kind_assign) && 13630 LHSType->isObjCRetainableType()) 13631 return; 13632 13633 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 13634 if (cast->getCastKind() == CK_ARCConsumeObject) { 13635 Diag(Loc, diag::warn_arc_retained_property_assign) 13636 << RHS->getSourceRange(); 13637 return; 13638 } 13639 RHS = cast->getSubExpr(); 13640 } 13641 } else if (Attributes & ObjCPropertyAttribute::kind_weak) { 13642 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) 13643 return; 13644 } 13645 } 13646 } 13647 13648 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// 13649 13650 static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, 13651 SourceLocation StmtLoc, 13652 const NullStmt *Body) { 13653 // Do not warn if the body is a macro that expands to nothing, e.g: 13654 // 13655 // #define CALL(x) 13656 // if (condition) 13657 // CALL(0); 13658 if (Body->hasLeadingEmptyMacro()) 13659 return false; 13660 13661 // Get line numbers of statement and body. 13662 bool StmtLineInvalid; 13663 unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc, 13664 &StmtLineInvalid); 13665 if (StmtLineInvalid) 13666 return false; 13667 13668 bool BodyLineInvalid; 13669 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), 13670 &BodyLineInvalid); 13671 if (BodyLineInvalid) 13672 return false; 13673 13674 // Warn if null statement and body are on the same line. 13675 if (StmtLine != BodyLine) 13676 return false; 13677 13678 return true; 13679 } 13680 13681 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, 13682 const Stmt *Body, 13683 unsigned DiagID) { 13684 // Since this is a syntactic check, don't emit diagnostic for template 13685 // instantiations, this just adds noise. 13686 if (CurrentInstantiationScope) 13687 return; 13688 13689 // The body should be a null statement. 13690 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13691 if (!NBody) 13692 return; 13693 13694 // Do the usual checks. 13695 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13696 return; 13697 13698 Diag(NBody->getSemiLoc(), DiagID); 13699 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13700 } 13701 13702 void Sema::DiagnoseEmptyLoopBody(const Stmt *S, 13703 const Stmt *PossibleBody) { 13704 assert(!CurrentInstantiationScope); // Ensured by caller 13705 13706 SourceLocation StmtLoc; 13707 const Stmt *Body; 13708 unsigned DiagID; 13709 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { 13710 StmtLoc = FS->getRParenLoc(); 13711 Body = FS->getBody(); 13712 DiagID = diag::warn_empty_for_body; 13713 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { 13714 StmtLoc = WS->getRParenLoc(); 13715 Body = WS->getBody(); 13716 DiagID = diag::warn_empty_while_body; 13717 } else 13718 return; // Neither `for' nor `while'. 13719 13720 // The body should be a null statement. 13721 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 13722 if (!NBody) 13723 return; 13724 13725 // Skip expensive checks if diagnostic is disabled. 13726 if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) 13727 return; 13728 13729 // Do the usual checks. 13730 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 13731 return; 13732 13733 // `for(...);' and `while(...);' are popular idioms, so in order to keep 13734 // noise level low, emit diagnostics only if for/while is followed by a 13735 // CompoundStmt, e.g.: 13736 // for (int i = 0; i < n; i++); 13737 // { 13738 // a(i); 13739 // } 13740 // or if for/while is followed by a statement with more indentation 13741 // than for/while itself: 13742 // for (int i = 0; i < n; i++); 13743 // a(i); 13744 bool ProbableTypo = isa<CompoundStmt>(PossibleBody); 13745 if (!ProbableTypo) { 13746 bool BodyColInvalid; 13747 unsigned BodyCol = SourceMgr.getPresumedColumnNumber( 13748 PossibleBody->getBeginLoc(), &BodyColInvalid); 13749 if (BodyColInvalid) 13750 return; 13751 13752 bool StmtColInvalid; 13753 unsigned StmtCol = 13754 SourceMgr.getPresumedColumnNumber(S->getBeginLoc(), &StmtColInvalid); 13755 if (StmtColInvalid) 13756 return; 13757 13758 if (BodyCol > StmtCol) 13759 ProbableTypo = true; 13760 } 13761 13762 if (ProbableTypo) { 13763 Diag(NBody->getSemiLoc(), DiagID); 13764 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 13765 } 13766 } 13767 13768 //===--- CHECK: Warn on self move with std::move. -------------------------===// 13769 13770 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, 13771 SourceLocation OpLoc) { 13772 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) 13773 return; 13774 13775 if (inTemplateInstantiation()) 13776 return; 13777 13778 // Strip parens and casts away. 13779 LHSExpr = LHSExpr->IgnoreParenImpCasts(); 13780 RHSExpr = RHSExpr->IgnoreParenImpCasts(); 13781 13782 // Check for a call to std::move or for a static_cast<T&&>(..) to an xvalue 13783 // which we can treat as an inlined std::move 13784 if (const auto *CE = dyn_cast<CallExpr>(RHSExpr); 13785 CE && CE->getNumArgs() == 1 && CE->isCallToStdMove()) 13786 RHSExpr = CE->getArg(0); 13787 else if (const auto *CXXSCE = dyn_cast<CXXStaticCastExpr>(RHSExpr); 13788 CXXSCE && CXXSCE->isXValue()) 13789 RHSExpr = CXXSCE->getSubExpr(); 13790 else 13791 return; 13792 13793 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); 13794 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); 13795 13796 // Two DeclRefExpr's, check that the decls are the same. 13797 if (LHSDeclRef && RHSDeclRef) { 13798 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13799 return; 13800 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13801 RHSDeclRef->getDecl()->getCanonicalDecl()) 13802 return; 13803 13804 auto D = Diag(OpLoc, diag::warn_self_move) 13805 << LHSExpr->getType() << LHSExpr->getSourceRange() 13806 << RHSExpr->getSourceRange(); 13807 if (const FieldDecl *F = 13808 getSelfAssignmentClassMemberCandidate(RHSDeclRef->getDecl())) 13809 D << 1 << F 13810 << FixItHint::CreateInsertion(LHSDeclRef->getBeginLoc(), "this->"); 13811 else 13812 D << 0; 13813 return; 13814 } 13815 13816 // Member variables require a different approach to check for self moves. 13817 // MemberExpr's are the same if every nested MemberExpr refers to the same 13818 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or 13819 // the base Expr's are CXXThisExpr's. 13820 const Expr *LHSBase = LHSExpr; 13821 const Expr *RHSBase = RHSExpr; 13822 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); 13823 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); 13824 if (!LHSME || !RHSME) 13825 return; 13826 13827 while (LHSME && RHSME) { 13828 if (LHSME->getMemberDecl()->getCanonicalDecl() != 13829 RHSME->getMemberDecl()->getCanonicalDecl()) 13830 return; 13831 13832 LHSBase = LHSME->getBase(); 13833 RHSBase = RHSME->getBase(); 13834 LHSME = dyn_cast<MemberExpr>(LHSBase); 13835 RHSME = dyn_cast<MemberExpr>(RHSBase); 13836 } 13837 13838 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); 13839 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); 13840 if (LHSDeclRef && RHSDeclRef) { 13841 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 13842 return; 13843 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 13844 RHSDeclRef->getDecl()->getCanonicalDecl()) 13845 return; 13846 13847 Diag(OpLoc, diag::warn_self_move) 13848 << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() 13849 << RHSExpr->getSourceRange(); 13850 return; 13851 } 13852 13853 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) 13854 Diag(OpLoc, diag::warn_self_move) 13855 << LHSExpr->getType() << 0 << LHSExpr->getSourceRange() 13856 << RHSExpr->getSourceRange(); 13857 } 13858 13859 //===--- Layout compatibility ----------------------------------------------// 13860 13861 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2); 13862 13863 /// Check if two enumeration types are layout-compatible. 13864 static bool isLayoutCompatible(const ASTContext &C, const EnumDecl *ED1, 13865 const EnumDecl *ED2) { 13866 // C++11 [dcl.enum] p8: 13867 // Two enumeration types are layout-compatible if they have the same 13868 // underlying type. 13869 return ED1->isComplete() && ED2->isComplete() && 13870 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); 13871 } 13872 13873 /// Check if two fields are layout-compatible. 13874 /// Can be used on union members, which are exempt from alignment requirement 13875 /// of common initial sequence. 13876 static bool isLayoutCompatible(const ASTContext &C, const FieldDecl *Field1, 13877 const FieldDecl *Field2, 13878 bool AreUnionMembers = false) { 13879 [[maybe_unused]] const Type *Field1Parent = 13880 Field1->getParent()->getTypeForDecl(); 13881 [[maybe_unused]] const Type *Field2Parent = 13882 Field2->getParent()->getTypeForDecl(); 13883 assert(((Field1Parent->isStructureOrClassType() && 13884 Field2Parent->isStructureOrClassType()) || 13885 (Field1Parent->isUnionType() && Field2Parent->isUnionType())) && 13886 "Can't evaluate layout compatibility between a struct field and a " 13887 "union field."); 13888 assert(((!AreUnionMembers && Field1Parent->isStructureOrClassType()) || 13889 (AreUnionMembers && Field1Parent->isUnionType())) && 13890 "AreUnionMembers should be 'true' for union fields (only)."); 13891 13892 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) 13893 return false; 13894 13895 if (Field1->isBitField() != Field2->isBitField()) 13896 return false; 13897 13898 if (Field1->isBitField()) { 13899 // Make sure that the bit-fields are the same length. 13900 unsigned Bits1 = Field1->getBitWidthValue(C); 13901 unsigned Bits2 = Field2->getBitWidthValue(C); 13902 13903 if (Bits1 != Bits2) 13904 return false; 13905 } 13906 13907 if (Field1->hasAttr<clang::NoUniqueAddressAttr>() || 13908 Field2->hasAttr<clang::NoUniqueAddressAttr>()) 13909 return false; 13910 13911 if (!AreUnionMembers && 13912 Field1->getMaxAlignment() != Field2->getMaxAlignment()) 13913 return false; 13914 13915 return true; 13916 } 13917 13918 /// Check if two standard-layout structs are layout-compatible. 13919 /// (C++11 [class.mem] p17) 13920 static bool isLayoutCompatibleStruct(const ASTContext &C, const RecordDecl *RD1, 13921 const RecordDecl *RD2) { 13922 // Get to the class where the fields are declared 13923 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) 13924 RD1 = D1CXX->getStandardLayoutBaseWithFields(); 13925 13926 if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) 13927 RD2 = D2CXX->getStandardLayoutBaseWithFields(); 13928 13929 // Check the fields. 13930 return llvm::equal(RD1->fields(), RD2->fields(), 13931 [&C](const FieldDecl *F1, const FieldDecl *F2) -> bool { 13932 return isLayoutCompatible(C, F1, F2); 13933 }); 13934 } 13935 13936 /// Check if two standard-layout unions are layout-compatible. 13937 /// (C++11 [class.mem] p18) 13938 static bool isLayoutCompatibleUnion(const ASTContext &C, const RecordDecl *RD1, 13939 const RecordDecl *RD2) { 13940 llvm::SmallPtrSet<const FieldDecl *, 8> UnmatchedFields; 13941 for (auto *Field2 : RD2->fields()) 13942 UnmatchedFields.insert(Field2); 13943 13944 for (auto *Field1 : RD1->fields()) { 13945 auto I = UnmatchedFields.begin(); 13946 auto E = UnmatchedFields.end(); 13947 13948 for ( ; I != E; ++I) { 13949 if (isLayoutCompatible(C, Field1, *I, /*IsUnionMember=*/true)) { 13950 bool Result = UnmatchedFields.erase(*I); 13951 (void) Result; 13952 assert(Result); 13953 break; 13954 } 13955 } 13956 if (I == E) 13957 return false; 13958 } 13959 13960 return UnmatchedFields.empty(); 13961 } 13962 13963 static bool isLayoutCompatible(const ASTContext &C, const RecordDecl *RD1, 13964 const RecordDecl *RD2) { 13965 if (RD1->isUnion() != RD2->isUnion()) 13966 return false; 13967 13968 if (RD1->isUnion()) 13969 return isLayoutCompatibleUnion(C, RD1, RD2); 13970 else 13971 return isLayoutCompatibleStruct(C, RD1, RD2); 13972 } 13973 13974 /// Check if two types are layout-compatible in C++11 sense. 13975 static bool isLayoutCompatible(const ASTContext &C, QualType T1, QualType T2) { 13976 if (T1.isNull() || T2.isNull()) 13977 return false; 13978 13979 // C++20 [basic.types] p11: 13980 // Two types cv1 T1 and cv2 T2 are layout-compatible types 13981 // if T1 and T2 are the same type, layout-compatible enumerations (9.7.1), 13982 // or layout-compatible standard-layout class types (11.4). 13983 T1 = T1.getCanonicalType().getUnqualifiedType(); 13984 T2 = T2.getCanonicalType().getUnqualifiedType(); 13985 13986 if (C.hasSameType(T1, T2)) 13987 return true; 13988 13989 const Type::TypeClass TC1 = T1->getTypeClass(); 13990 const Type::TypeClass TC2 = T2->getTypeClass(); 13991 13992 if (TC1 != TC2) 13993 return false; 13994 13995 if (TC1 == Type::Enum) { 13996 return isLayoutCompatible(C, 13997 cast<EnumType>(T1)->getDecl(), 13998 cast<EnumType>(T2)->getDecl()); 13999 } else if (TC1 == Type::Record) { 14000 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) 14001 return false; 14002 14003 return isLayoutCompatible(C, 14004 cast<RecordType>(T1)->getDecl(), 14005 cast<RecordType>(T2)->getDecl()); 14006 } 14007 14008 return false; 14009 } 14010 14011 bool Sema::IsLayoutCompatible(QualType T1, QualType T2) const { 14012 return isLayoutCompatible(getASTContext(), T1, T2); 14013 } 14014 14015 //===-------------- Pointer interconvertibility ----------------------------// 14016 14017 bool Sema::IsPointerInterconvertibleBaseOf(const TypeSourceInfo *Base, 14018 const TypeSourceInfo *Derived) { 14019 QualType BaseT = Base->getType()->getCanonicalTypeUnqualified(); 14020 QualType DerivedT = Derived->getType()->getCanonicalTypeUnqualified(); 14021 14022 if (BaseT->isStructureOrClassType() && DerivedT->isStructureOrClassType() && 14023 getASTContext().hasSameType(BaseT, DerivedT)) 14024 return true; 14025 14026 if (!IsDerivedFrom(Derived->getTypeLoc().getBeginLoc(), DerivedT, BaseT)) 14027 return false; 14028 14029 // Per [basic.compound]/4.3, containing object has to be standard-layout. 14030 if (DerivedT->getAsCXXRecordDecl()->isStandardLayout()) 14031 return true; 14032 14033 return false; 14034 } 14035 14036 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// 14037 14038 /// Given a type tag expression find the type tag itself. 14039 /// 14040 /// \param TypeExpr Type tag expression, as it appears in user's code. 14041 /// 14042 /// \param VD Declaration of an identifier that appears in a type tag. 14043 /// 14044 /// \param MagicValue Type tag magic value. 14045 /// 14046 /// \param isConstantEvaluated whether the evalaution should be performed in 14047 14048 /// constant context. 14049 static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, 14050 const ValueDecl **VD, uint64_t *MagicValue, 14051 bool isConstantEvaluated) { 14052 while(true) { 14053 if (!TypeExpr) 14054 return false; 14055 14056 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); 14057 14058 switch (TypeExpr->getStmtClass()) { 14059 case Stmt::UnaryOperatorClass: { 14060 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); 14061 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { 14062 TypeExpr = UO->getSubExpr(); 14063 continue; 14064 } 14065 return false; 14066 } 14067 14068 case Stmt::DeclRefExprClass: { 14069 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); 14070 *VD = DRE->getDecl(); 14071 return true; 14072 } 14073 14074 case Stmt::IntegerLiteralClass: { 14075 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); 14076 llvm::APInt MagicValueAPInt = IL->getValue(); 14077 if (MagicValueAPInt.getActiveBits() <= 64) { 14078 *MagicValue = MagicValueAPInt.getZExtValue(); 14079 return true; 14080 } else 14081 return false; 14082 } 14083 14084 case Stmt::BinaryConditionalOperatorClass: 14085 case Stmt::ConditionalOperatorClass: { 14086 const AbstractConditionalOperator *ACO = 14087 cast<AbstractConditionalOperator>(TypeExpr); 14088 bool Result; 14089 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx, 14090 isConstantEvaluated)) { 14091 if (Result) 14092 TypeExpr = ACO->getTrueExpr(); 14093 else 14094 TypeExpr = ACO->getFalseExpr(); 14095 continue; 14096 } 14097 return false; 14098 } 14099 14100 case Stmt::BinaryOperatorClass: { 14101 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); 14102 if (BO->getOpcode() == BO_Comma) { 14103 TypeExpr = BO->getRHS(); 14104 continue; 14105 } 14106 return false; 14107 } 14108 14109 default: 14110 return false; 14111 } 14112 } 14113 } 14114 14115 /// Retrieve the C type corresponding to type tag TypeExpr. 14116 /// 14117 /// \param TypeExpr Expression that specifies a type tag. 14118 /// 14119 /// \param MagicValues Registered magic values. 14120 /// 14121 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong 14122 /// kind. 14123 /// 14124 /// \param TypeInfo Information about the corresponding C type. 14125 /// 14126 /// \param isConstantEvaluated whether the evalaution should be performed in 14127 /// constant context. 14128 /// 14129 /// \returns true if the corresponding C type was found. 14130 static bool GetMatchingCType( 14131 const IdentifierInfo *ArgumentKind, const Expr *TypeExpr, 14132 const ASTContext &Ctx, 14133 const llvm::DenseMap<Sema::TypeTagMagicValue, Sema::TypeTagData> 14134 *MagicValues, 14135 bool &FoundWrongKind, Sema::TypeTagData &TypeInfo, 14136 bool isConstantEvaluated) { 14137 FoundWrongKind = false; 14138 14139 // Variable declaration that has type_tag_for_datatype attribute. 14140 const ValueDecl *VD = nullptr; 14141 14142 uint64_t MagicValue; 14143 14144 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue, isConstantEvaluated)) 14145 return false; 14146 14147 if (VD) { 14148 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { 14149 if (I->getArgumentKind() != ArgumentKind) { 14150 FoundWrongKind = true; 14151 return false; 14152 } 14153 TypeInfo.Type = I->getMatchingCType(); 14154 TypeInfo.LayoutCompatible = I->getLayoutCompatible(); 14155 TypeInfo.MustBeNull = I->getMustBeNull(); 14156 return true; 14157 } 14158 return false; 14159 } 14160 14161 if (!MagicValues) 14162 return false; 14163 14164 llvm::DenseMap<Sema::TypeTagMagicValue, 14165 Sema::TypeTagData>::const_iterator I = 14166 MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); 14167 if (I == MagicValues->end()) 14168 return false; 14169 14170 TypeInfo = I->second; 14171 return true; 14172 } 14173 14174 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, 14175 uint64_t MagicValue, QualType Type, 14176 bool LayoutCompatible, 14177 bool MustBeNull) { 14178 if (!TypeTagForDatatypeMagicValues) 14179 TypeTagForDatatypeMagicValues.reset( 14180 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); 14181 14182 TypeTagMagicValue Magic(ArgumentKind, MagicValue); 14183 (*TypeTagForDatatypeMagicValues)[Magic] = 14184 TypeTagData(Type, LayoutCompatible, MustBeNull); 14185 } 14186 14187 static bool IsSameCharType(QualType T1, QualType T2) { 14188 const BuiltinType *BT1 = T1->getAs<BuiltinType>(); 14189 if (!BT1) 14190 return false; 14191 14192 const BuiltinType *BT2 = T2->getAs<BuiltinType>(); 14193 if (!BT2) 14194 return false; 14195 14196 BuiltinType::Kind T1Kind = BT1->getKind(); 14197 BuiltinType::Kind T2Kind = BT2->getKind(); 14198 14199 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || 14200 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || 14201 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || 14202 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); 14203 } 14204 14205 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, 14206 const ArrayRef<const Expr *> ExprArgs, 14207 SourceLocation CallSiteLoc) { 14208 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); 14209 bool IsPointerAttr = Attr->getIsPointer(); 14210 14211 // Retrieve the argument representing the 'type_tag'. 14212 unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex(); 14213 if (TypeTagIdxAST >= ExprArgs.size()) { 14214 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 14215 << 0 << Attr->getTypeTagIdx().getSourceIndex(); 14216 return; 14217 } 14218 const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST]; 14219 bool FoundWrongKind; 14220 TypeTagData TypeInfo; 14221 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, 14222 TypeTagForDatatypeMagicValues.get(), FoundWrongKind, 14223 TypeInfo, isConstantEvaluatedContext())) { 14224 if (FoundWrongKind) 14225 Diag(TypeTagExpr->getExprLoc(), 14226 diag::warn_type_tag_for_datatype_wrong_kind) 14227 << TypeTagExpr->getSourceRange(); 14228 return; 14229 } 14230 14231 // Retrieve the argument representing the 'arg_idx'. 14232 unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex(); 14233 if (ArgumentIdxAST >= ExprArgs.size()) { 14234 Diag(CallSiteLoc, diag::err_tag_index_out_of_range) 14235 << 1 << Attr->getArgumentIdx().getSourceIndex(); 14236 return; 14237 } 14238 const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST]; 14239 if (IsPointerAttr) { 14240 // Skip implicit cast of pointer to `void *' (as a function argument). 14241 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) 14242 if (ICE->getType()->isVoidPointerType() && 14243 ICE->getCastKind() == CK_BitCast) 14244 ArgumentExpr = ICE->getSubExpr(); 14245 } 14246 QualType ArgumentType = ArgumentExpr->getType(); 14247 14248 // Passing a `void*' pointer shouldn't trigger a warning. 14249 if (IsPointerAttr && ArgumentType->isVoidPointerType()) 14250 return; 14251 14252 if (TypeInfo.MustBeNull) { 14253 // Type tag with matching void type requires a null pointer. 14254 if (!ArgumentExpr->isNullPointerConstant(Context, 14255 Expr::NPC_ValueDependentIsNotNull)) { 14256 Diag(ArgumentExpr->getExprLoc(), 14257 diag::warn_type_safety_null_pointer_required) 14258 << ArgumentKind->getName() 14259 << ArgumentExpr->getSourceRange() 14260 << TypeTagExpr->getSourceRange(); 14261 } 14262 return; 14263 } 14264 14265 QualType RequiredType = TypeInfo.Type; 14266 if (IsPointerAttr) 14267 RequiredType = Context.getPointerType(RequiredType); 14268 14269 bool mismatch = false; 14270 if (!TypeInfo.LayoutCompatible) { 14271 mismatch = !Context.hasSameType(ArgumentType, RequiredType); 14272 14273 // C++11 [basic.fundamental] p1: 14274 // Plain char, signed char, and unsigned char are three distinct types. 14275 // 14276 // But we treat plain `char' as equivalent to `signed char' or `unsigned 14277 // char' depending on the current char signedness mode. 14278 if (mismatch) 14279 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), 14280 RequiredType->getPointeeType())) || 14281 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) 14282 mismatch = false; 14283 } else 14284 if (IsPointerAttr) 14285 mismatch = !isLayoutCompatible(Context, 14286 ArgumentType->getPointeeType(), 14287 RequiredType->getPointeeType()); 14288 else 14289 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); 14290 14291 if (mismatch) 14292 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) 14293 << ArgumentType << ArgumentKind 14294 << TypeInfo.LayoutCompatible << RequiredType 14295 << ArgumentExpr->getSourceRange() 14296 << TypeTagExpr->getSourceRange(); 14297 } 14298 14299 void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD, 14300 CharUnits Alignment) { 14301 MisalignedMembers.emplace_back(E, RD, MD, Alignment); 14302 } 14303 14304 void Sema::DiagnoseMisalignedMembers() { 14305 for (MisalignedMember &m : MisalignedMembers) { 14306 const NamedDecl *ND = m.RD; 14307 if (ND->getName().empty()) { 14308 if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl()) 14309 ND = TD; 14310 } 14311 Diag(m.E->getBeginLoc(), diag::warn_taking_address_of_packed_member) 14312 << m.MD << ND << m.E->getSourceRange(); 14313 } 14314 MisalignedMembers.clear(); 14315 } 14316 14317 void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) { 14318 E = E->IgnoreParens(); 14319 if (!T->isPointerType() && !T->isIntegerType() && !T->isDependentType()) 14320 return; 14321 if (isa<UnaryOperator>(E) && 14322 cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) { 14323 auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 14324 if (isa<MemberExpr>(Op)) { 14325 auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op)); 14326 if (MA != MisalignedMembers.end() && 14327 (T->isDependentType() || T->isIntegerType() || 14328 (T->isPointerType() && (T->getPointeeType()->isIncompleteType() || 14329 Context.getTypeAlignInChars( 14330 T->getPointeeType()) <= MA->Alignment)))) 14331 MisalignedMembers.erase(MA); 14332 } 14333 } 14334 } 14335 14336 void Sema::RefersToMemberWithReducedAlignment( 14337 Expr *E, 14338 llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)> 14339 Action) { 14340 const auto *ME = dyn_cast<MemberExpr>(E); 14341 if (!ME) 14342 return; 14343 14344 // No need to check expressions with an __unaligned-qualified type. 14345 if (E->getType().getQualifiers().hasUnaligned()) 14346 return; 14347 14348 // For a chain of MemberExpr like "a.b.c.d" this list 14349 // will keep FieldDecl's like [d, c, b]. 14350 SmallVector<FieldDecl *, 4> ReverseMemberChain; 14351 const MemberExpr *TopME = nullptr; 14352 bool AnyIsPacked = false; 14353 do { 14354 QualType BaseType = ME->getBase()->getType(); 14355 if (BaseType->isDependentType()) 14356 return; 14357 if (ME->isArrow()) 14358 BaseType = BaseType->getPointeeType(); 14359 RecordDecl *RD = BaseType->castAs<RecordType>()->getDecl(); 14360 if (RD->isInvalidDecl()) 14361 return; 14362 14363 ValueDecl *MD = ME->getMemberDecl(); 14364 auto *FD = dyn_cast<FieldDecl>(MD); 14365 // We do not care about non-data members. 14366 if (!FD || FD->isInvalidDecl()) 14367 return; 14368 14369 AnyIsPacked = 14370 AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>()); 14371 ReverseMemberChain.push_back(FD); 14372 14373 TopME = ME; 14374 ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens()); 14375 } while (ME); 14376 assert(TopME && "We did not compute a topmost MemberExpr!"); 14377 14378 // Not the scope of this diagnostic. 14379 if (!AnyIsPacked) 14380 return; 14381 14382 const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts(); 14383 const auto *DRE = dyn_cast<DeclRefExpr>(TopBase); 14384 // TODO: The innermost base of the member expression may be too complicated. 14385 // For now, just disregard these cases. This is left for future 14386 // improvement. 14387 if (!DRE && !isa<CXXThisExpr>(TopBase)) 14388 return; 14389 14390 // Alignment expected by the whole expression. 14391 CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType()); 14392 14393 // No need to do anything else with this case. 14394 if (ExpectedAlignment.isOne()) 14395 return; 14396 14397 // Synthesize offset of the whole access. 14398 CharUnits Offset; 14399 for (const FieldDecl *FD : llvm::reverse(ReverseMemberChain)) 14400 Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(FD)); 14401 14402 // Compute the CompleteObjectAlignment as the alignment of the whole chain. 14403 CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars( 14404 ReverseMemberChain.back()->getParent()->getTypeForDecl()); 14405 14406 // The base expression of the innermost MemberExpr may give 14407 // stronger guarantees than the class containing the member. 14408 if (DRE && !TopME->isArrow()) { 14409 const ValueDecl *VD = DRE->getDecl(); 14410 if (!VD->getType()->isReferenceType()) 14411 CompleteObjectAlignment = 14412 std::max(CompleteObjectAlignment, Context.getDeclAlign(VD)); 14413 } 14414 14415 // Check if the synthesized offset fulfills the alignment. 14416 if (Offset % ExpectedAlignment != 0 || 14417 // It may fulfill the offset it but the effective alignment may still be 14418 // lower than the expected expression alignment. 14419 CompleteObjectAlignment < ExpectedAlignment) { 14420 // If this happens, we want to determine a sensible culprit of this. 14421 // Intuitively, watching the chain of member expressions from right to 14422 // left, we start with the required alignment (as required by the field 14423 // type) but some packed attribute in that chain has reduced the alignment. 14424 // It may happen that another packed structure increases it again. But if 14425 // we are here such increase has not been enough. So pointing the first 14426 // FieldDecl that either is packed or else its RecordDecl is, 14427 // seems reasonable. 14428 FieldDecl *FD = nullptr; 14429 CharUnits Alignment; 14430 for (FieldDecl *FDI : ReverseMemberChain) { 14431 if (FDI->hasAttr<PackedAttr>() || 14432 FDI->getParent()->hasAttr<PackedAttr>()) { 14433 FD = FDI; 14434 Alignment = std::min( 14435 Context.getTypeAlignInChars(FD->getType()), 14436 Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl())); 14437 break; 14438 } 14439 } 14440 assert(FD && "We did not find a packed FieldDecl!"); 14441 Action(E, FD->getParent(), FD, Alignment); 14442 } 14443 } 14444 14445 void Sema::CheckAddressOfPackedMember(Expr *rhs) { 14446 using namespace std::placeholders; 14447 14448 RefersToMemberWithReducedAlignment( 14449 rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1, 14450 _2, _3, _4)); 14451 } 14452 14453 bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) { 14454 if (checkArgCount(TheCall, 1)) 14455 return true; 14456 14457 ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); 14458 if (A.isInvalid()) 14459 return true; 14460 14461 TheCall->setArg(0, A.get()); 14462 QualType TyA = A.get()->getType(); 14463 14464 if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14465 return true; 14466 14467 TheCall->setType(TyA); 14468 return false; 14469 } 14470 14471 bool Sema::BuiltinElementwiseMath(CallExpr *TheCall, bool FPOnly) { 14472 QualType Res; 14473 if (BuiltinVectorMath(TheCall, Res, FPOnly)) 14474 return true; 14475 TheCall->setType(Res); 14476 return false; 14477 } 14478 14479 bool Sema::BuiltinVectorToScalarMath(CallExpr *TheCall) { 14480 QualType Res; 14481 if (BuiltinVectorMath(TheCall, Res)) 14482 return true; 14483 14484 if (auto *VecTy0 = Res->getAs<VectorType>()) 14485 TheCall->setType(VecTy0->getElementType()); 14486 else 14487 TheCall->setType(Res); 14488 14489 return false; 14490 } 14491 14492 bool Sema::BuiltinVectorMath(CallExpr *TheCall, QualType &Res, bool FPOnly) { 14493 if (checkArgCount(TheCall, 2)) 14494 return true; 14495 14496 ExprResult A = TheCall->getArg(0); 14497 ExprResult B = TheCall->getArg(1); 14498 // Do standard promotions between the two arguments, returning their common 14499 // type. 14500 Res = UsualArithmeticConversions(A, B, TheCall->getExprLoc(), ACK_Comparison); 14501 if (A.isInvalid() || B.isInvalid()) 14502 return true; 14503 14504 QualType TyA = A.get()->getType(); 14505 QualType TyB = B.get()->getType(); 14506 14507 if (Res.isNull() || TyA.getCanonicalType() != TyB.getCanonicalType()) 14508 return Diag(A.get()->getBeginLoc(), 14509 diag::err_typecheck_call_different_arg_types) 14510 << TyA << TyB; 14511 14512 if (FPOnly) { 14513 if (checkFPMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14514 return true; 14515 } else { 14516 if (checkMathBuiltinElementType(*this, A.get()->getBeginLoc(), TyA, 1)) 14517 return true; 14518 } 14519 14520 TheCall->setArg(0, A.get()); 14521 TheCall->setArg(1, B.get()); 14522 return false; 14523 } 14524 14525 bool Sema::BuiltinElementwiseTernaryMath(CallExpr *TheCall, 14526 bool CheckForFloatArgs) { 14527 if (checkArgCount(TheCall, 3)) 14528 return true; 14529 14530 Expr *Args[3]; 14531 for (int I = 0; I < 3; ++I) { 14532 ExprResult Converted = UsualUnaryConversions(TheCall->getArg(I)); 14533 if (Converted.isInvalid()) 14534 return true; 14535 Args[I] = Converted.get(); 14536 } 14537 14538 if (CheckForFloatArgs) { 14539 int ArgOrdinal = 1; 14540 for (Expr *Arg : Args) { 14541 if (checkFPMathBuiltinElementType(*this, Arg->getBeginLoc(), 14542 Arg->getType(), ArgOrdinal++)) 14543 return true; 14544 } 14545 } else { 14546 int ArgOrdinal = 1; 14547 for (Expr *Arg : Args) { 14548 if (checkMathBuiltinElementType(*this, Arg->getBeginLoc(), Arg->getType(), 14549 ArgOrdinal++)) 14550 return true; 14551 } 14552 } 14553 14554 for (int I = 1; I < 3; ++I) { 14555 if (Args[0]->getType().getCanonicalType() != 14556 Args[I]->getType().getCanonicalType()) { 14557 return Diag(Args[0]->getBeginLoc(), 14558 diag::err_typecheck_call_different_arg_types) 14559 << Args[0]->getType() << Args[I]->getType(); 14560 } 14561 14562 TheCall->setArg(I, Args[I]); 14563 } 14564 14565 TheCall->setType(Args[0]->getType()); 14566 return false; 14567 } 14568 14569 bool Sema::PrepareBuiltinReduceMathOneArgCall(CallExpr *TheCall) { 14570 if (checkArgCount(TheCall, 1)) 14571 return true; 14572 14573 ExprResult A = UsualUnaryConversions(TheCall->getArg(0)); 14574 if (A.isInvalid()) 14575 return true; 14576 14577 TheCall->setArg(0, A.get()); 14578 return false; 14579 } 14580 14581 bool Sema::BuiltinNonDeterministicValue(CallExpr *TheCall) { 14582 if (checkArgCount(TheCall, 1)) 14583 return true; 14584 14585 ExprResult Arg = TheCall->getArg(0); 14586 QualType TyArg = Arg.get()->getType(); 14587 14588 if (!TyArg->isBuiltinType() && !TyArg->isVectorType()) 14589 return Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14590 << 1 << /*vector, integer or floating point ty*/ 0 << TyArg; 14591 14592 TheCall->setType(TyArg); 14593 return false; 14594 } 14595 14596 ExprResult Sema::BuiltinMatrixTranspose(CallExpr *TheCall, 14597 ExprResult CallResult) { 14598 if (checkArgCount(TheCall, 1)) 14599 return ExprError(); 14600 14601 ExprResult MatrixArg = DefaultLvalueConversion(TheCall->getArg(0)); 14602 if (MatrixArg.isInvalid()) 14603 return MatrixArg; 14604 Expr *Matrix = MatrixArg.get(); 14605 14606 auto *MType = Matrix->getType()->getAs<ConstantMatrixType>(); 14607 if (!MType) { 14608 Diag(Matrix->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14609 << 1 << /* matrix ty*/ 1 << Matrix->getType(); 14610 return ExprError(); 14611 } 14612 14613 // Create returned matrix type by swapping rows and columns of the argument 14614 // matrix type. 14615 QualType ResultType = Context.getConstantMatrixType( 14616 MType->getElementType(), MType->getNumColumns(), MType->getNumRows()); 14617 14618 // Change the return type to the type of the returned matrix. 14619 TheCall->setType(ResultType); 14620 14621 // Update call argument to use the possibly converted matrix argument. 14622 TheCall->setArg(0, Matrix); 14623 return CallResult; 14624 } 14625 14626 // Get and verify the matrix dimensions. 14627 static std::optional<unsigned> 14628 getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) { 14629 SourceLocation ErrorPos; 14630 std::optional<llvm::APSInt> Value = 14631 Expr->getIntegerConstantExpr(S.Context, &ErrorPos); 14632 if (!Value) { 14633 S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_scalar_unsigned_arg) 14634 << Name; 14635 return {}; 14636 } 14637 uint64_t Dim = Value->getZExtValue(); 14638 if (!ConstantMatrixType::isDimensionValid(Dim)) { 14639 S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension) 14640 << Name << ConstantMatrixType::getMaxElementsPerDimension(); 14641 return {}; 14642 } 14643 return Dim; 14644 } 14645 14646 ExprResult Sema::BuiltinMatrixColumnMajorLoad(CallExpr *TheCall, 14647 ExprResult CallResult) { 14648 if (!getLangOpts().MatrixTypes) { 14649 Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled); 14650 return ExprError(); 14651 } 14652 14653 if (checkArgCount(TheCall, 4)) 14654 return ExprError(); 14655 14656 unsigned PtrArgIdx = 0; 14657 Expr *PtrExpr = TheCall->getArg(PtrArgIdx); 14658 Expr *RowsExpr = TheCall->getArg(1); 14659 Expr *ColumnsExpr = TheCall->getArg(2); 14660 Expr *StrideExpr = TheCall->getArg(3); 14661 14662 bool ArgError = false; 14663 14664 // Check pointer argument. 14665 { 14666 ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); 14667 if (PtrConv.isInvalid()) 14668 return PtrConv; 14669 PtrExpr = PtrConv.get(); 14670 TheCall->setArg(0, PtrExpr); 14671 if (PtrExpr->isTypeDependent()) { 14672 TheCall->setType(Context.DependentTy); 14673 return TheCall; 14674 } 14675 } 14676 14677 auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); 14678 QualType ElementTy; 14679 if (!PtrTy) { 14680 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14681 << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); 14682 ArgError = true; 14683 } else { 14684 ElementTy = PtrTy->getPointeeType().getUnqualifiedType(); 14685 14686 if (!ConstantMatrixType::isValidElementType(ElementTy)) { 14687 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14688 << PtrArgIdx + 1 << /* pointer to element ty*/ 2 14689 << PtrExpr->getType(); 14690 ArgError = true; 14691 } 14692 } 14693 14694 // Apply default Lvalue conversions and convert the expression to size_t. 14695 auto ApplyArgumentConversions = [this](Expr *E) { 14696 ExprResult Conv = DefaultLvalueConversion(E); 14697 if (Conv.isInvalid()) 14698 return Conv; 14699 14700 return tryConvertExprToType(Conv.get(), Context.getSizeType()); 14701 }; 14702 14703 // Apply conversion to row and column expressions. 14704 ExprResult RowsConv = ApplyArgumentConversions(RowsExpr); 14705 if (!RowsConv.isInvalid()) { 14706 RowsExpr = RowsConv.get(); 14707 TheCall->setArg(1, RowsExpr); 14708 } else 14709 RowsExpr = nullptr; 14710 14711 ExprResult ColumnsConv = ApplyArgumentConversions(ColumnsExpr); 14712 if (!ColumnsConv.isInvalid()) { 14713 ColumnsExpr = ColumnsConv.get(); 14714 TheCall->setArg(2, ColumnsExpr); 14715 } else 14716 ColumnsExpr = nullptr; 14717 14718 // If any part of the result matrix type is still pending, just use 14719 // Context.DependentTy, until all parts are resolved. 14720 if ((RowsExpr && RowsExpr->isTypeDependent()) || 14721 (ColumnsExpr && ColumnsExpr->isTypeDependent())) { 14722 TheCall->setType(Context.DependentTy); 14723 return CallResult; 14724 } 14725 14726 // Check row and column dimensions. 14727 std::optional<unsigned> MaybeRows; 14728 if (RowsExpr) 14729 MaybeRows = getAndVerifyMatrixDimension(RowsExpr, "row", *this); 14730 14731 std::optional<unsigned> MaybeColumns; 14732 if (ColumnsExpr) 14733 MaybeColumns = getAndVerifyMatrixDimension(ColumnsExpr, "column", *this); 14734 14735 // Check stride argument. 14736 ExprResult StrideConv = ApplyArgumentConversions(StrideExpr); 14737 if (StrideConv.isInvalid()) 14738 return ExprError(); 14739 StrideExpr = StrideConv.get(); 14740 TheCall->setArg(3, StrideExpr); 14741 14742 if (MaybeRows) { 14743 if (std::optional<llvm::APSInt> Value = 14744 StrideExpr->getIntegerConstantExpr(Context)) { 14745 uint64_t Stride = Value->getZExtValue(); 14746 if (Stride < *MaybeRows) { 14747 Diag(StrideExpr->getBeginLoc(), 14748 diag::err_builtin_matrix_stride_too_small); 14749 ArgError = true; 14750 } 14751 } 14752 } 14753 14754 if (ArgError || !MaybeRows || !MaybeColumns) 14755 return ExprError(); 14756 14757 TheCall->setType( 14758 Context.getConstantMatrixType(ElementTy, *MaybeRows, *MaybeColumns)); 14759 return CallResult; 14760 } 14761 14762 ExprResult Sema::BuiltinMatrixColumnMajorStore(CallExpr *TheCall, 14763 ExprResult CallResult) { 14764 if (checkArgCount(TheCall, 3)) 14765 return ExprError(); 14766 14767 unsigned PtrArgIdx = 1; 14768 Expr *MatrixExpr = TheCall->getArg(0); 14769 Expr *PtrExpr = TheCall->getArg(PtrArgIdx); 14770 Expr *StrideExpr = TheCall->getArg(2); 14771 14772 bool ArgError = false; 14773 14774 { 14775 ExprResult MatrixConv = DefaultLvalueConversion(MatrixExpr); 14776 if (MatrixConv.isInvalid()) 14777 return MatrixConv; 14778 MatrixExpr = MatrixConv.get(); 14779 TheCall->setArg(0, MatrixExpr); 14780 } 14781 if (MatrixExpr->isTypeDependent()) { 14782 TheCall->setType(Context.DependentTy); 14783 return TheCall; 14784 } 14785 14786 auto *MatrixTy = MatrixExpr->getType()->getAs<ConstantMatrixType>(); 14787 if (!MatrixTy) { 14788 Diag(MatrixExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14789 << 1 << /*matrix ty */ 1 << MatrixExpr->getType(); 14790 ArgError = true; 14791 } 14792 14793 { 14794 ExprResult PtrConv = DefaultFunctionArrayLvalueConversion(PtrExpr); 14795 if (PtrConv.isInvalid()) 14796 return PtrConv; 14797 PtrExpr = PtrConv.get(); 14798 TheCall->setArg(1, PtrExpr); 14799 if (PtrExpr->isTypeDependent()) { 14800 TheCall->setType(Context.DependentTy); 14801 return TheCall; 14802 } 14803 } 14804 14805 // Check pointer argument. 14806 auto *PtrTy = PtrExpr->getType()->getAs<PointerType>(); 14807 if (!PtrTy) { 14808 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_invalid_arg_type) 14809 << PtrArgIdx + 1 << /*pointer to element ty*/ 2 << PtrExpr->getType(); 14810 ArgError = true; 14811 } else { 14812 QualType ElementTy = PtrTy->getPointeeType(); 14813 if (ElementTy.isConstQualified()) { 14814 Diag(PtrExpr->getBeginLoc(), diag::err_builtin_matrix_store_to_const); 14815 ArgError = true; 14816 } 14817 ElementTy = ElementTy.getUnqualifiedType().getCanonicalType(); 14818 if (MatrixTy && 14819 !Context.hasSameType(ElementTy, MatrixTy->getElementType())) { 14820 Diag(PtrExpr->getBeginLoc(), 14821 diag::err_builtin_matrix_pointer_arg_mismatch) 14822 << ElementTy << MatrixTy->getElementType(); 14823 ArgError = true; 14824 } 14825 } 14826 14827 // Apply default Lvalue conversions and convert the stride expression to 14828 // size_t. 14829 { 14830 ExprResult StrideConv = DefaultLvalueConversion(StrideExpr); 14831 if (StrideConv.isInvalid()) 14832 return StrideConv; 14833 14834 StrideConv = tryConvertExprToType(StrideConv.get(), Context.getSizeType()); 14835 if (StrideConv.isInvalid()) 14836 return StrideConv; 14837 StrideExpr = StrideConv.get(); 14838 TheCall->setArg(2, StrideExpr); 14839 } 14840 14841 // Check stride argument. 14842 if (MatrixTy) { 14843 if (std::optional<llvm::APSInt> Value = 14844 StrideExpr->getIntegerConstantExpr(Context)) { 14845 uint64_t Stride = Value->getZExtValue(); 14846 if (Stride < MatrixTy->getNumRows()) { 14847 Diag(StrideExpr->getBeginLoc(), 14848 diag::err_builtin_matrix_stride_too_small); 14849 ArgError = true; 14850 } 14851 } 14852 } 14853 14854 if (ArgError) 14855 return ExprError(); 14856 14857 return CallResult; 14858 } 14859 14860 void Sema::CheckTCBEnforcement(const SourceLocation CallExprLoc, 14861 const NamedDecl *Callee) { 14862 // This warning does not make sense in code that has no runtime behavior. 14863 if (isUnevaluatedContext()) 14864 return; 14865 14866 const NamedDecl *Caller = getCurFunctionOrMethodDecl(); 14867 14868 if (!Caller || !Caller->hasAttr<EnforceTCBAttr>()) 14869 return; 14870 14871 // Search through the enforce_tcb and enforce_tcb_leaf attributes to find 14872 // all TCBs the callee is a part of. 14873 llvm::StringSet<> CalleeTCBs; 14874 for (const auto *A : Callee->specific_attrs<EnforceTCBAttr>()) 14875 CalleeTCBs.insert(A->getTCBName()); 14876 for (const auto *A : Callee->specific_attrs<EnforceTCBLeafAttr>()) 14877 CalleeTCBs.insert(A->getTCBName()); 14878 14879 // Go through the TCBs the caller is a part of and emit warnings if Caller 14880 // is in a TCB that the Callee is not. 14881 for (const auto *A : Caller->specific_attrs<EnforceTCBAttr>()) { 14882 StringRef CallerTCB = A->getTCBName(); 14883 if (CalleeTCBs.count(CallerTCB) == 0) { 14884 this->Diag(CallExprLoc, diag::warn_tcb_enforcement_violation) 14885 << Callee << CallerTCB; 14886 } 14887 } 14888 } 14889