-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] Support of TableGen tokens with unary operator like form, bang operators and numeric literals. #78996
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
Merged
hnakamura5
merged 1 commit into
llvm:main
from
hnakamura5:tablegen_unary_operator_like_token
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,13 +276,44 @@ void FormatTokenLexer::tryMergePreviousTokens() { | |
return; | ||
} | ||
} | ||
// TableGen's Multi line string starts with [{ | ||
if (Style.isTableGen() && tryMergeTokens({tok::l_square, tok::l_brace}, | ||
TT_TableGenMultiLineString)) { | ||
// Set again with finalizing. This must never be annotated as other types. | ||
Tokens.back()->setFinalizedType(TT_TableGenMultiLineString); | ||
Tokens.back()->Tok.setKind(tok::string_literal); | ||
return; | ||
if (Style.isTableGen()) { | ||
// TableGen's Multi line string starts with [{ | ||
if (tryMergeTokens({tok::l_square, tok::l_brace}, | ||
TT_TableGenMultiLineString)) { | ||
// Set again with finalizing. This must never be annotated as other types. | ||
Tokens.back()->setFinalizedType(TT_TableGenMultiLineString); | ||
Tokens.back()->Tok.setKind(tok::string_literal); | ||
return; | ||
} | ||
// TableGen's bang operator is the form !<name>. | ||
// !cond is a special case with specific syntax. | ||
if (tryMergeTokens({tok::exclaim, tok::identifier}, | ||
TT_TableGenBangOperator)) { | ||
Tokens.back()->Tok.setKind(tok::identifier); | ||
Tokens.back()->Tok.setIdentifierInfo(nullptr); | ||
if (Tokens.back()->TokenText == "!cond") | ||
Tokens.back()->setFinalizedType(TT_TableGenCondOperator); | ||
else | ||
Tokens.back()->setFinalizedType(TT_TableGenBangOperator); | ||
return; | ||
} | ||
if (tryMergeTokens({tok::exclaim, tok::kw_if}, TT_TableGenBangOperator)) { | ||
// Here, "! if" becomes "!if". That is, ! captures if even when the space | ||
// exists. That is only one possibility in TableGen's syntax. | ||
Tokens.back()->Tok.setKind(tok::identifier); | ||
Tokens.back()->Tok.setIdentifierInfo(nullptr); | ||
Tokens.back()->setFinalizedType(TT_TableGenBangOperator); | ||
return; | ||
} | ||
// +, - with numbers are literals. Not unary operators. | ||
if (tryMergeTokens({tok::plus, tok::numeric_constant}, TT_Unknown)) { | ||
Tokens.back()->Tok.setKind(tok::numeric_constant); | ||
return; | ||
} | ||
if (tryMergeTokens({tok::minus, tok::numeric_constant}, TT_Unknown)) { | ||
Tokens.back()->Tok.setKind(tok::numeric_constant); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
} | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a TableGen expert, but what does this do to
[Offset + 1]
type code in a td file? could we build a better set of FormatTableGen unit tests to ensure we don't cause any regressions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://llvm.org/docs/TableGen/ProgRef.html#values-and-expressions
As far as I read from the manual, TableGen does not have
+
as infix binary operator.And as noted in the warning above,
-
is lexed as the integer's prefix rather than infix operator for range and slice.I agree, and actually there is a comprehensive set of unit test for TableGen's syntax here. (Even this may be missing real examples of TableGen usage in target definition, mlir and so on.)
https://github.com/llvm/llvm-project/pull/76059/files#diff-2ce45a84684fe19d813e79bab2f732809f3544d38f344e3d2cfe23aa9216a1c8
Current pull request is separated from this PR. I'm wondering when to add the test. Because now it only recognizes tokens, and cannot format many part of that yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mydeveloperday
Thank you for reviewing this pull request.
Now it comes to 1 week since this PR is started. I want to continue before I forget.
Could you mind accepting or adding some suggestion?
Or if you do not intend neither, can I request another reviewer?