1 //===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C -*-===//
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 #ifndef LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
9 #define LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
10 
11 #include "flang/Frontend/FrontendOptions.h"
12 #include "flang/Parser/parsing.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/DiagnosticOptions.h"
15 #include "llvm/Option/ArgList.h"
16 
17 namespace Fortran::frontend {
18 
19 /// Fill out Opts based on the options given in Args.
20 ///
21 /// When errors are encountered, return false and, if Diags is non-null,
22 /// report the error(s).
23 bool ParseDiagnosticArgs(clang::DiagnosticOptions &opts,
24     llvm::opt::ArgList &args, bool defaultDiagColor = true);
25 
26 class CompilerInvocationBase {
27 public:
28   /// Options controlling the diagnostic engine.
29   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnosticOpts_;
30 
31   CompilerInvocationBase();
32   CompilerInvocationBase(const CompilerInvocationBase &x);
33   ~CompilerInvocationBase();
34 
GetDiagnosticOpts()35   clang::DiagnosticOptions &GetDiagnosticOpts() {
36     return *diagnosticOpts_.get();
37   }
GetDiagnosticOpts()38   const clang::DiagnosticOptions &GetDiagnosticOpts() const {
39     return *diagnosticOpts_.get();
40   }
41 };
42 
43 class CompilerInvocation : public CompilerInvocationBase {
44   /// Options for the frontend driver
45   // TODO: Merge with or translate to parserOpts_. We shouldn't need two sets of
46   // options.
47   FrontendOptions frontendOpts_;
48 
49   /// Options for Flang parser
50   // TODO: Merge with or translate to frontendOpts_. We shouldn't need two sets
51   // of options.
52   Fortran::parser::Options parserOpts_;
53 
54 public:
55   CompilerInvocation() = default;
56 
frontendOpts()57   FrontendOptions &frontendOpts() { return frontendOpts_; }
frontendOpts()58   const FrontendOptions &frontendOpts() const { return frontendOpts_; }
59 
fortranOpts()60   Fortran::parser::Options &fortranOpts() { return parserOpts_; }
fortranOpts()61   const Fortran::parser::Options &fortranOpts() const { return parserOpts_; }
62 
63   /// Create a compiler invocation from a list of input options.
64   /// \returns true on success.
65   /// \returns false if an error was encountered while parsing the arguments
66   /// \param [out] res - The resulting invocation.
67   static bool CreateFromArgs(CompilerInvocation &res,
68       llvm::ArrayRef<const char *> commandLineArgs,
69       clang::DiagnosticsEngine &diags);
70 
71   /// Set the Fortran options to predifined defaults. These defaults are
72   /// consistend with f18/f18.cpp.
73   // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we
74   // need to extend frontendOpts_ first. Next, we need to add the corresponding
75   // compiler driver options in libclangDriver.
76   void SetDefaultFortranOpts();
77 };
78 
79 } // end namespace Fortran::frontend
80 #endif // LLVM_FLANG_FRONTEND_COMPILERINVOCATION_H
81