Skip to content

Commit 46c7ee0

Browse files
committed
Swift: refactor RUN_UNDER code
1 parent 80debe1 commit 46c7ee0

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

swift/extractor/main.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ static void lockOutputSwiftModuleTraps(const codeql::SwiftExtractorConfiguration
5353
}
5454
}
5555

56+
static bool checkRunUnderFilter(int argc, char* const* argv) {
57+
auto runUnderFilter = getenv("CODEQL_EXTRACTOR_SWIFT_RUN_UNDER_FILTER");
58+
if (runUnderFilter == nullptr) {
59+
return true;
60+
}
61+
std::string call = argv[0];
62+
for (auto i = 1; i < argc; ++i) {
63+
call += ' ';
64+
call += argv[i];
65+
}
66+
std::regex filter{runUnderFilter, std::regex_constants::basic | std::regex_constants::nosubs};
67+
return std::regex_search(call, filter);
68+
}
69+
5670
// if `CODEQL_EXTRACTOR_SWIFT_RUN_UNDER` env variable is set, and either
5771
// * `CODEQL_EXTRACTOR_SWIFT_RUN_UNDER_FILTER` is not set, or
5872
// * it is set to a regexp matching any substring of the extractor call
@@ -62,41 +76,28 @@ static void lockOutputSwiftModuleTraps(const codeql::SwiftExtractorConfiguration
6276
// unpleasant loops.
6377
// An example usage is to run the extractor under `gdbserver :1234` when the
6478
// arguments match a given source file.
65-
void checkToRunUnderTool(int argc, char* const* argv) {
79+
static void checkWhetherToRunUnderTool(int argc, char* const* argv) {
80+
assert(argc > 0);
81+
6682
auto runUnder = getenv("CODEQL_EXTRACTOR_SWIFT_RUN_UNDER");
67-
if (runUnder == nullptr) {
83+
if (runUnder == nullptr || !checkRunUnderFilter(argc, argv)) {
6884
return;
6985
}
70-
auto runUnderFilter = getenv("CODEQL_EXTRACTOR_SWIFT_RUN_UNDER_FILTER");
71-
if (runUnderFilter != nullptr) {
72-
assert(argc > 0);
73-
std::string call = argv[0];
74-
for (auto i = 1; i < argc; ++i) {
75-
call += ' ';
76-
call += argv[i];
77-
}
78-
std::regex filter{runUnderFilter, std::regex_constants::basic | std::regex_constants::nosubs};
79-
if (!std::regex_search(call, filter)) {
80-
return;
81-
}
82-
}
8386
std::vector<char*> args;
87+
// split RUN_UNDER value by spaces to get args vector
8488
for (auto word = std::strtok(runUnder, " "); word != nullptr; word = std::strtok(nullptr, " ")) {
8589
args.push_back(word);
8690
}
87-
if (args.empty()) {
88-
return;
89-
}
90-
for (auto i = 0; i < argc; ++i) {
91-
args.push_back(argv[i]);
92-
}
91+
// append process args, including extractor executable path
92+
args.insert(args.end(), argv, argv + argc);
9393
args.push_back(nullptr);
94+
// avoid looping on this function
9495
unsetenv("CODEQL_EXTRACTOR_SWIFT_RUN_UNDER");
9596
execvp(args[0], args.data());
9697
}
9798

9899
int main(int argc, char** argv) {
99-
checkToRunUnderTool(argc, argv);
100+
checkWhetherToRunUnderTool(argc, argv);
100101

101102
if (argc == 1) {
102103
// TODO: print usage

0 commit comments

Comments
 (0)