Skip to content

Commit ca0d970

Browse files
committed
[clang-format] Avoid merging macro definitions.
Fixes #42087. Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D118879
1 parent 529aa4b commit ca0d970

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,10 @@ class LineJoiner {
621621
tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
622622
SmallVectorImpl<AnnotatedLine *>::const_iterator E,
623623
unsigned Limit) {
624+
// Don't merge with a preprocessor directive.
625+
if (I[1]->Type == LT_PreprocessorDirective)
626+
return 0;
627+
624628
AnnotatedLine &Line = **I;
625629

626630
// Don't merge ObjC @ keywords and methods.

clang/unittests/Format/FormatTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,21 @@ TEST_F(FormatTest, UnderstandsMacros) {
18051805
verifyFormat("#define xor(x) (^(x))");
18061806
verifyFormat("#define __except(x)");
18071807
verifyFormat("#define __try(x)");
1808+
1809+
FormatStyle Style = getLLVMStyle();
1810+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1811+
Style.BraceWrapping.AfterFunction = true;
1812+
// Test that a macro definition never gets merged with the following
1813+
// definition.
1814+
// FIXME: The AAA macro definition probably should not be split into 3 lines.
1815+
verifyFormat("#define AAA "
1816+
" \\\n"
1817+
" N "
1818+
" \\\n"
1819+
" {\n"
1820+
"#define BBB }\n",
1821+
Style);
1822+
// verifyFormat("#define AAA N { //\n", Style);
18081823
}
18091824

18101825
TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {

0 commit comments

Comments
 (0)