1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015 The Android Open Source Project
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 #-------------------------------------------------------------------------
25 from fnmatch import fnmatch
26 from optparse import OptionParser
28 HEADER_PATTERNS = ["*.hpp", "*.h"]
29 IGNORE_FILES = set(["tcuEAGLView.h", "tcuIOSAppDelegate.h", "tcuIOSViewController.h"])
30 CHECK_END_COMMENT = True
32 def getIncludeGuardName (headerFile):
33 return '_' + os.path.basename(headerFile).upper().replace('.', '_')
35 def hasValidIncludeGuard (headerFile):
36 includeGuard = getIncludeGuardName(headerFile)
37 f = open(headerFile, 'rb')
38 isHpp = headerFile[-4:] == ".hpp"
40 line0 = f.readline().strip()
41 line1 = f.readline().strip()
43 if line0 != ("#ifndef %s" % includeGuard):
45 if line1 != ("#define %s" % includeGuard):
50 expectedComment = ("#endif // %s" if isHpp else "#endif /* %s */") % includeGuard
52 lastLine = line.strip()
54 if lastLine != expectedComment:
55 # print "'%s' != '%s'" % (lastLine, expectedComment)
61 def fixIncludeGuard (headerFile):
62 f = open(headerFile, 'rb')
64 isHpp = headerFile[-4:] == ".hpp"
65 includeGuard = getIncludeGuardName(headerFile)
71 # Replace include guards
72 lines[0] = "#ifndef %s\n" % includeGuard
73 lines[1] = "#define %s\n" % includeGuard
76 lines[len(lines)-1] = ("#endif // %s\n" if isHpp else "#endif /* %s */\n") % includeGuard
78 f = open(headerFile, 'wb')
83 def isHeader (filename):
84 if os.path.basename(filename) in IGNORE_FILES:
87 for pattern in HEADER_PATTERNS:
88 if fnmatch(filename, pattern):
92 def getHeaderFileList (path):
94 if os.path.isfile(path):
98 for root, dirs, files in os.walk(path):
101 headers.append(os.path.join(root, file))
104 def checkIncludeGuards (files):
108 if not hasValidIncludeGuard(file):
110 print "File %s contains invalid include guards" % file
113 if __name__ == "__main__":
114 parser = OptionParser()
115 parser.add_option("-x", "--fix", action="store_true", dest="fix", default=False, help="attempt to fix include guards (use with caution)")
117 (options, args) = parser.parse_args()
123 headers += getHeaderFileList(os.path.normpath(dir))
126 for header in headers:
128 if not hasValidIncludeGuard(header):
129 invalidHeaders.append(header)
132 if len(invalidHeaders) > 0:
133 print "Found %d files with invalid include guards:" % len(invalidHeaders)
135 for header in invalidHeaders:
141 print "All headers have valid include guards."
145 for header in invalidHeaders:
146 fixIncludeGuard(header)
147 print "Fixed %s" % header