[PATCH] D108094: [clang-format] Improve detection of parameter declarations in K&R C

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 18 15:22:24 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG643f2be7b6af: [clang-format] Improve detection of parameter declarations in K&R C (authored by owenpan).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108094/new/

https://reviews.llvm.org/D108094

Files:
  clang/lib/Format/UnwrappedLineParser.cpp


Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1008,14 +1008,26 @@
 //   {
 //      return a + b;
 //   }
-static bool isC78ParameterDecl(const FormatToken *Tok) {
-  if (!Tok)
+static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next,
+                               const FormatToken *FuncName) {
+  assert(Tok);
+  assert(Next);
+  assert(FuncName);
+
+  if (FuncName->isNot(tok::identifier))
+    return false;
+
+  const FormatToken *Prev = FuncName->Previous;
+  if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev)))
     return false;
 
   if (!isC78Type(*Tok) &&
       !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union))
     return false;
 
+  if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
+    return false;
+
   Tok = Tok->Previous;
   if (!Tok || Tok->isNot(tok::r_paren))
     return false;
@@ -1378,21 +1390,11 @@
       parseParens();
       // Break the unwrapped line if a K&R C function definition has a parameter
       // declaration.
-      if (!IsTopLevel || !Style.isCpp())
-        break;
-      if (!Previous || Previous->isNot(tok::identifier))
-        break;
-      const FormatToken *PrevPrev = Previous->Previous;
-      if (!PrevPrev || (!isC78Type(*PrevPrev) && PrevPrev->isNot(tok::star)))
+      if (!IsTopLevel || !Style.isCpp() || !Previous || FormatTok->is(tok::eof))
         break;
       const unsigned Position = Tokens->getPosition() + 1;
-      if (Position == AllTokens.size())
-        break;
       assert(Position < AllTokens.size());
-      const FormatToken *Next = AllTokens[Position];
-      if (Next && Next->isOneOf(tok::l_paren, tok::semi))
-        break;
-      if (isC78ParameterDecl(FormatTok)) {
+      if (isC78ParameterDecl(FormatTok, AllTokens[Position], Previous)) {
         addUnwrappedLine();
         return;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108094.367340.patch
Type: text/x-patch
Size: 2005 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210818/b8619804/attachment.bin>


More information about the cfe-commits mailing list