blob: c74508d9cd845743bb87714a8596158397e5d007 [file] [log] [blame]
Chirayu Desai2248c172013-11-06 20:43:21 +05301#!/usr/bin/env python
Elliott Hughes0e57ccb2012-04-03 16:04:52 -07002#
Elliott Hughesd320a9a2013-10-06 21:48:46 -07003# Copyright (C) 2012 The Android Open Source Project
Elliott Hughes0e57ccb2012-04-03 16:04:52 -07004#
Elliott Hughesd320a9a2013-10-06 21:48:46 -07005# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
Elliott Hughes0e57ccb2012-04-03 16:04:52 -07008#
Elliott Hughesd320a9a2013-10-06 21:48:46 -07009# http://www.apache.org/licenses/LICENSE-2.0
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070010#
Elliott Hughesd320a9a2013-10-06 21:48:46 -070011# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070016
17"""Generates default implementations of operator<< for enum types."""
18
19import codecs
20import os
21import re
22import string
23import sys
24
25
Andreas Gampe833a4852014-05-21 18:46:59 -070026_ENUM_START_RE = re.compile(r'\benum\b\s+(class\s+)?(\S+)\s+:?.*\{(\s+// private)?')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070027_ENUM_VALUE_RE = re.compile(r'([A-Za-z0-9_]+)(.*)')
28_ENUM_END_RE = re.compile(r'^\s*\};$')
29_ENUMS = {}
Elliott Hughes460384f2012-04-04 16:53:10 -070030_NAMESPACES = {}
Andreas Gampe833a4852014-05-21 18:46:59 -070031_ENUM_CLASSES = {}
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070032
33def Confused(filename, line_number, line):
34 sys.stderr.write('%s:%d: confused by:\n%s\n' % (filename, line_number, line))
Elliott Hughes48257562012-06-06 17:42:44 -070035 raise Exception("giving up!")
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070036 sys.exit(1)
37
38
39def ProcessFile(filename):
40 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
41 in_enum = False
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070042 is_enum_private = False
Andreas Gampe833a4852014-05-21 18:46:59 -070043 is_enum_class = False
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070044 line_number = 0
Andreas Gampe833a4852014-05-21 18:46:59 -070045
Elliott Hughes460384f2012-04-04 16:53:10 -070046
47 namespaces = []
48 enclosing_classes = []
49
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070050 for raw_line in lines:
51 line_number += 1
52
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070053 if not in_enum:
Elliott Hughes460384f2012-04-04 16:53:10 -070054 # Is this the start of a new enum?
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070055 m = _ENUM_START_RE.search(raw_line)
56 if m:
57 # Yes, so add an empty entry to _ENUMS for this enum.
Andreas Gampe833a4852014-05-21 18:46:59 -070058
59 # Except when it's private
60 if m.group(3) is not None:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070061 is_enum_private = True
62 else:
63 is_enum_private = False
64 is_enum_class = m.group(1) is not None
65 enum_name = m.group(2)
66 if len(enclosing_classes) > 0:
67 enum_name = '::'.join(enclosing_classes) + '::' + enum_name
68 _ENUMS[enum_name] = []
69 _NAMESPACES[enum_name] = '::'.join(namespaces)
70 _ENUM_CLASSES[enum_name] = is_enum_class
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070071 in_enum = True
Elliott Hughes460384f2012-04-04 16:53:10 -070072 continue
73
74 # Is this the start or end of a namespace?
75 m = re.compile(r'^namespace (\S+) \{').search(raw_line)
76 if m:
77 namespaces.append(m.group(1))
78 continue
79 m = re.compile(r'^\}\s+// namespace').search(raw_line)
80 if m:
81 namespaces = namespaces[0:len(namespaces) - 1]
82 continue
83
84 # Is this the start or end of an enclosing class or struct?
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070085 m = re.compile(r'^\s*(?:class|struct)(?: MANAGED)?(?: PACKED\([0-9]\))? (\S+).* \{').search(raw_line)
Elliott Hughes460384f2012-04-04 16:53:10 -070086 if m:
87 enclosing_classes.append(m.group(1))
88 continue
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070089 m = re.compile(r'^\s*\}( .*)?;').search(raw_line)
Elliott Hughes460384f2012-04-04 16:53:10 -070090 if m:
91 enclosing_classes = enclosing_classes[0:len(enclosing_classes) - 1]
92 continue
93
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070094 continue
95
96 # Is this the end of the current enum?
97 m = _ENUM_END_RE.search(raw_line)
98 if m:
99 if not in_enum:
100 Confused(filename, line_number, raw_line)
101 in_enum = False
102 continue
103
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104 if is_enum_private:
105 continue
106
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700107 # The only useful thing in comments is the <<alternate text>> syntax for
108 # overriding the default enum value names. Pull that out...
109 enum_text = None
110 m_comment = re.compile(r'// <<(.*?)>>').search(raw_line)
111 if m_comment:
112 enum_text = m_comment.group(1)
113 # ...and then strip // comments.
114 line = re.sub(r'//.*', '', raw_line)
Elliott Hughes48257562012-06-06 17:42:44 -0700115
116 # Strip whitespace.
117 line = line.strip()
118
119 # Skip blank lines.
Elliott Hughes460384f2012-04-04 16:53:10 -0700120 if len(line) == 0:
121 continue
122
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700123 # Since we know we're in an enum type, and we're not looking at a comment
124 # or a blank line, this line should be the next enum value...
Elliott Hughes460384f2012-04-04 16:53:10 -0700125 m = _ENUM_VALUE_RE.search(line)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700126 if not m:
127 Confused(filename, line_number, raw_line)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700128 enum_value = m.group(1)
129
130 # By default, we turn "kSomeValue" into "SomeValue".
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700131 if enum_text == None:
132 enum_text = enum_value
133 if enum_text.startswith('k'):
134 enum_text = enum_text[1:]
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700135
136 # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
137 rest = m.group(2).strip()
Elliott Hughes460384f2012-04-04 16:53:10 -0700138 m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+|\'.\')').search(rest)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700139 if m_literal:
140 rest = rest[(len(m_literal.group(0))):]
141
142 # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
143 # TODO: check that the rhs is actually an existing value.
144 if rest.startswith('= k'):
145 continue
146
147 # Remove any trailing comma and whitespace
148 if rest.startswith(','):
149 rest = rest[1:]
150 rest = rest.strip()
151
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700152 # There shouldn't be anything left.
153 if len(rest):
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700154 sys.stderr.write('%s\n' % (rest))
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700155 Confused(filename, line_number, raw_line)
156
Sebastien Hertzf7958692015-06-09 14:09:14 +0200157 # If the enum is scoped, we must prefix enum value with enum name (which is already prefixed
158 # by enclosing classes).
159 if is_enum_class:
160 enum_value = enum_name + '::' + enum_value
161 else:
162 if len(enclosing_classes) > 0:
Andreas Gampe833a4852014-05-21 18:46:59 -0700163 enum_value = '::'.join(enclosing_classes) + '::' + enum_value
Elliott Hughes460384f2012-04-04 16:53:10 -0700164
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700165 _ENUMS[enum_name].append((enum_value, enum_text))
166
167def main():
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 local_path = sys.argv[1]
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700169 header_files = []
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 for header_file in sys.argv[2:]:
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700171 header_files.append(header_file)
172 ProcessFile(header_file)
173
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200174 print('#include <iostream>')
175 print('')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700176
177 for header_file in header_files:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 header_file = header_file.replace(local_path + '/', '')
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200179 print('#include "%s"' % header_file)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700180
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200181 print('')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700182
183 for enum_name in _ENUMS:
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200184 print('// This was automatically generated by %s --- do not edit!' % sys.argv[0])
Elliott Hughes460384f2012-04-04 16:53:10 -0700185
186 namespaces = _NAMESPACES[enum_name].split('::')
187 for namespace in namespaces:
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200188 print('namespace %s {' % namespace)
Elliott Hughes460384f2012-04-04 16:53:10 -0700189
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200190 print('std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name)
191 print(' switch (rhs) {')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700192 for (enum_value, enum_text) in _ENUMS[enum_name]:
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200193 print(' case %s: os << "%s"; break;' % (enum_value, enum_text))
Andreas Gampe833a4852014-05-21 18:46:59 -0700194 if not _ENUM_CLASSES[enum_name]:
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200195 print(' default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name)
196 print(' }')
197 print(' return os;')
198 print('}')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700199
Elliott Hughes460384f2012-04-04 16:53:10 -0700200 for namespace in reversed(namespaces):
Bernhard Rosenkränzerc2e02602014-07-14 13:30:58 +0200201 print('} // namespace %s' % namespace)
202 print('')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700203
204 sys.exit(0)
205
206
207if __name__ == '__main__':
208 main()