Static code check tool PC-Lint (2)

Source: Internet
Author: User
Tags ultraedit

3.5 format check

PC-Lint checks the format conflicts between printf and scanf (and their families). For example:
Printf ("% + C ",...)
A 566 alarm will be generated because the plus sign is only useful for numeric conversion. If there are more than one hundred such combinations, an alarm will be generated. The Compiler usually does not mark these contradictions, other alarms complain about bad formats, which are 557 and 567. We follow the rules set up by ansi c, and, more importantly, we also mark incorrectly sized formats (including alarms 558,559,560 and 561 ). For example, in % d format, int and unsigned int are allowed, but double and long (if long is longer than INT) are not supported. Similarly, scanf must point to the correct object size. If the parameter type (not the size) is inconsistent with the format, an alarm of 626 and 627 is generated. -The printf and-scanf options allow users to specify functions similar to the family of printf or scanf functions.-printf_code and-scanf_code can also be used to describe non-standard % codes.

3.6 indent check

According to the indentation problem in the code, PC-Lint will also generate corresponding alarms, because a large part of the indentation problem is caused by poor code structure or missing braces. For example:
If (...)
If (...)
Statement
Else statement
Obviously, here Else corresponds to the first if statement, and here the compiler maps it to the second if statement. PC-Lint generates an alarm for this situation. There are three alarms related to this indentation check: 725 (no positive indentation), 525 (negatively indented from), and 539 (did not distinct CT positive indentation from location) to check the indentation, first set the number of spaces corresponding to the tab key in the file. The default value is 8 spaces. You can use the-T # option to modify this parameter. For example,-T4 indicates that the tab key occupies four spaces. In addition, the indentation check is also related to the code encoding format policy and requires necessary adjustments.

3.7 const variable check

For the const variable check, PC-Lint is fully supported. Using the const variable is very helpful for improving the quality of the Code. See the following example in detail:
Char * strcpy (char *, const char *);
Const char c = 'a ';
Const char * P = & C;
Void main ()
{
Char Buf [100];
C = 'B ';
* P = 'C ';
Strcpy (p, Buf );
...
Here, both C and * P point to static variables and cannot be modified. The above code obviously violates this rule and produces error (11). In addition, when P is passed into strcpy as the first parameter, an alarm of 605 (increase in Pointer capability) is generated ), when the Buf is passed into the strcpy function as the second parameter, an alarm 603 (symbol 'symbol' (location) not initialized) is generated because the Buf is not initialized, the second parameter of static variables cannot be initialized in the strcpy function.

3.8 volatile variable check

For the volatile variable check, there is such a rule in PC-lint. If two identical volatile variables are used in an expression at the same time, a 564 alarm is triggered, this will cause the order of values.
Volatile char * P;
Volatile char F ();
N = (f () <8) | f ();/* warning 564 */
N = (* P <8) | * P;/* warning 564 */

4 pc-Lint software usage

4.1 installation and configuration

PC-Lint software is cost-effective, easy to learn, easy to promote and solidify into the software development and testing process, so it has been widely used in the world. PC-Lint is easy to use. You can use the command line method. For example, lint-NT-u STD. LNT test1.c test2.c test3.c can also use makefile. In addition, it can also be integrated into many development environments or commonly used code editing software, such as integration into source Insight/slickedit/MS vc6.0/Keil C... and so on. PC-Lint also supports various methods described in Scott meyes's popular C ++/more effective C ++ to improve efficiency and prevent errors.
PC-Lint installation is very simple. Take PC-Lint 8.0 as an example. Run the installer to release it to the specified installation directory, such as C:/pclint8. After that, run the configuration tool config.exegenerate and check the configuration file of pc-lint. for example, config.exe should be located at: C:/pclint8/config.exe. The configuration file is the basis for code check. PC-Lint comes with a standard configuration file STD. LNT, but this file does not contain directory information (header file directory), usually you need to specify some special include directory during code check, therefore, you must generate a custom configuration for code check of a project based on the standard configuration. The following uses the Microsoft Visual C ++ 6 development environment as an example to describe the process of custom configuration.
After running C:/pclint8/config.exe, a welcome page is displayed, indicating the copyright information, as shown in Figure 4.1:


