Skip to content

Commit 798496c

Browse files
committed
Consistently get extensions from swift/Frontend/FileTypes.h
...instead of sometimes hardcoding them and sometimes using Strings.h. The exceptions are the libraries that sit below Frontend; these can continue using strings.
1 parent 6e5aefc commit 798496c

File tree

10 files changed

+78
-54
lines changed

10 files changed

+78
-54
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_FRONTEND_FRONTENDOPTIONS_H
1414
#define SWIFT_FRONTEND_FRONTENDOPTIONS_H
1515

16+
#include "swift/Frontend/FileTypes.h"
1617
#include "swift/Frontend/FrontendInputsAndOutputs.h"
1718
#include "swift/Frontend/InputFile.h"
1819
#include "llvm/ADT/Hashing.h"
@@ -312,7 +313,7 @@ class FrontendOptions {
312313
static bool doesActionProduceOutput(ActionType);
313314
static bool doesActionProduceTextualOutput(ActionType);
314315
static bool needsProperModuleName(ActionType);
315-
static StringRef suffixForPrincipalOutputFileForAction(ActionType);
316+
static file_types::ID formatForPrincipalOutputFileForAction(ActionType);
316317
};
317318

318319
}

include/swift/Frontend/Types.def

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ TYPE("object", Object, "o", "")
4646
TYPE("dSYM", dSYM, "dSYM", "")
4747
TYPE("dependencies", Dependencies, "d", "")
4848
TYPE("autolink", AutolinkFile, "autolink", "")
49-
TYPE("swiftmodule", SwiftModuleFile, "swiftmodule", "")
50-
TYPE("swiftdoc", SwiftModuleDocFile, "swiftdoc", "")
49+
TYPE("swiftmodule", SwiftModuleFile,
50+
SERIALIZED_MODULE_EXTENSION, "")
51+
TYPE("swiftdoc", SwiftModuleDocFile,
52+
SERIALIZED_MODULE_DOC_EXTENSION, "")
5153
TYPE("swiftinterface", SwiftModuleInterfaceFile, "swiftinterface", "")
5254
TYPE("assembly", Assembly, "s", "")
5355
TYPE("raw-sil", RawSIL, "sil", "")
5456
TYPE("raw-sib", RawSIB, "sib", "")
55-
TYPE("llvm-ir", LLVM_IR, "ir", "")
57+
TYPE("llvm-ir", LLVM_IR, "ll", "")
5658
TYPE("llvm-bc", LLVM_BC, "bc", "")
5759
TYPE("diagnostics", SerializedDiagnostics, "dia", "")
5860
TYPE("objc-header", ObjCHeader, "h", "")
@@ -66,7 +68,7 @@ TYPE("opt-record", OptRecord, "opt.yaml", "")
6668

6769
// Misc types
6870
TYPE("pcm", ClangModuleFile, "pcm", "")
69-
TYPE("pch", PCH, "pch", "")
71+
TYPE("pch", PCH, PCH_EXTENSION, "")
7072
TYPE("none", Nothing, "", "")
7173

7274
#undef TYPE

include/swift/Strings.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ constexpr static const char SERIALIZED_MODULE_EXTENSION[] = "swiftmodule";
2424
constexpr static const char SERIALIZED_MODULE_DOC_EXTENSION[] = "swiftdoc";
2525
/// The extension for PCH files.
2626
constexpr static const char PCH_EXTENSION[] = "pch";
27-
/// The extension for replacement map files.
28-
constexpr static const char REMAP_EXTENSION[] = "remap";
29-
/// The extension for SIL files.
30-
constexpr static const char SIL_EXTENSION[] = "sil";
31-
/// The extension for SIB files.
32-
constexpr static const char SIB_EXTENSION[] = "sib";
33-
/// The extension for LLVM IR files.
34-
constexpr static const char LLVM_BC_EXTENSION[] = "bc";
35-
constexpr static const char LLVM_IR_EXTENSION[] = "ll";
3627
/// The name of the standard library, which is a reserved module name.
3728
constexpr static const char STDLIB_NAME[] = "Swift";
3829
/// The name of the Onone support library, which is a reserved module name.

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ OutputFilesComputer::create(const llvm::opt::ArgList &args,
117117
return None;
118118
}
119119

120+
const file_types::ID outputType =
121+
FrontendOptions::formatForPrincipalOutputFileForAction(requestedAction);
122+
120123
return OutputFilesComputer(
121124
diags, inputsAndOutputs, std::move(outputFileArguments),
122125
outputDirectoryArgument, firstInput, requestedAction,
123126
args.getLastArg(options::OPT_module_name),
124-
FrontendOptions::suffixForPrincipalOutputFileForAction(requestedAction),
127+
file_types::getExtension(outputType),
125128
FrontendOptions::doesActionProduceTextualOutput(requestedAction));
126129
}
127130

