[llvm-branch-commits] [clang] b685e13 - [clan-format] detect function definitions more conservatively

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Sep 9 20:03:50 PDT 2021


Author: Krasimir Georgiev
Date: 2021-09-09T20:00:23-07:00
New Revision: b685e13d1130a969dd58fb917a61eef5a33c0552

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

LOG: [clan-format] detect function definitions more conservatively

https://reviews.llvm.org/D105964 updated the detection of function
definitions. It had the unfortunate effect to start marking object
definitions with attribute-like macros as function definitions.

This addresses this issue.

Reviewed By: owenpan

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

(cherry picked from commit f6bc614546e169bb1b17a29c422ebace038e6c62)

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 54e6c7d38e7de..de3dabcfc639b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2482,7 +2482,7 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
   //     return i + 1;
   //   }
   if (Next->Next && Next->Next->is(tok::identifier) &&
-      !(Next->MatchingParen->Next && Next->MatchingParen->Next->is(tok::semi)))
+      Line.Last->isNot(tok::semi))
     return true;
   for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
        Tok = Tok->Next) {

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index de00864143f7b..67064d02c01a5 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8216,7 +8216,12 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
                "f(i)\n"
                "{\n"
                "  return i + 1;\n"
-               "}",
+               "}\n"
+               "int\n" // Break here.
+               "f(i)\n"
+               "{\n"
+               "  return i + 1;\n"
+               "};",
                Style);
   verifyFormat("int f(a, b, c);\n" // No break here.
                "int\n"             // Break here.
@@ -8225,8 +8230,20 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
                "float c;\n"
                "{\n"
                "  return a + b < c;\n"
-               "}",
+               "}\n"
+               "int\n"        // Break here.
+               "f(a, b, c)\n" // Break here.
+               "short a, b;\n"
+               "float c;\n"
+               "{\n"
+               "  return a + b < c;\n"
+               "};",
                Style);
+  // The return breaking style doesn't affect object definitions with
+  // attribute-like macros.
+  verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
+               "    ABSL_GUARDED_BY(mutex) = {};",
+               getGoogleStyleWithColumns(40));
 
   Style = getGNUStyle();
 


        


More information about the llvm-branch-commits mailing list