Figure 4.1 welcome window for configuration

Click "Next" and click "Next" to find out the pc-lint.exe command line instructions window (Figure 4.2 ):

Figure 4.2 pc-lint.exe command line instructions window

Click "Next" to continue, and then select the options to create or modify the existing configuration file STD. LNT:


Figure 4.3 select how to use the configuration file STD. LNT

Because this is the first configuration, select the above option "Create a new STD. this will not modify the existing configuration file STD. the content of LNT is to create a new std_x.lnt file. The X in the file name is any of the 26 English characters from "A" to "Z", which is generally arranged in order, start with ". The content of the std_x.lnt file is initialized as a copy of the STD. LNT content. 4.3, use the default PC-Lint path, and click "Next" to select the compiler:

Figure 4.4 select Compiler

Next, select the compiler. In the drop-down box, select your own compiler. Here we select Microsoft Visual C ++ 6.x (co-msc60.lnt )". If you do not have your own compiler, you can select the generic compiler "generic compilers ". This option will be reflected in the co-xxx.lnt file and stored in the previous configuration path we selected (C:/pclint8), followed by the configuration option we selected ***. LNT will be stored in this path. Click "Next" to select the memory mode:


Figure 4.5 select memory mode

You can select an appropriate memory model based on the actual size of your own program and data zone. The memory model options are reflected in the STD. LNT file or the newly created std_x.lnt. Because our development environment is a 32-bit windows, select "32-bit flat model" and click "Next" to select the configuration information of the desired supported database:


Figure 4.6 select the configuration information of the software library

PC-Lint provides custom configuration information for some commonly used software libraries. Choosing these custom information helps developers to focus their attention on errors or information in their own code, the selected supported library configuration will be introduced to STD. the LNT file or the newly created std_x.lnt file. Select common ATL, MFC, STL, and other configurations, and then click "Next:


Figure 4.7 programming recommendations for choosing a software celebrity

This is an interesting option, that is, to allow you to choose whether to support some personal opinions on programming from the authors who have made important suggestions for using C/C ++ programming. If you select an author's suggestion, the options for his proposed programming recommendations will be turned on, the author's suggested configuration is AU-xxx.LNT, it is recommended to select all, and then click the next button:

Figure 4.8 select whether to set the directory containing files

Next, select how to set the include file directory. If you select-I to help set the include file option, the next step requires entering one or more include paths. You can also skip this step and manually modify the configuration file later. The-I option is embodied in STD. in the LNT file or the newly created std_x.lnt file, each directory is prefixed with-I. The directories are separated by spaces. If the directory name contains a long file name or contains spaces, double quotation marks are used, for example,-I "E:/program files/Microsoft Visual C ++/vc98/indlue ". Here we select-I to help us set the settings, and then click "Next:


Figure 4.9 select whether to set the directory containing files

In this step, you can manually enter the file inclusion path in the text box below, use the Semicolon ";" or use Ctrl + enter to break multiple inclusion paths, or click brows, select directly in the directory tree. After entering the information, click "Next:


Figure 4.10 indicates that std_x.lnt has been created.

Because "Create a new STD. the following dialog box is displayed, indicating std_x.lnt, STD. LNT has been created in the configuration path. Here std_a.lnt actually contains STD. in addition to the LNT information, we also select the path and library configuration information. Click OK to continue:


Figure 4.11 shows whether to create a configuration file for other compiling Environments

After "OK" is selected, the system prompts whether to create a configuration file for other compilation environments. If "yes" is selected, a new configuration file will be created from step 4. Here we select "no ":


Figure 4.12 Replace the STD. LNT File

Next, you will be prompted whether to use the generated std_x.lnt file to replace the STD. LNT file. If "yes" is selected, the content of the std_x.lnt file will overwrite the content of the STD. LNT file, making the currently created configuration option the default configuration when creating a new configuration file. Generally, we choose "no" to continue the next step:

Figure 4.13 generate the global code check option file options. LNT

Next, you will prepare an option file that controls the display of global compilation information, options. 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 a series of core selection processes. 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. Select the "no" option here, that is, do not cancel these options, and then click "Next ":

Figure 4.14 select the supported integrated development environment

Then select the supported integrated development environment options. You can select either one or more options. PC-Lint provides the function of integrating multiple development environments, for example, it can be integrated into VC, BC, and source insight. Here we select microsift visual c ++ 6.0 so that the env-v6.lnt will be copied to the configuration path. Click "Next ":

Figure 4.15 how to use the Lin. BAT file

The installer generates a Lin. BAT file, which is a batch file running PC-lint. To enable the file to run in any path, the installer provides two methods for you to choose from. The first method is to let you copy Lin. BAT to any path 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. We recommend that you select the first method. The specified directory is the installation directory of the current PC-lint. Select the first method: "Copy Lin. BAT to one of my path directory", and then click "Next" to enter the path directory:

Figure 4.16 specify the path directory

Enter the installation directory c:/pclint8 as the path directory, and click "Next" to enter the final confirmation window:

Figure 4.17 confirm configuration completion

Now the installation and configuration of PC-Lint have been completed. Click "finish" to use PC-lint. Multiple * generated under the configuration path during the above configuration process *. LNT file, except STD. LNT, std_x.lnt, and option. LNT is generated for the Configuration Wizard and other co-xxx.lnt, lib-xxx.lnt, and env-xxx.lnt are copied from the original installation directory, there are other compilers, libraries, and LNT configuration files supported by pclint in this directory. All LNT files are text files.
The above configuration method is suitable for the use of PC-lint at the beginning, for skilled users can directly edit, write each *. the LNT configuration file can work with the above configuration, or customize a configuration environment that is more suitable for your use.

4.2 PC-Lint integration with common development tools

The use of PC-Lint is very simple. You can use the command line method or integrate it into the development environment. The following describes the usage of PC-lint.

4.2.1 use command line

The command line is the most basic use method of PC-lint and the basis of other integrated use methods. You can use the command line to complete all the code analysis work of PC-lint. The command line of PC-Lint has the following forms:
Lint-NT Option file1 [file1 file3…]
Among them, lint-nt is the executable program lint-nt.exe of PC-lint on Windows NT/2000/XP platform. It completes the basic functions of PC-lint. Option represents various options acceptable to PC-lint, this is the most complex part of PC-lint. It has more than 300 options, which can be divided: the third part of this article has already introduced the options such as "forbidden option", "variable type Size Option", "redundant information option", "flag option", "output format option", and other options; file is the source file to be checked.
In addition, you can add the aforementioned *. the LNT configuration file name can be viewed as an extension of the command line. The various options and file lists configured in it have the same effect as those written in the command line.

4.2.2 integration of PC-lint and Visual C ++ integrated development environment (IDE)

In all integrated development environments, PC-Lint 8.0 provides the most comprehensive support for VC ++ 6 and VC ++ 7.0, it even supports project files directly from VC (vc6 is *. DSP, vc7 is *. vcproj) export the corresponding project. LNT file. This file contains the precompiled macro in the project settings. The header file contains the path and source file name. You do not need to manually write the project. LNT file.
The integration of PC-lint and VC is to add several custom commands in the integrated development environment of VC. The method for adding custom commands is to select "mimize" of "Tools... "command, select the" Tools "tab in the pop-up customize window, and add the custom command in the tab of the custom tool command. First, you must add a function for exporting the. LNT configuration file of the current project to the integrated development environment of VC. The command line for exporting the. LNT file is:
Lint-nt.exe + linebuf $ (targetname). DSP> $ (targetname). LNT
The parameter + linebuf indicates the size of the double row buffer, initially 600 bytes. The row buffer is used to store the information of the current row and the longest row you read. $ (Targetname) is the environment variable of the VC integrated development environment. It indicates the name of the currently activated Project. Check the "use output window" option, in this way, the PC-Lint will output the information to the output window. The result 4.18 is displayed:

