Skip to content

Commit

Permalink
[clang-format] Fix idempotent format of hash in macro body (#118513)
Browse files Browse the repository at this point in the history
Fixes #118334.

(cherry picked from commit 54ca1c4)
  • Loading branch information
owenca authored and tru committed Jan 13, 2025
1 parent 9969e7f commit 1414560
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
break;
do {
NextTok = Tokens->getNextToken();
} while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
} while (!NextTok->HasUnescapedNewline && NextTok->isNot(tok::eof));

while (NextTok->is(tok::comment))
NextTok = Tokens->getNextToken();
Expand Down
18 changes: 18 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5724,6 +5724,24 @@ TEST_F(FormatTest, HashInMacroDefinition) {
getLLVMStyleWithColumns(22));

verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));

#if 0
// FIXME: The correct format is:
verifyFormat("{\n"
" {\n"
"#define GEN_ID(_x) char *_x{#_x}\n"
" GEN_ID(one);\n"
" }\n"
"}");
#endif
verifyFormat("{\n"
" {\n"
"#define GEN_ID(_x) \\\n"
" char *_x { #_x }\n"
" GEN_ID(one);\n"
" }\n"
"}",
getGoogleStyle());
}

TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
Expand Down
19 changes: 19 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3203,6 +3203,25 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);

Tokens = annotate("{\n"
" {\n"
"#define GEN_ID(_x) char *_x{#_x}\n"
" GEN_ID(one);\n"
" }\n"
"}");
ASSERT_EQ(Tokens.size(), 23u) << Tokens;
EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_BlockLBrace);
EXPECT_BRACE_KIND(Tokens[0], BK_Block);
EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_BlockLBrace);
EXPECT_BRACE_KIND(Tokens[1], BK_Block);
#if 0
// FIXME:
EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
EXPECT_BRACE_KIND(Tokens[14], BK_BracedInit);
#endif
EXPECT_BRACE_KIND(Tokens[20], BK_Block);
EXPECT_BRACE_KIND(Tokens[21], BK_Block);

Tokens = annotate("a = class extends goog.a {};",
getGoogleStyle(FormatStyle::LK_JavaScript));
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
Expand Down

0 comments on commit 1414560

Please sign in to comment.