Skip to content

Commit

Permalink
Add test for compile errors in parsed source file. (#454)
Browse files Browse the repository at this point in the history
More work is needed to fail for compile errors in transitively included headers.
  • Loading branch information
jbcoe authored and ajbennieston committed Oct 18, 2018
1 parent a955b0c commit 1bc1480
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
24 changes: 22 additions & 2 deletions ffig/cppmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from ffig.clang.cindex import AccessSpecifier
from ffig.clang.cindex import CursorKind
from ffig.clang.cindex import Diagnostic
from ffig.clang.cindex import ExceptionSpecificationKind
from ffig.clang.cindex import TypeKind

Expand Down Expand Up @@ -56,7 +57,8 @@ def __init__(self, cursor):
self.name = cursor.spelling
arguments = [x.spelling or None for x in cursor.get_arguments()]
argument_types = [Type(x) for x in cursor.type.argument_types()]
self.is_noexcept = (cursor.exception_specification_kind == ExceptionSpecificationKind.BASIC_NOEXCEPT)
self.is_noexcept = (cursor.exception_specification_kind ==
ExceptionSpecificationKind.BASIC_NOEXCEPT)
self.return_type = Type(cursor.type.get_result())
self.arguments = []
self.annotations = _get_annotations(cursor)
Expand Down Expand Up @@ -162,11 +164,29 @@ def __init__(self, translation_unit):
self.filename = translation_unit.spelling
self.functions = []
self.classes = []

def is_error_in_current_file(diagnostic):
if str(diagnostic.location.file) != str(translation_unit.spelling):
return False
if diagnostic.severity == Diagnostic.Error:
return True
if diagnostic.severity == Diagnostic.Fatal:
return True
return False

errors = [
d for d in translation_unit.diagnostics if is_error_in_current_file(d)]
if errors:
raise ValueError('Errors in source file:\{}'.format(
'\n'.join(str(e) for e in errors)))

self.add_child_nodes(translation_unit.cursor, [])

def __repr__(self):
return "<cppmodel.Model filename={}, classes={}, functions={}>".format(
self.filename, [c.name for c in self.classes], [f.name for f in self.functions])
self.filename,
[c.name for c in self.classes],
[f.name for f in self.functions])

def extend(self, translation_unit):
m = Model(translation_unit)
Expand Down
6 changes: 3 additions & 3 deletions tests/cppmodel/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_class_member_data():
class A {};
class B {
int x_;
B b_;
A a_;
};
"""

Expand All @@ -156,8 +156,8 @@ class B {
assert c.members[0].name == "x_"

assert c.members[1].type.kind == TypeKind.RECORD
assert c.members[1].type.name == "B"
assert c.members[1].name == "b_"
assert c.members[1].type.name == "A"
assert c.members[1].name == "a_"


def test_string_representation():
Expand Down
10 changes: 10 additions & 0 deletions tests/cppmodel/test_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from util import get_tu
import ffig.cppmodel
from nose.tools import assert_equals
from nose.tools import assert_raises


def test_repr():
Expand All @@ -12,3 +13,12 @@ def test_repr():
assert_equals(
str(model),
"<cppmodel.Model filename=t.cpp, classes=['A'], functions=['foo']>")


def test_exception_for_missing_include():
source = '#include "major_tom.h"'
tu = get_tu(source, 'cpp')

def f():
ffig.cppmodel.Model(tu)
assert_raises(ValueError, f)

0 comments on commit 1bc1480

Please sign in to comment.