From fa738af8a2b2b47ce6be83f883c6ff814ece1f18 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Fri, 18 Oct 2019 15:21:06 +0000 Subject: [clang-format] fix regression recognizing casts in Obj-C calls Summary: r373922 added checks for a few tokens that, following an `)` make it unlikely that the `)` is the closing paren of a cast expression. The specific check for `tok::l_square` there introduced a regression for casts of Obj-C calls, like: ``` (cast)[func arg] ``` From the tests added in r373922, I believe the `tok::l_square` case is added to capture the case where a non-cast `)` is directly followed by an attribute specifier, like: ``` int f(int x) [[noreturn]]; ``` I've specialized the code to look for such attribute specifier instead of `tok::l_square` in general. Also, I added a regression test and moved the test cases added in r373922 to an already existing place documenting other instances of historically misidentified casts. Reviewers: MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69164 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@375247 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 5 ++-- unittests/Format/FormatTest.cpp | 52 ++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 9823a5af9c..7aa1f378f0 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1607,8 +1607,9 @@ private: // Functions which end with decorations like volatile, noexcept are unlikely // to be casts. if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const, - tok::kw_throw, tok::l_square, tok::arrow, - Keywords.kw_override, Keywords.kw_final)) + tok::kw_throw, tok::arrow, Keywords.kw_override, + Keywords.kw_final) || + isCpp11AttributeSpecifier(*Tok.Next)) return false; // As Java has no function types, a "(" after the ")" likely means that this diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b0d7f08892..19269b2418 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -7541,6 +7541,8 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("my_int a = (ns::my_int)-2;"); verifyFormat("case (my_int)ONE:"); verifyFormat("auto x = (X)this;"); + // Casts in Obj-C style calls used to not be recognized as such. + verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); // FIXME: single value wrapped with paren will be treated as cast. verifyFormat("void f(int i = (kValue)*kMask) {}"); @@ -7581,6 +7583,29 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); verifyFormat("bool b = f(g) && c;"); verifyFormat("typedef void (*f)(int i) func;"); + verifyFormat("void operator++(int) noexcept;"); + verifyFormat("void operator++(int &) noexcept;"); + verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " + "&) noexcept;"); + verifyFormat( + "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); + verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); + verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); + verifyFormat("void operator delete(nothrow_t &) noexcept;"); + verifyFormat("void operator delete(foo &) noexcept;"); + verifyFormat("void operator delete(foo) noexcept;"); + verifyFormat("void operator delete(int) noexcept;"); + verifyFormat("void operator delete(int &) noexcept;"); + verifyFormat("void operator delete(int &) volatile noexcept;"); + verifyFormat("void operator delete(int &) const"); + verifyFormat("void operator delete(int &) = default"); + verifyFormat("void operator delete(int &) = delete"); + verifyFormat("void operator delete(int &) [[noreturn]]"); + verifyFormat("void operator delete(int &) throw();"); + verifyFormat("void operator delete(int &) throw(int);"); + verifyFormat("auto operator delete(int &) -> int;"); + verifyFormat("auto operator delete(int &) override"); + verifyFormat("auto operator delete(int &) final"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); @@ -14696,33 +14721,6 @@ TEST_F(FormatTest, AlternativeOperators) { */ } -TEST_F(FormatTest, NotCastRPaen) { - - verifyFormat("void operator++(int) noexcept;"); - verifyFormat("void operator++(int &) noexcept;"); - verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " - "&) noexcept;"); - verifyFormat( - "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); - verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); - verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); - verifyFormat("void operator delete(nothrow_t &) noexcept;"); - verifyFormat("void operator delete(foo &) noexcept;"); - verifyFormat("void operator delete(foo) noexcept;"); - verifyFormat("void operator delete(int) noexcept;"); - verifyFormat("void operator delete(int &) noexcept;"); - verifyFormat("void operator delete(int &) volatile noexcept;"); - verifyFormat("void operator delete(int &) const"); - verifyFormat("void operator delete(int &) = default"); - verifyFormat("void operator delete(int &) = delete"); - verifyFormat("void operator delete(int &) [[noreturn]]"); - verifyFormat("void operator delete(int &) throw();"); - verifyFormat("void operator delete(int &) throw(int);"); - verifyFormat("auto operator delete(int &) -> int;"); - verifyFormat("auto operator delete(int &) override"); - verifyFormat("auto operator delete(int &) final"); -} - TEST_F(FormatTest, STLWhileNotDefineChed) { verifyFormat("#if defined(while)\n" "#define while EMIT WARNING C4005\n" -- cgit v1.2.1