blob: 04c582a34eb6d0d856ea07bb187cc0fb0bdfe836 [file] [log] [blame]
Yegor Timoshenkoc2e49412018-10-07 01:58:27 +00001#!/usr/bin/env perl
Martin Rothbcaaad12015-10-18 11:16:25 -06002#
Patrick Georgi7333a112020-05-08 20:48:04 +02003# SPDX-License-Identifier: GPL-2.0-only
4
Martin Roth1b44f7e2016-01-11 14:31:38 -07005# perltidy -l=123
Martin Rothbcaaad12015-10-18 11:16:25 -06006
7package kconfig_lint;
8
9use strict;
10use warnings;
11use English qw( -no_match_vars );
12use File::Find;
13use Getopt::Long;
14use Getopt::Std;
15
Martin Rothabf7d4d2016-02-19 10:24:25 -070016# If taint mode is enabled, Untaint the path - git and grep must be in /bin, /usr/bin or /usr/local/bin
17if ( ${^TAINT} ) {
18 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
19 delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' };
20}
21
Martin Roth1b44f7e2016-01-11 14:31:38 -070022my $suppress_error_output = 0; # flag to prevent error text
23my $suppress_warning_output = 0; # flag to prevent warning text
24my $show_note_output = 0; # flag to show minor notes text
25my $print_full_output = 0; # flag to print wholeconfig output
26my $output_file = "-"; # filename of output - set stdout by default
Martin Rothd8080172015-11-26 19:12:44 -070027my $dont_use_git_grep = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -060028
Martin Rothabf7d4d2016-02-19 10:24:25 -070029# Globals
Martin Roth1b44f7e2016-01-11 14:31:38 -070030my $top_dir = "."; # Directory where Kconfig is run
31my $root_dir = "src"; # Directory of the top level Kconfig file
32my $errors_found = 0; # count of errors
Martin Rothd8080172015-11-26 19:12:44 -070033my $warnings_found = 0;
Martin Roth63ea4932016-01-25 16:08:27 -070034my $exclude_dirs_and_files =
Martin Rothab273d32016-11-23 17:28:00 -070035 '^build/\|^coreboot-builds/\|^configs/\|^util/\|^\.git/\|^payloads\|^Documentation\|^3rdparty'
Martin Roth63ea4932016-01-25 16:08:27 -070036 . '\|' . # directories to exclude when searching for used symbols
Julius Wernerf0286042019-04-09 15:57:07 -070037 '\.config\|\.txt$\|\.tex$\|\.tags\|/kconfig.h'; #files to exclude when looking for symbols
Martin Rothe69c58d2016-11-15 21:08:42 -070038my $payload_files_to_check='payloads/Makefile.inc payloads/external/Makefile.inc';
Martin Roth08cf90f2016-01-25 19:54:16 -070039my $config_file = ""; # name of config file to load symbol values from.
40my @wholeconfig; # document the entire kconfig structure
41my %loaded_files; # list of each Kconfig file loaded
42my %symbols; # main structure of all symbols declared
43my %referenced_symbols; # list of symbols referenced by expressions or select statements
44my %used_symbols; # structure of symbols used in the tree, and where they're found
45my @collected_symbols; #
46my %selected_symbols; # list of symbols that are enabled by a select statement
Martin Rothbcaaad12015-10-18 11:16:25 -060047
Martin Roth6bfbf1c2016-01-25 16:12:49 -070048my $exclude_unused = '_SPECIFIC_OPTIONS|SOUTH_BRIDGE_OPTIONS';
49
Martin Rothbcaaad12015-10-18 11:16:25 -060050Main();
51
52#-------------------------------------------------------------------------------
53# Main
54#
55# Start by loading and parsing the top level Kconfig, this pulls in the other
56# files. Parsing the tree creates several arrays and hashes that can be used
57# to check for errors
58#-------------------------------------------------------------------------------
59sub Main {
60
61 check_arguments();
62 open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
63
Martin Roth1b44f7e2016-01-11 14:31:38 -070064 if ( defined $top_dir ) {
65 chdir $top_dir or die "Error: can't cd to $top_dir\n";
Martin Rothbcaaad12015-10-18 11:16:25 -060066 }
67
Martin Roth1b44f7e2016-01-11 14:31:38 -070068 die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir );
Martin Rothbcaaad12015-10-18 11:16:25 -060069
70 #load the Kconfig tree, checking what we can and building up all the hash tables
71 build_and_parse_kconfig_tree("$root_dir/Kconfig");
72
Martin Rothbcaaad12015-10-18 11:16:25 -060073 load_config($config_file) if ($config_file);
74
Martin Roth08705f12016-11-09 14:27:00 -070075 check_type();
Martin Rothbcaaad12015-10-18 11:16:25 -060076 check_defaults();
77 check_referenced_symbols();
78
79 collect_used_symbols();
80 check_used_symbols();
81 check_for_ifdef();
82 check_for_def();
Nico Huberec017592019-04-06 16:09:46 +020083 check_config_macro();
Martin Rothb7c39b22016-01-14 09:04:53 -070084 check_selected_symbols();
Martin Rothbcaaad12015-10-18 11:16:25 -060085
Martin Rothabf7d4d2016-02-19 10:24:25 -070086 # Run checks based on the data that was found
87 if ( ( !$suppress_warning_output ) && ( ${^TAINT} == 0 ) ) {
88
89 # The find function is tainted - only run it if taint checking
90 # is disabled and warnings are enabled.
91 find( \&check_if_file_referenced, $root_dir );
92 }
93
Martin Rothbcaaad12015-10-18 11:16:25 -060094 print_wholeconfig();
95
Martin Rothd8080172015-11-26 19:12:44 -070096 if ($errors_found) {
97 print "# $errors_found errors";
Martin Roth1b44f7e2016-01-11 14:31:38 -070098 if ($warnings_found) {
Martin Rothd8080172015-11-26 19:12:44 -070099 print ", $warnings_found warnings";
100 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700101 print "\n";
Martin Rothd8080172015-11-26 19:12:44 -0700102 }
103
Martin Roth1b44f7e2016-01-11 14:31:38 -0700104 exit( $errors_found + $warnings_found );
Martin Rothd8080172015-11-26 19:12:44 -0700105}
106
107#-------------------------------------------------------------------------------
108# Print and count errors
109#-------------------------------------------------------------------------------
110sub show_error {
111 my ($error_msg) = @_;
112 unless ($suppress_error_output) {
113 print "#!!!!! Error: $error_msg\n";
114 $errors_found++;
115 }
116}
117
118#-------------------------------------------------------------------------------
119# Print and count warnings
120#-------------------------------------------------------------------------------
121sub show_warning {
122 my ($warning_msg) = @_;
123 unless ($suppress_warning_output) {
124 print "#!!!!! Warning: $warning_msg\n";
125 $warnings_found++;
126 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600127}
128
129#-------------------------------------------------------------------------------
Martin Rothb7c39b22016-01-14 09:04:53 -0700130# check selected symbols for validity
131# they must be bools
132# they cannot select symbols created in 'choice' blocks
133#-------------------------------------------------------------------------------
134sub check_selected_symbols {
135
136 #loop through symbols found in expressions and used by 'select' keywords
137 foreach my $symbol ( sort ( keys %selected_symbols ) ) {
138 my $type_failure = 0;
139 my $choice_failure = 0;
140
141 #errors selecting symbols that don't exist are already printed, so we
142 #don't need to print them again here
143
144 #make sure the selected symbols are bools
145 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "bool" ) ) {
146 $type_failure = 1;
147 }
148
149 #make sure we're not selecting choice symbols
150 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{choice} ) ) {
151 $choice_failure = 1;
152 }
153
154 #loop through each instance of the symbol to print out all of the failures
155 for ( my $i = 0 ; $i <= $referenced_symbols{$symbol}{count} ; $i++ ) {
156 next if ( !exists $selected_symbols{$symbol}{$i} );
157 my $file = $referenced_symbols{$symbol}{$i}{filename};
158 my $lineno = $referenced_symbols{$symbol}{$i}{line_no};
159 if ($type_failure) {
160 show_error(
161 "CONFIG_$symbol' selected at $file:$lineno." . " Selects only work on symbols of type bool." );
162 }
163 if ($choice_failure) {
164 show_error(
165 "'CONFIG_$symbol' selected at $file:$lineno." . " Symbols created in a choice cannot be selected." );
166 }
167 }
168 }
169}
170
171#-------------------------------------------------------------------------------
Martin Rothbcaaad12015-10-18 11:16:25 -0600172# check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and
173# #if defined(CONFIG_[symbol_name]).
174#
175# #ifdef symbol is valid for strings, but bool, hex, and INT are always defined.
176# #if defined(symbol) && symbol is also a valid construct.
177#-------------------------------------------------------------------------------
178sub check_for_ifdef {
179 my @ifdef_symbols = @collected_symbols;
180
181 #look for #ifdef SYMBOL
182 while ( my $line = shift @ifdef_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800183 if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) {
Martin Roth5b883012017-06-25 20:27:36 -0600184 my $file = $1;
185 my $lineno = $2;
186 my $symbol = $3;
187
188 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800189 show_error( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
Martin Roth5b883012017-06-25 20:27:36 -0600190 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
191 }
Julius Wernere5eb2de2019-03-05 17:10:19 -0800192 } elsif ( $line =~ /^([^:]+):(\d+):.+defined\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700193 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600194 my $lineno = $2;
195 my $symbol = $3;
196
Martin Roth1b44f7e2016-01-11 14:31:38 -0700197 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800198 show_error( "defined(CONFIG_$symbol) used at $file:$lineno."
Martin Roth1b44f7e2016-01-11 14:31:38 -0700199 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600200 }
201 }
202 }
203}
204
205#-------------------------------------------------------------------------------
206# check_for_def - Look for instances of #define CONFIG_[symbol_name]
207#
208# Symbols should not be redefined outside of Kconfig, and #defines should not
209# look like symbols
210#-------------------------------------------------------------------------------
211sub check_for_def {
212 my @def_symbols = @collected_symbols;
213
214 #look for #ifdef SYMBOL
215 while ( my $line = shift @def_symbols ) {
216 if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700217 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600218 my $lineno = $2;
219 my $symbol = $3;
220
Martin Roth1b44f7e2016-01-11 14:31:38 -0700221 if ( ( exists $symbols{$symbol} ) ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800222 show_error("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
Martin Roth1b44f7e2016-01-11 14:31:38 -0700223 }
224 else {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800225 show_error( "#define 'CONFIG_$symbol' used at $file:$lineno."
Martin Roth1b44f7e2016-01-11 14:31:38 -0700226 . " Other #defines should not look like Kconfig symbols." );
227 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600228 }
229 }
230}
231
232#-------------------------------------------------------------------------------
Martin Roth08705f12016-11-09 14:27:00 -0700233# check_type - Make sure that all symbols have a type defined.
234#
235# Conflicting types are found when parsing the Kconfig tree.
236#-------------------------------------------------------------------------------
237sub check_type {
238
239 # loop through each defined symbol
240 foreach my $sym ( sort ( keys %symbols ) ) {
241
242 # Make sure there's a type set for the symbol
243 if (!defined $symbols{$sym}{type}) {
244
245 #loop through each instance of that symbol
246 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
247
248 my $filename = $symbols{$sym}{$sym_num}{file};
249 my $line_no = $symbols{$sym}{$sym_num}{line_no};
250
251 show_error("No type defined for symbol $sym defined at $filename:$line_no.");
252 }
253 }
254 }
255}
256
257#-------------------------------------------------------------------------------
Nico Huberec017592019-04-06 16:09:46 +0200258# check_config_macro - The CONFIG() macro is only valid for symbols of type
259# bool. It would probably work on type hex or int if the value was 0 or 1,
260# but this seems like a bad plan. Using it on strings is dead out.
261#
262# The IS_ENABLED() macro is forbidden in coreboot now. Though, as long as
263# we keep its definition in libpayload for compatibility, we have to check
264# that it doesn't sneak back in.
Martin Rothbcaaad12015-10-18 11:16:25 -0600265#-------------------------------------------------------------------------------
Nico Huberec017592019-04-06 16:09:46 +0200266sub check_config_macro {
Martin Rothb6acc302015-11-27 18:51:19 -0700267 my @is_enabled_symbols = @collected_symbols;
Martin Rothbcaaad12015-10-18 11:16:25 -0600268
269 #sort through symbols found by grep and store them in a hash for easy access
270 while ( my $line = shift @is_enabled_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800271 if ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG\(.*)/ ) {
272 my $file = $1;
273 my $lineno = $2;
274 $line = $3;
275 while ( $line =~ /(.*)\bCONFIG\(([^)]*)\)(.*)/ ) {
276 my $symbol = $2;
277 $line = $1 . $3;
278
279 #make sure that the type is bool
280 if ( exists $symbols{$symbol} ) {
281 if ( $symbols{$symbol}{type} ne "bool" ) {
282 show_error( "CONFIG($symbol) used at $file:$lineno."
283 . " CONFIG() is only valid for type 'bool', not '$symbols{$symbol}{type}'." );
284 }
285 }
286 else {
Julius Wernerf0286042019-04-09 15:57:07 -0700287 show_error("CONFIG() used on unknown value ($symbol) at $file:$lineno.");
Julius Werneref7a3262019-03-05 16:57:52 -0800288 }
289 }
290 } elsif ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700291 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600292 my $lineno = $2;
Martin Rothb6acc302015-11-27 18:51:19 -0700293 $line = $3;
Martin Roth572a8562016-01-25 16:14:09 -0700294 if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) {
Nico Huberec017592019-04-06 16:09:46 +0200295 show_error("# uninterpreted IS_ENABLED at $file:$lineno: $line");
Martin Rothb6acc302015-11-27 18:51:19 -0700296 next;
Martin Rothbcaaad12015-10-18 11:16:25 -0600297 }
Martin Rothb6acc302015-11-27 18:51:19 -0700298 while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
299 my $symbol = $2;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700300 $line = $1 . $3;
Nico Huberec017592019-04-06 16:09:46 +0200301 show_error("IS_ENABLED(CONFIG_$symbol) at $file:$lineno is deprecated. Use CONFIG($symbol) instead.");
Martin Rothb6acc302015-11-27 18:51:19 -0700302 }
Martin Roth5b883012017-06-25 20:27:36 -0600303 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?.*?$/ ) {
304 my $file = $1;
305 my $lineno = $2;
306 my $symbol = $3;
Nico Huberec017592019-04-06 16:09:46 +0200307 # If the type is bool, give a warning that CONFIG() should be used
Martin Roth5b883012017-06-25 20:27:36 -0600308 if ( exists $symbols{$symbol} ) {
309 if ( $symbols{$symbol}{type} eq "bool" ) {
310 show_error( "#if CONFIG_$symbol used at $file:$lineno."
Julius Werneref7a3262019-03-05 16:57:52 -0800311 . " CONFIG($symbol) should be used for type 'bool'" );
Martin Roth5b883012017-06-25 20:27:36 -0600312 }
313 }
Julius Werneref7a3262019-03-05 16:57:52 -0800314 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if.*(?:&&|\|\|)\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?$/ ) {
Martin Roth5b883012017-06-25 20:27:36 -0600315 my $file = $1;
316 my $lineno = $2;
317 my $symbol = $3;
Nico Huberec017592019-04-06 16:09:46 +0200318 # If the type is bool, give a warning that CONFIG() should be used
Martin Roth5b883012017-06-25 20:27:36 -0600319 if ( exists $symbols{$symbol} ) {
320 if ( $symbols{$symbol}{type} eq "bool" ) {
321 show_error( "#if CONFIG_$symbol used at $file:$lineno."
Julius Werneref7a3262019-03-05 16:57:52 -0800322 . " CONFIG($symbol) should be used for type 'bool'" );
Martin Roth5b883012017-06-25 20:27:36 -0600323 }
324 }
Julius Wernere5eb2de2019-03-05 17:10:19 -0800325 } elsif ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG_.+)/ ) {
326 my $file = $1;
327 my $lineno = $2;
328 $line = $3;
329 if ( $file =~ /.*\.(c|h|asl|ld)/ ) {
330 while ( $line =~ /(.*)\bCONFIG_(\w+)(.*)/ && $1 !~ /\/\/|\/\*/ ) {
331 my $symbol = $2;
332 $line = $1 . $3;
333 if ( exists $symbols{$symbol} ) {
334 if ( $symbols{$symbol}{type} eq "bool" ) {
Martin Rotha0209032020-07-24 12:42:59 -0600335 show_error( "Naked reference to CONFIG_$symbol used at $file:$lineno."
Julius Wernere5eb2de2019-03-05 17:10:19 -0800336 . " A 'bool' Kconfig should always be accessed through CONFIG($symbol)." );
337 }
338 } else {
339 show_warning( "Unknown config option CONFIG_$symbol used at $file:$lineno." );
340 }
341 }
342 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600343 }
344 }
345}
346
347#-------------------------------------------------------------------------------
348# check_defaults - Look for defaults that come after a default with no
349# dependencies.
350#
351# TODO - check for defaults with the same dependencies
352#-------------------------------------------------------------------------------
353sub check_defaults {
354
355 # loop through each defined symbol
356 foreach my $sym ( sort ( keys %symbols ) ) {
357 my $default_set = 0;
358 my $default_filename = "";
359 my $default_line_no = "";
360
361 #loop through each instance of that symbol
362 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
363
364 #loop through any defaults for that instance of that symbol, if there are any
365 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
366 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
367
Martin Rothfa956252016-09-30 15:51:32 -0600368 my $filename = $symbols{$sym}{$sym_num}{file};
369 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
370
Martin Roth08705f12016-11-09 14:27:00 -0700371 # Make sure there's a type set for the symbol
372 next if (!defined $symbols{$sym}{type});
373
Martin Rothfa956252016-09-30 15:51:32 -0600374 # skip good defaults
375 if (! ((($symbols{$sym}{type} eq "hex") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^0x/)) ||
376 (($symbols{$sym}{type} eq "int") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[-0-9]+$/)) ||
377 (($symbols{$sym}{type} eq "string") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^".*"$/)) ||
378 (($symbols{$sym}{type} eq "bool") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[yn]$/)))
379 ) {
380
381 my ($checksym) = $symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /(\w+)/;
382
383 if (! exists $symbols{$checksym}) {
384
385 # verify the symbol type against the default value
386 if ($symbols{$sym}{type} eq "hex") {
387 show_error("non hex default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for hex symbol $sym at $filename:$line_no.");
388 } elsif ($symbols{$sym}{type} eq "int") {
389 show_error("non int default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for int symbol $sym at $filename:$line_no.");
390 } elsif ($symbols{$sym}{type} eq "string") {
391 # TODO: Remove special MAINBOARD_DIR check
392 if ($sym ne "MAINBOARD_DIR") {
393 show_error("no quotes around default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for string symbol $sym at $filename:$line_no.");
394 }
395 } elsif ($symbols{$sym}{type} eq "bool") {
396 if ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /[01YN]/) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800397 show_error("default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) for bool symbol $sym uses value other than y/n at $filename:$line_no.");
Martin Rothfa956252016-09-30 15:51:32 -0600398 } else {
399 show_error("non bool default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for bool symbol $sym at $filename:$line_no.");
400 }
401 }
402 }
403 }
404
Martin Rothbcaaad12015-10-18 11:16:25 -0600405 #if a default is already set, display an error
406 if ($default_set) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800407 show_error( "Default for '$sym' referenced at $filename:$line_no will never be set"
Martin Roth1b44f7e2016-01-11 14:31:38 -0700408 . " - overridden by default set at $default_filename:$default_line_no" );
Martin Rothbcaaad12015-10-18 11:16:25 -0600409 }
410 else {
411 #if no default is set, see if this is a default with no dependencies
412 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
413 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
414 {
415 $default_set = 1;
416 $default_filename = $symbols{$sym}{$sym_num}{file};
417 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
418 }
419 }
420 }
421 }
422 }
423}
424
425#-------------------------------------------------------------------------------
426# check_referenced_symbols - Make sure the symbols referenced by expressions and
427# select statements are actually valid symbols.
428#-------------------------------------------------------------------------------
429sub check_referenced_symbols {
430
431 #loop through symbols found in expressions and used by 'select' keywords
432 foreach my $key ( sort ( keys %referenced_symbols ) ) {
433
434 #make sure the symbol was defined by a 'config' or 'choice' keyword
435 next if ( exists $symbols{$key} );
436
Martin Rothd8080172015-11-26 19:12:44 -0700437 #loop through each instance of the symbol to print out all of the invalid references
438 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
439 my $filename = $referenced_symbols{$key}{$i}{filename};
440 my $line_no = $referenced_symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700441 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600442 }
443 }
444}
445
446#-------------------------------------------------------------------------------
447#-------------------------------------------------------------------------------
448sub collect_used_symbols {
449 # find all references to CONFIG_ statements in the tree
450
451 if ($dont_use_git_grep) {
Julius Werneref7a3262019-03-05 16:57:52 -0800452 @collected_symbols = `grep -Irn -- "CONFIG\\(_\\|(\\)" | grep -v '$exclude_dirs_and_files'; grep -In -- "CONFIG\\(_\\|(\\)" $payload_files_to_check`;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700453 }
454 else {
Julius Werneref7a3262019-03-05 16:57:52 -0800455 @collected_symbols = `git grep -In -- "CONFIG\\(_\\|(\\)" | grep -v '$exclude_dirs_and_files'; git grep -In -- "CONFIG\\(_\\|(\\)" $payload_files_to_check`;
Martin Rothbcaaad12015-10-18 11:16:25 -0600456 }
457
458 my @used_symbols = @collected_symbols;
459
460 #sort through symbols found by grep and store them in a hash for easy access
461 while ( my $line = shift @used_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800462 while ( $line =~ /[^A-Za-z0-9_]CONFIG(?:_|\()([A-Za-z0-9_]+)/g ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700463 my $symbol = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600464 my $filename = "";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700465 if ( $line =~ /^([^:]+):/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600466 $filename = $1;
467 }
468
Martin Roth1b44f7e2016-01-11 14:31:38 -0700469 if ( exists $used_symbols{$symbol}{count} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600470 $used_symbols{$symbol}{count}++;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700471 }
472 else {
473 $used_symbols{$symbol}{count} = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -0600474 }
475 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700476 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600477 }
478}
479
480#-------------------------------------------------------------------------------
481# check_used_symbols - Checks to see whether or not the created symbols are
482# actually used.
483#-------------------------------------------------------------------------------
484sub check_used_symbols {
485 # loop through all defined symbols and see if they're used anywhere
486 foreach my $key ( sort ( keys %symbols ) ) {
487
Martin Roth6bfbf1c2016-01-25 16:12:49 -0700488 if ( $key =~ /$exclude_unused/ ) {
489 next;
490 }
491
Martin Rothbcaaad12015-10-18 11:16:25 -0600492 #see if they're used internal to Kconfig
493 next if ( exists $referenced_symbols{$key} );
494
495 #see if they're used externally
496 next if exists $used_symbols{$key};
497
498 #loop through the definitions to print out all the places the symbol is defined.
499 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
500 my $filename = $symbols{$key}{$i}{file};
501 my $line_no = $symbols{$key}{$i}{line_no};
Martin Rotha7d00272016-10-03 23:00:04 +0200502 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600503 }
504 }
505}
506
507#-------------------------------------------------------------------------------
508# build_and_parse_kconfig_tree
509#-------------------------------------------------------------------------------
510#load the initial file and start parsing it
511sub build_and_parse_kconfig_tree {
512 my ($top_level_kconfig) = @_;
513 my @config_to_parse;
514 my @parseline;
515 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
516 my @inside_if = (); # stack of if dependencies
517 my $inside_config = ""; # set to symbol name of the config section
518 my @inside_menu = (); # stack of menu names
519 my $inside_choice = "";
Patrick Georgicbc5b992018-11-23 15:55:56 +0100520 my $choice_symbol = "";
Martin Rothbcaaad12015-10-18 11:16:25 -0600521 my $configs_inside_choice;
Martin Roth0e6c0e12016-03-02 12:16:13 -0700522 my %fileinfo;
Martin Rothbcaaad12015-10-18 11:16:25 -0600523
524 #start the tree off by loading the top level kconfig
525 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
526
Martin Rothbcaaad12015-10-18 11:16:25 -0600527 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
528 my $line = $parseline[0]{text};
529 my $filename = $parseline[0]{filename};
530 my $line_no = $parseline[0]{file_line_no};
531
532 #handle help - help text: "help" or "---help---"
Martin Roth811d93a2017-04-06 11:06:00 -0600533 my $lastline_was_help = $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -0600534 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
535 $parseline[0]{inside_help} = $inside_help;
536
537 #look for basic issues in the line, strip crlf
538 $line = simple_line_checks( $line, $filename, $line_no );
539
540 #strip comments
541 $line =~ s/\s*#.*$//;
542
543 #don't parse any more if we're inside a help block
544 if ($inside_help) {
545 #do nothing
546 }
547
548 #handle config
Martin Roth811d93a2017-04-06 11:06:00 -0600549 elsif ( $line =~ /^\s*config\s+/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600550 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
551 my $symbol = $1;
552 $inside_config = $symbol;
553 if ($inside_choice) {
554 $configs_inside_choice++;
555 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700556 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
Martin Rothbcaaad12015-10-18 11:16:25 -0600557 }
558
559 #bool|hex|int|string|tristate <expr> [if <expr>]
560 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
561 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
562 my ( $type, $prompt ) = ( $1, $2 );
563 handle_type( $type, $inside_config, $filename, $line_no );
564 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
565 }
566
567 # def_bool|def_tristate <expr> [if <expr>]
568 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
569 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
570 my ( $orgtype, $default ) = ( $1, $2 );
571 ( my $type = $orgtype ) =~ s/def_//;
572 handle_type( $type, $inside_config, $filename, $line_no );
573 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
574 }
575
576 #prompt <prompt> [if <expr>]
577 elsif ( $line =~ /^\s*prompt/ ) {
578 $line =~ /^\s*prompt\s+(.+)/;
579 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
580 }
581
582 # default <expr> [if <expr>]
583 elsif ( $line =~ /^\s*default/ ) {
584 $line =~ /^\s*default\s+(.*)/;
585 my $default = $1;
586 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
587 }
588
589 # depends on <expr>
590 elsif ( $line =~ /^\s*depends\s+on/ ) {
591 $line =~ /^\s*depends\s+on\s+(.*)$/;
592 my $expr = $1;
593 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
594 handle_expressions( $expr, $inside_config, $filename, $line_no );
595 }
596
597 # comment <prompt>
598 elsif ( $line =~ /^\s*comment/ ) {
599 $inside_config = "";
600 }
601
602 # choice [symbol]
603 elsif ( $line =~ /^\s*choice/ ) {
604 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
605 my $symbol = $1;
606 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
607 handle_type( "bool", $symbol, $filename, $line_no );
Patrick Georgicbc5b992018-11-23 15:55:56 +0100608 $choice_symbol = $symbol;
Martin Rothbcaaad12015-10-18 11:16:25 -0600609 }
610 $inside_config = "";
611 $inside_choice = "$filename $line_no";
612 $configs_inside_choice = 0;
Martin Roth08cf90f2016-01-25 19:54:16 -0700613
614 # Kconfig verifies that choice blocks have a prompt
Martin Rothbcaaad12015-10-18 11:16:25 -0600615 }
616
617 # endchoice
618 elsif ( $line =~ /^\s*endchoice/ ) {
619 $inside_config = "";
620 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700621 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600622 }
623
624 $inside_choice = "";
Patrick Georgicbc5b992018-11-23 15:55:56 +0100625 if (( $configs_inside_choice == 0 ) &&
626 ( $choice_symbol eq "" )) {
627 show_error("unnamed choice block has no symbols at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600628 }
629 $configs_inside_choice = 0;
Patrick Georgicbc5b992018-11-23 15:55:56 +0100630 $choice_symbol="";
Martin Rothbcaaad12015-10-18 11:16:25 -0600631 }
632
633 # [optional]
634 elsif ( $line =~ /^\s*optional/ ) {
635 if ($inside_config) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700636 show_error( "Keyword 'optional' appears inside config for '$inside_config'"
637 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600638 }
639 if ( !$inside_choice ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700640 show_error( "Keyword 'optional' appears outside of a choice block"
641 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600642 }
643 }
644
645 # mainmenu <prompt>
646 elsif ( $line =~ /^\s*mainmenu/ ) {
647 $inside_config = "";
Martin Roth08cf90f2016-01-25 19:54:16 -0700648
649 # Kconfig alread checks for multiple 'mainmenu' entries and mainmenu entries with no prompt
650 # Possible check: look for 'mainmenu ""'
651 # Possible check: verify that a mainmenu has been specified
Martin Rothbcaaad12015-10-18 11:16:25 -0600652 }
653
654 # menu <prompt>
655 elsif ( $line =~ /^\s*menu/ ) {
656 $line =~ /^\s*menu\s+(.*)/;
657 my $menu = $1;
658 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
659 $menu = $1;
660 }
661
662 $inside_config = "";
663 $inside_choice = "";
664 push( @inside_menu, $menu );
665 }
666
Patrick Georgifcc29502018-09-16 21:35:46 +0200667 # visible if <expr>
668 elsif ( $line =~ /^\s*visible if.*$/ ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800669 # Must come directly after menu line (and on a separate line)
670 # but kconfig already checks for that.
671 # Ignore it.
Patrick Georgifcc29502018-09-16 21:35:46 +0200672 }
673
Martin Rothbcaaad12015-10-18 11:16:25 -0600674 # endmenu
675 elsif ( $line =~ /^\s*endmenu/ ) {
676 $inside_config = "";
677 $inside_choice = "";
678 pop @inside_menu;
679 }
680
681 # "if" <expr>
682 elsif ( $line =~ /^\s*if/ ) {
683 $inside_config = "";
684 $line =~ /^\s*if\s+(.*)$/;
685 my $expr = $1;
686 push( @inside_if, $expr );
687 handle_expressions( $expr, $inside_config, $filename, $line_no );
Martin Roth0e6c0e12016-03-02 12:16:13 -0700688 $fileinfo{$filename}{iflevel}++;
Martin Rothbcaaad12015-10-18 11:16:25 -0600689 }
690
691 # endif
692 elsif ( $line =~ /^\s*endif/ ) {
693 $inside_config = "";
694 pop(@inside_if);
Martin Roth0e6c0e12016-03-02 12:16:13 -0700695 $fileinfo{$filename}{iflevel}--;
Martin Rothbcaaad12015-10-18 11:16:25 -0600696 }
697
698 #range <symbol> <symbol> [if <expr>]
699 elsif ( $line =~ /^\s*range/ ) {
700 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
701 handle_range( $1, $2, $inside_config, $filename, $line_no );
702 }
703
704 # select <symbol> [if <expr>]
705 elsif ( $line =~ /^\s*select/ ) {
706 unless ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700707 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600708 }
709
710 if ( $line =~ /^\s*select\s+(.*)$/ ) {
711 $line = $1;
712 my $expression;
713 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
714 if ($line) {
Martin Rothb7c39b22016-01-14 09:04:53 -0700715 add_referenced_symbol( $line, $filename, $line_no, 'select' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600716 }
717 }
718 }
719
720 # source <prompt>
721 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
722 my @newfile = load_kconfig_file( $1, $filename, $line_no, 0, $filename, $line_no );
723 unshift( @config_to_parse, @newfile );
724 $parseline[0]{text} = "# '$line'\n";
725 }
726 elsif (
727 ( $line =~ /^\s*#/ ) || #comments
728 ( $line =~ /^\s*$/ ) #blank lines
729 )
730 {
731 # do nothing
732 }
733 else {
Martin Roth811d93a2017-04-06 11:06:00 -0600734 if ($lastline_was_help) {
735 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized - supposed to be inside help?");
736 }
737 else {
738 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized");
739 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600740 }
741
Martin Roth819e6722016-01-25 16:38:05 -0700742 if ( defined $inside_menu[0] ) {
743 $parseline[0]{menus} = "";
744 }
745 else {
746 $parseline[0]{menus} = "top";
747 }
748
749 my $i = 0;
750 while ( defined $inside_menu[$i] ) {
751 $parseline[0]{menus} .= "$inside_menu[$i]";
752 $i++;
753 if ( defined $inside_menu[$i] ) {
754 $parseline[0]{menus} .= "->";
755 }
756 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600757 push @wholeconfig, @parseline;
758 }
Martin Roth0e6c0e12016-03-02 12:16:13 -0700759
760 foreach my $file ( keys %fileinfo ) {
761 if ( $fileinfo{$file}{iflevel} > 0 ) {
762 show_error("$file has $fileinfo{$file}{iflevel} more 'if' statement(s) than 'endif' statements.");
763 }
764 elsif ( $fileinfo{$file}{iflevel} < 0 ) {
765 show_error(
766 "$file has " . ( $fileinfo{$file}{iflevel} * -1 ) . " more 'endif' statement(s) than 'if' statements." );
767 }
768 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600769}
770
771#-------------------------------------------------------------------------------
772#-------------------------------------------------------------------------------
773sub handle_depends {
774 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
775
776 if ($inside_config) {
777 my $sym_num = $symbols{$inside_config}{count};
778 if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) {
779 $symbols{$inside_config}{$sym_num}{max_dependency}++;
780 }
781 else {
782 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
783 }
784
785 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
786 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
787 }
788}
789
790#-------------------------------------------------------------------------------
791#-------------------------------------------------------------------------------
792sub add_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700793 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600794 my @inside_if = @{$ifref};
795
796 #initialize the symbol or increment the use count.
Martin Roth1b44f7e2016-01-11 14:31:38 -0700797 if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600798 $symbols{$symbol}{count} = 0;
Martin Rothb7c39b22016-01-14 09:04:53 -0700799 if ($inside_choice) {
800 $symbols{$symbol}{choice} = 1;
801 }
802 else {
803 $symbols{$symbol}{choice} = 0;
804 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600805 }
806 else {
807 $symbols{$symbol}{count}++;
Martin Rothb7c39b22016-01-14 09:04:53 -0700808
809 if ( $inside_choice && !$symbols{$symbol}{choice} ) {
810 show_error( "$symbol entry at $filename:$line_no has already been created inside a choice block "
811 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
812 }
813 elsif ( !$inside_choice && $symbols{$symbol}{choice} ) {
814 show_error( "$symbol entry at $filename:$line_no has already been created outside a choice block "
815 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
816 }
Martin Rothaa206472017-03-24 08:51:51 -0600817 elsif ( $inside_choice && $symbols{$symbol}{choice} ) {
818 show_error( "$symbol entry at $filename:$line_no has already been created inside another choice block "
819 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
820 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600821 }
822
823 # add the location of this instance
824 my $symcount = $symbols{$symbol}{count};
825 $symbols{$symbol}{$symcount}{file} = $filename;
826 $symbols{$symbol}{$symcount}{line_no} = $line_no;
827
828 #Add the menu structure
829 if ( defined @$menu_array_ref[0] ) {
830 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
831 }
832
833 #Add any 'if' statements that the symbol is inside as dependencies
834 if (@inside_if) {
835 my $dep_num = 0;
836 for my $dependency (@inside_if) {
837 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
838 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
839 $dep_num++;
840 }
841 }
842}
843
844#-------------------------------------------------------------------------------
845# handle range
846#-------------------------------------------------------------------------------
847sub handle_range {
848 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
849
850 my $expression;
851 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
852
853 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
854 my $checkrange1 = $1;
855 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
856 my $checkrange2 = $1;
857
858 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700859 show_error("Range entry in $filename line $line_no value 1 ($range1) is greater than value 2 ($range2).");
Martin Rothbcaaad12015-10-18 11:16:25 -0600860 }
861
862 if ($inside_config) {
863 if ( exists( $symbols{$inside_config}{range1} ) ) {
864 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700865 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700866 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700867 print " not match the previously defined range $symbols{$inside_config}{range1}"
868 . " $symbols{$inside_config}{range2}";
Martin Roth7aa3cea2015-11-27 18:45:45 -0700869 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600870 }
871 }
872 }
873 else {
874 $symbols{$inside_config}{range1} = $range1;
875 $symbols{$inside_config}{range2} = $range2;
876 $symbols{$inside_config}{range_file} = $filename;
877 $symbols{$inside_config}{range_line_no} = $line_no;
878 }
879 }
880 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700881 show_error("Range entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600882 }
883}
884
885#-------------------------------------------------------------------------------
886# handle_default
887#-------------------------------------------------------------------------------
888sub handle_default {
889 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
890 my $expression;
891 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
892
893 if ($inside_config) {
894 handle_expressions( $default, $inside_config, $filename, $line_no );
895 my $sym_num = $symbols{$inside_config}{count};
896
897 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
898 $symbols{$inside_config}{$sym_num}{default_max} = 0;
899 }
900 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
901 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
902 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
903 if ($expression) {
904 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
905 }
906 }
907 elsif ($inside_choice) {
908 handle_expressions( $default, $inside_config, $filename, $line_no );
909 }
910 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700911 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600912 }
913}
914
915#-------------------------------------------------------------------------------
916# handle_if_line
917#-------------------------------------------------------------------------------
918sub handle_if_line {
919 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
920
921 if ( $exprline !~ /if/ ) {
922 return ( $exprline, "" );
923 }
924
925 #remove any quotes that might have an 'if' in them
926 my $savequote;
927 if ( $exprline =~ /^\s*("[^"]+")/ ) {
928 $savequote = $1;
929 $exprline =~ s/^\s*("[^"]+")//;
930 }
931
932 my $expr = "";
933 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
934 $expr = $1;
935 $exprline =~ s/\s*if\s+.*$//;
936
937 if ($expr) {
938 handle_expressions( $expr, $inside_config, $filename, $line_no );
939 }
940 }
941
942 if ($savequote) {
943 $exprline = $savequote;
944 }
945
946 return ( $exprline, $expr );
947}
948
949#-------------------------------------------------------------------------------
Nico Huberf6b2baa2021-04-02 21:23:32 +0200950# handle_symbol - log which symbols are being used
951#-------------------------------------------------------------------------------
952sub handle_symbol {
953 my ( $symbol, $filename, $line_no ) = @_;
954
955 #filter constant symbols first
956 if ( $symbol =~ /^[yn]$/ ) { # constant y/n
957 return;
958 }
959 if ( $symbol =~ /^-?(?:0x)?\p{XDigit}+$/ ) { # int/hex values
960 return;
961 }
962 if ( $symbol =~ /^"[^"]*"$/ ) { # string values
963 return;
964 }
965
966 if ( $symbol =~ /^([A-Za-z0-9_]+)$/ ) { # actual symbol
967 add_referenced_symbol( $1, $filename, $line_no, 'expression' );
968 }
969 else {
970 show_error("Unrecognized expression: expected symbol, "
971 . "found '$symbol' in $filename line $line_no.");
972 }
973}
974
975#-------------------------------------------------------------------------------
976# handle_expressions - find symbols in expressions
Martin Rothbcaaad12015-10-18 11:16:25 -0600977#-------------------------------------------------------------------------------
978sub handle_expressions {
979 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
980
Nico Huberf6b2baa2021-04-02 21:23:32 +0200981 my $strip = qr/\s*(.*[^\s]+)\s*/;
Martin Rothbcaaad12015-10-18 11:16:25 -0600982
Nico Huberf6b2baa2021-04-02 21:23:32 +0200983 my $parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
984 my $quotes = qr/"[^"]*"/;
985 my $balanced = qr/((?:$parens|$quotes|[^\(\)"])+)/;
Martin Rothbcaaad12015-10-18 11:16:25 -0600986
Nico Huberf6b2baa2021-04-02 21:23:32 +0200987 if ( $exprline =~ /^\s*$balanced\s*(?:\|\||&&)\s*(.+)$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +0200988 # <expr> '||' <expr>, <expr> '&&' <expr> (8)(7)
Nico Huberf6b2baa2021-04-02 21:23:32 +0200989 my ( $lhs, $rhs ) = ( $1, $3 );
990 handle_expressions( $lhs, $inside_config, $filename, $line_no );
991 handle_expressions( $rhs, $inside_config, $filename, $line_no );
992 }
993 elsif ( $exprline =~ /^\s*!(.+)$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +0200994 # '!' <expr> (6)
Martin Rothbcaaad12015-10-18 11:16:25 -0600995 handle_expressions( $1, $inside_config, $filename, $line_no );
996 }
Nico Huberf6b2baa2021-04-02 21:23:32 +0200997 elsif ( $exprline =~ /^\s*$parens\s*$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +0200998 # '(' <expr> ')' (5)
Nico Huberf6b2baa2021-04-02 21:23:32 +0200999 $exprline =~ /^\s*\((.*)\)\s*$/;
Martin Rothbcaaad12015-10-18 11:16:25 -06001000 handle_expressions( $1, $inside_config, $filename, $line_no );
1001 }
Nico Huber2d821952021-04-02 21:56:45 +02001002 elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*(?:[<>]=?|!=)$strip$/ ) {
1003 # <symbol> '<' <symbol>, <symbol> '!=' <symbol>, etc. (4)(3)
Nico Huberf6b2baa2021-04-02 21:23:32 +02001004 my ( $lhs, $rhs ) = ( $1, $2 );
1005 handle_symbol( $lhs, $filename, $line_no );
1006 handle_symbol( $rhs, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001007 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001008 elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*=$strip$/ ) {
1009 # <symbol> '=' <symbol> (2)
1010 my ( $lhs, $rhs ) = ( $1, $2 );
1011 handle_symbol( $lhs, $filename, $line_no );
1012 handle_symbol( $rhs, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001013 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001014 elsif ( $exprline =~ /^$strip$/ ) {
1015 # <symbol> (1)
1016 handle_symbol( $1, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001017 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001018}
1019
1020#-------------------------------------------------------------------------------
1021# add_referenced_symbol
1022#-------------------------------------------------------------------------------
1023sub add_referenced_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -07001024 my ( $symbol, $filename, $line_no, $reftype ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -06001025 if ( exists $referenced_symbols{$symbol} ) {
1026 $referenced_symbols{$symbol}{count}++;
1027 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
1028 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
1029 }
1030 else {
1031 $referenced_symbols{$symbol}{count} = 0;
1032 $referenced_symbols{$symbol}{0}{filename} = $filename;
1033 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
1034 }
Martin Rothb7c39b22016-01-14 09:04:53 -07001035
1036 #mark the symbol as being selected, use referenced symbols for location
1037 if ( $reftype eq 'select' ) {
1038 $selected_symbols{$symbol}{ $referenced_symbols{$symbol}{count} } = 1;
1039 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001040}
1041
1042#-------------------------------------------------------------------------------
1043# handle_help
1044#-------------------------------------------------------------------------------
1045{
1046 #create a non-global static variable by enclosing it and the subroutine
1047 my $help_whitespace = ""; #string to show length of the help whitespace
Martin Roth8849f3b2017-05-23 19:43:43 -06001048 my $help_keyword_whitespace = "";
Martin Rothbcaaad12015-10-18 11:16:25 -06001049
1050 sub handle_help {
1051 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1052
1053 if ($inside_help) {
1054
1055 #get the indentation level if it's not already set.
1056 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
1057 $line =~ /^(\s+)/; #find the indentation level.
1058 $help_whitespace = $1;
1059 if ( !$help_whitespace ) {
Martin Roth8849f3b2017-05-23 19:43:43 -06001060 show_error("$filename:$line_no - help text starts with no whitespace.");
1061 return $inside_help;
1062 }
1063 elsif ($help_keyword_whitespace eq $help_whitespace) {
1064 show_error("$filename:$line_no - help text needs additional indentation.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001065 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -06001066 }
1067 }
1068
1069 #help ends at the first line which has a smaller indentation than the first line of the help text.
Martin Roth8849f3b2017-05-23 19:43:43 -06001070 if ( ( $line !~ /^$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001071 $inside_help = 0;
1072 $help_whitespace = "";
Martin Roth8849f3b2017-05-23 19:43:43 -06001073 $help_keyword_whitespace = "";
Martin Rothbcaaad12015-10-18 11:16:25 -06001074 }
1075 else { #if it's not ended, add the line to the helptext array for the symbol's instance
1076 if ($inside_config) {
1077 my $sym_num = $symbols{$inside_config}{count};
1078 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
1079 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
1080 }
Martin Roth8849f3b2017-05-23 19:43:43 -06001081 if ( ($help_keyword_whitespace eq $help_whitespace) && ( $line !~ /^[\r\n]+/ ) ) {
1082 show_error("$filename:$line_no - help text needs additional indentation.");
1083 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001084 }
1085 }
1086 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
1087 $inside_help = $line_no;
Martin Roth8849f3b2017-05-23 19:43:43 -06001088 $line =~ /^(\s+)/;
1089 $help_keyword_whitespace = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001090 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001091 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001092 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001093 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001094 }
1095 elsif ($inside_config) {
1096 $help_whitespace = "";
1097 my $sym_num = $symbols{$inside_config}{count};
1098 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
1099 $symbols{$inside_config}{$sym_num}{helptext} = ();
1100 }
1101 }
1102 return $inside_help;
1103 }
1104}
1105
1106#-------------------------------------------------------------------------------
1107# handle_type
1108#-------------------------------------------------------------------------------
1109sub handle_type {
1110 my ( $type, $inside_config, $filename, $line_no ) = @_;
1111
1112 my $expression;
1113 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
1114
Martin Roth08ee1cf2016-01-25 16:39:32 -07001115 if ( $type =~ /tristate/ ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -08001116 show_error("$filename:$line_no - tristate types are not used.");
Martin Roth08ee1cf2016-01-25 16:39:32 -07001117 }
1118
Martin Rothbcaaad12015-10-18 11:16:25 -06001119 if ($inside_config) {
1120 if ( exists( $symbols{$inside_config}{type} ) ) {
1121 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001122 show_error( "Config '$inside_config' type entry $type"
1123 . " at $filename:$line_no does not match $symbols{$inside_config}{type}"
1124 . " defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001125 }
1126 }
1127 else {
1128 $symbols{$inside_config}{type} = $type;
1129 $symbols{$inside_config}{type_file} = $filename;
1130 $symbols{$inside_config}{type_line_no} = $line_no;
1131 }
1132 }
1133 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001134 show_error("Type entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001135 }
1136}
1137
1138#-------------------------------------------------------------------------------
1139# handle_prompt
1140#-------------------------------------------------------------------------------
1141sub handle_prompt {
1142 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1143
1144 my $expression;
1145 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
1146
1147 if ($inside_config) {
1148 if ( $prompt !~ /^\s*$/ ) {
1149 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
1150 $prompt = $1;
1151 }
1152
Martin Roth08cf90f2016-01-25 19:54:16 -07001153 #display an error if there's a prompt at the top menu level
Martin Rothbcaaad12015-10-18 11:16:25 -06001154 if ( !defined @$menu_array_ref[0] ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001155 show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu"
1156 . " at $filename:$line_no." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001157 }
1158
1159 my $sym_num = $symbols{$inside_config}{count};
Martin Rothb58d3492016-01-25 16:42:13 -07001160 if ( !exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001161 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
1162 }
Martin Rothb58d3492016-01-25 16:42:13 -07001163 else {
1164 $symbols{$inside_config}{$sym_num}{prompt_max}++;
1165 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001166 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
1167 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
1168 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
Martin Rothb58d3492016-01-25 16:42:13 -07001169
1170 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_menu} = @$menu_array_ref;
Martin Rothbcaaad12015-10-18 11:16:25 -06001171 if ($expression) {
1172 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
1173 }
1174 }
1175 }
1176 elsif ($inside_choice) {
1177
1178 #do nothing
1179 }
1180 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001181 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001182 }
1183}
1184
1185#-------------------------------------------------------------------------------
1186# simple_line_checks - Does some basic checks on the current line, then cleans the line
1187# up for further processing.
1188#-------------------------------------------------------------------------------
1189sub simple_line_checks {
1190 my ( $line, $filename, $line_no ) = @_;
1191
1192 #check for spaces instead of tabs
1193 if ( $line =~ /^ +/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001194 show_error("$filename:$line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001195 }
1196
1197 #verify a linefeed at the end of the line
1198 if ( $line !~ /.*\n/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001199 show_error( "$filename:$line_no does not end with linefeed."
1200 . " This can cause the line to not be recognized by the Kconfig parser.\n#($line)" );
Martin Rothbcaaad12015-10-18 11:16:25 -06001201 $line =~ s/\s*$//;
1202 }
1203 else {
1204 chop($line);
1205 }
1206
1207 return $line;
1208}
1209
1210#-------------------------------------------------------------------------------
1211# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
1212#-------------------------------------------------------------------------------
1213sub load_kconfig_file {
1214 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
1215 my @file_data;
1216 my @dir_file_data;
1217
1218 #recursively handle coreboot's new source glob operator
Arthur Heymans55f01322019-11-05 12:06:59 +01001219 if ( $input_file =~ /^(.*?)\/(\w*)\*(\w*)\/(.*)$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001220 my $dir_prefix = $1;
Arthur Heymans55f01322019-11-05 12:06:59 +01001221 my $dir_glob_prefix = $2;
1222 my $dir_glob_suffix = $3;
1223 my $dir_suffix = $4;
Martin Rothbcaaad12015-10-18 11:16:25 -06001224 if ( -d "$dir_prefix" ) {
1225
1226 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
1227 my @dirlist = sort { $a cmp $b } readdir(D);
1228 closedir(D);
1229
1230 while ( my $directory = shift @dirlist ) {
1231
1232 #ignore non-directory files
Arthur Heymans55f01322019-11-05 12:06:59 +01001233 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ )
1234 && ( $directory =~ /\Q$dir_glob_prefix\E.*\Q$dir_glob_suffix\E/ ) ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001235 push @dir_file_data,
1236 load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1237 $input_file, $loadline, 1, $loadfile, $loadline );
Martin Rothbcaaad12015-10-18 11:16:25 -06001238 }
1239 }
1240 }
1241
1242 #the directory should exist when using a glob
1243 else {
Julius Wernere5eb2de2019-03-05 17:10:19 -08001244 show_error("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -06001245 }
1246 }
1247
1248 #if the file exists, try to load it.
1249 elsif ( -e "$input_file" ) {
1250
Martin Rotha7d00272016-10-03 23:00:04 +02001251 #throw a warning if the file has already been loaded.
Martin Rothbcaaad12015-10-18 11:16:25 -06001252 if ( exists $loaded_files{$input_file} ) {
Martin Rotha7d00272016-10-03 23:00:04 +02001253 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -06001254 }
1255
1256 #load the file's contents and mark the file as loaded for checking later
1257 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
1258 @file_data = <$HANDLE>;
1259 close $HANDLE;
1260 $loaded_files{$input_file} = "'$loadfile' line $loadline";
1261 }
1262
1263 # if the file isn't being loaded from a glob, it should exist.
1264 elsif ( $expanded == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001265 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -06001266 }
1267
1268 my $line_in_file = 0;
1269 while ( my $line = shift @file_data ) {
1270
1271 #handle line continuation.
1272 my $continue_line = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001273 while ( $line =~ /(.*)\s+\\$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001274 my $text = $1;
1275
1276 # get rid of leading whitespace on all but the first and last lines
1277 $text =~ s/^\s*/ / if ($continue_line);
1278
1279 $dir_file_data[$line_in_file]{text} .= $text;
1280 $line = shift @file_data;
1281 $continue_line++;
1282
1283 #put the data into the continued lines (other than the first)
1284 $line =~ /^\s*(.*)\s*$/;
1285
Martin Roth1b44f7e2016-01-11 14:31:38 -07001286 $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n";
1287 $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file;
1288 $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001289
1290 #get rid of multiple leading spaces for last line
1291 $line = " $1\n";
1292 }
1293
Martin Roth1b44f7e2016-01-11 14:31:38 -07001294 $dir_file_data[$line_in_file]{text} .= $line;
Martin Rothbcaaad12015-10-18 11:16:25 -06001295 $dir_file_data[$line_in_file]{filename} = $input_file;
1296 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1297
1298 $line_in_file++;
1299 if ($continue_line) {
1300 $line_in_file += $continue_line;
1301 }
1302 }
1303
1304 if ($topfile) {
1305 my %file_data;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001306 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001307 $file_data{filename} = $topfile;
1308 $file_data{file_line_no} = "($topline)";
Martin Roth1b44f7e2016-01-11 14:31:38 -07001309 unshift( @dir_file_data, \%file_data );
Martin Rothbcaaad12015-10-18 11:16:25 -06001310 }
1311
1312 return @dir_file_data;
1313}
1314
Martin Rothbcaaad12015-10-18 11:16:25 -06001315#-------------------------------------------------------------------------------
1316# print_wholeconfig - prints out the parsed Kconfig file
1317#-------------------------------------------------------------------------------
1318sub print_wholeconfig {
1319
1320 return unless $print_full_output;
1321
1322 for ( my $i = 0 ; $i < $#wholeconfig ; $i++ ) {
1323 my $line = $wholeconfig[$i];
1324 chop( $line->{text} );
1325
1326 #replace tabs with spaces for consistency
1327 $line->{text} =~ s/\t/ /g;
Martin Roth819e6722016-01-25 16:38:05 -07001328 printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text};
Martin Rothbcaaad12015-10-18 11:16:25 -06001329 }
1330}
1331
1332#-------------------------------------------------------------------------------
1333# check_if_file_referenced - checks for kconfig files that are not being parsed
1334#-------------------------------------------------------------------------------
1335sub check_if_file_referenced {
1336 my $filename = $File::Find::name;
Martin Rothab2d7772016-01-25 16:45:14 -07001337 if ( ( $filename =~ /Kconfig/ )
1338 && ( !$filename =~ /\.orig$/ )
1339 && ( !$filename =~ /~$/ )
1340 && ( !exists $loaded_files{$filename} ) )
1341 {
Martin Rothd8080172015-11-26 19:12:44 -07001342 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001343 }
1344}
1345
1346#-------------------------------------------------------------------------------
1347# check_arguments parse the command line arguments
1348#-------------------------------------------------------------------------------
1349sub check_arguments {
1350 my $show_usage = 0;
1351 GetOptions(
1352 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001353 'e|errors_off' => \$suppress_error_output,
1354 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001355 'o|output=s' => \$output_file,
1356 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001357 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001358 'path=s' => \$top_dir,
1359 'c|config=s' => \$config_file,
1360 'G|no_git_grep' => \$dont_use_git_grep,
1361 );
Martin Rothd8080172015-11-26 19:12:44 -07001362
1363 if ($suppress_error_output) {
1364 $suppress_warning_output = 1;
1365 }
1366 if ($suppress_warning_output) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001367 $show_note_output = 0;
Martin Rothd8080172015-11-26 19:12:44 -07001368 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001369}
1370
1371#-------------------------------------------------------------------------------
1372# usage - Print the arguments for the user
1373#-------------------------------------------------------------------------------
1374sub usage {
1375 print "kconfig_lint <options>\n";
1376 print " -o|--output=file Set output filename\n";
1377 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001378 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001379 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001380 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001381 print " --path=dir Path to top level kconfig\n";
1382 print " -c|--config=file Filename of config file to load\n";
1383 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
1384
1385 exit(0);
1386}
1387
13881;