Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8f1c383
[Clang] disallow operator in attribute argument lists
a-tarasyuk Jul 7, 2025
962f703
update diagnostic message
a-tarasyuk Jul 7, 2025
a697990
disallow and operators in C++ mode only
a-tarasyuk Jul 7, 2025
697e437
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Jul 7, 2025
a762e0e
Merge branch 'main' into fix/147217
a-tarasyuk Jul 7, 2025
2a40e22
Merge branch 'main' into fix/147217
a-tarasyuk Jul 8, 2025
ac38e4d
update release notes
a-tarasyuk Jul 8, 2025
9204a36
handle preprocessor tokens in unknown attributes
a-tarasyuk Jul 8, 2025
b4f416c
Merge branch 'fix/147217' of https://github.com/a-tarasyuk/llvm-proje…
a-tarasyuk Jul 8, 2025
f923464
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Jul 8, 2025
2f1fc4b
Merge branch 'main' into fix/147217
a-tarasyuk Jul 9, 2025
20e9ce8
Merge branch 'main' into fix/147217
a-tarasyuk Jul 14, 2025
dc71fbf
Merge branch 'main' into fix/147217
a-tarasyuk Jul 17, 2025
7abb6d0
Merge branch 'main' into fix/147217
a-tarasyuk Jul 25, 2025
31aa310
Merge branch 'main' into fix/147217
a-tarasyuk Jul 30, 2025
4e429b0
update release notes
a-tarasyuk Jul 30, 2025
0667a32
Merge branch 'fix/147217' of https://github.com/a-tarasyuk/llvm-proje…
a-tarasyuk Jul 30, 2025
b495eed
change diagnostic category from error to extension
a-tarasyuk Aug 4, 2025
d3ea7c2
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Aug 4, 2025
7bbd741
add new diagnostic group
a-tarasyuk Aug 4, 2025
086460c
Merge branch 'main' of https://github.com/llvm/llvm-project into fix/…
a-tarasyuk Aug 4, 2025
e34a235
Merge branch 'main' into fix/147217
a-tarasyuk Aug 5, 2025
a21db7e
Merge branch 'main' into fix/147217
a-tarasyuk Aug 7, 2025
25a868c
Merge branch 'main' into fix/147217
a-tarasyuk Aug 8, 2025
aca39da
Merge branch 'main' into fix/147217
a-tarasyuk Aug 11, 2025
2a3b5db
Merge branch 'main' into fix/147217
a-tarasyuk Aug 14, 2025
7d827df
Merge branch 'main' into fix/147217
a-tarasyuk Aug 19, 2025
f065d6f
Merge branch 'main' into fix/147217
a-tarasyuk Aug 19, 2025
f9d5ccb
Merge branch 'main' into fix/147217
a-tarasyuk Aug 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ Improvements to Clang's diagnostics
- Clang now tries to avoid printing file paths that contain ``..``, instead preferring
the canonical file path if it ends up being shorter.

- Clang rejects the ``#`` and ``##`` preprocessor tokens in an attribute
argument list in C++. The operators can be used in macro replacement lists
with the usual preprocessor semantics. What is rejected are non-preprocessor
uses of the tokens. The same restrictions do not apply in C. (#GH147217)

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ def err_ms_property_expected_comma_or_rparen : Error<
"expected ',' or ')' at end of property accessor list">;
def err_ms_property_initializer : Error<
"property declaration cannot have a default member initializer">;
def err_invalid_attribute_argument
: Error<"'%0' is not allowed in an attribute argument list">;

def err_assume_attr_expects_cond_expr : Error<
"use of this expression in an %0 attribute requires parentheses">;
Expand Down
21 changes: 21 additions & 0 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4520,6 +4520,27 @@ bool Parser::ParseCXX11AttributeArgs(
Form = ParsedAttr::Form::Microsoft();
}

if (LO.CPlusPlus) {
TentativeParsingAction TPA(*this);
bool HasInvalidArgument = false;
while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof)) {
if (Tok.isOneOf(tok::hash, tok::hashhash)) {
Diag(Tok.getLocation(), diag::err_invalid_attribute_argument)
<< PP.getSpelling(Tok);
HasInvalidArgument = true;
}
ConsumeAnyToken();
}

if (HasInvalidArgument) {
SkipUntil(tok::r_paren);
TPA.Commit();
return true;
}

TPA.Revert();
}

// If the attribute isn't known, we will not attempt to parse any
// arguments.
if (Form.getSyntax() != ParsedAttr::AS_Microsoft &&
Expand Down
58 changes: 58 additions & 0 deletions clang/test/Parser/cxx0x-attributes-preprocessor-tokens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -E %s | FileCheck %s
// RUN: %clang_cc1 -x c -fsyntax-only -verify=c %s
// RUN: %clang_cc1 -x c -E %s | FileCheck %s

#define ATTR_STR(X) [[clang::annotate(#X)]]
#define ATTR_PASTE(X, Y) [[clang::annotate("test", X ## Y)]]

[[clang::assume(#)]] void f1(); // c-error {{expected expression}} \
// expected-error {{'#' is not allowed in an attribute argument list}}

[[clang::assume(##)]] void f2(); // c-error {{expected expression}} \
// expected-error {{'##' is not allowed in an attribute argument list}}

[[clang::assume(1#2#3)]] void f3(); // c-error {{use of this expression in an 'assume' attribute requires parentheses}} \
// c-error {{expected ')'}} \
// c-note {{to match this '('}} \
// expected-error {{'#' is not allowed in an attribute argument list}} \
// expected-error {{'#' is not allowed in an attribute argument list}}

[[unknown::unknown(#)]] void f4(); // c-warning {{unknown attribute 'unknown::unknown' ignored}} \
// expected-error {{'#' is not allowed in an attribute argument list}}

[[unknown::unknown(##)]] void f5(); // c-warning {{unknown attribute 'unknown::unknown' ignored}} \
// expected-error {{'##' is not allowed in an attribute argument list}}

[[unknown::unknown(1#2#3)]] void f6(); // c-warning {{unknown attribute 'unknown::unknown' ignored}} \
// expected-error {{'#' is not allowed in an attribute argument list}} \
// expected-error {{'#' is not allowed in an attribute argument list}}

[[clang::assume(%:)]] void f7(); // c-error {{expected expression}} \
// expected-error {{'%:' is not allowed in an attribute argument list}}


[[clang::assume(%:%:)]] void f8(); // c-error {{expected expression}} \
// expected-error {{'%:%:' is not allowed in an attribute argument list}}

[[clang::assume(1%:2%:3)]] void f9(); // c-error {{use of this expression in an 'assume' attribute requires parentheses}} \
// c-error {{expected ')'}} \
// c-note {{to match this '('}} \
// expected-error {{'%:' is not allowed in an attribute argument list}} \
// expected-error {{'%:' is not allowed in an attribute argument list}}

[[unknown::unknown(%:)]] void f10(); // c-warning {{unknown attribute 'unknown::unknown' ignored}} \
// expected-error {{'%:' is not allowed in an attribute argument list}}

[[unknown::unknown(%:%:)]] void f11(); // c-warning {{unknown attribute 'unknown::unknown' ignored}} \
// expected-error {{'%:%:' is not allowed in an attribute argument list}}

[[unknown::unknown(1%:2%:3)]] void f12(); // c-warning {{unknown attribute 'unknown::unknown' ignored}} \
// expected-error {{'%:' is not allowed in an attribute argument list}} \
// expected-error {{'%:' is not allowed in an attribute argument list}}

ATTR_STR(stringify) void f13();
// CHECK: {{\[\[}}clang{{::}}annotate("stringify"){{\]\]}} void f13();

ATTR_PASTE(1, 2) void f14();
// CHECK: {{\[\[}}clang{{::}}annotate("test", 12){{\]\]}} void f14();