Skip to content

Commit 5674086

Browse files
authored
Merge pull request #41465 from bitjammer/acgarland/rdar-88595293-frontend-forward-repl-invok
[Frontend] Forward intent to launch REPL to new driver
2 parents ac70bf7 + 8b91658 commit 5674086

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

lib/DriverTool/driver.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ extern int swift_api_extract_main(ArrayRef<const char *> Args,
9797
/// \returns True if running as a subcommand.
9898
static bool shouldRunAsSubcommand(StringRef ExecName,
9999
SmallString<256> &SubcommandName,
100-
const ArrayRef<const char *> Args,
101-
bool &isRepl) {
100+
const ArrayRef<const char *> Args) {
102101
assert(!Args.empty());
103102

104103
// If we are not run as 'swift', don't do anything special. This doesn't work
@@ -125,7 +124,6 @@ static bool shouldRunAsSubcommand(StringRef ExecName,
125124
// If the subcommand is the "built-in" 'repl', then use the
126125
// normal driver.
127126
if (Subcommand == "repl") {
128-
isRepl = true;
129127
return false;
130128
}
131129

@@ -184,15 +182,18 @@ static bool appendSwiftDriverName(SmallString<256> &buffer) {
184182
}
185183

186184
static int run_driver(StringRef ExecName,
187-
const ArrayRef<const char *> argv,
185+
ArrayRef<const char *> argv,
188186
const ArrayRef<const char *> originalArgv) {
189187
// This is done here and not done in FrontendTool.cpp, because
190188
// FrontendTool.cpp is linked to tools, which don't use swift modules.
191189
initializeSwiftModules();
192190

191+
bool isRepl = false;
192+
193193
// Handle integrated tools.
194194
if (argv.size() > 1) {
195195
StringRef FirstArg(argv[1]);
196+
196197
if (FirstArg == "-frontend") {
197198
return performFrontend(llvm::makeArrayRef(argv.data()+2,
198199
argv.data()+argv.size()),
@@ -212,6 +213,11 @@ static int run_driver(StringRef ExecName,
212213
argv.data()+argv.size()),
213214
argv[0], (void *)(intptr_t)getExecutablePath);
214215
}
216+
217+
if (FirstArg == "repl") {
218+
isRepl = true;
219+
argv = argv.drop_front();
220+
}
215221
}
216222

217223
std::string Path = getExecutablePath(argv[0]);
@@ -245,8 +251,14 @@ static int run_driver(StringRef ExecName,
245251
subCommandArgs.push_back(NewDriverPath.c_str());
246252

247253
// Push on the source program arguments
248-
subCommandArgs.insert(subCommandArgs.end(),
249-
originalArgv.begin() + 1, originalArgv.end());
254+
if (isRepl) {
255+
subCommandArgs.push_back("-repl");
256+
subCommandArgs.insert(subCommandArgs.end(),
257+
originalArgv.begin() + 2, originalArgv.end());
258+
} else {
259+
subCommandArgs.insert(subCommandArgs.end(),
260+
originalArgv.begin() + 1, originalArgv.end());
261+
}
250262

251263
// Execute the subcommand.
252264
subCommandArgs.push_back(nullptr);
@@ -361,8 +373,7 @@ int swift::mainEntry(int argc_, const char **argv_) {
361373
// Check if this invocation should execute a subcommand.
362374
StringRef ExecName = llvm::sys::path::stem(argv[0]);
363375
SmallString<256> SubcommandName;
364-
bool isRepl = false;
365-
if (shouldRunAsSubcommand(ExecName, SubcommandName, argv, isRepl)) {
376+
if (shouldRunAsSubcommand(ExecName, SubcommandName, argv)) {
366377
// Preserve argv for the stack trace.
367378
SmallVector<const char *, 256> subCommandArgs(argv.begin(), argv.end());
368379
subCommandArgs.erase(&subCommandArgs[1]);
@@ -396,12 +407,5 @@ int swift::mainEntry(int argc_, const char **argv_) {
396407
}
397408

398409
ArrayRef<const char *> originalArgv(argv_, &argv_[argc_]);
399-
if (isRepl) {
400-
// Preserve argv for the stack trace.
401-
SmallVector<const char *, 256> replArgs(argv.begin(), argv.end());
402-
replArgs.erase(&replArgs[1]);
403-
return run_driver(ExecName, replArgs, originalArgv);
404-
} else {
405-
return run_driver(ExecName, argv, originalArgv);
406-
}
410+
return run_driver(ExecName, argv, originalArgv);
407411
}

0 commit comments

Comments
 (0)