Skip to content

Commit

Permalink
Bug 1468273 - autopep8 on ipc/ r=froydnj
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 63pBUqmDlkZ

--HG--
extra : rebase_source : 6570b1b12e56c2ae1009b00d1cf9a70fb3f651d8
  • Loading branch information
sylvestre committed Jun 10, 2018
1 parent 9190744 commit eb7d846
Show file tree
Hide file tree
Showing 14 changed files with 1,516 additions and 1,050 deletions.
13 changes: 10 additions & 3 deletions ipc/ipdl/ipdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import optparse, os, re, sys
import optparse
import os
import re
import sys
from cStringIO import StringIO
import mozpack.path as mozpath
from ConfigParser import RawConfigParser

import ipdl


def log(minv, fmt, *args):
if _verbosity >= minv:
print fmt % args

# process command line


op = optparse.OptionParser(usage='ipdl.py [options] IPDLfiles...')
op.add_option('-I', '--include', dest='includedirs', default=[ ],
op.add_option('-I', '--include', dest='includedirs', default=[],
action='append',
help='Additional directory to search for included protocol specifications')
op.add_option('-s', '--sync-msg-list', dest='syncMsgList', default='sync-messages.ini',
Expand Down Expand Up @@ -44,7 +49,7 @@ def log(minv, fmt, *args):
msgMetadata = options.msgMetadata
headersdir = options.headersdir
cppdir = options.cppdir
includedirs = [ os.path.abspath(incdir) for incdir in options.includedirs ]
includedirs = [os.path.abspath(incdir) for incdir in options.includedirs]

if not len(files):
op.error("No IPDL files specified")
Expand All @@ -59,11 +64,13 @@ def log(minv, fmt, *args):
allmessageprognames = []
allprotocols = []


def normalizedFilename(f):
if f == '-':
return '<stdin>'
return f


log(2, 'Reading sync message list')
parser = RawConfigParser()
parser.readfp(open(options.syncMsgList))
Expand Down
18 changes: 11 additions & 7 deletions ipc/ipdl/ipdl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

__all__ = [ 'gencxx', 'genipdl', 'parse', 'typecheck', 'writeifmodified',
'checkSyncMessage', 'checkFixedSyncMessages' ]
__all__ = ['gencxx', 'genipdl', 'parse', 'typecheck', 'writeifmodified',
'checkSyncMessage', 'checkFixedSyncMessages']

import os, sys
import os
import sys
from cStringIO import StringIO

from ipdl.cgen import IPDLCodeGen
Expand All @@ -17,7 +18,7 @@
from ipdl.cxx.cgen import CxxCodeGen


def parse(specstring, filename='/stdin', includedirs=[ ], errout=sys.stderr):
def parse(specstring, filename='/stdin', includedirs=[], errout=sys.stderr):
'''Return an IPDL AST if parsing was successful. Print errors to |errout|
if it is not.'''
# The file type and name are later enforced by the type checker.
Expand All @@ -35,6 +36,7 @@ def parse(specstring, filename='/stdin', includedirs=[ ], errout=sys.stderr):
print >>errout, p
return None


def typecheck(ast, errout=sys.stderr):
'''Return True iff |ast| is well typed. Print errors to |errout| if
it is not.'''
Expand All @@ -51,11 +53,12 @@ def resolveHeader(hdr):
outheadersdir,
*([ns.name for ns in ast.namespaces] + [hdr.name]))
]

def resolveCpp(cpp):
return [ cpp, os.path.join(outcppdir, cpp.name) ]
return [cpp, os.path.join(outcppdir, cpp.name)]

for ast, filename in ([ resolveHeader(hdr) for hdr in headers ]
+ [ resolveCpp(cpp) for cpp in cpps ]):
for ast, filename in ([resolveHeader(hdr) for hdr in headers]
+ [resolveCpp(cpp) for cpp in cpps]):
tempfile = StringIO()
CxxCodeGen(tempfile).cgen(ast)
writeifmodified(tempfile.getvalue(), filename)
Expand All @@ -68,6 +71,7 @@ def genipdl(ast, outdir):
def genmsgenum(ast):
return msgenums(ast.protocol, pretty=True)


