[clang] [clang] Preserve `externs` following broken declarations (PR #161641)

Alejandro Álvarez Ayllón via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 2 06:49:15 PDT 2025


https://github.com/alejandro-alvarez-sonarsource updated https://github.com/llvm/llvm-project/pull/161641

>From 446f4f79aa07b0bef23cfb5ce8b87226651e12fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
 <alejandro.alvarez at sonarsource.com>
Date: Fri, 2 Jun 2023 18:38:24 +0200
Subject: [PATCH 1/2] [clang] Preserve `externs` following broken declarations

Treat them as namespaces: if they are at the beginning of the line,
they are likely a good recovery point.

CPP-4478
---
 clang/lib/Parse/ParseDecl.cpp                            | 3 +++
 .../Parser/recovery-after-expected-unqualified-id.cpp    | 9 +++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 clang/test/Parser/recovery-after-expected-unqualified-id.cpp

diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 22c01c4e371f3..d6cd7eb8c2c3d 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -2083,6 +2083,9 @@ void Parser::SkipMalformedDecl() {
         return;
       break;
 
+    case tok::kw_extern:
+      // 'extern' at the start of a line is almost certainly a good
+      // place to pick back up parsing
     case tok::kw_namespace:
       // 'namespace' at the start of a line is almost certainly a good
       // place to pick back up parsing, except in an Objective-C
diff --git a/clang/test/Parser/recovery-after-expected-unqualified-id.cpp b/clang/test/Parser/recovery-after-expected-unqualified-id.cpp
new file mode 100644
index 0000000000000..8019b46df1e7b
--- /dev/null
+++ b/clang/test/Parser/recovery-after-expected-unqualified-id.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify %s
+
+3.2 // expected-error {{expected unqualified-id}}
+
+extern "C" {
+    typedef int Int;
+}
+
+Int foo(); // Ok

>From b45ef61e2277b29d51ce51f7b8de0af3eb841b83 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
 <alejandro.alvarez at sonarsource.com>
Date: Thu, 2 Oct 2025 14:39:30 +0200
Subject: [PATCH 2/2] Update release notes

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c6ee1e282a008..74b0647f38795 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -446,6 +446,7 @@ Bug Fixes to AST Handling
   legal representation. This is fixed because ElaboratedTypes don't exist anymore. (#GH43179) (#GH68670) (#GH92757)
 - Fix unrecognized html tag causing undesirable comment lexing (#GH152944)
 - Fix comment lexing of special command names (#GH152943)
+- Use `extern` as a hint to continue parsing when recovering from a malformed declaration.
 
 Miscellaneous Bug Fixes
 ^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list