Skip to content

Commit 488befb

Browse files
committed
Swift: store TRAP files in a temporary folder until the extraction is complete
Currently, we have a number of assertions in the codebase and certain assumptions about the AST. These don't always hold, sometimes leading to a crash in the extractor. The crashes leave incomplete TRAP files that cannot be imported into the database. With this change, we still get those incomplete TRAP files, but we also get a database in the end (even thoough it is also incomplete as we cannot import everything).
1 parent 6c68872 commit 488befb

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
6262
// TODO: find a more robust approach to avoid collisions?
6363
llvm::StringRef filename = primaryFile ? primaryFile->getFilename() : module.getModuleFilename();
6464
std::string tempTrapName = filename.str() + '.' + std::to_string(getpid()) + ".trap";
65-
llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir);
65+
llvm::SmallString<PATH_MAX> tempTrapPath(config.tempTrapDir);
6666
llvm::sys::path::append(tempTrapPath, tempTrapName);
6767

68-
llvm::StringRef trapParent = llvm::sys::path::parent_path(tempTrapPath);
69-
if (std::error_code ec = llvm::sys::fs::create_directories(trapParent)) {
70-
std::cerr << "Cannot create trap directory '" << trapParent.str() << "': " << ec.message()
71-
<< "\n";
68+
llvm::StringRef tempTrapParent = llvm::sys::path::parent_path(tempTrapPath);
69+
if (std::error_code ec = llvm::sys::fs::create_directories(tempTrapParent)) {
70+
std::cerr << "Cannot create temp trap directory '" << tempTrapParent.str()
71+
<< "': " << ec.message() << "\n";
7272
return;
7373
}
7474

@@ -117,6 +117,13 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
117117
llvm::SmallString<PATH_MAX> trapPath(config.trapDir);
118118
llvm::sys::path::append(trapPath, trapName);
119119

120+
llvm::StringRef trapParent = llvm::sys::path::parent_path(trapPath);
121+
if (std::error_code ec = llvm::sys::fs::create_directories(trapParent)) {
122+
std::cerr << "Cannot create trap directory '" << trapParent.str() << "': " << ec.message()
123+
<< "\n";
124+
return;
125+
}
126+
120127
// TODO: The last process wins. Should we do better than that?
121128
if (std::error_code ec = llvm::sys::fs::rename(tempTrapPath, trapPath)) {
122129
std::cerr << "Cannot rename temp trap file '" << tempTrapPath.str().str() << "' -> '"

swift/extractor/SwiftExtractorConfiguration.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ struct SwiftExtractorConfiguration {
99
std::string trapDir;
1010
// The location for storing extracted source files.
1111
std::string sourceArchiveDir;
12-
// The arguments passed to the extractor. Used for debugging.
12+
// A temporary directory that exists during database creation, but is deleted once the DB is
13+
// finalized.
14+
std::string scratchDir;
15+
// A temporary directory that contains TRAP files before they moved into the final destination.
16+
// Subdirectory of the scratchDir.
17+
std::string tempTrapDir;
18+
19+
// The original arguments passed to the extractor. Used for debugging.
1320
std::vector<std::string> frontendOptions;
1421
};
1522
} // namespace codeql

swift/extractor/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ int main(int argc, char** argv) {
5151
codeql::SwiftExtractorConfiguration configuration{};
5252
configuration.trapDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR", ".");
5353
configuration.sourceArchiveDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_SOURCE_ARCHIVE_DIR", ".");
54+
configuration.scratchDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_SCRATCH_DIR", ".");
55+
56+
configuration.tempTrapDir = configuration.scratchDir + "/swift-trap-temp";
57+
5458
std::vector<const char*> args;
5559
for (int i = 1; i < argc; i++) {
5660
args.push_back(argv[i]);

0 commit comments

Comments
 (0)