Skip to content

Commit 55d9654

Browse files
committed
Teach GetType and friends about typedef names.
1 parent 039bf04 commit 55d9654

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

lib/CppInterOp/CppInterOp.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,19 +1951,30 @@ static QualType findBuiltinType(llvm::StringRef typeName, ASTContext& Context) {
19511951
*/
19521952
return QualType();
19531953
}
1954+
static std::optional<QualType> GetTypeInternal(Decl* D) {
1955+
if (!D)
1956+
return {};
1957+
// Even though typedefs derive from TypeDecl, their getTypeForDecl()
1958+
// returns a nullptr.
1959+
if (const auto* TND = llvm::dyn_cast_or_null<TypedefNameDecl>(D))
1960+
return TND->getUnderlyingType();
1961+
1962+
if (auto* VD = dyn_cast<ValueDecl>(D))
1963+
return VD->getType();
1964+
1965+
if (const auto* TD = llvm::dyn_cast_or_null<TypeDecl>(D))
1966+
return QualType(TD->getTypeForDecl(), 0);
1967+
1968+
return {};
1969+
}
19541970
} // namespace
19551971

19561972
TCppType_t GetType(const std::string& name) {
19571973
QualType builtin = findBuiltinType(name, getASTContext());
19581974
if (!builtin.isNull())
19591975
return builtin.getAsOpaquePtr();
19601976

1961-
auto* D = (Decl*)GetNamed(name, /* Within= */ 0);
1962-
if (auto* TD = llvm::dyn_cast_or_null<TypeDecl>(D)) {
1963-
return QualType(TD->getTypeForDecl(), 0).getAsOpaquePtr();
1964-
}
1965-
1966-
return (TCppType_t)0;
1977+
return GetTypeFromScope((Decl*)GetNamed(name, /*Within=*/nullptr));
19671978
}
19681979

19691980
TCppType_t GetComplexType(TCppType_t type) {
@@ -1974,17 +1985,12 @@ TCppType_t GetComplexType(TCppType_t type) {
19741985

19751986
TCppType_t GetTypeFromScope(TCppScope_t klass) {
19761987
if (!klass)
1977-
return 0;
1978-
1979-
auto* D = (Decl*)klass;
1980-
1981-
if (auto* VD = dyn_cast<ValueDecl>(D))
1982-
return VD->getType().getAsOpaquePtr();
1988+
return nullptr;
19831989

1984-
if (auto* TD = dyn_cast<TypeDecl>(D))
1985-
return getASTContext().getTypeDeclType(TD).getAsOpaquePtr();
1990+
if (auto QT = GetTypeInternal((Decl*)klass))
1991+
return QT->getAsOpaquePtr();
19861992

1987-
return (TCppType_t) nullptr;
1993+
return nullptr;
19881994
}
19891995

19901996
// Internal functions that are not needed outside the library are

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ using namespace TestUtils;
2525
using namespace llvm;
2626
using namespace clang;
2727

28+
TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_GetTypeOfTypedef) {
29+
std::string code = R"(
30+
typedef int Type_t;
31+
)";
32+
33+
std::vector<Decl*> Decls;
34+
GetAllTopLevelDecls(code, Decls);
35+
auto Ty = Cpp::GetType("Type_t");
36+
EXPECT_TRUE(Ty);
37+
EXPECT_TRUE(Ty == Cpp::GetTypeFromScope(Decls[0]));
38+
EXPECT_FALSE(Cpp::GetTypeFromScope(nullptr));
39+
}
40+
2841
TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_IsEnumScope) {
2942
std::vector<Decl *> Decls;
3043
std::vector<Decl *> SubDecls;

0 commit comments

Comments
 (0)