in cbmc_viewer/markup_trace.py [0:0]
def lookup(self, path, line):
"""A line of source code."""
if line <= 0: # line numbers are 1-based
logging.info("CodeSnippet lookup: line number not positive: %s", line)
return None
line -= 1 # list indices are 0-based
try:
if path not in self.source:
with open(os.path.join(self.root, path)) as code:
self.source[path] = code.read().splitlines()
except FileNotFoundError as error:
if srcloct.is_builtin(path): # <builtin-library-malloc>, etc.
return None
raise UserWarning(
"CodeSnippet lookup: file not found: {}".format(path)
) from error
# return the whole statement which may be broken over several lines
snippet = ' '.join(self.source[path][line:line+5])
snippet = re.sub(r'\s+', ' ', snippet).strip()
idx = snippet.find(';') # end of statement
if idx >= 0:
return html.escape(snippet[:idx+1])
idx = snippet.find('}') # end of block
if idx >= 0:
return html.escape(snippet[:idx+1])
return html.escape(snippet) # statement extends over more that 5 lines