-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] Handle templated elaborated type specifier in function… #77013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… return type. The behaviour now is consistent with the non templated version
@llvm/pr-subscribers-clang-format Author: None (XDeme) Changes… return type. The behavior now is consistent with the non template version. Full diff: https://github.com/llvm/llvm-project/pull/77013.diff 2 Files Affected:
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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, one last thing. From what I figured it was wrongly detected as class definition. So you could add a token annotator test, which verifies that the {
is not an ClassLBrace
?
Edit: I have updated it to check for a Identifier that is not a macro before a I have updated the comment a little, because there is some case we can't handle: template<typename T>
struct Goo<T> F() {} we can't distinguish if this is a class or function. We could check if F() is macro by checking If this is OK, I could do these checks, and possibly remove the entire comment. |
Co-authored-by: Owen Pan <[email protected]>
… return type.
The behavior now is consistent with the non template version.
Enabled and updated old test.