@@ -813,10 +813,31 @@ PointerVariableConstraint::mkString(Constraints &CS,
813813 if (!ForItype && BaseType == " void" )
814814 K = Atom::A_Wild;
815815
816+ // In a case like `_Ptr<T> g[1]`, we need to push the ` g` onto EndStrs
817+ // before we can push the `>` and start drilling into T.
818+ //
819+ // Exception: In a case like `int *g[1]`, we don't want an extra space after
820+ // the `*` but we don't yet know whether we have a `*`, so we skip emitting
821+ // the name. We have to also skip the addArrayAnnotations because it would
822+ // cause the array annotations to be emitted before the name.
823+ //
824+ // Exception to the exception: In a case like `void (*fs[2])()`, we still
825+ // want to avoid emitting the name (which would add an extra space after the
826+ // `*`), but we _do_ need to call addArrayAnnotations so the array
827+ // annotations end up with the variable name rather than after the function
828+ // parameter list. The function pointer code knows to emit the name before
829+ // EndStrs.
830+ //
831+ // This passes the current regression tests but feels very ad-hoc.
832+ // REVIEW: Help me redesign this code!
816833 if (PrevArr && ArrSizes.at (TypeIdx).first != O_SizedArray && !EmittedName) {
817- EmittedName = true ;
818- addArrayAnnotations (ConstArrs, EndStrs);
819- EndStrs.push_front (" " + UseName);
834+ if (K != Atom::A_Wild || FV != nullptr ) {
835+ addArrayAnnotations (ConstArrs, EndStrs);
836+ }
837+ if (K != Atom::A_Wild) {
838+ EmittedName = true ;
839+ EndStrs.push_front (" " + UseName);
840+ }
820841 }
821842 PrevArr = ArrSizes.at (TypeIdx).first == O_SizedArray;
822843
@@ -906,6 +927,8 @@ PointerVariableConstraint::mkString(Constraints &CS,
906927 // the the stack array type.
907928 if (PrevArr && !EmittedName && AllArrays) {
908929 EmittedName = true ;
930+ // Note: If the whole type is an array, we can't have a "*", so we don't
931+ // need to worry about an extra space.
909932 EndStrs.push_front (" " + UseName);
910933 }
911934
@@ -944,8 +967,14 @@ PointerVariableConstraint::mkString(Constraints &CS,
944967 }
945968
946969 // No space after itype.
947- if (!EmittedName && !UseName.empty ())
948- Ss << " " << UseName;
970+ if (!EmittedName && !UseName.empty ()) {
971+ // REVIEW: Is there a better way? This might actually be less of a
972+ // maintenance problem than trying to have all code paths correctly set an
973+ // explicit flag for whether to add a space.
974+ if (!StringRef (Ss.str ()).endswith (" *" ))
975+ Ss << " " ;
976+ Ss << UseName;
977+ }
949978
950979 // Final array dropping.
951980 if (!ConstArrs.empty ()) {
0 commit comments