Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active December 25, 2015 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pervognsen/6986969 to your computer and use it in GitHub Desktop.
Save pervognsen/6986969 to your computer and use it in GitHub Desktop.
annotate_clang_assembly.py
# Make assembly listings generated by clang -g -S more human readable.
import re
import sys
files = {}
def replace(text, pattern, replacement):
return re.sub(pattern, replacement, text, 0, re.M)
def getline(filename, line):
if filename not in files:
files[filename] = list(open(filename).readlines())
return files[filename][line]
s = open(sys.argv[1]).read()
# Replace .loc directives with source code.
s = replace(s, r"[^\n]+ ## ([^\n]+):(\d+):\d+\n", lambda m: "\n## %s:%s: %s" % (m.group(1), m.group(2), getline(m.group(1), int(m.group(2))-1)))
# Remove clutter.
s = replace(s, r"^Ltmp[^\n]*[\n]", "")
s = replace(s, r"^[\t]##DEBUG_VALUE[^\n]*[\n]", "")
s = replace(s, r"^[\t].cfi[^\n]*[\n]", "")
s = replace(s, r"^## BB[^\n]*[\n]", "")
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment