Exemplo n.º 1
0
def parse_cbmc_json(json_data, root):
    """Parse json output of goto-analyzer --reachable-functions --json."""

    root = srcloct.abspath(root)
    reachable = {}
    for function in json_data:
        func_name = function['function']
        if func_name.startswith('__CPROVER'):
            continue

        file_name = function.get('file')
        if file_name is None:
            logging.error(
                'Skipping reachable function with invalid source location: %s',
                function)
            continue

        file_name = srcloct.abspath(file_name)
        if srcloct.is_builtin(file_name):
            continue
        if not file_name.startswith(root):
            continue

        path = srcloct.relpath(file_name, root)
        reachable[path] = reachable.get(path, set())
        reachable[path].add(func_name)

    return reachable
Exemplo n.º 2
0
def do_make_symbol(viewer_symbol, make_source, tags_method, goto, wkdir,
                   srcdir, files):
    """Implementation of make-symbol."""

    wkdir = srcloct.abspath(wkdir) if wkdir else None
    srcdir = srcloct.abspath(srcdir) if srcdir else None

    # Command line options may enable more than one way
    # to generate the symbol table, so choose the one
    # that gives the most "accurate" results.

    if viewer_symbol:
        logging.info("Symbols by SymbolFromJson")
        return SymbolFromJson(viewer_symbol)

    if make_source:
        sources = sourcet.SourceFromJson(make_source)
        srcdir = sources.root
        files = sources.files

    if srcdir and files:
        # tags_method was set to a reasonable value by optionst.defaults()
        # if it was not specified on the command line.
        if tags_method == Tags.CTAGS:
            logging.info("Symbols by SymbolFromCtags")
            return SymbolFromCtags(srcdir, files)
        if tags_method == Tags.ETAGS:
            logging.info("Symbols by SymbolFromEtags")
            return SymbolFromEtags(srcdir, files)

    if goto and wkdir and srcdir:
        logging.info("Symbols by SymbolFromGoto")
        return SymbolFromGoto(goto, wkdir, srcdir)

    fail("Unable to generate a symbol table (is ctags installed?).")
Exemplo n.º 3
0
def source_files(goto, wkdir, srcdir=None):
    """Source files appearing in symbol table.

    Source file path names in symbol table are absolute or relative to
    wkdir.  If srcdir is given, return only files under srcdir.
    """

    wkdir = srcloct.abspath(wkdir)
    srcs = [dfn['file']
            for dfn in parse_symbol_table(symbol_table(goto), wkdir)]
    srcs = [src for src in srcs if src and not srcloct.is_builtin(src)]

    if srcdir:
        srcdir = srcloct.abspath(srcdir)
        srcs = [src for src in srcs if src.startswith(srcdir)]

    return sorted(set(srcs))
Exemplo n.º 4
0
def parse_cbmc_xml(xml_data, root):
    """Parse the xml output of cbmc --show-loops --xml-ui."""

    # Each loop element contains loop-id and location elements
    root = srcloct.abspath(root)
    return {
        loop.find("loop-id").text: srcloct.xml_srcloc(loop.find("location"),
                                                      root)
        for loop in xml_data.iter("loop")
    }
Exemplo n.º 5
0
def do_make_source(viewer_source, goto, source_method, srcdir, wkdir, exclude,
                   extensions):
    """The implementation of make-source."""

    wkdir = srcloct.abspath(wkdir) if wkdir else None
    srcdir = srcloct.abspath(srcdir) if srcdir else None

    if viewer_source:
        logging.info("Sources by SourceFromJson")
        return SourceFromJson(viewer_source)

    # source_method was set to a reasonable value by optionst.defaults()
    # if it was not specified on the command line.

    if source_method == Sources.GOTO:
        if goto is None or wkdir is None or srcdir is None:
            fail("make-source: expected --goto and --srcdir and --wkdir")
        logging.info("Sources by SourceFromGoto")
        return SourceFromGoto(goto, wkdir, srcdir)

    if source_method == Sources.MAKE:
        if srcdir is None or wkdir is None:
            fail("make-source: expected --srcdir and --wkdir")
        logging.info("Sources by SourceFromMake")
        return SourceFromMake(srcdir, wkdir)

    if source_method == Sources.FIND:
        if srcdir is None:
            fail("make-source: expected --srcdir")
        logging.info("Sources by SourceFromFind")
        return SourceFromFind(srcdir, exclude, extensions)

    if source_method == Sources.WALK:
        if srcdir is None:
            fail("make-source: expected --srcdir")
        logging.info("Sources by SourceFromWalk")
        return SourceFromWalk(srcdir, exclude, extensions)

    fail("Unable to list source files.")
Exemplo n.º 6
0
    def __init__(self, root, files, sloc=False):

        # Absolute path to the source root (initialized by subclass)
        self.root = srcloct.abspath(root)

        # Absolute paths to all source files (initialized by subclass)
        self.all_files = sorted({srcloct.abspath(path) for path in files})

        # Relative paths to those source files under the source root
        self.files = [
            srcloct.normpath(path[len(self.root) + 1:])
            for path in self.all_files if path.startswith(self.root)
        ]

        # Source code statistics generated by sloc
        if sloc:
            lines_of_code = self.sloc(self.files, self.root)
            if lines_of_code:
                self.lines_of_code = lines_of_code

        if not self.all_files:
            raise UserWarning("The list of source files is empty.")

        self.validate()
