diff options
Diffstat (limited to 'clang/lib')
28 files changed, 201 insertions, 193 deletions
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp index 2282141..b48eed8 100644 --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -77,11 +77,11 @@ DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT, Output.append(Str.begin(), Str.end()); } -DiagnosticsEngine::DiagnosticsEngine(IntrusiveRefCntPtr<DiagnosticIDs> diags, - DiagnosticOptions &DiagOpts, - DiagnosticConsumer *client, - bool ShouldOwnClient) - : Diags(std::move(diags)), DiagOpts(DiagOpts) { +DiagnosticsEngine::DiagnosticsEngine( + IntrusiveRefCntPtr<DiagnosticIDs> diags, + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, DiagnosticConsumer *client, + bool ShouldOwnClient) + : Diags(std::move(diags)), DiagOpts(std::move(DiagOpts)) { setClient(client, ShouldOwnClient); ArgToStringFn = DummyArgToStringFn; diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 09e5c65..4028bbf 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -2391,11 +2391,11 @@ SourceManagerForFile::SourceManagerForFile(StringRef FileName, // in `Environment` so that `FileMgr` can out-live this function scope. FileMgr = std::make_unique<FileManager>(FileSystemOptions(), InMemoryFileSystem); - DiagOpts = std::make_unique<DiagnosticOptions>(); // This is passed to `SM` as reference, so the pointer has to be referenced // by `Environment` due to the same reason above. Diagnostics = std::make_unique<DiagnosticsEngine>( - IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), *DiagOpts); + IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), + new DiagnosticOptions); SourceMgr = std::make_unique<SourceManager>(*Diagnostics, *FileMgr); FileEntryRef FE = llvm::cantFail(FileMgr->getFileRef(FileName)); FileID ID = diff --git a/clang/lib/CrossTU/CrossTranslationUnit.cpp b/clang/lib/CrossTU/CrossTranslationUnit.cpp index 6d0f042..ef395f4 100644 --- a/clang/lib/CrossTU/CrossTranslationUnit.cpp +++ b/clang/lib/CrossTU/CrossTranslationUnit.cpp @@ -560,15 +560,15 @@ CrossTranslationUnitContext::ASTLoader::load(StringRef Identifier) { CrossTranslationUnitContext::LoadResultTy CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) { - auto DiagOpts = std::make_shared<DiagnosticOptions>(); + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); TextDiagnosticPrinter *DiagClient = - new TextDiagnosticPrinter(llvm::errs(), *DiagOpts); + new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); IntrusiveRefCntPtr<DiagnosticsEngine> Diags( - new DiagnosticsEngine(DiagID, *DiagOpts, DiagClient)); + new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient)); return ASTUnit::LoadFromASTFile( ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(), - ASTUnit::LoadEverything, DiagOpts, Diags, CI.getFileSystemOpts(), + ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts()); } @@ -603,17 +603,17 @@ CrossTranslationUnitContext::ASTLoader::loadFromSource( CommandLineArgs.begin(), [](auto &&CmdPart) { return CmdPart.c_str(); }); - auto DiagOpts = std::make_shared<DiagnosticOptions>(CI.getDiagnosticOpts()); + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts{&CI.getDiagnosticOpts()}; auto *DiagClient = new ForwardingDiagnosticConsumer{CI.getDiagnosticClient()}; IntrusiveRefCntPtr<DiagnosticIDs> DiagID{ CI.getDiagnostics().getDiagnosticIDs()}; IntrusiveRefCntPtr<DiagnosticsEngine> Diags( - new DiagnosticsEngine{DiagID, *DiagOpts, DiagClient}); + new DiagnosticsEngine{DiagID, &*DiagOpts, DiagClient}); - return ASTUnit::LoadFromCommandLine( - CommandLineArgs.begin(), (CommandLineArgs.end()), - CI.getPCHContainerOperations(), DiagOpts, Diags, - CI.getHeaderSearchOpts().ResourceDir); + return ASTUnit::LoadFromCommandLine(CommandLineArgs.begin(), + (CommandLineArgs.end()), + CI.getPCHContainerOperations(), Diags, + CI.getHeaderSearchOpts().ResourceDir); } llvm::Expected<InvocationListTy> diff --git a/clang/lib/Frontend/ASTMerge.cpp b/clang/lib/Frontend/ASTMerge.cpp index a4ce883..b6b0644 100644 --- a/clang/lib/Frontend/ASTMerge.cpp +++ b/clang/lib/Frontend/ASTMerge.cpp @@ -41,13 +41,14 @@ void ASTMergeAction::ExecuteAction() { auto SharedState = std::make_shared<ASTImporterSharedState>( *CI.getASTContext().getTranslationUnitDecl()); for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { - IntrusiveRefCntPtr<DiagnosticsEngine> Diags(new DiagnosticsEngine( - DiagIDs, CI.getDiagnosticOpts(), - new ForwardingDiagnosticConsumer(*CI.getDiagnostics().getClient()), - /*ShouldOwnClient=*/true)); + IntrusiveRefCntPtr<DiagnosticsEngine> + Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(), + new ForwardingDiagnosticConsumer( + *CI.getDiagnostics().getClient()), + /*ShouldOwnClient=*/true)); std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile( - ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, - nullptr, Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts()); + ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags, + CI.getFileSystemOpts(), CI.getHeaderSearchOpts()); if (!Unit) continue; diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 457043c..5a79fe0 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -803,8 +803,7 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags, std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile( StringRef Filename, const PCHContainerReader &PCHContainerRdr, - WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts, - IntrusiveRefCntPtr<DiagnosticsEngine> Diags, + WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts, bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors, @@ -824,7 +823,6 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile( : std::make_unique<LangOptions>(); AST->OnlyLocalDecls = OnlyLocalDecls; AST->CaptureDiagnostics = CaptureDiagnostics; - AST->DiagOpts = DiagOpts; AST->Diagnostics = Diags; AST->FileMgr = new FileManager(FileSystemOpts, VFS); AST->UserFilesAreVolatile = UserFilesAreVolatile; @@ -1536,7 +1534,6 @@ StringRef ASTUnit::getASTFileName() const { std::unique_ptr<ASTUnit> ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, - std::shared_ptr<DiagnosticOptions> DiagOpts, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, CaptureDiagsKind CaptureDiagnostics, bool UserFilesAreVolatile) { @@ -1544,7 +1541,6 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, ConfigureDiags(Diags, *AST, CaptureDiagnostics); IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = createVFSFromCompilerInvocation(*CI, *Diags); - AST->DiagOpts = DiagOpts; AST->Diagnostics = Diags; AST->FileSystemOpts = CI->getFileSystemOpts(); AST->Invocation = std::move(CI); @@ -1560,7 +1556,6 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( std::shared_ptr<CompilerInvocation> CI, std::shared_ptr<PCHContainerOperations> PCHContainerOps, - std::shared_ptr<DiagnosticOptions> DiagOpts, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action, ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath, bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics, @@ -1572,8 +1567,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( ASTUnit *AST = Unit; if (!AST) { // Create the AST unit. - OwnAST = - create(CI, DiagOpts, Diags, CaptureDiagnostics, UserFilesAreVolatile); + OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile); AST = OwnAST.get(); if (!AST) return nullptr; @@ -1735,7 +1729,6 @@ bool ASTUnit::LoadFromCompilerInvocation( std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation( std::shared_ptr<CompilerInvocation> CI, std::shared_ptr<PCHContainerOperations> PCHContainerOps, - std::shared_ptr<DiagnosticOptions> DiagOpts, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr, bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics, unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, @@ -1744,7 +1737,6 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation( // Create the AST unit. std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); ConfigureDiags(Diags, *AST, CaptureDiagnostics); - AST->DiagOpts = DiagOpts; AST->Diagnostics = Diags; AST->OnlyLocalDecls = OnlyLocalDecls; AST->CaptureDiagnostics = CaptureDiagnostics; @@ -1774,7 +1766,6 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation( std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine( const char **ArgBegin, const char **ArgEnd, std::shared_ptr<PCHContainerOperations> PCHContainerOps, - std::shared_ptr<DiagnosticOptions> DiagOpts, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath, bool StorePreamblesInMemory, StringRef PreambleStoragePath, bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics, @@ -1837,7 +1828,6 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine( AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); AST->StoredDiagnostics.swap(StoredDiagnostics); ConfigureDiags(Diags, *AST, CaptureDiagnostics); - AST->DiagOpts = DiagOpts; AST->Diagnostics = Diags; AST->FileSystemOpts = CI->getFileSystemOpts(); VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS); diff --git a/clang/lib/Frontend/ChainedIncludesSource.cpp b/clang/lib/Frontend/ChainedIncludesSource.cpp index f9a398d..95b0ed2 100644 --- a/clang/lib/Frontend/ChainedIncludesSource.cpp +++ b/clang/lib/Frontend/ChainedIncludesSource.cpp @@ -117,10 +117,10 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource( CInvok->getFrontendOpts().Inputs.push_back(InputFile); TextDiagnosticPrinter *DiagClient = - new TextDiagnosticPrinter(llvm::errs(), CI.getDiagnosticOpts()); + new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions()); IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); IntrusiveRefCntPtr<DiagnosticsEngine> Diags( - new DiagnosticsEngine(DiagID, CI.getDiagnosticOpts(), DiagClient)); + new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient)); auto Clang = std::make_unique<CompilerInstance>( std::move(CInvok), CI.getPCHContainerOperations()); diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index cc39049..503d364 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -280,20 +280,20 @@ static void collectVFSEntries(CompilerInstance &CI, } // Diagnostics -static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts, +static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, const CodeGenOptions *CodeGenOpts, DiagnosticsEngine &Diags) { std::error_code EC; std::unique_ptr<raw_ostream> StreamOwner; raw_ostream *OS = &llvm::errs(); - if (DiagOpts.DiagnosticLogFile != "-") { + if (DiagOpts->DiagnosticLogFile != "-") { // Create the output stream. auto FileOS = std::make_unique<llvm::raw_fd_ostream>( - DiagOpts.DiagnosticLogFile, EC, + DiagOpts->DiagnosticLogFile, EC, llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF); if (EC) { Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) - << DiagOpts.DiagnosticLogFile << EC.message(); + << DiagOpts->DiagnosticLogFile << EC.message(); } else { FileOS->SetUnbuffered(); OS = FileOS.get(); @@ -315,7 +315,7 @@ static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts, } } -static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts, +static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, DiagnosticsEngine &Diags, StringRef OutputFile) { auto SerializedConsumer = @@ -333,12 +333,12 @@ static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts, void CompilerInstance::createDiagnostics(llvm::vfs::FileSystem &VFS, DiagnosticConsumer *Client, bool ShouldOwnClient) { - Diagnostics = createDiagnostics(VFS, getDiagnosticOpts(), Client, + Diagnostics = createDiagnostics(VFS, &getDiagnosticOpts(), Client, ShouldOwnClient, &getCodeGenOpts()); } IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics( - llvm::vfs::FileSystem &VFS, DiagnosticOptions &Opts, + llvm::vfs::FileSystem &VFS, DiagnosticOptions *Opts, DiagnosticConsumer *Client, bool ShouldOwnClient, const CodeGenOptions *CodeGenOpts) { IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); @@ -349,24 +349,25 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics( // implementing -verify. if (Client) { Diags->setClient(Client, ShouldOwnClient); - } else if (Opts.getFormat() == DiagnosticOptions::SARIF) { + } else if (Opts->getFormat() == DiagnosticOptions::SARIF) { Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts)); } else Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); // Chain in -verify checker, if requested. - if (Opts.VerifyDiagnostics) + if (Opts->VerifyDiagnostics) Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); // Chain in -diagnostic-log-file dumper, if requested. - if (!Opts.DiagnosticLogFile.empty()) + if (!Opts->DiagnosticLogFile.empty()) SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); - if (!Opts.DiagnosticSerializationFile.empty()) - SetupSerializedDiagnostics(Opts, *Diags, Opts.DiagnosticSerializationFile); + if (!Opts->DiagnosticSerializationFile.empty()) + SetupSerializedDiagnostics(Opts, *Diags, + Opts->DiagnosticSerializationFile); // Configure our handling of diagnostics. - ProcessWarningOptions(*Diags, Opts, VFS); + ProcessWarningOptions(*Diags, *Opts, VFS); return Diags; } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 9c33910..3c23073 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -125,14 +125,21 @@ static Expected<std::optional<uint32_t>> parseToleranceOption(StringRef Arg) { // Initialization. //===----------------------------------------------------------------------===// +namespace { template <class T> std::shared_ptr<T> make_shared_copy(const T &X) { return std::make_shared<T>(X); } +template <class T> +llvm::IntrusiveRefCntPtr<T> makeIntrusiveRefCntCopy(const T &X) { + return llvm::makeIntrusiveRefCnt<T>(X); +} +} // namespace + CompilerInvocationBase::CompilerInvocationBase() : LangOpts(std::make_shared<LangOptions>()), TargetOpts(std::make_shared<TargetOptions>()), - DiagnosticOpts(std::make_shared<DiagnosticOptions>()), + DiagnosticOpts(llvm::makeIntrusiveRefCnt<DiagnosticOptions>()), HSOpts(std::make_shared<HeaderSearchOptions>()), PPOpts(std::make_shared<PreprocessorOptions>()), AnalyzerOpts(std::make_shared<AnalyzerOptions>()), @@ -149,7 +156,7 @@ CompilerInvocationBase::deep_copy_assign(const CompilerInvocationBase &X) { if (this != &X) { LangOpts = make_shared_copy(X.getLangOpts()); TargetOpts = make_shared_copy(X.getTargetOpts()); - DiagnosticOpts = make_shared_copy(X.getDiagnosticOpts()); + DiagnosticOpts = makeIntrusiveRefCntCopy(X.getDiagnosticOpts()); HSOpts = make_shared_copy(X.getHeaderSearchOpts()); PPOpts = make_shared_copy(X.getPreprocessorOpts()); AnalyzerOpts = make_shared_copy(X.getAnalyzerOpts()); @@ -195,6 +202,7 @@ CompilerInvocation::operator=(const CowCompilerInvocation &X) { return *this; } +namespace { template <typename T> T &ensureOwned(std::shared_ptr<T> &Storage) { if (Storage.use_count() > 1) @@ -202,6 +210,14 @@ T &ensureOwned(std::shared_ptr<T> &Storage) { return *Storage; } +template <typename T> +T &ensureOwned(llvm::IntrusiveRefCntPtr<T> &Storage) { + if (Storage.useCount() > 1) + Storage = llvm::makeIntrusiveRefCnt<T>(*Storage); + return *Storage; +} +} // namespace + LangOptions &CowCompilerInvocation::getMutLangOpts() { return ensureOwned(LangOpts); } @@ -828,8 +844,7 @@ static bool RoundTrip(ParseFn Parse, GenerateFn Generate, }; // Setup a dummy DiagnosticsEngine. - DiagnosticOptions DummyDiagOpts; - DiagnosticsEngine DummyDiags(new DiagnosticIDs(), DummyDiagOpts); + DiagnosticsEngine DummyDiags(new DiagnosticIDs(), new DiagnosticOptions()); DummyDiags.setClient(new TextDiagnosticBuffer()); // Run the first parse on the original arguments with the dummy invocation and @@ -2648,11 +2663,9 @@ clang::CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv) { bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, DiagnosticsEngine *Diags, bool DefaultDiagColor) { - std::optional<DiagnosticOptions> IgnoringDiagOpts; std::optional<DiagnosticsEngine> IgnoringDiags; if (!Diags) { - IgnoringDiagOpts.emplace(); - IgnoringDiags.emplace(new DiagnosticIDs(), *IgnoringDiagOpts, + IgnoringDiags.emplace(new DiagnosticIDs(), new DiagnosticOptions(), new IgnoringDiagConsumer()); Diags = &*IgnoringDiags; } diff --git a/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp b/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp index 99212b8..d0b855f 100644 --- a/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp +++ b/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp @@ -31,15 +31,11 @@ std::unique_ptr<CompilerInvocation> clang::createInvocation(ArrayRef<const char *> ArgList, CreateInvocationOptions Opts) { assert(!ArgList.empty()); - std::optional<DiagnosticOptions> LocalDiagOpts; - IntrusiveRefCntPtr<DiagnosticsEngine> Diags; - if (Opts.Diags) { - Diags = std::move(Opts.Diags); - } else { - LocalDiagOpts.emplace(); - Diags = CompilerInstance::createDiagnostics( - Opts.VFS ? *Opts.VFS : *llvm::vfs::getRealFileSystem(), *LocalDiagOpts); - } + auto Diags = Opts.Diags + ? std::move(Opts.Diags) + : CompilerInstance::createDiagnostics( + Opts.VFS ? *Opts.VFS : *llvm::vfs::getRealFileSystem(), + new DiagnosticOptions); SmallVector<const char *, 16> Args(ArgList); diff --git a/clang/lib/Frontend/DiagnosticRenderer.cpp b/clang/lib/Frontend/DiagnosticRenderer.cpp index 1396b2b..3b120ab 100644 --- a/clang/lib/Frontend/DiagnosticRenderer.cpp +++ b/clang/lib/Frontend/DiagnosticRenderer.cpp @@ -30,7 +30,7 @@ using namespace clang; DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts, - DiagnosticOptions &DiagOpts) + DiagnosticOptions *DiagOpts) : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {} DiagnosticRenderer::~DiagnosticRenderer() = default; @@ -115,7 +115,7 @@ void DiagnosticRenderer::emitDiagnostic(FullSourceLoc Loc, // Find the ultimate expansion location for the diagnostic. Loc = Loc.getFileLoc(); - PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts.ShowPresumedLoc); + PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc); // First, if this diagnostic is not in the main file, print out the // "included from" lines. @@ -172,7 +172,7 @@ void DiagnosticRenderer::emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc, LastIncludeLoc = IncludeLoc; - if (!DiagOpts.ShowNoteIncludeStack && Level == DiagnosticsEngine::Note) + if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note) return; if (IncludeLoc.isValid()) @@ -191,7 +191,7 @@ void DiagnosticRenderer::emitIncludeStackRecursively(FullSourceLoc Loc) { return; } - PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts.ShowPresumedLoc); + PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc); if (PLoc.isInvalid()) return; @@ -232,7 +232,7 @@ void DiagnosticRenderer::emitImportStackRecursively(FullSourceLoc Loc, return; } - PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts.ShowPresumedLoc); + PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc); // Emit the other import frames first. std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc(); @@ -247,8 +247,9 @@ void DiagnosticRenderer::emitImportStackRecursively(FullSourceLoc Loc, void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) { ModuleBuildStack Stack = SM.getModuleBuildStack(); for (const auto &I : Stack) { - emitBuildingModuleLocation( - I.second, I.second.getPresumedLoc(DiagOpts.ShowPresumedLoc), I.first); + emitBuildingModuleLocation(I.second, I.second.getPresumedLoc( + DiagOpts->ShowPresumedLoc), + I.first); } } @@ -538,7 +539,7 @@ void DiagnosticRenderer::emitMacroExpansions(FullSourceLoc Loc, LocationStack.begin() + IgnoredEnd); unsigned MacroDepth = LocationStack.size(); - unsigned MacroLimit = DiagOpts.MacroBacktraceLimit; + unsigned MacroLimit = DiagOpts->MacroBacktraceLimit; if (MacroDepth <= MacroLimit || MacroLimit == 0) { for (auto I = LocationStack.rbegin(), E = LocationStack.rend(); I != E; ++I) diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index af9d18a..54a2e3e 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -766,8 +766,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); // The AST unit populates its own diagnostics engine rather than ours. - IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(new DiagnosticsEngine( - Diags->getDiagnosticIDs(), Diags->getDiagnosticOptions())); + IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags( + new DiagnosticsEngine(Diags->getDiagnosticIDs(), + &Diags->getDiagnosticOptions())); ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false); // FIXME: What if the input is a memory buffer? @@ -775,7 +776,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly, - nullptr, ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts()); + ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts()); if (!AST) return false; @@ -841,9 +842,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, StringRef InputFile = Input.getFile(); std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( - InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, nullptr, - Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts(), - &CI.getLangOpts()); + InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags, + CI.getFileSystemOpts(), CI.getHeaderSearchOpts(), &CI.getLangOpts()); if (!AST) return false; diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp index d14d091..8c75e1a 100644 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -685,21 +685,21 @@ namespace { return false; } - bool ReadDiagnosticOptions(DiagnosticOptions &DiagOpts, + bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, StringRef ModuleFilename, bool Complain) override { Out.indent(2) << "Diagnostic options:\n"; -#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts.Name, #Name); -#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ - Out.indent(4) << #Name << ": " << DiagOpts.get##Name() << "\n"; -#define VALUE_DIAGOPT(Name, Bits, Default) \ - Out.indent(4) << #Name << ": " << DiagOpts.Name << "\n"; +#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); +#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ + Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; +#define VALUE_DIAGOPT(Name, Bits, Default) \ + Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; #include "clang/Basic/DiagnosticOptions.def" Out.indent(4) << "Diagnostic flags:\n"; - for (const std::string &Warning : DiagOpts.Warnings) + for (const std::string &Warning : DiagOpts->Warnings) Out.indent(6) << "-W" << Warning << "\n"; - for (const std::string &Remark : DiagOpts.Remarks) + for (const std::string &Remark : DiagOpts->Remarks) Out.indent(6) << "-R" << Remark << "\n"; return false; diff --git a/clang/lib/Frontend/LogDiagnosticPrinter.cpp b/clang/lib/Frontend/LogDiagnosticPrinter.cpp index 2d18893..4e963af 100644 --- a/clang/lib/Frontend/LogDiagnosticPrinter.cpp +++ b/clang/lib/Frontend/LogDiagnosticPrinter.cpp @@ -18,10 +18,10 @@ using namespace clang; using namespace markup; LogDiagnosticPrinter::LogDiagnosticPrinter( - raw_ostream &os, DiagnosticOptions &DiagOpts, + raw_ostream &os, DiagnosticOptions *diags, std::unique_ptr<raw_ostream> StreamOwner) : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr), - DiagOpts(DiagOpts) {} + DiagOpts(diags) {} static StringRef getLevelName(DiagnosticsEngine::Level Level) { switch (Level) { diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp b/clang/lib/Frontend/SARIFDiagnostic.cpp index e2aec7f6..4e36153 100644 --- a/clang/lib/Frontend/SARIFDiagnostic.cpp +++ b/clang/lib/Frontend/SARIFDiagnostic.cpp @@ -31,7 +31,7 @@ namespace clang { SARIFDiagnostic::SARIFDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, - DiagnosticOptions &DiagOpts, + DiagnosticOptions *DiagOpts, SarifDocumentWriter *Writer) : DiagnosticRenderer(LangOpts, DiagOpts), Writer(Writer) {} @@ -163,7 +163,7 @@ SARIFDiagnostic::addDiagnosticLevelToRule(SarifRule Rule, llvm::StringRef SARIFDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { - if (DiagOpts.AbsolutePath) { + if (DiagOpts->AbsolutePath) { auto File = SM.getFileManager().getOptionalFileRef(Filename); if (File) { // We want to print a simplified absolute path, i. e. without "dots". diff --git a/clang/lib/Frontend/SARIFDiagnosticPrinter.cpp b/clang/lib/Frontend/SARIFDiagnosticPrinter.cpp index 23fbc3e..73928d1 100644 --- a/clang/lib/Frontend/SARIFDiagnosticPrinter.cpp +++ b/clang/lib/Frontend/SARIFDiagnosticPrinter.cpp @@ -26,15 +26,15 @@ namespace clang { SARIFDiagnosticPrinter::SARIFDiagnosticPrinter(raw_ostream &OS, - DiagnosticOptions &DiagOpts) - : OS(OS), DiagOpts(DiagOpts) {} + DiagnosticOptions *Diags) + : OS(OS), DiagOpts(Diags) {} void SARIFDiagnosticPrinter::BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) { // Build the SARIFDiagnostic utility. assert(hasSarifWriter() && "Writer not set!"); assert(!SARIFDiag && "SARIFDiagnostic already set."); - SARIFDiag = std::make_unique<SARIFDiagnostic>(OS, LO, DiagOpts, &*Writer); + SARIFDiag = std::make_unique<SARIFDiagnostic>(OS, LO, &*DiagOpts, &*Writer); // Initialize the SARIF object. Writer->createRun("clang", Prefix); } @@ -72,6 +72,7 @@ void SARIFDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, } // Assert that the rest of our infrastructure is setup properly. + assert(DiagOpts && "Unexpected diagnostic without options set"); assert(Info.hasSourceManager() && "Unexpected diagnostic with no source manager"); diff --git a/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp b/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp index ee49cdd..02aa3e8 100644 --- a/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp +++ b/clang/lib/Frontend/SerializedDiagnosticPrinter.cpp @@ -57,8 +57,8 @@ class SDiagsRenderer : public DiagnosticNoteRenderer { SDiagsWriter &Writer; public: SDiagsRenderer(SDiagsWriter &Writer, const LangOptions &LangOpts, - DiagnosticOptions &DiagOpts) - : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {} + DiagnosticOptions *DiagOpts) + : DiagnosticNoteRenderer(LangOpts, DiagOpts), Writer(Writer) {} ~SDiagsRenderer() override {} @@ -140,7 +140,7 @@ class SDiagsWriter : public DiagnosticConsumer { State(std::move(State)) {} public: - SDiagsWriter(StringRef File, DiagnosticOptions &Diags, bool MergeChildRecords) + SDiagsWriter(StringRef File, DiagnosticOptions *Diags, bool MergeChildRecords) : LangOpts(nullptr), OriginalInstance(true), MergeChildRecords(MergeChildRecords), State(std::make_shared<SharedState>(File, Diags)) { @@ -242,12 +242,12 @@ private: /// State that is shared among the various clones of this diagnostic /// consumer. struct SharedState { - SharedState(StringRef File, DiagnosticOptions &DiagOpts) - : DiagOpts(DiagOpts), Stream(Buffer), OutputFile(File.str()), + SharedState(StringRef File, DiagnosticOptions *Diags) + : DiagOpts(Diags), Stream(Buffer), OutputFile(File.str()), EmittedAnyDiagBlocks(false) {} /// Diagnostic options. - DiagnosticOptions DiagOpts; + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; /// The byte buffer for the serialized content. SmallString<1024> Buffer; @@ -295,11 +295,9 @@ private: namespace clang { namespace serialized_diags { -std::unique_ptr<DiagnosticConsumer> create(StringRef OutputFile, - DiagnosticOptions &DiagOpts, - bool MergeChildRecords) { - return std::make_unique<SDiagsWriter>(OutputFile, DiagOpts, - MergeChildRecords); +std::unique_ptr<DiagnosticConsumer> +create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords) { + return std::make_unique<SDiagsWriter>(OutputFile, Diags, MergeChildRecords); } } // end namespace serialized_diags @@ -619,7 +617,7 @@ void SDiagsWriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, assert(Info.hasSourceManager() && LangOpts && "Unexpected diagnostic with valid location outside of a source file"); - SDiagsRenderer Renderer(*this, *LangOpts, State->DiagOpts); + SDiagsRenderer Renderer(*this, *LangOpts, &*State->DiagOpts); Renderer.emitDiagnostic( FullSourceLoc(Info.getLocation(), Info.getSourceManager()), DiagLevel, State->diagBuf, Info.getRanges(), Info.getFixItHints(), &Info); @@ -757,9 +755,10 @@ DiagnosticsEngine *SDiagsWriter::getMetaDiags() { // normally not be used. if (!State->MetaDiagnostics) { IntrusiveRefCntPtr<DiagnosticIDs> IDs(new DiagnosticIDs()); - auto Client = new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts); - State->MetaDiagnostics = - std::make_unique<DiagnosticsEngine>(IDs, State->DiagOpts, Client); + auto Client = + new TextDiagnosticPrinter(llvm::errs(), State->DiagOpts.get()); + State->MetaDiagnostics = std::make_unique<DiagnosticsEngine>( + IDs, State->DiagOpts.get(), Client); } return State->MetaDiagnostics.get(); } diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index 25ab13b..4119ce6 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -654,7 +654,7 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns, } TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, - DiagnosticOptions &DiagOpts, + DiagnosticOptions *DiagOpts, const Preprocessor *PP) : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {} @@ -670,15 +670,15 @@ void TextDiagnostic::emitDiagnosticMessage( if (Loc.isValid()) emitDiagnosticLoc(Loc, PLoc, Level, Ranges); - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) OS.resetColor(); - if (DiagOpts.ShowLevel) - printDiagnosticLevel(OS, Level, DiagOpts.ShowColors); + if (DiagOpts->ShowLevel) + printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); printDiagnosticMessage(OS, /*IsSupplemental*/ Level == DiagnosticsEngine::Note, Message, OS.tell() - StartOfLocationInfo, - DiagOpts.MessageLength, DiagOpts.ShowColors); + DiagOpts->MessageLength, DiagOpts->ShowColors); } /*static*/ void @@ -743,7 +743,7 @@ void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { #ifdef _WIN32 SmallString<4096> TmpFilename; #endif - if (DiagOpts.AbsolutePath) { + if (DiagOpts->AbsolutePath) { auto File = SM.getFileManager().getOptionalFileRef(Filename); if (File) { // We want to print a simplified absolute path, i. e. without "dots". @@ -796,27 +796,27 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, } unsigned LineNo = PLoc.getLine(); - if (!DiagOpts.ShowLocation) + if (!DiagOpts->ShowLocation) return; - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) OS.changeColor(savedColor, true); emitFilename(PLoc.getFilename(), Loc.getManager()); - switch (DiagOpts.getFormat()) { + switch (DiagOpts->getFormat()) { case DiagnosticOptions::SARIF: case DiagnosticOptions::Clang: - if (DiagOpts.ShowLine) + if (DiagOpts->ShowLine) OS << ':' << LineNo; break; case DiagnosticOptions::MSVC: OS << '(' << LineNo; break; case DiagnosticOptions::Vi: OS << " +" << LineNo; break; } - if (DiagOpts.ShowColumn) + if (DiagOpts->ShowColumn) // Compute the column number. if (unsigned ColNo = PLoc.getColumn()) { - if (DiagOpts.getFormat() == DiagnosticOptions::MSVC) { + if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) { OS << ','; // Visual Studio 2010 or earlier expects column number to be off by one if (LangOpts.MSCompatibilityVersion && @@ -826,7 +826,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, OS << ':'; OS << ColNo; } - switch (DiagOpts.getFormat()) { + switch (DiagOpts->getFormat()) { case DiagnosticOptions::SARIF: case DiagnosticOptions::Clang: case DiagnosticOptions::Vi: OS << ':'; break; @@ -841,7 +841,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, break; } - if (DiagOpts.ShowSourceRanges && !Ranges.empty()) { + if (DiagOpts->ShowSourceRanges && !Ranges.empty()) { FileID CaretFileID = Loc.getExpansionLoc().getFileID(); bool PrintedRange = false; const SourceManager &SM = Loc.getManager(); @@ -881,7 +881,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, } void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { - if (DiagOpts.ShowLocation && PLoc.isValid()) { + if (DiagOpts->ShowLocation && PLoc.isValid()) { OS << "In file included from "; emitFilename(PLoc.getFilename(), Loc.getManager()); OS << ':' << PLoc.getLine() << ":\n"; @@ -891,7 +891,7 @@ void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) { - if (DiagOpts.ShowLocation && PLoc.isValid()) + if (DiagOpts->ShowLocation && PLoc.isValid()) OS << "In module '" << ModuleName << "' imported from " << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; else @@ -901,7 +901,7 @@ void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) { - if (DiagOpts.ShowLocation && PLoc.isValid()) + if (DiagOpts->ShowLocation && PLoc.isValid()) OS << "While building module '" << ModuleName << "' imported from " << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; else @@ -998,13 +998,14 @@ static void highlightRange(const LineRange &R, const SourceColumnMap &Map, std::fill(CaretLine.begin() + StartColNo, CaretLine.begin() + EndColNo, '~'); } -static std::string buildFixItInsertionLine(FileID FID, unsigned LineNo, +static std::string buildFixItInsertionLine(FileID FID, + unsigned LineNo, const SourceColumnMap &map, ArrayRef<FixItHint> Hints, const SourceManager &SM, - const DiagnosticOptions &DiagOpts) { + const DiagnosticOptions *DiagOpts) { std::string FixItInsertionLine; - if (Hints.empty() || !DiagOpts.ShowFixits) + if (Hints.empty() || !DiagOpts->ShowFixits) return FixItInsertionLine; unsigned PrevHintEndCol = 0; @@ -1056,7 +1057,7 @@ static std::string buildFixItInsertionLine(FileID FID, unsigned LineNo, } } - expandTabs(FixItInsertionLine, DiagOpts.TabStop); + expandTabs(FixItInsertionLine, DiagOpts->TabStop); return FixItInsertionLine; } @@ -1295,7 +1296,7 @@ void TextDiagnostic::emitSnippetAndCaret( // was part of a different warning or error diagnostic, or if the // diagnostic has ranges. We don't want to emit the same caret // multiple times if one loc has multiple diagnostics. - if (!DiagOpts.ShowCarets) + if (!DiagOpts->ShowCarets) return; if (Loc == LastLoc && Ranges.empty() && Hints.empty() && (LastLevel != DiagnosticsEngine::Note || Level == LastLevel)) @@ -1321,7 +1322,7 @@ void TextDiagnostic::emitSnippetAndCaret( return; // Find the set of lines to include. - const unsigned MaxLines = DiagOpts.SnippetLineLimit; + const unsigned MaxLines = DiagOpts->SnippetLineLimit; std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo}; unsigned DisplayLineNo = Loc.getPresumedLoc().getLine(); for (const auto &I : Ranges) { @@ -1337,7 +1338,7 @@ void TextDiagnostic::emitSnippetAndCaret( // Where [number] is MaxLineNoDisplayWidth columns // and the full thing is therefore MaxLineNoDisplayWidth + 4 columns. unsigned MaxLineNoDisplayWidth = - DiagOpts.ShowLineNumbers + DiagOpts->ShowLineNumbers ? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines)) : 0; auto indentForLineNumbers = [&] { @@ -1349,7 +1350,7 @@ void TextDiagnostic::emitSnippetAndCaret( // emit, starting from the first line. std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles = highlightLines(BufData, Lines.first, Lines.second, PP, LangOpts, - DiagOpts.ShowColors, FID, SM); + DiagOpts->ShowColors, FID, SM); SmallVector<LineRange> LineRanges = prepareAndFilterRanges(Ranges, SM, Lines, FID, LangOpts); @@ -1381,7 +1382,7 @@ void TextDiagnostic::emitSnippetAndCaret( SourceLine.pop_back(); // Build the byte to column map. - const SourceColumnMap sourceColMap(SourceLine, DiagOpts.TabStop); + const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); std::string CaretLine; // Highlight all of the characters covered by Ranges with ~ characters. @@ -1397,12 +1398,12 @@ void TextDiagnostic::emitSnippetAndCaret( CaretLine[Col] = '^'; } - std::string FixItInsertionLine = - buildFixItInsertionLine(FID, LineNo, sourceColMap, Hints, SM, DiagOpts); + std::string FixItInsertionLine = buildFixItInsertionLine( + FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get()); // If the source line is too long for our terminal, select only the // "interesting" source region within that line. - unsigned Columns = DiagOpts.MessageLength; + unsigned Columns = DiagOpts->MessageLength; if (Columns) selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, Columns, sourceColMap); @@ -1411,7 +1412,7 @@ void TextDiagnostic::emitSnippetAndCaret( // to produce easily machine parsable output. Add a space before the // source line and the caret to make it trivial to tell the main diagnostic // line from what the user is intended to see. - if (DiagOpts.ShowSourceRanges && !SourceLine.empty()) { + if (DiagOpts->ShowSourceRanges && !SourceLine.empty()) { SourceLine = ' ' + SourceLine; CaretLine = ' ' + CaretLine; } @@ -1422,22 +1423,22 @@ void TextDiagnostic::emitSnippetAndCaret( if (!CaretLine.empty()) { indentForLineNumbers(); - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) OS.changeColor(caretColor, true); OS << CaretLine << '\n'; - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) OS.resetColor(); } if (!FixItInsertionLine.empty()) { indentForLineNumbers(); - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) // Print fixit line in color OS.changeColor(fixitColor, false); - if (DiagOpts.ShowSourceRanges) + if (DiagOpts->ShowSourceRanges) OS << ' '; OS << FixItInsertionLine << '\n'; - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) OS.resetColor(); } } @@ -1463,10 +1464,10 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine, size_t I = 0; while (I < SourceLine.size()) { auto [Str, WasPrintable] = - printableTextForNextCharacter(SourceLine, &I, DiagOpts.TabStop); + printableTextForNextCharacter(SourceLine, &I, DiagOpts->TabStop); // Toggle inverted colors on or off for this character. - if (DiagOpts.ShowColors) { + if (DiagOpts->ShowColors) { if (WasPrintable == PrintReversed) { PrintReversed = !PrintReversed; if (PrintReversed) @@ -1497,7 +1498,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine, OS << Str; } - if (DiagOpts.ShowColors) + if (DiagOpts->ShowColors) OS.resetColor(); OS << '\n'; @@ -1505,7 +1506,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine, void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM) { - if (!DiagOpts.ShowParseableFixits) + if (!DiagOpts->ShowParseableFixits) return; // We follow FixItRewriter's example in not (yet) handling diff --git a/clang/lib/Frontend/TextDiagnosticPrinter.cpp b/clang/lib/Frontend/TextDiagnosticPrinter.cpp index e878c63..28f7218 100644 --- a/clang/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/clang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -22,9 +22,11 @@ using namespace clang; TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os, - DiagnosticOptions &DiagOpts, + DiagnosticOptions *diags, bool _OwnsOutputStream) - : OS(os), DiagOpts(DiagOpts), OwnsOutputStream(_OwnsOutputStream) {} + : OS(os), DiagOpts(diags), + OwnsOutputStream(_OwnsOutputStream) { +} TextDiagnosticPrinter::~TextDiagnosticPrinter() { if (OwnsOutputStream) @@ -34,7 +36,7 @@ TextDiagnosticPrinter::~TextDiagnosticPrinter() { void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) { // Build the TextDiagnostic utility. - TextDiag.reset(new TextDiagnostic(OS, LO, DiagOpts, PP)); + TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts, PP)); } void TextDiagnosticPrinter::EndSourceFile() { @@ -119,7 +121,7 @@ void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, Info.FormatDiagnostic(OutStr); llvm::raw_svector_ostream DiagMessageStream(OutStr); - printDiagnosticOptions(DiagMessageStream, Level, Info, DiagOpts); + printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts); // Keeps track of the starting position of the location // information (e.g., "foo.c:10:4:") that precedes the error @@ -135,16 +137,17 @@ void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, // diagnostics in a context that lacks language options, a source manager, or // other infrastructure necessary when emitting more rich diagnostics. if (!Info.getLocation().isValid()) { - TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts.ShowColors); + TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); TextDiagnostic::printDiagnosticMessage( OS, /*IsSupplemental=*/Level == DiagnosticsEngine::Note, DiagMessageStream.str(), OS.tell() - StartOfLocationInfo, - DiagOpts.MessageLength, DiagOpts.ShowColors); + DiagOpts->MessageLength, DiagOpts->ShowColors); OS.flush(); return; } // Assert that the rest of our infrastructure is setup properly. + assert(DiagOpts && "Unexpected diagnostic without options set"); assert(Info.hasSourceManager() && "Unexpected diagnostic with no source manager"); assert(TextDiag && "Unexpected diagnostic outside source file processing"); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index dac3888..aa90663 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -359,12 +359,13 @@ void ReplCodeCompleter::codeComplete(CompilerInstance *InterpCI, unsigned Col, const CompilerInstance *ParentCI, std::vector<std::string> &CCResults) { + auto DiagOpts = DiagnosticOptions(); auto consumer = ReplCompletionConsumer(CCResults, *this); auto diag = InterpCI->getDiagnosticsPtr(); std::unique_ptr<ASTUnit> AU(ASTUnit::LoadFromCompilerInvocationAction( InterpCI->getInvocationPtr(), std::make_shared<PCHContainerOperations>(), - nullptr, diag)); + diag)); llvm::SmallVector<clang::StoredDiagnostic, 8> sd = {}; llvm::SmallVector<const llvm::MemoryBuffer *, 1> tb = {}; InterpCI->getFrontendOpts().Inputs[0] = FrontendInputFile( diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index 84feff8..4b407a0 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -95,9 +95,9 @@ CreateCI(const llvm::opt::ArgStringList &Argv) { // Buffer diagnostics from argument parsing so that we can output them using // a well formed diagnostic object. - DiagnosticOptions DiagOpts; + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; - DiagnosticsEngine Diags(DiagID, DiagOpts, DiagsBuffer); + DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer); bool Success = CompilerInvocation::CreateFromArgs( Clang->getInvocation(), llvm::ArrayRef(Argv.begin(), Argv.size()), Diags); @@ -173,10 +173,10 @@ IncrementalCompilerBuilder::create(std::string TT, // Buffer diagnostics from argument parsing so that we can output them using a // well formed diagnostic object. IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); - std::unique_ptr<DiagnosticOptions> DiagOpts = + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = CreateAndPopulateDiagOpts(ClangArgv); TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; - DiagnosticsEngine Diags(DiagID, *DiagOpts, DiagsBuffer); + DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer); driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0], TT, Diags); Driver.setCheckInputsExist(false); // the input comes from mem buffers diff --git a/clang/lib/Rewrite/HTMLRewrite.cpp b/clang/lib/Rewrite/HTMLRewrite.cpp index 1829a4f..c75835d 100644 --- a/clang/lib/Rewrite/HTMLRewrite.cpp +++ b/clang/lib/Rewrite/HTMLRewrite.cpp @@ -636,8 +636,8 @@ static void HighlightMacrosImpl( // Temporarily change the diagnostics object so that we ignore any generated // diagnostics from this pass. DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(), - PP.getDiagnostics().getDiagnosticOptions(), - new IgnoringDiagConsumer); + &PP.getDiagnostics().getDiagnosticOptions(), + new IgnoringDiagConsumer); // FIXME: This is a huge hack; we reuse the input preprocessor because we want // its state, but we aren't actually changing it (we hope). This should really diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index c113fd7..d068f5e1 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -193,7 +193,8 @@ bool ChainedASTReaderListener::ReadTargetOptions( } bool ChainedASTReaderListener::ReadDiagnosticOptions( - DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) { + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, StringRef ModuleFilename, + bool Complain) { return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) || Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain); } @@ -594,16 +595,16 @@ static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, return M; } -bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts, - StringRef ModuleFilename, - bool Complain) { +bool PCHValidator::ReadDiagnosticOptions( + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, StringRef ModuleFilename, + bool Complain) { DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); IntrusiveRefCntPtr<DiagnosticsEngine> Diags( - new DiagnosticsEngine(DiagIDs, DiagOpts)); + new DiagnosticsEngine(DiagIDs, DiagOpts.get())); // This should never fail, because we would have processed these options // before writing them to an ASTFile. - ProcessWarningOptions(*Diags, DiagOpts, + ProcessWarningOptions(*Diags, *DiagOpts, PP.getFileManager().getVirtualFileSystem(), /*Report*/ false); @@ -6421,17 +6422,17 @@ bool ASTReader::ParseTargetOptions(const RecordData &Record, bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, StringRef ModuleFilename, bool Complain, ASTReaderListener &Listener) { - DiagnosticOptions DiagOpts; + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); unsigned Idx = 0; -#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++]; -#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ - DiagOpts.set##Name(static_cast<Type>(Record[Idx++])); +#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; +#define ENUM_DIAGOPT(Name, Type, Bits, Default) \ + DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); #include "clang/Basic/DiagnosticOptions.def" for (unsigned N = Record[Idx++]; N; --N) - DiagOpts.Warnings.push_back(ReadString(Record, Idx)); + DiagOpts->Warnings.push_back(ReadString(Record, Idx)); for (unsigned N = Record[Idx++]; N; --N) - DiagOpts.Remarks.push_back(ReadString(Record, Idx)); + DiagOpts->Remarks.push_back(ReadString(Record, Idx)); return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain); } diff --git a/clang/lib/Testing/TestAST.cpp b/clang/lib/Testing/TestAST.cpp index b59a8d5..748f59b 100644 --- a/clang/lib/Testing/TestAST.cpp +++ b/clang/lib/Testing/TestAST.cpp @@ -44,7 +44,7 @@ public: std::string Text; llvm::raw_string_ostream OS(Text); TextDiagnostic Renderer(OS, LangOpts, - Info.getDiags()->getDiagnosticOptions()); + &Info.getDiags()->getDiagnosticOptions()); Renderer.emitStoredDiagnostic(Out.back()); ADD_FAILURE() << Text; } diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 09596b9..af18194 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -243,13 +243,13 @@ std::string GetClangToolCommand() { static bool stripPositionalArgs(std::vector<const char *> Args, std::vector<std::string> &Result, std::string &ErrorMsg) { - DiagnosticOptions DiagOpts; + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); llvm::raw_string_ostream Output(ErrorMsg); - TextDiagnosticPrinter DiagnosticPrinter(Output, DiagOpts); + TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts); UnusedInputDiagConsumer DiagClient(DiagnosticPrinter); DiagnosticsEngine Diagnostics( - IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), DiagOpts, - &DiagClient, false); + IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), + &*DiagOpts, &DiagClient, false); // The clang executable path isn't required since the jobs the driver builds // will not be executed. diff --git a/clang/lib/Tooling/Core/Replacement.cpp b/clang/lib/Tooling/Core/Replacement.cpp index 9e2582e..92e9859 100644 --- a/clang/lib/Tooling/Core/Replacement.cpp +++ b/clang/lib/Tooling/Core/Replacement.cpp @@ -585,9 +585,9 @@ llvm::Expected<std::string> applyAllReplacements(StringRef Code, IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( new llvm::vfs::InMemoryFileSystem); FileManager Files(FileSystemOptions(), InMemoryFileSystem); - DiagnosticOptions DiagOpts; DiagnosticsEngine Diagnostics( - IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), DiagOpts); + IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), + new DiagnosticOptions); SourceManager SourceMgr(Diagnostics, Files); Rewriter Rewrite(SourceMgr, LangOptions()); InMemoryFileSystem->addFile( diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp index 207b0a9..21eea72 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -642,7 +642,7 @@ llvm::Error DependencyScanningWorker::computeDependencies( std::string DiagnosticOutput; llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); auto DiagOpts = createDiagOptions(CommandLine); - TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, *DiagOpts); + TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, DiagOpts.release()); if (computeDependencies(WorkingDirectory, CommandLine, Consumer, Controller, DiagPrinter, TUBuffer)) @@ -660,7 +660,7 @@ llvm::Error DependencyScanningWorker::computeDependencies( std::string DiagnosticOutput; llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); auto DiagOpts = createDiagOptions(CommandLine); - TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, *DiagOpts); + TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, DiagOpts.release()); if (computeDependencies(WorkingDirectory, CommandLine, Consumer, Controller, DiagPrinter, ModuleName)) @@ -744,7 +744,7 @@ bool DependencyScanningWorker::scanDependencies( sanitizeDiagOpts(*DiagOpts); IntrusiveRefCntPtr<DiagnosticsEngine> Diags = CompilerInstance::createDiagnostics(FileMgr->getVirtualFileSystem(), - *DiagOpts, &DC, + DiagOpts.release(), &DC, /*ShouldOwnClient=*/false); // Although `Diagnostics` are used only for command-line parsing, the diff --git a/clang/lib/Tooling/Refactoring.cpp b/clang/lib/Tooling/Refactoring.cpp index 874d44ff..961fc1c 100644 --- a/clang/lib/Tooling/Refactoring.cpp +++ b/clang/lib/Tooling/Refactoring.cpp @@ -39,11 +39,11 @@ int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) { } LangOptions DefaultLangOptions; - DiagnosticOptions DiagOpts; - TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts); + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); DiagnosticsEngine Diagnostics( - IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), DiagOpts, - &DiagnosticPrinter, false); + IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), + &*DiagOpts, &DiagnosticPrinter, false); SourceManager Sources(Diagnostics, getFiles()); Rewriter Rewrite(Sources, DefaultLangOptions); diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 1abd7c7..3c72f52 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -377,17 +377,17 @@ bool ToolInvocation::run() { // Parse diagnostic options from the driver command-line only if none were // explicitly set. - std::unique_ptr<DiagnosticOptions> ParsedDiagOpts; + IntrusiveRefCntPtr<DiagnosticOptions> ParsedDiagOpts; DiagnosticOptions *DiagOpts = this->DiagOpts; if (!DiagOpts) { ParsedDiagOpts = CreateAndPopulateDiagOpts(Argv); DiagOpts = &*ParsedDiagOpts; } - TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), *DiagOpts); + TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts); IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics = CompilerInstance::createDiagnostics( - Files->getVirtualFileSystem(), *DiagOpts, + Files->getVirtualFileSystem(), &*DiagOpts, DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); // Although `Diagnostics` are used only for command-line parsing, the custom // `DiagConsumer` might expect a `SourceManager` to be present. @@ -652,9 +652,9 @@ public: std::shared_ptr<PCHContainerOperations> PCHContainerOps, DiagnosticConsumer *DiagConsumer) override { std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( - Invocation, std::move(PCHContainerOps), nullptr, + Invocation, std::move(PCHContainerOps), CompilerInstance::createDiagnostics(Files->getVirtualFileSystem(), - Invocation->getDiagnosticOpts(), + &Invocation->getDiagnosticOpts(), DiagConsumer, /*ShouldOwnClient=*/false), Files); |