Index: lib/Serialization/ASTReader.cpp
===================================================================
--- lib/Serialization/ASTReader.cpp
+++ lib/Serialization/ASTReader.cpp
@@ -1,4 +1,4 @@
-//===-- ASTReader.cpp - AST File Reader ----------------------------------===//
+//===-- ASTReader.cpp - AST File Reader -------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -64,7 +64,6 @@
 using namespace clang::serialization::reader;
 using llvm::BitstreamCursor;
 
-
 //===----------------------------------------------------------------------===//
 // ChainedASTReaderListener implementation
 //===----------------------------------------------------------------------===//
@@ -334,7 +333,7 @@
     MacroDefinitionsMap;
   typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
     DeclsMap;
-}
+} // anonymous namespace
 
 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
                                          DiagnosticsEngine &Diags,
@@ -656,13 +655,10 @@
   OwnsDeserializationListener = TakeOwnership;
 }
 
-
-
 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
   return serialization::ComputeHash(Sel);
 }
 
-
 std::pair<unsigned, unsigned>
 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
   using namespace llvm::support;
@@ -1241,7 +1237,7 @@
       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
                                         ID, BaseOffset + Record[0]);
-    SrcMgr::FileInfo &FileInfo =
+    auto &FileInfo =
           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
     FileInfo.NumCreatedFIDs = Record[5];
     if (Record[3])
@@ -1473,7 +1469,7 @@
         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
         PreprocessingRecord::PPEntityID PPID =
             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
-        MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
+        auto *PPDef = cast_or_null<MacroDefinitionRecord>(
             PPRec.getPreprocessedEntity(PPID));
         if (PPDef)
           PPRec.RegisterMacroDefinition(Macro, PPDef);
@@ -1727,7 +1723,7 @@
     // files.
     IdentifierInfo *getIdentifierInfo() const { return Found; }
   };
-}
+} // anonymous namespace
 
 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
   // Note that we are loading an identifier.
@@ -3347,7 +3343,7 @@
     D->Hidden = false;
 
     if (wasHidden && SemaObj) {
-      if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
+      if (auto *Method = dyn_cast<ObjCMethodDecl>(D)) {
         moveMethodToBackOfGlobalList(*SemaObj, Method);
       }
     }
@@ -3391,9 +3387,7 @@
     // Push any exported modules onto the stack to be marked as visible.
     SmallVector<Module *, 16> Exports;
     Mod->getExportedModules(Exports);
-    for (SmallVectorImpl<Module *>::iterator
-           I = Exports.begin(), E = Exports.end(); I != E; ++I) {
-      Module *Exported = *I;
+    for (auto *Exported : Exports) {
       if (Visited.insert(Exported).second)
         Stack.push_back(Exported);
     }
@@ -3515,10 +3509,8 @@
   // Here comes stuff that we only do once the entire chain is loaded.
 
   // Load the AST blocks of all of the modules that we loaded.
-  for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
-                                              MEnd = Loaded.end();
-       M != MEnd; ++M) {
-    ModuleFile &F = *M->Mod;
+  for (const auto &ImportedModule : Loaded) {
+    ModuleFile &F = *ImportedModule.Mod;
 
     // Read the AST block.
     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
@@ -3549,7 +3541,7 @@
     // Preload all the pending interesting identifiers by marking them out of
     // date.
     for (auto Offset : F.PreloadIdentifierOffsets) {
-      const unsigned char *Data = reinterpret_cast<const unsigned char *>(
+      auto *Data = reinterpret_cast<const unsigned char *>(
           F.IdentifierTableData + Offset);
 
       ASTIdentifierLookupTrait Trait(*this, F);
@@ -3575,20 +3567,18 @@
 
   // Setup the import locations and notify the module manager that we've
   // committed to these module files.
-  for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
-                                              MEnd = Loaded.end();
-       M != MEnd; ++M) {
-    ModuleFile &F = *M->Mod;
+  for (const auto &M : Loaded) {
+    ModuleFile &F = *M.Mod;
 
     ModuleMgr.moduleFileAccepted(&F);
 
     // Set the import location.
     F.DirectImportLoc = ImportLoc;
-    if (!M->ImportedBy)
-      F.ImportLoc = M->ImportLoc;
+    if (!M.ImportedBy)
+      F.ImportLoc = M.ImportLoc;
     else
-      F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
-                                       M->ImportLoc.getRawEncoding());
+      F.ImportLoc = ReadSourceLocation(*M.ImportedBy,
+                                       M.ImportLoc.getRawEncoding());
   }
 
   if (!Context.getLangOpts().CPlusPlus ||
@@ -3600,10 +3590,8 @@
     // For C++ modules, we don't need information on many identifiers (just
     // those that provide macros or are poisoned), so we mark all of
     // the interesting ones via PreloadIdentifierOffsets.
-    for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
-                                IdEnd = PP.getIdentifierTable().end();
-         Id != IdEnd; ++Id)
-      Id->second->setOutOfDate(true);
+    for (auto &Id : PP.getIdentifierTable())
+      Id.second->setOutOfDate(true);
   }
   
   // Resolve any unresolved module exports.
@@ -4184,7 +4172,7 @@
                                       SuggestedPredefines, ExistingLangOpts);
     }
   };
