Skip to content

Consistently get extensions from FileTypes.h #18203

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 5 commits into from
Jul 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TYPE("swiftinterface", SwiftModuleInterfaceFile, "swiftinterface", "")
TYPE("assembly", Assembly, "s", "")
TYPE("raw-sil", RawSIL, "sil", "")
TYPE("raw-sib", RawSIB, "sib", "")
TYPE("llvm-ir", LLVM_IR, "ir", "")
TYPE("llvm-ir", LLVM_IR, "ll", "")
TYPE("llvm-bc", LLVM_BC, "bc", "")
TYPE("diagnostics", SerializedDiagnostics, "dia", "")
TYPE("objc-header", ObjCHeader, "h", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
//===--- FileTypes.h - Input & Temporary Driver Types -----------*- C++ -*-===//
//===--- FileTypes.h - Input & output formats used by the tools -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// 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_FILETYPES_H
#define SWIFT_FRONTEND_FILETYPES_H
#ifndef SWIFT_BASIC_FILETYPES_H
#define SWIFT_BASIC_FILETYPES_H

#include "swift/Basic/LLVM.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include <functional>

namespace swift {
namespace file_types {
enum ID : uint8_t {
#define TYPE(NAME, ID, TEMP_SUFFIX, FLAGS) TY_##ID,
#include "swift/Frontend/Types.def"
#define TYPE(NAME, ID, EXTENSION, FLAGS) TY_##ID,
#include "swift/Basic/FileTypes.def"
#undef TYPE
TY_INVALID
};

/// Return the name of the type for \p Id.
StringRef getTypeName(ID Id);

/// Return the suffix to use when creating a temp file of this type,
/// or null if unspecified.
StringRef getTypeTempSuffix(ID Id);
/// Return the extension to use when creating a file of this type,
/// or an empty string if unspecified.
StringRef getExtension(ID Id);

/// Lookup the type to use for the file extension \p Ext.
/// If the extension is empty or is otherwise not recognized, return
Expand All @@ -58,8 +58,12 @@ bool isAfterLLVM(ID Id);
/// These need to be passed to the Swift frontend
bool isPartOfSwiftCompilation(ID Id);

template <typename Fn> void forAllTypes(const Fn &fn);
} // namespace file_types
static inline void forAllTypes(llvm::function_ref<void(file_types::ID)> fn) {
for (uint8_t i = 0; i < static_cast<uint8_t>(TY_INVALID); ++i)
fn(static_cast<ID>(i));
}

} // end namespace file_types
} // end namespace swift

namespace llvm {
Expand All @@ -72,14 +76,6 @@ template <> struct DenseMapInfo<swift::file_types::ID> {
static unsigned getHashValue(ID Val) { return (unsigned)Val * 37U; }
static bool isEqual(ID LHS, ID RHS) { return LHS == RHS; }
};
} // namespace llvm

template <typename Fn> void swift::file_types::forAllTypes(const Fn &fn) {
static_assert(
std::is_constructible<std::function<void(file_types::ID)>, Fn>::value,
"must have the signature 'void(file_types::ID)'");
for (uint8_t i = 0; i < static_cast<uint8_t>(TY_INVALID); ++i)
fn(static_cast<ID>(i));
}
} // end namespace llvm

#endif
2 changes: 1 addition & 1 deletion include/swift/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#ifndef SWIFT_DRIVER_ACTION_H
#define SWIFT_DRIVER_ACTION_H

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Driver/Util.h"
#include "swift/Frontend/FileTypes.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSwitch.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#define SWIFT_DRIVER_DRIVER_H

#include "swift/AST/IRGenOptions.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/Sanitizers.h"
#include "swift/Driver/Util.h"
#include "swift/Frontend/FileTypes.h"
#include "swift/Frontend/OutputFileMap.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#ifndef SWIFT_DRIVER_JOB_H
#define SWIFT_DRIVER_JOB_H

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Driver/Action.h"
#include "swift/Driver/Util.h"
#include "swift/Frontend/FileTypes.h"
#include "swift/Frontend/OutputFileMap.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/PrettyStackTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef SWIFT_DRIVER_PRETTYSTACKTRACE_H
#define SWIFT_DRIVER_PRETTYSTACKTRACE_H

#include "swift/Frontend/FileTypes.h"
#include "swift/Basic/FileTypes.h"
#include "llvm/Support/PrettyStackTrace.h"

namespace swift {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#ifndef SWIFT_DRIVER_TOOLCHAIN_H
#define SWIFT_DRIVER_TOOLCHAIN_H

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Driver/Action.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/FileTypes.h"
#include "swift/Option/Options.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Option/Option.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#ifndef SWIFT_DRIVER_UTIL_H
#define SWIFT_DRIVER_UTIL_H

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Frontend/FileTypes.h"
#include "llvm/ADT/SmallVector.h"

namespace llvm {
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SWIFT_FRONTEND_FRONTENDOPTIONS_H
#define SWIFT_FRONTEND_FRONTENDOPTIONS_H

#include "swift/Basic/FileTypes.h"
#include "swift/Frontend/FrontendInputsAndOutputs.h"
#include "swift/Frontend/InputFile.h"
#include "llvm/ADT/Hashing.h"
Expand Down Expand Up @@ -312,7 +313,7 @@ class FrontendOptions {
static bool doesActionProduceOutput(ActionType);
static bool doesActionProduceTextualOutput(ActionType);
static bool needsProperModuleName(ActionType);
static StringRef suffixForPrincipalOutputFileForAction(ActionType);
static file_types::ID formatForPrincipalOutputFileForAction(ActionType);
};

}
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Frontend/OutputFileMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#ifndef SWIFT_DRIVER_OUTPUTFILEMAP_H
#define SWIFT_DRIVER_OUTPUTFILEMAP_H

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Frontend/FileTypes.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
Expand Down
15 changes: 0 additions & 15 deletions include/swift/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@

namespace swift {

/// The extension for serialized modules.
constexpr static const char SERIALIZED_MODULE_EXTENSION[] = "swiftmodule";
/// The extension for serialized documentation comments.
constexpr static const char SERIALIZED_MODULE_DOC_EXTENSION[] = "swiftdoc";
/// The extension for PCH files.
constexpr static const char PCH_EXTENSION[] = "pch";
/// The extension for replacement map files.
constexpr static const char REMAP_EXTENSION[] = "remap";
/// The extension for SIL files.
constexpr static const char SIL_EXTENSION[] = "sil";
/// The extension for SIB files.
constexpr static const char SIB_EXTENSION[] = "sib";
/// The extension for LLVM IR files.
constexpr static const char LLVM_BC_EXTENSION[] = "bc";
constexpr static const char LLVM_IR_EXTENSION[] = "ll";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W.r.t. the asymmetry comment above, another option would be to keep these here and use all of these consts in the TYPE definitions. Why one vs the other? (I'll bet you have a good reason.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an okay reason. :-/ The remaining constants here are ones that are used in libraries below Frontend (in layering terms). I'd prefer not to put them all here because I think the file_types abstractions are better, but it does make an ugly asymmetry here.

Another option would be to go a step further and sink the information in file_types down to Basic, which means they could be included from anywhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that option: sinking the info down to Basic! It's a nice unification which I suspect will pay off in the future!

/// The name of the standard library, which is a reserved module name.
constexpr static const char STDLIB_NAME[] = "Swift";
/// The name of the Onone support library, which is a reserved module name.
Expand Down
1 change: 1 addition & 0 deletions lib/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ add_swift_library(swiftBasic STATIC
Edit.cpp
EditorPlaceholder.cpp
FileSystem.cpp
FileTypes.cpp
JSONSerialization.cpp
LangOptions.cpp
LLVMContext.cpp
Expand Down
31 changes: 17 additions & 14 deletions lib/Frontend/Types.cpp → lib/Basic/FileTypes.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
//===--- Types.cpp - Driver input & temporary type information ------------===//
//===--- FileTypes.cpp - Input & output formats used by the tools ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// 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/FileTypes.h"
#include "swift/Basic/FileTypes.h"

#include "swift/Strings.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"

using namespace swift;
using namespace swift::file_types;

namespace {
struct TypeInfo {
const char *Name;
const char *Flags;
const char *TempSuffix;
const char *Extension;
};
} // end anonymous namespace

static const TypeInfo TypeInfos[] = {
#define TYPE(NAME, ID, TEMP_SUFFIX, FLAGS) \
{ NAME, FLAGS, TEMP_SUFFIX },
#include "swift/Frontend/Types.def"
#define TYPE(NAME, ID, EXTENSION, FLAGS) \
{ NAME, FLAGS, EXTENSION },
#include "swift/Basic/FileTypes.def"
};

static const TypeInfo &getInfo(unsigned Id) {
Expand All @@ -38,26 +41,26 @@ static const TypeInfo &getInfo(unsigned Id) {

StringRef file_types::getTypeName(ID Id) { return getInfo(Id).Name; }

StringRef file_types::getTypeTempSuffix(ID Id) {
return getInfo(Id).TempSuffix;
StringRef file_types::getExtension(ID Id) {
return getInfo(Id).Extension;
}

ID file_types::lookupTypeForExtension(StringRef Ext) {
if (Ext.empty())
return TY_INVALID;
assert(Ext.front() == '.' && "not a file extension");
return llvm::StringSwitch<file_types::ID>(Ext.drop_front())
#define TYPE(NAME, ID, SUFFIX, FLAGS) \
.Case(SUFFIX, TY_##ID)
#include "swift/Frontend/Types.def"
#define TYPE(NAME, ID, EXTENSION, FLAGS) \
.Case(EXTENSION, TY_##ID)
#include "swift/Basic/FileTypes.def"
.Default(TY_INVALID);
}

ID file_types::lookupTypeForName(StringRef Name) {
return llvm::StringSwitch<file_types::ID>(Name)
#define TYPE(NAME, ID, SUFFIX, FLAGS) \
#define TYPE(NAME, ID, EXTENSION, FLAGS) \
.Case(NAME, TY_##ID)
#include "swift/Frontend/Types.def"
#include "swift/Basic/FileTypes.def"
.Default(TY_INVALID);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "swift/Parse/Lexer.h"
#include "swift/Parse/Parser.h"
#include "swift/Config.h"
#include "swift/Strings.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Mangle.h"
#include "clang/Basic/CharInfo.h"
Expand Down Expand Up @@ -419,7 +418,7 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
auto languageVersion = ctx.LangOpts.EffectiveLanguageVersion;

if (llvm::sys::path::extension(importerOpts.BridgingHeader)
.endswith(PCH_EXTENSION)) {
.endswith(file_types::getExtension(file_types::TY_PCH))) {
invocationArgStrs.insert(invocationArgStrs.end(), {
"-include-pch", importerOpts.BridgingHeader
});
Expand Down Expand Up @@ -788,7 +787,7 @@ Optional<std::string>
ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
StringRef SwiftPCHHash, bool &isExplicit) {
if (llvm::sys::path::extension(ImporterOptions.BridgingHeader)
.endswith(PCH_EXTENSION)) {
.endswith(file_types::getExtension(file_types::TY_PCH))) {
isExplicit = true;
return ImporterOptions.BridgingHeader;
}
Expand Down Expand Up @@ -879,8 +878,8 @@ ClangImporter::create(ASTContext &ctx,
for (auto &argStr : invocationArgStrs)
invocationArgs.push_back(argStr.c_str());

if (llvm::sys::path::extension(importerOpts.BridgingHeader).endswith(
PCH_EXTENSION)) {
if (llvm::sys::path::extension(importerOpts.BridgingHeader)
.endswith(file_types::getExtension(file_types::TY_PCH))) {
importer->Impl.setSinglePCHImport(importerOpts.BridgingHeader);
importer->Impl.IsReadingBridgingPCH = true;
if (tracker) {
Expand Down Expand Up @@ -1289,7 +1288,8 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
SourceLoc diagLoc,
bool trackParsedSymbols,
bool implicitImport) {
if (llvm::sys::path::extension(header).endswith(PCH_EXTENSION)) {
if (llvm::sys::path::extension(header)
.endswith(file_types::getExtension(file_types::TY_PCH))) {
Impl.ImportedHeaderOwners.push_back(adapter);
// We already imported this with -include-pch above, so we should have
// collected a bunch of PCH-encoded module imports that we just need to
Expand Down
4 changes: 2 additions & 2 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#include "swift/AST/Module.h"
#include "swift/AST/Type.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Strings.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/Basic/IdentifierTable.h"
Expand Down Expand Up @@ -1360,7 +1360,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
void setSinglePCHImport(Optional<std::string> PCHFilename) {
if (PCHFilename.hasValue()) {
assert(llvm::sys::path::extension(PCHFilename.getValue())
.endswith(PCH_EXTENSION) &&
.endswith(file_types::getExtension(file_types::TY_PCH)) &&
"Single PCH imported filename doesn't have .pch extension!");
}
SinglePCHImport = PCHFilename;
Expand Down
Loading