Skip to content

Commit cd118e1

Browse files
cblichmanncopybara-github
authored andcommitted
clang_generator: Use regular PrintDecl() for array typedefs
Those were already correctly handled before. Added a test for this. PiperOrigin-RevId: 786185247 Change-Id: I3aa99fdbbdd6ed215a775ab081506bb98a07b492
1 parent 8a62f3b commit cd118e1

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

sandboxed_api/tools/clang_generator/emitter_base.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ std::string GetSpelling(const clang::Decl* decl) {
151151
if (clang::QualType canonical_type =
152152
typedef_name_decl->getUnderlyingType().getCanonicalType();
153153
IsPointerOrReference(canonical_type) &&
154-
// Need to skip function pointers/refs, as they are correctly emitted
155-
// already.
154+
// Skip function pointers/refs and array types. For arrays, we need to
155+
// check the final underlying pointee type.
156156
!canonical_type->isFunctionPointerType() &&
157-
!canonical_type->isFunctionReferenceType()) {
157+
!canonical_type->isFunctionReferenceType() &&
158+
!GetFinalPointeeType(canonical_type)->isArrayType()) {
158159
return absl::StrCat("typedef ", canonical_type.getAsString(),
159160
ToStringView(typedef_name_decl->getName()));
160161
}

sandboxed_api/tools/clang_generator/emitter_test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,25 @@ TEST_F(EmitterTest, TypedefTypeDependencies) {
493493
"struct _Image { StreamHandler stream; int size; }"));
494494
}
495495

496+
TEST_F(EmitterTest, TypedefArrays) {
497+
GeneratorOptions options;
498+
EmitterForTesting emitter(&options);
499+
EXPECT_THAT(RunFrontendAction(
500+
R"(typedef short JCOEF;
501+
typedef JCOEF JBLOCK[64];
502+
typedef JBLOCK *JBLOCKROW;
503+
typedef JBLOCKROW *JBLOCKARRAY;
504+
extern "C" void Array(JBLOCKARRAY);)",
505+
std::make_unique<GeneratorAction>(&emitter, &options)),
506+
IsOk());
507+
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
508+
509+
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
510+
ElementsAre("typedef short JCOEF", "typedef JCOEF JBLOCK[64]",
511+
"typedef JBLOCK *JBLOCKROW",
512+
"typedef JBLOCKROW *JBLOCKARRAY"));
513+
}
514+
496515
TEST_F(EmitterTest, OmitDependentTypes) {
497516
GeneratorOptions options;
498517
EmitterForTesting emitter(&options);

sandboxed_api/tools/clang_generator/types.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
#include "llvm/Support/Casting.h"
3535

3636
namespace sapi {
37+
38+
clang::QualType GetFinalPointeeType(clang::QualType qual) {
39+
clang::QualType pointee_type = qual;
40+
do {
41+
pointee_type = pointee_type->getPointeeType();
42+
} while (IsPointerOrReference(pointee_type));
43+
return pointee_type;
44+
}
45+
3746
namespace {
3847

3948
// Checks if a record declaration is a google::protobuf::Message.

sandboxed_api/tools/clang_generator/types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ inline bool IsPointerOrReference(clang::QualType qual) {
4949
#endif
5050
}
5151

52+
// Returns the final pointee type of a pointer or reference type.
53+
// For example, for `int**` it returns `int`.
54+
clang::QualType GetFinalPointeeType(clang::QualType qual);
55+
5256
// RenderedType objects are used to keep track of types that are going to be
5357
// emitted in the generated header.
5458
class RenderedType {

0 commit comments

Comments
 (0)