|
| 1 | +//===--- ArgsToFrontendOutputsConverter.h -----------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_FRONTEND_ARGSTOFRONTENDOUTPUTSCONVERTER_H |
| 14 | +#define SWIFT_FRONTEND_ARGSTOFRONTENDOUTPUTSCONVERTER_H |
| 15 | + |
| 16 | +#include "swift/AST/DiagnosticConsumer.h" |
| 17 | +#include "swift/AST/DiagnosticEngine.h" |
| 18 | +#include "swift/Basic/LLVM.h" |
| 19 | +#include "swift/Frontend/FrontendOptions.h" |
| 20 | +#include "swift/Option/Options.h" |
| 21 | +#include "llvm/Option/ArgList.h" |
| 22 | + |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace swift { |
| 26 | + |
| 27 | +/// Given the command line arguments and information about the inputs, |
| 28 | +/// Fill in all the information in FrontendInputsAndOutputs. |
| 29 | + |
| 30 | +class ArgsToFrontendOutputsConverter { |
| 31 | + const llvm::opt::ArgList &Args; |
| 32 | + StringRef ModuleName; |
| 33 | + FrontendInputsAndOutputs &InputsAndOutputs; |
| 34 | + DiagnosticEngine &Diags; |
| 35 | + |
| 36 | +public: |
| 37 | + ArgsToFrontendOutputsConverter(const llvm::opt::ArgList &args, |
| 38 | + StringRef moduleName, |
| 39 | + FrontendInputsAndOutputs &inputsAndOutputs, |
| 40 | + DiagnosticEngine &diags) |
| 41 | + : Args(args), ModuleName(moduleName), InputsAndOutputs(inputsAndOutputs), |
| 42 | + Diags(diags) {} |
| 43 | + |
| 44 | + Optional<std::vector<std::string>> convert(); |
| 45 | + |
| 46 | + /// Try to read an output file list file. |
| 47 | + /// \returns `None` if it could not open the filelist. |
| 48 | + static Optional<std::vector<std::string>> |
| 49 | + readOutputFileList(StringRef filelistPath, DiagnosticEngine &diags); |
| 50 | +}; |
| 51 | + |
| 52 | +class OutputFilesComputer { |
| 53 | + const llvm::opt::ArgList &Args; |
| 54 | + DiagnosticEngine &Diags; |
| 55 | + const FrontendInputsAndOutputs &InputsAndOutputs; |
| 56 | + const std::vector<std::string> OutputFileArguments; |
| 57 | + const std::string OutputDirectoryArgument; |
| 58 | + const StringRef FirstInput; |
| 59 | + const FrontendOptions::ActionType RequestedAction; |
| 60 | + const llvm::opt::Arg *const ModuleNameArg; |
| 61 | + const StringRef Suffix; |
| 62 | + const bool HasTextualOutput; |
| 63 | + |
| 64 | + OutputFilesComputer(const llvm::opt::ArgList &args, DiagnosticEngine &diags, |
| 65 | + const FrontendInputsAndOutputs &inputsAndOutputs, |
| 66 | + std::vector<std::string> outputFileArguments, |
| 67 | + StringRef outputDirectoryArgument, StringRef firstInput, |
| 68 | + FrontendOptions::ActionType requestedAction, |
| 69 | + const llvm::opt::Arg *moduleNameArg, StringRef suffix, |
| 70 | + bool hasTextualOutput); |
| 71 | + |
| 72 | +public: |
| 73 | + static Optional<OutputFilesComputer> |
| 74 | + create(const llvm::opt::ArgList &args, DiagnosticEngine &diags, |
| 75 | + const FrontendInputsAndOutputs &inputsAndOutputs); |
| 76 | + |
| 77 | + /// \return the output filenames on the command line or in the output |
| 78 | + /// filelist. If there |
| 79 | + /// were neither -o's nor an output filelist, returns an empty vector. |
| 80 | + static Optional<std::vector<std::string>> |
| 81 | + getOutputFilenamesFromCommandLineOrFilelist(const llvm::opt::ArgList &args, |
| 82 | + DiagnosticEngine &diags); |
| 83 | + |
| 84 | + Optional<std::vector<std::string>> computeOutputFiles() const; |
| 85 | + |
| 86 | +private: |
| 87 | + Optional<std::string> computeOutputFile(StringRef outputArg, |
| 88 | + const InputFile &input) const; |
| 89 | + |
| 90 | + /// \return the correct output filename when none was specified. |
| 91 | + /// |
| 92 | + /// Such an absence should only occur when invoking the frontend |
| 93 | + /// without the driver, |
| 94 | + /// because the driver will always pass -o with an appropriate filename |
| 95 | + /// if output is required for the requested action. |
| 96 | + Optional<std::string> deriveOutputFileFromInput(const InputFile &input) const; |
| 97 | + |
| 98 | + /// \return the correct output filename when a directory was specified. |
| 99 | + /// |
| 100 | + /// Such a specification should only occur when invoking the frontend |
| 101 | + /// directly, because the driver will always pass -o with an appropriate |
| 102 | + /// filename if output is required for the requested action. |
| 103 | + Optional<std::string> |
| 104 | + deriveOutputFileForDirectory(const InputFile &input) const; |
| 105 | + |
| 106 | + std::string determineBaseNameOfOutput(const InputFile &input) const; |
| 107 | + |
| 108 | + std::string deriveOutputFileFromParts(StringRef dir, StringRef base) const; |
| 109 | +}; |
| 110 | + |
| 111 | +} // namespace swift |
| 112 | + |
| 113 | +#endif /* SWIFT_FRONTEND_ARGSTOFRONTENDOUTPUTSCONVERTER_H */ |
0 commit comments