Skip to content

[Batch mode] Move various Frontend pieces into dedicated files. #14001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions include/swift/Frontend/ArgsToFrontendInputsConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//===--- ArgsToFrontendInputsConverter.h ------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_FRONTEND_ARGSTOFRONTENDINPUTSCONVERTER_H
#define SWIFT_FRONTEND_ARGSTOFRONTENDINPUTSCONVERTER_H

#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Frontend/FrontendOptions.h"
#include "llvm/Option/ArgList.h"

namespace swift {

/// Implement argument semantics in a way that will make it easier to have
/// >1 primary file (or even a primary file list) in the future without
/// breaking anything today.
///
/// Semantics today:
/// If input files are on command line, primary files on command line are also
/// input files; they are not repeated without -primary-file. If input files are
/// in a file list, the primary files on the command line are repeated in the
/// file list. Thus, if there are any primary files, it is illegal to have both
/// (non-primary) input files and a file list. Finally, the order of input files
/// must match the order given on the command line or the file list.
///
/// Side note:
/// since each input file will cause a lot of work for the compiler, this code
/// is biased towards clarity and not optimized.
/// In the near future, it will be possible to put primary files in the
/// filelist, or to have a separate filelist for primaries. The organization
/// here anticipates that evolution.

class ArgsToFrontendInputsConverter {
DiagnosticEngine &Diags;
const llvm::opt::ArgList &Args;
FrontendInputs &Inputs;

llvm::opt::Arg const *const FilelistPathArg;
llvm::opt::Arg const *const PrimaryFilelistPathArg;

SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> BuffersToKeepAlive;

llvm::SetVector<StringRef> Files;

public:
ArgsToFrontendInputsConverter(DiagnosticEngine &diags,
const llvm::opt::ArgList &args,
FrontendInputs &inputs);

bool convert();

private:
bool enforceFilelistExclusion();
bool readInputFilesFromCommandLine();
bool readInputFilesFromFilelist();
bool forAllFilesInFilelist(llvm::opt::Arg const *const pathArg,
llvm::function_ref<void(StringRef)> fn);
bool addFile(StringRef file);
Optional<std::set<StringRef>> readPrimaryFiles();
std::set<StringRef>
createInputFilesConsumingPrimaries(std::set<StringRef> primaryFiles);
bool checkForMissingPrimaryFiles(std::set<StringRef> primaryFiles);
};

} // namespace swift

#endif /* SWIFT_FRONTEND_ARGSTOFRONTENDINPUTSCONVERTER_H */
98 changes: 98 additions & 0 deletions include/swift/Frontend/ArgsToFrontendOptionsConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//===------ArgsToFrontendOptionsConverter.h-- --------------------*-C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_FRONTEND_ARGSTOFRONTENDOPTIONSCONVERTER_H
#define SWIFT_FRONTEND_ARGSTOFRONTENDOPTIONSCONVERTER_H

#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Frontend/FrontendOptions.h"
#include "swift/Option/Options.h"
#include "llvm/Option/ArgList.h"

#include <vector>

namespace swift {

class ArgsToFrontendOptionsConverter {
private:
DiagnosticEngine &Diags;
const llvm::opt::ArgList &Args;
FrontendOptions &Opts;

Optional<const std::vector<std::string>>
cachedOutputFilenamesFromCommandLineOrFilelist;

void handleDebugCrashGroupArguments();

void computeDebugTimeOptions();
bool computeFallbackModuleName();
bool computeModuleName();
bool computeOutputFilenames();
void computeDumpScopeMapLocations();
void computeHelpOptions();
void computeImplicitImportModuleNames();
void computeImportObjCHeaderOptions();
void computeLLVMArgs();
void computePlaygroundOptions();
void computePrintStatsOptions();
void computeTBDOptions();

void setUnsignedIntegerArgument(options::ID optionID, unsigned max,
unsigned &valueToSet);

FrontendOptions::ActionType determineRequestedAction() const;

bool setUpForSILOrLLVM();

/// Determine the correct output filename when none was specified.
///
/// Such an absence should only occur when invoking the frontend
/// without the driver,
/// because the driver will always pass -o with an appropriate filename
/// if output is required for the requested action.
bool deriveOutputFilenameFromInputFile();

/// Determine the correct output filename when a directory was specified.
///
/// Such a specification should only occur when invoking the frontend
/// directly, because the driver will always pass -o with an appropriate
/// filename if output is required for the requested action.
bool deriveOutputFilenameForDirectory(StringRef outputDir);

std::string determineBaseNameOfOutput() const;

void deriveOutputFilenameFromParts(StringRef dir, StringRef base);

void determineSupplementaryOutputFilenames();

/// Returns the output filenames on the command line or in the output
/// filelist. If there
/// were neither -o's nor an output filelist, returns an empty vector.
ArrayRef<std::string> getOutputFilenamesFromCommandLineOrFilelist();

bool checkForUnusedOutputPaths() const;

std::vector<std::string> readOutputFileList(StringRef filelistPath) const;

public:
ArgsToFrontendOptionsConverter(DiagnosticEngine &Diags,
const llvm::opt::ArgList &Args,
FrontendOptions &Opts)
: Diags(Diags), Args(Args), Opts(Opts) {}

bool convert();
};

} // namespace swift

