Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions devtools/code_health/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ClassInfo:
line: int
has_bases: bool
members: tuple
internal: bool = False # nested in a class, or in a detail or anonymous namespace


def tokens(source):
Expand Down Expand Up @@ -264,6 +265,16 @@ def scope_label(toks, index, heads):
return None


def is_internal_scope(toks, index, heads):
"""A '{' that opens a class, an anonymous namespace or a detail namespace."""
if index in heads:
return True
if index > 0 and toks[index - 1][0] == 'namespace':
return True
label = scope_label(toks, index, heads) or ''
return label == 'detail' or label.endswith('::detail')


def find_classes(source):
toks = tokens(source)
pairs = brace_pairs(toks)
Expand All @@ -273,7 +284,7 @@ def find_classes(source):
head = class_head(toks, index)
if head and head[0] and head[2] in pairs:
heads[head[2]] = (head[0], head[1], line, text)
result, stack = [], []
result, stack, internal = [], [], []
for index, (text, _) in enumerate(toks):
if text == '{':
label = scope_label(toks, index, heads)
Expand All @@ -282,14 +293,19 @@ def find_classes(source):
body = members(toks, index + 1, pairs[index], pairs, keyword, name)
if body is not None:
qualified = '::'.join([s for s in stack if s] + [name])
result.append(ClassInfo(qualified, line, bases, tuple(body)))
result.append(ClassInfo(qualified, line, bases, tuple(body), any(internal)))
stack.append(label)
internal.append(is_internal_scope(toks, index, heads))
elif text == '}' and stack:
stack.pop()
internal.pop()
return result


def is_single_method_interface(info):
"""Nested and detail-namespace classes are implementation details (e.g. type erasure)."""
if info.internal:
return False
methods = [m for m in info.members if m.kind == 'method']
others = [m for m in info.members
if m.kind != 'method' and not (m.kind == 'dtor' and m.virtual)]
Expand Down
14 changes: 14 additions & 0 deletions devtools/code_health/tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ def test_single_method_interface(self):
with self.subTest(source=source):
self.assertEqual(0, values(source)['single_method_interface'])

def test_single_method_interface_skips_implementation_details(self):
nested = ('class F{\nprivate:\n class Callable{\n public:\n virtual ~Callable() = default;\n'
' virtual void call() = 0;\n };\n Callable* m_impl;\n};')
self.assertEqual(0, values(nested, 'F::Callable')['single_method_interface'])
for source in ('namespace{ struct L{ virtual void on() = 0; }; }',
'namespace a{ namespace detail{ struct L{ virtual void on() = 0; }; } }',
'namespace a::detail{ struct L{ virtual void on() = 0; }; }'):
with self.subTest(source=source):
self.assertEqual(0, values(source)['single_method_interface'])
for source in ('namespace a{ struct L{ virtual void on() = 0; }; }',
'namespace a::b{ struct L{ virtual void on() = 0; }; }'):
with self.subTest(source=source):
self.assertEqual(1, values(source)['single_method_interface'])

def test_public_method_count_excludes_special_members(self):
extra = (' A();\n A(const A&) = delete;\n ~A();\n A& operator=(A&&);\n'
' operator bool() const;\n void gone() = delete;\nprivate:\n void hidden();\npublic:\n')
Expand Down
Loading