Move FrontendInputs into separate .h and .cpp files.

This commit is contained in:
David Ungar
2018-01-17 20:19:15 -08:00
parent 4ac390d24d
commit 361ff7244c
6 changed files with 308 additions and 218 deletions

View File

@@ -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 */

View File

@@ -14,6 +14,7 @@
#define SWIFT_FRONTEND_FRONTENDOPTIONS_H #define SWIFT_FRONTEND_FRONTENDOPTIONS_H
#include "swift/AST/Module.h" #include "swift/AST/Module.h"
#include "swift/Frontend/FrontendInputs.h"
#include "swift/Frontend/InputFile.h" #include "swift/Frontend/InputFile.h"
#include "llvm/ADT/Hashing.h" #include "llvm/ADT/Hashing.h"
@@ -26,142 +27,6 @@ namespace llvm {
namespace swift { 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) {
for (InputFile input : other.getAllFiles())
addInput(input);
}
FrontendInputs &operator=(const FrontendInputs &other) {
clearInputs();
for (InputFile input : other.getAllFiles())
addInput(input);
return *this;
}
// Readers:
ArrayRef<InputFile> getAllFiles() const { return AllFiles; }
std::vector<std::string> getInputFilenames() const {
std::vector<std::string> filenames;
for (auto &input : getAllFiles()) {
filenames.push_back(input.file());
}
return filenames;
}
unsigned inputCount() const { return getAllFiles().size(); }
bool hasInputs() const { return !AllFiles.empty(); }
bool hasSingleInput() const { return inputCount() == 1; }
StringRef getFilenameOfFirstInput() const {
assert(hasInputs());
const InputFile &inp = getAllFiles()[0];
StringRef f = inp.file();
assert(!f.empty());
return f;
}
bool isReadingFromStdin() const {
return hasSingleInput() && getFilenameOfFirstInput() == "-";
}
// 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 {
assert(primaryInputCount() < 2 &&
"have not implemented >1 primary input yet");
}
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 {
assertMustNotBeMoreThanOnePrimaryInput();
const auto b = PrimaryInputs.begin();
return b == PrimaryInputs.end() ? nullptr : &AllFiles[b->second];
}
const InputFile &getRequiredUniquePrimaryInput() const {
if (const auto *input = getUniquePrimaryInput())
return *input;
llvm_unreachable("No primary when one is required");
}
/// Return the name of the unique primary input, or an empty StrinRef if there
/// isn't one.
StringRef getNameOfUniquePrimaryInputFile() const {
const auto *input = getUniquePrimaryInput();
return input == nullptr ? StringRef() : input->file();
}
bool isFilePrimary(StringRef file) {
auto iterator = PrimaryInputs.find(file);
return iterator != PrimaryInputs.end() &&
AllFiles[iterator->second].isPrimary();
}
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) {
if (!input.file().empty() && input.isPrimary())
PrimaryInputs.insert(std::make_pair(input.file(), AllFiles.size()));
AllFiles.push_back(input);
}
void clearInputs() {
AllFiles.clear();
PrimaryInputs.clear();
}
};
/// Options for controlling the behavior of the frontend. /// Options for controlling the behavior of the frontend.
class FrontendOptions { class FrontendOptions {
@@ -454,8 +319,6 @@ public:
} }
private: private:
static const char *suffixForPrincipalOutputFileForAction(ActionType);
bool hasUnusedDependenciesFilePath() const; bool hasUnusedDependenciesFilePath() const;
static bool canActionEmitDependencies(ActionType); static bool canActionEmitDependencies(ActionType);
bool hasUnusedObjCHeaderOutputPath() const; bool hasUnusedObjCHeaderOutputPath() const;
@@ -467,9 +330,11 @@ private:
bool hasUnusedModuleDocOutputPath() const; bool hasUnusedModuleDocOutputPath() const;
static bool canActionEmitModuleDoc(ActionType); static bool canActionEmitModuleDoc(ActionType);
public:
static bool doesActionProduceOutput(ActionType); static bool doesActionProduceOutput(ActionType);
static bool doesActionProduceTextualOutput(ActionType); static bool doesActionProduceTextualOutput(ActionType);
static bool needsProperModuleName(ActionType); static bool needsProperModuleName(ActionType);
static const char *suffixForPrincipalOutputFileForAction(ActionType);
}; };
} }

View File

