[clang] [BoundsSafety] Unify ParseLexedAttributeTokens (PR #206689)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 30 02:32:12 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mohammed Ashraf (Holo-xy)
<details>
<summary>Changes</summary>
This PR is a continuation of the previous [Unify ParseLexedAttribute PR](https://github.com/llvm/llvm-project/pull/186033)
---
Full diff: https://github.com/llvm/llvm-project/pull/206689.diff
3 Files Affected:
- (modified) clang/include/clang/Parse/Parser.h (+2-2)
- (modified) clang/lib/Parse/ParseCXXInlineMethods.cpp (+6-33)
- (modified) clang/lib/Parse/ParseDecl.cpp (+14-12)
``````````diff
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index f0e06473bf615..d07f4467a17a3 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -1524,9 +1524,9 @@ class Parser : public CodeCompletionHandler {
ParsedAttributes &OutAttrs);
/// Parse cached tokens for a late-parsed attribute and return the parsed
- /// attributes. Shared implementation used by both ParseLexedCAttribute and
+ /// attributes. Shared implementation used by both ParseLexedAttribute and
/// ParseLexedTypeAttribute.
- ParsedAttributes ParseLexedCAttributeTokens(LateParsedAttribute &LA);
+ ParsedAttributes ParseLexedAttributeTokens(LateParsedAttribute &LPA);
/// Helper function to move LateParsedTypeAttribute pointers from one list
/// to another. Filters type attributes from \p From and appends them to \p
diff --git a/clang/lib/Parse/ParseCXXInlineMethods.cpp b/clang/lib/Parse/ParseCXXInlineMethods.cpp
index d13f73641218b..5011f5a76cb53 100644
--- a/clang/lib/Parse/ParseCXXInlineMethods.cpp
+++ b/clang/lib/Parse/ParseCXXInlineMethods.cpp
@@ -741,22 +741,6 @@ void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
void Parser::ParseLexedAttribute(LateParsedAttribute &LPA, bool EnterScope,
bool OnDefinition,
ParsedAttributes *OutAttrs) {
- // Create a fake EOF so that attribute parsing won't go off the end of the
- // attribute.
- Token AttrEnd;
- AttrEnd.startToken();
- AttrEnd.setKind(tok::eof);
- AttrEnd.setLocation(Tok.getLocation());
- AttrEnd.setEofData(LPA.Toks.data());
- LPA.Toks.push_back(AttrEnd);
-
- // Append the current token at the end of the new token stream so that it
- // doesn't get lost.
- LPA.Toks.push_back(Tok);
- PP.EnterTokenStream(LPA.Toks, true, /*IsReinject=*/true);
- // Consume the previously pushed token.
- ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
-
ParsedAttributes Attrs(AttrFactory);
if (LPA.Decls.size() > 0) {
@@ -783,20 +767,17 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LPA, bool EnterScope,
Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
}
- ParseGNUAttributeArgs(&LPA.AttrName, LPA.AttrNameLoc, Attrs,
- /*EndLoc=*/nullptr, /*ScopeName=*/nullptr,
- SourceLocation(), ParsedAttr::Form::GNU(),
- /*D=*/nullptr);
+ ParsedAttributes Parsed = ParseLexedAttributeTokens(LPA);
+ Attrs.takeAllAppendingFrom(Parsed);
if (HasFuncScope)
Actions.ActOnExitFunctionContext();
} else if (OutAttrs) {
- ParseGNUAttributeArgs(&LPA.AttrName, LPA.AttrNameLoc, Attrs,
- /*EndLoc=*/nullptr, /*ScopeName=*/nullptr,
- SourceLocation(), ParsedAttr::Form::GNU(),
- /*D=*/nullptr);
+ ParsedAttributes Parsed = ParseLexedAttributeTokens(LPA);
+ Attrs.takeAllAppendingFrom(Parsed);
} else {
- Diag(Tok, diag::warn_attribute_no_decl) << LPA.AttrName.getName();
+ Diag(LPA.AttrNameLoc, diag::warn_attribute_no_decl)
+ << LPA.AttrName.getName();
}
if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() &&
@@ -806,14 +787,6 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LPA, bool EnterScope,
for (auto *D : LPA.Decls)
Actions.ActOnFinishDelayedAttribute(getCurScope(), D, Attrs);
- // Due to a parsing error, we either went over the cached tokens or
- // there are still cached tokens left, so we skip the leftover tokens.
- while (Tok.isNot(tok::eof))
- ConsumeAnyToken();
-
- if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
- ConsumeAnyToken();
-
if (OutAttrs)
OutAttrs->takeAllAppendingFrom(Attrs);
}
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 3f41e7c5c6f0d..015c1125c0e48 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4847,33 +4847,32 @@ void Parser::ParseStructDeclaration(
}
}
-ParsedAttributes Parser::ParseLexedCAttributeTokens(LateParsedAttribute &LA) {
+ParsedAttributes Parser::ParseLexedAttributeTokens(LateParsedAttribute &LPA) {
// Create a fake EOF so that attribute parsing won't go off the end of the
// attribute.
Token AttrEnd;
AttrEnd.startToken();
AttrEnd.setKind(tok::eof);
AttrEnd.setLocation(Tok.getLocation());
- AttrEnd.setEofData(LA.Toks.data());
- LA.Toks.push_back(AttrEnd);
+ AttrEnd.setEofData(LPA.Toks.data());
+ LPA.Toks.push_back(AttrEnd);
// Append the current token at the end of the new token stream so that it
// doesn't get lost.
- LA.Toks.push_back(Tok);
- PP.EnterTokenStream(LA.Toks, /*DisableMacroExpansion=*/true,
+ LPA.Toks.push_back(Tok);
+ PP.EnterTokenStream(LPA.Toks, /*DisableMacroExpansion=*/true,
/*IsReinject=*/true);
+
// Drop the current token and bring the first cached one. It's the same token
// as when we entered this function.
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
ParsedAttributes Attrs(AttrFactory);
- assert(LA.Decls.size() <= 1 &&
- "late field attribute expects to have at most one declaration.");
-
- // Dispatch based on the attribute and parse it
- ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr, nullptr,
- SourceLocation(), ParsedAttr::Form::GNU(), nullptr);
+ ParseGNUAttributeArgs(&LPA.AttrName, LPA.AttrNameLoc, Attrs,
+ /*EndLoc=*/nullptr, /*ScopeName=*/nullptr,
+ SourceLocation(), ParsedAttr::Form::GNU(),
+ /*D=*/nullptr);
// Due to a parsing error, we either went over the cached tokens or
// there are still cached tokens left, so we skip the leftover tokens.
@@ -4889,7 +4888,10 @@ ParsedAttributes Parser::ParseLexedCAttributeTokens(LateParsedAttribute &LA) {
void Parser::ParseLexedTypeAttribute(LateParsedTypeAttribute &LA,
ParsedAttributes &OutAttrs) {
- ParsedAttributes Attrs = ParseLexedCAttributeTokens(LA);
+ assert(LA.Decls.size() <= 1 &&
+ "late field attribute expects to have at most one declaration.");
+
+ ParsedAttributes Attrs = ParseLexedAttributeTokens(LA);
OutAttrs.takeAllAppendingFrom(Attrs);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/206689
More information about the cfe-commits
mailing list