1 Star 0 Fork 3

FreeCAD/shaper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
correction_auto_2.py 33.26 KB
一键复制 编辑 原始数据 按行查看 历史
vsr 提交于 2021-03-25 10:10 . Copyright update 2021
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
# Copyright (C) 2014-2021 CEA/DEN, EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
import os, os.path, sys, re, argparse
from utils import *
#### définition des hooks.
# chaque hook doit :
# * avoir le prototype suivant : def <nom_hook>(<unicode>) avec <unicode> la ligne de texte à corriger
# * retourner deux valeurs : la ligne corrigée, en unicode et un booléen indiquant s'il faut faire un check humain avant de corriger
# * être inséré dans HOOKS_DICT en y faisant correspondre un dico explicitant les critères de séléction de ce hook
####
### whitespace/semicolon ###
def extra_space_before_last_semicolon(ligne, previous_ligne = None, warning=None):
liste=ligne.split(';\n')
ligne2=liste[0]
ligne3=ligne2.rstrip()
ligne=ligne3 + ';\n'
return ligne, previous_ligne, False
def missing_space_after_semicolon(ligne, previous_ligne = None, warning=None):
ligne=ligne.replace(';','; ')
return ligne, previous_ligne, False
#########################
### whitespace/operators ###
def missing_space_around_operator_egal(ligne, previous_ligne = None, warning=None):
i=ligne.find('=')
nextchar=ligne[i+1]
previouschar=ligne[i-1]
if nextchar not in OPERATORS and previouschar not in OPERATORS:
if nextchar.isspace() == False and previouschar.isspace()==True:
ligne=ligne.replace('=', '= ')
elif nextchar.isspace() == True and previouschar.isspace()==False:
ligne=ligne.replace('=', ' =')
elif nextchar.isspace() == False and previouschar.isspace()==False:
ligne=ligne.replace('=', ' = ')
else:
pass
return ligne, previous_ligne, False
def extra_space_for_operator_add(ligne, previous_ligne = None, warning=None):
while ligne.find('++ ') > -1:
ligne = ligne.replace('++ ','++')
while ligne.find(' ++') > -1 and ligne.find('; ++') == -1:
ligne = ligne.replace(' ++','++')
return ligne, previous_ligne, False
def extra_space_for_operator_diff(ligne, previous_ligne = None, warning=None):
while ligne.find('! ') > -1:
ligne = ligne.replace('! ','!')
return ligne, previous_ligne, False
def missing_space_around_operator_double_chevron(ligne, previous_ligne = None, warning=None):
i=ligne.find('<<')
nextchar=ligne[i+1]
previouschar=ligne[i-1]
if nextchar.isspace() == False and previouschar.isspace()==True:
ligne=ligne.replace('<<', '<< ')
elif nextchar.isspace() == True and previouschar.isspace()==False:
ligne=ligne.replace('<<', ' <<')
elif nextchar.isspace() == False and previouschar.isspace()==False:
ligne=ligne.replace('<<', ' << ')
else:
pass
return ligne, previous_ligne, False
def missing_space_around_operator_simple_chevron(ligne, previous_ligne = None, warning=None):
i=ligne.find('<')
nextchar=ligne[i+1]
previouschar=ligne[i-1]
if nextchar not in OPERATORS and previouschar not in OPERATORS:
if nextchar.isspace() == False and previouschar.isspace()==True:
ligne=ligne.replace('<', '< ')
elif nextchar.isspace() == True and previouschar.isspace()==False:
ligne=ligne.replace('<', ' <')
elif nextchar.isspace() == False and previouschar.isspace()==False:
ligne=ligne.replace('<', ' < ')
else:
pass
return ligne, previous_ligne, False
def missing_space_around_operator_diff_egal(ligne, previous_ligne = None, warning=None):
i=ligne.find('!=')
nextchar=ligne[i+1]
previouschar=ligne[i-1]
if nextchar.isspace() == False and previouschar.isspace()==True:
ligne=ligne.replace('!=', '!= ')
elif nextchar.isspace() == True and previouschar.isspace()==False:
ligne=ligne.replace('!=', ' !=')
elif nextchar.isspace() == False and previouschar.isspace()==False:
ligne=ligne.replace('!=', ' != ')
else:
pass
return ligne, previous_ligne, False
def missing_space_around_operator_double_egal(ligne, previous_ligne = None, warning=None):
i=ligne.find('==')
nextchar=ligne[i+1]
previouschar=ligne[i-1]
if nextchar.isspace() == False and previouschar.isspace()==True:
ligne=ligne.replace('==', '== ')
elif nextchar.isspace() == True and previouschar.isspace()==False:
ligne=ligne.replace('==', ' ==')
elif nextchar.isspace() == False and previouschar.isspace()==False:
ligne=ligne.replace('==', ' == ')
else:
pass
return ligne, previous_ligne, False
#########################
### whitespace/comments ###
def space_between_comments_and_code(ligne, previous_ligne = None, warning=None):
if ligne.find('//')>=0 :
ligne=ligne.replace('//', ' //')
elif ligne.find(' //')>=0 :
ligne=ligne.replace(' //', ' //')
else:
pass
return ligne, previous_ligne, False
def space_between_comments_and_double_slash(ligne, previous_ligne = None, warning=None):
return ligne.replace('//', '// '), previous_ligne, False
#########################
### legal/copyright ###
def legal_copyright(ficpath, ficline, warning=None):
replace_fic_copyright(ficpath)
#########################
### build/header_guard
def ifndef_header(ficpath, ficline, warning):
replace_fic_ifndef(ficpath, ficline, warning, "wrong_style")
#def no_ifndef_header(ficpath, ficline, warning):
# replace_fic_ifndef(ficpath, ficline, warning, "no_guard_found")
def endif_line(ligne, previous_ligne = None, warning=None):
liste1=warning.split('// ')
ligne1=liste1[1]
liste2=ligne1.split('"')
header_guard=liste2[0]
ligne= '#endif // ' + header_guard +'\n'
return ligne, previous_ligne, False
#########################
### whitespace/end_of_ligne ###
def whitespace_end_of_line(ligne, previous_ligne = None, warning=None):
ligne = ligne.rstrip()+'\n'
return ligne, previous_ligne, False
#########################
### whitespace/comma ###
def missing_space_after_comma(ligne, previous_ligne = None, warning=None):
ligne=ligne.replace(',',', ')
return ligne, previous_ligne, False
#########################
### readability/namespace ###
def namespace_should_be_terminated(ligne, previous_ligne = None, warning=None):
ligne2=ligne.rstrip('\n')
liste = warning.split('"')
namespace=liste[1]
if ligne.find('\r')>=0:
ligne=ligne2+' '+namespace+'\r\n'
else:
ligne=ligne2+' '+namespace+'\n'
return ligne, previous_ligne, False
def anonymous_namespace_should_be_terminated(ligne, previous_ligne = None, warning=None):
ligne2=ligne.rstrip('\n')
ligne=ligne2+' // namespace\n'
return ligne, previous_ligne, False
#########################
### whitespace/parens ###
def extra_space_after_opening_paranthesis(ligne, previous_ligne = None, warning=None):
while ligne.find('( ') > -1:
ligne = ligne.replace('( ','(')
return ligne, previous_ligne, False
def extra_space_before_closing_paranthesis(ligne, previous_ligne = None, warning=None):
while ligne.find(' )') > -1:
ligne = ligne.replace(' )',')')
return ligne, previous_ligne, False
def extra_space_before_opening_paranthesis_in_function_call(ligne, previous_ligne = None, warning=None):
while ligne.find(' (') > -1 and ligne.find(', (')==-1:
ligne = ligne.replace(' (','(')
return ligne, previous_ligne, False
def missing_space_before_opening_parenthesis_in_for(ligne, previous_ligne = None, warning=None):
return ligne.replace('for(','for ('), previous_ligne, False
def missing_space_before_opening_parenthesis_in_if(ligne, previous_ligne = None, warning=None):
return ligne.replace('if(','if ('), previous_ligne, False
def missing_space_before_opening_parenthesis_in_switch(ligne, previous_ligne = None, warning=None):
return ligne.replace('switch(','switch ('), previous_ligne, False
def missing_space_before_opening_parenthesis_in_while(ligne, previous_ligne = None, warning=None):
return ligne.replace('while(','while ('), previous_ligne, False
def mismatching_spaces_inside_paranthesis(ligne, previous_ligne = None, warning=None):
while ligne.find('( ') > -1:
ligne = ligne.replace('( ','(')
while ligne.find(' )') > -1:
ligne = ligne.replace(' )',')')
return ligne, previous_ligne, False
#########################
### whitespace/newline ###
def else_should_be_previous_line(ligne, previous_ligne=None, warning=None):
return position_of_else(ligne, previous_ligne, warning)
#########################
### whitespace/indent ###
def missing_space_before_public_protected_private(ligne, previous_ligne = None, warning=None):
# Ajout d'un espace avant public:, protected: et :private
if re.match("public.*:|protected.*:|private.*:", ligne):
ligne = " " + ligne
return ligne, previous_ligne, False
def wrong_number_of_spaces_while_indenting(ligne, previous_ligne = None, warning=None):
# Le nombre d'espaces pour l'indentation doit être pair
if re.match("\s{1}\S+|\s{3}\S+|\s{5}\S+|\s{7}\S+|\s{9}\S+", ligne):
if not re.match(" public.*:| protected.*:| private.*:| \*", ligne):
ligne = " " + ligne
# Suppression des espaces superflus sur une ligne sans code
if ligne.isspace():
ligne = "\n"
# Remplacement des tabulations par 8 espaces en début de ligne
ligne = ligne.replace("\t", " ")
return ligne, previous_ligne, False
#########################
### whitespace/ending_newline ###
def no_newline_at_the_end_of_file(ligne, previous_ligne = None, warning=None):
ligne += '\n\n'
return ligne, None, False
#################################
### build/storage_class ###
def order_of_storage_class_specifier(ligne, previous_ligne = None, warning=None):
if re.match("\s*const static", ligne):
# Inversion de const static en static const
ligne = ligne.replace("const static", "static const")
return ligne, previous_ligne, False
###########################
### whitespace/tab ###
def use_spaces_instead_of_tab(ligne, previous_ligne = None, warning=None):
if re.match(".*\t", ligne):
# Remplacement des tabulations par 8 espaces
ligne = ligne.replace("\t", " ")
return ligne, previous_ligne, False
######################
### readability/braces ###
def no_semicolon_after_brace(ligne, previous_ligne = None, warning=None):
return ligne.replace('};','}'), previous_ligne, False
def position_of_else(ligne, previous_ligne=None, warning=None):
'''Remonte une ligne du type } else {'''
if '}' not in previous_ligne:
# Si on ne trouve pas de '}' sur la ligne précédente, on ne fait rien
return ligne, None, False
m = re.match("(.*)else(.*)", ligne)
previous_ligne = previous_ligne.rstrip("\n") + ' else' + m.group(2) + '\n'
ligne = ''
return ligne, previous_ligne, False
##########################
### whitespace/braces ###
def position_of_opening_brace(ligne, previous_ligne=None, warning=None):
'''Remonte le { sur la ligne du dessus'''
m = re.match("(\s*){(.*)", ligne)
# On ne fait rien si on trouve un commentaire sur la ligne précédente (ce sera une correction manuelle)
if previous_ligne.find('//') == -1:
previous_ligne = previous_ligne.rstrip("\n") + ' {' + m.group(2) + '\n'
ligne = ''
else:
print("The previous line contains a comment, fixing has to be manual.")
return ligne, previous_ligne, False
def missing_space_before_opening_brace(ligne, previous_ligne = None, warning=None):
m = re.match("(.+)(\S){(.*)", ligne)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
print('group_2', m.group(2))
print('group_3', m.group(3))
ligne = m.group(1) + m.group(2) + ' {' + m.group(3) + '\n'
return ligne, previous_ligne, False
#########################
def missing_space_before_else(ligne, previous_ligne = None, warning=None):
m = re.match("(.+)(\S)else(.*)", ligne)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
print('group_2', m.group(2))
print('group_3', m.group(3))
ligne = m.group(1) + m.group(2) + ' else' + m.group(3) + '\n'
return ligne, previous_ligne, False
### runtime/references ###
def make_const_reference(ficpath, ligne, previous_ligne = None, warning=None):
""" Adding keyword 'const' """
print("ficpath = ", ficpath)
print("ligne = ", ligne)
print("warning = ", warning)
m = re.match("(.+)Is this a non-const reference\? If so, make const or use a pointer: (.+) (.+)", warning)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
print('group_2', m.group(2))
print('group_3', m.group(3))
arg_to_modify = m.group(2)
ligne = ligne.replace(arg_to_modify, "const "+arg_to_modify)
# Répercution des corrections dans le fichier ".cpp" correspondant si c'est un fichier ".h"
if ficpath.find('.h') > -1:
cpp_file_path = ficpath.replace('.h', '.cpp')
make_const_reference_cpp_file(cpp_file_path, arg_to_modify)
global H_FILE_MAKE_CONST_REFERENCE_MODIFIED
H_FILE_MAKE_CONST_REFERENCE_MODIFIED = True
else:
print("ERROR : The following pattern was not found : 'Is this a non-const reference? If so, make const or use a pointer:'")
return ligne, previous_ligne, False
def make_const_reference_cpp_file(ficpath, arg_to_modify):
if not os.path.isfile(ficpath):
print("WARNING : The file ", ficpath, " doesn't exist, manual fixing is required in methods of the file ", ficpath.replace('.cpp', '.h'), " in which arguments have been declared 'const'")
return
fic = open(get_src_path(ficpath),'r')
liste = fic_readlines(fic)
new_liste = []
for ligne in liste:
# Recherche de l'argument à modifier
if(ligne.find(arg_to_modify) > -1 and ligne.find('const '+arg_to_modify) == -1):
new_liste.append(ligne.replace(arg_to_modify, "const "+arg_to_modify))
else:
new_liste.append(ligne)
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
##########################
### runtime/int ###
def replace_short_by_int16(ligne, previous_ligne = None, warning=None):
ligne = ligne.replace('short', 'int16_t')
return ligne, None, False
def replace_long_by_int64(ligne, previous_ligne = None, warning=None):
ligne = ligne.replace('long', 'int64_t')
return ligne, None, False
###################
### runtime/explicit ###
def make_constructor_explicit(ligne, previous_ligne = None, warning=None):
m = re.match("(\s*)(.+)", ligne)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
print('group_2', m.group(2))
ligne = ligne.replace(m.group(2), 'explicit '+m.group(2))
return ligne, None, False
########################
### build/include ###
def cpp_file_should_include_h_file(ficpath, ficline, warning):
fic = open(get_src_path(ficpath),'r')
liste = fic_readlines(fic)
new_liste = []
m = re.match("(.+) should include its header file (.+) (.+)", warning)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
print('group_2', m.group(2))
# Nouveau chemin du fichier .h
new_h_file_path = m.group(2)
# Nom du fichier .h
h_file_name = os.path.basename(new_h_file_path)
# Recherche de la ligne à modifier
for ligne in liste:
m2 = re.match("#include.*"+h_file_name+".*", ligne)
if(m2):
print("FOUND : ", ligne)
new_liste.append("#include \""+new_h_file_path+"\"\n")
else:
print("NOT FOUND : ", ligne)
new_liste.append(ligne)
else:
print("ERROR : Pattern not found : \"should include its header file\"")
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
def _h_file_already_included(ligne, previous_ligne = None, warning=None):
return "", None, False
#####################
### whitespace/blank_line ###
def do_not_leave_blank_line_after_public_protected_private(ficpath, ficline, warning):
fic = open(get_src_path(ficpath),'r')
liste = fic_readlines(fic)
new_liste = []
flag = False
for ligne in liste:
if re.match(".*public.*:|.*protected.*:|.*private.*:", ligne):
# Détection d'une ligne public:, protected: ou :private
flag = True
if flag and ligne.isspace():
# Supprimer les lignes vides après public:, protected: et :private
print("Deleting empty line")
new_liste.append("")
continue
if not ligne.isspace() and not re.match(".*public.*:|.*protected.*:|.*private.*:", ligne):
flag = False
new_liste.append(ligne)
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
def do_not_leave_blank_line_at_the_start_of_code_block(ficpath, ficline, warning):
fic = open(get_src_path(ficpath),'r')
liste = fic_readlines(fic)
new_liste = []
flag = False
for ligne in liste:
if re.match(".*{", ligne):
# Détection d'un début de bloc
flag = True
if flag and ligne.isspace():
# Supprimer les lignes vides après un début de bloc
print("Deleting empty line")
new_liste.append("")
continue
if not ligne.isspace() and not re.match(".*{", ligne):
flag = False
new_liste.append(ligne)
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
def do_not_leave_blank_line_at_the_end_of_code_block(ficpath, ficline, warning):
fic = open(get_src_path(ficpath),'r')
liste = fic_readlines(fic)
new_liste = []
nb_blank_lines = 0
for ligne in liste:
if re.match(".*}", ligne):
# Détection d'une fin de bloc -> suppression des nb_blank_lines lignes vides précédentes
for i in range(0, nb_blank_lines):
new_liste.pop()
if ligne.isspace():
nb_blank_lines += 1
else:
nb_blank_lines = 0
new_liste.append(ligne)
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
def add_blank_line_before_public(ligne, previous_ligne = None, warning=None):
ligne = "\n" + ligne
return ligne, None, False
def add_blank_line_before_protected(ligne, previous_ligne = None, warning=None):
ligne = "\n" + ligne
return ligne, None, False
def add_blank_line_before_private(ligne, previous_ligne = None, warning=None):
ligne = "\n" + ligne
return ligne, None, False
##############################
### build/include_what_you_use ###
def add_include_what_you_use(ficpath, ficline, warning):
"""
Ajoute le #include suggéré dans le warning
"""
fic = open(get_src_path(ficpath), "r")
liste = fic_readlines(fic)
m = re.match("\s*Add (.+) for (.+)", warning)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
include = m.group(1)
# Recherche la ligne dans laquelle ajouter le #include
# On l'ajoutera après le dernier "#include <..."
num_ligne = 0
num_ligne_include_system = 0
num_ligne_include_local = 0
for ligne in liste:
num_ligne += 1
if ligne.find('#include <') > -1:
num_ligne_include_system = num_ligne
elif ligne.find('#include "') > -1:
num_ligne_include_local = num_ligne
num_ligne_include = max(num_ligne_include_system, num_ligne_include_local)
if num_ligne_include == 0:
print("WARNING : #include not found in file ", ficpath)
return
new_liste = []
num_ligne = 0
fic2 = open(get_src_path(ficpath), "r")
liste2 = fic_readlines(fic2)
for ligne in liste2:
num_ligne += 1
new_liste.append(ligne)
if num_ligne == num_ligne_include:
new_liste.append(include+'\n')
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
else:
print("ERROR : Pattern of include_what_you_use not found")
return
##################################
HOOKS_DICT = {
extra_space_before_last_semicolon:{'pattern':'whitespace/semicolon', 'pattern_AND':'Extra space before last semicolon'},
missing_space_after_semicolon:{'pattern':'whitespace/semicolon', 'pattern_AND':'Missing space after'},
missing_space_around_operator_egal:{'pattern':'whitespace/operators', 'pattern_AND':'Missing spaces around = '},
extra_space_for_operator_add:{'pattern':'whitespace/operators', 'pattern_AND':'Extra space for operator ++'},
extra_space_for_operator_diff:{'pattern':'whitespace/operators', 'pattern_AND':'Extra space for operator !'},
missing_space_around_operator_double_chevron:{'pattern':'whitespace/operators', 'pattern_AND':'Missing spaces around << '},
missing_space_around_operator_simple_chevron:{'pattern':'whitespace/operators', 'pattern_AND':'Missing spaces around < '},
missing_space_around_operator_diff_egal:{'pattern':'whitespace/operators', 'pattern_AND':'Missing spaces around !='},
missing_space_around_operator_double_egal:{'pattern':'whitespace/operators', 'pattern_AND':'Missing spaces around =='},
space_between_comments_and_code:{'pattern':'whitespace/comments', 'pattern_AND':'At least two spaces is best between code and comments'},
space_between_comments_and_double_slash:{'pattern':'whitespace/comments', 'pattern_AND':'Should have a space between // and comment '},
legal_copyright:{'pattern':'legal/copyright'}, # Script à n'executer qu'une fois
ifndef_header:{'pattern':'build/header_guard', 'pattern_AND':'#ifndef header guard has wrong style, please use'},
# no_ifndef_header:{'pattern':u'build/header_guard', 'pattern_AND':u'No #ifndef header guard found'},
endif_line:{'pattern':'build/header_guard', 'pattern_AND':'#endif line should be'},
whitespace_end_of_line:{'pattern':'whitespace/end_of_line', 'pattern_AND':'Line ends in whitespace'},
missing_space_after_comma:{'pattern':'whitespace/comma'},
namespace_should_be_terminated:{'pattern':'readability/namespace', 'pattern_AND':'Namespace should be terminated with'},
anonymous_namespace_should_be_terminated:{'pattern':'readability/namespace', 'pattern_AND':'Anonymous namespace should be terminated with'},
extra_space_after_opening_paranthesis:{'pattern':'whitespace/parens', 'pattern_AND':'Extra space after ('},
extra_space_before_closing_paranthesis:{'pattern':'whitespace/parens', 'pattern_AND':'Extra space before )'},
extra_space_before_opening_paranthesis_in_function_call:{'pattern':'whitespace/parens', 'pattern':'Extra space before ( in function call'},
missing_space_before_opening_parenthesis_in_for:{'pattern':'whitespace/parens', 'pattern_AND':'Missing space before ( in for('},
missing_space_before_opening_parenthesis_in_if:{'pattern':'whitespace/parens', 'pattern_AND':'Missing space before ( in if('},
missing_space_before_opening_parenthesis_in_switch:{'pattern':'whitespace/parens', 'pattern_AND':'Missing space before ( in switch('},
missing_space_before_opening_parenthesis_in_while:{'pattern':'whitespace/parens', 'pattern_AND':'Missing space before ( in while('},
mismatching_spaces_inside_paranthesis:{'pattern':'whitespace/parens', 'pattern_AND':'Mismatching spaces inside ()'},
missing_space_before_public_protected_private:{'pattern':'whitespace/indent', 'pattern_AND':' should be indented +1 space inside class'},
wrong_number_of_spaces_while_indenting:{'pattern':'whitespace/indent', 'pattern_AND':'Weird number of spaces at line-start'},
no_newline_at_the_end_of_file:{'pattern':'whitespace/ending_newline'},
order_of_storage_class_specifier:{'pattern':'build/storage_class'},
use_spaces_instead_of_tab:{'pattern':'whitespace/tab'},
no_semicolon_after_brace:{'pattern':'readability/braces', 'pattern_AND':'You don\'t need a ; after a }'},
# position_of_else:{'pattern':u'readability/braces', 'pattern_AND':u'If an else has a brace on one side'},
# else_should_be_previous_line:{'pattern':u'whitespace/newline', 'pattern_AND':u'An else should appear on the same line as the preceding'},
position_of_opening_brace:{'pattern':'whitespace/braces', 'pattern_AND':'{ should almost always be at the end of the previous line'},
missing_space_before_opening_brace:{'pattern':'whitespace/braces', 'pattern_AND':'Missing space before {'},
missing_space_before_else:{'pattern':'whitespace/braces', 'pattern_AND':'Missing space before else'},
make_const_reference:{'pattern':'runtime/references', 'pattern_AND':'Is this a non-const reference? If so, make const or use a pointer'},
make_constructor_explicit:{'pattern':'runtime/explicit'},
cpp_file_should_include_h_file:{'pattern':'build/include', 'pattern_AND':'should include its header file'},
_h_file_already_included:{'pattern':'build/include', 'pattern_AND':'already included at'},
replace_short_by_int16:{'pattern':'runtime/int', 'pattern_AND':'Use int16/int64/etc, rather than the C type short'},
replace_long_by_int64:{'pattern':'runtime/int', 'pattern_AND':'Use int16/int64/etc, rather than the C type long'},
do_not_leave_blank_line_after_public_protected_private:{'pattern':'whitespace/blank_line', 'pattern_AND':'Do not leave a blank line after'},
do_not_leave_blank_line_at_the_start_of_code_block:{'pattern':'whitespace/blank_line', 'pattern_AND':'Redundant blank line at the start of a code block should be deleted'},
do_not_leave_blank_line_at_the_end_of_code_block:{'pattern':'whitespace/blank_line', 'pattern_AND':'Redundant blank line at the end of a code block should be deleted'},
add_blank_line_before_public:{'pattern':'whitespace/blank_line', 'pattern_AND':'"public:" should be preceded by a blank line'},
add_blank_line_before_protected:{'pattern':'whitespace/blank_line', 'pattern_AND':'"protected:" should be preceded by a blank line'},
add_blank_line_before_private:{'pattern':'whitespace/blank_line', 'pattern_AND':'"private:" should be preceded by a blank line'},
add_include_what_you_use:{'pattern':'build/include_what_you_use'},
}
HOOKS_DELETING_OR_ADDING_LINES = [no_newline_at_the_end_of_file, position_of_opening_brace, _h_file_already_included,
add_blank_line_before_public, add_blank_line_before_protected, add_blank_line_before_private, do_not_leave_blank_line_after_public_protected_private, do_not_leave_blank_line_at_the_start_of_code_block, do_not_leave_blank_line_at_the_end_of_code_block, legal_copyright, add_include_what_you_use]
HOOKS_PARSING_THE_WHOLE_FILE = [cpp_file_should_include_h_file, do_not_leave_blank_line_after_public_protected_private,
do_not_leave_blank_line_at_the_start_of_code_block, do_not_leave_blank_line_at_the_end_of_code_block, legal_copyright, ifndef_header, add_include_what_you_use] #Hook à éxecuter avant les autres hooks
def replace_line_no(path, nol, oldline, newline):
"""
Remplace, la ligne No nol du fichier path (relatif à SRCROOTDIR) par la chaîne newline (un unicode)
oldline sert uniquement pour vérifier que tout est ok
"""
print("replace_line_no : ", oldline, " by ", newline)
# récupérer le contenu du fichier
fic = open(get_src_path(path), "r")
liste = fic.readlines()
fic.close()
# test de corruption
if liste[nol-1].decode(ENCODING) != oldline :
raise Exception("Le fichier source %s semble être corrompu" %path)
# remplacement de la ligne
liste[nol-1] = newline.encode(ENCODING)
# recréation du fichier corrigé
fic = open(get_src_path(path), "w")
fic.writelines(liste)
fic.close()
def replace_fic_copyright(ficpath, warning=None):
"""
Remplace le fichier sans la ligne copyright par le fichier avec la ligne copyright
"""
fic = open(get_src_path(ficpath), "r")
liste=fic_readlines(fic)
fic.close()
new_liste = []
if liste[0].find('/ Copyright (C) 2014-20xx CEA/DEN, EDF R&D\n\n')== -1:
newliste=['// Copyright (C) 2014-20xx CEA/DEN, EDF R&D\n\n']+liste
newliste=fic_writelines(newliste)
fic = open(get_src_path(ficpath), "w")
fic.writelines(newliste)
fic.close()
def replace_fic_ifndef(ficpath, ficline, warning, mode):
"""
Remplace dans le fichier, toutes les erreurs sur le style des #define et #ifndef à partir du warning
"""
fic = open(get_src_path(ficpath),'r')
liste = fic_readlines(fic)
new_liste = []
if mode == "no_guard_found":
m = re.match("(.*)No #ifndef header guard found, suggested CPP variable is: (.+) (.+)", warning)
else:
m = re.match("(.*)ifndef header guard has wrong style, please use: (.+) (.+)", warning)
if(m):
print('group_0', m.group(0))
print('group_1', m.group(1))
print('group_2', m.group(2))
# Header à changer
header_guard=m.group(2)
# Recherche de la ligne à modifier
i = 0
trouve = False
for ligne in liste:
i += 1
if i == ficline:
if ligne.find('#ifndef') >= 0:
new_liste.append("#ifndef "+header_guard+"\n")
trouve = True
else:
print("ERROR : Pattern #ifndef not found in line ", ficline)
return
else:
if trouve == True:
if ligne.find('#define') >= 0:
new_liste.append("#define "+header_guard+"\n")
trouve = False
else:
print("WARNING : Pattern #define not found in the line following the pattern #ifndef, we abandon.")
return
else:
new_liste.append(ligne)
else:
print("ERROR : Pattern not found : \"#ifndef header guard has wrong style, please use\"")
newliste=fic_writelines(new_liste)
fichier = open(get_src_path(ficpath), "w")
fichier.writelines(newliste)
def get_hook(warning, fichier):
"""
Fait le matching entre un warning et le hook permettant de le traiter.
Les critères pour sélectionner un ou l'autre des hook sont consignés dans un dict associé (dans HOOKS_DICT) à chaque hook disponible.
Le format de chaque dict de critère est le suivant :
{
"pattern": <motif principal devant se trouver dans le texte du warning>,
"pattern_AND": <motif secondaire devant se trouver également dans le texte du warning>,
"pattern_NOT": <motif secondaire ne devant pas se trouver dans le texte du warning>,
"ext": <extension du fichier considéré>
}
Seul le premier hook trouvé est renvoyé.
Les arguments sont les suivants :
* warning : le texte unicode du warning
* fichier : le chemin vers le fichier concerné
"""
def test_it(condition, data, warning, fichier):
if condition in ["pattern", "pattern_AND"] :
if data in warning:
return True
elif condition in ["PATTERN_NOT"]:
if data not in warning:
return True
elif condition in ["ext"]:
if data not in os.path.splitext(fichier)[1]:
return True
return False
for key, val in HOOKS_DICT.items(): # boucle sur les hooks existants
test = None
for condition, data in val.items():
if test is False:
# si une condition n'est pas réalisée => c'est pas le bon hook
continue
test = test_it(condition, data, warning, fichier)
if test is True:
return key
return
def main_routine(cpplint_output_file):
logfile = open(cpplint_output_file,"r")
generateur_w = create_warning_generator(logfile)
last_ficpath_with_modified_nb_lines = ""
for ficpath, ficline, warning in generateur_w:
oldline = get_line_no(ficpath, ficline)
if ficline > 0:
previous_oldline = get_line_no(ficpath, ficline-1)
else:
previous_oldline = None
print("===\nNew warning:")
print("\tFile = "+ficpath)
print("\tlast_ficpath_with_modified_nb_lines = "+last_ficpath_with_modified_nb_lines)
print("\tNo ligne =" , ficline)
if VERBOSE_FLAG == "True":
print("\tWarning ='"+warning+"'")
print("\tOld text of current line = '"+oldline+"'")
if previous_oldline:
print("\tOld text of previous line = '"+previous_oldline+"'")
if ficpath == last_ficpath_with_modified_nb_lines:
print("File ", ficpath, "already modified. Waiting next cpplint run to process it.")
continue
hook = get_hook(warning, ficpath)
print("hook = ", hook)
if hook is None:
print("No hook found")
continue
if VERBOSE_FLAG == "True":
print("\t=> Processing with hook", hook)
if hook in HOOKS_DELETING_OR_ADDING_LINES:
last_ficpath_with_modified_nb_lines = ficpath
if hook in HOOKS_PARSING_THE_WHOLE_FILE:
hook(ficpath, ficline, warning)
print("Done")
continue
# Cas particulier pour le hook make_const_reference : il faut faire un traitement sur le fichier .cpp associé au fichier .h concerné par le hook
if hook in [make_const_reference]:
if ficpath.find('.h') > -1:
newline, previous_newline, huchk = hook(ficpath, oldline, previous_oldline, warning)
else:
if H_FILE_MAKE_CONST_REFERENCE_MODIFIED:
continue
else:
newline, previous_newline, huchk = hook(ficpath, oldline, previous_oldline, warning)
else:
newline, previous_newline, huchk = hook(oldline, previous_oldline, warning)
if VERBOSE_FLAG == "True":
print("\tNew text = '"+newline+"'")
if huchk:
if previous_newline:
print("Replace line : \n'%s'\n with line \n'%s'\n O/N ? :"%(previous_oldline, previous_newline))
reponse = input()
if reponse in ['O', 'o']:
replace_line_no(ficpath, ficline-1, previous_oldline, previous_newline)
print("Replacement done.")
else :
print("Replacement aborted.")
print("Replace line : \n'%s'\n with line \n'%s'\n O/N ? :"%(oldline, newline))
reponse = input()
if reponse in ['O', 'o']:
replace_line_no(ficpath, ficline, oldline, newline)
print("Replacement done.")
else :
print("Replacement aborted.")
else :
if previous_newline:
replace_line_no(ficpath, ficline-1, previous_oldline, previous_newline)
replace_line_no(ficpath, ficline, oldline, newline)
print("Done")
logfile.close()
if __name__ == '__main__':
H_FILE_MAKE_CONST_REFERENCE_MODIFIED = False
parser = argparse.ArgumentParser()
parser.add_argument("--cpplint_output_file", type=str, help="Fichier de rapport cpplint à lire en entrée")
args = parser.parse_args()
if not args.cpplint_output_file:
parser.print_help()
else:
main_routine(args.cpplint_output_file)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/FreeCAD/shaper.git
git@gitee.com:FreeCAD/shaper.git
FreeCAD
shaper
shaper
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385