def setup(self): self.args = self._parser.parse_args() self.filepath = self.args.file self.flags = _getFlagsFromArgs(self.args) hardcodedFlags = ['-xc++', '-std=c++98', '-Wall'] self.transUnit = _getTranslationUnit(self.filepath, hardcodedFlags + self.flags) if self.args.verbose: traverse(self.transUnit.cursor, TreePrinter()) fileprinter.printf('\n') didFindError = _printErrors(self.transUnit, fileprinter.printerr) if didFindError and not self.args.ignoreErrors: sys.exit('FATAL One or more errors found in compilation unit.') return self.args, self.transUnit
import fileprinter printf = fileprinter.printf if args.verbose else doNothing printerr = fileprinter.printerr # import re # fileWhitelist = set([re.sub(r'\.cpp$', '.h', filepath), # re.sub(r'\.h$', '.cpp', filepath)]) # Empty whitelist means "accept all the files" fileWhitelist = set() fields = FieldFinder(fileWhitelist) inits = InitFinder(fileWhitelist) observers = ObserverGroup([fields, inits]) traverse(transUnit.cursor, observers) inits.fillInitFieldsText() if args.verbose: printf('\n\nThe fields:') fields.prettyPrint() printf('\n\nThe constructors:') inits.prettyPrint() printf('') rewritesPerFile = getRewrites(fields, inits) printf('\nThe rewrites:') printf(rewritesPerFile) printf("Now let's try to do the rewrites") from rewrite import rewrite
indentation, repeatedString(' ', tabWidth), '' if args.noTodo else ' /* TODO? */')) if __name__ == '__main__': import fileprinter printf = fileprinter.printf printerr = fileprinter.printerr util = fixer.Fixer("Add no-op 'default:' to incomplete switches on enum types.") util.add_argument('--no-todo', dest='noTodo', action='count', help="Don't add a TODO comment to each inserted 'default:'") args, transUnit = util.setup() filepath = util.filepath switchWarnLocations = set(HashableLocation(diag.location) \ for diag in transUnit.diagnostics \ if diag.option == '-Wswitch') finder = FindCursorParent(switchWarnLocations) traverse(transUnit.cursor, finder) rewrites = [getSwitchRewrite(cursor, printerr) for cursor in finder.cursors.itervalues()] if len(rewrites) > 0: fin = open(filepath, 'r') fout = open(filepath + '.rewrite', 'w') from rewrite import rewrite rewrite(fin, fout, rewrites)
printCursor(cursor, printerr) return None # Take the last rather than the first, return matches[-1] # in case the parameter shares its name with a type. if __name__ == '__main__': from fileprinter import printf, printerr import fixer util = fixer.Fixer('Remove unused parameter variables from function definitions.') args, translationUnit = util.setup() inFilepath = util.filepath finder = FindUnusedParameters() traverse(translationUnit.cursor, finder) from collections import defaultdict def rewritesByFile(unusedParameters): rewrites = defaultdict(list) for cursor in unusedParameters: token = eponymousToken(cursor) if not token: continue startOffset = token.extent.start.offset endOffset = token.extent.end.offset rewrite = (startOffset, endOffset - startOffset, '') rewrites[token.location.file.name].append(rewrite)
import fixer from observer import traverse, TreePrinter def printToken(token): s = '"{}"\t{}\t{}.{}({}) - {}.{}({})'.format( token.spelling, token.location.file.name if token.location.file else '', token.extent.start.line, token.extent.start.column, token.extent.start.offset, token.extent.end.line, token.extent.end.column, token.extent.end.offset) print(s) if __name__ == '__main__': util = fixer.Fixer('Print the abstract syntax tree (AST) of a C++ file') args, translationUnit = util.setup() # for token in translationUnit.get_tokens(extent=translationUnit.cursor.extent): # for token in translationUnit.cursor.get_tokens(): # printToken(token) # If --verbose was passed, then we already printed the tree. If not, # then we print the tree here, and restrict it to the specified file. # if not args.verbose: traverse(translationUnit.cursor, TreePrinter(whitelist=set([util.filepath])))
# def get_size(self): # def get_offset(self, fieldname): # def get_ref_qualifier(self): # def spelling(self): # def __eq__(self, other): # def __ne__(self, other): def printType(cppType, columnWidth, printer=printf): printer('{{:<{0}}}{{:<{0}}}{{:<{0}}}'.format(columnWidth), '"' + cppType.spelling + '"', cppType.kind, cppType.get_ref_qualifier()) if __name__ == '__main__': util = Fixer('Print all of the types in a translation unit.') util.add_argument('--column-width', dest='columnWidth', default=45, help='Width in characters of each output column') args, translationUnit = util.setup() grabber = TypeGrabber() traverse(translationUnit.cursor, grabber) if len(grabber.types): printf('{{:<{0}}}{{:<{0}}}{{:<{0}}}'.format(args.columnWidth), '---- Name ----', '---- Kind ----', '---- Ref Qualifier ----') for cppType in grabber.types: printType(cppType, args.columnWidth)