Exemplo n.º 7
0
def symbol_definitions(goto, wkdir, srcdir=None):
    """Symbol definitions appearing in symbol table.

    Source file path names in symbol table are absolute or relative to
    wkdir.  If srcdir is given, return only symbols defined in files
    under srcdir.
    """

    wkdir = srcloct.abspath(wkdir)
    srcdir = srcloct.abspath(srcdir)

    symbols = {}
    for dfn in parse_symbol_table(symbol_table(goto), wkdir):
        sym, src, num = dfn['symbol'], dfn['file'], dfn['line']

        if sym is None or src is None or num is None:
            logging.info("Skipping symbol table entry: %s: %s, %s",
                         sym, src, num)
            continue

        if srcdir and not src.startswith(srcdir):
            logging.info("Skipping symbol table entry: %s: %s, %s",
                         sym, src, num)
            continue

        srcloc = srcloct.make_srcloc(src, None, num, wkdir, srcdir)
        if sym in symbols and srcloc != symbols[sym]:
            logging.warning("Skipping redefinition of symbol name: %s", sym)
            logging.warning("  Old symbol %s: file %s, line %s",
                            sym, symbols[sym]["file"], symbols[sym]["line"])
            logging.warning("  New symbol %s: file %s, line %s",
                            sym, srcloc["file"], srcloc["line"])
            continue
        symbols[sym] = srcloc

    return symbols
def load_cbmc_xml(xmlfile, root):
    """Load a json file produced by cbmc --show-properties --xml-ui."""

    xml_data = parse.parse_xml_file(xmlfile, fail=True)
    assert xml_data is not None

    root = srcloct.abspath(root)
    return {
        property.get("name"): {
            "class": property.get("class"),
            "description": property.find("description").text,
            "expression": property.find("expression").text,
            'location': srcloct.xml_srcloc(property.find("location"), root)
        }
        for property in xml_data.iter("property")
    }
Exemplo n.º 9
0
def parse_cbmc_json(json_data, root):
    """Parse the json output of cbmc --show-loops --json-ui."""

    # Search cbmc output for {"loops": [ LOOP ]}
    loops = [json_map for json_map in json_data if "loops" in json_map]
    if len(loops) != 1:
        raise UserWarning(
            "Expected 1 set of loops in cbmc output, found {}".format(
                len(loops)))

    # Each LOOP is a dict that gives a loop name and location.
    root = srcloct.abspath(root)
    return {
        loop['name']: srcloct.json_srcloc(loop['sourceLocation'], root)
        for loop in loops[0]["loops"]
    }
Exemplo n.º 10
0
def load_cbmc_json(jsonfile, root):
    """Load a json file produced by cbmc --show-properties --json-ui."""

    json_data = parse.parse_json_file(jsonfile, fail=True)
    assert json_data is not None

    # Search cbmc output for {"properties": [ PROPERTY ]}
    asserts = [json_map for json_map in json_data if "properties" in json_map]
    if len(asserts) != 1:
        raise UserWarning("Expected 1 set of properties in cbmc output, "
                          "found {}".format(len(asserts)))

    # Each PROPERTY a loop property and definition
    root = srcloct.abspath(root)
    return {
        property['name']: {
            'class': property['class'],
            'description': property['description'],
            'expression': property['expression'],
            'location': srcloct.json_srcloc(property['sourceLocation'], root)
        }
        for property in asserts[0]["properties"]
    }
Exemplo n.º 11
0
 def __init__(self, json_files, root):
     root = srcloct.abspath(root)
     super().__init__(
         [load_cbmc_json(json_file, root) for json_file in json_files])
Exemplo n.º 12
0
 def __init__(self, json_files, root):
     root = srcloct.abspath(root)
     super(TraceFromCbmcJson, self).__init__(
         [parse_json_traces(json_file, root) for json_file in json_files])
Exemplo n.º 13
0
 def __init__(self, xml_files, root):
     root = srcloct.abspath(root)
     super(TraceFromCbmcXml, self).__init__(
         [parse_xml_traces(xml_file, root) for xml_file in xml_files])
Exemplo n.º 14
0
 def __init__(self, text_files, root, wkdir):
     root = srcloct.abspath(root)
     super(TraceFromCbmcText, self).__init__([
         parse_text_traces(text_file, root, wkdir)
         for text_file in text_files
     ])
Exemplo n.º 15
0
 def __init__(self, xml_files, root):
     root = srcloct.abspath(root)
     super().__init__(
         [load_cbmc_xml(xml_file, root) for xml_file in xml_files])
Exemplo n.º 16
0
 def __init__(self, json_files, root):
     root = srcloct.abspath(root)
     super(CoverageFromCbmcJson, self).__init__(
         [load_cbmc_json(json_file, root) for json_file in json_files]
     )
Exemplo n.º 17
0
 def __init__(self, xml_files, root):
     root = srcloct.abspath(root)
     super(CoverageFromCbmcXml, self).__init__(
         [load_cbmc_xml(xml_file, root) for xml_file in xml_files]
     )