-}
+} // anonymous namespace
 
 bool ASTReader::readASTFileControlBlock(
     StringRef Filename, FileManager &FileMgr,
@@ -4757,8 +4745,7 @@
   // Include entries.
   for (unsigned N = Record[Idx++]; N; --N) {
     std::string Path = ReadString(Record, Idx);
-    frontend::IncludeDirGroup Group
-      = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
+    auto Group = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
     bool IsFramework = Record[Idx++];
     bool IgnoreSysRoot = Record[Idx++];
     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
@@ -4906,7 +4893,7 @@
     // Decode the identifier info and then check again; if the macro is
     // still defined and associated with the identifier,
     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
-    MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
+    auto *MD = new (PPRec) MacroDefinitionRecord(II, Range);
 
     if (DeserializationListener)
       DeserializationListener->MacroDefinitionRead(PPID, MD);
@@ -4922,9 +4909,8 @@
       File = PP.getFileManager().getFile(FullFileName);
     
     // FIXME: Stable encoding
-    InclusionDirective::InclusionKind Kind
-      = static_cast<InclusionDirective::InclusionKind>(Record[2]);
-    InclusionDirective *ID
+    auto Kind = static_cast<InclusionDirective::InclusionKind>(Record[2]);
+    auto *ID
       = new (PPRec) InclusionDirective(PPRec, Kind,
                                        StringRef(Blob.data(), Record[0]),
                                        Record[1], Record[3],
@@ -4984,7 +4970,7 @@
   }
 };
 
-}
+} // anonymous namespace
 
 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
                                                        bool EndsAfter) const {
@@ -5085,7 +5071,7 @@
       : FE(FE) { }
 
     bool operator()(ModuleFile &M) {
-      HeaderFileInfoLookupTable *Table
+      auto *Table
         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
       if (!Table)
         return false;
@@ -5101,7 +5087,7 @@
     
     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
   };
-}
+} // anonymous namespace
 
 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
   HeaderFileInfoVisitor Visitor(FE);
@@ -5115,8 +5101,8 @@
 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
   // FIXME: Make it work properly with modules.
   SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
-  for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
-    ModuleFile &F = *(*I);
+  for (const auto &I : ModuleMgr) {
+    ModuleFile &F = *I;
     unsigned Idx = 0;
     DiagStates.clear();
     assert(!Diag.DiagStates.empty());
@@ -5454,7 +5440,7 @@
     }
     QualType modifiedType = readType(*Loc.F, Record, Idx);
     QualType equivalentType = readType(*Loc.F, Record, Idx);
-    AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
+    auto kind = static_cast<AttributedType::Kind>(Record[2]);
     return Context.getAttributedType(kind, modifiedType, equivalentType);
   }
 
@@ -5641,8 +5627,7 @@
                                   SmallVectorImpl<QualType> &Exceptions,
                                   FunctionProtoType::ExceptionSpecInfo &ESI,
                                   const RecordData &Record, unsigned &Idx) {
-  ExceptionSpecificationType EST =
-      static_cast<ExceptionSpecificationType>(Record[Idx++]);
+  auto EST = static_cast<ExceptionSpecificationType>(Record[Idx++]);
   ESI.Type = EST;
   if (EST == EST_Dynamic) {
     for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
@@ -6082,19 +6067,15 @@
     case PREDEF_TYPE_AUTO_DEDUCT:
       T = Context.getAutoDeductType();
       break;
-
     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
       T = Context.getAutoRRefDeductType();
       break;
-
     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
       T = Context.ARCUnbridgedCastTy;
       break;
-
     case PREDEF_TYPE_BUILTIN_FN:
       T = Context.BuiltinFnTy;
       break;
-
     case PREDEF_TYPE_OMP_ARRAY_SECTION:
       T = Context.OMPArraySectionTy;
       break;
@@ -6325,7 +6306,7 @@
   unsigned Idx = 0;
   unsigned NumBases = Record[Idx++];
   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
-  CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
+  auto *Bases = new (Mem) CXXBaseSpecifier [NumBases];
   for (unsigned I = 0; I != NumBases; ++I)
     Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
   return Bases;
@@ -6589,7 +6570,7 @@
   }
 };
 
