Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sublime, sublime_plugin
- import os
- import sys
- import subprocess
- import xml.etree.ElementTree as ET
- from collections import defaultdict
- class Error:
- def __init__(self, id, severity, msg, file, line):
- self.id = id
- self.msg = msg
- self.severity = severity
- self.file = file
- self.line = line
- def __eq__(self, other):
- return (isinstance(other, self.__class__)
- and self.__dict__ == other.__dict__)
- def __ne__(self, other):
- return not __eq__(other)
- def __hash__(self):
- return hash((self.id, self.msg, self.severity, self.file, self.line))
- def __str__(self):
- return "{0}, {1}\n {2}, {3}, {4}\n".format(self.file, self.line, self.severity, self.id, self.msg)
- # make args to cppcheck
- def process_file(path):
- if not path.endswith(('.cpp', '.c', '.h')):
- return None
- command = ' '.join(["cppcheck",
- "--enable=warning,style,information,performance",
- "--xml-version=2",
- "--template=gcc",
- path])
- res = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
- return res.communicate()[0]
- def parse_output(output):
- errors = []
- root = ET.fromstring(output)
- for err in root.iter('error'):
- id = err.get('id')
- msg = err.get('msg')
- severity = err.get('severity')
- try:
- file = err.find('location').get('file')
- line = err.find('location').get('line')
- errors.append(Error(id, severity, msg, file, line))
- except AttributeError:
- pass
- return errors
- def find_errors(msg, lst):
- if msg != None:
- out = parse_output(msg)
- lst.extend(out)
- return lst
- path = ""
- class CheckCommand(sublime_plugin.TextCommand):
- def run(self, edit, **args):
- file_path = self.view.file_name()
- v = self.view.window().new_file()
- errors_list = []
- if args['use_dir']:
- path = os.path.dirname(file_path)
- for root, dirs, files in os.walk(path):
- for file in files:
- output = process_file(os.path.join(root, file))
- errors_list = find_errors(output, errors_list)
- else:
- path = file_path
- output = process_file(path)
- errors_list = find_errors(output, errors_list)
- pt1 = pt2 = 0
- errors_d = defaultdict(int)
- errors_set = set(errors_list)
- for err in errors_set:
- errors_d[err.severity] += 1
- pt2 = pt1 + v.insert(edit, pt1, str(err) + '\n')
- v.add_regions('err' + str(pt2), [sublime.Region(pt1, pt2)])
- pt1 = pt2
- pt2 = pt1 + v.insert(edit, pt1, "Found " + str(len(errors_list)) + " errors\n")
- pt1 = pt2
- for key, val in errors_d.items():
- pt2 = pt1 + v.insert(edit, pt1, str(key) + ": " + str(val) + "\n")
- pt1 = pt2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement