eoanermine created this revision.
eoanermine added reviewers: owenpan, HazardyKnusperkeks, curdeius, 
MyDeveloperDay.
eoanermine added projects: clang, clang-format.
Herald added a project: All.
eoanermine requested review of this revision.
Herald added a subscriber: cfe-commits.

- Fix aligning of trailing comments after comment at the end of a namespace
- Add tests for aligning of trailing comments after comment that documents the 
closing of a namespace


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129892

Files:
  clang/lib/Format/WhitespaceManager.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17458,6 +17458,40 @@
                Alignment);
 }
 
+TEST_F(FormatTest, AlignTrailingComments) {
+  FormatStyle Style = getLLVMStyle();
+  EXPECT_EQ(Style.AlignTrailingComments, true);
+
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non",
+               Style);
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non\n",
+               Style);
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non other text",
+               Style);
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non other text\n",
+               Style);
+}
+
 TEST_F(FormatTest, AlignConsecutiveBitFields) {
   FormatStyle Alignment = getLLVMStyle();
   Alignment.AlignConsecutiveBitFields.Enabled = true;
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -966,6 +966,10 @@
 
         unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
             Changes[j].OriginalWhitespaceRange.getEnd());
+        // Handle files without newline before the end of file correct
+        if (Changes[j].Tok->is(tok::eof))
+          NextColumn = 1;
+
         // The start of the next token was previously aligned with the
         // start of this comment.
         WasAlignedWithStartOfNextLine =


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17458,6 +17458,40 @@
                Alignment);
 }
 
+TEST_F(FormatTest, AlignTrailingComments) {
+  FormatStyle Style = getLLVMStyle();
+  EXPECT_EQ(Style.AlignTrailingComments, true);
+
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non",
+               Style);
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non\n",
+               Style);
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non other text",
+               Style);
+  verifyFormat("namespace test {\n"
+               "class C;\n"
+               "} // namespace test\n"
+               "// Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+               "Nullam cursus nunc\n"
+               "// non other text\n",
+               Style);
+}
+
 TEST_F(FormatTest, AlignConsecutiveBitFields) {
   FormatStyle Alignment = getLLVMStyle();
   Alignment.AlignConsecutiveBitFields.Enabled = true;
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -966,6 +966,10 @@
 
         unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
             Changes[j].OriginalWhitespaceRange.getEnd());
+        // Handle files without newline before the end of file correct
+        if (Changes[j].Tok->is(tok::eof))
+          NextColumn = 1;
+
         // The start of the next token was previously aligned with the
         // start of this comment.
         WasAlignedWithStartOfNextLine =
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to