Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
myint committed Apr 29, 2018
1 parent b15dae7 commit f2a57f7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
25 changes: 11 additions & 14 deletions cpp/ast.py
Expand Up @@ -350,7 +350,6 @@ def __init__(self, start, end, name, templated_types, modifiers,
templated_types: [Class (Type?)] template type info between <>
modifiers: [str] type modifiers (keywords) eg, const, mutable, etc.
reference, pointer, array: bools
"""
_GenericDeclaration.__init__(self, start, end, name, [])
self.templated_types = templated_types
Expand Down Expand Up @@ -414,7 +413,6 @@ def to_type(self, tokens):
Returns:
[Class(...), ...]
"""
result = []
name_tokens = []
Expand Down Expand Up @@ -833,12 +831,12 @@ def _get_var_tokens_up_to_w_function(self, skip_bracket_content,
names = [token.name for token in tokens]
ctr = collections.Counter(names)
if ('(' in expected_tokens and
ctr['<'] != ctr['>'] and
ctr['function'] == 1 and
last.name == '('):
ctr['<'] != ctr['>'] and
ctr['function'] == 1 and
last.name == '('):

idx = names.index("function")
if idx + 1 < len(tokens) and tokens[idx + 1].name == "<":
idx = names.index('function')
if idx + 1 < len(tokens) and tokens[idx + 1].name == '<':
new_tokens, new_last = \
self._get_var_tokens_up_to(False, '(', ';')
tokens.append(last)
Expand Down Expand Up @@ -1096,10 +1094,10 @@ def _get_method(self, return_type_and_name, modifiers, templated_types,
member = member[0]
if token.name == '(' or token.name == '{':
end = '}' if token.name == '{' else ')'
initializers[member] = [x
for x in list(self._get_matching_char(
token.name, end))
if x.name != ',' and x.name != end]
initializers[member] = [
x for x in list(self._get_matching_char(token.name,
end))
if x.name != ',' and x.name != end]
token = self._get_next_token()

# Handle pointer to functions.
Expand Down Expand Up @@ -1448,7 +1446,7 @@ def handle_template(self):
elif token.name == 'template':
return self.handle_template()
self._add_back_token(token)
tokens, last = self._get_var_tokens_up_to_w_function(False, "(", ";")
tokens, last = self._get_var_tokens_up_to_w_function(False, '(', ';')
tokens.append(last)
self._add_back_tokens(tokens)
if last.name == '(':
Expand Down Expand Up @@ -1625,7 +1623,7 @@ def handle_using(self):
tokens = self._get_tokens_up_to(';')
assert tokens
new_type = self.converter.to_type(tokens)
if "namespace" in new_type[0].modifiers:
if 'namespace' in new_type[0].modifiers:
return Using(tokens[0].start, tokens[0].end, tokens)
else:
# aside from namespaces, "using" can be used just like a typedef
Expand Down Expand Up @@ -1659,7 +1657,6 @@ def builder_from_source(source, filename, system_includes,
Returns:
ASTBuilder
"""
return ASTBuilder(tokenize.get_tokens(source),
filename,
Expand Down
10 changes: 5 additions & 5 deletions cpp/find_warnings.py
Expand Up @@ -20,7 +20,6 @@
warnings will always be displayed. There is no way to suppress any.
There also needs to be a way to use annotations in the source code to
suppress warnings.
"""

from __future__ import absolute_import
Expand Down Expand Up @@ -303,13 +302,13 @@ def _add_use(node, namespace, name=''):
# Try to search for the value of the variable declaration for any
# symbols, such as `#define` values or other variable names which
# may be included in other files.
obj = getattr(node, "initial_value", None)
obj = getattr(node, 'initial_value', None)
if obj:
_do_lookup(obj, namespace)

# If node is a VariableDeclaration, check if the variable type is
# a symbol used in other includes.
obj = getattr(node, "type", None)
obj = getattr(node, 'type', None)
if obj and isinstance(obj.name, basestring):
_do_lookup(obj.name, namespace)

Expand All @@ -319,7 +318,8 @@ def _add_use(node, namespace, name=''):
return

def _add_variable(node, namespace, reference=False):
obj = node.type if isinstance(node, ast.VariableDeclaration) else node
obj = node.type if isinstance(
node, ast.VariableDeclaration) else node

if obj.reference or obj.pointer or reference:
_add_reference(obj.name, namespace)
Expand Down Expand Up @@ -397,7 +397,7 @@ def _add_template_use(name, types, namespace, reference=False):
# These are things like auto_ptr which do
# not require the class definition, only decl.
_add_reference(cls.name, namespace)
elif name.startswith('Q') and name.endswith("Pointer"):
elif name.startswith('Q') and name.endswith('Pointer'):
# Special case templated classes from the Qt framework.
_add_reference(cls.name, namespace)
else:
Expand Down
7 changes: 0 additions & 7 deletions cpp/symbols.py
Expand Up @@ -52,7 +52,6 @@ def _lookup_namespace(self, symbol, namespace):
Args:
symbol: Symbol
namespace: pointer into self.namespaces
"""
for namespace_part in symbol.parts:
namespace = namespace.get(namespace_part)
Expand All @@ -67,7 +66,6 @@ def _lookup_global(self, symbol):
Args:
symbol: Symbol
"""
assert symbol.parts
namespace = self.namespaces
Expand All @@ -91,7 +89,6 @@ def _lookup_in_all_namespaces(self, symbol):
Args:
symbol: Symbol
"""
namespace = self.namespaces
# Create a stack of namespaces.
Expand Down Expand Up @@ -123,7 +120,6 @@ def lookup_symbol(self, name, namespace_stack):
Raises:
Error if the symbol cannot be found.
"""
# TODO(nnorwitz): a convenient API for this depends on the
# representation of the name. e.g., does symbol_name contain
Expand All @@ -148,7 +144,6 @@ def _add(self, symbol_name, namespace, node, module):
"""Helper function for adding symbols.
See add_symbol().
"""
result = symbol_name in namespace
namespace[symbol_name] = node, module
Expand All @@ -165,7 +160,6 @@ def add_symbol(self, symbol_name, namespace_stack, node, module):
Returns:
bool(if symbol was *not* already present)
"""
# TODO(nnorwitz): verify symbol_name doesn't contain :: ?
if namespace_stack:
Expand All @@ -185,7 +179,6 @@ def get_namespace(self, name_seq):
Returns:
['names', 'that', 'are', 'namespaces', 'possibly', 'empty', 'list']
"""
namespaces = self.namespaces
result = []
Expand Down
8 changes: 3 additions & 5 deletions cpp/tokenize.py
Expand Up @@ -27,7 +27,7 @@
_letters = 'abcdefghijklmnopqrstuvwxyz'
_valid_identifier_first_char = _letters + _letters.upper() + '_$'
_valid_identifier_char = _valid_identifier_first_char + '0123456789'
VALID_IDENTIFIER_FIRST_CHARS = frozenset( _valid_identifier_first_char )
VALID_IDENTIFIER_FIRST_CHARS = frozenset(_valid_identifier_first_char)
VALID_IDENTIFIER_CHARS = frozenset(_valid_identifier_char)
HEX_DIGITS = frozenset('0123456789abcdefABCDEF')
INT_OR_FLOAT_DIGITS = frozenset('01234567890eE-+')
Expand Down Expand Up @@ -59,7 +59,6 @@ class Token(object):
start contains the index of the first char of the token in the source
end contains the index of the last char of the token in the source
"""

def __init__(self, token_type, name, start, end):
Expand Down Expand Up @@ -110,7 +109,6 @@ def get_tokens(source):
Yields:
Token that represents the next token in the source.
"""
if not source.endswith('\n'):
source += '\n'
Expand All @@ -137,7 +135,8 @@ def get_tokens(source):
token_type = UNKNOWN
start = i
c = source[i]
if c in valid_identifier_first_chars or c == '_': # Find a string token.
# Find a string token.
if c in valid_identifier_first_chars or c == '_':
token_type = NAME
while source[i] in valid_identifier_chars:
i += 1
Expand Down Expand Up @@ -291,7 +290,6 @@ def _find(string, sub_string, start_index):
"""Return index of sub_string in string.
Raise TokenError if sub_string is not found.
"""
result = string.find(sub_string, start_index)
if result == -1:
Expand Down
11 changes: 5 additions & 6 deletions test_ast.py
Expand Up @@ -35,7 +35,6 @@ def _install_generic_equal(cls, attrs):
Args:
cls: Python class to add __eq__ method to
attrs: string - space separated of attribute names to compare
"""
attrs = attrs.split()

Expand Down Expand Up @@ -610,7 +609,6 @@ class ASTBuilderIntegrationTest(unittest.TestCase):
an integration test.
It doesn't test any individual method. It tests whole code blocks.
"""

def test_variable_array(self):
Expand Down Expand Up @@ -993,7 +991,8 @@ class Foo {
arg2 = nodes[0].body[2]
arg3 = nodes[0].body[3]

exp_ctor = Function('Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[])
exp_ctor = Function(
'Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[])
exp_var = [VariableDeclaration('arg1', Type('int'), initial_value='1'),
VariableDeclaration('arg2', Type('int'), initial_value='2'),
VariableDeclaration('arg3', Type('int'), initial_value='3')]
Expand Down Expand Up @@ -1024,7 +1023,8 @@ class Foo {
arg2 = nodes[0].body[2]
arg3 = nodes[0].body[3]

exp_ctor = Function('Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[])
exp_ctor = Function(
'Foo', [], [], modifiers=ast.FUNCTION_CTOR, body=[])
exp_var = [VariableDeclaration('arg1', Type('int'), initial_value='1'),
VariableDeclaration('arg2', Type('int'), initial_value='2'),
VariableDeclaration('arg3', Type('int'), initial_value='3')]
Expand Down Expand Up @@ -1125,7 +1125,7 @@ def test_system_include(self):

def test_include_path_overrides(self):
paths = [os.path.dirname(os.path.realpath(__file__))]
fname = "test/include.h"
fname = 'test/include.h'

def _tokens():
tokens_quotes = get_tokens('#include "test/include.h"')
Expand Down Expand Up @@ -1235,6 +1235,5 @@ def test_inline_function(self):
nodes[0])



if __name__ == '__main__':
unittest.main()

0 comments on commit f2a57f7

Please sign in to comment.