Skip to content

Commit 97b152d

Browse files
committed
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.
1 parent 67a6a4f commit 97b152d

File tree

8 files changed

+51
-55
lines changed

8 files changed

+51
-55
lines changed

include/swift/Basic/FileTypes.def

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ TYPE("object", Object, "o", "")
4646
TYPE("dSYM", dSYM, "dSYM", "")
4747
TYPE("dependencies", Dependencies, "d", "")
4848
TYPE("autolink", AutolinkFile, "autolink", "")
49-
TYPE("swiftmodule", SwiftModuleFile,
50-
SERIALIZED_MODULE_EXTENSION, "")
51-
TYPE("swiftdoc", SwiftModuleDocFile,
52-
SERIALIZED_MODULE_DOC_EXTENSION, "")
49+
TYPE("swiftmodule", SwiftModuleFile, "swiftmodule", "")
50+
TYPE("swiftdoc", SwiftModuleDocFile, "swiftdoc", "")
5351
TYPE("swiftinterface", SwiftModuleInterfaceFile, "swiftinterface", "")
5452
TYPE("assembly", Assembly, "s", "")
5553
TYPE("raw-sil", RawSIL, "sil", "")
@@ -68,7 +66,7 @@ TYPE("opt-record", OptRecord, "opt.yaml", "")
6866

6967
// Misc types
7068
TYPE("pcm", ClangModuleFile, "pcm", "")
71-
TYPE("pch", PCH, PCH_EXTENSION, "")
69+
TYPE("pch", PCH, "pch", "")
7270
TYPE("none", Nothing, "", "")
7371

7472
#undef TYPE

include/swift/Strings.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818

