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

>From eff0ff383d8cb29958a87cf20a2c5ba30fd643c6 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <[email protected]>
Date: Tue, 27 Jan 2026 20:26:18 +0200
Subject: [PATCH 1/3] [Clang] prevent preprocessor crash on incomplete scoped
 __has_cpp_attribute arguments

---
 clang/docs/ReleaseNotes.rst                      |  1 +
 clang/lib/Lex/PPMacroExpansion.cpp               |  2 +-
 clang/test/Preprocessor/has_attribute_errors.cpp | 10 ++++++++++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d03211a200a29..a1a97492f6783 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -175,6 +175,7 @@ Bug Fixes in This Version
 - Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters 
are missing parentheses. (#GH175088)
 
 - Fix lifetime extension of temporaries in for-range-initializers in 
templates. (#GH165182)
+- Fixed a preprocessor crash in ``__has_cpp_attribute`` on incomplete scoped 
attributes. (#GH178098)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 5efa4b5b3f872..734969e46e400 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1364,7 +1364,7 @@ static void 
EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
 
   Token ResultTok;
   bool SuppressDiagnostic = false;
-  while (true) {
+  while (Tok.isNoneOf(tok::eod, tok::eof)) {
     // Parse next token.
     if (ExpandArgs)
       PP.Lex(Tok);
diff --git a/clang/test/Preprocessor/has_attribute_errors.cpp 
b/clang/test/Preprocessor/has_attribute_errors.cpp
index 1fc88d3f926fb..d7492ed3f4a2c 100644
--- a/clang/test/Preprocessor/has_attribute_errors.cpp
+++ b/clang/test/Preprocessor/has_attribute_errors.cpp
@@ -14,3 +14,13 @@ __has_cpp_attribute(__clang__::fallthrough) // 
expected-error {{missing ')' afte
                                             // expected-note {{to match this 
'('}} \
                                             // expected-error {{builtin 
feature check macro requires a parenthesized identifier}}
 
+namespace GH178098 {
+// expected-error@+2 {{builtin feature check macro requires a parenthesized 
identifier}}
+// expected-error@+1 {{expected value in expression}}
+#if __has_cpp_attribute(clang::
+#endif
+
+// expected-error@+3 {{builtin feature check macro requires a parenthesized 
identifier}}
+// expected-error@+2 {{unterminated function-like macro invocation}}
+__has_cpp_attribute(clang::
+}

>From 1ecc84c206ac71f18e22784b89cafe9dba672b41 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <[email protected]>
Date: Tue, 27 Jan 2026 20:49:52 +0200
Subject: [PATCH 2/3] fix formatting

---
 clang/lib/Lex/PPMacroExpansion.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 734969e46e400..350bd6c88eaad 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1443,7 +1443,7 @@ static void 
EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
       PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
       SuppressDiagnostic = true;
     }
-  }
+}
 }
 
 /// Helper function to return the IdentifierInfo structure of a Token

>From 05f4e0771524addbe37cd812b2bba0834199e793 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <[email protected]>
Date: Wed, 28 Jan 2026 14:49:10 +0200
Subject: [PATCH 3/3] add tests to cover __has_c_attribute/__has_attribute

---
 clang/test/Preprocessor/has_attribute_errors.cpp | 7 +++++++
 clang/test/Preprocessor/has_c_attribute_errors.c | 9 +++++++++
 2 files changed, 16 insertions(+)
 create mode 100644 clang/test/Preprocessor/has_c_attribute_errors.c

diff --git a/clang/test/Preprocessor/has_attribute_errors.cpp 
b/clang/test/Preprocessor/has_attribute_errors.cpp
index d7492ed3f4a2c..199227e90f054 100644
--- a/clang/test/Preprocessor/has_attribute_errors.cpp
+++ b/clang/test/Preprocessor/has_attribute_errors.cpp
@@ -20,6 +20,13 @@ namespace GH178098 {
 #if __has_cpp_attribute(clang::
 #endif
 
+// expected-error@+4 {{unterminated function-like macro invocation}}
+// expected-error@+3 {{missing ')' after 'clang'}}
+// expected-error@+2 {{expected value in expression}}
+// expected-note@+1 {{to match this '('}}
+#if __has_attribute(clang::
+#endif
+
 // expected-error@+3 {{builtin feature check macro requires a parenthesized 
identifier}}
 // expected-error@+2 {{unterminated function-like macro invocation}}
 __has_cpp_attribute(clang::
diff --git a/clang/test/Preprocessor/has_c_attribute_errors.c 
b/clang/test/Preprocessor/has_c_attribute_errors.c
new file mode 100644
index 0000000000000..026c0bd37e7fd
--- /dev/null
+++ b/clang/test/Preprocessor/has_c_attribute_errors.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple i386-unknown-unknown -Eonly -verify %s
+
+// expected-error@+2 {{builtin feature check macro requires a parenthesized 
identifier}}
+// expected-error@+1 {{expected value in expression}}
+#if __has_c_attribute(clang::
+#endif
+
+// expected-error@+1 {{builtin feature check macro requires a parenthesized 
identifier}}
+__has_c_attribute(clang::

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

Reply via email to