-}
+} // anonymous namespace
 
 void ASTReader::FindFileRegionDecls(FileID File,
                                     unsigned Offset, unsigned Length,
@@ -6651,7 +6632,7 @@
   // Load the list of declarations.
   SmallVector<NamedDecl *, 64> Decls;
   for (DeclID ID : It->second.Table.find(Name)) {
-    NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
+    auto *ND = cast<NamedDecl>(GetDecl(ID));
     if (ND->getDeclName() == Name)
       Decls.push_back(ND);
   }
@@ -6672,7 +6653,7 @@
   DeclsMap Decls;
 
   for (DeclID ID : It->second.Table.findAll()) {
-    NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
+    auto *ND = cast<NamedDecl>(GetDecl(ID));
     Decls[ND->getDeclName()].push_back(ND);
   }
 
@@ -6730,7 +6711,7 @@
 }
 
 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
-  if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
+  if (auto *ImplD = dyn_cast<ObjCImplDecl>(D))
     PassObjCImplDeclToConsumer(ImplD, Consumer);
   else
     Consumer->HandleInterestingDecl(DeclGroupRef(D));
@@ -6876,18 +6857,15 @@
                   GlobalPreprocessedEntityMap);
   
   llvm::errs() << "\n*** PCH/Modules Loaded:";
-  for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 
-                                       MEnd = ModuleMgr.end();
-       M != MEnd; ++M)
-    (*M)->dump();
+  for (const auto M : ModuleMgr)
+    M->dump();
 }
 
 /// Return the amount of memory used by memory buffers, breaking down
 /// by heap-backed versus mmap'ed memory.
 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
-  for (ModuleConstIterator I = ModuleMgr.begin(),
-      E = ModuleMgr.end(); I != E; ++I) {
-    if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
+  for (const auto I : ModuleMgr) {
+    if (llvm::MemoryBuffer *buf = I->Buffer.get()) {
       size_t bytes = buf->getBufferSize();
       switch (buf->getBufferKind()) {
         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
@@ -6908,7 +6886,7 @@
   // Makes sure any declarations that were deserialized "too early"
   // still get added to the identifier's declaration chains.
   for (uint64_t ID : PreloadedDeclIDs) {
-    NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
+    auto *D = cast<NamedDecl>(GetDecl(ID));
     pushExternalDeclIntoScope(D, D->getDeclName());
   }
   PreloadedDeclIDs.clear();
@@ -7012,7 +6990,7 @@
 
     StringRef Next() override;
   };
-}
+} // namespace clang
 
 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
   : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
@@ -7050,7 +7028,8 @@
   return new ASTIdentifierIterator(*this);
 }
 
-namespace clang { namespace serialization {
+namespace clang {
+namespace serialization {
   class ReadMethodPoolVisitor {
     ASTReader &Reader;
     Selector Sel;
@@ -7120,7 +7099,8 @@
     }
     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
   };
-} } // end namespace clang::serialization
+} // end namespace serialization
+} // end namespace clang
 
 /// \brief Add the given set of methods to the method list.
 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
@@ -7171,7 +7151,7 @@
   Namespaces.clear();
   
   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
