• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trolldbois / ctypeslib / 4773564691

pending completion
4773564691

push

github

Loic Jaquemet
try without baipp

1601 of 1912 relevant lines covered (83.73%)

50.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.7
/ctypeslib/codegen/util.py
1
# From clang/bindings/python/cindex/test
2
# This file provides common utility functions for the test suite.
3
#
4

5
from clang.cindex import Cursor
60✔
6
from clang.cindex import TranslationUnit
60✔
7
from collections.abc import Iterable
60✔
8

9
import logging
60✔
10
import re
60✔
11

12
from ctypeslib.codegen import typedesc
60✔
13

14
log = logging.getLogger('utils')
60✔
15

16

17
def get_tu(source, lang='c', all_warnings=False, flags=None):
60✔
18
    """Obtain a translation unit from source and language.
19

20
    By default, the translation unit is created from source file "t.<ext>"
21
    where <ext> is the default file extension for the specified language. By
22
    default it is C, so "t.c" is the default file name.
23

24
    Supported languages are {c, cpp, objc}.
25

26
    all_warnings is a convenience argument to enable all compiler warnings.
27
    """
28
    args = list(flags or [])
60✔
29
    name = 'memory_input.c'
60✔
30
    if lang == 'cpp':
60✔
31
        name = 'memory_input.cpp'
×
32
        args.append('-std=c++11')
×
33
    elif lang == 'objc':
60✔
34
        name = 'memory_input.m'
×
35
    elif lang != 'c':
60✔
36
        raise Exception('Unknown language: %s' % lang)
×
37

38
    if all_warnings:
60✔
39
        args += ['-Wall', '-Wextra']
×
40

41
    return TranslationUnit.from_source(name, args, unsaved_files=[(name, source)])
60✔
42

43

44
def get_cursor(source, spelling):
60✔
45
    """Obtain a cursor from a source object.
46

47
    This provides a convenient search mechanism to find a cursor with specific
48
    spelling within a source. The first argument can be either a
49
    TranslationUnit or Cursor instance.
50

51
    If the cursor is not found, None is returned.
52
    """
53
    children = []
60✔
54
    if isinstance(source, Cursor):
60✔
55
        children = source.get_children()
60✔
56
    else:
×
57
        # Assume TU
58
        children = source.cursor.get_children()
60✔
59

60
    for cursor in children:
60✔
61
        if cursor.spelling == spelling:
60✔
62
            return cursor
60✔
63

64
        # Recurse into children.
65
        result = get_cursor(cursor, spelling)
60✔
66
        if result is not None:
60✔
67
            return result
60✔
68

69
    return None
60✔
70

71

72
def get_cursors(source, spelling):
60✔
73
    """Obtain all cursors from a source object with a specific spelling.
74

75
    This provides a convenient search mechanism to find all cursors with specific
76
    spelling within a source. The first argument can be either a
77
    TranslationUnit or Cursor instance.
78

79
    If no cursors are found, an empty list is returned.
80
    """
81
    cursors = []
×
82
    children = []
×
83
    if isinstance(source, Cursor):
×
84
        children = source.get_children()
×
85
    else:
×
86
        # Assume TU
87
        children = source.cursor.get_children()
×
88

89
    for cursor in children:
×
90
        if cursor.spelling == spelling:
×
91
            cursors.append(cursor)
×
92

93
        # Recurse into children.
94
        cursors.extend(get_cursors(cursor, spelling))
×
95

96
    return cursors
×
97

98

99
def decorator(dec):
60✔
100
    def new_decorator(f):
60✔
101
        g = dec(f)
60✔
102
        g.__name__ = f.__name__
60✔
103
        g.__doc__ = f.__doc__
60✔
104
        g.__dict__.update(f.__dict__)
60✔
105
        return g
60✔
106
    new_decorator.__name__ = dec.__name__
60✔
107
    new_decorator.__doc__ = dec.__doc__
60✔
108
    new_decorator.__dict__.update(dec.__dict__)
60✔
109
    return new_decorator
60✔
110

111

112
@decorator
60✔
113
def log_entity(func):
48✔
114
    def fn(*args, **kwargs):
60✔
115
        name = args[0].get_unique_name(args[1])
60✔
116
        if name == '':
60✔
117
            parent = args[1].semantic_parent
60✔
118
            if parent:
60✔
119
                name = 'child of %s' % parent.displayname
60✔
120
        log.debug("%s: displayname:'%s'",func.__name__, name)
60✔
121
        # print 'calling {}'.format(func.__name__)
122
        return func(*args, **kwargs)
60✔
123
    return fn
60✔
124

125

126
class ADict(dict):
60✔
127

128
    def __getattr__(self, name):
60✔
129
        try:
60✔
130
            return self[name]
60✔
131
        except KeyError:
×
132
            raise AttributeError(name)
×
133

134

135
_c_literal_regex = re.compile(
60✔
136
    r"^([+-]?((\d+(e|E)[+-]?\d+)|(\d+(\.\d*)?((e|E)[+-]?\d+)?)|(\.\d+((e|E)[+-]?\d+)?)))(f|F|l|L)?$"
60✔
137
)
138

139

140
def from_c_float_literal(value):
60✔
141
    if (not isinstance(value, str) and
60✔
142
            isinstance(value, Iterable) and
60✔
143
            all(map(lambda v: isinstance(v, str), value))):
60✔
144
        value = "".join(value)
60✔
145
    if not isinstance(value, str):
60✔
146
        return None
×
147
    match = _c_literal_regex.match(value)
60✔
148
    if not match:
60✔
149
        return None
60✔
150
    return match.group(1)
60✔
151

152

153
def contains_undefined_identifier(macro):
60✔
154
    # body is undefined
155
    if isinstance(macro.body, typedesc.UndefinedIdentifier):
60✔
156
        return True
60✔
157

158
    def _list_contains_undefined_identifier(l):
60✔
159
        for b in l:
60✔
160
            if isinstance(b, typedesc.UndefinedIdentifier):
60✔
161
                return True
60✔
162
            if isinstance(b, list) and _list_contains_undefined_identifier(b):
60✔
163
                return True
60✔
164
        return False
60✔
165

166
    # or one item is undefined
167
    if isinstance(macro.body, list):
60✔
168
        if _list_contains_undefined_identifier(macro.body):
60✔
169
            return True
60✔
170

171
    return False
60✔
172

173

174
def token_is_string(token):
60✔
175
    # we need at list 2 delimiters in there
176
    if not isinstance(token, Iterable) or len(token) < 2:
60✔
177
        return False
60✔
178
    delim = token[0]
60✔
179
    return delim in ["'", '"'] and token[0] == token[-1]
60✔
180

181

182
def body_is_all_string_tokens(macro_body):
60✔
183
    if isinstance(macro_body, list):
60✔
184
        for b in macro_body:
60✔
185
            if token_is_string(b):
60✔
186
                continue
60✔
187
            else:
×
188
                return False
60✔
189
        return True
60✔
190
    return False
×
191

192

193
__all__ = [
48✔
194
    'get_cursor',
24✔
195
    'get_cursors',
24✔
196
    'get_tu',
24✔
197
    'from_c_float_literal',
24✔
198
]
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc