Files
llvm-project/clang/test/Preprocessor/has_attribute_errors.cpp
Oleksandr Tarasiuk bf7e2b81e7 [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


3f5a5d45d1/clang/lib/Lex/PPMacroExpansion.cpp (L1877-L1881)

and then proceed with


3f5a5d45d1/clang/lib/Lex/PPMacroExpansion.cpp (L1425-L1430)


since `eof`/`eod` is not guarded at


3f5a5d45d1/clang/lib/Lex/PPMacroExpansion.cpp (L1367-L1372)

this could lead to a preprocessor crash


3f5a5d45d1/clang/lib/Lex/PPMacroExpansion.cpp (L1370)
2026-01-28 15:44:48 +02:00

34 lines
1.6 KiB
C++

// RUN: %clang_cc1 -triple i386-unknown-unknown -Eonly -verify %s
// We warn users if they write an attribute like
// [[__clang__::fallthrough]] because __clang__ is a macro that expands to 1.
// Instead, we suggest users use [[_Clang::fallthrough]] in this situation.
// However, because __has_cpp_attribute (and __has_c_attribute) require
// expanding their argument tokens, __clang__ expands to 1 in the feature test
// macro as well. We don't currently give users a kind warning in this case,
// but we previously did not expand macros and so this would return 0. Now that
// we properly expand macros, users will now get an error about using incorrect
// syntax.
__has_cpp_attribute(__clang__::fallthrough) // expected-error {{missing ')' after <numeric_constant>}} \
// 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@+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::
}