1- mod extractor;
2- mod trap;
3-
4- extern crate num_cpus;
5-
61use rayon:: prelude:: * ;
72use std:: fs;
83use std:: io:: BufRead ;
94use std:: path:: { Path , PathBuf } ;
105
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} ;
397
408fn main ( ) -> std:: io:: Result < ( ) > {
419 tracing_subscriber:: fmt ( )
@@ -45,7 +13,23 @@ fn main() -> std::io::Result<()> {
4513 . with_env_filter ( tracing_subscriber:: EnvFilter :: from_default_env ( ) )
4614 . init ( ) ;
4715
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+ } ;
4933 tracing:: info!(
5034 "Using {} {}" ,
5135 num_threads,
@@ -55,6 +39,20 @@ fn main() -> std::io::Result<()> {
5539 "threads"
5640 }
5741 ) ;
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+
5856 rayon:: ThreadPoolBuilder :: new ( )
5957 . num_threads ( num_threads)
6058 . build_global ( )
@@ -79,7 +77,6 @@ fn main() -> std::io::Result<()> {
7977 . value_of ( "output-dir" )
8078 . expect ( "missing --output-dir" ) ;
8179 let trap_dir = PathBuf :: from ( trap_dir) ;
82- let trap_compression = trap:: Compression :: from_env ( "CODEQL_QL_TRAP_COMPRESSION" ) ;
8380
8481 let file_list = matches. value_of ( "file-list" ) . expect ( "missing --file-list" ) ;
8582 let file_list = fs:: File :: open ( file_list) ?;
@@ -119,26 +116,29 @@ fn main() -> std::io::Result<()> {
119116 let source = std:: fs:: read ( & path) ?;
120117 let code_ranges = vec ! [ ] ;
121118 let mut trap_writer = trap:: Writer :: new ( ) ;
119+ let mut diagnostics_writer = diagnostics. logger ( ) ;
122120 if line. ends_with ( ".dbscheme" ) {
123121 extractor:: extract (
124122 dbscheme,
125123 "dbscheme" ,
126124 & dbscheme_schema,
125+ & mut diagnostics_writer,
127126 & mut trap_writer,
128127 & path,
129128 & source,
130129 & code_ranges,
131- ) ?
130+ )
132131 } else if line. ends_with ( "qlpack.yml" ) {
133132 extractor:: extract (
134133 yaml,
135134 "yaml" ,
136135 & yaml_schema,
136+ & mut diagnostics_writer,
137137 & mut trap_writer,
138138 & path,
139139 & source,
140140 & code_ranges,
141- ) ?
141+ )
142142 } else if line. ends_with ( ".json" )
143143 || line. ends_with ( ".jsonl" )
144144 || line. ends_with ( ".jsonc" )
@@ -147,31 +147,34 @@ fn main() -> std::io::Result<()> {
147147 json,
148148 "json" ,
149149 & json_schema,
150+ & mut diagnostics_writer,
150151 & mut trap_writer,
151152 & path,
152153 & source,
153154 & code_ranges,
154- ) ?
155+ )
155156 } else if line. ends_with ( ".blame" ) {
156157 extractor:: extract (
157158 blame,
158159 "blame" ,
159160 & blame_schema,
161+ & mut diagnostics_writer,
160162 & mut trap_writer,
161163 & path,
162164 & source,
163165 & code_ranges,
164- ) ?
166+ )
165167 } else {
166168 extractor:: extract (
167169 language,
168170 "ql" ,
169171 & schema,
172+ & mut diagnostics_writer,
170173 & mut trap_writer,
171174 & path,
172175 & source,
173176 & code_ranges,
174- ) ?
177+ )
175178 }
176179 std:: fs:: create_dir_all ( & src_archive_file. parent ( ) . unwrap ( ) ) ?;
177180 std:: fs:: copy ( & path, & src_archive_file) ?;
0 commit comments