Commit 017ceae4 authored by Alastair Robertson's avatar Alastair Robertson

Clang parser: Add support for unions

parent 61690d65
......@@ -116,7 +116,8 @@ void ClangParser::parse(ast::Program *program, StructMap &structs)
{
auto &structs = *static_cast<StructMap*>(client_data);
if (clang_getCursorKind(parent) != CXCursor_StructDecl) // TODO CXCursor_UnionDecl
if (clang_getCursorKind(parent) != CXCursor_StructDecl &&
clang_getCursorKind(parent) != CXCursor_UnionDecl)
return CXChildVisit_Recurse;
if (clang_getCursorKind(c) == CXCursor_FieldDecl)
......
......@@ -92,7 +92,7 @@ pid|tid|uid|gid|nsecs|cpu|comm|stack|ustack|arg[0-9]|retval|func {
<STR>. { driver.error(loc, std::string("invalid character '") +
std::string(yytext) + std::string("'")); }
struct { BEGIN(STRUCT); buffer = std::string(yytext); open_brackets = 0; }
struct|union { BEGIN(STRUCT); buffer = std::string(yytext); open_brackets = 0; }
<STRUCT>"{" { BEGIN(STRUCT); buffer += std::string(yytext); open_brackets++; }
<STRUCT>"}" { buffer += std::string(yytext);
if (open_brackets == 1)
......
......@@ -43,6 +43,38 @@ TEST(clang_parser, integers)
EXPECT_EQ(structs["Foo"].fields["z"].offset, 8);
}
TEST(clang_parser, c_union)
{
StructMap structs;
parse("union Foo { char c; short s; int i; long l; }", structs);
ASSERT_EQ(structs.size(), 1);
ASSERT_EQ(structs.count("Foo"), 1);
EXPECT_EQ(structs["Foo"].size, 8);
ASSERT_EQ(structs["Foo"].fields.size(), 4);
ASSERT_EQ(structs["Foo"].fields.count("c"), 1);
ASSERT_EQ(structs["Foo"].fields.count("s"), 1);
ASSERT_EQ(structs["Foo"].fields.count("i"), 1);
ASSERT_EQ(structs["Foo"].fields.count("l"), 1);
EXPECT_EQ(structs["Foo"].fields["c"].type.type, Type::integer);
EXPECT_EQ(structs["Foo"].fields["c"].type.size, 1);
EXPECT_EQ(structs["Foo"].fields["c"].offset, 0);
EXPECT_EQ(structs["Foo"].fields["s"].type.type, Type::integer);
EXPECT_EQ(structs["Foo"].fields["s"].type.size, 2);
EXPECT_EQ(structs["Foo"].fields["s"].offset, 0);
EXPECT_EQ(structs["Foo"].fields["i"].type.type, Type::integer);
EXPECT_EQ(structs["Foo"].fields["i"].type.size, 4);
EXPECT_EQ(structs["Foo"].fields["i"].offset, 0);
EXPECT_EQ(structs["Foo"].fields["l"].type.type, Type::integer);
EXPECT_EQ(structs["Foo"].fields["l"].type.size, 8);
EXPECT_EQ(structs["Foo"].fields["l"].offset, 0);
}
TEST(clang_parser, integer_ptr)
{
StructMap structs;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment