[clang] [Clang] handle invalid close location in static assert declaration (PR #108701)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 15 04:34:21 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/108701

>From cbe06cf33277f904fe002db90d428a55b777ff5d Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sat, 14 Sep 2024 17:02:01 +0300
Subject: [PATCH 1/3] [Clang] handle invalid close location in static assert
 declaration

---
 clang/docs/ReleaseNotes.rst                | 2 +-
 clang/lib/Parse/ParseDeclCXX.cpp           | 3 ++-
 clang/test/SemaCXX/static-assert-cxx26.cpp | 3 +++
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 79b154ef1aef5e..27dde935ad19b9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -388,7 +388,7 @@ Bug Fixes to C++ Support
 - Fixed a crash in the typo correction of an invalid CTAD guide. (#GH107887)
 - Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
   pack. #GH63819, #GH107560
-
+- Fix a crash when a static assert declaration has an invalid close location. (#GH108687)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 6370da1fab0042..6f0f5a0311bc18 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1109,7 +1109,8 @@ Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) {
     }
   }
 
-  T.consumeClose();
+  if (T.consumeClose())
+    return nullptr;
 
   DeclEnd = Tok.getLocation();
   ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert, TokName);
diff --git a/clang/test/SemaCXX/static-assert-cxx26.cpp b/clang/test/SemaCXX/static-assert-cxx26.cpp
index 7d896d8b365b74..95ffa8cd68cf7c 100644
--- a/clang/test/SemaCXX/static-assert-cxx26.cpp
+++ b/clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -415,3 +415,6 @@ static_assert(
       // expected-note at -1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
 );
 }
+
+static_assert(true, "" // expected-note {{to match this '('}}
+                       // expected-error {{expected ')'}}

>From 77063cc64c5047144c2fa0291c38248401d33164 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sun, 15 Sep 2024 00:51:21 +0300
Subject: [PATCH 2/3] add test to cover c++2a compliance

---
 clang/test/SemaCXX/static-assert-cxx20.cpp | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 clang/test/SemaCXX/static-assert-cxx20.cpp

diff --git a/clang/test/SemaCXX/static-assert-cxx20.cpp b/clang/test/SemaCXX/static-assert-cxx20.cpp
new file mode 100644
index 00000000000000..479fd14dc5bfad
--- /dev/null
+++ b/clang/test/SemaCXX/static-assert-cxx20.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -std=c++2a -triple=x86_64-linux -fsyntax-only %s -verify
+
+static_assert(true, "" // expected-warning {{'static_assert' with a user-generated message is a C++26 extension}} \
+                       // expected-note {{to match this '('}}
+                       // expected-error {{expected ')'}}

>From f807d212aebbac2fb31dc25fbac0b1ccc2f46570 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sun, 15 Sep 2024 14:34:04 +0300
Subject: [PATCH 3/3] move tests from SemaXXX to Parser

---
 clang/test/Parser/static_assert.cpp        | 6 ++++++
 clang/test/SemaCXX/static-assert-cxx20.cpp | 5 -----
 clang/test/SemaCXX/static-assert-cxx26.cpp | 3 ---
 3 files changed, 6 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/Parser/static_assert.cpp
 delete mode 100644 clang/test/SemaCXX/static-assert-cxx20.cpp

diff --git a/clang/test/Parser/static_assert.cpp b/clang/test/Parser/static_assert.cpp
new file mode 100644
index 00000000000000..4fe7d3cda7b214
--- /dev/null
+++ b/clang/test/Parser/static_assert.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -triple=x86_64-linux -std=c++2a -verify=cxx2a %s
+// RUN: %clang_cc1 -fsyntax-only -triple=x86_64-linux -std=c++2c -verify=cxx2c %s
+
+static_assert(true, "" // cxx2a-warning {{'static_assert' with a user-generated message is a C++26 extension}} \
+                       // cxx2a-note {{to match this '('}} cxx2c-note {{to match this '('}}
+                       // cxx2a-error {{expected ')'}}     cxx2c-error {{expected ')'}}
diff --git a/clang/test/SemaCXX/static-assert-cxx20.cpp b/clang/test/SemaCXX/static-assert-cxx20.cpp
deleted file mode 100644
index 479fd14dc5bfad..00000000000000
--- a/clang/test/SemaCXX/static-assert-cxx20.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-// RUN: %clang_cc1 -std=c++2a -triple=x86_64-linux -fsyntax-only %s -verify
-
-static_assert(true, "" // expected-warning {{'static_assert' with a user-generated message is a C++26 extension}} \
-                       // expected-note {{to match this '('}}
-                       // expected-error {{expected ')'}}
diff --git a/clang/test/SemaCXX/static-assert-cxx26.cpp b/clang/test/SemaCXX/static-assert-cxx26.cpp
index 95ffa8cd68cf7c..7d896d8b365b74 100644
--- a/clang/test/SemaCXX/static-assert-cxx26.cpp
+++ b/clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -415,6 +415,3 @@ static_assert(
       // expected-note at -1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
 );
 }
-
-static_assert(true, "" // expected-note {{to match this '('}}
-                       // expected-error {{expected ')'}}



More information about the cfe-commits mailing list