1919
namespace swift {
2020

21-
/// The extension for serialized modules.
22-
constexpr static const char SERIALIZED_MODULE_EXTENSION[] = "swiftmodule";
23-
/// The extension for serialized documentation comments.
24-
constexpr static const char SERIALIZED_MODULE_DOC_EXTENSION[] = "swiftdoc";
25-
/// The extension for PCH files.
26-
constexpr static const char PCH_EXTENSION[] = "pch";
2721
/// The name of the standard library, which is a reserved module name.
2822
constexpr static const char STDLIB_NAME[] = "Swift";
2923
/// The name of the Onone support library, which is a reserved module name.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "swift/Parse/Lexer.h"
3737
#include "swift/Parse/Parser.h"
3838
#include "swift/Config.h"
39-
#include "swift/Strings.h"
4039
#include "clang/AST/ASTContext.h"
4140
#include "clang/AST/Mangle.h"
4241
#include "clang/Basic/CharInfo.h"
@@ -419,7 +418,7 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
419418
auto languageVersion = ctx.LangOpts.EffectiveLanguageVersion;
420419

421420
if (llvm::sys::path::extension(importerOpts.BridgingHeader)
422-
.endswith(PCH_EXTENSION)) {
421+
.endswith(file_types::getExtension(file_types::TY_PCH))) {
423422
invocationArgStrs.insert(invocationArgStrs.end(), {
424423
"-include-pch", importerOpts.BridgingHeader
425424
});
@@ -788,7 +787,7 @@ Optional<std::string>
788787
ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
789788
StringRef SwiftPCHHash, bool &isExplicit) {
790789
if (llvm::sys::path::extension(ImporterOptions.BridgingHeader)
791-
.endswith(PCH_EXTENSION)) {
790+
.endswith(file_types::getExtension(file_types::TY_PCH))) {
792791
isExplicit = true;
793792
return ImporterOptions.BridgingHeader;
794793
}
@@ -879,8 +878,8 @@ ClangImporter::create(ASTContext &ctx,
879878
for (auto &argStr : invocationArgStrs)
880879
invocationArgs.push_back(argStr.c_str());
881880

882-
if (llvm::sys::path::extension(importerOpts.BridgingHeader).endswith(
883-
PCH_EXTENSION)) {
881+
if (llvm::sys::path::extension(importerOpts.BridgingHeader)
882+
.endswith(file_types::getExtension(file_types::TY_PCH))) {
884883
importer->Impl.setSinglePCHImport(importerOpts.BridgingHeader);
885884
importer->Impl.IsReadingBridgingPCH = true;
886885
if (tracker) {
@@ -1289,7 +1288,8 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
12891288
SourceLoc diagLoc,
12901289
bool trackParsedSymbols,
12911290
bool implicitImport) {
1292-
if (llvm::sys::path::extension(header).endswith(PCH_EXTENSION)) {
1291+
if (llvm::sys::path::extension(header)
1292+
.endswith(file_types::getExtension(file_types::TY_PCH))) {
12931293
Impl.ImportedHeaderOwners.push_back(adapter);
12941294
// We already imported this with -include-pch above, so we should have
12951295
// collected a bunch of PCH-encoded module imports that we just need to

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include "swift/AST/Module.h"
2929
#include "swift/AST/Type.h"
3030
#include "swift/AST/ForeignErrorConvention.h"
31+
#include "swift/Basic/FileTypes.h"
3132
#include "swift/Basic/StringExtras.h"
32-
#include "swift/Strings.h"
3333
#include "clang/AST/ASTContext.h"
3434
#include "clang/AST/DeclVisitor.h"
3535
#include "clang/Basic/IdentifierTable.h"
@@ -1360,7 +1360,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13601360
void setSinglePCHImport(Optional<std::string> PCHFilename) {
13611361
if (PCHFilename.hasValue()) {
13621362
assert(llvm::sys::path::extension(PCHFilename.getValue())
1363-
.endswith(PCH_EXTENSION) &&
1363+
.endswith(file_types::getExtension(file_types::TY_PCH)) &&
13641364
"Single PCH imported filename doesn't have .pch extension!");
13651365
}
13661366
SinglePCHImport = PCHFilename;

lib/Driver/Driver.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,7 @@ static void formFilenameFromBaseAndExt(StringRef base, StringRef newExt,
20192019
static Optional<StringRef> getOutputFilenameFromPathArgOrAsTopLevel(
20202020
const OutputInfo &OI, const llvm::opt::DerivedArgList &Args,
20212021
llvm::opt::OptSpecifier PathArg, file_types::ID ExpectedOutputType,
2022-
bool TreatAsTopLevelOutput, StringRef workingDirectory, StringRef ext,
2022+
bool TreatAsTopLevelOutput, StringRef workingDirectory,
20232023
llvm::SmallString<128> &Buffer) {
20242024
if (const Arg *A = Args.getLastArg(PathArg))
20252025
return StringRef(A->getValue());
@@ -2033,13 +2033,17 @@ static Optional<StringRef> getOutputFilenameFromPathArgOrAsTopLevel(
20332033
Buffer = A->getValue();
20342034
llvm::sys::path::remove_filename(Buffer);
20352035
llvm::sys::path::append(Buffer, OI.ModuleName);
2036-
llvm::sys::path::replace_extension(Buffer, ext);
2036+
llvm::sys::path::replace_extension(
2037+
Buffer, file_types::getExtension(ExpectedOutputType));
20372038
return Buffer.str();
20382039
}
20392040

20402041
// A top-level output wasn't specified, so just output to
20412042
// <ModuleName>.<ext>.
2042-
formFilenameFromBaseAndExt(OI.ModuleName, ext, workingDirectory, Buffer);
2043+
formFilenameFromBaseAndExt(OI.ModuleName,
2044+
file_types::getExtension(ExpectedOutputType),
2045+
workingDirectory,
2046+
Buffer);
20432047
return Buffer.str();
20442048
}
20452049

@@ -2118,8 +2122,7 @@ static StringRef getOutputFilename(Compilation &C,
21182122
if (isa<MergeModuleJobAction>(JA)) {
21192123
auto optFilename = getOutputFilenameFromPathArgOrAsTopLevel(
21202124
OI, Args, options::OPT_emit_module_path, file_types::TY_SwiftModuleFile,
2121-
OI.ShouldTreatModuleAsTopLevelOutput, workingDirectory,
2122-
SERIALIZED_MODULE_EXTENSION, Buffer);
2125+
OI.ShouldTreatModuleAsTopLevelOutput, workingDirectory, Buffer);
21232126
if (optFilename)
21242127
return *optFilename;
21252128
}
@@ -2627,17 +2630,16 @@ void Driver::chooseSwiftModuleOutputPath(Compilation &C, const OutputInfo &OI,
26272630
}
26282631

26292632
const Arg *A = C.getArgs().getLastArg(options::OPT_emit_module_path);
2633+
using file_types::TY_SwiftModuleFile;
26302634

26312635
if (!OFMModuleOutputPath.empty()) {
26322636
// Prefer a path from the OutputMap.
2633-
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile,
2634-
OFMModuleOutputPath);
2637+
Output->setAdditionalOutputForType(TY_SwiftModuleFile, OFMModuleOutputPath);
26352638
} else if (A && OI.CompilerMode == OutputInfo::Mode::SingleCompile) {
26362639
// We're performing a single compilation (and thus no merge module step),
26372640
// so prefer to use -emit-module-path, if present.
2638-
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile,
2639-
A->getValue());
2640-
} else if (Output->getPrimaryOutputType() == file_types::TY_SwiftModuleFile) {
2641+
Output->setAdditionalOutputForType(TY_SwiftModuleFile, A->getValue());
2642+
} else if (Output->getPrimaryOutputType() == TY_SwiftModuleFile) {
26412643
// If the primary type is already a module type, we're out of
26422644
// options for overriding the primary name choice: stop now.
26432645
assert(!Output->getPrimaryOutputFilename().empty());
@@ -2647,29 +2649,28 @@ void Driver::chooseSwiftModuleOutputPath(Compilation &C, const OutputInfo &OI,
26472649
// We're performing a single compile and don't have -emit-module-path,
26482650
// but have been told to treat the module as a top-level output.
26492651
// Determine an appropriate path.
2652+
llvm::SmallString<128> Path;
26502653
if (const Arg *A = C.getArgs().getLastArg(options::OPT_o)) {
26512654
// Put the module next to the top-level output.
2652-
llvm::SmallString<128> Path(A->getValue());
2655+
Path = A->getValue();
26532656
llvm::sys::path::remove_filename(Path);
2654-
llvm::sys::path::append(Path, OI.ModuleName);
2655-
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_EXTENSION);
2656-
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile, Path);
26572657
} else {
26582658
// A top-level output wasn't specified, so just output to
2659-
// <ModuleName>.swiftmodule.
2660-
llvm::SmallString<128> Path(OI.ModuleName);
2661-
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_EXTENSION);
2662-
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleFile, Path);
2659+
// <ModuleName>.swiftmodule in the current directory.
26632660
}
2661+
llvm::sys::path::append(Path, OI.ModuleName);
2662+
llvm::sys::path::replace_extension(
2663+
Path, file_types::getExtension(TY_SwiftModuleFile));
2664+
Output->setAdditionalOutputForType(TY_SwiftModuleFile, Path);
26642665
} else if (Output->getPrimaryOutputType() != file_types::TY_Nothing) {
26652666
// We're only generating the module as an intermediate, so put it next
26662667
// to the primary output of the compile command.
26672668
llvm::SmallString<128> Path(Output->getPrimaryOutputFilenames()[0]);
26682669
assert(!Path.empty());
26692670
bool isTempFile = C.isTemporaryFile(Path);
2670-
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_EXTENSION);
2671-
Output->setAdditionalOutputForType(file_types::ID::TY_SwiftModuleFile,
2672-
Path);
2671+
llvm::sys::path::replace_extension(
2672+
Path, file_types::getExtension(TY_SwiftModuleFile));
2673+
Output->setAdditionalOutputForType(TY_SwiftModuleFile, Path);
26732674
if (isTempFile)
26742675
C.addTemporaryFile(Path);
26752676
}
@@ -2698,7 +2699,8 @@ void Driver::chooseSwiftModuleDocOutputPath(Compilation &C,
26982699
llvm::SmallString<128> Path(
26992700
Output->getAnyOutputForType(file_types::TY_SwiftModuleFile));
27002701
bool isTempFile = C.isTemporaryFile(Path);
2701-
llvm::sys::path::replace_extension(Path, SERIALIZED_MODULE_DOC_EXTENSION);
2702+
llvm::sys::path::replace_extension(
2703+
Path, file_types::getExtension(file_types::TY_SwiftModuleDocFile));
27022704
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleDocFile, Path);
27032705
if (isTempFile)
27042706
C.addTemporaryFile(Path);
@@ -2799,7 +2801,7 @@ void Driver::chooseLoadedModuleTracePath(Compilation &C, const OutputInfo &OI,
27992801
filename = *getOutputFilenameFromPathArgOrAsTopLevel(
28002802
OI, C.getArgs(), options::OPT_emit_loaded_module_trace_path,
28012803
file_types::TY_ModuleTrace,
2802-
/*TreatAsTopLevelOutput=*/true, workingDirectory, "trace.json", Buf);
2804+
/*TreatAsTopLevelOutput=*/true, workingDirectory, Buf);
28032805
}
28042806

28052807
Output->setAdditionalOutputForType(file_types::TY_ModuleTrace, filename);
@@ -2829,7 +2831,7 @@ void Driver::chooseOptimizationRecordPath(Compilation &C, const OutputInfo &OI,
28292831
auto filename = *getOutputFilenameFromPathArgOrAsTopLevel(
28302832
OI, C.getArgs(), options::OPT_save_optimization_record_path,
28312833
file_types::TY_OptRecord, /*TreatAsTopLevelOutput=*/true,
2832-
workingDirectory, "opt.yaml", Buf);
2834+
workingDirectory, Buf);
28332835

28342836
Output->setAdditionalOutputForType(file_types::TY_OptRecord, filename);
28352837
} else

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/DiagnosticsFrontend.h"
2121
#include "swift/AST/DiagnosticsSema.h"
2222
#include "swift/AST/Module.h"
23+
#include "swift/Basic/FileTypes.h"
2324
#include "swift/Basic/SourceManager.h"
2425
#include "swift/Basic/Statistic.h"
2526
#include "swift/Parse/DelayedParsingCallbacks.h"
@@ -345,8 +346,9 @@ CompilerInstance::getInputBufferAndModuleDocBufferIfPresent(
345346
Optional<std::unique_ptr<llvm::MemoryBuffer>>
346347
CompilerInstance::openModuleDoc(const InputFile &input) {
347348
llvm::SmallString<128> moduleDocFilePath(input.file());
348-
llvm::sys::path::replace_extension(moduleDocFilePath,
349-
SERIALIZED_MODULE_DOC_EXTENSION);
349+
llvm::sys::path::replace_extension(
350+
moduleDocFilePath,
351+
file_types::getExtension(file_types::TY_SwiftModuleDocFile));
350352
using FileOrError = llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>;
351353
FileOrError moduleDocFileOrErr =
352354
llvm::MemoryBuffer::getFileOrSTDIN(moduleDocFilePath);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "ReferenceDependencies.h"
2626
#include "TBD.h"
2727

28-
#include "swift/Strings.h"
2928
#include "swift/Subsystems.h"
3029
#include "swift/AST/ASTScope.h"
3130
#include "swift/AST/DiagnosticsFrontend.h"
@@ -225,8 +224,8 @@ static bool emitLoadedModuleTraceIfNeeded(ASTContext &ctxt,
225224
// Decide if this is a swiftmodule based on the extension of the raw
226225
// dependency path, as the true file may have a different one.
227226
auto ext = llvm::sys::path::extension(dep);
228-
if (ext.startswith(".") &&
229-
ext.drop_front() == SERIALIZED_MODULE_EXTENSION) {
227+
if (file_types::lookupTypeForExtension(ext) ==
228+
file_types::TY_SwiftModuleFile) {
230229
swiftModules.push_back(realPath);
231230
}
232231
}

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
#include "swift/Serialization/SerializedModuleLoader.h"
1414
#include "swift/Serialization/ModuleFile.h"
15-
#include "swift/Strings.h"
1615
#include "swift/AST/ASTContext.h"
1716
#include "swift/AST/DiagnosticsSema.h"
1817
#include "swift/Basic/Defer.h"
18+
#include "swift/Basic/FileTypes.h"
1919
#include "swift/Basic/STLExtras.h"
2020
#include "swift/Basic/SourceManager.h"
2121
#include "swift/Basic/Version.h"
@@ -106,8 +106,8 @@ static void addDiagnosticInfoForArchitectureMismatch(ASTContext &ctx,
106106
auto entry = *directoryIterator;
107107
StringRef filePath(entry.path());
108108
StringRef extension = llvm::sys::path::extension(filePath);
109-
if (extension.startswith(".") &&
110-
extension.drop_front() == SERIALIZED_MODULE_EXTENSION) {
109+
if (file_types::lookupTypeForExtension(extension) ==
110+
file_types::TY_SwiftModuleFile) {
111111
foundArchs = foundArchs + (foundArchs.length() > 0 ? ", " : "") +
112112
llvm::sys::path::stem(filePath).str();
113113
}
@@ -125,11 +125,12 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
125125
llvm::SmallString<64> moduleName(moduleID.first.str());
126126
llvm::SmallString<64> moduleFilename(moduleName);
127127
moduleFilename += '.';
128-
moduleFilename += SERIALIZED_MODULE_EXTENSION;
128+
moduleFilename += file_types::getExtension(file_types::TY_SwiftModuleFile);
129129

130130
llvm::SmallString<64> moduleDocFilename(moduleID.first.str());
131131
moduleDocFilename += '.';
132-
moduleDocFilename += SERIALIZED_MODULE_DOC_EXTENSION;
132+
moduleDocFilename +=
133+
file_types::getExtension(file_types::TY_SwiftModuleDocFile);
133134

134135
// FIXME: Which name should we be using here? Do we care about CPU subtypes?
135136
// FIXME: At the very least, don't hardcode "arch".
@@ -139,10 +140,10 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
139140
llvm::SmallString<16> archDocFile{archName};
140141
if (!archFile.empty()) {
141142
archFile += '.';
142-
archFile += SERIALIZED_MODULE_EXTENSION;
143+
archFile += file_types::getExtension(file_types::TY_SwiftModuleFile);
143144

144145
archDocFile += '.';
145-
archDocFile += SERIALIZED_MODULE_DOC_EXTENSION;
146+
archDocFile += file_types::getExtension(file_types::TY_SwiftModuleDocFile);
146147
}
147148

148149
llvm::SmallString<128> scratch;

0 commit comments

Comments
 (0)