File: findcasts.py

package info (click to toggle)
cppcheck 2.17.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 25,384 kB
  • sloc: cpp: 263,341; python: 19,737; ansic: 7,953; sh: 1,018; makefile: 996; xml: 994; cs: 291
file content (32 lines) | stat: -rwxr-xr-x 839 bytes parent folder | download
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
#!/usr/bin/env python3
#
# Locate casts in the code
#

import cppcheck

@cppcheck.checker
def cast(cfg, data):
    for token in cfg.tokenlist:
        if token.str != '(' or not token.astOperand1 or token.astOperand2:
            continue

        # Is it a lambda?
        if token.astOperand1.str == '{':
            continue

        # we probably have a cast.. if there is something inside the parentheses
        # there is a cast. Otherwise this is a function call.
        typetok = token.next
        if not typetok.isName:
            continue

        # cast number => skip output
        if token.astOperand1.isNumber:
            continue

        # void cast => often used to suppress compiler warnings
        if typetok.str == 'void':
            continue

        cppcheck.reportError(token, 'information', 'found a cast')