@@ -112,32 +112,46 @@ static void debugFailWithCrash() {
112
112
LLVM_BUILTIN_TRAP;
113
113
}
114
114
115
- static unsigned readFileList (std::vector<std::string> &inputFiles,
116
- const llvm::opt::Arg *filelistPath,
117
- const llvm::opt::Arg *primaryFileArg = nullptr ) {
118
- bool foundPrimaryFile = false ;
119
- unsigned primaryFileIndex = 0 ;
120
- StringRef primaryFile;
121
- if (primaryFileArg)
122
- primaryFile = primaryFileArg->getValue ();
115
+ // / Try to read a file list file.
116
+ // /
117
+ // / Returns false on error.
118
+ static bool readFileList (DiagnosticEngine &diags,
119
+ std::vector<std::string> &inputFiles,
120
+ const llvm::opt::Arg *filelistPath,
121
+ const llvm::opt::Arg *primaryFileArg = nullptr ,
122
+ unsigned *primaryFileIndex = nullptr ) {
123
+ assert ((primaryFileArg == nullptr ) || (primaryFileIndex != nullptr ) &&
124
+ " did not provide argument for primary file index" );
123
125
124
126
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
125
127
llvm::MemoryBuffer::getFile (filelistPath->getValue ());
126
- assert (buffer && " can't read filelist; unrecoverable" );
128
+ if (!buffer) {
129
+ diags.diagnose (SourceLoc (), diag::cannot_open_file,
130
+ filelistPath->getValue (), buffer.getError ().message ());
131
+ return false ;
132
+ }
133
+
134
+ bool foundPrimaryFile = false ;
135
+ if (primaryFileIndex) *primaryFileIndex = 0 ;
127
136
128
137
for (StringRef line : make_range (llvm::line_iterator (*buffer.get ()), {})) {
129
138
inputFiles.push_back (line);
130
- if (foundPrimaryFile)
139
+
140
+ if (foundPrimaryFile || primaryFileArg == nullptr )
131
141
continue ;
132
- if (line == primaryFile )
142
+ if (line == primaryFileArg-> getValue () )
133
143
foundPrimaryFile = true ;
134
144
else
135
- ++primaryFileIndex;
145
+ ++* primaryFileIndex;
136
146
}
137
147
138
- if (primaryFileArg)
139
- assert (foundPrimaryFile && " primary file not found in filelist" );
140
- return primaryFileIndex;
148
+ if (primaryFileArg && !foundPrimaryFile) {
149
+ diags.diagnose (SourceLoc (), diag::error_primary_file_not_found,
150
+ primaryFileArg->getValue (), filelistPath->getValue ());
151
+ return false ;
152
+ }
153
+
154
+ return true ;
141
155
}
142
156
143
157
static bool ParseFrontendArgs (FrontendOptions &Opts, ArgList &Args,
@@ -199,11 +213,13 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
199
213
200
214
if (const Arg *A = Args.getLastArg (OPT_filelist)) {
201
215
const Arg *primaryFileArg = Args.getLastArg (OPT_primary_file);
202
- auto primaryFileIndex = readFileList (Opts.InputFilenames , A,
203
- primaryFileArg);
204
- if (primaryFileArg)
205
- Opts.PrimaryInput = SelectedInput (primaryFileIndex);
206
- assert (!Args.hasArg (OPT_INPUT) && " mixing -filelist with inputs" );
216
+ unsigned primaryFileIndex = 0 ;
217
+ if (readFileList (Diags, Opts.InputFilenames , A,
218
+ primaryFileArg, &primaryFileIndex)) {
219
+ if (primaryFileArg)
220
+ Opts.PrimaryInput = SelectedInput (primaryFileIndex);
221
+ assert (!Args.hasArg (OPT_INPUT) && " mixing -filelist with inputs" );
222
+ }
207
223
} else {
208
224
for (const Arg *A : make_range (Args.filtered_begin (OPT_INPUT,
209
225
OPT_primary_file),
@@ -355,7 +371,7 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
355
371
Opts.InputKind = InputFileKind::IFK_Swift;
356
372
357
373
if (const Arg *A = Args.getLastArg (OPT_output_filelist)) {
358
- readFileList (Opts.OutputFilenames , A);
374
+ readFileList (Diags, Opts.OutputFilenames , A);
359
375
assert (!Args.hasArg (OPT_o) && " don't use -o with -output-filelist" );
360
376
} else {
361
377
Opts.OutputFilenames = Args.getAllArgValues (OPT_o);
0 commit comments