[clang] 56313f6 - [clang-format] Put peekNextToken(/*SkipComment=*/true) to good use

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 24 18:40:20 PST 2023


Author: Owen Pan
Date: 2023-01-24T18:40:14-08:00
New Revision: 56313f65cce71366fdc659d1d08e0eeaa5d40b63

URL: https://github.com/llvm/llvm-project/commit/56313f65cce71366fdc659d1d08e0eeaa5d40b63
DIFF: https://github.com/llvm/llvm-project/commit/56313f65cce71366fdc659d1d08e0eeaa5d40b63.diff

LOG: [clang-format] Put peekNextToken(/*SkipComment=*/true) to good use

To prevent potential bugs in situations where we want to peek the next
non-comment token.

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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 6d5b92c61c78b..3e58b6f90559b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1906,7 +1906,9 @@ void UnwrappedLineParser::parseStructuralElement(
       // declaration.
       if (!IsTopLevel || !Style.isCpp() || !Previous || eof())
         break;
-      if (isC78ParameterDecl(FormatTok, Tokens->peekNextToken(), Previous)) {
+      if (isC78ParameterDecl(FormatTok,
+                             Tokens->peekNextToken(/*SkipComment=*/true),
+                             Previous)) {
         addUnwrappedLine();
         return;
       }
@@ -2376,7 +2378,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
   if (FormatTok->is(tok::l_square))
     return false;
   if (FormatTok->is(tok::r_square)) {
-    const FormatToken *Next = Tokens->peekNextToken();
+    const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
     if (Next->is(tok::greater))
       return false;
   }

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b7d0a398dd432..6530e2005e4c4 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3444,6 +3444,13 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
                "\n"
                "private:\n"
                "  int c;\n"
+               "};\n"
+               "class B {\n"
+               "public:\n"
+               "  std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
+               "\n"
+               "private:\n"
+               "  int c;\n"
                "};");
 }
 
@@ -9637,6 +9644,19 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
                "  return a;\n"
                "}",
                Style);
+  verifyFormat("byte *\n"
+               "f(a)\n"
+               "byte /* K&R C */ a[];\n"
+               "{\n"
+               "  return a;\n"
+               "}\n"
+               "byte *\n"
+               "g(p)\n"
+               "byte /* K&R C */ *p;\n"
+               "{\n"
+               "  return p;\n"
+               "}",
+               Style);
   verifyFormat("bool f(int a, int) override;\n"
                "Bar g(int a, Bar) final;\n"
                "Bar h(a, Bar) final;",


        


More information about the cfe-commits mailing list