xref: /aosp_15_r20/external/coreboot/util/lint/kconfig_lint (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/usr/bin/env perl
2#
3# SPDX-License-Identifier: GPL-2.0-only
4
5# perltidy -l=123
6
7package kconfig_lint;
8
9use strict;
10use warnings;
11use English qw( -no_match_vars );
12use File::Find;
13use Getopt::Long;
14use Getopt::Std;
15
16# 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
22my $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
27my $dont_use_git_grep       = 0;
28my $include_site_local      = 0;
29
30# Globals
31my $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
34my $warnings_found = 0;
35my $exclude_dirs_and_files =
36  '^build/\|^coreboot-builds/\|^configs/\|^util/\|^\.git/\|^payloads\|^Documentation\|^3rdparty'
37  . '\|' .                            # directories to exclude when searching for used symbols
38  '\.config\|\.txt$\|\.tex$\|\.tags\|/kconfig.h\|\.fmd'; #files to exclude when looking for symbols
39my $payload_files_to_check='payloads/Makefile.mk payloads/external/Makefile.mk';
40my $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
48
49my $exclude_unused = '_SPECIFIC_OPTIONS';
50
51Main();
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
62	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	}
67    if ( !$include_site_local) {
68        $exclude_dirs_and_files = "^site-local\|" . $exclude_dirs_and_files;
69    }
70
71    open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
72
73    if ( defined $top_dir ) {
74        chdir $top_dir or die "Error: can't cd to $top_dir\n";
75    }
76
77    die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir );
78
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
82    load_config($config_file) if ($config_file);
83
84    check_type();
85    check_defaults();
86    check_referenced_symbols();
87
88    collect_used_symbols();
89    check_used_symbols();
90    check_for_ifdef();
91    check_for_def();
92    check_config_macro();
93    check_selected_symbols();
94
95    # 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
103    print_wholeconfig();
104
105    if ($errors_found) {
106        print "# $errors_found errors";
107        if ($warnings_found) {
108            print ", $warnings_found warnings";
109        }
110        print "\n";
111    }
112
113    exit( $errors_found + $warnings_found );
114}
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    }
136}
137
138#-------------------------------------------------------------------------------
139# 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#-------------------------------------------------------------------------------
181# 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 ) {
192        if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) {
193            my $file   = $1;
194            my $lineno = $2;
195            my $symbol = $3;
196
197            if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
198                show_error( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
199                      . "  Symbols of type '$symbols{$symbol}{type}' are always defined." );
200            }
201        } elsif ( $line =~ /^([^:]+):(\d+):.+defined\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) {
202            my $file   = $1;
203            my $lineno = $2;
204            my $symbol = $3;
205
206            if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
207                show_error( "defined(CONFIG_$symbol) used at $file:$lineno."
208                      . "  Symbols of type '$symbols{$symbol}{type}' are always defined." );
209            }
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+)/ ) {
226            my $file   = $1;
227            my $lineno = $2;
228            my $symbol = $3;
229
230            if ( ( exists $symbols{$symbol} ) ) {
231                show_error("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
232            }
233            else {
234                show_error( "#define 'CONFIG_$symbol' used at $file:$lineno."
235                      . "  Other #defines should not look like Kconfig symbols." );
236            }
237        }
238    }
239}
240
241#-------------------------------------------------------------------------------
242# 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#-------------------------------------------------------------------------------
267# 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.
274#-------------------------------------------------------------------------------
275sub check_config_macro {
276    my @is_enabled_symbols = @collected_symbols;
277
278    #sort through symbols found by grep and store them in a hash for easy access
279    while ( my $line = shift @is_enabled_symbols ) {
280        if ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG\(.*)/ ) {
281            my $file   = $1;
282            my $lineno = $2;
283            $line = $3;
284            while ( $line =~ /(.*)\bCONFIG\(([^)]*)\)(.*)/ ) {
285                my $symbol = $2;
286                $line = $1 . $3;
287
288                #make sure that the type is bool
289                if ( exists $symbols{$symbol} ) {
290                    if ( $symbols{$symbol}{type} ne "bool" ) {
291                        show_error( "CONFIG($symbol) used at $file:$lineno."
292                              . "  CONFIG() is only valid for type 'bool', not '$symbols{$symbol}{type}'." );
293                    }
294                } elsif ($symbol =~ /\S+##\S+/ ) {
295                    show_warning( "C preprocessor macro CONFIG_$symbol found at $file:$lineno." );
296                } else {
297                    show_error("CONFIG() used on unknown value ($symbol) at $file:$lineno.");
298                }
299            }
300        } elsif ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
301            my $file   = $1;
302            my $lineno = $2;
303            $line = $3;
304            if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) {
305                show_error("# uninterpreted IS_ENABLED at $file:$lineno: $line");
306                next;
307            }
308            while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
309                my $symbol = $2;
310                $line = $1 . $3;
311                show_error("IS_ENABLED(CONFIG_$symbol) at $file:$lineno is deprecated. Use CONFIG($symbol) instead.");
312            }
313        } 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;
317            # If the type is bool, give a warning that CONFIG() should be used
318            if ( exists $symbols{$symbol} ) {
319                if ( $symbols{$symbol}{type} eq "bool" ) {
320                    show_error( "#if CONFIG_$symbol used at $file:$lineno."
321                          . "  CONFIG($symbol) should be used for type 'bool'" );
322                }
323            }
324        } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if.*(?:&&|\|\|)\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?$/ ) {
325            my $file   = $1;
326            my $lineno = $2;
327            my $symbol = $3;
328            # If the type is bool, give a warning that CONFIG() should be used
329            if ( exists $symbols{$symbol} ) {
330                if ( $symbols{$symbol}{type} eq "bool" ) {
331                    show_error( "#if CONFIG_$symbol used at $file:$lineno."
332                          . "  CONFIG($symbol) should be used for type 'bool'" );
333                }
334            }
335        } 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" ) {
345                            show_error( "Naked reference to CONFIG_$symbol used at $file:$lineno."
346                                . "  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            }
353        }
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
378                my $filename = $symbols{$sym}{$sym_num}{file};
379                my $line_no  = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
380
381                # Make sure there's a type set for the symbol
382                next if (!defined $symbols{$sym}{type});
383
384                # 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 "
387                               . "$symbols{$sym}{choice}, is not allowed.");
388                }
389
390                # 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]/) {
413                                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.");
414                            } 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
421                #if a default is already set, display an error
422                if ($default_set) {
423                    show_error( "Default for '$sym' referenced at $filename:$line_no will never be set"
424                          . " - overridden by default set at $default_filename:$default_line_no" );
425                }
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
453        #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};
457            show_error("Undefined Symbol '$key' used at $filename:$line_no.");
458        }
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) {
468        @collected_symbols = `grep -Irn -- "CONFIG\\(_\\|(\\)" | grep -v '$exclude_dirs_and_files'; grep -In -- "CONFIG\\(_\\|(\\)" $payload_files_to_check`;
469    }
470    else {
471        @collected_symbols = `git grep -In -- "CONFIG\\(_\\|(\\)" | grep -v '$exclude_dirs_and_files'; git grep -In -- "CONFIG\\(_\\|(\\)" $payload_files_to_check`;
472    }
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 ) {
478        while ( $line =~ /[^A-Za-z0-9_]CONFIG(?:_|\()([A-Za-z0-9_]+)/g ) {
479            my $symbol   = $1;
480            my $filename = "";
481            if ( $line =~ /^([^:]+):/ ) {
482                $filename = $1;
483            }
484
485            if ( exists $used_symbols{$symbol}{count} ) {
486                $used_symbols{$symbol}{count}++;
487            }
488            else {
489                $used_symbols{$symbol}{count} = 0;
490            }
491            $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
492        }
493    }
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
504        if ( $key =~ /$exclude_unused/ ) {
505            next;
506        }
507
508        #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};
518            show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
519        }
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 = "";
536    my $choice_symbol = "";
537    my $configs_inside_choice;
538    my %fileinfo;
539
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
543    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---"
549        my $lastline_was_help = $inside_help;
550        $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
565        elsif ( $line =~ /^\s*config\s+/ ) {
566            $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
567            my $symbol = $1;
568            $inside_config = $symbol;
569            if ($inside_choice) {
570                $configs_inside_choice++;
571            }
572            add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
573        }
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 );
624		$choice_symbol = $symbol;
625            }
626            $inside_config         = "";
627            $inside_choice         = "$filename:$line_no";
628            $configs_inside_choice = 0;
629
630            # Kconfig verifies that choice blocks have a prompt
631        }
632
633        # endchoice
634        elsif ( $line =~ /^\s*endchoice/ ) {
635            $inside_config = "";
636            if ( !$inside_choice ) {
637                show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
638            }
639
640            $inside_choice = "";
641            if (( $configs_inside_choice == 0 ) &&
642	        ( $choice_symbol eq "" )) {
643                show_error("unnamed choice block has no symbols at $filename:$line_no.");
644            }
645            $configs_inside_choice = 0;
646	    $choice_symbol="";
647        }
648
649        # [optional]
650        elsif ( $line =~ /^\s*optional/ ) {
651            if ($inside_config) {
652                show_error( "Keyword 'optional' appears inside config for '$inside_config'"
653                      . " at $filename:$line_no.  This is not valid." );
654            }
655            if ( !$inside_choice ) {
656                show_error( "Keyword 'optional' appears outside of a choice block"
657                      . " at $filename:$line_no.  This is not valid." );
658            }
659        }
660
661        # mainmenu <prompt>
662        elsif ( $line =~ /^\s*mainmenu/ ) {
663            $inside_config = "";
664
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
668        }
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
683        # visible if <expr>
684        elsif ( $line =~ /^\s*visible if.*$/ ) {
685        # Must come directly after menu line (and on a separate line)
686        # but kconfig already checks for that.
687        # Ignore it.
688        }
689
690        # 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 );
704            $fileinfo{$filename}{iflevel}++;
705        }
706
707        # endif
708        elsif ( $line =~ /^\s*endif/ ) {
709            $inside_config = "";
710            pop(@inside_if);
711            $fileinfo{$filename}{iflevel}--;
712        }
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) {
723                show_error("Keyword 'select' appears outside of config at $filename:$line_no.  This is not valid.");
724            }
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) {
731                    add_referenced_symbol( $line, $filename, $line_no, 'select' );
732                }
733            }
734        }
735
736        # source <prompt>
737        elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
738            my $input_file = $1;
739            $parseline[0]{text} = "# '$line'\n";
740            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            }
744        }
745        elsif (
746            ( $line =~ /^\s*#/ ) ||    #comments
747            ( $line =~ /^\s*$/ )       #blank lines
748          )
749        {
750            # do nothing
751        }
752        else {
753            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            }
759        }
760
761        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        }
776        push @wholeconfig, @parseline;
777    }
778
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    }
788}
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 {
812    my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
813    my @inside_if = @{$ifref};
814
815    #initialize the symbol or increment the use count.
816    if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
817        $symbols{$symbol}{count} = 0;
818        # remember the location of the choice (or "")
819        $symbols{$symbol}{choice} = $inside_choice;
820    }
821    else {
822        $symbols{$symbol}{count}++;
823        if ( $inside_choice && $symbols{$symbol}{choice} ) {
824            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        }
827    }
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) ) ) {
865        show_error("Range entry in $filename line $line_no value 1 ($range1) is greater than value 2 ($range2).");
866    }
867
868    if ($inside_config) {
869        if ( exists( $symbols{$inside_config}{range1} ) ) {
870            if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
871                if ($show_note_output) {
872                    print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
873                    print " not match the previously defined range $symbols{$inside_config}{range1}"
874                      . " $symbols{$inside_config}{range2}";
875                    print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
876                }
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 {
887        show_error("Range entry at $filename:$line_no is not inside a config block.");
888    }
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 {
917        show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
918    }
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#-------------------------------------------------------------------------------
956# 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
983#-------------------------------------------------------------------------------
984sub handle_expressions {
985    my ( $exprline, $inside_config, $filename, $line_no ) = @_;
986
987    my $strip = qr/\s*(.*[^\s]+)\s*/;
988
989    my $parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
990    my $quotes = qr/"[^"]*"/;
991    my $balanced = qr/((?:$parens|$quotes|[^\(\)"])+)/;
992
993    if ( $exprline =~ /^\s*$balanced\s*(?:\|\||&&)\s*(.+)$/ ) {
994        # <expr> '||' <expr>, <expr> '&&' <expr>                  (8)(7)
995        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*!(.+)$/ ) {
1000        # '!' <expr>                                                 (6)
1001        handle_expressions( $1, $inside_config, $filename, $line_no );
1002    }
1003    elsif ( $exprline =~ /^\s*$parens\s*$/ ) {
1004        # '(' <expr> ')'                                             (5)
1005        $exprline =~ /^\s*\((.*)\)\s*$/;
1006        handle_expressions( $1, $inside_config, $filename, $line_no );
1007    }
1008    elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*(?:[<>]=?|!=)$strip$/ ) {
1009        # <symbol> '<' <symbol>, <symbol> '!=' <symbol>, etc.     (4)(3)
1010        my ( $lhs, $rhs ) = ( $1, $2 );
1011        handle_symbol( $lhs, $filename, $line_no );
1012        handle_symbol( $rhs, $filename, $line_no );
1013    }
1014    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 );
1019    }
1020    elsif ( $exprline =~ /^$strip$/ ) {
1021        # <symbol>                                                   (1)
1022        handle_symbol( $1, $filename, $line_no );
1023    }
1024}
1025
1026#-------------------------------------------------------------------------------
1027# add_referenced_symbol
1028#-------------------------------------------------------------------------------
1029sub 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;
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    }
1041
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    }
1046}
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
1054    my $help_keyword_whitespace = "";
1055
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 ) {
1066                    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.");
1071                    return $inside_help;
1072                }
1073            }
1074
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 = "";
1080            }
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                }
1087                if ( ($help_keyword_whitespace eq $help_whitespace) && ( $line !~ /^[\r\n]+/ ) ) {
1088                    show_error("$filename:$line_no - help text needs additional indentation.");
1089                }
1090            }
1091        }
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";
1099                }
1100            }
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
1121    if ( $type =~ /tristate/ ) {
1122        show_error("$filename:$line_no - tristate types are not used.");
1123    }
1124
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}." );
1131            }
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 {
1140        show_error("Type entry at $filename:$line_no is not inside a config block.");
1141    }
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
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." );
1163            }
1164
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;
1168            }
1169            else {
1170                $symbols{$inside_config}{$sym_num}{prompt_max}++;
1171            }
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;
1175
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;
1179            }
1180        }
1181    }
1182    elsif ($inside_choice) {
1183
1184        #do nothing
1185    }
1186    else {
1187        show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
1188    }
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 =~ /^ +/ ) {
1200        show_error("$filename:$line_no starts with a space.");
1201    }
1202
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*$//;
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
1225    if ( $input_file =~ /^(.*?)\/(\w*)\*(\w*)\/(.*)$/ ) {
1226        my $dir_prefix = $1;
1227        my $dir_glob_prefix = $2;
1228        my $dir_glob_suffix = $3;
1229        my $dir_suffix = $4;
1230        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
1239                if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ )
1240                     && ( $directory =~ /\Q$dir_glob_prefix\E.*\Q$dir_glob_suffix\E/ ) ) {
1241                    push @dir_file_data,
1242                      load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1243                        $input_file, $loadline, 1, $loadfile, $loadline );
1244                }
1245            }
1246        }
1247
1248        #the directory should exist when using a glob
1249        else {
1250            show_error("Could not find dir '$dir_prefix'");
1251        }
1252    }
1253
1254    #if the file exists, try to load it.
1255    elsif ( -e "$input_file" ) {
1256
1257        #throw a warning if the file has already been loaded.
1258        if ( exists $loaded_files{$input_file} ) {
1259            show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
1260        }
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 ) {
1271        show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
1272    }
1273
1274    my $line_in_file = 0;
1275    while ( my $line = shift @file_data ) {
1276
1277        #handle line continuation.
1278        my $continue_line = 0;
1279        while ( $line =~ /(.*)\s+\\$/ ) {
1280            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
1292            $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;
1295
1296            #get rid of multiple leading spaces for last line
1297            $line = " $1\n";
1298        }
1299
1300        $dir_file_data[$line_in_file]{text} .= $line;
1301        $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;
1312        $file_data{text}         = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
1313        $file_data{filename}     = $topfile;
1314        $file_data{file_line_no} = "($topline)";
1315        unshift( @dir_file_data, \%file_data );
1316    }
1317
1318    return @dir_file_data;
1319}
1320
1321#-------------------------------------------------------------------------------
1322# print_wholeconfig - prints out the parsed Kconfig file
1323#-------------------------------------------------------------------------------
1324sub print_wholeconfig {
1325
1326    return unless $print_full_output;
1327
1328    for ( my $i = 0 ; $i <= $#wholeconfig ; $i++ ) {
1329        my $line = $wholeconfig[$i];
1330        chop( $line->{text} );
1331
1332        #replace tabs with spaces for consistency
1333        $line->{text} =~ s/\t/        /g;
1334        printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text};
1335    }
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;
1343    if (   ( $filename =~ /Kconfig/ )
1344        && ( !$filename =~ /\.orig$/ )
1345        && ( !$filename =~ /~$/ )
1346        && ( !exists $loaded_files{$filename} ) )
1347    {
1348        show_warning("'$filename' is never referenced");
1349    }
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() },
1359        'e|errors_off'   => \$suppress_error_output,
1360        'n|notes'        => \$show_note_output,
1361        'o|output=s'     => \$output_file,
1362        'p|print'        => \$print_full_output,
1363        'w|warnings_off' => \$suppress_warning_output,
1364        'path=s'         => \$top_dir,
1365        'c|config=s'     => \$config_file,
1366        'G|no_git_grep'  => \$dont_use_git_grep,
1367        'S|site_local'   => \$include_site_local,
1368    );
1369
1370    if ($suppress_error_output) {
1371        $suppress_warning_output = 1;
1372    }
1373    if ($suppress_warning_output) {
1374        $show_note_output = 0;
1375    }
1376}
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";
1385    print " -e|--errors_off     Don't print warnings or errors\n";
1386    print " -w|--warnings_off   Don't print warnings\n";
1387    print " -n|--notes          Show minor notes\n";
1388    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";
1391    print " -S|--site_local     Include the site-local directory\n";
1392
1393    exit(0);
1394}
1395
13961;
1397