[PATCH] D94217: [clang-format] Find main include after block ended with #pragma hdrstop

RafaƂ Jelonek via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 8 10:00:34 PST 2021


rjelonek updated this revision to Diff 315431.
rjelonek added a comment.

rebase patch on master/main


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

https://reviews.llvm.org/D94217

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
@@ -879,6 +879,45 @@
                  "#include \"a.h\""));
 }
 
+TEST_F(SortIncludesTest, DoNotTreatPrecompiledHeadersAsFirstBlock) {
+  Style.IncludeBlocks = Style.IBS_Merge;
+  std::string Code = "#include \"d.h\"\r\n"
+                     "#include \"b.h\"\r\n"
+                     "#pragma hdrstop\r\n"
+                     "\r\n"
+                     "#include \"c.h\"\r\n"
+                     "#include \"a.h\"\r\n"
+                     "#include \"e.h\"\r\n";
+
+  std::string Expected = "#include \"b.h\"\r\n"
+                         "#include \"d.h\"\r\n"
+                         "#pragma hdrstop\r\n"
+                         "\r\n"
+                         "#include \"e.h\"\r\n"
+                         "#include \"a.h\"\r\n"
+                         "#include \"c.h\"\r\n";
+
+  EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
+
+  Code = "#include \"d.h\"\n"
+         "#include \"b.h\"\n"
+         "#pragma hdrstop( \"c:\\projects\\include\\myinc.pch\" )\n"
+         "\n"
+         "#include \"c.h\"\n"
+         "#include \"a.h\"\n"
+         "#include \"e.h\"\n";
+
+  Expected = "#include \"b.h\"\n"
+             "#include \"d.h\"\n"
+             "#pragma hdrstop(\"c:\\projects\\include\\myinc.pch\")\n"
+             "\n"
+             "#include \"e.h\"\n"
+             "#include \"a.h\"\n"
+             "#include \"c.h\"\n";
+
+  EXPECT_EQ(Expected, sort(Code, "e.cpp", 2));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2305,7 +2305,10 @@
         sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Code,
                         Replaces, Cursor);
         IncludesInBlock.clear();
-        FirstIncludeBlock = false;
+        if (Trimmed.startswith("#pragma hdrstop")) // precompiled headers
+          FirstIncludeBlock = true;
+        else
+          FirstIncludeBlock = false;
       }
       Prev = Pos + 1;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94217.315431.patch
Type: text/x-patch
Size: 2291 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210108/27fcc84b/attachment.bin>


More information about the cfe-commits mailing list