]> granicus.if.org Git - clang/commitdiff
clang-format: Make sorting includes respect // clang-format off
authorDaniel Jasper <djasper@google.com>
Sat, 21 Nov 2015 09:17:08 +0000 (09:17 +0000)
committerDaniel Jasper <djasper@google.com>
Sat, 21 Nov 2015 09:17:08 +0000 (09:17 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253772 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/SortIncludesTest.cpp

index e7e71c4c7e343d02d9bf32692f36c44168cf3999..2a7b9acf23089e21e9b28b28aab9331e52d08ce9 100644 (file)
@@ -1780,11 +1780,20 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
   for (const auto &Category : Style.IncludeCategories)
     CategoryRegexs.emplace_back(Category.Regex);
 
+  bool FormattingOff = false;
+
   for (;;) {
     auto Pos = Code.find('\n', SearchFrom);
     StringRef Line =
         Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
-    if (!Line.endswith("\\")) {
+
+    StringRef Trimmed = Line.trim();
+    if (Trimmed == "// clang-format off")
+      FormattingOff = true;
+    else if (Trimmed == "// clang-format on")
+      FormattingOff = false;
+
+    if (!FormattingOff && !Line.endswith("\\")) {
       if (IncludeRegex.match(Line, &Matches)) {
         StringRef IncludeName = Matches[2];
         unsigned Category;
index 30109ffb728fb3ca3e0aae9ef12b35b37bcc7b4d..a9f90e3f7bfb98faec6016e5186164b48587cf71 100644 (file)
@@ -40,6 +40,25 @@ TEST_F(SortIncludesTest, BasicSorting) {
                  "#include \"b.h\"\n"));
 }
 
+TEST_F(SortIncludesTest, SupportClangFormatOff) {
+  EXPECT_EQ("#include <a>\n"
+            "#include <b>\n"
+            "#include <c>\n"
+            "// clang-format off\n"
+            "#include <b>\n"
+            "#include <a>\n"
+            "#include <c>\n"
+            "// clang-format on\n",
+            sort("#include <b>\n"
+                 "#include <a>\n"
+                 "#include <c>\n"
+                 "// clang-format off\n"
+                 "#include <b>\n"
+                 "#include <a>\n"
+                 "#include <c>\n"
+                 "// clang-format on\n"));
+}
+
 TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
   Style.SortIncludes = false;
   EXPECT_EQ("#include \"a.h\"\n"