1414#include " swift/extractor/trap/generated/TrapClasses.h"
1515#include " swift/extractor/trap/TrapDomain.h"
1616#include " swift/extractor/visitors/SwiftVisitor.h"
17- #include " swift/extractor/infra/TargetFile .h"
17+ #include " swift/extractor/TargetTrapFile .h"
1818
1919using namespace codeql ;
2020using namespace std ::string_literals;
@@ -56,8 +56,10 @@ static std::string getFilename(swift::ModuleDecl& module, swift::SourceFile* pri
5656 }
5757 // PCM clang module
5858 if (module .isNonSwiftModule ()) {
59- // Several modules with different name might come from .pcm (clang module) files
59+ // Several modules with different names might come from .pcm (clang module) files
6060 // In this case we want to differentiate them
61+ // Moreover, pcm files may come from caches located in different directories, but are
62+ // unambiguously identified by the base file name, so we can discard the absolute directory
6163 std::string filename = " /pcms/" s + llvm::sys::path::filename (module .getModuleFilename ()).str ();
6264 filename += " -" ;
6365 filename += module .getName ().str ();
@@ -78,20 +80,6 @@ static llvm::SmallVector<swift::Decl*> getTopLevelDecls(swift::ModuleDecl& modul
7880 return ret;
7981}
8082
81- static void dumpArgs (TargetFile& out, const SwiftExtractorConfiguration& config) {
82- out << " /* extractor-args:\n " ;
83- for (const auto & opt : config.frontendOptions ) {
84- out << " " << std::quoted (opt) << " \\\n " ;
85- }
86- out << " \n */\n " ;
87-
88- out << " /* swift-frontend-args:\n " ;
89- for (const auto & opt : config.patchedFrontendOptions ) {
90- out << " " << std::quoted (opt) << " \\\n " ;
91- }
92- out << " \n */\n " ;
93- }
94-
9583static void extractDeclarations (const SwiftExtractorConfiguration& config,
9684 swift::CompilerInstance& compiler,
9785 swift::ModuleDecl& module ,
@@ -101,12 +89,11 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
10189 // The extractor can be called several times from different processes with
10290 // the same input file(s). Using `TargetFile` the first process will win, and the following
10391 // will just skip the work
104- auto trapTarget = TargetFile::create (filename + " .trap " , config. trapDir , config. getTempTrapDir () );
92+ auto trapTarget = createTargetTrapFile ( config, filename );
10593 if (!trapTarget) {
10694 // another process arrived first, nothing to do for us
10795 return ;
10896 }
109- dumpArgs (*trapTarget, config);
11097 TrapDomain trap{*trapTarget};
11198
11299 // TODO: remove this and recreate it with IPA when we have that
@@ -171,21 +158,21 @@ void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
171158 auto modules = collectModules (compiler);
172159
173160 for (auto & module : modules) {
174- // We only extract system and builtin modules here as the other "user" modules can be built
175- // during the build process and then re-used at a later stage. In this case, we extract the
176- // user code twice: once during the module build in a form of a source file, and then as
177- // a pre-built module during building of the dependent source files.
178- if (module ->isSystemModule () || module ->isBuiltinModule ()) {
179- extractDeclarations (config, compiler, *module );
180- } else {
181- for (auto file : module ->getFiles ()) {
182- auto sourceFile = llvm::dyn_cast<swift::SourceFile>(file);
183- if (!sourceFile || inputFiles.count (sourceFile->getFilename ().str ()) == 0 ) {
184- continue ;
185- }
186- archiveFile (config, *sourceFile);
187- extractDeclarations (config, compiler, *module , sourceFile);
161+ bool isFromSourceFile = false ;
162+ for (auto file : module ->getFiles ()) {
163+ auto sourceFile = llvm::dyn_cast<swift::SourceFile>(file);
164+ if (!sourceFile) {
165+ continue ;
166+ }
167+ isFromSourceFile = true ;
168+ if (inputFiles.count (sourceFile->getFilename ().str ()) == 0 ) {
169+ continue ;
188170 }
171+ archiveFile (config, *sourceFile);
172+ extractDeclarations (config, compiler, *module , sourceFile);
173+ }
174+ if (!isFromSourceFile) {
175+ extractDeclarations (config, compiler, *module );
189176 }
190177 }
191178}
0 commit comments