diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -60,7 +60,7 @@ // Update the indent level cache size so that we can rely on it // having the right size in adjustToUnmodifiedline. skipLine(Line, /*UnknownIndent=*/true); - if (Line.InPPDirective || + if ((Line.InPPDirective && !Line.InMacroBody) || (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && Line.Type == LT_CommentAbovePPDirective)) { unsigned IndentWidth = diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5021,6 +5021,7 @@ " int y = 0;\n" "}\n", style); + verifyFormat("#if 1\n" " // some comments\n" " // another\n" @@ -5040,6 +5041,44 @@ " int y = 0;\n" "}", style); + + style.ColumnLimit = 40; + style.IndentPPDirectives = FormatStyle::PPDIS_None; + verifyFormat("#ifdef foo\n" + "#define bar() \\\n" + " if (A) { \\\n" + " B(); \\\n" + " } \\\n" + " C();\n" + "#endif", + "#ifdef foo\n" + "#define bar() if (A) { B(); } C();\n" + "#endif", + style); + style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; + verifyFormat("#ifdef foo\n" + "# define bar() \\\n" + " if (A) { \\\n" + " B(); \\\n" + " } \\\n" + " C();\n" + "#endif", + "#ifdef foo\n" + "#define bar() if (A) { B(); } C();\n" + "#endif", + style); + style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; + verifyFormat("#ifdef foo\n" + " #define bar() \\\n" + " if (A) { \\\n" + " B(); \\\n" + " } \\\n" + " C();\n" + "#endif", + "#ifdef foo\n" + "#define bar() if (A) { B(); } C();\n" + "#endif", + style); } TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {