1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#***********************************************************************
# This file is part of OpenMolcas. *
# *
# OpenMolcas is free software; you can redistribute it and/or modify *
# it under the terms of the GNU Lesser General Public License, v. 2.1. *
# OpenMolcas is distributed in the hope that it will be useful, but it *
# is provided "as is" and without any express or implied warranties. *
# For more details see the full text of the license in the file *
# LICENSE or in <http://www.gnu.org/licenses/>. *
# *
# Copyright (C) 2017, Ignacio Fdez. Galván *
#***********************************************************************
# Inspired by the "police" script of V. Veryazov
'''
This script checks some basic stylistic and formatting rules in the source code
'''
import sys
from os import environ, walk
from os.path import isdir, join, relpath
import re
# Make sure the MOLCAS environment variable is defined
if ('MOLCAS' not in environ):
sys.exit('MOLCAS not defined')
MOLCAS=environ['MOLCAS']
if (not isdir(MOLCAS)):
sys.exit('{0} is not a directory'.format(MOLCAS))
#====================================================
# Each test is a function that takes two arguments:
# the line (bytes) and the filename, and returns True
# if there is a problem.
#====================================================
# Non-ASCII characters
def non_ascii(line, filename):
if (filename.endswith(('.py'))):
return False
try:
a = line.decode('ascii')
return False
except:
return True
# Tab characters
def test_tabs(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90', '.c'))):
return False
return (b'\t' in line)
# Trailing blanks
blanks = re.compile(r'\s$')
def test_blanks(line, filename):
return blanks.search(line.decode('ascii', 'ignore').strip('\n'))
# "write" with a star as unit
write_star = re.compile(r'^ .*write\s*\(\s*\*', re.IGNORECASE)
def test_write(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
if ('blas_util' in filename):
return False
return write_star.search(line.decode('ascii', 'ignore').strip('\n'))
# _DEBUG_ compile flag used
compile_flag = re.compile(r'(\bifdef\b|\bifndef).*?_DEBUG_.*?')
def test_debug_flag(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90', '.c', '.h', '.fh'))):
return False
return compile_flag.search(line.decode('ascii', 'ignore').strip('\n'))
# Bare (without wrapper) LAPACK/BLAS calls
unwrapped_sub = re.compile(r'call\s*(drot|dswap|dscal|[ds]copy|daxpy|dgemm|dspmv|dgemv|dopmtr|dspgv|dsptrd|dstevr|dgetrs|dspev|dgeev|dgesvd|dgetrf|dgesv|dsyev|dsyevr|dgetri|zheev|zhpev|dsygv|dpotrf|dgels|dsytrd|dposv|dsterf|dsteqr|dlascl|dorgtr|dgecon|dger)\s*\(', re.IGNORECASE)
unwrapped_func = re.compile(r'\b(ddot|dnrm2|dasum|idamax|ilaenv|dlansy|dlange)\s*\(', re.IGNORECASE)
def test_unwrapped(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
if (('lapack_' in filename) or ('blas_' in filename) or
('lapack.' in filename) or ('blas.' in filename)):
return False
l = line.decode('ascii', 'ignore').strip('\n')
return (unwrapped_sub.search(l) or unwrapped_func.search(l))
# Double precision
double_precision = re.compile(r'^\s+(implicit\s+)?double\s*(precision|complex)', re.IGNORECASE)
def test_dp(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
if (('lapack_util' in filename) or ('blas_util' in filename)):
return False
return double_precision.search(line.decode('ascii', 'ignore').strip('\n'))
# STOP statement
stop = re.compile(r'[\s\d]*stop(?!\s*=)', re.IGNORECASE)
def test_stop(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
if (filename.endswith(('xerbla.f', 'xquit.F90', 'xabort.F90'))):
return False
return stop.match(line.decode('ascii', 'ignore').strip('\n'))
# PRINT statement
printst = re.compile(r'[\s\d]*print\s*[*"\'\d]', re.IGNORECASE)
def test_printst(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
return printst.match(line.decode('ascii', 'ignore').strip('\n'))
# OPEN statement
openst = re.compile(r'[\s\d]*open\s*\(', re.IGNORECASE)
def test_openst(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
return openst.match(line.decode('ascii', 'ignore').strip('\n'))
# MATMUL function
matmulfn = re.compile(r'\bmatmul\s*\(', re.IGNORECASE)
def test_matmulfn(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
return (matmulfn.search(line.decode('ascii', 'ignore').strip('\n')))
# FORALL function
forallfn = re.compile(r'\bforall\s*\(', re.IGNORECASE)
def test_forallfn(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
return (forallfn.search(line.decode('ascii', 'ignore').strip('\n')))
# CHARACTER(n) declaration
characternfn1 = re.compile(r'^[^\'"]*\bcharacter\s*\(', re.IGNORECASE)
characternfn2 = re.compile(r'\bcharacter\s*\((\s*(kind|len)\s*=\s*[^,]*,?)*\)', re.IGNORECASE)
def test_characternfn(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
l = line.decode('ascii', 'ignore').strip('\n')
if (not characternfn1.search(l)):
return False
return (not characternfn2.search(l))
# Wrong KIND
kindfn1 = re.compile(r'\b(real|complex)\s*\(([^()]|\(.*\))*kind\s*=\s*iwp', re.IGNORECASE)
kindfn2 = re.compile(r'\b(int(eger)?|logical)\s*\(([^()]|\(.*\))*kind\s*=\s*wp', re.IGNORECASE)
kindfn3 = re.compile(r'[^0-9ed.+-][0-9+-]*_wp', re.IGNORECASE)
def test_kindfn(line, filename):
if (not filename.endswith(('.f', '.F', '.f90', '.F90'))):
return False
l = line.decode('ascii', 'ignore').strip('\n')
return (kindfn1.search(l) or kindfn2.search(l) or kindfn3.search(l))
#====================================================
#====================================================
# Each element of "tests" has the function, the message
# to print if the function returns True, and whether
# in that case it is considered as an error (otherwise
# it would be just a warning).
#====================================================
# Build the list of tests to perform
tests = []
tests.append({'func': test_blanks, 'print': '[{0}] Trailing blanks in {1}, line {2}', 'error': True})
tests.append({'func': non_ascii, 'print': '[{0}] Non-ASCII characters in {1}, line {2}', 'error': True})
tests.append({'func': test_tabs, 'print': '[{0}] Tab characters in {1}, line {2}', 'error': True})
tests.append({'func': test_write, 'print': '[{0}] "write(*" in {1}, line {2}', 'error': True})
tests.append({'func': test_unwrapped, 'print': '[{0}] "Unwrapped LAPACK/BLAS call in {1}, line {2}', 'error': True})
tests.append({'func': test_dp, 'print': '[{0}] "double precision" in {1}, line {2}', 'error': True})
tests.append({'func': test_stop, 'print': '[{0}] "STOP" statement in {1}, line {2}', 'error': True})
tests.append({'func': test_printst, 'print': '[{0}] "PRINT" statement in {1}, line {2}', 'error': True})
tests.append({'func': test_openst, 'print': '[{0}] "OPEN" statement in {1}, line {2} (use molcas_open)', 'error': False})
tests.append({'func': test_matmulfn, 'print': '[{0}] "MATMUL" function in {1}, line {2} (use LAPACK routines)', 'error': True})
tests.append({'func': test_forallfn, 'print': '[{0}] "FORALL" statement in {1}, line {2} (obsolescent in Fortran 2018)', 'error': True})
tests.append({'func': test_debug_flag, 'print': '[{0}] "_DEBUG_ compile flag in {1}, line {2} (use _DEBUGPRINT_ or _ADDITIONAL_RUNTIME_CHECK_)', 'error': True})
tests.append({'func': test_characternfn, 'print': '[{0}] "CHARACTER(n)" declaration in {1}, line {2} (use CHARACTER(LEN=n))', 'error': True})
tests.append({'func': test_kindfn, 'print': '[{0}] Wrong "KIND" value in {1}, line {2} (switched IWP/WP or integer constant as real)', 'error': True})
def run_tests(line, fname, ln):
j = 0
for test in tests:
j += 1
if (test['func'](line, fname)):
print(test['print'].format(j, relpath(fname, MOLCAS), ln))
errors[j-1] += 1
# Get a list of files
files = []
for dname, dirs, fnames in walk(join(MOLCAS, 'src')):
for fname in fnames:
files.append(join(dname, fname))
# Go through all files and apply the check for each line
errors = [0] * len(tests)
for fname in files:
with open(fname, 'rb') as f:
i = 0
for line in f:
i += 1
run_tests(line, fname, i)
rc = 0
for i in range(len(errors)):
if (tests[i]['error']):
rc += errors[i]
sys.exit(rc)
|