Figure 4.18 add custom commands for exporting the project. LNT File

Then add a custom command to check the current file and check the Command Behavior of the file:
Lint-nt.exe-I "C:/pclint8"-u std_g.lnt env-vc6.lnt "$ (filename) $ (fileext )"
The first parameter-I "C:/pclint8" is the directory of the PC-Lint search *. LNT file. Here is the configuration path. Std_g.lnt is a configuration file customized for the VC compiling environment. $ (filename) and $ (fileext) are the environment variables of the VC integrated development environment. "$ (filename) $ (fileext) "indicates the file name of the current file. Like the export. LNT command, this command also needs to use the output window of the VC integrated environment to output the check information, so select the "use output window" option, as shown in 4.19:


Figure 4.19 Add a custom command to check the current file

Finally, you need to add a custom command to check the entire project. The command line to check the entire project is:
Lint-nt.exe + FFN-I "C:/pclint8" std_g.lnt env-vc6.lnt $ (targetname). LNT> $ (targetname). Chk
The result of this command is to output the check result of the entire project to the. Chk file with the same name as the project. In the parameter, + FFN indicates full file names, which can be used to control whether the full path name is used.
The following is a simple example to describe how to use PC-lint in the VC integrated development environment. First, create a simple project of the "Win32 console application" type (output "Hello World"), and add the example code referenced in Chapter 2 of this article to the project code, finally, manually add the include directory that this project code relies on to the configuration file, because the code check requires searching for stdafx. h: The pre-compiled file. Therefore, in this example, you must manually add the directory where the project code is located. The configuration file generated in this example is std_g.lnt. Open std_g.lnt in a text file and add a line to the file:
-Ic:/unzipped/test
"C:/unzipped/test" is the directory where the example project is located (stdafx. H is in this directory ). If your project is large and there are many header files containing directories, you need to add these directories to the configuration file one by one. After you make sure that there is no error in the code input (it does not matter if there is an error page, PC-Lint will check the error), you can start the code check. Example project: Open the code file to be checked. In this example, test. CPP, and then select the "pc_lint 8.0 check current file" command under the "Tools" menu. The output window outputs the check results for this file, as shown in 4.20:


Figure 4.20 check results

4.2.3 integration of PC-lint and source insight

The integration of PC-lint and source insight is also implemented by adding custom commands. Select the custom commands command item from the Options menu. Click "Add ..." As shown in figure 4.21, complete the following input in the pop-up "m commands" window:
Enter "PC-Lint check current file" in the name column. In principle, this name can be used as long as you can understand its meaning;
In the run column, enter "C:/pclint/lint-NT-u-ic:/pclint/lint std_f env-Si % F", where C: /pclint is the installation directory of your PC-LINT, std_f represents the configuration file std_f.lnt customized for source insight;
Select "iconic window" and "Capture output" in the output column;
Select "save files first" in the control column ";
In the source links in output column, select "parse links in output", "file, then line ";
In the pattern column, enter "^/([^] */)/([0-9] + /)";


Figure 4.21 add custom commands in source insight

After the command is added, you can click the "run" button to perform a PC-Lint check on the current file. For ease of use, you can also click "menu..." to add this custom command to the menu of source insight.

4.2.4 integration of PC-lint and ultraedit

