blob: 9bad31e58548264474fdc256dac26bc433f51c7f [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 Rothbcaaad12015-10-18 11:16:25 -060018
19package kconfig_lint;
20
21use strict;
22use warnings;
23use English qw( -no_match_vars );
24use File::Find;
25use Getopt::Long;
26use Getopt::Std;
27
Martin Rothd8080172015-11-26 19:12:44 -070028my $suppress_error_output = 0; # flag to prevent error text
29my $suppress_warning_output = 0; # flag to prevent warning text
30my $show_note_output = 0; # flag to show minor notes text
31my $print_full_output = 0; # flag to print wholeconfig output
32my $output_file = "-"; # filename of output - set stdout by default
33my $dont_use_git_grep = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -060034
35#globals
Martin Rothd8080172015-11-26 19:12:44 -070036my $top_dir = "."; # Directory where Kconfig is run
37my $root_dir = "src"; # Directory of the top level Kconfig file
38my $errors_found = 0; # count of errors
39my $warnings_found = 0;
40my $exclude_dirs = '--exclude-dir="build" --exclude-dir="coreboot-builds" --exclude-dir="payloads" --exclude-dir="configs" --exclude-dir="util"'; # directories to exclude when searching for used symbols - NOT USED FOR GIT GREP (TODO)
41my @exclude_files = ('\.txt$', '\.tex$', 'config', '\.tags'); #files to exclude when looking for symbols
42my $config_file = ""; # name of config file to load symbol values from.
Martin Rothbcaaad12015-10-18 11:16:25 -060043my @wholeconfig; # document the entire kconfig structure
44my %loaded_files; # list of each Kconfig file loaded
45my %symbols; # main structure of all symbols declared
46my %referenced_symbols; # list of symbols referenced by expressions or select statements
47my %used_symbols; # structure of symbols used in the tree, and where they're found
48my @collected_symbols; #
49
50Main();
51
52#-------------------------------------------------------------------------------
53# Main
54#
55# Start by loading and parsing the top level Kconfig, this pulls in the other
56# files. Parsing the tree creates several arrays and hashes that can be used
57# to check for errors
58#-------------------------------------------------------------------------------
59sub Main {
60
61 check_arguments();
62 open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
63
64 if (defined $top_dir) {
65 chdir $top_dir or die "Error: can't cd to $top_dir\n";
66 }
67
68 die "Error: $top_dir/$root_dir does not exist.\n" unless (-d $root_dir);
69
70 #load the Kconfig tree, checking what we can and building up all the hash tables
71 build_and_parse_kconfig_tree("$root_dir/Kconfig");
72
73 #run checks based on the data that was found
74 find( \&check_if_file_referenced, $root_dir );
75
76 load_config($config_file) if ($config_file);
77
78 check_defaults();
79 check_referenced_symbols();
80
81 collect_used_symbols();
82 check_used_symbols();
83 check_for_ifdef();
84 check_for_def();
85 check_is_enabled();
86
87 print_wholeconfig();
88
Martin Rothd8080172015-11-26 19:12:44 -070089 if ($errors_found) {
90 print "# $errors_found errors";
91 if ($warnings_found) {
92 print ", $warnings_found warnings";
93 }
94 print "\n";
95 }
96
97 exit($errors_found + $warnings_found);
98}
99
100#-------------------------------------------------------------------------------
101# Print and count errors
102#-------------------------------------------------------------------------------
103sub show_error {
104 my ($error_msg) = @_;
105 unless ($suppress_error_output) {
106 print "#!!!!! Error: $error_msg\n";
107 $errors_found++;
108 }
109}
110
111#-------------------------------------------------------------------------------
112# Print and count warnings
113#-------------------------------------------------------------------------------
114sub show_warning {
115 my ($warning_msg) = @_;
116 unless ($suppress_warning_output) {
117 print "#!!!!! Warning: $warning_msg\n";
118 $warnings_found++;
119 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600120}
121
122#-------------------------------------------------------------------------------
123# check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and
124# #if defined(CONFIG_[symbol_name]).
125#
126# #ifdef symbol is valid for strings, but bool, hex, and INT are always defined.
127# #if defined(symbol) && symbol is also a valid construct.
128#-------------------------------------------------------------------------------
129sub check_for_ifdef {
130 my @ifdef_symbols = @collected_symbols;
131
132 #look for #ifdef SYMBOL
133 while ( my $line = shift @ifdef_symbols ) {
134 if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s+CONFIG_(\w+)/ ) {
135 my $file = $1;
136 my $lineno = $2;
137 my $symbol = $3;
138
139 if ((exists $symbols{$symbol}) && ($symbols{$symbol}{type} ne "string")) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700140 show_error("#ifdef 'CONFIG_$symbol' used at $file:$lineno. Symbols of type '$symbols{$symbol}{type}' are always defined.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600141 }
142 }
143 }
144
145 # look for (#if) defined SYMBOL
146 @ifdef_symbols = @collected_symbols;
147 while ( my $line = shift @ifdef_symbols ) {
148 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_(\w+)/ ) {
149 my $file = $1;
150 my $lineno = $2;
151 my $symbol = $3;
152
153 #ignore '#if defined(symbol) && symbol' type statements
154 next if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_$symbol.*(&&|\|\|)\s*!?\s*\(?\s*CONFIG_$symbol/ );
155
156 if ((exists $symbols{$symbol}) && ($symbols{$symbol}{type} ne "string")) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700157 show_error("defined 'CONFIG_$symbol' used at $file:$lineno. Symbols of type '$symbols{$symbol}{type}' are always defined.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600158 }
159 }
160 }
161}
162
163#-------------------------------------------------------------------------------
164# check_for_def - Look for instances of #define CONFIG_[symbol_name]
165#
166# Symbols should not be redefined outside of Kconfig, and #defines should not
167# look like symbols
168#-------------------------------------------------------------------------------
169sub check_for_def {
170 my @def_symbols = @collected_symbols;
171
172 #look for #ifdef SYMBOL
173 while ( my $line = shift @def_symbols ) {
174 if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) {
175 my $file = $1;
176 my $lineno = $2;
177 my $symbol = $3;
178
179 if ((exists $symbols{$symbol})) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700180 show_warning("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600181 } else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700182 show_warning("#define 'CONFIG_$symbol' used at $file:$lineno. Other #defines should not look like Kconfig symbols.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600183 }
184 }
185 }
186}
187
188#-------------------------------------------------------------------------------
189# check_is_enabled - The IS_ENABLED() macro is only valid for symbols of type
190# bool. It would probably work on type hex or int if the value was 0 or 1, but
191# this seems like a bad plan. Using it on strings is dead out.
192#-------------------------------------------------------------------------------
193sub check_is_enabled {
194 my @is_enabled_symbols = `grep -Grn 'IS_ENABLED\\s*(\\s*CONFIG_' -- "$root_dir"`;
195
196 #sort through symbols found by grep and store them in a hash for easy access
197 while ( my $line = shift @is_enabled_symbols ) {
198 if ( $line =~ /^([^:]+):(\d+):.+IS_ENABLED\s*\(\s*CONFIG_(\w+)/ ) {
199 my $file = $1;
200 my $lineno = $2;
201 my $symbol = $3;
202
203 #make sure that
204 if (exists $symbols{$symbol}) {
205 if ($symbols{$symbol}{type} ne "bool") {
Martin Rothd8080172015-11-26 19:12:44 -0700206 show_error("IS_ENABLED(CONFIG_$symbol) used in $file at line $lineno. IS_ENABLED is only valid for type 'bool', not '$symbols{$symbol}{type}'.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600207 }
208 } else {
Martin Rothd8080172015-11-26 19:12:44 -0700209 show_error("IS_ENABLED() used on unknown value CONFIG_$symbol in $file at line $lineno.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600210 }
211 } else {
Martin Rothd8080172015-11-26 19:12:44 -0700212 show_error("# uninterpreted IS_ENABLED line: $line");
Martin Rothbcaaad12015-10-18 11:16:25 -0600213 }
214 }
215}
216
217#-------------------------------------------------------------------------------
218# check_defaults - Look for defaults that come after a default with no
219# dependencies.
220#
221# TODO - check for defaults with the same dependencies
222#-------------------------------------------------------------------------------
223sub check_defaults {
224
225 # loop through each defined symbol
226 foreach my $sym ( sort ( keys %symbols ) ) {
227 my $default_set = 0;
228 my $default_filename = "";
229 my $default_line_no = "";
230
231 #loop through each instance of that symbol
232 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
233
234 #loop through any defaults for that instance of that symbol, if there are any
235 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
236 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
237
238 #if a default is already set, display an error
239 if ($default_set) {
240 my $filename = $symbols{$sym}{$sym_num}{file};
241 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700242 show_warning("Default for '$sym' referenced at $filename:$line_no will never be set - overridden by default set at $default_filename:$default_line_no");
Martin Rothbcaaad12015-10-18 11:16:25 -0600243 }
244 else {
245 #if no default is set, see if this is a default with no dependencies
246 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
247 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
248 {
249 $default_set = 1;
250 $default_filename = $symbols{$sym}{$sym_num}{file};
251 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
252 }
253 }
254 }
255 }
256 }
257}
258
259#-------------------------------------------------------------------------------
260# check_referenced_symbols - Make sure the symbols referenced by expressions and
261# select statements are actually valid symbols.
262#-------------------------------------------------------------------------------
263sub check_referenced_symbols {
264
265 #loop through symbols found in expressions and used by 'select' keywords
266 foreach my $key ( sort ( keys %referenced_symbols ) ) {
267
268 #make sure the symbol was defined by a 'config' or 'choice' keyword
269 next if ( exists $symbols{$key} );
270
Martin Rothd8080172015-11-26 19:12:44 -0700271 #loop through each instance of the symbol to print out all of the invalid references
272 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
273 my $filename = $referenced_symbols{$key}{$i}{filename};
274 my $line_no = $referenced_symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700275 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600276 }
277 }
278}
279
280#-------------------------------------------------------------------------------
281#-------------------------------------------------------------------------------
282sub collect_used_symbols {
283 # find all references to CONFIG_ statements in the tree
284
285 if ($dont_use_git_grep) {
286 @collected_symbols = `grep -Irn $exclude_dirs -- "CONFIG_"`;
287 } else {
288 @collected_symbols = `git grep -In -- "CONFIG_"`;
289 }
290
291 my @used_symbols = @collected_symbols;
292
293 #sort through symbols found by grep and store them in a hash for easy access
294 while ( my $line = shift @used_symbols ) {
295 while ( $line =~ /[^A-Za-z0-9_]CONFIG_([A-Za-z0-9_]+)/g ) {
296 my $symbol = $1;
297 my $filename = "";
298 if ($line =~ /^([^:]+):/) {
299 $filename = $1;
300 }
301
302 my $skip = 0;
303 foreach my $exfile ( @exclude_files) {
304 $skip = ($filename =~ /$exfile/);
305 last if $skip;
306 }
307 last if $skip;
308
309 if (exists $used_symbols{$symbol}{count}) {
310 $used_symbols{$symbol}{count}++;
311 } else {
312 $used_symbols{$symbol}{count} = 0;
313 }
314 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
315 }
316 }
317}
318
319#-------------------------------------------------------------------------------
320# check_used_symbols - Checks to see whether or not the created symbols are
321# actually used.
322#-------------------------------------------------------------------------------
323sub check_used_symbols {
324 # loop through all defined symbols and see if they're used anywhere
325 foreach my $key ( sort ( keys %symbols ) ) {
326
327 #see if they're used internal to Kconfig
328 next if ( exists $referenced_symbols{$key} );
329
330 #see if they're used externally
331 next if exists $used_symbols{$key};
332
333 #loop through the definitions to print out all the places the symbol is defined.
334 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
335 my $filename = $symbols{$key}{$i}{file};
336 my $line_no = $symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700337 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600338 }
339 }
340}
341
342#-------------------------------------------------------------------------------
343# build_and_parse_kconfig_tree
344#-------------------------------------------------------------------------------
345#load the initial file and start parsing it
346sub build_and_parse_kconfig_tree {
347 my ($top_level_kconfig) = @_;
348 my @config_to_parse;
349 my @parseline;
350 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
351 my @inside_if = (); # stack of if dependencies
352 my $inside_config = ""; # set to symbol name of the config section
353 my @inside_menu = (); # stack of menu names
354 my $inside_choice = "";
355 my $configs_inside_choice;
356
357 #start the tree off by loading the top level kconfig
358 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
359
360
361 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
362 my $line = $parseline[0]{text};
363 my $filename = $parseline[0]{filename};
364 my $line_no = $parseline[0]{file_line_no};
365
366 #handle help - help text: "help" or "---help---"
367 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
368 $parseline[0]{inside_help} = $inside_help;
369
370 #look for basic issues in the line, strip crlf
371 $line = simple_line_checks( $line, $filename, $line_no );
372
373 #strip comments
374 $line =~ s/\s*#.*$//;
375
376 #don't parse any more if we're inside a help block
377 if ($inside_help) {
378 #do nothing
379 }
380
381 #handle config
382 elsif ( $line =~ /^\s*config/ ) {
383 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
384 my $symbol = $1;
385 $inside_config = $symbol;
386 if ($inside_choice) {
387 $configs_inside_choice++;
388 }
389 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
390 }
391
392 #bool|hex|int|string|tristate <expr> [if <expr>]
393 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
394 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
395 my ( $type, $prompt ) = ( $1, $2 );
396 handle_type( $type, $inside_config, $filename, $line_no );
397 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
398 }
399
400 # def_bool|def_tristate <expr> [if <expr>]
401 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
402 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
403 my ( $orgtype, $default ) = ( $1, $2 );
404 ( my $type = $orgtype ) =~ s/def_//;
405 handle_type( $type, $inside_config, $filename, $line_no );
406 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
407 }
408
409 #prompt <prompt> [if <expr>]
410 elsif ( $line =~ /^\s*prompt/ ) {
411 $line =~ /^\s*prompt\s+(.+)/;
412 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
413 }
414
415 # default <expr> [if <expr>]
416 elsif ( $line =~ /^\s*default/ ) {
417 $line =~ /^\s*default\s+(.*)/;
418 my $default = $1;
419 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
420 }
421
422 # depends on <expr>
423 elsif ( $line =~ /^\s*depends\s+on/ ) {
424 $line =~ /^\s*depends\s+on\s+(.*)$/;
425 my $expr = $1;
426 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
427 handle_expressions( $expr, $inside_config, $filename, $line_no );
428 }
429
430 # comment <prompt>
431 elsif ( $line =~ /^\s*comment/ ) {
432 $inside_config = "";
433 }
434
435 # choice [symbol]
436 elsif ( $line =~ /^\s*choice/ ) {
437 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
438 my $symbol = $1;
439 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
440 handle_type( "bool", $symbol, $filename, $line_no );
441 }
442 $inside_config = "";
443 $inside_choice = "$filename $line_no";
444 $configs_inside_choice = 0;
445 }
446
447 # endchoice
448 elsif ( $line =~ /^\s*endchoice/ ) {
449 $inside_config = "";
450 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700451 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600452 }
453
454 $inside_choice = "";
455 if ( $configs_inside_choice == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700456 show_error("choice block has no symbols at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600457 }
458 $configs_inside_choice = 0;
459 }
460
461 # [optional]
462 elsif ( $line =~ /^\s*optional/ ) {
463 if ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700464 show_error("Keyword 'optional' appears inside config for '$inside_config' at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600465 }
466 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700467 show_error("Keyword 'optional' appears outside of a choice block at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600468 }
469 }
470
471 # mainmenu <prompt>
472 elsif ( $line =~ /^\s*mainmenu/ ) {
473 $inside_config = "";
474 }
475
476 # menu <prompt>
477 elsif ( $line =~ /^\s*menu/ ) {
478 $line =~ /^\s*menu\s+(.*)/;
479 my $menu = $1;
480 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
481 $menu = $1;
482 }
483
484 $inside_config = "";
485 $inside_choice = "";
486 push( @inside_menu, $menu );
487 }
488
489 # endmenu
490 elsif ( $line =~ /^\s*endmenu/ ) {
491 $inside_config = "";
492 $inside_choice = "";
493 pop @inside_menu;
494 }
495
496 # "if" <expr>
497 elsif ( $line =~ /^\s*if/ ) {
498 $inside_config = "";
499 $line =~ /^\s*if\s+(.*)$/;
500 my $expr = $1;
501 push( @inside_if, $expr );
502 handle_expressions( $expr, $inside_config, $filename, $line_no );
503
504 }
505
506 # endif
507 elsif ( $line =~ /^\s*endif/ ) {
508 $inside_config = "";
509 pop(@inside_if);
510 }
511
512 #range <symbol> <symbol> [if <expr>]
513 elsif ( $line =~ /^\s*range/ ) {
514 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
515 handle_range( $1, $2, $inside_config, $filename, $line_no );
516 }
517
518 # select <symbol> [if <expr>]
519 elsif ( $line =~ /^\s*select/ ) {
520 unless ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700521 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600522 }
523
524 if ( $line =~ /^\s*select\s+(.*)$/ ) {
525 $line = $1;
526 my $expression;
527 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
528 if ($line) {
529 add_referenced_symbol( $line, $filename, $line_no );
530 }
531 }
532 }
533
534 # source <prompt>
535 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
536 my @newfile = load_kconfig_file( $1, $filename, $line_no, 0, $filename, $line_no );
537 unshift( @config_to_parse, @newfile );
538 $parseline[0]{text} = "# '$line'\n";
539 }
540 elsif (
541 ( $line =~ /^\s*#/ ) || #comments
542 ( $line =~ /^\s*$/ ) #blank lines
543 )
544 {
545 # do nothing
546 }
547 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700548 show_error("$line ($filename:$line_no unrecognized)");
Martin Rothbcaaad12015-10-18 11:16:25 -0600549 }
550
551 push @wholeconfig, @parseline;
552 }
553}
554
555#-------------------------------------------------------------------------------
556#-------------------------------------------------------------------------------
557sub handle_depends {
558 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
559
560 if ($inside_config) {
561 my $sym_num = $symbols{$inside_config}{count};
562 if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) {
563 $symbols{$inside_config}{$sym_num}{max_dependency}++;
564 }
565 else {
566 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
567 }
568
569 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
570 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
571 }
572}
573
574#-------------------------------------------------------------------------------
575#-------------------------------------------------------------------------------
576sub add_symbol {
577 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref ) = @_;
578 my @inside_if = @{$ifref};
579
580 #initialize the symbol or increment the use count.
581 if (( !exists $symbols{$symbol}) || ( !exists $symbols{$symbol}{count} )) {
582 $symbols{$symbol}{count} = 0;
583 }
584 else {
585 $symbols{$symbol}{count}++;
586 }
587
588 # add the location of this instance
589 my $symcount = $symbols{$symbol}{count};
590 $symbols{$symbol}{$symcount}{file} = $filename;
591 $symbols{$symbol}{$symcount}{line_no} = $line_no;
592
593 #Add the menu structure
594 if ( defined @$menu_array_ref[0] ) {
595 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
596 }
597
598 #Add any 'if' statements that the symbol is inside as dependencies
599 if (@inside_if) {
600 my $dep_num = 0;
601 for my $dependency (@inside_if) {
602 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
603 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
604 $dep_num++;
605 }
606 }
607}
608
609#-------------------------------------------------------------------------------
610# handle range
611#-------------------------------------------------------------------------------
612sub handle_range {
613 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
614
615 my $expression;
616 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
617
618 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
619 my $checkrange1 = $1;
620 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
621 my $checkrange2 = $1;
622
623 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700624 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 -0600625 }
626
627 if ($inside_config) {
628 if ( exists( $symbols{$inside_config}{range1} ) ) {
629 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700630 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700631 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
Martin Rothbcaaad12015-10-18 11:16:25 -0600632 print " not match the previously defined range $symbols{$inside_config}{range1} $symbols{$inside_config}{range2}";
Martin Roth7aa3cea2015-11-27 18:45:45 -0700633 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600634 }
635 }
636 }
637 else {
638 $symbols{$inside_config}{range1} = $range1;
639 $symbols{$inside_config}{range2} = $range2;
640 $symbols{$inside_config}{range_file} = $filename;
641 $symbols{$inside_config}{range_line_no} = $line_no;
642 }
643 }
644 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700645 show_error("Range entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600646 }
647}
648
649#-------------------------------------------------------------------------------
650# handle_default
651#-------------------------------------------------------------------------------
652sub handle_default {
653 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
654 my $expression;
655 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
656
657 if ($inside_config) {
658 handle_expressions( $default, $inside_config, $filename, $line_no );
659 my $sym_num = $symbols{$inside_config}{count};
660
661 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
662 $symbols{$inside_config}{$sym_num}{default_max} = 0;
663 }
664 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
665 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
666 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
667 if ($expression) {
668 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
669 }
670 }
671 elsif ($inside_choice) {
672 handle_expressions( $default, $inside_config, $filename, $line_no );
673 }
674 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700675 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600676 }
677}
678
679#-------------------------------------------------------------------------------
680# handle_if_line
681#-------------------------------------------------------------------------------
682sub handle_if_line {
683 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
684
685 if ( $exprline !~ /if/ ) {
686 return ( $exprline, "" );
687 }
688
689 #remove any quotes that might have an 'if' in them
690 my $savequote;
691 if ( $exprline =~ /^\s*("[^"]+")/ ) {
692 $savequote = $1;
693 $exprline =~ s/^\s*("[^"]+")//;
694 }
695
696 my $expr = "";
697 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
698 $expr = $1;
699 $exprline =~ s/\s*if\s+.*$//;
700
701 if ($expr) {
702 handle_expressions( $expr, $inside_config, $filename, $line_no );
703 }
704 }
705
706 if ($savequote) {
707 $exprline = $savequote;
708 }
709
710 return ( $exprline, $expr );
711}
712
713#-------------------------------------------------------------------------------
714# handle_expressions - log which symbols are being used
715#-------------------------------------------------------------------------------
716sub handle_expressions {
717 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
718
719 return unless ($exprline);
720
721 #filter constant symbols first
722 if ( $exprline =~ /^\s*"?([yn])"?\s*$/ ) { # constant y/n
723 return;
724 }
725 elsif ( $exprline =~ /^\s*"?((?:-)\d+)"?\s*$/ ) { # int values
726 return;
727 }
728 elsif ( $exprline =~ /^\s*"?((?:-)?(?:0x)?\p{XDigit})+"?\s*$/ ) { # hex values
729 return;
730 }
731 elsif ( $exprline =~ /^\s*("[^"]*")\s*$/ ) { # String values
732 return;
733 }
734 elsif ( $exprline =~ /^\s*([A-Za-z0-9_]+)\s*$/ ) { # <symbol> (1)
735 add_referenced_symbol( $1, $filename, $line_no );
736 }
737 elsif ( $exprline =~ /^\s*!(.+)$/ ) { # '!' <expr> (5)
738
739 handle_expressions( $1, $inside_config, $filename, $line_no );
740 }
741 elsif ( $exprline =~ /^\s*\(([^)]+)\)\s*$/ ) { # '(' <expr> ')' (4)
742 handle_expressions( $1, $inside_config, $filename, $line_no );
743 }
744 elsif ( $exprline =~ /^\s*(.+)\s*!=\s*(.+)\s*$/ ) { # <symbol> '!=' <symbol> (3)
745 handle_expressions( $1, $inside_config, $filename, $line_no );
746 handle_expressions( $2, $inside_config, $filename, $line_no );
747 }
748 elsif ( $exprline =~ /^\s*(.+)\s*=\s*(.+)\s*$/ ) { # <symbol> '=' <symbol> (2)
749 handle_expressions( $1, $inside_config, $filename, $line_no );
750 handle_expressions( $2, $inside_config, $filename, $line_no );
751 }
752 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*&&\s*(.+)\s*$/ ) { # <expr> '&&' <expr> (6)
753 handle_expressions( $1, $inside_config, $filename, $line_no );
754 handle_expressions( $2, $inside_config, $filename, $line_no );
755 }
756 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*\|\|\s*(.+)\s*$/ ) { # <expr> '||' <expr> (7)
757 handle_expressions( $1, $inside_config, $filename, $line_no );
758 handle_expressions( $2, $inside_config, $filename, $line_no );
759 }
760
761 # work around kconfig spec violation for now - paths not in quotes
762 elsif ( $exprline =~ /^\s*([A-Za-z0-9_\-\/]+)\s*$/ ) { # <symbol> (1)
763 return;
764 }
765 else {
Martin Rothd8080172015-11-26 19:12:44 -0700766 show_error("Unrecognized expression '$exprline' in $filename line $line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600767 }
768
769 return;
770}
771
772#-------------------------------------------------------------------------------
773# add_referenced_symbol
774#-------------------------------------------------------------------------------
775sub add_referenced_symbol {
776 my ( $symbol, $filename, $line_no ) = @_;
777 if ( exists $referenced_symbols{$symbol} ) {
778 $referenced_symbols{$symbol}{count}++;
779 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
780 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
781 }
782 else {
783 $referenced_symbols{$symbol}{count} = 0;
784 $referenced_symbols{$symbol}{0}{filename} = $filename;
785 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
786 }
787}
788
789#-------------------------------------------------------------------------------
790# handle_help
791#-------------------------------------------------------------------------------
792{
793 #create a non-global static variable by enclosing it and the subroutine
794 my $help_whitespace = ""; #string to show length of the help whitespace
795
796 sub handle_help {
797 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
798
799 if ($inside_help) {
800
801 #get the indentation level if it's not already set.
802 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
803 $line =~ /^(\s+)/; #find the indentation level.
804 $help_whitespace = $1;
805 if ( !$help_whitespace ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700806 show_warning("$filename:$line_no - help text starts with no whitespace.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600807 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -0600808 }
809 }
810
811 #help ends at the first line which has a smaller indentation than the first line of the help text.
812 if ( ( $line !~ /$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
813 $inside_help = 0;
814 $help_whitespace = "";
815 }
816 else { #if it's not ended, add the line to the helptext array for the symbol's instance
817 if ($inside_config) {
818 my $sym_num = $symbols{$inside_config}{count};
819 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
820 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
821 }
822 }
823 }
824 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
825 $inside_help = $line_no;
826 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700827 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700828 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600829 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600830 }
831 elsif ($inside_config) {
832 $help_whitespace = "";
833 my $sym_num = $symbols{$inside_config}{count};
834 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
835 $symbols{$inside_config}{$sym_num}{helptext} = ();
836 }
837 }
838 return $inside_help;
839 }
840}
841
842#-------------------------------------------------------------------------------
843# handle_type
844#-------------------------------------------------------------------------------
845sub handle_type {
846 my ( $type, $inside_config, $filename, $line_no ) = @_;
847
848 my $expression;
849 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
850
851 if ($inside_config) {
852 if ( exists( $symbols{$inside_config}{type} ) ) {
853 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700854 show_error("Config '$inside_config' type entry $type at $filename:$line_no does not match $symbols{$inside_config}{type} defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600855 }
856 }
857 else {
858 $symbols{$inside_config}{type} = $type;
859 $symbols{$inside_config}{type_file} = $filename;
860 $symbols{$inside_config}{type_line_no} = $line_no;
861 }
862 }
863 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700864 show_error("Type entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600865 }
866}
867
868#-------------------------------------------------------------------------------
869# handle_prompt
870#-------------------------------------------------------------------------------
871sub handle_prompt {
872 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
873
874 my $expression;
875 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
876
877 if ($inside_config) {
878 if ( $prompt !~ /^\s*$/ ) {
879 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
880 $prompt = $1;
881 }
882
883 if ( !defined @$menu_array_ref[0] ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700884 show_error("Symbol '$inside_config' with prompt '$prompt' appears outside of a menu at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600885 }
886
887 my $sym_num = $symbols{$inside_config}{count};
888 unless ( exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
889 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
890 }
891 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
892 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
893 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
894 if ($expression) {
895 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
896 }
897 }
898 }
899 elsif ($inside_choice) {
900
901 #do nothing
902 }
903 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700904 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600905 }
906}
907
908#-------------------------------------------------------------------------------
909# simple_line_checks - Does some basic checks on the current line, then cleans the line
910# up for further processing.
911#-------------------------------------------------------------------------------
912sub simple_line_checks {
913 my ( $line, $filename, $line_no ) = @_;
914
915 #check for spaces instead of tabs
916 if ( $line =~ /^ +/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700917 show_error("$filename:$line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600918 }
919
920 #verify a linefeed at the end of the line
921 if ( $line !~ /.*\n/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700922 show_error("$filename:$line_no does not end with linefeed. This can cause the line to not be recognized by the Kconfig parser.\n#($line)");
Martin Rothbcaaad12015-10-18 11:16:25 -0600923 $line =~ s/\s*$//;
924 }
925 else {
926 chop($line);
927 }
928
929 return $line;
930}
931
932#-------------------------------------------------------------------------------
933# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
934#-------------------------------------------------------------------------------
935sub load_kconfig_file {
936 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
937 my @file_data;
938 my @dir_file_data;
939
940 #recursively handle coreboot's new source glob operator
941 if ( $input_file =~ /^(.*?)\/\*\/(.*)$/ ) {
942 my $dir_prefix = $1;
943 my $dir_suffix = $2;
944 if ( -d "$dir_prefix" ) {
945
946 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
947 my @dirlist = sort { $a cmp $b } readdir(D);
948 closedir(D);
949
950 while ( my $directory = shift @dirlist ) {
951
952 #ignore non-directory files
953 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ ) ) {
954 push @dir_file_data, load_kconfig_file( "$dir_prefix/$directory/$dir_suffix", $input_file, $loadline, 1, $loadfile, $loadline );
955 }
956 }
957 }
958
959 #the directory should exist when using a glob
960 else {
Martin Rothd8080172015-11-26 19:12:44 -0700961 show_warning("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -0600962 }
963 }
964
965 #if the file exists, try to load it.
966 elsif ( -e "$input_file" ) {
967
968 #throw a warning if the file has already been loaded.
969 if ( exists $loaded_files{$input_file} ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700970 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -0600971 }
972
973 #load the file's contents and mark the file as loaded for checking later
974 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
975 @file_data = <$HANDLE>;
976 close $HANDLE;
977 $loaded_files{$input_file} = "'$loadfile' line $loadline";
978 }
979
980 # if the file isn't being loaded from a glob, it should exist.
981 elsif ( $expanded == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700982 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -0600983 }
984
985 my $line_in_file = 0;
986 while ( my $line = shift @file_data ) {
987
988 #handle line continuation.
989 my $continue_line = 0;
990 while ($line =~ /(.*)\s+\\$/) {
991 my $text = $1;
992
993 # get rid of leading whitespace on all but the first and last lines
994 $text =~ s/^\s*/ / if ($continue_line);
995
996 $dir_file_data[$line_in_file]{text} .= $text;
997 $line = shift @file_data;
998 $continue_line++;
999
1000 #put the data into the continued lines (other than the first)
1001 $line =~ /^\s*(.*)\s*$/;
1002
1003 $dir_file_data[$line_in_file + $continue_line]{text} = "\t# continued line ( " . $1 . " )\n";
1004 $dir_file_data[$line_in_file + $continue_line]{filename} = $input_file;
1005 $dir_file_data[$line_in_file + $continue_line]{file_line_no} = $line_in_file + $continue_line + 1;
1006
1007 #get rid of multiple leading spaces for last line
1008 $line = " $1\n";
1009 }
1010
1011 $dir_file_data[$line_in_file]{text} .= $line;
1012 $dir_file_data[$line_in_file]{filename} = $input_file;
1013 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1014
1015 $line_in_file++;
1016 if ($continue_line) {
1017 $line_in_file += $continue_line;
1018 }
1019 }
1020
1021 if ($topfile) {
1022 my %file_data;
1023 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n" ;
1024 $file_data{filename} = $topfile;
1025 $file_data{file_line_no} = "($topline)";
1026 unshift (@dir_file_data, \%file_data);
1027 }
1028
1029 return @dir_file_data;
1030}
1031
1032
1033#-------------------------------------------------------------------------------
1034# print_wholeconfig - prints out the parsed Kconfig file
1035#-------------------------------------------------------------------------------
1036sub print_wholeconfig {
1037
1038 return unless $print_full_output;
1039
1040 for ( my $i = 0 ; $i < $#wholeconfig ; $i++ ) {
1041 my $line = $wholeconfig[$i];
1042 chop( $line->{text} );
1043
1044 #replace tabs with spaces for consistency
1045 $line->{text} =~ s/\t/ /g;
1046 printf "%-120s # $line->{filename} line $line->{file_line_no}\n", $line->{text};
1047 }
1048}
1049
1050#-------------------------------------------------------------------------------
1051# check_if_file_referenced - checks for kconfig files that are not being parsed
1052#-------------------------------------------------------------------------------
1053sub check_if_file_referenced {
1054 my $filename = $File::Find::name;
1055 if ( ( $filename =~ /Kconfig/ ) && ( !exists $loaded_files{$filename} ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001056 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001057 }
1058}
1059
1060#-------------------------------------------------------------------------------
1061# check_arguments parse the command line arguments
1062#-------------------------------------------------------------------------------
1063sub check_arguments {
1064 my $show_usage = 0;
1065 GetOptions(
1066 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001067 'e|errors_off' => \$suppress_error_output,
1068 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001069 'o|output=s' => \$output_file,
1070 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001071 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001072 'path=s' => \$top_dir,
1073 'c|config=s' => \$config_file,
1074 'G|no_git_grep' => \$dont_use_git_grep,
1075 );
Martin Rothd8080172015-11-26 19:12:44 -07001076
1077 if ($suppress_error_output) {
1078 $suppress_warning_output = 1;
1079 }
1080 if ($suppress_warning_output) {
1081 $show_note_output=0;
1082 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001083}
1084
1085#-------------------------------------------------------------------------------
1086# usage - Print the arguments for the user
1087#-------------------------------------------------------------------------------
1088sub usage {
1089 print "kconfig_lint <options>\n";
1090 print " -o|--output=file Set output filename\n";
1091 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001092 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001093 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001094 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001095 print " --path=dir Path to top level kconfig\n";
1096 print " -c|--config=file Filename of config file to load\n";
1097 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
1098
1099 exit(0);
1100}
1101
11021;