Blame opensm/osm_check

Packit 13e616
#!/usr/bin/perl -W
Packit 13e616
#!/usr/bin/perl -W
Packit 13e616
#
Packit 13e616
#
Packit 13e616
# Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
Packit 13e616
# Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit 13e616
# Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit 13e616
#
Packit 13e616
# This software is available to you under a choice of one of two
Packit 13e616
# licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
# General Public License (GPL) Version 2, available from the file
Packit 13e616
# COPYING in the main directory of this source tree, or the
Packit 13e616
# OpenIB.org BSD license below:
Packit 13e616
#
Packit 13e616
#     Redistribution and use in source and binary forms, with or
Packit 13e616
#     without modification, are permitted provided that the following
Packit 13e616
#     conditions are met:
Packit 13e616
#
Packit 13e616
#      - Redistributions of source code must retain the above
Packit 13e616
#        copyright notice, this list of conditions and the following
Packit 13e616
#        disclaimer.
Packit 13e616
#
Packit 13e616
#      - Redistributions in binary form must reproduce the above
Packit 13e616
#        copyright notice, this list of conditions and the following
Packit 13e616
#        disclaimer in the documentation and/or other materials
Packit 13e616
#        provided with the distribution.
Packit 13e616
#
Packit 13e616
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
# SOFTWARE.
Packit 13e616
#
Packit 13e616
#
Packit 13e616
#
Packit 13e616
#  Abstract:
Packit 13e616
#  	Perl script for simple source code error checking.
Packit 13e616
#
Packit 13e616
#  Environment:
Packit 13e616
#  	Linux User Mode
Packit 13e616
#
Packit 13e616
#  $Revision: 1.4 $
Packit 13e616
#
Packit 13e616
#
Packit 13e616
#
Packit 13e616
# DESCRIPTION:
Packit 13e616
#
Packit 13e616
# This script performs some simple conformance checks on the
Packit 13e616
# OpenSM source code.  It does NOT attempt to act like a full
Packit 13e616
# blown 'C' language parser, so it can be fooled.  Something
Packit 13e616
# is better than nothing.  Running the 'osm_indent' script before
Packit 13e616
# running this script will increase your chances of catching
Packit 13e616
# problems.
Packit 13e616
#
Packit 13e616
#
Packit 13e616
# The following checks are performed:
Packit 13e616
# 1) Verify that the function name provided in a log statement
Packit 13e616
# matches the name of the current function.
Packit 13e616
#
Packit 13e616
# 2) Verify that log statements are in the form that this script
Packit 13e616
# can readily parse.  Improvements to the regular expressions
Packit 13e616
# might make this unnecessary.
Packit 13e616
#
Packit 13e616
# 3) Verify that lower two digits of the error codes used in log
Packit 13e616
# statements are unique within that file.
Packit 13e616
#
Packit 13e616
# 4) Verify that upper two digits of the error codes used in log
Packit 13e616
# statements are not used by any other module.
Packit 13e616
#
Packit 13e616
# USAGE:
Packit 13e616
#
Packit 13e616
# In the OpenSM source directory, type:
Packit 13e616
# osm_check.pl *.c
Packit 13e616
#
Packit 13e616
Packit 13e616
# Do necessary upfront initialization
Packit 13e616
$verbose = 0;
Packit 13e616
$in_c_comment = 0;
Packit 13e616
Packit 13e616
if( !exists $ARGV[0] )
Packit 13e616
{
Packit 13e616
	print "ERROR: You must specify the files on which to operate, such as '*.c'\n";
Packit 13e616
	osm_check_usage();
Packit 13e616
	exit;
Packit 13e616
}
Packit 13e616
Packit 13e616
# loop through all the command line options
Packit 13e616
do
Packit 13e616
{
Packit 13e616
	$doing_params = 0;
Packit 13e616
Packit 13e616
	# First, look for command line options.
Packit 13e616
	if( $ARGV[0] =~ /-[v|V]/ )
Packit 13e616
	{
Packit 13e616
		$verbose += 1;
Packit 13e616
		shift;
Packit 13e616
		print "Verbose mode on, level = $verbose.\n";
Packit 13e616
		$doing_params = 1;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	if( !exists $ARGV[0] )
Packit 13e616
	{
Packit 13e616
		print "ERROR: You must specify the files on which to operate, such as '*.c'\n";
Packit 13e616
		osm_check_usage();
Packit 13e616
		exit;
Packit 13e616
	}
Packit 13e616
}while( $doing_params == 1 );
Packit 13e616
Packit 13e616
LINE: while( <> )
Packit 13e616
{
Packit 13e616
	# Skip C single line C style comments
Packit 13e616
	# This line must come before the multi-line C comment check!
Packit 13e616
	if( /\/\*.*\*\// )
Packit 13e616
	{
Packit 13e616
		$in_c_comment = 0;
Packit 13e616
		next LINE;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# skip multi-line C style comments
Packit 13e616
	if( /\/\*/ )
Packit 13e616
	{
Packit 13e616
		$in_c_comment = 1;
Packit 13e616
		next LINE;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# end skipping of multi-line C style comments
Packit 13e616
	if( /\*\// )
Packit 13e616
	{
Packit 13e616
		$in_c_comment = 0;
Packit 13e616
		next LINE;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# We're still in a C comment, so ignore input
Packit 13e616
	if( $in_c_comment == 1 )
Packit 13e616
	{
Packit 13e616
		next LINE;
Packit 13e616
	}
Packit 13e616
Packit 13e616
Packit 13e616
	# skip C++ style comment lines
Packit 13e616
	if( /^\s*\/\// )
Packit 13e616
	{
Packit 13e616
		next LINE;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# check for bad PRIx64 usage
Packit 13e616
	# It's a common mistake to forget the % before the PRIx64
Packit 13e616
	if( /[^%]\"\s*PRIx64/ )
Packit 13e616
	{
Packit 13e616
		print "No % sign before PRx64!!: $ARGV $.\n";
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# This simple script doesn't handle checking PRIx64 usage
Packit 13e616
	# when PRIx64 starts the line.  Just give a warning.
Packit 13e616
	if( /^\s*PRIx64/ )
Packit 13e616
	{
Packit 13e616
		print "Warning: PRIx64 at start of line.  $ARGV $.\n";
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# Attempt to locate function names.
Packit 13e616
	# Function names must start on the beginning of the line.
Packit 13e616
	if( /^(\w+)\s*\(/ )
Packit 13e616
	{
Packit 13e616
		$current_func = $1;
Packit 13e616
		if( $verbose == 1 )
Packit 13e616
		{
Packit 13e616
			print "Processing $ARGV: $current_func\n";
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# Attempt to find OSM_LOG_ENTER entries.
Packit 13e616
	# When found, verify that the function name provided matches
Packit 13e616
	# the actual function.
Packit 13e616
	if( /OSM_LOG_ENTER\s*\(\s*([\-\.\>\w]+)\s*,\s*(\w+)\s*\)/ )
Packit 13e616
	{
Packit 13e616
		$log_func = $2;
Packit 13e616
		if( $current_func ne $log_func )
Packit 13e616
		{
Packit 13e616
			printf "MISMATCH!! $ARGV $.: $current_func != $log_func\n";
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# Check for non-conforming log statements.
Packit 13e616
	# Log statements must not start the log string on the same line
Packit 13e616
	# as the osm_log function itself.
Packit 13e616
	# Watch out for the #include "osm_log.h" statement as a false positive.
Packit 13e616
	if( /osm_log\s*\(.*\"/ )
Packit 13e616
	{
Packit 13e616
		print "NON-CONFORMING LOG STATEMENT!! $ARGV $.\n";
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# Attempt to find osm_log entries.
Packit 13e616
	if( /^\s*\"(\w+):/ )
Packit 13e616
	{
Packit 13e616
		$log_func = $1;
Packit 13e616
		if( $current_func ne $log_func )
Packit 13e616
		{
Packit 13e616
			print "MISMATCHED LOG FUNCTION!! $ARGV $.: $current_func != $log_func\n";
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# Error logging must look like 'ERR 1234:'
Packit 13e616
	# The upper two digits are error range assigned to that module.
Packit 13e616
	# The lower two digits are the error code itself.
Packit 13e616
	# Error codes are in hexadecimal.
Packit 13e616
	if( /ERR(\s+)([0-9a-fA-F]{2})([0-9a-fA-F]{2})(..)/ )
Packit 13e616
	{
Packit 13e616
		# Check if we already established the error prefix for this module
Packit 13e616
		$err_prefix = $module_err_prefixes{$ARGV};
Packit 13e616
		if( $err_prefix )
Packit 13e616
		{
Packit 13e616
			if( $err_prefix ne $2 )
Packit 13e616
			{
Packit 13e616
				print "BAD ERR RANGE IN LOG ENTRY!! $ARGV $.: $current_func\n";
Packit 13e616
				print "\tExpected $err_prefix but found $2\n";
Packit 13e616
			}
Packit 13e616
		}
Packit 13e616
		else
Packit 13e616
		{
Packit 13e616
			# Create a new prefix for this module.
Packit 13e616
			$module_err_prefixes{$ARGV} = $2;
Packit 13e616
		}
Packit 13e616
Packit 13e616
		$err_base = $module_err_bases{$3};
Packit 13e616
		if( $err_base )
Packit 13e616
		{
Packit 13e616
			print "DUPLICATE ERR NUMBER IN LOG ENTRY!! $ARGV $.: $current_func: $3\n";
Packit 13e616
			print "\tPrevious use on line $err_base.\n";
Packit 13e616
		}
Packit 13e616
		else
Packit 13e616
		{
Packit 13e616
			# Add this error code to the list used by this module
Packit 13e616
			# The data stored in the line number on which it is used.
Packit 13e616
			$module_err_bases{$3} = $.;
Packit 13e616
			if( $verbose > 1 )
Packit 13e616
			{
Packit 13e616
				print "Adding new error: $1$2 in $ARGV.\n";
Packit 13e616
			}
Packit 13e616
		}
Packit 13e616
Packit 13e616
		if( $4 ne ": " )
Packit 13e616
		{
Packit 13e616
			print "MALFORMED LOG STATEMENT!!  NEEDS ': ' $ARGV $.\n";
Packit 13e616
		}
Packit 13e616
Packit 13e616
		if( $1 ne " " )
Packit 13e616
		{
Packit 13e616
			print "USE ONLY 1 SPACE AFTER ERR!! $ARGV $.\n";
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	# verify expected use of sizeof() with pointers
Packit 13e616
	if( /sizeof\s*\(\s*[h|p]_/ )
Packit 13e616
	{
Packit 13e616
		print "SUSPICIOUS USE OF SIZEOF(), DO YOU NEED AN '*' $ARGV $.\n";
Packit 13e616
	}
Packit 13e616
Packit 13e616
Packit 13e616
}
Packit 13e616
continue
Packit 13e616
{
Packit 13e616
	# reset the module base error index when we finished out
Packit 13e616
	# each source file.
Packit 13e616
	if( eof )
Packit 13e616
	{
Packit 13e616
		# reset the base error value, since each module can
Packit 13e616
		# repeat this range.
Packit 13e616
		%module_err_bases = ();
Packit 13e616
		# closing the file here resets the line number with each new file
Packit 13e616
		close ARGV;
Packit 13e616
	}
Packit 13e616
}
Packit 13e616
Packit 13e616
sub osm_check_usage
Packit 13e616
{
Packit 13e616
	print "Usage:\n";
Packit 13e616
	print "osm_check.pl [-v|V] <file list>\n";
Packit 13e616
	print "[-v|V] - enable verbose mode.\n\n";
Packit 13e616
}