The method of integrating PC-lint in ultraedit is similar to that of source insight. You can also add a custom command menu by clicking the "tool configuration" command in the "advanced" menu of ultraedit, 4.22. Enter the following content in the configuration window:
In the "menu project name" field, enter "PC-Lint check current file ";
In the "command line" column, enter the following command: C:/pclint/lint-NT-u-ic:/pclint STD env-Si % F, where C: /pclint is the installation directory of PC-lint, Using STD. the configuration in LNT is similar to that in the source insightde check environment, so the environment configuration in env-Si is used;
In the "working directory" column, enter the following path: e:/code, which is the directory where the code is located;
Select "Save all files first;
In the "command output" column, select "output to list" and "Capture output;
Click "insert" to insert the command line to the ultraedit menu;

Figure 4.22 add custom commands in ultraedit

At this time, a "PC-Lint check current file" menu will be added to the "advanced" menu of ultraedit. Click this menu to perform a PC-Lint check on the current file.

Summary

Software Debugging is a major factor in the development cost and delay of software projects. PC-Lint can help you find coding errors before dynamic testing of the program and reduce the cost of eliminating software errors. With PC-lint, you can check code before code reading and unit testing. You can detect program hidden errors in advance, improve code quality, and save test time. In addition, the use of PC-Lint encoding rules check can effectively regulate the coding behavior of software personnel. If you can effectively use the PC-Lint code check tool during software development, it will greatly improve the code quality and reduce software costs.

References

[1] gimpel software. Reference Manual for PC-Lint/flexelint. July, 2001
[2] explanation of PC-Lint options

Appendix I PC-Lint important documents

Msg.txt: Describes the alert content.
Options. LNT: Option file that reflects the display of global compilation information. You need to add custom options to strictly check the code.
Env-xx.lnt: describes how to combine PC-lint with the corresponding editing environment, XX is Si represents the check environment configured for source insight, if XX is vc6, it indicates the check Environment prepared for Visual C ++ 6.0.
Co-xxx.lnt: The selected compiler and library options.
STD. LNT: the standard configuration file, including the memory model and other global things.
Lib-xxx.lnt: library type list, including standard C/C ++ library, MFC Library, owl library and so on.
Au-xxx.LNT: the author of C ++ programming who has made important suggestions. After selecting an author, his options for programming recommendations will be turned on.

Appendix 2 Description of disallow options for error messages

Example in the command format description code
-E # Hide certain errors/* lint-e725 */
-E (#) hides a certain type of error in the next expression/* lint-E (534 )*/
Printf ("It's all ");
! E # Hide errors in this row/* lint! E534 */printf ("It's all ");
-Esym (#, symbol) hides a symbol error/* lint-esym (534, printf )*/
Printf ("It's all ");
-Elib (#) hides a certain type of error in the header file/* lint-elib (129 )*/
# Include "r01.h"
-Efunc (#, <func>) hides a specific error in a function/* lint-efunc (534, mchrelall )*/
Unsigned int mchrelall (mchhodata
* Phodata)
{
Printf ("It's all ");
}

Appendix 3 Common Errors in PC-Lint Detection

 

Example of error code errors
40 variable not declared
506 fixed Boolean value char c = 3;
If (C <300 ){}
525 the format of the contraction is incorrect.
527 statements that cannot be executed if (A> B)
Return true;
Else
Return false;
Return false;
529 why the variable is not referenced and the variable is not referenced
530 use uninitialized variables
534 ignore function return values
539 The format of the contraction is incorrect.
545 use & char arr [100], * P; for Array Variables;
P = & arr;
603 the pointer has not initialized void print_str (const char * P );
...
Char * SZ;
Print_str (sz );
605 enhanced void write_str (char * lpsz );
...
Write_str ("string ");
613 NULL pointer may be used
616 The break is not used in the switch statement;
650 when comparing values, the constant range exceeds if (CH = 0xff )...
Variable range
713 assign a signed value to the unsigned Value
Value
715 the variable is not referenced
725 indentation Error
734 variable out-of-bounds int A, B, C when assigning values;
...
C = A * B;
737 unsigned type changes/constants and variable types
Changes/constants exist in the same expression.
744 no default in the switch statement
752 locally declared functions are not used
762 repeated function declaration
774 Boolean expression always returns true/false char C;
If (C <1, 300)

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.