[clang] [clang-format] Don't allow comma in front of structural enum (PR #91056)

Emilia Kond via cfe-commits cfe-commits at lists.llvm.org
Sat May 4 03:08:06 PDT 2024


https://github.com/rymiel created https://github.com/llvm/llvm-project/pull/91056

Assume that a comma in front of `enum` means it is actually a part of an elaborated type in a template parameter list.

Fixes https://github.com/llvm/llvm-project/issues/47782

>From f625311fe7259a07607cc2bb8c13e05715775c68 Mon Sep 17 00:00:00 2001
From: Emilia Kond <emilia at rymiel.space>
Date: Sat, 4 May 2024 12:57:49 +0300
Subject: [PATCH] [clang-format] Don't allow comma in front of structural enum

Assume that a comma in front of `enum` means it is actually a part of
an elaborated type in a template parameter list.

Fixes https://github.com/llvm/llvm-project/issues/47782
---
 clang/lib/Format/UnwrappedLineParser.cpp      | 5 +++--
 clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index e8a8dd58d07eea..b5415fa9ecab55 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1763,8 +1763,9 @@ void UnwrappedLineParser::parseStructuralElement(
       break;
     }
     case tok::kw_enum:
-      // Ignore if this is part of "template <enum ..." or "... -> enum".
-      if (Previous && Previous->isOneOf(tok::less, tok::arrow)) {
+      // Ignore if this is part of "template <enum ..." or "... -> enum" or
+      // "template <..., enum ...>".
+      if (Previous && Previous->isOneOf(tok::less, tok::arrow, tok::comma)) {
         nextToken();
         break;
       }
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 01daf8dee505bc..b424424b85777b 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -489,6 +489,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
   EXPECT_TOKEN(Tokens[24], tok::amp, TT_UnaryOperator);
   EXPECT_TOKEN(Tokens[27], tok::l_square, TT_ArraySubscriptLSquare);
   EXPECT_TOKEN(Tokens[32], tok::r_brace, TT_StructRBrace);
+
+  Tokens = annotate("template <typename T, enum E e> struct S {};");
+  ASSERT_EQ(Tokens.size(), 15u) << Tokens;
+  EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_StructLBrace);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUnions) {



More information about the cfe-commits mailing list