1//===-- fc1_main.cpp - Flang FC1 Compiler Frontend ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the entry point to the flang -fc1 functionality, which implements the
10// core compiler functionality along with a number of additional tools for
11// demonstration and testing purposes.
12//
13//===----------------------------------------------------------------------===//
14//
15// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
16//
17//===----------------------------------------------------------------------===//
18
19#include "flang/Frontend/CompilerInstance.h"
20#include "flang/Frontend/CompilerInvocation.h"
21#include "flang/Frontend/TextDiagnosticBuffer.h"
22#include "flang/FrontendTool/Utils.h"
23#include "clang/Driver/DriverDiagnostic.h"
24#include "llvm/Option/Arg.h"
25#include "llvm/Option/ArgList.h"
26#include "llvm/Option/OptTable.h"
27#include "llvm/Support/TargetSelect.h"
28
29#include <cstdio>
30
31using namespace Fortran::frontend;
32
33int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
34 // Create CompilerInstance
35 std::unique_ptr<CompilerInstance> flang(new CompilerInstance());
36
37 // Create DiagnosticsEngine for the frontend driver
38 flang->createDiagnostics();
39 if (!flang->hasDiagnostics())
40 return 1;
41
42 // We will buffer diagnostics from argument parsing so that we can output
43 // them using a well formed diagnostic object.
44 TextDiagnosticBuffer *diagsBuffer = new TextDiagnosticBuffer;
45
46 // Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine
47 // for parsing the arguments
48 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(
49 new clang::DiagnosticIDs());
50 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts =
51 new clang::DiagnosticOptions();
52 clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagsBuffer);
53 bool success = CompilerInvocation::createFromArgs(flang->getInvocation(),
54 argv, diags, argv0);
55
56 // Initialize targets first, so that --version shows registered targets.
57 llvm::InitializeAllTargets();
58 llvm::InitializeAllTargetMCs();
59 llvm::InitializeAllAsmPrinters();
60
61 diagsBuffer->flushDiagnostics(flang->getDiagnostics());
62
63 if (!success)
64 return 1;
65
66 // Execute the frontend actions.
67 success = executeCompilerInvocation(flang.get());
68
69 // Delete output files to free Compiler Instance
70 flang->clearOutputFiles(/*EraseFiles=*/false);
71
72 return !success;
73}
74

source code of flang/tools/flang-driver/fc1_main.cpp