def testPartialTokenStream(self): """Partial token stream does not raise an ExceptionCppDiagnosticPartialTokenStream.""" myObj = CppDiagnostic.PreprocessDiagnosticKeepGoing() self.assertEqual(0, myObj._cntrPartialTokenStream) myObj.partialTokenStream( "PreprocessDiagnosticKeepGoing.partialTokenStream()") self.assertEqual(1, myObj._cntrPartialTokenStream)
def _initReader(self): """Create and return a reader, initialise internals.""" if self._keepGoing: myDiagnostic = CppDiagnostic.PreprocessDiagnosticKeepGoing() else: myDiagnostic = None try: myItt = ItuToTokens.ItuToTokens( theFileObj=self._ituFileObj, theFileId=self._fpIn, theDiagnostic=myDiagnostic, ) except IOError as err: raise ExceptionItuToHTML(str(err)) self._lineNum = 0 return myItt
def preProcessForIncludes(theItu, incUsr, incSys, theDefineS, preIncS, keepGoing, ignorePragma): """Pre-process a file for included files.""" myIncH = IncludeHandler.CppIncludeStdOs( theUsrDirs=incUsr or [], theSysDirs=incSys or [], ) myPreIncFiles = [] # Add macros in psuedo pre-include if theDefineS: myStr = '\n'.join( ['#define ' + ' '.join(d.split('=')) for d in theDefineS]) + '\n' myPreIncFiles = [ io.StringIO(myStr), ] myPreIncFiles.extend([open(f) for f in preIncS]) myDiag = None if keepGoing: myDiag = CppDiagnostic.PreprocessDiagnosticKeepGoing() myPh = None if ignorePragma: myPh = PragmaHandler.PragmaHandlerNull() # Create the lexer. myLexer = PpLexer.PpLexer( theItu, myIncH, preIncFiles=myPreIncFiles, diagnostic=myDiag, pragmaHandler=myPh, ) logging.info('Preprocessing TU: %s' % theItu) for t in myLexer.ppTokens(): pass logging.info('Preprocessing TU done.') retVal = retIncludedFileSet(myLexer) # Remove any artificial files try: retVal.remove(PpLexer.UNNAMED_FILE_NAME) except KeyError: pass return retVal
def main(): """Processes command line to preprocess a file or a directory.""" program_version = "v%s" % __version__ program_shortdesc = __import__('__main__').__doc__.split("\n")[1] program_license = """%s Created by Paul Ross on %s. Copyright 2008-2015. All rights reserved. Licensed under GPL 2.0 USAGE """ % (program_shortdesc, str(__date__)) parser = argparse.ArgumentParser( description=program_license, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( "-c", action="store_true", dest="plot_conditional", default=False, help= "Add conditionally included files to the plots. [default: %(default)s]" ) parser.add_argument("-d", "--dump", action="append", dest="dump", default=[], help="""Dump output, additive. Can be: C - Conditional compilation graph. F - File names encountered and their count. I - Include graph. M - Macro environment. T - Token count. R - Macro dependencies as an input to DOT. [default: %(default)s]""") parser.add_argument( "-g", "--glob", type=str, dest="glob", default="*.*", help= "Pattern match to use when processing directories. [default: %(default)s]" ) parser.add_argument("--heap", action="store_true", dest="heap", default=False, help="Profile memory usage. [default: %(default)s]") parser.add_argument( "-j", "--jobs", type=int, dest="jobs", default=0, help="""Max simultaneous processes when pre-processing directories. Zero uses number of native CPUs [%d]. 1 means no multiprocessing.""" % multiprocessing.cpu_count() \ + " [default: %(default)s]" ) parser.add_argument("-k", "--keep-going", action="store_true", dest="keep_going", default=False, help="Keep going. [default: %(default)s]") parser.add_argument( "-l", "--loglevel", type=int, dest="loglevel", default=30, help="Log Level (debug=10, info=20, warning=30, error=40, critical=50)" \ " [default: %(default)s]" ) parser.add_argument("-o", "--output", type=str, dest="output", default="out", help="Output directory. [default: %(default)s]") parser.add_argument( "-p", action="store_true", dest="ignore_pragma", default=False, help="Ignore pragma statements. [default: %(default)s]") parser.add_argument( "-r", "--recursive", action="store_true", dest="recursive", default=False, help="Recursively process directories. [default: %(default)s]") parser.add_argument( "-t", "--dot", action="store_true", dest="include_dot", default=False, help="""Write an DOT include dependency table and execute DOT on it to create a SVG file. [default: %(default)s]""") parser.add_argument( "-G", action="store_true", dest="gcc_extensions", default=False, help= """Support GCC extensions. Currently only #include_next. [default: %(default)s]""" ) parser.add_argument(dest="path", nargs=1, help="Path to source file.") Cpp.addStandardArguments(parser) args = parser.parse_args() # print(' ARGS '.center(75, '-')) # print(args) # print(' END: ARGS '.center(75, '-')) clkStart = time.clock() # Initialise logging etc. inPath = args.path[0] if args.jobs != 1 and os.path.isdir(inPath): # Multiprocessing logFormat = '%(asctime)s %(levelname)-8s [%(process)5d] %(message)s' else: logFormat = '%(asctime)s %(levelname)-8s %(message)s' logging.basicConfig( level=args.loglevel, format=logFormat, # datefmt='%y-%m-%d % %H:%M:%S', stream=sys.stdout) # Memory usage dump if args.heap: try: from guppy import hpy except ImportError: print('Can not profile memory as you do not have guppy installed:' \ ' http://guppy-pe.sourceforge.net/') args.heap = False # Start memory profiling if requested if args.heap: myHeap = hpy() myHeap.setrelheap() else: myHeap = None # Create objects to pass to pre-processor myIncH = IncludeHandler.CppIncludeStdOs( theUsrDirs=args.incUsr or [], theSysDirs=args.incSys or [], ) preDefMacros = {} if args.predefines: for d in args.predefines: _tup = d.split('=') if len(_tup) == 2: preDefMacros[_tup[0]] = _tup[1] + '\n' elif len(_tup) == 1: preDefMacros[_tup[0]] = '\n' else: raise ValueError('Can not read macro definition: %s' % d) # Create the job specification jobSpec = MainJobSpec( incHandler=myIncH, preDefMacros=preDefMacros, preIncFiles=Cpp.predefinedFileObjects(args), diagnostic=CppDiagnostic.PreprocessDiagnosticKeepGoing() if args.keep_going else None, pragmaHandler=PragmaHandler.PragmaHandlerNull() if args.ignore_pragma else None, keepGoing=args.keep_going, conditionalLevel=2 if args.plot_conditional else 0, dumpList=args.dump, helpMap=retOptionMap(parser, args), includeDOT=args.include_dot, cmdLine=' '.join(sys.argv), gccExtensions=args.gcc_extensions, ) if os.path.isfile(inPath): preprocessFileToOutput(inPath, args.output, jobSpec) writeIndexHtml([inPath], args.output, jobSpec) elif os.path.isdir(inPath): preprocessDirToOutput( inPath, args.output, jobSpec, globMatch=args.glob, recursive=args.recursive, numJobs=args.jobs, ) else: logging.fatal('%s is neither a file or a directory!' % inPath) return 1 if args.heap and myHeap is not None: print('Dump of heap:') h = myHeap.heap() print(h) print() print('Dump of heap byrcs:') print(h.byrcs) print() clkExec = time.clock() - clkStart print('CPU time = %8.3f (S)' % clkExec) print('Bye, bye!') return 0
def testUndefined(self): """Undefined behaviour does not raise an ExceptionCppDiagnosticUndefined.""" myObj = CppDiagnostic.PreprocessDiagnosticKeepGoing() self.assertEqual(0, myObj._cntrUndefined) myObj.undefined("PreprocessDiagnosticKeepGoing.undefined()") self.assertEqual(1, myObj._cntrUndefined)