1# -*- coding: utf-8 -*- 2 3#------------------------------------------------------------------------- 4# drawElements Quality Program utilities 5# -------------------------------------- 6# 7# Copyright (c) 2017 The Khronos Group Inc. 8# 9# Licensed under the Apache License, Version 2.0 (the "License"); 10# you may not use this file except in compliance with the License. 11# You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, software 16# distributed under the License is distributed on an "AS IS" BASIS, 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18# See the License for the specific language governing permissions and 19# limitations under the License. 20# 21#------------------------------------------------------------------------- 22 23import os 24import re 25import sys 26from argparse import ArgumentParser 27from common import getChangedFiles, getAllProjectFiles, isTextFile 28 29CHECK_LITERAL_PATTERNS = [ 30 r'\b[us]*int[0-9]+_t\b', 31 r'\b[U]*INT(_LEAST|_FAST|)[0-9]+_MAX\b', 32 r'\b0b', 33] 34 35CHECK_LIST = [ 36 ".cpp", 37 ".hpp", 38 ".c", 39 ".h", 40] 41 42EXCLUSION_LIST = [ 43 "framework/delibs/debase/deDefs.h", 44 "framework/platform/android/tcuAndroidPlatform.cpp", 45 "framework/platform/android/tcuAndroidWindow.hpp", 46 "framework/platform/android/tcuAndroidWindow.cpp", 47 "framework/platform/lnx/X11/tcuLnxX11Xcb.cpp", 48 "framework/platform/lnx/wayland/tcuLnxWayland.hpp", 49 "framework/platform/lnx/wayland/tcuLnxWayland.cpp", 50 "framework/delibs/debase/deFloat16.c", 51] 52 53def checkEnds(line, ends): 54 return any(line.endswith(end) for end in ends) 55 56def checkFileInvalidLiterals (file): 57 error = False 58 59 if checkEnds(file.replace("\\", "/"), CHECK_LIST) and not checkEnds(file.replace("\\", "/"), EXCLUSION_LIST): 60 f = open(file, 'rb') 61 for lineNum, line in enumerate(f): 62 # Remove inline comments 63 idx = line.find("//") 64 if idx > 0: 65 line = line[:idx] 66 # Remove text in quoted literals 67 if line.find("\"") > 0: 68 list = line.split('"') 69 del list[1::2] 70 line = ' ' 71 line = line.join(list) 72 for pattern in CHECK_LITERAL_PATTERNS: 73 found = re.search(pattern, line) 74 if found is not None: 75 error = True 76 print "%s:%i Unacceptable type found (pattern:%s)" % (file, lineNum+1, pattern) 77 f.close() 78 79 return not error 80 81def checkInvalidLiterals (files): 82 error = False 83 for file in files: 84 if isTextFile(file): 85 if not checkFileInvalidLiterals(file): 86 error = True 87 88 return not error 89 90if __name__ == "__main__": 91 parser = ArgumentParser() 92 parser.add_argument("-e", "--only-errors", action="store_true", dest="onlyErrors", default=False, help="Print only on error") 93 parser.add_argument("-i", "--only-changed", action="store_true", dest="useGitIndex", default=False, help="Check only modified files. Uses git.") 94 95 args = parser.parse_args() 96 97 if args.useGitIndex: 98 files = getChangedFiles() 99 else: 100 files = getAllProjectFiles() 101 102 error = not checkInvalidLiterals(files) 103 104 if error: 105 print "One or more checks failed" 106 sys.exit(1) 107 if not args.onlyErrors: 108 print "All checks passed" 109