Skip to content

Commit 9dc4968

Browse files
Stop mkString from producing the extra space in int * x[10].
The problem already affected several regression tests for more complex cases (which expected the extra space), and we didn't seem to notice or care. But now it is affecting every unchanged variable of that form in a multi-decl that gets rewritten, and this broke several other regression tests.
1 parent 17de215 commit 9dc4968

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

clang/lib/3C/ConstraintVariables.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,31 @@ PointerVariableConstraint::mkString(Constraints &CS,
858858
if (!ForItype && InferredGenericIndex == -1 && isVoidPtr())
859859
K = Atom::A_Wild;
860860

861+
// In a case like `_Ptr<T> g[1]`, we need to push the ` g` onto EndStrs
862+
// before we can push the `>` and start drilling into T.
863+
//
864+
// Exception: In a case like `int *g[1]`, we don't want an extra space after
865+
// the `*` but we don't yet know whether we have a `*`, so we skip emitting
866+
// the name. We have to also skip the addArrayAnnotations because it would
867+
// cause the array annotations to be emitted before the name.
868+
//
869+
// Exception to the exception: In a case like `void (*fs[2])()`, we still
870+
// want to avoid emitting the name (which would add an extra space after the
871+
// `*`), but we _do_ need to call addArrayAnnotations so the array
872+
// annotations end up with the variable name rather than after the function
873+
// parameter list. The function pointer code knows to emit the name before
874+
// EndStrs.
875+
//
876+
// This passes the current regression tests but feels very ad-hoc.
877+
// REVIEW: Help me redesign this code!
861878
if (PrevArr && ArrSizes.at(TypeIdx).first != O_SizedArray && !EmittedName) {
862-
EmittedName = true;
863-
addArrayAnnotations(ConstArrs, EndStrs);
864-
EndStrs.push_front(" " + UseName);
879+
if (K != Atom::A_Wild || FV != nullptr) {
880+
addArrayAnnotations(ConstArrs, EndStrs);
881+
}
882+
if (K != Atom::A_Wild) {
883+
EmittedName = true;
884+
EndStrs.push_front(" " + UseName);
885+
}
865886
}
866887
PrevArr = ArrSizes.at(TypeIdx).first == O_SizedArray;
867888

@@ -951,6 +972,8 @@ PointerVariableConstraint::mkString(Constraints &CS,
951972
// the the stack array type.
952973
if (PrevArr && !EmittedName && AllArrays) {
953974
EmittedName = true;
975+
// Note: If the whole type is an array, we can't have a "*", so we don't
976+
// need to worry about an extra space.
954977
EndStrs.push_front(" " + UseName);
955978
}
956979

clang/test/3C/compound_literal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void lists() {
6767

6868
int *d[2] = (int *[2]){&x, (int *)1};
6969
//CHECK_NOALL: int *d[2] = (int *[2]){&x, (int *)1};
70-
//CHECK_ALL: int * d _Checked[2] = (int * _Checked[2]){&x, (int *)1};
70+
//CHECK_ALL: int *d _Checked[2] = (int * _Checked[2]){&x, (int *)1};
7171
int *d0 = d[0];
7272
//CHECK: int *d0 = d[0];
7373

clang/test/3C/fptr_array.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
void (*fs[2])(int *);
1111
void (*f)(int *);
12-
//CHECK_NOALL: void (* fs[2])(_Ptr<int>) = {((void *)0)};
12+
//CHECK_NOALL: void (*fs[2])(_Ptr<int>) = {((void *)0)};
1313
//CHECK_NOALL: void (*f)(_Ptr<int>) = ((void *)0);
1414
//CHECK_ALL: _Ptr<void (_Ptr<int>)> fs _Checked[2] = {((void *)0)};
1515
//CHECK_ALL: _Ptr<void (_Ptr<int>)> f = ((void *)0);
1616

1717
void (*gs[2])(int *);
1818
void g_impl(int *x) { x = 1; }
1919
void (*g)(int *) = g_impl;
20-
//CHECK_NOALL: void (* gs[2])(int * : itype(_Ptr<int>)) = {((void *)0)};
20+
//CHECK_NOALL: void (*gs[2])(int * : itype(_Ptr<int>)) = {((void *)0)};
2121
//CHECK_NOALL: void g_impl(int *x : itype(_Ptr<int>)) { x = 1; }
2222
//CHECK_NOALL: void (*g)(int * : itype(_Ptr<int>)) = g_impl;
2323

@@ -30,21 +30,21 @@ void (*h)(void *);
3030

3131
int *(*is[2])(void);
3232
int *(*i)(void);
33-
//CHECK_NOALL: _Ptr<int> (* is[2])(void) = {((void *)0)};
33+
//CHECK_NOALL: _Ptr<int> (*is[2])(void) = {((void *)0)};
3434
//CHECK_NOALL: _Ptr<int> (*i)(void) = ((void *)0);
3535
//CHECK_ALL: _Ptr<_Ptr<int> (void)> is _Checked[2] = {((void *)0)};
3636
//CHECK_ALL: _Ptr<_Ptr<int> (void)> i = ((void *)0);
3737

3838
int *(**js[2])(void);
3939
int *(**j)(void);
40-
//CHECK_NOALL: _Ptr<int> (** js[2])(void) = {((void *)0)};
40+
//CHECK_NOALL: _Ptr<int> (**js[2])(void) = {((void *)0)};
4141
//CHECK_NOALL: _Ptr<int> (**j)(void) = ((void *)0);
4242
//CHECK_ALL: _Ptr<_Ptr<_Ptr<int> (void)>> js _Checked[2] = {((void *)0)};
4343
//CHECK_ALL: _Ptr<_Ptr<_Ptr<int> (void)>> j = ((void *)0);
4444

4545
int *(*ks[2][2])(void);
4646
int *(*k)(void);
47-
//CHECK_NOALL: _Ptr<int> (* ks[2][2])(void) = {((void *)0)};
47+
//CHECK_NOALL: _Ptr<int> (*ks[2][2])(void) = {((void *)0)};
4848
//CHECK_NOALL: _Ptr<int> (*k)(void) = ((void *)0);
4949
//CHECK_ALL: _Ptr<_Ptr<int> (void)> ks _Checked[2] _Checked[2] = {((void *)0)};
5050
//CHECK_ALL: _Ptr<_Ptr<int> (void)> k = ((void *)0);

clang/test/3C/macro_end_of_decl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int *f EQ 0;
3838

3939
int(*g0) ARGS, g1 SIZE, *g2 EQ 0;
4040
//CHECK: _Ptr<int (void)> g0 = ((void *)0);
41-
//CHECK_NOALL: int g1[1];
41+
//CHECK_NOALL: int g1 SIZE;
4242
//CHECK_ALL: int g1 _Checked SIZE;
4343
//CHECK: _Ptr<int> g2 = 0;
4444

clang/test/3C/ptr_array.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void test1(int *a) {
2828

2929
int *b[1] = {a};
3030
//CHECK_NOALL: int *b[1] = {a};
31-
//CHECK_ALL: int * b _Checked[1] = {a};
31+
//CHECK_ALL: int *b _Checked[1] = {a};
3232
}
3333

3434
/* Example from from the issue */
@@ -40,7 +40,7 @@ int *foo() {
4040
int z = 3;
4141
int *ptrs[4] = {&x, &y, &z, (int *)5};
4242
//CHECK_NOALL: int *ptrs[4] = {&x, &y, &z, (int *)5};
43-
//CHECK_ALL: int * ptrs _Checked[4] = {&x, &y, &z, (int *)5};
43+
//CHECK_ALL: int *ptrs _Checked[4] = {&x, &y, &z, (int *)5};
4444
int *ret;
4545
//CHECK: int *ret;
4646
for (int i = 0; i < 4; i++) {

0 commit comments

Comments
 (0)