Index: lib/AST/ASTContext.cpp =================================================================== --- lib/AST/ASTContext.cpp +++ lib/AST/ASTContext.cpp @@ -5896,18 +5896,20 @@ ObjCInterfaceDecl *OI = T->castAs()->getInterface(); S += '{'; S += OI->getObjCRuntimeNameAsString(); - S += '='; - SmallVector Ivars; - DeepCollectObjCIvars(OI, true, Ivars); - for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { - const FieldDecl *Field = cast(Ivars[i]); - if (Field->isBitField()) - getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); - else - getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, - false, false, false, false, false, - EncodePointerToObjCTypedef, - NotEncodedT); + if (ExpandStructures) { + S += '='; + SmallVector Ivars; + DeepCollectObjCIvars(OI, true, Ivars); + for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { + const FieldDecl *Field = cast(Ivars[i]); + if (Field->isBitField()) + getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); + else + getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, + false, false, false, false, false, + EncodePointerToObjCTypedef, + NotEncodedT); + } } S += '}'; return; Index: test/CodeGenObjCXX/encode.mm =================================================================== --- test/CodeGenObjCXX/encode.mm +++ test/CodeGenObjCXX/encode.mm @@ -224,3 +224,24 @@ // CHECK: @_ZN7PR171421xE = constant [14 x i8] c"{E=^^?i^^?ii}\00" extern const char x[] = @encode(E); } + +// This test used to cause infinite recursion. +template +struct S { + typedef T Ty; + Ty *t; +}; + +@interface N +{ + S a; +} +@end + +@implementation N +@end + +const char *expand_struct() { + // CHECK: @{{.*}} = private unnamed_addr constant [16 x i8] c"{N={S=^{N}}}\00" + return @encode(N); +}