[clang] 95bf0a9 - [clang-format] Don't break block comments when sorting includes.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 31 23:51:14 PST 2022


Author: Marek Kurdej
Date: 2022-02-01T08:51:10+01:00
New Revision: 95bf0a9ebdb4ee6f78699e20751602c8c3c5733f

URL: https://github.com/llvm/llvm-project/commit/95bf0a9ebdb4ee6f78699e20751602c8c3c5733f
DIFF: https://github.com/llvm/llvm-project/commit/95bf0a9ebdb4ee6f78699e20751602c8c3c5733f.diff

LOG: [clang-format] Don't break block comments when sorting includes.

Fixes https://github.com/llvm/llvm-project/issues/34626.

Before, the include sorter would break the code:
```
#include <stdio.h>
#include <stdint.h> /* long
                       comment */
```
and change it into:
```
#include <stdint.h> /* long
#include <stdio.h>
                       comment */
```

This commit handles only the most basic case of a single block comment on an include line, but does not try to handle all the possible edge cases with multiple comments.

Reviewed By: HazardyKnusperkeks

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

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 04e2915e3af69..baf9c6885a86b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2678,6 +2678,15 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
     if (!FormattingOff && !MergeWithNextLine) {
       if (IncludeRegex.match(Line, &Matches)) {
         StringRef IncludeName = Matches[2];
+        if (Line.contains("/*") && !Line.contains("*/")) {
+          // #include with a start of a block comment, but without the end.
+          // Need to keep all the lines until the end of the comment together.
+          // FIXME: This is somehow simplified check that probably does not work
+          // correctly if there are multiple comments on a line.
+          Pos = Code.find("*/", SearchFrom);
+          Line = Code.substr(
+              Prev, (Pos != StringRef::npos ? Pos + 2 : Code.size()) - Prev);
+        }
         int Category = Categories.getIncludePriority(
             IncludeName,
             /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);

diff  --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index 1d215af715382..b0ee6bfc35979 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -70,6 +70,21 @@ TEST_F(SortIncludesTest, BasicSorting) {
                  {tooling::Range(25, 1)}));
 }
 
+TEST_F(SortIncludesTest, TrailingComments) {
+  EXPECT_EQ("#include \"a.h\"\n"
+            "#include \"b.h\" /* long\n"
+            "                  * long\n"
+            "                  * comment*/\n"
+            "#include \"c.h\"\n"
+            "#include \"d.h\"\n",
+            sort("#include \"a.h\"\n"
+                 "#include \"c.h\"\n"
+                 "#include \"b.h\" /* long\n"
+                 "                  * long\n"
+                 "                  * comment*/\n"
+                 "#include \"d.h\"\n"));
+}
+
 TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) {
   FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
   FmtStyle.IncludeStyle.IncludeCategories = {


        


More information about the cfe-commits mailing list