[PATCH] D105964: [clang-format] Make AlwaysBreakAfterReturnType work with K&R C function definitions

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 14 00:05:19 PDT 2021


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

See PR50999 <https://bugs.llvm.org/show_bug.cgi?id=50999>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105964

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -8209,6 +8209,16 @@
                "}\n",
                Style);
 
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterFunction = true;
+  verifyFormat("int f(i);\n" // No break here.
+               "int\n"       // Break here.
+               "f(i)\n"
+               "{\n"
+               "  return i + 1;\n"
+               "}\n",
+               Style);
+
   Style = getGNUStyle();
 
   // Test for comments at the end of function declarations.
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2476,6 +2476,14 @@
   if (Next->MatchingParen->Next &&
       Next->MatchingParen->Next->is(TT_PointerOrReference))
     return true;
+  // Check for K&R C function definitions, e.g.:
+  //   int f(i)
+  //   {
+  //     return i + 1;
+  //   }
+  if (Next->Next && Next->Next->is(tok::identifier) &&
+      !(Next->MatchingParen->Next && Next->MatchingParen->Next->is(tok::semi)))
+    return true;
   for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
        Tok = Tok->Next) {
     if (Tok->is(TT_TypeDeclarationParen))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105964.358530.patch
Type: text/x-patch
Size: 1426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210714/dbd41f60/attachment.bin>


More information about the cfe-commits mailing list