summaryrefslogtreecommitdiffstats
path: root/tools/scan-build-py/libscanbuild/runner.py
blob: 248ca90ad3e6204dafdb6d79feb8126654a55101 (plain)
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
# -*- coding: utf-8 -*-
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
""" This module is responsible to run the analyzer commands. """

import os
import os.path
import tempfile
import functools
import subprocess
import logging
from libscanbuild.command import classify_parameters, Action, classify_source
from libscanbuild.clang import get_arguments, get_version
from libscanbuild.shell import decode

__all__ = ['run']


def require(required):
    """ Decorator for checking the required values in state.

    It checks the required attributes in the passed state and stop when
    any of those is missing. """

    def decorator(function):
        @functools.wraps(function)
        def wrapper(*args, **kwargs):
            for key in required:
                if key not in args[0]:
                    raise KeyError(
                        '{0} not passed to {1}'.format(key, function.__name__))

            return function(*args, **kwargs)

        return wrapper

    return decorator


@require(['command', 'directory', 'file',  # an entry from compilation database
          'clang', 'direct_args',  # compiler name, and arguments from command
          'output_dir', 'output_format', 'output_failures'])
def run(opts):
    """ Entry point to run (or not) static analyzer against a single entry
    of the compilation database.

    This complex task is decomposed into smaller methods which are calling
    each other in chain. If the analyzis is not possibe the given method
    just return and break the chain.

    The passed parameter is a python dictionary. Each method first check
    that the needed parameters received. (This is done by the 'require'
    decorator. It's like an 'assert' to check the contract between the
    caller and the called method.) """

    try:
        command = opts.pop('command')
        logging.debug("Run analyzer against '%s'", command)
        opts.update(classify_parameters(decode(command)))

        return action_check(opts)
    except Exception:
        logging.error("Problem occured during analyzis.", exc_info=1)
        return None


@require(['report', 'directory', 'clang', 'output_dir', 'language', 'file',
          'error_type', 'error_output', 'exit_code'])
def report_failure(opts):
    """ Create report when analyzer failed.

    The major report is the preprocessor output. The output filename generated
    randomly. The compiler output also captured into '.stderr.txt' file.
    And some more execution context also saved into '.info.txt' file. """

    def extension(opts):
        """ Generate preprocessor file extension. """

        mapping = {'objective-c++': '.mii', 'objective-c': '.mi', 'c++': '.ii'}
        return mapping.get(opts['language'], '.i')

    def destination(opts):
        """ Creates failures directory if not exits yet. """

        name = os.path.join(opts['output_dir'], 'failures')
        if not os.path.isdir(name):
            os.makedirs(name)
        return name

    error = opts['error_type']
    (handle, name) = tempfile.mkstemp(suffix=extension(opts),
                                      prefix='clang_' + error + '_',
                                      dir=destination(opts))
    os.close(handle)
    cwd = opts['directory']
    cmd = get_arguments([opts['clang']] + opts['report'] + ['-o', name], cwd)
    logging.debug('exec command in %s: %s', cwd, ' '.join(cmd))
    subprocess.call(cmd, cwd=cwd)

    with open(name + '.info.txt', 'w') as handle:
        handle.write(opts['file'] + os.linesep)
        handle.write(error.title().replace('_', ' ') + os.linesep)
        handle.write(' '.join(cmd) + os.linesep)
        handle.write(' '.join(os.uname()) + os.linesep)
        handle.write(get_version(cmd[0]))
        handle.close()

    with open(name + '.stderr.txt', 'w') as handle:
        handle.writelines(opts['error_output'])
        handle.close()

    return {
        'error_output': opts['error_output'],
        'exit_code': opts['exit_code']
    }


