MyDeveloperDay created this revision. MyDeveloperDay added reviewers: klimek, mitchell-stellar, owenpan, sammccall. MyDeveloperDay added projects: clang-format, clang-tools-extra. Herald added a project: clang.
see https://bugs.llvm.org/show_bug.cgi?id=35518 clang-format removes spaces around deduction guides but not trailing return types, make the consistent template <typename T> S(T)->S<T>; auto f(int, int) -> double; becomes template <typename T> S(T) -> S<T>; auto f(int, int) -> double; Repository: rC Clang https://reviews.llvm.org/D69577 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 @@ -4977,6 +4977,15 @@ verifyFormat("void f() { auto a = b->c(); }"); } +TEST_F(FormatTest, DeductionGuides) { + verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); + verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); + verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); + verifyFormat( + "template <class... T>\n" + "array(T &&... t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); +} + TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { // Avoid breaking before trailing 'const' or other trailing annotations, if // they are not function-like. Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2932,6 +2932,16 @@ return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles); } + + // Deduction guides add a space around the arrow "...) -> A<T>". + if (Left.is(tok::r_paren) && + Right.startsSequence(tok::arrow, TT_TrailingAnnotation, + TT_TemplateOpener)) + return true; + if (Left.is(tok::arrow) && + Right.startsSequence(TT_TrailingAnnotation, TT_TemplateOpener)) + return true; + if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod)))
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -4977,6 +4977,15 @@ verifyFormat("void f() { auto a = b->c(); }"); } +TEST_F(FormatTest, DeductionGuides) { + verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); + verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); + verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); + verifyFormat( + "template <class... T>\n" + "array(T &&... t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); +} + TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { // Avoid breaking before trailing 'const' or other trailing annotations, if // they are not function-like. Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2932,6 +2932,16 @@ return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles); } + + // Deduction guides add a space around the arrow "...) -> A<T>". + if (Left.is(tok::r_paren) && + Right.startsSequence(tok::arrow, TT_TrailingAnnotation, + TT_TemplateOpener)) + return true; + if (Left.is(tok::arrow) && + Right.startsSequence(TT_TrailingAnnotation, TT_TemplateOpener)) + return true; + if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod)))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits