1515
1616using namespace codeql ;
1717using namespace std ::string_literals;
18+ namespace fs = std::filesystem;
1819
19- static void archiveFile (const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
20- if (std::error_code ec = llvm::sys::fs::create_directories (config.trapDir )) {
21- std::cerr << " Cannot create TRAP directory: " << ec.message () << " \n " ;
22- return ;
23- }
20+ static fs::path toPath (llvm::StringRef s) {
21+ return {static_cast <std::string_view>(s)};
22+ }
2423
25- if (std::error_code ec = llvm::sys::fs::create_directories (config.sourceArchiveDir )) {
26- std::cerr << " Cannot create source archive directory: " << ec.message () << " \n " ;
27- return ;
24+ static void ensureDirectory (const char * label, const fs::path& dir) {
25+ std::error_code ec;
26+ fs::create_directories (dir, ec);
27+ if (ec) {
28+ std::cerr << " Cannot create " << label << " directory: " << ec.message () << " \n " ;
29+ std::abort ();
2830 }
31+ }
2932
30- llvm::SmallString<PATH_MAX> srcFilePath (file.getFilename ());
31- llvm::sys::fs::make_absolute (srcFilePath);
33+ static void archiveFile (const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
34+ ensureDirectory (" TRAP" , config.trapDir );
35+ ensureDirectory (" source archive" , config.sourceArchiveDir );
3236
33- llvm::SmallString<PATH_MAX> dstFilePath (config.sourceArchiveDir );
34- llvm::sys::path::append (dstFilePath, srcFilePath);
37+ fs::path srcFilePath = fs::absolute (toPath (file.getFilename ()));
38+ auto dstFilePath = config.sourceArchiveDir ;
39+ dstFilePath += srcFilePath;
3540
36- llvm::StringRef parent = llvm::sys::path::parent_path (dstFilePath);
37- if (std::error_code ec = llvm::sys::fs::create_directories (parent)) {
38- std::cerr << " Cannot create source archive destination directory '" << parent.str ()
39- << " ': " << ec.message () << " \n " ;
40- return ;
41- }
41+ ensureDirectory (" source archive destination" , dstFilePath.parent_path ());
42+
43+ std::error_code ec;
44+ fs::copy (srcFilePath, dstFilePath, fs::copy_options::overwrite_existing, ec);
4245
43- if (std::error_code ec = llvm::sys::fs::copy_file (srcFilePath, dstFilePath) ) {
44- std::cerr << " Cannot archive source file ' " << srcFilePath. str (). str () << " ' -> ' "
45- << dstFilePath. str (). str () << " ': " << ec.message () << " \n " ;
46- return ;
46+ if (ec ) {
47+ std::cerr << " Cannot archive source file " << srcFilePath << " -> " << dstFilePath << " : "
48+ << ec.message () << " \n " ;
49+ std::abort () ;
4750 }
4851}
4952
50- static std::string getFilename (swift::ModuleDecl& module , swift::SourceFile* primaryFile) {
53+ static fs::path getFilename (swift::ModuleDecl& module , swift::SourceFile* primaryFile) {
5154 if (primaryFile) {
5255 return primaryFile->getFilename ().str ();
5356 }
@@ -57,7 +60,8 @@ static std::string getFilename(swift::ModuleDecl& module, swift::SourceFile* pri
5760 // In this case we want to differentiate them
5861 // Moreover, pcm files may come from caches located in different directories, but are
5962 // unambiguously identified by the base file name, so we can discard the absolute directory
60- std::string filename = " /pcms/" s + llvm::sys::path::filename (module .getModuleFilename ()).str ();
63+ fs::path filename = " /pcms" ;
64+ filename /= toPath (module .getModuleFilename ()).filename ();
6165 filename += " -" ;
6266 filename += module .getName ().str ();
6367 return filename;
@@ -66,7 +70,7 @@ static std::string getFilename(swift::ModuleDecl& module, swift::SourceFile* pri
6670 // The Builtin module has an empty filename, let's fix that
6771 return " /__Builtin__" ;
6872 }
69- auto filename = module .getModuleFilename (). str ( );
73+ auto filename = toPath ( module .getModuleFilename ());
7074 // there is a special case of a module without an actual filename reporting `<imports>`: in this
7175 // case we want to avoid the `<>` characters, in case a dirty DB is imported on Windows
7276 if (filename == " <imports>" ) {
0 commit comments