[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
https://github.com/s-barannikov closed https://github.com/llvm/llvm-project/pull/171017 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
https://github.com/AaronBallman approved this pull request. LGTM! https://github.com/llvm/llvm-project/pull/171017 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
@@ -2117,10 +2117,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName);
- bool
- ParseAttributeArgumentList(const clang::IdentifierInfo &AttrName,
- SmallVectorImpl &Exprs,
- ParsedAttributeArgumentsProperties
ArgsProperties);
+ bool parseAttributeArgumentList(
s-barannikov wrote:
I tried to document it and also reverted the naming change.
https://github.com/llvm/llvm-project/pull/171017
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
https://github.com/s-barannikov updated
https://github.com/llvm/llvm-project/pull/171017
>From e8d8e8a2fcaf17ce95471012b2451eef78e22df5 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov
Date: Sun, 7 Dec 2025 10:16:19 +0300
Subject: [PATCH 1/4] precommit test
---
clang/test/Sema/attr-modular-format.c | 4
1 file changed, 4 insertions(+)
diff --git a/clang/test/Sema/attr-modular-format.c
b/clang/test/Sema/attr-modular-format.c
index fc5b28b0b88be..1a22de115a315 100644
--- a/clang/test/Sema/attr-modular-format.c
+++ b/clang/test/Sema/attr-modular-format.c
@@ -3,6 +3,10 @@
int printf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float"))); //
no-error
int myprintf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float"))); //
expected-error {{'modular_format' attribute requires 'format' attribute}}
+int lprintf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, L"__printf", L"float"),
format(printf, 1, 2)));
+// expected-error@-1 {{'modular_format' attribute requires a string}}
+// expected-warning@-2 {{encoding prefix 'L' on an unevaluated string literal
has no effect}}
+
int dupe(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float", "int",
"float"), format(printf, 1, 2))); // expected-error {{duplicate aspect 'float'
in 'modular_format' attribute}}
int multi_dupe(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float", "int",
"float", "int"), format(printf, 1, 2))); // expected-error {{duplicate aspect
'float' in 'modular_format' attribute}} \
// expected-error {{duplicate aspect 'int' in 'modular_format' attribute}}
>From 14aa9063d863ec895b9e34ffb98c9f474c8a6fee Mon Sep 17 00:00:00 2001
From: Sergei Barannikov
Date: Sun, 7 Dec 2025 10:49:17 +0300
Subject: [PATCH 2/4] [clang] Fix string literal parsing on some attributes
At the time ParseAttributeArgumentList is called, the first argument
of an attribute may have already been parsed. We need to take this into
account when accessing ParsedAttributeArgumentsProperties mask, which
specifies which of the attribute arguments are string literals.
---
clang/include/clang/Parse/Parser.h| 7 +++
clang/lib/Parse/ParseDecl.cpp | 8
clang/test/Sema/attr-modular-format.c | 3 +--
3 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/Parse/Parser.h
b/clang/include/clang/Parse/Parser.h
index e03d7994e2fa5..373d496f19cd1 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -2117,10 +2117,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName);
- bool
- ParseAttributeArgumentList(const clang::IdentifierInfo &AttrName,
- SmallVectorImpl &Exprs,
- ParsedAttributeArgumentsProperties
ArgsProperties);
+ bool parseAttributeArgumentList(
+ const IdentifierInfo &AttrName, SmallVectorImpl &Exprs,
+ ParsedAttributeArgumentsProperties ArgsProperties, unsigned Arg);
/// Parses syntax-generic attribute arguments for attributes which are
/// known to the implementation, and adds them to the given ParsedAttributes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 8688ccf41acb5..26a1f7c0a5b93 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -421,11 +421,10 @@ Parser::ParseUnevaluatedStringInAttribute(const
IdentifierInfo &AttrName) {
return ParseUnevaluatedStringLiteralExpression();
}
-bool Parser::ParseAttributeArgumentList(
+bool Parser::parseAttributeArgumentList(
const IdentifierInfo &AttrName, SmallVectorImpl &Exprs,
-ParsedAttributeArgumentsProperties ArgsProperties) {
+ParsedAttributeArgumentsProperties ArgsProperties, unsigned Arg) {
bool SawError = false;
- unsigned Arg = 0;
while (true) {
ExprResult Expr;
if (ArgsProperties.isStringLiteralArg(Arg)) {
@@ -580,7 +579,8 @@ unsigned Parser::ParseAttributeArgsCommon(
ParsedAttributeArgumentsProperties ArgProperties =
attributeStringLiteralListArg(getTargetInfo().getTriple(), *AttrName,
Form.getSyntax(), ScopeName);
- if (ParseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties)) {
+ if (parseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties,
+ ArgExprs.size())) {
SkipUntil(tok::r_paren, StopAtSemi);
return 0;
}
diff --git a/clang/test/Sema/attr-modular-format.c
b/clang/test/Sema/attr-modular-format.c
index 1a22de115a315..b7ae519cedbeb 100644
--- a/clang/test/Sema/attr-modular-format.c
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
@@ -2117,10 +2117,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName);
- bool
- ParseAttributeArgumentList(const clang::IdentifierInfo &AttrName,
- SmallVectorImpl &Exprs,
- ParsedAttributeArgumentsProperties
ArgsProperties);
+ bool parseAttributeArgumentList(
AaronBallman wrote:
If we're going this route, I think we should document the arguments. It's not
going to be clear what the `Arg` parameter is for.
https://github.com/llvm/llvm-project/pull/171017
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
@@ -2117,10 +2117,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName);
- bool
- ParseAttributeArgumentList(const clang::IdentifierInfo &AttrName,
- SmallVectorImpl &Exprs,
- ParsedAttributeArgumentsProperties
ArgsProperties);
+ bool parseAttributeArgumentList(
AaronBallman wrote:
The other nearby attribute functions are generally going with an initial
capital letter; probably should stick with the nearby formatting instead of
changing it for just one API.
https://github.com/llvm/llvm-project/pull/171017
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: Sergei Barannikov (s-barannikov)
Changes
At the time ParseAttributeArgumentList is called, the first argument
of an attribute may have already been parsed. We need to take this into
account when accessing ParsedAttributeArgumentsProperties mask, which
specifies which of the attribute arguments are string literals.
---
Full diff: https://github.com/llvm/llvm-project/pull/171017.diff
3 Files Affected:
- (modified) clang/include/clang/Parse/Parser.h (+3-4)
- (modified) clang/lib/Parse/ParseDecl.cpp (+4-4)
- (modified) clang/test/Sema/attr-modular-format.c (+3)
``diff
diff --git a/clang/include/clang/Parse/Parser.h
b/clang/include/clang/Parse/Parser.h
index 58eb1c0a7c114..e51ae9bfa37c3 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -2117,10 +2117,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName);
- bool
- ParseAttributeArgumentList(const clang::IdentifierInfo &AttrName,
- SmallVectorImpl &Exprs,
- ParsedAttributeArgumentsProperties
ArgsProperties);
+ bool parseAttributeArgumentList(
+ const IdentifierInfo &AttrName, SmallVectorImpl &Exprs,
+ ParsedAttributeArgumentsProperties ArgsProperties, unsigned Arg);
/// Parses syntax-generic attribute arguments for attributes which are
/// known to the implementation, and adds them to the given ParsedAttributes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 8688ccf41acb5..26a1f7c0a5b93 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -421,11 +421,10 @@ Parser::ParseUnevaluatedStringInAttribute(const
IdentifierInfo &AttrName) {
return ParseUnevaluatedStringLiteralExpression();
}
-bool Parser::ParseAttributeArgumentList(
+bool Parser::parseAttributeArgumentList(
const IdentifierInfo &AttrName, SmallVectorImpl &Exprs,
-ParsedAttributeArgumentsProperties ArgsProperties) {
+ParsedAttributeArgumentsProperties ArgsProperties, unsigned Arg) {
bool SawError = false;
- unsigned Arg = 0;
while (true) {
ExprResult Expr;
if (ArgsProperties.isStringLiteralArg(Arg)) {
@@ -580,7 +579,8 @@ unsigned Parser::ParseAttributeArgsCommon(
ParsedAttributeArgumentsProperties ArgProperties =
attributeStringLiteralListArg(getTargetInfo().getTriple(), *AttrName,
Form.getSyntax(), ScopeName);
- if (ParseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties)) {
+ if (parseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties,
+ ArgExprs.size())) {
SkipUntil(tok::r_paren, StopAtSemi);
return 0;
}
diff --git a/clang/test/Sema/attr-modular-format.c
b/clang/test/Sema/attr-modular-format.c
index fc5b28b0b88be..b7ae519cedbeb 100644
--- a/clang/test/Sema/attr-modular-format.c
+++ b/clang/test/Sema/attr-modular-format.c
@@ -3,6 +3,9 @@
int printf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float"))); //
no-error
int myprintf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float"))); //
expected-error {{'modular_format' attribute requires 'format' attribute}}
+int lprintf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, L"__printf", L"float"),
format(printf, 1, 2)));
+// expected-warning@-1 2{{encoding prefix 'L' on an unevaluated string literal
has no effect}}
+
int dupe(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float", "int",
"float"), format(printf, 1, 2))); // expected-error {{duplicate aspect 'float'
in 'modular_format' attribute}}
int multi_dupe(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float", "int",
"float", "int"), format(printf, 1, 2))); // expected-error {{duplicate aspect
'float' in 'modular_format' attribute}} \
// expected-error {{duplicate aspect 'int' in 'modular_format' attribute}}
``
https://github.com/llvm/llvm-project/pull/171017
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Fix string literal parsing on some attributes (PR #171017)
https://github.com/s-barannikov created
https://github.com/llvm/llvm-project/pull/171017
At the time ParseAttributeArgumentList is called, the first argument
of an attribute may have already been parsed. We need to take this into
account when accessing ParsedAttributeArgumentsProperties mask, which
specifies which of the attribute arguments are string literals.
>From 9cdfb079145046f57d078b2b590b560555def835 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov
Date: Sun, 7 Dec 2025 10:16:19 +0300
Subject: [PATCH 1/2] precommit test
---
clang/test/Sema/attr-modular-format.c | 4
1 file changed, 4 insertions(+)
diff --git a/clang/test/Sema/attr-modular-format.c
b/clang/test/Sema/attr-modular-format.c
index fc5b28b0b88be..1a22de115a315 100644
--- a/clang/test/Sema/attr-modular-format.c
+++ b/clang/test/Sema/attr-modular-format.c
@@ -3,6 +3,10 @@
int printf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float"))); //
no-error
int myprintf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float"))); //
expected-error {{'modular_format' attribute requires 'format' attribute}}
+int lprintf(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, L"__printf", L"float"),
format(printf, 1, 2)));
+// expected-error@-1 {{'modular_format' attribute requires a string}}
+// expected-warning@-2 {{encoding prefix 'L' on an unevaluated string literal
has no effect}}
+
int dupe(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float", "int",
"float"), format(printf, 1, 2))); // expected-error {{duplicate aspect 'float'
in 'modular_format' attribute}}
int multi_dupe(const char *fmt, ...)
__attribute__((modular_format(__modular_printf, "__printf", "float", "int",
"float", "int"), format(printf, 1, 2))); // expected-error {{duplicate aspect
'float' in 'modular_format' attribute}} \
// expected-error {{duplicate aspect 'int' in 'modular_format' attribute}}
>From 0c43ad17ee059743c40147653618f79c709e597e Mon Sep 17 00:00:00 2001
From: Sergei Barannikov
Date: Sun, 7 Dec 2025 10:49:17 +0300
Subject: [PATCH 2/2] [clang] Fix string literal parsing on some attributes
At the time ParseAttributeArgumentList is called, the first argument
of an attribute may have already been parsed. We need to take this into
account when accessing ParsedAttributeArgumentsProperties mask, which
specifies which of the attribute arguments are string literals.
---
clang/include/clang/Parse/Parser.h| 7 +++
clang/lib/Parse/ParseDecl.cpp | 8
clang/test/Sema/attr-modular-format.c | 3 +--
3 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/Parse/Parser.h
b/clang/include/clang/Parse/Parser.h
index 58eb1c0a7c114..e51ae9bfa37c3 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -2117,10 +2117,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName);
- bool
- ParseAttributeArgumentList(const clang::IdentifierInfo &AttrName,
- SmallVectorImpl &Exprs,
- ParsedAttributeArgumentsProperties
ArgsProperties);
+ bool parseAttributeArgumentList(
+ const IdentifierInfo &AttrName, SmallVectorImpl &Exprs,
+ ParsedAttributeArgumentsProperties ArgsProperties, unsigned Arg);
/// Parses syntax-generic attribute arguments for attributes which are
/// known to the implementation, and adds them to the given ParsedAttributes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 8688ccf41acb5..26a1f7c0a5b93 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -421,11 +421,10 @@ Parser::ParseUnevaluatedStringInAttribute(const
IdentifierInfo &AttrName) {
return ParseUnevaluatedStringLiteralExpression();
}
-bool Parser::ParseAttributeArgumentList(
+bool Parser::parseAttributeArgumentList(
const IdentifierInfo &AttrName, SmallVectorImpl &Exprs,
-ParsedAttributeArgumentsProperties ArgsProperties) {
+ParsedAttributeArgumentsProperties ArgsProperties, unsigned Arg) {
bool SawError = false;
- unsigned Arg = 0;
while (true) {
ExprResult Expr;
if (ArgsProperties.isStringLiteralArg(Arg)) {
@@ -580,7 +579,8 @@ unsigned Parser::ParseAttributeArgsCommon(
ParsedAttributeArgumentsProperties ArgProperties =
attributeStringLiteralListArg(getTargetInfo().getTriple(), *AttrName,
Form.getSyntax(), ScopeName);
- if (ParseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties)) {
+ if (parseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties,
+
