]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/calc-must-ids.pl
Source Format Enforcement (#763)
[thirdparty/squid.git] / scripts / calc-must-ids.pl
1 #!/usr/bin/perl
2 #
3 ## Copyright (C) 1996-2021 The Squid Software Foundation and contributors
4 ##
5 ## Squid software is distributed under GPLv2+ license and includes
6 ## contributions from numerous individuals and organizations.
7 ## Please see the COPYING and CONTRIBUTORS files for details.
8 ##
9 #
10 # Author: Tsantilas Christos
11 # (C) 2010 The Measurement Factory
12 #
13 # Usage:
14 # calc-must-ids.pl file1 file2 ...
15 # Compute the ids of Must expressions of the given files.
16 # It returns one line per Must expression in the form:
17 # filename: line: MustID 'Must Text'
18 #
19
20 use warnings;
21 use strict;
22
23 # This constant should be synced with ERR_DETAIL_EXCEPTION_START enum
24 # defined in src/err_detail_type.h
25 use constant ERR_DETAIL_EXCEPTION_START => 110000;
26
27 my $file;
28 while ($file = shift @ARGV) {
29 ComputeMustIds($file);
30 }
31 sub FileNameHash
32 {
33 my($name) = @_;
34
35 # Keep in sync with FileNameHash() in src/base/Here.cc!
36
37 $name =~ s/.*\///g;
38 my($i) = 0;
39 my($j) =0;
40 my($n) = 0;
41 my(@na) = split(//, $name);
42 for($j=0; $j < @na; $j++) {
43 $n = $n ^ (271 * ord($na[$j]));
44 }
45 return $n ^ ($j *271);
46 }
47
48 sub ComputeMustIds
49 {
50 my($file) = @_;
51
52 # Keep in sync with SourceLocation::id() in src/base/Here.cc!
53
54 my $fullHash = &FileNameHash($file);
55 my $hash = $fullHash % 0x3FFFF;
56
57 if(!open(IN, "<$file")) {
58 printf STDERR "error opening file $file. Ignore ...";
59 return;
60 }
61 while(<IN>) {
62 my($line) = $_;
63
64 next if $line =~ /^\s*#/; # ignore simple single-line C++ macros
65 $line =~ s@//.*@@; # strip simple // comments
66 $line =~ s@/[*].*?[*]/@@; # strip simple single-line /* comments */
67
68 my($id);
69 if ($line =~ /\bMust\s*\(/ || # Must(...)
70 $line =~ /\bTexcHere\s*\(/ || # TexcHere(...)
71 $line =~ /\bHere\s*\(\s*\)/) { # Here()
72 $line =~ s/^\s*//;
73 $id= ($hash <<14) | ($. & 0x3FFF);
74 $id += ERR_DETAIL_EXCEPTION_START;
75 # print "$file:$.: $id $line";
76 printf "%s:%d: 0x%X %s", $file, $., $id, $line;
77 }
78 }
79 close(IN);
80 }