Skip to content
Snippets Groups Projects
Commit 2eb84d9e authored by Alberto Nidasio's avatar Alberto Nidasio
Browse files

[SBS][Linter] Updated cppcheck and copyright output

parent 47b91f10
No related branches found
No related tags found
No related merge requests found
...@@ -277,9 +277,9 @@ lint_cppcheck() { ...@@ -277,9 +277,9 @@ lint_cppcheck() {
declare -a opts=() declare -a opts=()
[ -n "$jobs" ] && opts+=("-j $jobs") [ -n "$jobs" ] && opts+=("-j $jobs")
cppcheck --language=c++ --std=c++11 --enable=all --inline-suppr \ cppcheck --language=c++ --std=c++14 --enable=all --inline-suppr \
--suppress=unmatchedSuppression --suppress=unusedFunction \ --suppress=unmatchedSuppression --suppress=unusedFunction \
--suppress=missingInclude --error-exitcode=1 "${opts[@]}" \ --suppress=missingInclude --error-exitcode=1 -q "${opts[@]}" \
"$source_dir/src" "$source_dir/src"
} }
......
...@@ -92,10 +92,6 @@ def print_banner(): ...@@ -92,10 +92,6 @@ def print_banner():
print('+------------------------------+') print('+------------------------------+')
def linter_print(*strings):
print('[linter]', *strings)
class Colors(): class Colors():
BLACK = '\033[30m' BLACK = '\033[30m'
RED = '\033[31m' RED = '\033[31m'
...@@ -111,7 +107,7 @@ class Colors(): ...@@ -111,7 +107,7 @@ class Colors():
# Checks for the right copyright notice in all .h and .cpp files # Checks for the right copyright notice in all .h and .cpp files
def check_copyright(directory): def check_copyright(directory):
linter_print(Colors.GREEN + 'Copyright check' + Colors.RESET) print(Colors.GREEN + 'Copyright check' + Colors.RESET)
# Statistics # Statistics
totalCheckdFilesCounter = 0 totalCheckdFilesCounter = 0
...@@ -134,7 +130,7 @@ def check_copyright(directory): ...@@ -134,7 +130,7 @@ def check_copyright(directory):
filesWithErrorsCounter += 1 filesWithErrorsCounter += 1
# The file's copyright notice does not match the template! # The file's copyright notice does not match the template!
linter_print(Colors.YELLOW + 'Wrong copyright notice in file {0}'.format( print(Colors.YELLOW + 'Wrong copyright notice in file {0}'.format(
currentFilepath) + Colors.RESET) currentFilepath) + Colors.RESET)
else: else:
fileAuthors = [a.strip() fileAuthors = [a.strip()
...@@ -142,10 +138,10 @@ def check_copyright(directory): ...@@ -142,10 +138,10 @@ def check_copyright(directory):
# Check the number of authors against 'Author' or `Authors` # Check the number of authors against 'Author' or `Authors`
if len(fileAuthors) == 1 and match.group(1)[-1] == 's': if len(fileAuthors) == 1 and match.group(1)[-1] == 's':
linter_print('\'Authors\' should to be changed to \'Author\' in {0}'.format( print('\'Authors\' should to be changed to \'Author\' in {0}'.format(
currentFilepath)) currentFilepath))
if len(fileAuthors) > 1 and match.group(1)[-1] != 's': if len(fileAuthors) > 1 and match.group(1)[-1] != 's':
linter_print('\'Author\' should to be changed to \'Authors\' in {0}'.format( print('\'Author\' should to be changed to \'Authors\' in {0}'.format(
currentFilepath)) currentFilepath))
# Save statistics on authors # Save statistics on authors
...@@ -157,19 +153,20 @@ def check_copyright(directory): ...@@ -157,19 +153,20 @@ def check_copyright(directory):
averageAuthorsPerFile += len(fileAuthors) averageAuthorsPerFile += len(fileAuthors)
averageAuthorsPerFile /= totalCheckdFilesCounter averageAuthorsPerFile /= totalCheckdFilesCounter
linter_print('Checked {} files'.format(totalCheckdFilesCounter)) print('Checked {} files'.format(totalCheckdFilesCounter))
if filesWithErrorsCounter == 0: if filesWithErrorsCounter == 0:
linter_print('All the files have the correct copyright notice') print('All the files have the correct copyright notice')
else: else:
linter_print(Colors.RED + '{:.1f}% ({}/{}) of all analyzed files do not match with the copyright template!'.format( print(Colors.RED + '{:.1f}% ({}/{}) of all analyzed files do not match with the copyright template!'.format(
100*filesWithErrorsCounter/totalCheckdFilesCounter, filesWithErrorsCounter, totalCheckdFilesCounter) + Colors.RESET) 100*filesWithErrorsCounter/totalCheckdFilesCounter, filesWithErrorsCounter, totalCheckdFilesCounter) + Colors.RESET)
if (not args.quiet): if (not args.quiet):
linter_print('{:.2} authors per file'.format( print('{:.2} authors per file'.format(
averageAuthorsPerFile)) averageAuthorsPerFile))
linter_print('Number of mentions per author:')
for author in authors: print('Number of mentions per author:')
linter_print('{:3} - {}'.format(authors[author], author)) for author in sorted(authors.items(), key=lambda item: item[1], reverse=True):
print('{:3} - {}'.format(author[1], author[0]))
# Exit if error if at least one file isn't correct # Exit if error if at least one file isn't correct
if (filesWithErrorsCounter > 0): if (filesWithErrorsCounter > 0):
...@@ -178,7 +175,7 @@ def check_copyright(directory): ...@@ -178,7 +175,7 @@ def check_copyright(directory):
# Checks if all .h and .cpp files respects the clang format specified in .clang-format file # Checks if all .h and .cpp files respects the clang format specified in .clang-format file
def check_format(directory): def check_format(directory):
linter_print(Colors.GREEN + 'Formatting check' + Colors.RESET) print(Colors.GREEN + 'Formatting check' + Colors.RESET)
# Statistics # Statistics
totalCheckdFilesCounter = 0 totalCheckdFilesCounter = 0
...@@ -201,14 +198,14 @@ def check_format(directory): ...@@ -201,14 +198,14 @@ def check_format(directory):
filesWithErrorsCounter += 1 filesWithErrorsCounter += 1
if (not args.quiet): if (not args.quiet):
linter_print(Colors.YELLOW + 'Wrong code format for file {1}'.format( print(Colors.YELLOW + 'Wrong code format for file {1}'.format(
returnCode, currentFilepath) + Colors.RESET) returnCode, currentFilepath) + Colors.RESET)
linter_print('Checked {} files'.format(totalCheckdFilesCounter)) print('Checked {} files'.format(totalCheckdFilesCounter))
if filesWithErrorsCounter == 0: if filesWithErrorsCounter == 0:
linter_print('All the files match the Skyward formatting style') print('All the files match the Skyward formatting style')
else: else:
linter_print(Colors.RED + '{:4.1f}% ({}/{}) of all analyzed files do not match Skyward formatting style!'.format( print(Colors.RED + '{:4.1f}% ({}/{}) of all analyzed files do not match Skyward formatting style!'.format(
100*filesWithErrorsCounter/totalCheckdFilesCounter, filesWithErrorsCounter, totalCheckdFilesCounter), Colors.RESET) 100*filesWithErrorsCounter/totalCheckdFilesCounter, filesWithErrorsCounter, totalCheckdFilesCounter), Colors.RESET)
# Exit if error if at least one file isn't correct # Exit if error if at least one file isn't correct
...@@ -217,7 +214,7 @@ def check_format(directory): ...@@ -217,7 +214,7 @@ def check_format(directory):
def find_in_code(directory, searchTerm, extensionFilters=('.cpp', '.h'), pathFilter=None): def find_in_code(directory, searchTerm, extensionFilters=('.cpp', '.h'), pathFilter=None):
linter_print( print(
Colors.GREEN + 'Checking for \'{}\' in code files'.format(searchTerm) + Colors.RESET) Colors.GREEN + 'Checking for \'{}\' in code files'.format(searchTerm) + Colors.RESET)
# Statistics # Statistics
...@@ -244,15 +241,15 @@ def find_in_code(directory, searchTerm, extensionFilters=('.cpp', '.h'), pathFil ...@@ -244,15 +241,15 @@ def find_in_code(directory, searchTerm, extensionFilters=('.cpp', '.h'), pathFil
# The current file has the error # The current file has the error
if (not args.quiet): if (not args.quiet):
linter_print(Colors.YELLOW + 'Found \'{}\' in file {}'.format(searchTerm, print(Colors.YELLOW + 'Found \'{}\' in file {}'.format(searchTerm,
currentFilepath) + Colors.RESET) currentFilepath) + Colors.RESET)
linter_print('Checked {} files'.format(totalCheckdFilesCounter)) print('Checked {} files'.format(totalCheckdFilesCounter))
if filesWithErrorsCounter == 0: if filesWithErrorsCounter == 0:
linter_print( print(
'All the files does not contain \'{}\''.format(searchTerm)) 'All the files does not contain \'{}\''.format(searchTerm))
else: else:
linter_print(Colors.RED + '{:.1f}% ({}/{}) of all analyzed files contain \'{}\'!'.format( print(Colors.RED + '{:.1f}% ({}/{}) of all analyzed files contain \'{}\'!'.format(
100*filesWithErrorsCounter/totalCheckdFilesCounter, filesWithErrorsCounter, totalCheckdFilesCounter, searchTerm) + Colors.RESET) 100*filesWithErrorsCounter/totalCheckdFilesCounter, filesWithErrorsCounter, totalCheckdFilesCounter, searchTerm) + Colors.RESET)
return filesWithErrorsCounter return filesWithErrorsCounter
...@@ -260,7 +257,8 @@ def find_in_code(directory, searchTerm, extensionFilters=('.cpp', '.h'), pathFil ...@@ -260,7 +257,8 @@ def find_in_code(directory, searchTerm, extensionFilters=('.cpp', '.h'), pathFil
def check_find(directory): def check_find(directory):
sum = find_in_code(directory, r'^using namespace', '.h') sum = find_in_code(directory, r'^using namespace', '.h')
sum += find_in_code(directory, r'[^a-zA-Z0-9]printf\(', pathFilter='shared') sum += find_in_code(directory,
r'[^a-zA-Z0-9]printf\(', pathFilter='shared')
sum += find_in_code(directory, r'( |^)assert\(') sum += find_in_code(directory, r'( |^)assert\(')
sum += find_in_code(directory, '^ *throw ', pathFilter='catch') sum += find_in_code(directory, '^ *throw ', pathFilter='catch')
...@@ -269,7 +267,7 @@ def check_find(directory): ...@@ -269,7 +267,7 @@ def check_find(directory):
def check_cppcheck(directory): def check_cppcheck(directory):
linter_print(Colors.GREEN + 'cppcheck' + Colors.RESET) print(Colors.GREEN + 'cppcheck' + Colors.RESET)
# Run cppcheck on the directory # Run cppcheck on the directory
try: try:
result = check_output(['cppcheck', '-q', '--language=c++', '--template=gcc', '--std=c++11', '--enable=all', '--inline-suppr', result = check_output(['cppcheck', '-q', '--language=c++', '--template=gcc', '--std=c++11', '--enable=all', '--inline-suppr',
...@@ -281,20 +279,20 @@ def check_cppcheck(directory): ...@@ -281,20 +279,20 @@ def check_cppcheck(directory):
errors = Counter(errors) errors = Counter(errors)
if (not args.quiet): if (not args.quiet):
linter_print('cppcheck found the following errors:') print('cppcheck found the following errors:')
for error in errors: for error in errors:
linter_print('{:3} - {}'.format(errors[error], error)) print('{:3} - {}'.format(errors[error], error))
totalErrors = sum(errors.values()) totalErrors = sum(errors.values())
if (totalErrors > 0): if (totalErrors > 0):
linter_print( print(
Colors.RED + 'cppcheck found {} errors in total'.format(totalErrors) + Colors.RESET) Colors.RED + 'cppcheck found {} errors in total'.format(totalErrors) + Colors.RESET)
exit(-1) exit(-1)
else: else:
linter_print('cppcheck did not find any errors') print('cppcheck did not find any errors')
except CalledProcessError as e: except CalledProcessError as e:
linter_print(e.output.decode('utf-8')) print(e.output.decode('utf-8'))
exit(-1) exit(-1)
# ------------------------------------------------------------- # -------------------------------------------------------------
...@@ -306,7 +304,7 @@ parser = config_cmd_parser() ...@@ -306,7 +304,7 @@ parser = config_cmd_parser()
args = parser.parse_args() args = parser.parse_args()
if (not args.directory): if (not args.directory):
linter_print('No directory specified') print('No directory specified')
print('') print('')
parser.print_help() parser.print_help()
exit(-1) exit(-1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment