aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h')
-rw-r--r--contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h357
1 files changed, 233 insertions, 124 deletions
diff --git a/contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h b/contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h
index 2245439d0632..c6528779bde7 100644
--- a/contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/contrib/llvm-project/clang/include/clang/Frontend/CompilerInvocation.h
@@ -9,6 +9,7 @@
#ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
#define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
+#include "clang/APINotes/APINotesOptions.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileSystemOptions.h"
@@ -50,6 +51,11 @@ class HeaderSearchOptions;
class PreprocessorOptions;
class TargetOptions;
+// This lets us create the DiagnosticsEngine with a properly-filled-out
+// DiagnosticOptions instance.
+std::unique_ptr<DiagnosticOptions>
+CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv);
+
/// Fill out Opts based on the options given in Args.
///
/// Args must have been created from the OptTable returned by
@@ -61,16 +67,12 @@ bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
DiagnosticsEngine *Diags = nullptr,
bool DefaultDiagColor = true);
-/// The base class of CompilerInvocation with reference semantics.
-///
-/// This class stores option objects behind reference-counted pointers. This is
-/// useful for clients that want to keep some option object around even after
-/// CompilerInvocation gets destroyed, without making a copy.
-///
-/// This is a separate class so that we can implement the copy constructor and
-/// assignment here and leave them defaulted in the rest of CompilerInvocation.
-class CompilerInvocationRefBase {
-public:
+/// The base class of CompilerInvocation. It keeps individual option objects
+/// behind reference-counted pointers, which is useful for clients that want to
+/// keep select option objects alive (even after CompilerInvocation gets
+/// destroyed) without making a copy.
+class CompilerInvocationBase {
+protected:
/// Options controlling the language variant.
std::shared_ptr<LangOptions> LangOpts;
@@ -81,103 +83,122 @@ public:
IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
/// Options controlling the \#include directive.
- std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
+ std::shared_ptr<HeaderSearchOptions> HSOpts;
/// Options controlling the preprocessor (aside from \#include handling).
- std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
+ std::shared_ptr<PreprocessorOptions> PPOpts;
/// Options controlling the static analyzer.
AnalyzerOptionsRef AnalyzerOpts;
- CompilerInvocationRefBase();
- CompilerInvocationRefBase(const CompilerInvocationRefBase &X);
- CompilerInvocationRefBase(CompilerInvocationRefBase &&X);
- CompilerInvocationRefBase &operator=(CompilerInvocationRefBase X);
- CompilerInvocationRefBase &operator=(CompilerInvocationRefBase &&X);
- ~CompilerInvocationRefBase();
-
- LangOptions *getLangOpts() { return LangOpts.get(); }
- const LangOptions *getLangOpts() const { return LangOpts.get(); }
-
- TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
- const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
+ std::shared_ptr<MigratorOptions> MigratorOpts;
- DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
-
- HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
-
- const HeaderSearchOptions &getHeaderSearchOpts() const {
- return *HeaderSearchOpts;
- }
-
- std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
- return HeaderSearchOpts;
- }
-
- std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
- return PreprocessorOpts;
- }
-
- PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
-
- const PreprocessorOptions &getPreprocessorOpts() const {
- return *PreprocessorOpts;
- }
-
- AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; }
-};
-
-/// The base class of CompilerInvocation with value semantics.
-class CompilerInvocationValueBase {
-protected:
- MigratorOptions MigratorOpts;
+ /// Options controlling API notes.
+ std::shared_ptr<APINotesOptions> APINotesOpts;
/// Options controlling IRgen and the backend.
- CodeGenOptions CodeGenOpts;
-
- /// Options controlling dependency output.
- DependencyOutputOptions DependencyOutputOpts;
+ std::shared_ptr<CodeGenOptions> CodeGenOpts;
/// Options controlling file system operations.
- FileSystemOptions FileSystemOpts;
+ std::shared_ptr<FileSystemOptions> FSOpts;
/// Options controlling the frontend itself.
- FrontendOptions FrontendOpts;
+ std::shared_ptr<FrontendOptions> FrontendOpts;
+
+ /// Options controlling dependency output.
+ std::shared_ptr<DependencyOutputOptions> DependencyOutputOpts;
/// Options controlling preprocessed output.
- PreprocessorOutputOptions PreprocessorOutputOpts;
+ std::shared_ptr<PreprocessorOutputOptions> PreprocessorOutputOpts;
+
+ /// Dummy tag type whose instance can be passed into the constructor to
+ /// prevent creation of the reference-counted option objects.
+ struct EmptyConstructor {};
+
+ CompilerInvocationBase();
+ CompilerInvocationBase(EmptyConstructor) {}
+ CompilerInvocationBase(const CompilerInvocationBase &X) = delete;
+ CompilerInvocationBase(CompilerInvocationBase &&X) = default;
+ CompilerInvocationBase &operator=(const CompilerInvocationBase &X) = delete;
+ CompilerInvocationBase &deep_copy_assign(const CompilerInvocationBase &X);
+ CompilerInvocationBase &shallow_copy_assign(const CompilerInvocationBase &X);
+ CompilerInvocationBase &operator=(CompilerInvocationBase &&X) = default;
+ ~CompilerInvocationBase() = default;
public:
- MigratorOptions &getMigratorOpts() { return MigratorOpts; }
- const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
-
- CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
- const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
-
- DependencyOutputOptions &getDependencyOutputOpts() {
- return DependencyOutputOpts;
+ /// Const getters.
+ /// @{
+ const LangOptions &getLangOpts() const { return *LangOpts; }
+ const TargetOptions &getTargetOpts() const { return *TargetOpts; }
+ const DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
+ const HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
+ const PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
+ const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
+ const MigratorOptions &getMigratorOpts() const { return *MigratorOpts; }
+ const APINotesOptions &getAPINotesOpts() const { return *APINotesOpts; }
+ const CodeGenOptions &getCodeGenOpts() const { return *CodeGenOpts; }
+ const FileSystemOptions &getFileSystemOpts() const { return *FSOpts; }
+ const FrontendOptions &getFrontendOpts() const { return *FrontendOpts; }
+ const DependencyOutputOptions &getDependencyOutputOpts() const {
+ return *DependencyOutputOpts;
+ }
+ const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
+ return *PreprocessorOutputOpts;
}
+ /// @}
- const DependencyOutputOptions &getDependencyOutputOpts() const {
- return DependencyOutputOpts;
+ /// Command line generation.
+ /// @{
+ using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
+ /// Generate cc1-compatible command line arguments from this instance.
+ ///
+ /// \param [out] Args - The generated arguments. Note that the caller is
+ /// responsible for inserting the path to the clang executable and "-cc1" if
+ /// desired.
+ /// \param SA - A function that given a Twine can allocate storage for a given
+ /// command line argument and return a pointer to the newly allocated string.
+ /// The returned pointer is what gets appended to Args.
+ void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
+ StringAllocator SA) const {
+ generateCC1CommandLine([&](const Twine &Arg) {
+ // No need to allocate static string literals.
+ Args.push_back(Arg.isSingleStringLiteral()
+ ? Arg.getSingleStringRef().data()
+ : SA(Arg));
+ });
}
- FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
+ using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
+ /// Generate cc1-compatible command line arguments from this instance.
+ ///
+ /// \param Consumer - Callback that gets invoked for every single generated
+ /// command line argument.
+ void generateCC1CommandLine(ArgumentConsumer Consumer) const;
- const FileSystemOptions &getFileSystemOpts() const {
- return FileSystemOpts;
- }
+ /// Generate cc1-compatible command line arguments from this instance,
+ /// wrapping the result as a std::vector<std::string>.
+ ///
+ /// This is a (less-efficient) wrapper over generateCC1CommandLine().
+ std::vector<std::string> getCC1CommandLine() const;
- FrontendOptions &getFrontendOpts() { return FrontendOpts; }
- const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
+private:
+ /// Generate command line options from DiagnosticOptions.
+ static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
+ ArgumentConsumer Consumer,
+ bool DefaultDiagColor);
- PreprocessorOutputOptions &getPreprocessorOutputOpts() {
- return PreprocessorOutputOpts;
- }
+ /// Generate command line options from LangOptions.
+ static void GenerateLangArgs(const LangOptions &Opts,
+ ArgumentConsumer Consumer, const llvm::Triple &T,
+ InputKind IK);
- const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
- return PreprocessorOutputOpts;
- }
+ // Generate command line options from CodeGenOptions.
+ static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
+ ArgumentConsumer Consumer,
+ const llvm::Triple &T,
+ const std::string &OutputFile,
+ const LangOptions *LangOpts);
+ /// @}
};
/// Helper class for holding the data necessary to invoke the compiler.
@@ -185,9 +206,73 @@ public:
/// This class is designed to represent an abstract "invocation" of the
/// compiler, including data such as the include paths, the code generation
/// options, the warning flags, and so on.
-class CompilerInvocation : public CompilerInvocationRefBase,
- public CompilerInvocationValueBase {
+class CompilerInvocation : public CompilerInvocationBase {
public:
+ CompilerInvocation() = default;
+ CompilerInvocation(const CompilerInvocation &X)
+ : CompilerInvocationBase(EmptyConstructor{}) {
+ deep_copy_assign(X);
+ }
+ CompilerInvocation(CompilerInvocation &&) = default;
+ CompilerInvocation &operator=(const CompilerInvocation &X) {
+ deep_copy_assign(X);
+ return *this;
+ }
+ ~CompilerInvocation() = default;
+
+ /// Const getters.
+ /// @{
+ // Note: These need to be pulled in manually. Otherwise, they get hidden by
+ // the mutable getters with the same names.
+ using CompilerInvocationBase::getLangOpts;
+ using CompilerInvocationBase::getTargetOpts;
+ using CompilerInvocationBase::getDiagnosticOpts;
+ using CompilerInvocationBase::getHeaderSearchOpts;
+ using CompilerInvocationBase::getPreprocessorOpts;
+ using CompilerInvocationBase::getAnalyzerOpts;
+ using CompilerInvocationBase::getMigratorOpts;
+ using CompilerInvocationBase::getAPINotesOpts;
+ using CompilerInvocationBase::getCodeGenOpts;
+ using CompilerInvocationBase::getFileSystemOpts;
+ using CompilerInvocationBase::getFrontendOpts;
+ using CompilerInvocationBase::getDependencyOutputOpts;
+ using CompilerInvocationBase::getPreprocessorOutputOpts;
+ /// @}
+
+ /// Mutable getters.
+ /// @{
+ LangOptions &getLangOpts() { return *LangOpts; }
+ TargetOptions &getTargetOpts() { return *TargetOpts; }
+ DiagnosticOptions &getDiagnosticOpts() { return *DiagnosticOpts; }
+ HeaderSearchOptions &getHeaderSearchOpts() { return *HSOpts; }
+ PreprocessorOptions &getPreprocessorOpts() { return *PPOpts; }
+ AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
+ MigratorOptions &getMigratorOpts() { return *MigratorOpts; }
+ APINotesOptions &getAPINotesOpts() { return *APINotesOpts; }
+ CodeGenOptions &getCodeGenOpts() { return *CodeGenOpts; }
+ FileSystemOptions &getFileSystemOpts() { return *FSOpts; }
+ FrontendOptions &getFrontendOpts() { return *FrontendOpts; }
+ DependencyOutputOptions &getDependencyOutputOpts() {
+ return *DependencyOutputOpts;
+ }
+ PreprocessorOutputOptions &getPreprocessorOutputOpts() {
+ return *PreprocessorOutputOpts;
+ }
+ /// @}
+
+ /// Base class internals.
+ /// @{
+ using CompilerInvocationBase::LangOpts;
+ using CompilerInvocationBase::TargetOpts;
+ using CompilerInvocationBase::DiagnosticOpts;
+ std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() {
+ return HSOpts;
+ }
+ std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
+ return PPOpts;
+ }
+ /// @}
+
/// Create a compiler invocation from a list of input options.
/// \returns true on success.
///
@@ -214,70 +299,89 @@ public:
/// executable), for finding the builtin compiler path.
static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
- /// Set language defaults for the given input language and
- /// language standard in the given LangOptions object.
- ///
- /// \param Opts - The LangOptions object to set up.
- /// \param IK - The input language.
- /// \param T - The target triple.
- /// \param Includes - The affected list of included files.
- /// \param LangStd - The input language standard.
- static void
- setLangDefaults(LangOptions &Opts, InputKind IK, const llvm::Triple &T,
- std::vector<std::string> &Includes,
- LangStandard::Kind LangStd = LangStandard::lang_unspecified);
-
/// Retrieve a module hash string that is suitable for uniquely
/// identifying the conditions under which the module was built.
std::string getModuleHash() const;
- using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
- /// Generate a cc1-compatible command line arguments from this instance.
+ /// Check that \p Args can be parsed and re-serialized without change,
+ /// emiting diagnostics for any differences.
///
- /// \param [out] Args - The generated arguments. Note that the caller is
- /// responsible for inserting the path to the clang executable and "-cc1" if
- /// desired.
- /// \param SA - A function that given a Twine can allocate storage for a given
- /// command line argument and return a pointer to the newly allocated string.
- /// The returned pointer is what gets appended to Args.
- void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
- StringAllocator SA) const;
+ /// This check is only suitable for command-lines that are expected to already
+ /// be canonical.
+ ///
+ /// \return false if there are any errors.
+ static bool checkCC1RoundTrip(ArrayRef<const char *> Args,
+ DiagnosticsEngine &Diags,
+ const char *Argv0 = nullptr);
+
+ /// Reset all of the options that are not considered when building a
+ /// module.
+ void resetNonModularOptions();
+
+ /// Disable implicit modules and canonicalize options that are only used by
+ /// implicit modules.
+ void clearImplicitModuleBuildOptions();
private:
static bool CreateFromArgsImpl(CompilerInvocation &Res,
ArrayRef<const char *> CommandLineArgs,
DiagnosticsEngine &Diags, const char *Argv0);
- /// Generate command line options from DiagnosticOptions.
- static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
- SmallVectorImpl<const char *> &Args,
- StringAllocator SA, bool DefaultDiagColor);
-
/// Parse command line options that map to LangOptions.
static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
InputKind IK, const llvm::Triple &T,
std::vector<std::string> &Includes,
DiagnosticsEngine &Diags);
- /// Generate command line options from LangOptions.
- static void GenerateLangArgs(const LangOptions &Opts,
- SmallVectorImpl<const char *> &Args,
- StringAllocator SA, const llvm::Triple &T,
- InputKind IK);
-
/// Parse command line options that map to CodeGenOptions.
static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
InputKind IK, DiagnosticsEngine &Diags,
const llvm::Triple &T,
const std::string &OutputFile,
const LangOptions &LangOptsRef);
+};
- // Generate command line options from CodeGenOptions.
- static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
- SmallVectorImpl<const char *> &Args,
- StringAllocator SA, const llvm::Triple &T,
- const std::string &OutputFile,
- const LangOptions *LangOpts);
+/// Same as \c CompilerInvocation, but with copy-on-write optimization.
+class CowCompilerInvocation : public CompilerInvocationBase {
+public:
+ CowCompilerInvocation() = default;
+ CowCompilerInvocation(const CowCompilerInvocation &X)
+ : CompilerInvocationBase(EmptyConstructor{}) {
+ shallow_copy_assign(X);
+ }
+ CowCompilerInvocation(CowCompilerInvocation &&) = default;
+ CowCompilerInvocation &operator=(const CowCompilerInvocation &X) {
+ shallow_copy_assign(X);
+ return *this;
+ }
+ ~CowCompilerInvocation() = default;
+
+ CowCompilerInvocation(const CompilerInvocation &X)
+ : CompilerInvocationBase(EmptyConstructor{}) {
+ deep_copy_assign(X);
+ }
+
+ CowCompilerInvocation(CompilerInvocation &&X)
+ : CompilerInvocationBase(std::move(X)) {}
+
+ // Const getters are inherited from the base class.
+
+ /// Mutable getters.
+ /// @{
+ LangOptions &getMutLangOpts();
+ TargetOptions &getMutTargetOpts();
+ DiagnosticOptions &getMutDiagnosticOpts();
+ HeaderSearchOptions &getMutHeaderSearchOpts();
+ PreprocessorOptions &getMutPreprocessorOpts();
+ AnalyzerOptions &getMutAnalyzerOpts();
+ MigratorOptions &getMutMigratorOpts();
+ APINotesOptions &getMutAPINotesOpts();
+ CodeGenOptions &getMutCodeGenOpts();
+ FileSystemOptions &getMutFileSystemOpts();
+ FrontendOptions &getMutFrontendOpts();
+ DependencyOutputOptions &getMutDependencyOutputOpts();
+ PreprocessorOutputOptions &getMutPreprocessorOutputOpts();
+ /// @}
};
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
@@ -288,6 +392,11 @@ IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
+IntrusiveRefCntPtr<llvm::vfs::FileSystem>
+createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,
+ DiagnosticsEngine &Diags,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
+
} // namespace clang
#endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H