sbsv1/abld/e32util/checksource.pl
changeset 40 68f68128601f
equal deleted inserted replaced
39:fa9d7d89d3d6 40:68f68128601f
       
     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 FindBin;
       
    19 use Cwd;
       
    20 use Getopt::Long qw(:config posix_default);		#permits "+" in arguments, rather than as an option identifier
       
    21 use lib $FindBin::Bin;
       
    22 use CheckSource;
       
    23 use Pathutl;
       
    24 
       
    25 my $preprocessCall = "cpp.exe -w -undef -nostdinc -+ -dI";
       
    26 
       
    27 my $verbose = 0;
       
    28 
       
    29 my $preprocess = 0;
       
    30 my $parsefile = 0;
       
    31 my $metadata = 0;
       
    32 
       
    33 GetOptions ('preprocess|pp' => \$preprocess, 'parsefile|pf' => \$parsefile, 'metadata|md' => \$metadata);
       
    34 
       
    35 my %warnings;
       
    36 
       
    37 $verbose = 1 if $ENV{CHECKSOURCE_VERBOSE};
       
    38 
       
    39 
       
    40 if (!@ARGV)
       
    41 	{
       
    42 	print ("\nchecksource.pl --preprocess|--parsefile|--metadata args\n\n");
       
    43 	}
       
    44 elsif ($preprocess)
       
    45 	{
       
    46 	my $PREPROCESSPIPE;
       
    47 	open PREPROCESSPIPE,"$preprocessCall @ARGV |" or die "ERROR: Can't invoke preprocessor for checksource processing.\n";
       
    48 
       
    49 	my %processedIncludes;
       
    50 	
       
    51 	my %homelessInclude;
       
    52 	$homelessInclude{REFERENCE} = "";
       
    53 
       
    54 	my $lineNumber = 0;
       
    55 	my $currentFile = "";
       
    56 
       
    57 	print "Preprocessing file    : ".$ARGV[scalar(@ARGV)-1]."\n" if ($verbose);
       
    58 
       
    59 	while (<PREPROCESSPIPE>)
       
    60 		{
       
    61 		if (/^\s*$/o)
       
    62 			{
       
    63 			$lineNumber++;
       
    64 			next;
       
    65 			}	
       
    66 		elsif (/^# (\d+) "(.*)"( \d+)?/o)
       
    67 			{
       
    68 			$lineNumber = scalar $1;
       
    69 			$currentFile=$2;
       
    70 			$currentFile=~s-\\\\-\\-go;
       
    71 			$currentFile=~s-\\\/-\\-go;
       
    72 			$currentFile=~s-\/\\-\\-go;
       
    73 			$currentFile=~s-\/\/-\\-go;
       
    74 			$currentFile=~s/\//\\/g;
       
    75 			$currentFile=~s/^\w://;
       
    76 			}
       
    77 
       
    78 		if ($homelessInclude{REFERENCE})
       
    79 			{
       
    80 			# Resolve #include references using "locating" comments in CPP output.  These may either follow the reference directly,
       
    81 			# or may have appeared earlier in prior CPP processing
       
    82 			if ($currentFile =~ /$homelessInclude{SEARCH}$/i)
       
    83 				{
       
    84 				CheckSource_Physical (%warnings, $homelessInclude{SOURCEFILE}, "#include", $homelessInclude{REFERENCE}, $homelessInclude{LINENUMBER}, $currentFile, $verbose);
       
    85 				$processedIncludes{lc($currentFile)} = 1;
       
    86 				}
       
    87 			else
       
    88 				{
       
    89 				my @foundProcessedIncludes;
       
    90 				foreach my $processedInclude (keys %processedIncludes)
       
    91 					{
       
    92 					push @foundProcessedIncludes, $processedInclude if ($processedInclude =~ /$homelessInclude{SEARCH}$/i);
       
    93 					}
       
    94 		
       
    95 				if (@foundProcessedIncludes == 1)
       
    96 					{
       
    97 					CheckSource_Physical  (%warnings, $homelessInclude{SOURCEFILE}, "#include", $homelessInclude{REFERENCE}, $homelessInclude{LINENUMBER}, $foundProcessedIncludes[0], $verbose);
       
    98 					}
       
    99 				elsif ((@foundProcessedIncludes > 1) && $verbose)
       
   100 					{
       
   101 					print ("WARNING: Multiple matches for #include reference $homelessInclude{REFERENCE} : $homelessInclude{SOURCEFILE}($homelessInclude{LINENUMBER})\n");
       
   102 					print "\t$_\n" foreach (@foundProcessedIncludes);
       
   103 					}
       
   104 				elsif ($verbose)
       
   105 					{
       
   106 					print ("WARNING: Couldn't find #include reference $homelessInclude{REFERENCE} : $homelessInclude{SOURCEFILE}($homelessInclude{LINENUMBER})\n");
       
   107 					}
       
   108 				}
       
   109 			$homelessInclude{REFERENCE} = "";
       
   110 			}
       
   111 
       
   112 		if (/^\s*#include\s*[\"|\<]{1}(.*)[\"|\>]{1}/)
       
   113 			{
       
   114 			CheckSource_UnixSlash (%warnings, $currentFile, "#include", $1, $lineNumber, $verbose);
       
   115 
       
   116 			$homelessInclude{SOURCEFILE} = $currentFile;
       
   117 			$homelessInclude{REFERENCE} = $1;
       
   118 			$homelessInclude{LINENUMBER} = $lineNumber;
       
   119 			$homelessInclude{SEARCH} = "\\".$homelessInclude{REFERENCE};
       
   120 			$homelessInclude{SEARCH} =~ s/\//\\/g;
       
   121 			$homelessInclude{SEARCH} = quotemeta($homelessInclude{SEARCH});
       
   122 			}
       
   123 
       
   124 		$lineNumber++ if (!/^# (\d+) "(.*)"( \d+)?/o);
       
   125 		}
       
   126 
       
   127 	close PREPROCESSPIPE;
       
   128 	}
       
   129 elsif ($parsefile)
       
   130 	{
       
   131 	my $SOURCE_FILE = $ARGV[0];		
       
   132 	open SOURCE_FILE, "< $SOURCE_FILE" or die "ERROR: Can't open $SOURCE_FILE for \"-checksource\".\n";
       
   133 
       
   134 	print "Parsing include file  : $SOURCE_FILE\n" if ($verbose);
       
   135 
       
   136 	my $lineNumber = 0;
       
   137 	
       
   138 	while (<SOURCE_FILE>)
       
   139 		{
       
   140 		$lineNumber++;
       
   141 
       
   142 		if (/^\s*#include\s*[\"|\<]{1}(.*)[\"|\>]{1}/)
       
   143 			{
       
   144 			CheckSource_UnixSlash (%warnings, $SOURCE_FILE, "#include", $1, $lineNumber, $verbose);
       
   145 			CheckSource_Lowercase (%warnings, $SOURCE_FILE, "#include", $1, $lineNumber, $verbose);
       
   146 			}		
       
   147 		}
       
   148 
       
   149 	close SOURCE_FILE;
       
   150 	}
       
   151 elsif ($metadata)
       
   152 	{
       
   153 	my ($sourcefile, $item, $reference, $linenumber, $physical, $offset) = @ARGV;
       
   154 
       
   155 	$physical = 0 if (!$physical);
       
   156 	$offset = undef if (!$offset);
       
   157 
       
   158 	CheckSource_UnixSlash (%warnings, $sourcefile, $item, $reference, $linenumber, $verbose);
       
   159 
       
   160 	if ($physical)
       
   161 		{
       
   162 		CheckSource_Physical (%warnings, $sourcefile, $item, $reference, $linenumber, $offset, $verbose);
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		CheckSource_Lowercase (%warnings, $sourcefile, $item, $reference, $linenumber, $verbose);
       
   167 		}
       
   168 	}
       
   169 
       
   170 print "$_\n" foreach (keys %warnings);