-
Notifications
You must be signed in to change notification settings - Fork 487
/
style.pm
1296 lines (997 loc) · 37.6 KB
/
style.pm
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
#!/usr/bin/perl
#
# Copyright (c) 2014 Microsoft Open Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
# LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS
# FOR A PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
# Microsoft would like to thank the following companies for their review and
# assistance with these files: Intel Corporation, Mellanox Technologies Ltd,
# Dell Products, L.P., Facebook, Inc., Marvell International Ltd.
#
# @file style.pm
#
# @brief This module defines SAI Metadata Style Parser
#
package style;
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
use utils;
require Exporter;
sub RunAspell
{
my $hash = shift;
my %wordsToCheck = %{ $hash };
if (not -e "/usr/bin/aspell")
{
LogError "ASPELL IS NOT PRESENT, please install aspell";
return;
}
LogInfo "Running Aspell";
my @keys = sort keys %wordsToCheck;
my $count = @keys;
my $all = "@keys";
LogInfo "Words to check: $count";
my @result = `echo "$all" | /usr/bin/aspell -l en -a -p ./aspell.en.pws 2>&1`;
for my $res (@result)
{
if ($res =~ /error/i)
{
LogError "aspell error: $res";
last;
}
next if not $res =~ /^\s*&\s*(\S+)/;
my $word = $1;
chomp $res;
my $where = "??";
if (not defined $wordsToCheck{$word})
{
for my $k (@keys)
{
if ($k =~ /(^$word|$word$)/)
{
$where = $wordsToCheck{$k};
last;
}
$where = $wordsToCheck{$k} if ($k =~ /$word/);
}
}
else
{
$where = $wordsToCheck{$word};
}
if ($word =~ /^[A-Z0-9]{2,}$/)
{
LogWarning "Word '$word' is misspelled or is acronym, add to acronyms.txt? $where";
}
else
{
LogWarning "Word '$word' is misspelled $where";
}
}
}
sub CheckDoxygenStyle
{
my ($header, $line, $n) = @_;
return if (not $line =~ /\@(\w+)/);
my $mark = $1;
if ($mark eq "file" and not $line =~ /\@file\s+($header)/)
{
LogWarning "\@file should match format: sai\\w+.h: $header $n:$line";
return;
}
if ($mark eq "brief" and not $line =~ /\@brief\s+[A-Z]/)
{
LogWarning "\@brief should start with capital letter: $header $n:$line";
return;
}
if ($mark eq "return" and not $line =~ /\@return\s+(#SAI_|[A-Z][a-z])/)
{
LogWarning "\@return should start with #: $header $n:$line";
return;
}
if ($mark eq "param" and not $line =~ /\@param\[(in|out|inout)\] (\.\.\.|[a-z]\w+)\s+([A-Z]\w+)/)
{
LogWarning "\@param should be in format \@param[in|out|inout] [a-z]\\w+ [A-Z]\\w+: $header $n:$line";
return;
}
if ($mark eq "defgroup" and not $line =~ /\@defgroup SAI\w* SAI - \w+/)
{
LogWarning "\@defgroup should be in format \@defgroup SAI\\w* SAI - \\w+: $header $n:$line";
return;
}
}
sub ExtractComments
{
my $input = shift;
my @comments = ();
# good enough comments extractor C/C++ source
while ($input =~ m!(".*?")|//.*?[\r\n]|/\*.*?\*/!s)
{
$input = $';
push @comments,$& if not $1;
}
return @comments;
}
sub CheckHeaderLicense
{
my ($data, $file) = @_;
my $header = <<_END_
/**
* Copyright (c) 20XX Microsoft Open Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS
* FOR A PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing
* permissions and limitations under the License.
*
* Microsoft would like to thank the following companies for their review and
* assistance with these files: Intel Corporation, Mellanox Technologies Ltd,
* Dell Products, L.P., Facebook, Inc., Marvell International Ltd.
_END_
;
$header =~ s/^( \*|\/\*\*)/#/gm if $data =~ /^#/;
# remove first line (shell definition)
$data =~ s/^#.+\n// if $data =~ /^#/;
my $is = substr($data, 0, length($header));
$is =~ s/ 20\d\d / 20XX /;
return if $is eq $header;
LogWarning "Wrong header in $file, is:\n$is\n should be:\n\n$header";
}
sub CheckStatsFunction
{
my ($fname,$fn,$fnparams) = @_;
return if $fname eq "sai_clear_port_all_stats_fn"; # exception
return if $fname eq "sai_get_tam_snapshot_stats_fn"; # exception
return if $fname eq "sai_bulk_object_get_stats_fn"; # exception
return if $fname eq "sai_bulk_object_clear_stats_fn"; # exception
if (not $fname =~ /^sai_((get|clear)_(\w+)_stats|get_\w+_stats_ext)_fn$/)
{
LogWarning "wrong stat function name: $fname, expected: sai_(get|clear)_\\w+_stats(_ext)?_fn";
}
if (not $fnparams =~ /^\w+_id number_of_counters counter_ids( (mode )?counters)?$/)
{
if (not $fnparams =~ /^\w+_entry number_of_counters counter_ids( (mode )?counters)?$/)
{
LogWarning "invalid stat function $fname params names: $fnparams";
}
}
my @paramtypes = $fn =~ /_(?:In|Out|Inout)_\s*(.+?)\s*(?:\w+?)\s*[,\)]/gis;
my $ptypes = "@paramtypes";
if (not $ptypes =~ /^sai_object_id_t uint32_t const sai_stat_id_t \*( (sai_stats_mode_t )?uint64_t \*)?$/)
{
if (not $ptypes =~ /^const \w+_entry_t \* uint32_t const sai_stat_id_t \*( (sai_stats_mode_t )?uint64_t \*)?$/)
{
LogWarning "invalid stat function $fname param types: $ptypes";
}
}
}
sub CheckFunctionsParams
{
#
# make sure that doxygen params match function params names
#
my ($data, $file) = @_;
my $doxygenCommentPattern = '/\*\*((?:(?!\*/).)*?)\*/';
my $fnTypeDefinition = 'typedef\s*\w+[^;]+?(\w+_fn)\s*\)([^;]+?);';
my $globalFunction = 'sai_\w+\s*(sai_\w+)[^;]*?\(([^;]+?);';
while ($data =~ m/$doxygenCommentPattern\s*(?:$fnTypeDefinition|$globalFunction)/gis)
{
my $comment = $1;
my $fname = $2;
my $fn = $3;
$fname = $4 if defined $4;
$fn = $5 if defined $5;
my @params = $comment =~ /\@param\[\w+]\s+(\.\.\.|\w+)/gis;
my @fnparams = $fn =~ /_(?:In|Out|Inout)_.+?(\.\.\.|\w+)\s*[,\)]/gis;
for my $p (@params)
{
LogWarning "param $p in $fname should not be prefixed sai_" if $p =~ /sai_/;
}
my $params = "@params";
my $fnparams = "@fnparams";
if ($params ne $fnparams)
{
LogWarning "not matching params in function $fname: $file";
LogWarning " doxygen '$params' vs code '$fnparams'";
}
if ("@params" =~ /[A-Z]/)
{
LogWarning "params should use small letters only '@params' in $fname: $file";
}
# exceptions
next if $fname eq "sai_remove_all_neighbor_entries_fn";
next if $fname eq "sai_switch_register_write_fn";
next if $fname eq "sai_switch_register_read_fn";
next if $fname eq "sai_switch_mdio_write_fn";
next if $fname eq "sai_switch_mdio_read_fn";
next if $fname eq "sai_switch_mdio_cl22_write_fn";
next if $fname eq "sai_switch_mdio_cl22_read_fn";
my @paramsFlags = lc($comment) =~ /\@param\[(\w+)]/gis;
my @fnparamsFlags = lc($fn) =~ /_(\w+)_.+?(?:\.\.\.|\w+)\s*[,\)]/gis;
if (not "@paramsFlags" eq "@fnparamsFlags")
{
LogWarning "params flags not match ('@paramsFlags' vs '@fnparamsFlags') in $fname: $file";
}
next if not $fname =~ /_fn$/; # below don't apply for global functions
if (not $fnparams =~ /^(\w+)(| attr| attr_count attr_list| switch_id attr_count attr_list)$/ and
not $fname =~ /_(stats|stats_ext|notification)_fn$|^sai_(send|allocate|free|recv|bulk)_|^sai_meta/)
{
LogWarning "wrong param names: $fnparams: $fname";
LogWarning " expected: $params[0](| attr| attr_count attr_list| switch_id attr_count attr_list)";
}
if ($fname =~ /^sai_(get|set|create|remove)_(\w+?)(_attribute)?(_stats|_stats_ext)?_fn/)
{
my $pattern = $2;
my $first = $params[0];
if ($pattern =~ /_entry$/)
{
$pattern = "${pattern}_id|${pattern}";
}
else
{
$pattern = "${pattern}_id";
}
if (not $first =~ /^$pattern$/)
{
LogWarning "first param should be called ${pattern} but is $first in $fname: $file";
}
}
if ($fname =~ /^sai_\w+_stats_/)
{
CheckStatsFunction($fname,$fn,$fnparams);
}
}
}
sub CheckNonDoxygenComments
{
my ($data, $file) = @_;
while ($data =~ m%( */\*[^\*](?:(?!\*/).)*?\*/)(\n *(\w+))?%gis)
{
my $comment = $1;
my $stick = $3;
if (($comment =~ /\W\@\w+/is) or defined $stick)
{
LogWarning "candidate for doxygen comment in $file:\n$comment";
LogWarning "comment sticked to $stick" if defined $stick;
}
}
}
sub CheckDoxygenCommentFormating
{
my ($data, $file) = @_;
while ($data =~ m%/\*\*(?:(?!\*/).)*?(\*/\n[\n]+(\s*[a-z][^\n]*))%gis)
{
LogWarning "empty line between doxygen comment and definition: $file: $2";
}
while ($data =~ m%( *)(/\*\*(?:(?!\*/).)*?\*/)%gis)
{
my $spaces = $1 . " ";
my $comment = $2;
next if $comment =~ m!^/\*\*.*\*/$!; # single line comment
my @lines = split/\n/,$comment;
my $first = shift @lines;
my $last = pop @lines;
if (not $first =~ m!^\s*/..$!)
{
LogWarning "first line doxygen comment should be with '/**': $file: $first";
next;
}
if (not $last =~ m!^\s*\*/$!)
{
LogWarning "last line doxygen comment should be '*/': $file: $last";
next;
}
if (not $lines[0] =~ m!\* (\@|Copyright )!)
{
LogWarning "first doxygen line should contain \@ tag $file: $lines[0]";
}
if ($lines[$#lines] =~ m!^\s*\*\s*$!)
{
LogWarning "last doxygen line should not be empty $file: $lines[$#lines]";
}
for my $line (@lines)
{
if (not $line =~ m!^\s*\*( |$)!)
{
LogWarning "multiline doxygen comments should start with '* ': $file: $line";
}
if (not $line =~ /^$spaces\*/)
{
LogWarning "doxygen comment has invalid ident: $file: $line";
}
}
next; # disabled for now since it generates too much changes
$comment =~ s!^ *(/\*\*|\*/|\* *)!!gm;
if ($comment =~ m!\@brief\s+(.+?)\n\n!s)
{
my $brief = $1;
if (not $brief =~ /\.$/)
{
LogWarning "brief should end with dot $file: $brief";
}
}
my @n = split/^\@\S+ /m,$comment;
}
while($data =~ m!(([^\n ])+\n */\*\*.{1,30}.+?\n)!isg)
{
next if $2 eq "{";
LogWarning "doxygen comment must be preceded with blank line: $file:\n$1";
}
}
sub IsObjectName
{
my $ot = shift;
return 1 if defined $main::OBJTOAPIMAP{$ot} or defined $main::OBJTOAPIMAP{uc("SAI_OBJECT_TYPE_".$ot)};
return 0;
}
sub CheckFunctionNaming
{
my ($header, $n, $line) = @_;
return if not $line =~ /^\s*sai_(\w+)_fn\s+(\w+)\s*;/;
my $typename = $1;
my $name = $2;
my @listex = qw(
allocate_hostif_packet
flush_fdb_entries
free_hostif_packet
profile_get_next_value
profile_get_value
recv_hostif_packet
remove_all_neighbor_entries
send_hostif_packet
switch_mdio_read
switch_mdio_write
switch_mdio_cl22_read
switch_mdio_cl22_write
switch_register_read
switch_register_write);
my $REG = "(" . (join"|",@listex) . ")";
if ($name =~ /^$REG$/)
{
# ok
}
elsif ($name =~ /^(get|clear)_(\w+?)_(all_)?stats(_ext)?$/)
{
LogWarning "not object name $2 in $name" if not IsObjectName($2);
}
elsif ($name =~ /^(create|remove|get|set)_(\w+?)(_attribute)?$/)
{
my $n = $2;
$n =~ s/_entries$/_entry/ if $typename =~ /^bulk/;
$n =~ s/s$// if $typename =~ /^bulk/;
LogWarning "not object name $n in $name" if not IsObjectName($n);
}
else
{
LogWarning "Line not matching any name pattern: $line";
}
if ($typename ne $name and not $typename =~ /^bulk_/)
{
LogWarning "function not matching $typename vs $name in $header:$n:$line";
}
if (not $name =~ /^(create|remove|get|set)_\w+?(_attribute)?$|^clear_\w+_stats$/)
{
# exceptions
return if $name =~ /^$REG$/;
LogWarning "function not follow convention in $header:$n:$line";
}
}
sub CheckQuadApi
{
my ($data, $file) = @_;
return if not $data =~ m!(sai_\w+_api_t)(.+?)\1;!igs;
my $apis = $2;
my @fns = $apis =~ /sai_(\w+)_fn/g;
# this function forces order of existing and new added apis in api struct
my $order = "";
for my $function (@fns)
{
my $f = $function;
$f =~ s/remove_all_neighbor_entries/X/;
$f =~ s/clear_port_all_stats/X/;
$f =~ s/^create_\w+/c/;
$f =~ s/^remove_\w+/r/;
$f =~ s/^set_\w+_attribute/s/;
$f =~ s/^get_\w+_attribute/g/;
$f =~ s/^get_\w+_stats$/0/;
$f =~ s/^get_\w+_stats_ext/1/;
$f =~ s/^clear_\w+_stats/2/;
$f =~ s/^bulk_object_create/C/;
$f =~ s/^bulk_object_remove/R/;
$f =~ s/^bulk_object_set_attribute/S/;
$f =~ s/^bulk_object_get_attribute/G/;
$f =~ s/^bulk_create_\w+/C/;
$f =~ s/^bulk_remove_\w+/R/;
$f =~ s/^bulk_set_\w+_attribute/S/;
$f =~ s/^bulk_get_\w+_attribute/G/;
$f = "X" if length $f != 1;
$order .= $f;
}
$order =~ s/crsg012/t/g; # order should be: create,remove,set,get,get_stats,get_stats_ext,clear_stats
$order =~ s/crsg/q/g; # order should be: create,remove,set,get
$order =~ s/CRSG/Q/g; # order should be: bulk_create,bulk_remove,bulk_set,bulk_get
$order =~ s/012/s/g; # order should be: get_stats,get_stats_ext,clear_stats
$order =~ s/CR/E/g; # order should be: bulk_create,bulk_remove
$order =~ s/SG/T/g; # order should be: bulk_set,bulk_get
$order =~ s/X+/X/g; # order should be: any non quad and non stats api
if (not $order =~ /^[tqQsETX]*$/)
{
LogWarning "Wrong api order: $order";
LogWarning "$apis";
}
my $fn = join" ",@fns;
my @quad = split/\bcreate_/,$fn;
for my $q (@quad)
{
next if $q eq "";
if (not $q =~ /(\w+) remove_\1 set_\1_attribute get_\1_attribute( |$)/)
{
LogWarning "quad api must be in order: create remove set get: $file: $q";
}
}
}
sub CheckSwitchKeys
{
my ($data, $file) = @_;
my $keycount = $1 if $data =~ /#define\s+SAI_SWITCH_ATTR_MAX_KEY_COUNT\s+(\d+)/s;
my $count = 0;
while ($data =~ m!#define\s+SAI_KEY_(\w+)\s+"SAI_(\w+)"!gs)
{
if ($1 ne $2)
{
LogWarning "SAI_(KEY_)$1 should match SAI_$2";
}
$count++;
}
if ($count != $keycount)
{
LogWarning "SAI_SWITCH_ATTR_MAX_KEY_COUNT is $keycount, but found only $count keys";
}
}
sub CheckStructAlignment
{
my ($data, $file) = @_;
# return if $file eq "saitypes.h";
while ($data =~ m!typedef\s+(?:struct|union)\s+_(sai_\w+)(.+?)}\s*(\w+);!igs)
{
my $struct = $1;
my $inner = $2;
my $enddef = $3;
if ($struct ne $enddef)
{
LogError "expected same names for _$struct $enddef";
}
# we use space in regex since \s will capture \n
$inner =~ m/^( *)(\w.+\s+)(\w+)\s*;$/im;
my $spaces = $1;
my $inside = $2;
my $name = $3;
while ($inner =~ m/^( *)(\w.+\s+)(\w+)\s*;$/gim)
{
my $itemname = $2;
if ($1 ne $spaces or (length($2) != length($inside) and $struct =~ /_api_t/))
{
LogError "$struct items has invalid column ident: $file: $itemname";
}
}
}
}
sub GetAcronyms
{
# load acronyms list from file
my $filename = "acronyms.txt";
open FILE, $filename or die "Couldn't open file $filename: $!";
my @acronyms;
while (<FILE>)
{
chomp;
my $line = $_;
next if $line =~ /(^#|^$)/;
if (not $line =~ /^([A-Z0-9]{2,})(\s+-\s+(.+))?$/)
{
LogWarning "invalid line in $filename: $line";
next;
}
my $acronym = $1;
my $definition = $3;
push @acronyms,$acronym;
}
close FILE;
my $prev = "";
for my $acr (@acronyms)
{
LogWarning "Acronyms are not sorted: $prev, $acr" if ($prev cmp $acr) > 0;
$prev = $acr;
}
return @acronyms;
}
sub CheckMetadataSourceFiles
{
my @files = GetMetadataSourceFiles();
for my $file (@files)
{
# skip auto generated headers
next if $file eq "saimetadata.h";
next if $file eq "saimetadata.c";
next if $file eq "saimetadatatest.c";
next if $file eq "saimetadatasize.h";
next if $file eq "saiattrversion.h";
next if $file eq "sai_rpc_server.cpp";
next if $file =~ /swig|wrap/;
my $data = ReadHeaderFile($file);
CheckHeaderLicense($data, $file);
LogError "missing declaration '\@file $file'" if (not $data =~ /[*#]\s+\@file\s+\Q$file\E$/m);
my @lines = split/\n/,$data;
my $n = 0;
for my $line(@lines)
{
$n++;
LogWarning "found trailing spaces in $file:$n: $line" if $line =~ /\s+$/;
$line =~ s/\t+/ /g if $file =~ /Makefile/;
if ($line =~ /[^\x20-\x7e]/)
{
LogWarning "line contains non ascii characters $file:$n: $line";
}
}
}
}
sub CheckInOutParams
{
my ($header, $n, $line) = @_;
return if not $line =~ / _(In|Out|Inout)_ /;
return if $header eq "saiserialize.h";
return if $header eq "saimetadatalogger.h";
if (not $line =~ / _(In|Out|Inout)_ (const )?(\w+)( \*| \*\*| )(\w+)[\),]/)
{
LogError "not matching param line: $header:$n: $line";
return;
}
my $inout = $1;
my $const = (not defined $2) ? "" : "const";
my $type = $3;
my $ptr = $4;
my $param = $5;
if ($type eq "sai_object_id_t" and not $line =~ /_In_ sai_object_id_t \w+|_In_ const sai_object_id_t \*\w+|_Out_ sai_object_id_t \*\w+/)
{
LogError "wrong in/out param mix: $header:$n:$line";
}
if ($type eq "sai_attribute_t" and not $line =~ /_In_ const sai_attribute_t \*\*?\w+|_Inout_ sai_attribute_t \*\*?\w+/)
{
return if $header eq "saihostif.h";
LogError "wrong in/out param mix: $header:$n:$line";
}
# TODO we should know if type is simple or struct/union
return if $line =~ /_In_ \w+ \w+/ and $const eq ""; # non const types without pointer should be In
return if $line =~ /_Inout_ \w+ \*\w+/ and $const eq ""; # non const types with pointer should be Inout
return if $line =~ /_Out_ \w+ \*\w+/ and $const eq ""; # non const types with pointer should be Out
return if $line =~ /_In_ const \w+ \*\*?\w+/; # const types with pointer should be In
return if $line =~ /_In_ const \w+ \w+/; # const types without pointer
return if $line =~ /_Out_ const char \*\*\w+/;
return if $line =~ /_Out_ void \*\*\w+/;
return if $line =~ /_Inout_ sai_attribute_t \*\*\w+/;
LogWarning "Not supported param prefixes, FIXME: $header:$n $line";
}
sub CheckComment
{
my ($data, $header) = @_;
my @lines = split/\n/,$data;
return if $data =~ /\s*\s*\@(file|defgroup|}|def |extraparam|passparam)/;
my $c = "";
for my $line (@lines)
{
next if $line =~ m!^/\*\*|\*/!;
$line =~ s/\@note|\@par //g;
if ($line =~ /^\s*\*\s+\@warning/) { $c .= "W"; next }
if ($line =~ /^\s*\*\s+\@param/) { $c .= "P"; next }
if ($line =~ /^\s*\*$/) { $c .= " "; next }
if ($line =~ /^\s*\*\s+\@brief/) { $c .= "B"; next }
if ($line =~ /^\s*\*\s+\@return/) { $c .= "R"; next }
if ($line =~ /^\s*\*\s+\@/) { $c .= "@"; next }
$c .= "x";
}
return if $c eq "";
$c =~ s/x+/x/g;
$c =~ s/Px/P/g;
$c =~ s/P( P)+/P/g;
$c =~ s/P+/P/g;
$c =~ s/Bx/B/g;
$c =~ s/Rx/R/g;
$c =~ s/x( x)+/x/g;
$c =~ s/\@+/@/g;
return if $c =~ /^B( W)?( x)?( \@)?( P)?( R)?$/;
LogWarning "empty line required between each below elements:";
LogWarning "desired elemen order: \@brief \@warning? description? \@attributes? \@params? \@return?";
LogWarning "invalid spacing ($c) on $header:\n$data\n";
}
sub CheckDoxygenSpacing
{
my ($data, $header) = @_;
my @comments = ExtractComments($data, $header);
for my $com (@comments)
{
next if $com =~ m!^//!;
next if not $com =~ m!^/\*\*!;
CheckComment($com, $header);
}
}
sub GetWordsFromSources
{
my $wordsToCheck = shift;
my @sources = GetMetaSourceFiles();
my @acronyms = GetAcronyms();
my @spellExceptions = qw/ IPv4 IPv6 /;
my %exceptions = map { $_ => $_ } @spellExceptions;
my %ac = ();
$ac{$_} = 1 for @acronyms;
for my $src (sort @sources)
{
next if $src =~ /saimetadata.c/;
next if $src =~ /saimetadatatest.c/;
next if $src =~ /saiswig/;
next if $src =~ /sai_rpc_server.cpp/;
my $data = ReadHeaderFile($src);
my @comments = ExtractComments($data);
for my $comment(@comments)
{
my @lines = split/\n/,$comment;
for my $line (@lines)
{
while ($line =~ /\b([a-z0-9]+)\b/ig)
{
my $pre = $`;
my $post = $';
my $word = $1;
next if $word =~ /xFF/;
next if defined $ac{$word};
next if defined $wordsToCheck->{$word};
next if defined $exceptions{$word};
$wordsToCheck->{$word} = $src;
}
}
}
}
}
sub CheckHeadersStyle
{
#
# Purpose of this check is to find out
# whether header files are correctly formated
#
# Wrong formating includes:
# - multiple empty lines
# - double spaces
# - wrong spacing idient
#
# we could put that to local dictionary file
my @acronyms = GetAcronyms();
my @spellExceptions = qw/ CRC32 IPv4 IPv6 BGPv6 6th 0xFF /;
my %exceptions = map { $_ => $_ } @spellExceptions;
my %wordsToCheck = ();
my %wordsChecked = ();
CheckMetadataSourceFiles();
my @headers = GetHeaderFiles();
my @metaheaders = GetMetaHeaderFiles();
my @exheaders = GetExperimentalHeaderFiles();
@headers = (@headers, @metaheaders, @exheaders);
for my $header (@headers)
{
next if $header eq "saimetadata.h"; # skip auto generated header
next if $header eq "saimetadatasize.h"; # skip auto generated header
next if $header eq "saiattrversion.h"; # skip auto generated header
my $data = ReadHeaderFile($header);
my $oncedef = uc ("__${header}_");
$oncedef =~ s/\./_/g;
my $oncedefCount = 0;
CheckHeaderLicense($data, $header);
CheckFunctionsParams($data, $header);
CheckDoxygenCommentFormating($data, $header);
CheckQuadApi($data, $header);
CheckStructAlignment($data, $header);
CheckNonDoxygenComments($data, $header);
CheckSwitchKeys($data, $header) if $header eq "saiswitch.h";
CheckDoxygenSpacing($data, $header);
my @lines = split/\n/,$data;
my $n = 0;
my $empty = 0;
my $emptydoxy = 0;
for my $line (@lines)
{
$n++;
CheckFunctionNaming($header, $n, $line);
CheckInOutParams($header, $n, $line);
$oncedefCount++ if $line =~ /\b$oncedef\b/;
# detect multiple empty lines
if ($line =~ /^$/)
{
$empty++;
if ($empty > 1)
{
LogWarning "header contains two empty lines one after another $header $n";
}
}
else { $empty = 0 }
# detect multiple empty lines in doxygen comments
if ($line =~ /^\s+\*\s*$/)
{
$emptydoxy++;
if ($emptydoxy > 1)
{
LogWarning "header contains two empty lines in doxygen $header $n";
}
}
else { $emptydoxy = 0 }
if ($line =~ /^\s+\* / and not $line =~ /\*( {4}| {8}| )[^ ]/)
{
LogWarning "not expected number of spaces after * (1,4,8) $header $n:$line";
}
if ($line =~ /\*\s+[^ ].* / and not $line =~ /\* \@(brief|file|note)/)
{
if (not $line =~ /const.+const\s+\w+;/ and not $line =~ m!\\$!)
{
LogWarning "too many spaces after *\\s+ $header $n:$line";
}
}
if ($line =~ /(typedef|{|}|_In\w+|_Out\w+)( [^ ].* | )/ and not $line =~ /typedef\s+u?int/i and not $line =~ m!\\$!)
{
LogWarning "too many spaces $header $n:$line";
}
if ($line =~ m!/\*\*! and not $line =~ m!/\*\*\s*$! and not $line =~ m!/\*\*.+\*/!)
{
LogWarning "multiline doxygen comment should start '/**' $header $n:$line";
}
if ($line =~ m![^ ]\*/!)
{