@@ -2,6 +2,7 @@ add_swift_library(swiftFrontend STATIC
CompilerInvocation.cpp CompilerInvocation.cpp
DiagnosticVerifier.cpp DiagnosticVerifier.cpp
Frontend.cpp Frontend.cpp
FrontendInputs.cpp
FrontendOptions.cpp FrontendOptions.cpp
PrintingDiagnosticConsumer.cpp PrintingDiagnosticConsumer.cpp
SerializedDiagnosticConsumer.cpp SerializedDiagnosticConsumer.cpp

View File

@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/Frontend/Frontend.h" #include "swift/Frontend/Frontend.h"
#include "swift/AST/DiagnosticsFrontend.h" #include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/Platform.h" #include "swift/Basic/Platform.h"
#include "swift/Option/Options.h" #include "swift/Option/Options.h"

View File

@@ -0,0 +1,176 @@
//===--- FrontendInputs.cpp -------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
#include "swift/Frontend/FrontendInputs.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Frontend/FrontendOptions.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Option/Options.h"
#include "swift/Parse/Lexer.h"
#include "swift/Strings.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/Path.h"
using namespace swift;
using namespace llvm::opt;
FrontendInputs::FrontendInputs(const FrontendInputs &other) {
for (InputFile input : other.getAllFiles())
addInput(input);
}
FrontendInputs &FrontendInputs::operator=(const FrontendInputs &other) {
clearInputs();
for (InputFile input : other.getAllFiles())
addInput(input);
return *this;
}
std::vector<std::string> FrontendInputs::getInputFilenames() const {
std::vector<std::string> filenames;
for (auto &input : getAllFiles()) {
filenames.push_back(input.file());
}
return filenames;
}
bool FrontendInputs::isReadingFromStdin() const {
return hasSingleInput() && getFilenameOfFirstInput() == "-";
}
void FrontendInputs::assertMustNotBeMoreThanOnePrimaryInput() const {
assert(primaryInputCount() < 2 &&
"have not implemented >1 primary input yet");
}
const InputFile *FrontendInputs::getUniquePrimaryInput() const {
assertMustNotBeMoreThanOnePrimaryInput();
const auto b = PrimaryInputs.begin();
return b == PrimaryInputs.end() ? nullptr : &AllFiles[b->second];
}
const InputFile &FrontendInputs::getRequiredUniquePrimaryInput() const {
if (const auto *input = getUniquePrimaryInput())
return *input;
llvm_unreachable("No primary when one is required");
}
StringRef FrontendInputs::getNameOfUniquePrimaryInputFile() const {
const auto *input = getUniquePrimaryInput();
return input == nullptr ? StringRef() : input->file();
}
bool FrontendInputs::isFilePrimary(StringRef file) const {
auto iterator = PrimaryInputs.find(file);
return iterator != PrimaryInputs.end() &&
AllFiles[iterator->second].isPrimary();
}
StringRef FrontendInputs::getFilenameOfFirstInput() const {
assert(hasInputs());
const InputFile &inp = getAllFiles()[0];
StringRef f = inp.file();
assert(!f.empty());
return f;
}
bool FrontendInputs::shouldTreatAsLLVM() const {
if (hasSingleInput()) {
StringRef Input(getFilenameOfFirstInput());
return llvm::sys::path::extension(Input).endswith(LLVM_BC_EXTENSION) ||
llvm::sys::path::extension(Input).endswith(LLVM_IR_EXTENSION);
}
return false;
}
bool FrontendInputs::shouldTreatAsSIL() const {
if (hasSingleInput()) {
// If we have exactly one input filename, and its extension is "sil",
// treat the input as SIL.
StringRef Input(getFilenameOfFirstInput());
return llvm::sys::path::extension(Input).endswith(SIL_EXTENSION);
}
// If we have one primary input and it's a filename with extension "sil",
// treat the input as SIL.
unsigned silPrimaryCount = numberOfPrimaryInputsEndingWith(SIL_EXTENSION);
if (silPrimaryCount == 0)
return false;
if (silPrimaryCount == primaryInputCount()) {
// Not clear what to do someday with multiple primaries
assertMustNotBeMoreThanOnePrimaryInput();
return true;
}
llvm_unreachable("Either all primaries or none must end with .sil");
}
void FrontendInputs::addInput(const InputFile &input) {
if (!input.file().empty() && input.isPrimary())
PrimaryInputs.insert(std::make_pair(input.file(), AllFiles.size()));
AllFiles.push_back(input);
}
unsigned
FrontendInputs::numberOfPrimaryInputsEndingWith(const char *extension) const {
return count_if(
PrimaryInputs, [&](const llvm::StringMapEntry<unsigned> &elem) -> bool {
StringRef filename = AllFiles[elem.second].file();
return llvm::sys::path::extension(filename).endswith(extension);
});
}
bool FrontendInputs::verifyInputs(DiagnosticEngine &diags, bool treatAsSIL,
bool isREPLRequested,
bool isNoneRequested) const {
if (isREPLRequested) {
if (hasInputs()) {
diags.diagnose(SourceLoc(), diag::error_repl_requires_no_input_files);
return true;
}
} else if (treatAsSIL) {
if (isWholeModule()) {
if (inputCount() != 1) {
diags.diagnose(SourceLoc(), diag::error_mode_requires_one_input_file);
return true;
}
} else {
assertMustNotBeMoreThanOnePrimaryInput();
// If we have the SIL as our primary input, we can waive the one file
// requirement as long as all the other inputs are SIBs.
if (!areAllNonPrimariesSIB()) {
diags.diagnose(SourceLoc(),
diag::error_mode_requires_one_sil_multi_sib);
return true;
}
}
} else if (!isNoneRequested && !hasInputs()) {
diags.diagnose(SourceLoc(), diag::error_mode_requires_an_input_file);
return true;
}
return false;
}
bool FrontendInputs::areAllNonPrimariesSIB() const {
for (const InputFile &input : getAllFiles()) {
if (input.isPrimary())
continue;
if (!llvm::sys::path::extension(input.file()).endswith(SIB_EXTENSION)) {
return false;
}
}
return true;
}

View File

@@ -27,86 +27,6 @@
using namespace swift; using namespace swift;
using namespace llvm::opt; using namespace llvm::opt;
bool FrontendInputs::shouldTreatAsLLVM() const {
if (hasSingleInput()) {
StringRef Input(getFilenameOfFirstInput());
return llvm::sys::path::extension(Input).endswith(LLVM_BC_EXTENSION) ||
llvm::sys::path::extension(Input).endswith(LLVM_IR_EXTENSION);
}
return false;
}
bool FrontendInputs::shouldTreatAsSIL() const {
if (hasSingleInput()) {
// If we have exactly one input filename, and its extension is "sil",
// treat the input as SIL.
StringRef Input(getFilenameOfFirstInput());
return llvm::sys::path::extension(Input).endswith(SIL_EXTENSION);
}
// If we have one primary input and it's a filename with extension "sil",
// treat the input as SIL.
unsigned silPrimaryCount = numberOfPrimaryInputsEndingWith(SIL_EXTENSION);
if (silPrimaryCount == 0)
return false;
if (silPrimaryCount == primaryInputCount()) {
// Not clear what to do someday with multiple primaries
assertMustNotBeMoreThanOnePrimaryInput();
return true;
}
llvm_unreachable("Either all primaries or none must end with .sil");
}
unsigned
FrontendInputs::numberOfPrimaryInputsEndingWith(const char *extension) const {
return count_if(
PrimaryInputs, [&](const llvm::StringMapEntry<unsigned> &elem) -> bool {
StringRef filename = AllFiles[elem.second].file();
return llvm::sys::path::extension(filename).endswith(extension);
});
}
bool FrontendInputs::verifyInputs(DiagnosticEngine &diags, bool treatAsSIL,
bool isREPLRequested,
bool isNoneRequested) const {
if (isREPLRequested) {
if (hasInputs()) {
diags.diagnose(SourceLoc(), diag::error_repl_requires_no_input_files);
return true;
}
} else if (treatAsSIL) {
if (isWholeModule()) {
if (inputCount() != 1) {
diags.diagnose(SourceLoc(), diag::error_mode_requires_one_input_file);
return true;
}
} else {
assertMustNotBeMoreThanOnePrimaryInput();
// If we have the SIL as our primary input, we can waive the one file
// requirement as long as all the other inputs are SIBs.
if (!areAllNonPrimariesSIB()) {
diags.diagnose(SourceLoc(),
diag::error_mode_requires_one_sil_multi_sib);
return true;
}
}
} else if (!isNoneRequested && !hasInputs()) {
diags.diagnose(SourceLoc(), diag::error_mode_requires_an_input_file);
return true;
}
return false;
}
bool FrontendInputs::areAllNonPrimariesSIB() const {
for (const InputFile &input : getAllFiles()) {
if (input.isPrimary())
continue;
if (!llvm::sys::path::extension(input.file()).endswith(SIB_EXTENSION)) {
return false;
}
}
return true;
}
bool FrontendOptions::needsProperModuleName(ActionType action) { bool FrontendOptions::needsProperModuleName(ActionType action) {
switch (action) { switch (action) {
case ActionType::NoneAction: case ActionType::NoneAction: