northbridge: Remove unneeded include <pc80/mc146818rtc.h>
[coreboot.git] / util / lint / kconfig_lint
blobeddd8de6ee6dd98229a36cffff709050e40c6518
1 #!/usr/bin/env perl
4 # This file is part of the coreboot project.
6 # Copyright (C) 2015 Martin L Roth <gaumless@gmail.com>
7 # Copyright (C) 2015-2016 Google, Inc.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # perltidy -l=123
20 package kconfig_lint;
22 use strict;
23 use warnings;
24 use English qw( -no_match_vars );
25 use File::Find;
26 use Getopt::Long;
27 use Getopt::Std;
29 # If taint mode is enabled, Untaint the path - git and grep must be in /bin, /usr/bin or /usr/local/bin
30 if ( ${^TAINT} ) {
31 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
32 delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' };
35 my $suppress_error_output = 0; # flag to prevent error text
36 my $suppress_warning_output = 0; # flag to prevent warning text
37 my $show_note_output = 0; # flag to show minor notes text
38 my $print_full_output = 0; # flag to print wholeconfig output
39 my $output_file = "-"; # filename of output - set stdout by default
40 my $dont_use_git_grep = 0;
42 # Globals
43 my $top_dir = "."; # Directory where Kconfig is run
44 my $root_dir = "src"; # Directory of the top level Kconfig file
45 my $errors_found = 0; # count of errors
46 my $warnings_found = 0;
47 my $exclude_dirs_and_files =
48 '^build/\|^coreboot-builds/\|^configs/\|^util/\|^\.git/\|^payloads\|^Documentation\|^3rdparty'
49 . '\|' . # directories to exclude when searching for used symbols
50 '\.config\|\.txt$\|\.tex$\|\.tags'; #files to exclude when looking for symbols
51 my $payload_files_to_check='payloads/Makefile.inc payloads/external/Makefile.inc';
52 my $config_file = ""; # name of config file to load symbol values from.
53 my @wholeconfig; # document the entire kconfig structure
54 my %loaded_files; # list of each Kconfig file loaded
55 my %symbols; # main structure of all symbols declared
56 my %referenced_symbols; # list of symbols referenced by expressions or select statements
57 my %used_symbols; # structure of symbols used in the tree, and where they're found
58 my @collected_symbols; #
59 my %selected_symbols; # list of symbols that are enabled by a select statement
61 my $exclude_unused = '_SPECIFIC_OPTIONS|SOUTH_BRIDGE_OPTIONS';
63 Main();
65 #-------------------------------------------------------------------------------
66 # Main
68 # Start by loading and parsing the top level Kconfig, this pulls in the other
69 # files. Parsing the tree creates several arrays and hashes that can be used
70 # to check for errors
71 #-------------------------------------------------------------------------------
72 sub Main {
74 check_arguments();
75 open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
77 if ( defined $top_dir ) {
78 chdir $top_dir or die "Error: can't cd to $top_dir\n";
81 die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir );
83 #load the Kconfig tree, checking what we can and building up all the hash tables
84 build_and_parse_kconfig_tree("$root_dir/Kconfig");
86 load_config($config_file) if ($config_file);
88 check_type();
89 check_defaults();
90 check_referenced_symbols();
92 collect_used_symbols();
93 check_used_symbols();
94 check_for_ifdef();
95 check_for_def();
96 check_is_enabled();
97 check_selected_symbols();
99 # Run checks based on the data that was found
100 if ( ( !$suppress_warning_output ) && ( ${^TAINT} == 0 ) ) {
102 # The find function is tainted - only run it if taint checking
103 # is disabled and warnings are enabled.
104 find( \&check_if_file_referenced, $root_dir );
107 print_wholeconfig();
109 if ($errors_found) {
110 print "# $errors_found errors";
111 if ($warnings_found) {
112 print ", $warnings_found warnings";
114 print "\n";
117 exit( $errors_found + $warnings_found );
120 #-------------------------------------------------------------------------------
121 # Print and count errors
122 #-------------------------------------------------------------------------------
123 sub show_error {
124 my ($error_msg) = @_;
125 unless ($suppress_error_output) {
126 print "#!!!!! Error: $error_msg\n";
127 $errors_found++;
131 #-------------------------------------------------------------------------------
132 # Print and count warnings
133 #-------------------------------------------------------------------------------
134 sub show_warning {
135 my ($warning_msg) = @_;
136 unless ($suppress_warning_output) {
137 print "#!!!!! Warning: $warning_msg\n";
138 $warnings_found++;
142 #-------------------------------------------------------------------------------
143 # check selected symbols for validity
144 # they must be bools
145 # they cannot select symbols created in 'choice' blocks
146 #-------------------------------------------------------------------------------
147 sub check_selected_symbols {
149 #loop through symbols found in expressions and used by 'select' keywords
150 foreach my $symbol ( sort ( keys %selected_symbols ) ) {
151 my $type_failure = 0;
152 my $choice_failure = 0;
154 #errors selecting symbols that don't exist are already printed, so we
155 #don't need to print them again here
157 #make sure the selected symbols are bools
158 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "bool" ) ) {
159 $type_failure = 1;
162 #make sure we're not selecting choice symbols
163 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{choice} ) ) {
164 $choice_failure = 1;
167 #loop through each instance of the symbol to print out all of the failures
168 for ( my $i = 0 ; $i <= $referenced_symbols{$symbol}{count} ; $i++ ) {
169 next if ( !exists $selected_symbols{$symbol}{$i} );
170 my $file = $referenced_symbols{$symbol}{$i}{filename};
171 my $lineno = $referenced_symbols{$symbol}{$i}{line_no};
172 if ($type_failure) {
173 show_error(
174 "CONFIG_$symbol' selected at $file:$lineno." . " Selects only work on symbols of type bool." );
176 if ($choice_failure) {
177 show_error(
178 "'CONFIG_$symbol' selected at $file:$lineno." . " Symbols created in a choice cannot be selected." );
184 #-------------------------------------------------------------------------------
185 # check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and
186 # #if defined(CONFIG_[symbol_name]).
188 # #ifdef symbol is valid for strings, but bool, hex, and INT are always defined.
189 # #if defined(symbol) && symbol is also a valid construct.
190 #-------------------------------------------------------------------------------
191 sub check_for_ifdef {
192 my @ifdef_symbols = @collected_symbols;
194 #look for #ifdef SYMBOL
195 while ( my $line = shift @ifdef_symbols ) {
196 if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s*\(?\s*CONFIG_(\w+)/ ) {
197 my $file = $1;
198 my $lineno = $2;
199 my $symbol = $3;
201 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
202 show_warning( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
203 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
205 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*if\s+!?\s*defined\s*\(?\s*CONFIG_(\w+)/ ) {
206 my $file = $1;
207 my $lineno = $2;
208 my $symbol = $3;
210 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
211 show_warning( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
212 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
217 # look for (#if) defined SYMBOL
218 @ifdef_symbols = @collected_symbols;
219 while ( my $line = shift @ifdef_symbols ) {
220 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_(\w+)/ ) {
221 my $file = $1;
222 my $lineno = $2;
223 my $symbol = $3;
225 #ignore '#if defined(symbol) && symbol' type statements
226 next
227 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_$symbol.*(&&|\|\|)\s*!?\s*\(?\s*CONFIG_$symbol/ );
229 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
230 show_warning( "defined 'CONFIG_$symbol' used at $file:$lineno."
231 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
236 my @collected_is_enabled;
237 if ($dont_use_git_grep) {
238 @collected_is_enabled =
239 `grep -Irn -- "[[:space:]]IS_ENABLED[[:space:]]*(.*)" | grep -v '$exclude_dirs_and_files' | grep -v "kconfig.h"`;
241 else {
242 @collected_is_enabled =
243 `git grep -In -- "[[:space:]]IS_ENABLED[[:space:]]*(.*)" | grep -v '$exclude_dirs_and_files' | grep -v "kconfig.h"`;
246 while ( my $line = shift @collected_is_enabled ) {
247 if ($line !~ /CONFIG_/ && $line =~ /^([^:]+):(\d+):.+IS_ENABLED\s*\(\s*(\w+)/ ) {
248 my $file = $1;
249 my $lineno = $2;
250 my $symbol = $3;
251 if ( ( exists $symbols{$symbol} ) ) {
252 show_error("IS_ENABLED missing CONFIG_ prefix on symbol '$symbol' at $file:$lineno.");
258 #-------------------------------------------------------------------------------
259 # check_for_def - Look for instances of #define CONFIG_[symbol_name]
261 # Symbols should not be redefined outside of Kconfig, and #defines should not
262 # look like symbols
263 #-------------------------------------------------------------------------------
264 sub check_for_def {
265 my @def_symbols = @collected_symbols;
267 #look for #ifdef SYMBOL
268 while ( my $line = shift @def_symbols ) {
269 if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) {
270 my $file = $1;
271 my $lineno = $2;
272 my $symbol = $3;
274 if ( ( exists $symbols{$symbol} ) ) {
275 show_warning("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
277 else {
278 show_warning( "#define 'CONFIG_$symbol' used at $file:$lineno."
279 . " Other #defines should not look like Kconfig symbols." );
285 #-------------------------------------------------------------------------------
286 # check_type - Make sure that all symbols have a type defined.
288 # Conflicting types are found when parsing the Kconfig tree.
289 #-------------------------------------------------------------------------------
290 sub check_type {
292 # loop through each defined symbol
293 foreach my $sym ( sort ( keys %symbols ) ) {
295 # Make sure there's a type set for the symbol
296 if (!defined $symbols{$sym}{type}) {
298 #loop through each instance of that symbol
299 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
301 my $filename = $symbols{$sym}{$sym_num}{file};
302 my $line_no = $symbols{$sym}{$sym_num}{line_no};
304 show_error("No type defined for symbol $sym defined at $filename:$line_no.");
310 #-------------------------------------------------------------------------------
311 # check_is_enabled - The IS_ENABLED() macro is only valid for symbols of type
312 # bool. It would probably work on type hex or int if the value was 0 or 1, but
313 # this seems like a bad plan. Using it on strings is dead out.
314 #-------------------------------------------------------------------------------
315 sub check_is_enabled {
316 my @is_enabled_symbols = @collected_symbols;
318 #sort through symbols found by grep and store them in a hash for easy access
319 while ( my $line = shift @is_enabled_symbols ) {
320 if ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
321 my $file = $1;
322 my $lineno = $2;
323 $line = $3;
324 if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) {
325 show_warning("# uninterpreted IS_ENABLED at $file:$lineno: $line");
326 next;
328 while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
329 my $symbol = $2;
330 $line = $1 . $3;
332 #make sure that the type is bool
333 if ( exists $symbols{$symbol} ) {
334 if ( $symbols{$symbol}{type} ne "bool" ) {
335 show_error( "IS_ENABLED(CONFIG_$symbol) used at $file:$lineno."
336 . " IS_ENABLED is only valid for type 'bool', not '$symbols{$symbol}{type}'." );
339 else {
340 show_error("IS_ENABLED() used on unknown value CONFIG_$symbol at $file:$lineno.");
343 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?.*?$/ ) {
344 my $file = $1;
345 my $lineno = $2;
346 my $symbol = $3;
347 # If the type is bool, give a warning that IS_ENABLED should be used
348 if ( exists $symbols{$symbol} ) {
349 if ( $symbols{$symbol}{type} eq "bool" ) {
350 show_error( "#if CONFIG_$symbol used at $file:$lineno."
351 . " IS_ENABLED should be used for type 'bool'" );
354 } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if.*(?:&&|\|\|)\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?$/ ) {
355 my $file = $1;
356 my $lineno = $2;
357 my $symbol = $3;
358 # If the type is bool, give a warning that IS_ENABLED should be used
359 if ( exists $symbols{$symbol} ) {
360 if ( $symbols{$symbol}{type} eq "bool" ) {
361 show_error( "#if CONFIG_$symbol used at $file:$lineno."
362 . " IS_ENABLED should be used for type 'bool'" );
369 #-------------------------------------------------------------------------------
370 # check_defaults - Look for defaults that come after a default with no
371 # dependencies.
373 # TODO - check for defaults with the same dependencies
374 #-------------------------------------------------------------------------------
375 sub check_defaults {
377 # loop through each defined symbol
378 foreach my $sym ( sort ( keys %symbols ) ) {
379 my $default_set = 0;
380 my $default_filename = "";
381 my $default_line_no = "";
383 #loop through each instance of that symbol
384 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
386 #loop through any defaults for that instance of that symbol, if there are any
387 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
388 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
390 my $filename = $symbols{$sym}{$sym_num}{file};
391 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
393 # Make sure there's a type set for the symbol
394 next if (!defined $symbols{$sym}{type});
396 # skip good defaults
397 if (! ((($symbols{$sym}{type} eq "hex") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^0x/)) ||
398 (($symbols{$sym}{type} eq "int") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[-0-9]+$/)) ||
399 (($symbols{$sym}{type} eq "string") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^".*"$/)) ||
400 (($symbols{$sym}{type} eq "bool") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[yn]$/)))
403 my ($checksym) = $symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /(\w+)/;
405 if (! exists $symbols{$checksym}) {
407 # verify the symbol type against the default value
408 if ($symbols{$sym}{type} eq "hex") {
409 show_error("non hex default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for hex symbol $sym at $filename:$line_no.");
410 } elsif ($symbols{$sym}{type} eq "int") {
411 show_error("non int default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for int symbol $sym at $filename:$line_no.");
412 } elsif ($symbols{$sym}{type} eq "string") {
413 # TODO: Remove special MAINBOARD_DIR check
414 if ($sym ne "MAINBOARD_DIR") {
415 show_error("no quotes around default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for string symbol $sym at $filename:$line_no.");
417 } elsif ($symbols{$sym}{type} eq "bool") {
418 if ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /[01YN]/) {
419 show_warning("default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) for bool symbol $sym uses value other than y/n at $filename:$line_no.");
420 } else {
421 show_error("non bool default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for bool symbol $sym at $filename:$line_no.");
427 #if a default is already set, display an error
428 if ($default_set) {
429 show_warning( "Default for '$sym' referenced at $filename:$line_no will never be set"
430 . " - overridden by default set at $default_filename:$default_line_no" );
432 else {
433 #if no default is set, see if this is a default with no dependencies
434 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
435 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
437 $default_set = 1;
438 $default_filename = $symbols{$sym}{$sym_num}{file};
439 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
447 #-------------------------------------------------------------------------------
448 # check_referenced_symbols - Make sure the symbols referenced by expressions and
449 # select statements are actually valid symbols.
450 #-------------------------------------------------------------------------------
451 sub check_referenced_symbols {
453 #loop through symbols found in expressions and used by 'select' keywords
454 foreach my $key ( sort ( keys %referenced_symbols ) ) {
456 #make sure the symbol was defined by a 'config' or 'choice' keyword
457 next if ( exists $symbols{$key} );
459 #loop through each instance of the symbol to print out all of the invalid references
460 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
461 my $filename = $referenced_symbols{$key}{$i}{filename};
462 my $line_no = $referenced_symbols{$key}{$i}{line_no};
463 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
468 #-------------------------------------------------------------------------------
469 #-------------------------------------------------------------------------------
470 sub collect_used_symbols {
471 # find all references to CONFIG_ statements in the tree
473 if ($dont_use_git_grep) {
474 @collected_symbols = `grep -Irn -- "CONFIG_" | grep -v '$exclude_dirs_and_files'; grep -In -- "CONFIG_" $payload_files_to_check`;
476 else {
477 @collected_symbols = `git grep -In -- "CONFIG_" | grep -v '$exclude_dirs_and_files'; git grep -In -- "CONFIG_" $payload_files_to_check`;
480 my @used_symbols = @collected_symbols;
482 #sort through symbols found by grep and store them in a hash for easy access
483 while ( my $line = shift @used_symbols ) {
484 while ( $line =~ /[^A-Za-z0-9_]CONFIG_([A-Za-z0-9_]+)/g ) {
485 my $symbol = $1;
486 my $filename = "";
487 if ( $line =~ /^([^:]+):/ ) {
488 $filename = $1;
491 if ( exists $used_symbols{$symbol}{count} ) {
492 $used_symbols{$symbol}{count}++;
494 else {
495 $used_symbols{$symbol}{count} = 0;
497 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
502 #-------------------------------------------------------------------------------
503 # check_used_symbols - Checks to see whether or not the created symbols are
504 # actually used.
505 #-------------------------------------------------------------------------------
506 sub check_used_symbols {
507 # loop through all defined symbols and see if they're used anywhere
508 foreach my $key ( sort ( keys %symbols ) ) {
510 if ( $key =~ /$exclude_unused/ ) {
511 next;
514 #see if they're used internal to Kconfig
515 next if ( exists $referenced_symbols{$key} );
517 #see if they're used externally
518 next if exists $used_symbols{$key};
520 #loop through the definitions to print out all the places the symbol is defined.
521 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
522 my $filename = $symbols{$key}{$i}{file};
523 my $line_no = $symbols{$key}{$i}{line_no};
524 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
529 #-------------------------------------------------------------------------------
530 # build_and_parse_kconfig_tree
531 #-------------------------------------------------------------------------------
532 #load the initial file and start parsing it
533 sub build_and_parse_kconfig_tree {
534 my ($top_level_kconfig) = @_;
535 my @config_to_parse;
536 my @parseline;
537 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
538 my @inside_if = (); # stack of if dependencies
539 my $inside_config = ""; # set to symbol name of the config section
540 my @inside_menu = (); # stack of menu names
541 my $inside_choice = "";
542 my $configs_inside_choice;
543 my %fileinfo;
545 #start the tree off by loading the top level kconfig
546 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
548 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
549 my $line = $parseline[0]{text};
550 my $filename = $parseline[0]{filename};
551 my $line_no = $parseline[0]{file_line_no};
553 #handle help - help text: "help" or "---help---"
554 my $lastline_was_help = $inside_help;
555 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
556 $parseline[0]{inside_help} = $inside_help;
558 #look for basic issues in the line, strip crlf
559 $line = simple_line_checks( $line, $filename, $line_no );
561 #strip comments
562 $line =~ s/\s*#.*$//;
564 #don't parse any more if we're inside a help block
565 if ($inside_help) {
566 #do nothing
569 #handle config
570 elsif ( $line =~ /^\s*config\s+/ ) {
571 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
572 my $symbol = $1;
573 $inside_config = $symbol;
574 if ($inside_choice) {
575 $configs_inside_choice++;
577 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
580 #bool|hex|int|string|tristate <expr> [if <expr>]
581 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
582 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
583 my ( $type, $prompt ) = ( $1, $2 );
584 handle_type( $type, $inside_config, $filename, $line_no );
585 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
588 # def_bool|def_tristate <expr> [if <expr>]
589 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
590 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
591 my ( $orgtype, $default ) = ( $1, $2 );
592 ( my $type = $orgtype ) =~ s/def_//;
593 handle_type( $type, $inside_config, $filename, $line_no );
594 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
597 #prompt <prompt> [if <expr>]
598 elsif ( $line =~ /^\s*prompt/ ) {
599 $line =~ /^\s*prompt\s+(.+)/;
600 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
603 # default <expr> [if <expr>]
604 elsif ( $line =~ /^\s*default/ ) {
605 $line =~ /^\s*default\s+(.*)/;
606 my $default = $1;
607 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
610 # depends on <expr>
611 elsif ( $line =~ /^\s*depends\s+on/ ) {
612 $line =~ /^\s*depends\s+on\s+(.*)$/;
613 my $expr = $1;
614 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
615 handle_expressions( $expr, $inside_config, $filename, $line_no );
618 # comment <prompt>
619 elsif ( $line =~ /^\s*comment/ ) {
620 $inside_config = "";
623 # choice [symbol]
624 elsif ( $line =~ /^\s*choice/ ) {
625 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
626 my $symbol = $1;
627 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
628 handle_type( "bool", $symbol, $filename, $line_no );
630 $inside_config = "";
631 $inside_choice = "$filename $line_no";
632 $configs_inside_choice = 0;
634 # Kconfig verifies that choice blocks have a prompt
637 # endchoice
638 elsif ( $line =~ /^\s*endchoice/ ) {
639 $inside_config = "";
640 if ( !$inside_choice ) {
641 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
644 $inside_choice = "";
645 if ( $configs_inside_choice == 0 ) {
646 show_error("choice block has no symbols at $filename:$line_no.");
648 $configs_inside_choice = 0;
651 # [optional]
652 elsif ( $line =~ /^\s*optional/ ) {
653 if ($inside_config) {
654 show_error( "Keyword 'optional' appears inside config for '$inside_config'"
655 . " at $filename:$line_no. This is not valid." );
657 if ( !$inside_choice ) {
658 show_error( "Keyword 'optional' appears outside of a choice block"
659 . " at $filename:$line_no. This is not valid." );
663 # mainmenu <prompt>
664 elsif ( $line =~ /^\s*mainmenu/ ) {
665 $inside_config = "";
667 # Kconfig alread checks for multiple 'mainmenu' entries and mainmenu entries with no prompt
668 # Possible check: look for 'mainmenu ""'
669 # Possible check: verify that a mainmenu has been specified
672 # menu <prompt>
673 elsif ( $line =~ /^\s*menu/ ) {
674 $line =~ /^\s*menu\s+(.*)/;
675 my $menu = $1;
676 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
677 $menu = $1;
680 $inside_config = "";
681 $inside_choice = "";
682 push( @inside_menu, $menu );
685 # visible if <expr>
686 elsif ( $line =~ /^\s*visible if.*$/ ) {
687 # Must come directly after menu line (and on a separate line)
688 # but kconfig already checks for that.
689 # Ignore it.
692 # endmenu
693 elsif ( $line =~ /^\s*endmenu/ ) {
694 $inside_config = "";
695 $inside_choice = "";
696 pop @inside_menu;
699 # "if" <expr>
700 elsif ( $line =~ /^\s*if/ ) {
701 $inside_config = "";
702 $line =~ /^\s*if\s+(.*)$/;
703 my $expr = $1;
704 push( @inside_if, $expr );
705 handle_expressions( $expr, $inside_config, $filename, $line_no );
706 $fileinfo{$filename}{iflevel}++;
709 # endif
710 elsif ( $line =~ /^\s*endif/ ) {
711 $inside_config = "";
712 pop(@inside_if);
713 $fileinfo{$filename}{iflevel}--;
716 #range <symbol> <symbol> [if <expr>]
717 elsif ( $line =~ /^\s*range/ ) {
718 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
719 handle_range( $1, $2, $inside_config, $filename, $line_no );
722 # select <symbol> [if <expr>]
723 elsif ( $line =~ /^\s*select/ ) {
724 unless ($inside_config) {
725 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
728 if ( $line =~ /^\s*select\s+(.*)$/ ) {
729 $line = $1;
730 my $expression;
731 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
732 if ($line) {
733 add_referenced_symbol( $line, $filename, $line_no, 'select' );
738 # source <prompt>
739 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
740 my @newfile = load_kconfig_file( $1, $filename, $line_no, 0, $filename, $line_no );
741 unshift( @config_to_parse, @newfile );
742 $parseline[0]{text} = "# '$line'\n";
744 elsif (
745 ( $line =~ /^\s*#/ ) || #comments
746 ( $line =~ /^\s*$/ ) #blank lines
749 # do nothing
751 else {
752 if ($lastline_was_help) {
753 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized - supposed to be inside help?");
755 else {
756 show_error("The line \"$line\" ($filename:$line_no) wasn't recognized");
760 if ( defined $inside_menu[0] ) {
761 $parseline[0]{menus} = "";
763 else {
764 $parseline[0]{menus} = "top";
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} .= "->";
775 push @wholeconfig, @parseline;
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.");
782 elsif ( $fileinfo{$file}{iflevel} < 0 ) {
783 show_error(
784 "$file has " . ( $fileinfo{$file}{iflevel} * -1 ) . " more 'endif' statement(s) than 'if' statements." );
789 #-------------------------------------------------------------------------------
790 #-------------------------------------------------------------------------------
791 sub handle_depends {
792 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
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}++;
799 else {
800 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
803 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
804 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
808 #-------------------------------------------------------------------------------
809 #-------------------------------------------------------------------------------
810 sub add_symbol {
811 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
812 my @inside_if = @{$ifref};
814 #initialize the symbol or increment the use count.
815 if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
816 $symbols{$symbol}{count} = 0;
817 if ($inside_choice) {
818 $symbols{$symbol}{choice} = 1;
820 else {
821 $symbols{$symbol}{choice} = 0;
824 else {
825 $symbols{$symbol}{count}++;
827 if ( $inside_choice && !$symbols{$symbol}{choice} ) {
828 show_error( "$symbol entry at $filename:$line_no has already been created inside a choice block "
829 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
831 elsif ( !$inside_choice && $symbols{$symbol}{choice} ) {
832 show_error( "$symbol entry at $filename:$line_no has already been created outside a choice block "
833 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
835 elsif ( $inside_choice && $symbols{$symbol}{choice} ) {
836 show_error( "$symbol entry at $filename:$line_no has already been created inside another choice block "
837 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
841 # add the location of this instance
842 my $symcount = $symbols{$symbol}{count};
843 $symbols{$symbol}{$symcount}{file} = $filename;
844 $symbols{$symbol}{$symcount}{line_no} = $line_no;
846 #Add the menu structure
847 if ( defined @$menu_array_ref[0] ) {
848 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
851 #Add any 'if' statements that the symbol is inside as dependencies
852 if (@inside_if) {
853 my $dep_num = 0;
854 for my $dependency (@inside_if) {
855 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
856 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
857 $dep_num++;
862 #-------------------------------------------------------------------------------
863 # handle range
864 #-------------------------------------------------------------------------------
865 sub handle_range {
866 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
868 my $expression;
869 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
871 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
872 my $checkrange1 = $1;
873 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
874 my $checkrange2 = $1;
876 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
877 show_error("Range entry in $filename line $line_no value 1 ($range1) is greater than value 2 ($range2).");
880 if ($inside_config) {
881 if ( exists( $symbols{$inside_config}{range1} ) ) {
882 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
883 if ($show_note_output) {
884 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
885 print " not match the previously defined range $symbols{$inside_config}{range1}"
886 . " $symbols{$inside_config}{range2}";
887 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
891 else {
892 $symbols{$inside_config}{range1} = $range1;
893 $symbols{$inside_config}{range2} = $range2;
894 $symbols{$inside_config}{range_file} = $filename;
895 $symbols{$inside_config}{range_line_no} = $line_no;
898 else {
899 show_error("Range entry at $filename:$line_no is not inside a config block.");
903 #-------------------------------------------------------------------------------
904 # handle_default
905 #-------------------------------------------------------------------------------
906 sub handle_default {
907 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
908 my $expression;
909 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
911 if ($inside_config) {
912 handle_expressions( $default, $inside_config, $filename, $line_no );
913 my $sym_num = $symbols{$inside_config}{count};
915 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
916 $symbols{$inside_config}{$sym_num}{default_max} = 0;
918 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
919 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
920 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
921 if ($expression) {
922 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
925 elsif ($inside_choice) {
926 handle_expressions( $default, $inside_config, $filename, $line_no );
928 else {
929 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
933 #-------------------------------------------------------------------------------
934 # handle_if_line
935 #-------------------------------------------------------------------------------
936 sub handle_if_line {
937 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
939 if ( $exprline !~ /if/ ) {
940 return ( $exprline, "" );
943 #remove any quotes that might have an 'if' in them
944 my $savequote;
945 if ( $exprline =~ /^\s*("[^"]+")/ ) {
946 $savequote = $1;
947 $exprline =~ s/^\s*("[^"]+")//;
950 my $expr = "";
951 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
952 $expr = $1;
953 $exprline =~ s/\s*if\s+.*$//;
955 if ($expr) {
956 handle_expressions( $expr, $inside_config, $filename, $line_no );
960 if ($savequote) {
961 $exprline = $savequote;
964 return ( $exprline, $expr );
967 #-------------------------------------------------------------------------------
968 # handle_expressions - log which symbols are being used
969 #-------------------------------------------------------------------------------
970 sub handle_expressions {
971 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
973 return unless ($exprline);
975 #filter constant symbols first
976 if ( $exprline =~ /^\s*"?([yn])"?\s*$/ ) { # constant y/n
977 return;
979 elsif ( $exprline =~ /^\s*"?((?:-)\d+)"?\s*$/ ) { # int values
980 return;
982 elsif ( $exprline =~ /^\s*"?((?:-)?(?:0x)?\p{XDigit})+"?\s*$/ ) { # hex values
983 return;
985 elsif ( $exprline =~ /^\s*("[^"]*")\s*$/ ) { # String values
986 return;
988 elsif ( $exprline =~ /^\s*([A-Za-z0-9_]+)\s*$/ ) { # <symbol> (1)
989 add_referenced_symbol( $1, $filename, $line_no, 'expression' );
991 elsif ( $exprline =~ /^\s*!(.+)$/ ) { # '!' <expr> (5)
993 handle_expressions( $1, $inside_config, $filename, $line_no );
995 elsif ( $exprline =~ /^\s*\(([^)]+)\)\s*$/ ) { # '(' <expr> ')' (4)
996 handle_expressions( $1, $inside_config, $filename, $line_no );
998 elsif ( $exprline =~ /^\s*(.+)\s*!=\s*(.+)\s*$/ ) { # <symbol> '!=' <symbol> (3)
999 handle_expressions( $1, $inside_config, $filename, $line_no );
1000 handle_expressions( $2, $inside_config, $filename, $line_no );
1002 elsif ( $exprline =~ /^\s*(.+)\s*=\s*(.+)\s*$/ ) { # <symbol> '=' <symbol> (2)
1003 handle_expressions( $1, $inside_config, $filename, $line_no );
1004 handle_expressions( $2, $inside_config, $filename, $line_no );
1006 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*&&\s*(.+)\s*$/ ) { # <expr> '&&' <expr> (6)
1007 handle_expressions( $1, $inside_config, $filename, $line_no );
1008 handle_expressions( $2, $inside_config, $filename, $line_no );
1010 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*\|\|\s*(.+)\s*$/ ) { # <expr> '||' <expr> (7)
1011 handle_expressions( $1, $inside_config, $filename, $line_no );
1012 handle_expressions( $2, $inside_config, $filename, $line_no );
1015 # work around kconfig spec violation for now - paths not in quotes
1016 elsif ( $exprline =~ /^\s*([A-Za-z0-9_\-\/]+)\s*$/ ) { # <symbol> (1)
1017 return;
1019 else {
1020 show_error("Unrecognized expression '$exprline' in $filename line $line_no.");
1023 return;
1026 #-------------------------------------------------------------------------------
1027 # add_referenced_symbol
1028 #-------------------------------------------------------------------------------
1029 sub add_referenced_symbol {
1030 my ( $symbol, $filename, $line_no, $reftype ) = @_;
1031 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;
1036 else {
1037 $referenced_symbols{$symbol}{count} = 0;
1038 $referenced_symbols{$symbol}{0}{filename} = $filename;
1039 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
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;
1048 #-------------------------------------------------------------------------------
1049 # handle_help
1050 #-------------------------------------------------------------------------------
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
1054 my $help_keyword_whitespace = "";
1056 sub handle_help {
1057 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1059 if ($inside_help) {
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 ) {
1066 show_error("$filename:$line_no - help text starts with no whitespace.");
1067 return $inside_help;
1069 elsif ($help_keyword_whitespace eq $help_whitespace) {
1070 show_error("$filename:$line_no - help text needs additional indentation.");
1071 return $inside_help;
1075 #help ends at the first line which has a smaller indentation than the first line of the help text.
1076 if ( ( $line !~ /^$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
1077 $inside_help = 0;
1078 $help_whitespace = "";
1079 $help_keyword_whitespace = "";
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 );
1087 if ( ($help_keyword_whitespace eq $help_whitespace) && ( $line !~ /^[\r\n]+/ ) ) {
1088 show_error("$filename:$line_no - help text needs additional indentation.");
1092 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
1093 $inside_help = $line_no;
1094 $line =~ /^(\s+)/;
1095 $help_keyword_whitespace = $1;
1096 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
1097 if ($show_note_output) {
1098 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
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} = ();
1108 return $inside_help;
1112 #-------------------------------------------------------------------------------
1113 # handle_type
1114 #-------------------------------------------------------------------------------
1115 sub handle_type {
1116 my ( $type, $inside_config, $filename, $line_no ) = @_;
1118 my $expression;
1119 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
1121 if ( $type =~ /tristate/ ) {
1122 show_warning("$filename:$line_no - tristate types are not used.");
1125 if ($inside_config) {
1126 if ( exists( $symbols{$inside_config}{type} ) ) {
1127 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
1128 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}." );
1133 else {
1134 $symbols{$inside_config}{type} = $type;
1135 $symbols{$inside_config}{type_file} = $filename;
1136 $symbols{$inside_config}{type_line_no} = $line_no;
1139 else {
1140 show_error("Type entry at $filename:$line_no is not inside a config block.");
1144 #-------------------------------------------------------------------------------
1145 # handle_prompt
1146 #-------------------------------------------------------------------------------
1147 sub handle_prompt {
1148 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1150 my $expression;
1151 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
1153 if ($inside_config) {
1154 if ( $prompt !~ /^\s*$/ ) {
1155 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
1156 $prompt = $1;
1159 #display an error if there's a prompt at the top menu level
1160 if ( !defined @$menu_array_ref[0] ) {
1161 show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu"
1162 . " at $filename:$line_no." );
1165 my $sym_num = $symbols{$inside_config}{count};
1166 if ( !exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
1167 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
1169 else {
1170 $symbols{$inside_config}{$sym_num}{prompt_max}++;
1172 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;
1176 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_menu} = @$menu_array_ref;
1177 if ($expression) {
1178 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
1182 elsif ($inside_choice) {
1184 #do nothing
1186 else {
1187 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
1191 #-------------------------------------------------------------------------------
1192 # simple_line_checks - Does some basic checks on the current line, then cleans the line
1193 # up for further processing.
1194 #-------------------------------------------------------------------------------
1195 sub simple_line_checks {
1196 my ( $line, $filename, $line_no ) = @_;
1198 #check for spaces instead of tabs
1199 if ( $line =~ /^ +/ ) {
1200 show_error("$filename:$line_no starts with a space.");
1203 #verify a linefeed at the end of the line
1204 if ( $line !~ /.*\n/ ) {
1205 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)" );
1207 $line =~ s/\s*$//;
1209 else {
1210 chop($line);
1213 return $line;
1216 #-------------------------------------------------------------------------------
1217 # load_kconfig_file - Loads a single Kconfig file or expands * wildcard
1218 #-------------------------------------------------------------------------------
1219 sub load_kconfig_file {
1220 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
1221 my @file_data;
1222 my @dir_file_data;
1224 #recursively handle coreboot's new source glob operator
1225 if ( $input_file =~ /^(.*?)\/\*\/(.*)$/ ) {
1226 my $dir_prefix = $1;
1227 my $dir_suffix = $2;
1228 if ( -d "$dir_prefix" ) {
1230 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
1231 my @dirlist = sort { $a cmp $b } readdir(D);
1232 closedir(D);
1234 while ( my $directory = shift @dirlist ) {
1236 #ignore non-directory files
1237 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ ) ) {
1238 push @dir_file_data,
1239 load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1240 $input_file, $loadline, 1, $loadfile, $loadline );
1245 #the directory should exist when using a glob
1246 else {
1247 show_warning("Could not find dir '$dir_prefix'");
1251 #if the file exists, try to load it.
1252 elsif ( -e "$input_file" ) {
1254 #throw a warning if the file has already been loaded.
1255 if ( exists $loaded_files{$input_file} ) {
1256 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
1259 #load the file's contents and mark the file as loaded for checking later
1260 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
1261 @file_data = <$HANDLE>;
1262 close $HANDLE;
1263 $loaded_files{$input_file} = "'$loadfile' line $loadline";
1266 # if the file isn't being loaded from a glob, it should exist.
1267 elsif ( $expanded == 0 ) {
1268 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
1271 my $line_in_file = 0;
1272 while ( my $line = shift @file_data ) {
1274 #handle line continuation.
1275 my $continue_line = 0;
1276 while ( $line =~ /(.*)\s+\\$/ ) {
1277 my $text = $1;
1279 # get rid of leading whitespace on all but the first and last lines
1280 $text =~ s/^\s*/ / if ($continue_line);
1282 $dir_file_data[$line_in_file]{text} .= $text;
1283 $line = shift @file_data;
1284 $continue_line++;
1286 #put the data into the continued lines (other than the first)
1287 $line =~ /^\s*(.*)\s*$/;
1289 $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n";
1290 $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file;
1291 $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1;
1293 #get rid of multiple leading spaces for last line
1294 $line = " $1\n";
1297 $dir_file_data[$line_in_file]{text} .= $line;
1298 $dir_file_data[$line_in_file]{filename} = $input_file;
1299 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1301 $line_in_file++;
1302 if ($continue_line) {
1303 $line_in_file += $continue_line;
1307 if ($topfile) {
1308 my %file_data;
1309 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
1310 $file_data{filename} = $topfile;
1311 $file_data{file_line_no} = "($topline)";
1312 unshift( @dir_file_data, \%file_data );
1315 return @dir_file_data;
1318 #-------------------------------------------------------------------------------
1319 # print_wholeconfig - prints out the parsed Kconfig file
1320 #-------------------------------------------------------------------------------
1321 sub print_wholeconfig {
1323 return unless $print_full_output;
1325 for ( my $i = 0 ; $i < $#wholeconfig ; $i++ ) {
1326 my $line = $wholeconfig[$i];
1327 chop( $line->{text} );
1329 #replace tabs with spaces for consistency
1330 $line->{text} =~ s/\t/ /g;
1331 printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text};
1335 #-------------------------------------------------------------------------------
1336 # check_if_file_referenced - checks for kconfig files that are not being parsed
1337 #-------------------------------------------------------------------------------
1338 sub check_if_file_referenced {
1339 my $filename = $File::Find::name;
1340 if ( ( $filename =~ /Kconfig/ )
1341 && ( !$filename =~ /\.orig$/ )
1342 && ( !$filename =~ /~$/ )
1343 && ( !exists $loaded_files{$filename} ) )
1345 show_warning("'$filename' is never referenced");
1349 #-------------------------------------------------------------------------------
1350 # check_arguments parse the command line arguments
1351 #-------------------------------------------------------------------------------
1352 sub check_arguments {
1353 my $show_usage = 0;
1354 GetOptions(
1355 'help|?' => sub { usage() },
1356 'e|errors_off' => \$suppress_error_output,
1357 'n|notes' => \$show_note_output,
1358 'o|output=s' => \$output_file,
1359 'p|print' => \$print_full_output,
1360 'w|warnings_off' => \$suppress_warning_output,
1361 'path=s' => \$top_dir,
1362 'c|config=s' => \$config_file,
1363 'G|no_git_grep' => \$dont_use_git_grep,
1366 if ($suppress_error_output) {
1367 $suppress_warning_output = 1;
1369 if ($suppress_warning_output) {
1370 $show_note_output = 0;
1374 #-------------------------------------------------------------------------------
1375 # usage - Print the arguments for the user
1376 #-------------------------------------------------------------------------------
1377 sub usage {
1378 print "kconfig_lint <options>\n";
1379 print " -o|--output=file Set output filename\n";
1380 print " -p|--print Print full output\n";
1381 print " -e|--errors_off Don't print warnings or errors\n";
1382 print " -w|--warnings_off Don't print warnings\n";
1383 print " -n|--notes Show minor notes\n";
1384 print " --path=dir Path to top level kconfig\n";
1385 print " -c|--config=file Filename of config file to load\n";
1386 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
1388 exit(0);