From: Duncan P. N. Exon Smith Date: Sat, 28 Jan 2017 23:22:40 +0000 (+0000) Subject: Modules: Return early in ModuleManager::addModule; NFC X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=65a6eb20b74183b299770c5a5c07d9159ce8a571;p=clang Modules: Return early in ModuleManager::addModule; NFC Invert the main branch in ModuleManager::addModule to return early and reduce indentation, and clean up a bunch of logic as a result. I split out a function called updateModuleImports to avoid triggering code duplication. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@293400 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Serialization/ModuleManager.cpp b/lib/Serialization/ModuleManager.cpp index 609eedf881..84cdd90182 100644 --- a/lib/Serialization/ModuleManager.cpp +++ b/lib/Serialization/ModuleManager.cpp @@ -63,6 +63,19 @@ static bool checkSignature(ASTFileSignature Signature, return true; } +static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, + SourceLocation ImportLoc) { + if (ImportedBy) { + MF.ImportedBy.insert(ImportedBy); + ImportedBy->Imports.insert(&MF); + } else { + if (!MF.DirectlyImported) + MF.ImportLoc = ImportLoc; + + MF.DirectlyImported = true; + } +} + ModuleManager::AddModuleResult ModuleManager::addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc, ModuleFile *ImportedBy, @@ -95,91 +108,79 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type, } // Check whether we already loaded this module, before - ModuleFile *ModuleEntry = Modules[Entry]; - std::unique_ptr NewModule; - if (!ModuleEntry) { - // Allocate a new module. - NewModule = llvm::make_unique(Type, Generation); - NewModule->Index = Chain.size(); - NewModule->FileName = FileName.str(); - NewModule->File = Entry; - NewModule->ImportLoc = ImportLoc; - NewModule->InputFilesValidationTimestamp = 0; - - if (NewModule->Kind == MK_ImplicitModule) { - std::string TimestampFilename = NewModule->getTimestampFilename(); - vfs::Status Status; - // A cached stat value would be fine as well. - if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) - NewModule->InputFilesValidationTimestamp = - llvm::sys::toTimeT(Status.getLastModificationTime()); - } + if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) { + // Check the stored signature. + if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) + return OutOfDate; - // Load the contents of the module - if (std::unique_ptr Buffer = lookupBuffer(FileName)) { - // The buffer was already provided for us. - NewModule->Buffer = std::move(Buffer); - } else { - // Open the AST file. - llvm::ErrorOr> Buf( - (std::error_code())); - if (FileName == "-") { - Buf = llvm::MemoryBuffer::getSTDIN(); - } else { - // Leave the FileEntry open so if it gets read again by another - // ModuleManager it must be the same underlying file. - // FIXME: Because FileManager::getFile() doesn't guarantee that it will - // give us an open file, this may not be 100% reliable. - Buf = FileMgr.getBufferForFile(NewModule->File, - /*IsVolatile=*/false, - /*ShouldClose=*/false); - } + Module = ModuleEntry; + updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc); + return AlreadyLoaded; + } - if (!Buf) { - ErrorStr = Buf.getError().message(); - return Missing; - } + // Allocate a new module. + auto NewModule = llvm::make_unique(Type, Generation); + NewModule->Index = Chain.size(); + NewModule->FileName = FileName.str(); + NewModule->File = Entry; + NewModule->ImportLoc = ImportLoc; + NewModule->InputFilesValidationTimestamp = 0; + + if (NewModule->Kind == MK_ImplicitModule) { + std::string TimestampFilename = NewModule->getTimestampFilename(); + vfs::Status Status; + // A cached stat value would be fine as well. + if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) + NewModule->InputFilesValidationTimestamp = + llvm::sys::toTimeT(Status.getLastModificationTime()); + } - NewModule->Buffer = std::move(*Buf); + // Load the contents of the module + if (std::unique_ptr Buffer = lookupBuffer(FileName)) { + // The buffer was already provided for us. + NewModule->Buffer = std::move(Buffer); + } else { + // Open the AST file. + llvm::ErrorOr> Buf((std::error_code())); + if (FileName == "-") { + Buf = llvm::MemoryBuffer::getSTDIN(); + } else { + // Leave the FileEntry open so if it gets read again by another + // ModuleManager it must be the same underlying file. + // FIXME: Because FileManager::getFile() doesn't guarantee that it will + // give us an open file, this may not be 100% reliable. + Buf = FileMgr.getBufferForFile(NewModule->File, + /*IsVolatile=*/false, + /*ShouldClose=*/false); } - // Initialize the stream. - NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer); - - // Read the signature eagerly now so that we can check it. - if (checkSignature(ReadSignature(NewModule->Data), ExpectedSignature, ErrorStr)) - return OutOfDate; + if (!Buf) { + ErrorStr = Buf.getError().message(); + return Missing; + } - // We're keeping this module. Update the map entry. - ModuleEntry = NewModule.get(); - } else if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) { - return OutOfDate; + NewModule->Buffer = std::move(*Buf); } - if (ImportedBy) { - ModuleEntry->ImportedBy.insert(ImportedBy); - ImportedBy->Imports.insert(ModuleEntry); - } else { - if (!ModuleEntry->DirectlyImported) - ModuleEntry->ImportLoc = ImportLoc; - - ModuleEntry->DirectlyImported = true; - } + // Initialize the stream. + NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer); - Module = ModuleEntry; + // Read the signature eagerly now so that we can check it. + if (checkSignature(ReadSignature(NewModule->Data), ExpectedSignature, + ErrorStr)) + return OutOfDate; - if (!NewModule) - return AlreadyLoaded; + // We're keeping this module. Store it everywhere. + Module = Modules[Entry] = NewModule.get(); - assert(!Modules[Entry] && "module loaded twice"); - Modules[Entry] = ModuleEntry; + updateModuleImports(*NewModule, ImportedBy, ImportLoc); - Chain.push_back(std::move(NewModule)); - if (!ModuleEntry->isModule()) - PCHChain.push_back(ModuleEntry); + if (!NewModule->isModule()) + PCHChain.push_back(NewModule.get()); if (!ImportedBy) - Roots.push_back(ModuleEntry); + Roots.push_back(NewModule.get()); + Chain.push_back(std::move(NewModule)); return NewlyLoaded; }