Skip to content

Commit 78d1f1b

Browse files
committed
[utils][TableGen] Unify converting names to upper-camel case
There were 3 different functions in DirectiveEmitter.cpp doing essentially the same thing: taking a name separated with _ or whitepace, and converting it to the upper-camel case. Extract that into a single function that can handle different sets of separators.
1 parent 809e512 commit 78d1f1b

File tree

2 files changed

+33
-45
lines changed

2 files changed

+33
-45
lines changed

llvm/include/llvm/TableGen/DirectiveEmitter.h

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,39 @@ class BaseRecord {
113113

114114
// Returns the name of the directive formatted for output. Whitespace are
115115
// replaced with underscores.
116-
static std::string formatName(StringRef Name) {
116+
static std::string getSnakeName(StringRef Name) {
117117
std::string N = Name.str();
118118
llvm::replace(N, ' ', '_');
119119
return N;
120120
}
121121

122+
static std::string getUpperCamelName(StringRef Name, StringRef Sep) {
123+
std::string Camel = Name.str();
124+
// Convert to uppercase
125+
bool Cap = true;
126+
llvm::transform(Camel, Camel.begin(), [&](unsigned char C) {
127+
if (Sep.contains(C)) {
128+
assert(!Cap && "No initial or repeated separators");
129+
Cap = true;
130+
} else if (Cap) {
131+
C = llvm::toUpper(C);
132+
Cap = false;
133+
}
134+
return C;
135+
});
136+
size_t Out = 0;
137+
// Remove separators
138+
for (size_t In = 0, End = Camel.size(); In != End; ++In) {
139+
unsigned char C = Camel[In];
140+
if (!Sep.contains(C))
141+
Camel[Out++] = C;
142+
}
143+
Camel.resize(Out);
144+
return Camel;
145+
}
146+
122147
std::string getFormattedName() const {
123-
return formatName(Def->getValueAsString("name"));
148+
return getSnakeName(Def->getValueAsString("name"));
124149
}
125150

126151
bool isDefault() const { return Def->getValueAsBit("isDefault"); }
@@ -172,26 +197,13 @@ class Directive : public BaseRecord {
172197

173198
// Clang uses a different format for names of its directives enum.
174199
std::string getClangAccSpelling() const {
175-
std::string Name = Def->getValueAsString("name").str();
200+
StringRef Name = Def->getValueAsString("name");
176201

177202
// Clang calls the 'unknown' value 'invalid'.
178203
if (Name == "unknown")
179204
return "Invalid";
180205

181-
// Clang entries all start with a capital letter, so apply that.
182-
Name[0] = std::toupper(Name[0]);
183-
// Additionally, spaces/underscores are handled by capitalizing the next
184-
// letter of the name and removing the space/underscore.
185-
for (unsigned I = 0; I < Name.size(); ++I) {
186-
if (Name[I] == ' ' || Name[I] == '_') {
187-
Name.erase(I, 1);
188-
assert(Name[I] != ' ' && Name[I] != '_' &&
189-
"No double spaces/underscores");
190-
Name[I] = std::toupper(Name[I]);
191-
}
192-
}
193-
194-
return Name;
206+
return BaseRecord::getUpperCamelName(Name, " _");
195207
}
196208
};
197209

@@ -218,19 +230,7 @@ class Clause : public BaseRecord {
218230
// num_threads -> NumThreads
219231
std::string getFormattedParserClassName() const {
220232
StringRef Name = Def->getValueAsString("name");
221-
std::string N = Name.str();
222-
bool Cap = true;
223-
llvm::transform(N, N.begin(), [&Cap](unsigned char C) {
224-
if (Cap == true) {
225-
C = toUpper(C);
226-
Cap = false;
227-
} else if (C == '_') {
228-
Cap = true;
229-
}
230-
return C;
231-
});
232-
erase(N, '_');
233-
return N;
233+
return BaseRecord::getUpperCamelName(Name, "_");
234234
}
235235

236236
// Clang uses a different format for names of its clause enum, which can be
@@ -241,20 +241,8 @@ class Clause : public BaseRecord {
241241
!ClangSpelling.empty())
242242
return ClangSpelling.str();
243243

244-
std::string Name = Def->getValueAsString("name").str();
245-
// Clang entries all start with a capital letter, so apply that.
246-
Name[0] = std::toupper(Name[0]);
247-
// Additionally, underscores are handled by capitalizing the next letter of
248-
// the name and removing the underscore.
249-
for (unsigned I = 0; I < Name.size(); ++I) {
250-
if (Name[I] == '_') {
251-
Name.erase(I, 1);
252-
assert(Name[I] != '_' && "No double underscores");
253-
Name[I] = std::toupper(Name[I]);
254-
}
255-
}
256-
257-
return Name;
244+
StringRef Name = Def->getValueAsString("name");
245+
return BaseRecord::getUpperCamelName(Name, "_");
258246
}
259247

260248
// Optional field.

llvm/utils/TableGen/Basic/DirectiveEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ static void generateGetDirectiveLanguages(const DirectiveLanguage &DirLang,
839839
D.getSourceLanguages(), OS,
840840
[&](const Record *L) {
841841
StringRef N = L->getValueAsString("name");
842-
OS << "SourceLanguage::" << BaseRecord::formatName(N);
842+
OS << "SourceLanguage::" << BaseRecord::getSnakeName(N);
843843
},
844844
" | ");
845845
OS << ";\n";

0 commit comments

Comments
 (0)