18
18
19
19
using namespace codeql ;
20
20
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) {
24
22
if (std::error_code ec = llvm::sys::fs::create_directories (config.trapDir )) {
25
23
std::cerr << " Cannot create TRAP directory: " << ec.message () << " \n " ;
26
24
return ;
@@ -31,7 +29,7 @@ static void extractFile(const SwiftExtractorConfiguration& config,
31
29
return ;
32
30
}
33
31
34
- llvm::SmallString<PATH_MAX> srcFilePath (file. getFilename ());
32
+ llvm::SmallString<PATH_MAX> srcFilePath (file-> getFilename ());
35
33
llvm::sys::fs::make_absolute (srcFilePath);
36
34
37
35
llvm::SmallString<PATH_MAX> dstFilePath (config.sourceArchiveDir );
@@ -49,12 +47,16 @@ static void extractFile(const SwiftExtractorConfiguration& config,
49
47
<< dstFilePath.str ().str () << " ': " << ec.message () << " \n " ;
50
48
return ;
51
49
}
50
+ }
52
51
52
+ static void extractFile (const SwiftExtractorConfiguration& config,
53
+ swift::CompilerInstance& compiler,
54
+ swift::SourceFile* file) {
53
55
// The extractor can be called several times from different processes with
54
56
// the same input file(s)
55
57
// We are using PID to avoid concurrent access
56
58
// TODO: find a more robust approach to avoid collisions?
57
- std::string tempTrapName = file. getFilename ().str () + ' .' + std::to_string (getpid ()) + " .trap" ;
59
+ std::string tempTrapName = file-> getFilename ().str () + ' .' + std::to_string (getpid ()) + " .trap" ;
58
60
llvm::SmallString<PATH_MAX> tempTrapPath (config.trapDir );
59
61
llvm::sys::path::append (tempTrapPath, tempTrapName);
60
62
@@ -84,17 +86,20 @@ static void extractFile(const SwiftExtractorConfiguration& config,
84
86
85
87
// In the case of emtpy files, the dispatcher is not called, but we still want to 'record' the
86
88
// 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);
87
92
auto fileLabel = arena.allocateLabel <FileTag>();
88
93
trap.assignKey (fileLabel, srcFilePath.str ().str ());
89
94
trap.emit (FilesTrap{fileLabel, srcFilePath.str ().str ()});
90
95
91
96
SwiftVisitor visitor (compiler.getSourceMgr (), arena, trap);
92
- for (swift::Decl* decl : file. getTopLevelDecls ()) {
97
+ for (swift::Decl* decl : file-> getTopLevelDecls ()) {
93
98
visitor.extract (decl);
94
99
}
95
100
96
101
// TODO: Pick a better name to avoid collisions
97
- std::string trapName = file. getFilename ().str () + " .trap" ;
102
+ std::string trapName = file-> getFilename ().str () + " .trap" ;
98
103
llvm::SmallString<PATH_MAX> trapPath (config.trapDir );
99
104
llvm::sys::path::append (trapPath, trapName);
100
105
@@ -107,19 +112,8 @@ static void extractFile(const SwiftExtractorConfiguration& config,
107
112
108
113
void codeql::extractSwiftFiles (const SwiftExtractorConfiguration& config,
109
114
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
+ for (auto s : compiler.getPrimarySourceFiles ()) {
116
+ archiveFile (config, s);
117
+ extractFile (config, compiler, s);
124
118
}
125
119
}
0 commit comments