Yegor Timoshenko | c2e4941 | 2018-10-07 01:58:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 2 | # |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 5 | # perltidy -l=123 |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 6 | |
| 7 | package kconfig_lint; |
| 8 | |
| 9 | use strict; |
| 10 | use warnings; |
| 11 | use English qw( -no_match_vars ); |
| 12 | use File::Find; |
| 13 | use Getopt::Long; |
| 14 | use Getopt::Std; |
| 15 | |
Martin Roth | abf7d4d | 2016-02-19 10:24:25 -0700 | [diff] [blame] | 16 | # If taint mode is enabled, Untaint the path - git and grep must be in /bin, /usr/bin or /usr/local/bin |
| 17 | if ( ${^TAINT} ) { |
| 18 | $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin'; |
| 19 | delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV' }; |
| 20 | } |
| 21 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 22 | my $suppress_error_output = 0; # flag to prevent error text |
| 23 | my $suppress_warning_output = 0; # flag to prevent warning text |
| 24 | my $show_note_output = 0; # flag to show minor notes text |
| 25 | my $print_full_output = 0; # flag to print wholeconfig output |
| 26 | my $output_file = "-"; # filename of output - set stdout by default |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 27 | my $dont_use_git_grep = 0; |
Martin Roth | 873ebf2 | 2023-08-10 10:00:51 -0600 | [diff] [blame] | 28 | my $include_site_local = 0; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 29 | |
Martin Roth | abf7d4d | 2016-02-19 10:24:25 -0700 | [diff] [blame] | 30 | # Globals |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 31 | my $top_dir = "."; # Directory where Kconfig is run |
| 32 | my $root_dir = "src"; # Directory of the top level Kconfig file |
| 33 | my $errors_found = 0; # count of errors |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 34 | my $warnings_found = 0; |
Martin Roth | 63ea493 | 2016-01-25 16:08:27 -0700 | [diff] [blame] | 35 | my $exclude_dirs_and_files = |
Martin Roth | ab273d3 | 2016-11-23 17:28:00 -0700 | [diff] [blame] | 36 | '^build/\|^coreboot-builds/\|^configs/\|^util/\|^\.git/\|^payloads\|^Documentation\|^3rdparty' |
Martin Roth | 63ea493 | 2016-01-25 16:08:27 -0700 | [diff] [blame] | 37 | . '\|' . # directories to exclude when searching for used symbols |
Martin Roth | b357e53 | 2022-11-22 14:47:12 -0700 | [diff] [blame] | 38 | '\.config\|\.txt$\|\.tex$\|\.tags\|/kconfig.h\|\.fmd'; #files to exclude when looking for symbols |
Martin Roth | 3543893 | 2024-01-18 16:33:55 -0700 | [diff] [blame^] | 39 | my $payload_files_to_check='payloads/Makefile.mk payloads/external/Makefile.mk'; |
Martin Roth | 08cf90f | 2016-01-25 19:54:16 -0700 | [diff] [blame] | 40 | my $config_file = ""; # name of config file to load symbol values from. |
| 41 | my @wholeconfig; # document the entire kconfig structure |
| 42 | my %loaded_files; # list of each Kconfig file loaded |
| 43 | my %symbols; # main structure of all symbols declared |
| 44 | my %referenced_symbols; # list of symbols referenced by expressions or select statements |
| 45 | my %used_symbols; # structure of symbols used in the tree, and where they're found |
| 46 | my @collected_symbols; # |
| 47 | my %selected_symbols; # list of symbols that are enabled by a select statement |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 48 | |
Elyes Haouas | 336fd64 | 2024-01-12 08:41:54 +0100 | [diff] [blame] | 49 | my $exclude_unused = '_SPECIFIC_OPTIONS'; |
Martin Roth | 6bfbf1c | 2016-01-25 16:12:49 -0700 | [diff] [blame] | 50 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 51 | Main(); |
| 52 | |
| 53 | #------------------------------------------------------------------------------- |
| 54 | # Main |
| 55 | # |
| 56 | # Start by loading and parsing the top level Kconfig, this pulls in the other |
| 57 | # files. Parsing the tree creates several arrays and hashes that can be used |
| 58 | # to check for errors |
| 59 | #------------------------------------------------------------------------------- |
| 60 | sub Main { |
| 61 | |
Solomon Alan-Dei | a6e60f0 | 2022-08-11 11:13:28 -0600 | [diff] [blame] | 62 | check_arguments(); |
| 63 | if ( !($dont_use_git_grep || `git rev-parse --is-inside-work-tree`) ) { |
| 64 | $dont_use_git_grep = 1; |
| 65 | print STDERR "\nGit grep unavailable, falling back to regular grep...\n"; |
| 66 | } |
Martin Roth | 873ebf2 | 2023-08-10 10:00:51 -0600 | [diff] [blame] | 67 | if ( !$include_site_local) { |
| 68 | $exclude_dirs_and_files = "^site-local\|" . $exclude_dirs_and_files; |
| 69 | } |
Solomon Alan-Dei | a6e60f0 | 2022-08-11 11:13:28 -0600 | [diff] [blame] | 70 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 71 | open( STDOUT, "> $output_file" ) or die "Can't open $output_file for output: $!\n"; |
| 72 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 73 | if ( defined $top_dir ) { |
| 74 | chdir $top_dir or die "Error: can't cd to $top_dir\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 77 | die "Error: $top_dir/$root_dir does not exist.\n" unless ( -d $root_dir ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 78 | |
| 79 | #load the Kconfig tree, checking what we can and building up all the hash tables |
| 80 | build_and_parse_kconfig_tree("$root_dir/Kconfig"); |
| 81 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 82 | load_config($config_file) if ($config_file); |
| 83 | |
Martin Roth | 08705f1 | 2016-11-09 14:27:00 -0700 | [diff] [blame] | 84 | check_type(); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 85 | check_defaults(); |
| 86 | check_referenced_symbols(); |
| 87 | |
| 88 | collect_used_symbols(); |
| 89 | check_used_symbols(); |
| 90 | check_for_ifdef(); |
| 91 | check_for_def(); |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 92 | check_config_macro(); |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 93 | check_selected_symbols(); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 94 | |
Martin Roth | abf7d4d | 2016-02-19 10:24:25 -0700 | [diff] [blame] | 95 | # Run checks based on the data that was found |
| 96 | if ( ( !$suppress_warning_output ) && ( ${^TAINT} == 0 ) ) { |
| 97 | |
| 98 | # The find function is tainted - only run it if taint checking |
| 99 | # is disabled and warnings are enabled. |
| 100 | find( \&check_if_file_referenced, $root_dir ); |
| 101 | } |
| 102 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 103 | print_wholeconfig(); |
| 104 | |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 105 | if ($errors_found) { |
| 106 | print "# $errors_found errors"; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 107 | if ($warnings_found) { |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 108 | print ", $warnings_found warnings"; |
| 109 | } |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 110 | print "\n"; |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 113 | exit( $errors_found + $warnings_found ); |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #------------------------------------------------------------------------------- |
| 117 | # Print and count errors |
| 118 | #------------------------------------------------------------------------------- |
| 119 | sub show_error { |
| 120 | my ($error_msg) = @_; |
| 121 | unless ($suppress_error_output) { |
| 122 | print "#!!!!! Error: $error_msg\n"; |
| 123 | $errors_found++; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #------------------------------------------------------------------------------- |
| 128 | # Print and count warnings |
| 129 | #------------------------------------------------------------------------------- |
| 130 | sub show_warning { |
| 131 | my ($warning_msg) = @_; |
| 132 | unless ($suppress_warning_output) { |
| 133 | print "#!!!!! Warning: $warning_msg\n"; |
| 134 | $warnings_found++; |
| 135 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | #------------------------------------------------------------------------------- |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 139 | # check selected symbols for validity |
| 140 | # they must be bools |
| 141 | # they cannot select symbols created in 'choice' blocks |
| 142 | #------------------------------------------------------------------------------- |
| 143 | sub check_selected_symbols { |
| 144 | |
| 145 | #loop through symbols found in expressions and used by 'select' keywords |
| 146 | foreach my $symbol ( sort ( keys %selected_symbols ) ) { |
| 147 | my $type_failure = 0; |
| 148 | my $choice_failure = 0; |
| 149 | |
| 150 | #errors selecting symbols that don't exist are already printed, so we |
| 151 | #don't need to print them again here |
| 152 | |
| 153 | #make sure the selected symbols are bools |
| 154 | if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "bool" ) ) { |
| 155 | $type_failure = 1; |
| 156 | } |
| 157 | |
| 158 | #make sure we're not selecting choice symbols |
| 159 | if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{choice} ) ) { |
| 160 | $choice_failure = 1; |
| 161 | } |
| 162 | |
| 163 | #loop through each instance of the symbol to print out all of the failures |
| 164 | for ( my $i = 0 ; $i <= $referenced_symbols{$symbol}{count} ; $i++ ) { |
| 165 | next if ( !exists $selected_symbols{$symbol}{$i} ); |
| 166 | my $file = $referenced_symbols{$symbol}{$i}{filename}; |
| 167 | my $lineno = $referenced_symbols{$symbol}{$i}{line_no}; |
| 168 | if ($type_failure) { |
| 169 | show_error( |
| 170 | "CONFIG_$symbol' selected at $file:$lineno." . " Selects only work on symbols of type bool." ); |
| 171 | } |
| 172 | if ($choice_failure) { |
| 173 | show_error( |
| 174 | "'CONFIG_$symbol' selected at $file:$lineno." . " Symbols created in a choice cannot be selected." ); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | #------------------------------------------------------------------------------- |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 181 | # check_for_ifdef - Look for instances of #ifdef CONFIG_[symbol_name] and |
| 182 | # #if defined(CONFIG_[symbol_name]). |
| 183 | # |
| 184 | # #ifdef symbol is valid for strings, but bool, hex, and INT are always defined. |
| 185 | # #if defined(symbol) && symbol is also a valid construct. |
| 186 | #------------------------------------------------------------------------------- |
| 187 | sub check_for_ifdef { |
| 188 | my @ifdef_symbols = @collected_symbols; |
| 189 | |
| 190 | #look for #ifdef SYMBOL |
| 191 | while ( my $line = shift @ifdef_symbols ) { |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 192 | if ( $line =~ /^([^:]+):(\d+):\s*#\s*ifn?def\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) { |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 193 | my $file = $1; |
| 194 | my $lineno = $2; |
| 195 | my $symbol = $3; |
| 196 | |
| 197 | if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 198 | show_error( "#ifdef 'CONFIG_$symbol' used at $file:$lineno." |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 199 | . " Symbols of type '$symbols{$symbol}{type}' are always defined." ); |
| 200 | } |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 201 | } elsif ( $line =~ /^([^:]+):(\d+):.+defined\s*\(?\s*CONFIG(?:_|\()(\w+)/ ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 202 | my $file = $1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 203 | my $lineno = $2; |
| 204 | my $symbol = $3; |
| 205 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 206 | if ( ( exists $symbols{$symbol} ) && ( $symbols{$symbol}{type} ne "string" ) ) { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 207 | show_error( "defined(CONFIG_$symbol) used at $file:$lineno." |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 208 | . " Symbols of type '$symbols{$symbol}{type}' are always defined." ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #------------------------------------------------------------------------------- |
| 215 | # check_for_def - Look for instances of #define CONFIG_[symbol_name] |
| 216 | # |
| 217 | # Symbols should not be redefined outside of Kconfig, and #defines should not |
| 218 | # look like symbols |
| 219 | #------------------------------------------------------------------------------- |
| 220 | sub check_for_def { |
| 221 | my @def_symbols = @collected_symbols; |
| 222 | |
| 223 | #look for #ifdef SYMBOL |
| 224 | while ( my $line = shift @def_symbols ) { |
| 225 | if ( $line =~ /^([^:]+):(\d+):\s*#\s*define\s+CONFIG_(\w+)/ ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 226 | my $file = $1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 227 | my $lineno = $2; |
| 228 | my $symbol = $3; |
| 229 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 230 | if ( ( exists $symbols{$symbol} ) ) { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 231 | show_error("#define of symbol 'CONFIG_$symbol' used at $file:$lineno."); |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 232 | } |
| 233 | else { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 234 | show_error( "#define 'CONFIG_$symbol' used at $file:$lineno." |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 235 | . " Other #defines should not look like Kconfig symbols." ); |
| 236 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | #------------------------------------------------------------------------------- |
Martin Roth | 08705f1 | 2016-11-09 14:27:00 -0700 | [diff] [blame] | 242 | # check_type - Make sure that all symbols have a type defined. |
| 243 | # |
| 244 | # Conflicting types are found when parsing the Kconfig tree. |
| 245 | #------------------------------------------------------------------------------- |
| 246 | sub check_type { |
| 247 | |
| 248 | # loop through each defined symbol |
| 249 | foreach my $sym ( sort ( keys %symbols ) ) { |
| 250 | |
| 251 | # Make sure there's a type set for the symbol |
| 252 | if (!defined $symbols{$sym}{type}) { |
| 253 | |
| 254 | #loop through each instance of that symbol |
| 255 | for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) { |
| 256 | |
| 257 | my $filename = $symbols{$sym}{$sym_num}{file}; |
| 258 | my $line_no = $symbols{$sym}{$sym_num}{line_no}; |
| 259 | |
| 260 | show_error("No type defined for symbol $sym defined at $filename:$line_no."); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #------------------------------------------------------------------------------- |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 267 | # check_config_macro - The CONFIG() macro is only valid for symbols of type |
| 268 | # bool. It would probably work on type hex or int if the value was 0 or 1, |
| 269 | # but this seems like a bad plan. Using it on strings is dead out. |
| 270 | # |
| 271 | # The IS_ENABLED() macro is forbidden in coreboot now. Though, as long as |
| 272 | # we keep its definition in libpayload for compatibility, we have to check |
| 273 | # that it doesn't sneak back in. |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 274 | #------------------------------------------------------------------------------- |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 275 | sub check_config_macro { |
Martin Roth | b6acc30 | 2015-11-27 18:51:19 -0700 | [diff] [blame] | 276 | my @is_enabled_symbols = @collected_symbols; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 277 | |
| 278 | #sort through symbols found by grep and store them in a hash for easy access |
| 279 | while ( my $line = shift @is_enabled_symbols ) { |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 280 | if ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG\(.*)/ ) { |
| 281 | my $file = $1; |
| 282 | my $lineno = $2; |
| 283 | $line = $3; |
| 284 | while ( $line =~ /(.*)\bCONFIG\(([^)]*)\)(.*)/ ) { |
| 285 | my $symbol = $2; |
| 286 | $line = $1 . $3; |
| 287 | |
| 288 | #make sure that the type is bool |
| 289 | if ( exists $symbols{$symbol} ) { |
| 290 | if ( $symbols{$symbol}{type} ne "bool" ) { |
| 291 | show_error( "CONFIG($symbol) used at $file:$lineno." |
| 292 | . " CONFIG() is only valid for type 'bool', not '$symbols{$symbol}{type}'." ); |
| 293 | } |
Martin Roth | c7139f9 | 2023-10-27 11:03:48 -0600 | [diff] [blame] | 294 | } elsif ($symbol =~ /\S+##\S+/ ) { |
| 295 | show_warning( "C preprocessor macro CONFIG_$symbol found at $file:$lineno." ); |
| 296 | } else { |
Julius Werner | f028604 | 2019-04-09 15:57:07 -0700 | [diff] [blame] | 297 | show_error("CONFIG() used on unknown value ($symbol) at $file:$lineno."); |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } elsif ( $line =~ /^([^:]+):(\d+):(.+IS_ENABLED.*)/ ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 301 | my $file = $1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 302 | my $lineno = $2; |
Martin Roth | b6acc30 | 2015-11-27 18:51:19 -0700 | [diff] [blame] | 303 | $line = $3; |
Martin Roth | 572a856 | 2016-01-25 16:14:09 -0700 | [diff] [blame] | 304 | if ( ( $line !~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) && ( $line !~ /(\/[\*\/])(.*)IS_ENABLED/ ) ) { |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 305 | show_error("# uninterpreted IS_ENABLED at $file:$lineno: $line"); |
Martin Roth | b6acc30 | 2015-11-27 18:51:19 -0700 | [diff] [blame] | 306 | next; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 307 | } |
Martin Roth | b6acc30 | 2015-11-27 18:51:19 -0700 | [diff] [blame] | 308 | while ( $line =~ /(.*)IS_ENABLED\s*\(\s*CONFIG_(\w+)(.*)/ ) { |
| 309 | my $symbol = $2; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 310 | $line = $1 . $3; |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 311 | show_error("IS_ENABLED(CONFIG_$symbol) at $file:$lineno is deprecated. Use CONFIG($symbol) instead."); |
Martin Roth | b6acc30 | 2015-11-27 18:51:19 -0700 | [diff] [blame] | 312 | } |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 313 | } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?.*?$/ ) { |
| 314 | my $file = $1; |
| 315 | my $lineno = $2; |
| 316 | my $symbol = $3; |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 317 | # If the type is bool, give a warning that CONFIG() should be used |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 318 | if ( exists $symbols{$symbol} ) { |
| 319 | if ( $symbols{$symbol}{type} eq "bool" ) { |
| 320 | show_error( "#if CONFIG_$symbol used at $file:$lineno." |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 321 | . " CONFIG($symbol) should be used for type 'bool'" ); |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 322 | } |
| 323 | } |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 324 | } elsif ( $line =~ /^([^:]+):(\d+):\s*#\s*(?:el)?if.*(?:&&|\|\|)\s+!?\s*\(?\s*CONFIG_(\w+)\)?(\s*==\s*1)?$/ ) { |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 325 | my $file = $1; |
| 326 | my $lineno = $2; |
| 327 | my $symbol = $3; |
Nico Huber | ec01759 | 2019-04-06 16:09:46 +0200 | [diff] [blame] | 328 | # If the type is bool, give a warning that CONFIG() should be used |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 329 | if ( exists $symbols{$symbol} ) { |
| 330 | if ( $symbols{$symbol}{type} eq "bool" ) { |
| 331 | show_error( "#if CONFIG_$symbol used at $file:$lineno." |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 332 | . " CONFIG($symbol) should be used for type 'bool'" ); |
Martin Roth | 5b88301 | 2017-06-25 20:27:36 -0600 | [diff] [blame] | 333 | } |
| 334 | } |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 335 | } elsif ( $line =~ /^([^:]+):(\d+):(.+\bCONFIG_.+)/ ) { |
| 336 | my $file = $1; |
| 337 | my $lineno = $2; |
| 338 | $line = $3; |
| 339 | if ( $file =~ /.*\.(c|h|asl|ld)/ ) { |
| 340 | while ( $line =~ /(.*)\bCONFIG_(\w+)(.*)/ && $1 !~ /\/\/|\/\*/ ) { |
| 341 | my $symbol = $2; |
| 342 | $line = $1 . $3; |
| 343 | if ( exists $symbols{$symbol} ) { |
| 344 | if ( $symbols{$symbol}{type} eq "bool" ) { |
Martin Roth | a020903 | 2020-07-24 12:42:59 -0600 | [diff] [blame] | 345 | show_error( "Naked reference to CONFIG_$symbol used at $file:$lineno." |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 346 | . " A 'bool' Kconfig should always be accessed through CONFIG($symbol)." ); |
| 347 | } |
| 348 | } else { |
| 349 | show_warning( "Unknown config option CONFIG_$symbol used at $file:$lineno." ); |
| 350 | } |
| 351 | } |
| 352 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | #------------------------------------------------------------------------------- |
| 358 | # check_defaults - Look for defaults that come after a default with no |
| 359 | # dependencies. |
| 360 | # |
| 361 | # TODO - check for defaults with the same dependencies |
| 362 | #------------------------------------------------------------------------------- |
| 363 | sub check_defaults { |
| 364 | |
| 365 | # loop through each defined symbol |
| 366 | foreach my $sym ( sort ( keys %symbols ) ) { |
| 367 | my $default_set = 0; |
| 368 | my $default_filename = ""; |
| 369 | my $default_line_no = ""; |
| 370 | |
| 371 | #loop through each instance of that symbol |
| 372 | for ( my $sym_num = 0 ; $sym_num <= $symbols{$sym}{count} ; $sym_num++ ) { |
| 373 | |
| 374 | #loop through any defaults for that instance of that symbol, if there are any |
| 375 | next unless ( exists $symbols{$sym}{$sym_num}{default_max} ); |
| 376 | for ( my $def_num = 0 ; $def_num <= $symbols{$sym}{$sym_num}{default_max} ; $def_num++ ) { |
| 377 | |
Martin Roth | fa95625 | 2016-09-30 15:51:32 -0600 | [diff] [blame] | 378 | my $filename = $symbols{$sym}{$sym_num}{file}; |
| 379 | my $line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no}; |
| 380 | |
Martin Roth | 08705f1 | 2016-11-09 14:27:00 -0700 | [diff] [blame] | 381 | # Make sure there's a type set for the symbol |
| 382 | next if (!defined $symbols{$sym}{type}); |
| 383 | |
Michael Niewöhner | 90fcffb | 2021-09-16 17:56:14 +0200 | [diff] [blame] | 384 | # Symbols created/used inside a choice must not have a default set. The default is set by the choice itself. |
| 385 | if ($symbols{$sym}{choice}) { |
| 386 | show_error("Defining a default for symbol '$sym' at $filename:$line_no, used inside choice at " |
Michael Niewöhner | 4396358 | 2021-10-17 17:48:50 +0200 | [diff] [blame] | 387 | . "$symbols{$sym}{choice}, is not allowed."); |
Michael Niewöhner | 90fcffb | 2021-09-16 17:56:14 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Martin Roth | fa95625 | 2016-09-30 15:51:32 -0600 | [diff] [blame] | 390 | # skip good defaults |
| 391 | if (! ((($symbols{$sym}{type} eq "hex") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^0x/)) || |
| 392 | (($symbols{$sym}{type} eq "int") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[-0-9]+$/)) || |
| 393 | (($symbols{$sym}{type} eq "string") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^".*"$/)) || |
| 394 | (($symbols{$sym}{type} eq "bool") && ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /^[yn]$/))) |
| 395 | ) { |
| 396 | |
| 397 | my ($checksym) = $symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /(\w+)/; |
| 398 | |
| 399 | if (! exists $symbols{$checksym}) { |
| 400 | |
| 401 | # verify the symbol type against the default value |
| 402 | if ($symbols{$sym}{type} eq "hex") { |
| 403 | show_error("non hex default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for hex symbol $sym at $filename:$line_no."); |
| 404 | } elsif ($symbols{$sym}{type} eq "int") { |
| 405 | show_error("non int default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for int symbol $sym at $filename:$line_no."); |
| 406 | } elsif ($symbols{$sym}{type} eq "string") { |
| 407 | # TODO: Remove special MAINBOARD_DIR check |
| 408 | if ($sym ne "MAINBOARD_DIR") { |
| 409 | show_error("no quotes around default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for string symbol $sym at $filename:$line_no."); |
| 410 | } |
| 411 | } elsif ($symbols{$sym}{type} eq "bool") { |
| 412 | if ($symbols{$sym}{$sym_num}{default}{$def_num}{default} =~ /[01YN]/) { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 413 | show_error("default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) for bool symbol $sym uses value other than y/n at $filename:$line_no."); |
Martin Roth | fa95625 | 2016-09-30 15:51:32 -0600 | [diff] [blame] | 414 | } else { |
| 415 | show_error("non bool default value ($symbols{$sym}{$sym_num}{default}{$def_num}{default}) used for bool symbol $sym at $filename:$line_no."); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 421 | #if a default is already set, display an error |
| 422 | if ($default_set) { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 423 | show_error( "Default for '$sym' referenced at $filename:$line_no will never be set" |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 424 | . " - overridden by default set at $default_filename:$default_line_no" ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 425 | } |
| 426 | else { |
| 427 | #if no default is set, see if this is a default with no dependencies |
| 428 | unless ( ( exists $symbols{$sym}{$sym_num}{default}{$def_num}{default_depends_on} ) |
| 429 | || ( exists $symbols{$sym}{$sym_num}{max_dependency} ) ) |
| 430 | { |
| 431 | $default_set = 1; |
| 432 | $default_filename = $symbols{$sym}{$sym_num}{file}; |
| 433 | $default_line_no = $symbols{$sym}{$sym_num}{default}{$def_num}{default_line_no}; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | #------------------------------------------------------------------------------- |
| 442 | # check_referenced_symbols - Make sure the symbols referenced by expressions and |
| 443 | # select statements are actually valid symbols. |
| 444 | #------------------------------------------------------------------------------- |
| 445 | sub check_referenced_symbols { |
| 446 | |
| 447 | #loop through symbols found in expressions and used by 'select' keywords |
| 448 | foreach my $key ( sort ( keys %referenced_symbols ) ) { |
| 449 | |
| 450 | #make sure the symbol was defined by a 'config' or 'choice' keyword |
| 451 | next if ( exists $symbols{$key} ); |
| 452 | |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 453 | #loop through each instance of the symbol to print out all of the invalid references |
| 454 | for ( my $i = 0 ; $i <= $referenced_symbols{$key}{count} ; $i++ ) { |
| 455 | my $filename = $referenced_symbols{$key}{$i}{filename}; |
| 456 | my $line_no = $referenced_symbols{$key}{$i}{line_no}; |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 457 | show_error("Undefined Symbol '$key' used at $filename:$line_no."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | #------------------------------------------------------------------------------- |
| 463 | #------------------------------------------------------------------------------- |
| 464 | sub collect_used_symbols { |
| 465 | # find all references to CONFIG_ statements in the tree |
| 466 | |
| 467 | if ($dont_use_git_grep) { |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 468 | @collected_symbols = `grep -Irn -- "CONFIG\\(_\\|(\\)" | grep -v '$exclude_dirs_and_files'; grep -In -- "CONFIG\\(_\\|(\\)" $payload_files_to_check`; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 469 | } |
| 470 | else { |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 471 | @collected_symbols = `git grep -In -- "CONFIG\\(_\\|(\\)" | grep -v '$exclude_dirs_and_files'; git grep -In -- "CONFIG\\(_\\|(\\)" $payload_files_to_check`; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | my @used_symbols = @collected_symbols; |
| 475 | |
| 476 | #sort through symbols found by grep and store them in a hash for easy access |
| 477 | while ( my $line = shift @used_symbols ) { |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 478 | while ( $line =~ /[^A-Za-z0-9_]CONFIG(?:_|\()([A-Za-z0-9_]+)/g ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 479 | my $symbol = $1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 480 | my $filename = ""; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 481 | if ( $line =~ /^([^:]+):/ ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 482 | $filename = $1; |
| 483 | } |
| 484 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 485 | if ( exists $used_symbols{$symbol}{count} ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 486 | $used_symbols{$symbol}{count}++; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 487 | } |
| 488 | else { |
| 489 | $used_symbols{$symbol}{count} = 0; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 490 | } |
| 491 | $used_symbols{$symbol}{"num_$used_symbols{$symbol}{count}"} = $filename; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 492 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | #------------------------------------------------------------------------------- |
| 497 | # check_used_symbols - Checks to see whether or not the created symbols are |
| 498 | # actually used. |
| 499 | #------------------------------------------------------------------------------- |
| 500 | sub check_used_symbols { |
| 501 | # loop through all defined symbols and see if they're used anywhere |
| 502 | foreach my $key ( sort ( keys %symbols ) ) { |
| 503 | |
Martin Roth | 6bfbf1c | 2016-01-25 16:12:49 -0700 | [diff] [blame] | 504 | if ( $key =~ /$exclude_unused/ ) { |
| 505 | next; |
| 506 | } |
| 507 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 508 | #see if they're used internal to Kconfig |
| 509 | next if ( exists $referenced_symbols{$key} ); |
| 510 | |
| 511 | #see if they're used externally |
| 512 | next if exists $used_symbols{$key}; |
| 513 | |
| 514 | #loop through the definitions to print out all the places the symbol is defined. |
| 515 | for ( my $i = 0 ; $i <= $symbols{$key}{count} ; $i++ ) { |
| 516 | my $filename = $symbols{$key}{$i}{file}; |
| 517 | my $line_no = $symbols{$key}{$i}{line_no}; |
Martin Roth | a7d0027 | 2016-10-03 23:00:04 +0200 | [diff] [blame] | 518 | show_warning("Unused symbol '$key' referenced at $filename:$line_no."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | #------------------------------------------------------------------------------- |
| 524 | # build_and_parse_kconfig_tree |
| 525 | #------------------------------------------------------------------------------- |
| 526 | #load the initial file and start parsing it |
| 527 | sub build_and_parse_kconfig_tree { |
| 528 | my ($top_level_kconfig) = @_; |
| 529 | my @config_to_parse; |
| 530 | my @parseline; |
| 531 | my $inside_help = 0; # set to line number of 'help' keyword if this line is inside a help block |
| 532 | my @inside_if = (); # stack of if dependencies |
| 533 | my $inside_config = ""; # set to symbol name of the config section |
| 534 | my @inside_menu = (); # stack of menu names |
| 535 | my $inside_choice = ""; |
Patrick Georgi | cbc5b99 | 2018-11-23 15:55:56 +0100 | [diff] [blame] | 536 | my $choice_symbol = ""; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 537 | my $configs_inside_choice; |
Martin Roth | 0e6c0e1 | 2016-03-02 12:16:13 -0700 | [diff] [blame] | 538 | my %fileinfo; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 539 | |
| 540 | #start the tree off by loading the top level kconfig |
| 541 | @config_to_parse = load_kconfig_file( $top_level_kconfig, "", 0, 0, "", 0 ); |
| 542 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 543 | while ( ( @parseline = shift(@config_to_parse) ) && ( exists $parseline[0]{text} ) ) { |
| 544 | my $line = $parseline[0]{text}; |
| 545 | my $filename = $parseline[0]{filename}; |
| 546 | my $line_no = $parseline[0]{file_line_no}; |
| 547 | |
| 548 | #handle help - help text: "help" or "---help---" |
Martin Roth | 811d93a | 2017-04-06 11:06:00 -0600 | [diff] [blame] | 549 | my $lastline_was_help = $inside_help; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 550 | $inside_help = handle_help( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ); |
| 551 | $parseline[0]{inside_help} = $inside_help; |
| 552 | |
| 553 | #look for basic issues in the line, strip crlf |
| 554 | $line = simple_line_checks( $line, $filename, $line_no ); |
| 555 | |
| 556 | #strip comments |
| 557 | $line =~ s/\s*#.*$//; |
| 558 | |
| 559 | #don't parse any more if we're inside a help block |
| 560 | if ($inside_help) { |
| 561 | #do nothing |
| 562 | } |
| 563 | |
| 564 | #handle config |
Martin Roth | 811d93a | 2017-04-06 11:06:00 -0600 | [diff] [blame] | 565 | elsif ( $line =~ /^\s*config\s+/ ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 566 | $line =~ /^\s*config\s+([^"\s]+)\s*(?>#.*)?$/; |
| 567 | my $symbol = $1; |
| 568 | $inside_config = $symbol; |
| 569 | if ($inside_choice) { |
| 570 | $configs_inside_choice++; |
| 571 | } |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 572 | add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if, $inside_choice ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | #bool|hex|int|string|tristate <expr> [if <expr>] |
| 576 | elsif ( $line =~ /^\s*(bool|string|hex|int|tristate)/ ) { |
| 577 | $line =~ /^\s*(bool|string|hex|int|tristate)\s*(.*)/; |
| 578 | my ( $type, $prompt ) = ( $1, $2 ); |
| 579 | handle_type( $type, $inside_config, $filename, $line_no ); |
| 580 | handle_prompt( $prompt, $type, \@inside_menu, $inside_config, $inside_choice, $filename, $line_no ); |
| 581 | } |
| 582 | |
| 583 | # def_bool|def_tristate <expr> [if <expr>] |
| 584 | elsif ( $line =~ /^\s*(def_bool|def_tristate)/ ) { |
| 585 | $line =~ /^\s*(def_bool|def_tristate)\s+(.*)/; |
| 586 | my ( $orgtype, $default ) = ( $1, $2 ); |
| 587 | ( my $type = $orgtype ) =~ s/def_//; |
| 588 | handle_type( $type, $inside_config, $filename, $line_no ); |
| 589 | handle_default( $default, $orgtype, $inside_config, $inside_choice, $filename, $line_no ); |
| 590 | } |
| 591 | |
| 592 | #prompt <prompt> [if <expr>] |
| 593 | elsif ( $line =~ /^\s*prompt/ ) { |
| 594 | $line =~ /^\s*prompt\s+(.+)/; |
| 595 | handle_prompt( $1, "prompt", \@inside_menu, $inside_config, $inside_choice, $filename, $line_no ); |
| 596 | } |
| 597 | |
| 598 | # default <expr> [if <expr>] |
| 599 | elsif ( $line =~ /^\s*default/ ) { |
| 600 | $line =~ /^\s*default\s+(.*)/; |
| 601 | my $default = $1; |
| 602 | handle_default( $default, "default", $inside_config, $inside_choice, $filename, $line_no ); |
| 603 | } |
| 604 | |
| 605 | # depends on <expr> |
| 606 | elsif ( $line =~ /^\s*depends\s+on/ ) { |
| 607 | $line =~ /^\s*depends\s+on\s+(.*)$/; |
| 608 | my $expr = $1; |
| 609 | handle_depends( $expr, $inside_config, $inside_choice, $filename, $line_no ); |
| 610 | handle_expressions( $expr, $inside_config, $filename, $line_no ); |
| 611 | } |
| 612 | |
| 613 | # comment <prompt> |
| 614 | elsif ( $line =~ /^\s*comment/ ) { |
| 615 | $inside_config = ""; |
| 616 | } |
| 617 | |
| 618 | # choice [symbol] |
| 619 | elsif ( $line =~ /^\s*choice/ ) { |
| 620 | if ( $line =~ /^\s*choice\s*([A-Za-z0-9_]+)$/ ) { |
| 621 | my $symbol = $1; |
| 622 | add_symbol( $symbol, \@inside_menu, $filename, $line_no, \@inside_if ); |
| 623 | handle_type( "bool", $symbol, $filename, $line_no ); |
Patrick Georgi | cbc5b99 | 2018-11-23 15:55:56 +0100 | [diff] [blame] | 624 | $choice_symbol = $symbol; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 625 | } |
| 626 | $inside_config = ""; |
Michael Niewöhner | 70fb5cb | 2021-10-17 17:43:53 +0200 | [diff] [blame] | 627 | $inside_choice = "$filename:$line_no"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 628 | $configs_inside_choice = 0; |
Martin Roth | 08cf90f | 2016-01-25 19:54:16 -0700 | [diff] [blame] | 629 | |
| 630 | # Kconfig verifies that choice blocks have a prompt |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | # endchoice |
| 634 | elsif ( $line =~ /^\s*endchoice/ ) { |
| 635 | $inside_config = ""; |
| 636 | if ( !$inside_choice ) { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 637 | show_error("'endchoice' keyword not within a choice block at $filename:$line_no."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | $inside_choice = ""; |
Patrick Georgi | cbc5b99 | 2018-11-23 15:55:56 +0100 | [diff] [blame] | 641 | if (( $configs_inside_choice == 0 ) && |
| 642 | ( $choice_symbol eq "" )) { |
| 643 | show_error("unnamed choice block has no symbols at $filename:$line_no."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 644 | } |
| 645 | $configs_inside_choice = 0; |
Patrick Georgi | cbc5b99 | 2018-11-23 15:55:56 +0100 | [diff] [blame] | 646 | $choice_symbol=""; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | # [optional] |
| 650 | elsif ( $line =~ /^\s*optional/ ) { |
| 651 | if ($inside_config) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 652 | show_error( "Keyword 'optional' appears inside config for '$inside_config'" |
| 653 | . " at $filename:$line_no. This is not valid." ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 654 | } |
| 655 | if ( !$inside_choice ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 656 | show_error( "Keyword 'optional' appears outside of a choice block" |
| 657 | . " at $filename:$line_no. This is not valid." ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | # mainmenu <prompt> |
| 662 | elsif ( $line =~ /^\s*mainmenu/ ) { |
| 663 | $inside_config = ""; |
Martin Roth | 08cf90f | 2016-01-25 19:54:16 -0700 | [diff] [blame] | 664 | |
| 665 | # Kconfig alread checks for multiple 'mainmenu' entries and mainmenu entries with no prompt |
| 666 | # Possible check: look for 'mainmenu ""' |
| 667 | # Possible check: verify that a mainmenu has been specified |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | # menu <prompt> |
| 671 | elsif ( $line =~ /^\s*menu/ ) { |
| 672 | $line =~ /^\s*menu\s+(.*)/; |
| 673 | my $menu = $1; |
| 674 | if ( $menu =~ /^\s*"([^"]*)"\s*$/ ) { |
| 675 | $menu = $1; |
| 676 | } |
| 677 | |
| 678 | $inside_config = ""; |
| 679 | $inside_choice = ""; |
| 680 | push( @inside_menu, $menu ); |
| 681 | } |
| 682 | |
Patrick Georgi | fcc2950 | 2018-09-16 21:35:46 +0200 | [diff] [blame] | 683 | # visible if <expr> |
| 684 | elsif ( $line =~ /^\s*visible if.*$/ ) { |
Julius Werner | ef7a326 | 2019-03-05 16:57:52 -0800 | [diff] [blame] | 685 | # Must come directly after menu line (and on a separate line) |
| 686 | # but kconfig already checks for that. |
| 687 | # Ignore it. |
Patrick Georgi | fcc2950 | 2018-09-16 21:35:46 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 690 | # endmenu |
| 691 | elsif ( $line =~ /^\s*endmenu/ ) { |
| 692 | $inside_config = ""; |
| 693 | $inside_choice = ""; |
| 694 | pop @inside_menu; |
| 695 | } |
| 696 | |
| 697 | # "if" <expr> |
| 698 | elsif ( $line =~ /^\s*if/ ) { |
| 699 | $inside_config = ""; |
| 700 | $line =~ /^\s*if\s+(.*)$/; |
| 701 | my $expr = $1; |
| 702 | push( @inside_if, $expr ); |
| 703 | handle_expressions( $expr, $inside_config, $filename, $line_no ); |
Martin Roth | 0e6c0e1 | 2016-03-02 12:16:13 -0700 | [diff] [blame] | 704 | $fileinfo{$filename}{iflevel}++; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | # endif |
| 708 | elsif ( $line =~ /^\s*endif/ ) { |
| 709 | $inside_config = ""; |
| 710 | pop(@inside_if); |
Martin Roth | 0e6c0e1 | 2016-03-02 12:16:13 -0700 | [diff] [blame] | 711 | $fileinfo{$filename}{iflevel}--; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | #range <symbol> <symbol> [if <expr>] |
| 715 | elsif ( $line =~ /^\s*range/ ) { |
| 716 | $line =~ /^\s*range\s+(\S+)\s+(.*)$/; |
| 717 | handle_range( $1, $2, $inside_config, $filename, $line_no ); |
| 718 | } |
| 719 | |
| 720 | # select <symbol> [if <expr>] |
| 721 | elsif ( $line =~ /^\s*select/ ) { |
| 722 | unless ($inside_config) { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 723 | show_error("Keyword 'select' appears outside of config at $filename:$line_no. This is not valid."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if ( $line =~ /^\s*select\s+(.*)$/ ) { |
| 727 | $line = $1; |
| 728 | my $expression; |
| 729 | ( $line, $expression ) = handle_if_line( $line, $inside_config, $filename, $line_no ); |
| 730 | if ($line) { |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 731 | add_referenced_symbol( $line, $filename, $line_no, 'select' ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | # source <prompt> |
| 737 | elsif ( $line =~ /^\s*source\s+"?([^"\s]+)"?\s*(?>#.*)?$/ ) { |
Martin Roth | 873ebf2 | 2023-08-10 10:00:51 -0600 | [diff] [blame] | 738 | my $input_file = $1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 739 | $parseline[0]{text} = "# '$line'\n"; |
Martin Roth | 873ebf2 | 2023-08-10 10:00:51 -0600 | [diff] [blame] | 740 | if ( $line !~ "site-local" || $include_site_local ) { |
| 741 | my @newfile = load_kconfig_file( $input_file, $filename, $line_no, 0, $filename, $line_no ); |
| 742 | unshift( @config_to_parse, @newfile ); |
| 743 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 744 | } |
| 745 | elsif ( |
| 746 | ( $line =~ /^\s*#/ ) || #comments |
| 747 | ( $line =~ /^\s*$/ ) #blank lines |
| 748 | ) |
| 749 | { |
| 750 | # do nothing |
| 751 | } |
| 752 | else { |
Martin Roth | 811d93a | 2017-04-06 11:06:00 -0600 | [diff] [blame] | 753 | if ($lastline_was_help) { |
| 754 | show_error("The line \"$line\" ($filename:$line_no) wasn't recognized - supposed to be inside help?"); |
| 755 | } |
| 756 | else { |
| 757 | show_error("The line \"$line\" ($filename:$line_no) wasn't recognized"); |
| 758 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 759 | } |
| 760 | |
Martin Roth | 819e672 | 2016-01-25 16:38:05 -0700 | [diff] [blame] | 761 | if ( defined $inside_menu[0] ) { |
| 762 | $parseline[0]{menus} = ""; |
| 763 | } |
| 764 | else { |
| 765 | $parseline[0]{menus} = "top"; |
| 766 | } |
| 767 | |
| 768 | my $i = 0; |
| 769 | while ( defined $inside_menu[$i] ) { |
| 770 | $parseline[0]{menus} .= "$inside_menu[$i]"; |
| 771 | $i++; |
| 772 | if ( defined $inside_menu[$i] ) { |
| 773 | $parseline[0]{menus} .= "->"; |
| 774 | } |
| 775 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 776 | push @wholeconfig, @parseline; |
| 777 | } |
Martin Roth | 0e6c0e1 | 2016-03-02 12:16:13 -0700 | [diff] [blame] | 778 | |
| 779 | foreach my $file ( keys %fileinfo ) { |
| 780 | if ( $fileinfo{$file}{iflevel} > 0 ) { |
| 781 | show_error("$file has $fileinfo{$file}{iflevel} more 'if' statement(s) than 'endif' statements."); |
| 782 | } |
| 783 | elsif ( $fileinfo{$file}{iflevel} < 0 ) { |
| 784 | show_error( |
| 785 | "$file has " . ( $fileinfo{$file}{iflevel} * -1 ) . " more 'endif' statement(s) than 'if' statements." ); |
| 786 | } |
| 787 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | #------------------------------------------------------------------------------- |
| 791 | #------------------------------------------------------------------------------- |
| 792 | sub handle_depends { |
| 793 | my ( $expr, $inside_config, $inside_choice, $filename, $line_no ) = @_; |
| 794 | |
| 795 | if ($inside_config) { |
| 796 | my $sym_num = $symbols{$inside_config}{count}; |
| 797 | if ( exists $symbols{$inside_config}{$sym_num}{max_dependency} ) { |
| 798 | $symbols{$inside_config}{$sym_num}{max_dependency}++; |
| 799 | } |
| 800 | else { |
| 801 | $symbols{$inside_config}{$sym_num}{max_dependency} = 0; |
| 802 | } |
| 803 | |
| 804 | my $dep_num = $symbols{$inside_config}{$sym_num}{max_dependency}; |
| 805 | $symbols{$inside_config}{$sym_num}{dependency}{$dep_num} = $expr; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | #------------------------------------------------------------------------------- |
| 810 | #------------------------------------------------------------------------------- |
| 811 | sub add_symbol { |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 812 | my ( $symbol, $menu_array_ref, $filename, $line_no, $ifref, $inside_choice ) = @_; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 813 | my @inside_if = @{$ifref}; |
| 814 | |
| 815 | #initialize the symbol or increment the use count. |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 816 | if ( ( !exists $symbols{$symbol} ) || ( !exists $symbols{$symbol}{count} ) ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 817 | $symbols{$symbol}{count} = 0; |
Michael Niewöhner | 4396358 | 2021-10-17 17:48:50 +0200 | [diff] [blame] | 818 | # remember the location of the choice (or "") |
| 819 | $symbols{$symbol}{choice} = $inside_choice; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 820 | } |
| 821 | else { |
| 822 | $symbols{$symbol}{count}++; |
Nico Huber | 967730f | 2021-07-17 11:43:44 +0200 | [diff] [blame] | 823 | if ( $inside_choice && $symbols{$symbol}{choice} ) { |
Martin Roth | aa20647 | 2017-03-24 08:51:51 -0600 | [diff] [blame] | 824 | show_error( "$symbol entry at $filename:$line_no has already been created inside another choice block " |
| 825 | . "at $symbols{$symbol}{0}{file}:$symbols{$symbol}{0}{line_no}." ); |
| 826 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | # add the location of this instance |
| 830 | my $symcount = $symbols{$symbol}{count}; |
| 831 | $symbols{$symbol}{$symcount}{file} = $filename; |
| 832 | $symbols{$symbol}{$symcount}{line_no} = $line_no; |
| 833 | |
| 834 | #Add the menu structure |
| 835 | if ( defined @$menu_array_ref[0] ) { |
| 836 | $symbols{$symbol}{$symcount}{menu} = $menu_array_ref; |
| 837 | } |
| 838 | |
| 839 | #Add any 'if' statements that the symbol is inside as dependencies |
| 840 | if (@inside_if) { |
| 841 | my $dep_num = 0; |
| 842 | for my $dependency (@inside_if) { |
| 843 | $symbols{$symbol}{$symcount}{dependency}{$dep_num} = $dependency; |
| 844 | $symbols{$symbol}{$symcount}{max_dependency} = $dep_num; |
| 845 | $dep_num++; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | #------------------------------------------------------------------------------- |
| 851 | # handle range |
| 852 | #------------------------------------------------------------------------------- |
| 853 | sub handle_range { |
| 854 | my ( $range1, $range2, $inside_config, $filename, $line_no ) = @_; |
| 855 | |
| 856 | my $expression; |
| 857 | ( $range2, $expression ) = handle_if_line( $range2, $inside_config, $filename, $line_no ); |
| 858 | |
| 859 | $range1 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/; |
| 860 | my $checkrange1 = $1; |
| 861 | $range2 =~ /^\s*(?:0x)?([A-Fa-f0-9]+)\s*$/; |
| 862 | my $checkrange2 = $1; |
| 863 | |
| 864 | if ( $checkrange1 && $checkrange2 && ( hex($checkrange1) > hex($checkrange2) ) ) { |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 865 | show_error("Range entry in $filename line $line_no value 1 ($range1) is greater than value 2 ($range2)."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | if ($inside_config) { |
| 869 | if ( exists( $symbols{$inside_config}{range1} ) ) { |
| 870 | if ( ( $symbols{$inside_config}{range1} != $range1 ) || ( $symbols{$inside_config}{range2} != $range2 ) ) { |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 871 | if ($show_note_output) { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 872 | print "#!!!!! Note: Config '$inside_config' range entry $range1 $range2 at $filename:$line_no does"; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 873 | print " not match the previously defined range $symbols{$inside_config}{range1}" |
| 874 | . " $symbols{$inside_config}{range2}"; |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 875 | print " defined at $symbols{$inside_config}{range_file}:$symbols{$inside_config}{range_line_no}.\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | else { |
| 880 | $symbols{$inside_config}{range1} = $range1; |
| 881 | $symbols{$inside_config}{range2} = $range2; |
| 882 | $symbols{$inside_config}{range_file} = $filename; |
| 883 | $symbols{$inside_config}{range_line_no} = $line_no; |
| 884 | } |
| 885 | } |
| 886 | else { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 887 | show_error("Range entry at $filename:$line_no is not inside a config block."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | #------------------------------------------------------------------------------- |
| 892 | # handle_default |
| 893 | #------------------------------------------------------------------------------- |
| 894 | sub handle_default { |
| 895 | my ( $default, $name, $inside_config, $inside_choice, $filename, $line_no ) = @_; |
| 896 | my $expression; |
| 897 | ( $default, $expression ) = handle_if_line( $default, $inside_config, $filename, $line_no ); |
| 898 | |
| 899 | if ($inside_config) { |
| 900 | handle_expressions( $default, $inside_config, $filename, $line_no ); |
| 901 | my $sym_num = $symbols{$inside_config}{count}; |
| 902 | |
| 903 | unless ( exists $symbols{$inside_config}{$sym_num}{default_max} ) { |
| 904 | $symbols{$inside_config}{$sym_num}{default_max} = 0; |
| 905 | } |
| 906 | my $default_max = $symbols{$inside_config}{$sym_num}{default_max}; |
| 907 | $symbols{$inside_config}{$sym_num}{default}{$default_max}{default} = $default; |
| 908 | $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_line_no} = $line_no; |
| 909 | if ($expression) { |
| 910 | $symbols{$inside_config}{$sym_num}{default}{$default_max}{default_depends_on} = $expression; |
| 911 | } |
| 912 | } |
| 913 | elsif ($inside_choice) { |
| 914 | handle_expressions( $default, $inside_config, $filename, $line_no ); |
| 915 | } |
| 916 | else { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 917 | show_error("$name entry at $filename:$line_no is not inside a config or choice block."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | #------------------------------------------------------------------------------- |
| 922 | # handle_if_line |
| 923 | #------------------------------------------------------------------------------- |
| 924 | sub handle_if_line { |
| 925 | my ( $exprline, $inside_config, $filename, $line_no ) = @_; |
| 926 | |
| 927 | if ( $exprline !~ /if/ ) { |
| 928 | return ( $exprline, "" ); |
| 929 | } |
| 930 | |
| 931 | #remove any quotes that might have an 'if' in them |
| 932 | my $savequote; |
| 933 | if ( $exprline =~ /^\s*("[^"]+")/ ) { |
| 934 | $savequote = $1; |
| 935 | $exprline =~ s/^\s*("[^"]+")//; |
| 936 | } |
| 937 | |
| 938 | my $expr = ""; |
| 939 | if ( $exprline =~ /\s*if\s+(.*)$/ ) { |
| 940 | $expr = $1; |
| 941 | $exprline =~ s/\s*if\s+.*$//; |
| 942 | |
| 943 | if ($expr) { |
| 944 | handle_expressions( $expr, $inside_config, $filename, $line_no ); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | if ($savequote) { |
| 949 | $exprline = $savequote; |
| 950 | } |
| 951 | |
| 952 | return ( $exprline, $expr ); |
| 953 | } |
| 954 | |
| 955 | #------------------------------------------------------------------------------- |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 956 | # handle_symbol - log which symbols are being used |
| 957 | #------------------------------------------------------------------------------- |
| 958 | sub handle_symbol { |
| 959 | my ( $symbol, $filename, $line_no ) = @_; |
| 960 | |
| 961 | #filter constant symbols first |
| 962 | if ( $symbol =~ /^[yn]$/ ) { # constant y/n |
| 963 | return; |
| 964 | } |
| 965 | if ( $symbol =~ /^-?(?:0x)?\p{XDigit}+$/ ) { # int/hex values |
| 966 | return; |
| 967 | } |
| 968 | if ( $symbol =~ /^"[^"]*"$/ ) { # string values |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | if ( $symbol =~ /^([A-Za-z0-9_]+)$/ ) { # actual symbol |
| 973 | add_referenced_symbol( $1, $filename, $line_no, 'expression' ); |
| 974 | } |
| 975 | else { |
| 976 | show_error("Unrecognized expression: expected symbol, " |
| 977 | . "found '$symbol' in $filename line $line_no."); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | #------------------------------------------------------------------------------- |
| 982 | # handle_expressions - find symbols in expressions |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 983 | #------------------------------------------------------------------------------- |
| 984 | sub handle_expressions { |
| 985 | my ( $exprline, $inside_config, $filename, $line_no ) = @_; |
| 986 | |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 987 | my $strip = qr/\s*(.*[^\s]+)\s*/; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 988 | |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 989 | my $parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
| 990 | my $quotes = qr/"[^"]*"/; |
| 991 | my $balanced = qr/((?:$parens|$quotes|[^\(\)"])+)/; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 992 | |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 993 | if ( $exprline =~ /^\s*$balanced\s*(?:\|\||&&)\s*(.+)$/ ) { |
Nico Huber | 2d82195 | 2021-04-02 21:56:45 +0200 | [diff] [blame] | 994 | # <expr> '||' <expr>, <expr> '&&' <expr> (8)(7) |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 995 | my ( $lhs, $rhs ) = ( $1, $3 ); |
| 996 | handle_expressions( $lhs, $inside_config, $filename, $line_no ); |
| 997 | handle_expressions( $rhs, $inside_config, $filename, $line_no ); |
| 998 | } |
| 999 | elsif ( $exprline =~ /^\s*!(.+)$/ ) { |
Nico Huber | 2d82195 | 2021-04-02 21:56:45 +0200 | [diff] [blame] | 1000 | # '!' <expr> (6) |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1001 | handle_expressions( $1, $inside_config, $filename, $line_no ); |
| 1002 | } |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 1003 | elsif ( $exprline =~ /^\s*$parens\s*$/ ) { |
Nico Huber | 2d82195 | 2021-04-02 21:56:45 +0200 | [diff] [blame] | 1004 | # '(' <expr> ')' (5) |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 1005 | $exprline =~ /^\s*\((.*)\)\s*$/; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1006 | handle_expressions( $1, $inside_config, $filename, $line_no ); |
| 1007 | } |
Nico Huber | 2d82195 | 2021-04-02 21:56:45 +0200 | [diff] [blame] | 1008 | elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*(?:[<>]=?|!=)$strip$/ ) { |
| 1009 | # <symbol> '<' <symbol>, <symbol> '!=' <symbol>, etc. (4)(3) |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 1010 | my ( $lhs, $rhs ) = ( $1, $2 ); |
| 1011 | handle_symbol( $lhs, $filename, $line_no ); |
| 1012 | handle_symbol( $rhs, $filename, $line_no ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1013 | } |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 1014 | elsif ( $exprline =~ /^\s*($quotes|[^"\s]+)\s*=$strip$/ ) { |
| 1015 | # <symbol> '=' <symbol> (2) |
| 1016 | my ( $lhs, $rhs ) = ( $1, $2 ); |
| 1017 | handle_symbol( $lhs, $filename, $line_no ); |
| 1018 | handle_symbol( $rhs, $filename, $line_no ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1019 | } |
Nico Huber | f6b2baa | 2021-04-02 21:23:32 +0200 | [diff] [blame] | 1020 | elsif ( $exprline =~ /^$strip$/ ) { |
| 1021 | # <symbol> (1) |
| 1022 | handle_symbol( $1, $filename, $line_no ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1023 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | #------------------------------------------------------------------------------- |
| 1027 | # add_referenced_symbol |
| 1028 | #------------------------------------------------------------------------------- |
| 1029 | sub add_referenced_symbol { |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 1030 | my ( $symbol, $filename, $line_no, $reftype ) = @_; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1031 | if ( exists $referenced_symbols{$symbol} ) { |
| 1032 | $referenced_symbols{$symbol}{count}++; |
| 1033 | $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{filename} = $filename; |
| 1034 | $referenced_symbols{$symbol}{ $referenced_symbols{$symbol}{count} }{line_no} = $line_no; |
| 1035 | } |
| 1036 | else { |
| 1037 | $referenced_symbols{$symbol}{count} = 0; |
| 1038 | $referenced_symbols{$symbol}{0}{filename} = $filename; |
| 1039 | $referenced_symbols{$symbol}{0}{line_no} = $line_no; |
| 1040 | } |
Martin Roth | b7c39b2 | 2016-01-14 09:04:53 -0700 | [diff] [blame] | 1041 | |
| 1042 | #mark the symbol as being selected, use referenced symbols for location |
| 1043 | if ( $reftype eq 'select' ) { |
| 1044 | $selected_symbols{$symbol}{ $referenced_symbols{$symbol}{count} } = 1; |
| 1045 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | #------------------------------------------------------------------------------- |
| 1049 | # handle_help |
| 1050 | #------------------------------------------------------------------------------- |
| 1051 | { |
| 1052 | #create a non-global static variable by enclosing it and the subroutine |
| 1053 | my $help_whitespace = ""; #string to show length of the help whitespace |
Martin Roth | 8849f3b | 2017-05-23 19:43:43 -0600 | [diff] [blame] | 1054 | my $help_keyword_whitespace = ""; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1055 | |
| 1056 | sub handle_help { |
| 1057 | my ( $line, $inside_help, $inside_config, $inside_choice, $filename, $line_no ) = @_; |
| 1058 | |
| 1059 | if ($inside_help) { |
| 1060 | |
| 1061 | #get the indentation level if it's not already set. |
| 1062 | if ( ( !$help_whitespace ) && ( $line !~ /^[\r\n]+/ ) ) { |
| 1063 | $line =~ /^(\s+)/; #find the indentation level. |
| 1064 | $help_whitespace = $1; |
| 1065 | if ( !$help_whitespace ) { |
Martin Roth | 8849f3b | 2017-05-23 19:43:43 -0600 | [diff] [blame] | 1066 | show_error("$filename:$line_no - help text starts with no whitespace."); |
| 1067 | return $inside_help; |
| 1068 | } |
| 1069 | elsif ($help_keyword_whitespace eq $help_whitespace) { |
| 1070 | show_error("$filename:$line_no - help text needs additional indentation."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1071 | return $inside_help; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | #help ends at the first line which has a smaller indentation than the first line of the help text. |
Martin Roth | 8849f3b | 2017-05-23 19:43:43 -0600 | [diff] [blame] | 1076 | if ( ( $line !~ /^$help_whitespace/ ) && ( $line !~ /^[\r\n]+/ ) ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1077 | $inside_help = 0; |
| 1078 | $help_whitespace = ""; |
Martin Roth | 8849f3b | 2017-05-23 19:43:43 -0600 | [diff] [blame] | 1079 | $help_keyword_whitespace = ""; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1080 | } |
| 1081 | else { #if it's not ended, add the line to the helptext array for the symbol's instance |
| 1082 | if ($inside_config) { |
| 1083 | my $sym_num = $symbols{$inside_config}{count}; |
| 1084 | if ($help_whitespace) { $line =~ s/^$help_whitespace//; } |
| 1085 | push( @{ $symbols{$inside_config}{$sym_num}{helptext} }, $line ); |
| 1086 | } |
Martin Roth | 8849f3b | 2017-05-23 19:43:43 -0600 | [diff] [blame] | 1087 | if ( ($help_keyword_whitespace eq $help_whitespace) && ( $line !~ /^[\r\n]+/ ) ) { |
| 1088 | show_error("$filename:$line_no - help text needs additional indentation."); |
| 1089 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | elsif ( ( $line =~ /^(\s*)help/ ) || ( $line =~ /^(\s*)---help---/ ) ) { |
| 1093 | $inside_help = $line_no; |
Martin Roth | 8849f3b | 2017-05-23 19:43:43 -0600 | [diff] [blame] | 1094 | $line =~ /^(\s+)/; |
| 1095 | $help_keyword_whitespace = $1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1096 | if ( ( !$inside_config ) && ( !$inside_choice ) ) { |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1097 | if ($show_note_output) { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 1098 | print "# Note: $filename:$line_no help is not inside a config or choice block.\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1099 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1100 | } |
| 1101 | elsif ($inside_config) { |
| 1102 | $help_whitespace = ""; |
| 1103 | my $sym_num = $symbols{$inside_config}{count}; |
| 1104 | $symbols{$inside_config}{$sym_num}{help_line_no} = $line_no; |
| 1105 | $symbols{$inside_config}{$sym_num}{helptext} = (); |
| 1106 | } |
| 1107 | } |
| 1108 | return $inside_help; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | #------------------------------------------------------------------------------- |
| 1113 | # handle_type |
| 1114 | #------------------------------------------------------------------------------- |
| 1115 | sub handle_type { |
| 1116 | my ( $type, $inside_config, $filename, $line_no ) = @_; |
| 1117 | |
| 1118 | my $expression; |
| 1119 | ( $type, $expression ) = handle_if_line( $type, $inside_config, $filename, $line_no ); |
| 1120 | |
Martin Roth | 08ee1cf | 2016-01-25 16:39:32 -0700 | [diff] [blame] | 1121 | if ( $type =~ /tristate/ ) { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 1122 | show_error("$filename:$line_no - tristate types are not used."); |
Martin Roth | 08ee1cf | 2016-01-25 16:39:32 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1125 | if ($inside_config) { |
| 1126 | if ( exists( $symbols{$inside_config}{type} ) ) { |
| 1127 | if ( $symbols{$inside_config}{type} !~ /$type/ ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1128 | show_error( "Config '$inside_config' type entry $type" |
| 1129 | . " at $filename:$line_no does not match $symbols{$inside_config}{type}" |
| 1130 | . " defined at $symbols{$inside_config}{type_file}:$symbols{$inside_config}{type_line_no}." ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | else { |
| 1134 | $symbols{$inside_config}{type} = $type; |
| 1135 | $symbols{$inside_config}{type_file} = $filename; |
| 1136 | $symbols{$inside_config}{type_line_no} = $line_no; |
| 1137 | } |
| 1138 | } |
| 1139 | else { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 1140 | show_error("Type entry at $filename:$line_no is not inside a config block."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | #------------------------------------------------------------------------------- |
| 1145 | # handle_prompt |
| 1146 | #------------------------------------------------------------------------------- |
| 1147 | sub handle_prompt { |
| 1148 | my ( $prompt, $name, $menu_array_ref, $inside_config, $inside_choice, $filename, $line_no ) = @_; |
| 1149 | |
| 1150 | my $expression; |
| 1151 | ( $prompt, $expression ) = handle_if_line( $prompt, $inside_config, $filename, $line_no ); |
| 1152 | |
| 1153 | if ($inside_config) { |
| 1154 | if ( $prompt !~ /^\s*$/ ) { |
| 1155 | if ( $prompt =~ /^\s*"([^"]*)"\s*$/ ) { |
| 1156 | $prompt = $1; |
| 1157 | } |
| 1158 | |
Martin Roth | 08cf90f | 2016-01-25 19:54:16 -0700 | [diff] [blame] | 1159 | #display an error if there's a prompt at the top menu level |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1160 | if ( !defined @$menu_array_ref[0] ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1161 | show_error( "Symbol '$inside_config' with prompt '$prompt' appears outside of a menu" |
| 1162 | . " at $filename:$line_no." ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | my $sym_num = $symbols{$inside_config}{count}; |
Martin Roth | b58d349 | 2016-01-25 16:42:13 -0700 | [diff] [blame] | 1166 | if ( !exists $symbols{$inside_config}{$sym_num}{prompt_max} ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1167 | $symbols{$inside_config}{$sym_num}{prompt_max} = 0; |
| 1168 | } |
Martin Roth | b58d349 | 2016-01-25 16:42:13 -0700 | [diff] [blame] | 1169 | else { |
| 1170 | $symbols{$inside_config}{$sym_num}{prompt_max}++; |
| 1171 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1172 | my $prompt_max = $symbols{$inside_config}{$sym_num}{prompt_max}; |
| 1173 | $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt} = $prompt; |
| 1174 | $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_line_no} = $line_no; |
Martin Roth | b58d349 | 2016-01-25 16:42:13 -0700 | [diff] [blame] | 1175 | |
| 1176 | $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_menu} = @$menu_array_ref; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1177 | if ($expression) { |
| 1178 | $symbols{$inside_config}{$sym_num}{prompt}{$prompt_max}{prompt_depends_on} = $expression; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | elsif ($inside_choice) { |
| 1183 | |
| 1184 | #do nothing |
| 1185 | } |
| 1186 | else { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 1187 | show_error("$name entry at $filename:$line_no is not inside a config or choice block."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | #------------------------------------------------------------------------------- |
| 1192 | # simple_line_checks - Does some basic checks on the current line, then cleans the line |
| 1193 | # up for further processing. |
| 1194 | #------------------------------------------------------------------------------- |
| 1195 | sub simple_line_checks { |
| 1196 | my ( $line, $filename, $line_no ) = @_; |
| 1197 | |
| 1198 | #check for spaces instead of tabs |
| 1199 | if ( $line =~ /^ +/ ) { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 1200 | show_error("$filename:$line_no starts with a space."); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | #verify a linefeed at the end of the line |
| 1204 | if ( $line !~ /.*\n/ ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1205 | show_error( "$filename:$line_no does not end with linefeed." |
| 1206 | . " This can cause the line to not be recognized by the Kconfig parser.\n#($line)" ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1207 | $line =~ s/\s*$//; |
| 1208 | } |
| 1209 | else { |
| 1210 | chop($line); |
| 1211 | } |
| 1212 | |
| 1213 | return $line; |
| 1214 | } |
| 1215 | |
| 1216 | #------------------------------------------------------------------------------- |
| 1217 | # load_kconfig_file - Loads a single Kconfig file or expands * wildcard |
| 1218 | #------------------------------------------------------------------------------- |
| 1219 | sub load_kconfig_file { |
| 1220 | my ( $input_file, $loadfile, $loadline, $expanded, $topfile, $topline ) = @_; |
| 1221 | my @file_data; |
| 1222 | my @dir_file_data; |
| 1223 | |
| 1224 | #recursively handle coreboot's new source glob operator |
Arthur Heymans | 55f0132 | 2019-11-05 12:06:59 +0100 | [diff] [blame] | 1225 | if ( $input_file =~ /^(.*?)\/(\w*)\*(\w*)\/(.*)$/ ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1226 | my $dir_prefix = $1; |
Arthur Heymans | 55f0132 | 2019-11-05 12:06:59 +0100 | [diff] [blame] | 1227 | my $dir_glob_prefix = $2; |
| 1228 | my $dir_glob_suffix = $3; |
| 1229 | my $dir_suffix = $4; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1230 | if ( -d "$dir_prefix" ) { |
| 1231 | |
| 1232 | opendir( D, "$dir_prefix" ) || die "Can't open directory '$dir_prefix'\n"; |
| 1233 | my @dirlist = sort { $a cmp $b } readdir(D); |
| 1234 | closedir(D); |
| 1235 | |
| 1236 | while ( my $directory = shift @dirlist ) { |
| 1237 | |
| 1238 | #ignore non-directory files |
Arthur Heymans | 55f0132 | 2019-11-05 12:06:59 +0100 | [diff] [blame] | 1239 | if ( ( -d "$dir_prefix/$directory" ) && !( $directory =~ /^\..*/ ) |
| 1240 | && ( $directory =~ /\Q$dir_glob_prefix\E.*\Q$dir_glob_suffix\E/ ) ) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1241 | push @dir_file_data, |
| 1242 | load_kconfig_file( "$dir_prefix/$directory/$dir_suffix", |
| 1243 | $input_file, $loadline, 1, $loadfile, $loadline ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | #the directory should exist when using a glob |
| 1249 | else { |
Julius Werner | e5eb2de | 2019-03-05 17:10:19 -0800 | [diff] [blame] | 1250 | show_error("Could not find dir '$dir_prefix'"); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | #if the file exists, try to load it. |
| 1255 | elsif ( -e "$input_file" ) { |
| 1256 | |
Martin Roth | a7d0027 | 2016-10-03 23:00:04 +0200 | [diff] [blame] | 1257 | #throw a warning if the file has already been loaded. |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1258 | if ( exists $loaded_files{$input_file} ) { |
Martin Roth | a7d0027 | 2016-10-03 23:00:04 +0200 | [diff] [blame] | 1259 | show_warning("'$input_file' sourced at $loadfile:$loadline was already loaded by $loaded_files{$input_file}"); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | #load the file's contents and mark the file as loaded for checking later |
| 1263 | open( my $HANDLE, "<", "$input_file" ) or die "Error: could not open file '$input_file'\n"; |
| 1264 | @file_data = <$HANDLE>; |
| 1265 | close $HANDLE; |
| 1266 | $loaded_files{$input_file} = "'$loadfile' line $loadline"; |
| 1267 | } |
| 1268 | |
| 1269 | # if the file isn't being loaded from a glob, it should exist. |
| 1270 | elsif ( $expanded == 0 ) { |
Martin Roth | 7aa3cea | 2015-11-27 18:45:45 -0700 | [diff] [blame] | 1271 | show_warning("Could not find file '$input_file' sourced at $loadfile:$loadline"); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | my $line_in_file = 0; |
| 1275 | while ( my $line = shift @file_data ) { |
| 1276 | |
| 1277 | #handle line continuation. |
| 1278 | my $continue_line = 0; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1279 | while ( $line =~ /(.*)\s+\\$/ ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1280 | my $text = $1; |
| 1281 | |
| 1282 | # get rid of leading whitespace on all but the first and last lines |
| 1283 | $text =~ s/^\s*/ / if ($continue_line); |
| 1284 | |
| 1285 | $dir_file_data[$line_in_file]{text} .= $text; |
| 1286 | $line = shift @file_data; |
| 1287 | $continue_line++; |
| 1288 | |
| 1289 | #put the data into the continued lines (other than the first) |
| 1290 | $line =~ /^\s*(.*)\s*$/; |
| 1291 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1292 | $dir_file_data[ $line_in_file + $continue_line ]{text} = "\t# continued line ( " . $1 . " )\n"; |
| 1293 | $dir_file_data[ $line_in_file + $continue_line ]{filename} = $input_file; |
| 1294 | $dir_file_data[ $line_in_file + $continue_line ]{file_line_no} = $line_in_file + $continue_line + 1; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1295 | |
| 1296 | #get rid of multiple leading spaces for last line |
| 1297 | $line = " $1\n"; |
| 1298 | } |
| 1299 | |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1300 | $dir_file_data[$line_in_file]{text} .= $line; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1301 | $dir_file_data[$line_in_file]{filename} = $input_file; |
| 1302 | $dir_file_data[$line_in_file]{file_line_no} = $line_in_file + 1; |
| 1303 | |
| 1304 | $line_in_file++; |
| 1305 | if ($continue_line) { |
| 1306 | $line_in_file += $continue_line; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | if ($topfile) { |
| 1311 | my %file_data; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1312 | $file_data{text} = "\t### File '$input_file' loaded from '$topfile' line $topline\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1313 | $file_data{filename} = $topfile; |
| 1314 | $file_data{file_line_no} = "($topline)"; |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1315 | unshift( @dir_file_data, \%file_data ); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | return @dir_file_data; |
| 1319 | } |
| 1320 | |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1321 | #------------------------------------------------------------------------------- |
| 1322 | # print_wholeconfig - prints out the parsed Kconfig file |
| 1323 | #------------------------------------------------------------------------------- |
| 1324 | sub print_wholeconfig { |
| 1325 | |
| 1326 | return unless $print_full_output; |
| 1327 | |
Martin Roth | a7648f2 | 2021-11-05 17:24:29 -0600 | [diff] [blame] | 1328 | for ( my $i = 0 ; $i <= $#wholeconfig ; $i++ ) { |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1329 | my $line = $wholeconfig[$i]; |
| 1330 | chop( $line->{text} ); |
| 1331 | |
| 1332 | #replace tabs with spaces for consistency |
| 1333 | $line->{text} =~ s/\t/ /g; |
Martin Roth | 819e672 | 2016-01-25 16:38:05 -0700 | [diff] [blame] | 1334 | printf "%-120s # $line->{filename} line $line->{file_line_no} ($line->{menus})\n", $line->{text}; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | #------------------------------------------------------------------------------- |
| 1339 | # check_if_file_referenced - checks for kconfig files that are not being parsed |
| 1340 | #------------------------------------------------------------------------------- |
| 1341 | sub check_if_file_referenced { |
| 1342 | my $filename = $File::Find::name; |
Martin Roth | ab2d777 | 2016-01-25 16:45:14 -0700 | [diff] [blame] | 1343 | if ( ( $filename =~ /Kconfig/ ) |
| 1344 | && ( !$filename =~ /\.orig$/ ) |
| 1345 | && ( !$filename =~ /~$/ ) |
| 1346 | && ( !exists $loaded_files{$filename} ) ) |
| 1347 | { |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1348 | show_warning("'$filename' is never referenced"); |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | #------------------------------------------------------------------------------- |
| 1353 | # check_arguments parse the command line arguments |
| 1354 | #------------------------------------------------------------------------------- |
| 1355 | sub check_arguments { |
| 1356 | my $show_usage = 0; |
| 1357 | GetOptions( |
| 1358 | 'help|?' => sub { usage() }, |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1359 | 'e|errors_off' => \$suppress_error_output, |
| 1360 | 'n|notes' => \$show_note_output, |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1361 | 'o|output=s' => \$output_file, |
| 1362 | 'p|print' => \$print_full_output, |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1363 | 'w|warnings_off' => \$suppress_warning_output, |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1364 | 'path=s' => \$top_dir, |
| 1365 | 'c|config=s' => \$config_file, |
| 1366 | 'G|no_git_grep' => \$dont_use_git_grep, |
Martin Roth | 873ebf2 | 2023-08-10 10:00:51 -0600 | [diff] [blame] | 1367 | 'S|site_local' => \$include_site_local, |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1368 | ); |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1369 | |
| 1370 | if ($suppress_error_output) { |
| 1371 | $suppress_warning_output = 1; |
| 1372 | } |
| 1373 | if ($suppress_warning_output) { |
Martin Roth | 1b44f7e | 2016-01-11 14:31:38 -0700 | [diff] [blame] | 1374 | $show_note_output = 0; |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1375 | } |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | #------------------------------------------------------------------------------- |
| 1379 | # usage - Print the arguments for the user |
| 1380 | #------------------------------------------------------------------------------- |
| 1381 | sub usage { |
| 1382 | print "kconfig_lint <options>\n"; |
| 1383 | print " -o|--output=file Set output filename\n"; |
| 1384 | print " -p|--print Print full output\n"; |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1385 | print " -e|--errors_off Don't print warnings or errors\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1386 | print " -w|--warnings_off Don't print warnings\n"; |
Martin Roth | d808017 | 2015-11-26 19:12:44 -0700 | [diff] [blame] | 1387 | print " -n|--notes Show minor notes\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1388 | print " --path=dir Path to top level kconfig\n"; |
| 1389 | print " -c|--config=file Filename of config file to load\n"; |
| 1390 | print " -G|--no_git_grep Use standard grep tools instead of git grep\n"; |
Martin Roth | 873ebf2 | 2023-08-10 10:00:51 -0600 | [diff] [blame] | 1391 | print " -S|--site_local Include the site-local directory\n"; |
Martin Roth | bcaaad1 | 2015-10-18 11:16:25 -0600 | [diff] [blame] | 1392 | |
| 1393 | exit(0); |
| 1394 | } |
| 1395 | |
| 1396 | 1; |