sbsv1_os/e32toolp/e32util/checksource.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Front-end to routines checking that source matches filename policy constraints
       
    15 # 
       
    16 #
       
    17 
       
    18 use strict;
       
    19 use FindBin;
       
    20 use Cwd;
       
    21 use Getopt::Long qw(:config posix_default);		#permits "+" in arguments, rather than as an option identifier
       
    22 use lib $FindBin::Bin;
       
    23 use CheckSource;
       
    24 use Pathutl;
       
    25 
       
    26 my $preprocessCall = "cpp.exe -w -undef -nostdinc -+ -dI";
       
    27 
       
    28 my $verbose = 0;
       
    29 
       
    30 my $preprocess = 0;
       
    31 my $parsefile = 0;
       
    32 my $metadata = 0;
       
    33 
       
    34 GetOptions ('preprocess|pp' => \$preprocess, 'parsefile|pf' => \$parsefile, 'metadata|md' => \$metadata);
       
    35 
       
    36 my %warnings;
       
    37 
       
    38 $verbose = 1 if $ENV{CHECKSOURCE_VERBOSE};
       
    39 
       
    40 
       
    41 if (!@ARGV)
       
    42 	{
       
    43 	print ("\nchecksource.pl --preprocess|--parsefile|--metadata args\n\n");
       
    44 	}
       
    45 elsif ($preprocess)
       
    46 	{
       
    47 	my $PREPROCESSPIPE;
       
    48 	open PREPROCESSPIPE,"$preprocessCall @ARGV |" or die "ERROR: Can't invoke preprocessor for checksource processing.\n";
       
    49 
       
    50 	my %processedIncludes;
       
    51 	
       
    52 	my %homelessInclude;
       
    53 	$homelessInclude{REFERENCE} = "";
       
    54 
       
    55 	my $lineNumber = 0;
       
    56 	my $currentFile = "";
       
    57 
       
    58 	print "Preprocessing file    : ".$ARGV[scalar(@ARGV)-1]."\n" if ($verbose);
       
    59 
       
    60 	while (<PREPROCESSPIPE>)
       
    61 		{
       
    62 		if (/^\s*$/o)
       
    63 			{
       
    64 			$lineNumber++;
       
    65 			next;
       
    66 			}	
       
    67 		elsif (/^# (\d+) "(.*)"( \d+)?/o)
       
    68 			{
       
    69 			$lineNumber = scalar $1;
       
    70 			$currentFile=$2;
       
    71 			$currentFile=~s-\\\\-\\-go;
       
    72 			$currentFile=~s-\\\/-\\-go;
       
    73 			$currentFile=~s-\/\\-\\-go;
       
    74 			$currentFile=~s-\/\/-\\-go;
       
    75 			$currentFile=~s/\//\\/g;
       
    76 			$currentFile=~s/^\w://;
       
    77 			}
       
    78 
       
    79 		if ($homelessInclude{REFERENCE})
       
    80 			{
       
    81 			# Resolve #include references using "locating" comments in CPP output.  These may either follow the reference directly,
       
    82 			# or may have appeared earlier in prior CPP processing
       
    83 			if ($currentFile =~ /$homelessInclude{SEARCH}$/i)
       
    84 				{
       
    85 				CheckSource_Physical (%warnings, $homelessInclude{SOURCEFILE}, "#include", $homelessInclude{REFERENCE}, $homelessInclude{LINENUMBER}, $currentFile, $verbose);
       
    86 				$processedIncludes{lc($currentFile)} = 1;
       
    87 				}
       
    88 			else
       
    89 				{
       
    90 				my @foundProcessedIncludes;
       
    91 				foreach my $processedInclude (keys %processedIncludes)
       
    92 					{
       
    93 					push @foundProcessedIncludes, $processedInclude if ($processedInclude =~ /$homelessInclude{SEARCH}$/i);
       
    94 					}
       
    95 		
       
    96 				if (@foundProcessedIncludes == 1)
       
    97 					{
       
    98 					CheckSource_Physical  (%warnings, $homelessInclude{SOURCEFILE}, "#include", $homelessInclude{REFERENCE}, $homelessInclude{LINENUMBER}, $foundProcessedIncludes[0], $verbose);
       
    99 					}
       
   100 				elsif ((@foundProcessedIncludes > 1) && $verbose)
       
   101 					{
       
   102 					print ("WARNING: Multiple matches for #include reference $homelessInclude{REFERENCE} : $homelessInclude{SOURCEFILE}($homelessInclude{LINENUMBER})\n");
       
   103 					print "\t$_\n" foreach (@foundProcessedIncludes);
       
   104 					}
       
   105 				elsif ($verbose)
       
   106 					{
       
   107 					print ("WARNING: Couldn't find #include reference $homelessInclude{REFERENCE} : $homelessInclude{SOURCEFILE}($homelessInclude{LINENUMBER})\n");
       
   108 					}
       
   109 				}
       
   110 			$homelessInclude{REFERENCE} = "";
       
   111 			}
       
   112 
       
   113 		if (/^\s*#include\s*[\"|\<]{1}(.*)[\"|\>]{1}/)
       
   114 			{
       
   115 			CheckSource_UnixSlash (%warnings, $currentFile, "#include", $1, $lineNumber, $verbose);
       
   116 
       
   117 			$homelessInclude{SOURCEFILE} = $currentFile;
       
   118 			$homelessInclude{REFERENCE} = $1;
       
   119 			$homelessInclude{LINENUMBER} = $lineNumber;
       
   120 			$homelessInclude{SEARCH} = "\\".$homelessInclude{REFERENCE};
       
   121 			$homelessInclude{SEARCH} =~ s/\//\\/g;
       
   122 			$homelessInclude{SEARCH} = quotemeta($homelessInclude{SEARCH});
       
   123 			}
       
   124 
       
   125 		$lineNumber++ if (!/^# (\d+) "(.*)"( \d+)?/o);
       
   126 		}
       
   127 
       
   128 	close PREPROCESSPIPE;
       
   129 	}
       
   130 elsif ($parsefile)
       
   131 	{
       
   132 	my $SOURCE_FILE = $ARGV[0];		
       
   133 	open SOURCE_FILE, "< $SOURCE_FILE" or die "ERROR: Can't open $SOURCE_FILE for \"-checksource\".\n";
       
   134 
       
   135 	print "Parsing include file  : $SOURCE_FILE\n" if ($verbose);
       
   136 
       
   137 	my $lineNumber = 0;
       
   138 	
       
   139 	while (<SOURCE_FILE>)
       
   140 		{
       
   141 		$lineNumber++;
       
   142 
       
   143 		if (/^\s*#include\s*[\"|\<]{1}(.*)[\"|\>]{1}/)
       
   144 			{
       
   145 			CheckSource_UnixSlash (%warnings, $SOURCE_FILE, "#include", $1, $lineNumber, $verbose);
       
   146 			CheckSource_Lowercase (%warnings, $SOURCE_FILE, "#include", $1, $lineNumber, $verbose);
       
   147 			}		
       
   148 		}
       
   149 
       
   150 	close SOURCE_FILE;
       
   151 	}
       
   152 elsif ($metadata)
       
   153 	{
       
   154 	my ($sourcefile, $item, $reference, $linenumber, $physical, $offset) = @ARGV;
       
   155 
       
   156 	$physical = 0 if (!$physical);
       
   157 	$offset = undef if (!$offset);
       
   158 
       
   159 	CheckSource_UnixSlash (%warnings, $sourcefile, $item, $reference, $linenumber, $verbose);
       
   160 
       
   161 	if ($physical)
       
   162 		{
       
   163 		CheckSource_Physical (%warnings, $sourcefile, $item, $reference, $linenumber, $offset, $verbose);
       
   164 		}
       
   165 	else
       
   166 		{
       
   167 		CheckSource_Lowercase (%warnings, $sourcefile, $item, $reference, $linenumber, $verbose);
       
   168 		}
       
   169 	}
       
   170 
       
   171 print "$_\n" foreach (keys %warnings);