Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Fix a bug in TCAS_Leave using tabs for indentation #98427

Merged
merged 1 commit into from
Jul 12, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jul 11, 2024

Fixes #92530.

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #92530.


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

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+1-1)
  • (modified) clang/unittests/Format/FormatTestComments.cpp (+17)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 50531aee9d597..a31874a7c3195 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1116,7 +1116,7 @@ void WhitespaceManager::alignTrailingComments() {
       // leave the comments.
       if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
         break;
-      C.Spaces = OriginalSpaces;
+      C.Spaces = C.NewlinesBefore > 0 ? C.Tok->OriginalColumn : OriginalSpaces;
       continue;
     }
 
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 3e75707a9faec..8f84d59cbb2e2 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3145,6 +3145,23 @@ TEST_F(FormatTestComments, AlignTrailingCommentsLeave) {
                    "int bar = 1234;       // This is a very long comment\n"
                    "          // which is wrapped arround.",
                    Style));
+
+  Style = getLLVMStyle();
+  Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
+  Style.TabWidth = 2;
+  Style.UseTab = FormatStyle::UT_ForIndentation;
+  verifyNoChange("{\n"
+                 "\t// f\n"
+                 "\tf();\n"
+                 "\n"
+                 "\t// g\n"
+                 "\tg();\n"
+                 "\t{\n"
+                 "\t\t// h();  // h\n"
+                 "\t\tfoo();  // foo\n"
+                 "\t}\n"
+                 "}",
+                 Style);
 }
 
 TEST_F(FormatTestComments, DontAlignNamespaceComments) {

@owenca owenca merged commit dcf6b7a into llvm:main Jul 12, 2024
9 checks passed
@owenca owenca deleted the 92530 branch July 12, 2024 02:36
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
@llvm llvm deleted a comment from llvm-ci Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format option AlignTrailingComments: Leave misaligns comment-only lines
3 participants