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 1 commit
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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ Improvements to Clang's diagnostics
false positives in exception-heavy code, though only simple patterns
are currently recognized.

- Clang now rejects ``#`` operators in attribute argument lists. (#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 attribute argument lists">;

def err_assume_attr_expects_cond_expr : Error<
"use of this expression in an %0 attribute requires parentheses">;
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ unsigned Parser::ParseAttributeArgsCommon(
bool AttributeHasVariadicIdentifierArg =
attributeHasVariadicIdentifierArg(*AttrName, Form.getSyntax(), ScopeName);

if (Tok.is(tok::hash) || Tok.is(tok::hashhash)) {
Diag(Tok.getLocation(), diag::err_invalid_attribute_argument)
<< PP.getSpelling(Tok);
SkipUntil(tok::r_paren, StopAtSemi);
return 0;
}

// Interpret "kw_this" as an identifier if the attributed requests it.
if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
Tok.setKind(tok::identifier);
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Parser/cxx0x-attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,8 @@ namespace P2361 {
}

alignas(int) struct AlignAsAttribute {}; // expected-error {{misplaced attributes; expected attributes here}}

namespace GH147217 {
[[clang::annotate(#)]] void a(); // expected-error {{'#' is not allowed in attribute argument lists}}
[[clang::annotate(##)]] void b(); // expected-error {{'##' is not allowed in attribute argument lists}}
}
Loading