mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
clang_generator: Correctly emit nested C-like structs
- Add more tests for this PiperOrigin-RevId: 435296715 Change-Id: I7b42dbc58dc054d2565af9ad22498d98416b7af7
This commit is contained in:
parent
b8579e4746
commit
92ccfeae67
@ -238,10 +238,12 @@ std::string GetSpelling(const clang::Decl* decl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (const auto* record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(decl)) {
|
if (const auto* record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(decl)) {
|
||||||
// For C++ classes/structs, only emit a forward declaration.
|
if (!record_decl->isCLike()) {
|
||||||
return absl::StrCat(PrintRecordTemplateArguments(record_decl),
|
// For C++ classes/structs, only emit a forward declaration.
|
||||||
record_decl->isClass() ? "class " : "struct ",
|
return absl::StrCat(PrintRecordTemplateArguments(record_decl),
|
||||||
ToStringView(record_decl->getName()));
|
record_decl->isClass() ? "class " : "struct ",
|
||||||
|
ToStringView(record_decl->getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return PrintDecl(decl);
|
return PrintDecl(decl);
|
||||||
}
|
}
|
||||||
@ -423,8 +425,7 @@ void Emitter::CollectType(clang::QualType qual) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ns_name = absl::StrCat(ns_path[0].empty() ? "" : " ",
|
ns_name = absl::StrJoin(ns_path, "::");
|
||||||
absl::StrJoin(ns_path, "::"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rendered_types_[ns_name].push_back(GetSpelling(decl));
|
rendered_types_[ns_name].push_back(GetSpelling(decl));
|
||||||
|
@ -93,10 +93,68 @@ TEST_F(EmitterTest, RelatedTypes) {
|
|||||||
}
|
}
|
||||||
EXPECT_THAT(ugly_types,
|
EXPECT_THAT(ugly_types,
|
||||||
ElementsAre("typedef enum { kRed, kGreen, kBlue } Color",
|
ElementsAre("typedef enum { kRed, kGreen, kBlue } Color",
|
||||||
"struct Channel", "struct ByValue",
|
"struct Channel {"
|
||||||
|
" Color color;"
|
||||||
|
" std::size_t width;"
|
||||||
|
" std::size_t height; }",
|
||||||
|
"struct ByValue { int value; }",
|
||||||
"typedef struct { int member; } MyStruct"));
|
"typedef struct { int member; } MyStruct"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(EmitterTest, NestedStruct) {
|
||||||
|
EmitterForTesting emitter;
|
||||||
|
RunFrontendAction(
|
||||||
|
R"(
|
||||||
|
struct A {
|
||||||
|
struct B {
|
||||||
|
int number;
|
||||||
|
};
|
||||||
|
B b;
|
||||||
|
int data;
|
||||||
|
};
|
||||||
|
extern "C" void Structize(A* s);
|
||||||
|
)",
|
||||||
|
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||||
|
|
||||||
|
std::vector<std::string> ugly_types;
|
||||||
|
for (const auto& type : emitter.rendered_types_[""]) {
|
||||||
|
ugly_types.push_back(Uglify(type));
|
||||||
|
}
|
||||||
|
EXPECT_THAT(ugly_types, ElementsAre("struct A {"
|
||||||
|
" struct B {"
|
||||||
|
" int number;"
|
||||||
|
" };"
|
||||||
|
" A::B b;"
|
||||||
|
" int data; "
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(EmitterTest, NestedAnonymousStruct) {
|
||||||
|
EmitterForTesting emitter;
|
||||||
|
RunFrontendAction(
|
||||||
|
R"(
|
||||||
|
struct A {
|
||||||
|
struct {
|
||||||
|
int number;
|
||||||
|
} b;
|
||||||
|
int data;
|
||||||
|
};
|
||||||
|
extern "C" void Structize(A* s);
|
||||||
|
)",
|
||||||
|
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||||
|
|
||||||
|
std::vector<std::string> ugly_types;
|
||||||
|
for (const auto& type : emitter.rendered_types_[""]) {
|
||||||
|
ugly_types.push_back(Uglify(type));
|
||||||
|
}
|
||||||
|
EXPECT_THAT(ugly_types, ElementsAre("struct A {"
|
||||||
|
" struct {"
|
||||||
|
" int number;"
|
||||||
|
" } b;"
|
||||||
|
" int data; "
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(IncludeGuard, CreatesRandomizedGuardForEmptyFilename) {
|
TEST(IncludeGuard, CreatesRandomizedGuardForEmptyFilename) {
|
||||||
// Copybara will transform the string. This is intentional.
|
// Copybara will transform the string. This is intentional.
|
||||||
constexpr absl::string_view kGeneratedHeaderPrefix =
|
constexpr absl::string_view kGeneratedHeaderPrefix =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user