xref: /llvm-project/flang/tools/flang-driver/fc1_main.cpp (revision 9ffb5b0469ae593f8b7e1d7d9ef6ea46cd366e6e)
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 #include "flang/Frontend/CompilerInstance.h"
16 #include "flang/Frontend/CompilerInvocation.h"
17 #include "flang/Frontend/TextDiagnosticBuffer.h"
18 #include "flang/FrontendTool/Utils.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Option/OptTable.h"
23 
24 #include <cstdio>
25 
26 using namespace Fortran::frontend;
27 
28 int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
29   // Create CompilerInstance
30   std::unique_ptr<CompilerInstance> flang(new CompilerInstance());
31 
32   // Create DiagnosticsEngine for the frontend driver
33   flang->CreateDiagnostics();
34   if (!flang->HasDiagnostics())
35     return 1;
36 
37   // We will buffer diagnostics from argument parsing so that we can output
38   // them using a well formed diagnostic object.
39   TextDiagnosticBuffer *diagsBuffer = new TextDiagnosticBuffer;
40 
41   // Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine
42   // for parsing the arguments
43   llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(
44       new clang::DiagnosticIDs());
45   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts =
46       new clang::DiagnosticOptions();
47   clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagsBuffer);
48   bool success =
49       CompilerInvocation::CreateFromArgs(flang->invocation(), argv, diags);
50 
51   diagsBuffer->FlushDiagnostics(flang->diagnostics());
52 
53   if (!success)
54     return 1;
55 
56   // Execute the frontend actions.
57   success = ExecuteCompilerInvocation(flang.get());
58 
59   // Delete output files to free Compiler Instance
60   flang->ClearOutputFiles(/*EraseFiles=*/false);
61 
62   return !success;
63 }
64