diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -1363,12 +1363,7 @@ Expected ToDeclOrErr = import(T->getDecl()); if (!ToDeclOrErr) return ToDeclOrErr.takeError(); - ExpectedType ToUnderlyingTypeOrErr = import(T->desugar()); - if (!ToUnderlyingTypeOrErr) - return ToUnderlyingTypeOrErr.takeError(); - - return Importer.getToContext().getTypedefType(*ToDeclOrErr, - *ToUnderlyingTypeOrErr); + return Importer.getToContext().getTypeDeclType(*ToDeclOrErr); } ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -8474,6 +8474,45 @@ ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType())); } +TEST_P(ASTImporterOptionSpecificTestBase, ImportAnonymousStruct) { + // Tests importing of anonymous structures. This is a reduction of a real + // issue found in the wild into unittest case form. This crashes without + // the fix. Just don't crash when this unittest case is run. + Decl *ToTU = getToTuDecl( + R"( + typedef struct _XDisplay Display; + typedef struct { + int var; + } *_XPrivDisplay; + extern Display *xterm_dpy; + int function(int); + int internal(Display *dpy) { + return function(((_XPrivDisplay)dpy)->var ); + } + )", + Lang_C99); + auto *ToX = FirstDeclMatcher().match( + ToTU, functionDecl(hasName("internal"), isDefinition())); + ASSERT_TRUE(ToX); + + Decl *FromTU = getTuDecl( + R"( + typedef struct _XDisplay Display; + typedef struct { + int var; + } *_XPrivDisplay; + extern Display *xterm_dpy; + int function(int var) { + return ((_XPrivDisplay)xterm_dpy)->var; + } + )", + Lang_C99, "input1.c"); + auto *FromF = FirstDeclMatcher().match( + FromTU, functionDecl(hasName("function"), isDefinition())); + auto *ToF = Import(FromF, Lang_C99); + EXPECT_TRUE(ToF); +} + INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions);