Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / tools / check_for_bad_includes.pl
blob455df8ea6f0c0ce049cd661bbca19405648f03ad
1 #!/usr/bin/perl -w
2 #
3 # Script to replace all incorrect "include" statements in WRF trunk. For user-defined files,
4 # include statements must use quotes, not brackets, according to the C standard:
6 # INCORRECT
7 # #include <model_data_order.inc>
9 # CORRECT
10 # #include "model_data_order.inc"
12 # OPTIONS:
13 # use "--search_path" to specify the search pattern for files you want to edit.
14 # - Separate different search patterns with spaces
15 # - Be sure to escape wildcard characters with a backslash!
16 # - Default is --search_path="../\*/\*.F ../\*/\*/\*.F ../\*/\*/\*/\*.F ../\*/\*/\*/\*/\*.F ../\*/\*.inc ../\*/\*/\*.f90 ../\*/\*/\*/\*.f90"
17 # use "--dryrun=yes" to do a dry run, where all the output of what would be changed is printed out, but no changes are made.
19 # Created by Michael Kavulich, November 2015
20 # No rights reserved
23 use strict;
24 use warnings;
25 use File::Basename;
26 use Getopt::Long;
28 my $dryrun = "no";
29 my $search_path = "../\*/\*.F ../\*/\*/\*.F ../\*/\*/\*/\*.F ../\*/\*/\*/\*/\*.F ../\*/\*.inc ../\*/\*/\*.f90 ../\*/\*/\*/\*.f90";
30 GetOptions ('search_path=s' => \$search_path,
31 "dryrun:s" => \$dryrun ) or die "\nInvalid option(s) specified, view script comments for help\n";
33 print "\nSearching for brackets in file(s): $search_path\n\n";
35 my @source_files=glob("$search_path");
37 my $found=0;
38 my $notfound=0;
39 my $changed=0;
40 foreach my $filename (@source_files) {
41 open (IN, $filename) or die "Cannot open file $filename for read: $!";
42 my @lines=<IN>;
43 close IN;
45 if (grep(/#(\s*)(include|INCLUDE)(\s*)</,@lines)) {
46 print "Brackets found in file: $filename\n";
47 $found ++;
48 } else {
49 # print "Brackets NOT found in file: $filename\n";
50 $notfound ++;
51 next;
54 open (OUT, ">", $filename) or die "Cannot open file $filename for write: $!";
55 foreach my $line (@lines) {
56 if ($line =~ /#(\s*)(include|INCLUDE)(\s*)</) {
57 print "Found line with brackets: $line\n";
58 my @inc_files = split /[<>]/,$line;
59 if ($inc_files[1] =~ /<mpi.*/) {
60 print "Skipping line that contains 'mpi':\n";
61 print "$line\n";
62 } else {
63 if ( $dryrun =~ /y/i ) {
64 my $linetemp = $line;
65 $linetemp =~ s/<$inc_files[1]>/\"$inc_files[1]\"/;
66 print "Changed line to: $linetemp\n";
67 } else {
68 $line =~ s/<$inc_files[1]>/\"$inc_files[1]\"/;
69 print "Changed line to: $line\n";
71 $changed++;
74 print OUT $line;
76 close OUT;
79 print "\nBrackets found in $found files.\n";
80 print "\nBrackets NOT found in $notfound files.\n";
81 print "\n$changed lines changed\n";