[clang-format] Reorganize RawStringFormat based on language
Summary:
This patch changes the structure for raw string formatting options by making it
language based (enumerate delimiters per language) as opposed to delimiter-based
(specify the language for a delimiter). The raw string formatting now uses an
appropriate style from the .clang-format file, if exists.
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D42098
llvm-svn: 322634
diff --git a/clang/unittests/Format/FormatTestRawStrings.cpp b/clang/unittests/Format/FormatTestRawStrings.cpp
index 6e7b706..241cbe7 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/unittests/Format/FormatTestRawStrings.cpp
@@ -65,23 +65,23 @@
FormatStyle getRawStringPbStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = ColumnLimit;
- Style.RawStringFormats = {{/*Delimiter=*/"pb",
- /*Kind=*/FormatStyle::LK_TextProto,
+ Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_TextProto,
+ /*Delimiters=*/{"pb"},
/*BasedOnStyle=*/"google"}};
return Style;
}
FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
FormatStyle Style = getLLVMStyle();
- Style.RawStringFormats = {{/*Delimiter=*/"cpp",
- /*Kind=*/FormatStyle::LK_Cpp, BasedOnStyle}};
+ Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_Cpp,
+ /*Delimiters=*/{"cpp"}, BasedOnStyle}};
return Style;
}
FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
- Style.RawStringFormats = {{/*Delimiter=*/"cpp",
- /*Kind=*/FormatStyle::LK_Cpp, BasedOnStyle}};
+ Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_Cpp,
+ /*Delimiters=*/{"cpp"}, BasedOnStyle}};
return Style;
}
@@ -112,6 +112,21 @@
getRawStringGoogleCppStyleBasedOn("llvm")));
}
+TEST_F(FormatTestRawStrings, UsesConfigurationOverBaseStyle) {
+ // llvm style puts '*' on the right.
+ // google style puts '*' on the left.
+
+ // Uses the configured google style inside raw strings even if BasedOnStyle in
+ // the raw string format is llvm.
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
+ EXPECT_EQ(0, parseConfiguration("---\n"
+ "Language: Cpp\n"
+ "BasedOnStyle: Google", &Style).value());
+ Style.RawStringFormats = {{FormatStyle::LK_Cpp, {"cpp"}, "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));
+}
+
TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) {
// Don't touch the 'PB' raw string, format the 'pb' raw string.
expect_eq(R"test(
@@ -121,29 +136,6 @@
s = R"PB(item:1)PB";
t = R"pb(item:1)pb";)test",
getRawStringPbStyleWithColumns(40)));
-
- FormatStyle MixedStyle = getLLVMStyle();
- MixedStyle.RawStringFormats = {
- {/*Delimiter=*/"cpp", /*Kind=*/FormatStyle::LK_Cpp,
- /*BasedOnStyle=*/"llvm"},
- {/*Delimiter=*/"CPP", /*Kind=*/FormatStyle::LK_Cpp,
- /*BasedOnStyle=*/"google"}};
-
- // Format the 'cpp' raw string with '*' on the right.
- // Format the 'CPP' raw string with '*' on the left.
- // Do not format the 'Cpp' raw string.
- // Do not format non-raw strings.
- expect_eq(R"test(
-a = R"cpp(int *i = 0;)cpp";
-b = R"CPP(int* j = 0;)CPP";
-c = R"Cpp(int * k = 0;)Cpp";
-d = R"cpp(int * k = 0;)Cpp";)test",
- format(R"test(
-a = R"cpp(int * i = 0;)cpp";
-b = R"CPP(int * j = 0;)CPP";
-c = R"Cpp(int * k = 0;)Cpp";
-d = R"cpp(int * k = 0;)Cpp";)test",
- MixedStyle));
}
TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) {