1219820Sjeff#!/usr/bin/perl -W
2219820Sjeff#!/usr/bin/perl -W
3219820Sjeff#
4219820Sjeff#
5219820Sjeff# Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
6219820Sjeff# Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
7219820Sjeff# Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
8219820Sjeff#
9219820Sjeff# This software is available to you under a choice of one of two
10219820Sjeff# licenses.  You may choose to be licensed under the terms of the GNU
11219820Sjeff# General Public License (GPL) Version 2, available from the file
12219820Sjeff# COPYING in the main directory of this source tree, or the
13219820Sjeff# OpenIB.org BSD license below:
14219820Sjeff#
15219820Sjeff#     Redistribution and use in source and binary forms, with or
16219820Sjeff#     without modification, are permitted provided that the following
17219820Sjeff#     conditions are met:
18219820Sjeff#
19219820Sjeff#      - Redistributions of source code must retain the above
20219820Sjeff#        copyright notice, this list of conditions and the following
21219820Sjeff#        disclaimer.
22219820Sjeff#
23219820Sjeff#      - Redistributions in binary form must reproduce the above
24219820Sjeff#        copyright notice, this list of conditions and the following
25219820Sjeff#        disclaimer in the documentation and/or other materials
26219820Sjeff#        provided with the distribution.
27219820Sjeff#
28219820Sjeff# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29219820Sjeff# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30219820Sjeff# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31219820Sjeff# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32219820Sjeff# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33219820Sjeff# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34219820Sjeff# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35219820Sjeff# SOFTWARE.
36219820Sjeff#
37219820Sjeff#
38219820Sjeff#
39219820Sjeff#  Abstract:
40219820Sjeff#  	Perl script for simple source code error checking.
41219820Sjeff#
42219820Sjeff#  Environment:
43219820Sjeff#  	Linux User Mode
44219820Sjeff#
45219820Sjeff#  $Revision: 1.4 $
46219820Sjeff#
47219820Sjeff#
48219820Sjeff#
49219820Sjeff# DESCRIPTION:
50219820Sjeff#
51219820Sjeff# This script performs some simple conformance checks on the
52219820Sjeff# OpenSM source code.  It does NOT attempt to act like a full
53219820Sjeff# blown 'C' language parser, so it can be fooled.  Something
54219820Sjeff# is better than nothing.  Running the 'osm_indent' script before
55219820Sjeff# running this script will increase your chances of catching
56219820Sjeff# problems.
57219820Sjeff#
58219820Sjeff#
59219820Sjeff# The following checks are performed:
60219820Sjeff# 1) Verify that the function name provided in a log statement
61219820Sjeff# matches the name of the current function.
62219820Sjeff#
63219820Sjeff# 2) Verify that log statements are in the form that this script
64219820Sjeff# can readily parse.  Improvements to the regular expressions
65219820Sjeff# might make this unnecessary.
66219820Sjeff#
67219820Sjeff# 3) Verify that lower two digits of the error codes used in log
68219820Sjeff# statements are unique within that file.
69219820Sjeff#
70219820Sjeff# 4) Verify that upper two digits of the error codes used in log
71219820Sjeff# statements are not used by any other module.
72219820Sjeff#
73219820Sjeff# USAGE:
74219820Sjeff#
75219820Sjeff# In the OpenSM source directory, type:
76219820Sjeff# osm_check.pl *.c
77219820Sjeff#
78219820Sjeff
79219820Sjeff# Do necessary upfront initialization
80219820Sjeff$verbose = 0;
81219820Sjeff$in_c_comment = 0;
82219820Sjeff
83219820Sjeffif( !exists $ARGV[0] )
84219820Sjeff{
85219820Sjeff	print "ERROR: You must specify the files on which to operate, such as '*.c'\n";
86219820Sjeff	osm_check_usage();
87219820Sjeff	exit;
88219820Sjeff}
89219820Sjeff
90219820Sjeff# loop through all the command line options
91219820Sjeffdo
92219820Sjeff{
93219820Sjeff	$doing_params = 0;
94219820Sjeff
95219820Sjeff	# First, look for command line options.
96219820Sjeff	if( $ARGV[0] =~ /-[v|V]/ )
97219820Sjeff	{
98219820Sjeff		$verbose += 1;
99219820Sjeff		shift;
100219820Sjeff		print "Verbose mode on, level = $verbose.\n";
101219820Sjeff		$doing_params = 1;
102219820Sjeff	}
103219820Sjeff
104219820Sjeff	if( !exists $ARGV[0] )
105219820Sjeff	{
106219820Sjeff		print "ERROR: You must specify the files on which to operate, such as '*.c'\n";
107219820Sjeff		osm_check_usage();
108219820Sjeff		exit;
109219820Sjeff	}
110219820Sjeff}while( $doing_params == 1 );
111219820Sjeff
112219820SjeffLINE: while( <> )
113219820Sjeff{
114219820Sjeff	# Skip C single line C style comments
115219820Sjeff	# This line must come before the multi-line C comment check!
116219820Sjeff	if( /\/\*.*\*\// )
117219820Sjeff	{
118219820Sjeff		$in_c_comment = 0;
119219820Sjeff		next LINE;
120219820Sjeff	}
121219820Sjeff
122219820Sjeff	# skip multi-line C style comments
123219820Sjeff	if( /\/\*/ )
124219820Sjeff	{
125219820Sjeff		$in_c_comment = 1;
126219820Sjeff		next LINE;
127219820Sjeff	}
128219820Sjeff
129219820Sjeff	# end skipping of multi-line C style comments
130219820Sjeff	if( /\*\// )
131219820Sjeff	{
132219820Sjeff		$in_c_comment = 0;
133219820Sjeff		next LINE;
134219820Sjeff	}
135219820Sjeff
136219820Sjeff	# We're still in a C comment, so ignore input
137219820Sjeff	if( $in_c_comment == 1 )
138219820Sjeff	{
139219820Sjeff		next LINE;
140219820Sjeff	}
141219820Sjeff
142219820Sjeff
143219820Sjeff	# skip C++ style comment lines
144219820Sjeff	if( /^\s*\/\// )
145219820Sjeff	{
146219820Sjeff		next LINE;
147219820Sjeff	}
148219820Sjeff
149219820Sjeff	# check for bad PRIx64 usage
150219820Sjeff	# It's a common mistake to forget the % before the PRIx64
151219820Sjeff	if( /[^%]\"\s*PRIx64/ )
152219820Sjeff	{
153219820Sjeff		print "No % sign before PRx64!!: $ARGV $.\n";
154219820Sjeff	}
155219820Sjeff
156219820Sjeff	# This simple script doesn't handle checking PRIx64 usage
157219820Sjeff	# when PRIx64 starts the line.  Just give a warning.
158219820Sjeff	if( /^\s*PRIx64/ )
159219820Sjeff	{
160219820Sjeff		print "Warning: PRIx64 at start of line.  $ARGV $.\n";
161219820Sjeff	}
162219820Sjeff
163219820Sjeff	# Attempt to locate function names.
164219820Sjeff	# Function names must start on the beginning of the line.
165219820Sjeff	if( /^(\w+)\s*\(/ )
166219820Sjeff	{
167219820Sjeff		$current_func = $1;
168219820Sjeff		if( $verbose == 1 )
169219820Sjeff		{
170219820Sjeff			print "Processing $ARGV: $current_func\n";
171219820Sjeff		}
172219820Sjeff	}
173219820Sjeff
174219820Sjeff	# Attempt to find OSM_LOG_ENTER entries.
175219820Sjeff	# When found, verify that the function name provided matches
176219820Sjeff	# the actual function.
177219820Sjeff	if( /OSM_LOG_ENTER\s*\(\s*([\-\.\>\w]+)\s*,\s*(\w+)\s*\)/ )
178219820Sjeff	{
179219820Sjeff		$log_func = $2;
180219820Sjeff		if( $current_func ne $log_func )
181219820Sjeff		{
182219820Sjeff			printf "MISMATCH!! $ARGV $.: $current_func != $log_func\n";
183219820Sjeff		}
184219820Sjeff	}
185219820Sjeff
186219820Sjeff	# Check for non-conforming log statements.
187219820Sjeff	# Log statements must not start the log string on the same line
188219820Sjeff	# as the osm_log function itself.
189219820Sjeff	# Watch out for the #include "osm_log.h" statement as a false positive.
190219820Sjeff	if( /osm_log\s*\(.*\"/ )
191219820Sjeff	{
192219820Sjeff		print "NON-CONFORMING LOG STATEMENT!! $ARGV $.\n";
193219820Sjeff	}
194219820Sjeff
195219820Sjeff	# Attempt to find osm_log entries.
196219820Sjeff	if( /^\s*\"(\w+):/ )
197219820Sjeff	{
198219820Sjeff		$log_func = $1;
199219820Sjeff		if( $current_func ne $log_func )
200219820Sjeff		{
201219820Sjeff			print "MISMATCHED LOG FUNCTION!! $ARGV $.: $current_func != $log_func\n";
202219820Sjeff		}
203219820Sjeff	}
204219820Sjeff
205219820Sjeff	# Error logging must look like 'ERR 1234:'
206219820Sjeff	# The upper two digits are error range assigned to that module.
207219820Sjeff	# The lower two digits are the error code itself.
208219820Sjeff	# Error codes are in hexadecimal.
209219820Sjeff	if( /ERR(\s+)([0-9a-fA-F]{2})([0-9a-fA-F]{2})(..)/ )
210219820Sjeff	{
211219820Sjeff		# Check if we already established the error prefix for this module
212219820Sjeff		$err_prefix = $module_err_prefixes{$ARGV};
213219820Sjeff		if( $err_prefix )
214219820Sjeff		{
215219820Sjeff			if( $err_prefix ne $2 )
216219820Sjeff			{
217219820Sjeff				print "BAD ERR RANGE IN LOG ENTRY!! $ARGV $.: $current_func\n";
218219820Sjeff				print "\tExpected $err_prefix but found $2\n";
219219820Sjeff			}
220219820Sjeff		}
221219820Sjeff		else
222219820Sjeff		{
223219820Sjeff			# Create a new prefix for this module.
224219820Sjeff			$module_err_prefixes{$ARGV} = $2;
225219820Sjeff		}
226219820Sjeff
227219820Sjeff		$err_base = $module_err_bases{$3};
228219820Sjeff		if( $err_base )
229219820Sjeff		{
230219820Sjeff			print "DUPLICATE ERR NUMBER IN LOG ENTRY!! $ARGV $.: $current_func: $3\n";
231219820Sjeff			print "\tPrevious use on line $err_base.\n";
232219820Sjeff		}
233219820Sjeff		else
234219820Sjeff		{
235219820Sjeff			# Add this error code to the list used by this module
236219820Sjeff			# The data stored in the line number on which it is used.
237219820Sjeff			$module_err_bases{$3} = $.;
238219820Sjeff			if( $verbose > 1 )
239219820Sjeff			{
240219820Sjeff				print "Adding new error: $1$2 in $ARGV.\n";
241219820Sjeff			}
242219820Sjeff		}
243219820Sjeff
244219820Sjeff		if( $4 ne ": " )
245219820Sjeff		{
246219820Sjeff			print "MALFORMED LOG STATEMENT!!  NEEDS ': ' $ARGV $.\n";
247219820Sjeff		}
248219820Sjeff
249219820Sjeff		if( $1 ne " " )
250219820Sjeff		{
251219820Sjeff			print "USE ONLY 1 SPACE AFTER ERR!! $ARGV $.\n";
252219820Sjeff		}
253219820Sjeff	}
254219820Sjeff
255219820Sjeff	# verify expected use of sizeof() with pointers
256219820Sjeff	if( /sizeof\s*\(\s*[h|p]_/ )
257219820Sjeff	{
258219820Sjeff		print "SUSPICIOUS USE OF SIZEOF(), DO YOU NEED AN '*' $ARGV $.\n";
259219820Sjeff	}
260219820Sjeff
261219820Sjeff
262219820Sjeff}
263219820Sjeffcontinue
264219820Sjeff{
265219820Sjeff	# reset the module base error index when we finished out
266219820Sjeff	# each source file.
267219820Sjeff	if( eof )
268219820Sjeff	{
269219820Sjeff		# reset the base error value, since each module can
270219820Sjeff		# repeat this range.
271219820Sjeff		%module_err_bases = ();
272219820Sjeff		# closing the file here resets the line number with each new file
273219820Sjeff		close ARGV;
274219820Sjeff	}
275219820Sjeff}
276219820Sjeff
277219820Sjeffsub osm_check_usage
278219820Sjeff{
279219820Sjeff	print "Usage:\n";
280219820Sjeff	print "osm_check.pl [-v|V] <file list>\n";
281219820Sjeff	print "[-v|V] - enable verbose mode.\n\n";
282219820Sjeff}
283