blob: bbf3e38142e0a42d98c52534b536465378307f28 [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>
Martin Roth63ea4932016-01-25 16:08:27 -07007# Copyright (C) 2015-2016 Google, Inc.
Martin Rothbcaaad12015-10-18 11:16:25 -06008#
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 Rothabf7d4d2016-02-19 10:24:25 -070029# If taint mode is enabled, Untaint the path - git and grep must be in /bin, /usr/bin or /usr/local/bin
30if ( ${^TAINT} ) {
31 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
32 delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' };
33}
34
Martin Roth1b44f7e2016-01-11 14:31:38 -070035my $suppress_error_output = 0; # flag to prevent error text
36my $suppress_warning_output = 0; # flag to prevent warning text
37my $show_note_output = 0; # flag to show minor notes text
38my $print_full_output = 0; # flag to print wholeconfig output
39my $output_file = "-"; # filename of output - set stdout by default
Martin Rothd8080172015-11-26 19:12:44 -070040my $dont_use_git_grep = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -060041
Martin Rothabf7d4d2016-02-19 10:24:25 -070042# Globals
Martin Roth1b44f7e2016-01-11 14:31:38 -070043my $top_dir = "."; # Directory where Kconfig is run
44my $root_dir = "src"; # Directory of the top level Kconfig file
45my $errors_found = 0; # count of errors
Martin Rothd8080172015-11-26 19:12:44 -070046my $warnings_found = 0;
Martin Roth63ea4932016-01-25 16:08:27 -070047my $exclude_dirs_and_files =
Martin Rothab273d32016-11-23 17:28:00 -070048 '^build/\|^coreboot-builds/\|^configs/\|^util/\|^\.git/\|^payloads\|^Documentation\|^3rdparty'
Martin Roth63ea4932016-01-25 16:08:27 -070049 . '\|' . # directories to exclude when searching for used symbols
Martin Rothab273d32016-11-23 17:28:00 -070050 '\.config\|\.txt$\|\.tex$\|\.tags'; #files to exclude when looking for symbols
Martin Rothe69c58d2016-11-15 21:08:42 -070051my $payload_files_to_check='payloads/Makefile.inc payloads/external/Makefile.inc';
Martin Roth08cf90f2016-01-25 19:54:16 -070052my $config_file = ""; # name of config file to load symbol values from.
53my @wholeconfig; # document the entire kconfig structure
54my %loaded_files; # list of each Kconfig file loaded
55my %symbols; # main structure of all symbols declared
56my %referenced_symbols; # list of symbols referenced by expressions or select statements
57my %used_symbols; # structure of symbols used in the tree, and where they're found
58my @collected_symbols; #
59my %selected_symbols; # list of symbols that are enabled by a select statement
Martin Rothbcaaad12015-10-18 11:16:25 -060060
Martin Roth6bfbf1c2016-01-25 16:12:49 -070061my $exclude_unused = '_SPECIFIC_OPTIONS|SOUTH_BRIDGE_OPTIONS';
62
Martin Rothbcaaad12015-10-18 11:16:25 -060063Main();
64
65#-------------------------------------------------------------------------------
66# Main
67#
68# Start by loading and parsing the top level Kconfig, this pulls in the other
69# files. Parsing the tree creates several arrays and hashes that can be used
70# to check for errors
71#-------------------------------------------------------------------------------
72sub Main {
73
74 check_arguments();
75 open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n";
76
Martin Roth1b44f7e2016-01-11 14:31:38 -070077 if ( defined $top_dir ) {
78 chdir $top_dir or die "Error: can't cd to $top_dir\n";
Martin Rothbcaaad12015-10-18 11:16:25 -060079 }
80
Martin Roth1b44f7e2016-01-11 14:31:38 -070081 die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir );
Martin Rothbcaaad12015-10-18 11:16:25 -060082
83 #load the Kconfig tree, checking what we can and building up all the hash tables
84 build_and_parse_kconfig_tree("$root_dir/Kconfig");
85
Martin Rothbcaaad12015-10-18 11:16:25 -060086 load_config($config_file) if ($config_file);
87
Martin Roth08705f12016-11-09 14:27:00 -070088 check_type();
Martin Rothbcaaad12015-10-18 11:16:25 -060089 check_defaults();
90 check_referenced_symbols();
91
92 collect_used_symbols();
93 check_used_symbols();
94 check_for_ifdef();
95 check_for_def();
96 check_is_enabled();
Martin Rothb7c39b22016-01-14 09:04:53 -070097 check_selected_symbols();
Martin Rothbcaaad12015-10-18 11:16:25 -060098
Martin Rothabf7d4d2016-02-19 10:24:25 -070099 # Run checks based on the data that was found
100 if ( ( !$suppress_warning_output ) && ( ${^TAINT} == 0 ) ) {
101
102 # The find function is tainted - only run it if taint checking
103 # is disabled and warnings are enabled.
104 find( \&check_if_file_referenced, $root_dir );
105 }
106
Martin Rothbcaaad12015-10-18 11:16:25 -0600107 print_wholeconfig();
108
Martin Rothd8080172015-11-26 19:12:44 -0700109 if ($errors_found) {
110 print "# $errors_found errors";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700111 if ($warnings_found) {
Martin Rothd8080172015-11-26 19:12:44 -0700112 print ", $warnings_found warnings";
113 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700114 print "\n";
Martin Rothd8080172015-11-26 19:12:44 -0700115 }
116
Martin Roth1b44f7e2016-01-11 14:31:38 -0700117 exit( $errors_found + $warnings_found );
Martin Rothd8080172015-11-26 19:12:44 -0700118}
119
120#-------------------------------------------------------------------------------
121# Print and count errors
122#-------------------------------------------------------------------------------
123sub show_error {
124 my ($error_msg) = @_;
125 unless ($suppress_error_output) {
126 print "#!!!!! Error: $error_msg\n";
127 $errors_found++;
128 }
129}
130
131#-------------------------------------------------------------------------------
132# Print and count warnings
133#-------------------------------------------------------------------------------
134sub show_warning {
135 my ($warning_msg) = @_;
136 unless ($suppress_warning_output) {
137 print "#!!!!! Warning: $warning_msg\n";
138 $warnings_found++;
139 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600140}
141
142#-------------------------------------------------------------------------------
Martin Rothb7c39b22016-01-14 09:04:53 -0700143# check selected symbols for validity
144# they must be bools
145# they cannot select symbols created in 'choice' blocks
146#-------------------------------------------------------------------------------
147sub check_selected_symbols {
148
149 #loop through symbols found in expressions and used by 'select' keywords
150 foreach my $symbol ( sort ( keys %selected_symbols ) ) {
151 my $type_failure = 0;
152 my $choice_failure = 0;
153
154 #errors selecting symbols that don't exist are already printed, so we
155 #don't need to print them again here
156
157 #make sure the selected symbols are bools
158 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "bool" ) ) {
159 $type_failure = 1;
160 }
161
162 #make sure we're not selecting choice symbols
163 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{choice} ) ) {
164 $choice_failure = 1;
165 }
166
167 #loop through each instance of the symbol to print out all of the failures
168 for ( my $i = 0 ; $i <= $referenced_symbols{$symbol}{count} ; $i++ ) {
169 next if ( !exists $selected_symbols{$symbol}{$i} );
170 my $file = $referenced_symbols{$symbol}{$i}{filename};
171 my $lineno = $referenced_symbols{$symbol}{$i}{line_no};
172 if ($type_failure) {
173 show_error(
174 "CONFIG_$symbol' selected at $file:$lineno." . " Selects only work on symbols of type bool." );
175 }
176 if ($choice_failure) {
177 show_error(
178 "'CONFIG_$symbol' selected at $file:$lineno." . " Symbols created in a choice cannot be selected." );
179 }
180 }
181 }
182}
183
184#-------------------------------------------------------------------------------
Martin Rothbcaaad12015-10-18 11:16:25 -0600185# check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and
186# #if defined(CONFIG_[symbol_name]).
187#
188# #ifdef symbol is valid for strings, but bool, hex, and INT are always defined.
189# #if defined(symbol) && symbol is also a valid construct.
190#-------------------------------------------------------------------------------
191sub check_for_ifdef {
192 my @ifdef_symbols = @collected_symbols;
193
194 #look for #ifdef SYMBOL
195 while ( my $line = shift @ifdef_symbols ) {
196 if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s+CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700197 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600198 my $lineno = $2;
199 my $symbol = $3;
200
Martin Roth1b44f7e2016-01-11 14:31:38 -0700201 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
Martin Rotha7d00272016-10-03 23:00:04 +0200202 show_warning( "#ifdef 'CONFIG_$symbol' used at $file:$lineno."
Martin Roth1b44f7e2016-01-11 14:31:38 -0700203 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600204 }
205 }
206 }
207
208 # look for (#if) defined SYMBOL
209 @ifdef_symbols = @collected_symbols;
210 while ( my $line = shift @ifdef_symbols ) {
211 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700212 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600213 my $lineno = $2;
214 my $symbol = $3;
215
216 #ignore '#if defined(symbol) && symbol' type statements
Martin Roth1b44f7e2016-01-11 14:31:38 -0700217 next
218 if ( $line =~ /^([^:]+):(\d+):.+defined\s*\(\s*CONFIG_$symbol.*(&&|\|\|)\s*!?\s*\(?\s*CONFIG_$symbol/ );
Martin Rothbcaaad12015-10-18 11:16:25 -0600219
Martin Roth1b44f7e2016-01-11 14:31:38 -0700220 if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) {
Martin Roth15f4d8c2016-01-31 10:44:33 -0700221 show_warning( "defined 'CONFIG_$symbol' used at $file:$lineno."
Martin Roth1b44f7e2016-01-11 14:31:38 -0700222 . " Symbols of type '$symbols{$symbol}{type}' are always defined." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600223 }
224 }
225 }
Martin Roth66223762016-01-31 10:34:13 -0700226
227 my @collected_is_enabled;
228 if ($dont_use_git_grep) {
229 @collected_is_enabled =
230 `grep -Irn -- "[[:space:]]IS_ENABLED[[:space:]]*(.*)" | grep -v '$exclude_dirs_and_files' | grep -v "kconfig.h"`;
231 }
232 else {
233 @collected_is_enabled =
234 `git grep -In -- "[[:space:]]IS_ENABLED[[:space:]]*(.*)" | grep -v '$exclude_dirs_and_files' | grep -v "kconfig.h"`;
235 }
236
237 while ( my $line = shift @collected_is_enabled ) {
238 if ($line !~ /CONFIG_/ && $line =~ /^([^:]+):(\d+):.+IS_ENABLED\s*\(\s*(\w+)/ ) {
239 my $file = $1;
240 my $lineno = $2;
241 my $symbol = $3;
242 if ( ( exists $symbols{$symbol} ) ) {
243 show_error("IS_ENABLED missing CONFIG_ prefix on symbol '$symbol' at $file:$lineno.");
244 }
245 }
246 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600247}
248
249#-------------------------------------------------------------------------------
250# check_for_def - Look for instances of #define CONFIG_[symbol_name]
251#
252# Symbols should not be redefined outside of Kconfig, and #defines should not
253# look like symbols
254#-------------------------------------------------------------------------------
255sub check_for_def {
256 my @def_symbols = @collected_symbols;
257
258 #look for #ifdef SYMBOL
259 while ( my $line = shift @def_symbols ) {
260 if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700261 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600262 my $lineno = $2;
263 my $symbol = $3;
264
Martin Roth1b44f7e2016-01-11 14:31:38 -0700265 if ( ( exists $symbols{$symbol} ) ) {
Martin Rotha7d00272016-10-03 23:00:04 +0200266 show_warning("#define of symbol 'CONFIG_$symbol' used at $file:$lineno.");
Martin Roth1b44f7e2016-01-11 14:31:38 -0700267 }
268 else {
269 show_warning( "#define 'CONFIG_$symbol' used at $file:$lineno."
270 . " Other #defines should not look like Kconfig symbols." );
271 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600272 }
273 }
274}
275
276#-------------------------------------------------------------------------------
Martin Roth08705f12016-11-09 14:27:00 -0700277# check_type - Make sure that all symbols have a type defined.
278#
279# Conflicting types are found when parsing the Kconfig tree.
280#-------------------------------------------------------------------------------
281sub check_type {
282
283 # loop through each defined symbol
284 foreach my $sym ( sort ( keys %symbols ) ) {
285
286 # Make sure there's a type set for the symbol
287 if (!defined $symbols{$sym}{type}) {
288
289 #loop through each instance of that symbol
290 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
291
292 my $filename = $symbols{$sym}{$sym_num}{file};
293 my $line_no = $symbols{$sym}{$sym_num}{line_no};
294
295 show_error("No type defined for symbol $sym defined at $filename:$line_no.");
296 }
297 }
298 }
299}
300
301#-------------------------------------------------------------------------------
Martin Rothbcaaad12015-10-18 11:16:25 -0600302# check_is_enabled - The IS_ENABLED() macro is only valid for symbols of type
303# bool. It would probably work on type hex or int if the value was 0 or 1, but
304# this seems like a bad plan. Using it on strings is dead out.
305#-------------------------------------------------------------------------------
306sub check_is_enabled {
Martin Rothb6acc302015-11-27 18:51:19 -0700307 my @is_enabled_symbols = @collected_symbols;
Martin Rothbcaaad12015-10-18 11:16:25 -0600308
309 #sort through symbols found by grep and store them in a hash for easy access
310 while ( my $line = shift @is_enabled_symbols ) {
Martin Rothb6acc302015-11-27 18:51:19 -0700311 if ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700312 my $file = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600313 my $lineno = $2;
Martin Rothb6acc302015-11-27 18:51:19 -0700314 $line = $3;
Martin Roth572a8562016-01-25 16:14:09 -0700315 if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) {
Martin Rothb6acc302015-11-27 18:51:19 -0700316 show_warning("# uninterpreted IS_ENABLED at $file:$lineno: $line");
317 next;
Martin Rothbcaaad12015-10-18 11:16:25 -0600318 }
Martin Rothb6acc302015-11-27 18:51:19 -0700319 while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) {
320 my $symbol = $2;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700321 $line = $1 . $3;
Martin Rothb6acc302015-11-27 18:51:19 -0700322
323 #make sure that
Martin Roth1b44f7e2016-01-11 14:31:38 -0700324 if ( exists $symbols{$symbol} ) {
325 if ( $symbols{$symbol}{type} ne "bool" ) {
326 show_error( "IS_ENABLED(CONFIG_$symbol) used at $file:$lineno."
327 . " IS_ENABLED is only valid for type 'bool', not '$symbols{$symbol}{type}'." );
Martin Rothb6acc302015-11-27 18:51:19 -0700328 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700329 }
330 else {
Martin Rothb6acc302015-11-27 18:51:19 -0700331 show_error("IS_ENABLED() used on unknown value CONFIG_$symbol at $file:$lineno.");
332 }
333 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600334 }
335 }
336}
337
338#-------------------------------------------------------------------------------
339# check_defaults - Look for defaults that come after a default with no
340# dependencies.
341#
342# TODO - check for defaults with the same dependencies
343#-------------------------------------------------------------------------------
344sub check_defaults {
345
346 # loop through each defined symbol
347 foreach my $sym ( sort ( keys %symbols ) ) {
348 my $default_set = 0;
349 my $default_filename = "";
350 my $default_line_no = "";
351
352 #loop through each instance of that symbol
353 for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) {
354
355 #loop through any defaults for that instance of that symbol, if there are any
356 next unless ( exists $symbols{$sym}{$sym_num}{default_max} );
357 for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) {
358
Martin Rothfa956252016-09-30 15:51:32 -0600359 my $filename = $symbols{$sym}{$sym_num}{file};
360 my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
361
Martin Roth08705f12016-11-09 14:27:00 -0700362 # Make sure there's a type set for the symbol
363 next if (!defined $symbols{$sym}{type});
364
Martin Rothfa956252016-09-30 15:51:32 -0600365 # skip good defaults
366 if (! ((($symbols{$sym}{type} eq "hex") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^0x/)) ||
367 (($symbols{$sym}{type} eq "int") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[-0-9]+$/)) ||
368 (($symbols{$sym}{type} eq "string") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^".*"$/)) ||
369 (($symbols{$sym}{type} eq "bool") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[yn]$/)))
370 ) {
371
372 my ($checksym) = $symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /(\w+)/;
373
374 if (! exists $symbols{$checksym}) {
375
376 # verify the symbol type against the default value
377 if ($symbols{$sym}{type} eq "hex") {
378 show_error("non hex default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for hex symbol $sym at $filename:$line_no.");
379 } elsif ($symbols{$sym}{type} eq "int") {
380 show_error("non int default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for int symbol $sym at $filename:$line_no.");
381 } elsif ($symbols{$sym}{type} eq "string") {
382 # TODO: Remove special MAINBOARD_DIR check
383 if ($sym ne "MAINBOARD_DIR") {
384 show_error("no quotes around default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for string symbol $sym at $filename:$line_no.");
385 }
386 } elsif ($symbols{$sym}{type} eq "bool") {
387 if ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /[01YN]/) {
388 show_warning("default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) for bool symbol $sym uses value other than y/n at $filename:$line_no.");
389 } else {
390 show_error("non bool default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for bool symbol $sym at $filename:$line_no.");
391 }
392 }
393 }
394 }
395
Martin Rothbcaaad12015-10-18 11:16:25 -0600396 #if a default is already set, display an error
397 if ($default_set) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700398 show_warning( "Default for '$sym' referenced at $filename:$line_no will never be set"
399 . " - overridden by default set at $default_filename:$default_line_no" );
Martin Rothbcaaad12015-10-18 11:16:25 -0600400 }
401 else {
402 #if no default is set, see if this is a default with no dependencies
403 unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} )
404 || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) )
405 {
406 $default_set = 1;
407 $default_filename = $symbols{$sym}{$sym_num}{file};
408 $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no};
409 }
410 }
411 }
412 }
413 }
414}
415
416#-------------------------------------------------------------------------------
417# check_referenced_symbols - Make sure the symbols referenced by expressions and
418# select statements are actually valid symbols.
419#-------------------------------------------------------------------------------
420sub check_referenced_symbols {
421
422 #loop through symbols found in expressions and used by 'select' keywords
423 foreach my $key ( sort ( keys %referenced_symbols ) ) {
424
425 #make sure the symbol was defined by a 'config' or 'choice' keyword
426 next if ( exists $symbols{$key} );
427
Martin Rothd8080172015-11-26 19:12:44 -0700428 #loop through each instance of the symbol to print out all of the invalid references
429 for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) {
430 my $filename = $referenced_symbols{$key}{$i}{filename};
431 my $line_no = $referenced_symbols{$key}{$i}{line_no};
Martin Roth7aa3cea2015-11-27 18:45:45 -0700432 show_error("Undefined Symbol '$key' used at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600433 }
434 }
435}
436
437#-------------------------------------------------------------------------------
438#-------------------------------------------------------------------------------
439sub collect_used_symbols {
440 # find all references to CONFIG_ statements in the tree
441
442 if ($dont_use_git_grep) {
Martin Rothe69c58d2016-11-15 21:08:42 -0700443 @collected_symbols = `grep -Irn -- "CONFIG_" | grep -v '$exclude_dirs_and_files'; grep -In -- "CONFIG_" $payload_files_to_check`;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700444 }
445 else {
Martin Rothe69c58d2016-11-15 21:08:42 -0700446 @collected_symbols = `git grep -In -- "CONFIG_" | grep -v '$exclude_dirs_and_files'; git grep -In -- "CONFIG_" $payload_files_to_check`;
Martin Rothbcaaad12015-10-18 11:16:25 -0600447 }
448
449 my @used_symbols = @collected_symbols;
450
451 #sort through symbols found by grep and store them in a hash for easy access
452 while ( my $line = shift @used_symbols ) {
453 while ( $line =~ /[^A-Za-z0-9_]CONFIG_([A-Za-z0-9_]+)/g ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700454 my $symbol = $1;
Martin Rothbcaaad12015-10-18 11:16:25 -0600455 my $filename = "";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700456 if ( $line =~ /^([^:]+):/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600457 $filename = $1;
458 }
459
Martin Roth1b44f7e2016-01-11 14:31:38 -0700460 if ( exists $used_symbols{$symbol}{count} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600461 $used_symbols{$symbol}{count}++;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700462 }
463 else {
464 $used_symbols{$symbol}{count} = 0;
Martin Rothbcaaad12015-10-18 11:16:25 -0600465 }
466 $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename;
Martin Roth1b44f7e2016-01-11 14:31:38 -0700467 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600468 }
469}
470
471#-------------------------------------------------------------------------------
472# check_used_symbols - Checks to see whether or not the created symbols are
473# actually used.
474#-------------------------------------------------------------------------------
475sub check_used_symbols {
476 # loop through all defined symbols and see if they're used anywhere
477 foreach my $key ( sort ( keys %symbols ) ) {
478
Martin Roth6bfbf1c2016-01-25 16:12:49 -0700479 if ( $key =~ /$exclude_unused/ ) {
480 next;
481 }
482
Martin Rothbcaaad12015-10-18 11:16:25 -0600483 #see if they're used internal to Kconfig
484 next if ( exists $referenced_symbols{$key} );
485
486 #see if they're used externally
487 next if exists $used_symbols{$key};
488
489 #loop through the definitions to print out all the places the symbol is defined.
490 for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) {
491 my $filename = $symbols{$key}{$i}{file};
492 my $line_no = $symbols{$key}{$i}{line_no};
Martin Rotha7d00272016-10-03 23:00:04 +0200493 show_warning("Unused symbol '$key' referenced at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600494 }
495 }
496}
497
498#-------------------------------------------------------------------------------
499# build_and_parse_kconfig_tree
500#-------------------------------------------------------------------------------
501#load the initial file and start parsing it
502sub build_and_parse_kconfig_tree {
503 my ($top_level_kconfig) = @_;
504 my @config_to_parse;
505 my @parseline;
506 my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block
507 my @inside_if = (); # stack of if dependencies
508 my $inside_config = ""; # set to symbol name of the config section
509 my @inside_menu = (); # stack of menu names
510 my $inside_choice = "";
511 my $configs_inside_choice;
Martin Roth0e6c0e12016-03-02 12:16:13 -0700512 my %fileinfo;
Martin Rothbcaaad12015-10-18 11:16:25 -0600513
514 #start the tree off by loading the top level kconfig
515 @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 );
516
Martin Rothbcaaad12015-10-18 11:16:25 -0600517 while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) {
518 my $line = $parseline[0]{text};
519 my $filename = $parseline[0]{filename};
520 my $line_no = $parseline[0]{file_line_no};
521
522 #handle help - help text: "help" or "---help---"
523 $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no );
524 $parseline[0]{inside_help} = $inside_help;
525
526 #look for basic issues in the line, strip crlf
527 $line = simple_line_checks( $line, $filename, $line_no );
528
529 #strip comments
530 $line =~ s/\s*#.*$//;
531
532 #don't parse any more if we're inside a help block
533 if ($inside_help) {
534 #do nothing
535 }
536
537 #handle config
538 elsif ( $line =~ /^\s*config/ ) {
539 $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/;
540 my $symbol = $1;
541 $inside_config = $symbol;
542 if ($inside_choice) {
543 $configs_inside_choice++;
544 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700545 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice );
Martin Rothbcaaad12015-10-18 11:16:25 -0600546 }
547
548 #bool|hex|int|string|tristate <expr> [if <expr>]
549 elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) {
550 $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/;
551 my ( $type, $prompt ) = ( $1, $2 );
552 handle_type( $type, $inside_config, $filename, $line_no );
553 handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
554 }
555
556 # def_bool|def_tristate <expr> [if <expr>]
557 elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) {
558 $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/;
559 my ( $orgtype, $default ) = ( $1, $2 );
560 ( my $type = $orgtype ) =~ s/def_//;
561 handle_type( $type, $inside_config, $filename, $line_no );
562 handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no );
563 }
564
565 #prompt <prompt> [if <expr>]
566 elsif ( $line =~ /^\s*prompt/ ) {
567 $line =~ /^\s*prompt\s+(.+)/;
568 handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no );
569 }
570
571 # default <expr> [if <expr>]
572 elsif ( $line =~ /^\s*default/ ) {
573 $line =~ /^\s*default\s+(.*)/;
574 my $default = $1;
575 handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no );
576 }
577
578 # depends on <expr>
579 elsif ( $line =~ /^\s*depends\s+on/ ) {
580 $line =~ /^\s*depends\s+on\s+(.*)$/;
581 my $expr = $1;
582 handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no );
583 handle_expressions( $expr, $inside_config, $filename, $line_no );
584 }
585
586 # comment <prompt>
587 elsif ( $line =~ /^\s*comment/ ) {
588 $inside_config = "";
589 }
590
591 # choice [symbol]
592 elsif ( $line =~ /^\s*choice/ ) {
593 if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) {
594 my $symbol = $1;
595 add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if );
596 handle_type( "bool", $symbol, $filename, $line_no );
597 }
598 $inside_config = "";
599 $inside_choice = "$filename $line_no";
600 $configs_inside_choice = 0;
Martin Roth08cf90f2016-01-25 19:54:16 -0700601
602 # Kconfig verifies that choice blocks have a prompt
Martin Rothbcaaad12015-10-18 11:16:25 -0600603 }
604
605 # endchoice
606 elsif ( $line =~ /^\s*endchoice/ ) {
607 $inside_config = "";
608 if ( !$inside_choice ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700609 show_error("'endchoice' keyword not within a choice block at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600610 }
611
612 $inside_choice = "";
613 if ( $configs_inside_choice == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700614 show_error("choice block has no symbols at $filename:$line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600615 }
616 $configs_inside_choice = 0;
617 }
618
619 # [optional]
620 elsif ( $line =~ /^\s*optional/ ) {
621 if ($inside_config) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700622 show_error( "Keyword 'optional' appears inside config for '$inside_config'"
623 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600624 }
625 if ( !$inside_choice ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -0700626 show_error( "Keyword 'optional' appears outside of a choice block"
627 . " at $filename:$line_no. This is not valid." );
Martin Rothbcaaad12015-10-18 11:16:25 -0600628 }
629 }
630
631 # mainmenu <prompt>
632 elsif ( $line =~ /^\s*mainmenu/ ) {
633 $inside_config = "";
Martin Roth08cf90f2016-01-25 19:54:16 -0700634
635 # Kconfig alread checks for multiple 'mainmenu' entries and mainmenu entries with no prompt
636 # Possible check: look for 'mainmenu ""'
637 # Possible check: verify that a mainmenu has been specified
Martin Rothbcaaad12015-10-18 11:16:25 -0600638 }
639
640 # menu <prompt>
641 elsif ( $line =~ /^\s*menu/ ) {
642 $line =~ /^\s*menu\s+(.*)/;
643 my $menu = $1;
644 if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) {
645 $menu = $1;
646 }
647
648 $inside_config = "";
649 $inside_choice = "";
650 push( @inside_menu, $menu );
651 }
652
653 # endmenu
654 elsif ( $line =~ /^\s*endmenu/ ) {
655 $inside_config = "";
656 $inside_choice = "";
657 pop @inside_menu;
658 }
659
660 # "if" <expr>
661 elsif ( $line =~ /^\s*if/ ) {
662 $inside_config = "";
663 $line =~ /^\s*if\s+(.*)$/;
664 my $expr = $1;
665 push( @inside_if, $expr );
666 handle_expressions( $expr, $inside_config, $filename, $line_no );
Martin Roth0e6c0e12016-03-02 12:16:13 -0700667 $fileinfo{$filename}{iflevel}++;
Martin Rothbcaaad12015-10-18 11:16:25 -0600668 }
669
670 # endif
671 elsif ( $line =~ /^\s*endif/ ) {
672 $inside_config = "";
673 pop(@inside_if);
Martin Roth0e6c0e12016-03-02 12:16:13 -0700674 $fileinfo{$filename}{iflevel}--;
Martin Rothbcaaad12015-10-18 11:16:25 -0600675 }
676
677 #range <symbol> <symbol> [if <expr>]
678 elsif ( $line =~ /^\s*range/ ) {
679 $line =~ /^\s*range\s+(\S+)\s+(.*)$/;
680 handle_range( $1, $2, $inside_config, $filename, $line_no );
681 }
682
683 # select <symbol> [if <expr>]
684 elsif ( $line =~ /^\s*select/ ) {
685 unless ($inside_config) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700686 show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600687 }
688
689 if ( $line =~ /^\s*select\s+(.*)$/ ) {
690 $line = $1;
691 my $expression;
692 ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no );
693 if ($line) {
Martin Rothb7c39b22016-01-14 09:04:53 -0700694 add_referenced_symbol( $line, $filename, $line_no, 'select' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600695 }
696 }
697 }
698
699 # source <prompt>
700 elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) {
701 my @newfile = load_kconfig_file( $1, $filename, $line_no, 0, $filename, $line_no );
702 unshift( @config_to_parse, @newfile );
703 $parseline[0]{text} = "# '$line'\n";
704 }
705 elsif (
706 ( $line =~ /^\s*#/ ) || #comments
707 ( $line =~ /^\s*$/ ) #blank lines
708 )
709 {
710 # do nothing
711 }
712 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700713 show_error("$line ($filename:$line_no unrecognized)");
Martin Rothbcaaad12015-10-18 11:16:25 -0600714 }
715
Martin Roth819e6722016-01-25 16:38:05 -0700716 if ( defined $inside_menu[0] ) {
717 $parseline[0]{menus} = "";
718 }
719 else {
720 $parseline[0]{menus} = "top";
721 }
722
723 my $i = 0;
724 while ( defined $inside_menu[$i] ) {
725 $parseline[0]{menus} .= "$inside_menu[$i]";
726 $i++;
727 if ( defined $inside_menu[$i] ) {
728 $parseline[0]{menus} .= "->";
729 }
730 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600731 push @wholeconfig, @parseline;
732 }
Martin Roth0e6c0e12016-03-02 12:16:13 -0700733
734 foreach my $file ( keys %fileinfo ) {
735 if ( $fileinfo{$file}{iflevel} > 0 ) {
736 show_error("$file has $fileinfo{$file}{iflevel} more 'if' statement(s) than 'endif' statements.");
737 }
738 elsif ( $fileinfo{$file}{iflevel} < 0 ) {
739 show_error(
740 "$file has " . ( $fileinfo{$file}{iflevel} * -1 ) . " more 'endif' statement(s) than 'if' statements." );
741 }
742 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600743}
744
745#-------------------------------------------------------------------------------
746#-------------------------------------------------------------------------------
747sub handle_depends {
748 my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_;
749
750 if ($inside_config) {
751 my $sym_num = $symbols{$inside_config}{count};
752 if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) {
753 $symbols{$inside_config}{$sym_num}{max_dependency}++;
754 }
755 else {
756 $symbols{$inside_config}{$sym_num}{max_dependency} = 0;
757 }
758
759 my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency};
760 $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr;
761 }
762}
763
764#-------------------------------------------------------------------------------
765#-------------------------------------------------------------------------------
766sub add_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700767 my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600768 my @inside_if = @{$ifref};
769
770 #initialize the symbol or increment the use count.
Martin Roth1b44f7e2016-01-11 14:31:38 -0700771 if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) {
Martin Rothbcaaad12015-10-18 11:16:25 -0600772 $symbols{$symbol}{count} = 0;
Martin Rothb7c39b22016-01-14 09:04:53 -0700773 if ($inside_choice) {
774 $symbols{$symbol}{choice} = 1;
775 }
776 else {
777 $symbols{$symbol}{choice} = 0;
778 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600779 }
780 else {
781 $symbols{$symbol}{count}++;
Martin Rothb7c39b22016-01-14 09:04:53 -0700782
783 if ( $inside_choice && !$symbols{$symbol}{choice} ) {
784 show_error( "$symbol entry at $filename:$line_no has already been created inside a choice block "
785 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
786 }
787 elsif ( !$inside_choice && $symbols{$symbol}{choice} ) {
788 show_error( "$symbol entry at $filename:$line_no has already been created outside a choice block "
789 . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." );
790 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600791 }
792
793 # add the location of this instance
794 my $symcount = $symbols{$symbol}{count};
795 $symbols{$symbol}{$symcount}{file} = $filename;
796 $symbols{$symbol}{$symcount}{line_no} = $line_no;
797
798 #Add the menu structure
799 if ( defined @$menu_array_ref[0] ) {
800 $symbols{$symbol}{$symcount}{menu} = $menu_array_ref;
801 }
802
803 #Add any 'if' statements that the symbol is inside as dependencies
804 if (@inside_if) {
805 my $dep_num = 0;
806 for my $dependency (@inside_if) {
807 $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency;
808 $symbols{$symbol}{$symcount}{max_dependency} = $dep_num;
809 $dep_num++;
810 }
811 }
812}
813
814#-------------------------------------------------------------------------------
815# handle range
816#-------------------------------------------------------------------------------
817sub handle_range {
818 my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_;
819
820 my $expression;
821 ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no );
822
823 $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
824 my $checkrange1 = $1;
825 $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/;
826 my $checkrange2 = $1;
827
828 if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700829 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 -0600830 }
831
832 if ($inside_config) {
833 if ( exists( $symbols{$inside_config}{range1} ) ) {
834 if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) {
Martin Rothd8080172015-11-26 19:12:44 -0700835 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700836 print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does";
Martin Roth1b44f7e2016-01-11 14:31:38 -0700837 print " not match the previously defined range $symbols{$inside_config}{range1}"
838 . " $symbols{$inside_config}{range2}";
Martin Roth7aa3cea2015-11-27 18:45:45 -0700839 print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -0600840 }
841 }
842 }
843 else {
844 $symbols{$inside_config}{range1} = $range1;
845 $symbols{$inside_config}{range2} = $range2;
846 $symbols{$inside_config}{range_file} = $filename;
847 $symbols{$inside_config}{range_line_no} = $line_no;
848 }
849 }
850 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700851 show_error("Range entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600852 }
853}
854
855#-------------------------------------------------------------------------------
856# handle_default
857#-------------------------------------------------------------------------------
858sub handle_default {
859 my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_;
860 my $expression;
861 ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no );
862
863 if ($inside_config) {
864 handle_expressions( $default, $inside_config, $filename, $line_no );
865 my $sym_num = $symbols{$inside_config}{count};
866
867 unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) {
868 $symbols{$inside_config}{$sym_num}{default_max} = 0;
869 }
870 my $default_max = $symbols{$inside_config}{$sym_num}{default_max};
871 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default;
872 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no;
873 if ($expression) {
874 $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression;
875 }
876 }
877 elsif ($inside_choice) {
878 handle_expressions( $default, $inside_config, $filename, $line_no );
879 }
880 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -0700881 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600882 }
883}
884
885#-------------------------------------------------------------------------------
886# handle_if_line
887#-------------------------------------------------------------------------------
888sub handle_if_line {
889 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
890
891 if ( $exprline !~ /if/ ) {
892 return ( $exprline, "" );
893 }
894
895 #remove any quotes that might have an 'if' in them
896 my $savequote;
897 if ( $exprline =~ /^\s*("[^"]+")/ ) {
898 $savequote = $1;
899 $exprline =~ s/^\s*("[^"]+")//;
900 }
901
902 my $expr = "";
903 if ( $exprline =~ /\s*if\s+(.*)$/ ) {
904 $expr = $1;
905 $exprline =~ s/\s*if\s+.*$//;
906
907 if ($expr) {
908 handle_expressions( $expr, $inside_config, $filename, $line_no );
909 }
910 }
911
912 if ($savequote) {
913 $exprline = $savequote;
914 }
915
916 return ( $exprline, $expr );
917}
918
919#-------------------------------------------------------------------------------
920# handle_expressions - log which symbols are being used
921#-------------------------------------------------------------------------------
922sub handle_expressions {
923 my ( $exprline, $inside_config, $filename, $line_no ) = @_;
924
925 return unless ($exprline);
926
927 #filter constant symbols first
Martin Roth1b44f7e2016-01-11 14:31:38 -0700928 if ( $exprline =~ /^\s*"?([yn])"?\s*$/ ) { # constant y/n
Martin Rothbcaaad12015-10-18 11:16:25 -0600929 return;
930 }
Martin Roth1b44f7e2016-01-11 14:31:38 -0700931 elsif ( $exprline =~ /^\s*"?((?:-)\d+)"?\s*$/ ) { # int values
Martin Rothbcaaad12015-10-18 11:16:25 -0600932 return;
933 }
934 elsif ( $exprline =~ /^\s*"?((?:-)?(?:0x)?\p{XDigit})+"?\s*$/ ) { # hex values
935 return;
936 }
937 elsif ( $exprline =~ /^\s*("[^"]*")\s*$/ ) { # String values
938 return;
939 }
940 elsif ( $exprline =~ /^\s*([A-Za-z0-9_]+)\s*$/ ) { # <symbol> (1)
Martin Rothb7c39b22016-01-14 09:04:53 -0700941 add_referenced_symbol( $1, $filename, $line_no, 'expression' );
Martin Rothbcaaad12015-10-18 11:16:25 -0600942 }
943 elsif ( $exprline =~ /^\s*!(.+)$/ ) { # '!' <expr> (5)
944
945 handle_expressions( $1, $inside_config, $filename, $line_no );
946 }
947 elsif ( $exprline =~ /^\s*\(([^)]+)\)\s*$/ ) { # '(' <expr> ')' (4)
948 handle_expressions( $1, $inside_config, $filename, $line_no );
949 }
950 elsif ( $exprline =~ /^\s*(.+)\s*!=\s*(.+)\s*$/ ) { # <symbol> '!=' <symbol> (3)
951 handle_expressions( $1, $inside_config, $filename, $line_no );
952 handle_expressions( $2, $inside_config, $filename, $line_no );
953 }
954 elsif ( $exprline =~ /^\s*(.+)\s*=\s*(.+)\s*$/ ) { # <symbol> '=' <symbol> (2)
955 handle_expressions( $1, $inside_config, $filename, $line_no );
956 handle_expressions( $2, $inside_config, $filename, $line_no );
957 }
958 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*&&\s*(.+)\s*$/ ) { # <expr> '&&' <expr> (6)
959 handle_expressions( $1, $inside_config, $filename, $line_no );
960 handle_expressions( $2, $inside_config, $filename, $line_no );
961 }
962 elsif ( $exprline =~ /^\s*([^(]+|\(.+\))\s*\|\|\s*(.+)\s*$/ ) { # <expr> '||' <expr> (7)
963 handle_expressions( $1, $inside_config, $filename, $line_no );
964 handle_expressions( $2, $inside_config, $filename, $line_no );
965 }
966
967 # work around kconfig spec violation for now - paths not in quotes
968 elsif ( $exprline =~ /^\s*([A-Za-z0-9_\-\/]+)\s*$/ ) { # <symbol> (1)
969 return;
970 }
971 else {
Martin Rothd8080172015-11-26 19:12:44 -0700972 show_error("Unrecognized expression '$exprline' in $filename line $line_no.");
Martin Rothbcaaad12015-10-18 11:16:25 -0600973 }
974
975 return;
976}
977
978#-------------------------------------------------------------------------------
979# add_referenced_symbol
980#-------------------------------------------------------------------------------
981sub add_referenced_symbol {
Martin Rothb7c39b22016-01-14 09:04:53 -0700982 my ( $symbol, $filename, $line_no, $reftype ) = @_;
Martin Rothbcaaad12015-10-18 11:16:25 -0600983 if ( exists $referenced_symbols{$symbol} ) {
984 $referenced_symbols{$symbol}{count}++;
985 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename;
986 $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no;
987 }
988 else {
989 $referenced_symbols{$symbol}{count} = 0;
990 $referenced_symbols{$symbol}{0}{filename} = $filename;
991 $referenced_symbols{$symbol}{0}{line_no} = $line_no;
992 }
Martin Rothb7c39b22016-01-14 09:04:53 -0700993
994 #mark the symbol as being selected, use referenced symbols for location
995 if ( $reftype eq 'select' ) {
996 $selected_symbols{$symbol}{ $referenced_symbols{$symbol}{count} } = 1;
997 }
Martin Rothbcaaad12015-10-18 11:16:25 -0600998}
999
1000#-------------------------------------------------------------------------------
1001# handle_help
1002#-------------------------------------------------------------------------------
1003{
1004 #create a non-global static variable by enclosing it and the subroutine
1005 my $help_whitespace = ""; #string to show length of the help whitespace
1006
1007 sub handle_help {
1008 my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1009
1010 if ($inside_help) {
1011
1012 #get the indentation level if it's not already set.
1013 if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) {
1014 $line =~ /^(\s+)/; #find the indentation level.
1015 $help_whitespace = $1;
1016 if ( !$help_whitespace ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001017 show_warning("$filename:$line_no - help text starts with no whitespace.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001018 return $inside_help;
Martin Rothbcaaad12015-10-18 11:16:25 -06001019 }
1020 }
1021
1022 #help ends at the first line which has a smaller indentation than the first line of the help text.
1023 if ( ( $line !~ /$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) {
1024 $inside_help = 0;
1025 $help_whitespace = "";
1026 }
1027 else { #if it's not ended, add the line to the helptext array for the symbol's instance
1028 if ($inside_config) {
1029 my $sym_num = $symbols{$inside_config}{count};
1030 if ($help_whitespace) { $line =~ s/^$help_whitespace//; }
1031 push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line );
1032 }
1033 }
1034 }
1035 elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) {
1036 $inside_help = $line_no;
1037 if ( ( !$inside_config ) && ( !$inside_choice ) ) {
Martin Rothd8080172015-11-26 19:12:44 -07001038 if ($show_note_output) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001039 print "# Note: $filename:$line_no help is not inside a config or choice block.\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001040 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001041 }
1042 elsif ($inside_config) {
1043 $help_whitespace = "";
1044 my $sym_num = $symbols{$inside_config}{count};
1045 $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no;
1046 $symbols{$inside_config}{$sym_num}{helptext} = ();
1047 }
1048 }
1049 return $inside_help;
1050 }
1051}
1052
1053#-------------------------------------------------------------------------------
1054# handle_type
1055#-------------------------------------------------------------------------------
1056sub handle_type {
1057 my ( $type, $inside_config, $filename, $line_no ) = @_;
1058
1059 my $expression;
1060 ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no );
1061
Martin Roth08ee1cf2016-01-25 16:39:32 -07001062 if ( $type =~ /tristate/ ) {
1063 show_warning("$filename:$line_no - tristate types are not used.");
1064 }
1065
Martin Rothbcaaad12015-10-18 11:16:25 -06001066 if ($inside_config) {
1067 if ( exists( $symbols{$inside_config}{type} ) ) {
1068 if ( $symbols{$inside_config}{type} !~ /$type/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001069 show_error( "Config '$inside_config' type entry $type"
1070 . " at $filename:$line_no does not match $symbols{$inside_config}{type}"
1071 . " defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001072 }
1073 }
1074 else {
1075 $symbols{$inside_config}{type} = $type;
1076 $symbols{$inside_config}{type_file} = $filename;
1077 $symbols{$inside_config}{type_line_no} = $line_no;
1078 }
1079 }
1080 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001081 show_error("Type entry at $filename:$line_no is not inside a config block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001082 }
1083}
1084
1085#-------------------------------------------------------------------------------
1086# handle_prompt
1087#-------------------------------------------------------------------------------
1088sub handle_prompt {
1089 my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_;
1090
1091 my $expression;
1092 ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no );
1093
1094 if ($inside_config) {
1095 if ( $prompt !~ /^\s*$/ ) {
1096 if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) {
1097 $prompt = $1;
1098 }
1099
Martin Roth08cf90f2016-01-25 19:54:16 -07001100 #display an error if there's a prompt at the top menu level
Martin Rothbcaaad12015-10-18 11:16:25 -06001101 if ( !defined @$menu_array_ref[0] ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001102 show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu"
1103 . " at $filename:$line_no." );
Martin Rothbcaaad12015-10-18 11:16:25 -06001104 }
1105
1106 my $sym_num = $symbols{$inside_config}{count};
Martin Rothb58d3492016-01-25 16:42:13 -07001107 if ( !exists $symbols{$inside_config}{$sym_num}{prompt_max} ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001108 $symbols{$inside_config}{$sym_num}{prompt_max} = 0;
1109 }
Martin Rothb58d3492016-01-25 16:42:13 -07001110 else {
1111 $symbols{$inside_config}{$sym_num}{prompt_max}++;
1112 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001113 my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max};
1114 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt;
1115 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no;
Martin Rothb58d3492016-01-25 16:42:13 -07001116
1117 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_menu} = @$menu_array_ref;
Martin Rothbcaaad12015-10-18 11:16:25 -06001118 if ($expression) {
1119 $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression;
1120 }
1121 }
1122 }
1123 elsif ($inside_choice) {
1124
1125 #do nothing
1126 }
1127 else {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001128 show_error("$name entry at $filename:$line_no is not inside a config or choice block.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001129 }
1130}
1131
1132#-------------------------------------------------------------------------------
1133# simple_line_checks - Does some basic checks on the current line, then cleans the line
1134# up for further processing.
1135#-------------------------------------------------------------------------------
1136sub simple_line_checks {
1137 my ( $line, $filename, $line_no ) = @_;
1138
1139 #check for spaces instead of tabs
1140 if ( $line =~ /^ +/ ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001141 show_error("$filename:$line_no starts with a space.");
Martin Rothbcaaad12015-10-18 11:16:25 -06001142 }
1143
1144 #verify a linefeed at the end of the line
1145 if ( $line !~ /.*\n/ ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001146 show_error( "$filename:$line_no does not end with linefeed."
1147 . " This can cause the line to not be recognized by the Kconfig parser.\n#($line)" );
Martin Rothbcaaad12015-10-18 11:16:25 -06001148 $line =~ s/\s*$//;
1149 }
1150 else {
1151 chop($line);
1152 }
1153
1154 return $line;
1155}
1156
1157#-------------------------------------------------------------------------------
1158# load_kconfig_file - Loads a single Kconfig file or expands * wildcard
1159#-------------------------------------------------------------------------------
1160sub load_kconfig_file {
1161 my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_;
1162 my @file_data;
1163 my @dir_file_data;
1164
1165 #recursively handle coreboot's new source glob operator
1166 if ( $input_file =~ /^(.*?)\/\*\/(.*)$/ ) {
1167 my $dir_prefix = $1;
1168 my $dir_suffix = $2;
1169 if ( -d "$dir_prefix" ) {
1170
1171 opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n";
1172 my @dirlist = sort { $a cmp $b } readdir(D);
1173 closedir(D);
1174
1175 while ( my $directory = shift @dirlist ) {
1176
1177 #ignore non-directory files
1178 if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ ) ) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001179 push @dir_file_data,
1180 load_kconfig_file( "$dir_prefix/$directory/$dir_suffix",
1181 $input_file, $loadline, 1, $loadfile, $loadline );
Martin Rothbcaaad12015-10-18 11:16:25 -06001182 }
1183 }
1184 }
1185
1186 #the directory should exist when using a glob
1187 else {
Martin Rothd8080172015-11-26 19:12:44 -07001188 show_warning("Could not find dir '$dir_prefix'");
Martin Rothbcaaad12015-10-18 11:16:25 -06001189 }
1190 }
1191
1192 #if the file exists, try to load it.
1193 elsif ( -e "$input_file" ) {
1194
Martin Rotha7d00272016-10-03 23:00:04 +02001195 #throw a warning if the file has already been loaded.
Martin Rothbcaaad12015-10-18 11:16:25 -06001196 if ( exists $loaded_files{$input_file} ) {
Martin Rotha7d00272016-10-03 23:00:04 +02001197 show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}");
Martin Rothbcaaad12015-10-18 11:16:25 -06001198 }
1199
1200 #load the file's contents and mark the file as loaded for checking later
1201 open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n";
1202 @file_data = <$HANDLE>;
1203 close $HANDLE;
1204 $loaded_files{$input_file} = "'$loadfile' line $loadline";
1205 }
1206
1207 # if the file isn't being loaded from a glob, it should exist.
1208 elsif ( $expanded == 0 ) {
Martin Roth7aa3cea2015-11-27 18:45:45 -07001209 show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline");
Martin Rothbcaaad12015-10-18 11:16:25 -06001210 }
1211
1212 my $line_in_file = 0;
1213 while ( my $line = shift @file_data ) {
1214
1215 #handle line continuation.
1216 my $continue_line = 0;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001217 while ( $line =~ /(.*)\s+\\$/ ) {
Martin Rothbcaaad12015-10-18 11:16:25 -06001218 my $text = $1;
1219
1220 # get rid of leading whitespace on all but the first and last lines
1221 $text =~ s/^\s*/ / if ($continue_line);
1222
1223 $dir_file_data[$line_in_file]{text} .= $text;
1224 $line = shift @file_data;
1225 $continue_line++;
1226
1227 #put the data into the continued lines (other than the first)
1228 $line =~ /^\s*(.*)\s*$/;
1229
Martin Roth1b44f7e2016-01-11 14:31:38 -07001230 $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n";
1231 $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file;
1232 $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1;
Martin Rothbcaaad12015-10-18 11:16:25 -06001233
1234 #get rid of multiple leading spaces for last line
1235 $line = " $1\n";
1236 }
1237
Martin Roth1b44f7e2016-01-11 14:31:38 -07001238 $dir_file_data[$line_in_file]{text} .= $line;
Martin Rothbcaaad12015-10-18 11:16:25 -06001239 $dir_file_data[$line_in_file]{filename} = $input_file;
1240 $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1;
1241
1242 $line_in_file++;
1243 if ($continue_line) {
1244 $line_in_file += $continue_line;
1245 }
1246 }
1247
1248 if ($topfile) {
1249 my %file_data;
Martin Roth1b44f7e2016-01-11 14:31:38 -07001250 $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001251 $file_data{filename} = $topfile;
1252 $file_data{file_line_no} = "($topline)";
Martin Roth1b44f7e2016-01-11 14:31:38 -07001253 unshift( @dir_file_data, \%file_data );
Martin Rothbcaaad12015-10-18 11:16:25 -06001254 }
1255
1256 return @dir_file_data;
1257}
1258
Martin Rothbcaaad12015-10-18 11:16:25 -06001259#-------------------------------------------------------------------------------
1260# print_wholeconfig - prints out the parsed Kconfig file
1261#-------------------------------------------------------------------------------
1262sub print_wholeconfig {
1263
1264 return unless $print_full_output;
1265
1266 for ( my $i = 0 ; $i < $#wholeconfig ; $i++ ) {
1267 my $line = $wholeconfig[$i];
1268 chop( $line->{text} );
1269
1270 #replace tabs with spaces for consistency
1271 $line->{text} =~ s/\t/ /g;
Martin Roth819e6722016-01-25 16:38:05 -07001272 printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text};
Martin Rothbcaaad12015-10-18 11:16:25 -06001273 }
1274}
1275
1276#-------------------------------------------------------------------------------
1277# check_if_file_referenced - checks for kconfig files that are not being parsed
1278#-------------------------------------------------------------------------------
1279sub check_if_file_referenced {
1280 my $filename = $File::Find::name;
Martin Rothab2d7772016-01-25 16:45:14 -07001281 if ( ( $filename =~ /Kconfig/ )
1282 && ( !$filename =~ /\.orig$/ )
1283 && ( !$filename =~ /~$/ )
1284 && ( !exists $loaded_files{$filename} ) )
1285 {
Martin Rothd8080172015-11-26 19:12:44 -07001286 show_warning("'$filename' is never referenced");
Martin Rothbcaaad12015-10-18 11:16:25 -06001287 }
1288}
1289
1290#-------------------------------------------------------------------------------
1291# check_arguments parse the command line arguments
1292#-------------------------------------------------------------------------------
1293sub check_arguments {
1294 my $show_usage = 0;
1295 GetOptions(
1296 'help|?' => sub { usage() },
Martin Rothd8080172015-11-26 19:12:44 -07001297 'e|errors_off' => \$suppress_error_output,
1298 'n|notes' => \$show_note_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001299 'o|output=s' => \$output_file,
1300 'p|print' => \$print_full_output,
Martin Rothd8080172015-11-26 19:12:44 -07001301 'w|warnings_off' => \$suppress_warning_output,
Martin Rothbcaaad12015-10-18 11:16:25 -06001302 'path=s' => \$top_dir,
1303 'c|config=s' => \$config_file,
1304 'G|no_git_grep' => \$dont_use_git_grep,
1305 );
Martin Rothd8080172015-11-26 19:12:44 -07001306
1307 if ($suppress_error_output) {
1308 $suppress_warning_output = 1;
1309 }
1310 if ($suppress_warning_output) {
Martin Roth1b44f7e2016-01-11 14:31:38 -07001311 $show_note_output = 0;
Martin Rothd8080172015-11-26 19:12:44 -07001312 }
Martin Rothbcaaad12015-10-18 11:16:25 -06001313}
1314
1315#-------------------------------------------------------------------------------
1316# usage - Print the arguments for the user
1317#-------------------------------------------------------------------------------
1318sub usage {
1319 print "kconfig_lint <options>\n";
1320 print " -o|--output=file Set output filename\n";
1321 print " -p|--print Print full output\n";
Martin Rothd8080172015-11-26 19:12:44 -07001322 print " -e|--errors_off Don't print warnings or errors\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001323 print " -w|--warnings_off Don't print warnings\n";
Martin Rothd8080172015-11-26 19:12:44 -07001324 print " -n|--notes Show minor notes\n";
Martin Rothbcaaad12015-10-18 11:16:25 -06001325 print " --path=dir Path to top level kconfig\n";
1326 print " -c|--config=file Filename of config file to load\n";
1327 print " -G|--no_git_grep Use standard grep tools instead of git grep\n";
1328
1329 exit(0);
1330}
1331
13321;