Skip to content

Commit 98563b1

Browse files
authored
[libc][TableGen] Migrate libc-hdrgen backend to use const RecordKeeper (#107542)
Migrate libc-hdrgen backend to use const RecordKeeper
1 parent b60c6cb commit 98563b1

File tree

12 files changed

+76
-73
lines changed

12 files changed

+76
-73
lines changed

libc/utils/HdrGen/Command.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Command {
4545
virtual ~Command();
4646

4747
virtual void run(llvm::raw_ostream &OS, const ArgVector &Args,
48-
llvm::StringRef StdHeader, llvm::RecordKeeper &Records,
48+
llvm::StringRef StdHeader, const llvm::RecordKeeper &Records,
4949
const ErrorReporter &Reporter) const = 0;
5050
};
5151

libc/utils/HdrGen/Generator.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ void Generator::parseCommandArgs(llvm::StringRef ArgStr, ArgVector &Args) {
6464
}
6565
}
6666

67-
void Generator::generate(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
67+
void Generator::generate(llvm::raw_ostream &OS,
68+
const llvm::RecordKeeper &Records) {
6869
auto DefFileBuffer = llvm::MemoryBuffer::getFile(HeaderDefFile);
6970
if (!DefFileBuffer) {
7071
llvm::errs() << "Unable to open " << HeaderDefFile << ".\n";
@@ -126,7 +127,7 @@ void Generator::generate(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
126127
}
127128

128129
void Generator::generateDecls(llvm::raw_ostream &OS,
129-
llvm::RecordKeeper &Records) {
130+
const llvm::RecordKeeper &Records) {
130131

131132
OS << "//===-- C standard declarations for " << StdHeader << " "
132133
<< std::string(80 - (42 + StdHeader.size()), '-') << "===//\n"
@@ -161,15 +162,15 @@ void Generator::generateDecls(llvm::raw_ostream &OS,
161162
if (G.FunctionSpecMap.find(Name) == G.FunctionSpecMap.end())
162163
continue;
163164

164-
llvm::Record *FunctionSpec = G.FunctionSpecMap[Name];
165-
llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
166-
llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
165+
const llvm::Record *FunctionSpec = G.FunctionSpecMap[Name];
166+
const llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
167+
const llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
167168

168169
OS << G.getTypeAsString(ReturnType) << " " << Name << "(";
169170

170171
auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
171172
for (size_t i = 0; i < ArgsList.size(); ++i) {
172-
llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
173+
const llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
173174
OS << G.getTypeAsString(ArgType);
174175
if (i < ArgsList.size() - 1)
175176
OS << ", ";
@@ -182,7 +183,7 @@ void Generator::generateDecls(llvm::raw_ostream &OS,
182183
for (const auto &Name : EntrypointNameList) {
183184
if (G.ObjectSpecMap.find(Name) == G.ObjectSpecMap.end())
184185
continue;
185-
llvm::Record *ObjectSpec = G.ObjectSpecMap[Name];
186+
const llvm::Record *ObjectSpec = G.ObjectSpecMap[Name];
186187
auto Type = ObjectSpec->getValueAsString("Type");
187188
OS << "extern " << Type << " " << Name << " __LIBC_ATTRS;\n";
188189
}

libc/utils/HdrGen/Generator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class Generator {
5151
: HeaderDefFile(DefFile), EntrypointNameList(EN), StdHeader(Header),
5252
ArgMap(Map) {}
5353

54-
void generate(llvm::raw_ostream &OS, llvm::RecordKeeper &Records);
55-
void generateDecls(llvm::raw_ostream &OS, llvm::RecordKeeper &Records);
54+
void generate(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records);
55+
void generateDecls(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records);
5656
};
5757

5858
} // namespace llvm_libc

libc/utils/HdrGen/IncludeFileCommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const char IncludeFileCommand::Name[] = "include_file";
1919

2020
void IncludeFileCommand::run(llvm::raw_ostream &OS, const ArgVector &Args,
2121
llvm::StringRef StdHeader,
22-
llvm::RecordKeeper &Records,
22+
const llvm::RecordKeeper &Records,
2323
const Command::ErrorReporter &Reporter) const {
2424
if (Args.size() != 1) {
2525
Reporter.printFatalError(

libc/utils/HdrGen/IncludeFileCommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class IncludeFileCommand : public Command {
2323
static const char Name[];
2424

2525
void run(llvm::raw_ostream &OS, const ArgVector &Args,
26-
llvm::StringRef StdHeader, llvm::RecordKeeper &Records,
26+
llvm::StringRef StdHeader, const llvm::RecordKeeper &Records,
2727
const Command::ErrorReporter &Reporter) const override;
2828
};
2929

libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ llvm::cl::list<std::string>
2121

2222
} // anonymous namespace
2323

24-
bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) {
24+
bool TestGeneratorMain(llvm::raw_ostream &OS,
25+
const llvm::RecordKeeper &records) {
2526
OS << "#include \"src/__support/CPP/type_traits.h\"\n";
2627
llvm_libc::APIIndexer G(records);
2728
std::unordered_set<std::string> headerFileSet;

libc/utils/HdrGen/PublicAPICommand.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool isAsciiIdentifier(llvm::StringRef S) {
7070
return true;
7171
}
7272

73-
static AttributeStyle getAttributeStyle(llvm::Record *Instance) {
73+
static AttributeStyle getAttributeStyle(const llvm::Record *Instance) {
7474
llvm::StringRef Style = Instance->getValueAsString("Style");
7575
return llvm::StringSwitch<AttributeStyle>(Style)
7676
.Case("cxx11", AttributeStyle::Cxx11)
@@ -79,29 +79,28 @@ static AttributeStyle getAttributeStyle(llvm::Record *Instance) {
7979
.Default(AttributeStyle::Gnu);
8080
}
8181

82-
static AttributeNamespace getAttributeNamespace(llvm::Record *Instance) {
82+
static AttributeNamespace getAttributeNamespace(const llvm::Record *Instance) {
8383
llvm::StringRef Namespace = Instance->getValueAsString("Namespace");
8484
return llvm::StringSwitch<AttributeNamespace>(Namespace)
8585
.Case("clang", AttributeNamespace::Clang)
8686
.Case("gnu", AttributeNamespace::Gnu)
8787
.Default(AttributeNamespace::None);
8888
}
8989

90-
using AttributeMap = llvm::DenseMap<llvm::StringRef, llvm::Record *>;
90+
using AttributeMap = llvm::DenseMap<llvm::StringRef, const llvm::Record *>;
9191

9292
template <class SpecMap, class FuncList>
9393
static AttributeMap collectAttributeMacros(const SpecMap &Spec,
9494
const FuncList &Funcs) {
95-
llvm::DenseMap<llvm::StringRef, llvm::Record *> MacroAttr;
95+
llvm::DenseMap<llvm::StringRef, const llvm::Record *> MacroAttr;
9696
for (const auto &Name : Funcs) {
9797
auto Iter = Spec.find(Name);
9898
if (Iter == Spec.end())
9999
continue;
100100

101-
llvm::Record *FunctionSpec = Iter->second;
102-
std::vector<llvm::Record *> Attributes =
103-
FunctionSpec->getValueAsListOfDefs("Attributes");
104-
for (llvm::Record *Attr : Attributes)
101+
const llvm::Record *FunctionSpec = Iter->second;
102+
for (const llvm::Record *Attr :
103+
FunctionSpec->getValueAsListOfDefs("Attributes"))
105104
MacroAttr[Attr->getValueAsString("Macro")] = Attr;
106105
}
107106
return MacroAttr;
@@ -112,11 +111,11 @@ static void emitAttributeMacroDecls(const AttributeMap &MacroAttr,
112111
for (auto &[Macro, Attr] : MacroAttr) {
113112
std::vector<llvm::Record *> Instances =
114113
Attr->getValueAsListOfDefs("Instances");
115-
llvm::SmallVector<std::pair<AttributeStyle, llvm::Record *>> Styles;
114+
llvm::SmallVector<std::pair<AttributeStyle, const llvm::Record *>> Styles;
116115
std::transform(Instances.begin(), Instances.end(),
117116
std::back_inserter(Styles),
118-
[&](llvm::Record *Instance)
119-
-> std::pair<AttributeStyle, llvm::Record *> {
117+
[&](const llvm::Record *Instance)
118+
-> std::pair<AttributeStyle, const llvm::Record *> {
120119
auto Style = getAttributeStyle(Instance);
121120
return {Style, Instance};
122121
});
@@ -195,7 +194,7 @@ static void emitAttributeMacroForFunction(const llvm::Record *FunctionSpec,
195194
FunctionSpec->getValueAsListOfDefs("Attributes");
196195
llvm::interleave(
197196
Attributes.begin(), Attributes.end(),
198-
[&](llvm::Record *Attr) { OS << Attr->getValueAsString("Macro"); },
197+
[&](const llvm::Record *Attr) { OS << Attr->getValueAsString("Macro"); },
199198
[&]() { OS << ' '; });
200199
if (!Attributes.empty())
201200
OS << ' ';
@@ -217,7 +216,7 @@ static void writeAPIFromIndex(APIIndexer &G,
217216
if (!G.MacroSpecMap.count(Name))
218217
llvm::PrintFatalError(Name + " not found in any standard spec.\n");
219218

220-
llvm::Record *MacroDef = Pair.second;
219+
const llvm::Record *MacroDef = Pair.second;
221220
dedentAndWrite(MacroDef->getValueAsString("Defn"), OS);
222221

223222
OS << '\n';
@@ -237,7 +236,7 @@ static void writeAPIFromIndex(APIIndexer &G,
237236
llvm::PrintFatalError(
238237
Name + " is not listed as an enumeration in any standard spec.\n");
239238

240-
llvm::Record *EnumerationSpec = G.EnumerationSpecMap[Name];
239+
const llvm::Record *EnumerationSpec = G.EnumerationSpecMap[Name];
241240
OS << " " << EnumerationSpec->getValueAsString("Name");
242241
auto Value = EnumerationSpec->getValueAsString("Value");
243242
if (Value == "__default__") {
@@ -267,9 +266,9 @@ static void writeAPIFromIndex(APIIndexer &G,
267266
if (Iter == G.FunctionSpecMap.end())
268267
continue;
269268

270-
llvm::Record *FunctionSpec = Iter->second;
271-
llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
272-
llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
269+
const llvm::Record *FunctionSpec = Iter->second;
270+
const llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
271+
const llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
273272

274273
// TODO: https://github.com/llvm/llvm-project/issues/81208
275274
// Ideally, we should group functions based on their guarding macros.
@@ -285,7 +284,7 @@ static void writeAPIFromIndex(APIIndexer &G,
285284

286285
auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
287286
for (size_t i = 0; i < ArgsList.size(); ++i) {
288-
llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
287+
const llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
289288
OS << G.getTypeAsString(ArgType);
290289
if (i < ArgsList.size() - 1)
291290
OS << ", ";
@@ -304,7 +303,7 @@ static void writeAPIFromIndex(APIIndexer &G,
304303
auto Iter = G.ObjectSpecMap.find(Name);
305304
if (Iter == G.ObjectSpecMap.end())
306305
continue;
307-
llvm::Record *ObjectSpec = Iter->second;
306+
const llvm::Record *ObjectSpec = Iter->second;
308307
auto Type = ObjectSpec->getValueAsString("Type");
309308
OS << "extern " << Type << " " << Name << ";\n";
310309
}
@@ -314,13 +313,13 @@ static void writeAPIFromIndex(APIIndexer &G,
314313
emitUndefsForAttributeMacros(MacroAttr, OS);
315314
}
316315

317-
void writePublicAPI(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {}
316+
void writePublicAPI(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records) {}
318317

319318
const char PublicAPICommand::Name[] = "public_api";
320319

321320
void PublicAPICommand::run(llvm::raw_ostream &OS, const ArgVector &Args,
322321
llvm::StringRef StdHeader,
323-
llvm::RecordKeeper &Records,
322+
const llvm::RecordKeeper &Records,
324323
const Command::ErrorReporter &Reporter) const {
325324
if (Args.size() != 0)
326325
Reporter.printFatalError("public_api command does not take any arguments.");

libc/utils/HdrGen/PublicAPICommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PublicAPICommand : public Command {
3939
: EntrypointNameList(EntrypointNames) {}
4040

4141
void run(llvm::raw_ostream &OS, const ArgVector &Args,
42-
llvm::StringRef StdHeader, llvm::RecordKeeper &Records,
42+
llvm::StringRef StdHeader, const llvm::RecordKeeper &Records,
4343
const Command::ErrorReporter &Reporter) const override;
4444
};
4545

libc/utils/LibcTableGenUtil/APIIndexer.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static const char StructTypeClassName[] = "Struct";
2424
static const char StandardSpecClassName[] = "StandardSpec";
2525
static const char PublicAPIClassName[] = "PublicAPI";
2626

27-
static bool isa(llvm::Record *Def, llvm::Record *TypeClass) {
27+
static bool isa(const llvm::Record *Def, const llvm::Record *TypeClass) {
2828
llvm::RecordRecTy *RecordType = Def->getType();
2929
llvm::ArrayRef<llvm::Record *> Classes = RecordType->getClasses();
3030
// We want exact types. That is, we don't want the classes listed in
@@ -35,35 +35,35 @@ static bool isa(llvm::Record *Def, llvm::Record *TypeClass) {
3535
return Classes[0] == TypeClass;
3636
}
3737

38-
bool APIIndexer::isaNamedType(llvm::Record *Def) {
38+
bool APIIndexer::isaNamedType(const llvm::Record *Def) {
3939
return isa(Def, NamedTypeClass);
4040
}
4141

42-
bool APIIndexer::isaStructType(llvm::Record *Def) {
42+
bool APIIndexer::isaStructType(const llvm::Record *Def) {
4343
return isa(Def, StructClass);
4444
}
4545

46-
bool APIIndexer::isaPtrType(llvm::Record *Def) {
46+
bool APIIndexer::isaPtrType(const llvm::Record *Def) {
4747
return isa(Def, PtrTypeClass);
4848
}
4949

50-
bool APIIndexer::isaConstType(llvm::Record *Def) {
50+
bool APIIndexer::isaConstType(const llvm::Record *Def) {
5151
return isa(Def, ConstTypeClass);
5252
}
5353

54-
bool APIIndexer::isaRestrictedPtrType(llvm::Record *Def) {
54+
bool APIIndexer::isaRestrictedPtrType(const llvm::Record *Def) {
5555
return isa(Def, RestrictedPtrTypeClass);
5656
}
5757

58-
bool APIIndexer::isaStandardSpec(llvm::Record *Def) {
58+
bool APIIndexer::isaStandardSpec(const llvm::Record *Def) {
5959
return isa(Def, StandardSpecClass);
6060
}
6161

62-
bool APIIndexer::isaPublicAPI(llvm::Record *Def) {
62+
bool APIIndexer::isaPublicAPI(const llvm::Record *Def) {
6363
return isa(Def, PublicAPIClass);
6464
}
6565

66-
std::string APIIndexer::getTypeAsString(llvm::Record *TypeRecord) {
66+
std::string APIIndexer::getTypeAsString(const llvm::Record *TypeRecord) {
6767
if (isaNamedType(TypeRecord) || isaStructType(TypeRecord)) {
6868
return std::string(TypeRecord->getValueAsString("Name"));
6969
} else if (isaPtrType(TypeRecord)) {
@@ -79,7 +79,7 @@ std::string APIIndexer::getTypeAsString(llvm::Record *TypeRecord) {
7979
}
8080
}
8181

82-
void APIIndexer::indexStandardSpecDef(llvm::Record *StandardSpec) {
82+
void APIIndexer::indexStandardSpecDef(const llvm::Record *StandardSpec) {
8383
auto HeaderSpecList = StandardSpec->getValueAsListOfDefs("Headers");
8484
for (llvm::Record *HeaderSpec : HeaderSpecList) {
8585
llvm::StringRef Header = HeaderSpec->getValueAsString("Name");
@@ -119,7 +119,7 @@ void APIIndexer::indexStandardSpecDef(llvm::Record *StandardSpec) {
119119
}
120120
}
121121

122-
void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
122+
void APIIndexer::indexPublicAPIDef(const llvm::Record *PublicAPI) {
123123
// While indexing the public API, we do not check if any of the entities
124124
// requested is from an included standard. Such a check is done while
125125
// generating the API.
@@ -148,7 +148,7 @@ void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
148148
Objects.insert(std::string(ObjectName));
149149
}
150150

151-
void APIIndexer::index(llvm::RecordKeeper &Records) {
151+
void APIIndexer::index(const llvm::RecordKeeper &Records) {
152152
NamedTypeClass = Records.getClass(NamedTypeClassName);
153153
PtrTypeClass = Records.getClass(PtrTypeClassName);
154154
RestrictedPtrTypeClass = Records.getClass(RestrictedPtrTypeClassName);
@@ -159,7 +159,7 @@ void APIIndexer::index(llvm::RecordKeeper &Records) {
159159

160160
const auto &DefsMap = Records.getDefs();
161161
for (auto &Pair : DefsMap) {
162-
llvm::Record *Def = Pair.second.get();
162+
const llvm::Record *Def = Pair.second.get();
163163
if (isaStandardSpec(Def))
164164
indexStandardSpecDef(Def);
165165
if (isaPublicAPI(Def)) {

libc/utils/LibcTableGenUtil/APIIndexer.h

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,38 @@ class APIIndexer {
2424
std::optional<llvm::StringRef> StdHeader;
2525

2626
// TableGen classes in spec.td.
27-
llvm::Record *NamedTypeClass;
28-
llvm::Record *PtrTypeClass;
29-
llvm::Record *RestrictedPtrTypeClass;
30-
llvm::Record *ConstTypeClass;
31-
llvm::Record *StructClass;
32-
llvm::Record *StandardSpecClass;
33-
llvm::Record *PublicAPIClass;
34-
35-
bool isaNamedType(llvm::Record *Def);
36-
bool isaStructType(llvm::Record *Def);
37-
bool isaPtrType(llvm::Record *Def);
38-
bool isaConstType(llvm::Record *Def);
39-
bool isaRestrictedPtrType(llvm::Record *Def);
40-
bool isaStandardSpec(llvm::Record *Def);
41-
bool isaPublicAPI(llvm::Record *Def);
42-
43-
void indexStandardSpecDef(llvm::Record *StandardSpec);
44-
void indexPublicAPIDef(llvm::Record *PublicAPI);
45-
void index(llvm::RecordKeeper &Records);
27+
const llvm::Record *NamedTypeClass;
28+
const llvm::Record *PtrTypeClass;
29+
const llvm::Record *RestrictedPtrTypeClass;
30+
const llvm::Record *ConstTypeClass;
31+
const llvm::Record *StructClass;
32+
const llvm::Record *StandardSpecClass;
33+
const llvm::Record *PublicAPIClass;
34+
35+
bool isaNamedType(const llvm::Record *Def);
36+
bool isaStructType(const llvm::Record *Def);
37+
bool isaPtrType(const llvm::Record *Def);
38+
bool isaConstType(const llvm::Record *Def);
39+
bool isaRestrictedPtrType(const llvm::Record *Def);
40+
bool isaStandardSpec(const llvm::Record *Def);
41+
bool isaPublicAPI(const llvm::Record *Def);
42+
43+
void indexStandardSpecDef(const llvm::Record *StandardSpec);
44+
void indexPublicAPIDef(const llvm::Record *PublicAPI);
45+
void index(const llvm::RecordKeeper &Records);
4646

4747
public:
48-
using NameToRecordMapping = std::unordered_map<std::string, llvm::Record *>;
48+
using NameToRecordMapping =
49+
std::unordered_map<std::string, const llvm::Record *>;
4950
using NameSet = std::unordered_set<std::string>;
5051

5152
// This indexes all headers, not just a specified one.
52-
explicit APIIndexer(llvm::RecordKeeper &Records) : StdHeader(std::nullopt) {
53+
explicit APIIndexer(const llvm::RecordKeeper &Records)
54+
: StdHeader(std::nullopt) {
5355
index(Records);
5456
}
5557

56-
APIIndexer(llvm::StringRef Header, llvm::RecordKeeper &Records)
58+
APIIndexer(llvm::StringRef Header, const llvm::RecordKeeper &Records)
5759
: StdHeader(Header) {
5860
index(Records);
5961
}
@@ -76,7 +78,7 @@ class APIIndexer {
7678
NameSet Objects;
7779
NameSet PublicHeaders;
7880

79-
std::string getTypeAsString(llvm::Record *TypeRecord);
81+
std::string getTypeAsString(const llvm::Record *TypeRecord);
8082
};
8183

8284
} // namespace llvm_libc

0 commit comments

Comments
 (0)