@require(['clang', 'analyze', 'directory', 'output'])
def run_analyzer(opts, continuation=report_failure):
    """ It assembles the analysis command line and executes it. Capture the
    output of the analysis and returns with it. If failure reports are
    requested, it calls the continuation to generate it. """

    cwd = opts['directory']
    cmd = get_arguments([opts['clang']] + opts['analyze'] + opts['output'],
                        cwd)
    logging.debug('exec command in %s: %s', cwd, ' '.join(cmd))
    child = subprocess.Popen(cmd,
                             cwd=cwd,
                             universal_newlines=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
    output = child.stdout.readlines()
    child.stdout.close()
    # do report details if it were asked
    child.wait()
    if opts.get('output_failures', False) and child.returncode:
        error_type = 'crash' if child.returncode & 127 else 'other_error'
        opts.update({
            'error_type': error_type,
            'error_output': output,
            'exit_code': child.returncode
        })
        return continuation(opts)
    return {'error_output': output, 'exit_code': child.returncode}


@require(['output_dir'])
def set_analyzer_output(opts, continuation=run_analyzer):
    """ Create output file if was requested.

    This plays a role only if .plist files are requested. """

    if opts.get('output_format') in {'plist', 'plist-html'}:
        with tempfile.NamedTemporaryFile(prefix='report-',
                                         suffix='.plist',
                                         delete=False,
                                         dir=opts['output_dir']) as output:
            opts.update({'output': ['-o', output.name]})
            return continuation(opts)
    else:
        opts.update({'output': ['-o', opts['output_dir']]})
        return continuation(opts)


@require(['file', 'directory', 'clang', 'direct_args', 'language',
          'output_dir', 'output_format', 'output_failures'])
def create_commands(opts, continuation=set_analyzer_output):
    """ Create command to run analyzer or failure report generation.

    It generates commands (from compilation database entries) which contains
    enough information to run the analyzer (and the crash report generation
    if that was requested). """

    common = []
    if 'arch' in opts:
        common.extend(['-arch', opts.pop('arch')])
    common.extend(opts.pop('compile_options', []))
    common.extend(['-x', opts['language']])
    common.append(os.path.relpath(opts['file'], opts['directory']))

    opts.update({
        'analyze': ['--analyze'] + opts['direct_args'] + common,
        'report': ['-fsyntax-only', '-E'] + common
    })

    return continuation(opts)


@require(['file', 'c++'])
def language_check(opts, continuation=create_commands):
    """ Find out the language from command line parameters or file name
    extension. The decision also influenced by the compiler invocation. """

    accepteds = {
        'c', 'c++', 'objective-c', 'objective-c++', 'c-cpp-output',
        'c++-cpp-output', 'objective-c-cpp-output'
    }

    key = 'language'
    language = opts[key] if key in opts else \
        classify_source(opts['file'], opts['c++'])

    if language is None:
        logging.debug('skip analysis, language not known')
        return None
    elif language not in accepteds:
        logging.debug('skip analysis, language not supported')
        return None
    else:
        logging.debug('analysis, language: %s', language)
        opts.update({key: language})
        return continuation(opts)


@require([])
def arch_check(opts, continuation=language_check):
    """ Do run analyzer through one of the given architectures. """

    disableds = {'ppc', 'ppc64'}

    key = 'archs_seen'
    if key in opts:
        # filter out disabled architectures and -arch switches
        archs = [a for a in opts[key] if a not in disableds]

        if not archs:
            logging.debug('skip analysis, found not supported arch')
            return None
        else:
            # There should be only one arch given (or the same multiple
            # times). If there are multiple arch are given and are not
            # the same, those should not change the pre-processing step.
            # But that's the only pass we have before run the analyzer.
            arch = archs.pop()
            logging.debug('analysis, on arch: %s', arch)

            opts.update({'arch': arch})
            del opts[key]
            return continuation(opts)
    else:
        logging.debug('analysis, on default arch')
        return continuation(opts)


@require(['action'])
def action_check(opts, continuation=arch_check):
    """ Continue analysis only if it compilation or link. """

    if opts.pop('action') <= Action.Compile:
        return continuation(opts)
    else:
        logging.debug('skip analysis, not compilation nor link')
        return None