diff mbox series

[RFC,v2,3/4] developer: add an alternative script for detecting broken N_()

Message ID 20210901091941.34886-4-carenas@gmail.com (mailing list archive)
State New, archived
Headers show
Series developer: support pedantic | expand

Commit Message

Carlo Marcelo Arenas Belón Sept. 1, 2021, 9:19 a.m. UTC
obviously incomplete and buggy (ex: won't detect two overlapping matches)

it could be added to some makefile target or documented better as an
alternative to the compilation errors the previous implementation did,
but I have to admit, I haven't found any place in the codebase where
a valid concatenation could take place, so at least the tracking of
exceptions might not be worthy, even if it might be the best part.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 .../find_accidentally_concat_i18n_strings.pl  | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 devtools/find_accidentally_concat_i18n_strings.pl
diff mbox series

Patch

diff --git a/devtools/find_accidentally_concat_i18n_strings.pl b/devtools/find_accidentally_concat_i18n_strings.pl
new file mode 100755
index 0000000000..82bffb2477
--- /dev/null
+++ b/devtools/find_accidentally_concat_i18n_strings.pl
@@ -0,0 +1,69 @@ 
+#!/usr/bin/perl
+
+#
+# find .. \( -name "*.c" -o -name "*.h" \) -exec ./find_accidentally_concat_i18n_strings.pl {} \;
+#
+# this will help find places in the code that might have strings that
+# are marked for translation but are not correctly separated, causing
+# problems like the one reported in :
+#
+#   https://lore.kernel.org/git/ecb18f9d6ac56da0a61c3b98f8f2236@74d39fa044aa309eaea14b9f57fe79c/
+# 
+
+use strict;
+use warnings;
+
+my $myself = $0;
+my $file = $ARGV[0];
+my $errors = 0;
+my $key;
+
+chomp(my @exceptions = <DATA>);
+
+sub ask {
+	local $| = 1;
+	print "possible bug found in $key:\n";
+	print "\n$&\n";
+	print "\nadd exception (y/N): ";
+	chomp(my $answer = <STDIN>);
+	if (lc($answer) ne 'y') {
+		++$errors;
+		return;
+	}
+	return 1;
+}
+
+sub process_file {
+	my $content;
+	open(my $fh, '<', $file) or die;
+	{
+		local $/;
+		$content = <$fh>;
+	}
+	close($fh);
+	while ($content =~ /N_\(.*?\)[ \t\n]+N_\(.*?\)/g) {
+		my $index = length($`);
+		$key = "$file:$index";
+		if (!grep {/^$key$/} @exceptions) {
+			push @exceptions, $key if ask();
+		}
+	}
+}
+
+&process_file;
+
+{
+	local *MYSELF;
+	local $/ = "\n__END__";
+	open (MYSELF, $myself);
+	chomp(my $file = <MYSELF>);
+	close MYSELF;
+	open (MYSELF, ">$myself") || die "can't update myself";
+	print MYSELF $file, "\n__END__\n";
+	foreach (@exceptions) {
+		print MYSELF "$_\n";
+	}
+	close MYSELF;
+}
+exit($errors);
+__END__