Skip to content

Conversation

@mugiwaraluffy56
Copy link
Contributor

@mugiwaraluffy56 mugiwaraluffy56 commented Jan 29, 2026

Summary

Add type generic builtins to the allowed variadics list in the cppcoreguidelines-pro-type-vararg check (also used by hicpp-vararg):

  • __builtin_clzg
  • __builtin_ctzg
  • __builtin_popcountg
  • __builtin_bswapg

Root Cause

These builtins are declared as variadic (int(...)) to accept any integer type via CustomTypeChecking. However, they are not C style vararg functions , they take exactly one argument of a generic integer type.

Test

Added test cases in pro-type-vararg.cpp to verify no warning is emitted.

Fixes #178629

@mugiwaraluffy56
Copy link
Contributor Author

@PiotrZSL Could you please review this? This fixes a false positive for type generic builtins like __builtin_popcountg. Thanks!

@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2026

@llvm/pr-subscribers-clang-tools-extra

Author: puneeth_aditya_5656 (mugiwaraluffy56)

Changes

Summary

Add type generic builtins to the allowed variadics list in the cppcoreguidelines-pro-type-vararg check (also used by hicpp-vararg):

  • __builtin_clzg
  • __builtin_ctzg
  • __builtin_popcountg
  • __builtin_bswapg

Root Cause

These builtins are declared as variadic (int(...)) to accept any integer type via CustomTypeChecking. However, they are not C style vararg functions , they take exactly one argument of a generic integer type.

Test

Added test cases in pro-type-vararg.cpp to verify no warning is emitted.

Fixes #17862


Full diff: https://github.com/llvm/llvm-project/pull/178656.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp (+5)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp (+6)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
index d5ff4af84b0b7..73cadfdf14614 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
@@ -56,6 +56,11 @@ static constexpr StringRef AllowedVariadics[] = {
     "__builtin_nontemporal_store",
     "__builtin_nontemporal_load",
     "__builtin_ms_va_start",
+    // Type-generic builtins: declared variadic to accept any integer type.
+    "__builtin_clzg",
+    "__builtin_ctzg",
+    "__builtin_popcountg",
+    "__builtin_bswapg",
     // clang-format on
 };
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp
index 3f73d1de333f4..1a5a5fb7dd93c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp
@@ -57,6 +57,12 @@ void ignoredBuiltinsTest(void *ptr) {
   (void)__builtin_fpclassify(0, 0, 0, 0, 0, 0.f);
   (void)__builtin_isinf_sign(0.f);
   (void)__builtin_prefetch(nullptr);
+
+  // GH#178629: Type-generic builtins should not warn.
+  (void)__builtin_clzg(1u);
+  (void)__builtin_ctzg(1u);
+  (void)__builtin_popcountg(1u);
+  (void)__builtin_bswapg(1u);
 }
 
 // Some implementations of __builtin_va_list and __builtin_ms_va_list desugared

@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2026

@llvm/pr-subscribers-clang-tidy

Author: puneeth_aditya_5656 (mugiwaraluffy56)

Changes

Summary

Add type generic builtins to the allowed variadics list in the cppcoreguidelines-pro-type-vararg check (also used by hicpp-vararg):

  • __builtin_clzg
  • __builtin_ctzg
  • __builtin_popcountg
  • __builtin_bswapg

Root Cause

These builtins are declared as variadic (int(...)) to accept any integer type via CustomTypeChecking. However, they are not C style vararg functions , they take exactly one argument of a generic integer type.

Test

Added test cases in pro-type-vararg.cpp to verify no warning is emitted.

Fixes #17862


Full diff: https://github.com/llvm/llvm-project/pull/178656.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp (+5)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp (+6)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
index d5ff4af84b0b7..73cadfdf14614 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
@@ -56,6 +56,11 @@ static constexpr StringRef AllowedVariadics[] = {
     "__builtin_nontemporal_store",
     "__builtin_nontemporal_load",
     "__builtin_ms_va_start",
+    // Type-generic builtins: declared variadic to accept any integer type.
+    "__builtin_clzg",
+    "__builtin_ctzg",
+    "__builtin_popcountg",
+    "__builtin_bswapg",
     // clang-format on
 };
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp
index 3f73d1de333f4..1a5a5fb7dd93c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp
@@ -57,6 +57,12 @@ void ignoredBuiltinsTest(void *ptr) {
   (void)__builtin_fpclassify(0, 0, 0, 0, 0, 0.f);
   (void)__builtin_isinf_sign(0.f);
   (void)__builtin_prefetch(nullptr);
+
+  // GH#178629: Type-generic builtins should not warn.
+  (void)__builtin_clzg(1u);
+  (void)__builtin_ctzg(1u);
+  (void)__builtin_popcountg(1u);
+  (void)__builtin_bswapg(1u);
 }
 
 // Some implementations of __builtin_va_list and __builtin_ms_va_list desugared

Copy link
Member

@zeyi2 zeyi2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an entry in ReleaseNotes.rst :)

@mugiwaraluffy56 mugiwaraluffy56 force-pushed the fix-clang-tidy-hicpp-vararg-builtin branch from 3455ca6 to 183e6c6 Compare January 29, 2026 14:01
@mugiwaraluffy56 mugiwaraluffy56 requested a review from zeyi2 January 29, 2026 14:01
@mugiwaraluffy56
Copy link
Contributor Author

Please add an entry in ReleaseNotes.rst :)

Added the ReleaseNotes entry. Thanks for the review!

Copy link
Member

@zeyi2 zeyi2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, thank you!

But please wait for other reviewers :)

Copy link
Contributor

@localspook localspook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the open comment, LGTM

"__builtin_clzg",
"__builtin_ctzg",
"__builtin_popcountg",
"__builtin_bswapg",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just check hasCustomTypechecking() instead of listing out a bunch of builtins here? The fact that these builtins are "variadic" is an clang implementation detail, and all the functions you're excluding here have that bit set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@efriedma-quic Great suggestion! Updated to check hasCustomTypechecking() instead of listing individual builtins. This is more maintainable and handles all builtins that use variadic declarations as an implementation detail.

@mugiwaraluffy56 mugiwaraluffy56 force-pushed the fix-clang-tidy-hicpp-vararg-builtin branch from 183e6c6 to 5ce1602 Compare January 30, 2026 05:04
@github-actions
Copy link

github-actions bot commented Jan 30, 2026

✅ With the latest revision this PR passed the C/C++ code linter.

@mugiwaraluffy56 mugiwaraluffy56 force-pushed the fix-clang-tidy-hicpp-vararg-builtin branch from 5ce1602 to 00688cf Compare January 30, 2026 05:13
@mugiwaraluffy56
Copy link
Contributor Author

@efriedma-quic @localspook @zeyi2 @PiotrZSL @vbvictor can you review this

@mugiwaraluffy56 mugiwaraluffy56 force-pushed the fix-clang-tidy-hicpp-vararg-builtin branch from 00688cf to 3498a11 Compare January 31, 2026 13:35
@mugiwaraluffy56 mugiwaraluffy56 requested a review from zeyi2 January 31, 2026 13:36
@github-actions
Copy link

github-actions bot commented Jan 31, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

Add __builtin_clzg, __builtin_ctzg, __builtin_popcountg, and
__builtin_bswapg to the allowed variadics list. These type-generic
builtins are declared as variadic to accept any integer type, but
they are not C-style vararg functions and should not trigger the
hicpp-vararg / cppcoreguidelines-pro-type-vararg warning.

Fixes llvm#178629
@mugiwaraluffy56 mugiwaraluffy56 force-pushed the fix-clang-tidy-hicpp-vararg-builtin branch from 3498a11 to 5227669 Compare January 31, 2026 13:39
Copy link
Member

@zeyi2 zeyi2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-tidy] false positive for variadic builtin function (hicpp-vararg)

5 participants