Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for compile errors in parsed source file. #454

Merged
merged 2 commits into from
Oct 18, 2018
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
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_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this just an error before?

};
"""

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)