2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 # Use: echo filename1.cc ... | find_copyrights.pl
7 # or: find_copyrights.pl list_file
8 # or: find_files.pl ... | find_copyrights.pl
14 sub check_is_generated_file
($);
15 sub start_copyright_parsing
();
17 my $progname = basename
($0);
19 my $generated_file_scan_boundary = 25;
25 open (F
, "<$file") or die "$progname: Unable to access $file\n";
26 my $parse_copyright = start_copyright_parsing
();
28 $file_header .= $_ unless $. > $generated_file_scan_boundary;
29 my $copyright_match = $parse_copyright->($_, $.);
30 if ($copyright_match) {
31 $copyrights{lc("$copyright_match")} = "$copyright_match";
35 my $copyright = join(" / ", sort values %copyrights);
37 if (check_is_generated_file
($file_header)) {
38 print "GENERATED FILE";
40 print ($copyright or "*No copyright*");
45 sub check_is_generated_file
($) {
46 my $license = uc($_[0]);
47 # Remove Python multiline comments to avoid false positives
48 if (index($license, '"""') != -1) {
49 $license =~ s/"""[^"]*(?:"""|$)//mg;
51 if (index($license, "'''") != -1) {
52 $license =~ s/'''[^']*(?:'''|$)//mg;
54 # Quick checks using index.
55 if (index($license, 'ALL CHANGES MADE IN THIS FILE WILL BE LOST') != -1) {
58 if (index($license, 'DO NOT EDIT') != -1 ||
59 index($license, 'DO NOT DELETE') != -1 ||
60 index($license, 'GENERATED') != -1) {
61 return ($license =~ /(All changes made
in this file will be lost
' .
62 'DO NOT
(EDIT
|delete this file
)|Generated
(at
|automatically
|data
)' .
63 '|Automatically generated
|\Wgenerated\s
+(?
:\w
+\s
+)*file\W
)/i
);
68 sub are_within_increasing_progression
($$$) {
69 my $delta = $_[0] - $_[1];
70 return $delta >= 0 && $delta <= $_[2];
73 sub start_copyright_parsing
() {
74 my $max_line_numbers_proximity = 3;
75 # Set up the defaults the way that proximity checks will not succeed.
76 my $last_a_item_line_number = -200;
77 my $last_b_item_line_number = -100;
81 my $line_number = $_[1];
83 # Remove C / C++ strings to avoid false positives.
84 if (index($line, '"') != -1) {
85 $line =~ s/"[^"\\]*(?:\\.[^"\\]*)*"//g;
88 my $uc_line = uc($line);
90 # Record '(a)' and '(b)' last occurences in C++ comments.
91 my $cpp_comment_idx = index($uc_line, '//');
92 if ($cpp_comment_idx != -1) {
93 if (index($uc_line, '(A)') > $cpp_comment_idx) {
94 $last_a_item_line_number = $line_number;
96 if (index($uc_line, '(B)') > $cpp_comment_idx) {
97 $last_b_item_line_number = $line_number;
101 # Fast bailout, uses the same patterns as the regexp.
102 if (index($uc_line, 'COPYRIGHT') == -1 &&
103 index($uc_line, 'COPR.') == -1 &&
104 index($uc_line, '\x{00a9}') == -1 &&
105 index($uc_line, '\xc2\xa9') == -1) {
107 my $c_item_index = index($uc_line, '(C)');
108 return '' if ($c_item_index == -1);
109 # Filter out 'c' used as a list item inside C++ comments.
110 # E.g. "// blah-blah (a) blah\n// blah-blah (b) and (c) blah"
111 if ($c_item_index > $cpp_comment_idx &&
112 are_within_increasing_progression
(
114 $last_b_item_line_number,
115 $max_line_numbers_proximity) &&
116 are_within_increasing_progression
(
117 $last_b_item_line_number,
118 $last_a_item_line_number,
119 $max_line_numbers_proximity)) {
124 my $copyright_indicator_regex =
125 '(?:copyright|copr\.|\x{00a9}|\xc2\xa9|\(c\))';
126 my $full_copyright_indicator_regex =
127 sprintf '(?:\W|^)%s(?::\s*|\s+)(\w.*)$', $copyright_indicator_regex;
128 my $copyright_disindicator_regex =
129 '\b(?:info(?:rmation)?|notice|and|or)\b';
132 if ($line =~ m
%$full_copyright_indicator_regex%i) {
134 if ($match !~ m
%^\s
*$copyright_disindicator_regex%i) {
135 $match =~ s/([,.])?\s*$//;
136 $match =~ s/$copyright_indicator_regex//ig;
138 $match =~ s/\s{2,}/ /g;