-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format][NFC] Annotate control statement r_braces #68621
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
HazardyKnusperkeks
merged 1 commit into
llvm:main
from
HazardyKnusperkeks:annotate-control-r-brace
Oct 13, 2023
Merged
[clang-format][NFC] Annotate control statement r_braces #68621
HazardyKnusperkeks
merged 1 commit into
llvm:main
from
HazardyKnusperkeks:annotate-control-r-brace
Oct 13, 2023
Conversation
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
@llvm/pr-subscribers-clang-format ChangesAnnotating switch braces for the first time. Also in preparation of #67906. Full diff: https://github.com/llvm/llvm-project/pull/68621.diff 3 Files Affected:
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 527f1d744a58089..606e9e790ad833b 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -52,6 +52,7 @@ namespace format {
TYPE(ConflictStart) \
/* l_brace of if/for/while */ \
TYPE(ControlStatementLBrace) \
+ TYPE(ControlStatementRBrace) \
TYPE(CppCastLParen) \
TYPE(CSharpGenericTypeConstraint) \
TYPE(CSharpGenericTypeConstraintColon) \
@@ -67,6 +68,7 @@ namespace format {
TYPE(DesignatedInitializerPeriod) \
TYPE(DictLiteral) \
TYPE(ElseLBrace) \
+ TYPE(ElseRBrace) \
TYPE(EnumLBrace) \
TYPE(EnumRBrace) \
TYPE(FatArrow) \
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 3275d7b6a71aaa0..9769d536bee32aa 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2756,6 +2756,10 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
CompoundStatementIndenter Indenter(this, Style, Line->Level);
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
/*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
+ if (auto Prev = FormatTok->getPreviousNonComment();
+ Prev && Prev->is(tok::r_brace)) {
+ Prev->setFinalizedType(TT_ControlStatementRBrace);
+ }
if (Style.BraceWrapping.BeforeElse)
addUnwrappedLine();
else
@@ -2794,6 +2798,10 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
FormatToken *IfLBrace =
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
/*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
+ if (auto Prev = FormatTok->getPreviousNonComment();
+ Prev && Prev->is(tok::r_brace)) {
+ Prev->setFinalizedType(TT_ElseRBrace);
+ }
if (FormatTok->is(tok::kw_else)) {
KeepElseBraces = KeepElseBraces ||
ElseBlockKind == IfStmtKind::IfOnly ||
@@ -3057,12 +3065,15 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
keepAncestorBraces();
if (isBlockBegin(*FormatTok)) {
- if (!KeepBraces)
- FormatTok->setFinalizedType(TT_ControlStatementLBrace);
+ FormatTok->setFinalizedType(TT_ControlStatementLBrace);
FormatToken *LeftBrace = FormatTok;
CompoundStatementIndenter Indenter(this, Style, Line->Level);
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
/*MunchSemi=*/true, KeepBraces);
+ if (auto Prev = FormatTok->getPreviousNonComment();
+ Prev && Prev->is(tok::r_brace)) {
+ Prev->setFinalizedType(TT_ControlStatementRBrace);
+ }
if (!KeepBraces) {
assert(!NestedTooDeep.empty());
if (!NestedTooDeep.back())
@@ -3196,7 +3207,12 @@ void UnwrappedLineParser::parseSwitch() {
if (FormatTok->is(tok::l_brace)) {
CompoundStatementIndenter Indenter(this, Style, Line->Level);
+ FormatTok->setFinalizedType(TT_ControlStatementLBrace);
parseBlock();
+ if (auto Prev = FormatTok->getPreviousNonComment();
+ Prev && Prev->is(tok::r_brace)) {
+ Prev->setFinalizedType(TT_ControlStatementRBrace);
+ }
addUnwrappedLine();
} else {
addUnwrappedLine();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 2d590f2af05e63a..b6d4cf166de0281 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2151,6 +2151,37 @@ TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
}
+TEST_F(TokenAnnotatorTest, UnderstandsControlStatements) {
+ auto Tokens = annotate("while (true) {}");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
+ EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
+
+ Tokens = annotate("for (;;) {}");
+ ASSERT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_ControlStatementLBrace);
+ EXPECT_TOKEN(Tokens[6], tok::r_brace, TT_ControlStatementRBrace);
+
+ Tokens = annotate("do {} while (true);");
+ ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+ EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_ControlStatementLBrace);
+ EXPECT_TOKEN(Tokens[2], tok::r_brace, TT_ControlStatementRBrace);
+
+ Tokens = annotate("if (true) {} else if (false) {} else {}");
+ ASSERT_EQ(Tokens.size(), 17u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
+ EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
+ EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_ControlStatementLBrace);
+ EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_ControlStatementRBrace);
+ EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_ElseLBrace);
+ EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_ElseRBrace);
+
+ Tokens = annotate("switch (foo) {}");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
+ EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
+}
+
} // namespace
} // namespace format
} // namespace clang
|
mydeveloperday
approved these changes
Oct 10, 2023
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.
LGTM
owenca
approved these changes
Oct 10, 2023
Annotating switch braces for the first time. Extract the type setting into its own function. Also in preparation of llvm#67906.
358cbf4
to
a7425ee
Compare
owenca
approved these changes
Oct 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Annotating switch braces for the first time.
Also in preparation of #67906.