US 5,293,629 AGrant
Method of Analyzing Computer Source Code
Issue Date:1994-03-08
•10 Claims
•1 Drawing Sheets
Abstract
A method of analyzing computer source code provides a determination of the extent to which the source code complies with guidelines relating to, for example, style, maintainability, and portability. The method includes defining a Boolean expression (12) that is assigned a TRIGGER logic state in accordance with the presence in the source code of a condition relating to a particular guideline. A guideline support computer program is also defined (14) and is executed whenever the Boolean expression is assigned the TRIGGER logic state. The guideline support program can perform a variety of operations including notification of noncompliance with the predefined guideline.
Metadata
Assignee
- Abraxas Software, Inc.
Inventors
- Patrick O. Conley
- Loren Cobb
Application Information
Application Number:US 6212484
Filing Date:1990-11-30
Priority Date:1990-11-30
Art Unit:236
Classifications
IPC:
G06F 944
Field of Search:
395364700;52;650513
Patent Drawings (1 sheets)
Description
Technical Field
The present invention relates to a method of analyzing computer source code and, in particular, to such a method in which compilable computer source code is analyzed to determine whether it complies with guidelines concerning, for example, style, maintainability, and portability to different compiler and processor environments.
Background of the Invention
Most computer programs are written as source code in a high-level programming language such as the C language. Typically, the program may be written in many different ways to obtain the same overall result. The different ways of writing such a program reflect different balances between various constraints that include the speed at which the program is to operate, the programmer's or programming manager's sense of style, the importance of maintaining or updating the program over time, and the portability of the program from the processor and compiler environment in which it is written to other processor or compiler formats.
To maximize the long-term usefulness of source code, software development managers often establish guidelines for the style, maintainability, and portability of the source code to be written. Style guidelines are important in large programs, for example, to ensure consistent format in the program modules or sections written by different programmers.
Portability guidelines address the compatibility of a particular program with different types of computer, compiler, or operating system. For example, the C language has several main "standards" and is implemented in different computers and operating systems with many nonstandard features. Portability guidelines can help prevent a programmer from incorporating into the source code nonstandard or unique features of the particular C language "standard" in which a program is written. As a result, the source code would be compatible with different computers, compilers, and operating systems despite being written in one particular environment.
Maintainability guidelines address the ability to update, correct, or modify the source code sometime after it is originally written. Some programmers tend to write source code that is very abstract, complex, and compact. Such source code can be extremely difficult to maintain. Maintainability guidelines typically require that source code be written with a particular degree of simplicity and clarity to facilitate later modification. As a result, maintainability guidelines can reduce the need to discard a program and write a completely new one.
Compliance with programming guidelines is typically monitored by manual inspection of source code. For example, the source code may be read by the manager of a software development group or by other programmers within the group. Automatic (i.e., computer-executed) source code analysis is typically limited to the detection of source code errors that prevent compilation. Such automatic source code analysis is further limited in that the errors to be detected are typically fixed by the requirements of the compiler, thereby being unadaptable to guidelines concerning style, maintainability, and portability.
Summary of the Invention
An object of the present invention is, therefore, to provide a method of analyzing computer source code for compliance with particular guidelines.
Another object of this invention is to provide such a method that is performed automatically by a computer.
A further object of this invention is to provide such a method that is programmable by the user.
The present invention includes a method of analyzing computer source code to determine the extent to which it complies with guidelines relating to, for example, style, maintainability, and portability to different processing and compiler environments. The method is automatically executed by a computer and allows the user to select the particular guidelines.
The method includes defining guidelines for conditions that might arise in the source code. A Boolean expression is defined in a computer programming language and is assigned a logic state in accordance with the presence or absence of the condition in the source code. For example, a style guideline could require a particular indentation (e.g., two tab spacings) for each function definition. A Boolean variable, which is a simple Boolean expression, could be assigned a particular logic state based upon the presence of a function definition in the source code. Accordingly, the Boolean variable would be assigned a TRIGGER logic state whenever a function definition is present in the source code and would be assigned a NO TRIGGER logic state otherwise.
A guideline support computer program is also defined in the programming language and is executed whenever the Boolean variable is assigned the TRIGGER logic state. The logic state of the Boolean variable functions, therefore, as a trigger condition that controls the execution of the guideline support program.
The guideline support program can perform a variety of operations. With reference to the function indentation example, the guideline support program can measure the indentation of a function definition, determine whether the indentation differs from two tab spacings, and, if appropriate, deliver a notice of noncompliance to an analysis report file that displays all such notices when the analysis of the source code is completed.
In operation, the trigger condition and the guideline support program are compiled to form a compiled analysis program. The compiled analysis program is then executed automatically by a computer to analyze the source code. During the analysis, the source code is checked at applicable points in the source code, which are called checkpoints, for compliance with the trigger conditions in the compiled analysis program. With reference to the indentation example, function definitions are typically located in a definition section of the source code. Accordingly, the definition section of the source code is the checkpoint for determining whether a function definition is present.
The execution of particular guideline support programs in response to identified trigger conditions is analogous to the organization of a forward-chaining expert system. Such forward-chaining organization is beneficial because it greatly increases the speed at which the analysis program can be executed. The method of the present invention and conventional expert systems differ, however, in how they respond to a trigger condition. A conventional expert system merely reaches a factual conclusion, whereas the present invention executes a guideline support computer program.
Additional objects and advantages of the present invention will be apparent from the following detailed description of a preferred embodiment thereof, which proceeds with reference to the accompanying drawings.
Brief Description of the Drawings
FIG. 1 is a flow diagram showing a method of preparing a source code analysis program.
FIG. 2 is a flow diagram showing a method of analyzing compilable source code.
Detailed Description of Preferred Embodiment
FIG. 1 is a flow diagram showing a method of preparing a computer source code analysis program in accordance with the present invention. Process block 10 indicates that a user such as, for example, a manager of a software development organization, defines criteria or guidelines for the computer source code to be written by members of the organization. The user may define any number of guidelines, a large number of guidelines being directed to complete analysis of the source code and a small number of guidelines being directed to inspection of particular aspects or features of the source code. The guidelines preferably relate to particular aspects of or conditions concerning the style, portability, and maintainability of the source code. The guidelines may also be used to measure the size, logical complexity, or density of the source code.
As examples, the guidelines can be used to detect overly complex expressions; detect functions and declarations not explained by comments; monitor compliance with size standards for statements, functions, and modules; and determine whether the source code is portable to different nonstandard compilers for a particular programming language. With regard to the C language, for example, portability could be determined with regard to the "standard" compiler formats of Kernighan & Richie, Harbison & Steele, and ANSI C, as well as different compilers such as Microsoft.RTM. C, Quick.RTM. C, Turbo.RTM. C, VAX.RTM. C, AT&T.RTM. C++, and Apple.RTM. MPW C. In addition, guidelines can be selected to identify "silent" errors, which are typically valid programming instructions that are based upon unconfirmed, possibly false assumptions that can cause program execution errors.
The computer source code analysis program can include predefined guidelines residing in the program, as well as guidelines defined by the user. Accordingly, the guidelines defined in process block 10 can be defined by the user as described or by a programmer writing the source code analysis program and incorporating into it the predefined guidelines.
Process block 12 indicates that a Boolean expression indicative of the condition is defined in a programming language, which is preferably the C language. The Boolean expression may include a single Boolean variable or multiple numeric or Boolean variables in a logic expression. The Boolean expression is assigned a TRIGGER logic state whenever the condition is present within the source code being analyzed and is assigned a NO TRIGGER logic state otherwise.
For example, a style guideline could require that the beginning of each function definition have an indentation of two tab spacings. Accordingly, a single Boolean variable "fcn.sub.-- begin" could be defined to assume the TRIGGER logic state whenever the beginning of a function definition is located in the source code. Alternatively, a Boolean expression "fcn.sub.-- begin AND [lin.sub.-- indent.sub.-- tabs>2 OR lin.sub.-- indent.sub.-- tab <2]" could be defined with the "fcn-begin" variable and a "lin.sub.-- indent.sub.-- tab" variable that is assigned the number of tab spacings preceding the first non-white, non-comment character in the line. This Boolean expression assumes the TRIGGER logic state whenever a function definition is indented by an amount other than two tabs.
Process block 14 indicates that a guideline support program is defined in the C language. The guideline support program may perform a variety of functions whenever the Boolean expression indicates that the condition is present in the source code. For example, the guideline support program may provide notification to an analysis report file, count the number of occurrences of the condition or related conditions to perform a statistical analysis of the conditions, or access a different analysis program.
Execution of the guideline support program is initiated whenever the Boolean expression assumes the TRIGGER logic state. Accordingly, the Boolean expression functions as a trigger for the execution of the guideline support program. The Boolean expression together with its associated guideline support program is called a "rule."
With regard to the example of the function definition indentation, different guideline support programs could be used in accordance with the different Boolean expressions used to trigger the program. If the variable "fcn.sub.-- begin" is used to trigger the guideline support program, the program could include the steps of measuring the indentation of the function definition, determining whether the indentation differs from two tab spacings, and, if appropriate, delivering a notice of noncompliance to an analysis report file that displays all such notices when the analysis of the source code is completed. If the expression "fcn.sub.-- begin AND [lin.sub.-- indent.sub.-- tab>2 OR lin.sub.-- indent.sub.-- tab<2]]" is used to trigger the guideline support program, the program could be as simple as delivering a notice to an analysis report file or temporarily stopping the analysis of the source code and immediately displaying the notice.
Process block 16 indicates that the Boolean expression and associated guideline support program, which are expressed in the C language, are compiled by a host computer (not shown). Compilation of the rule allows it to be executed very quickly by the host computer when analyzing computer source code.
Process block 18 indicates that the compiled analysis program is executed by a host computer to analyze a source code program. The execution of the compiled analysis program is described in greater detail with reference to FIG. 2.
The variables defined in process block 12 may be indicative of a wide variety of conditions in the computer source code. The conditions and, therefore, the variables, may be classified in accordance with different hierarchical levels of a computer program. In particular, the variables may be classified as lexical variables, preprocessor variables, declaration variables, operator variables, expression variables, line variables, statement variables, function variables, module variables, or project variables.
The classification of the variables in accordance with the hierarchical levels of the source code allows the analysis program to search for source code conditions in a level-by-level manner. Each variable and related condition occur at a particular source code level of the source code hierarchy. While checking or analyzing the source code, the analysis program searches only for the conditions corresponding to the particular level being analyzed. For example, the analysis program searches for conditions represented by declaration variables only when the declaration level of the source code is being analyzed.
The hierarchical levels of the source code represent, therefore, checkpoints at which the analysis program searches for particular conditions. The use of checkpoints in the analysis of the source code increases the speed at which the analysis program can analyze source code. The increase in analysis speed is achieved because the analysis program searches only for conditions that are applicable to the checkpoint being analyzed. Searches for other conditions are delayed until their corresponding checkpoints are analyzed. This delayed searching functions as a delayed triggering of the corresponding guideline support programs.
Table 1 (set forth at the end of this specification) is a listing of preferred predefined variables incorporated into the source code analysis program of the present invention. The variables are listed according to hierarchical class, with an explanation of the source code conditions on which the variables are based. The explanations of many variables state that they are "set to 1" under certain circumstances. The logic state 1 referred to in Table 1 typically corresponds to the TRIGGER state.
It will be appreciated that many of the specific symbols or terms (i.e., conditions) to which the variables in Table 1 are directed are particular to the C language. A source code analysis program directed to another language would, therefore, employ variables directed to different conditions. The C language symbols and terms recited in Table 1 are known by persons skilled in the art and are therefore not defined herein.
Lexical variables refer to the tokens used to write or express the source code. Such tokens include, for example, names, numbers, punctuation marks, and alphabetic characters. Every lexical variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each token.
Preprocessor variables refer to compiler commands positioned at the beginning of the source code as instructions to the compiler. Such instructions include, for example, defining a constant that is used in the source code or directing the compiler to incorporate the contents of another source code listing into the present program. Every preprocessor variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each preprocessor directive.
Declaration variables refer to "declarators" that declare the name, type, and initial value of a source code variable or function. Every declaration variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each declarator. Whenever the declarator assigns an initial value to a source code variable, reinitialization of the declaration variable also occurs at the end of the scan of the assignment.
Operator variables refer to symbols that specify how an operand or operands of an expression are algebraically or logically manipulated. Operators may be arithmetic symbols such as + or .div., or logical operators such as AND or OR expressions. Every operator variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each token. As a result, the reinitialization of operator variables is similar to that of lexical variables.
Expression variables refer to combinations of operands and operators that yield a single value or logic state. An expression could include, for example, an arithmetic equation or a logic expression. Every expression variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each statement.
Line variables refer to the characteristics of a line of source code. Every line variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each line. The end of a line is typically marked by a "newline" character.
Statement variables refer to the characteristics of source code statements, which are defined broadly to include preprocessor directives, declarations, program-control keywords, expressions, and function calls. In the C programming language, simple statements end with a semicolon and compound statements are collections of simple statements enclosed by braces (i.e.,"{ }"). Every statement variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each statement.
Function variables refer to the characteristics of source code functions, which have collections of declarations and statements that have a unique name and can return a value. Every function variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each function.
Module variables refer to the characteristics of modules, which are independently compilable source code files that cooperate with others to operate as a computer program. Modules differ from "header files" in that the former are typically substantially complete programs and the latter typically contain only commonly used declarations and definitions. Every module variable is initialized in the NO TRIGGER state when the analysis program is executed and is reinitialized to the NO TRIGGER state at the end of the scan of each module.
Project variables refer to the characteristics of entire projects, which can include multiple modules. Every project variable is initialized in the NO TRIGGER state and is reinitialized to the NO TRIGGER state at the end of the scan of an entire project.
The variables used by the source code analysis program are defined globally to simplify the organization and operation of the program. Similarly, the variables are preferably limited to simple types, such as integer variables, floating point variables, character variables, and literal strings or arrays of characters.
Some of the variables listed in Table 1 are used for measurement or counting purposes. The measurements or counts assigned to these variables may be incorporated into a Boolean expression to function as a trigger condition. In addition, the values of a variable may be stored to facilitate the calculation of statistical values (e.g., means and medians) representing the characteristics of the source code program being analyzed. Such variables are designated in Table 1 as being statistic variables.
To use the values of a statistic variable in statistical calculations, it is necessary to store each of the values assigned to the statistic variable. The statistical calculations may then be performed by a guideline support program created by a user or by invoking an analysis function that resides in the source code analysis program. It will be appreciated that the values of the nonstatistic variables are typically not stored beyond the reinitialization of the variable at the completion of the analysis at a corresponding checkpoint.
Table 2A (set forth at the end of this specification) is a listing of the functions that reside within the source code analysis program. The functions are typically incorporated into the guideline support program of a user-defined rule. Table 2A provides for each of the listed functions a statement of the arguments on which the function acts and a brief description of the function. Table 2B explains the notation of the arguments listed in Table 2A.
The source code analysis program also recognizes a predefined constant that may be incorporated into the source code being analyzed. The constant functions to identify for the source code analysis program sections of the source code that are not to be analyzed. For example, the predefined constant could precede assembly language code that is intermixed with C language program code that is being analyzed.
Execution of the source code analysis program is initiated by a command line of the form:
check -options mysource.c
in which "check" invokes the source code analysis program, "mysource.c" refers to the name of a C language source code file to be analyzed, and "-options" identifies the rules to be used by the analysis program. If no options are specified, the source code analysis program defaults to the set of rules most recently used. Table 3 (set forth at the end of this specification) lists predefined options that reside in the analysis program.
FIG. 2 is a flow diagram showing the execution of the source code analysis program referred to in process block 18 of FIG. 1. In particular, process block 50 indicates that the trigger variables defined in process block 12 of FIG. 1 are associated with their particular classes and checkpoints. It will be appreciated, therefore, that the trigger variables and their associated guideline support programs need not be defined in any particular order or sequence as would be the case in a conventional computer program. Such flexibility in the definition of the analysis rules greatly simplifies the use of the source code analysis program.
Process block 52 indicates that the host computer scans the source code being analyzed.
Decision block 54 represents an inquiry as to whether the scan of the source code identifies a checkpoint corresponding to one of the classes of trigger variables identified at process block 50. Whenever the source code does not correspond to such a checkpoint, decision block 54 returns to process block 52. Whenever the source code does correspond to such a checkpoint, decision block proceeds to process block 56.
Process block 56 indicates that the conditions corresponding to the variables associated with the identified checkpoint are analyzed by the host computer. The analysis is executed for each of the variables applicable to the checkpoint.
Decision block 58 represents an inquiry as to whether any of the variables analyzed at the checkpoint is assigned the TRIGGER state. Whenever none of the variables is assigned the TRIGGER state (i.e., all variables remain in the NO TRIGGER state), decision block 58 returns to process block 52. Whenever any of the variables is assigned the TRIGGER state, decision block 58 proceeds to process block 60.
Process block 60 indicates that the guideline support programs associated with the trigger variables assigned the TRIGGER state are executed.
Process block 62 indicates that the variables assigned the TRIGGER state are reinitialized. Process block 62 returns to process block 52.
It will be obvious to those having skill in the art that many changes may be made to the details of the above-described preferred embodiment of the present invention without departing from the underlying principles thereof. The scope of the present invention should be determined, therefore, only by the following claims.
Table 2a
float corr(statistic x, statistic y)
This function returns the correlation of its two argument variables. The argument variables must have the same number of cases. Pearson's product-moment correlation is returned.
void define(char name[ ], int n )
This function defines a macro with the specified name, and Dgives it the specified numeric value n.
void exit(int n )
This function causes an immediate exit from the source code analysis program returning the error number n to the operating system.
void fatal(int n, char message[ ] )
This function prints an error number and message to "stderr[ and then exits the source code analysis program, returning the error number n to the operating system.
char * file.sub.-- name(void )
This function returns the name of the file that is currently being checked. See also the functions: mod.sub.-- name(), prj.sub.-- name().
char * fcn.sub.-- name(void )
This function returns the name of the function that is currently being checked.
int identifier(char name[ ] )
This function is designed to be used as a trigger in an analysis program rule. It returns the value 1 whenever a new identifier has been encountered that matches its argument string. See also: keyword().
int keyword(char name[ ] )
This function is designed to be used as a trigger in an analysis program rule. It returns the value 1 whenever a Kernhigan & Ritchie C, ANSI C. or C++ keyword has been encountered that matches its argument string. See also the function: identifier().
char * lib.sub.-- name(void )
This function is designed to be used as a trigger in an analysis program rule. It returns the filename whenever a library header file is opened with the "#include <filename>" directive.
float maximum) statistic x)
This function returns the maximum value of its statistic argument variable.
float mean(statistic x )
This function returns the mean (i.e., arithmetic average) of its statistic argument variable.
float minimum(statistic x )
This function returns the minimum value of its statistic argument variable.
char * mod.sub.-- name(void )
This function returns the name of the module that is currently being checked. A "module" is a C source file and all of its
header files. Its name is the name of the first source file in the module. See also the functions: file.sub.-- name(), prj.sub.-- name().
int ncases(statistic x )
This function returns the number of cases recorded for its statistic argument variable.
int option(char c )
This function returns 1 if the command line option specified by the variable c is in effect (i.e., has been specified by the user), otherwise it returns 0.
void printf(char format[ ], . . . )
The printf function is modelled after the C "stdio" function "printf". The argument variables may be of type int, float, char, char[ ], or char *.
char * prj.sub.-- name(void )
This function returns the name of the project that is currently being checked (i.e.. the name of the .prj file that identifies the component modules of a project to the analysis program.) See also the functions: mod.sub.-- name(). file.sub.-- name().
void reset(statistic x )
This function resets its statistic argument variable. All recorded cases are erased, and the case count is reset to zero.
void set.sub.-- option(char c, int n )
This function assigns the value n to the command line option specified by the variable c. For example, when set.sub.-- option("B",1) is executed by the analysis program, all succeeding rules are evaluated as if the user had specified the -B option (Table 3) on the command line.
float stdav(statistic x )
This function returns the standard deviation of its statistic argument variable.
void undefine(char name[ ] )
This function undefines the macro with the specified name.
float variance(statistic x )
This function returns the variance of its statistic argument variable.
void warn(int n, char message[ ] )
This function prints an error number and message to the "stderr" stream, together with the filename and line number of the C source which triggered the message. If a listing file is open, then the warning message is echoed in the listing file with a marker indicating the position of the error.
Claims
What is claimed is:
1. A method of analyzing computer source code having characteristics of maintainability, portability, and style and being organized as plural hierarchical program levels, to detect noncompliance with a predefined criterion relating to at least one of the characteristics, comprising: defining a source code trigger condition relating to the predefined criterion; assigning the source code trigger condition to a condition class corresponding to one of the hierarchical program levels; defining in a selected programming language a criterion support program that supports the predefined criterion; directing a computer to review the computer source code to determine whether the source code trigger condition is present therein; and whenever the source code trigger condition is present in the computer source code, executing the criterion support program to support maintenance of the predetermined criterion in the computer source code.
2. The method of claim 1 in which the source code trigger condition is expressed in the selected programming language.
3. The method of claim 2 in which the selected programming language is a C language.
4. The method of claim 1 in which the source code trigger condition is expressed as a Boolean condition in the selected programming language.
5. The method of claim 4 in which the source code trigger condition and the criterion support program are compiled.
6. The method of claim 1 in which the review of the computer source code is directed to the hierarchical program level corresponding to the condition class to which the source code trigger condition is assigned.
7. The method of claim 4 in which the review of the computer source code is directed to the hierarchical program level corresponding to the condition class to which the source code trigger condition is assigned.
8. The method of claim 7 in which the Boolean condition is initialized in a first logic state when review of the computer source code begins and is reinitialized to the first logic state after each review of the computer source code directed to the hierarchical program level.
9. The method of claim 4 in which the Boolean condition and the criterion support program are compiled to increase the speed at which the computer source code is analyzed.
10. The method of claim 1 in which execution of the criterion support program includes delivering notification to a notification file residing within the computer whenever the source code trigger condition is present.