[clang] [clang-format] Skip block commented out includes when sorting them (PR #97787)

2024-07-05 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/97787
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Skip block commented out includes when sorting them (PR #97787)

2024-07-05 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks approved this pull request.


https://github.com/llvm/llvm-project/pull/97787
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Skip block commented out includes when sorting them (PR #97787)

2024-07-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)


Changes

Fixes #97539.

---
Full diff: https://github.com/llvm/llvm-project/pull/97787.diff


2 Files Affected:

- (modified) clang/lib/Format/Format.cpp (+11-4) 
- (modified) clang/unittests/Format/SortIncludesTest.cpp (+9) 


``diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 6a8883b77a730..7fd42e46e0ccb 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3222,10 +3222,16 @@ tooling::Replacements sortCppIncludes(const FormatStyle 
, StringRef Code,
 if (Trimmed.contains(RawStringTermination))
   FormattingOff = false;
 
-if (isClangFormatOff(Trimmed))
+bool IsBlockComment = false;
+
+if (isClangFormatOff(Trimmed)) {
   FormattingOff = true;
-else if (isClangFormatOn(Trimmed))
+} else if (isClangFormatOn(Trimmed)) {
   FormattingOff = false;
+} else if (Trimmed.starts_with("/*")) {
+  IsBlockComment = true;
+  Pos = Code.find("*/", SearchFrom + 2);
+}
 
 const bool EmptyLineSkipped =
 Trimmed.empty() &&
@@ -3235,9 +3241,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle 
, StringRef Code,
 
 bool MergeWithNextLine = Trimmed.ends_with("\\");
 if (!FormattingOff && !MergeWithNextLine) {
-  if (tooling::HeaderIncludes::IncludeRegex.match(Line, )) {
+  if (!IsBlockComment &&
+  tooling::HeaderIncludes::IncludeRegex.match(Trimmed, )) {
 StringRef IncludeName = Matches[2];
-if (Line.contains("/*") && !Line.contains("*/")) {
+if (Trimmed.contains("/*") && !Trimmed.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
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 2eeb16b4ab9f5..3175382564637 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -1455,6 +1455,15 @@ TEST_F(SortIncludesTest, DisableRawStringLiteralSorting) 
{
 #undef X
 }
 
+TEST_F(SortIncludesTest, BlockCommentedOutIncludes) {
+  StringRef Code{"/* #include \"foo.h\"\n"
+ "#include \"bar.h\" */\n"
+ "#include "};
+
+  FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
+  verifyFormat(Code, sort(Code, "input.cpp", 0));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang

``




https://github.com/llvm/llvm-project/pull/97787
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Skip block commented out includes when sorting them (PR #97787)

2024-07-04 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/97787

Fixes #97539.

>From 73d92b38268e5b44d6ed7e6ac9219267003cc7fb Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 4 Jul 2024 21:39:38 -0700
Subject: [PATCH] [clang-format] Skip block commented out includes when sorting
 them

Fixes #97539.
---
 clang/lib/Format/Format.cpp | 15 +++
 clang/unittests/Format/SortIncludesTest.cpp |  9 +
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 6a8883b77a730..7fd42e46e0ccb 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3222,10 +3222,16 @@ tooling::Replacements sortCppIncludes(const FormatStyle 
, StringRef Code,
 if (Trimmed.contains(RawStringTermination))
   FormattingOff = false;
 
-if (isClangFormatOff(Trimmed))
+bool IsBlockComment = false;
+
+if (isClangFormatOff(Trimmed)) {
   FormattingOff = true;
-else if (isClangFormatOn(Trimmed))
+} else if (isClangFormatOn(Trimmed)) {
   FormattingOff = false;
+} else if (Trimmed.starts_with("/*")) {
+  IsBlockComment = true;
+  Pos = Code.find("*/", SearchFrom + 2);
+}
 
 const bool EmptyLineSkipped =
 Trimmed.empty() &&
@@ -3235,9 +3241,10 @@ tooling::Replacements sortCppIncludes(const FormatStyle 
, StringRef Code,
 
 bool MergeWithNextLine = Trimmed.ends_with("\\");
 if (!FormattingOff && !MergeWithNextLine) {
-  if (tooling::HeaderIncludes::IncludeRegex.match(Line, )) {
+  if (!IsBlockComment &&
+  tooling::HeaderIncludes::IncludeRegex.match(Trimmed, )) {
 StringRef IncludeName = Matches[2];
-if (Line.contains("/*") && !Line.contains("*/")) {
+if (Trimmed.contains("/*") && !Trimmed.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
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 2eeb16b4ab9f5..3175382564637 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -1455,6 +1455,15 @@ TEST_F(SortIncludesTest, DisableRawStringLiteralSorting) 
{
 #undef X
 }
 
+TEST_F(SortIncludesTest, BlockCommentedOutIncludes) {
+  StringRef Code{"/* #include \"foo.h\"\n"
+ "#include \"bar.h\" */\n"
+ "#include "};
+
+  FmtStyle = getGoogleStyle(FormatStyle::LK_Cpp);
+  verifyFormat(Code, sort(Code, "input.cpp", 0));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits