#!/usr/bin/env python # # Compare the AST of Cppcheck and Clang # # Example usage: # cd cppcheck/tools # g++ `llvm-config-3.8 --cxxflags --ldflags` -lclang -o clang-ast clang-ast.cpp # python compare-ast-clang-and-cppcheck.py lib/token.cpp # # If you get such output: # - # Then that means there is a missing function in the Cppcheck SymbolDatabase # import subprocess import sys sys.path.insert(0, '../addons') import cppcheckdata CLANG_AST = './clang-ast' CPPCHECK = '../cppcheck' def clang_ast(sourcefile): p = subprocess.Popen([CLANG_AST,sourcefile], stdout=subprocess.PIPE, stderr=subprocess.PIPE) comm = p.communicate() ret = [] for line in comm[0].split('\n'): while len(line)>1 and line[-1] in ' \r\n': line = line[:-1] if line.find('/usr/') > 0: continue if line.startswith('' ret.append(s) for scope in cfg.scopes: if scope.type != 'Function': continue argStart = scope.bodyStart while argStart and argStart.str != '(': argStart = argStart.previous s = '' if s not in ret: ret.append(s) ret.sort() return ret print('Clang AST...') ast1 = clang_ast(sys.argv[1]) print('Cppcheck AST...') ast2 = cppcheck_ast(sys.argv[1]) #for func in ast2: # print(func) print('Compare...') for func in ast1: if func not in ast2: print('- ' + func) #for func in ast2: # if func not in ast1: # print('+ ' + func)