Merge vk-gl-cts/opengl-cts-4.6.0 into vk-gl-cts/master
[platform/upstream/VK-GL-CTS.git] / scripts / src_util / check_invalid_types.py
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
23 import os
24 import re
25 import sys
26 from argparse import ArgumentParser
27 from common import getChangedFiles, getAllProjectFiles, isTextFile
28
29 CHECK_LIST = [
30         ".cpp",
31         ".hpp",
32         ".c",
33         ".h",
34 ]
35
36 EXCLUSION_LIST = [
37         "framework/delibs/debase/deDefs.h",
38         "framework/platform/android/tcuAndroidPlatform.cpp",
39         "framework/platform/android/tcuAndroidWindow.hpp",
40         "framework/platform/android/tcuAndroidWindow.cpp",
41         "framework/platform/lnx/X11/tcuLnxX11Xcb.cpp",
42         "framework/platform/lnx/wayland/tcuLnxWayland.hpp",
43         "framework/platform/lnx/wayland/tcuLnxWayland.cpp",
44 ]
45
46 def checkEnds(line, ends):
47         return any(line.endswith(end) for end in ends)
48
49 def checkFileInvalidTypes (file):
50         error = False
51
52         if checkEnds(file.replace("\\", "/"), CHECK_LIST) and not checkEnds(file.replace("\\", "/"), EXCLUSION_LIST):
53                 f = open(file, 'rb')
54                 for lineNum, line in enumerate(f):
55                         # Remove inline comments
56                         idx = line.find("//")
57                         if idx > 0:
58                                 line = line[:idx]
59                         # Remove text in quoted literals
60                         if line.find("\"") > 0:
61                                 list = line.split('"')
62                                 del list[1::2]
63                                 line = ' '
64                                 line = line.join(list)
65                         found = re.search(r'\b[us]*int[0-9]+_t\b', line)
66                         if found is not None:
67                                 error = True
68                                 print "%s:%i Unacceptable type found" % (file, lineNum+1)
69                 f.close()
70
71         return not error
72
73 def checkInvalidTypes (files):
74         error = False
75         for file in files:
76                 if isTextFile(file):
77                         if not checkFileInvalidTypes(file):
78                                 error = True
79
80         return not error
81
82 if __name__ == "__main__":
83         parser = ArgumentParser()
84         parser.add_argument("-e", "--only-errors",  action="store_true", dest="onlyErrors",   default=False, help="Print only on error")
85         parser.add_argument("-i", "--only-changed", action="store_true", dest="useGitIndex",  default=False, help="Check only modified files. Uses git.")
86
87         args = parser.parse_args()
88
89         if args.useGitIndex:
90                 files = getChangedFiles()
91         else:
92                 files = getAllProjectFiles()
93
94         error = not checkInvalidTypes(files)
95
96         if error:
97                 print "One or more checks failed"
98                 sys.exit(1)
99         if not args.onlyErrors:
100                 print "All checks passed"