[clang] [clang-format] Don't allow comma in front of structural enum (PR #91056)
via cfe-commits
cfe-commits at lists.llvm.org
Sun May 5 13:16:45 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Emilia Kond (rymiel)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/91056.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+3-2)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+4)
``````````diff
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/91056
More information about the cfe-commits
mailing list