blob: 7fa0d8858bd34138937e2b1d2f9171563060e9ce [file] [log] [blame]
Martin Rothbcaaad12015-10-18 11:16:25 -06001#!/usr/bin/perl
2
3#
4# This file is part of the coreboot project.
5#
6# Copyright (C) 2015 Martin L Roth <gaumless@gmail.com>
7# Copyright (C) 2015 Google, Inc.
8#
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.
12#
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.
17#
Martin Roth1b44f7e2016-01-11 14:31:38 -070018# perltidy -l=123
Martin Rothbcaaad12015-10-18 11:16:25 -060019
20package kconfig_lint;
21
22use strict;
23use warnings;
24use English qw( -no_match_vars );
25use File::Find;
26use Getopt::Long;
27use Getopt::Std;
28
Martin Roth1b44f7e2016-01-11 14:31:38 -070029my $suppress_error_output = 0; # flag to prevent error text
30my $suppress_warning_output = 0; # flag to prevent warning text
31my $show_note_output = 0; # flag to show minor notes text
32my $print_full_output = 0; # flag to print wholeconfig output
33my $output_file = "-"; # filename of output - set stdout by default
Martin Rothd8080172015-11-26 19:12:44 -070034my $dont_use_git_grep = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -060035
36#globals
Martin Roth1b44f7e2016-01-11 14:31:38 -070037my $top_dir = "."; # Directory where Kconfig is run
38my $root_dir = "src"; # Directory of the top level Kconfig file
39my $errors_found = 0; # count of errors
Martin Rothd8080172015-11-26 19:12:44 -070040my $warnings_found = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -070041my $exclude_dirs =
42 '--exclude-dir="build" --exclude-dir="coreboot-builds" '
43 . '--exclude-dir="payloads" --exclude-dir="configs" '
44 . '--exclude-dir="util"'; # directories to exclude when searching for used symbols - NOT USED FOR GIT GREP (TODO)
45my @exclude_files = ( '\.txt$', '\.tex$', 'config', '\.tags' ); #files to exclude when looking for symbols
46my $config_file = ""; # name of config file to load symbol values from.
47my @wholeconfig; # document the entire kconfig structure
48my %loaded_files; # list of each Kconfig file loaded
49my %symbols; # main structure of all symbols declared
50my %referenced_symbols; # list of symbols referenced by expressions or select statements
51my %used_symbols; # structure of symbols used in the tree, and where they're found
52my @collected_symbols; #
Martin Rothb7c39b22016-01-14 09:04:53 -070053my %selected_symbols; # list of symbols that are enabled by a select statement
Martin Rothbcaaad12015-10-18 11:16:25 -060054
55Main();
56
57#-------------------------------------------------------------------------------
58# Main
59#
60# Start by loading and parsing the top level Kconfig, this pulls in the other
61# files. Parsing the tree creates several arrays and hashes that can be used
62# to check for errors
63#-------------------------------------------------------------------------------
64sub Main {
65
66 check_arguments();
67 open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
68
Martin Roth1b44f7e2016-01-11 14:31:38 -070069 if ( defined $top_dir ) {
70 chdir $top_dir or die "Error: can't cd to $top_dir\n";
Martin Rothbcaaad12015-10-18 11:16:25 -060071 }
72
Martin Roth1b44f7e2016-01-11 14:31:38 -070073 die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir );
Martin Rothbcaaad12015-10-18 11:16:25 -060074
75 #load the Kconfig tree, checking what we can and building up all the hash tables
76 build_and_parse_kconfig_tree("$root_dir/Kconfig");
77
78 #run checks based on the data that was found
79 find( \&check_if_file_referenced, $root_dir );
80
81 load_config($config_file) if ($config_file);
82
83 check_defaults();
84 check_referenced_symbols();
85
86 collect_used_symbols();
87 check_used_symbols();
88 check_for_ifdef();
89 check_for_def();
90 check_is_enabled();
Martin Rothb7c39b22016-01-14 09:04:53 -070091 check_selected_symbols();
Martin Rothbcaaad12015-10-18 11:16:25 -060092
93 print_wholeconfig();
94
Martin Rothd8080172015-11-26 19:12:44 -070095 if ($errors_found) {
96 print "# $errors_found errors";
Martin Roth1b44f7e2016-01-11 14:31:38 -070097 if ($warnings_found) {
Martin Rothd8080172015-11-26 19:12:44 -070098 print ", $warnings_found warnings";
99 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700100 print "\n";
Martin Rothd8080172015-11-26 19:12:44 -0700101 }
102
Martin Roth1b44f7e2016-01-11 14:31:38 -0700103 exit( $errors_found + $warnings_found );
Martin Rothd8080172015-11-26 19:12:44 -0700104}
105
106#-------------------------------------------------------------------------------
107# Print and count errors
108#-------------------------------------------------------------------------------
109sub show_error {
110 my ($error_msg) = @_;
111 unless ($suppress_error_output) {
112 print "#!!!!! Error: $error_msg\n";
113 $errors_found++;
114 }
115}
116
117#-------------------------------------------------------------------------------
118# Print and count warnings
119#-------------------------------------------------------------------------------
120sub show_warning {
121 my ($warning_msg) = @_;
122 unless ($suppress_warning_output) {
123 print "#!!!!! Warning: $warning_msg\n";
124 $warnings_found++;
125 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600126}
127
128#-------------------------------------------------------------------------------
Martin Rothb7c39b22016-01-14 09:04:53 -0700129# check selected symbols for validity
130# they must be bools
131# they cannot select symbols created in 'choice' blocks
132#-------------------------------------------------------------------------------
133sub check_selected_symbols {
134
135 #loop through symbols found in expressions and used by 'select' keywords
136 foreach my $symbol ( sort ( keys %selected_symbols ) ) {
137 my $type_failure = 0;
138 my $choice_failure = 0;
139
140 #errors selecting symbols that don't exist are already printed, so we
141 #don't need to print them again here
142
143 #make sure the selected symbols are bools
144 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "bool" ) ) {
145 $type_failure = 1;
146 }
147
148 #make sure we're not selecting choice symbols
149 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{choice} ) ) {
150 $choice_failure = 1;
151 }
152
153 #loop through each instance of the symbol to print out all of the failures
154 for ( my $i = 0 ; $i <= $referenced_symbols{$symbol}{count} ; $i++ ) {
155 next if ( !exists $selected_symbols{$symbol}{$i} );
156 my $file = $referenced_symbols{$symbol}{$i}{filename};
157 my $lineno = $referenced_symbols{$symbol}{$i}{line_no};
158 if ($type_failure) {
159 show_error(
160 "CONFIG_$symbol' selected at $file:$lineno." . " Selects only work on symbols of type bool." );
161 }
162 if ($choice_failure) {
163 show_error(
164 "'CONFIG_$symbol' selected at $file:$lineno." . " Symbols created in a choice cannot be selected." );
165 }
166 }
167 }
168}
169
170#-------------------------------------------------------------------------------
Martin Rothbcaaad12015-10-18 11:16:25 -0600171# check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and
172# #if defined(CONFIG_[symbol_name]).
173#
174# #ifdef symbol is valid for strings, but bool, hex, and INT are always defined.
175# #if defined(symbol) && symbol is also a valid construct.
176#-------------------------------------------------------------------------------
177sub check_for_ifdef {
178 my @ifdef_symbols = @collected_symbols;
179
180 #look for #ifdef SYMBOL
181 while ( my $line = shift @ifdef_symbols ) {
182 if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s+CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700183 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600184 my $lineno = $2;
185 my $symbol = $3;
186
Martin Roth1b44f7e2016-01-11 14:31:38 -0700187 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
188 show_error( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
189 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600190 }
191 }
192 }
193
194 # look for (#if) defined SYMBOL
195 @ifdef_symbols = @collected_symbols;
196 while ( my $line = shift @ifdef_symbols ) {
197 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700198 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600199 my $lineno = $2;
200 my $symbol = $3;
201
202 #ignore '#if defined(symbol) && symbol' type statements
Martin Roth1b44f7e2016-01-11 14:31:38 -0700203 next
204 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_$symbol.*(&&|\|\|)\s*!?\s*\(?\s*CONFIG_$symbol/ );
Martin Rothbcaaad12015-10-18 11:16:25 -0600205
Martin Roth1b44f7e2016-01-11 14:31:38 -0700206 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." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600209 }
210 }
211 }
212}
213
214#-------------------------------------------------------------------------------
215# check_for_def - Look for instances of #define CONFIG_[symbol_name]
216#
217# Symbols should not be redefined outside of Kconfig, and #defines should not
218# look like symbols
219#-------------------------------------------------------------------------------
220sub check_for_def {
221 my @def_symbols = @collected_symbols;
222
223 #look for #ifdef SYMBOL
224 while ( my $line = shift @def_symbols ) {
225 if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700226 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600227 my $lineno = $2;
228 my $symbol = $3;
229
Martin Roth1b44f7e2016-01-11 14:31:38 -0700230 if ( ( exists $symbols{$symbol} ) ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700231 show_warning("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
Martin Roth1b44f7e2016-01-11 14:31:38 -0700232 }
233 else {
234 show_warning( "#define 'CONFIG_$symbol' used at $file:$lineno."
235 . " Other #defines should not look like Kconfig symbols." );
236 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600237 }
238 }
239}
240
241#-------------------------------------------------------------------------------
242# check_is_enabled - The IS_ENABLED() macro is only valid for symbols of type
243# bool. It would probably work on type hex or int if the value was 0 or 1, but
244# this seems like a bad plan. Using it on strings is dead out.
245#-------------------------------------------------------------------------------
246sub check_is_enabled {
Martin Rothb6acc302015-11-27 18:51:19 -0700247 my @is_enabled_symbols = @collected_symbols;
Martin Rothbcaaad12015-10-18 11:16:25 -0600248
249 #sort through symbols found by grep and store them in a hash for easy access
250 while ( my $line = shift @is_enabled_symbols ) {
Martin Rothb6acc302015-11-27 18:51:19 -0700251 if ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700252 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600253 my $lineno = $2;
Martin Rothb6acc302015-11-27 18:51:19 -0700254 $line = $3;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700255 if ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
Martin Rothb6acc302015-11-27 18:51:19 -0700256 show_warning("# uninterpreted IS_ENABLED at $file:$lineno: $line");
257 next;
Martin Rothbcaaad12015-10-18 11:16:25 -0600258 }
Martin Rothb6acc302015-11-27 18:51:19 -0700259 while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
260 my $symbol = $2;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700261 $line = $1 . $3;
Martin Rothb6acc302015-11-27 18:51:19 -0700262
263 #make sure that
Martin Roth1b44f7e2016-01-11 14:31:38 -0700264 if ( exists $symbols{$symbol} ) {
265 if ( $symbols{$symbol}{type} ne "bool" ) {
266 show_error( "IS_ENABLED(CONFIG_$symbol) used at $file:$lineno."
267 . " IS_ENABLED is only valid for type 'bool', not '$symbols{$symbol}{type}'." );
Martin Rothb6acc302015-11-27 18:51:19 -0700268 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700269 }
270 else {
Martin Rothb6acc302015-11-27 18:51:19 -0700271 show_error("IS_ENABLED() used on unknown value CONFIG_$symbol at $file:$lineno.");
272 }
273 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600274 }
275 }
276}
277
278#-------------------------------------------------------------------------------
279# check_defaults - Look for defaults that come after a default with no
280# dependencies.
281#
282# TODO - check for defaults with the same dependencies
283#-------------------------------------------------------------------------------
284sub check_defaults {
285
286 # loop through each defined symbol
287 foreach my $sym ( sort ( keys %symbols ) ) {
288 my $default_set = 0;
289 my $default_filename = "";
290 my $default_line_no = "";
291
292 #loop through each instance of that symbol
293 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
294
295 #loop through any defaults for that instance of that symbol, if there are any
296 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
297 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
298
299 #if a default is already set, display an error
300 if ($default_set) {
301 my $filename = $symbols{$sym}{$sym_num}{file};
302 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
Martin Roth1b44f7e2016-01-11 14:31:38 -0700303 show_warning( "Default for '$sym' referenced at $filename:$line_no will never be set"
304 . " - overridden by default set at $default_filename:$default_line_no" );
Martin Rothbcaaad12015-10-18 11:16:25 -0600305 }
306 else {
307 #if no default is set, see if this is a default with no dependencies
308 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
309 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
310 {
311 $default_set = 1;
312 $default_filename = $symbols{$sym}{$sym_num}{file};
313 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
314 }
315 }
316 }
317 }
318 }
319}
320
321#-------------------------------------------------------------------------------
322# check_referenced_symbols - Make sure the symbols referenced by expressions and
323# select statements are actually valid symbols.
324#-------------------------------------------------------------------------------
325sub check_referenced_symbols {
326
327 #loop through symbols found in expressions and used by 'select' keywords
328 foreach my $key ( sort ( keys %referenced_symbols ) ) {
329
330 #make sure the symbol was defined by a 'config' or 'choice' keyword
331 next if ( exists $symbols{$key} );
332
Martin Rothd8080172015-11-26 19:12:44 -0700333 #loop through each instance of the symbol to print out all of the invalid references
334 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
335 my $filename = $referenced_symbols{$key}{$i}{filename};
336 my $line_no = $referenced_symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700337 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600338 }
339 }
340}
341
342#-------------------------------------------------------------------------------
343#-------------------------------------------------------------------------------
344sub collect_used_symbols {
345 # find all references to CONFIG_ statements in the tree
346
347 if ($dont_use_git_grep) {
348 @collected_symbols = `grep -Irn $exclude_dirs -- "CONFIG_"`;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700349 }
350 else {
Martin Rothbcaaad12015-10-18 11:16:25 -0600351 @collected_symbols = `git grep -In -- "CONFIG_"`;
352 }
353
354 my @used_symbols = @collected_symbols;
355
356 #sort through symbols found by grep and store them in a hash for easy access
357 while ( my $line = shift @used_symbols ) {
358 while ( $line =~ /[^A-Za-z0-9_]CONFIG_([A-Za-z0-9_]+)/g ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700359 my $symbol = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600360 my $filename = "";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700361 if ( $line =~ /^([^:]+):/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600362 $filename = $1;
363 }
364
365 my $skip = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700366 foreach my $exfile (@exclude_files) {
367 $skip = ( $filename =~ /$exfile/ );
Martin Rothbcaaad12015-10-18 11:16:25 -0600368 last if $skip;
369 }
370 last if $skip;
371
Martin Roth1b44f7e2016-01-11 14:31:38 -0700372 if ( exists $used_symbols{$symbol}{count} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600373 $used_symbols{$symbol}{count}++;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700374 }
375 else {
376 $used_symbols{$symbol}{count} = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -0600377 }
378 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700379 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600380 }
381}
382
383#-------------------------------------------------------------------------------
384# check_used_symbols - Checks to see whether or not the created symbols are
385# actually used.
386#-------------------------------------------------------------------------------
387sub check_used_symbols {
388 # loop through all defined symbols and see if they're used anywhere
389 foreach my $key ( sort ( keys %symbols ) ) {
390
391 #see if they're used internal to Kconfig
392 next if ( exists $referenced_symbols{$key} );
393
394 #see if they're used externally
395 next if exists $used_symbols{$key};
396
397 #loop through the definitions to print out all the places the symbol is defined.
398 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
399 my $filename = $symbols{$key}{$i}{file};
400 my $line_no = $symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700401 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600402 }
403 }
404}
405
406#-------------------------------------------------------------------------------
407# build_and_parse_kconfig_tree
408#-------------------------------------------------------------------------------
409#load the initial file and start parsing it
410sub build_and_parse_kconfig_tree {
411 my ($top_level_kconfig) = @_;
412 my @config_to_parse;
413 my @parseline;
414 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
415 my @inside_if = (); # stack of if dependencies
416 my $inside_config = ""; # set to symbol name of the config section
417 my @inside_menu = (); # stack of menu names
418 my $inside_choice = "";
419 my $configs_inside_choice;
420
421 #start the tree off by loading the top level kconfig
422 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
423
424
425 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
426 my $line = $parseline[0]{text};
427 my $filename = $parseline[0]{filename};
428 my $line_no = $parseline[0]{file_line_no};
429
430 #handle help - help text: "help" or "---help---"
431 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
432 $parseline[0]{inside_help} = $inside_help;
433
434 #look for basic issues in the line, strip crlf
435 $line = simple_line_checks( $line, $filename, $line_no );
436
437 #strip comments
438 $line =~ s/\s*#.*$//;
439
440 #don't parse any more if we're inside a help block
441 if ($inside_help) {
442 #do nothing
443 }
444
445 #handle config
446 elsif ( $line =~ /^\s*config/ ) {
447 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
448 my $symbol = $1;
449 $inside_config = $symbol;
450 if ($inside_choice) {
451 $configs_inside_choice++;
452 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700453 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
Martin Rothbcaaad12015-10-18 11:16:25 -0600454 }
455
456 #bool|hex|int|string|tristate <expr> [if <expr>]
457 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
458 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
459 my ( $type, $prompt ) = ( $1, $2 );
460 handle_type( $type, $inside_config, $filename, $line_no );
461 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
462 }
463
464 # def_bool|def_tristate <expr> [if <expr>]
465 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
466 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
467 my ( $orgtype, $default ) = ( $1, $2 );
468 ( my $type = $orgtype ) =~ s/def_//;
469 handle_type( $type, $inside_config, $filename, $line_no );
470 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
471 }
472
473 #prompt <prompt> [if <expr>]
474 elsif ( $line =~ /^\s*prompt/ ) {
475 $line =~ /^\s*prompt\s+(.+)/;
476 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
477 }
478
479 # default <expr> [if <expr>]
480 elsif ( $line =~ /^\s*default/ ) {
481 $line =~ /^\s*default\s+(.*)/;
482 my $default = $1;
483 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
484 }
485
486 # depends on <expr>
487 elsif ( $line =~ /^\s*depends\s+on/ ) {
488 $line =~ /^\s*depends\s+on\s+(.*)$/;
489 my $expr = $1;
490 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
491 handle_expressions( $expr, $inside_config, $filename, $line_no );
492 }
493
494 # comment <prompt>
495 elsif ( $line =~ /^\s*comment/ ) {
496 $inside_config = "";
497 }
498
499 # choice [symbol]
500 elsif ( $line =~ /^\s*choice/ ) {
501 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
502 my $symbol = $1;
503 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
504 handle_type( "bool", $symbol, $filename, $line_no );
505 }
506 $inside_config = "";
507 $inside_choice = "$filename $line_no";
508 $configs_inside_choice = 0;
509 }
510
511 # endchoice
512 elsif ( $line =~ /^\s*endchoice/ ) {
513 $inside_config = "";
514 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700515 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600516 }
517
518 $inside_choice = "";
519 if ( $configs_inside_choice == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700520 show_error("choice block has no symbols at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600521 }
522 $configs_inside_choice = 0;
523 }
524
525 # [optional]
526 elsif ( $line =~ /^\s*optional/ ) {
527 if ($inside_config) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700528 show_error( "Keyword 'optional' appears inside config for '$inside_config'"
529 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600530 }
531 if ( !$inside_choice ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700532 show_error( "Keyword 'optional' appears outside of a choice block"
533 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600534 }
535 }
536
537 # mainmenu <prompt>
538 elsif ( $line =~ /^\s*mainmenu/ ) {
539 $inside_config = "";
540 }
541
542 # menu <prompt>
543 elsif ( $line =~ /^\s*menu/ ) {
544 $line =~ /^\s*menu\s+(.*)/;
545 my $menu = $1;
546 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
547 $menu = $1;
548 }
549
550 $inside_config = "";
551 $inside_choice = "";
552 push( @inside_menu, $menu );
553 }
554
555 # endmenu
556 elsif ( $line =~ /^\s*endmenu/ ) {
557 $inside_config = "";
558 $inside_choice = "";
559 pop @inside_menu;
560 }
561
562 # "if" <expr>
563 elsif ( $line =~ /^\s*if/ ) {
564 $inside_config = "";
565 $line =~ /^\s*if\s+(.*)$/;
566 my $expr = $1;
567 push( @inside_if, $expr );
568 handle_expressions( $expr, $inside_config, $filename, $line_no );
569
570 }
571
572 # endif
573 elsif ( $line =~ /^\s*endif/ ) {
574 $inside_config = "";
575 pop(@inside_if);
576 }
577
578 #range <symbol> <symbol> [if <expr>]
579 elsif ( $line =~ /^\s*range/ ) {
580 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
581 handle_range( $1, $2, $inside_config, $filename, $line_no );
582 }
583
584 # select <symbol> [if <expr>]
585 elsif ( $line =~ /^\s*select/ ) {
586 unless ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700587 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600588 }
589
590 if ( $line =~ /^\s*select\s+(.*)$/ ) {
591 $line = $1;
592 my $expression;
593 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
594 if ($line) {
Martin Rothb7c39b22016-01-14 09:04:53 -0700595 add_referenced_symbol( $line, $filename, $line_no, 'select' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600596 }
597 }
598 }
599
600 # source <prompt>
601 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
602 my @newfile = load_kconfig_file( $1, $filename, $line_no, 0, $filename, $line_no );
603 unshift( @config_to_parse, @newfile );
604 $parseline[0]{text} = "# '$line'\n";
605 }
606 elsif (
607 ( $line =~ /^\s*#/ ) || #comments
608 ( $line =~ /^\s*$/ ) #blank lines
609 )
610 {
611 # do nothing
612 }
613 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700614 show_error("$line ($filename:$line_no unrecognized)");
Martin Rothbcaaad12015-10-18 11:16:25 -0600615 }
616
617 push @wholeconfig, @parseline;
618 }
619}
620
621#-------------------------------------------------------------------------------
622#-------------------------------------------------------------------------------
623sub handle_depends {
624 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
625
626 if ($inside_config) {
627 my $sym_num = $symbols{$inside_config}{count};
628 if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) {
629 $symbols{$inside_config}{$sym_num}{max_dependency}++;
630 }
631 else {
632 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
633 }
634
635 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
636 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
637 }
638}
639
640#-------------------------------------------------------------------------------
641#-------------------------------------------------------------------------------
642sub add_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700643 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600644 my @inside_if = @{$ifref};
645
646 #initialize the symbol or increment the use count.
Martin Roth1b44f7e2016-01-11 14:31:38 -0700647 if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600648 $symbols{$symbol}{count} = 0;
Martin Rothb7c39b22016-01-14 09:04:53 -0700649 if ($inside_choice) {
650 $symbols{$symbol}{choice} = 1;
651 }
652 else {
653 $symbols{$symbol}{choice} = 0;
654 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600655 }
656 else {
657 $symbols{$symbol}{count}++;
Martin Rothb7c39b22016-01-14 09:04:53 -0700658
659 if ( $inside_choice && !$symbols{$symbol}{choice} ) {
660 show_error( "$symbol entry at $filename:$line_no has already been created inside a choice block "
661 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
662 }
663 elsif ( !$inside_choice && $symbols{$symbol}{choice} ) {
664 show_error( "$symbol entry at $filename:$line_no has already been created outside a choice block "
665 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
666 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600667 }
668
Martin Rothb7c39b22016-01-14 09:04:53 -0700669
Martin Rothbcaaad12015-10-18 11:16:25 -0600670 # add the location of this instance
671 my $symcount = $symbols{$symbol}{count};
672 $symbols{$symbol}{$symcount}{file} = $filename;
673 $symbols{$symbol}{$symcount}{line_no} = $line_no;
674
675 #Add the menu structure
676 if ( defined @$menu_array_ref[0] ) {
677 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
678 }
679
680 #Add any 'if' statements that the symbol is inside as dependencies
681 if (@inside_if) {
682 my $dep_num = 0;
683 for my $dependency (@inside_if) {
684 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
685 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
686 $dep_num++;
687 }
688 }
689}
690
691#-------------------------------------------------------------------------------
692# handle range
693#-------------------------------------------------------------------------------
694sub handle_range {
695 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
696
697 my $expression;
698 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
699
700 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
701 my $checkrange1 = $1;
702 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
703 my $checkrange2 = $1;
704
705 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700706 show_error("Range entry in $filename line $line_no value 1 ($range1) is greater than value 2 ($range2).");
Martin Rothbcaaad12015-10-18 11:16:25 -0600707 }
708
709 if ($inside_config) {
710 if ( exists( $symbols{$inside_config}{range1} ) ) {
711 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700712 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700713 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700714 print " not match the previously defined range $symbols{$inside_config}{range1}"
715 . " $symbols{$inside_config}{range2}";
Martin Roth7aa3cea2015-11-27 18:45:45 -0700716 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600717 }
718 }
719 }
720 else {
721 $symbols{$inside_config}{range1} = $range1;
722 $symbols{$inside_config}{range2} = $range2;
723 $symbols{$inside_config}{range_file} = $filename;
724 $symbols{$inside_config}{range_line_no} = $line_no;
725 }
726 }
727 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700728 show_error("Range entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600729 }
730}
731
732#-------------------------------------------------------------------------------
733# handle_default
734#-------------------------------------------------------------------------------
735sub handle_default {
736 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
737 my $expression;
738 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
739
740 if ($inside_config) {
741 handle_expressions( $default, $inside_config, $filename, $line_no );
742 my $sym_num = $symbols{$inside_config}{count};
743
744 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
745 $symbols{$inside_config}{$sym_num}{default_max} = 0;
746 }
747 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
748 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
749 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
750 if ($expression) {
751 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
752 }
753 }
754 elsif ($inside_choice) {
755 handle_expressions( $default, $inside_config, $filename, $line_no );
756 }
757 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700758 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600759 }
760}
761
762#-------------------------------------------------------------------------------
763# handle_if_line
764#-------------------------------------------------------------------------------
765sub handle_if_line {
766 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
767
768 if ( $exprline !~ /if/ ) {
769 return ( $exprline, "" );
770 }
771
772 #remove any quotes that might have an 'if' in them
773 my $savequote;
774 if ( $exprline =~ /^\s*("[^"]+")/ ) {
775 $savequote = $1;
776 $exprline =~ s/^\s*("[^"]+")//;
777 }
778
779 my $expr = "";
780 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
781 $expr = $1;
782 $exprline =~ s/\s*if\s+.*$//;
783
784 if ($expr) {
785 handle_expressions( $expr, $inside_config, $filename, $line_no );
786 }
787 }
788
789 if ($savequote) {
790 $exprline = $savequote;
791 }
792
793 return ( $exprline, $expr );
794}
795
796#-------------------------------------------------------------------------------
797# handle_expressions - log which symbols are being used
798#-------------------------------------------------------------------------------
799sub handle_expressions {
800 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
801
802 return unless ($exprline);
803
804 #filter constant symbols first
Martin Roth1b44f7e2016-01-11 14:31:38 -0700805 if ( $exprline =~ /^\s*"?([yn])"?\s*$/ ) { # constant y/n
Martin Rothbcaaad12015-10-18 11:16:25 -0600806 return;
807 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700808 elsif ( $exprline =~ /^\s*"?((?:-)\d+)"?\s*$/ ) { # int values
Martin Rothbcaaad12015-10-18 11:16:25 -0600809 return;
810 }
811 elsif ( $exprline =~ /^\s*"?((?:-)?(?:0x)?\p{XDigit})+"?\s*$/ ) { # hex values
812 return;
813 }
814 elsif ( $exprline =~ /^\s*("[^"]*")\s*$/ ) { # String values
815 return;
816 }
817 elsif ( $exprline =~ /^\s*([A-Za-z0-9_]+)\s*$/ ) { # <symbol> (1)
Martin Rothb7c39b22016-01-14 09:04:53 -0700818 add_referenced_symbol( $1, $filename, $line_no, 'expression' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600819 }
820 elsif ( $exprline =~ /^\s*!(.+)$/ ) { # '!' <expr> (5)
821
822 handle_expressions( $1, $inside_config, $filename, $line_no );
823 }
824 elsif ( $exprline =~ /^\s*\(([^)]+)\)\s*$/ ) { # '(' <expr> ')' (4)
825 handle_expressions( $1, $inside_config, $filename, $line_no );
826 }
827 elsif ( $exprline =~ /^\s*(.+)\s*!=\s*(.+)\s*$/ ) { # <symbol> '!=' <symbol> (3)
828 handle_expressions( $1, $inside_config, $filename, $line_no );
829 handle_expressions( $2, $inside_config, $filename, $line_no );
830 }
831 elsif ( $exprline =~ /^\s*(.+)\s*=\s*(.+)\s*$/ ) { # <symbol> '=' <symbol> (2)
832 handle_expressions( $1, $inside_config, $filename, $line_no );
833 handle_expressions( $2, $inside_config, $filename, $line_no );
834 }
835 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*&&\s*(.+)\s*$/ ) { # <expr> '&&' <expr> (6)
836 handle_expressions( $1, $inside_config, $filename, $line_no );
837 handle_expressions( $2, $inside_config, $filename, $line_no );
838 }
839 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*\|\|\s*(.+)\s*$/ ) { # <expr> '||' <expr> (7)
840 handle_expressions( $1, $inside_config, $filename, $line_no );
841 handle_expressions( $2, $inside_config, $filename, $line_no );
842 }
843
844 # work around kconfig spec violation for now - paths not in quotes
845 elsif ( $exprline =~ /^\s*([A-Za-z0-9_\-\/]+)\s*$/ ) { # <symbol> (1)
846 return;
847 }
848 else {
Martin Rothd8080172015-11-26 19:12:44 -0700849 show_error("Unrecognized expression '$exprline' in $filename line $line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600850 }
851
852 return;
853}
854
855#-------------------------------------------------------------------------------
856# add_referenced_symbol
857#-------------------------------------------------------------------------------
858sub add_referenced_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700859 my ( $symbol, $filename, $line_no, $reftype ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600860 if ( exists $referenced_symbols{$symbol} ) {
861 $referenced_symbols{$symbol}{count}++;
862 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
863 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
864 }
865 else {
866 $referenced_symbols{$symbol}{count} = 0;
867 $referenced_symbols{$symbol}{0}{filename} = $filename;
868 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
869 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700870
871 #mark the symbol as being selected, use referenced symbols for location
872 if ( $reftype eq 'select' ) {
873 $selected_symbols{$symbol}{ $referenced_symbols{$symbol}{count} } = 1;
874 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600875}
876
877#-------------------------------------------------------------------------------
878# handle_help
879#-------------------------------------------------------------------------------
880{
881 #create a non-global static variable by enclosing it and the subroutine
882 my $help_whitespace = ""; #string to show length of the help whitespace
883
884 sub handle_help {
885 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
886
887 if ($inside_help) {
888
889 #get the indentation level if it's not already set.
890 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
891 $line =~ /^(\s+)/; #find the indentation level.
892 $help_whitespace = $1;
893 if ( !$help_whitespace ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700894 show_warning("$filename:$line_no - help text starts with no whitespace.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600895 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -0600896 }
897 }
898
899 #help ends at the first line which has a smaller indentation than the first line of the help text.
900 if ( ( $line !~ /$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
901 $inside_help = 0;
902 $help_whitespace = "";
903 }
904 else { #if it's not ended, add the line to the helptext array for the symbol's instance
905 if ($inside_config) {
906 my $sym_num = $symbols{$inside_config}{count};
907 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
908 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
909 }
910 }
911 }
912 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
913 $inside_help = $line_no;
914 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700915 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700916 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600917 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600918 }
919 elsif ($inside_config) {
920 $help_whitespace = "";
921 my $sym_num = $symbols{$inside_config}{count};
922 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
923 $symbols{$inside_config}{$sym_num}{helptext} = ();
924 }
925 }
926 return $inside_help;
927 }
928}
929
930#-------------------------------------------------------------------------------
931# handle_type
932#-------------------------------------------------------------------------------
933sub handle_type {
934 my ( $type, $inside_config, $filename, $line_no ) = @_;
935
936 my $expression;
937 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
938
939 if ($inside_config) {
940 if ( exists( $symbols{$inside_config}{type} ) ) {
941 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700942 show_error( "Config '$inside_config' type entry $type"
943 . " at $filename:$line_no does not match $symbols{$inside_config}{type}"
944 . " defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600945 }
946 }
947 else {
948 $symbols{$inside_config}{type} = $type;
949 $symbols{$inside_config}{type_file} = $filename;
950 $symbols{$inside_config}{type_line_no} = $line_no;
951 }
952 }
953 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700954 show_error("Type entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600955 }
956}
957
958#-------------------------------------------------------------------------------
959# handle_prompt
960#-------------------------------------------------------------------------------
961sub handle_prompt {
962 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
963
964 my $expression;
965 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
966
967 if ($inside_config) {
968 if ( $prompt !~ /^\s*$/ ) {
969 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
970 $prompt = $1;
971 }
972
973 if ( !defined @$menu_array_ref[0] ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700974 show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu"
975 . " at $filename:$line_no." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600976 }
977
978 my $sym_num = $symbols{$inside_config}{count};
979 unless ( exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
980 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
981 }
982 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
983 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
984 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
985 if ($expression) {
986 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
987 }
988 }
989 }
990 elsif ($inside_choice) {
991
992 #do nothing
993 }
994 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700995 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600996 }
997}
998
999#-------------------------------------------------------------------------------
1000# simple_line_checks - Does some basic checks on the current line, then cleans the line
1001# up for further processing.
1002#-------------------------------------------------------------------------------
1003sub simple_line_checks {
1004 my ( $line, $filename, $line_no ) = @_;
1005
1006 #check for spaces instead of tabs
1007 if ( $line =~ /^ +/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001008 show_error("$filename:$line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001009 }
1010
1011 #verify a linefeed at the end of the line
1012 if ( $line !~ /.*\n/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001013 show_error( "$filename:$line_no does not end with linefeed."
1014 . " This can cause the line to not be recognized by the Kconfig parser.\n#($line)" );
Martin Rothbcaaad12015-10-18 11:16:25 -06001015 $line =~ s/\s*$//;
1016 }
1017 else {
1018 chop($line);
1019 }
1020
1021 return $line;
1022}
1023
1024#-------------------------------------------------------------------------------
1025# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
1026#-------------------------------------------------------------------------------
1027sub load_kconfig_file {
1028 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
1029 my @file_data;
1030 my @dir_file_data;
1031
1032 #recursively handle coreboot's new source glob operator
1033 if ( $input_file =~ /^(.*?)\/\*\/(.*)$/ ) {
1034 my $dir_prefix = $1;
1035 my $dir_suffix = $2;
1036 if ( -d "$dir_prefix" ) {
1037
1038 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
1039 my @dirlist = sort { $a cmp $b } readdir(D);
1040 closedir(D);
1041
1042 while ( my $directory = shift @dirlist ) {
1043
1044 #ignore non-directory files
1045 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ ) ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001046 push @dir_file_data,
1047 load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1048 $input_file, $loadline, 1, $loadfile, $loadline );
Martin Rothbcaaad12015-10-18 11:16:25 -06001049 }
1050 }
1051 }
1052
1053 #the directory should exist when using a glob
1054 else {
Martin Rothd8080172015-11-26 19:12:44 -07001055 show_warning("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -06001056 }
1057 }
1058
1059 #if the file exists, try to load it.
1060 elsif ( -e "$input_file" ) {
1061
1062 #throw a warning if the file has already been loaded.
1063 if ( exists $loaded_files{$input_file} ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001064 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -06001065 }
1066
1067 #load the file's contents and mark the file as loaded for checking later
1068 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
1069 @file_data = <$HANDLE>;
1070 close $HANDLE;
1071 $loaded_files{$input_file} = "'$loadfile' line $loadline";
1072 }
1073
1074 # if the file isn't being loaded from a glob, it should exist.
1075 elsif ( $expanded == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001076 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -06001077 }
1078
1079 my $line_in_file = 0;
1080 while ( my $line = shift @file_data ) {
1081
1082 #handle line continuation.
1083 my $continue_line = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001084 while ( $line =~ /(.*)\s+\\$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001085 my $text = $1;
1086
1087 # get rid of leading whitespace on all but the first and last lines
1088 $text =~ s/^\s*/ / if ($continue_line);
1089
1090 $dir_file_data[$line_in_file]{text} .= $text;
1091 $line = shift @file_data;
1092 $continue_line++;
1093
1094 #put the data into the continued lines (other than the first)
1095 $line =~ /^\s*(.*)\s*$/;
1096
Martin Roth1b44f7e2016-01-11 14:31:38 -07001097 $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n";
1098 $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file;
1099 $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001100
1101 #get rid of multiple leading spaces for last line
1102 $line = " $1\n";
1103 }
1104
Martin Roth1b44f7e2016-01-11 14:31:38 -07001105 $dir_file_data[$line_in_file]{text} .= $line;
Martin Rothbcaaad12015-10-18 11:16:25 -06001106 $dir_file_data[$line_in_file]{filename} = $input_file;
1107 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1108
1109 $line_in_file++;
1110 if ($continue_line) {
1111 $line_in_file += $continue_line;
1112 }
1113 }
1114
1115 if ($topfile) {
1116 my %file_data;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001117 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001118 $file_data{filename} = $topfile;
1119 $file_data{file_line_no} = "($topline)";
Martin Roth1b44f7e2016-01-11 14:31:38 -07001120 unshift( @dir_file_data, \%file_data );
Martin Rothbcaaad12015-10-18 11:16:25 -06001121 }
1122
1123 return @dir_file_data;
1124}
1125
1126
1127#-------------------------------------------------------------------------------
1128# print_wholeconfig - prints out the parsed Kconfig file
1129#-------------------------------------------------------------------------------
1130sub print_wholeconfig {
1131
1132 return unless $print_full_output;
1133
1134 for ( my $i = 0 ; $i < $#wholeconfig ; $i++ ) {
1135 my $line = $wholeconfig[$i];
1136 chop( $line->{text} );
1137
1138 #replace tabs with spaces for consistency
1139 $line->{text} =~ s/\t/ /g;
1140 printf "%-120s # $line->{filename} line $line->{file_line_no}\n", $line->{text};
1141 }
1142}
1143
1144#-------------------------------------------------------------------------------
1145# check_if_file_referenced - checks for kconfig files that are not being parsed
1146#-------------------------------------------------------------------------------
1147sub check_if_file_referenced {
1148 my $filename = $File::Find::name;
1149 if ( ( $filename =~ /Kconfig/ ) && ( !exists $loaded_files{$filename} ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001150 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001151 }
1152}
1153
1154#-------------------------------------------------------------------------------
1155# check_arguments parse the command line arguments
1156#-------------------------------------------------------------------------------
1157sub check_arguments {
1158 my $show_usage = 0;
1159 GetOptions(
1160 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001161 'e|errors_off' => \$suppress_error_output,
1162 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001163 'o|output=s' => \$output_file,
1164 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001165 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001166 'path=s' => \$top_dir,
1167 'c|config=s' => \$config_file,
1168 'G|no_git_grep' => \$dont_use_git_grep,
1169 );
Martin Rothd8080172015-11-26 19:12:44 -07001170
1171 if ($suppress_error_output) {
1172 $suppress_warning_output = 1;
1173 }
1174 if ($suppress_warning_output) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001175 $show_note_output = 0;
Martin Rothd8080172015-11-26 19:12:44 -07001176 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001177}
1178
1179#-------------------------------------------------------------------------------
1180# usage - Print the arguments for the user
1181#-------------------------------------------------------------------------------
1182sub usage {
1183 print "kconfig_lint <options>\n";
1184 print " -o|--output=file Set output filename\n";
1185 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001186 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001187 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001188 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001189 print " --path=dir Path to top level kconfig\n";
1190 print " -c|--config=file Filename of config file to load\n";
1191 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
1192
1193 exit(0);
1194}
1195
11961;