Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
18855d79f5982a1aaaa288ace0624a59d8124994
[simgrid.git] / tools / internal / spell_comments.pl
1 #!/usr/bin/perl
2
3 # Copyright (c) 2013-2015. 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
17 die "Please install iamerican to use that script." 
18   unless (-r "/usr/lib/ispell/american.hash");
19
20 sub check_content($) {
21         my $content = shift;
22         $content =~ tr/*/ /;
23         print POUT "$content\n";
24 }
25
26 my $TEMPFILE="/tmp/spell.tmp";
27 my $DICTFILE="tools/internal/spell_dict.txt";
28 $DICTFILE="./spell_dict.txt" unless (-e $DICTFILE);
29 die "Call this script from its location or from the SimGrid root directory\n" unless (-e $DICTFILE);
30
31 die "Usage: ". ($DICTFILE eq "./spell_dict.txt"? "./":"tools/internal/")."spell_comments.pl "
32            ."`find ". ($DICTFILE eq "./spell_dict.txt"? "../../":".")." -name '*.[ch]' -o -name '*.hpp' -o -name '*.cpp'`\n"
33   unless length(@ARGV)>1;
34
35 my $total = 0;
36 foreach my $file (@ARGV) {
37         open (FI, $file) || die "Cannot open $file: $!\n";
38         my $content = join ("", <FI>);
39         close (FI);
40
41         open(POUT, "> $TEMPFILE") || die;
42         $content =~ s!//(.+)$!check_content($1)!egm;
43         $content =~ s!/\*(.+?)\*/!check_content($1)!egs;
44         close(POUT);
45
46         open(PIN, "ispell -d american -p $DICTFILE -l < $TEMPFILE | sort -uf |") || die;
47         my @badwords;
48         while (my $err = <PIN>) {
49             chomp $err;     
50             push(@badwords, $err) if ($err =~ /\w/ && length($err)>0);
51         }
52         close(PIN) || die;
53
54         if (@badwords) {
55                 print "$file: ".scalar(@badwords)." errors: '".join("','",@badwords)."'\n";
56                 $total += scalar(@badwords);    
57         }
58 }
59
60 print "Total: $total\n";
61
62 unlink($TEMPFILE);