Exemple #1
0
    def test_from_clang_fixit(self):
        joined_file = 'struct { int f0; }\nx = { f0 :1 };\n'
        file = joined_file.splitlines(True)
        fixed_file = ['struct { int f0; }\n', 'x = { .f0 = 1 };\n']

        tu = Index.create().parse('t.c', unsaved_files=[
            ('t.c'.encode(), joined_file.encode())])
        fixit = tu.diagnostics[0].fixits[0]

        clang_fixed_file = Diff.from_clang_fixit(fixit, file).modified
        self.assertEqual(fixed_file, clang_fixed_file)
Exemple #2
0
    def run(self,
            filename,
            file):
        """
        This bear is meant for debugging purposes relating to clang. It just
        prints out the whole AST for a file to the DEBUG channel.
        """
        root = Index.create().parse(
            filename,
            options=TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD).cursor

        self.print_node(root, filename)
    def get_vectors_for_file(self, filename, include_paths=()):
        """
        Creates a dictionary associating each function name within the given
        file with another dictionary associating each variable name (local to
        the function) with a CountVector object. Functions of included files
        will not be analyzed.

        :param filename: The path to the file to parse.
        :return:         The dictionary holding CountVectors for all variables
                         in all functions.
        """
        args = ["-I"+path for path in include_paths]
        root = Index.create().parse(filename, args=args).cursor

        return self._get_vectors_for_cursor(root, filename)
Exemple #4
0
    def run(self, filename, file, clang_cli_options: typed_list(str)=None):
        """
        Runs Clang over the given files and raises/fixes any upcoming issues.

        :param clang_cli_options: Any options that will be passed through to
                                  Clang.
        """
        index = Index.create()
        diagnostics = index.parse(
            filename,
            args=clang_cli_options,
            unsaved_files=[(filename.encode(),
                            ''.join(file).encode())]).diagnostics
        for diag in diagnostics:
            severity = {0: RESULT_SEVERITY.INFO,
                        1: RESULT_SEVERITY.INFO,
                        2: RESULT_SEVERITY.NORMAL,
                        3: RESULT_SEVERITY.MAJOR,
                        4: RESULT_SEVERITY.MAJOR}.get(diag.severity)
            affected_code = tuple(SourceRange.from_clang_range(range)
                                  for range in diag.ranges)

            diffs = None
            fixits = list(diag.fixits)
            if len(fixits) > 0:
                # FIXME: coala doesn't support choice of diffs, for now
                # append first one only, often there's only one anyway
                diffs = {filename: Diff.from_clang_fixit(fixits[0], file)}

                # No affected code yet? Let's derive it from the fix!
                if len(affected_code) == 0:
                    affected_code = diffs[filename].affected_code(filename)

            # Still no affected code? Position is the best we can get...
            if len(affected_code) == 0:
                affected_code = (SourceRange.from_values(
                    diag.location.file.name.decode(),
                    diag.location.line,
                    diag.location.column),)

            yield Result(
                self,
                diag.spelling.decode(),
                severity=severity,
                affected_code=affected_code,
                diffs=diffs)
def skip_test():
    try:
        Index.create()
        return False
    except LibclangError as error:
        return str(error)