Completed
Pull Request — master (#1183)
by Lasse
01:39
created

bears.c_languages.clang_available()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 11
rs 9.4286
1
from coalib.bears.LocalBear import LocalBear
2
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
3
from coalib.results.Result import Result
4
from coalib.results.Diff import Diff
5
from clang.cindex import Index, LibclangError
6
from coalib.results.SourceRange import SourceRange
7
from coalib.settings.Setting import typed_list
8
9
10
def clang_available(cls):
11
    """
12
    Checks if Clang is available and ready to use.
13
14
    :return: True if Clang is available, a description of the error else.
15
    """
16
    try:
17
        Index.create()
18
        return True
19
    except LibclangError as error:  # pragma: no cover
20
        return str(error)
21
22
23
class ClangBear(LocalBear):
24
    check_prerequisites = classmethod(clang_available)
25
26
    def run(self, filename, file, clang_cli_options: typed_list(str)=None):
27
        """
28
        Runs Clang over the given files and raises/fixes any upcoming issues.
29
30
        :param clang_cli_options: Any options that will be passed through to
31
                                  Clang.
32
        """
33
        index = Index.create()
34
        diagnostics = index.parse(
35
            filename,
36
            args=clang_cli_options,
37
            unsaved_files=[(filename, ''.join(file))]).diagnostics
38
        for diag in diagnostics:
39
            severity = {0: RESULT_SEVERITY.INFO,
40
                        1: RESULT_SEVERITY.INFO,
41
                        2: RESULT_SEVERITY.NORMAL,
42
                        3: RESULT_SEVERITY.MAJOR,
43
                        4: RESULT_SEVERITY.MAJOR}.get(diag.severity)
44
            affected_code = tuple(SourceRange.from_clang_range(range)
45
                                  for range in diag.ranges)
46
47
            diffs = None
48
            fixits = list(diag.fixits)
49
            if len(fixits) > 0:
50
                # FIXME: coala doesn't support choice of diffs, for now
51
                # append first one only, often there's only one anyway
52
                diffs = {filename: Diff.from_clang_fixit(fixits[0], file)}
53
54
                # No affected code yet? Let's derive it from the fix!
55
                if len(affected_code) == 0:
56
                    affected_code = diffs[filename].affected_code(filename)
57
58
            # Still no affected code? Position is the best we can get...
59
            if len(affected_code) == 0:
60
                affected_code = (SourceRange.from_values(
61
                    diag.location.file.name,
62
                    diag.location.line,
63
                    diag.location.column),)
64
65
            yield Result(
66
                self,
67
                diag.spelling,
68
                severity=severity,
69
                affected_code=affected_code,
70
                diffs=diffs)
71