Changeset View
Changeset View
Standalone View
Standalone View
flang/tools/flang-driver/fc1_main.cpp
//===-- fc1_main.cpp - Flang FC1 Compiler Frontend ------------------------===// | //===-- fc1_main.cpp - Flang FC1 Compiler Frontend ------------------------===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// | // | ||||
// This is the entry point to the flang -fc1 functionality, which implements the | // This is the entry point to the flang -fc1 functionality, which implements the | ||||
// core compiler functionality along with a number of additional tools for | // core compiler functionality along with a number of additional tools for | ||||
// demonstration and testing purposes. | // demonstration and testing purposes. | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "flang/Frontend/CompilerInstance.h" | #include "flang/Frontend/CompilerInstance.h" | ||||
#include "flang/Frontend/CompilerInvocation.h" | #include "flang/Frontend/CompilerInvocation.h" | ||||
#include "flang/Frontend/TextDiagnosticBuffer.h" | |||||
#include "flang/FrontendTool/Utils.h" | #include "flang/FrontendTool/Utils.h" | ||||
#include "clang/Driver/DriverDiagnostic.h" | #include "clang/Driver/DriverDiagnostic.h" | ||||
#include "clang/Frontend/TextDiagnosticBuffer.h" | |||||
#include "llvm/Option/Arg.h" | #include "llvm/Option/Arg.h" | ||||
#include "llvm/Option/ArgList.h" | #include "llvm/Option/ArgList.h" | ||||
#include "llvm/Option/OptTable.h" | #include "llvm/Option/OptTable.h" | ||||
#include <cstdio> | #include <cstdio> | ||||
using namespace Fortran::frontend; | using namespace Fortran::frontend; | ||||
int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) { | int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) { | ||||
// Create CompilerInstance | // Create CompilerInstance | ||||
std::unique_ptr<CompilerInstance> flang(new CompilerInstance()); | std::unique_ptr<CompilerInstance> flang(new CompilerInstance()); | ||||
// Create DiagnosticsEngine for the frontend driver | // Create DiagnosticsEngine for the frontend driver | ||||
flang->CreateDiagnostics(); | flang->CreateDiagnostics(); | ||||
if (!flang->HasDiagnostics()) | if (!flang->HasDiagnostics()) | ||||
return 1; | return 1; | ||||
// We will buffer diagnostics from argument parsing so that we can output | |||||
// them using a well formed diagnostic object. | |||||
TextDiagnosticBuffer *diagsBuffer = new TextDiagnosticBuffer; | |||||
kiranchandramohan: Anyreason why this was moved up here (compared to the previous version which was close to its… | |||||
Sorry, this is unrelated. I wanted to better highlight that we are using TextDiagnosticBuffer (instead of e.g. TextDiagnosticPrinter) and to separate the creation of CompilerInvocation from this. awarzynski: Sorry, this is unrelated.
I wanted to better highlight that we are using… | |||||
Not Done ReplyInline Actions#JustAsking: Should this be deleted somewhere? kiranchandramohan: #JustAsking: Should this be deleted somewhere? | |||||
DiagnosticsConsumer (in this case, diagsBuffer), is required to construct diags, i.e. an instance of DiagnosticsEngine.
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; Reset(); }
explicit DiagnosticsEngine(IntrusiveRefCntPtr<DiagnosticIDs> Diags, IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, DiagnosticConsumer *client = nullptr, bool ShouldOwnClient = true); Note that ShouldOwnClient defaults to true. So this should be fine #fingers-crossed :) awarzynski: `DiagnosticsConsumer` (in this case, `diagsBuffer`), is required to construct `diags`, i.e. an… | |||||
// Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine | // Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine | ||||
// for parsing the arguments | // for parsing the arguments | ||||
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID( | llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID( | ||||
new clang::DiagnosticIDs()); | new clang::DiagnosticIDs()); | ||||
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts = | llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts = | ||||
new clang::DiagnosticOptions(); | new clang::DiagnosticOptions(); | ||||
clang::TextDiagnosticBuffer *diagsBuffer = new clang::TextDiagnosticBuffer; | |||||
clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagsBuffer); | clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagsBuffer); | ||||
bool success = | bool success = | ||||
CompilerInvocation::CreateFromArgs(flang->GetInvocation(), argv, diags); | CompilerInvocation::CreateFromArgs(flang->GetInvocation(), argv, diags); | ||||
diagsBuffer->FlushDiagnostics(flang->getDiagnostics()); | diagsBuffer->FlushDiagnostics(flang->getDiagnostics()); | ||||
if (!success) | if (!success) | ||||
return 1; | return 1; | ||||
// Execute the frontend actions. | // Execute the frontend actions. | ||||
success = ExecuteCompilerInvocation(flang.get()); | success = ExecuteCompilerInvocation(flang.get()); | ||||
return !success; | return !success; | ||||
} | } |
Anyreason why this was moved up here (compared to the previous version which was close to its use)?