[PATCH] D142296: [clang-format] Fix a bug in parsing C++20 import statements

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 21 19:54:58 PST 2023


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

Fixes https://github.com/llvm/llvm-project/issues/60145.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142296

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -12810,6 +12810,7 @@
   // But 'import' might also be a regular C++ namespace.
   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+  verifyFormat("import::Bar foo(val ? 2 : 1);");
 }
 
 //===----------------------------------------------------------------------===//
@@ -24654,6 +24655,8 @@
   verifyFormat("import", Style);
   verifyFormat("module", Style);
   verifyFormat("export", Style);
+
+  verifyFormat("import = val ? 2 : 1;");
 }
 
 TEST_F(FormatTest, CoroutineForCoawait) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -41,7 +41,7 @@
 
   // Returns the token that would be returned by the next call to
   // getNextToken().
-  virtual FormatToken *peekNextToken() = 0;
+  virtual FormatToken *peekNextToken(bool SkipComment = false) = 0;
 
   // Returns whether we are at the end of the file.
   // This can be different from whether getNextToken() returned an eof token
@@ -169,10 +169,10 @@
     return PreviousTokenSource->getPreviousToken();
   }
 
-  FormatToken *peekNextToken() override {
+  FormatToken *peekNextToken(bool SkipComment) override {
     if (eof())
       return &FakeEOF;
-    return PreviousTokenSource->peekNextToken();
+    return PreviousTokenSource->peekNextToken(SkipComment);
   }
 
   bool isEOF() override { return PreviousTokenSource->isEOF(); }
@@ -288,8 +288,11 @@
     return Position > 0 ? Tokens[Position - 1] : nullptr;
   }
 
-  FormatToken *peekNextToken() override {
+  FormatToken *peekNextToken(bool SkipComment) override {
     int Next = Position + 1;
+    if (SkipComment)
+      while (Tokens[Next]->is(tok::comment))
+        ++Next;
     LLVM_DEBUG({
       llvm::dbgs() << "Peeking ";
       dbgToken(Next);
@@ -1727,8 +1730,13 @@
         return;
       }
       if (Style.isCpp()) {
-        parseModuleImport();
-        return;
+        if (auto Token = Tokens->peekNextToken(/*SkipComment=*/true);
+            Token->Tok.getIdentifierInfo() ||
+            Token->TokenText.startswith("\"") ||
+            Token->isOneOf(tok::less, tok::colon)) {
+          parseModuleImport();
+          return;
+        }
       }
     }
     if (Style.isCpp() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142296.491120.patch
Type: text/x-patch
Size: 2577 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230122/4d2a619c/attachment.bin>


More information about the cfe-commits mailing list