def writeifmodified(contents, file):
dir = os.path.dirname(file)
os.path.exists(dir) or os.makedirs(dir)
Expand Down
93 changes: 69 additions & 24 deletions ipc/ipdl/ipdl/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
INPUT_PRIORITY = 2
HIGH_PRIORITY = 3


class Visitor:
def defaultVisit(self, node):
raise Exception, "INTERNAL ERROR: no visitor for node type `%s'"% (
raise Exception, "INTERNAL ERROR: no visitor for node type `%s'" % (
node.__class__.__name__)

def visitTranslationUnit(self, tu):
Expand All @@ -31,7 +32,6 @@ def visitTranslationUnit(self, tu):
if tu.protocol:
tu.protocol.accept(self)


def visitCxxInclude(self, inc):
pass

Expand Down Expand Up @@ -88,27 +88,33 @@ def visitTypeSpec(self, ts):
def visitDecl(self, d):
pass


class Loc:
def __init__(self, filename='<??>', lineno=0):
assert filename
self.filename = filename
self.lineno = lineno

def __repr__(self):
return '%r:%r'% (self.filename, self.lineno)
return '%r:%r' % (self.filename, self.lineno)

def __str__(self):
return '%s:%s'% (self.filename, self.lineno)
return '%s:%s' % (self.filename, self.lineno)


Loc.NONE = Loc(filename='<??>', lineno=0)


class _struct:
pass


class Node:
def __init__(self, loc=Loc.NONE):
self.loc = loc

def accept(self, visitor):
visit = getattr(visitor, 'visit'+ self.__class__.__name__, None)
visit = getattr(visitor, 'visit' + self.__class__.__name__, None)
if visit is None:
return getattr(visitor, 'defaultVisit')(self)
return visit(self)
Expand All @@ -122,40 +128,47 @@ class NamespacedNode(Node):
def __init__(self, loc=Loc.NONE, name=None):
Node.__init__(self, loc)
self.name = name
self.namespaces = [ ]
self.namespaces = []

def addOuterNamespace(self, namespace):
self.namespaces.insert(0, namespace)

def qname(self):
return QualifiedId(self.loc, self.name,
[ ns.name for ns in self.namespaces ])
[ns.name for ns in self.namespaces])


class TranslationUnit(NamespacedNode):
def __init__(self, type, name):
NamespacedNode.__init__(self, name=name)
self.filetype = type
self.filename = None
self.cxxIncludes = [ ]
self.includes = [ ]
self.builtinUsing = [ ]
self.using = [ ]
self.structsAndUnions = [ ]
self.cxxIncludes = []
self.includes = []
self.builtinUsing = []
self.using = []
self.structsAndUnions = []
self.protocol = None

def addCxxInclude(self, cxxInclude): self.cxxIncludes.append(cxxInclude)

def addInclude(self, inc): self.includes.append(inc)

def addStructDecl(self, struct): self.structsAndUnions.append(struct)

def addUnionDecl(self, union): self.structsAndUnions.append(union)

def addUsingStmt(self, using): self.using.append(using)

def setProtocol(self, protocol): self.protocol = protocol


class CxxInclude(Node):
def __init__(self, loc, cxxFile):
Node.__init__(self, loc)
self.file = cxxFile


class Include(Node):
def __init__(self, loc, type, name):
Node.__init__(self, loc)
Expand All @@ -164,43 +177,61 @@ def __init__(self, loc, type, name):
suffix += 'h'
self.file = "%s.%s" % (name, suffix)


class UsingStmt(Node):
def __init__(self, loc, cxxTypeSpec, cxxHeader=None, kind=None, refcounted=False):
Node.__init__(self, loc)
assert not isinstance(cxxTypeSpec, str)
assert cxxHeader is None or isinstance(cxxHeader, str);
assert cxxHeader is None or isinstance(cxxHeader, str)
assert kind is None or kind == 'class' or kind == 'struct'
self.type = cxxTypeSpec
self.header = cxxHeader
self.kind = kind
self.refcounted = refcounted

