def __init__(self, theDiagnostic): """Constructor, takes a CppDiagnostic object to give to the PpTokeniser.""" self._diagnostic = theDiagnostic # Stack of FileInclude objects self._fincS = [] # Allied to the file stack is the include graph recorder. self._figr = FileIncludeGraph.FileIncludeGraphRoot()
def processIncGraphToXml(theLex, theFilePath): """Convert a Include graph from a PpLexer to SVG in theFilePath.""" myVis = FileIncludeGraph.FigVisitorTree(IncGraphXML) theLex.fileIncludeGraphRoot.acceptVisitor(myVis) # Tree is now a graph of class IncGraphXML objects myIgs = myVis.tree() # Write to file myIgs.writeToFilePath(theFilePath)
def __init__(self, theDiagnostic): """Constructor, takes a CppDiagnostic object to give to the PpTokeniser. :param theDiagnostic: The diagnostic for emitting messages. :type theDiagnostic: :py:class:`cpip.core.CppDiagnostic.PreprocessDiagnosticStd` :returns: ``NoneType`` """ self._diagnostic = theDiagnostic # Stack of FileInclude objects self._fincS = [] # Allied to the file stack is the include graph recorder. self._figr = FileIncludeGraph.FileIncludeGraphRoot()
def main(): print('Processing:', sys.argv[1]) myH = IncludeHandler.CppIncludeStdOs( theUsrDirs=[ '../usr', ], theSysDirs=[ '../sys', ], ) myLex = PpLexer.PpLexer(sys.argv[1], myH) tu = ''.join(tok.t for tok in myLex.ppTokens(minWs=True)) myVis = FileIncludeGraph.FigVisitorTree(MyVisitorTreeNode) myLex.fileIncludeGraphRoot.acceptVisitor(myVis) myTree = myVis.tree() print(myTree)
def processIncGraphToSvg(theLex, theFilePath, theClass, tptPos, tptSweep): """Convert a Include graph from a PpLexer to SVG in theFilePath.""" myVis = FileIncludeGraph.FigVisitorTree(theClass) theLex.fileIncludeGraphRoot.acceptVisitor(myVis) # Tree is now a graph of: theClass myIgs = myVis.tree() # Pad the canvass myWidth = CANVAS_PADDING.prev \ + myIgs.plotCanvas.width \ + CANVAS_PADDING.next myDepth = CANVAS_PADDING.parent \ + myIgs.plotCanvas.depth \ + CANVAS_PADDING.child # Round up myWidth = Coord.Dim(int(myWidth.value+0.5), myWidth.units) myDepth = Coord.Dim(int(myDepth.value+0.5), myDepth.units) myCanvas = Coord.Box(myWidth, myDepth) #Create a plot configuration myTpt = TreePlotTransform.TreePlotTransform(myCanvas, tptPos, tptSweep) # Write to file myIgs.plotToFilePath(theFilePath, myTpt)
def retFileCountMap(theLexer): myFigr = theLexer.fileIncludeGraphRoot myFileNameVis = FileIncludeGraph.FigVisitorFileSet() myFigr.acceptVisitor(myFileNameVis) return myFileNameVis.fileNameMap
def includeStart(self, theFpo, theLineNum, isUncond, condStr, incLogic): """Start an ``#include`` file. :param theFpo: A :py:class:`.FileLocation.FilePathOrigin` object that identifies the file. :type theFpo: ``cpip.core.IncludeHandler.FilePathOrigin([_io.StringIO, str, NoneType, str]), cpip.core.IncludeHandler.FilePathOrigin([_io.TextIOWrapper, str, str, str])`` :param theLineNum: The integer line number of the file that includes (None if Root). :type theLineNum: ``NoneType, int`` :param isUncond: A boolean that is the conditional compilation state. :type isUncond: ``bool`` :param condStr: A string of the conditional compilation stack. :type condStr: ``str`` :param incLogic: A string that describes the find include logic. :type incLogic: ``list([]), list([str])`` :returns: ``NoneType`` """ logging.debug('FileIncludeStack.includeStart(): %s line=%d', theFpo.filePath, theLineNum) # print 'FileIncludeStack.includeStart(): new file %s included from line=%s' % (theFpo.filePath, str(theLineNum)) assert (len(self._fincS) == 0 and theLineNum is None or theLineNum == self._fincS[-1].ppt.pLineCol[0]) # import traceback # print ''.join(traceback.format_list(traceback.extract_stack())) self._fincS.append(FileInclude(theFpo, self._diagnostic)) # Now adjust the file graph, these could (but shouldn't!) raise as: # a. self._figr.addGraph just appends so can't raise. # b. FileIncludeGraph.__init__(...) just copies data so can't raise. # c. self._figr.graph() could raise if there is not a graph there but # previous calls should ensure that. # Thus assert(self._figr.numTrees() > 0) # d. addBranch can raise a ExceptionFileIncludeGraph if: # 0. The branch is zero length. # 1. The branch does not match the existing graph (this just # immediately checks the first item on the branch but then # recurses). # 2. theLine is a duplicate of an existing line. # 3. The branch has missing nodes. # # NOTE: Test is against 1 as we have appended to self._fincS above. if self.depth == 1: assert (theLineNum is None) # Add a new FileIncludeGraph self._figr.addGraph( FileIncludeGraph.FileIncludeGraph( theFpo.filePath, True, '', '', )) else: assert (self._figr.numTrees() > 0) # Add a branch to the existing FileIncludeGraph myFileStack = self.fileStack # print(' includeStart(): myFileStack %d:' % theLineNum, [os.path.basename(p)for p in self.fileStack]) # self._printFileList(' includeStart(): myFileStack %d:' % theLineNum, self.fileStack) # print(' As list:', self.fileStack) self._figr.graph.addBranch( myFileStack[:-1], # And subtract 1 as the "#include ...\\n" has been consumed theLineNum - 1, myFileStack[-1], isUncond, condStr, incLogic, )
def retIncludedFileSet(theLexer): """Returns a set of included file paths from a lexer.""" myFigr = theLexer.fileIncludeGraphRoot myFileNameVis = FileIncludeGraph.FigVisitorFileSet() myFigr.acceptVisitor(myFileNameVis) return myFileNameVis.fileNameSet
def test_00(self): _pathsUsr = [ os.path.join('usr'), os.path.join('usr', 'inc'), ] _pathsSys = [ os.path.join('sys'), os.path.join('sys', 'inc'), ] _initialTuContents = u"""Include usr/spam: #include "spam.h" Include usr/inc/eggs: #include "inc/eggs.h" Include sys/chips: #include <chips.h> Include sys/inc/beans: #include <inc/beans.h> """ _incFileMap = { os.path.join('usr', 'spam.h'): u"""Content of: user, spam.h """, os.path.join('usr', 'inc', 'eggs.h'): u"""Content of: user, include, eggs.h Which is much bigger.""", os.path.join('sys', 'chips.h'): u"""chips.h """, os.path.join('sys', 'inc', 'beans.h'): u"""Content of: system, include, beans.h Which is very big, 1, def, 345, and loads of other things. """, } _incSim = CppIncludeStringIO( _pathsUsr, _pathsSys, _initialTuContents, _incFileMap, ) _incSim.validateCpStack() self.assertEqual([], _incSim.cpStack) self.assertEqual(0, _incSim.cpStackSize) myLexer = PpLexer.PpLexer('src/spam.c', _incSim) result = ''.join([t.t for t in myLexer.ppTokens()]) # print('Result:') # print(result) expectedResult = """Include usr/spam: Content of: user, spam.h Include usr/inc/eggs: Content of: user, include, eggs.h Which is much bigger. Include sys/chips: chips.h Include sys/inc/beans: Content of: system, include, beans.h Which is very big, 1, def, 345, and loads of other things. """ self.assertEqual(result, expectedResult) myLexer.finalise() myFigr = myLexer.fileIncludeGraphRoot # print('FileIncludeGraph:') # print(myFigr) # WARN: excape \b (two places on last two lines expGraph = """src/spam.c [32, 24]: True "" "" 000002: #include usr/spam.h usr/spam.h [12, 8]: True "" "['"spam.h"', 'CP=None', 'usr=usr']" 000004: #include usr/inc/eggs.h usr/inc/eggs.h [23, 15]: True "" "['"inc/eggs.h"', 'CP=None', 'usr=usr']" 000006: #include sys/chips.h sys/chips.h [4, 3]: True "" "['<chips.h>', 'sys=sys']" 000008: #include sys/inc/beans.h sys/inc/beans.h [44, 27]: True "" "['<inc/beans.h>', 'sys=sys']\"""" # print('Exp FileIncludeGraph:') # print(expGraph) # self.maxDiff = None #for i, c in enumerate(str(myFigr)): # if c != expGraph[i]: # print '[%d] %s != %s' % (i, c, expGraph[i]) self.assertEqual(expGraph, str(myFigr)) # print() # myFigr.dumpGraph() self.assertEqual(expGraph, str(myFigr)) # Now visit the graph myVis = FileIncludeGraph.FigVisitorTree(IncGraphSVG.SVGTreeNodeMain) myFigr.acceptVisitor(myVis) # Tree is now a graph of IncGraphSVG.SVGTreeNode myIgs = myVis.tree() # print() # print('myIgs') #print myIgs # myIgs.dumpToStream() # print() # Create a plot configuration myTpt = TreePlotTransform.TreePlotTransform(myIgs.plotCanvas, 'left', '+') mySvg = io.StringIO() myIgs.plotToFileObj(mySvg, myTpt) # print() # print(mySvg.getvalue()) for aPos in myTpt.genRootPos(): for aDir in myTpt.genSweepDir(): aTpt = TreePlotTransform.TreePlotTransform( myIgs.plotCanvas, aPos, aDir) mySvg = io.StringIO() myIgs.plotToFileObj(mySvg, aTpt)
def test_10(self): """TestIncGraphSVGVisitor: Two pre-includes and a graph.""" return # First create an include graph myFigr = FileIncludeGraph.FileIncludeGraphRoot() myTcs = PpTokenCount.PpTokenCountStack() myFs = [] # push PreInclude_00 myFigr.addGraph( FileIncludeGraph.FileIncludeGraph('PreInclude_00', True, 'a >= b+2', 'Forced PreInclude_00')) myTcs.push() myFs.append('PreInclude_00') myTcs.counter().inc(PpToken.PpToken('PreInclude_00', 'identifier'), True, 8) myTcs.counter().inc(PpToken.PpToken('PreInclude_00', 'identifier'), False, 148) # pop PreInclude_00 myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 0) # push PreInclude_01 myFigr.addGraph( FileIncludeGraph.FileIncludeGraph('PreInclude_01', True, 'x > 1', 'Forced PreInclude_00')) myTcs.push() myFs.append('PreInclude_01') myTcs.counter().inc(PpToken.PpToken('PreInclude_01', 'identifier'), True, 7) myTcs.counter().inc(PpToken.PpToken('PreInclude_01', 'identifier'), False, 76) # pop PreInclude_01 myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 0) # push ITU.h myFigr.addGraph( FileIncludeGraph.FileIncludeGraph('ITU.h', True, '', 'CP=.')) myTcs.push() myFs.append('ITU.h') self.assertEqual(3, myFigr.numTrees()) # push ITU.h/a.h myFigr.graph.addBranch([ 'ITU.h', ], 15, 'a.h', True, '', 'CP=.') myTcs.push() myFs.append('a.h') myTcs.counter().inc(PpToken.PpToken('a.h', 'identifier'), True, 1) myTcs.counter().inc(PpToken.PpToken('a.h', 'identifier'), False, 1) # push ITU.h/a.h/aa.h myFigr.graph.addBranch(['ITU.h', 'a.h'], 17, 'aa.h', True, '', 'CP=.') myTcs.push() myFs.append('aa.h') myTcs.counter().inc(PpToken.PpToken('aa.h', 'identifier'), True, 2) myTcs.counter().inc(PpToken.PpToken('aa.h', 'identifier'), False, 2) # pop ITU.h/a.h/aa.h myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 2) # push ITU.h/a.h/ab.h myFigr.graph.addBranch(['ITU.h', 'a.h'], 19, 'ab.h', True, '', 'CP=.') myTcs.push() myFs.append('ab.h') myTcs.counter().inc(PpToken.PpToken('ab.h', 'identifier'), True, 4) myTcs.counter().inc(PpToken.PpToken('ab.h', 'identifier'), False, 4) # pop ITU.h/a.h/ab.h myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 2) # pop ITU.h/a.h myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 1) # push ITU.h/b.h myFigr.graph.addBranch([ 'ITU.h', ], 115, 'b.h', True, '', 'CP=.') myTcs.push() myFs.append('b.h') myTcs.counter().inc(PpToken.PpToken('b.h', 'identifier'), True, 8) myTcs.counter().inc(PpToken.PpToken('b.h', 'identifier'), False, 8) # push ITU.h/b.h/ba.h myFigr.graph.addBranch(['ITU.h', 'b.h'], 117, 'ba.h', True, '', 'CP=.') myTcs.push() myFs.append('ba.h') myTcs.counter().inc(PpToken.PpToken('ba.h', 'identifier'), True, 16) myTcs.counter().inc(PpToken.PpToken('ba.h', 'identifier'), False, 16) # pop ITU.h/b.h/ba.h myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 2) # push ITU.h/b.h/bb.h myFigr.graph.addBranch(['ITU.h', 'b.h'], 119, 'bb.h', True, '', 'CP=.') myTcs.push() myFs.append('bb.h') myTcs.counter().inc(PpToken.PpToken('bb.h', 'identifier'), True, 32) myTcs.counter().inc(PpToken.PpToken('bb.h', 'identifier'), False, 32) # pop ITU.h/b.h/bb.h myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 2) # pop ITU.h/b.h myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 1) # ITU.h myTcs.counter().inc(PpToken.PpToken('ITU.h', 'identifier'), True, 70) myTcs.counter().inc(PpToken.PpToken('ITU.h', 'identifier'), False, 70) myFigr.graph.retLatestNode(myFs).setTokenCounter(myTcs.pop()) myFs.pop() self.assertEqual(len(myFs), 0) myTcs.close() expGraph = """PreInclude_00 [156, 8]: True "a >= b+2" "Forced PreInclude_00" PreInclude_01 [83, 7]: True "x > 1" "Forced PreInclude_00" ITU.h [140, 70]: True "" "CP=." 000015: #include a.h a.h [2, 1]: True "" "CP=." 000017: #include aa.h aa.h [4, 2]: True "" "CP=." 000019: #include ab.h ab.h [8, 4]: True "" "CP=." 000115: #include b.h b.h [16, 8]: True "" "CP=." 000117: #include ba.h ba.h [32, 16]: True "" "CP=." 000119: #include bb.h bb.h [64, 32]: True "" "CP=.\"""" print() print(expGraph) print() print(str(myFigr)) print() myFigr.dumpGraph() self.assertEqual(expGraph, str(myFigr)) # Now visit the graph myVis = FileIncludeGraph.FigVisitorTree(IncGraphSVG.SVGTreeNode) myFigr.acceptVisitor(myVis) # Tree is now a graph of IncGraphSVG.SVGTreeNode myIgs = myVis.tree() print() print('myIgs') #print myIgs myIgs.dumpToStream() print() # Create a plot configuration myTpt = TreePlotTransform.TreePlotTransform(myIgs.plotCanvas, 'top', '-') mySvg = io.StringIO() myIgs.plotToFileObj(mySvg, myTpt) print() print(mySvg.getvalue())