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

Support symbolizing enums #3515

Merged
merged 14 commits into from
Oct 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests: clang_parser: Add tests for enum metadata
Add tests to ensure we are parsing the data we expect in clang_parser.
  • Loading branch information
danobi committed Oct 25, 2024
commit ceabfc5b852f578e8267ed428ad3433025f3a5d2
93 changes: 91 additions & 2 deletions tests/clang_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "clang_parser.h"

#include <iostream>
#include <utility>

#include "ast/passes/field_analyser.h"
#include "bpftrace.h"
#include "driver.h"
#include "struct.h"
#include "gtest/gtest.h"
#include <iostream>
#include <llvm/Config/llvm-config.h>

namespace bpftrace::test::clang_parser {
Expand Down Expand Up @@ -89,7 +92,7 @@ TEST(clang_parser, c_union)
TEST(clang_parser, c_enum)
{
BPFtrace bpftrace;
parse("enum E {NONE}; struct Foo { enum E e; }", bpftrace);
parse("enum E { NONE, SOME = 99, }; struct Foo { enum E e; }", bpftrace);

ASSERT_TRUE(bpftrace.structs.Has("struct Foo"));
auto foo = bpftrace.structs.Lookup("struct Foo").lock();
Expand All @@ -101,6 +104,92 @@ TEST(clang_parser, c_enum)
EXPECT_TRUE(foo->GetField("e").type.IsIntTy());
EXPECT_EQ(foo->GetField("e").type.GetSize(), 4U);
EXPECT_EQ(foo->GetField("e").offset, 0);

ASSERT_TRUE(bpftrace.enums_.contains("NONE"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["NONE"]), 0);
EXPECT_EQ(std::get<1>(bpftrace.enums_["NONE"]), "E");
ASSERT_TRUE(bpftrace.enums_.contains("SOME"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["SOME"]), 99);
EXPECT_EQ(std::get<1>(bpftrace.enums_["SOME"]), "E");

ASSERT_TRUE(bpftrace.enum_defs_.contains("E"));
ASSERT_TRUE(bpftrace.enum_defs_["E"].contains(0));
EXPECT_EQ(bpftrace.enum_defs_["E"][0], "NONE");
ASSERT_TRUE(bpftrace.enum_defs_["E"].contains(99));
EXPECT_EQ(bpftrace.enum_defs_["E"][99], "SOME");
}

TEST(clang_parser, c_enum_anonymous)
{
BPFtrace bpftrace;
parse(
"enum { ANON_A_VARIANT_1 = 0, ANON_A_VARIANT_2, ANON_A_CONFLICT = 99, }; "
"enum { ANON_B_VARIANT_1 = 0, ANON_B_CONFLICT = 99, }; ",
bpftrace);

ASSERT_EQ(bpftrace.enums_.size(), 5);
ASSERT_EQ(bpftrace.enum_defs_.size(), 2);

//
// Check enums_ contains first anonymous enum
//

// Check first variant present
ASSERT_TRUE(bpftrace.enums_.contains("ANON_A_VARIANT_1"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["ANON_A_VARIANT_1"]), 0);
auto anon_a_name = std::get<1>(bpftrace.enums_["ANON_A_VARIANT_1"]);
ASSERT_FALSE(anon_a_name.empty());

// Check second variant present
ASSERT_TRUE(bpftrace.enums_.contains("ANON_A_VARIANT_2"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["ANON_A_VARIANT_2"]), 1);
EXPECT_EQ(std::get<1>(bpftrace.enums_["ANON_A_CONFLICT"]), anon_a_name);

// Check conflict variant present
ASSERT_TRUE(bpftrace.enums_.contains("ANON_A_CONFLICT"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["ANON_A_CONFLICT"]), 99);
EXPECT_EQ(std::get<1>(bpftrace.enums_["ANON_A_CONFLICT"]), anon_a_name);

//
// Check enum_defs_ contains first anonymous enum, with ANON_A_CONFLICT
// value resolving correctly to the this enum and not the other.
//

ASSERT_TRUE(bpftrace.enum_defs_.contains(anon_a_name));
ASSERT_EQ(bpftrace.enum_defs_[anon_a_name].size(), 3);
ASSERT_TRUE(bpftrace.enum_defs_[anon_a_name].contains(0));
EXPECT_EQ(bpftrace.enum_defs_[anon_a_name][0], "ANON_A_VARIANT_1");
ASSERT_TRUE(bpftrace.enum_defs_[anon_a_name].contains(1));
EXPECT_EQ(bpftrace.enum_defs_[anon_a_name][1], "ANON_A_VARIANT_2");
ASSERT_TRUE(bpftrace.enum_defs_[anon_a_name].contains(99));
EXPECT_EQ(bpftrace.enum_defs_[anon_a_name][99], "ANON_A_CONFLICT");

//
// Check enums_ contains second anonymous enum
//

// Check first variant present
ASSERT_TRUE(bpftrace.enums_.contains("ANON_B_VARIANT_1"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["ANON_B_VARIANT_1"]), 0);
auto anon_b_name = std::get<1>(bpftrace.enums_["ANON_B_VARIANT_1"]);
ASSERT_FALSE(anon_b_name.empty());

// Check conflict variant present
ASSERT_TRUE(bpftrace.enums_.contains("ANON_B_CONFLICT"));
EXPECT_EQ(std::get<0>(bpftrace.enums_["ANON_B_CONFLICT"]), 99);
EXPECT_EQ(std::get<1>(bpftrace.enums_["ANON_B_CONFLICT"]), anon_b_name);

//
// Check enum_defs_ contains second anonymous enum, with ANON_B_CONFLICT
// value resolving correctly to the this enum and not the first.
//

ASSERT_TRUE(bpftrace.enum_defs_.contains(anon_b_name));
ASSERT_EQ(bpftrace.enum_defs_[anon_b_name].size(), 2);
ASSERT_TRUE(bpftrace.enum_defs_[anon_b_name].contains(0));
EXPECT_EQ(bpftrace.enum_defs_[anon_b_name][0], "ANON_B_VARIANT_1");
ASSERT_TRUE(bpftrace.enum_defs_[anon_b_name].contains(99));
EXPECT_EQ(bpftrace.enum_defs_[anon_b_name][99], "ANON_B_CONFLICT");
}

TEST(clang_parser, integer_ptr)
Expand Down