Consistently get extensions from swift/Basic/FileTypes.h (part 2)

Remove the last few literal extension strings from Strings.h in favor
of the file_types APIs, and use those APIs in a few more places.
This commit is contained in:
Jordan Rose
2018-07-25 21:12:43 -07:00
parent 67a6a4ff18
commit 97b152d71e
8 changed files with 51 additions and 55 deletions

View File

@@ -46,10 +46,8 @@ TYPE("object", Object, "o", "")
TYPE("dSYM", dSYM, "dSYM", "")
TYPE("dependencies", Dependencies, "d", "")
TYPE("autolink", AutolinkFile, "autolink", "")
TYPE("swiftmodule", SwiftModuleFile,
SERIALIZED_MODULE_EXTENSION, "")
TYPE("swiftdoc", SwiftModuleDocFile,
SERIALIZED_MODULE_DOC_EXTENSION, "")
TYPE("swiftmodule", SwiftModuleFile, "swiftmodule", "")
TYPE("swiftdoc", SwiftModuleDocFile, "swiftdoc", "")
TYPE("swiftinterface", SwiftModuleInterfaceFile, "swiftinterface", "")
TYPE("assembly", Assembly, "s", "")
TYPE("raw-sil", RawSIL, "sil", "")
@@ -68,7 +66,7 @@ TYPE("opt-record", OptRecord, "opt.yaml", "")
// Misc types
TYPE("pcm", ClangModuleFile, "pcm", "")
TYPE("pch", PCH, PCH_EXTENSION, "")
TYPE("pch", PCH, "pch", "")
TYPE("none", Nothing, "", "")
#undef TYPE

View File

@@ -18,12 +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 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.

View File

@@ -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"
@@ -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
});
@@ -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;
}
@@ -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) {
@@ -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

View File

@@ -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"
@@ -1360,7 +1360,7 @@ public:
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;

View File

@@ -2019,7 +2019,7 @@ static void formFilenameFromBaseAndExt(StringRef base, StringRef newExt,
static Optional<StringRef> getOutputFilenameFromPathArgOrAsTopLevel(
const OutputInfo &OI, const llvm::opt::DerivedArgList &Args,
llvm::opt::OptSpecifier PathArg, file_types::ID ExpectedOutputType,
bool TreatAsTopLevelOutput, StringRef workingDirectory, StringRef ext,
bool TreatAsTopLevelOutput, StringRef workingDirectory,
llvm::SmallString<128> &Buffer) {
if (const Arg *A = Args.getLastArg(PathArg))
return StringRef(A->getValue());
@@ -2033,13 +2033,17 @@ static Optional<StringRef> getOutputFilenameFromPathArgOrAsTopLevel(
Buffer = A->getValue();
llvm::sys::path::remove_filename(Buffer);
llvm::sys::path::append(Buffer, OI.ModuleName);
llvm::sys::path::replace_extension(Buffer, ext);
llvm::sys::path::replace_extension(
Buffer, file_types::getExtension(ExpectedOutputType));
return Buffer.str();
}
// A top-level output wasn't specified, so just output to
// <ModuleName>.<ext>.
formFilenameFromBaseAndExt(OI.ModuleName, ext, workingDirectory, Buffer);
formFilenameFromBaseAndExt(OI.ModuleName,
file_types::getExtension(ExpectedOutputType),
workingDirectory,
Buffer);
return Buffer.str();
}
@@ -2118,8 +2122,7 @@ static StringRef getOutputFilename(Compilation &C,
if (isa<MergeModuleJobAction>(JA)) {
auto optFilename = getOutputFilenameFromPathArgOrAsTopLevel(
OI, Args, options::OPT_emit_module_path, file_types::TY_SwiftModuleFile,
OI.ShouldTreatModuleAsTopLevelOutput, workingDirectory,
SERIALIZED_MODULE_EXTENSION, Buffer);
OI.ShouldTreatModuleAsTopLevelOutput, workingDirectory, Buffer);
if (optFilename)
return *optFilename;
}
@@ -2627,17 +2630,16 @@ void Driver::chooseSwiftModuleOutputPath(Compilation &C, const OutputInfo &OI,
}
const Arg *A = C.getArgs().getLastArg(options::OPT_emit_module_path);
using file_types::TY_SwiftModuleFile;
if (!OFMModuleOutputPath.empty()) {
// Prefer a path from the OutputMap.
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile,
OFMModuleOutputPath);
Output->setAdditionalOutputForType(TY_SwiftModuleFile, OFMModuleOutputPath);
} else if (A && OI.CompilerMode == OutputInfo::Mode::SingleCompile) {
// We're performing a single compilation (and thus no merge module step),
// so prefer to use -emit-module-path, if present.
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile,
A->getValue());
} else if (Output->getPrimaryOutputType() == file_types::TY_SwiftModuleFile) {
Output->setAdditionalOutputForType(TY_SwiftModuleFile, A->getValue());
} else if (Output->getPrimaryOutputType() == TY_SwiftModuleFile) {
// If the primary type is already a module type, we're out of
// options for overriding the primary name choice: stop now.
assert(!Output->getPrimaryOutputFilename().empty());
@@ -2647,29 +2649,28 @@ void Driver::chooseSwiftModuleOutputPath(Compilation &C, const OutputInfo &OI,
// We're performing a single compile and don't have -emit-module-path,
// but have been told to treat the module as a top-level output.
// Determine an appropriate path.
llvm::SmallString<128> Path;
if (const Arg *A = C.getArgs().getLastArg(options::OPT_o)) {
// Put the module next to the top-level output.
llvm::SmallString<128> Path(A->getValue());
Path = A->getValue();
llvm::sys::path::remove_filename(Path);
llvm::sys::path::append(Path, OI.ModuleName);
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_EXTENSION);
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile, Path);
} else {
// A top-level output wasn't specified, so just output to
// <ModuleName>.swiftmodule.
llvm::SmallString<128> Path(OI.ModuleName);
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_EXTENSION);
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile, Path);
// <ModuleName>.swiftmodule in the current directory.
}
llvm::sys::path::append(Path, OI.ModuleName);
llvm::sys::path::replace_extension(
Path, file_types::getExtension(TY_SwiftModuleFile));
Output->setAdditionalOutputForType(TY_SwiftModuleFile, Path);
} else if (Output->getPrimaryOutputType() != file_types::TY_Nothing) {
// We're only generating the module as an intermediate, so put it next
// to the primary output of the compile command.
llvm::SmallString<128> Path(Output->getPrimaryOutputFilenames()[0]);
assert(!Path.empty());
bool isTempFile = C.isTemporaryFile(Path);
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_EXTENSION);
Output->setAdditionalOutputForType(file_types::ID::TY_SwiftModuleFile,
Path);
llvm::sys::path::replace_extension(
Path, file_types::getExtension(TY_SwiftModuleFile));
Output->setAdditionalOutputForType(TY_SwiftModuleFile, Path);
if (isTempFile)
C.addTemporaryFile(Path);
}
@@ -2698,7 +2699,8 @@ void Driver::chooseSwiftModuleDocOutputPath(Compilation &C,
llvm::SmallString<128> Path(
Output->getAnyOutputForType(file_types::TY_SwiftModuleFile));
bool isTempFile = C.isTemporaryFile(Path);
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_DOC_EXTENSION);
llvm::sys::path::replace_extension(
Path, file_types::getExtension(file_types::TY_SwiftModuleDocFile));
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleDocFile, Path);
if (isTempFile)
C.addTemporaryFile(Path);
@@ -2799,7 +2801,7 @@ void Driver::chooseLoadedModuleTracePath(Compilation &C, const OutputInfo &OI,
filename = *getOutputFilenameFromPathArgOrAsTopLevel(
OI, C.getArgs(), options::OPT_emit_loaded_module_trace_path,
file_types::TY_ModuleTrace,
/*TreatAsTopLevelOutput=*/true, workingDirectory, "trace.json", Buf);
/*TreatAsTopLevelOutput=*/true, workingDirectory, Buf);
}
Output->setAdditionalOutputForType(file_types::TY_ModuleTrace, filename);
@@ -2829,7 +2831,7 @@ void Driver::chooseOptimizationRecordPath(Compilation &C, const OutputInfo &OI,
auto filename = *getOutputFilenameFromPathArgOrAsTopLevel(
OI, C.getArgs(), options::OPT_save_optimization_record_path,
file_types::TY_OptRecord, /*TreatAsTopLevelOutput=*/true,
workingDirectory, "opt.yaml", Buf);
workingDirectory, Buf);
Output->setAdditionalOutputForType(file_types::TY_OptRecord, filename);
} else

View File

@@ -20,6 +20,7 @@
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/Module.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/Parse/DelayedParsingCallbacks.h"
@@ -345,8 +346,9 @@ CompilerInstance::getInputBufferAndModuleDocBufferIfPresent(
Optional<std::unique_ptr<llvm::MemoryBuffer>>
CompilerInstance::openModuleDoc(const InputFile &input) {
llvm::SmallString<128> moduleDocFilePath(input.file());
llvm::sys::path::replace_extension(moduleDocFilePath,
SERIALIZED_MODULE_DOC_EXTENSION);
llvm::sys::path::replace_extension(
moduleDocFilePath,
file_types::getExtension(file_types::TY_SwiftModuleDocFile));
using FileOrError = llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>;
FileOrError moduleDocFileOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(moduleDocFilePath);

View File

@@ -25,7 +25,6 @@
#include "ReferenceDependencies.h"
#include "TBD.h"
#include "swift/Strings.h"
#include "swift/Subsystems.h"
#include "swift/AST/ASTScope.h"
#include "swift/AST/DiagnosticsFrontend.h"
@@ -225,8 +224,8 @@ static bool emitLoadedModuleTraceIfNeeded(ASTContext &ctxt,
// Decide if this is a swiftmodule based on the extension of the raw
// dependency path, as the true file may have a different one.
auto ext = llvm::sys::path::extension(dep);
if (ext.startswith(".") &&
ext.drop_front() == SERIALIZED_MODULE_EXTENSION) {
if (file_types::lookupTypeForExtension(ext) ==
file_types::TY_SwiftModuleFile) {
swiftModules.push_back(realPath);
}
}

View File

@@ -12,10 +12,10 @@
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Serialization/ModuleFile.h"
#include "swift/Strings.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Version.h"
@@ -106,8 +106,8 @@ static void addDiagnosticInfoForArchitectureMismatch(ASTContext &ctx,
auto entry = *directoryIterator;
StringRef filePath(entry.path());
StringRef extension = llvm::sys::path::extension(filePath);
if (extension.startswith(".") &&
extension.drop_front() == SERIALIZED_MODULE_EXTENSION) {
if (file_types::lookupTypeForExtension(extension) ==
file_types::TY_SwiftModuleFile) {
foundArchs = foundArchs + (foundArchs.length() > 0 ? ", " : "") +
llvm::sys::path::stem(filePath).str();
}
@@ -125,11 +125,12 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
llvm::SmallString<64> moduleName(moduleID.first.str());
llvm::SmallString<64> moduleFilename(moduleName);
moduleFilename += '.';
moduleFilename += SERIALIZED_MODULE_EXTENSION;
moduleFilename += file_types::getExtension(file_types::TY_SwiftModuleFile);
llvm::SmallString<64> moduleDocFilename(moduleID.first.str());
moduleDocFilename += '.';
moduleDocFilename += SERIALIZED_MODULE_DOC_EXTENSION;
moduleDocFilename +=
file_types::getExtension(file_types::TY_SwiftModuleDocFile);
// FIXME: Which name should we be using here? Do we care about CPU subtypes?
// FIXME: At the very least, don't hardcode "arch".
@@ -139,10 +140,10 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
llvm::SmallString<16> archDocFile{archName};
if (!archFile.empty()) {
archFile += '.';
archFile += SERIALIZED_MODULE_EXTENSION;
archFile += file_types::getExtension(file_types::TY_SwiftModuleFile);
archDocFile += '.';
archDocFile += SERIALIZED_MODULE_DOC_EXTENSION;
archDocFile += file_types::getExtension(file_types::TY_SwiftModuleDocFile);
}
llvm::SmallString<128> scratch;