Skip to content

Commit 1414560

Browse files
authored andcommitted
[clang-format] Fix idempotent format of hash in macro body (#118513)
Fixes #118334. (cherry picked from commit 54ca1c4)
1 parent 9969e7f commit 1414560

File tree

Image for: File tree

3 files changed

Image for: 3 files changed
+38
-1
lines changed

3 files changed

Image for: 3 files changed
+38
-1
lines changed

‎clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
512512
break;
513513
do {
514514
NextTok = Tokens->getNextToken();
515-
} while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
515+
} while (!NextTok->HasUnescapedNewline && NextTok->isNot(tok::eof));
516516

517517
while (NextTok->is(tok::comment))
518518
NextTok = Tokens->getNextToken();

‎clang/unittests/Format/FormatTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5724,6 +5724,24 @@ TEST_F(FormatTest, HashInMacroDefinition) {
57245724
getLLVMStyleWithColumns(22));
57255725

57265726
verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5727+
5728+
#if 0
5729+
// FIXME: The correct format is:
5730+
verifyFormat("{\n"
5731+
" {\n"
5732+
"#define GEN_ID(_x) char *_x{#_x}\n"
5733+
" GEN_ID(one);\n"
5734+
" }\n"
5735+
"}");
5736+
#endif
5737+
verifyFormat("{\n"
5738+
" {\n"
5739+
"#define GEN_ID(_x) \\\n"
5740+
" char *_x { #_x }\n"
5741+
" GEN_ID(one);\n"
5742+
" }\n"
5743+
"}",
5744+
getGoogleStyle());
57275745
}
57285746

57295747
TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {

‎clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,6 +3203,25 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
32033203
EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
32043204
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
32053205

3206+
Tokens = annotate("{\n"
3207+
" {\n"
3208+
"#define GEN_ID(_x) char *_x{#_x}\n"
3209+
" GEN_ID(one);\n"
3210+
" }\n"
3211+
"}");
3212+
ASSERT_EQ(Tokens.size(), 23u) << Tokens;
3213+
EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_BlockLBrace);
3214+
EXPECT_BRACE_KIND(Tokens[0], BK_Block);
3215+
EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_BlockLBrace);
3216+
EXPECT_BRACE_KIND(Tokens[1], BK_Block);
3217+
#if 0
3218+
// FIXME:
3219+
EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
3220+
EXPECT_BRACE_KIND(Tokens[14], BK_BracedInit);
3221+
#endif
3222+
EXPECT_BRACE_KIND(Tokens[20], BK_Block);
3223+
EXPECT_BRACE_KIND(Tokens[21], BK_Block);
3224+
32063225
Tokens = annotate("a = class extends goog.a {};",
32073226
getGoogleStyle(FormatStyle::LK_JavaScript));
32083227
ASSERT_EQ(Tokens.size(), 11u) << Tokens;

0 commit comments

Image for: 0 commit comments
Comments
 (0)