[clang] 61dc0f2 - [Format] Don't sort includes if DisableFormat is true

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Tue May 4 11:04:21 PDT 2021


Author: Nathan James
Date: 2021-05-04T19:04:12+01:00
New Revision: 61dc0f2b593da149a4c0cea67819cd7bdbdd50b8

URL: https://github.com/llvm/llvm-project/commit/61dc0f2b593da149a4c0cea67819cd7bdbdd50b8
DIFF: https://github.com/llvm/llvm-project/commit/61dc0f2b593da149a4c0cea67819cd7bdbdd50b8.diff

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

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ba7b03de8b3d3..f1508b98653d7 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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;

diff  --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index 47ec319294bae..4efeb96124bf4 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/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 @@ 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


        


More information about the cfe-commits mailing list