aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2025-04-30 19:56:22 -0700
committerOwen Pan <owenpiano@gmail.com>2025-04-30 19:58:59 -0700
commit8effc8da292bfacb823a7e3c4134296da481fedc (patch)
tree6877bf8d5ec5c48ca744e0bdb20cb6074d2985a1 /clang/unittests/Format
parentc588224ca797886064a7a79f6c0114a6963c325e (diff)
downloadllvm-8effc8da292bfacb823a7e3c4134296da481fedc.zip
llvm-8effc8da292bfacb823a7e3c4134296da481fedc.tar.gz
llvm-8effc8da292bfacb823a7e3c4134296da481fedc.tar.bz2
Reland [clang-format] Add OneLineFormatOffRegex option (#137577)
Diffstat (limited to 'clang/unittests/Format')
-rw-r--r--clang/unittests/Format/ConfigParseTest.cpp1
-rw-r--r--clang/unittests/Format/FormatTest.cpp99
2 files changed, 100 insertions, 0 deletions
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 2b08b79..f7ab554 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -295,6 +295,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
+ CHECK_PARSE("OneLineFormatOffRegex: // ab$", OneLineFormatOffRegex, "// ab$");
Style.QualifierAlignment = FormatStyle::QAS_Right;
CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 333d40d..c4fcc55 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24954,6 +24954,105 @@ TEST_F(FormatTest, DisableRegions) {
"// clang-format on");
}
+TEST_F(FormatTest, OneLineFormatOffRegex) {
+ auto Style = getLLVMStyle();
+ Style.OneLineFormatOffRegex = "// format off$";
+
+ verifyFormat(" // format off\n"
+ " int i ;\n"
+ "int j;",
+ " // format off\n"
+ " int i ;\n"
+ " int j ;",
+ Style);
+ verifyFormat("// format off?\n"
+ "int i;",
+ " // format off?\n"
+ " int i ;",
+ Style);
+ verifyFormat("f(\"// format off\");", " f(\"// format off\") ;", Style);
+
+ verifyFormat("int i;\n"
+ " // format off\n"
+ " int j ;\n"
+ "int k;",
+ " int i ;\n"
+ " // format off\n"
+ " int j ;\n"
+ " int k ;",
+ Style);
+
+ verifyFormat(" // format off\n"
+ "\n"
+ "int i;",
+ " // format off\n"
+ " \n"
+ " int i ;",
+ Style);
+
+ verifyFormat("int i;\n"
+ " int j ; // format off\n"
+ "int k;",
+ " int i ;\n"
+ " int j ; // format off\n"
+ " int k ;",
+ Style);
+
+ verifyFormat("// clang-format off\n"
+ " int i ;\n"
+ " int j ; // format off\n"
+ " int k ;\n"
+ "// clang-format on\n"
+ "f();",
+ " // clang-format off\n"
+ " int i ;\n"
+ " int j ; // format off\n"
+ " int k ;\n"
+ " // clang-format on\n"
+ " f() ;",
+ Style);
+
+ Style.OneLineFormatOffRegex = "^/\\* format off \\*/";
+ verifyFormat("int i;\n"
+ " /* format off */ int j ;\n"
+ "int k;",
+ " int i ;\n"
+ " /* format off */ int j ;\n"
+ " int k ;",
+ Style);
+ verifyFormat("f(\"/* format off */\");", " f(\"/* format off */\") ;", Style);
+
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
+ verifyFormat("#define A \\\n"
+ " do { \\\n"
+ " /* format off */\\\n"
+ " f() ; \\\n"
+ " g(); \\\n"
+ " } while (0)",
+ "# define A\\\n"
+ " do{ \\\n"
+ " /* format off */\\\n"
+ " f() ; \\\n"
+ " g() ;\\\n"
+ " } while (0 )",
+ Style);
+
+ Style.ColumnLimit = 50;
+ Style.OneLineFormatOffRegex = "^LogErrorPrint$";
+ verifyFormat(" myproject::LogErrorPrint(logger, \"Don't split me!\");\n"
+ "myproject::MyLogErrorPrinter(myLogger,\n"
+ " \"Split me!\");",
+ " myproject::LogErrorPrint(logger, \"Don't split me!\");\n"
+ " myproject::MyLogErrorPrinter(myLogger, \"Split me!\");",
+ Style);
+
+ Style.OneLineFormatOffRegex = "//(< clang-format off| NO_TRANSLATION)$";
+ verifyNoChange(
+ " int i ; //< clang-format off\n"
+ " msg = sprintf(\"Long string with placeholders.\"); // NO_TRANSLATION",
+ Style);
+}
+
TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
format("? ) =");
verifyNoCrash("#define a\\\n /**/}");