Skip to content

Commit

Permalink
[clang-format] Avoid merging macro definitions.
Browse files Browse the repository at this point in the history
Fixes #42087.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118879
  • Loading branch information
mkurdej committed Feb 3, 2022
1 parent 529aa4b commit ca0d970
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ class LineJoiner {
tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
SmallVectorImpl<AnnotatedLine *>::const_iterator E,
unsigned Limit) {
// Don't merge with a preprocessor directive.
if (I[1]->Type == LT_PreprocessorDirective)
return 0;

AnnotatedLine &Line = **I;

// Don't merge ObjC @ keywords and methods.
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,21 @@ TEST_F(FormatTest, UnderstandsMacros) {
verifyFormat("#define xor(x) (^(x))");
verifyFormat("#define __except(x)");
verifyFormat("#define __try(x)");

FormatStyle Style = getLLVMStyle();
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterFunction = true;
// Test that a macro definition never gets merged with the following
// definition.
// FIXME: The AAA macro definition probably should not be split into 3 lines.
verifyFormat("#define AAA "
" \\\n"
" N "
" \\\n"
" {\n"
"#define BBB }\n",
Style);
// verifyFormat("#define AAA N { //\n", Style);
}

TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
Expand Down

0 comments on commit ca0d970

Please sign in to comment.