[clang] 2185f64 - [clang-format] Handle comments between access specifier and colon

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 16 20:18:34 PDT 2022


Author: owenca
Date: 2022-08-16T20:18:21-07:00
New Revision: 2185f64771f039774d54a0be654cce39931580bf

URL: https://github.com/llvm/llvm-project/commit/2185f64771f039774d54a0be654cce39931580bf
DIFF: https://github.com/llvm/llvm-project/commit/2185f64771f039774d54a0be654cce39931580bf.diff

LOG: [clang-format] Handle comments between access specifier and colon

Fixes #56740.

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

Added: 
    

Modified: 
    clang/lib/Format/FormatToken.h
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 7d963cf6af7f7..21ed9bb5e04ef 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -584,8 +584,12 @@ struct FormatToken {
   }
 
   bool isAccessSpecifier(bool ColonRequired = true) const {
-    return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
-           (!ColonRequired || (Next && Next->is(tok::colon)));
+    if (!isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
+      return false;
+    if (!ColonRequired)
+      return true;
+    const auto NextNonComment = getNextNonComment();
+    return NextNonComment && NextNonComment->is(tok::colon);
   }
 
   bool canBePointerOrReferenceQualifier() const {

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ef68c528cb5c6..dc450029ccc2d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24799,6 +24799,11 @@ TEST_F(FormatTest, IndentAccessModifiers) {
                "    int i;\n"
                "};\n",
                Style);
+  verifyFormat("class C {\n"
+               "  public /* comment */:\n"
+               "    int i;\n"
+               "};",
+               Style);
   verifyFormat("struct S {\n"
                "  private:\n"
                "    class C {\n"
@@ -24827,6 +24832,11 @@ TEST_F(FormatTest, IndentAccessModifiers) {
                "      int i;\n"
                "};\n",
                Style);
+  verifyFormat("class C {\n"
+               "   public /**/:\n"
+               "      int i;\n"
+               "};",
+               Style);
 }
 
 TEST_F(FormatTest, LimitlessStringsAndComments) {


        


More information about the cfe-commits mailing list