Cppcheck Code Analysis (1)

Source: Internet
Author: User

First, the checkpoint

1, automatic variable check:

Returns an automatic variable (local variable) pointer

2. Cross-border inspection:

Array out-of-bounds return automatic variable (local variable) pointer

3. Class Check:

Constructor initialization

4. Memory leak Check:

5. Null pointer check:

6. Obsolete function check

7. Other:

Second, Cppcheck structure analysis

Overall

Third, check point Cppcheck system C + + implementation class

Parameter Analysis External input

Internal Abstraction Classsetings

Class Settings

{

...

Std::string _append;

Std::string Userdefines;

Std::list<std::string> _includepaths;

Std::list<rule> rules;

...

}
character Interaction mode
Cmdlineparser parser (&_settings);

Iv. Total process pretreatment of Cppcheck

preprocessing is handled by preprocessor class Implementation

Execute class Preprocessor::p reprocess ()

The pretreatment phase mainly deals with:

Go to extra space

Remove assembly Code

Handling # include and nesting

Uniform preprocessing statements (example: #if define=> #ifdef)

Extract preprocessing configuration settings (config)

Replace macro definition

Tokenize parsing code into symbols

Implemented by class Tokenizer

Implement Interface Class Tokenizer::tokenize ()

Symbol: +-*/, ..... such as

Variable name

Name of function

Simplify

Objective:

Simplifying Complex code

Harmonization of

Implemented by class Tokenizer

Implement Interface Class Tokenizer::simplifytokenlist ()

Simplify rules

Pairs of variables

The conditional loop statement if, for, while

Five, Cppcheck core classes class Cppcheck

Core function Check ()

Handles the entry, where the function carries out preliminary parsing of the input code, and finally passes the code to Checkfile ().

Core function Checkfile ()

function function is to parse a code file, Checkfile () will do further analysis of the code flow, do tokenize,simplify, parse the code after processing, report the error

Class Cppcheck::check () function &class Cppcheck:: Checkfile () implementation of the function



Cppcheck Check implementation class check

Cppcheck Core class Check

Class Check

Protected

Const std::string _name;

Const TOKENIZER * Const _tokenizer;

Const Settings * Const _settings;

Errorlogger * Const _errorlogger;

virtual void runchecks (* * *) {}// Not all Check subclasses will implement

virtual void runsimplifiedchecks (* *) = 0; all check subclasses will implement

void ReportError ()// Report Errors, all Check subclasses will use the

virtual void geterrormessages (* * *) = 0; all check subclasses will be implemented and will eventually call ReportError ()

Void Runchecks ()

The main is to check the tokenize, but not simplify code flow

Void Runsimplifiedchecks ()

The main is to check the tokenize, but not simplify code flow

User Extension interface

Checkother:public Check

......

virtual void runchecks (* * *) {}// Implement

virtual void runsimplifiedchecks (* *) = 0; Implement

To add a check function method:

void Runchecks (const tokenizer *tokenizer, const Settings *settings, Errorlogger *errorlogger)

{

Checkother Checkother (tokenizer, Settings, Errorlogger);

Coding Style Checks

Checkother.warningoldstylepointercast ();

Checkother.checkunsigneddivision ();

Checkother.addcheck ();

...

}

Cppcheck Code Analysis (1)

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.