[PATCH] D107961: [clang-format] Distinguish K&R C function definition and attribute

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 12 06:31:02 PDT 2021


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

This is a follow-up on https://reviews.llvm.org/D107950 which missed user-defined types in K&R C.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107961

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
@@ -8247,6 +8247,13 @@
                "  return a + b < c;\n"
                "};",
                Style);
+  verifyFormat("byte *\n" // Break here.
+               "f(a)\n"   // Break here.
+               "byte a[];\n"
+               "{\n"
+               "  return a;\n"
+               "}",
+               Style);
 
   // The return breaking style doesn't affect:
   // * function and object definitions with attribute-like macros
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -14,7 +14,6 @@
 
 #include "UnwrappedLineParser.h"
 #include "FormatToken.h"
-#include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
@@ -995,6 +994,14 @@
       Keywords.kw_import, tok::kw_export);
 }
 
+// This function checks whether a token is a K&R C (aka C78) type including
+// user-defined types.
+static bool isC78Type(const FormatToken &Tok) {
+  return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
+                     tok::kw_unsigned, tok::kw_float, tok::kw_double,
+                     tok::identifier);
+}
+
 // This function checks whether a token starts the first parameter declaration
 // in a K&R C (aka C78) function definition, e.g.:
 //   int f(a, b)
@@ -1003,12 +1010,7 @@
 //      return a + b;
 //   }
 static bool isC78ParameterDecl(const FormatToken *Tok) {
-  if (!Tok)
-    return false;
-
-  if (!Tok->isOneOf(tok::kw_int, tok::kw_char, tok::kw_float, tok::kw_double,
-                    tok::kw_struct, tok::kw_union, tok::kw_long, tok::kw_short,
-                    tok::kw_unsigned, tok::kw_register))
+  if (!Tok || (Tok->isNot(tok::kw_register) && !isC78Type(*Tok)))
     return false;
 
   Tok = Tok->Previous;
@@ -1369,7 +1371,7 @@
     case tok::r_brace:
       addUnwrappedLine();
       return;
-    case tok::l_paren:
+    case tok::l_paren: {
       parseParens();
       // Break the unwrapped line if a K&R C function definition has a parameter
       // declaration.
@@ -1377,14 +1379,18 @@
         break;
       if (!Previous || Previous->isNot(tok::identifier))
         break;
-      if (Previous->Previous && Previous->Previous->is(tok::at))
+      const FormatToken *PrevPrev = Previous->Previous;
+      if (!PrevPrev || (!isC78Type(*PrevPrev) && PrevPrev->isNot(tok::star)))
+        break;
+      const FormatToken *Next = AllTokens[Tokens->getPosition() + 1];
+      if (Next && Next->is(tok::l_paren))
         break;
-      if (!Line->Tokens.begin()->Tok->is(tok::kw_typedef) &&
-          isC78ParameterDecl(FormatTok)) {
+      if (isC78ParameterDecl(FormatTok)) {
         addUnwrappedLine();
         return;
       }
       break;
+    }
     case tok::kw_operator:
       nextToken();
       if (FormatTok->isBinaryOperator())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107961.365988.patch
Type: text/x-patch
Size: 3147 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210812/5c2ec8c2/attachment-0001.bin>


More information about the cfe-commits mailing list