[clang] [Sema] Check arg count for builtins with CustomTypeChecking (PR #222841)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 11 06:55:23 PDT 2026
https://github.com/im-lunex updated https://github.com/llvm/llvm-project/pull/222841
>From 98a6983e0cb9301fbd5c88ea6365828b0d91c6e2 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 11 Sep 2026 00:58:58 +0600
Subject: [PATCH 1/4] [Sema] Check arg count for builtins with
CustomTypeChecking
---
clang/lib/Sema/SemaChecking.cpp | 15 +++++++++++++
.../PowerPC/builtins-too-many-args-check.cpp | 22 +++++++++++++++++++
2 files changed, 37 insertions(+)
create mode 100644 clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 0e85af73696dc..cc68bf5ef5413 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4193,6 +4193,21 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
if (getLangOpts().HLSL && HLSL().CheckBuiltinFunctionCall(BuiltinID, TheCall))
return ExprError();
+ // check if the builtin has CustomTypeChecking or not, if it dose we get the
+ // already parsed type string of the builtin and compare that with the
+ // caller's passed args and give error for too much args
+ if (Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) {
+ ASTContext::GetBuiltinTypeError Error;
+ QualType BuiltinFTy = Context.GetBuiltinType(BuiltinID, Error);
+ if (!BuiltinFTy.isNull() && !Error) {
+ if (const FunctionProtoType *FPT =
+ dyn_cast<FunctionProtoType>(BuiltinFTy.getTypePtr()))
+ if (!FPT->isVariadic() &&
+ checkArgCountAtMost(TheCall, FPT->getNumParams()))
+ return ExprError();
+ }
+ }
+
// Since the target specific builtins for each arch overlap, only check those
// of the arch we are compiling for.
if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
diff --git a/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp b/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
new file mode 100644
index 0000000000000..15d30fd1dffe5
--- /dev/null
+++ b/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
@@ -0,0 +1,22 @@
+// Testfile for (https://github.com/llvm/llvm-project/issues/216669)
+
+// REQUIRES: powerpc-registered-target
+// RUN: %clang_cc1 -target-feature +power9-vector -triple powerpc64-unknown-unknown -fsyntax-only -verify %s
+// RUN: %clang_cc1 -target-feature +power9-vector -triple powerpc64le-unknown-unknown -fsyntax-only -verify %s
+// RUN: %clang_cc1 -target-feature +power9-vector -triple powerpc-unknown-unknown -fsyntax-only -verify %s
+
+vector unsigned char test_bcdsetsign_too_many(vector unsigned char a) {
+ return __builtin_ppc_bcdsetsign(a, 1, 2); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+}
+
+vector unsigned char test_national2packed_too_many(vector unsigned char a) {
+ return __builtin_ppc_national2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+}
+
+vector unsigned char test_packed2zoned_too_many(vector unsigned char a) {
+ return __builtin_ppc_packed2zoned(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+}
+
+vector unsigned char test_zoned2packed_too_many(vector unsigned char a) {
+ return __builtin_ppc_zoned2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+}
>From 3ea9c6d8126538e7134c2eaa46244408a1c309df Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 11 Sep 2026 12:08:59 +0600
Subject: [PATCH 2/4] make error message from at most to just expected
---
clang/lib/Sema/SemaChecking.cpp | 5 ++---
clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp | 8 ++++----
2 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index cc68bf5ef5413..4b398cdabf4bf 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4195,15 +4195,14 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
// check if the builtin has CustomTypeChecking or not, if it dose we get the
// already parsed type string of the builtin and compare that with the
- // caller's passed args and give error for too much args
+ // caller's passed args and give error for too many args
if (Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) {
ASTContext::GetBuiltinTypeError Error;
QualType BuiltinFTy = Context.GetBuiltinType(BuiltinID, Error);
if (!BuiltinFTy.isNull() && !Error) {
if (const FunctionProtoType *FPT =
dyn_cast<FunctionProtoType>(BuiltinFTy.getTypePtr()))
- if (!FPT->isVariadic() &&
- checkArgCountAtMost(TheCall, FPT->getNumParams()))
+ if (!FPT->isVariadic() && checkArgCount(TheCall, FPT->getNumParams()))
return ExprError();
}
}
diff --git a/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp b/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
index 15d30fd1dffe5..da2aba5e9768c 100644
--- a/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
+++ b/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
@@ -6,17 +6,17 @@
// RUN: %clang_cc1 -target-feature +power9-vector -triple powerpc-unknown-unknown -fsyntax-only -verify %s
vector unsigned char test_bcdsetsign_too_many(vector unsigned char a) {
- return __builtin_ppc_bcdsetsign(a, 1, 2); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+ return __builtin_ppc_bcdsetsign(a, 1, 2); // expected-error {{too many arguments to function call, expected 2, have 3}}
}
vector unsigned char test_national2packed_too_many(vector unsigned char a) {
- return __builtin_ppc_national2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+ return __builtin_ppc_national2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected 2, have 3}}
}
vector unsigned char test_packed2zoned_too_many(vector unsigned char a) {
- return __builtin_ppc_packed2zoned(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+ return __builtin_ppc_packed2zoned(a, 1, 0); // expected-error {{too many arguments to function call, expected 2, have 3}}
}
vector unsigned char test_zoned2packed_too_many(vector unsigned char a) {
- return __builtin_ppc_zoned2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+ return __builtin_ppc_zoned2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected 2, have 3}}
}
>From 24499797e15b852ac93731009baf69ea72990368 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 11 Sep 2026 15:43:18 +0600
Subject: [PATCH 3/4] try new approach to have the validation specifically for
only the PPC architecture to avoid conflict with other architectures
---
clang/lib/Sema/SemaChecking.cpp | 14 -------------
clang/lib/Sema/SemaPPC.cpp | 16 +++++++++++++++
.../PowerPC/builtins-too-many-args-check.cpp | 20 +++++++++++++++----
3 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4b398cdabf4bf..0e85af73696dc 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4193,20 +4193,6 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
if (getLangOpts().HLSL && HLSL().CheckBuiltinFunctionCall(BuiltinID, TheCall))
return ExprError();
- // check if the builtin has CustomTypeChecking or not, if it dose we get the
- // already parsed type string of the builtin and compare that with the
- // caller's passed args and give error for too many args
- if (Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) {
- ASTContext::GetBuiltinTypeError Error;
- QualType BuiltinFTy = Context.GetBuiltinType(BuiltinID, Error);
- if (!BuiltinFTy.isNull() && !Error) {
- if (const FunctionProtoType *FPT =
- dyn_cast<FunctionProtoType>(BuiltinFTy.getTypePtr()))
- if (!FPT->isVariadic() && checkArgCount(TheCall, FPT->getNumParams()))
- return ExprError();
- }
- }
-
// Since the target specific builtins for each arch overlap, only check those
// of the arch we are compiling for.
if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
diff --git a/clang/lib/Sema/SemaPPC.cpp b/clang/lib/Sema/SemaPPC.cpp
index 8a594fc86dea6..a9ac931dda8c3 100644
--- a/clang/lib/Sema/SemaPPC.cpp
+++ b/clang/lib/Sema/SemaPPC.cpp
@@ -145,6 +145,16 @@ bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
// - IsIntType: enforces any integer type
// Lambdas centralize type checks for BCD builtin handlers
+ // reject calls with more args than the builtin's declared prototype
+ auto CheckArgCountAtMost = [&](unsigned BuiltinID) -> bool {
+ ASTContext::GetBuiltinTypeError Error;
+ if (const auto *FPT = dyn_cast<FunctionProtoType>(
+ Context.GetBuiltinType(BuiltinID, Error).getTypePtr()))
+ if (!FPT->isVariadic())
+ return SemaRef.checkArgCountAtMost(TheCall, FPT->getNumParams());
+ return false;
+ };
+
// Lambda 1: verify vector unsigned char type
auto IsTypeVecUChar = [&](QualType ArgTy, unsigned ArgIndex) -> bool {
QualType VecType = Context.getVectorType(Context.UnsignedCharTy, 16,
@@ -173,6 +183,8 @@ bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
default:
return false;
case PPC::BI__builtin_ppc_bcdsetsign: {
+ if (CheckArgCountAtMost(BuiltinID))
+ return true;
// Arg0 must be vector unsigned char
if (!IsTypeVecUChar(TheCall->getArg(0)->getType(), 0))
return false;
@@ -183,10 +195,14 @@ bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
case PPC::BI__builtin_ppc_national2packed:
case PPC::BI__builtin_ppc_packed2zoned:
case PPC::BI__builtin_ppc_zoned2packed:
+ if (CheckArgCountAtMost(BuiltinID))
+ return true;
return SemaRef.BuiltinConstantArgRange(TheCall, 1, 0, 1);
case PPC::BI__builtin_ppc_bcdshift:
case PPC::BI__builtin_ppc_bcdshiftround:
case PPC::BI__builtin_ppc_bcdtruncate: {
+ if (CheckArgCountAtMost(BuiltinID))
+ return true;
// Arg0 must be vector unsigned char
if (!IsTypeVecUChar(TheCall->getArg(0)->getType(), 0))
diff --git a/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp b/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
index da2aba5e9768c..d1fa1e543b04b 100644
--- a/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
+++ b/clang/test/Sema/PowerPC/builtins-too-many-args-check.cpp
@@ -6,17 +6,29 @@
// RUN: %clang_cc1 -target-feature +power9-vector -triple powerpc-unknown-unknown -fsyntax-only -verify %s
vector unsigned char test_bcdsetsign_too_many(vector unsigned char a) {
- return __builtin_ppc_bcdsetsign(a, 1, 2); // expected-error {{too many arguments to function call, expected 2, have 3}}
+ return __builtin_ppc_bcdsetsign(a, 1, 2); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
}
vector unsigned char test_national2packed_too_many(vector unsigned char a) {
- return __builtin_ppc_national2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected 2, have 3}}
+ return __builtin_ppc_national2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
}
vector unsigned char test_packed2zoned_too_many(vector unsigned char a) {
- return __builtin_ppc_packed2zoned(a, 1, 0); // expected-error {{too many arguments to function call, expected 2, have 3}}
+ return __builtin_ppc_packed2zoned(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
}
vector unsigned char test_zoned2packed_too_many(vector unsigned char a) {
- return __builtin_ppc_zoned2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected 2, have 3}}
+ return __builtin_ppc_zoned2packed(a, 1, 0); // expected-error {{too many arguments to function call, expected at most 2, have 3}}
+}
+
+vector unsigned char test_bcdshift_too_many(vector unsigned char a) {
+ return __builtin_ppc_bcdshift(a, 1, 0, 3); // expected-error {{too many arguments to function call, expected at most 3, have 4}}
+}
+
+vector unsigned char test_bcdshiftround_too_many(vector unsigned char a) {
+ return __builtin_ppc_bcdshiftround(a, 1, 0, 3); // expected-error {{too many arguments to function call, expected at most 3, have 4}}
+}
+
+vector unsigned char test_bcdtruncate_too_many(vector unsigned char a) {
+ return __builtin_ppc_bcdtruncate(a, 1, 0, 3); // expected-error {{too many arguments to function call, expected at most 3, have 4}}
}
>From 50c66522dc1f6ec3310f369ba8e05ca4f75b7d8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=CA=9F=E1=B4=9C=C9=B4=E1=B4=87x?= <imlunex2011 at gmail.com>
Date: Fri, 11 Sep 2026 19:55:13 +0600
Subject: [PATCH 4/4] Update clang/lib/Sema/SemaPPC.cpp
Co-authored-by: Aaron Ballman <aaron at aaronballman.com>
---
clang/lib/Sema/SemaPPC.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaPPC.cpp b/clang/lib/Sema/SemaPPC.cpp
index a9ac931dda8c3..38e7704945d9b 100644
--- a/clang/lib/Sema/SemaPPC.cpp
+++ b/clang/lib/Sema/SemaPPC.cpp
@@ -145,7 +145,7 @@ bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
// - IsIntType: enforces any integer type
// Lambdas centralize type checks for BCD builtin handlers
- // reject calls with more args than the builtin's declared prototype
+ // Reject calls with more args than the builtin's declared prototype.
auto CheckArgCountAtMost = [&](unsigned BuiltinID) -> bool {
ASTContext::GetBuiltinTypeError Error;
if (const auto *FPT = dyn_cast<FunctionProtoType>(
More information about the cfe-commits
mailing list