Skip to content

Commit 0c8b594

Browse files
authored
[llvm-dlltool][NFC] Factor out parseModuleDefinition helper. (#81620)
In preparation for ARM64EC support.
1 parent f1b2865 commit 0c8b594

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,46 @@ std::optional<std::string> getPrefix(StringRef Argv0) {
110110
return ProgName.str();
111111
}
112112

113+
bool parseModuleDefinition(StringRef DefFileName, MachineTypes Machine,
114+
bool AddUnderscores,
115+
std::vector<COFFShortExport> &Exports,
116+
std::string &OutputFile) {
117+
std::unique_ptr<MemoryBuffer> MB = openFile(DefFileName);
118+
if (!MB)
119+
return false;
120+
121+
if (!MB->getBufferSize()) {
122+
llvm::errs() << "definition file empty\n";
123+
return false;
124+
}
125+
126+
Expected<COFFModuleDefinition> Def = parseCOFFModuleDefinition(
127+
*MB, Machine, /*MingwDef=*/true, AddUnderscores);
128+
if (!Def) {
129+
llvm::errs() << "error parsing definition\n"
130+
<< errorToErrorCode(Def.takeError()).message() << "\n";
131+
return false;
132+
}
133+
134+
if (OutputFile.empty())
135+
OutputFile = std::move(Def->OutputFile);
136+
137+
// If ExtName is set (if the "ExtName = Name" syntax was used), overwrite
138+
// Name with ExtName and clear ExtName. When only creating an import
139+
// library and not linking, the internal name is irrelevant. This avoids
140+
// cases where writeImportLibrary tries to transplant decoration from
141+
// symbol decoration onto ExtName.
142+
for (COFFShortExport &E : Def->Exports) {
143+
if (!E.ExtName.empty()) {
144+
E.Name = E.ExtName;
145+
E.ExtName.clear();
146+
}
147+
}
148+
149+
Exports = std::move(Def->Exports);
150+
return true;
151+
}
152+
113153
} // namespace
114154

115155
int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
@@ -141,16 +181,6 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
141181
return 1;
142182
}
143183

144-
std::unique_ptr<MemoryBuffer> MB =
145-
openFile(Args.getLastArg(OPT_d)->getValue());
146-
if (!MB)
147-
return 1;
148-
149-
if (!MB->getBufferSize()) {
150-
llvm::errs() << "definition file empty\n";
151-
return 1;
152-
}
153-
154184
COFF::MachineTypes Machine = getDefaultMachine();
155185
if (std::optional<std::string> Prefix = getPrefix(ArgsArr[0])) {
156186
Triple T(*Prefix);
@@ -166,40 +196,23 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
166196
}
167197

168198
bool AddUnderscores = !Args.hasArg(OPT_no_leading_underscore);
169-
Expected<COFFModuleDefinition> Def = parseCOFFModuleDefinition(
170-
*MB, Machine, /*MingwDef=*/true, AddUnderscores);
171199

172-
if (!Def) {
173-
llvm::errs() << "error parsing definition\n"
174-
<< errorToErrorCode(Def.takeError()).message() << "\n";
175-
return 1;
176-
}
177-
178-
// Do this after the parser because parseCOFFModuleDefinition sets OutputFile.
200+
std::string OutputFile;
179201
if (auto *Arg = Args.getLastArg(OPT_D))
180-
Def->OutputFile = Arg->getValue();
202+
OutputFile = Arg->getValue();
181203

182-
if (Def->OutputFile.empty()) {
183-
llvm::errs() << "no DLL name specified\n";
204+
std::vector<COFFShortExport> Exports;
205+
if (!parseModuleDefinition(Args.getLastArg(OPT_d)->getValue(), Machine,
206+
AddUnderscores, Exports, OutputFile))
184207
return 1;
185-
}
186208

187-
std::string Path = std::string(Args.getLastArgValue(OPT_l));
188-
189-
// If ExtName is set (if the "ExtName = Name" syntax was used), overwrite
190-
// Name with ExtName and clear ExtName. When only creating an import
191-
// library and not linking, the internal name is irrelevant. This avoids
192-
// cases where writeImportLibrary tries to transplant decoration from
193-
// symbol decoration onto ExtName.
194-
for (COFFShortExport& E : Def->Exports) {
195-
if (!E.ExtName.empty()) {
196-
E.Name = E.ExtName;
197-
E.ExtName.clear();
198-
}
209+
if (OutputFile.empty()) {
210+
llvm::errs() << "no DLL name specified\n";
211+
return 1;
199212
}
200213

201214
if (Machine == IMAGE_FILE_MACHINE_I386 && Args.hasArg(OPT_k)) {
202-
for (COFFShortExport& E : Def->Exports) {
215+
for (COFFShortExport &E : Exports) {
203216
if (!E.AliasTarget.empty() || (!E.Name.empty() && E.Name[0] == '?'))
204217
continue;
205218
E.SymbolName = E.Name;
@@ -215,8 +228,9 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
215228
}
216229
}
217230

218-
if (!Path.empty() && writeImportLibrary(Def->OutputFile, Path, Def->Exports,
219-
Machine, /*MinGW=*/true))
231+
std::string Path = std::string(Args.getLastArgValue(OPT_l));
232+
if (!Path.empty() && writeImportLibrary(OutputFile, Path, Exports, Machine,
233+
/*MinGW=*/true))
220234
return 1;
221235
return 0;
222236
}

0 commit comments

Comments
 (0)