[clang] [clang-format] Handle templated elaborated type specifier in function… (PR #77013)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 4 14:09:17 PST 2024


https://github.com/XDeme created https://github.com/llvm/llvm-project/pull/77013

… return type.

The behavior now is consistent with the non template version.
Enabled and updated old test.

>From 45d01cbc7ec958518b1739daa9e9b0dc35c2d194 Mon Sep 17 00:00:00 2001
From: XDeme <fernando.tagawa.gamail.com at gmail.com>
Date: Thu, 4 Jan 2024 19:04:21 -0300
Subject: [PATCH] [clang-format] Handle templated elaborated type specifier in
 function return type. The behaviour now is consistent with the non templated
 version

---
 clang/lib/Format/UnwrappedLineParser.cpp | 8 ++++++++
 clang/unittests/Format/FormatTest.cpp    | 4 +---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 684609747a5513..aaff6319dd45ef 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3914,7 +3914,15 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   // (this would still leave us with an ambiguity between template function
   // and class declarations).
   if (FormatTok->isOneOf(tok::colon, tok::less)) {
+    int AngleNestingLevel = 0;
     do {
+      if (FormatTok->is(tok::less))
+        ++AngleNestingLevel;
+      else if (FormatTok->is(tok::greater))
+        --AngleNestingLevel;
+
+      if (AngleNestingLevel == 0 && FormatTok->is(tok::r_paren))
+        break;
       if (FormatTok->is(tok::l_brace)) {
         calculateBraceTypes(/*ExpectClassBody=*/true);
         if (!tryToParseBracedList())
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 762fc8254bdfc9..f304407d0ce2f4 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14583,9 +14583,7 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
   verifyFormat("int i = SomeFunction(a<b, a> b);");
 
-  // FIXME:
-  // This now gets parsed incorrectly as class definition.
-  // verifyFormat("class A<int> f() {\n}\nint n;");
+  verifyFormat("class A<int> f() {}\nint n;");
 
   // Elaborate types where incorrectly parsing the structural element would
   // break the indent.



More information about the cfe-commits mailing list