diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2277,12 +2277,15 @@ // Align comments for preprocessor lines with the # in column 0 if // preprocessor lines are not indented. Otherwise, align with the next // line. - (*I)->Level = - (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && - (NextNonCommentLine->Type == LT_PreprocessorDirective || - NextNonCommentLine->Type == LT_ImportStatement)) - ? 0 - : NextNonCommentLine->Level; + if ((*I)->First->TokenText != "// clang-format off" && + (*I)->First->TokenText != "/* clang-format off */") { + (*I)->Level = + (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && + (NextNonCommentLine->Type == LT_PreprocessorDirective || + NextNonCommentLine->Type == LT_ImportStatement)) + ? 0 + : NextNonCommentLine->Level; + } } else { NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr; } 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 @@ -17206,6 +17206,41 @@ "}", Style); } + +TEST_F(FormatTest, FormatTurnOffFollowedByMacro1) { + EXPECT_EQ("void main() {\n" + " // clang-format off\n" + " #define Sum(x, y) ((x) + (y))\n" + " Sum(1, 2);\n" + " #undef Sum\n" + " // clang-format on\n" + "};", + format("void main() {\n" + " // clang-format off\n" + " #define Sum(x, y) ((x) + (y))\n" + " Sum(1, 2);\n" + " #undef Sum\n" + " // clang-format on\n" + "};")); +} + +TEST_F(FormatTest, FormatTurnOffFollowedByMacro2) { + EXPECT_EQ("void main() {\n" + " // clang-format off\n" + " #define Sum(x, y) ((x) + (y))\n" + " Sum(1, 2);\n" + " #undef Sum\n" + " // clang-format on\n" + "};", + format("void main() {\n" + "// clang-format off\n" + " #define Sum(x, y) ((x) + (y))\n" + " Sum(1, 2);\n" + " #undef Sum\n" + " // clang-format on\n" + "};")); +} + } // namespace } // namespace format } // namespace clang