1341825Sdim//===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C++ -*-===//
2199482Srdivacky//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6199482Srdivacky//
7199482Srdivacky//===----------------------------------------------------------------------===//
8199482Srdivacky
9341825Sdim#ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
10341825Sdim#define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
11199482Srdivacky
12344779Sdim#include "clang/Basic/CodeGenOptions.h"
13249423Sdim#include "clang/Basic/DiagnosticOptions.h"
14249423Sdim#include "clang/Basic/FileSystemOptions.h"
15341825Sdim#include "clang/Basic/LLVM.h"
16199482Srdivacky#include "clang/Basic/LangOptions.h"
17360784Sdim#include "clang/Basic/LangStandard.h"
18199482Srdivacky#include "clang/Frontend/DependencyOutputOptions.h"
19199482Srdivacky#include "clang/Frontend/FrontendOptions.h"
20249423Sdim#include "clang/Frontend/MigratorOptions.h"
21199482Srdivacky#include "clang/Frontend/PreprocessorOutputOptions.h"
22249423Sdim#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
23221345Sdim#include "llvm/ADT/IntrusiveRefCntPtr.h"
24360784Sdim#include "llvm/ADT/ArrayRef.h"
25341825Sdim#include <memory>
26199482Srdivacky#include <string>
27199482Srdivacky
28261991Sdimnamespace llvm {
29341825Sdim
30314564Sdimclass Triple;
31314564Sdim
32261991Sdimnamespace opt {
33341825Sdim
34261991Sdimclass ArgList;
35261991Sdim
36341825Sdim} // namespace opt
37341825Sdim
38344779Sdimnamespace vfs {
39344779Sdim
40344779Sdimclass FileSystem;
41344779Sdim
42344779Sdim} // namespace vfs
43344779Sdim
44341825Sdim} // namespace llvm
45341825Sdim
46199482Srdivackynamespace clang {
47341825Sdim
48341825Sdimclass DiagnosticsEngine;
49341825Sdimclass HeaderSearchOptions;
50314564Sdimclass PreprocessorOptions;
51314564Sdimclass TargetOptions;
52199990Srdivacky
53341825Sdim/// Fill out Opts based on the options given in Args.
54239462Sdim///
55234353Sdim/// Args must have been created from the OptTable returned by
56239462Sdim/// createCC1OptTable().
57239462Sdim///
58239462Sdim/// When errors are encountered, return false and, if Diags is non-null,
59239462Sdim/// report the error(s).
60261991Sdimbool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
61309124Sdim                         DiagnosticsEngine *Diags = nullptr,
62314564Sdim                         bool DefaultDiagColor = true,
63314564Sdim                         bool DefaultShowOpt = true);
64261991Sdim
65314564Sdimclass CompilerInvocationBase {
66276479Sdimpublic:
67234353Sdim  /// Options controlling the language variant.
68276479Sdim  std::shared_ptr<LangOptions> LangOpts;
69243830Sdim
70243830Sdim  /// Options controlling the target.
71276479Sdim  std::shared_ptr<TargetOptions> TargetOpts;
72243830Sdim
73243830Sdim  /// Options controlling the diagnostic engine.
74243830Sdim  IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
75243830Sdim
76243830Sdim  /// Options controlling the \#include directive.
77314564Sdim  std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
78243830Sdim
79243830Sdim  /// Options controlling the preprocessor (aside from \#include handling).
80314564Sdim  std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
81243830Sdim
82234353Sdim  CompilerInvocationBase();
83341825Sdim  CompilerInvocationBase(const CompilerInvocationBase &X);
84341825Sdim  CompilerInvocationBase &operator=(const CompilerInvocationBase &) = delete;
85276479Sdim  ~CompilerInvocationBase();
86234353Sdim
87276479Sdim  LangOptions *getLangOpts() { return LangOpts.get(); }
88276479Sdim  const LangOptions *getLangOpts() const { return LangOpts.get(); }
89243830Sdim
90276479Sdim  TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
91341825Sdim  const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
92243830Sdim
93243830Sdim  DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
94243830Sdim
95243830Sdim  HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
96341825Sdim
97243830Sdim  const HeaderSearchOptions &getHeaderSearchOpts() const {
98243830Sdim    return *HeaderSearchOpts;
99243830Sdim  }
100341825Sdim
101314564Sdim  std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
102314564Sdim    return HeaderSearchOpts;
103314564Sdim  }
104243830Sdim
105314564Sdim  std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
106314564Sdim    return PreprocessorOpts;
107314564Sdim  }
108341825Sdim
109243830Sdim  PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
110341825Sdim
111243830Sdim  const PreprocessorOptions &getPreprocessorOpts() const {
112243830Sdim    return *PreprocessorOpts;
113243830Sdim  }
114234353Sdim};
115341825Sdim
116341825Sdim/// Helper class for holding the data necessary to invoke the compiler.
117199482Srdivacky///
118199482Srdivacky/// This class is designed to represent an abstract "invocation" of the
119199482Srdivacky/// compiler, including data such as the include paths, the code generation
120199482Srdivacky/// options, the warning flags, and so on.
121234353Sdimclass CompilerInvocation : public CompilerInvocationBase {
122199482Srdivacky  /// Options controlling the static analyzer.
123243830Sdim  AnalyzerOptionsRef AnalyzerOpts;
124199482Srdivacky
125234353Sdim  MigratorOptions MigratorOpts;
126341825Sdim
127199482Srdivacky  /// Options controlling IRgen and the backend.
128199482Srdivacky  CodeGenOptions CodeGenOpts;
129199482Srdivacky
130199482Srdivacky  /// Options controlling dependency output.
131199482Srdivacky  DependencyOutputOptions DependencyOutputOpts;
132199482Srdivacky
133218893Sdim  /// Options controlling file system operations.
134218893Sdim  FileSystemOptions FileSystemOpts;
135218893Sdim
136199482Srdivacky  /// Options controlling the frontend itself.
137199482Srdivacky  FrontendOptions FrontendOpts;
138199482Srdivacky
139199482Srdivacky  /// Options controlling preprocessed output.
140199482Srdivacky  PreprocessorOutputOptions PreprocessorOutputOpts;
141199482Srdivacky
142199482Srdivackypublic:
143243830Sdim  CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
144199482Srdivacky
145199482Srdivacky  /// @name Utility Methods
146199482Srdivacky  /// @{
147199482Srdivacky
148341825Sdim  /// Create a compiler invocation from a list of input options.
149239462Sdim  /// \returns true on success.
150199482Srdivacky  ///
151360784Sdim  /// \returns false if an error was encountered while parsing the arguments
152360784Sdim  /// and attempts to recover and continue parsing the rest of the arguments.
153360784Sdim  /// The recovery is best-effort and only guarantees that \p Res will end up in
154360784Sdim  /// one of the vaild-to-access (albeit arbitrary) states.
155360784Sdim  ///
156239462Sdim  /// \param [out] Res - The resulting invocation.
157234353Sdim  static bool CreateFromArgs(CompilerInvocation &Res,
158360784Sdim                             ArrayRef<const char *> CommandLineArgs,
159226633Sdim                             DiagnosticsEngine &Diags);
160200583Srdivacky
161341825Sdim  /// Get the directory where the compiler headers
162200583Srdivacky  /// reside, relative to the compiler binary (found by the passed in
163200583Srdivacky  /// arguments).
164200583Srdivacky  ///
165199990Srdivacky  /// \param Argv0 - The program path (from argv[0]), for finding the builtin
166199990Srdivacky  /// compiler path.
167199990Srdivacky  /// \param MainAddr - The address of main (or some other function in the main
168199990Srdivacky  /// executable), for finding the builtin compiler path.
169200583Srdivacky  static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
170199482Srdivacky
171341825Sdim  /// Set language defaults for the given input language and
172218893Sdim  /// language standard in the given LangOptions object.
173218893Sdim  ///
174239462Sdim  /// \param Opts - The LangOptions object to set up.
175218893Sdim  /// \param IK - The input language.
176309124Sdim  /// \param T - The target triple.
177309124Sdim  /// \param PPOpts - The PreprocessorOptions affected.
178218893Sdim  /// \param LangStd - The input language standard.
179218893Sdim  static void setLangDefaults(LangOptions &Opts, InputKind IK,
180309124Sdim                   const llvm::Triple &T, PreprocessorOptions &PPOpts,
181218893Sdim                   LangStandard::Kind LangStd = LangStandard::lang_unspecified);
182341825Sdim
183341825Sdim  /// Retrieve a module hash string that is suitable for uniquely
184226633Sdim  /// identifying the conditions under which the module was built.
185226633Sdim  std::string getModuleHash() const;
186341825Sdim
187199482Srdivacky  /// @}
188199482Srdivacky  /// @name Option Subgroups
189199482Srdivacky  /// @{
190199482Srdivacky
191341825Sdim  AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
192199482Srdivacky
193234353Sdim  MigratorOptions &getMigratorOpts() { return MigratorOpts; }
194341825Sdim  const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
195341825Sdim
196199482Srdivacky  CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
197341825Sdim  const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
198199482Srdivacky
199199482Srdivacky  DependencyOutputOptions &getDependencyOutputOpts() {
200199482Srdivacky    return DependencyOutputOpts;
201199482Srdivacky  }
202341825Sdim
203199482Srdivacky  const DependencyOutputOptions &getDependencyOutputOpts() const {
204199482Srdivacky    return DependencyOutputOpts;
205199482Srdivacky  }
206199482Srdivacky
207218893Sdim  FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
208341825Sdim
209218893Sdim  const FileSystemOptions &getFileSystemOpts() const {
210218893Sdim    return FileSystemOpts;
211218893Sdim  }
212218893Sdim
213199482Srdivacky  FrontendOptions &getFrontendOpts() { return FrontendOpts; }
214341825Sdim  const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
215199482Srdivacky
216199482Srdivacky  PreprocessorOutputOptions &getPreprocessorOutputOpts() {
217199482Srdivacky    return PreprocessorOutputOpts;
218199482Srdivacky  }
219341825Sdim
220199482Srdivacky  const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
221199482Srdivacky    return PreprocessorOutputOpts;
222199482Srdivacky  }
223199482Srdivacky
224199482Srdivacky  /// @}
225199482Srdivacky};
226199482Srdivacky
227344779SdimIntrusiveRefCntPtr<llvm::vfs::FileSystem>
228276479SdimcreateVFSFromCompilerInvocation(const CompilerInvocation &CI,
229276479Sdim                                DiagnosticsEngine &Diags);
230276479Sdim
231344779SdimIntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
232344779Sdim    const CompilerInvocation &CI, DiagnosticsEngine &Diags,
233344779Sdim    IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
234321369Sdim
235341825Sdim} // namespace clang
236199482Srdivacky
237341825Sdim#endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
238