xyb created this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. xyb requested review of this revision.
For example, the above code: void main() { // clang-format off #define Sum(x, y) ((x) + (y)) Sum(1, 2); #undef Sum // clang-format on } If we run clang-format we will get the following result: void main() { // clang-format off #define Sum(x, y) ((x) + (y)) Sum(1, 2); #undef Sum // clang-format on } But if we run clang-format again, we will get another result: void main() { // clang-format off #define Sum(x, y) ((x) + (y)) Sum(1, 2); #undef Sum // clang-format on } I think the expectation should be "no mater how many times clang-format runs, the result should be the same." This patch tries to fix this issue. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D89918 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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 Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ 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; }
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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 Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ 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; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits