1818
1919using namespace codeql ;
2020
21- static void extractFile (const SwiftExtractorConfiguration& config,
22- swift::CompilerInstance& compiler,
23- swift::SourceFile& file) {
21+ static void archiveFile (const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
2422 if (std::error_code ec = llvm::sys::fs::create_directories (config.trapDir )) {
2523 std::cerr << " Cannot create TRAP directory: " << ec.message () << " \n " ;
2624 return ;
@@ -49,7 +47,11 @@ static void extractFile(const SwiftExtractorConfiguration& config,
4947 << dstFilePath.str ().str () << " ': " << ec.message () << " \n " ;
5048 return ;
5149 }
50+ }
5251
52+ static void extractFile (const SwiftExtractorConfiguration& config,
53+ swift::CompilerInstance& compiler,
54+ swift::SourceFile& file) {
5355 // The extractor can be called several times from different processes with
5456 // the same input file(s)
5557 // We are using PID to avoid concurrent access
@@ -84,6 +86,9 @@ static void extractFile(const SwiftExtractorConfiguration& config,
8486
8587 // In the case of emtpy files, the dispatcher is not called, but we still want to 'record' the
8688 // fact that the file was extracted
89+ // TODO: to be moved elsewhere
90+ llvm::SmallString<PATH_MAX> srcFilePath (file.getFilename ());
91+ llvm::sys::fs::make_absolute (srcFilePath);
8792 auto fileLabel = arena.allocateLabel <FileTag>();
8893 trap.assignKey (fileLabel, srcFilePath.str ().str ());
8994 trap.emit (FilesTrap{fileLabel, srcFilePath.str ().str ()});
@@ -107,19 +112,10 @@ static void extractFile(const SwiftExtractorConfiguration& config,
107112
108113void codeql::extractSwiftFiles (const SwiftExtractorConfiguration& config,
109114 swift::CompilerInstance& compiler) {
110- // Swift frontend can be called in several different modes, we are interested
111- // only in the cases when either a primary or a main source file is present
112- if (compiler.getPrimarySourceFiles ().empty ()) {
113- swift::ModuleDecl* module = compiler.getMainModule ();
114- if (!module ->getFiles ().empty () &&
115- module ->getFiles ().front ()->getKind () == swift::FileUnitKind::Source) {
116- // We can only call getMainSourceFile if the first file is of a Source kind
117- swift::SourceFile& file = module ->getMainSourceFile ();
118- extractFile (config, compiler, file);
119- }
120- } else {
121- for (auto s : compiler.getPrimarySourceFiles ()) {
122- extractFile (config, compiler, *s);
123- }
115+ // The extraction will only work if one (or more) `-primary-file` CLI option is provided, which
116+ // is what always happen in case of `swift build` and `xcodebuild`
117+ for (auto s : compiler.getPrimarySourceFiles ()) {
118+ archiveFile (config, *s);
119+ extractFile (config, compiler, *s);
124120 }
125121}
0 commit comments