@@ -354,36 +357,41 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
354357
using namespace options;
355358

356359
auto dependenciesFilePath = determineSupplementaryOutputFilename(
357-
OPT_emit_dependencies, pathsFromArguments.DependenciesFilePath, "d", "",
360+
OPT_emit_dependencies, pathsFromArguments.DependenciesFilePath,
361+
file_types::TY_Dependencies, "",
358362
defaultSupplementaryOutputPathExcludingExtension);
359363

360364
auto referenceDependenciesFilePath = determineSupplementaryOutputFilename(
361365
OPT_emit_reference_dependencies,
362-
pathsFromArguments.ReferenceDependenciesFilePath, "swiftdeps", "",
366+
pathsFromArguments.ReferenceDependenciesFilePath,
367+
file_types::TY_SwiftDeps, "",
363368
defaultSupplementaryOutputPathExcludingExtension);
364369

365370
auto serializedDiagnosticsPath = determineSupplementaryOutputFilename(
366371
OPT_serialize_diagnostics, pathsFromArguments.SerializedDiagnosticsPath,
367-
"dia", "", defaultSupplementaryOutputPathExcludingExtension);
372+
file_types::TY_SerializedDiagnostics, "",
373+
defaultSupplementaryOutputPathExcludingExtension);
368374

369375
// There is no non-path form of -emit-fixits-path
370376
auto fixItsOutputPath = pathsFromArguments.FixItsOutputPath;
371377

372378
auto objcHeaderOutputPath = determineSupplementaryOutputFilename(
373-
OPT_emit_objc_header, pathsFromArguments.ObjCHeaderOutputPath, "h", "",
379+
OPT_emit_objc_header, pathsFromArguments.ObjCHeaderOutputPath,
380+
file_types::TY_ObjCHeader, "",
374381
defaultSupplementaryOutputPathExcludingExtension);
375382

376383
auto loadedModuleTracePath = determineSupplementaryOutputFilename(
377384
OPT_emit_loaded_module_trace, pathsFromArguments.LoadedModuleTracePath,
378-
"trace.json", "", defaultSupplementaryOutputPathExcludingExtension);
385+
file_types::TY_ModuleTrace, "",
386+
defaultSupplementaryOutputPathExcludingExtension);
379387

380388
auto tbdPath = determineSupplementaryOutputFilename(
381-
OPT_emit_tbd, pathsFromArguments.TBDPath, "tbd", "",
389+
OPT_emit_tbd, pathsFromArguments.TBDPath, file_types::TY_TBD, "",
382390
defaultSupplementaryOutputPathExcludingExtension);
383391

384392
auto moduleDocOutputPath = determineSupplementaryOutputFilename(
385393
OPT_emit_module_doc, pathsFromArguments.ModuleDocOutputPath,
386-
SERIALIZED_MODULE_DOC_EXTENSION, "",
394+
file_types::TY_SwiftModuleDocFile, "",
387395
defaultSupplementaryOutputPathExcludingExtension);
388396

389397
// There is no non-path form of -emit-interface-path
@@ -396,8 +404,8 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
396404
mainOutputIfUsableForModule);
397405

398406
auto moduleOutputPath = determineSupplementaryOutputFilename(
399-
emitModuleOption, pathsFromArguments.ModuleOutputPath, moduleExtension,
400-
mainOutputIfUsableForModule,
407+
emitModuleOption, pathsFromArguments.ModuleOutputPath,
408+
file_types::TY_SwiftModuleFile, mainOutputIfUsableForModule,
401409
defaultSupplementaryOutputPathExcludingExtension);
402410

403411
SupplementaryOutputPaths sop;
@@ -429,7 +437,7 @@ StringRef SupplementaryOutputPathsComputer::
429437

430438
std::string
431439
SupplementaryOutputPathsComputer::determineSupplementaryOutputFilename(
432-
options::ID emitOpt, std::string pathFromArguments, StringRef extension,
440+
options::ID emitOpt, std::string pathFromArguments, file_types::ID type,
433441
StringRef mainOutputIfUsable,
434442
StringRef defaultSupplementaryOutputPathExcludingExtension) const {
435443

@@ -444,7 +452,7 @@ SupplementaryOutputPathsComputer::determineSupplementaryOutputFilename(
444452
}
445453

446454
llvm::SmallString<128> path(defaultSupplementaryOutputPathExcludingExtension);
447-
llvm::sys::path::replace_extension(path, extension);
455+
llvm::sys::path::replace_extension(path, file_types::getExtension(type));
448456
return path.str().str();
449457
};
450458

