MyDeveloperDay created this revision. MyDeveloperDay added reviewers: krasimir, curdeius, JakeMerdichAMD. MyDeveloperDay added projects: clang, clang-format.
https://bugs.llvm.org/show_bug.cgi?id=46609 Ensure `*...` obey they left/middle/right rules of Pointer alignment Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D83564 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 @@ -5325,7 +5325,7 @@ verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); verifyFormat( "template <class... T>\n" - "array(T &&... t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); + "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); @@ -8179,13 +8179,20 @@ } TEST_F(FormatTest, UnderstandsEllipsis) { + FormatStyle Style = getLLVMStyle(); verifyFormat("int printf(const char *fmt, ...);"); verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); - verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); + verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); + + verifyFormat("template <int *...PP> a;", Style); + + Style.PointerAlignment = FormatStyle::PAS_Left; + verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); + + verifyFormat("template <int*... PP> a;", Style); - FormatStyle PointersLeft = getLLVMStyle(); - PointersLeft.PointerAlignment = FormatStyle::PAS_Left; - verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); + Style.PointerAlignment = FormatStyle::PAS_Middle; + verifyFormat("template <int *... PP> a;", Style); } TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2844,6 +2844,11 @@ Left.Previous && !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon, tok::l_square)); + // Ensure right pointer alignement with ellipsis e.g. int *...P + if (Left.is(tok::ellipsis) && Left.Previous && + Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) + return Style.PointerAlignment != FormatStyle::PAS_Right; + if (Right.is(tok::star) && Left.is(tok::l_paren)) return false; if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -5325,7 +5325,7 @@ verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); verifyFormat( "template <class... T>\n" - "array(T &&... t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); + "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); @@ -8179,13 +8179,20 @@ } TEST_F(FormatTest, UnderstandsEllipsis) { + FormatStyle Style = getLLVMStyle(); verifyFormat("int printf(const char *fmt, ...);"); verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); - verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); + verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); + + verifyFormat("template <int *...PP> a;", Style); + + Style.PointerAlignment = FormatStyle::PAS_Left; + verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); + + verifyFormat("template <int*... PP> a;", Style); - FormatStyle PointersLeft = getLLVMStyle(); - PointersLeft.PointerAlignment = FormatStyle::PAS_Left; - verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); + Style.PointerAlignment = FormatStyle::PAS_Middle; + verifyFormat("template <int *... PP> a;", Style); } TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2844,6 +2844,11 @@ Left.Previous && !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon, tok::l_square)); + // Ensure right pointer alignement with ellipsis e.g. int *...P + if (Left.is(tok::ellipsis) && Left.Previous && + Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) + return Style.PointerAlignment != FormatStyle::PAS_Right; + if (Right.is(tok::star) && Left.is(tok::l_paren)) return false; if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits