@@ -297,6 +297,48 @@ void CommandLineInterface::handleFormal()
297
297
cout << " Formal version:" << endl << m_compiler->formalTranslation () << endl;
298
298
}
299
299
300
+ void CommandLineInterface::readInputFilesAndConfigureRemappings ()
301
+ {
302
+ if (!m_args.count (" input-file" ))
303
+ {
304
+ string s;
305
+ while (!cin.eof ())
306
+ {
307
+ getline (cin, s);
308
+ m_sourceCodes[g_stdinFileName].append (s + ' \n ' );
309
+ }
310
+ }
311
+ else
312
+ for (string const & infile: m_args[" input-file" ].as <vector<string>>())
313
+ {
314
+ auto eq = find (infile.begin (), infile.end (), ' =' );
315
+ if (eq != infile.end ())
316
+ m_remappings.push_back (make_pair (
317
+ string (infile.begin (), eq),
318
+ string (eq + 1 , infile.end ())
319
+ ));
320
+ else
321
+ {
322
+ auto path = boost::filesystem::path (infile);
323
+ if (!boost::filesystem::exists (path))
324
+ {
325
+ cerr << " Skipping non existant input file \" " << infile << " \" " << endl;
326
+ continue ;
327
+ }
328
+
329
+ if (!boost::filesystem::is_regular_file (path))
330
+ {
331
+ cerr << " \" " << infile << " \" is not a valid file. Skipping" << endl;
332
+ continue ;
333
+ }
334
+
335
+ m_sourceCodes[infile] = dev::contentsString (infile);
336
+ }
337
+ }
338
+ // Add empty remapping to try the path itself.
339
+ m_remappings.push_back (make_pair (string (), string ()));
340
+ }
341
+
300
342
bool CommandLineInterface::parseLibraryOption (string const & _input)
301
343
{
302
344
namespace fs = boost::filesystem;
@@ -457,33 +499,7 @@ Allowed options)",
457
499
458
500
bool CommandLineInterface::processInput ()
459
501
{
460
- if (!m_args.count (" input-file" ))
461
- {
462
- string s;
463
- while (!cin.eof ())
464
- {
465
- getline (cin, s);
466
- m_sourceCodes[g_stdinFileName].append (s + ' \n ' );
467
- }
468
- }
469
- else
470
- for (string const & infile: m_args[" input-file" ].as <vector<string>>())
471
- {
472
- auto path = boost::filesystem::path (infile);
473
- if (!boost::filesystem::exists (path))
474
- {
475
- cerr << " Skipping non existant input file \" " << infile << " \" " << endl;
476
- continue ;
477
- }
478
-
479
- if (!boost::filesystem::is_regular_file (path))
480
- {
481
- cerr << " \" " << infile << " \" is not a valid file. Skipping" << endl;
482
- continue ;
483
- }
484
-
485
- m_sourceCodes[infile] = dev::contentsString (infile);
486
- }
502
+ readInputFilesAndConfigureRemappings ();
487
503
488
504
if (m_args.count (" libraries" ))
489
505
for (string const & library: m_args[" libraries" ].as <vector<string>>())
@@ -499,13 +515,38 @@ bool CommandLineInterface::processInput()
499
515
500
516
function<pair<string,string>(string const &)> fileReader = [this ](string const & _path)
501
517
{
502
- auto path = boost::filesystem::path (_path);
503
- if (!boost::filesystem::exists (path))
518
+ // Try to find the longest prefix match in all remappings. At the end, there will be an
519
+ // empty remapping so that we also try the path itself.
520
+ int errorLevel = 0 ;
521
+ size_t longestPrefix = 0 ;
522
+ string bestMatchPath;
523
+ for (auto const & redir: m_remappings)
524
+ {
525
+ auto const & virt = redir.first ;
526
+ if (longestPrefix > 0 && virt.length () <= longestPrefix)
527
+ continue ;
528
+ if (virt.length () > _path.length () || !std::equal (virt.begin (), virt.end (), _path.begin ()))
529
+ continue ;
530
+ string path = redir.second ;
531
+ path.append (_path.begin () + virt.length (), _path.end ());
532
+ auto boostPath = boost::filesystem::path (path);
533
+ if (!boost::filesystem::exists (boostPath))
534
+ errorLevel = max (errorLevel, 0 );
535
+ else if (!boost::filesystem::is_regular_file (boostPath))
536
+ errorLevel = max (errorLevel, 1 );
537
+ else
538
+ {
539
+ longestPrefix = virt.length ();
540
+ bestMatchPath = path;
541
+ }
542
+ }
543
+ if (!bestMatchPath.empty ())
544
+ return make_pair (m_sourceCodes[bestMatchPath] = dev::contentsString (bestMatchPath), string ());
545
+ if (errorLevel == 0 )
504
546
return make_pair (string (), string (" File not found." ));
505
- else if (!boost::filesystem::is_regular_file (path))
506
- return make_pair (string (), string (" Not a valid file." ));
507
547
else
508
- return make_pair (m_sourceCodes[_path] = dev::contentsString (_path), string ());
548
+ return make_pair (string (), string (" Not a valid file." ));
549
+
509
550
};
510
551
511
552
m_compiler.reset (new CompilerStack (m_args.count (g_argAddStandard) > 0 , fileReader));
0 commit comments