[PATCH] D23274: Make clang-format remove duplicate headers when sorting #includes.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 10 02:09:35 PDT 2016


djasper added inline comments.

================
Comment at: lib/Format/Format.cpp:1224
@@ -1222,1 +1223,3 @@
 
+// Finds the index of the #include on which the cursor will be put after
+// sorting/deduplicating.
----------------
First off, make this return a pair<unsigned, unsigned> with the two values.

And then the comment needs to be more precise:

  // Returns a pair (Index, OffsetToEOL) describing the position of the cursor
  // before sorting/deduplicating. Index is the index of the include under the cursor
  // in the original set of includes. If this include has duplicates, it is the index of
  // the first of the duplicates as the others are going to be removed. OffsetToEOL
  // Describes the cursor's position relative to the end of its current line.

================
Comment at: lib/Format/Format.cpp:1260
@@ -1226,3 +1259,3 @@
 static void sortCppIncludes(const FormatStyle &Style,
                          const SmallVectorImpl<IncludeDirective> &Includes,
                          ArrayRef<tooling::Range> Ranges, StringRef FileName,
----------------
Fix indent while here.

================
Comment at: lib/Format/Format.cpp:1306
@@ -1263,7 +1305,3 @@
 
-  // Sorting #includes shouldn't change their total number of characters.
-  // This would otherwise mess up 'Ranges'.
-  assert(result.size() ==
-         Includes.back().Offset + Includes.back().Text.size() -
-             Includes.front().Offset);
-
+  unsigned num_chars_replaced = Includes.back().Offset +
+                                Includes.back().Text.size() -
----------------
This can also be calculated upfront with:

  Includes.back().Offset + Includes.back().Text.size() - Includes.front().Offset

And maybe you can pull some of those out into variables as they are also used in the very first line of this function.
  


https://reviews.llvm.org/D23274





More information about the cfe-commits mailing list