[clang] [Sema] Check arg count for builtins with CustomTypeChecking (PR #222841)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 10 23:09:18 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/2] [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/2] 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}}
 }



More information about the cfe-commits mailing list