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 CHECK_END_COMMENT = True
31 def getIncludeGuardName (headerFile):
32 return '_' + os.path.basename(headerFile).upper().replace('.', '_')
34 def hasValidIncludeGuard (headerFile):
35 includeGuard = getIncludeGuardName(headerFile)
36 f = open(headerFile, 'rb')
37 isHpp = headerFile[-4:] == ".hpp"
39 line0 = f.readline().strip()
40 line1 = f.readline().strip()
42 if line0 != ("#ifndef %s" % includeGuard):
44 if line1 != ("#define %s" % includeGuard):
49 expectedComment = ("#endif // %s" if isHpp else "#endif /* %s */") % includeGuard
51 lastLine = line.strip()
53 if lastLine != expectedComment:
54 # print "'%s' != '%s'" % (lastLine, expectedComment)
60 def fixIncludeGuard (headerFile):
61 f = open(headerFile, 'rb')
63 isHpp = headerFile[-4:] == ".hpp"
64 includeGuard = getIncludeGuardName(headerFile)
70 # Replace include guards
71 lines[0] = "#ifndef %s\n" % includeGuard
72 lines[1] = "#define %s\n" % includeGuard
75 lines[len(lines)-1] = ("#endif // %s\n" if isHpp else "#endif /* %s */\n") % includeGuard
77 f = open(headerFile, 'wb')
82 def isHeader (filename):
83 for pattern in HEADER_PATTERNS:
84 if fnmatch(filename, pattern):
88 def getHeaderFileList (path):
90 if os.path.isfile(path):
94 for root, dirs, files in os.walk(path):
97 headers.append(os.path.join(root, file))
100 def checkIncludeGuards (files):
104 if not hasValidIncludeGuard(file):
106 print "File %s contains invalid include guards" % file
109 if __name__ == "__main__":
110 parser = OptionParser()
111 parser.add_option("-x", "--fix", action="store_true", dest="fix", default=False, help="attempt to fix include guards (use with caution)")
113 (options, args) = parser.parse_args()
119 headers += getHeaderFileList(os.path.normpath(dir))
122 for header in headers:
124 if not hasValidIncludeGuard(header):
125 invalidHeaders.append(header)
128 if len(invalidHeaders) > 0:
129 print "Found %d files with invalid include guards:" % len(invalidHeaders)
131 for header in invalidHeaders:
137 print "All headers have valid include guards."
141 for header in invalidHeaders:
142 fixIncludeGuard(header)
143 print "Fixed %s" % header