Skip to content

Commit d4923e2

Browse files
mkString fixes that were previously part of the multi-decl overhaul.
Back when the multi-decl overhaul (#657) switched all unchanged multi-decl members from Decl::print to mkString, these fixes were needed to prevent 3C's output from getting worse on some unchanged multi-decl members in the regression tests. The fixes are no longer needed for that reason, but Mike still asked me to submit them. They do benefit some more complex cases that occur in the regression tests, so the updates to the expected output of those tests provide test coverage for the fixes.
1 parent 2a0df40 commit d4923e2

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

clang/lib/3C/ConstraintVariables.cpp

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

836+
// In a case like `_Ptr<T> g[1]`, we need to push the ` g` onto EndStrs
837+
// before we can push the `>` and start drilling into T.
838+
//
839+
// Exception: In a case like `int *g[1]`, we don't want an extra space after
840+
// the `*` but we don't yet know whether we have a `*`, so we skip emitting
841+
// the name. We have to also skip the addArrayAnnotations because it would
842+
// cause the array annotations to be emitted before the name.
843+
//
844+
// Exception to the exception: In a case like `void (*fs[2])()`, we still
845+
// want to avoid emitting the name (which would add an extra space after the
846+
// `*`), but we _do_ need to call addArrayAnnotations so the array
847+
// annotations end up with the variable name rather than after the function
848+
// parameter list. The function pointer code knows to emit the name before
849+
// EndStrs.
850+
//
851+
// This passes the current regression tests but feels very ad-hoc.
852+
// REVIEW: Help me redesign this code!
836853
if (PrevArr && ArrSizes.at(TypeIdx).first != O_SizedArray && !EmittedName) {
837-
EmittedName = true;
838-
addArrayAnnotations(ConstArrs, EndStrs);
839-
EndStrs.push_front(" " + UseName);
854+
if (K != Atom::A_Wild || FV != nullptr) {
855+
addArrayAnnotations(ConstArrs, EndStrs);
856+
}
857+
if (K != Atom::A_Wild) {
858+
EmittedName = true;
859+
EndStrs.push_front(" " + UseName);
860+
}
840861
}
841862
PrevArr = ArrSizes.at(TypeIdx).first == O_SizedArray;
842863

@@ -926,6 +947,8 @@ PointerVariableConstraint::mkString(Constraints &CS,
926947
// the the stack array type.
927948
if (PrevArr && !EmittedName && AllArrays) {
928949
EmittedName = true;
950+
// Note: If the whole type is an array, we can't have a "*", so we don't
951+
// need to worry about an extra space.
929952
EndStrs.push_front(" " + UseName);
930953
}
931954

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/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)