blob: 523fa3e9eb1926d9ce231bc67dac1fbc3bbcfe23 [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 }
Martin Rothc7139f92023-10-27 11:03:48 -0600294 } elsif ($symbol =~ /\S+##\S+/ ) {
295 show_warning( "C preprocessor macro CONFIG_$symbol found at $file:$lineno." );
296 } else {
Julius Wernerf0286042019-04-09 15:57:07 -0700297 show_error("CONFIG() used on unknown value ($symbol) at $file:$lineno.");
Julius Werneref7a3262019-03-05 16:57:52 -0800298 }
299 }
300 } elsif ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700301 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600302 my $lineno = $2;
Martin Rothb6acc302015-11-27 18:51:19 -0700303 $line = $3;
Martin Roth572a8562016-01-25 16:14:09 -0700304 if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) {
Nico Huberec017592019-04-06 16:09:46 +0200305 show_error("# uninterpreted IS_ENABLED at $file:$lineno: $line");
Martin Rothb6acc302015-11-27 18:51:19 -0700306 next;
Martin Rothbcaaad12015-10-18 11:16:25 -0600307 }
Martin Rothb6acc302015-11-27 18:51:19 -0700308 while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
309 my $symbol = $2;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700310 $line = $1 . $3;
Nico Huberec017592019-04-06 16:09:46 +0200311 show_error("IS_ENABLED(CONFIG_$symbol) at $file:$lineno is deprecated. Use CONFIG($symbol) instead.");
Martin Rothb6acc302015-11-27 18:51:19 -0700312 }
Martin Roth5b883012017-06-25 20:27:36 -0600313 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?.*?$/ ) {
314 my $file = $1;
315 my $lineno = $2;
316 my $symbol = $3;
Nico Huberec017592019-04-06 16:09:46 +0200317 # If the type is bool, give a warning that CONFIG() should be used
Martin Roth5b883012017-06-25 20:27:36 -0600318 if ( exists $symbols{$symbol} ) {
319 if ( $symbols{$symbol}{type} eq "bool" ) {
320 show_error( "#if CONFIG_$symbol used at $file:$lineno."
Julius Werneref7a3262019-03-05 16:57:52 -0800321 . " CONFIG($symbol) should be used for type 'bool'" );
Martin Roth5b883012017-06-25 20:27:36 -0600322 }
323 }
Julius Werneref7a3262019-03-05 16:57:52 -0800324 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if.*(?:&&|\|\|)\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?$/ ) {
Martin Roth5b883012017-06-25 20:27:36 -0600325 my $file = $1;
326 my $lineno = $2;
327 my $symbol = $3;
Nico Huberec017592019-04-06 16:09:46 +0200328 # If the type is bool, give a warning that CONFIG() should be used
Martin Roth5b883012017-06-25 20:27:36 -0600329 if ( exists $symbols{$symbol} ) {
330 if ( $symbols{$symbol}{type} eq "bool" ) {
331 show_error( "#if CONFIG_$symbol used at $file:$lineno."
Julius Werneref7a3262019-03-05 16:57:52 -0800332 . " CONFIG($symbol) should be used for type 'bool'" );
Martin Roth5b883012017-06-25 20:27:36 -0600333 }
334 }
Julius Wernere5eb2de2019-03-05 17:10:19 -0800335 } elsif ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG_.+)/ ) {
336 my $file = $1;
337 my $lineno = $2;
338 $line = $3;
339 if ( $file =~ /.*\.(c|h|asl|ld)/ ) {
340 while ( $line =~ /(.*)\bCONFIG_(\w+)(.*)/ && $1 !~ /\/\/|\/\*/ ) {
341 my $symbol = $2;
342 $line = $1 . $3;
343 if ( exists $symbols{$symbol} ) {
344 if ( $symbols{$symbol}{type} eq "bool" ) {
Martin Rotha0209032020-07-24 12:42:59 -0600345 show_error( "Naked reference to CONFIG_$symbol used at $file:$lineno."
Julius Wernere5eb2de2019-03-05 17:10:19 -0800346 . " A 'bool' Kconfig should always be accessed through CONFIG($symbol)." );
347 }
348 } else {
349 show_warning( "Unknown config option CONFIG_$symbol used at $file:$lineno." );
350 }
351 }
352 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600353 }
354 }
355}
356
357#-------------------------------------------------------------------------------
358# check_defaults - Look for defaults that come after a default with no
359# dependencies.
360#
361# TODO - check for defaults with the same dependencies
362#-------------------------------------------------------------------------------
363sub check_defaults {
364
365 # loop through each defined symbol
366 foreach my $sym ( sort ( keys %symbols ) ) {
367 my $default_set = 0;
368 my $default_filename = "";
369 my $default_line_no = "";
370
371 #loop through each instance of that symbol
372 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
373
374 #loop through any defaults for that instance of that symbol, if there are any
375 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
376 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
377
Martin Rothfa956252016-09-30 15:51:32 -0600378 my $filename = $symbols{$sym}{$sym_num}{file};
379 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
380
Martin Roth08705f12016-11-09 14:27:00 -0700381 # Make sure there's a type set for the symbol
382 next if (!defined $symbols{$sym}{type});
383
Michael Niewöhner90fcffb2021-09-16 17:56:14 +0200384 # Symbols created/used inside a choice must not have a default set. The default is set by the choice itself.
385 if ($symbols{$sym}{choice}) {
386 show_error("Defining a default for symbol '$sym' at $filename:$line_no, used inside choice at "
Michael Niewöhner43963582021-10-17 17:48:50 +0200387 . "$symbols{$sym}{choice}, is not allowed.");
Michael Niewöhner90fcffb2021-09-16 17:56:14 +0200388 }
389
Martin Rothfa956252016-09-30 15:51:32 -0600390 # skip good defaults
391 if (! ((($symbols{$sym}{type} eq "hex") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^0x/)) ||
392 (($symbols{$sym}{type} eq "int") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[-0-9]+$/)) ||
393 (($symbols{$sym}{type} eq "string") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^".*"$/)) ||
394 (($symbols{$sym}{type} eq "bool") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[yn]$/)))
395 ) {
396
397 my ($checksym) = $symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /(\w+)/;
398
399 if (! exists $symbols{$checksym}) {
400
401 # verify the symbol type against the default value
402 if ($symbols{$sym}{type} eq "hex") {
403 show_error("non hex default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for hex symbol $sym at $filename:$line_no.");
404 } elsif ($symbols{$sym}{type} eq "int") {
405 show_error("non int default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for int symbol $sym at $filename:$line_no.");
406 } elsif ($symbols{$sym}{type} eq "string") {
407 # TODO: Remove special MAINBOARD_DIR check
408 if ($sym ne "MAINBOARD_DIR") {
409 show_error("no quotes around default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for string symbol $sym at $filename:$line_no.");
410 }
411 } elsif ($symbols{$sym}{type} eq "bool") {
412 if ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /[01YN]/) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800413 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 -0600414 } else {
415 show_error("non bool default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for bool symbol $sym at $filename:$line_no.");
416 }
417 }
418 }
419 }
420
Martin Rothbcaaad12015-10-18 11:16:25 -0600421 #if a default is already set, display an error
422 if ($default_set) {
Julius Wernere5eb2de2019-03-05 17:10:19 -0800423 show_error( "Default for '$sym' referenced at $filename:$line_no will never be set"
Martin Roth1b44f7e2016-01-11 14:31:38 -0700424 . " - overridden by default set at $default_filename:$default_line_no" );
Martin Rothbcaaad12015-10-18 11:16:25 -0600425 }
426 else {
427 #if no default is set, see if this is a default with no dependencies
428 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
429 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
430 {
431 $default_set = 1;
432 $default_filename = $symbols{$sym}{$sym_num}{file};
433 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
434 }
435 }
436 }
437 }
438 }
439}
440
441#-------------------------------------------------------------------------------
442# check_referenced_symbols - Make sure the symbols referenced by expressions and
443# select statements are actually valid symbols.
444#-------------------------------------------------------------------------------
445sub check_referenced_symbols {
446
447 #loop through symbols found in expressions and used by 'select' keywords
448 foreach my $key ( sort ( keys %referenced_symbols ) ) {
449
450 #make sure the symbol was defined by a 'config' or 'choice' keyword
451 next if ( exists $symbols{$key} );
452
Martin Rothd8080172015-11-26 19:12:44 -0700453 #loop through each instance of the symbol to print out all of the invalid references
454 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
455 my $filename = $referenced_symbols{$key}{$i}{filename};
456 my $line_no = $referenced_symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700457 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600458 }
459 }
460}
461
462#-------------------------------------------------------------------------------
463#-------------------------------------------------------------------------------
464sub collect_used_symbols {
465 # find all references to CONFIG_ statements in the tree
466
467 if ($dont_use_git_grep) {
Julius Werneref7a3262019-03-05 16:57:52 -0800468 @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 -0700469 }
470 else {
Julius Werneref7a3262019-03-05 16:57:52 -0800471 @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 -0600472 }
473
474 my @used_symbols = @collected_symbols;
475
476 #sort through symbols found by grep and store them in a hash for easy access
477 while ( my $line = shift @used_symbols ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800478 while ( $line =~ /[^A-Za-z0-9_]CONFIG(?:_|\()([A-Za-z0-9_]+)/g ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700479 my $symbol = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600480 my $filename = "";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700481 if ( $line =~ /^([^:]+):/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600482 $filename = $1;
483 }
484
Martin Roth1b44f7e2016-01-11 14:31:38 -0700485 if ( exists $used_symbols{$symbol}{count} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600486 $used_symbols{$symbol}{count}++;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700487 }
488 else {
489 $used_symbols{$symbol}{count} = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -0600490 }
491 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700492 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600493 }
494}
495
496#-------------------------------------------------------------------------------
497# check_used_symbols - Checks to see whether or not the created symbols are
498# actually used.
499#-------------------------------------------------------------------------------
500sub check_used_symbols {
501 # loop through all defined symbols and see if they're used anywhere
502 foreach my $key ( sort ( keys %symbols ) ) {
503
Martin Roth6bfbf1c2016-01-25 16:12:49 -0700504 if ( $key =~ /$exclude_unused/ ) {
505 next;
506 }
507
Martin Rothbcaaad12015-10-18 11:16:25 -0600508 #see if they're used internal to Kconfig
509 next if ( exists $referenced_symbols{$key} );
510
511 #see if they're used externally
512 next if exists $used_symbols{$key};
513
514 #loop through the definitions to print out all the places the symbol is defined.
515 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
516 my $filename = $symbols{$key}{$i}{file};
517 my $line_no = $symbols{$key}{$i}{line_no};
Martin Rotha7d00272016-10-03 23:00:04 +0200518 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600519 }
520 }
521}
522
523#-------------------------------------------------------------------------------
524# build_and_parse_kconfig_tree
525#-------------------------------------------------------------------------------
526#load the initial file and start parsing it
527sub build_and_parse_kconfig_tree {
528 my ($top_level_kconfig) = @_;
529 my @config_to_parse;
530 my @parseline;
531 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
532 my @inside_if = (); # stack of if dependencies
533 my $inside_config = ""; # set to symbol name of the config section
534 my @inside_menu = (); # stack of menu names
535 my $inside_choice = "";
Patrick Georgicbc5b992018-11-23 15:55:56 +0100536 my $choice_symbol = "";
Martin Rothbcaaad12015-10-18 11:16:25 -0600537 my $configs_inside_choice;
Martin Roth0e6c0e12016-03-02 12:16:13 -0700538 my %fileinfo;
Martin Rothbcaaad12015-10-18 11:16:25 -0600539
540 #start the tree off by loading the top level kconfig
541 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
542
Martin Rothbcaaad12015-10-18 11:16:25 -0600543 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
544 my $line = $parseline[0]{text};
545 my $filename = $parseline[0]{filename};
546 my $line_no = $parseline[0]{file_line_no};
547
548 #handle help - help text: "help" or "---help---"
Martin Roth811d93a2017-04-06 11:06:00 -0600549 my $lastline_was_help = $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -0600550 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
551 $parseline[0]{inside_help} = $inside_help;
552
553 #look for basic issues in the line, strip crlf
554 $line = simple_line_checks( $line, $filename, $line_no );
555
556 #strip comments
557 $line =~ s/\s*#.*$//;
558
559 #don't parse any more if we're inside a help block
560 if ($inside_help) {
561 #do nothing
562 }
563
564 #handle config
Martin Roth811d93a2017-04-06 11:06:00 -0600565 elsif ( $line =~ /^\s*config\s+/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600566 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
567 my $symbol = $1;
568 $inside_config = $symbol;
569 if ($inside_choice) {
570 $configs_inside_choice++;
571 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700572 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
Martin Rothbcaaad12015-10-18 11:16:25 -0600573 }
574
575 #bool|hex|int|string|tristate <expr> [if <expr>]
576 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
577 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
578 my ( $type, $prompt ) = ( $1, $2 );
579 handle_type( $type, $inside_config, $filename, $line_no );
580 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
581 }
582
583 # def_bool|def_tristate <expr> [if <expr>]
584 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
585 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
586 my ( $orgtype, $default ) = ( $1, $2 );
587 ( my $type = $orgtype ) =~ s/def_//;
588 handle_type( $type, $inside_config, $filename, $line_no );
589 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
590 }
591
592 #prompt <prompt> [if <expr>]
593 elsif ( $line =~ /^\s*prompt/ ) {
594 $line =~ /^\s*prompt\s+(.+)/;
595 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
596 }
597
598 # default <expr> [if <expr>]
599 elsif ( $line =~ /^\s*default/ ) {
600 $line =~ /^\s*default\s+(.*)/;
601 my $default = $1;
602 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
603 }
604
605 # depends on <expr>
606 elsif ( $line =~ /^\s*depends\s+on/ ) {
607 $line =~ /^\s*depends\s+on\s+(.*)$/;
608 my $expr = $1;
609 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
610 handle_expressions( $expr, $inside_config, $filename, $line_no );
611 }
612
613 # comment <prompt>
614 elsif ( $line =~ /^\s*comment/ ) {
615 $inside_config = "";
616 }
617
618 # choice [symbol]
619 elsif ( $line =~ /^\s*choice/ ) {
620 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
621 my $symbol = $1;
622 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
623 handle_type( "bool", $symbol, $filename, $line_no );
Patrick Georgicbc5b992018-11-23 15:55:56 +0100624 $choice_symbol = $symbol;
Martin Rothbcaaad12015-10-18 11:16:25 -0600625 }
626 $inside_config = "";
Michael Niewöhner70fb5cb2021-10-17 17:43:53 +0200627 $inside_choice = "$filename:$line_no";
Martin Rothbcaaad12015-10-18 11:16:25 -0600628 $configs_inside_choice = 0;
Martin Roth08cf90f2016-01-25 19:54:16 -0700629
630 # Kconfig verifies that choice blocks have a prompt
Martin Rothbcaaad12015-10-18 11:16:25 -0600631 }
632
633 # endchoice
634 elsif ( $line =~ /^\s*endchoice/ ) {
635 $inside_config = "";
636 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700637 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600638 }
639
640 $inside_choice = "";
Patrick Georgicbc5b992018-11-23 15:55:56 +0100641 if (( $configs_inside_choice == 0 ) &&
642 ( $choice_symbol eq "" )) {
643 show_error("unnamed choice block has no symbols at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600644 }
645 $configs_inside_choice = 0;
Patrick Georgicbc5b992018-11-23 15:55:56 +0100646 $choice_symbol="";
Martin Rothbcaaad12015-10-18 11:16:25 -0600647 }
648
649 # [optional]
650 elsif ( $line =~ /^\s*optional/ ) {
651 if ($inside_config) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700652 show_error( "Keyword 'optional' appears inside config for '$inside_config'"
653 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600654 }
655 if ( !$inside_choice ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700656 show_error( "Keyword 'optional' appears outside of a choice block"
657 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600658 }
659 }
660
661 # mainmenu <prompt>
662 elsif ( $line =~ /^\s*mainmenu/ ) {
663 $inside_config = "";
Martin Roth08cf90f2016-01-25 19:54:16 -0700664
665 # Kconfig alread checks for multiple 'mainmenu' entries and mainmenu entries with no prompt
666 # Possible check: look for 'mainmenu ""'
667 # Possible check: verify that a mainmenu has been specified
Martin Rothbcaaad12015-10-18 11:16:25 -0600668 }
669
670 # menu <prompt>
671 elsif ( $line =~ /^\s*menu/ ) {
672 $line =~ /^\s*menu\s+(.*)/;
673 my $menu = $1;
674 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
675 $menu = $1;
676 }
677
678 $inside_config = "";
679 $inside_choice = "";
680 push( @inside_menu, $menu );
681 }
682
Patrick Georgifcc29502018-09-16 21:35:46 +0200683 # visible if <expr>
684 elsif ( $line =~ /^\s*visible if.*$/ ) {
Julius Werneref7a3262019-03-05 16:57:52 -0800685 # Must come directly after menu line (and on a separate line)
686 # but kconfig already checks for that.
687 # Ignore it.
Patrick Georgifcc29502018-09-16 21:35:46 +0200688 }
689
Martin Rothbcaaad12015-10-18 11:16:25 -0600690 # endmenu
691 elsif ( $line =~ /^\s*endmenu/ ) {
692 $inside_config = "";
693 $inside_choice = "";
694 pop @inside_menu;
695 }
696
697 # "if" <expr>
698 elsif ( $line =~ /^\s*if/ ) {
699 $inside_config = "";
700 $line =~ /^\s*if\s+(.*)$/;
701 my $expr = $1;
702 push( @inside_if, $expr );
703 handle_expressions( $expr, $inside_config, $filename, $line_no );
Martin Roth0e6c0e12016-03-02 12:16:13 -0700704 $fileinfo{$filename}{iflevel}++;
Martin Rothbcaaad12015-10-18 11:16:25 -0600705 }
706
707 # endif
708 elsif ( $line =~ /^\s*endif/ ) {
709 $inside_config = "";
710 pop(@inside_if);
Martin Roth0e6c0e12016-03-02 12:16:13 -0700711 $fileinfo{$filename}{iflevel}--;
Martin Rothbcaaad12015-10-18 11:16:25 -0600712 }
713
714 #range <symbol> <symbol> [if <expr>]
715 elsif ( $line =~ /^\s*range/ ) {
716 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
717 handle_range( $1, $2, $inside_config, $filename, $line_no );
718 }
719
720 # select <symbol> [if <expr>]
721 elsif ( $line =~ /^\s*select/ ) {
722 unless ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700723 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600724 }
725
726 if ( $line =~ /^\s*select\s+(.*)$/ ) {
727 $line = $1;
728 my $expression;
729 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
730 if ($line) {
Martin Rothb7c39b22016-01-14 09:04:53 -0700731 add_referenced_symbol( $line, $filename, $line_no, 'select' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600732 }
733 }
734 }
735
736 # source <prompt>
737 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
Martin Roth873ebf22023-08-10 10:00:51 -0600738 my $input_file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600739 $parseline[0]{text} = "# '$line'\n";
Martin Roth873ebf22023-08-10 10:00:51 -0600740 if ( $line !~ "site-local" || $include_site_local ) {
741 my @newfile = load_kconfig_file( $input_file, $filename, $line_no, 0, $filename, $line_no );
742 unshift( @config_to_parse, @newfile );
743 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600744 }
745 elsif (
746 ( $line =~ /^\s*#/ ) || #comments
747 ( $line =~ /^\s*$/ ) #blank lines
748 )
749 {
750 # do nothing
751 }
752 else {
Martin Roth811d93a2017-04-06 11:06:00 -0600753 if ($lastline_was_help) {
754 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized - supposed to be inside help?");
755 }
756 else {
757 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized");
758 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600759 }
760
Martin Roth819e6722016-01-25 16:38:05 -0700761 if ( defined $inside_menu[0] ) {
762 $parseline[0]{menus} = "";
763 }
764 else {
765 $parseline[0]{menus} = "top";
766 }
767
768 my $i = 0;
769 while ( defined $inside_menu[$i] ) {
770 $parseline[0]{menus} .= "$inside_menu[$i]";
771 $i++;
772 if ( defined $inside_menu[$i] ) {
773 $parseline[0]{menus} .= "->";
774 }
775 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600776 push @wholeconfig, @parseline;
777 }
Martin Roth0e6c0e12016-03-02 12:16:13 -0700778
779 foreach my $file ( keys %fileinfo ) {
780 if ( $fileinfo{$file}{iflevel} > 0 ) {
781 show_error("$file has $fileinfo{$file}{iflevel} more 'if' statement(s) than 'endif' statements.");
782 }
783 elsif ( $fileinfo{$file}{iflevel} < 0 ) {
784 show_error(
785 "$file has " . ( $fileinfo{$file}{iflevel} * -1 ) . " more 'endif' statement(s) than 'if' statements." );
786 }
787 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600788}
789
790#-------------------------------------------------------------------------------
791#-------------------------------------------------------------------------------
792sub handle_depends {
793 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
794
795 if ($inside_config) {
796 my $sym_num = $symbols{$inside_config}{count};
797 if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) {
798 $symbols{$inside_config}{$sym_num}{max_dependency}++;
799 }
800 else {
801 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
802 }
803
804 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
805 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
806 }
807}
808
809#-------------------------------------------------------------------------------
810#-------------------------------------------------------------------------------
811sub add_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700812 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600813 my @inside_if = @{$ifref};
814
815 #initialize the symbol or increment the use count.
Martin Roth1b44f7e2016-01-11 14:31:38 -0700816 if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600817 $symbols{$symbol}{count} = 0;
Michael Niewöhner43963582021-10-17 17:48:50 +0200818 # remember the location of the choice (or "")
819 $symbols{$symbol}{choice} = $inside_choice;
Martin Rothbcaaad12015-10-18 11:16:25 -0600820 }
821 else {
822 $symbols{$symbol}{count}++;
Nico Huber967730f2021-07-17 11:43:44 +0200823 if ( $inside_choice && $symbols{$symbol}{choice} ) {
Martin Rothaa206472017-03-24 08:51:51 -0600824 show_error( "$symbol entry at $filename:$line_no has already been created inside another choice block "
825 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
826 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600827 }
828
829 # add the location of this instance
830 my $symcount = $symbols{$symbol}{count};
831 $symbols{$symbol}{$symcount}{file} = $filename;
832 $symbols{$symbol}{$symcount}{line_no} = $line_no;
833
834 #Add the menu structure
835 if ( defined @$menu_array_ref[0] ) {
836 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
837 }
838
839 #Add any 'if' statements that the symbol is inside as dependencies
840 if (@inside_if) {
841 my $dep_num = 0;
842 for my $dependency (@inside_if) {
843 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
844 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
845 $dep_num++;
846 }
847 }
848}
849
850#-------------------------------------------------------------------------------
851# handle range
852#-------------------------------------------------------------------------------
853sub handle_range {
854 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
855
856 my $expression;
857 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
858
859 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
860 my $checkrange1 = $1;
861 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
862 my $checkrange2 = $1;
863
864 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700865 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 -0600866 }
867
868 if ($inside_config) {
869 if ( exists( $symbols{$inside_config}{range1} ) ) {
870 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700871 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700872 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700873 print " not match the previously defined range $symbols{$inside_config}{range1}"
874 . " $symbols{$inside_config}{range2}";
Martin Roth7aa3cea2015-11-27 18:45:45 -0700875 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600876 }
877 }
878 }
879 else {
880 $symbols{$inside_config}{range1} = $range1;
881 $symbols{$inside_config}{range2} = $range2;
882 $symbols{$inside_config}{range_file} = $filename;
883 $symbols{$inside_config}{range_line_no} = $line_no;
884 }
885 }
886 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700887 show_error("Range entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600888 }
889}
890
891#-------------------------------------------------------------------------------
892# handle_default
893#-------------------------------------------------------------------------------
894sub handle_default {
895 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
896 my $expression;
897 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
898
899 if ($inside_config) {
900 handle_expressions( $default, $inside_config, $filename, $line_no );
901 my $sym_num = $symbols{$inside_config}{count};
902
903 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
904 $symbols{$inside_config}{$sym_num}{default_max} = 0;
905 }
906 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
907 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
908 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
909 if ($expression) {
910 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
911 }
912 }
913 elsif ($inside_choice) {
914 handle_expressions( $default, $inside_config, $filename, $line_no );
915 }
916 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700917 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600918 }
919}
920
921#-------------------------------------------------------------------------------
922# handle_if_line
923#-------------------------------------------------------------------------------
924sub handle_if_line {
925 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
926
927 if ( $exprline !~ /if/ ) {
928 return ( $exprline, "" );
929 }
930
931 #remove any quotes that might have an 'if' in them
932 my $savequote;
933 if ( $exprline =~ /^\s*("[^"]+")/ ) {
934 $savequote = $1;
935 $exprline =~ s/^\s*("[^"]+")//;
936 }
937
938 my $expr = "";
939 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
940 $expr = $1;
941 $exprline =~ s/\s*if\s+.*$//;
942
943 if ($expr) {
944 handle_expressions( $expr, $inside_config, $filename, $line_no );
945 }
946 }
947
948 if ($savequote) {
949 $exprline = $savequote;
950 }
951
952 return ( $exprline, $expr );
953}
954
955#-------------------------------------------------------------------------------
Nico Huberf6b2baa2021-04-02 21:23:32 +0200956# handle_symbol - log which symbols are being used
957#-------------------------------------------------------------------------------
958sub handle_symbol {
959 my ( $symbol, $filename, $line_no ) = @_;
960
961 #filter constant symbols first
962 if ( $symbol =~ /^[yn]$/ ) { # constant y/n
963 return;
964 }
965 if ( $symbol =~ /^-?(?:0x)?\p{XDigit}+$/ ) { # int/hex values
966 return;
967 }
968 if ( $symbol =~ /^"[^"]*"$/ ) { # string values
969 return;
970 }
971
972 if ( $symbol =~ /^([A-Za-z0-9_]+)$/ ) { # actual symbol
973 add_referenced_symbol( $1, $filename, $line_no, 'expression' );
974 }
975 else {
976 show_error("Unrecognized expression: expected symbol, "
977 . "found '$symbol' in $filename line $line_no.");
978 }
979}
980
981#-------------------------------------------------------------------------------
982# handle_expressions - find symbols in expressions
Martin Rothbcaaad12015-10-18 11:16:25 -0600983#-------------------------------------------------------------------------------
984sub handle_expressions {
985 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
986
Nico Huberf6b2baa2021-04-02 21:23:32 +0200987 my $strip = qr/\s*(.*[^\s]+)\s*/;
Martin Rothbcaaad12015-10-18 11:16:25 -0600988
Nico Huberf6b2baa2021-04-02 21:23:32 +0200989 my $parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
990 my $quotes = qr/"[^"]*"/;
991 my $balanced = qr/((?:$parens|$quotes|[^\(\)"])+)/;
Martin Rothbcaaad12015-10-18 11:16:25 -0600992
Nico Huberf6b2baa2021-04-02 21:23:32 +0200993 if ( $exprline =~ /^\s*$balanced\s*(?:\|\||&&)\s*(.+)$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +0200994 # <expr> '||' <expr>, <expr> '&&' <expr> (8)(7)
Nico Huberf6b2baa2021-04-02 21:23:32 +0200995 my ( $lhs, $rhs ) = ( $1, $3 );
996 handle_expressions( $lhs, $inside_config, $filename, $line_no );
997 handle_expressions( $rhs, $inside_config, $filename, $line_no );
998 }
999 elsif ( $exprline =~ /^\s*!(.+)$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +02001000 # '!' <expr> (6)
Martin Rothbcaaad12015-10-18 11:16:25 -06001001 handle_expressions( $1, $inside_config, $filename, $line_no );
1002 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001003 elsif ( $exprline =~ /^\s*$parens\s*$/ ) {
Nico Huber2d821952021-04-02 21:56:45 +02001004 # '(' <expr> ')' (5)
Nico Huberf6b2baa2021-04-02 21:23:32 +02001005 $exprline =~ /^\s*\((.*)\)\s*$/;
Martin Rothbcaaad12015-10-18 11:16:25 -06001006 handle_expressions( $1, $inside_config, $filename, $line_no );
1007 }
Nico Huber2d821952021-04-02 21:56:45 +02001008 elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*(?:[<>]=?|!=)$strip$/ ) {
1009 # <symbol> '<' <symbol>, <symbol> '!=' <symbol>, etc. (4)(3)
Nico Huberf6b2baa2021-04-02 21:23:32 +02001010 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 =~ /^\s*($quotes|[^"\s]+)\s*=$strip$/ ) {
1015 # <symbol> '=' <symbol> (2)
1016 my ( $lhs, $rhs ) = ( $1, $2 );
1017 handle_symbol( $lhs, $filename, $line_no );
1018 handle_symbol( $rhs, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001019 }
Nico Huberf6b2baa2021-04-02 21:23:32 +02001020 elsif ( $exprline =~ /^$strip$/ ) {
1021 # <symbol> (1)
1022 handle_symbol( $1, $filename, $line_no );
Martin Rothbcaaad12015-10-18 11:16:25 -06001023 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001024}
1025
1026#-------------------------------------------------------------------------------
1027# add_referenced_symbol
1028#-------------------------------------------------------------------------------
1029sub add_referenced_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -07001030 my ( $symbol, $filename, $line_no, $reftype ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -06001031 if ( exists $referenced_symbols{$symbol} ) {
1032 $referenced_symbols{$symbol}{count}++;
1033 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
1034 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
1035 }
1036 else {
1037 $referenced_symbols{$symbol}{count} = 0;
1038 $referenced_symbols{$symbol}{0}{filename} = $filename;
1039 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
1040 }
Martin Rothb7c39b22016-01-14 09:04:53 -07001041
1042 #mark the symbol as being selected, use referenced symbols for location
1043 if ( $reftype eq 'select' ) {
1044 $selected_symbols{$symbol}{ $referenced_symbols{$symbol}{count} } = 1;
1045 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001046}
1047
1048#-------------------------------------------------------------------------------
1049# handle_help
1050#-------------------------------------------------------------------------------
1051{
1052 #create a non-global static variable by enclosing it and the subroutine
1053 my $help_whitespace = ""; #string to show length of the help whitespace
Martin Roth8849f3b2017-05-23 19:43:43 -06001054 my $help_keyword_whitespace = "";
Martin Rothbcaaad12015-10-18 11:16:25 -06001055
1056 sub handle_help {
1057 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1058
1059 if ($inside_help) {
1060
1061 #get the indentation level if it's not already set.
1062 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
1063 $line =~ /^(\s+)/; #find the indentation level.
1064 $help_whitespace = $1;
1065 if ( !$help_whitespace ) {
Martin Roth8849f3b2017-05-23 19:43:43 -06001066 show_error("$filename:$line_no - help text starts with no whitespace.");
1067 return $inside_help;
1068 }
1069 elsif ($help_keyword_whitespace eq $help_whitespace) {
1070 show_error("$filename:$line_no - help text needs additional indentation.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001071 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -06001072 }
1073 }
1074
1075 #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 -06001076 if ( ( $line !~ /^$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001077 $inside_help = 0;
1078 $help_whitespace = "";
Martin Roth8849f3b2017-05-23 19:43:43 -06001079 $help_keyword_whitespace = "";
Martin Rothbcaaad12015-10-18 11:16:25 -06001080 }
1081 else { #if it's not ended, add the line to the helptext array for the symbol's instance
1082 if ($inside_config) {
1083 my $sym_num = $symbols{$inside_config}{count};
1084 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
1085 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
1086 }
Martin Roth8849f3b2017-05-23 19:43:43 -06001087 if ( ($help_keyword_whitespace eq $help_whitespace) && ( $line !~ /^[\r\n]+/ ) ) {
1088 show_error("$filename:$line_no - help text needs additional indentation.");
1089 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001090 }
1091 }
1092 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
1093 $inside_help = $line_no;
Martin Roth8849f3b2017-05-23 19:43:43 -06001094 $line =~ /^(\s+)/;
1095 $help_keyword_whitespace = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001096 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001097 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001098 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001099 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001100 }
1101 elsif ($inside_config) {
1102 $help_whitespace = "";
1103 my $sym_num = $symbols{$inside_config}{count};
1104 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
1105 $symbols{$inside_config}{$sym_num}{helptext} = ();
1106 }
1107 }
1108 return $inside_help;
1109 }
1110}
1111
1112#-------------------------------------------------------------------------------
1113# handle_type
1114#-------------------------------------------------------------------------------
1115sub handle_type {
1116 my ( $type, $inside_config, $filename, $line_no ) = @_;
1117
1118 my $expression;
1119 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
1120
Martin Roth08ee1cf2016-01-25 16:39:32 -07001121 if ( $type =~ /tristate/ ) {
Julius Wernere5eb2de2019-03-05 17:10:19 -08001122 show_error("$filename:$line_no - tristate types are not used.");
Martin Roth08ee1cf2016-01-25 16:39:32 -07001123 }
1124
Martin Rothbcaaad12015-10-18 11:16:25 -06001125 if ($inside_config) {
1126 if ( exists( $symbols{$inside_config}{type} ) ) {
1127 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001128 show_error( "Config '$inside_config' type entry $type"
1129 . " at $filename:$line_no does not match $symbols{$inside_config}{type}"
1130 . " defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001131 }
1132 }
1133 else {
1134 $symbols{$inside_config}{type} = $type;
1135 $symbols{$inside_config}{type_file} = $filename;
1136 $symbols{$inside_config}{type_line_no} = $line_no;
1137 }
1138 }
1139 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001140 show_error("Type entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001141 }
1142}
1143
1144#-------------------------------------------------------------------------------
1145# handle_prompt
1146#-------------------------------------------------------------------------------
1147sub handle_prompt {
1148 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1149
1150 my $expression;
1151 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
1152
1153 if ($inside_config) {
1154 if ( $prompt !~ /^\s*$/ ) {
1155 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
1156 $prompt = $1;
1157 }
1158
Martin Roth08cf90f2016-01-25 19:54:16 -07001159 #display an error if there's a prompt at the top menu level
Martin Rothbcaaad12015-10-18 11:16:25 -06001160 if ( !defined @$menu_array_ref[0] ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001161 show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu"
1162 . " at $filename:$line_no." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001163 }
1164
1165 my $sym_num = $symbols{$inside_config}{count};
Martin Rothb58d3492016-01-25 16:42:13 -07001166 if ( !exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001167 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
1168 }
Martin Rothb58d3492016-01-25 16:42:13 -07001169 else {
1170 $symbols{$inside_config}{$sym_num}{prompt_max}++;
1171 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001172 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
1173 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
1174 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
Martin Rothb58d3492016-01-25 16:42:13 -07001175
1176 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_menu} = @$menu_array_ref;
Martin Rothbcaaad12015-10-18 11:16:25 -06001177 if ($expression) {
1178 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
1179 }
1180 }
1181 }
1182 elsif ($inside_choice) {
1183
1184 #do nothing
1185 }
1186 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001187 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001188 }
1189}
1190
1191#-------------------------------------------------------------------------------
1192# simple_line_checks - Does some basic checks on the current line, then cleans the line
1193# up for further processing.
1194#-------------------------------------------------------------------------------
1195sub simple_line_checks {
1196 my ( $line, $filename, $line_no ) = @_;
1197
1198 #check for spaces instead of tabs
1199 if ( $line =~ /^ +/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001200 show_error("$filename:$line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001201 }
1202
1203 #verify a linefeed at the end of the line
1204 if ( $line !~ /.*\n/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001205 show_error( "$filename:$line_no does not end with linefeed."
1206 . " This can cause the line to not be recognized by the Kconfig parser.\n#($line)" );
Martin Rothbcaaad12015-10-18 11:16:25 -06001207 $line =~ s/\s*$//;
1208 }
1209 else {
1210 chop($line);
1211 }
1212
1213 return $line;
1214}
1215
1216#-------------------------------------------------------------------------------
1217# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
1218#-------------------------------------------------------------------------------
1219sub load_kconfig_file {
1220 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
1221 my @file_data;
1222 my @dir_file_data;
1223
1224 #recursively handle coreboot's new source glob operator
Arthur Heymans55f01322019-11-05 12:06:59 +01001225 if ( $input_file =~ /^(.*?)\/(\w*)\*(\w*)\/(.*)$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001226 my $dir_prefix = $1;
Arthur Heymans55f01322019-11-05 12:06:59 +01001227 my $dir_glob_prefix = $2;
1228 my $dir_glob_suffix = $3;
1229 my $dir_suffix = $4;
Martin Rothbcaaad12015-10-18 11:16:25 -06001230 if ( -d "$dir_prefix" ) {
1231
1232 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
1233 my @dirlist = sort { $a cmp $b } readdir(D);
1234 closedir(D);
1235
1236 while ( my $directory = shift @dirlist ) {
1237
1238 #ignore non-directory files
Arthur Heymans55f01322019-11-05 12:06:59 +01001239 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ )
1240 && ( $directory =~ /\Q$dir_glob_prefix\E.*\Q$dir_glob_suffix\E/ ) ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001241 push @dir_file_data,
1242 load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1243 $input_file, $loadline, 1, $loadfile, $loadline );
Martin Rothbcaaad12015-10-18 11:16:25 -06001244 }
1245 }
1246 }
1247
1248 #the directory should exist when using a glob
1249 else {
Julius Wernere5eb2de2019-03-05 17:10:19 -08001250 show_error("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -06001251 }
1252 }
1253
1254 #if the file exists, try to load it.
1255 elsif ( -e "$input_file" ) {
1256
Martin Rotha7d00272016-10-03 23:00:04 +02001257 #throw a warning if the file has already been loaded.
Martin Rothbcaaad12015-10-18 11:16:25 -06001258 if ( exists $loaded_files{$input_file} ) {
Martin Rotha7d00272016-10-03 23:00:04 +02001259 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -06001260 }
1261
1262 #load the file's contents and mark the file as loaded for checking later
1263 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
1264 @file_data = <$HANDLE>;
1265 close $HANDLE;
1266 $loaded_files{$input_file} = "'$loadfile' line $loadline";
1267 }
1268
1269 # if the file isn't being loaded from a glob, it should exist.
1270 elsif ( $expanded == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001271 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -06001272 }
1273
1274 my $line_in_file = 0;
1275 while ( my $line = shift @file_data ) {
1276
1277 #handle line continuation.
1278 my $continue_line = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001279 while ( $line =~ /(.*)\s+\\$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001280 my $text = $1;
1281
1282 # get rid of leading whitespace on all but the first and last lines
1283 $text =~ s/^\s*/ / if ($continue_line);
1284
1285 $dir_file_data[$line_in_file]{text} .= $text;
1286 $line = shift @file_data;
1287 $continue_line++;
1288
1289 #put the data into the continued lines (other than the first)
1290 $line =~ /^\s*(.*)\s*$/;
1291
Martin Roth1b44f7e2016-01-11 14:31:38 -07001292 $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n";
1293 $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file;
1294 $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001295
1296 #get rid of multiple leading spaces for last line
1297 $line = " $1\n";
1298 }
1299
Martin Roth1b44f7e2016-01-11 14:31:38 -07001300 $dir_file_data[$line_in_file]{text} .= $line;
Martin Rothbcaaad12015-10-18 11:16:25 -06001301 $dir_file_data[$line_in_file]{filename} = $input_file;
1302 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1303
1304 $line_in_file++;
1305 if ($continue_line) {
1306 $line_in_file += $continue_line;
1307 }
1308 }
1309
1310 if ($topfile) {
1311 my %file_data;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001312 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001313 $file_data{filename} = $topfile;
1314 $file_data{file_line_no} = "($topline)";
Martin Roth1b44f7e2016-01-11 14:31:38 -07001315 unshift( @dir_file_data, \%file_data );
Martin Rothbcaaad12015-10-18 11:16:25 -06001316 }
1317
1318 return @dir_file_data;
1319}
1320
Martin Rothbcaaad12015-10-18 11:16:25 -06001321#-------------------------------------------------------------------------------
1322# print_wholeconfig - prints out the parsed Kconfig file
1323#-------------------------------------------------------------------------------
1324sub print_wholeconfig {
1325
1326 return unless $print_full_output;
1327
Martin Rotha7648f22021-11-05 17:24:29 -06001328 for ( my $i = 0 ; $i <= $#wholeconfig ; $i++ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001329 my $line = $wholeconfig[$i];
1330 chop( $line->{text} );
1331
1332 #replace tabs with spaces for consistency
1333 $line->{text} =~ s/\t/ /g;
Martin Roth819e6722016-01-25 16:38:05 -07001334 printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text};
Martin Rothbcaaad12015-10-18 11:16:25 -06001335 }
1336}
1337
1338#-------------------------------------------------------------------------------
1339# check_if_file_referenced - checks for kconfig files that are not being parsed
1340#-------------------------------------------------------------------------------
1341sub check_if_file_referenced {
1342 my $filename = $File::Find::name;
Martin Rothab2d7772016-01-25 16:45:14 -07001343 if ( ( $filename =~ /Kconfig/ )
1344 && ( !$filename =~ /\.orig$/ )
1345 && ( !$filename =~ /~$/ )
1346 && ( !exists $loaded_files{$filename} ) )
1347 {
Martin Rothd8080172015-11-26 19:12:44 -07001348 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001349 }
1350}
1351
1352#-------------------------------------------------------------------------------
1353# check_arguments parse the command line arguments
1354#-------------------------------------------------------------------------------
1355sub check_arguments {
1356 my $show_usage = 0;
1357 GetOptions(
1358 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001359 'e|errors_off' => \$suppress_error_output,
1360 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001361 'o|output=s' => \$output_file,
1362 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001363 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001364 'path=s' => \$top_dir,
1365 'c|config=s' => \$config_file,
1366 'G|no_git_grep' => \$dont_use_git_grep,
Martin Roth873ebf22023-08-10 10:00:51 -06001367 'S|site_local' => \$include_site_local,
Martin Rothbcaaad12015-10-18 11:16:25 -06001368 );
Martin Rothd8080172015-11-26 19:12:44 -07001369
1370 if ($suppress_error_output) {
1371 $suppress_warning_output = 1;
1372 }
1373 if ($suppress_warning_output) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001374 $show_note_output = 0;
Martin Rothd8080172015-11-26 19:12:44 -07001375 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001376}
1377
1378#-------------------------------------------------------------------------------
1379# usage - Print the arguments for the user
1380#-------------------------------------------------------------------------------
1381sub usage {
1382 print "kconfig_lint <options>\n";
1383 print " -o|--output=file Set output filename\n";
1384 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001385 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001386 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001387 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001388 print " --path=dir Path to top level kconfig\n";
1389 print " -c|--config=file Filename of config file to load\n";
1390 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
Martin Roth873ebf22023-08-10 10:00:51 -06001391 print " -S|--site_local Include the site-local directory\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001392
1393 exit(0);
1394}
1395
13961;