#endif /* SWIFT_FRONTEND_ARGSTOFRONTENDOPTIONSCONVERTER_H */
127 changes: 127 additions & 0 deletions include/swift/Frontend/FrontendInputs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//===--- FrontendInputs.h ---------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_FRONTEND_FRONTENDINPUTS_H
#define SWIFT_FRONTEND_FRONTENDINPUTS_H

#include "swift/AST/Module.h"
#include "swift/Frontend/FrontendInputs.h"
#include "swift/Frontend/InputFile.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/MapVector.h"

#include <string>
#include <vector>

namespace llvm {
class MemoryBuffer;
}

namespace swift {

/// Information about all the inputs to the frontend.
class FrontendInputs {
friend class ArgsToFrontendInputsConverter;

std::vector<InputFile> AllFiles;
typedef llvm::StringMap<unsigned> InputFileMap;
InputFileMap PrimaryInputs;

public:
FrontendInputs() = default;

FrontendInputs(const FrontendInputs &other);

FrontendInputs &operator=(const FrontendInputs &other);

// Readers:

ArrayRef<InputFile> getAllFiles() const { return AllFiles; }

std::vector<std::string> getInputFilenames() const;

unsigned inputCount() const { return getAllFiles().size(); }

bool hasInputs() const { return !AllFiles.empty(); }

bool hasSingleInput() const { return inputCount() == 1; }

StringRef getFilenameOfFirstInput() const;

bool isReadingFromStdin() const;

// If we have exactly one input filename, and its extension is "bc" or "ll",
// treat the input as LLVM_IR.
bool shouldTreatAsLLVM() const;

// Primary input readers

private:
void assertMustNotBeMoreThanOnePrimaryInput() const;

bool areAllNonPrimariesSIB() const;

public:
unsigned primaryInputCount() const { return PrimaryInputs.size(); }

// Primary count readers:

bool hasUniquePrimaryInput() const { return primaryInputCount() == 1; }

bool hasPrimaryInputs() const { return primaryInputCount() > 0; }

bool isWholeModule() const { return !hasPrimaryInputs(); }

// Count-dependend readers:

/// \return the unique primary input, if one exists.
const InputFile *getUniquePrimaryInput() const;

const InputFile &getRequiredUniquePrimaryInput() const;

/// \return the name of the unique primary input, or an empty StrinRef if
/// there isn't one.
StringRef getNameOfUniquePrimaryInputFile() const;

bool isFilePrimary(StringRef file) const;

unsigned numberOfPrimaryInputsEndingWith(const char *extension) const;

// Multi-facet readers

bool shouldTreatAsSIL() const;

/// \return true for error
bool verifyInputs(DiagnosticEngine &diags, bool treatAsSIL,
bool isREPLRequested, bool isNoneRequested) const;

// Writers

void addInputFile(StringRef file, llvm::MemoryBuffer *buffer = nullptr) {
addInput(InputFile(file, false, buffer));
}
void addPrimaryInputFile(StringRef file,
llvm::MemoryBuffer *buffer = nullptr) {
addInput(InputFile(file, true, buffer));
}

void addInput(const InputFile &input);

void clearInputs() {
AllFiles.clear();
PrimaryInputs.clear();
}
};

} // namespace swift

#endif /* SWIFT_FRONTEND_FRONTENDINPUTS_H */
Loading