diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-21 19:57:54 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-21 19:57:54 +0000 | 
| commit | e123fc8fd8677e4dc86f274cffd069e5d56f4a01 (patch) | |
| tree | 806fb806ca9ace304565efd09b494e81ac2aff6f /contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp | |
| parent | 54db30ce18663e6c2991958f3b5d18362e8e93c4 (diff) | |
| parent | 2298981669bf3bd63335a4be179bc0f96823a8f4 (diff) | |
Notes
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp')
| -rw-r--r-- | contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp | 150 | 
1 files changed, 75 insertions, 75 deletions
diff --git a/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp b/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp index f66674535423..cf0267549e75 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp @@ -1,9 +1,8 @@  //===--- CompilerInstance.cpp ---------------------------------------------===//  // -//                     The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception  //  //===----------------------------------------------------------------------===// @@ -14,7 +13,6 @@  #include "clang/Basic/CharInfo.h"  #include "clang/Basic/Diagnostic.h"  #include "clang/Basic/FileManager.h" -#include "clang/Basic/MemoryBufferCache.h"  #include "clang/Basic/SourceManager.h"  #include "clang/Basic/Stack.h"  #include "clang/Basic/TargetInfo.h" @@ -36,6 +34,7 @@  #include "clang/Sema/Sema.h"  #include "clang/Serialization/ASTReader.h"  #include "clang/Serialization/GlobalModuleIndex.h" +#include "clang/Serialization/InMemoryModuleCache.h"  #include "llvm/ADT/Statistic.h"  #include "llvm/Support/BuryPointer.h"  #include "llvm/Support/CrashRecoveryContext.h" @@ -47,6 +46,7 @@  #include "llvm/Support/Path.h"  #include "llvm/Support/Program.h"  #include "llvm/Support/Signals.h" +#include "llvm/Support/TimeProfiler.h"  #include "llvm/Support/Timer.h"  #include "llvm/Support/raw_ostream.h"  #include <sys/stat.h> @@ -58,15 +58,12 @@ using namespace clang;  CompilerInstance::CompilerInstance(      std::shared_ptr<PCHContainerOperations> PCHContainerOps, -    MemoryBufferCache *SharedPCMCache) -    : ModuleLoader(/* BuildingModule = */ SharedPCMCache), +    InMemoryModuleCache *SharedModuleCache) +    : ModuleLoader(/* BuildingModule = */ SharedModuleCache),        Invocation(new CompilerInvocation()), -      PCMCache(SharedPCMCache ? SharedPCMCache : new MemoryBufferCache), -      ThePCHContainerOperations(std::move(PCHContainerOps)) { -  // Don't allow this to invalidate buffers in use by others. -  if (SharedPCMCache) -    getPCMCache().finalizeCurrentBuffers(); -} +      ModuleCache(SharedModuleCache ? SharedModuleCache +                                    : new InMemoryModuleCache), +      ThePCHContainerOperations(std::move(PCHContainerOps)) {}  CompilerInstance::~CompilerInstance() {    assert(OutputFiles.empty() && "Still output files in flight?"); @@ -93,10 +90,6 @@ void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }  void CompilerInstance::setFileManager(FileManager *Value) {    FileMgr = Value; -  if (Value) -    VirtualFileSystem = Value->getVirtualFileSystem(); -  else -    VirtualFileSystem.reset();  }  void CompilerInstance::setSourceManager(SourceManager *Value) { @@ -137,7 +130,7 @@ IntrusiveRefCntPtr<ASTReader> CompilerInstance::getModuleManager() const {    return ModuleManager;  }  void CompilerInstance::setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader) { -  assert(PCMCache.get() == &Reader->getModuleManager().getPCMCache() && +  assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() &&           "Expected ASTReader to use the same PCM cache");    ModuleManager = std::move(Reader);  } @@ -177,7 +170,7 @@ static void collectIncludePCH(CompilerInstance &CI,    std::error_code EC;    SmallString<128> DirNative;    llvm::sys::path::native(PCHDir->getName(), DirNative); -  llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); +  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();    SimpleASTReaderListener Validator(CI.getPreprocessor());    for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;         Dir != DirEnd && !EC; Dir.increment(EC)) { @@ -239,9 +232,13 @@ static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,                                                          std::move(StreamOwner));    if (CodeGenOpts)      Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); -  assert(Diags.ownsClient()); -  Diags.setClient( -      new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger))); +  if (Diags.ownsClient()) { +    Diags.setClient( +        new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger))); +  } else { +    Diags.setClient( +        new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger))); +  }  }  static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, @@ -301,13 +298,14 @@ CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,  // File Manager -FileManager *CompilerInstance::createFileManager() { -  if (!hasVirtualFileSystem()) { -    IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = -        createVFSFromCompilerInvocation(getInvocation(), getDiagnostics()); -    setVirtualFileSystem(VFS); -  } -  FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem); +FileManager *CompilerInstance::createFileManager( +    IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { +  if (!VFS) +    VFS = FileMgr ? &FileMgr->getVirtualFileSystem() +                  : createVFSFromCompilerInvocation(getInvocation(), +                                                    getDiagnostics()); +  assert(VFS && "FileManager has no VFS?"); +  FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));    return FileMgr.get();  } @@ -379,11 +377,11 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {    HeaderSearch *HeaderInfo =        new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(),                         getDiagnostics(), getLangOpts(), &getTarget()); -  PP = std::make_shared<Preprocessor>( -      Invocation->getPreprocessorOptsPtr(), getDiagnostics(), getLangOpts(), -      getSourceManager(), getPCMCache(), *HeaderInfo, *this, -      /*IdentifierInfoLookup=*/nullptr, -      /*OwnsHeaderSearch=*/true, TUKind); +  PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(), +                                      getDiagnostics(), getLangOpts(), +                                      getSourceManager(), *HeaderInfo, *this, +                                      /*IdentifierInfoLookup=*/nullptr, +                                      /*OwnsHeaderSearch=*/true, TUKind);    getTarget().adjust(getLangOpts());    PP->Initialize(getTarget(), getAuxTarget()); @@ -417,8 +415,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {    // Handle generating dependencies, if requested.    const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();    if (!DepOpts.OutputFile.empty()) -    TheDependencyFileGenerator.reset( -        DependencyFileGenerator::CreateAndAttachToPreprocessor(*PP, DepOpts)); +    addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts));    if (!DepOpts.DOTOutputFile.empty())      AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,                               getHeaderSearchOpts().Sysroot); @@ -490,29 +487,26 @@ void CompilerInstance::createPCHExternalASTSource(    bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;    ModuleManager = createPCHExternalASTSource(        Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation, -      AllowPCHWithCompilerErrors, getPreprocessor(), getASTContext(), -      getPCHContainerReader(), -      getFrontendOpts().ModuleFileExtensions, -      TheDependencyFileGenerator.get(), -      DependencyCollectors, -      DeserializationListener, -      OwnDeserializationListener, Preamble, +      AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(), +      getASTContext(), getPCHContainerReader(), +      getFrontendOpts().ModuleFileExtensions, DependencyCollectors, +      DeserializationListener, OwnDeserializationListener, Preamble,        getFrontendOpts().UseGlobalModuleIndex);  }  IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(      StringRef Path, StringRef Sysroot, bool DisablePCHValidation, -    bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context, +    bool AllowPCHWithCompilerErrors, Preprocessor &PP, +    InMemoryModuleCache &ModuleCache, ASTContext &Context,      const PCHContainerReader &PCHContainerRdr,      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, -    DependencyFileGenerator *DependencyFile,      ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,      void *DeserializationListener, bool OwnDeserializationListener,      bool Preamble, bool UseGlobalModuleIndex) {    HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();    IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader( -      PP, &Context, PCHContainerRdr, Extensions, +      PP, ModuleCache, &Context, PCHContainerRdr, Extensions,        Sysroot.empty() ? "" : Sysroot.data(), DisablePCHValidation,        AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,        HSOpts.ModulesValidateSystemHeaders, UseGlobalModuleIndex)); @@ -525,8 +519,6 @@ IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(        static_cast<ASTDeserializationListener *>(DeserializationListener),        /*TakeOwnership=*/OwnDeserializationListener); -  if (DependencyFile) -    DependencyFile->AttachToASTReader(*Reader);    for (auto &Listener : DependencyCollectors)      Listener->attachToASTReader(*Reader); @@ -593,12 +585,6 @@ void CompilerInstance::createCodeCompletionConsumer() {      setCodeCompletionConsumer(nullptr);      return;    } - -  if (CompletionConsumer->isOutputBinary() && -      llvm::sys::ChangeStdoutToBinary()) { -    getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); -    setCodeCompletionConsumer(nullptr); -  }  }  void CompilerInstance::createFrontendTimer() { @@ -929,6 +915,9 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {    // Adjust target options based on codegen options.    getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts()); +  if (auto *Aux = getAuxTarget()) +    getTarget().setAuxTarget(Aux); +    // rewriter project will change target built-in bool type from its default.    if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)      getTarget().noSignedCharForObjCBool(); @@ -952,7 +941,9 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {        getSourceManager().clearIDTables();      if (Act.BeginSourceFile(*this, FIF)) { -      Act.Execute(); +      if (llvm::Error Err = Act.Execute()) { +        consumeError(std::move(Err)); // FIXME this drops errors on the floor. +      }        Act.EndSourceFile();      }    } @@ -1031,6 +1022,8 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,                        [](CompilerInstance &) {},                    llvm::function_ref<void(CompilerInstance &)> PostBuildStep =                        [](CompilerInstance &) {}) { +  llvm::TimeTraceScope TimeScope("Module Compile", ModuleName); +    // Construct a compiler invocation for creating this module.    auto Invocation =        std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation()); @@ -1092,11 +1085,11 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,           Invocation->getModuleHash() && "Module hash mismatch!");    // Construct a compiler instance that will be used to actually create the -  // module.  Since we're sharing a PCMCache, +  // module.  Since we're sharing an in-memory module cache,    // CompilerInstance::CompilerInstance is responsible for finalizing the    // buffers to prevent use-after-frees.    CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(), -                            &ImportingInstance.getPreprocessor().getPCMCache()); +                            &ImportingInstance.getModuleCache());    auto &Inv = *Invocation;    Instance.setInvocation(std::move(Invocation)); @@ -1104,8 +1097,6 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,                                     ImportingInstance.getDiagnosticClient()),                               /*ShouldOwnClient=*/true); -  Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem()); -    // Note that this module is part of the module build stack, so that we    // can detect cycles in the module graph.    Instance.setFileManager(&ImportingInstance.getFileManager()); @@ -1253,7 +1244,7 @@ static bool compileAndLoadModule(CompilerInstance &ImportingInstance,      llvm::LockFileManager Locked(ModuleFileName);      switch (Locked) {      case llvm::LockFileManager::LFS_Error: -      // PCMCache takes care of correctness and locks are only necessary for +      // ModuleCache takes care of correctness and locks are only necessary for        // performance. Fallback to building the module in case of any lock        // related errors.        Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure) @@ -1280,9 +1271,9 @@ static bool compileAndLoadModule(CompilerInstance &ImportingInstance,        case llvm::LockFileManager::Res_OwnerDied:          continue; // try again to get the lock.        case llvm::LockFileManager::Res_Timeout: -        // Since PCMCache takes care of correctness, we try waiting for another -        // process to complete the build so clang does not do it done twice. If -        // case of timeout, build it ourselves. +        // Since ModuleCache takes care of correctness, we try waiting for +        // another process to complete the build so clang does not do it done +        // twice. If case of timeout, build it ourselves.          Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)              << Module->Name;          // Clear the lock file so that future invocations can make progress. @@ -1480,14 +1471,13 @@ void CompilerInstance::createModuleManager() {                                                   "Reading modules",                                                   *FrontendTimerGroup);      ModuleManager = new ASTReader( -        getPreprocessor(), &getASTContext(), getPCHContainerReader(), -        getFrontendOpts().ModuleFileExtensions, +        getPreprocessor(), getModuleCache(), &getASTContext(), +        getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,          Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,          /*AllowASTWithCompilerErrors=*/false,          /*AllowConfigurationMismatch=*/false,          HSOpts.ModulesValidateSystemHeaders, -        getFrontendOpts().UseGlobalModuleIndex, -        std::move(ReadTimer)); +        getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer));      if (hasASTConsumer()) {        ModuleManager->setDeserializationListener(          getASTConsumer().GetASTDeserializationListener()); @@ -1500,8 +1490,6 @@ void CompilerInstance::createModuleManager() {      if (hasASTConsumer())        ModuleManager->StartTranslationUnit(&getASTConsumer()); -    if (TheDependencyFileGenerator) -      TheDependencyFileGenerator->AttachToASTReader(*ModuleManager);      for (auto &Listener : DependencyCollectors)        Listener->attachToASTReader(*ModuleManager);    } @@ -1710,6 +1698,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,        Timer.init("loading." + ModuleFileName, "Loading " + ModuleFileName,                   *FrontendTimerGroup);      llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr); +    llvm::TimeTraceScope TimeScope("Module Load", ModuleName);      // Try to load the module file. If we are not trying to load from the      // module cache, we don't know how to rebuild modules. @@ -2056,9 +2045,16 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(        hasPreprocessor()) {      llvm::sys::fs::create_directories(        getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); -    GlobalModuleIndex::writeIndex( -        getFileManager(), getPCHContainerReader(), -        getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); +    if (llvm::Error Err = GlobalModuleIndex::writeIndex( +            getFileManager(), getPCHContainerReader(), +            getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) { +      // FIXME this drops the error on the floor. This code is only used for +      // typo correction and drops more than just this one source of errors +      // (such as the directory creation failure above). It should handle the +      // error. +      consumeError(std::move(Err)); +      return nullptr; +    }      ModuleManager->resetForReload();      ModuleManager->loadGlobalIndex();      GlobalIndex = ModuleManager->getGlobalIndex(); @@ -2083,9 +2079,13 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(        }      }      if (RecreateIndex) { -      GlobalModuleIndex::writeIndex( -          getFileManager(), getPCHContainerReader(), -          getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); +      if (llvm::Error Err = GlobalModuleIndex::writeIndex( +              getFileManager(), getPCHContainerReader(), +              getPreprocessor().getHeaderSearchInfo().getModuleCachePath())) { +        // FIXME As above, this drops the error on the floor. +        consumeError(std::move(Err)); +        return nullptr; +      }        ModuleManager->resetForReload();        ModuleManager->loadGlobalIndex();        GlobalIndex = ModuleManager->getGlobalIndex();  | 
