blob: 77913a648300a58eadf3d11985b5352049795e33 [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 Rothd8080172015-11-26 19:12:44 -0700140 show_error("#ifdef 'CONFIG_$symbol' used in $file at line $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 Rothd8080172015-11-26 19:12:44 -0700157 show_error("defined 'CONFIG_$symbol' used in $file at line $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 Rothd8080172015-11-26 19:12:44 -0700180 show_warning("#define of symbol 'CONFIG_$symbol' used in $file at line $lineno.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600181 } else {
Martin Rothd8080172015-11-26 19:12:44 -0700182 show_warning("#define 'CONFIG_$symbol' used in $file at line $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 Rothd8080172015-11-26 19:12:44 -0700242 show_warning("Default for '$sym' referenced in $filename at line $line_no will never be set - overridden by default set in $default_filename at line $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};
275 show_error("Undefined Symbol '$key' used in $filename at line $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 Rothd8080172015-11-26 19:12:44 -0700337 show_warning("Unused symbol '$key' referenced in $filename at line $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 Rothd8080172015-11-26 19:12:44 -0700451 show_error("'endchoice' keyword not within a choice block in $filename at line $line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600452 }
453
454 $inside_choice = "";
455 if ( $configs_inside_choice == 0 ) {
Martin Rothd8080172015-11-26 19:12:44 -0700456 show_error("choice block has no symbols in $filename at line $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 Rothd8080172015-11-26 19:12:44 -0700464 show_error("Keyword 'optional' appears inside config for '$inside_config' in $filename at line $line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600465 }
466 if ( !$inside_choice ) {
Martin Rothd8080172015-11-26 19:12:44 -0700467 show_error("Keyword 'optional' appears outside of a choice block in $filename at line $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 Rothd8080172015-11-26 19:12:44 -0700521 show_error("Keyword 'select' appears outside of config in $filename at line $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 Rothd8080172015-11-26 19:12:44 -0700548 show_error("$line ($filename line $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 Rothbcaaad12015-10-18 11:16:25 -0600631 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename line $line_no does";
632 print " not match the previously defined range $symbols{$inside_config}{range1} $symbols{$inside_config}{range2}";
633 print " defined in $symbols{$inside_config}{range_file} on line";
634 print " $symbols{$inside_config}{range_line_no}.\n";
635 }
636 }
637 }
638 else {
639 $symbols{$inside_config}{range1} = $range1;
640 $symbols{$inside_config}{range2} = $range2;
641 $symbols{$inside_config}{range_file} = $filename;
642 $symbols{$inside_config}{range_line_no} = $line_no;
643 }
644 }
645 else {
Martin Rothd8080172015-11-26 19:12:44 -0700646 show_error("Range entry in $filename line $line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600647 }
648}
649
650#-------------------------------------------------------------------------------
651# handle_default
652#-------------------------------------------------------------------------------
653sub handle_default {
654 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
655 my $expression;
656 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
657
658 if ($inside_config) {
659 handle_expressions( $default, $inside_config, $filename, $line_no );
660 my $sym_num = $symbols{$inside_config}{count};
661
662 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
663 $symbols{$inside_config}{$sym_num}{default_max} = 0;
664 }
665 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
666 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
667 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
668 if ($expression) {
669 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
670 }
671 }
672 elsif ($inside_choice) {
673 handle_expressions( $default, $inside_config, $filename, $line_no );
674 }
675 else {
Martin Rothd8080172015-11-26 19:12:44 -0700676 show_error("$name entry in $filename line $line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600677 }
678}
679
680#-------------------------------------------------------------------------------
681# handle_if_line
682#-------------------------------------------------------------------------------
683sub handle_if_line {
684 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
685
686 if ( $exprline !~ /if/ ) {
687 return ( $exprline, "" );
688 }
689
690 #remove any quotes that might have an 'if' in them
691 my $savequote;
692 if ( $exprline =~ /^\s*("[^"]+")/ ) {
693 $savequote = $1;
694 $exprline =~ s/^\s*("[^"]+")//;
695 }
696
697 my $expr = "";
698 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
699 $expr = $1;
700 $exprline =~ s/\s*if\s+.*$//;
701
702 if ($expr) {
703 handle_expressions( $expr, $inside_config, $filename, $line_no );
704 }
705 }
706
707 if ($savequote) {
708 $exprline = $savequote;
709 }
710
711 return ( $exprline, $expr );
712}
713
714#-------------------------------------------------------------------------------
715# handle_expressions - log which symbols are being used
716#-------------------------------------------------------------------------------
717sub handle_expressions {
718 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
719
720 return unless ($exprline);
721
722 #filter constant symbols first
723 if ( $exprline =~ /^\s*"?([yn])"?\s*$/ ) { # constant y/n
724 return;
725 }
726 elsif ( $exprline =~ /^\s*"?((?:-)\d+)"?\s*$/ ) { # int values
727 return;
728 }
729 elsif ( $exprline =~ /^\s*"?((?:-)?(?:0x)?\p{XDigit})+"?\s*$/ ) { # hex values
730 return;
731 }
732 elsif ( $exprline =~ /^\s*("[^"]*")\s*$/ ) { # String values
733 return;
734 }
735 elsif ( $exprline =~ /^\s*([A-Za-z0-9_]+)\s*$/ ) { # <symbol> (1)
736 add_referenced_symbol( $1, $filename, $line_no );
737 }
738 elsif ( $exprline =~ /^\s*!(.+)$/ ) { # '!' <expr> (5)
739
740 handle_expressions( $1, $inside_config, $filename, $line_no );
741 }
742 elsif ( $exprline =~ /^\s*\(([^)]+)\)\s*$/ ) { # '(' <expr> ')' (4)
743 handle_expressions( $1, $inside_config, $filename, $line_no );
744 }
745 elsif ( $exprline =~ /^\s*(.+)\s*!=\s*(.+)\s*$/ ) { # <symbol> '!=' <symbol> (3)
746 handle_expressions( $1, $inside_config, $filename, $line_no );
747 handle_expressions( $2, $inside_config, $filename, $line_no );
748 }
749 elsif ( $exprline =~ /^\s*(.+)\s*=\s*(.+)\s*$/ ) { # <symbol> '=' <symbol> (2)
750 handle_expressions( $1, $inside_config, $filename, $line_no );
751 handle_expressions( $2, $inside_config, $filename, $line_no );
752 }
753 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*&&\s*(.+)\s*$/ ) { # <expr> '&&' <expr> (6)
754 handle_expressions( $1, $inside_config, $filename, $line_no );
755 handle_expressions( $2, $inside_config, $filename, $line_no );
756 }
757 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*\|\|\s*(.+)\s*$/ ) { # <expr> '||' <expr> (7)
758 handle_expressions( $1, $inside_config, $filename, $line_no );
759 handle_expressions( $2, $inside_config, $filename, $line_no );
760 }
761
762 # work around kconfig spec violation for now - paths not in quotes
763 elsif ( $exprline =~ /^\s*([A-Za-z0-9_\-\/]+)\s*$/ ) { # <symbol> (1)
764 return;
765 }
766 else {
Martin Rothd8080172015-11-26 19:12:44 -0700767 show_error("Unrecognized expression '$exprline' in $filename line $line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600768 }
769
770 return;
771}
772
773#-------------------------------------------------------------------------------
774# add_referenced_symbol
775#-------------------------------------------------------------------------------
776sub add_referenced_symbol {
777 my ( $symbol, $filename, $line_no ) = @_;
778 if ( exists $referenced_symbols{$symbol} ) {
779 $referenced_symbols{$symbol}{count}++;
780 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
781 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
782 }
783 else {
784 $referenced_symbols{$symbol}{count} = 0;
785 $referenced_symbols{$symbol}{0}{filename} = $filename;
786 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
787 }
788}
789
790#-------------------------------------------------------------------------------
791# handle_help
792#-------------------------------------------------------------------------------
793{
794 #create a non-global static variable by enclosing it and the subroutine
795 my $help_whitespace = ""; #string to show length of the help whitespace
796
797 sub handle_help {
798 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
799
800 if ($inside_help) {
801
802 #get the indentation level if it's not already set.
803 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
804 $line =~ /^(\s+)/; #find the indentation level.
805 $help_whitespace = $1;
806 if ( !$help_whitespace ) {
Martin Rothd8080172015-11-26 19:12:44 -0700807 show_warning("$filename line $line_no help text starts with no whitespace.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600808 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -0600809 }
810 }
811
812 #help ends at the first line which has a smaller indentation than the first line of the help text.
813 if ( ( $line !~ /$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
814 $inside_help = 0;
815 $help_whitespace = "";
816 }
817 else { #if it's not ended, add the line to the helptext array for the symbol's instance
818 if ($inside_config) {
819 my $sym_num = $symbols{$inside_config}{count};
820 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
821 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
822 }
823 }
824 }
825 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
826 $inside_help = $line_no;
827 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700828 if ($show_note_output) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600829 print "# Note: $filename line $line_no help is not inside a config or choice block.\n";
830 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600831 }
832 elsif ($inside_config) {
833 $help_whitespace = "";
834 my $sym_num = $symbols{$inside_config}{count};
835 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
836 $symbols{$inside_config}{$sym_num}{helptext} = ();
837 }
838 }
839 return $inside_help;
840 }
841}
842
843#-------------------------------------------------------------------------------
844# handle_type
845#-------------------------------------------------------------------------------
846sub handle_type {
847 my ( $type, $inside_config, $filename, $line_no ) = @_;
848
849 my $expression;
850 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
851
852 if ($inside_config) {
853 if ( exists( $symbols{$inside_config}{type} ) ) {
854 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Rothd8080172015-11-26 19:12:44 -0700855 show_error("Config '$inside_config' type entry $type at $filename line $line_no does not match $symbols{$inside_config}{type} defined in $symbols{$inside_config}{type_file} on line $symbols{$inside_config}{type_line_no}.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600856 }
857 }
858 else {
859 $symbols{$inside_config}{type} = $type;
860 $symbols{$inside_config}{type_file} = $filename;
861 $symbols{$inside_config}{type_line_no} = $line_no;
862 }
863 }
864 else {
Martin Rothd8080172015-11-26 19:12:44 -0700865 show_error("Type entry in $filename line $line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600866 }
867}
868
869#-------------------------------------------------------------------------------
870# handle_prompt
871#-------------------------------------------------------------------------------
872sub handle_prompt {
873 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
874
875 my $expression;
876 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
877
878 if ($inside_config) {
879 if ( $prompt !~ /^\s*$/ ) {
880 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
881 $prompt = $1;
882 }
883
884 if ( !defined @$menu_array_ref[0] ) {
Martin Rothd8080172015-11-26 19:12:44 -0700885 show_error("Symbol '$inside_config' with prompt '$prompt' appears outside of a menu in $filename at line $line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600886 }
887
888 my $sym_num = $symbols{$inside_config}{count};
889 unless ( exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
890 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
891 }
892 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
893 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
894 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
895 if ($expression) {
896 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
897 }
898 }
899 }
900 elsif ($inside_choice) {
901
902 #do nothing
903 }
904 else {
Martin Rothd8080172015-11-26 19:12:44 -0700905 show_error("$name entry in $filename line $line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600906 }
907}
908
909#-------------------------------------------------------------------------------
910# simple_line_checks - Does some basic checks on the current line, then cleans the line
911# up for further processing.
912#-------------------------------------------------------------------------------
913sub simple_line_checks {
914 my ( $line, $filename, $line_no ) = @_;
915
916 #check for spaces instead of tabs
917 if ( $line =~ /^ +/ ) {
Martin Rothd8080172015-11-26 19:12:44 -0700918 show_error("$filename line $line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600919 }
920
921 #verify a linefeed at the end of the line
922 if ( $line !~ /.*\n/ ) {
Martin Rothd8080172015-11-26 19:12:44 -0700923 show_error("$filename line $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 -0600924 $line =~ s/\s*$//;
925 }
926 else {
927 chop($line);
928 }
929
930 return $line;
931}
932
933#-------------------------------------------------------------------------------
934# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
935#-------------------------------------------------------------------------------
936sub load_kconfig_file {
937 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
938 my @file_data;
939 my @dir_file_data;
940
941 #recursively handle coreboot's new source glob operator
942 if ( $input_file =~ /^(.*?)\/\*\/(.*)$/ ) {
943 my $dir_prefix = $1;
944 my $dir_suffix = $2;
945 if ( -d "$dir_prefix" ) {
946
947 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
948 my @dirlist = sort { $a cmp $b } readdir(D);
949 closedir(D);
950
951 while ( my $directory = shift @dirlist ) {
952
953 #ignore non-directory files
954 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ ) ) {
955 push @dir_file_data, load_kconfig_file( "$dir_prefix/$directory/$dir_suffix", $input_file, $loadline, 1, $loadfile, $loadline );
956 }
957 }
958 }
959
960 #the directory should exist when using a glob
961 else {
Martin Rothd8080172015-11-26 19:12:44 -0700962 show_warning("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -0600963 }
964 }
965
966 #if the file exists, try to load it.
967 elsif ( -e "$input_file" ) {
968
969 #throw a warning if the file has already been loaded.
970 if ( exists $loaded_files{$input_file} ) {
Martin Rothd8080172015-11-26 19:12:44 -0700971 show_warning("'$input_file' sourced in '$loadfile' at line $loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -0600972 }
973
974 #load the file's contents and mark the file as loaded for checking later
975 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
976 @file_data = <$HANDLE>;
977 close $HANDLE;
978 $loaded_files{$input_file} = "'$loadfile' line $loadline";
979 }
980
981 # if the file isn't being loaded from a glob, it should exist.
982 elsif ( $expanded == 0 ) {
Martin Rothd8080172015-11-26 19:12:44 -0700983 show_warning("Could not find file '$input_file' sourced in $loadfile at line $loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -0600984 }
985
986 my $line_in_file = 0;
987 while ( my $line = shift @file_data ) {
988
989 #handle line continuation.
990 my $continue_line = 0;
991 while ($line =~ /(.*)\s+\\$/) {
992 my $text = $1;
993
994 # get rid of leading whitespace on all but the first and last lines
995 $text =~ s/^\s*/ / if ($continue_line);
996
997 $dir_file_data[$line_in_file]{text} .= $text;
998 $line = shift @file_data;
999 $continue_line++;
1000
1001 #put the data into the continued lines (other than the first)
1002 $line =~ /^\s*(.*)\s*$/;
1003
1004 $dir_file_data[$line_in_file + $continue_line]{text} = "\t# continued line ( " . $1 . " )\n";
1005 $dir_file_data[$line_in_file + $continue_line]{filename} = $input_file;
1006 $dir_file_data[$line_in_file + $continue_line]{file_line_no} = $line_in_file + $continue_line + 1;
1007
1008 #get rid of multiple leading spaces for last line
1009 $line = " $1\n";
1010 }
1011
1012 $dir_file_data[$line_in_file]{text} .= $line;
1013 $dir_file_data[$line_in_file]{filename} = $input_file;
1014 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1015
1016 $line_in_file++;
1017 if ($continue_line) {
1018 $line_in_file += $continue_line;
1019 }
1020 }
1021
1022 if ($topfile) {
1023 my %file_data;
1024 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n" ;
1025 $file_data{filename} = $topfile;
1026 $file_data{file_line_no} = "($topline)";
1027 unshift (@dir_file_data, \%file_data);
1028 }
1029
1030 return @dir_file_data;
1031}
1032
1033
1034#-------------------------------------------------------------------------------
1035# print_wholeconfig - prints out the parsed Kconfig file
1036#-------------------------------------------------------------------------------
1037sub print_wholeconfig {
1038
1039 return unless $print_full_output;
1040
1041 for ( my $i = 0 ; $i < $#wholeconfig ; $i++ ) {
1042 my $line = $wholeconfig[$i];
1043 chop( $line->{text} );
1044
1045 #replace tabs with spaces for consistency
1046 $line->{text} =~ s/\t/ /g;
1047 printf "%-120s # $line->{filename} line $line->{file_line_no}\n", $line->{text};
1048 }
1049}
1050
1051#-------------------------------------------------------------------------------
1052# check_if_file_referenced - checks for kconfig files that are not being parsed
1053#-------------------------------------------------------------------------------
1054sub check_if_file_referenced {
1055 my $filename = $File::Find::name;
1056 if ( ( $filename =~ /Kconfig/ ) && ( !exists $loaded_files{$filename} ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001057 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001058 }
1059}
1060
1061#-------------------------------------------------------------------------------
1062# check_arguments parse the command line arguments
1063#-------------------------------------------------------------------------------
1064sub check_arguments {
1065 my $show_usage = 0;
1066 GetOptions(
1067 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001068 'e|errors_off' => \$suppress_error_output,
1069 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001070 'o|output=s' => \$output_file,
1071 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001072 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001073 'path=s' => \$top_dir,
1074 'c|config=s' => \$config_file,
1075 'G|no_git_grep' => \$dont_use_git_grep,
1076 );
Martin Rothd8080172015-11-26 19:12:44 -07001077
1078 if ($suppress_error_output) {
1079 $suppress_warning_output = 1;
1080 }
1081 if ($suppress_warning_output) {
1082 $show_note_output=0;
1083 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001084}
1085
1086#-------------------------------------------------------------------------------
1087# usage - Print the arguments for the user
1088#-------------------------------------------------------------------------------
1089sub usage {
1090 print "kconfig_lint <options>\n";
1091 print " -o|--output=file Set output filename\n";
1092 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001093 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001094 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001095 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001096 print " --path=dir Path to top level kconfig\n";
1097 print " -c|--config=file Filename of config file to load\n";
1098 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
1099
1100 exit(0);
1101}
1102
11031;