From 880337da30b1a14d5170c1be4ec55279be573c22 Mon Sep 17 00:00:00 2001 From: Paul Hoad Date: Mon, 7 Oct 2019 17:03:44 +0000 Subject: [PATCH] [clang-format] [PR27004] omits leading space for noexcept when formatting operator delete() Summary: clang-format is incorrectly thinking the parameter parens are part of a cast operation, this is resulting in there sometimes being not space between the paren and the noexcept (and other keywords like volatile etc..) ``` void operator++(int) noexcept; void operator++(int &) noexcept; void operator delete(void *, std::size_t, const std::nothrow_t &)noexcept; ``` Reviewers: klimek, owenpan, mitchell-stellar Reviewed By: mitchell-stellar Subscribers: cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D68481 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@373922 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 7 +++++++ unittests/Format/FormatTest.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 2cc69108b2..d4519fe9c4 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1611,6 +1611,13 @@ private: if (Tok.Next->is(tok::question)) return false; + // 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)) + return false; + // As Java has no function types, a "(" after the ")" likely means that this // is a cast. if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren)) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8263dc41b3..8c2bc1ab22 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -14678,6 +14678,33 @@ 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"); +} + } // end namespace } // end namespace format } // end namespace clang -- 2.40.0