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"
120using namespace clang;
124 unsigned ByteNo)
const {
135 unsigned ArgCount =
Call->getNumArgs();
136 if (ArgCount >= MinArgCount)
139 return Diag(
Call->getEndLoc(), diag::err_typecheck_call_too_few_args)
140 << 0 << MinArgCount << ArgCount
141 << 0 <<
Call->getSourceRange();
145 unsigned ArgCount =
Call->getNumArgs();
146 if (ArgCount <= MaxArgCount)
148 return Diag(
Call->getEndLoc(), diag::err_typecheck_call_too_many_args_at_most)
149 << 0 << MaxArgCount << ArgCount
150 << 0 <<
Call->getSourceRange();
154 unsigned MaxArgCount) {
160 unsigned ArgCount =
Call->getNumArgs();
161 if (ArgCount == DesiredArgCount)
166 assert(ArgCount > DesiredArgCount &&
"should have diagnosed this");
170 Call->getArg(ArgCount - 1)->getEndLoc());
173 << 0 << DesiredArgCount << ArgCount
174 << 0 <<
Call->getArg(1)->getSourceRange();
178 bool HasError =
false;
180 for (
unsigned I = 0; I <
Call->getNumArgs(); ++I) {
187 int DiagMsgKind = -1;
189 if (!ArgString.has_value())
191 else if (ArgString->find(
'$') != std::string::npos)
194 if (DiagMsgKind >= 0) {
205 if (
Value->isTypeDependent())
236 if (!Literal || !Literal->isOrdinary()) {
249 S.
Diag(TheCall->
getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
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();
294 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(
325 bool IsBooleanAlignBuiltin = ID == Builtin::BI__builtin_is_aligned;
327 auto IsValidIntegerType = [](
QualType Ty) {
328 return Ty->isIntegerType() && !Ty->isEnumeralType() && !Ty->isBooleanType();
335 if ((!SrcTy->
isPointerType() && !IsValidIntegerType(SrcTy)) ||
339 S.
Diag(Source->
getExprLoc(), diag::err_typecheck_expect_scalar_operand)
345 if (!IsValidIntegerType(AlignOp->
getType())) {
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;
363 if (llvm::APSInt::compareValues(AlignValue, MaxValue) > 0) {
368 if (!AlignValue.isPowerOf2()) {
369 S.
Diag(AlignOp->
getExprLoc(), diag::err_alignment_not_power_of_two);
372 if (AlignValue == 1) {
373 S.
Diag(AlignOp->
getExprLoc(), diag::warn_alignment_builtin_useless)
374 << IsBooleanAlignBuiltin;
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" },
408 bool CkdOperation = llvm::any_of(Builtins, [&](
const std::pair<
unsigned,
410 return BuiltinID ==
P.first && TheCall->
getExprLoc().isMacroID() &&
415 auto ValidCkdIntType = [](
QualType QT) {
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;
429 for (
unsigned I = 0; I < 2; ++I) {
435 bool IsValid = CkdOperation ? ValidCkdIntType(Ty) : Ty->
isIntegerType();
454 !PtrTy->getPointeeType()->isIntegerType() ||
455 (!ValidCkdIntType(PtrTy->getPointeeType()) && CkdOperation) ||
456 PtrTy->getPointeeType().isConstQualified()) {
458 diag::err_overflow_builtin_must_be_ptr_int)
466 if (BuiltinID == Builtin::BI__builtin_mul_overflow) {
467 for (
unsigned I = 0; I < 3; ++I) {
468 const auto Arg = TheCall->
getArg(I);
471 if (Ty->isBitIntType() && Ty->isSignedIntegerType() &&
473 return S.
Diag(Arg->getBeginLoc(),
474 diag::err_overflow_builtin_bit_int_max_size)
483struct BuiltinDumpStructGenerator {
492 : S(S), TheCall(TheCall), ErrorTracker(S.getDiagnostics()),
493 Policy(S.Context.getPrintingPolicy()) {
497 Expr *makeOpaqueValueExpr(
Expr *Inner) {
500 Inner->getObjectKind(), Inner);
501 Actions.push_back(OVE);
505 Expr *getStringLiteral(llvm::StringRef Str) {
511 bool callPrintFunction(llvm::StringRef Format,
515 Args.reserve((TheCall->
getNumArgs() - 2) + 1 + Exprs.size());
517 Args.push_back(getStringLiteral(Format));
518 Args.insert(Args.end(), Exprs.begin(), Exprs.end());
534 Actions.push_back(RealCall.
get());
540 Expr *getIndentString(
unsigned Depth) {
546 return getStringLiteral(Indent);
550 return getStringLiteral(
T.getAsString(Policy));
554 llvm::raw_svector_ostream OS(Str);
559 switch (BT->getKind()) {
560 case BuiltinType::Bool:
563 case BuiltinType::Char_U:
564 case BuiltinType::UChar:
567 case BuiltinType::Char_S:
568 case BuiltinType::SChar:
580 analyze_printf::PrintfConversionSpecifier::sArg) {
606 bool dumpUnnamedRecord(
const RecordDecl *RD,
Expr *
E,
unsigned Depth) {
607 Expr *IndentLit = getIndentString(Depth);
609 if (IndentLit ? callPrintFunction(
"%s%s", {IndentLit, TypeLit})
610 : callPrintFunction(
"%s", {TypeLit}))
613 return dumpRecordValue(RD,
E, IndentLit, Depth);
626 Expr *RecordArg = makeOpaqueValueExpr(
E);
629 if (callPrintFunction(
" {\n"))
633 if (
const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
634 for (
const auto &
Base : CXXRD->bases()) {
642 dumpUnnamedRecord(
Base.getType()->getAsRecordDecl(), BasePtr.
get(),
648 Expr *FieldIndentArg = getIndentString(Depth + 1);
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())
660 getStringLiteral(FD->getName())};
662 if (FD->isBitField()) {
666 FD->getBitWidthValue(S.
Context));
680 if (
Field.isInvalid())
683 auto *InnerRD = FD->getType()->getAsRecordDecl();
684 auto *InnerCXXRD = dyn_cast_or_null<CXXRecordDecl>(InnerRD);
685 if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) {
687 if (callPrintFunction(Format, Args) ||
688 dumpRecordValue(InnerRD,
Field.get(), FieldIndentArg, Depth + 1))
692 if (appendFormatSpecifier(FD->getType(), Format)) {
694 Args.push_back(
Field.get());
704 Args.push_back(FieldAddr.
get());
707 if (callPrintFunction(Format, Args))
712 return RecordIndent ? callPrintFunction(
"%s}\n", RecordIndent)
713 : callPrintFunction(
"}\n");
716 Expr *buildWrapper() {
719 TheCall->
setType(Wrapper->getType());
740 diag::err_expected_struct_pointer_argument)
749 diag::err_incomplete_type))
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:
770 diag::err_expected_callable_argument)
776 BuiltinDumpStructGenerator Generator(S, TheCall);
782 Expr *PtrArg = PtrArgResult.
get();
786 if (Generator.dumpUnnamedRecord(RD, PtrArg, 0))
789 return Generator.buildWrapper();
801 if (
Call->getStmtClass() != Stmt::CallExprClass) {
802 S.
Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
803 <<
Call->getSourceRange();
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();
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();
822 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
823 S.
Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
824 <<
Call->getSourceRange();
832 S.
Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
846 BuiltinCall->
setType(CE->getType());
850 BuiltinCall->
setArg(1, ChainResult.
get());
857class ScanfDiagnosticFormatHandler
861 using ComputeSizeFunction =
862 llvm::function_ref<std::optional<llvm::APSInt>(
unsigned)>;
866 using DiagnoseFunction =
867 llvm::function_ref<void(
unsigned,
unsigned,
unsigned)>;
869 ComputeSizeFunction ComputeSizeArgument;
870 DiagnoseFunction Diagnose;
873 ScanfDiagnosticFormatHandler(ComputeSizeFunction ComputeSizeArgument,
874 DiagnoseFunction Diagnose)
875 : ComputeSizeArgument(ComputeSizeArgument), Diagnose(Diagnose) {}
878 const char *StartSpecifier,
879 unsigned specifierLen)
override {
880 if (!FS.consumesDataArgument())
883 unsigned NulByte = 0;
884 switch ((FS.getConversionSpecifier().getKind())) {
897 analyze_format_string::OptionalAmount::HowSpecified::Constant)
902 std::optional<llvm::APSInt> DestSizeAPS =
903 ComputeSizeArgument(FS.getArgIndex());
907 unsigned DestSize = DestSizeAPS->getZExtValue();
909 if (DestSize < SourceSize)
910 Diagnose(FS.getArgIndex(), DestSize, SourceSize);
916class EstimateSizeFormatHandler
921 bool IsKernelCompatible =
true;
924 EstimateSizeFormatHandler(StringRef Format)
925 :
Size(
std::
min(Format.find(0), Format.size()) +
929 const char *,
unsigned SpecifierLen,
932 const size_t FieldWidth = computeFieldWidth(FS);
933 const size_t Precision = computePrecision(FS);
936 switch (FS.getConversionSpecifier().getKind()) {
940 Size += std::max(FieldWidth, (
size_t)1);
952 Size += std::max(FieldWidth, Precision);
968 Size += std::max(FieldWidth, 1 +
969 (Precision ? 1 + Precision
979 (Precision ? 1 + Precision : 0) +
989 (Precision ? 1 + Precision : 0) +
1004 IsKernelCompatible =
false;
1005 Size += std::max(FieldWidth, 2 + Precision);
1017 Size += FS.hasPlusPrefix() || FS.hasSpacePrefix();
1019 if (FS.hasAlternativeForm()) {
1020 switch (FS.getConversionSpecifier().getKind()) {
1049 Size += (Precision ? 0 : 1);
1056 assert(SpecifierLen <= Size &&
"no underflow");
1057 Size -= SpecifierLen;
1061 size_t getSizeLowerBound()
const {
return Size; }
1062 bool isKernelCompatible()
const {
return IsKernelCompatible; }
1067 size_t FieldWidth = 0;
1075 size_t Precision = 0;
1080 switch (FS.getConversionSpecifier().getKind()) {
1122 StringRef &FormatStrRef,
size_t &StrLen,
1124 if (
const auto *Format = dyn_cast<StringLiteral>(FormatExpr);
1125 Format && (Format->isOrdinary() || Format->isUTF8())) {
1126 FormatStrRef = Format->getString();
1129 assert(
T &&
"String literal not of constant array type!");
1130 size_t TypeSize =
T->getZExtSize();
1132 StrLen = std::min(std::max(TypeSize,
size_t(1)) - 1, FormatStrRef.find(0));
1138void Sema::checkFortifiedBuiltinMemoryFunction(
FunctionDecl *FD,
1144 bool UseDABAttr =
false;
1147 const auto *DABAttr = FD->
getAttr<DiagnoseAsBuiltinAttr>();
1149 UseDecl = DABAttr->getFunction();
1150 assert(UseDecl &&
"Missing FunctionDecl in DiagnoseAsBuiltin attribute!");
1162 auto TranslateIndex = [&](
unsigned Index) -> std::optional<unsigned> {
1169 unsigned DABIndices = DABAttr->argIndices_size();
1170 unsigned NewIndex = Index < DABIndices
1171 ? DABAttr->argIndices_begin()[Index]
1174 return std::nullopt;
1178 auto ComputeExplicitObjectSizeArgument =
1179 [&](
unsigned Index) -> std::optional<llvm::APSInt> {
1180 std::optional<unsigned> IndexOptional = TranslateIndex(Index);
1182 return std::nullopt;
1183 unsigned NewIndex = *IndexOptional;
1187 return std::nullopt;
1193 auto ComputeSizeArgument =
1194 [&](
unsigned Index) -> std::optional<llvm::APSInt> {
1200 if (Index < FD->getNumParams()) {
1201 if (
const auto *POS =
1203 BOSType = POS->getType();
1206 std::optional<unsigned> IndexOptional = TranslateIndex(Index);
1208 return std::nullopt;
1209 unsigned NewIndex = *IndexOptional;
1212 return std::nullopt;
1214 const Expr *ObjArg = TheCall->
getArg(NewIndex);
1217 return std::nullopt;
1220 return llvm::APSInt::getUnsigned(
Result).extOrTrunc(SizeTypeWidth);
1223 auto ComputeStrLenArgument =
1224 [&](
unsigned Index) -> std::optional<llvm::APSInt> {
1225 std::optional<unsigned> IndexOptional = TranslateIndex(Index);
1227 return std::nullopt;
1228 unsigned NewIndex = *IndexOptional;
1230 const Expr *ObjArg = TheCall->
getArg(NewIndex);
1233 return std::nullopt;
1235 return llvm::APSInt::getUnsigned(
Result + 1).extOrTrunc(SizeTypeWidth);
1238 std::optional<llvm::APSInt> SourceSize;
1239 std::optional<llvm::APSInt> DestinationSize;
1240 unsigned DiagID = 0;
1241 bool IsChkVariant =
false;
1243 auto GetFunctionName = [&]() {
1249 FunctionName = FunctionName.drop_front(std::strlen(
"__builtin___"));
1250 FunctionName = FunctionName.drop_back(std::strlen(
"_chk"));
1252 FunctionName.consume_front(
"__builtin_");
1254 return FunctionName;
1257 switch (BuiltinID) {
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);
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;
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) {
1286 const auto *FormatExpr =
1289 StringRef FormatStrRef;
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();
1300 PDiag(DiagID) << FunctionName << (Index + 1)
1301 << DestSize << SourceSize);
1304 auto ShiftedComputeSizeArgument = [&](
unsigned Index) {
1305 return ComputeSizeArgument(Index + DataIndex);
1307 ScanfDiagnosticFormatHandler H(ShiftedComputeSizeArgument,
Diagnose);
1308 const char *FormatBytes = FormatStrRef.data();
1319 case Builtin::BIsprintf:
1320 case Builtin::BI__builtin___sprintf_chk: {
1321 size_t FormatIndex = BuiltinID == Builtin::BIsprintf ? 1 : 3;
1324 StringRef FormatStrRef;
1327 EstimateSizeFormatHandler H(FormatStrRef);
1328 const char *FormatBytes = FormatStrRef.data();
1330 H, FormatBytes, FormatBytes + StrLen,
getLangOpts(),
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;
1341 DestinationSize = ComputeSizeArgument(0);
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);
1361 ComputeExplicitObjectSizeArgument(TheCall->
getNumArgs() - 1);
1362 IsChkVariant =
true;
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;
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: {
1386 DiagID = diag::warn_fortify_source_size_mismatch;
1387 SourceSize = ComputeExplicitObjectSizeArgument(TheCall->
getNumArgs() - 1);
1388 DestinationSize = ComputeSizeArgument(0);
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);
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);
1412 StringRef FormatStrRef;
1416 EstimateSizeFormatHandler H(FormatStrRef);
1417 const char *FormatBytes = FormatStrRef.data();
1419 H, FormatBytes, FormatBytes + StrLen,
getLangOpts(),
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;
1430 SourceSize->toString(SpecifiedSizeStr, 10);
1431 FormatSize.toString(FormatSizeStr, 10);
1433 PDiag(TruncationDiagID)
1434 << GetFunctionName() << SpecifiedSizeStr
1439 DestinationSize = ComputeSizeArgument(0);
1443 if (!SourceSize || !DestinationSize ||
1444 llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0)
1447 StringRef FunctionName = GetFunctionName();
1451 DestinationSize->toString(DestinationStr, 10);
1452 SourceSize->toString(SourceStr, 10);
1455 << FunctionName << DestinationStr << SourceStr);
1468 while (S && !S->isSEHExceptScope())
1470 if (!S || !(S->getFlags() & NeededScopeFlags)) {
1473 << DRE->getDecl()->getIdentifier();
1485 "__builtin_alloca has invalid address space");
1493enum PointerAuthOpKind {
1542 llvm::raw_svector_ostream Str(
Value);
1551 Result = KeyValue->getZExtValue();
1555static std::pair<const ValueDecl *, CharUnits>
1562 const auto *BaseDecl =
1567 return {BaseDecl,
Result.Val.getLValueOffset()};
1571 bool RequireConstant =
false) {
1579 auto AllowsPointer = [](PointerAuthOpKind OpKind) {
1580 return OpKind != PAO_BlendInteger;
1582 auto AllowsInteger = [](PointerAuthOpKind OpKind) {
1583 return OpKind == PAO_Discriminator || OpKind == PAO_BlendInteger ||
1584 OpKind == PAO_SignGeneric;
1593 }
else if (AllowsInteger(OpKind) &&
1600 <<
unsigned(OpKind == PAO_Discriminator ? 1
1601 : OpKind == PAO_BlendPointer ? 2
1602 : OpKind == PAO_BlendInteger ? 3
1604 <<
unsigned(AllowsInteger(OpKind) ? (AllowsPointer(OpKind) ? 2 : 1) : 0)
1614 if (!RequireConstant) {
1616 if ((OpKind == PAO_Sign || OpKind == PAO_Auth) &&
1619 ? diag::warn_ptrauth_sign_null_pointer
1620 : diag::warn_ptrauth_auth_null_pointer)
1630 if (OpKind == PAO_Sign) {
1640 else if (isa<FunctionDecl>(BaseDecl))
1648 S.
Diag(Arg->
getExprLoc(), diag::err_ptrauth_bad_constant_pointer);
1653 assert(OpKind == PAO_Discriminator);
1659 if (
Call->getBuiltinCallee() ==
1660 Builtin::BI__builtin_ptrauth_blend_discriminator) {
1675 assert(
Pointer->getType()->isPointerType());
1681 if (!BaseDecl || !isa<VarDecl>(BaseDecl))
1687 assert(
Integer->getType()->isIntegerType());
1693 S.
Diag(Arg->
getExprLoc(), diag::err_ptrauth_bad_constant_discriminator);
1706 Call->setType(
Call->getArgs()[0]->getType());
1737 PointerAuthOpKind OpKind,
1738 bool RequireConstant) {
1749 Call->setType(
Call->getArgs()[0]->getType());
1765 Call->setType(
Call->getArgs()[0]->getType());
1774 const Expr *Arg =
Call->getArg(0)->IgnoreParenImpCasts();
1777 const auto *Literal = dyn_cast<StringLiteral>(Arg);
1778 if (!Literal || Literal->getCharByteWidth() != 1) {
1808 auto DiagSelect = [&]() -> std::optional<unsigned> {
1815 return std::optional<unsigned>{};
1830 diag::err_incomplete_type))
1834 "Unhandled non-object pointer case");
1852 llvm::Triple::ObjectFormatType CurObjFormat =
1854 if (llvm::is_contained(UnsupportedObjectFormatTypes, CurObjFormat)) {
1867 llvm::Triple::ArchType CurArch =
1869 if (llvm::is_contained(SupportedArchs, CurArch))
1879bool Sema::CheckTSBuiltinFunctionCall(
const TargetInfo &TI,
unsigned BuiltinID,
1886 case llvm::Triple::arm:
1887 case llvm::Triple::armeb:
1888 case llvm::Triple::thumb:
1889 case llvm::Triple::thumbeb:
1891 case llvm::Triple::aarch64:
1892 case llvm::Triple::aarch64_32:
1893 case llvm::Triple::aarch64_be:
1895 case llvm::Triple::bpfeb:
1896 case llvm::Triple::bpfel:
1898 case llvm::Triple::hexagon:
1900 case llvm::Triple::mips:
1901 case llvm::Triple::mipsel:
1902 case llvm::Triple::mips64:
1903 case llvm::Triple::mips64el:
1905 case llvm::Triple::systemz:
1907 case llvm::Triple::x86:
1908 case llvm::Triple::x86_64:
1910 case llvm::Triple::ppc:
1911 case llvm::Triple::ppcle:
1912 case llvm::Triple::ppc64:
1913 case llvm::Triple::ppc64le:
1915 case llvm::Triple::amdgcn:
1917 case llvm::Triple::riscv32:
1918 case llvm::Triple::riscv64:
1920 case llvm::Triple::loongarch32:
1921 case llvm::Triple::loongarch64:
1924 case llvm::Triple::wasm32:
1925 case llvm::Triple::wasm64:
1927 case llvm::Triple::nvptx:
1928 case llvm::Triple::nvptx64:
1940 return S.
Diag(
Loc, diag::err_builtin_invalid_arg_type)
1941 << ArgIndex << 0 << ArgTy;
1951 EltTy = VecTy->getElementType();
1954 return S.
Diag(
Loc, diag::err_builtin_invalid_arg_type)
1955 << ArgIndex << 5 << ArgTy;
1965 const TargetInfo *AuxTI,
unsigned BuiltinID) {
1966 assert((BuiltinID == Builtin::BI__builtin_cpu_supports ||
1967 BuiltinID == Builtin::BI__builtin_cpu_is) &&
1968 "Expecting __builtin_cpu_...");
1970 bool IsCPUSupports = BuiltinID == Builtin::BI__builtin_cpu_supports;
1972 auto SupportsBI = [=](
const TargetInfo *TInfo) {
1973 return TInfo && ((IsCPUSupports && TInfo->supportsCpuSupports()) ||
1974 (!IsCPUSupports && TInfo->supportsCpuIs()));
1976 if (!SupportsBI(&TI) && SupportsBI(AuxTI))
1983 ? diag::err_builtin_aix_os_unsupported
1984 : diag::err_builtin_target_unsupported)
1989 if (!isa<StringLiteral>(Arg))
1990 return S.
Diag(TheCall->
getBeginLoc(), diag::err_expr_not_string_literal)
1994 StringRef Feature = cast<StringLiteral>(Arg)->getString();
2041 TheCall->
setArg(0, Arg0);
2047 << 1 << 7 << Arg0Ty;
2057 TheCall->
setArg(1, Arg1);
2063 << 2 << 8 << Arg1Ty;
2072Sema::CheckBuiltinFunctionCall(
FunctionDecl *FDecl,
unsigned BuiltinID,
2077 unsigned ICEArguments = 0;
2084 for (
unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
2086 if ((ICEArguments & (1 << ArgNo)) == 0)
continue;
2091 if (ArgNo < TheCall->getNumArgs() &&
2094 ICEArguments &= ~(1 << ArgNo);
2098 switch (BuiltinID) {
2099 case Builtin::BI__builtin_cpu_supports:
2100 case Builtin::BI__builtin_cpu_is:
2105 case Builtin::BI__builtin_cpu_init:
2112 case Builtin::BI__builtin___CFStringMakeConstantString:
2116 *
this, BuiltinID, TheCall,
2117 {llvm::Triple::GOFF, llvm::Triple::XCOFF}))
2120 "Wrong # arguments to builtin CFStringMakeConstantString");
2121 if (
ObjC().CheckObjCString(TheCall->
getArg(0)))
2124 case Builtin::BI__builtin_ms_va_start:
2125 case Builtin::BI__builtin_stdarg_start:
2126 case Builtin::BI__builtin_va_start:
2127 if (BuiltinVAStart(BuiltinID, TheCall))
2130 case Builtin::BI__va_start: {
2132 case llvm::Triple::aarch64:
2133 case llvm::Triple::arm:
2134 case llvm::Triple::thumb:
2135 if (BuiltinVAStartARMMicrosoft(TheCall))
2139 if (BuiltinVAStart(BuiltinID, TheCall))
2147 case Builtin::BI_interlockedbittestandset_acq:
2148 case Builtin::BI_interlockedbittestandset_rel:
2149 case Builtin::BI_interlockedbittestandset_nf:
2150 case Builtin::BI_interlockedbittestandreset_acq:
2151 case Builtin::BI_interlockedbittestandreset_rel:
2152 case Builtin::BI_interlockedbittestandreset_nf:
2155 {llvm::Triple::arm, llvm::Triple::thumb, llvm::Triple::aarch64}))
2160 case Builtin::BI_bittest64:
2161 case Builtin::BI_bittestandcomplement64:
2162 case Builtin::BI_bittestandreset64:
2163 case Builtin::BI_bittestandset64:
2164 case Builtin::BI_interlockedbittestandreset64:
2165 case Builtin::BI_interlockedbittestandset64:
2168 {llvm::Triple::x86_64, llvm::Triple::arm, llvm::Triple::thumb,
2169 llvm::Triple::aarch64, llvm::Triple::amdgcn}))
2173 case Builtin::BI__builtin_set_flt_rounds:
2176 {llvm::Triple::x86, llvm::Triple::x86_64, llvm::Triple::arm,
2177 llvm::Triple::thumb, llvm::Triple::aarch64, llvm::Triple::amdgcn}))
2181 case Builtin::BI__builtin_isgreater:
2182 case Builtin::BI__builtin_isgreaterequal:
2183 case Builtin::BI__builtin_isless:
2184 case Builtin::BI__builtin_islessequal:
2185 case Builtin::BI__builtin_islessgreater:
2186 case Builtin::BI__builtin_isunordered:
2187 if (BuiltinUnorderedCompare(TheCall, BuiltinID))
2190 case Builtin::BI__builtin_fpclassify:
2191 if (BuiltinFPClassification(TheCall, 6, BuiltinID))
2194 case Builtin::BI__builtin_isfpclass:
2195 if (BuiltinFPClassification(TheCall, 2, BuiltinID))
2198 case Builtin::BI__builtin_isfinite:
2199 case Builtin::BI__builtin_isinf:
2200 case Builtin::BI__builtin_isinf_sign:
2201 case Builtin::BI__builtin_isnan:
2202 case Builtin::BI__builtin_issignaling:
2203 case Builtin::BI__builtin_isnormal:
2204 case Builtin::BI__builtin_issubnormal:
2205 case Builtin::BI__builtin_iszero:
2206 case Builtin::BI__builtin_signbit:
2207 case Builtin::BI__builtin_signbitf:
2208 case Builtin::BI__builtin_signbitl:
2209 if (BuiltinFPClassification(TheCall, 1, BuiltinID))
2212 case Builtin::BI__builtin_shufflevector:
2216 case Builtin::BI__builtin_prefetch:
2217 if (BuiltinPrefetch(TheCall))
2220 case Builtin::BI__builtin_alloca_with_align:
2221 case Builtin::BI__builtin_alloca_with_align_uninitialized:
2222 if (BuiltinAllocaWithAlign(TheCall))
2225 case Builtin::BI__builtin_alloca:
2226 case Builtin::BI__builtin_alloca_uninitialized:
2233 case Builtin::BI__arithmetic_fence:
2234 if (BuiltinArithmeticFence(TheCall))
2237 case Builtin::BI__assume:
2238 case Builtin::BI__builtin_assume:
2239 if (BuiltinAssume(TheCall))
2242 case Builtin::BI__builtin_assume_aligned:
2243 if (BuiltinAssumeAligned(TheCall))
2246 case Builtin::BI__builtin_dynamic_object_size:
2247 case Builtin::BI__builtin_object_size:
2251 case Builtin::BI__builtin_longjmp:
2252 if (BuiltinLongjmp(TheCall))
2255 case Builtin::BI__builtin_setjmp:
2256 if (BuiltinSetjmp(TheCall))
2259 case Builtin::BI__builtin_classify_type:
2264 case Builtin::BI__builtin_complex:
2265 if (BuiltinComplex(TheCall))
2268 case Builtin::BI__builtin_constant_p: {
2277 case Builtin::BI__builtin_launder:
2279 case Builtin::BI__sync_fetch_and_add:
2280 case Builtin::BI__sync_fetch_and_add_1:
2281 case Builtin::BI__sync_fetch_and_add_2:
2282 case Builtin::BI__sync_fetch_and_add_4:
2283 case Builtin::BI__sync_fetch_and_add_8:
2284 case Builtin::BI__sync_fetch_and_add_16:
2285 case Builtin::BI__sync_fetch_and_sub:
2286 case Builtin::BI__sync_fetch_and_sub_1:
2287 case Builtin::BI__sync_fetch_and_sub_2:
2288 case Builtin::BI__sync_fetch_and_sub_4:
2289 case Builtin::BI__sync_fetch_and_sub_8:
2290 case Builtin::BI__sync_fetch_and_sub_16:
2291 case Builtin::BI__sync_fetch_and_or:
2292 case Builtin::BI__sync_fetch_and_or_1:
2293 case Builtin::BI__sync_fetch_and_or_2:
2294 case Builtin::BI__sync_fetch_and_or_4:
2295 case Builtin::BI__sync_fetch_and_or_8:
2296 case Builtin::BI__sync_fetch_and_or_16:
2297 case Builtin::BI__sync_fetch_and_and:
2298 case Builtin::BI__sync_fetch_and_and_1:
2299 case Builtin::BI__sync_fetch_and_and_2:
2300 case Builtin::BI__sync_fetch_and_and_4:
2301 case Builtin::BI__sync_fetch_and_and_8:
2302 case Builtin::BI__sync_fetch_and_and_16:
2303 case Builtin::BI__sync_fetch_and_xor:
2304 case Builtin::BI__sync_fetch_and_xor_1:
2305 case Builtin::BI__sync_fetch_and_xor_2:
2306 case Builtin::BI__sync_fetch_and_xor_4:
2307 case Builtin::BI__sync_fetch_and_xor_8:
2308 case Builtin::BI__sync_fetch_and_xor_16:
2309 case Builtin::BI__sync_fetch_and_nand:
2310 case Builtin::BI__sync_fetch_and_nand_1:
2311 case Builtin::BI__sync_fetch_and_nand_2:
2312 case Builtin::BI__sync_fetch_and_nand_4:
2313 case Builtin::BI__sync_fetch_and_nand_8:
2314 case Builtin::BI__sync_fetch_and_nand_16:
2315 case Builtin::BI__sync_add_and_fetch:
2316 case Builtin::BI__sync_add_and_fetch_1:
2317 case Builtin::BI__sync_add_and_fetch_2:
2318 case Builtin::BI__sync_add_and_fetch_4:
2319 case Builtin::BI__sync_add_and_fetch_8:
2320 case Builtin::BI__sync_add_and_fetch_16:
2321 case Builtin::BI__sync_sub_and_fetch:
2322 case Builtin::BI__sync_sub_and_fetch_1:
2323 case Builtin::BI__sync_sub_and_fetch_2:
2324 case Builtin::BI__sync_sub_and_fetch_4:
2325 case Builtin::BI__sync_sub_and_fetch_8:
2326 case Builtin::BI__sync_sub_and_fetch_16:
2327 case Builtin::BI__sync_and_and_fetch:
2328 case Builtin::BI__sync_and_and_fetch_1:
2329 case Builtin::BI__sync_and_and_fetch_2:
2330 case Builtin::BI__sync_and_and_fetch_4:
2331 case Builtin::BI__sync_and_and_fetch_8:
2332 case Builtin::BI__sync_and_and_fetch_16:
2333 case Builtin::BI__sync_or_and_fetch:
2334 case Builtin::BI__sync_or_and_fetch_1:
2335 case Builtin::BI__sync_or_and_fetch_2:
2336 case Builtin::BI__sync_or_and_fetch_4:
2337 case Builtin::BI__sync_or_and_fetch_8:
2338 case Builtin::BI__sync_or_and_fetch_16:
2339 case Builtin::BI__sync_xor_and_fetch:
2340 case Builtin::BI__sync_xor_and_fetch_1:
2341 case Builtin::BI__sync_xor_and_fetch_2:
2342 case Builtin::BI__sync_xor_and_fetch_4:
2343 case Builtin::BI__sync_xor_and_fetch_8:
2344 case Builtin::BI__sync_xor_and_fetch_16:
2345 case Builtin::BI__sync_nand_and_fetch:
2346 case Builtin::BI__sync_nand_and_fetch_1:
2347 case Builtin::BI__sync_nand_and_fetch_2:
2348 case Builtin::BI__sync_nand_and_fetch_4:
2349 case Builtin::BI__sync_nand_and_fetch_8:
2350 case Builtin::BI__sync_nand_and_fetch_16:
2351 case Builtin::BI__sync_val_compare_and_swap:
2352 case Builtin::BI__sync_val_compare_and_swap_1:
2353 case Builtin::BI__sync_val_compare_and_swap_2:
2354 case Builtin::BI__sync_val_compare_and_swap_4:
2355 case Builtin::BI__sync_val_compare_and_swap_8:
2356 case Builtin::BI__sync_val_compare_and_swap_16:
2357 case Builtin::BI__sync_bool_compare_and_swap:
2358 case Builtin::BI__sync_bool_compare_and_swap_1:
2359 case Builtin::BI__sync_bool_compare_and_swap_2:
2360 case Builtin::BI__sync_bool_compare_and_swap_4:
2361 case Builtin::BI__sync_bool_compare_and_swap_8:
2362 case Builtin::BI__sync_bool_compare_and_swap_16:
2363 case Builtin::BI__sync_lock_test_and_set:
2364 case Builtin::BI__sync_lock_test_and_set_1:
2365 case Builtin::BI__sync_lock_test_and_set_2:
2366 case Builtin::BI__sync_lock_test_and_set_4:
2367 case Builtin::BI__sync_lock_test_and_set_8:
2368 case Builtin::BI__sync_lock_test_and_set_16:
2369 case Builtin::BI__sync_lock_release:
2370 case Builtin::BI__sync_lock_release_1:
2371 case Builtin::BI__sync_lock_release_2:
2372 case Builtin::BI__sync_lock_release_4:
2373 case Builtin::BI__sync_lock_release_8:
2374 case Builtin::BI__sync_lock_release_16:
2375 case Builtin::BI__sync_swap:
2376 case Builtin::BI__sync_swap_1:
2377 case Builtin::BI__sync_swap_2:
2378 case Builtin::BI__sync_swap_4:
2379 case Builtin::BI__sync_swap_8:
2380 case Builtin::BI__sync_swap_16:
2381 return BuiltinAtomicOverloaded(TheCallResult);
2382 case Builtin::BI__sync_synchronize:
2386 case Builtin::BI__builtin_nontemporal_load:
2387 case Builtin::BI__builtin_nontemporal_store:
2388 return BuiltinNontemporalOverloaded(TheCallResult);
2389 case Builtin::BI__builtin_memcpy_inline: {
2402 case Builtin::BI__builtin_memset_inline: {
2413#define BUILTIN(ID, TYPE, ATTRS)
2414#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
2415 case Builtin::BI##ID: \
2416 return AtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
2417#include "clang/Basic/Builtins.inc"
2418 case Builtin::BI__annotation:
2422 case Builtin::BI__builtin_annotation:
2426 case Builtin::BI__builtin_addressof:
2430 case Builtin::BI__builtin_function_start:
2434 case Builtin::BI__builtin_is_aligned:
2435 case Builtin::BI__builtin_align_up:
2436 case Builtin::BI__builtin_align_down:
2440 case Builtin::BI__builtin_add_overflow:
2441 case Builtin::BI__builtin_sub_overflow:
2442 case Builtin::BI__builtin_mul_overflow:
2446 case Builtin::BI__builtin_operator_new:
2447 case Builtin::BI__builtin_operator_delete: {
2448 bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete;
2450 BuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete);
2455 case Builtin::BI__builtin_dump_struct:
2457 case Builtin::BI__builtin_expect_with_probability: {
2468 Diag(ProbArg->
getBeginLoc(), diag::err_probability_not_constant_float)
2475 bool LoseInfo =
false;
2476 Probability.convert(llvm::APFloat::IEEEdouble(),
2477 llvm::RoundingMode::Dynamic, &LoseInfo);
2478 if (!(Probability >= llvm::APFloat(0.0) &&
2479 Probability <= llvm::APFloat(1.0))) {
2486 case Builtin::BI__builtin_preserve_access_index:
2490 case Builtin::BI__builtin_call_with_static_chain:
2494 case Builtin::BI__exception_code:
2495 case Builtin::BI_exception_code:
2497 diag::err_seh___except_block))
2500 case Builtin::BI__exception_info:
2501 case Builtin::BI_exception_info:
2503 diag::err_seh___except_filter))
2506 case Builtin::BI__GetExceptionInfo:
2518 case Builtin::BIaddressof:
2519 case Builtin::BI__addressof:
2520 case Builtin::BIforward:
2521 case Builtin::BIforward_like:
2522 case Builtin::BImove:
2523 case Builtin::BImove_if_noexcept:
2524 case Builtin::BIas_const: {
2532 bool ReturnsPointer = BuiltinID == Builtin::BIaddressof ||
2533 BuiltinID == Builtin::BI__addressof;
2535 (ReturnsPointer ?
Result->isAnyPointerType()
2536 :
Result->isReferenceType()) &&
2538 Result->getPointeeType()))) {
2539 Diag(TheCall->
getBeginLoc(), diag::err_builtin_move_forward_unsupported)
2545 case Builtin::BI__builtin_ptrauth_strip:
2547 case Builtin::BI__builtin_ptrauth_blend_discriminator:
2549 case Builtin::BI__builtin_ptrauth_sign_constant:
2552 case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
2555 case Builtin::BI__builtin_ptrauth_auth:
2558 case Builtin::BI__builtin_ptrauth_sign_generic_data:
2560 case Builtin::BI__builtin_ptrauth_auth_and_resign:
2562 case Builtin::BI__builtin_ptrauth_string_discriminator:
2565 case Builtin::BIread_pipe:
2566 case Builtin::BIwrite_pipe:
2569 if (
OpenCL().checkBuiltinRWPipe(TheCall))
2572 case Builtin::BIreserve_read_pipe:
2573 case Builtin::BIreserve_write_pipe:
2574 case Builtin::BIwork_group_reserve_read_pipe:
2575 case Builtin::BIwork_group_reserve_write_pipe:
2576 if (
OpenCL().checkBuiltinReserveRWPipe(TheCall))
2579 case Builtin::BIsub_group_reserve_read_pipe:
2580 case Builtin::BIsub_group_reserve_write_pipe:
2581 if (
OpenCL().checkSubgroupExt(TheCall) ||
2582 OpenCL().checkBuiltinReserveRWPipe(TheCall))
2585 case Builtin::BIcommit_read_pipe:
2586 case Builtin::BIcommit_write_pipe:
2587 case Builtin::BIwork_group_commit_read_pipe:
2588 case Builtin::BIwork_group_commit_write_pipe:
2589 if (
OpenCL().checkBuiltinCommitRWPipe(TheCall))
2592 case Builtin::BIsub_group_commit_read_pipe:
2593 case Builtin::BIsub_group_commit_write_pipe:
2594 if (
OpenCL().checkSubgroupExt(TheCall) ||
2595 OpenCL().checkBuiltinCommitRWPipe(TheCall))
2598 case Builtin::BIget_pipe_num_packets:
2599 case Builtin::BIget_pipe_max_packets:
2600 if (
OpenCL().checkBuiltinPipePackets(TheCall))
2603 case Builtin::BIto_global:
2604 case Builtin::BIto_local:
2605 case Builtin::BIto_private:
2606 if (
OpenCL().checkBuiltinToAddr(BuiltinID, TheCall))
2610 case Builtin::BIenqueue_kernel:
2611 if (
OpenCL().checkBuiltinEnqueueKernel(TheCall))
2614 case Builtin::BIget_kernel_work_group_size:
2615 case Builtin::BIget_kernel_preferred_work_group_size_multiple:
2616 if (
OpenCL().checkBuiltinKernelWorkGroupSize(TheCall))
2619 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
2620 case Builtin::BIget_kernel_sub_group_count_for_ndrange:
2621 if (
OpenCL().checkBuiltinNDRangeAndBlock(TheCall))
2624 case Builtin::BI__builtin_os_log_format:
2627 case Builtin::BI__builtin_os_log_format_buffer_size:
2628 if (BuiltinOSLogFormat(TheCall))
2631 case Builtin::BI__builtin_frame_address:
2632 case Builtin::BI__builtin_return_address: {
2641 Result.Val.getInt() != 0)
2643 << ((BuiltinID == Builtin::BI__builtin_return_address)
2644 ?
"__builtin_return_address"
2645 :
"__builtin_frame_address")
2650 case Builtin::BI__builtin_nondeterministic_value: {
2651 if (BuiltinNonDeterministicValue(TheCall))
2658 case Builtin::BI__builtin_elementwise_abs: {
2666 EltTy = VecTy->getElementType();
2669 diag::err_builtin_invalid_arg_type)
2678 case Builtin::BI__builtin_elementwise_acos:
2679 case Builtin::BI__builtin_elementwise_asin:
2680 case Builtin::BI__builtin_elementwise_atan:
2681 case Builtin::BI__builtin_elementwise_ceil:
2682 case Builtin::BI__builtin_elementwise_cos:
2683 case Builtin::BI__builtin_elementwise_cosh:
2684 case Builtin::BI__builtin_elementwise_exp:
2685 case Builtin::BI__builtin_elementwise_exp2:
2686 case Builtin::BI__builtin_elementwise_floor:
2687 case Builtin::BI__builtin_elementwise_log:
2688 case Builtin::BI__builtin_elementwise_log2:
2689 case Builtin::BI__builtin_elementwise_log10:
2690 case Builtin::BI__builtin_elementwise_roundeven:
2691 case Builtin::BI__builtin_elementwise_round:
2692 case Builtin::BI__builtin_elementwise_rint:
2693 case Builtin::BI__builtin_elementwise_nearbyint:
2694 case Builtin::BI__builtin_elementwise_sin:
2695 case Builtin::BI__builtin_elementwise_sinh:
2696 case Builtin::BI__builtin_elementwise_sqrt:
2697 case Builtin::BI__builtin_elementwise_tan:
2698 case Builtin::BI__builtin_elementwise_tanh:
2699 case Builtin::BI__builtin_elementwise_trunc:
2700 case Builtin::BI__builtin_elementwise_canonicalize: {
2710 case Builtin::BI__builtin_elementwise_fma: {
2718 case Builtin::BI__builtin_elementwise_pow: {
2719 if (BuiltinElementwiseMath(TheCall))
2733 case Builtin::BI__builtin_elementwise_add_sat:
2734 case Builtin::BI__builtin_elementwise_sub_sat: {
2735 if (BuiltinElementwiseMath(TheCall))
2743 EltTy = VecTy->getElementType();
2753 case Builtin::BI__builtin_elementwise_min:
2754 case Builtin::BI__builtin_elementwise_max:
2755 if (BuiltinElementwiseMath(TheCall))
2759 case Builtin::BI__builtin_elementwise_bitreverse: {
2768 EltTy = VecTy->getElementType();
2778 case Builtin::BI__builtin_elementwise_copysign: {
2798 diag::err_typecheck_call_different_arg_types)
2799 << MagnitudeTy << SignTy;
2807 case Builtin::BI__builtin_reduce_max:
2808 case Builtin::BI__builtin_reduce_min: {
2809 if (PrepareBuiltinReduceMathOneArgCall(TheCall))
2817 ElTy = TyA->getElementType();
2821 if (ElTy.isNull()) {
2833 case Builtin::BI__builtin_reduce_add:
2834 case Builtin::BI__builtin_reduce_mul:
2835 case Builtin::BI__builtin_reduce_xor:
2836 case Builtin::BI__builtin_reduce_or:
2837 case Builtin::BI__builtin_reduce_and: {
2838 if (PrepareBuiltinReduceMathOneArgCall(TheCall))
2846 ElTy = TyA->getElementType();
2850 if (ElTy.isNull() || !ElTy->isIntegerType()) {
2860 case Builtin::BI__builtin_matrix_transpose:
2861 return BuiltinMatrixTranspose(TheCall, TheCallResult);
2863 case Builtin::BI__builtin_matrix_column_major_load:
2864 return BuiltinMatrixColumnMajorLoad(TheCall, TheCallResult);
2866 case Builtin::BI__builtin_matrix_column_major_store:
2867 return BuiltinMatrixColumnMajorStore(TheCall, TheCallResult);
2869 case Builtin::BI__builtin_verbose_trap:
2874 case Builtin::BI__builtin_get_device_side_mangled_name: {
2875 auto Check = [](
CallExpr *TheCall) {
2881 auto *
D = DRE->getDecl();
2882 if (!isa<FunctionDecl>(
D) && !isa<VarDecl>(
D))
2887 if (!Check(TheCall)) {
2889 diag::err_hip_invalid_args_builtin_mangled_name);
2894 case Builtin::BI__builtin_popcountg:
2898 case Builtin::BI__builtin_clzg:
2899 case Builtin::BI__builtin_ctzg:
2904 case Builtin::BI__builtin_allow_runtime_check: {
2924 "Aux Target Builtin, but not an aux target?");
2926 if (CheckTSBuiltinFunctionCall(
2937 return TheCallResult;
2952 if (
Result.isShiftedMask() || (~
Result).isShiftedMask())
2956 diag::err_argument_not_contiguous_bit_field)
2962 if (Format->getFirstArg() == 0)
2964 else if (IsVariadic)
2968 FSI->
FormatIdx = Format->getFormatIdx() - 1;
2992 if (isa<CXXNullPtrLiteralExpr>(
3007 if (
const auto *CLE = dyn_cast<CompoundLiteralExpr>(
Expr))
3008 if (
const auto *ILE = dyn_cast<InitListExpr>(CLE->getInitializer()))
3009 Expr = ILE->getInit(0);
3019 const Expr *ArgExpr,
3023 S.
PDiag(diag::warn_null_arg)
3029 if (
auto nullability =
type->getNullability())
3040 assert((FDecl || Proto) &&
"Need a function declaration or prototype");
3046 llvm::SmallBitVector NonNullArgs;
3052 for (
const auto *Arg : Args)
3059 unsigned IdxAST = Idx.getASTIndex();
3060 if (IdxAST >= Args.size())
3062 if (NonNullArgs.empty())
3063 NonNullArgs.resize(Args.size());
3064 NonNullArgs.set(IdxAST);
3069 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) {
3073 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
3076 parms = cast<ObjCMethodDecl>(FDecl)->parameters();
3078 unsigned ParamIndex = 0;
3080 I !=
E; ++I, ++ParamIndex) {
3083 if (NonNullArgs.empty())
3084 NonNullArgs.resize(Args.size());
3086 NonNullArgs.set(ParamIndex);
3093 if (
const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
3098 type = blockType->getPointeeType();
3112 if (NonNullArgs.empty())
3113 NonNullArgs.resize(Args.size());
3115 NonNullArgs.set(Index);
3124 for (
unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
3125 ArgIndex != ArgIndexEnd; ++ArgIndex) {
3126 if (NonNullArgs[ArgIndex])
3132 StringRef ParamName,
QualType ArgTy,
3160 if (ArgAlign < ParamAlign)
3161 Diag(
Loc, diag::warn_param_mismatched_alignment)
3163 << ParamName << (FDecl !=
nullptr) << FDecl;
3175 llvm::SmallBitVector CheckedVarArgs;
3179 CheckedVarArgs.resize(Args.size());
3181 CheckFormatArguments(I, Args, IsMemberFunction, CallType,
Loc,
Range,
3188 auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl);
3192 : isa_and_nonnull<FunctionDecl>(FDecl)
3193 ? cast<FunctionDecl>(FDecl)->getNumParams()
3194 : isa_and_nonnull<ObjCMethodDecl>(FDecl)
3195 ? cast<ObjCMethodDecl>(FDecl)->param_size()
3198 for (
unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) {
3200 if (
const Expr *Arg = Args[ArgIdx]) {
3201 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx])
3207 if (FDecl || Proto) {
3212 for (
const auto *I : FDecl->
specific_attrs<ArgumentWithTypeTagAttr>())
3213 CheckArgumentWithTypeTag(I, Args,
Loc);
3219 if (!Proto && FDecl) {
3221 if (isa_and_nonnull<FunctionProtoType>(FT))
3227 const auto N = std::min<unsigned>(Proto->
getNumParams(), Args.size());
3229 bool IsScalableArg =
false;
3230 for (
unsigned ArgIdx = 0; ArgIdx < N; ++ArgIdx) {
3232 if (
const Expr *Arg = Args[ArgIdx]) {
3244 IsScalableArg =
true;
3246 CheckArgAlignment(Arg->
getExprLoc(), FDecl, std::to_string(ArgIdx + 1),
3255 if (
auto *CallerFD = dyn_cast<FunctionDecl>(
CurContext)) {
3256 llvm::StringMap<bool> CallerFeatureMap;
3258 if (!CallerFeatureMap.contains(
"sme"))
3259 Diag(
Loc, diag::err_sme_call_in_non_sme_target);
3261 Diag(
Loc, diag::err_sme_call_in_non_sme_target);
3268 const auto *CallerFD = dyn_cast<FunctionDecl>(
CurContext);
3270 (IsScalableArg || IsScalableRet)) {
3271 bool IsCalleeStreaming =
3273 bool IsCalleeStreamingCompatible =
3277 if (!IsCalleeStreamingCompatible &&
3281 Diag(
Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming)
3284 Diag(
Loc, diag::warn_sme_streaming_pass_return_vl_to_non_streaming)
3295 bool CallerHasZAState =
false;
3296 bool CallerHasZT0State =
false;
3298 auto *
Attr = CallerFD->getAttr<ArmNewAttr>();
3300 CallerHasZAState =
true;
3302 CallerHasZT0State =
true;
3306 FPT->getExtProtoInfo().AArch64SMEAttributes) !=
3308 CallerHasZT0State |=
3310 FPT->getExtProtoInfo().AArch64SMEAttributes) !=
3316 Diag(
Loc, diag::err_sme_za_call_no_za_state);
3319 Diag(
Loc, diag::err_sme_zt0_call_no_zt0_state);
3323 Diag(
Loc, diag::err_sme_unimplemented_za_save_restore);
3324 Diag(
Loc, diag::note_sme_use_preserves_za);
3329 if (FDecl && FDecl->
hasAttr<AllocAlignAttr>()) {
3330 auto *AA = FDecl->
getAttr<AllocAlignAttr>();
3331 const Expr *Arg = Args[AA->getParamIndex().getASTIndex()];
3332 if (!Arg->isValueDependent()) {
3334 if (Arg->EvaluateAsInt(Align,
Context)) {
3335 const llvm::APSInt &I = Align.
Val.
getInt();
3336 if (!I.isPowerOf2())
3337 Diag(Arg->getExprLoc(), diag::warn_alignment_not_power_of_two)
3338 << Arg->getSourceRange();
3341 Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great)
3364 auto *Ctor = cast<CXXConstructorDecl>(FDecl);
3369 checkCall(FDecl, Proto,
nullptr, Args,
true,
3375 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
3376 isa<CXXMethodDecl>(FDecl);
3377 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
3378 IsMemberOperatorCall;
3384 Expr *ImplicitThis =
nullptr;
3389 ImplicitThis = Args[0];
3392 }
else if (IsMemberFunction && !FDecl->
isStatic() &&
3395 cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument();
3407 cast<CXXMethodDecl>(FDecl)->getFunctionObjectParameterType());
3409 CheckArgAlignment(TheCall->
getRParenLoc(), FDecl,
"'this'", ThisType,
3427 CheckAbsoluteValueFunction(TheCall, FDecl);
3428 CheckMaxUnsignedZero(TheCall, FDecl);
3429 CheckInfNaNFunction(TheCall, FDecl);
3440 case Builtin::BIstrlcpy:
3441 case Builtin::BIstrlcat:
3442 CheckStrlcpycatArguments(TheCall, FnInfo);
3444 case Builtin::BIstrncat:
3445 CheckStrncatArguments(TheCall, FnInfo);
3447 case Builtin::BIfree:
3448 CheckFreeArguments(TheCall);
3451 CheckMemaccessArguments(TheCall, CMId, FnInfo);
3460 if (
const auto *
V = dyn_cast<VarDecl>(NDecl))
3461 Ty =
V->getType().getNonReferenceType();
3462 else if (
const auto *F = dyn_cast<FieldDecl>(NDecl))
3463 Ty = F->getType().getNonReferenceType();
3500 if (!llvm::isValidAtomicOrderingCABI(Ordering))
3503 auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering;
3505 case AtomicExpr::AO__c11_atomic_init:
3506 case AtomicExpr::AO__opencl_atomic_init:
3507 llvm_unreachable(
"There is no ordering argument for an init");
3509 case AtomicExpr::AO__c11_atomic_load:
3510 case AtomicExpr::AO__opencl_atomic_load:
3511 case AtomicExpr::AO__hip_atomic_load:
3512 case AtomicExpr::AO__atomic_load_n:
3513 case AtomicExpr::AO__atomic_load:
3514 case AtomicExpr::AO__scoped_atomic_load_n:
3515 case AtomicExpr::AO__scoped_atomic_load:
3516 return OrderingCABI != llvm::AtomicOrderingCABI::release &&
3517 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
3519 case AtomicExpr::AO__c11_atomic_store:
3520 case AtomicExpr::AO__opencl_atomic_store:
3521 case AtomicExpr::AO__hip_atomic_store:
3522 case AtomicExpr::AO__atomic_store:
3523 case AtomicExpr::AO__atomic_store_n:
3524 case AtomicExpr::AO__scoped_atomic_store:
3525 case AtomicExpr::AO__scoped_atomic_store_n:
3526 return OrderingCABI != llvm::AtomicOrderingCABI::consume &&
3527 OrderingCABI != llvm::AtomicOrderingCABI::acquire &&
3528 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
3537 CallExpr *TheCall = cast<CallExpr>(TheCallResult.
get());
3581 const unsigned NumForm = GNUCmpXchg + 1;
3582 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 };
3583 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 };
3591 static_assert(
sizeof(NumArgs)/
sizeof(NumArgs[0]) == NumForm
3592 &&
sizeof(NumVals)/
sizeof(NumVals[0]) == NumForm,
3593 "need to update code for modified forms");
3594 static_assert(AtomicExpr::AO__atomic_add_fetch == 0 &&
3595 AtomicExpr::AO__atomic_xor_fetch + 1 ==
3596 AtomicExpr::AO__c11_atomic_compare_exchange_strong,
3597 "need to update code for modified C11 atomics");
3598 bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_compare_exchange_strong &&
3599 Op <= AtomicExpr::AO__opencl_atomic_store;
3600 bool IsHIP = Op >= AtomicExpr::AO__hip_atomic_compare_exchange_strong &&
3601 Op <= AtomicExpr::AO__hip_atomic_store;
3602 bool IsScoped = Op >= AtomicExpr::AO__scoped_atomic_add_fetch &&
3603 Op <= AtomicExpr::AO__scoped_atomic_xor_fetch;
3604 bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_compare_exchange_strong &&
3605 Op <= AtomicExpr::AO__c11_atomic_store) ||
3607 bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
3608 Op == AtomicExpr::AO__atomic_store_n ||
3609 Op == AtomicExpr::AO__atomic_exchange_n ||
3610 Op == AtomicExpr::AO__atomic_compare_exchange_n ||
3611 Op == AtomicExpr::AO__scoped_atomic_load_n ||
3612 Op == AtomicExpr::AO__scoped_atomic_store_n ||
3613 Op == AtomicExpr::AO__scoped_atomic_exchange_n ||
3614 Op == AtomicExpr::AO__scoped_atomic_compare_exchange_n;
3618 enum ArithOpExtraValueType {
3623 unsigned ArithAllows = AOEVT_None;
3626 case AtomicExpr::AO__c11_atomic_init:
3627 case AtomicExpr::AO__opencl_atomic_init:
3631 case AtomicExpr::AO__c11_atomic_load:
3632 case AtomicExpr::AO__opencl_atomic_load:
3633 case AtomicExpr::AO__hip_atomic_load:
3634 case AtomicExpr::AO__atomic_load_n:
3635 case AtomicExpr::AO__scoped_atomic_load_n:
3639 case AtomicExpr::AO__atomic_load:
3640 case AtomicExpr::AO__scoped_atomic_load:
3644 case AtomicExpr::AO__c11_atomic_store:
3645 case AtomicExpr::AO__opencl_atomic_store:
3646 case AtomicExpr::AO__hip_atomic_store:
3647 case AtomicExpr::AO__atomic_store:
3648 case AtomicExpr::AO__atomic_store_n:
3649 case AtomicExpr::AO__scoped_atomic_store:
3650 case AtomicExpr::AO__scoped_atomic_store_n:
3653 case AtomicExpr::AO__atomic_fetch_add:
3654 case AtomicExpr::AO__atomic_fetch_sub:
3655 case AtomicExpr::AO__atomic_add_fetch:
3656 case AtomicExpr::AO__atomic_sub_fetch:
3657 case AtomicExpr::AO__scoped_atomic_fetch_add:
3658 case AtomicExpr::AO__scoped_atomic_fetch_sub:
3659 case AtomicExpr::AO__scoped_atomic_add_fetch:
3660 case AtomicExpr::AO__scoped_atomic_sub_fetch:
3661 case AtomicExpr::AO__c11_atomic_fetch_add:
3662 case AtomicExpr::AO__c11_atomic_fetch_sub:
3663 case AtomicExpr::AO__opencl_atomic_fetch_add:
3664 case AtomicExpr::AO__opencl_atomic_fetch_sub:
3665 case AtomicExpr::AO__hip_atomic_fetch_add:
3666 case AtomicExpr::AO__hip_atomic_fetch_sub:
3667 ArithAllows = AOEVT_Pointer | AOEVT_FP;
3670 case AtomicExpr::AO__atomic_fetch_max:
3671 case AtomicExpr::AO__atomic_fetch_min:
3672 case AtomicExpr::AO__atomic_max_fetch:
3673 case AtomicExpr::AO__atomic_min_fetch:
3674 case AtomicExpr::AO__scoped_atomic_fetch_max:
3675 case AtomicExpr::AO__scoped_atomic_fetch_min:
3676 case AtomicExpr::AO__scoped_atomic_max_fetch:
3677 case AtomicExpr::AO__scoped_atomic_min_fetch:
3678 case AtomicExpr::AO__c11_atomic_fetch_max:
3679 case AtomicExpr::AO__c11_atomic_fetch_min:
3680 case AtomicExpr::AO__opencl_atomic_fetch_max:
3681 case AtomicExpr::AO__opencl_atomic_fetch_min:
3682 case AtomicExpr::AO__hip_atomic_fetch_max:
3683 case AtomicExpr::AO__hip_atomic_fetch_min:
3684 ArithAllows = AOEVT_FP;
3687 case AtomicExpr::AO__c11_atomic_fetch_and:
3688 case AtomicExpr::AO__c11_atomic_fetch_or:
3689 case AtomicExpr::AO__c11_atomic_fetch_xor:
3690 case AtomicExpr::AO__hip_atomic_fetch_and:
3691 case AtomicExpr::AO__hip_atomic_fetch_or:
3692 case AtomicExpr::AO__hip_atomic_fetch_xor:
3693 case AtomicExpr::AO__c11_atomic_fetch_nand:
3694 case AtomicExpr::AO__opencl_atomic_fetch_and:
3695 case AtomicExpr::AO__opencl_atomic_fetch_or:
3696 case AtomicExpr::AO__opencl_atomic_fetch_xor:
3697 case AtomicExpr::AO__atomic_fetch_and:
3698 case AtomicExpr::AO__atomic_fetch_or:
3699 case AtomicExpr::AO__atomic_fetch_xor:
3700 case AtomicExpr::AO__atomic_fetch_nand:
3701 case AtomicExpr::AO__atomic_and_fetch:
3702 case AtomicExpr::AO__atomic_or_fetch:
3703 case AtomicExpr::AO__atomic_xor_fetch:
3704 case AtomicExpr::AO__atomic_nand_fetch:
3705 case AtomicExpr::AO__scoped_atomic_fetch_and:
3706 case AtomicExpr::AO__scoped_atomic_fetch_or:
3707 case AtomicExpr::AO__scoped_atomic_fetch_xor:
3708 case AtomicExpr::AO__scoped_atomic_fetch_nand:
3709 case AtomicExpr::AO__scoped_atomic_and_fetch:
3710 case AtomicExpr::AO__scoped_atomic_or_fetch:
3711 case AtomicExpr::AO__scoped_atomic_xor_fetch:
3712 case AtomicExpr::AO__scoped_atomic_nand_fetch:
3716 case AtomicExpr::AO__c11_atomic_exchange:
3717 case AtomicExpr::AO__hip_atomic_exchange:
3718 case AtomicExpr::AO__opencl_atomic_exchange:
3719 case AtomicExpr::AO__atomic_exchange_n:
3720 case AtomicExpr::AO__scoped_atomic_exchange_n:
3724 case AtomicExpr::AO__atomic_exchange:
3725 case AtomicExpr::AO__scoped_atomic_exchange:
3729 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
3730 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
3731 case AtomicExpr::AO__hip_atomic_compare_exchange_strong:
3732 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
3733 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
3734 case AtomicExpr::AO__hip_atomic_compare_exchange_weak:
3738 case AtomicExpr::AO__atomic_compare_exchange:
3739 case AtomicExpr::AO__atomic_compare_exchange_n:
3740 case AtomicExpr::AO__scoped_atomic_compare_exchange:
3741 case AtomicExpr::AO__scoped_atomic_compare_exchange_n:
3746 unsigned AdjustedNumArgs = NumArgs[Form];
3747 if ((IsOpenCL || IsHIP || IsScoped) &&
3748 Op != AtomicExpr::AO__opencl_atomic_init)
3751 if (Args.size() < AdjustedNumArgs) {
3752 Diag(CallRange.
getEnd(), diag::err_typecheck_call_too_few_args)
3753 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
3756 }
else if (Args.size() > AdjustedNumArgs) {
3757 Diag(Args[AdjustedNumArgs]->getBeginLoc(),
3758 diag::err_typecheck_call_too_many_args)
3759 << 0 << AdjustedNumArgs << static_cast<unsigned>(Args.size())
3765 Expr *Ptr = Args[0];
3770 Ptr = ConvertedPtr.
get();
3773 Diag(ExprRange.
getBegin(), diag::err_atomic_builtin_must_be_pointer)
3783 Diag(ExprRange.
getBegin(), diag::err_atomic_op_needs_atomic)
3789 Diag(ExprRange.
getBegin(), diag::err_atomic_op_needs_non_const_atomic)
3795 }
else if (Form != Load && Form != LoadCopy) {
3797 Diag(ExprRange.
getBegin(), diag::err_atomic_op_needs_non_const_pointer)
3805 diag::err_incomplete_type))
3808 Diag(ExprRange.
getBegin(), diag::err_atomic_builtin_must_be_pointer)
3814 if (Form == Arithmetic) {
3817 auto IsAllowedValueType = [&](
QualType ValType,
3818 unsigned AllowedType) ->
bool {
3822 return AllowedType & AOEVT_Pointer;
3828 &llvm::APFloat::x87DoubleExtended())
3832 if (!IsAllowedValueType(ValType, ArithAllows)) {
3833 auto DID = ArithAllows & AOEVT_FP
3834 ? (ArithAllows & AOEVT_Pointer
3835 ? diag::err_atomic_op_needs_atomic_int_ptr_or_fp
3836 : diag::err_atomic_op_needs_atomic_int_or_fp)
3837 : diag::err_atomic_op_needs_atomic_int;
3844 diag::err_incomplete_type)) {
3850 Diag(ExprRange.
getBegin(), diag::err_atomic_op_needs_atomic_int_or_ptr)
3861 Diag(ExprRange.
getBegin(), diag::err_atomic_op_needs_trivial_copy)
3877 Diag(ExprRange.
getBegin(), diag::err_arc_atomic_ownership)
3889 if (Form ==
Copy || Form == LoadCopy || Form == GNUXchg ||
3892 else if (Form == C11CmpXchg || Form == GNUCmpXchg)
3898 bool IsPassedByAddress =
false;
3899 if (!IsC11 && !IsHIP && !IsN) {
3901 IsPassedByAddress =
true;
3906 APIOrderedArgs.push_back(Args[0]);
3910 APIOrderedArgs.push_back(Args[1]);
3916 APIOrderedArgs.push_back(Args[2]);
3917 APIOrderedArgs.push_back(Args[1]);
3920 APIOrderedArgs.push_back(Args[2]);
3921 APIOrderedArgs.push_back(Args[3]);
3922 APIOrderedArgs.push_back(Args[1]);
3925 APIOrderedArgs.push_back(Args[2]);
3926 APIOrderedArgs.push_back(Args[4]);
3927 APIOrderedArgs.push_back(Args[1]);
3928 APIOrderedArgs.push_back(Args[3]);
3931 APIOrderedArgs.push_back(Args[2]);
3932 APIOrderedArgs.push_back(Args[4]);
3933 APIOrderedArgs.push_back(Args[5]);
3934 APIOrderedArgs.push_back(Args[1]);
3935 APIOrderedArgs.push_back(Args[3]);
3939 APIOrderedArgs.append(Args.begin(), Args.end());
3946 for (
unsigned i = 0; i != APIOrderedArgs.size(); ++i) {
3948 if (i < NumVals[Form] + 1) {
3961 assert(Form != Load);
3964 else if (Form ==
Init || Form == Arithmetic)
3966 else if (Form ==
Copy || Form == Xchg) {
3967 if (IsPassedByAddress) {
3974 Expr *ValArg = APIOrderedArgs[i];
3981 AS = PtrTy->getPointeeType().getAddressSpace();
3990 if (IsPassedByAddress)
4010 APIOrderedArgs[i] = Arg.
get();
4015 SubExprs.push_back(Ptr);
4019 SubExprs.push_back(APIOrderedArgs[1]);
4022 SubExprs.push_back(APIOrderedArgs[1]);
4028 SubExprs.push_back(APIOrderedArgs[2]);
4029 SubExprs.push_back(APIOrderedArgs[1]);
4033 SubExprs.push_back(APIOrderedArgs[3]);
4034 SubExprs.push_back(APIOrderedArgs[1]);
4035 SubExprs.push_back(APIOrderedArgs[2]);
4038 SubExprs.push_back(APIOrderedArgs[3]);
4039 SubExprs.push_back(APIOrderedArgs[1]);
4040 SubExprs.push_back(APIOrderedArgs[4]);
4041 SubExprs.push_back(APIOrderedArgs[2]);
4044 SubExprs.push_back(APIOrderedArgs[4]);
4045 SubExprs.push_back(APIOrderedArgs[1]);
4046 SubExprs.push_back(APIOrderedArgs[5]);
4047 SubExprs.push_back(APIOrderedArgs[2]);
4048 SubExprs.push_back(APIOrderedArgs[3]);
4053 if (SubExprs.size() >= 2 && Form !=
Init) {
4054 std::optional<llvm::APSInt>
Success =
4055 SubExprs[1]->getIntegerConstantExpr(
Context);
4057 Diag(SubExprs[1]->getBeginLoc(),
4058 diag::warn_atomic_op_has_invalid_memory_order)
4059 << (Form == C11CmpXchg || Form == GNUCmpXchg)
4060 << SubExprs[1]->getSourceRange();
4062 if (SubExprs.size() >= 5) {
4063 if (std::optional<llvm::APSInt> Failure =
4064 SubExprs[3]->getIntegerConstantExpr(
Context)) {
4065 if (!llvm::is_contained(
4066 {llvm::AtomicOrderingCABI::relaxed,
4067 llvm::AtomicOrderingCABI::consume,
4068 llvm::AtomicOrderingCABI::acquire,
4069 llvm::AtomicOrderingCABI::seq_cst},
4070 (llvm::AtomicOrderingCABI)Failure->getSExtValue())) {
4071 Diag(SubExprs[3]->getBeginLoc(),
4072 diag::warn_atomic_op_has_invalid_memory_order)
4073 << 2 << SubExprs[3]->getSourceRange();
4080 auto *
Scope = Args[Args.size() - 1];
4081 if (std::optional<llvm::APSInt>
Result =
4083 if (!ScopeModel->isValid(
Result->getZExtValue()))
4084 Diag(
Scope->getBeginLoc(), diag::err_atomic_op_has_invalid_synch_scope)
4085 <<
Scope->getSourceRange();
4087 SubExprs.push_back(
Scope);
4093 if ((Op == AtomicExpr::AO__c11_atomic_load ||
4094 Op == AtomicExpr::AO__c11_atomic_store ||
4095 Op == AtomicExpr::AO__opencl_atomic_load ||
4096 Op == AtomicExpr::AO__hip_atomic_load ||
4097 Op == AtomicExpr::AO__opencl_atomic_store ||
4098 Op == AtomicExpr::AO__hip_atomic_store) &&
4101 << ((Op == AtomicExpr::AO__c11_atomic_load ||
4102 Op == AtomicExpr::AO__opencl_atomic_load ||
4103 Op == AtomicExpr::AO__hip_atomic_load)
4108 Diag(Ptr->
getExprLoc(), diag::err_atomic_builtin_bit_int_prohibit);
4124 assert(Fn &&
"builtin call without direct callee!");
4135 E->setArg(ArgIndex, Arg.
get());
4147 Diag(TheCall->
getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
4149 <<
Callee->getSourceRange();
4162 FirstArg = FirstArgResult.
get();
4163 TheCall->
setArg(0, FirstArg);
4175 Diag(DRE->
getBeginLoc(), diag::err_atomic_builtin_must_be_pointer_intptr)
4210#define BUILTIN_ROW(x) \
4211 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
4212 Builtin::BI##x##_8, Builtin::BI##x##_16 }
4214 static const unsigned BuiltinIndices[][5] = {
4240 case 1: SizeIndex = 0;
break;
4241 case 2: SizeIndex = 1;
break;
4242 case 4: SizeIndex = 2;
break;
4243 case 8: SizeIndex = 3;
break;
4244 case 16: SizeIndex = 4;
break;
4256 unsigned BuiltinIndex, NumFixed = 1;
4257 bool WarnAboutSemanticsChange =
false;
4258 switch (BuiltinID) {
4259 default: llvm_unreachable(
"Unknown overloaded atomic builtin!");
4260 case Builtin::BI__sync_fetch_and_add:
4261 case Builtin::BI__sync_fetch_and_add_1:
4262 case Builtin::BI__sync_fetch_and_add_2:
4263 case Builtin::BI__sync_fetch_and_add_4:
4264 case Builtin::BI__sync_fetch_and_add_8:
4265 case Builtin::BI__sync_fetch_and_add_16:
4269 case Builtin::BI__sync_fetch_and_sub:
4270 case Builtin::BI__sync_fetch_and_sub_1:
4271 case Builtin::BI__sync_fetch_and_sub_2:
4272 case Builtin::BI__sync_fetch_and_sub_4:
4273 case Builtin::BI__sync_fetch_and_sub_8:
4274 case Builtin::BI__sync_fetch_and_sub_16:
4278 case Builtin::BI__sync_fetch_and_or:
4279 case Builtin::BI__sync_fetch_and_or_1:
4280 case Builtin::BI__sync_fetch_and_or_2:
4281 case Builtin::BI__sync_fetch_and_or_4:
4282 case Builtin::BI__sync_fetch_and_or_8:
4283 case Builtin::BI__sync_fetch_and_or_16:
4287 case Builtin::BI__sync_fetch_and_and:
4288 case Builtin::BI__sync_fetch_and_and_1:
4289 case Builtin::BI__sync_fetch_and_and_2:
4290 case Builtin::BI__sync_fetch_and_and_4:
4291 case Builtin::BI__sync_fetch_and_and_8:
4292 case Builtin::BI__sync_fetch_and_and_16:
4296 case Builtin::BI__sync_fetch_and_xor:
4297 case Builtin::BI__sync_fetch_and_xor_1:
4298 case Builtin::BI__sync_fetch_and_xor_2:
4299 case Builtin::BI__sync_fetch_and_xor_4:
4300 case Builtin::BI__sync_fetch_and_xor_8:
4301 case Builtin::BI__sync_fetch_and_xor_16:
4305 case Builtin::BI__sync_fetch_and_nand:
4306 case Builtin::BI__sync_fetch_and_nand_1:
4307 case Builtin::BI__sync_fetch_and_nand_2:
4308 case Builtin::BI__sync_fetch_and_nand_4:
4309 case Builtin::BI__sync_fetch_and_nand_8:
4310 case Builtin::BI__sync_fetch_and_nand_16:
4312 WarnAboutSemanticsChange =
true;
4315 case Builtin::BI__sync_add_and_fetch:
4316 case Builtin::BI__sync_add_and_fetch_1:
4317 case Builtin::BI__sync_add_and_fetch_2:
4318 case Builtin::BI__sync_add_and_fetch_4:
4319 case Builtin::BI__sync_add_and_fetch_8:
4320 case Builtin::BI__sync_add_and_fetch_16:
4324 case Builtin::BI__sync_sub_and_fetch:
4325 case Builtin::BI__sync_sub_and_fetch_1:
4326 case Builtin::BI__sync_sub_and_fetch_2:
4327 case Builtin::BI__sync_sub_and_fetch_4:
4328 case Builtin::BI__sync_sub_and_fetch_8:
4329 case Builtin::BI__sync_sub_and_fetch_16:
4333 case Builtin::BI__sync_and_and_fetch:
4334 case Builtin::BI__sync_and_and_fetch_1:
4335 case Builtin::BI__sync_and_and_fetch_2:
4336 case Builtin::BI__sync_and_and_fetch_4:
4337 case Builtin::BI__sync_and_and_fetch_8:
4338 case Builtin::BI__sync_and_and_fetch_16:
4342 case Builtin::BI__sync_or_and_fetch:
4343 case Builtin::BI__sync_or_and_fetch_1:
4344 case Builtin::BI__sync_or_and_fetch_2:
4345 case Builtin::BI__sync_or_and_fetch_4:
4346 case Builtin::BI__sync_or_and_fetch_8:
4347 case Builtin::BI__sync_or_and_fetch_16:
4351 case Builtin::BI__sync_xor_and_fetch:
4352 case Builtin::BI__sync_xor_and_fetch_1:
4353 case Builtin::BI__sync_xor_and_fetch_2:
4354 case Builtin::BI__sync_xor_and_fetch_4:
4355 case Builtin::BI__sync_xor_and_fetch_8:
4356 case Builtin::BI__sync_xor_and_fetch_16:
4360 case Builtin::BI__sync_nand_and_fetch:
4361 case Builtin::BI__sync_nand_and_fetch_1:
4362 case Builtin::BI__sync_nand_and_fetch_2:
4363 case Builtin::BI__sync_nand_and_fetch_4:
4364 case Builtin::BI__sync_nand_and_fetch_8:
4365 case Builtin::BI__sync_nand_and_fetch_16:
4367 WarnAboutSemanticsChange =
true;
4370 case Builtin::BI__sync_val_compare_and_swap:
4371 case Builtin::BI__sync_val_compare_and_swap_1:
4372 case Builtin::BI__sync_val_compare_and_swap_2:
4373 case Builtin::BI__sync_val_compare_and_swap_4:
4374 case Builtin::BI__sync_val_compare_and_swap_8:
4375 case Builtin::BI__sync_val_compare_and_swap_16:
4380 case Builtin::BI__sync_bool_compare_and_swap:
4381 case Builtin::BI__sync_bool_compare_and_swap_1:
4382 case Builtin::BI__sync_bool_compare_and_swap_2:
4383 case Builtin::BI__sync_bool_compare_and_swap_4:
4384 case Builtin::BI__sync_bool_compare_and_swap_8:
4385 case Builtin::BI__sync_bool_compare_and_swap_16:
4391 case Builtin::BI__sync_lock_test_and_set:
4392 case Builtin::BI__sync_lock_test_and_set_1:
4393 case Builtin::BI__sync_lock_test_and_set_2:
4394 case Builtin::BI__sync_lock_test_and_set_4:
4395 case Builtin::BI__sync_lock_test_and_set_8:
4396 case Builtin::BI__sync_lock_test_and_set_16:
4400 case Builtin::BI__sync_lock_release:
4401 case Builtin::BI__sync_lock_release_1:
4402 case Builtin::BI__sync_lock_release_2:
4403 case Builtin::BI__sync_lock_release_4:
4404 case Builtin::BI__sync_lock_release_8:
4405 case Builtin::BI__sync_lock_release_16:
4411 case Builtin::BI__sync_swap:
4412 case Builtin::BI__sync_swap_1:
4413 case Builtin::BI__sync_swap_2:
4414 case Builtin::BI__sync_swap_4:
4415 case Builtin::BI__sync_swap_8:
4416 case Builtin::BI__sync_swap_16:
4424 Diag(TheCall->
getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
4425 << 0 << 1 + NumFixed << TheCall->
getNumArgs() << 0
4426 <<
Callee->getSourceRange();
4430 Diag(TheCall->
getEndLoc(), diag::warn_atomic_implicit_seq_cst)
4431 <<
Callee->getSourceRange();
4433 if (WarnAboutSemanticsChange) {
4434 Diag(TheCall->
getEndLoc(), diag::warn_sync_fetch_and_nand_semantics_change)
4435 <<
Callee->getSourceRange();
4440 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
4443 if (NewBuiltinID == BuiltinID)
4444 NewBuiltinDecl = FDecl;
4450 assert(Res.getFoundDecl());
4451 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
4452 if (!NewBuiltinDecl)
4459 for (
unsigned i = 0; i != NumFixed; ++i) {
4490 CK_BuiltinFnToFnPtr);
4502 if (BitIntValType && !llvm::isPowerOf2_64(BitIntValType->getNumBits())) {
4503 Diag(FirstArg->
getExprLoc(), diag::err_atomic_builtin_ext_int_size);
4507 return TheCallResult;
4516 assert((BuiltinID == Builtin::BI__builtin_nontemporal_store ||
4517 BuiltinID == Builtin::BI__builtin_nontemporal_load) &&
4518 "Unexpected nontemporal load/store builtin!");
4519 bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store;
4520 unsigned numArgs = isStore ? 2 : 1;
4530 Expr *PointerArg = TheCall->
getArg(numArgs - 1);
4536 PointerArg = PointerArgResult.
get();
4537 TheCall->
setArg(numArgs - 1, PointerArg);
4541 Diag(DRE->
getBeginLoc(), diag::err_nontemporal_builtin_must_be_pointer)
4554 diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector)
4561 return TheCallResult;
4573 return TheCallResult;
4580 auto *
Literal = dyn_cast<StringLiteral>(Arg);
4582 if (
auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) {
4583 Literal = ObjcLiteral->getString();
4587 if (!Literal || (!
Literal->isOrdinary() && !
Literal->isUTF8())) {
4605 bool IsX64 = TT.getArch() == llvm::Triple::x86_64;
4606 bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 ||
4607 TT.getArch() == llvm::Triple::aarch64_32);
4608 bool IsWindows = TT.isOSWindows();
4609 bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start;
4610 if (IsX64 || IsAArch64) {
4617 return S.
Diag(Fn->getBeginLoc(),
4618 diag::err_ms_va_start_used_in_sysv_function);
4626 return S.
Diag(Fn->getBeginLoc(),
4627 diag::err_va_start_used_in_wrong_abi_function)
4634 return S.
Diag(Fn->getBeginLoc(), diag::err_builtin_x64_aarch64_only);
4642 bool IsVariadic =
false;
4645 if (
auto *
Block = dyn_cast<BlockDecl>(Caller)) {
4646 IsVariadic =
Block->isVariadic();
4647 Params =
Block->parameters();
4648 }
else if (
auto *FD = dyn_cast<FunctionDecl>(Caller)) {
4651 }
else if (
auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) {
4652 IsVariadic = MD->isVariadic();
4654 Params = MD->parameters();
4655 }
else if (isa<CapturedDecl>(Caller)) {
4657 S.
Diag(Fn->getBeginLoc(), diag::err_va_start_captured_stmt);
4661 S.
Diag(Fn->getBeginLoc(), diag::err_va_start_outside_function);
4666 S.
Diag(Fn->getBeginLoc(), diag::err_va_start_fixed_function);
4671 *LastParam = Params.empty() ? nullptr : Params.back();
4676bool Sema::BuiltinVAStart(
unsigned BuiltinID,
CallExpr *TheCall) {
4701 bool SecondArgIsLastNamedArgument =
false;
4703 if (std::optional<llvm::APSInt> Val =
4712 bool IsCRegister =
false;
4714 if (
const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
4715 if (
const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
4716 SecondArgIsLastNamedArgument = PV == LastParam;
4718 Type = PV->getType();
4719 ParamLoc = PV->getLocation();
4725 if (!SecondArgIsLastNamedArgument)
4727 diag::warn_second_arg_of_va_start_not_last_named_param);
4732 if (!Context.isPromotableIntegerType(Type))
4734 if (!Type->isEnumeralType())
4736 const EnumDecl *ED = Type->castAs<EnumType>()->getDecl();
4738 Context.typesAreCompatible(ED->getPromotionType(), Type));
4740 unsigned Reason = 0;
4742 else if (IsCRegister) Reason = 2;
4743 Diag(Arg->
getBeginLoc(), diag::warn_va_start_type_is_undefined) << Reason;
4744 Diag(ParamLoc, diag::note_parameter_type) <<
Type;
4751 auto IsSuitablyTypedFormatArgument = [
this](
const Expr *Arg) ->
bool {
4771 if (
Call->getNumArgs() < 3)
4773 diag::err_typecheck_call_too_few_args_at_least)
4774 << 0 << 3 <<
Call->getNumArgs()
4787 const Expr *Arg1 =
Call->getArg(1)->IgnoreParens();
4790 const Expr *Arg2 =
Call->getArg(2)->IgnoreParens();
4795 if (!Arg1Ty->
isPointerType() || !IsSuitablyTypedFormatArgument(Arg1))
4797 << Arg1->
getType() << ConstCharPtrTy << 1
4800 << 2 << Arg1->
getType() << ConstCharPtrTy;
4805 << Arg2->
getType() << SizeTy << 1
4808 << 3 << Arg2->
getType() << SizeTy;
4813bool Sema::BuiltinUnorderedCompare(
CallExpr *TheCall,
unsigned BuiltinID) {
4817 if (BuiltinID == Builtin::BI__builtin_isunordered &&
4845 diag::err_typecheck_call_invalid_ordered_compare)
4853bool Sema::BuiltinFPClassification(
CallExpr *TheCall,
unsigned NumArgs,
4854 unsigned BuiltinID) {
4859 if (FPO.getNoHonorInfs() && (BuiltinID == Builtin::BI__builtin_isfinite ||
4860 BuiltinID == Builtin::BI__builtin_isinf ||
4861 BuiltinID == Builtin::BI__builtin_isinf_sign))
4865 if (FPO.getNoHonorNaNs() && (BuiltinID == Builtin::BI__builtin_isnan ||
4866 BuiltinID == Builtin::BI__builtin_isunordered))
4870 bool IsFPClass = NumArgs == 2;
4873 unsigned FPArgNo = IsFPClass ? 0 : NumArgs - 1;
4877 for (
unsigned i = 0; i < FPArgNo; ++i) {
4902 TheCall->
setArg(FPArgNo, OrigArg);
4916 diag::err_typecheck_call_invalid_unary_fp)
4928 if (!VectorResultTy.
isNull())
4929 ResultTy = VectorResultTy;
4938bool Sema::BuiltinComplex(
CallExpr *TheCall) {
4943 for (
unsigned I = 0; I != 2; ++I) {
4954 return Diag(Arg->
getBeginLoc(), diag::err_typecheck_call_requires_real_fp)
4973 diag::err_typecheck_call_different_arg_types)
4997 diag::err_typecheck_call_too_few_args_at_least)
5005 unsigned numElements = 0;
5020 unsigned numResElements = TheCall->
getNumArgs() - 2;
5029 diag::err_vec_builtin_incompatible_vector)
5036 diag::err_vec_builtin_incompatible_vector)
5041 }
else if (numElements != numResElements) {
5048 for (
unsigned i = 2; i < TheCall->
getNumArgs(); i++) {
5053 std::optional<llvm::APSInt>
Result;
5056 diag::err_shufflevector_nonconstant_argument)
5063 if (
Result->getActiveBits() > 64 ||
5064 Result->getZExtValue() >= numElements * 2)
5066 diag::err_shufflevector_argument_too_large)
5072 for (
unsigned i = 0, e = TheCall->
getNumArgs(); i != e; i++) {
5073 exprs.push_back(TheCall->
getArg(i));
5074 TheCall->
setArg(i,
nullptr);