-    if (NamespaceDecl *Namespace 
+    if (auto *Namespace 
                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
       Namespaces.push_back(Namespace);
   }
@@ -7180,7 +7160,7 @@
 void ASTReader::ReadUndefinedButUsed(
                         llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
-    NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
+    auto *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
     SourceLocation Loc =
         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
     Undefined.insert(std::make_pair(D, Loc));
@@ -7191,7 +7171,7 @@
     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
                                                      Exprs) {
   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
-    FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
+    auto *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
     uint64_t Count = DelayedDeleteExprs[Idx++];
     for (uint64_t C = 0; C < Count; ++C) {
       SourceLocation DeleteLoc =
@@ -7205,7 +7185,7 @@
 void ASTReader::ReadTentativeDefinitions(
                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
-    VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
+    auto *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
     if (Var)
       TentativeDefs.push_back(Var);
   }
@@ -7215,7 +7195,7 @@
 void ASTReader::ReadUnusedFileScopedDecls(
                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
-    DeclaratorDecl *D
+    auto *D
       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
     if (D)
       Decls.push_back(D);
@@ -7226,7 +7206,7 @@
 void ASTReader::ReadDelegatingConstructors(
                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
-    CXXConstructorDecl *D
+    auto *D
       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
     if (D)
       Decls.push_back(D);
@@ -7236,7 +7216,7 @@
 
 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
-    TypedefNameDecl *D
+    auto *D
       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
     if (D)
       Decls.push_back(D);
@@ -7248,7 +7228,7 @@
     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
        ++I) {
-    TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
+    auto *D = dyn_cast_or_null<TypedefNameDecl>(
         GetDecl(UnusedLocalTypedefNameCandidates[I]));
     if (D)
       Decls.insert(D);
@@ -7309,7 +7289,7 @@
 void ASTReader::ReadPendingInstantiations(
        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
-    ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
+    auto *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
     SourceLocation Loc
       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
 
@@ -7322,9 +7302,9 @@
     llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
        /* In loop */) {
-    FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
+    auto *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
 
-    LateParsedTemplate *LT = new LateParsedTemplate;
+    auto *LT = new LateParsedTemplate;
     LT->D = GetDecl(LateParsedTemplates[Idx++]);
 
     ModuleFile *F = getOwningModuleFile(LT->D);
@@ -7388,7 +7368,7 @@
       continue;
     }
 
-    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
+    auto *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
 
     // If we're simply supposed to record the declarations, do so now.
     if (Decls) {
@@ -7813,7 +7793,7 @@
     return TemplateArgument(ReadExpr(F));
   case TemplateArgument::Pack: {
     unsigned NumArgs = Record[Idx++];
-    TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
+    auto *Args = new (Context) TemplateArgument[NumArgs];
     for (unsigned I = 0; I != NumArgs; ++I)
       Args[I] = ReadTemplateArgument(F, Record, Idx);
     return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
@@ -7870,7 +7850,7 @@
                                 const RecordData &Record, unsigned &Idx) {
   bool isVirtual = static_cast<bool>(Record[Idx++]);
   bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
-  AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
+  auto AS = static_cast<AccessSpecifier>(Record[Idx++]);
   bool inheritConstructors = static_cast<bool>(Record[Idx++]);
   TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
   SourceRange Range = ReadSourceRange(F, Record, Idx);
@@ -8182,14 +8162,10 @@
 
 void ASTReader::ReadComments() {
   std::vector<RawComment *> Comments;
-  for (SmallVectorImpl<std::pair<BitstreamCursor,
-                                 serialization::ModuleFile *> >::iterator
-       I = CommentsCursors.begin(),
-       E = CommentsCursors.end();
-       I != E; ++I) {
+  for (auto &I : CommentsCursors) {
     Comments.clear();
-    BitstreamCursor &Cursor = I->first;
-    serialization::ModuleFile &F = *I->second;
+    BitstreamCursor &Cursor = I.first;
+    serialization::ModuleFile &F = *I.second;
     SavedStreamPosition SavedPosition(Cursor);
 
     RecordData Record;
@@ -8314,8 +8290,8 @@
     while (!PendingDeclContextInfos.empty()) {
       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
       PendingDeclContextInfos.pop_front();
-      DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
-      DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
+      auto *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
+      auto *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
     }
 
@@ -8337,8 +8313,8 @@
   // that all redeclarations point to the definitions. Note that this can only 
   // happen now, after the redeclaration chains have been fully wired.
   for (Decl *D : PendingDefinitions) {
-    if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
-      if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
+    if (auto *TD = dyn_cast<TagDecl>(D)) {
+      if (const auto *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
         // Make sure that the TagType points at the definition.
         const_cast<TagType*>(TagT)->decl = TD;
       }
@@ -8384,20 +8360,18 @@
   // this now (delayed) so that we can be sure that the declaration chains
   // have been fully wired up (hasBody relies on this).
   // FIXME: We shouldn't require complete redeclaration chains here.
-  for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
-                               PBEnd = PendingBodies.end();
-       PB != PBEnd; ++PB) {
-    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
+  for (const auto &PB : PendingBodies) {
+    if (auto *FD = dyn_cast<FunctionDecl>(PB.first)) {
       // FIXME: Check for =delete/=default?
       // FIXME: Complain about ODR violations here?
       if (!getContext().getLangOpts().Modules || !FD->hasBody())
-        FD->setLazyBody(PB->second);
+        FD->setLazyBody(PB.second);
       continue;
     }
 
-    ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
+    auto *MD = cast<ObjCMethodDecl>(PB.first);
     if (!getContext().getLangOpts().Modules || !MD->hasBody())
-      MD->setLazyBody(PB->second);
+      MD->setLazyBody(PB.second);
   }
   PendingBodies.clear();