[PATCH] D101628: [Format] Don't sort includes if DisableFormat is true

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 4 09:34:15 PDT 2021


njames93 updated this revision to Diff 342778.
njames93 added a comment.

Add test case demonstrating new behvaiour.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101628/new/

https://reviews.llvm.org/D101628

Files:
  clang/lib/Format/Format.cpp
  clang/unittests/Format/SortIncludesTest.cpp


Index: clang/unittests/Format/SortIncludesTest.cpp
===================================================================
--- clang/unittests/Format/SortIncludesTest.cpp
+++ clang/unittests/Format/SortIncludesTest.cpp
@@ -9,6 +9,7 @@
 #include "FormatTestUtils.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/None.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
@@ -1034,6 +1035,16 @@
   EXPECT_EQ(Expected, sort(Code, "a.cpp", 1));
 }
 
+TEST_F(SortIncludesTest, DisableFormatDisablesIncludeSorting) {
+  StringRef Sorted = "#include <a.h>\n"
+                     "#include <b.h>\n";
+  StringRef Unsorted = "#include <b.h>\n"
+                       "#include <a.h>\n";
+  EXPECT_EQ(Sorted, sort(Unsorted));
+  FmtStyle.DisableFormat = true;
+  EXPECT_EQ(Unsorted, sort(Unsorted, "input.cpp", 0));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2603,7 +2603,7 @@
                                    ArrayRef<tooling::Range> Ranges,
                                    StringRef FileName, unsigned *Cursor) {
   tooling::Replacements Replaces;
-  if (!Style.SortIncludes)
+  if (!Style.SortIncludes || Style.DisableFormat)
     return Replaces;
   if (isLikelyXml(Code))
     return Replaces;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101628.342778.patch
Type: text/x-patch
Size: 1452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210504/88d985d0/attachment.bin>


More information about the cfe-commits mailing list