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