Lint static source code Check Tool

Source: Internet
Author: User

Note: I have never known that there are tools for static checks.ProgramOr not, reprinted for reference.

Lint tool is a software quality assurance tool. Many large foreign professional software companies, such as Microsoft, use it as a program inspection tool, ensure that the lint check is passed before the program is integrated into the trial version or delivered for testing. They require software engineers to turn on all the compilation switches when using lint, if you must turn off some switches, you must give a legitimate reason to turn them off.
It is conceivable that if we use lint to check the program when compiling the program for the first time after encoding and ensure that all lint alarms are eliminated, we will not encounter so many alarms as today. Even today, if we can spare some effort to eliminate the lint alarm in the program, it is easy to maintain this alarm-free status in the future. The improvement of program quality is self-evident.
PC-LINT is a product of gimpel software, where the content is very extensive, there are more than 300 options, involving all aspects of program compilation and syntax use. The purpose of this training material is to guide readers to get started, learn the basic use of PC-LINT, play a valuable role, let readers start from here to continue to study how to skillfully use the various options of PC-LINT, it can fully serve our development work.
1. Overview
If you want to define the next image point of the lint tool, it is a more rigorous compiler. It not only checks common syntax errors as common compilers do, but also checks errors that, although fully compliant with syntax requirements, are likely to be potential and hard to find. See the following example:
1:
2: char * Report (int m, int N, char * P)
3 :{
4: int result;
5: char * temp;
6: Long nm;
7: int I, K, KK;
8: Char name [11] = "Joe jakeson ";
9:
10: Nm = N * m;
11: temp = P = ""? "Null": P;
12: for (I = 0; I 13 :{
14: K ++;
15: Kk = I;
16 :}
17:
18: If (k = 1) Result = Nm;
19: else if (KK> 0) Result = 1;
20: else if (KK <0) Result =-1;
21:
22: If (M = Result) Return (temp );
23: else return (name );
24 :}
The aboveCode Compiling with a general compiler is a piece of effective code, but there are several warnings when compiling with a PC-LINT. First, the NUL character is lost when the first row assigns a value to the name array. The multiplication precision of the second row will be inaccurate. The comparison of the second row is incorrect. The variable k of the second row is not initialized, the KK of row 15th May not be initialized, and the result of row 22nd May not be initialized. Row 23rd returns the address of a local object. This code can be compiled smoothly in most compilers. Manual debugging is required to continue searching for errors. If the program is large, it will be a cumbersome task, no one can be sure to find out all this type of problem, but the PC-LINT can only be done through a simple compilation, obviously saving us a lot of development time.
Next let's take a look at how to install and use PC-lint.
2. How to install PC-LINT
PC-LINT software installation process is more complex, more options, the following according to the installation process, one by one to describe the meaning of each step.
0) if it is a zip file, expand the zip installation file to the directory c: \ lint. in INS, Enter command prompt, first line Directory ing subst g: C: \ lint. INS, and then go to G: To execute install. The other steps are the same as the following steps for installing a floppy disk.
1) insert the PC-LINT installation disk in A: Drive, enter a: \> install command, enter the start installation bar, press any key to continue, enter the PC-LINT introduction bar, and then press any key to continue.
2) enter the environment selection column, which has three options:
Windows NT/Windows 95
The MS-DOS (DOS extended)
OS/2 (32bit)
If Win95, win97, Win98, or WINNT is installed on your computer, select Windows NT/Windows 95. If
Select dos for only dos.
3) enter the installation directory selection bar. c: \> Lint is recommended. If you do not want to install it in this directory, enter the directory you want to install and press enter to confirm, if the directory to be installed does not exist, it will prompt you to create this directory. Here we select c: \> lint
4) Select the disk drive where the installation disk is located. Here, select:
5) determine whether to select multiple compiler or compilation library configurations.
Int, select Yes; otherwise, select No. Then press enter to confirm.
6) you can see a compiler list and select your own compiler in this table. If the table does not have your own compiler, you can select the General compiler: Generic compilers. Press enter to confirm. This option is reflected in the co-xxx.lnt file.
7) then, the installation program will allow you to select a memory model. You can select an appropriate memory model based on the actual size of your program and data zone. If the CPU is a 32-bit 68k series, select 32-bit flat module.
The options of the memory model are reflected in the STD. LNT file.
8) After selecting the memory model, you will see a list of database types. Here, select one or more libraries used for compilation. This option is reflected in the LIB-xxx.LNT file.
9) then let you select the author who has made important suggestions for using C ++ programming. After selecting an author, the options for his programming recommendations will be turned on. The options related to the author selection are reflected in the AU-xxx.LNT file.
10) the next step is to set the directory containing files. There are two options. The first option is to use the environment variable include. The environment variable is set in the batch processing file. After the environment variable, each directory is separated by a semicolon. For example, you can set "include = C: \ MRI \ mcc68k; D: \ lap \ SRC \ Inc ". The second option is to use the-I option, and the-I option is embodied in STD. in the LNT file, each directory is guided by-I, and the directories are separated by spaces. For example, you can set "-ic: \ MRI \ mcc68k-ID: \ lap \ SRC \ Inc ". If you select the-I option, the installer will then ask you to enter the directory containing the file.
11) If you have selected multiple compiling environments, you will be asked if you want to select more compiling environments. If yes is selected, the repeat will be started from step 1. If no is selected, the compiler selection is terminated.
12) An options file that reflects the display of global compilation information will be generated. LNT: The file is generated in two ways. One is that the installer explains several core options one by one and asks if you want to cancel the option. If you choose to cancel the option, it will be reflected in options. in the LNT file, the specific embodiment is to add-E before the information encoding, followed by 13th ~ Step 18 is the process of selecting core options one by one. If you select the second option, the installation file will generate an empty options. LNT file, and you will add the necessary options in the future.
13) Whether to disable the Boolean test alarm when assigning values, such as: if (a = f ()){...
14) Whether to disable the non-matching alarm between the signed amount and the unsigned amount during the assignment. Normally, this assignment will not cause problems. When you choose to disable the alarm information, alarms for mixed operations between other types of signed and unsigned volumes are still enabled.
15) When an integer is assigned to a smaller value than it, the latter will lose its precision. For example, assign an int value to a char. This step allows you to choose whether to disable this type of alarm.
16) whether to turn off the alarm with the left shifting symbol volume. Usually the PC-LINT will generate an alarm for all the movement of the amount of symbols, but the right shift is generally determined by a different CPU to determine whether to move the symbol bit into, left shift is generally will not produce any problem, you can disable the alarm.
17) calling a C function before it is defined or declared does not always produce errors. You can choose whether to disable this alarm option here. This option does not work for C ++ programs.
18) Whether to disable the alarm of "calling a nonexistent function prototype. Some programmers do not want to follow the strict original function definition conventions, but the PC-LINT will generate an alarm when calling an undefined function prototype, where you can choose to turn off the alarm.
19) after confirming the format of the options. LNT file through the above steps, select the compiling environment. PC-LINT provides the ability to work in a variety of development environments, for example, in VC, BC, source insight. Let us select Source insight here. After selecting this option, the installer will continue to ask if you want to select another environment. You can select one or more development environments based on the actual situation of your application. The selection of development environments is documented in the env-xxx.lnt file.
20) the installer generates a Lin. the bat file, which is a batch file that runs the PC-LINT, provides two ways for you to choose to run the file in any path. The first method is to let you choose to copy Lin. BAT to any path directory. When you run the lcopy. BAT file after installation, Lin. bat will be copied to your specified directory. The second method is to generate an lset. BAT file, run it to set the path each time before using the PC-LINT, or copy the content of the lset. BAT file to the autoexec. BAT file.
21) after the installation program is executed, the first thing is to execute the lcopy. BAT file in your installation directory. It copies some files from the installation disk to the installation directory, and sets the parameters in the file according to your selection during the installation process.
3. Lint a c file
3.1 Use the command line method for lint
If Lin. bat batch processing file lint, before lint should first look at the content of the batch processing file, contains the LINT-NT command and Command Options, you can modify, increase or decrease the options according to your own requirements. We can see that a STD is nested in this batch of commands. LNT file, in STD. the LNT file also contains Co. LNT, options. LNT and lib-stl.lnt files, in principle *. the LNT file can be nested without restriction. This type of file generally contains lint options. You can modify these files to modify the lint options. The options are executed in the order from left to right. Run the following command:
C: \ ABC \ SRC> Lin Alpha. C Beta. C gamma. c
Generally, for a program composed of multiple C modules, it is best to perform a lint check on each C Module unit first. When the unit Lint is executed, the following operations can be performed:
C: \ ABS \ SRC> Lin-u Alpha. c
Here, "-U" is the unit option. After "-U" is used, you can disable some alarms generated when multiple modules are checked, for example, "the function is not used" or "the function is not defined.
You can also directly use the lint command instead of using Lin. BAT to process files in batches. The lint command is lint. EXE in DOS, LINT-NT.EXE in Windows95/NT, and LINT-OS2.EXE in os2. To directly use the lint command, you must set the path of the lint directory before use. The best way is to add the path to the autoexec. BAT file. Other methods are the same as those for batch files. For example:
C: \ ABS \ SRC> lint-NT-ic: \ lint \ STD. LNT-OS (_ lint. tmp) *. c
3.2 use the development environment for lint
You can also use the development environment to execute lint operations. Generally, the development environment supports running executable files, but not necessarily supports running batch files. The following uses source insight, ultra edit, msvc 6.0 illustrates how to implement lint in the development environment.
3.2.1 integration in source insight
If you choose to use a development environment during installation, the installer will generate a env-xxx.lnt file under the directory you install, for example, there will be a env-si.lnt file when you select Source insight. Open the file in the editor and explain in the comments at the beginning of the file how to integrate PC-LINT functionality into the development environment, the process of integration into source insight is as follows:
1) Select the "m commands" command item from the Options menu.
2) Enter "PC-Lint" in the name column. In principle, this name can be used as long as you can understand its meaning.
3) In the run column, enter "C: \ lint-NT-u-ic: \ lint STD env-Si % F", where C: \ Lint is the installation directory for your PC-LINT.
4) Select "iconic window" and "Capture output" in the output column ".
5) Select "save files first" in the control column ".
6) in the source links in output column, select "parse links in output", "file, then line ".
7) in the pattern column, enter "^ \ ([^] * \) \ ([0-9] + \)".
8) Click Add to add the command. For example:
9) Open the lint file under source insight and open the "Custom commands" command item in the Options menu, in the "command" column, select the "PC-Lint unit check" command to run.
I noticed that the parameters in the "run" column are different from the above prompts. In fact, all my other weird parameters are put in c: \ lint \ STD. LNT. Please note that no matter how you configure the parameters, you must not forget to include the si-env.lnt in your configuration file, otherwise you will not be able to make the error message and the automatic correspondence of the program.
For ease of use, you can also configure the menu button to add it to the System Menu 0. This is a general
The source insight application is not described here.
Second, when I use source insight in NT, it seems that the integration fails. The reason is unknown. The above example is successfully tested under Win 95.
If you want to modify the lint option, you can directly modify it in the run column or edit a special *. put the LNT file in the C: \ lint directory and add the file name to the run column, which is the same as the command line.
3.2.2 integration in Ultra Editor
Select menu | advanced | tool configuration..., as shown in:
1) Click "insert ",
2) Enter c: \ lint-nt c: \ lint \ STD. LNT % F in command line :.
3) Fill in: PC-LINT in menu item
4) In command output, select (x) output to list box and (x) Capture output.
5) Click "OK"
The configuration is successfully tested under ue6.0/NT 4.0.
3.2.3 integration in msvc 6.0
The basic principles are the same:
1) Select menu | tools | customize .....
2) Select the Tools tab:
3) Click the dotted box above the main dialog box to create a tool item.
4) input name: PC-LINT
(5) input command: C: \ lint \ lint-nt.exe
6) Input arguments: C: \ lint \ STD. LNT $ (filepath)
7) Select (x) Use Output Window
8) Close
After that, there is a PC-LINT option under the Tools menu.
The following figure shows the tool configuration in the case of vc6/Win NT 4.0:
3.3lint options
Lint options can be placed in comments, for example:
/* Lint option1 option2... optional Commentary */
// Lint option1 option2... optional commentary
The options must be separated by spaces. The lint command must be in lowercase and followed by/* Or/. spaces are not allowed. If the option consists of parts similar to operators and operands, such as-esym (534, printf, scanf, operat or new), the last option is Operator new, therefore, there can be only one null value between operator and new.
Grid.
Options can also be placed in macro definitions, for example:
# Define divzero (X)/* lint-save-e54 */(X)/O)/* lint-Restore */
Many lint options include over 300, which can be divided into the following categories:
1) Options for disabling error messages
This option is used to prohibit generation of certain types of error information. The most common options are-E and + E.-E is to prohibit generation of certain types of error information, and + E is to restore generation of certain types of error information. You can run msg.exeunder the lintdirectory to get the msg.txt file. This 5000-row-long file contains all the error message numbers and explanations.
-W is not displayed for all alarms greater than the level.
-Wlib () does not display any warning information about the number of library functions that is greater than the level. We can use-wlib (0) to shield the warning information of all library functions.-wlib (1) only displays syntax errors in library functions.
-Esym (#,) can block an alarm message for a specific symbol.
2) variable type and size options
The size of variable types (such as short integer and integer) varies with the target machine and compilation system. This type of option is used to set the variable type size for the target machine. Because the default settings match most compilers, these special settings are usually not needed and are only used in special target machine structures. For example, for an m68000 target machine, its int type and pointer type are usually 32bit. In this case, you should use the option:-Si4
-SP4. The current value of these size parameters can be obtained through the help screen. For example, you can enter the following command line:
Lin-Si4-SP4?
3) lengthy information options
Lengthy information refers to some information generated in the lint process related to the compilation process, rather than the real alarm information and error information. Whether to generate the information can be determined by the-V and + V Options. + V is used to generate the information, and-V is used to disable the information. In this set of options, except for + V, all other options can disable the + V option.
4) Tag options
The options starting with + F, ++ F,-F, and -- F are tag options. Their logical meanings are as follows:
+ F...: Set the flag to 1 and set it to on
-F...: Set the flag to 0 and off.
+ + F...: the flag is increased by 1.
-- F...: Mark minus 1
The next two are used when you want to set a flag to on locally without affecting global settings. For example, you can use:
/* Lint ++ flb */
Int printf ();
/* Lint -- flb */
There are many types of tag options. The basic meaning is to enable or disable certain types of syntaxes. For example, you can use abbreviation struct names, anonymous consortium, and C ++ compiling for all modules.
5) message display options
The message display option defines the message output format. The options include message height, message width, and Message format.
6) Other Options
There are many other options and there are great differences among them. I will not introduce them here. I suggest you take a look at PC-lint. Chapter 5 provides a detailed description of each option. Lint itself also has some instructions, lint-NT 2> lint.txt, and then press a few carriage returns to generate a description file of the lint option.
4. Lint multiple C files under a project
4.1 why lint multiple C files
In the initial stage of program coding, we may only care about the syntax issues in a single C Module. After the programming stage, for programs composed of multiple C modules, we want to know whether there are Syntax problems between modules after multiple modules are connected together. At this time, although the compiler can give some warnings, but the PC-LINT connection can give more warnings. In addition, when we can ensure that several of the modules are relatively stable and the other modules are still faulty, We can first compile several stable modules and connect them into a target file, after each modification, the file is compiled separately and then connected to the total target file.
4.2 How To lint multiple C files under a project
Like the compilation tools we usually use, the PC-LINT will first compile each c file to generate an intermediate target file when compiling and connecting multiple C files *. and then connect all the lob files together. Lob is the abbreviation of lint object module. This file contains all the external information of a C or C ++ module. There are three options to generate a lob file:-u. To generate a lob file using lint, you must add the-u option; the second option is the-zero or-zero (500) option. This option must be added to ensure that the lob file can be generated when the module has an error; the third type is-oo [(filename)]. filename is the name of the generated lob file. It can be added or removed after-oo. If this parameter is not added, the lob file name is the same as the name of the original C Module, for example:
Lint-u Alpha. C-Oo (A1)
The generated lob file name is a1.lob.
Lint-u Alpha. C-oo
The generated lob file name is Alpha. lob.
Lint multiple C modules in a project. In the user's source program directory, three files are generally required: one option file (*. LNT), a batch processing file (*. BAT) and a makefile (*. mak ). The following describes how to create these files.
1) Option file (*. LNT)
As mentioned earlier, you can list all the common options that you use in each lint C file in this file. The options take effect from left to right, from top to bottom. This type of files can be nested layer by layer, and there is no limit on the number of nested layers. For example, make. LNT file:
-Ic: \ lint
STD. LNT
+ OS (temp)
-E46
+ VM
-Zero
2) batch file (*. BAT)
When creating a batch file, you must call the tcmake. EXE file and MAKEFILE file in the file, such as the lintmake. Mak file:
@ Echo lint making 'makelap ':
Tcmake-flintmake. Mak
@ Echo end of making
3) makefile (*. Mak)
The tcmake syntax used by makefile is the same as the syntax format of the MAKEFILE file used for development and compilation. For example, the following lintmake. Mak file:
Mccpath = c: \ mcc68k
Option =-u make. LNT-oo
Globle = OS. h l2lap. h
Mail_depend = $ (Globle) q931.h mail. h
Lapmain_depend = $ (Globle) l1pubdef. h q931.h mail. h
Lapos_depend = $ (Globle)
Fhdlc1_depend = $ (Globle) cpuhdlc. h bd_prar.h q931.h
OBJ = Mail. lob lapmain. lob lapos. lob fhdlc1.lob
Project. lob: $ (OBJ)
Lint-nt make. LNT-e768-e769 *. lob
Mail. lob: mail. C $ (mail_depend)
Lint-NT $ (option) mail. c
Lapmain. lob: lapmain. C $ (lapmain_depend)
Lint-NT $ (option) lapmain. c
Lapos. lob: lapos. C $ (lapos_depend)
Lint-NT $ (option) lapos. c
Fhdlc1.lob: fhdlc1.c $ (fhdlc1_depend)
Lint-NT $ (option) fhdlc1.c
4.3 simple lint multiple files
Assuming that our project is not complex, we can afford the overhead of lint all the files every time, or do not use the above Regular usage. In practice, I found that placing all the *. c files in a lint command can also accomplish the purpose of the entire lint project.
For example:
Lint-nt c: \ lint \ STD. LNT allmysource. LNT
Include all the source files in your project in allmysource. LNT:
A1.c
A2.c
A3.c
Note that the-u option is not required in the STD. LNT file.

From: http://www.avrw.com/article/art_109_2102.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.