@@ -18,11 +18,13 @@ Extractor::Extractor(const Configuration& config, swift::CompilerInstance& insta
1818 : config{config}, compiler{instance} {}
1919
2020void Extractor::extract () {
21+ // Swift frontend can be called in several different modes, we are interested
22+ // only in the cases when either a primary or a main source file is present
2123 if (compiler.getPrimarySourceFiles ().empty ()) {
2224 swift::ModuleDecl* module = compiler.getMainModule ();
2325 if (!module ->getFiles ().empty () &&
2426 module ->getFiles ().front ()->getKind () == swift::FileUnitKind::Source) {
25- // / We can only call getMainSourceFile if the first file is of a Source kind
27+ // We can only call getMainSourceFile if the first file is of a Source kind
2628 swift::SourceFile& file = module ->getMainSourceFile ();
2729 extractFile (file);
2830 }
@@ -63,7 +65,10 @@ void Extractor::extractFile(swift::SourceFile& file) {
6365 return ;
6466 }
6567
66- // / TODO: find a more robust approach to avoid collisions?
68+ // The extractor can be called several times from different processes with
69+ // the same input file(s)
70+ // We are using PID to avoid concurrent access
71+ // TODO: find a more robust approach to avoid collisions?
6772 std::string tempTrapName = file.getFilename ().str () + ' .' + std::to_string (getpid ()) + " .trap" ;
6873 llvm::SmallString<PATH_MAX> tempTrapPath (config.trapDir );
6974 llvm::sys::path::append (tempTrapPath, tempTrapName);
@@ -87,12 +92,12 @@ void Extractor::extractFile(swift::SourceFile& file) {
8792 trap << " #0=*\n " ;
8893 trap << " files(#0, " << std::quoted (srcFilePath.str ().str ()) << " )\n " ;
8994
90- // / TODO: Pick a better name to avoid collisions
95+ // TODO: Pick a better name to avoid collisions
9196 std::string trapName = file.getFilename ().str () + " .trap" ;
9297 llvm::SmallString<PATH_MAX> trapPath (config.trapDir );
9398 llvm::sys::path::append (trapPath, trapName);
9499
95- // / TODO: The last process wins. Should we do better than that?
100+ // TODO: The last process wins. Should we do better than that?
96101 if (std::error_code ec = llvm::sys::fs::rename (tempTrapPath, trapPath)) {
97102 std::cerr << " Cannot rename temp trap file '" << tempTrapPath.str ().str () << " ' -> '"
98103 << trapPath.str ().str () << " ': " << ec.message () << " \n " ;
0 commit comments