From ca0d97072e79f8d7159c50db616647eeb1b094b8 Mon Sep 17 00:00:00 2001 From: Marek Kurdej Date: Thu, 3 Feb 2022 18:46:35 +0100 Subject: [PATCH] [clang-format] Avoid merging macro definitions. Fixes https://github.com/llvm/llvm-project/issues/42087. Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D118879 --- clang/lib/Format/UnwrappedLineFormatter.cpp | 4 ++++ clang/unittests/Format/FormatTest.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 08ba442b5f0e..f7d26b813f4f 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -621,6 +621,10 @@ private: tryMergeSimpleBlock(SmallVectorImpl::const_iterator I, SmallVectorImpl::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. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 3d9e38616450..094b9142aa10 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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) {