[PATCH] D127873: [clang-format] Fix misplacemnt of `*` in declaration of pointer to struct
Jack Huang via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 17 06:14:24 PDT 2022
jackhong12 updated this revision to Diff 437868.
jackhong12 edited the summary of this revision.
jackhong12 added a comment.
- Add annotator test
- Add formatting test
- Handle reference cases by `PrevToken->MatchingParen`
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D127873/new/
https://reviews.llvm.org/D127873
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -85,6 +85,17 @@
Tokens = annotate("case &x:");
EXPECT_EQ(Tokens.size(), 5u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);
+
+ Tokens = annotate("struct {\n"
+ " int element;\n"
+ "} *ptr;");
+ EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::star, TT_PointerOrReference);
+ Tokens = annotate("struct {\n"
+ " int element;\n"
+ "} &&ptr = {};");
+ EXPECT_EQ(Tokens.size(), 13u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_PointerOrReference);
}
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10431,6 +10431,13 @@
"void F();",
getGoogleStyleWithColumns(68));
+ verifyFormat("struct {\n"
+ " int element;\n"
+ "} *ptr;");
+ verifyFormat("struct {\n"
+ " int element;\n"
+ "} &&ptr = {};");
+
verifyIndependentOfContext("MACRO(int *i);");
verifyIndependentOfContext("MACRO(auto *a);");
verifyIndependentOfContext("MACRO(const A *a);");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2306,10 +2306,14 @@
if (PrevToken->Tok.isLiteral() ||
PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
- tok::kw_false, tok::r_brace)) {
+ tok::kw_false)) {
return TT_BinaryOperator;
}
+ if (PrevToken->is(tok::r_brace) && Tok.isOneOf(tok::amp, tok::ampamp) &&
+ PrevToken->MatchingParen && PrevToken->MatchingParen->is(TT_Unknown))
+ return TT_BinaryOperator;
+
const FormatToken *NextNonParen = NextToken;
while (NextNonParen && NextNonParen->is(tok::l_paren))
NextNonParen = NextNonParen->getNextNonComment();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127873.437868.patch
Type: text/x-patch
Size: 2336 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220617/99cd7e97/attachment.bin>
More information about the cfe-commits
mailing list