Tracks line numbers for includes, and the order in which includes appear. include_list contains list of lists of (header, line number) pairs. It's a lists of lists rather than just one flat list to make it easier to update across preprocessor boundaries. Call CheckNextIncludeOrder(
| 1210 | |
| 1211 | |
| 1212 | class _IncludeState: |
| 1213 | """Tracks line numbers for includes, and the order in which includes appear. |
| 1214 | |
| 1215 | include_list contains list of lists of (header, line number) pairs. |
| 1216 | It's a lists of lists rather than just one flat list to make it |
| 1217 | easier to update across preprocessor boundaries. |
| 1218 | |
| 1219 | Call CheckNextIncludeOrder() once for each header in the file, passing |
| 1220 | in the type constants defined above. Calls in an illegal order will |
| 1221 | raise an _IncludeError with an appropriate error message. |
| 1222 | |
| 1223 | """ |
| 1224 | |
| 1225 | # self._section will move monotonically through this set. If it ever |
| 1226 | # needs to move backwards, CheckNextIncludeOrder will raise an error. |
| 1227 | _INITIAL_SECTION = 0 |
| 1228 | _MY_H_SECTION = 1 |
| 1229 | _C_SECTION = 2 |
| 1230 | _CPP_SECTION = 3 |
| 1231 | _OTHER_SYS_SECTION = 4 |
| 1232 | _OTHER_H_SECTION = 5 |
| 1233 | |
| 1234 | _TYPE_NAMES = { |
| 1235 | _C_SYS_HEADER: "C system header", |
| 1236 | _CPP_SYS_HEADER: "C++ system header", |
| 1237 | _OTHER_SYS_HEADER: "other system header", |
| 1238 | _LIKELY_MY_HEADER: "header this file implements", |
| 1239 | _POSSIBLE_MY_HEADER: "header this file may implement", |
| 1240 | _OTHER_HEADER: "other header", |
| 1241 | } |
| 1242 | _SECTION_NAMES = { |
| 1243 | _INITIAL_SECTION: "... nothing. (This can't be an error.)", |
| 1244 | _MY_H_SECTION: "a header this file implements", |
| 1245 | _C_SECTION: "C system header", |
| 1246 | _CPP_SECTION: "C++ system header", |
| 1247 | _OTHER_SYS_SECTION: "other system header", |
| 1248 | _OTHER_H_SECTION: "other header", |
| 1249 | } |
| 1250 | |
| 1251 | def __init__(self): |
| 1252 | self.include_list = [[]] |
| 1253 | self._section = None |
| 1254 | self._last_header = None |
| 1255 | self.ResetSection("") |
| 1256 | |
| 1257 | def FindHeader(self, header): |
| 1258 | """Check if a header has already been included. |
| 1259 | |
| 1260 | Args: |
| 1261 | header: header to check. |
| 1262 | Returns: |
| 1263 | Line number of previous occurrence, or -1 if the header has not |
| 1264 | been seen before. |
| 1265 | """ |
| 1266 | for section_list in self.include_list: |
| 1267 | for f in section_list: |
| 1268 | if f[0] == header: |
| 1269 | return f[1] |