Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / tools / internal / spell_comments.pl
1 #!/usr/bin/env perl
2
3 # Copyright (c) 2013-2019. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 # C comment spell checker
10 # For each given source file, print the filename, a colon, and the number
11 # of misspelled words, then a list of misspelled words.
12 # Words contained in the file stopwords.txt are not considered spelling errors.
13 # Copyright 2003, Dan Kegel.  Licensed under GPL.  See the file ../COPYING for details.
14
15 use strict;
16 use warnings;
17
18 die "Please install iamerican to use that script.\n"
19   unless (-r "/usr/lib/ispell/american.hash");
20
21 sub check_content($) {
22         my $content = shift;
23         $content =~ tr/*/ /;
24         print POUT "$content\n";
25 }
26
27 my $TEMPFILE="/tmp/spell.tmp";
28 my $DICTFILE="tools/internal/spell_dict.txt";
29 $DICTFILE="./spell_dict.txt" unless (-e $DICTFILE);
30 die "Call this script from its location or from the SimGrid root directory\n" unless (-e $DICTFILE);
31
32 die "Usage: ". ($DICTFILE eq "./spell_dict.txt"? "./":"tools/internal/")."spell_comments.pl "
33            ."`find ". ($DICTFILE eq "./spell_dict.txt"? "../../":".")." -name '*.[ch]' -o -name '*.hpp' -o -name '*.cpp' |grep -v umpire|grep -v smpi/mpich3-test|grep -v NAS | grep -v src/smpi/colls`\n"
34   unless scalar(@ARGV)>0;
35
36 my $total = 0;
37 foreach my $file (@ARGV) {
38         open (FI, $file) || die "Cannot open $file: $!\n";
39         my $content = join ("", <FI>);
40         close (FI);
41
42         open(POUT, "> $TEMPFILE") || die;
43         $content =~ s!//(.+)$!check_content($1)!egm;
44         $content =~ s!/\*(.+?)\*/!check_content($1)!egs;
45         close(POUT);
46
47         open(PIN, "ispell -d american -p $DICTFILE -l < $TEMPFILE | sort -uf |") || die;
48         my @badwords;
49         while (my $err = <PIN>) {
50             chomp $err; 
51             push(@badwords, $err) if ($err =~ /\w/ && length($err)>0);
52         }
53         close(PIN) || die;
54
55         if (@badwords) {
56                 print "$file: ".scalar(@badwords)." errors: '".join("','",@badwords)."'\n";
57                 $total += scalar(@badwords);
58         }
59 }
60
61 print "Total: $total\n";
62
63 unlink($TEMPFILE);