[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 30 19:54:15 PDT 2024


https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/87173

>From 710a72c43ae9612e577172a978bfafe6553a6f9e Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst    | 2 ++
 clang/lib/Sema/SemaType.cpp    | 3 +++
 clang/test/SemaCXX/PR75221.cpp | 6 ++++++
 3 files changed, 11 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 37b843915a0dee..20578c9b60e33c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -465,6 +465,8 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash caused by defined struct in a type alias template when the structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index fd94caa4e1d449..973ad20c943bde 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3899,6 +3899,9 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
       SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
           << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
       D.setInvalidType(true);
+      OwnedTagDecl->setCompleteDefinition(false);
+      OwnedTagDecl->setInvalidDecl();
+      OwnedTagDecl->setCompleteDefinition();
     }
   }
 
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00000000000000..b342e347c5606a
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+template <class T> using foo = struct foo { // expected-error {{'foo' cannot be defined in a type alias template}}
+  T size = 0;
+};
+foo a;



More information about the cfe-commits mailing list