[Format] Don't sort includes if DisableFormat is true
authorNathan James <n.james93@hotmail.co.uk>
Tue, 4 May 2021 18:04:11 +0000 (19:04 +0100)
committerNathan James <n.james93@hotmail.co.uk>
Tue, 4 May 2021 18:04:12 +0000 (19:04 +0100)
Fixes https://llvm.org/PR35099.

I'm not sure if this decision was intentional but its definitely confusing for users.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, curdeius

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

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

index ba7b03d..f1508b9 100644 (file)
@@ -2605,7 +2605,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
                                    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;
index 47ec319..4efeb96 100644 (file)
@@ -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 @@ TEST_F(SortIncludesTest, MergeLines) {
   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