def run(self, cmd, code): """Override the default run command to inject preprocessor step.""" # print('=== BEGIN LINTER DEBUG ===') # print('ORIGINAL_CODE:') # o_lines = code.splitlines(False) # o_n = 0 # for o_l in o_lines: # print('{0} |{1}'.format(get_auto_padding(o_n), o_l)) # o_n += 1 mcpp_path = find_mcpp() # if mcpp_path is not None else Linter.communicate(self,cmd,code) if mcpp_path is not None: opt = '-W0' mcpp_output = Linter.communicate(self, (mcpp_path, opt), code) lines = mcpp_output.splitlines(False) line_number = 0 OutputTuple = namedtuple( 'OutputTuple', 'mcpp_in_line\ orig_line\ file') MCPPMessage = namedtuple( 'MCPPMessage', 'source\ line\ message') preproc_bank = [] # print('MCPP Output:') mcpp_messages = [] for line in lines: # print('{0} |{1}'.format(get_auto_padding(line_number), line)) if (line.startswith('#line')): message = line.split(' ') # print('message:{0}'.format(message)) preproc_bank.append( OutputTuple( mcpp_in_line=line_number, orig_line=int(message[1]), file=message[2])) elif (line.startswith('<stdin>:')): # Capture mcpp output and store into a variable message = line.split(':') mcpp_messages.append( MCPPMessage( source=message[0], line=message[1], message=message[3])) line_number += 1 # print("DEBUG:: preproc_bank: {0}".format(preproc_bank[0])) linter_result = Linter.communicate(self, cmd, mcpp_output) # print("DEBUG:: LINTER_OUT output:\n{0}".format(linter_result)) # Go through every error and replace the line number (from the # inlined file) with the one from the script we fed the # precompiler, to restore the link between the error and the code # inside the editor so that we can properly show linting visual # hints. linter_output_lines = linter_result.splitlines(False) # print('LINTER_OUT:{0}'.format(linter_output_lines)) # Get line at which the current file was inserted # TODO: make sure multi-include works fixed_output_lines = [] p = re.compile(r"^\s*?(ERROR|WARN)\:\:\s\(\s*(\d*)\.*$") for iter_line in linter_output_lines: # print('LINE:[{0}]'.format(iter_line)) if iter_line.startswith("TOTAL::") is False: tokens = iter_line.split(',') # print('Tokens:[{0}]'.format(tokens)) rres = p.match(tokens[0]) if rres is not None: number = int(rres.group(2)) # print("number: '{0}'".format(number)) result = getLastOffset(preproc_bank, number) offset = result[0] # print("Offset: {0}".format(offset)) # tokminoff = str(number - int(offset)) tokminoff = str(number - int(offset)) new_line = re.sub(str(number), tokminoff, iter_line) # print("result[1]="+result[1]) if result[1] != '"<stdin>"': index = getLastStdin(preproc_bank, number) new_number = preproc_bank[ index + 1].mcpp_in_line + 1 # noqa: E501 offset = getLastOffset(preproc_bank, new_number)[0] tokminoff = str(new_number - int(offset)) token_match = rres.group(1) new_line = '{0}:: ({1:>3}, 1): in file {2}: {3}'\ .format(token_match, tokminoff, result[1], new_line) fixed_output_lines.append(new_line) else: continue continue else: fixed_output_lines.append(iter_line) # print("New Lines: {0}".format(fixed_output_lines)) # Transform back into a string # Add messages from MCPP first mcpp_msg_out = "" for this_tuple in mcpp_messages: mcpp_msg_out += "ERROR:: ({0},1): {1}\n"\ .format(this_tuple.line, this_tuple.message) # print("MCPP:\n{0}".format(mcpp_msg_out)) linter_result = mcpp_msg_out linter_result += "".join(str(x) + "\n" for x in fixed_output_lines) print("LINTER:\n" + linter_result) else: linter_result = Linter.communicate(self, cmd, code) # print("DEBUG:: Linter output: {0}".format(linter_result)) # print('=== END LINTER DEBUG ===') return linter_result
def run(self, cmd, code): """Override the default run command to inject preprocessor step.""" # print('=== BEGIN LINTER DEBUG ===') # print('ORIGINAL_CODE:') # o_lines = code.splitlines(False) # o_n = 0 # for o_l in o_lines: # print('{0} |{1}'.format(get_auto_padding(o_n), o_l)) # o_n += 1 mcpp_path = find_mcpp() # if mcpp_path is not None else Linter.communicate(self,cmd,code) if mcpp_path is not None: # Capture mcpp output and store into a variable opt = '-W0' mcpp_output = Linter.communicate(self, mcpp_path + " " + opt, code) lines = mcpp_output.splitlines(False) line_number = 0 OutputTuple = namedtuple( 'OutputTuple', 'mcpp_in_line\ orig_line\ file') preproc_bank = [] # print('MCPP Output:') for line in lines: # print('{0} |{1}'.format(get_auto_padding(line_number), line)) if (line.startswith('#line')): message = line.split(' ') # print('message:{0}'.format(message)) preproc_bank.append( OutputTuple(mcpp_in_line=line_number, orig_line=int(message[1]), file=message[2])) line_number += 1 code = mcpp_output # print("DEBUG:: preproc_bank: {0}".format(preproc_bank[2])) linter_result = Linter.communicate(self, cmd, code) # print("DEBUG:: LINTER_OUT output:\n{0}".format(linter_result)) if mcpp_path is not None: # Go through every error and replace the line number (from the # inlined file) with the one from the script we fed the # precompiler, to restore the link between the error and the code # inside the editor so that we can properly show linting visual # hints. linter_output_lines = linter_result.splitlines(False) # print('LINTER_OUT:{0}'.format(linter_output_lines)) # Get line at which the current file was inserted # TODO: make sure multi-include works fixed_output_lines = [] p = re.compile('^\s*(ERROR|WARN)\:\:\s\(\s*(\d*)\.*$') for iter_line in linter_output_lines: # print('LINE:[{0}]'.format(iter_line)) if iter_line.startswith("TOTAL::") is False: tokens = iter_line.split(',') # print('Tokens:[{0}]'.format(tokens)) token = tokens[0] # print("Token:{0}".format(token)) number = int(p.match(token).group(2).strip()) # print("number: '{0}'".format(number)) offset = getLastOffset(preproc_bank, number) # print("Offset: {0}".format(offset)) tokminoff = str(number - int(offset)) # print("Token - offset: {0}".format(tokminoff)) new_line = re.sub(str(number), tokminoff, iter_line) # print("New Line: {0}".format(new_line)) fixed_output_lines.append(new_line) continue else: fixed_output_lines.append(iter_line) # print("New Lines: {0}".format(fixed_output_lines)) # Transform back into a string linter_result = "".join(str(x) + "\n" for x in fixed_output_lines) # print("DEBUG:: Linter output: {0}".format(linter_result)) # print('=== END LINTER DEBUG ===') return linter_result