[clang-format] Adds a canonical delimiter to raw string formatting
Summary:
This patch adds canonical delimiter support to the raw string formatting.
This allows matching delimiters to be updated to the canonical one.
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D42187
llvm-svn: 322956
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ac5184e..dcb1089 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10429,13 +10429,15 @@
FormatStyle::LK_TextProto,
{"pb", "proto"},
{"PARSE_TEXT_PROTO"},
+ /*CanonicalDelimiter=*/"",
"llvm",
},
{
FormatStyle::LK_Cpp,
{"cc", "cpp"},
{"C_CODEBLOCK", "CPPEVAL"},
- "",
+ /*CanonicalDelimiter=*/"cc",
+ /*BasedOnStyle=*/"",
},
};
@@ -10453,7 +10455,8 @@
" - 'cpp'\n"
" EnclosingFunctions:\n"
" - 'C_CODEBLOCK'\n"
- " - 'CPPEVAL'\n",
+ " - 'CPPEVAL'\n"
+ " CanonicalDelimiter: 'cc'",
RawStringFormats, ExpectedRawStringFormats);
}
diff --git a/clang/unittests/Format/FormatTestRawStrings.cpp b/clang/unittests/Format/FormatTestRawStrings.cpp
index d5ba792..941aa2e 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/unittests/Format/FormatTestRawStrings.cpp
@@ -66,10 +66,13 @@
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = ColumnLimit;
Style.RawStringFormats = {
- {/*Language=*/FormatStyle::LK_TextProto,
- /*Delimiters=*/{"pb"},
- /*EnclosingFunctions=*/{},
- /*BasedOnStyle=*/"google"},
+ {
+ /*Language=*/FormatStyle::LK_TextProto,
+ /*Delimiters=*/{"pb"},
+ /*EnclosingFunctions=*/{},
+ /*CanonicalDelimiter=*/"",
+ /*BasedOnStyle=*/"google",
+ },
};
return Style;
}
@@ -77,9 +80,13 @@
FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
FormatStyle Style = getLLVMStyle();
Style.RawStringFormats = {
- {/*Language=*/FormatStyle::LK_Cpp,
- /*Delimiters=*/{"cpp"},
- /*EnclosingFunctions=*/{}, BasedOnStyle},
+ {
+ /*Language=*/FormatStyle::LK_Cpp,
+ /*Delimiters=*/{"cpp"},
+ /*EnclosingFunctions=*/{},
+ /*CanonicalDelimiter=*/"",
+ BasedOnStyle,
+ },
};
return Style;
}
@@ -87,9 +94,13 @@
FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
Style.RawStringFormats = {
- {/*Language=*/FormatStyle::LK_Cpp,
- /*Delimiters=*/{"cpp"},
- /*EnclosingFunctions=*/{}, BasedOnStyle},
+ {
+ /*Language=*/FormatStyle::LK_Cpp,
+ /*Delimiters=*/{"cpp"},
+ /*EnclosingFunctions=*/{},
+ /*CanonicalDelimiter=*/"",
+ BasedOnStyle,
+ },
};
return Style;
}
@@ -131,7 +142,13 @@
EXPECT_EQ(0, parseConfiguration("---\n"
"Language: Cpp\n"
"BasedOnStyle: Google", &Style).value());
- Style.RawStringFormats = {{FormatStyle::LK_Cpp, {"cpp"}, {}, "llvm"}};
+ Style.RawStringFormats = {{
+ FormatStyle::LK_Cpp,
+ {"cpp"},
+ {},
+ /*CanonicalDelimiter=*/"",
+ /*BasedOnStyle=*/"llvm",
+ }};
expect_eq(R"test(int* i = R"cpp(int* j = 0;)cpp";)test",
format(R"test(int * i = R"cpp(int * j = 0;)cpp";)test", Style));
}
@@ -752,6 +769,18 @@
Style));
}
+TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) {
+ FormatStyle Style = getRawStringPbStyleWithColumns(25);
+ Style.RawStringFormats[0].CanonicalDelimiter = "proto";
+ expect_eq(R"test(a = R"proto(key: value)proto";)test",
+ format(R"test(a = R"pb(key:value)pb";)test", Style));
+
+ // Don't update to canonical delimiter if it occurs as a raw string suffix in
+ // the raw string content.
+ expect_eq(R"test(a = R"pb(key: ")proto")pb";)test",
+ format(R"test(a = R"pb(key:")proto")pb";)test", Style));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang