[clang] 58494c8 - [clang-format] Make BreakAfterReturnType work with K&R C functions

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 14 14:42:04 PDT 2021


Author: owenca
Date: 2021-07-14T14:38:02-07:00
New Revision: 58494c856a15f5b0e886c7baf5d505ac6c05dfe5

URL: https://github.com/llvm/llvm-project/commit/58494c856a15f5b0e886c7baf5d505ac6c05dfe5
DIFF: https://github.com/llvm/llvm-project/commit/58494c856a15f5b0e886c7baf5d505ac6c05dfe5.diff

LOG: [clang-format] Make BreakAfterReturnType work with K&R C functions

This fixes PR50999.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 2b83ff4f78503..54e6c7d38e7de 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2476,6 +2476,14 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
   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))

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6747fe749a2fa..eed3ea4cdbe37 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8209,6 +8209,16 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
                "}\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.


        


More information about the cfe-commits mailing list