@@ -608,7 +608,7 @@ static void emitLeafTable(const DirectiveLanguage &DirLang, raw_ostream &OS,
608
608
std::vector<int > Ordering (Directives.size ());
609
609
std::iota (Ordering.begin (), Ordering.end (), 0 );
610
610
611
- sort (Ordering, [&](int A, int B) {
611
+ llvm:: sort (Ordering, [&](int A, int B) {
612
612
auto &LeavesA = LeafTable[A];
613
613
auto &LeavesB = LeafTable[B];
614
614
int DirA = LeavesA[0 ], DirB = LeavesB[0 ];
@@ -1113,59 +1113,63 @@ static void generateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
1113
1113
<< " Parser clause\" );\n " ;
1114
1114
}
1115
1115
1116
- static bool compareClauseName (const Record *R1, const Record *R2) {
1117
- Clause C1 (R1);
1118
- Clause C2 (R2);
1119
- return (C1.getName () > C2.getName ());
1116
+ using RecordWithText = std::pair<const Record *, StringRef>;
1117
+
1118
+ static bool compareRecordText (const RecordWithText &A,
1119
+ const RecordWithText &B) {
1120
+ return A.second > B.second ;
1121
+ }
1122
+
1123
+ static std::vector<RecordWithText>
1124
+ getSpellingTexts (ArrayRef<const Record *> Records) {
1125
+ std::vector<RecordWithText> List;
1126
+ for (const Record *R : Records) {
1127
+ Clause C (R);
1128
+ List.push_back (std::make_pair (R, C.getName ()));
1129
+ llvm::transform (C.getAliases (), std::back_inserter (List),
1130
+ [R](StringRef S) { return std::make_pair (R, S); });
1131
+ }
1132
+ return List;
1120
1133
}
1121
1134
1122
1135
// Generate the parser for the clauses.
1123
1136
static void generateFlangClausesParser (const DirectiveLanguage &DirLang,
1124
1137
raw_ostream &OS) {
1125
1138
std::vector<const Record *> Clauses = DirLang.getClauses ();
1126
- // Sort clauses in reverse alphabetical order so with clauses with same
1127
- // beginning, the longer option is tried before.
1128
- sort (Clauses, compareClauseName);
1139
+ // Sort clauses in the reverse alphabetical order with respect to their
1140
+ // names and aliases, so that longer names are tried before shorter ones.
1141
+ std::vector<std::pair<const Record *, StringRef>> Names =
1142
+ getSpellingTexts (Clauses);
1143
+ llvm::sort (Names, compareRecordText);
1129
1144
IfDefScope Scope (" GEN_FLANG_CLAUSES_PARSER" , OS);
1130
1145
StringRef Base = DirLang.getFlangClauseBaseClass ();
1131
1146
1147
+ unsigned LastIndex = Names.size () - 1 ;
1132
1148
OS << " \n " ;
1133
- unsigned Index = 0 ;
1134
- unsigned LastClauseIndex = Clauses.size () - 1 ;
1135
1149
OS << " TYPE_PARSER(\n " ;
1136
- for (const Clause Clause : Clauses) {
1137
- const std::vector<StringRef> &Aliases = Clause.getAliases ();
1138
- if (Aliases.empty ()) {
1139
- OS << " \" " << Clause.getName () << " \" " ;
1140
- } else {
1141
- OS << " ("
1142
- << " \" " << Clause.getName () << " \" _tok" ;
1143
- for (StringRef Alias : Aliases) {
1144
- OS << " || \" " << Alias << " \" _tok" ;
1145
- }
1146
- OS << " )" ;
1147
- }
1150
+ for (auto [Index, RecTxt] : llvm::enumerate (Names)) {
1151
+ auto [R, N] = RecTxt;
1152
+ Clause C (R);
1148
1153
1149
- StringRef FlangClass = Clause .getFlangClass ();
1150
- OS << " >> construct<" << Base << " >(construct<" << Base
1151
- << " ::" << Clause .getFormattedParserClassName () << " >(" ;
1154
+ StringRef FlangClass = C .getFlangClass ();
1155
+ OS << " \" " << N << " \" >> construct<" << Base << " >(construct<" << Base
1156
+ << " ::" << C .getFormattedParserClassName () << " >(" ;
1152
1157
if (FlangClass.empty ()) {
1153
1158
OS << " ))" ;
1154
- if (Index != LastClauseIndex )
1159
+ if (Index != LastIndex )
1155
1160
OS << " ||" ;
1156
1161
OS << " \n " ;
1157
- ++Index;
1158
1162
continue ;
1159
1163
}
1160
1164
1161
- if (Clause .isValueOptional ())
1165
+ if (C .isValueOptional ())
1162
1166
OS << " maybe(" ;
1163
1167
OS << " parenthesized(" ;
1164
- if (Clause .isValueList ())
1168
+ if (C .isValueList ())
1165
1169
OS << " nonemptyList(" ;
1166
1170
1167
- if (!Clause .getPrefix ().empty ())
1168
- OS << " \" " << Clause .getPrefix () << " :\" >> " ;
1171
+ if (!C .getPrefix ().empty ())
1172
+ OS << " \" " << C .getPrefix () << " :\" >> " ;
1169
1173
1170
1174
// The common Flang parser are used directly. Their name is identical to
1171
1175
// the Flang class with first letter as lowercase. If the Flang class is
@@ -1181,19 +1185,18 @@ static void generateFlangClausesParser(const DirectiveLanguage &DirLang,
1181
1185
.Case (" ScalarLogicalExpr" , " scalarLogicalExpr" )
1182
1186
.Default ((" Parser<" + FlangClass + " >{}" ).toStringRef (Scratch));
1183
1187
OS << Parser;
1184
- if (!Clause .getPrefix ().empty () && Clause .isPrefixOptional ())
1188
+ if (!C .getPrefix ().empty () && C .isPrefixOptional ())
1185
1189
OS << " || " << Parser;
1186
- if (Clause .isValueList ()) // close nonemptyList(.
1190
+ if (C .isValueList ()) // close nonemptyList(.
1187
1191
OS << " )" ;
1188
1192
OS << " )" ; // close parenthesized(.
1189
1193
1190
- if (Clause .isValueOptional ()) // close maybe(.
1194
+ if (C .isValueOptional ()) // close maybe(.
1191
1195
OS << " )" ;
1192
1196
OS << " ))" ;
1193
- if (Index != LastClauseIndex )
1197
+ if (Index != LastIndex )
1194
1198
OS << " ||" ;
1195
1199
OS << " \n " ;
1196
- ++Index;
1197
1200
}
1198
1201
OS << " )\n " ;
1199
1202
}
0 commit comments