@@ -53,6 +53,20 @@ static void lockOutputSwiftModuleTraps(const codeql::SwiftExtractorConfiguration
53
53
}
54
54
}
55
55
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
+
56
70
// if `CODEQL_EXTRACTOR_SWIFT_RUN_UNDER` env variable is set, and either
57
71
// * `CODEQL_EXTRACTOR_SWIFT_RUN_UNDER_FILTER` is not set, or
58
72
// * it is set to a regexp matching any substring of the extractor call
@@ -62,41 +76,28 @@ static void lockOutputSwiftModuleTraps(const codeql::SwiftExtractorConfiguration
62
76
// unpleasant loops.
63
77
// An example usage is to run the extractor under `gdbserver :1234` when the
64
78
// 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
+
66
82
auto runUnder = getenv (" CODEQL_EXTRACTOR_SWIFT_RUN_UNDER" );
67
- if (runUnder == nullptr ) {
83
+ if (runUnder == nullptr || ! checkRunUnderFilter (argc, argv) ) {
68
84
return ;
69
85
}
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
- }
83
86
std::vector<char *> args;
87
+ // split RUN_UNDER value by spaces to get args vector
84
88
for (auto word = std::strtok (runUnder, " " ); word != nullptr ; word = std::strtok (nullptr , " " )) {
85
89
args.push_back (word);
86
90
}
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);
93
93
args.push_back (nullptr );
94
+ // avoid looping on this function
94
95
unsetenv (" CODEQL_EXTRACTOR_SWIFT_RUN_UNDER" );
95
96
execvp (args[0 ], args.data ());
96
97
}
97
98
98
99
int main (int argc, char ** argv) {
99
- checkToRunUnderTool (argc, argv);
100
+ checkWhetherToRunUnderTool (argc, argv);
100
101
101
102
if (argc == 1 ) {
102
103
// TODO: print usage
0 commit comments