1
- mod extractor;
2
- mod trap;
3
-
4
- extern crate num_cpus;
5
-
6
1
use rayon:: prelude:: * ;
7
2
use std:: fs;
8
3
use std:: io:: BufRead ;
9
4
use std:: path:: { Path , PathBuf } ;
10
5
11
- /**
12
- * Gets the number of threads the extractor should use, by reading the
13
- * CODEQL_THREADS environment variable and using it as described in the
14
- * extractor spec:
15
- *
16
- * "If the number is positive, it indicates the number of threads that should
17
- * be used. If the number is negative or zero, it should be added to the number
18
- * of cores available on the machine to determine how many threads to use
19
- * (minimum of 1). If unspecified, should be considered as set to -1."
20
- */
21
- fn num_codeql_threads ( ) -> usize {
22
- let threads_str = std:: env:: var ( "CODEQL_THREADS" ) . unwrap_or_else ( |_| "-1" . to_owned ( ) ) ;
23
- match threads_str. parse :: < i32 > ( ) {
24
- Ok ( num) if num <= 0 => {
25
- let reduction = -num as usize ;
26
- std:: cmp:: max ( 1 , num_cpus:: get ( ) - reduction)
27
- }
28
- Ok ( num) => num as usize ,
29
-
30
- Err ( _) => {
31
- tracing:: error!(
32
- "Unable to parse CODEQL_THREADS value '{}'; defaulting to 1 thread." ,
33
- & threads_str
34
- ) ;
35
- 1
36
- }
37
- }
38
- }
6
+ use codeql_extractor:: { diagnostics, extractor, node_types, trap} ;
39
7
40
8
fn main ( ) -> std:: io:: Result < ( ) > {
41
9
tracing_subscriber:: fmt ( )
@@ -45,7 +13,23 @@ fn main() -> std::io::Result<()> {
45
13
. with_env_filter ( tracing_subscriber:: EnvFilter :: from_default_env ( ) )
46
14
. init ( ) ;
47
15
48
- let num_threads = num_codeql_threads ( ) ;
16
+ let diagnostics = diagnostics:: DiagnosticLoggers :: new ( "ql" ) ;
17
+ let mut main_thread_logger = diagnostics. logger ( ) ;
18
+ let num_threads = match codeql_extractor:: options:: num_threads ( ) {
19
+ Ok ( num) => num,
20
+ Err ( e) => {
21
+ main_thread_logger. write (
22
+ main_thread_logger
23
+ . new_entry ( "configuration-error" , "Configuration error" )
24
+ . message (
25
+ "{}; defaulting to 1 thread." ,
26
+ & [ diagnostics:: MessageArg :: Code ( & e) ] ,
27
+ )
28
+ . severity ( diagnostics:: Severity :: Warning ) ,
29
+ ) ;
30
+ 1
31
+ }
32
+ } ;
49
33
tracing:: info!(
50
34
"Using {} {}" ,
51
35
num_threads,
@@ -55,6 +39,20 @@ fn main() -> std::io::Result<()> {
55
39
"threads"
56
40
}
57
41
) ;
42
+ let trap_compression = match trap:: Compression :: from_env ( "CODEQL_QL_TRAP_COMPRESSION" ) {
43
+ Ok ( x) => x,
44
+ Err ( e) => {
45
+ main_thread_logger. write (
46
+ main_thread_logger
47
+ . new_entry ( "configuration-error" , "Configuration error" )
48
+ . message ( "{}; using gzip." , & [ diagnostics:: MessageArg :: Code ( & e) ] )
49
+ . severity ( diagnostics:: Severity :: Warning ) ,
50
+ ) ;
51
+ trap:: Compression :: Gzip
52
+ }
53
+ } ;
54
+ drop ( main_thread_logger) ;
55
+
58
56
rayon:: ThreadPoolBuilder :: new ( )
59
57
. num_threads ( num_threads)
60
58
. build_global ( )
@@ -79,7 +77,6 @@ fn main() -> std::io::Result<()> {
79
77
. value_of ( "output-dir" )
80
78
. expect ( "missing --output-dir" ) ;
81
79
let trap_dir = PathBuf :: from ( trap_dir) ;
82
- let trap_compression = trap:: Compression :: from_env ( "CODEQL_QL_TRAP_COMPRESSION" ) ;
83
80
84
81
let file_list = matches. value_of ( "file-list" ) . expect ( "missing --file-list" ) ;
85
82
let file_list = fs:: File :: open ( file_list) ?;
@@ -119,26 +116,29 @@ fn main() -> std::io::Result<()> {
119
116
let source = std:: fs:: read ( & path) ?;
120
117
let code_ranges = vec ! [ ] ;
121
118
let mut trap_writer = trap:: Writer :: new ( ) ;
119
+ let mut diagnostics_writer = diagnostics. logger ( ) ;
122
120
if line. ends_with ( ".dbscheme" ) {
123
121
extractor:: extract (
124
122
dbscheme,
125
123
"dbscheme" ,
126
124
& dbscheme_schema,
125
+ & mut diagnostics_writer,
127
126
& mut trap_writer,
128
127
& path,
129
128
& source,
130
129
& code_ranges,
131
- ) ?
130
+ )
132
131
} else if line. ends_with ( "qlpack.yml" ) {
133
132
extractor:: extract (
134
133
yaml,
135
134
"yaml" ,
136
135
& yaml_schema,
136
+ & mut diagnostics_writer,
137
137
& mut trap_writer,
138
138
& path,
139
139
& source,
140
140
& code_ranges,
141
- ) ?
141
+ )
142
142
} else if line. ends_with ( ".json" )
143
143
|| line. ends_with ( ".jsonl" )
144
144
|| line. ends_with ( ".jsonc" )
@@ -147,31 +147,34 @@ fn main() -> std::io::Result<()> {
147
147
json,
148
148
"json" ,
149
149
& json_schema,
150
+ & mut diagnostics_writer,
150
151
& mut trap_writer,
151
152
& path,
152
153
& source,
153
154
& code_ranges,
154
- ) ?
155
+ )
155
156
} else if line. ends_with ( ".blame" ) {
156
157
extractor:: extract (
157
158
blame,
158
159
"blame" ,
159
160
& blame_schema,
161
+ & mut diagnostics_writer,
160
162
& mut trap_writer,
161
163
& path,
162
164
& source,
163
165
& code_ranges,
164
- ) ?
166
+ )
165
167
} else {
166
168
extractor:: extract (
167
169
language,
168
170
"ql" ,
169
171
& schema,
172
+ & mut diagnostics_writer,
170
173
& mut trap_writer,
171
174
& path,
172
175
& source,
173
176
& code_ranges,
174
- ) ?
177
+ )
175
178
}
176
179
std:: fs:: create_dir_all ( & src_archive_file. parent ( ) . unwrap ( ) ) ?;
177
180
std:: fs:: copy ( & path, & src_archive_file) ?;
0 commit comments