[PATCH] D89918: Fix issue: clang-format result is not consistent if "// clang-format off" is followed by macro definition.
Yubo Xie via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 21 17:16:35 PDT 2020
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;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89918.299827.patch
Type: text/x-patch
Size: 2700 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201022/84dddcbd/attachment.bin>
More information about the cfe-commits
mailing list