def canBeForwardDeclared(self):
return self.isClass() or self.isStruct()

def isClass(self):
return self.kind == 'class'

def isStruct(self):
return self.kind == 'struct'

def isRefcounted(self):
return self.refcounted

# "singletons"


class PrettyPrinted:
@classmethod
def __hash__(cls): return hash(cls.pretty)

@classmethod
def __str__(cls): return cls.pretty
def __str__(cls): return cls.pretty


class ASYNC(PrettyPrinted):
pretty = 'async'


class INTR(PrettyPrinted):
pretty = 'intr'


class SYNC(PrettyPrinted):
pretty = 'sync'


class INOUT(PrettyPrinted):
pretty = 'inout'


class IN(PrettyPrinted):
pretty = 'in'


class OUT(PrettyPrinted):
pretty = 'out'

Expand All @@ -210,41 +241,48 @@ def __init__(self, loc, namespace):
Node.__init__(self, loc)
self.name = namespace


class Protocol(NamespacedNode):
def __init__(self, loc):
NamespacedNode.__init__(self, loc)
self.sendSemantics = ASYNC
self.nested = NOT_NESTED
self.managers = [ ]
self.managesStmts = [ ]
self.messageDecls = [ ]
self.managers = []
self.managesStmts = []
self.messageDecls = []


class StructField(Node):
def __init__(self, loc, type, name):
Node.__init__(self, loc)
self.typespec = type
self.name = name


class StructDecl(NamespacedNode):
def __init__(self, loc, name, fields):
NamespacedNode.__init__(self, loc, name)
self.fields = fields


class UnionDecl(NamespacedNode):
def __init__(self, loc, name, components):
NamespacedNode.__init__(self, loc, name)
self.components = components


class Manager(Node):
def __init__(self, loc, managerName):
Node.__init__(self, loc)
self.name = managerName


class ManagesStmt(Node):
def __init__(self, loc, managedName):
Node.__init__(self, loc)
self.name = managedName


class MessageDecl(Node):
def __init__(self, loc):
Node.__init__(self, loc)
Expand All @@ -253,8 +291,8 @@ def __init__(self, loc):
self.nested = NOT_NESTED
self.prio = NORMAL_PRIORITY
self.direction = None
self.inParams = [ ]
self.outParams = [ ]
self.inParams = []
self.outParams = []
self.compress = ''
self.verify = ''

Expand All @@ -271,29 +309,34 @@ def addModifiers(self, modifiers):
elif modifier == 'verify':
self.verify = modifier
elif modifier != '':
raise Exception, "Unexpected message modifier `%s'"% modifier
raise Exception, "Unexpected message modifier `%s'" % modifier


class Param(Node):
def __init__(self, loc, typespec, name):
Node.__init__(self, loc)
self.name = name
self.typespec = typespec


class TypeSpec(Node):
def __init__(self, loc, spec):
Node.__init__(self, loc)
self.spec = spec # QualifiedId
self.array = 0 # bool
self.nullable = 0 # bool

def basename(self):
return self.spec.baseid

def __str__(self): return str(self.spec)
def __str__(self): return str(self.spec)


class QualifiedId: # FIXME inherit from node?
def __init__(self, loc, baseid, quals=[ ]):
def __init__(self, loc, baseid, quals=[]):
assert isinstance(baseid, str)
for qual in quals: assert isinstance(qual, str)
for qual in quals:
assert isinstance(qual, str)

self.loc = loc
self.baseid = baseid
Expand All @@ -306,9 +349,11 @@ def qualify(self, id):
def __str__(self):
if 0 == len(self.quals):
return self.baseid
return '::'.join(self.quals) +'::'+ self.baseid
return '::'.join(self.quals) + '::' + self.baseid

# added by type checking passes


class Decl(Node):
def __init__(self, loc):
Node.__init__(self, loc)
Expand Down
Loading

0 comments on commit eb7d846

Please sign in to comment.