Skip to content

Commit f23aaf8

Browse files
gixmemfrob
authored andcommitted
[clang-format] Fix east const pointer alignment of operators
This patch fixes left pointer alignment after pointer qualifiers of operators. Currently "operator void const*()" is formatted with a space between const and pointer despite setting PointerAlignment to Left. AFAICS this has been broken since clang-format 10. Reviewed By: MyDeveloperDay, curdeius Differential Revision: https://reviews.llvm.org/D99458
1 parent 21f7af2 commit f23aaf8

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,18 +2957,24 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
29572957
// Space between the type and the * in:
29582958
// operator void*()
29592959
// operator char*()
2960+
// operator void const*()
2961+
// operator void volatile*()
29602962
// operator /*comment*/ const char*()
29612963
// operator volatile /*comment*/ char*()
29622964
// operator Foo*()
29632965
// operator C<T>*()
29642966
// operator std::Foo*()
29652967
// operator C<T>::D<U>*()
29662968
// dependent on PointerAlignment style.
2967-
if (Previous &&
2968-
(Previous->endsSequence(tok::kw_operator) ||
2969-
Previous->endsSequence(tok::kw_const, tok::kw_operator) ||
2970-
Previous->endsSequence(tok::kw_volatile, tok::kw_operator)))
2971-
return (Style.PointerAlignment != FormatStyle::PAS_Left);
2969+
if (Previous) {
2970+
if (Previous->endsSequence(tok::kw_operator))
2971+
return (Style.PointerAlignment != FormatStyle::PAS_Left);
2972+
if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile))
2973+
return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
2974+
(Style.SpaceAroundPointerQualifiers ==
2975+
FormatStyle::SAPQ_After) ||
2976+
(Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
2977+
}
29722978
}
29732979
const auto SpaceRequiredForArrayInitializerLSquare =
29742980
[](const FormatToken &LSquareTok, const FormatStyle &Style) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12731,6 +12731,27 @@ TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
1273112731
verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
1273212732
verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
1273312733

12734+
verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
12735+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
12736+
SAPQ_Default);
12737+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
12738+
SAPQ_Default);
12739+
12740+
verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
12741+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
12742+
SAPQ_Before);
12743+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
12744+
SAPQ_Before);
12745+
12746+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
12747+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
12748+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
12749+
SAPQ_After);
12750+
12751+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
12752+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
12753+
verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
12754+
1273412755
#undef verifyQualifierSpaces
1273512756

1273612757
FormatStyle Spaces = getLLVMStyle();
@@ -18735,6 +18756,10 @@ TEST_F(FormatTest, OperatorSpacing) {
1873518756
verifyFormat("Foo::operator void **();", Style);
1873618757
verifyFormat("Foo::operator void *&();", Style);
1873718758
verifyFormat("Foo::operator void *&&();", Style);
18759+
verifyFormat("Foo::operator void const *();", Style);
18760+
verifyFormat("Foo::operator void const **();", Style);
18761+
verifyFormat("Foo::operator void const *&();", Style);
18762+
verifyFormat("Foo::operator void const *&&();", Style);
1873818763
verifyFormat("Foo::operator()(void *);", Style);
1873918764
verifyFormat("Foo::operator*(void *);", Style);
1874018765
verifyFormat("Foo::operator*();", Style);
@@ -18756,6 +18781,7 @@ TEST_F(FormatTest, OperatorSpacing) {
1875618781

1875718782
verifyFormat("Foo::operator&();", Style);
1875818783
verifyFormat("Foo::operator void &();", Style);
18784+
verifyFormat("Foo::operator void const &();", Style);
1875918785
verifyFormat("Foo::operator()(void &);", Style);
1876018786
verifyFormat("Foo::operator&(void &);", Style);
1876118787
verifyFormat("Foo::operator&();", Style);
@@ -18764,6 +18790,7 @@ TEST_F(FormatTest, OperatorSpacing) {
1876418790
verifyFormat("Foo::operator&&();", Style);
1876518791
verifyFormat("Foo::operator**();", Style);
1876618792
verifyFormat("Foo::operator void &&();", Style);
18793+
verifyFormat("Foo::operator void const &&();", Style);
1876718794
verifyFormat("Foo::operator()(void &&);", Style);
1876818795
verifyFormat("Foo::operator&&(void &&);", Style);
1876918796
verifyFormat("Foo::operator&&();", Style);
@@ -18784,6 +18811,11 @@ TEST_F(FormatTest, OperatorSpacing) {
1878418811
verifyFormat("Foo::operator void*();", Style);
1878518812
verifyFormat("Foo::operator void**();", Style);
1878618813
verifyFormat("Foo::operator void*&();", Style);
18814+
verifyFormat("Foo::operator void*&&();", Style);
18815+
verifyFormat("Foo::operator void const*();", Style);
18816+
verifyFormat("Foo::operator void const**();", Style);
18817+
verifyFormat("Foo::operator void const*&();", Style);
18818+
verifyFormat("Foo::operator void const*&&();", Style);
1878718819
verifyFormat("Foo::operator/*comment*/ void*();", Style);
1878818820
verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
1878918821
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
@@ -18805,6 +18837,7 @@ TEST_F(FormatTest, OperatorSpacing) {
1880518837

1880618838
verifyFormat("Foo::operator&();", Style);
1880718839
verifyFormat("Foo::operator void&();", Style);
18840+
verifyFormat("Foo::operator void const&();", Style);
1880818841
verifyFormat("Foo::operator/*comment*/ void&();", Style);
1880918842
verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
1881018843
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
@@ -18815,6 +18848,7 @@ TEST_F(FormatTest, OperatorSpacing) {
1881518848

1881618849
verifyFormat("Foo::operator&&();", Style);
1881718850
verifyFormat("Foo::operator void&&();", Style);
18851+
verifyFormat("Foo::operator void const&&();", Style);
1881818852
verifyFormat("Foo::operator/*comment*/ void&&();", Style);
1881918853
verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
1882018854
verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
@@ -18850,13 +18884,15 @@ TEST_F(FormatTest, OperatorSpacing) {
1885018884

1885118885
verifyFormat("Foo::operator&();", Style);
1885218886
verifyFormat("Foo::operator void &();", Style);
18887+
verifyFormat("Foo::operator void const &();", Style);
1885318888
verifyFormat("Foo::operator()(void &);", Style);
1885418889
verifyFormat("Foo::operator&(void &);", Style);
1885518890
verifyFormat("Foo::operator&();", Style);
1885618891
verifyFormat("operator&(int (&)(), class Foo);", Style);
1885718892

1885818893
verifyFormat("Foo::operator&&();", Style);
1885918894
verifyFormat("Foo::operator void &&();", Style);
18895+
verifyFormat("Foo::operator void const &&();", Style);
1886018896
verifyFormat("Foo::operator()(void &&);", Style);
1886118897
verifyFormat("Foo::operator&&(void &&);", Style);
1886218898
verifyFormat("Foo::operator&&();", Style);

0 commit comments

Comments
 (0)