Author: Oleksandr T.
Date: 2026-01-06T21:38:41+02:00
New Revision: dfc0abd325962bfef7b18fbc13db9776ee63647f

URL: 
https://github.com/llvm/llvm-project/commit/dfc0abd325962bfef7b18fbc13db9776ee63647f
DIFF: 
https://github.com/llvm/llvm-project/commit/dfc0abd325962bfef7b18fbc13db9776ee63647f.diff

LOG: [Clang] prevent crash in early parsing of enable_if for C function 
declarators with identifier-list parameters (#174017)

Fixes #173826

---

This patch resolves an issue where Clang could crash while parsing the
`enable_if` attribute on C function declarators with identifier-list
parameters.

In C, identifier-list function declarators use parameter identifiers
rather than full parameter declarations, as specified by the C spec.
Since `enable_if` is parsed early to participate in redeclaration checks


https://github.com/llvm/llvm-project/blob/0f3a9f658a7f459b9aa701ec42a999b645653862/clang/lib/Parse/ParseDecl.cpp#L674-L675

the parser could encounter such identifiers before their parameter
declarations exist and incorrectly assume they were already formed,
leading to an assertion failure.

This change makes the early `enable_if` parsing path handle parameters
that have not yet been declared, preventing the crash while preserving
existing behavior for well-formed function prototypes.

Added: 
    clang/test/Parser/enableif-attr.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Parse/ParseDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3f6b435175d6d..77e2078a5ffbf 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -632,6 +632,7 @@ Bug Fixes to C++ Support
 - Fix the result of ``__is_pointer_interconvertible_base_of`` when arguments 
are qualified and passed via template parameters. (#GH135273)
 - Fixed a crash when evaluating nested requirements in requires-expressions 
that reference invented parameters. (#GH166325)
 - Fixed a crash when standard comparison categories (e.g. 
``std::partial_ordering``) are defined with incorrect static member types. 
(#GH170015) (#GH56571)
+- Fixed a crash when parsing the ``enable_if`` attribute on C function 
declarations with identifier-list parameters. (#GH173826)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 8688ccf41acb5..0e2caa214f3fd 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -680,10 +680,9 @@ void Parser::ParseGNUAttributeArgs(
     PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
                                      Scope::FunctionDeclarationScope |
                                      Scope::DeclScope);
-    for (unsigned i = 0; i != FTI.NumParams; ++i) {
-      ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
-      Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
-    }
+    for (unsigned i = 0; i != FTI.NumParams; ++i)
+      Actions.ActOnReenterCXXMethodParameter(
+          getCurScope(), dyn_cast_or_null<ParmVarDecl>(FTI.Params[i].Param));
   }
 
   ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,

diff  --git a/clang/test/Parser/enableif-attr.cpp 
b/clang/test/Parser/enableif-attr.cpp
new file mode 100644
index 0000000000000..6d80a5bd713ce
--- /dev/null
+++ b/clang/test/Parser/enableif-attr.cpp
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -x c -fsyntax-only -verify %s
+
+void f1(x) __attribute__((enable_if(1, "")));    // expected-error {{a 
parameter list without types is only allowed in a function definition}}
+void f2(x, y) __attribute__((enable_if(1, ""))); // expected-error {{a 
parameter list without types is only allowed in a function definition}}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to