MyDeveloperDay updated this revision to Diff 227083.
MyDeveloperDay marked 2 inline comments as done.
MyDeveloperDay added a comment.
Extend this revision to cover additional
https://bugs.llvm.org/show_bug.cgi?id=43783 issue (which has overlap)
New revision correctly formats the following code:
constexpr auto
operator*() const -> reference {
bfexpects_when_compliant(m_i < m_t->size());
return m_t->get()[m_i];
}
constexpr auto
operator->() const -> pointer {
bfexpects_when_compliant(m_i < m_t->size());
return &m_t->get()[m_i];
}
constexpr auto
operator[](index_type n) const -> reference {
bfexpects_when_compliant(m_i < m_t->size());
return m_t->get()[m_i];
}
constexpr auto
operator++() -> lra_iterator & {
++m_i;
return *this;
}
constexpr auto
operator()() const -> reference {}
constexpr auto
operator++() const -> reference {}
constexpr auto
operator--() const -> reference {}
constexpr auto
operator*() const -> reference {}
constexpr auto
operator->() const -> reference {}
constexpr auto
operator>>() const -> reference {}
constexpr auto
operator<<() const -> reference {}
constexpr auto
operator[]() const -> reference {}
constexpr auto
operator void*() const -> reference {}
constexpr auto
operator char*() const -> reference {}
constexpr auto
operator Foo*() const -> reference {}
constexpr auto
operator!() const -> reference {}
class Foo {
public:
bool operator!() const;
bool operator<(Foo const &) const;
bool operator*() const;
bool operator->() const;
bool operator+() const;
bool operator-() const;
bool f() const;
};
bool
Foo::operator!() const {
return true;
}
bool
Foo::operator<(Foo const &) const {
return true;
}
bool
Foo::operator*() const {
return true;
}
bool
Foo::operator->() const {
return true;
}
bool
Foo::operator+() const {
return true;
}
bool
Foo::operator-() const {
return true;
}
bool
Foo::f() const {
return true;
}
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D69573/new/
https://reviews.llvm.org/D69573
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -6111,7 +6111,40 @@
"void\n"
"A::operator>>() {}\n"
"void\n"
- "A::operator+() {}\n",
+ "A::operator+() {}\n"
+ "void\n"
+ "A::operator*() {}\n"
+ "void\n"
+ "A::operator->() {}\n"
+ "void\n"
+ "A::operator void*() {}\n"
+ "void\n"
+ "A::operator char*() {}\n"
+ "void\n"
+ "A::operator[]() {}\n"
+ "void\n"
+ "A::operator!() {}\n",
+ Style);
+ verifyFormat("constexpr auto\n"
+ "operator()() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator>>() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator+() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator*() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator->() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator++() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator void*() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator char*() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator!() const -> reference {}\n"
+ "constexpr auto\n"
+ "operator[]() const -> reference {}\n",
Style);
verifyFormat("void *operator new(std::size_t s);", // No break here.
Style);
@@ -6953,7 +6986,7 @@
verifyFormat("bool operator[]();");
verifyFormat("operator bool();");
verifyFormat("operator int();");
- verifyFormat("operator void *();");
+ verifyFormat("operator void*();");
verifyFormat("operator SomeType<int>();");
verifyFormat("operator SomeType<int, int>();");
verifyFormat("operator SomeType<SomeType<int>>();");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -952,7 +952,7 @@
consumeToken();
if (CurrentToken &&
CurrentToken->Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator,
- tok::comma))
+ tok::comma, tok::star, tok::arrow))
CurrentToken->Previous->Type = TT_OverloadedOperator;
}
if (CurrentToken) {
@@ -1344,8 +1344,12 @@
Contexts.back().IsExpression = false;
} else if (Current.is(tok::kw_new)) {
Contexts.back().CanBeExpression = false;
- } else if (Current.isOneOf(tok::semi, tok::exclaim)) {
+ } else if (Current.is(tok::semi) ||
+ (Current.is(tok::exclaim) && Current.Previous &&
+ !Current.Previous->is(tok::kw_operator))) {
// This should be the condition or increment in a for-loop.
+ // But not operator !() (can't use TT_OverloadedOperator here as its not
+ // been annotated yet).
Contexts.back().IsExpression = true;
}
}
@@ -2087,11 +2091,22 @@
continue;
if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
// For 'new[]' and 'delete[]'.
- if (Next->Next && Next->Next->is(tok::l_square) && Next->Next->Next &&
- Next->Next->Next->is(tok::r_square))
+ if (Next->Next &&
+ Next->Next->startsSequence(tok::l_square, tok::r_square))
Next = Next->Next->Next;
continue;
}
+ if (Next->startsSequence(tok::l_square, tok::r_square)) {
+ // For operator[]().
+ Next = Next->Next;
+ continue;
+ }
+ if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
+ Next->Next && Next->Next->is(tok::star)) {
+ // For operator void*(), operator char*(), operator Foo*().
+ Next = Next->Next;
+ continue;
+ }
break;
}
@@ -2605,6 +2620,12 @@
tok::l_square));
if (Right.is(tok::star) && Left.is(tok::l_paren))
return false;
+ if (Right.is(tok::star) &&
+ (Left.is(tok::identifier) || Left.isSimpleTypeSpecifier()) &&
+ Left.Previous && Left.Previous->is(tok::kw_operator))
+ // No space between the type and the *
+ // operator void*(), operator char*(), operator Foo*().
+ return false;
const auto SpaceRequiredForArrayInitializerLSquare =
[](const FormatToken &LSquareTok, const FormatStyle &Style) {
return Style.SpacesInContainerLiterals ||
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits