Skip to content

Commit 462133e

Browse files
committed
Swift: add more comments
1 parent aa13891 commit 462133e

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

swift/extractor/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cc_binary(
1515
"main.cpp",
1616
],
1717
copts = [
18+
# Required by LLVM/Swift
1819
"-fno-rtti",
1920
],
2021
target_compatible_with = select({

swift/extractor/Configuration.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
namespace codeql {
88
struct Configuration {
9+
// The location for storing TRAP files to be imported by CodeQL engine.
910
std::string trapDir;
11+
// The location for storing extracted source files.
1012
std::string sourceArchiveDir;
11-
std::vector<const char*> frontendOptions;
13+
// The arguments passed to the extractor. Used for debugging.
14+
std::vector<std::string> frontendOptions;
1215
};
1316
} // namespace codeql
1417

swift/extractor/Extractor.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ Extractor::Extractor(const Configuration& config, swift::CompilerInstance& insta
1818
: config{config}, compiler{instance} {}
1919

2020
void Extractor::extract() {
21+
// Swift frontend can be called in several different modes, we are interested
22+
// only in the cases when either a primary or a main source file is present
2123
if (compiler.getPrimarySourceFiles().empty()) {
2224
swift::ModuleDecl* module = compiler.getMainModule();
2325
if (!module->getFiles().empty() &&
2426
module->getFiles().front()->getKind() == swift::FileUnitKind::Source) {
25-
/// We can only call getMainSourceFile if the first file is of a Source kind
27+
// We can only call getMainSourceFile if the first file is of a Source kind
2628
swift::SourceFile& file = module->getMainSourceFile();
2729
extractFile(file);
2830
}
@@ -63,7 +65,10 @@ void Extractor::extractFile(swift::SourceFile& file) {
6365
return;
6466
}
6567

66-
/// TODO: find a more robust approach to avoid collisions?
68+
// The extractor can be called several times from different processes with
69+
// the same input file(s)
70+
// We are using PID to avoid concurrent access
71+
// TODO: find a more robust approach to avoid collisions?
6772
std::string tempTrapName = file.getFilename().str() + '.' + std::to_string(getpid()) + ".trap";
6873
llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir);
6974
llvm::sys::path::append(tempTrapPath, tempTrapName);
@@ -87,12 +92,12 @@ void Extractor::extractFile(swift::SourceFile& file) {
8792
trap << "#0=*\n";
8893
trap << "files(#0, " << std::quoted(srcFilePath.str().str()) << ")\n";
8994

90-
/// TODO: Pick a better name to avoid collisions
95+
// TODO: Pick a better name to avoid collisions
9196
std::string trapName = file.getFilename().str() + ".trap";
9297
llvm::SmallString<PATH_MAX> trapPath(config.trapDir);
9398
llvm::sys::path::append(trapPath, trapName);
9499

95-
/// TODO: The last process wins. Should we do better than that?
100+
// TODO: The last process wins. Should we do better than that?
96101
if (std::error_code ec = llvm::sys::fs::rename(tempTrapPath, trapPath)) {
97102
std::cerr << "Cannot rename temp trap file '" << tempTrapPath.str().str() << "' -> '"
98103
<< trapPath.str().str() << "': " << ec.message() << "\n";

swift/extractor/Extractor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace codeql {
1010

11+
// TODO: add documentation for the class and its public methods
1112
class Extractor {
1213
public:
1314
explicit Extractor(const Configuration& config, swift::CompilerInstance& instance);

swift/extractor/main.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Observer : public swift::FrontendObserver {
2222
const codeql::Configuration& config;
2323
};
2424

25-
static std::string getenv_default(const char* envvar, const std::string& def) {
25+
static std::string getenv_or(const char* envvar, const std::string& def) {
2626
if (const char* var = getenv(envvar)) {
2727
return var;
2828
}
@@ -31,28 +31,28 @@ static std::string getenv_default(const char* envvar, const std::string& def) {
3131

3232
int main(int argc, char** argv) {
3333
if (argc == 1) {
34-
/// TODO: print usage
34+
// TODO: print usage
3535
return 1;
3636
}
37-
/// The frontend can be called in different modes, we are only interested
38-
/// in -frontend mode
39-
/// TODO: filter out at the tracer level
37+
// The frontend can be called in different modes, we are only interested
38+
// in -frontend mode
39+
// TODO: filter out at the tracer level
4040
if ("-frontend"s != argv[1]) {
4141
return 0;
4242
}
4343
PROGRAM_START(argc, argv);
4444

4545
codeql::Configuration configuration{};
46-
configuration.trapDir = getenv_default("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR", ".");
47-
configuration.sourceArchiveDir = getenv_default("CODEQL_EXTRACTOR_SWIFT_SOURCE_ARCHIVE_DIR", ".");
46+
configuration.trapDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR", ".");
47+
configuration.sourceArchiveDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_SOURCE_ARCHIVE_DIR", ".");
4848
std::vector<const char*> args;
4949
for (int i = 1; i < argc; i++) {
5050
if ("-frontend"s == argv[i]) {
5151
continue;
5252
}
5353
args.push_back(argv[i]);
5454
}
55-
configuration.frontendOptions = args;
55+
std::copy(std::begin(args), std::end(args), std::back_inserter(configuration.frontendOptions));
5656
Observer observer(configuration);
5757
int frontend_rc = swift::performFrontend(args, "swift-extractor", (void*)main, &observer);
5858
llvm::llvm_shutdown();

0 commit comments

Comments
 (0)