[clang-format] adds enclosing function detection to raw string formatting

Summary: This patch adds enclosing function detection to raw string formatting.

Reviewers: bkramer

Reviewed By: bkramer

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D42167

llvm-svn: 322678
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b453a62..61973c62 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10408,8 +10408,18 @@
 
   Style.RawStringFormats.clear();
   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
-      {FormatStyle::LK_TextProto, {"pb", "proto"}, "llvm"},
-      {FormatStyle::LK_Cpp, {"cc", "cpp"}, "google"},
+      {
+          FormatStyle::LK_TextProto,
+          {"pb", "proto"},
+          {"PARSE_TEXT_PROTO"},
+          "llvm",
+      },
+      {
+          FormatStyle::LK_Cpp,
+          {"cc", "cpp"},
+          {"C_CODEBLOCK", "CPPEVAL"},
+          "",
+      },
   };
 
   CHECK_PARSE("RawStringFormats:\n"
@@ -10417,12 +10427,16 @@
               "    Delimiters:\n"
               "      - 'pb'\n"
               "      - 'proto'\n"
+              "    EnclosingFunctions:\n"
+              "      - 'PARSE_TEXT_PROTO'\n"
               "    BasedOnStyle: llvm\n"
               "  - Language: Cpp\n"
               "    Delimiters:\n"
               "      - 'cc'\n"
               "      - 'cpp'\n"
-              "    BasedOnStyle: google\n",
+              "    EnclosingFunctions:\n"
+              "      - 'C_CODEBLOCK'\n"
+              "      - 'CPPEVAL'\n",
               RawStringFormats, ExpectedRawStringFormats);
 }
 
diff --git a/clang/unittests/Format/FormatTestRawStrings.cpp b/clang/unittests/Format/FormatTestRawStrings.cpp
index 241cbe7..d5ba792 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/unittests/Format/FormatTestRawStrings.cpp
@@ -65,23 +65,32 @@
   FormatStyle getRawStringPbStyleWithColumns(unsigned ColumnLimit) {
     FormatStyle Style = getLLVMStyle();
     Style.ColumnLimit = ColumnLimit;
-    Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_TextProto,
-                               /*Delimiters=*/{"pb"},
-                               /*BasedOnStyle=*/"google"}};
+    Style.RawStringFormats = {
+        {/*Language=*/FormatStyle::LK_TextProto,
+         /*Delimiters=*/{"pb"},
+         /*EnclosingFunctions=*/{},
+         /*BasedOnStyle=*/"google"},
+    };
     return Style;
   }
 
   FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
     FormatStyle Style = getLLVMStyle();
-    Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_Cpp,
-                               /*Delimiters=*/{"cpp"}, BasedOnStyle}};
+    Style.RawStringFormats = {
+        {/*Language=*/FormatStyle::LK_Cpp,
+         /*Delimiters=*/{"cpp"},
+         /*EnclosingFunctions=*/{}, BasedOnStyle},
+    };
     return Style;
   }
 
   FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
     FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
-    Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_Cpp,
-                               /*Delimiters=*/{"cpp"}, BasedOnStyle}};
+    Style.RawStringFormats = {
+        {/*Language=*/FormatStyle::LK_Cpp,
+         /*Delimiters=*/{"cpp"},
+         /*EnclosingFunctions=*/{}, BasedOnStyle},
+    };
     return Style;
   }
 
@@ -122,7 +131,7 @@
   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"}, {}, "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));
 }
@@ -720,6 +729,29 @@
                    getRawStringPbStyleWithColumns(20)));
 }
 
+TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) {
+  FormatStyle Style = getRawStringPbStyleWithColumns(40);
+  Style.RawStringFormats[0].EnclosingFunctions.push_back(
+      "PARSE_TEXT_PROTO");
+  Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto");
+  expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test",
+            format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style));
+
+  expect_eq(R"test(
+a = PARSE_TEXT_PROTO /**/ (
+    /**/ R"(key: value)");)test",
+            format(R"test(
+a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
+                   Style));
+
+  expect_eq(R"test(
+a = ParseTextProto<ProtoType>(
+    R"(key: value)");)test",
+            format(R"test(
+a = ParseTextProto<ProtoType>(R"(key:value)");)test",
+                   Style));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang