Skip to content

[Clang] disallow # operators in attribute argument lists #147308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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();