-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[clang-format] Fix a regression in annotating BK_BracedInit #87450
Merged
Conversation
This file contains 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
owenca
requested review from
HazardyKnusperkeks,
hnakamura5,
rymiel and
mydeveloperday
April 3, 2024 04:47
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #86539. Full diff: https://github.com/llvm/llvm-project/pull/87450.diff 3 Files Affected:
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 98ae1c8f62bbc2..b11114465f5c48 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -495,20 +495,22 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
};
SmallVector<StackEntry, 8> LBraceStack;
assert(Tok->is(tok::l_brace));
+
do {
- // Get next non-comment, non-preprocessor token.
FormatToken *NextTok;
do {
NextTok = Tokens->getNextToken();
} while (NextTok->is(tok::comment));
- if (!Style.isTableGen()) {
- // InTableGen, '#' is like binary operator. Not a preprocessor directive.
- while (NextTok->is(tok::hash) && !Line->InMacroBody) {
- NextTok = Tokens->getNextToken();
+
+ if (!Line->InMacroBody) {
+ // Skip PPDirective lines and comments.
+ while (NextTok->is(tok::hash)) {
do {
NextTok = Tokens->getNextToken();
- } while (NextTok->is(tok::comment) ||
- (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof)));
+ } while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
+
+ while (NextTok->is(tok::comment))
+ NextTok = Tokens->getNextToken();
}
}
@@ -640,6 +642,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
default:
break;
}
+
PrevTok = Tok;
Tok = NextTok;
} while (Tok->isNot(tok::eof) && !LBraceStack.empty());
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 33dec7dae319f0..768e5ad1d1f433 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27235,6 +27235,18 @@ TEST_F(FormatTest, PPBranchesInBracedInit) {
"};");
}
+TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
+ verifyFormat("{\n"
+ " char *a[] = {\n"
+ " /* abc */ \"abc\",\n"
+ "#if FOO\n"
+ " /* xyz */ \"xyz\",\n"
+ "#endif\n"
+ " /* last */ \"last\"};\n"
+ "}",
+ getLLVMStyleWithColumns(30));
+}
+
TEST_F(FormatTest, StreamOutputOperator) {
verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
verifyFormat("std::cout << \"foo\\n\"\n"
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 2539d3d76ef019..293b8481f5636f 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2805,6 +2805,19 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
+
+ Tokens = annotate("{\n"
+ " char *a[] = {\n"
+ " /* abc */ \"abc\",\n"
+ "#if FOO\n"
+ " /* xyz */ \"xyz\",\n"
+ "#endif\n"
+ " /* last */ \"last\"};\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 25u) << Tokens;
+ EXPECT_BRACE_KIND(Tokens[0], BK_Block);
+ EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
+ EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
}
TEST_F(TokenAnnotatorTest, StreamOperator) {
|
hnakamura5
previously requested changes
Apr 3, 2024
HazardyKnusperkeks
approved these changes
Apr 4, 2024
/cherry-pick 7c9c38e |
Failed to cherry-pick: 7c9c38e https://github.com/llvm/llvm-project/actions/runs/8756450944 Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request |
owenca
added a commit
to owenca/llvm-project
that referenced
this pull request
Apr 20, 2024
tstellar
pushed a commit
that referenced
this pull request
Apr 25, 2024
Closed
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.
Fixes #86539.