compile_commands.json解析

将compile_commands.json解析为编译命令,比较烦人的是宏定义的双引号。当文件很大时,直接将文件转换成json对象会造成内存不足,先读取整个文件,再分段解析

import json
import sys
# args: comdbWrapper comdbfile_path
if len(sys.argv) != 2:
    print("usage: comdbWrapper comdbfile_path")
    quit()  
compdb_path=sys.argv[1]
compdb_file=open(compdb_path,"r")
content=compdb_file.read()

end=len(content)
start = content.find("{", 0, end)
stop=content.find("}", start, end)
while -1 != start:
    jsonstr=content[start:stop]+"}"
    #print(jsonstr)
    jsonobject=json.loads(jsonstr)
    command=""
    outfile=''
    for arg in jsonobject["arguments"]:
        if arg == "cc":
            command+="clang -flto -g "
        elif arg == "-O2":
            command+="-O0 "
        elif arg.find(".o") != -1:
            outfile=str(arg).replace(".o", ".bc")
            command+=outfile+" "
        elif arg == "-o":
            command+="-emit-llvm -o"
        elif len(arg)>2 and arg[0:2]=="-D" and arg.find("=\"") != -1:
            command+=str(arg[0:len(arg)-1]).replace('''="''','''="\\"''')+'''\\""'''+" "
        elif arg.find("fsanitize") != -1:
            continue
        else:
            command+=arg+" "
    print(command)
    optcmd="opt -mem2reg " + outfile + " -o " + outfile.replace(".bc", ".opt")
    print(optcmd)
    #print(jsonobject["file"])
    #print("{} {}".format(start, stop))
    start = content.find("{", stop, end)
    stop = content.find("}", start, end)

你可能感兴趣的:(compile_commands.json解析)