[clang] bf7e2b8 - [Clang] prevent preprocessor crash on incomplete scoped __has_cpp_attribute arguments (#178273)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 28 05:44:53 PST 2026
Author: Oleksandr Tarasiuk
Date: 2026-01-28T15:44:48+02:00
New Revision: bf7e2b81e779713cc535c884e287ec6cf02fcfef
URL: https://github.com/llvm/llvm-project/commit/bf7e2b81e779713cc535c884e287ec6cf02fcfef
DIFF: https://github.com/llvm/llvm-project/commit/bf7e2b81e779713cc535c884e287ec6cf02fcfef.diff
LOG: [Clang] prevent preprocessor crash on incomplete scoped __has_cpp_attribute arguments (#178273)
Fixes #178098
---
This patch addressed the issue when `__has_cpp_attribute` is expanded
with incomplete scoped attributes. The scoped name parsing can lex to
`eof`/`eod` at
https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1877-L1881
and then proceed with
https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1425-L1430
since `eof`/`eod` is not guarded at
https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1367-L1372
this could lead to a preprocessor crash
https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1370
Added:
clang/test/Preprocessor/has_c_attribute_errors.c
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Lex/PPMacroExpansion.cpp
clang/test/Preprocessor/has_attribute_errors.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b5327c01fbb81..42a300ca94581 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -210,6 +210,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..350bd6c88eaad 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);
@@ -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
diff --git a/clang/test/Preprocessor/has_attribute_errors.cpp b/clang/test/Preprocessor/has_attribute_errors.cpp
index 1fc88d3f926fb..199227e90f054 100644
--- a/clang/test/Preprocessor/has_attribute_errors.cpp
+++ b/clang/test/Preprocessor/has_attribute_errors.cpp
@@ -14,3 +14,20 @@ __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 at +2 {{builtin feature check macro requires a parenthesized identifier}}
+// expected-error at +1 {{expected value in expression}}
+#if __has_cpp_attribute(clang::
+#endif
+
+// expected-error at +4 {{unterminated function-like macro invocation}}
+// expected-error at +3 {{missing ')' after 'clang'}}
+// expected-error at +2 {{expected value in expression}}
+// expected-note at +1 {{to match this '('}}
+#if __has_attribute(clang::
+#endif
+
+// expected-error at +3 {{builtin feature check macro requires a parenthesized identifier}}
+// expected-error at +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 at +2 {{builtin feature check macro requires a parenthesized identifier}}
+// expected-error at +1 {{expected value in expression}}
+#if __has_c_attribute(clang::
+#endif
+
+// expected-error at +1 {{builtin feature check macro requires a parenthesized identifier}}
+__has_c_attribute(clang::
More information about the cfe-commits
mailing list