@@ -464,7 +472,8 @@ void SupplementaryOutputPathsComputer::deriveModulePathParameters(
464472
RequestedAction == FrontendOptions::ActionType::MergeModules ||
465473
RequestedAction == FrontendOptions::ActionType::EmitModuleOnly || isSIB;
466474

467-
extension = isSIB ? SIB_EXTENSION : SERIALIZED_MODULE_EXTENSION;
475+
extension = file_types::getExtension(
476+
isSIB ? file_types::TY_SIB : file_types::TY_SwiftModuleFile);
468477

469478
mainOutputIfUsable =
470479
canUseMainOutputForModule && !OutputFiles.empty() ? OutputFiles[0] : "";

lib/Frontend/ArgsToFrontendOutputsConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class SupplementaryOutputPathsComputer {
164164
/// \return empty string if no output file.
165165
std::string determineSupplementaryOutputFilename(
166166
options::ID emitOpt, std::string pathFromArgumentsOrFilelists,
167-
StringRef extension, StringRef mainOutputIfUsable,
167+
file_types::ID type, StringRef mainOutputIfUsable,
168168
StringRef defaultSupplementaryOutputPathExcludingExtension) const;
169169

170170
void deriveModulePathParameters(options::ID &emitOption,

lib/Frontend/FrontendInputsAndOutputs.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "swift/AST/DiagnosticsFrontend.h"
1616
#include "swift/Basic/PrimarySpecificPaths.h"
17+
#include "swift/Frontend/FileTypes.h"
1718
#include "swift/Frontend/FrontendOptions.h"
1819
#include "swift/Option/Options.h"
1920
#include "swift/Parse/Lexer.h"
@@ -155,9 +156,14 @@ unsigned FrontendInputsAndOutputs::numberOfPrimaryInputsEndingWith(
155156

156157
bool FrontendInputsAndOutputs::shouldTreatAsLLVM() const {
157158
if (hasSingleInput()) {
158-
StringRef Input(getFilenameOfFirstInput());
159-
return llvm::sys::path::extension(Input).endswith(LLVM_BC_EXTENSION) ||
160-
llvm::sys::path::extension(Input).endswith(LLVM_IR_EXTENSION);
159+
StringRef InputExt = llvm::sys::path::extension(getFilenameOfFirstInput());
160+
switch (file_types::lookupTypeForExtension(InputExt)) {
161+
case file_types::TY_LLVM_BC:
162+
case file_types::TY_LLVM_IR:
163+
return true;
164+
default:
165+
return false;
166+
}
161167
}
162168
return false;
163169
}
@@ -166,12 +172,13 @@ bool FrontendInputsAndOutputs::shouldTreatAsSIL() const {
166172
if (hasSingleInput()) {
167173
// If we have exactly one input filename, and its extension is "sil",
168174
// treat the input as SIL.
169-
const std::string &Input(getFilenameOfFirstInput());
170-
return llvm::sys::path::extension(Input).endswith(SIL_EXTENSION);
175+
StringRef extension = llvm::sys::path::extension(getFilenameOfFirstInput());
176+
return file_types::lookupTypeForExtension(extension) == file_types::TY_SIL;
171177
}
172178
// If we have one primary input and it's a filename with extension "sil",
173179
// treat the input as SIL.
174-
unsigned silPrimaryCount = numberOfPrimaryInputsEndingWith(SIL_EXTENSION);
180+
const unsigned silPrimaryCount = numberOfPrimaryInputsEndingWith(
181+
file_types::getExtension(file_types::TY_SIL));
175182
if (silPrimaryCount == 0)
176183
return false;
177184
if (silPrimaryCount == primaryInputCount()) {
@@ -186,7 +193,8 @@ bool FrontendInputsAndOutputs::areAllNonPrimariesSIB() const {
186193
for (const InputFile &input : AllInputs) {
187194
if (input.isPrimary())
188195
continue;
189-
if (!llvm::sys::path::extension(input.file()).endswith(SIB_EXTENSION)) {
196+
StringRef extension = llvm::sys::path::extension(input.file());
197+
if (file_types::lookupTypeForExtension(extension) != file_types::TY_SIB) {
190198
return false;
191199
}
192200
}

lib/Frontend/FrontendOptions.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -130,11 +130,13 @@ void FrontendOptions::forAllOutputPaths(
130130
}
131131
}
132132

133-
StringRef
134-
FrontendOptions::suffixForPrincipalOutputFileForAction(ActionType action) {
133+
file_types::ID
134+
FrontendOptions::formatForPrincipalOutputFileForAction(ActionType action) {
135+
using namespace file_types;
136+
135137
switch (action) {
136138
case ActionType::NoneAction:
137-
return StringRef();
139+
return TY_Nothing;
138140

139141
case ActionType::Parse:
140142
case ActionType::ResolveImports:
@@ -146,42 +148,46 @@ FrontendOptions::suffixForPrincipalOutputFileForAction(ActionType action) {
146148
case ActionType::PrintAST:
147149
case ActionType::DumpScopeMaps:
148150
case ActionType::DumpTypeRefinementContexts:
149-
return StringRef();
151+
return TY_Nothing;
150152

151153
case ActionType::EmitPCH:
152-
return PCH_EXTENSION;
154+
return TY_PCH;
153155

154156
case ActionType::EmitSILGen:
157+
return TY_RawSIL;
158+
155159
case ActionType::EmitSIL:
156-
return SIL_EXTENSION;
160+
return TY_SIL;
157161

158162
case ActionType::EmitSIBGen:
163+
return TY_RawSIB;
164+
159165
case ActionType::EmitSIB:
160-
return SIB_EXTENSION;
166+
return TY_SIB;
161167

162168
case ActionType::MergeModules:
163169
case ActionType::EmitModuleOnly:
164-
return SERIALIZED_MODULE_EXTENSION;
170+
return TY_SwiftModuleFile;
165171

166172
case ActionType::Immediate:
167173
case ActionType::REPL:
168174
// These modes have no frontend-generated output.
169-
return StringRef();
175+
return TY_Nothing;
170176

171177
case ActionType::EmitAssembly:
172-
return "s";
178+
return TY_Assembly;
173179

174180
case ActionType::EmitIR:
175-
return "ll";
181+
return TY_LLVM_IR;
176182

177183
case ActionType::EmitBC:
178-
return "bc";
184+
return TY_LLVM_BC;
179185

180186
case ActionType::EmitObject:
181-
return "o";
187+
return TY_Object;
182188

183189
case ActionType::EmitImportedModules:
184-
return "importedmodules";
190+
return TY_ImportedModules;
185191
}
186192
}
187193

lib/Frontend/Types.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/Frontend/FileTypes.h"
1414

15+
#include "swift/Strings.h"
1516
#include "llvm/ADT/STLExtras.h"
1617
#include "llvm/ADT/StringSwitch.h"
1718
#include "llvm/Support/ErrorHandling.h"

tools/sil-func-extractor/SILFunctionExtractor.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/Demangling/Demangle.h"
2727
#include "swift/Demangling/ManglingMacros.h"
2828
#include "swift/Frontend/DiagnosticVerifier.h"
29+
#include "swift/Frontend/FileTypes.h"
2930
#include "swift/Frontend/Frontend.h"
3031
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
3132
#include "swift/SIL/SILBuilder.h"
@@ -347,10 +348,12 @@ int main(int argc, char **argv) {
347348
OutputFile = OutputFilename;
348349
} else if (ModuleName.size()) {
349350
OutputFile = ModuleName;
350-
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
351+
llvm::sys::path::replace_extension(
352+
OutputFile, file_types::getExtension(file_types::TY_SIB));
351353
} else {
352354
OutputFile = CI.getMainModule()->getName().str();
353-
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
355+
llvm::sys::path::replace_extension(
356+
OutputFile, file_types::getExtension(file_types::TY_SIB));
354357
}
355358

356359
SerializationOptions serializationOpts;

tools/sil-opt/SILOpt.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Basic/LLVMInitialize.h"
2323
#include "swift/Basic/LLVMContext.h"
2424
#include "swift/Frontend/DiagnosticVerifier.h"
25+
#include "swift/Frontend/FileTypes.h"
2526
#include "swift/Frontend/Frontend.h"
2627
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2728
#include "swift/SILOptimizer/Analysis/Analysis.h"
@@ -452,10 +453,12 @@ int main(int argc, char **argv) {
452453
OutputFile = OutputFilename;
453454
} else if (ModuleName.size()) {
454455
OutputFile = ModuleName;
455-
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
456+
llvm::sys::path::replace_extension(
457+
OutputFile, file_types::getExtension(file_types::TY_SIB));
456458
} else {
457459
OutputFile = CI.getMainModule()->getName().str();
458-
llvm::sys::path::replace_extension(OutputFile, SIB_EXTENSION);
460+
llvm::sys::path::replace_extension(
461+
OutputFile, file_types::getExtension(file_types::TY_SIB));
459462
}
460463

461464
SerializationOptions serializationOpts;

0 commit comments

Comments
 (0)