blob: 107f01cdcae5dd0009bbc5a5f9d25cd396a81d0d [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 Roth873ebf22023-08-10 10:00:51 -060028my $include_site_local = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -060029
Martin Rothabf7d4d2016-02-19 10:24:25 -070030# Globals
Martin Roth1b44f7e2016-01-11 14:31:38 -070031my $top_dir = "."; # Directory where Kconfig is run
32my $root_dir = "src"; # Directory of the top level Kconfig file
33my $errors_found = 0; # count of errors
Martin Rothd8080172015-11-26 19:12:44 -070034my $warnings_found = 0;
Martin Roth63ea4932016-01-25 16:08:27 -070035my $exclude_dirs_and_files =
Martin Rothab273d32016-11-23 17:28:00 -070036 '^build/\|^coreboot-builds/\|^configs/\|^util/\|^\.git/\|^payloads\|^Documentation\|^3rdparty'
Martin Roth63ea4932016-01-25 16:08:27 -070037 . '\|' . # directories to exclude when searching for used symbols
Martin Rothb357e532022-11-22 14:47:12 -070038 '\.config\|\.txt$\|\.tex$\|\.tags\|/kconfig.h\|\.fmd'; #files to exclude when looking for symbols
Martin Rothe69c58d2016-11-15 21:08:42 -070039my $payload_files_to_check='payloads/Makefile.inc payloads/external/Makefile.inc';
Martin Roth08cf90f2016-01-25 19:54:16 -070040my $config_file = ""; # name of config file to load symbol values from.
41my @wholeconfig; # document the entire kconfig structure
42my %loaded_files; # list of each Kconfig file loaded
43my %symbols; # main structure of all symbols declared
44my %referenced_symbols; # list of symbols referenced by expressions or select statements
45my %used_symbols; # structure of symbols used in the tree, and where they're found
46my @collected_symbols; #
47my %selected_symbols; # list of symbols that are enabled by a select statement
Martin Rothbcaaad12015-10-18 11:16:25 -060048
Martin Roth6bfbf1c2016-01-25 16:12:49 -070049my $exclude_unused = '_SPECIFIC_OPTIONS|SOUTH_BRIDGE_OPTIONS';
50
Martin Rothbcaaad12015-10-18 11:16:25 -060051Main();
52
53#-------------------------------------------------------------------------------
54# Main
55#
56# Start by loading and parsing the top level Kconfig, this pulls in the other
57# files. Parsing the tree creates several arrays and hashes that can be used
58# to check for errors
59#-------------------------------------------------------------------------------
60sub Main {
61
Solomon Alan-Deia6e60f02022-08-11 11:13:28 -060062 check_arguments();
63 if ( !($dont_use_git_grep || `git rev-parse --is-inside-work-tree`) ) {
64 $dont_use_git_grep = 1;
65 print STDERR "\nGit grep unavailable, falling back to regular grep...\n";
66 }
Martin Roth873ebf22023-08-10 10:00:51 -060067 if ( !$include_site_local) {
68 $exclude_dirs_and_files = "^site-local\|" . $exclude_dirs_and_files;
69 }
Solomon Alan-Deia6e60f02022-08-11 11:13:28 -060070
Martin Rothbcaaad12015-10-18 11:16:25 -060071 open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
72
Martin Roth1b44f7e2016-01-11 14:31:38 -070073 if ( defined $top_dir ) {
74 chdir $top_dir or die "Error: can't cd to $top_dir\n";
Martin Rothbcaaad12015-10-18 11:16:25 -060075 }
76
Martin Roth1b44f7e2016-01-11 14:31:38 -070077 die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir );
Martin Rothbcaaad12015-10-18 11:16:25 -060078
79 #load the Kconfig tree, checking what we can and building up all the hash tables
80 build_and_parse_kconfig_tree("$root_dir/Kconfig");
81
Martin Rothbcaaad12015-10-18 11:16:25 -060082 load_config($config_file) if ($config_file);
83
Martin Roth08705f12016-11-09 14:27:00 -070084 check_type();
Martin Rothbcaaad12015-10-18 11:16:25 -060085 check_defaults();
86 check_referenced_symbols();
87
88 collect_used_symbols();
89 check_used_symbols();
90 check_for_ifdef();
91 check_for_def();
Nico Huberec017592019-04-06 16:09:46 +020092 check_config_macro();
Martin Rothb7c39b22016-01-14 09:04:53 -070093 check_selected_symbols();
Martin Rothbcaaad12015-10-18 11:16:25 -060094
Martin Rothabf7d4d2016-02-19 10:24:25 -070095 # Run checks based on the data that was found
96 if ( ( !$suppress_warning_output ) && ( ${^TAINT} == 0 ) ) {
97
98 # The find function is tainted - only run it if taint checking
99 # is disabled and warnings are enabled.
100 find( \&check_if_file_referenced, $root_dir );
101 }
102
Martin Rothbcaaad12015-10-18 11:16:25 -0600103 print_wholeconfig();
104
Martin Rothd8080172015-11-26 19:12:44 -0700105 if ($errors_found) {
106 print "# $errors_found errors";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700107 if ($warnings_found) {
Martin Rothd8080172015-11-26 19:12:44 -0700108 print ", $warnings_found warnings";
109 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700110 print "\n";
Martin Rothd8080172015-11-26 19:12:44 -0700111 }
112
Martin Roth1b44f7e2016-01-11 14:31:38 -0700113 exit( $errors_found + $warnings_found );
Martin Rothd8080172015-11-26 19:12:44 -0700114}
115
116#-------------------------------------------------------------------------------
117# Print and count errors
118#-------------------------------------------------------------------------------
119sub show_error {
120 my ($error_msg) = @_;
121 unless ($suppress_error_output) {
122 print "#!!!!! Error: $error_msg\n";
123 $errors_found++;
124 }
125}
126
127#-------------------------------------------------------------------------------
128# Print and count warnings
129#-------------------------------------------------------------------------------
130sub show_warning {
131 my ($warning_msg) = @_;
132 unless ($suppress_warning_output) {
133 print "#!!!!! Warning: $warning_msg\n";
134 $warnings_found++;
135 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600136}
137
138#-------------------------------------------------------------------------------
Martin Rothb7c39b22016-01-14 09:04:53 -0700139# check selected symbols for validity
140# they must be bools
141# they cannot select symbols created in 'choice' blocks
142#-------------------------------------------------------------------------------
143sub check_selected_symbols {
144
145 #loop through symbols found in expressions and used by 'select' keywords
146 foreach my $symbol ( sort ( keys %selected_symbols ) ) {
147 my $type_failure = 0;
148 my $choice_failure = 0;
149
150 #errors selecting symbols that don't exist are already printed, so we
151 #don't need to print them again here
152
153 #make sure the selected symbols are bools
154 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "bool" ) ) {
155 $type_failure = 1;
156 }
157
158 #make sure we're not selecting choice symbols
159 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{choice} ) ) {
160 $choice_failure = 1;
161 }
162
163 #loop through each instance of the symbol to print out all of the failures
164 for ( my $i = 0 ; $i <= $referenced_symbols{$symbol}{count} ; $i++ ) {
165 next if ( !exists $selected_symbols{$symbol}{$i} );
166 my $file = $referenced_symbols{$symbol}{$i}{filename};
167 my $lineno = $referenced_symbols{$symbol}{$i}{line_no};
168 if ($type_failure) {
169 show_error(
170 "CONFIG_$symbol' selected at $file:$lineno." . " Selects only work on symbols of type bool." );
171 }
172 if ($choice_failure) {
173 show_error(
174 "'CONFIG_$symbol' selected at $file:$lineno." . " Symbols created in a choice cannot be selected." );
175 }
176 }
177 }
178}
179
180#-------------------------------------------------------------------------------
Martin Rothbcaaad12015-10-18 11:16:25 -0600181# check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and
182# #if defined(CONFIG_[symbol_name]).
183#
184# #ifdef symbol is valid for strings, but bool, hex, and INT are always defined.
185# #if defined(symbol) && symbol is also a valid construct.
186#-------------------------------------------------------------------------------
187sub check_for_ifdef {
188 my @ifdef_symbols = @collected_symbols;
189
190 #look for #ifdef SYMBOL
191 while ( my $line = shift @ifdef_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800192 if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) {
Martin Roth5b883012017-06-25 20:27:36 -0600193 my $file = $1;
194 my $lineno = $2;
195 my $symbol = $3;
196
197 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800198 show_error( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
Martin Roth5b883012017-06-25 20:27:36 -0600199 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
200 }
Julius Wernere5eb2de2019-03-05 17:10:19 -0800201 } elsif ( $line =~ /^([^:]+):(\d+):.+defined\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700202 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600203 my $lineno = $2;
204 my $symbol = $3;
205
Martin Roth1b44f7e2016-01-11 14:31:38 -0700206 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800207 show_error( "defined(CONFIG_$symbol) used at $file:$lineno."
Martin Roth1b44f7e2016-01-11 14:31:38 -0700208 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600209 }
210 }
211 }
212}
213
214#-------------------------------------------------------------------------------
215# check_for_def - Look for instances of #define CONFIG_[symbol_name]
216#
217# Symbols should not be redefined outside of Kconfig, and #defines should not
218# look like symbols
219#-------------------------------------------------------------------------------
220sub check_for_def {
221 my @def_symbols = @collected_symbols;
222
223 #look for #ifdef SYMBOL
224 while ( my $line = shift @def_symbols ) {
225 if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700226 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600227 my $lineno = $2;
228 my $symbol = $3;
229
Martin Roth1b44f7e2016-01-11 14:31:38 -0700230 if ( ( exists $symbols{$symbol} ) ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800231 show_error("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
Martin Roth1b44f7e2016-01-11 14:31:38 -0700232 }
233 else {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800234 show_error( "#define 'CONFIG_$symbol' used at $file:$lineno."
Martin Roth1b44f7e2016-01-11 14:31:38 -0700235 . " Other #defines should not look like Kconfig symbols." );
236 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600237 }
238 }
239}
240
241#-------------------------------------------------------------------------------
Martin Roth08705f12016-11-09 14:27:00 -0700242# check_type - Make sure that all symbols have a type defined.
243#
244# Conflicting types are found when parsing the Kconfig tree.
245#-------------------------------------------------------------------------------
246sub check_type {
247
248 # loop through each defined symbol
249 foreach my $sym ( sort ( keys %symbols ) ) {
250
251 # Make sure there's a type set for the symbol
252 if (!defined $symbols{$sym}{type}) {
253
254 #loop through each instance of that symbol
255 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
256
257 my $filename = $symbols{$sym}{$sym_num}{file};
258 my $line_no = $symbols{$sym}{$sym_num}{line_no};
259
260 show_error("No type defined for symbol $sym defined at $filename:$line_no.");
261 }
262 }
263 }
264}
265
266#-------------------------------------------------------------------------------
Nico Huberec017592019-04-06 16:09:46 +0200267# check_config_macro - The CONFIG() macro is only valid for symbols of type
268# bool. It would probably work on type hex or int if the value was 0 or 1,
269# but this seems like a bad plan. Using it on strings is dead out.
270#
271# The IS_ENABLED() macro is forbidden in coreboot now. Though, as long as
272# we keep its definition in libpayload for compatibility, we have to check
273# that it doesn't sneak back in.
Martin Rothbcaaad12015-10-18 11:16:25 -0600274#-------------------------------------------------------------------------------
Nico Huberec017592019-04-06 16:09:46 +0200275sub check_config_macro {
Martin Rothb6acc302015-11-27 18:51:19 -0700276 my @is_enabled_symbols = @collected_symbols;
Martin Rothbcaaad12015-10-18 11:16:25 -0600277
278 #sort through symbols found by grep and store them in a hash for easy access
279 while ( my $line = shift @is_enabled_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800280 if ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG\(.*)/ ) {
281 my $file = $1;
282 my $lineno = $2;
283 $line = $3;
284 while ( $line =~ /(.*)\bCONFIG\(([^)]*)\)(.*)/ ) {
285 my $symbol = $2;
286 $line = $1 . $3;
287
288 #make sure that the type is bool
289 if ( exists $symbols{$symbol} ) {
290 if ( $symbols{$symbol}{type} ne "bool" ) {
291 show_error( "CONFIG($symbol) used at $file:$lineno."
292 . " CONFIG() is only valid for type 'bool', not '$symbols{$symbol}{type}'." );
293 }
294 }
295 else {
Julius Wernerf0286042019-04-09 15:57:07 -0700296 show_error("CONFIG() used on unknown value ($symbol) at $file:$lineno.");
Julius Werneref7a3262019-03-05 16:57:52 -0800297 }
298 }
299 } elsif ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700300 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600301 my $lineno = $2;
Martin Rothb6acc302015-11-27 18:51:19 -0700302 $line = $3;
Martin Roth572a8562016-01-25 16:14:09 -0700303 if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) {
Nico Huberec017592019-04-06 16:09:46 +0200304 show_error("# uninterpreted IS_ENABLED at $file:$lineno: $line");
Martin Rothb6acc302015-11-27 18:51:19 -0700305 next;
Martin Rothbcaaad12015-10-18 11:16:25 -0600306 }
Martin Rothb6acc302015-11-27 18:51:19 -0700307 while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
308 my $symbol = $2;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700309 $line = $1 . $3;
Nico Huberec017592019-04-06 16:09:46 +0200310 show_error("IS_ENABLED(CONFIG_$symbol) at $file:$lineno is deprecated. Use CONFIG($symbol) instead.");
Martin Rothb6acc302015-11-27 18:51:19 -0700311 }
Martin Roth5b883012017-06-25 20:27:36 -0600312 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?.*?$/ ) {
313 my $file = $1;
314 my $lineno = $2;
315 my $symbol = $3;
Nico Huberec017592019-04-06 16:09:46 +0200316 # If the type is bool, give a warning that CONFIG() should be used
Martin Roth5b883012017-06-25 20:27:36 -0600317 if ( exists $symbols{$symbol} ) {
318 if ( $symbols{$symbol}{type} eq "bool" ) {
319 show_error( "#if CONFIG_$symbol used at $file:$lineno."
Julius Werneref7a3262019-03-05 16:57:52 -0800320 . " CONFIG($symbol) should be used for type 'bool'" );
Martin Roth5b883012017-06-25 20:27:36 -0600321 }
322 }
Julius Werneref7a3262019-03-05 16:57:52 -0800323 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if.*(?:&&|\|\|)\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?$/ ) {
Martin Roth5b883012017-06-25 20:27:36 -0600324 my $file = $1;
325 my $lineno = $2;
326 my $symbol = $3;
Nico Huberec017592019-04-06 16:09:46 +0200327 # If the type is bool, give a warning that CONFIG() should be used
Martin Roth5b883012017-06-25 20:27:36 -0600328 if ( exists $symbols{$symbol} ) {
329 if ( $symbols{$symbol}{type} eq "bool" ) {
330 show_error( "#if CONFIG_$symbol used at $file:$lineno."
Julius Werneref7a3262019-03-05 16:57:52 -0800331 . " CONFIG($symbol) should be used for type 'bool'" );
Martin Roth5b883012017-06-25 20:27:36 -0600332 }
333 }
Julius Wernere5eb2de2019-03-05 17:10:19 -0800334 } elsif ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG_.+)/ ) {
335 my $file = $1;
336 my $lineno = $2;
337 $line = $3;
338 if ( $file =~ /.*\.(c|h|asl|ld)/ ) {
339 while ( $line =~ /(.*)\bCONFIG_(\w+)(.*)/ && $1 !~ /\/\/|\/\*/ ) {
340 my $symbol = $2;
341 $line = $1 . $3;
342 if ( exists $symbols{$symbol} ) {
343 if ( $symbols{$symbol}{type} eq "bool" ) {
Martin Rotha0209032020-07-24 12:42:59 -0600344 show_error( "Naked reference to CONFIG_$symbol used at $file:$lineno."
Julius Wernere5eb2de2019-03-05 17:10:19 -0800345 . " A 'bool' Kconfig should always be accessed through CONFIG($symbol)." );
346 }
347 } else {
348 show_warning( "Unknown config option CONFIG_$symbol used at $file:$lineno." );
349 }
350 }
351 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600352 }
353 }
354}
355
356#-------------------------------------------------------------------------------
357# check_defaults - Look for defaults that come after a default with no
358# dependencies.
359#
360# TODO - check for defaults with the same dependencies
361#-------------------------------------------------------------------------------
362sub check_defaults {
363
364 # loop through each defined symbol
365 foreach my $sym ( sort ( keys %symbols ) ) {
366 my $default_set = 0;
367 my $default_filename = "";
368 my $default_line_no = "";
369
370 #loop through each instance of that symbol
371 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
372
373 #loop through any defaults for that instance of that symbol, if there are any
374 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
375 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
376
Martin Rothfa956252016-09-30 15:51:32 -0600377 my $filename = $symbols{$sym}{$sym_num}{file};
378 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
379
Martin Roth08705f12016-11-09 14:27:00 -0700380 # Make sure there's a type set for the symbol
381 next if (!defined $symbols{$sym}{type});
382
Michael Niewöhner90fcffb2021-09-16 17:56:14 +0200383 # Symbols created/used inside a choice must not have a default set. The default is set by the choice itself.
384 if ($symbols{$sym}{choice}) {
385 show_error("Defining a default for symbol '$sym' at $filename:$line_no, used inside choice at "
Michael Niewöhner43963582021-10-17 17:48:50 +0200386 . "$symbols{$sym}{choice}, is not allowed.");
Michael Niewöhner90fcffb2021-09-16 17:56:14 +0200387 }
388
Martin Rothfa956252016-09-30 15:51:32 -0600389 # skip good defaults
390 if (! ((($symbols{$sym}{type} eq "hex") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^0x/)) ||
391 (($symbols{$sym}{type} eq "int") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[-0-9]+$/)) ||
392 (($symbols{$sym}{type} eq "string") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^".*"$/)) ||
393 (($symbols{$sym}{type} eq "bool") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[yn]$/)))
394 ) {
395
396 my ($checksym) = $symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /(\w+)/;
397
398 if (! exists $symbols{$checksym}) {
399
400 # verify the symbol type against the default value
401 if ($symbols{$sym}{type} eq "hex") {
402 show_error("non hex default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for hex symbol $sym at $filename:$line_no.");
403 } elsif ($symbols{$sym}{type} eq "int") {
404 show_error("non int default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for int symbol $sym at $filename:$line_no.");
405 } elsif ($symbols{$sym}{type} eq "string") {
406 # TODO: Remove special MAINBOARD_DIR check
407 if ($sym ne "MAINBOARD_DIR") {
408 show_error("no quotes around default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for string symbol $sym at $filename:$line_no.");
409 }
410 } elsif ($symbols{$sym}{type} eq "bool") {
411 if ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /[01YN]/) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800412 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 -0600413 } else {
414 show_error("non bool default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for bool symbol $sym at $filename:$line_no.");
415 }
416 }
417 }
418 }
419
Martin Rothbcaaad12015-10-18 11:16:25 -0600420 #if a default is already set, display an error
421 if ($default_set) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800422 show_error( "Default for '$sym' referenced at $filename:$line_no will never be set"
Martin Roth1b44f7e2016-01-11 14:31:38 -0700423 . " - overridden by default set at $default_filename:$default_line_no" );
Martin Rothbcaaad12015-10-18 11:16:25 -0600424 }
425 else {
426 #if no default is set, see if this is a default with no dependencies
427 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
428 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
429 {
430 $default_set = 1;
431 $default_filename = $symbols{$sym}{$sym_num}{file};
432 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
433 }
434 }
435 }
436 }
437 }
438}
439
440#-------------------------------------------------------------------------------
441# check_referenced_symbols - Make sure the symbols referenced by expressions and
442# select statements are actually valid symbols.
443#-------------------------------------------------------------------------------
444sub check_referenced_symbols {
445
446 #loop through symbols found in expressions and used by 'select' keywords
447 foreach my $key ( sort ( keys %referenced_symbols ) ) {
448
449 #make sure the symbol was defined by a 'config' or 'choice' keyword
450 next if ( exists $symbols{$key} );
451
Martin Rothd8080172015-11-26 19:12:44 -0700452 #loop through each instance of the symbol to print out all of the invalid references
453 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
454 my $filename = $referenced_symbols{$key}{$i}{filename};
455 my $line_no = $referenced_symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700456 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600457 }
458 }
459}
460
461#-------------------------------------------------------------------------------
462#-------------------------------------------------------------------------------
463sub collect_used_symbols {
464 # find all references to CONFIG_ statements in the tree
465
466 if ($dont_use_git_grep) {
Julius Werneref7a3262019-03-05 16:57:52 -0800467 @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 -0700468 }
469 else {
Julius Werneref7a3262019-03-05 16:57:52 -0800470 @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 -0600471 }
472
473 my @used_symbols = @collected_symbols;
474
475 #sort through symbols found by grep and store them in a hash for easy access
476 while ( my $line = shift @used_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800477 while ( $line =~ /[^A-Za-z0-9_]CONFIG(?:_|\()([A-Za-z0-9_]+)/g ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700478 my $symbol = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600479 my $filename = "";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700480 if ( $line =~ /^([^:]+):/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600481 $filename = $1;
482 }
483
Martin Roth1b44f7e2016-01-11 14:31:38 -0700484 if ( exists $used_symbols{$symbol}{count} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600485 $used_symbols{$symbol}{count}++;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700486 }
487 else {
488 $used_symbols{$symbol}{count} = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -0600489 }
490 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700491 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600492 }
493}
494
495#-------------------------------------------------------------------------------
496# check_used_symbols - Checks to see whether or not the created symbols are
497# actually used.
498#-------------------------------------------------------------------------------
499sub check_used_symbols {
500 # loop through all defined symbols and see if they're used anywhere
501 foreach my $key ( sort ( keys %symbols ) ) {
502
Martin Roth6bfbf1c2016-01-25 16:12:49 -0700503 if ( $key =~ /$exclude_unused/ ) {
504 next;
505 }
506
Martin Rothbcaaad12015-10-18 11:16:25 -0600507 #see if they're used internal to Kconfig
508 next if ( exists $referenced_symbols{$key} );
509
510 #see if they're used externally
511 next if exists $used_symbols{$key};
512
513 #loop through the definitions to print out all the places the symbol is defined.
514 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
515 my $filename = $symbols{$key}{$i}{file};
516 my $line_no = $symbols{$key}{$i}{line_no};
Martin Rotha7d00272016-10-03 23:00:04 +0200517 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600518 }
519 }
520}
521
522#-------------------------------------------------------------------------------
523# build_and_parse_kconfig_tree
524#-------------------------------------------------------------------------------
525#load the initial file and start parsing it
526sub build_and_parse_kconfig_tree {
527 my ($top_level_kconfig) = @_;
528 my @config_to_parse;
529 my @parseline;
530 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
531 my @inside_if = (); # stack of if dependencies
532 my $inside_config = ""; # set to symbol name of the config section
533 my @inside_menu = (); # stack of menu names
534 my $inside_choice = "";
Patrick Georgicbc5b992018-11-23 15:55:56 +0100535 my $choice_symbol = "";
Martin Rothbcaaad12015-10-18 11:16:25 -0600536 my $configs_inside_choice;
Martin Roth0e6c0e12016-03-02 12:16:13 -0700537 my %fileinfo;
Martin Rothbcaaad12015-10-18 11:16:25 -0600538
539 #start the tree off by loading the top level kconfig
540 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
541
Martin Rothbcaaad12015-10-18 11:16:25 -0600542 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
543 my $line = $parseline[0]{text};
544 my $filename = $parseline[0]{filename};
545 my $line_no = $parseline[0]{file_line_no};
546
547 #handle help - help text: "help" or "---help---"
Martin Roth811d93a2017-04-06 11:06:00 -0600548 my $lastline_was_help = $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -0600549 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
550 $parseline[0]{inside_help} = $inside_help;
551
552 #look for basic issues in the line, strip crlf
553 $line = simple_line_checks( $line, $filename, $line_no );
554
555 #strip comments
556 $line =~ s/\s*#.*$//;
557
558 #don't parse any more if we're inside a help block
559 if ($inside_help) {
560 #do nothing
561 }
562
563 #handle config
Martin Roth811d93a2017-04-06 11:06:00 -0600564 elsif ( $line =~ /^\s*config\s+/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600565 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
566 my $symbol = $1;
567 $inside_config = $symbol;
568 if ($inside_choice) {
569 $configs_inside_choice++;
570 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700571 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
Martin Rothbcaaad12015-10-18 11:16:25 -0600572 }
573
574 #bool|hex|int|string|tristate <expr> [if <expr>]
575 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
576 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
577 my ( $type, $prompt ) = ( $1, $2 );
578 handle_type( $type, $inside_config, $filename, $line_no );
579 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
580 }
581
582 # def_bool|def_tristate <expr> [if <expr>]
583 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
584 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
585 my ( $orgtype, $default ) = ( $1, $2 );
586 ( my $type = $orgtype ) =~ s/def_//;
587 handle_type( $type, $inside_config, $filename, $line_no );
588 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
589 }
590
591 #prompt <prompt> [if <expr>]
592 elsif ( $line =~ /^\s*prompt/ ) {
593 $line =~ /^\s*prompt\s+(.+)/;
594 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
595 }
596
597 # default <expr> [if <expr>]
598 elsif ( $line =~ /^\s*default/ ) {
599 $line =~ /^\s*default\s+(.*)/;
600 my $default = $1;
601 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
602 }
603
604 # depends on <expr>
605 elsif ( $line =~ /^\s*depends\s+on/ ) {
606 $line =~ /^\s*depends\s+on\s+(.*)$/;
607 my $expr = $1;
608 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
609 handle_expressions( $expr, $inside_config, $filename, $line_no );
610 }
611
612 # comment <prompt>
613 elsif ( $line =~ /^\s*comment/ ) {
614 $inside_config = "";
615 }
616
617 # choice [symbol]
618 elsif ( $line =~ /^\s*choice/ ) {
619 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
620 my $symbol = $1;
621 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
622 handle_type( "bool", $symbol, $filename, $line_no );
Patrick Georgicbc5b992018-11-23 15:55:56 +0100623 $choice_symbol = $symbol;
Martin Rothbcaaad12015-10-18 11:16:25 -0600624 }
625 $inside_config = "";
Michael Niewöhner70fb5cb2021-10-17 17:43:53 +0200626 $inside_choice = "$filename:$line_no";
Martin Rothbcaaad12015-10-18 11:16:25 -0600627 $configs_inside_choice = 0;
Martin Roth08cf90f2016-01-25 19:54:16 -0700628
629 # Kconfig verifies that choice blocks have a prompt
Martin Rothbcaaad12015-10-18 11:16:25 -0600630 }
631
632 # endchoice
633 elsif ( $line =~ /^\s*endchoice/ ) {
634 $inside_config = "";
635 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700636 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600637 }
638
639 $inside_choice = "";
Patrick Georgicbc5b992018-11-23 15:55:56 +0100640 if (( $configs_inside_choice == 0 ) &&
641 ( $choice_symbol eq "" )) {
642 show_error("unnamed choice block has no symbols at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600643 }
644 $configs_inside_choice = 0;
Patrick Georgicbc5b992018-11-23 15:55:56 +0100645 $choice_symbol="";
Martin Rothbcaaad12015-10-18 11:16:25 -0600646 }
647
648 # [optional]
649 elsif ( $line =~ /^\s*optional/ ) {
650 if ($inside_config) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700651 show_error( "Keyword 'optional' appears inside config for '$inside_config'"
652 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600653 }
654 if ( !$inside_choice ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700655 show_error( "Keyword 'optional' appears outside of a choice block"
656 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600657 }
658 }
659
660 # mainmenu <prompt>
661 elsif ( $line =~ /^\s*mainmenu/ ) {
662 $inside_config = "";
Martin Roth08cf90f2016-01-25 19:54:16 -0700663
664 # Kconfig alread checks for multiple 'mainmenu' entries and mainmenu entries with no prompt
665 # Possible check: look for 'mainmenu ""'
666 # Possible check: verify that a mainmenu has been specified
Martin Rothbcaaad12015-10-18 11:16:25 -0600667 }
668
669 # menu <prompt>
670 elsif ( $line =~ /^\s*menu/ ) {
671 $line =~ /^\s*menu\s+(.*)/;
672 my $menu = $1;
673 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
674 $menu = $1;
675 }
676
677 $inside_config = "";
678 $inside_choice = "";
679 push( @inside_menu, $menu );
680 }
681
Patrick Georgifcc29502018-09-16 21:35:46 +0200682 # visible if <expr>
683 elsif ( $line =~ /^\s*visible if.*$/ ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800684 # Must come directly after menu line (and on a separate line)
685 # but kconfig already checks for that.
686 # Ignore it.
Patrick Georgifcc29502018-09-16 21:35:46 +0200687 }
688
Martin Rothbcaaad12015-10-18 11:16:25 -0600689 # endmenu
690 elsif ( $line =~ /^\s*endmenu/ ) {
691 $inside_config = "";
692 $inside_choice = "";
693 pop @inside_menu;
694 }
695
696 # "if" <expr>
697 elsif ( $line =~ /^\s*if/ ) {
698 $inside_config = "";
699 $line =~ /^\s*if\s+(.*)$/;
700 my $expr = $1;
701 push( @inside_if, $expr );
702 handle_expressions( $expr, $inside_config, $filename, $line_no );
Martin Roth0e6c0e12016-03-02 12:16:13 -0700703 $fileinfo{$filename}{iflevel}++;
Martin Rothbcaaad12015-10-18 11:16:25 -0600704 }
705
706 # endif
707 elsif ( $line =~ /^\s*endif/ ) {
708 $inside_config = "";
709 pop(@inside_if);
Martin Roth0e6c0e12016-03-02 12:16:13 -0700710 $fileinfo{$filename}{iflevel}--;
Martin Rothbcaaad12015-10-18 11:16:25 -0600711 }
712
713 #range <symbol> <symbol> [if <expr>]
714 elsif ( $line =~ /^\s*range/ ) {
715 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
716 handle_range( $1, $2, $inside_config, $filename, $line_no );
717 }
718
719 # select <symbol> [if <expr>]
720 elsif ( $line =~ /^\s*select/ ) {
721 unless ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700722 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600723 }
724
725 if ( $line =~ /^\s*select\s+(.*)$/ ) {
726 $line = $1;
727 my $expression;
728 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
729 if ($line) {
Martin Rothb7c39b22016-01-14 09:04:53 -0700730 add_referenced_symbol( $line, $filename, $line_no, 'select' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600731 }
732 }
733 }
734
735 # source <prompt>
736 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
Martin Roth873ebf22023-08-10 10:00:51 -0600737 my $input_file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600738 $parseline[0]{text} = "# '$line'\n";
Martin Roth873ebf22023-08-10 10:00:51 -0600739 if ( $line !~ "site-local" || $include_site_local ) {
740 my @newfile = load_kconfig_file( $input_file, $filename, $line_no, 0, $filename, $line_no );
741 unshift( @config_to_parse, @newfile );
742 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600743 }
744 elsif (
745 ( $line =~ /^\s*#/ ) || #comments
746 ( $line =~ /^\s*$/ ) #blank lines
747 )
748 {
749 # do nothing
750 }
751 else {
Martin Roth811d93a2017-04-06 11:06:00 -0600752 if ($lastline_was_help) {
753 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized - supposed to be inside help?");
754 }
755 else {
756 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized");
757 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600758 }
759
Martin Roth819e6722016-01-25 16:38:05 -0700760 if ( defined $inside_menu[0] ) {
761 $parseline[0]{menus} = "";
762 }
763 else {
764 $parseline[0]{menus} = "top";
765 }
766
767 my $i = 0;
768 while ( defined $inside_menu[$i] ) {
769 $parseline[0]{menus} .= "$inside_menu[$i]";
770 $i++;
771 if ( defined $inside_menu[$i] ) {
772 $parseline[0]{menus} .= "->";
773 }
774 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600775 push @wholeconfig, @parseline;
776 }
Martin Roth0e6c0e12016-03-02 12:16:13 -0700777
778 foreach my $file ( keys %fileinfo ) {
779 if ( $fileinfo{$file}{iflevel} > 0 ) {
780 show_error("$file has $fileinfo{$file}{iflevel} more 'if' statement(s) than 'endif' statements.");
781 }
782 elsif ( $fileinfo{$file}{iflevel} < 0 ) {
783 show_error(
784 "$file has " . ( $fileinfo{$file}{iflevel} * -1 ) . " more 'endif' statement(s) than 'if' statements." );
785 }
786 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600787}
788
789#-------------------------------------------------------------------------------
790#-------------------------------------------------------------------------------
791sub handle_depends {
792 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
793
794 if ($inside_config) {
795 my $sym_num = $symbols{$inside_config}{count};
796 if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) {
797 $symbols{$inside_config}{$sym_num}{max_dependency}++;
798 }
799 else {
800 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
801 }
802
803 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
804 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
805 }
806}
807
808#-------------------------------------------------------------------------------
809#-------------------------------------------------------------------------------
810sub add_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700811 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600812 my @inside_if = @{$ifref};
813
814 #initialize the symbol or increment the use count.
Martin Roth1b44f7e2016-01-11 14:31:38 -0700815 if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600816 $symbols{$symbol}{count} = 0;
Michael Niewöhner43963582021-10-17 17:48:50 +0200817 # remember the location of the choice (or "")
818 $symbols{$symbol}{choice} = $inside_choice;
Martin Rothbcaaad12015-10-18 11:16:25 -0600819 }
820 else {
821 $symbols{$symbol}{count}++;
Nico Huber967730f2021-07-17 11:43:44 +0200822 if ( $inside_choice && $symbols{$symbol}{choice} ) {
Martin Rothaa206472017-03-24 08:51:51 -0600823 show_error( "$symbol entry at $filename:$line_no has already been created inside another choice block "
824 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
825 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600826 }
827
828 # add the location of this instance
829 my $symcount = $symbols{$symbol}{count};
830 $symbols{$symbol}{$symcount}{file} = $filename;
831 $symbols{$symbol}{$symcount}{line_no} = $line_no;
832
833 #Add the menu structure
834 if ( defined @$menu_array_ref[0] ) {
835 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
836 }
837
838 #Add any 'if' statements that the symbol is inside as dependencies
839 if (@inside_if) {
840 my $dep_num = 0;
841 for my $dependency (@inside_if) {
842 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
843 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
844 $dep_num++;
845 }
846 }
847}
848
849#-------------------------------------------------------------------------------
850# handle range
851#-------------------------------------------------------------------------------
852sub handle_range {
853 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
854
855 my $expression;
856 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
857
858 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
859 my $checkrange1 = $1;
860 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
861 my $checkrange2 = $1;
862
863 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700864 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 -0600865 }
866
867 if ($inside_config) {
868 if ( exists( $symbols{$inside_config}{range1} ) ) {
869 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700870 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700871 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700872 print " not match the previously defined range $symbols{$inside_config}{range1}"
873 . " $symbols{$inside_config}{range2}";
Martin Roth7aa3cea2015-11-27 18:45:45 -0700874 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600875 }
876 }
877 }
878 else {
879 $symbols{$inside_config}{range1} = $range1;
880 $symbols{$inside_config}{range2} = $range2;
881 $symbols{$inside_config}{range_file} = $filename;
882 $symbols{$inside_config}{range_line_no} = $line_no;
883 }
884 }
885 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700886 show_error("Range entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600887 }
888}
889
890#-------------------------------------------------------------------------------
891# handle_default
892#-------------------------------------------------------------------------------
893sub handle_default {
894 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
895 my $expression;
896 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
897
898 if ($inside_config) {
899 handle_expressions( $default, $inside_config, $filename, $line_no );
900 my $sym_num = $symbols{$inside_config}{count};
901
902 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
903 $symbols{$inside_config}{$sym_num}{default_max} = 0;
904 }
905 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
906 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
907 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
908 if ($expression) {
909 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
910 }
911 }
912 elsif ($inside_choice) {
913 handle_expressions( $default, $inside_config, $filename, $line_no );
914 }
915 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700916 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600917 }
918}
919
920#-------------------------------------------------------------------------------
921# handle_if_line
922#-------------------------------------------------------------------------------
923sub handle_if_line {
924 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
925
926 if ( $exprline !~ /if/ ) {
927 return ( $exprline, "" );
928 }
929
930 #remove any quotes that might have an 'if' in them
931 my $savequote;
932 if ( $exprline =~ /^\s*("[^"]+")/ ) {
933 $savequote = $1;
934 $exprline =~ s/^\s*("[^"]+")//;
935 }
936
937 my $expr = "";
938 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
939 $expr = $1;
940 $exprline =~ s/\s*if\s+.*$//;
941
942 if ($expr) {
943 handle_expressions( $expr, $inside_config, $filename, $line_no );
944 }
945 }
946
947 if ($savequote) {
948 $exprline = $savequote;
949 }
950
951 return ( $exprline, $expr );
952}
953
954#-------------------------------------------------------------------------------
Nico Huberf6b2baa2021-04-02 21:23:32 +0200955# handle_symbol - log which symbols are being used
956#-------------------------------------------------------------------------------
957sub handle_symbol {
958 my ( $symbol, $filename, $line_no ) = @_;
959
960 #filter constant symbols first
961 if ( $symbol =~ /^[yn]$/ ) { # constant y/n
962 return;
963 }
964 if ( $symbol =~ /^-?(?:0x)?\p{XDigit}+$/ ) { # int/hex values
965 return;
966 }
967 if ( $symbol =~ /^"[^"]*"$/ ) { # string values
968 return;
969 }
970
971 if ( $symbol =~ /^([A-Za-z0-9_]+)$/ ) { # actual symbol
972 add_referenced_symbol( $1, $filename, $line_no, 'expression' );
973 }
974 else {
975 show_error("Unrecognized expression: expected symbol, "
976 . "found '$symbol' in $filename line $line_no.");
977 }
978}
979
980#-------------------------------------------------------------------------------
981# handle_expressions - find symbols in expressions
Martin Rothbcaaad12015-10-18 11:16:25 -0600982#-------------------------------------------------------------------------------
983sub handle_expressions {
984 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
985
Nico Huberf6b2baa2021-04-02 21:23:32 +0200986 my $strip = qr/\s*(.*[^\s]+)\s*/;
Martin Rothbcaaad12015-10-18 11:16:25 -0600987
Nico Huberf6b2baa2021-04-02 21:23:32 +0200988 my $parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
989 my $quotes = qr/"[^"]*"/;
990 my $balanced = qr/((?:$parens|$quotes|[^\(\)"])+)/;
Martin Rothbcaaad12015-10-18 11:16:25 -0600991
Nico Huberf6b2baa2021-04-02 21:23:32 +0200992 if ( $exprline =~ /^\s*$balanced\s*(?:\|\||&&)\s*(.+)$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +0200993 # <expr> '||' <expr>, <expr> '&&' <expr> (8)(7)
Nico Huberf6b2baa2021-04-02 21:23:32 +0200994 my ( $lhs, $rhs ) = ( $1, $3 );
995 handle_expressions( $lhs, $inside_config, $filename, $line_no );
996 handle_expressions( $rhs, $inside_config, $filename, $line_no );
997 }
998 elsif ( $exprline =~ /^\s*!(.+)$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +0200999 # '!' <expr> (6)
Martin Rothbcaaad12015-10-18 11:16:25 -06001000 handle_expressions( $1, $inside_config, $filename, $line_no );
1001 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001002 elsif ( $exprline =~ /^\s*$parens\s*$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +02001003 # '(' <expr> ')' (5)
Nico Huberf6b2baa2021-04-02 21:23:32 +02001004 $exprline =~ /^\s*\((.*)\)\s*$/;
Martin Rothbcaaad12015-10-18 11:16:25 -06001005 handle_expressions( $1, $inside_config, $filename, $line_no );
1006 }
Nico Huber2d821952021-04-02 21:56:45 +02001007 elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*(?:[<>]=?|!=)$strip$/ ) {
1008 # <symbol> '<' <symbol>, <symbol> '!=' <symbol>, etc. (4)(3)
Nico Huberf6b2baa2021-04-02 21:23:32 +02001009 my ( $lhs, $rhs ) = ( $1, $2 );
1010 handle_symbol( $lhs, $filename, $line_no );
1011 handle_symbol( $rhs, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001012 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001013 elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*=$strip$/ ) {
1014 # <symbol> '=' <symbol> (2)
1015 my ( $lhs, $rhs ) = ( $1, $2 );
1016 handle_symbol( $lhs, $filename, $line_no );
1017 handle_symbol( $rhs, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001018 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001019 elsif ( $exprline =~ /^$strip$/ ) {
1020 # <symbol> (1)
1021 handle_symbol( $1, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001022 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001023}
1024
1025#-------------------------------------------------------------------------------
1026# add_referenced_symbol
1027#-------------------------------------------------------------------------------
1028sub add_referenced_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -07001029 my ( $symbol, $filename, $line_no, $reftype ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -06001030 if ( exists $referenced_symbols{$symbol} ) {
1031 $referenced_symbols{$symbol}{count}++;
1032 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
1033 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
1034 }
1035 else {
1036 $referenced_symbols{$symbol}{count} = 0;
1037 $referenced_symbols{$symbol}{0}{filename} = $filename;
1038 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
1039 }
Martin Rothb7c39b22016-01-14 09:04:53 -07001040
1041 #mark the symbol as being selected, use referenced symbols for location
1042 if ( $reftype eq 'select' ) {
1043 $selected_symbols{$symbol}{ $referenced_symbols{$symbol}{count} } = 1;
1044 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001045}
1046
1047#-------------------------------------------------------------------------------
1048# handle_help
1049#-------------------------------------------------------------------------------
1050{
1051 #create a non-global static variable by enclosing it and the subroutine
1052 my $help_whitespace = ""; #string to show length of the help whitespace
Martin Roth8849f3b2017-05-23 19:43:43 -06001053 my $help_keyword_whitespace = "";
Martin Rothbcaaad12015-10-18 11:16:25 -06001054
1055 sub handle_help {
1056 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1057
1058 if ($inside_help) {
1059
1060 #get the indentation level if it's not already set.
1061 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
1062 $line =~ /^(\s+)/; #find the indentation level.
1063 $help_whitespace = $1;
1064 if ( !$help_whitespace ) {
Martin Roth8849f3b2017-05-23 19:43:43 -06001065 show_error("$filename:$line_no - help text starts with no whitespace.");
1066 return $inside_help;
1067 }
1068 elsif ($help_keyword_whitespace eq $help_whitespace) {
1069 show_error("$filename:$line_no - help text needs additional indentation.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001070 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -06001071 }
1072 }
1073
1074 #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 -06001075 if ( ( $line !~ /^$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001076 $inside_help = 0;
1077 $help_whitespace = "";
Martin Roth8849f3b2017-05-23 19:43:43 -06001078 $help_keyword_whitespace = "";
Martin Rothbcaaad12015-10-18 11:16:25 -06001079 }
1080 else { #if it's not ended, add the line to the helptext array for the symbol's instance
1081 if ($inside_config) {
1082 my $sym_num = $symbols{$inside_config}{count};
1083 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
1084 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
1085 }
Martin Roth8849f3b2017-05-23 19:43:43 -06001086 if ( ($help_keyword_whitespace eq $help_whitespace) && ( $line !~ /^[\r\n]+/ ) ) {
1087 show_error("$filename:$line_no - help text needs additional indentation.");
1088 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001089 }
1090 }
1091 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
1092 $inside_help = $line_no;
Martin Roth8849f3b2017-05-23 19:43:43 -06001093 $line =~ /^(\s+)/;
1094 $help_keyword_whitespace = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001095 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001096 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001097 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001098 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001099 }
1100 elsif ($inside_config) {
1101 $help_whitespace = "";
1102 my $sym_num = $symbols{$inside_config}{count};
1103 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
1104 $symbols{$inside_config}{$sym_num}{helptext} = ();
1105 }
1106 }
1107 return $inside_help;
1108 }
1109}
1110
1111#-------------------------------------------------------------------------------
1112# handle_type
1113#-------------------------------------------------------------------------------
1114sub handle_type {
1115 my ( $type, $inside_config, $filename, $line_no ) = @_;
1116
1117 my $expression;
1118 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
1119
Martin Roth08ee1cf2016-01-25 16:39:32 -07001120 if ( $type =~ /tristate/ ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -08001121 show_error("$filename:$line_no - tristate types are not used.");
Martin Roth08ee1cf2016-01-25 16:39:32 -07001122 }
1123
Martin Rothbcaaad12015-10-18 11:16:25 -06001124 if ($inside_config) {
1125 if ( exists( $symbols{$inside_config}{type} ) ) {
1126 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001127 show_error( "Config '$inside_config' type entry $type"
1128 . " at $filename:$line_no does not match $symbols{$inside_config}{type}"
1129 . " defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001130 }
1131 }
1132 else {
1133 $symbols{$inside_config}{type} = $type;
1134 $symbols{$inside_config}{type_file} = $filename;
1135 $symbols{$inside_config}{type_line_no} = $line_no;
1136 }
1137 }
1138 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001139 show_error("Type entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001140 }
1141}
1142
1143#-------------------------------------------------------------------------------
1144# handle_prompt
1145#-------------------------------------------------------------------------------
1146sub handle_prompt {
1147 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1148
1149 my $expression;
1150 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
1151
1152 if ($inside_config) {
1153 if ( $prompt !~ /^\s*$/ ) {
1154 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
1155 $prompt = $1;
1156 }
1157
Martin Roth08cf90f2016-01-25 19:54:16 -07001158 #display an error if there's a prompt at the top menu level
Martin Rothbcaaad12015-10-18 11:16:25 -06001159 if ( !defined @$menu_array_ref[0] ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001160 show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu"
1161 . " at $filename:$line_no." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001162 }
1163
1164 my $sym_num = $symbols{$inside_config}{count};
Martin Rothb58d3492016-01-25 16:42:13 -07001165 if ( !exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001166 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
1167 }
Martin Rothb58d3492016-01-25 16:42:13 -07001168 else {
1169 $symbols{$inside_config}{$sym_num}{prompt_max}++;
1170 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001171 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
1172 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
1173 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
Martin Rothb58d3492016-01-25 16:42:13 -07001174
1175 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_menu} = @$menu_array_ref;
Martin Rothbcaaad12015-10-18 11:16:25 -06001176 if ($expression) {
1177 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
1178 }
1179 }
1180 }
1181 elsif ($inside_choice) {
1182
1183 #do nothing
1184 }
1185 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001186 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001187 }
1188}
1189
1190#-------------------------------------------------------------------------------
1191# simple_line_checks - Does some basic checks on the current line, then cleans the line
1192# up for further processing.
1193#-------------------------------------------------------------------------------
1194sub simple_line_checks {
1195 my ( $line, $filename, $line_no ) = @_;
1196
1197 #check for spaces instead of tabs
1198 if ( $line =~ /^ +/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001199 show_error("$filename:$line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001200 }
1201
1202 #verify a linefeed at the end of the line
1203 if ( $line !~ /.*\n/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001204 show_error( "$filename:$line_no does not end with linefeed."
1205 . " This can cause the line to not be recognized by the Kconfig parser.\n#($line)" );
Martin Rothbcaaad12015-10-18 11:16:25 -06001206 $line =~ s/\s*$//;
1207 }
1208 else {
1209 chop($line);
1210 }
1211
1212 return $line;
1213}
1214
1215#-------------------------------------------------------------------------------
1216# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
1217#-------------------------------------------------------------------------------
1218sub load_kconfig_file {
1219 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
1220 my @file_data;
1221 my @dir_file_data;
1222
1223 #recursively handle coreboot's new source glob operator
Arthur Heymans55f01322019-11-05 12:06:59 +01001224 if ( $input_file =~ /^(.*?)\/(\w*)\*(\w*)\/(.*)$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001225 my $dir_prefix = $1;
Arthur Heymans55f01322019-11-05 12:06:59 +01001226 my $dir_glob_prefix = $2;
1227 my $dir_glob_suffix = $3;
1228 my $dir_suffix = $4;
Martin Rothbcaaad12015-10-18 11:16:25 -06001229 if ( -d "$dir_prefix" ) {
1230
1231 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
1232 my @dirlist = sort { $a cmp $b } readdir(D);
1233 closedir(D);
1234
1235 while ( my $directory = shift @dirlist ) {
1236
1237 #ignore non-directory files
Arthur Heymans55f01322019-11-05 12:06:59 +01001238 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ )
1239 && ( $directory =~ /\Q$dir_glob_prefix\E.*\Q$dir_glob_suffix\E/ ) ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001240 push @dir_file_data,
1241 load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1242 $input_file, $loadline, 1, $loadfile, $loadline );
Martin Rothbcaaad12015-10-18 11:16:25 -06001243 }
1244 }
1245 }
1246
1247 #the directory should exist when using a glob
1248 else {
Julius Wernere5eb2de2019-03-05 17:10:19 -08001249 show_error("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -06001250 }
1251 }
1252
1253 #if the file exists, try to load it.
1254 elsif ( -e "$input_file" ) {
1255
Martin Rotha7d00272016-10-03 23:00:04 +02001256 #throw a warning if the file has already been loaded.
Martin Rothbcaaad12015-10-18 11:16:25 -06001257 if ( exists $loaded_files{$input_file} ) {
Martin Rotha7d00272016-10-03 23:00:04 +02001258 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -06001259 }
1260
1261 #load the file's contents and mark the file as loaded for checking later
1262 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
1263 @file_data = <$HANDLE>;
1264 close $HANDLE;
1265 $loaded_files{$input_file} = "'$loadfile' line $loadline";
1266 }
1267
1268 # if the file isn't being loaded from a glob, it should exist.
1269 elsif ( $expanded == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001270 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -06001271 }
1272
1273 my $line_in_file = 0;
1274 while ( my $line = shift @file_data ) {
1275
1276 #handle line continuation.
1277 my $continue_line = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001278 while ( $line =~ /(.*)\s+\\$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001279 my $text = $1;
1280
1281 # get rid of leading whitespace on all but the first and last lines
1282 $text =~ s/^\s*/ / if ($continue_line);
1283
1284 $dir_file_data[$line_in_file]{text} .= $text;
1285 $line = shift @file_data;
1286 $continue_line++;
1287
1288 #put the data into the continued lines (other than the first)
1289 $line =~ /^\s*(.*)\s*$/;
1290
Martin Roth1b44f7e2016-01-11 14:31:38 -07001291 $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n";
1292 $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file;
1293 $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001294
1295 #get rid of multiple leading spaces for last line
1296 $line = " $1\n";
1297 }
1298
Martin Roth1b44f7e2016-01-11 14:31:38 -07001299 $dir_file_data[$line_in_file]{text} .= $line;
Martin Rothbcaaad12015-10-18 11:16:25 -06001300 $dir_file_data[$line_in_file]{filename} = $input_file;
1301 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1302
1303 $line_in_file++;
1304 if ($continue_line) {
1305 $line_in_file += $continue_line;
1306 }
1307 }
1308
1309 if ($topfile) {
1310 my %file_data;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001311 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001312 $file_data{filename} = $topfile;
1313 $file_data{file_line_no} = "($topline)";
Martin Roth1b44f7e2016-01-11 14:31:38 -07001314 unshift( @dir_file_data, \%file_data );
Martin Rothbcaaad12015-10-18 11:16:25 -06001315 }
1316
1317 return @dir_file_data;
1318}
1319
Martin Rothbcaaad12015-10-18 11:16:25 -06001320#-------------------------------------------------------------------------------
1321# print_wholeconfig - prints out the parsed Kconfig file
1322#-------------------------------------------------------------------------------
1323sub print_wholeconfig {
1324
1325 return unless $print_full_output;
1326
Martin Rotha7648f22021-11-05 17:24:29 -06001327 for ( my $i = 0 ; $i <= $#wholeconfig ; $i++ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001328 my $line = $wholeconfig[$i];
1329 chop( $line->{text} );
1330
1331 #replace tabs with spaces for consistency
1332 $line->{text} =~ s/\t/ /g;
Martin Roth819e6722016-01-25 16:38:05 -07001333 printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text};
Martin Rothbcaaad12015-10-18 11:16:25 -06001334 }
1335}
1336
1337#-------------------------------------------------------------------------------
1338# check_if_file_referenced - checks for kconfig files that are not being parsed
1339#-------------------------------------------------------------------------------
1340sub check_if_file_referenced {
1341 my $filename = $File::Find::name;
Martin Rothab2d7772016-01-25 16:45:14 -07001342 if ( ( $filename =~ /Kconfig/ )
1343 && ( !$filename =~ /\.orig$/ )
1344 && ( !$filename =~ /~$/ )
1345 && ( !exists $loaded_files{$filename} ) )
1346 {
Martin Rothd8080172015-11-26 19:12:44 -07001347 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001348 }
1349}
1350
1351#-------------------------------------------------------------------------------
1352# check_arguments parse the command line arguments
1353#-------------------------------------------------------------------------------
1354sub check_arguments {
1355 my $show_usage = 0;
1356 GetOptions(
1357 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001358 'e|errors_off' => \$suppress_error_output,
1359 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001360 'o|output=s' => \$output_file,
1361 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001362 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001363 'path=s' => \$top_dir,
1364 'c|config=s' => \$config_file,
1365 'G|no_git_grep' => \$dont_use_git_grep,
Martin Roth873ebf22023-08-10 10:00:51 -06001366 'S|site_local' => \$include_site_local,
Martin Rothbcaaad12015-10-18 11:16:25 -06001367 );
Martin Rothd8080172015-11-26 19:12:44 -07001368
1369 if ($suppress_error_output) {
1370 $suppress_warning_output = 1;
1371 }
1372 if ($suppress_warning_output) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001373 $show_note_output = 0;
Martin Rothd8080172015-11-26 19:12:44 -07001374 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001375}
1376
1377#-------------------------------------------------------------------------------
1378# usage - Print the arguments for the user
1379#-------------------------------------------------------------------------------
1380sub usage {
1381 print "kconfig_lint <options>\n";
1382 print " -o|--output=file Set output filename\n";
1383 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001384 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001385 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001386 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001387 print " --path=dir Path to top level kconfig\n";
1388 print " -c|--config=file Filename of config file to load\n";
1389 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
Martin Roth873ebf22023-08-10 10:00:51 -06001390 print " -S|--site_local Include the site-local directory\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001391
1392 exit(0);
1393}
1394
13951;