[clang] Sema: detect adjusted function prototypes to fix crash with __typeof + __asm + noreturn (#173598) (PR #174092)
Dhanashree Petare via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 2 05:36:12 PST 2026
https://github.com/DhanashreePetare updated https://github.com/llvm/llvm-project/pull/174092
>From edc1e48d565c5d100b553f17505b0b9e19cc425d Mon Sep 17 00:00:00 2001
From: DhanashreePetare <dhanashreepetare8125 at gmail.com>
Date: Wed, 31 Dec 2025 19:05:59 +0530
Subject: [PATCH] Sema: handle adjusted function types;add regression test for
__typeof+__asm+noreturn(#173598)
Signed-off-by: DhanashreePetare <dhanashreepetare8125 at gmail.com>
---
clang/docs/ReleaseNotes.rst | 4 ++++
clang/lib/Sema/SemaDecl.cpp | 28 +++++++++++++++++++++------
clang/test/Sema/typeof-asm-noreturn.c | 10 ++++++++++
3 files changed, 36 insertions(+), 6 deletions(-)
create mode 100644 clang/test/Sema/typeof-asm-noreturn.c
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ecdbbc05cdef4..472f5a4d49860 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -248,6 +248,8 @@ C23 Feature Support
Non-comprehensive list of changes in this release
-------------------------------------------------
+ - Clang: Avoid crash when declaring functions with ``__typeof``, ``__asm__`` alias and ``__attribute__((noreturn))`` in C2X mode. (PR #174092)
+
- Added ``__scoped_atomic_uinc_wrap`` and ``__scoped_atomic_udec_wrap``.
- Removed OpenCL header-only feature macros (previously unconditionally enabled
@@ -506,6 +508,8 @@ Improvements to Coverage Mapping
Bug Fixes in This Version
-------------------------
+- Avoid crash when declaring functions with ``__typeof``, ``__asm__`` alias and ``__attribute__((noreturn))`` in C2X mode. (PR #174092)
+
- Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
``#pragma pop_macro("")``. (#GH149762).
- Fix a crash in variable length array (e.g. ``int a[*]``) function parameter type
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f636e8e35a3d5..aefb4c7dd476c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -9529,12 +9529,28 @@ static FunctionDecl *CreateNewFunctionDecl(Sema &SemaRef, Declarator &D,
// declarator to still have a function type. e.g.,
// typedef void func(int a);
// __attribute__((noreturn)) func other_func; // This has a prototype
- bool HasPrototype =
- (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype) ||
- (D.getDeclSpec().isTypeRep() &&
- SemaRef.GetTypeFromParser(D.getDeclSpec().getRepAsType(), nullptr)
- ->isFunctionProtoType()) ||
- (!R->getAsAdjusted<FunctionType>() && R->isFunctionProtoType());
+ // Determine whether the function was written with a prototype. We
+ // consider several sources: an explicit prototype in the declarator,
+ // a typedef reference that resolves to a function prototype, or the
+ // result type `R` being a function prototype.
+ auto hasPrototype = [&]() -> bool {
+ if (D.isFunctionDeclarator() && D.getFunctionTypeInfo().hasPrototype)
+ return true;
+
+ if (D.getDeclSpec().isTypeRep()) {
+ QualType RepTy = SemaRef.GetTypeFromParser(D.getDeclSpec().getRepAsType(),
+ nullptr);
+ if (!RepTy.isNull() && RepTy->getAs<FunctionProtoType>())
+ return true;
+ }
+
+ if (!R.isNull() && R->getAs<FunctionProtoType>())
+ return true;
+
+ return false;
+ };
+
+ bool HasPrototype = hasPrototype();
assert(
(HasPrototype || !SemaRef.getLangOpts().requiresStrictPrototypes()) &&
"Strict prototypes are required");
diff --git a/clang/test/Sema/typeof-asm-noreturn.c b/clang/test/Sema/typeof-asm-noreturn.c
new file mode 100644
index 0000000000000..c96d10b2ea6e6
--- /dev/null
+++ b/clang/test/Sema/typeof-asm-noreturn.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c2x -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+int memcmp(const void *, const void *, unsigned long);
+
+__typeof(memcmp) memcmp_alias __asm__("memory_compare") __attribute__((noreturn));
+
+void use(void) {
+ (void)memcmp_alias(0, 0, 0);
+}
More information about the cfe-commits
mailing list