1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
---
title: Writing addons
subtitle: Version 2.18
author: Cppcheck team
lang: en
documentclass: report
---
# Introduction
This document provides an overview about writing Cppcheck addons.
# Overview of data
## Tokens
See class `Token` in cppcheckdata.py
Cppcheck splits the code up in tokens: operators, numbers, identifiers, etc.
Example C code:
ab = a + b;
Addon code:
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
print(token.str)
Output:
$ cppcheck --dump test.c
$ python3 runaddon.py myaddon.py test.c.dump
Checking 1.c.dump...
Checking 1.c.dump, config ...
ab
=
a
+
b
;
The `cfg.tokenlist` does not always match the raw input code exactly. For instance:
* The `cfg.tokenlist` is preprocessed.
* There is no typedefs in `cfg.tokenlist`.
* C++ templates are instantiated when possible in `cfg.tokenlist`.
* Variable declarations are sometimes split up.
* If you don't write {} around the body for a if/else/while/for etc then those are inserted in the `cfg.tokenlist`.
* ...
There are various properties in the `Token` class and some of those will be discussed below.
## AST - Abstract syntax tree
Cppcheck creates a syntax tree for every expression.
Example C code:
ab = a + b;
Addon code:
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
out = token.str
if token.astParent:
out += f' parent:"{token.astParent.str}"'
if token.astOperand1:
out += f' astOperand1:"{token.astOperand1.str}"'
if token.astOperand2:
out += f' astOperand2:"{token.astOperand2.str}"'
print(out)
Output:
$ cppcheck --dump test.c
$ python3 runaddon.py myaddon.py test.c.dump
Checking 1.c.dump...
Checking 1.c.dump, config ...
ab parent:"="
= astOperand1:"ab" astOperand2:"+"
a parent:"+"
+ parent:"=" astOperand1:"a" astOperand2:"b"
b parent:"+"
;
## ValueType
Data type of expressions are provided by `class ValueType` in cppcheckdata.py.
Example C code:
short a;
a = a + 10;
Addon code:
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
print(f'{token.str} : {token.valueType}')
Output:
$ cppcheck --dump test.c
$ python3 runaddon.py myaddon.py test.c.dump
Checking 1.c.dump...
Checking 1.c.dump, config ...
short : None
a : ValueType(type='short', sign='signed', bits=0, typeScopeId=None, originalTypeName=None, constness=0, pointer=0)
; : None
a : ValueType(type='short', sign='signed', bits=0, typeScopeId=None, originalTypeName=None, constness=0, pointer=0)
= : ValueType(type='short', sign='signed', bits=0, typeScopeId=None, originalTypeName=None, constness=0, pointer=0)
a : ValueType(type='short', sign='signed', bits=0, typeScopeId=None, originalTypeName=None, constness=0, pointer=0)
+ : ValueType(type='int', sign='signed', bits=0, typeScopeId=None, originalTypeName=None, constness=0, pointer=0)
10 : ValueType(type='int', sign='signed', bits=0, typeScopeId=None, originalTypeName=None, constness=0, pointer=0)
; : None
The `pointer` property is a simple counter.
int p => pointer=0
int *p => pointer=1
int **p => pointer=2
The `constness` property is a bitmask.
int * * => constness=0
const int * * => constness=1
int * const * => constness=2
int * * const => constness=4
const int * const * => constness=3
const int * const * const => constness=7
## Variable
Information about a variable is provided by `class Variable` in cppcheckdata.py.
Example code:
short a[10];
a[0] = 0;
Addon code:
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
print(f'{token.str} : {token.variable}')
Output:
$ cppcheck --dump test.c
$ python3 runaddon.py myaddon.py test.c.dump
Checking 1.c.dump...
Checking 1.c.dump, config ...
a : Variable(Id='0x55f432fc1580', nameTokenId='0x55f432fe0850', typeStartTokenId='0x55f432fc0eb0', typeEndTokenId='0x55f432fc0eb0', access='Global', scopeId='0x55f432fe0360', isArgument=False, isArray=True, isClass=False, isConst=False, isGlobal=True, isExtern=False, isLocal=False, isPointer=False, isReference=False, isStatic=False, constness=0)
[ : None
10 : None
] : None
; : None
a : Variable(Id='0x55f432fc1580', nameTokenId='0x55f432fe0850', typeStartTokenId='0x55f432fc0eb0', typeEndTokenId='0x55f432fc0eb0', access='Global', scopeId='0x55f432fe0360', isArgument=False, isArray=True, isClass=False, isConst=False, isGlobal=True, isExtern=False, isLocal=False, isPointer=False, isReference=False, isStatic=False, constness=0)
[ : None
0 : None
] : None
= : None
0 : None
; : None
## Scope
See `class Scope` in cppcheckdata.py.
Every token is in some scope.
If you see {} in the code then that is a scope of some kind. There is one scope that is not surrounded by {}; the global scope.
Example c code:
int x;
void foo()
{
if (x)
{
x = 0;
}
}
Example addon #1 (list all scopes):
import cppcheck
@cppcheck.checker
def func(cfg, data):
for scope in cfg.scopes:
print(scope.type)
Output:
$ cppcheck --dump test.c
$ python3 runaddon.py myaddon.py test.c.dump
Checking 1.c.dump...
Checking 1.c.dump, config ...
Global
Function
If
Example addon #2 (show scope for each token):
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
print(f'{token.str} : {scope.type}')
Output:
$ cppcheck --dump test.c
$ python3 runaddon.py myaddon.py test.c.dump
Checking 1.c.dump...
Checking 1.c.dump, config ...
int : Global
x : Global
; : Global
void : Global
foo : Global
( : Global
) : Global
{ : Function
if : Function
( : Function
x : Function
) : Function
{ : If
x : If
= : If
0 : If
; : If
} : If
} : Function
### Special tokenlist tweaks and else if
The `cfg.tokenlist` has some tweaks. In C/C++ code it is optional to use `{` and `}` around the if/else/for/while body,
if the body is only a single statement. However Cppcheck adds "{" and "}" tokens if those are missing.
One more tweak is that in cfg.tokenlist there is no "else if" scope.
Example C code:
void foo(int x)
{
if (x > 0)
--x;
else if (x < 0)
++x;
}
The tokens in the `cfg.tokenlist` will look like this:
void foo(int x)
{
if (x > 0)
{
--x;
}
else
{
if (x < 0)
{
++x;
}
}
}
And there are 2 "If" scopes here. And 1 "Else" scope.
## Function
The class `Function` in cppcheckdata.py represents a function that is declared somewhere.
Example code:
void foo(int x);
void bar()
{
foo(1);
}
Example addon #1:
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
print(f'{token.str} : {token.function}')
Output:
void : None
foo : Function(Id='0x55c5f9330f60', tokenId='0', tokenDefId='0x55c5f93510d0', name='foo', type='Function', isVirtual=False, isImplicitlyVirtual=False, isInlineKeyword=False, isStatic=False, argumentId={1: '0x55c5f9351260'})
( : None
int : None
x : None
) : None
; : None
void : None
bar : Function(Id='0x55c5f9331040', tokenId='0x55c5f93314a0', tokenDefId='0x55c5f93314a0', name='bar', type='Function', isVirtual=False, isImplicitlyVirtual=False, isInlineKeyword=False, isStatic=False, argumentId={})
( : None
) : None
{ : None
foo : Function(Id='0x55c5f9330f60', tokenId='0', tokenDefId='0x55c5f93510d0', name='foo', type='Function', isVirtual=False, isImplicitlyVirtual=False, isInlineKeyword=False, isStatic=False, argumentId={1: '0x55c5f9351260'})
( : None
1 : None
) : None
; : None
} : None
## Value flow
For Cppcheck, by default variables and expressions have "unknown" values. If the value is only constrained by the data type that is typically not enough information to write warnings with precision. Unless all the values possible in the data type are bad.
In Cppcheck terminology an expression will have a "possible" value if Cppcheck can determine that there will be a specific value in some control flow paths.
In Cppcheck terminology an expression will have a "known" value if Cppcheck can determine that there will be a specific value in all control flow paths.
An expression can have several "possible" values. But it can't have several "known" values.
Example code:
void foo(int x) // <- values of x is only constrained by data type. there are no "possible" or "known" values here.
{
a = x; // <- assuming that condition below is not redundant, x can have value 2.
if (x == 2)
{
b = x + 2; // <- value of x is always 2 when this code is executed. It's "known".
}
else
{
c = x;
}
d = x + 10; // <- value of x can be 2 when this code is executed. It's "possible".
}
Addon:
import cppcheck
@cppcheck.checker
def func(cfg, data):
for token in cfg.tokenlist:
if token.values:
print(f'line {token.linenr} str="{token.str}"')
for value in token.values:
print(f' {value}')
Output:
$ ../cppcheck --dump 1.c ; python3 runaddon.py myaddon.py 1.c.dump
Checking 1.c ...
Checking 1.c.dump...
Checking 1.c.dump, config ...
line 3 str="="
Value(intvalue=2, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='possible', inconclusive=False)
line 3 str="x"
Value(intvalue=2, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='possible', inconclusive=False)
line 4 str="2"
Value(intvalue=2, tokvalue=None, floatvalue=None, containerSize=None, condition=None, valueKind='known', inconclusive=False)
line 6 str="="
Value(intvalue=4, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='known', inconclusive=False)
line 6 str="x"
Value(intvalue=2, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='known', inconclusive=False)
line 6 str="+"
Value(intvalue=4, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='known', inconclusive=False)
line 6 str="2"
Value(intvalue=2, tokvalue=None, floatvalue=None, containerSize=None, condition=None, valueKind='known', inconclusive=False)
line 12 str="="
Value(intvalue=12, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='possible', inconclusive=False)
line 12 str="x"
Value(intvalue=2, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='possible', inconclusive=False)
line 12 str="+"
Value(intvalue=12, tokvalue=None, floatvalue=None, containerSize=None, condition=4, valueKind='possible', inconclusive=False)
line 12 str="10"
Value(intvalue=10, tokvalue=None, floatvalue=None, containerSize=None, condition=None, valueKind='known', inconclusive=False)
If the value is determined from a condition then the attribute `condition` points out the line that has the condition.
|