@@ -552,8 +552,11 @@ class SymbolAlias {
552
552
// / Print this alias to the given stream.
553
553
void print (raw_ostream &os) const {
554
554
os << (isType ? " !" : " #" ) << name;
555
- if (suffixIndex)
555
+ if (suffixIndex) {
556
+ if (isdigit (name.back ()))
557
+ os << ' _' ;
556
558
os << suffixIndex;
559
+ }
557
560
}
558
561
559
562
// / Returns true if this is a type alias.
@@ -659,6 +662,12 @@ class AliasInitializer {
659
662
template <typename T>
660
663
void generateAlias (T symbol, InProgressAliasInfo &alias, bool canBeDeferred);
661
664
665
+ // / Uniques the given alias name within the printer by generating name index
666
+ // / used as alias name suffix.
667
+ static unsigned
668
+ uniqueAliasNameIndex (StringRef alias, llvm::StringMap<unsigned > &nameCounts,
669
+ llvm::StringSet<llvm::BumpPtrAllocator &> &usedAliases);
670
+
662
671
// / Given a collection of aliases and symbols, initialize a mapping from a
663
672
// / symbol to a given alias.
664
673
static void initializeAliases (
@@ -1025,8 +1034,7 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
1025
1034
// / the string needs to be modified in any way, the provided buffer is used to
1026
1035
// / store the new copy,
1027
1036
static StringRef sanitizeIdentifier (StringRef name, SmallString<16 > &buffer,
1028
- StringRef allowedPunctChars = " $._-" ,
1029
- bool allowTrailingDigit = true ) {
1037
+ StringRef allowedPunctChars = " $._-" ) {
1030
1038
assert (!name.empty () && " Shouldn't have an empty name here" );
1031
1039
1032
1040
auto validChar = [&](char ch) {
@@ -1053,14 +1061,6 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
1053
1061
return buffer;
1054
1062
}
1055
1063
1056
- // If the name ends with a trailing digit, add a '_' to avoid potential
1057
- // conflicts with autogenerated ID's.
1058
- if (!allowTrailingDigit && isdigit (name.back ())) {
1059
- copyNameToBuffer ();
1060
- buffer.push_back (' _' );
1061
- return buffer;
1062
- }
1063
-
1064
1064
// Check to see that the name consists of only valid identifier characters.
1065
1065
for (char ch : name) {
1066
1066
if (!validChar (ch)) {
@@ -1073,6 +1073,37 @@ static StringRef sanitizeIdentifier(StringRef name, SmallString<16> &buffer,
1073
1073
return name;
1074
1074
}
1075
1075
1076
+ unsigned AliasInitializer::uniqueAliasNameIndex (
1077
+ StringRef alias, llvm::StringMap<unsigned > &nameCounts,
1078
+ llvm::StringSet<llvm::BumpPtrAllocator &> &usedAliases) {
1079
+ // Get nameIndex that will not generate conflicting name.
1080
+ unsigned nameIndex = 0 ;
1081
+ if (!usedAliases.count (alias)) {
1082
+ usedAliases.insert (alias);
1083
+ } else {
1084
+ // Otherwise, we had a conflict - probe until we find a unique name.
1085
+ SmallString<64 > probeAlias (alias);
1086
+ // alias with trailing digit will be printed as _N
1087
+ if (isdigit (alias.back ()))
1088
+ probeAlias.push_back (' _' );
1089
+ // nameCounts start from 1 because 0 is not printed in SymbolAlias.
1090
+ if (nameCounts[probeAlias] == 0 )
1091
+ nameCounts[probeAlias] = 1 ;
1092
+ // This is guaranteed to terminate (and usually in a single iteration)
1093
+ // because it generates new names by incrementing nameCounts.
1094
+ while (true ) {
1095
+ nameIndex = nameCounts[probeAlias]++;
1096
+ probeAlias += llvm::utostr (nameIndex);
1097
+ if (!usedAliases.count (probeAlias)) {
1098
+ usedAliases.insert (probeAlias);
1099
+ break ;
1100
+ }
1101
+ probeAlias.resize (alias.size () + isdigit (alias.back ()) ? 1 : 0 );
1102
+ }
1103
+ }
1104
+ return nameIndex;
1105
+ }
1106
+
1076
1107
// / Given a collection of aliases and symbols, initialize a mapping from a
1077
1108
// / symbol to a given alias.
1078
1109
void AliasInitializer::initializeAliases (
@@ -1084,12 +1115,17 @@ void AliasInitializer::initializeAliases(
1084
1115
return lhs.second < rhs.second ;
1085
1116
});
1086
1117
1118
+ // This keeps track of all of the non-numeric names that are in flight,
1119
+ // allowing us to check for duplicates.
1120
+ llvm::BumpPtrAllocator usedAliasAllocator;
1121
+ llvm::StringSet<llvm::BumpPtrAllocator &> usedAliases (usedAliasAllocator);
1122
+
1087
1123
llvm::StringMap<unsigned > nameCounts;
1088
1124
for (auto &[symbol, aliasInfo] : unprocessedAliases) {
1089
1125
if (!aliasInfo.alias )
1090
1126
continue ;
1091
1127
StringRef alias = *aliasInfo.alias ;
1092
- unsigned nameIndex = nameCounts[ alias]++ ;
1128
+ unsigned nameIndex = uniqueAliasNameIndex ( alias, nameCounts, usedAliases) ;
1093
1129
symbolToAlias.insert (
1094
1130
{symbol, SymbolAlias (alias, nameIndex, aliasInfo.isType ,
1095
1131
aliasInfo.canBeDeferred )});
@@ -1196,8 +1232,7 @@ void AliasInitializer::generateAlias(T symbol, InProgressAliasInfo &alias,
1196
1232
1197
1233
SmallString<16 > tempBuffer;
1198
1234
StringRef name =
1199
- sanitizeIdentifier (nameBuffer, tempBuffer, /* allowedPunctChars=*/ " $_-" ,
1200
- /* allowTrailingDigit=*/ false );
1235
+ sanitizeIdentifier (nameBuffer, tempBuffer, /* allowedPunctChars=*/ " $_-" );
1201
1236
name = name.copy (aliasAllocator);
1202
1237
alias = InProgressAliasInfo (name);
1203
1238
}
0 commit comments