Skip to content

Commit 841eeb0

Browse files
authored
Merge pull request #30403 from MForster/forster/string-fixes
Cherry-pick StringRef->std::string conversion fixes into `master`
2 parents 2af51be + 345a914 commit 841eeb0

File tree

107 files changed

+364
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+364
-345
lines changed

Diff for: include/swift/ABI/TypeIdentity.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,16 @@ class TypeImportInfo {
129129
value = value.drop_front(1);
130130

131131
switch (component) {
132-
#define case_setIfNonEmpty(FIELD) \
133-
case TypeImportComponent::FIELD: \
134-
check(!value.empty(), "incoming value of " #FIELD " was empty"); \
135-
check(FIELD.empty(), #FIELD " was already set"); \
136-
FIELD = value; \
137-
return true; \
138-
139-
case_setIfNonEmpty(ABIName)
140-
case_setIfNonEmpty(SymbolNamespace)
141-
case_setIfNonEmpty(RelatedEntityName)
132+
#define case_setIfNonEmpty(FIELD) \
133+
case TypeImportComponent::FIELD: \
134+
check(!value.empty(), "incoming value of " #FIELD " was empty"); \
135+
check(FIELD.empty(), #FIELD " was already set"); \
136+
FIELD = StringType(value); \
137+
return true;
138+
139+
case_setIfNonEmpty(ABIName)
140+
case_setIfNonEmpty(SymbolNamespace)
141+
case_setIfNonEmpty(RelatedEntityName)
142142

143143
#undef case_setIfNonEmpty
144144
#undef check

Diff for: include/swift/AST/Identifier.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ class Identifier {
8383
const char *get() const { return Pointer; }
8484

8585
StringRef str() const { return Pointer; }
86-
86+
87+
explicit operator std::string() const { return std::string(Pointer); }
88+
8789
unsigned getLength() const {
8890
assert(Pointer != nullptr && "Tried getting length of empty identifier");
8991
return ::strlen(Pointer);

Diff for: include/swift/Basic/LangOptions.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ namespace swift {
369369
/// compiler flag.
370370
void addCustomConditionalCompilationFlag(StringRef Name) {
371371
assert(!Name.empty());
372-
CustomConditionalCompilationFlags.push_back(Name);
372+
CustomConditionalCompilationFlags.push_back(Name.str());
373373
}
374374

375375
/// Determines if a given conditional compilation flag has been set.

Diff for: include/swift/Demangling/TypeDecoder.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class TypeDecoder {
384384
}
385385
case NodeKind::BuiltinTypeName: {
386386
auto mangledName = Demangle::mangleNode(Node);
387-
return Builder.createBuiltinType(Node->getText(), mangledName);
387+
return Builder.createBuiltinType(Node->getText().str(), mangledName);
388388
}
389389
case NodeKind::Metatype:
390390
case NodeKind::ExistentialMetatype: {
@@ -683,12 +683,12 @@ class TypeDecoder {
683683
auto assocTypeChild = Node->getChild(1);
684684
auto member = assocTypeChild->getFirstChild()->getText();
685685
if (assocTypeChild->getNumChildren() < 2)
686-
return Builder.createDependentMemberType(member, base);
686+
return Builder.createDependentMemberType(member.str(), base);
687687

688688
auto protocol = decodeMangledProtocolType(assocTypeChild->getChild(1));
689689
if (!protocol)
690690
return BuiltType();
691-
return Builder.createDependentMemberType(member, base, protocol);
691+
return Builder.createDependentMemberType(member.str(), base, protocol);
692692
}
693693
case NodeKind::DependentAssociatedTypeRef: {
694694
if (Node->getNumChildren() < 2)

Diff for: include/swift/Remote/MetadataReader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ class MetadataReader {
823823

824824
#if SWIFT_OBJC_INTEROP
825825
BuiltProtocolDecl objcProtocol(StringRef name) {
826-
return builder.createObjCProtocolDecl(name);
826+
return builder.createObjCProtocolDecl(name.str());
827827
}
828828
#endif
829829
} resolver{Builder};

Diff for: include/swift/SIL/SILFunction.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ class SILFunction
692692
void addSemanticsAttr(StringRef Ref) {
693693
if (hasSemanticsAttr(Ref))
694694
return;
695-
SemanticsAttrSet.push_back(Ref);
695+
SemanticsAttrSet.push_back(Ref.str());
696696
std::sort(SemanticsAttrSet.begin(), SemanticsAttrSet.end());
697697
}
698698

Diff for: lib/AST/ASTContext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ void ASTContext::addSearchPath(StringRef searchPath, bool isFramework,
14311431
if (isFramework)
14321432
SearchPathOpts.FrameworkSearchPaths.push_back({searchPath, isSystem});
14331433
else
1434-
SearchPathOpts.ImportSearchPaths.push_back(searchPath);
1434+
SearchPathOpts.ImportSearchPaths.push_back(searchPath.str());
14351435

14361436
if (auto *clangLoader = getClangModuleLoader())
14371437
clangLoader->addSearchPath(searchPath, isFramework, isSystem);

Diff for: lib/AST/ASTPrinter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ std::string ASTPrinter::sanitizeUtf8(StringRef Text) {
272272
}
273273
Data += Step;
274274
}
275-
return Builder.str();
275+
return std::string(Builder.str());
276276
}
277277

278278
void ASTPrinter::anchor() {}
@@ -4543,7 +4543,7 @@ AnyFunctionType::getParamListAsString(ArrayRef<AnyFunctionType::Param> Params,
45434543
SmallString<16> Scratch;
45444544
llvm::raw_svector_ostream OS(Scratch);
45454545
AnyFunctionType::printParams(Params, OS);
4546-
return OS.str();
4546+
return std::string(OS.str());
45474547
}
45484548

45494549
void LayoutConstraintInfo::print(raw_ostream &OS,

Diff for: lib/AST/DiagnosticEngine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
989989
while (associatedNotes && *associatedNotes) {
990990
SmallString<128> notePath(getDiagnosticDocumentationPath());
991991
llvm::sys::path::append(notePath, *associatedNotes);
992-
educationalNotePaths.push_back(notePath.str());
992+
educationalNotePaths.push_back(notePath.str().str());
993993
associatedNotes++;
994994
}
995995
info->EducationalNotePaths = educationalNotePaths;

Diff for: lib/AST/Module.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,7 @@ StringRef ModuleEntity::getName() const {
24092409
std::string ModuleEntity::getFullName() const {
24102410
assert(!Mod.isNull());
24112411
if (auto SwiftMod = Mod.dyn_cast<const ModuleDecl*>())
2412-
return SwiftMod->getName().str();
2412+
return std::string(SwiftMod->getName());
24132413
return getClangModule(Mod)->getFullModuleName();
24142414
}
24152415

Diff for: lib/AST/ReferencedNameTracker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void ReferencedNameTracker::enumerateMemberUses(
136136
std::string context =
137137
DependencyKey::computeContextForProvidedEntity<NodeKind::member>(
138138
nominal);
139-
std::string name = rawName.userFacingName();
139+
std::string name = rawName.userFacingName().str();
140140
enumerateUsedDecl(NodeKind::member, context, name, isCascadingUse);
141141
}
142142
}

Diff for: lib/AST/SwiftNameTranslation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ getErrorDomainStringForObjC(const EnumDecl *ED) {
7171
outerTypes.push_back(D);
7272
}
7373

74-
std::string buffer = ED->getParentModule()->getNameStr();
74+
std::string buffer = ED->getParentModule()->getNameStr().str();
7575
for (auto D : llvm::reverse(outerTypes)) {
7676
buffer += ".";
7777
buffer += D->getNameStr();

Diff for: lib/AST/USRGeneration.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
215215
if (auto ClangD = ClangN.getAsDecl()) {
216216
bool Ignore = clang::index::generateUSRForDecl(ClangD, Buffer);
217217
if (!Ignore) {
218-
return Buffer.str();
218+
return std::string(Buffer.str());
219219
} else {
220220
return std::string();
221221
}
@@ -229,7 +229,7 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
229229
ClangMacroInfo->getDefinitionLoc(),
230230
Importer.getClangASTContext().getSourceManager(), Buffer);
231231
if (!Ignore)
232-
return Buffer.str();
232+
return std::string(Buffer.str());
233233
else
234234
return std::string();
235235
}
@@ -238,7 +238,7 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
238238
if (printObjCUSR(D, OS)) {
239239
return std::string();
240240
} else {
241-
return OS.str();
241+
return std::string(OS.str());
242242
}
243243
}
244244

Diff for: lib/ASTSectionImporter/ASTSectionImporter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
4747

4848
// Register the memory buffer.
4949
Loader.registerMemoryBuffer(info.name, std::move(bitstream));
50-
foundModules.push_back(info.name);
50+
foundModules.push_back(info.name.str());
5151
}
5252
} else {
5353
llvm::dbgs() << "Unable to load module";

Diff for: lib/Basic/SourceLoc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool SourceManager::openVirtualFile(SourceLoc loc, StringRef name,
105105
}
106106

107107
CharSourceRange range = CharSourceRange(*this, loc, end);
108-
VirtualFiles[end.Value.getPointer()] = { range, name, lineOffset };
108+
VirtualFiles[end.Value.getPointer()] = {range, name.str(), lineOffset};
109109
CachedVFile = {nullptr, nullptr};
110110
return true;
111111
}

Diff for: lib/ClangImporter/ClangImporter.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,8 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
481481
if (!llvm::sys::fs::exists(shimsPath)) {
482482
shimsPath = searchPathOpts.SDKPath;
483483
llvm::sys::path::append(shimsPath, "usr", "lib", "swift", "shims");
484-
invocationArgStrs.insert(invocationArgStrs.end(), {
485-
"-isystem", shimsPath.str()
486-
});
484+
invocationArgStrs.insert(invocationArgStrs.end(),
485+
{"-isystem", std::string(shimsPath.str())});
487486
}
488487

489488
// Construct the invocation arguments for the current target.
@@ -638,7 +637,7 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
638637
llvm::sys::path::native(path);
639638

640639
invocationArgStrs.push_back("-isystem");
641-
invocationArgStrs.push_back(path.str());
640+
invocationArgStrs.push_back(std::string(path.str()));
642641
} else {
643642
// On Darwin, Clang uses -isysroot to specify the include
644643
// system root. On other targets, it seems to use --sysroot.
@@ -750,7 +749,7 @@ addCommonInvocationArguments(std::vector<std::string> &invocationArgStrs,
750749

751750
// Set the Clang resource directory to the path we computed.
752751
invocationArgStrs.push_back("-resource-dir");
753-
invocationArgStrs.push_back(resourceDir.str());
752+
invocationArgStrs.push_back(std::string(resourceDir.str()));
754753
} else {
755754
invocationArgStrs.push_back("-resource-dir");
756755
invocationArgStrs.push_back(overrideResourceDir);
@@ -1535,7 +1534,7 @@ ClangImporter::emitBridgingPCH(StringRef headerPath,
15351534

15361535
auto &FrontendOpts = invocation.getFrontendOpts();
15371536
FrontendOpts.Inputs = {inputFile};
1538-
FrontendOpts.OutputFile = outputPCHPath;
1537+
FrontendOpts.OutputFile = outputPCHPath.str();
15391538
FrontendOpts.ProgramAction = clang::frontend::GeneratePCH;
15401539

15411540
auto action = wrapActionForIndexingIfEnabled(
@@ -1559,7 +1558,7 @@ bool ClangImporter::emitPrecompiledModule(StringRef moduleMapPath,
15591558

15601559
auto LangOpts = invocation.getLangOpts();
15611560
LangOpts->setCompilingModule(clang::LangOptions::CMK_ModuleMap);
1562-
LangOpts->ModuleName = moduleName;
1561+
LangOpts->ModuleName = moduleName.str();
15631562
LangOpts->CurrentModule = LangOpts->ModuleName;
15641563

15651564
auto language = getLanguageFromOptions(LangOpts);
@@ -1569,8 +1568,8 @@ bool ClangImporter::emitPrecompiledModule(StringRef moduleMapPath,
15691568

15701569
auto &FrontendOpts = invocation.getFrontendOpts();
15711570
FrontendOpts.Inputs = {inputFile};
1572-
FrontendOpts.OriginalModuleMap = moduleMapPath;
1573-
FrontendOpts.OutputFile = outputPath;
1571+
FrontendOpts.OriginalModuleMap = moduleMapPath.str();
1572+
FrontendOpts.OutputFile = outputPath.str();
15741573
FrontendOpts.ProgramAction = clang::frontend::GenerateModule;
15751574

15761575
auto action = wrapActionForIndexingIfEnabled(
@@ -1598,7 +1597,7 @@ bool ClangImporter::dumpPrecompiledModule(StringRef modulePath,
15981597

15991598
auto &FrontendOpts = invocation.getFrontendOpts();
16001599
FrontendOpts.Inputs = {inputFile};
1601-
FrontendOpts.OutputFile = outputPath;
1600+
FrontendOpts.OutputFile = outputPath.str();
16021601

16031602
auto action = std::make_unique<clang::DumpModuleInfoAction>();
16041603
dumpInstance->ExecuteAction(*action);

Diff for: lib/Demangling/NodePrinter.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ class NodePrinter {
692692
auto Label = LabelList->getChild(Index);
693693
assert(Label && (Label->getKind() == Node::Kind::Identifier ||
694694
Label->getKind() == Node::Kind::FirstElementMarker));
695-
return Label->getKind() == Node::Kind::Identifier ? Label->getText()
695+
return Label->getKind() == Node::Kind::Identifier ? Label->getText().str()
696696
: "_";
697697
};
698698

@@ -1147,7 +1147,8 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
11471147
return nullptr;
11481148
case Node::Kind::Suffix:
11491149
if (Options.DisplayUnmangledSuffix) {
1150-
Printer << " with unmangled suffix " << QuotedString(Node->getText());
1150+
Printer << " with unmangled suffix "
1151+
<< QuotedString(Node->getText().str());
11511152
}
11521153
return nullptr;
11531154
case Node::Kind::Initializer:

Diff for: lib/Demangling/OldRemangler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace {
4848
}
4949

5050
void setAnonymousContextDiscriminator(StringRef discriminator) {
51-
AnonymousContextDiscriminator = discriminator;
51+
AnonymousContextDiscriminator = discriminator.str();
5252
}
5353

5454
std::string takeAnonymousContextDiscriminator() {

Diff for: lib/Driver/CoarseGrainedDependencyGraph.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,16 @@ CoarseGrainedDependencyGraphImpl::loadFromBuffer(const void *node,
268268
});
269269

270270
if (iter == provides.end())
271-
provides.push_back({name, kind});
271+
provides.push_back({name.str(), kind});
272272
else
273273
iter->kindMask |= kind;
274274

275275
return LoadResult::UpToDate;
276276
};
277277

278278
auto interfaceHashCallback = [this, node](StringRef hash) -> LoadResult {
279-
auto insertResult = InterfaceHashes.insert(std::make_pair(node, hash));
279+
auto insertResult =
280+
InterfaceHashes.insert(std::make_pair(node, hash.str()));
280281

281282
if (insertResult.second) {
282283
// Treat a newly-added hash as up-to-date. This includes the initial
@@ -286,7 +287,7 @@ CoarseGrainedDependencyGraphImpl::loadFromBuffer(const void *node,
286287

287288
auto iter = insertResult.first;
288289
if (hash != iter->second) {
289-
iter->second = hash;
290+
iter->second = hash.str();
290291
return LoadResult::AffectsDownstream;
291292
}
292293

Diff for: lib/Driver/Driver.cpp

+23-19
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
16491649
// REPL mode should always use the REPL module.
16501650
OI.ModuleName = "REPL";
16511651
} else if (const Arg *A = Args.getLastArg(options::OPT_o)) {
1652-
OI.ModuleName = llvm::sys::path::stem(A->getValue());
1652+
OI.ModuleName = llvm::sys::path::stem(A->getValue()).str();
16531653
if ((OI.LinkAction == LinkKind::DynamicLibrary ||
16541654
OI.LinkAction == LinkKind::StaticLibrary) &&
16551655
!llvm::sys::path::extension(A->getValue()).empty() &&
@@ -1658,7 +1658,8 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
16581658
OI.ModuleName.erase(0, strlen("lib"));
16591659
}
16601660
} else if (Inputs.size() == 1) {
1661-
OI.ModuleName = llvm::sys::path::stem(Inputs.front().second->getValue());
1661+
OI.ModuleName =
1662+
llvm::sys::path::stem(Inputs.front().second->getValue()).str();
16621663
}
16631664

16641665
if (!Lexer::isIdentifier(OI.ModuleName) ||
@@ -2187,10 +2188,10 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
21872188
if (const Arg *A = Args.getLastArg(options::OPT_driver_use_frontend_path)) {
21882189
DriverExecutable = A->getValue();
21892190
std::string commandString =
2190-
Args.getLastArgValue(options::OPT_driver_use_frontend_path);
2191+
Args.getLastArgValue(options::OPT_driver_use_frontend_path).str();
21912192
SmallVector<StringRef, 10> commandArgs;
21922193
StringRef(commandString).split(commandArgs, ';', -1, false);
2193-
DriverExecutable = commandArgs[0];
2194+
DriverExecutable = commandArgs[0].str();
21942195
DriverExecutableArgs.assign(std::begin(commandArgs) + 1,
21952196
std::end(commandArgs));
21962197
}
@@ -2837,26 +2838,29 @@ Job *Driver::buildJobsForAction(Compilation &C, const JobAction *JA,
28372838
[] { llvm::outs() << ", "; });
28382839
if (!InputActions.empty() && !J->getInputs().empty())
28392840
llvm::outs() << ", ";
2840-
interleave(J->getInputs().begin(), J->getInputs().end(),
2841-
[](const Job *Input) {
2842-
auto FileNames = Input->getOutput().getPrimaryOutputFilenames();
2843-
interleave(FileNames.begin(), FileNames.end(),
2844-
[](const std::string &FileName) {
2845-
llvm::outs() << '"' << FileName << '"';
2846-
},
2847-
[] { llvm::outs() << ", "; });
2848-
},
2849-
[] { llvm::outs() << ", "; });
2841+
interleave(
2842+
J->getInputs().begin(), J->getInputs().end(),
2843+
[](const Job *Input) {
2844+
auto FileNames = Input->getOutput().getPrimaryOutputFilenames();
2845+
interleave(
2846+
FileNames.begin(), FileNames.end(),
2847+
[](StringRef FileName) {
2848+
llvm::outs() << '"' << FileName << '"';
2849+
},
2850+
[] { llvm::outs() << ", "; });
2851+
},
2852+
[] { llvm::outs() << ", "; });
28502853

28512854
llvm::outs() << "], output: {";
28522855
auto OutputFileNames = J->getOutput().getPrimaryOutputFilenames();
28532856
StringRef TypeName =
28542857
file_types::getTypeName(J->getOutput().getPrimaryOutputType());
2855-
interleave(OutputFileNames.begin(), OutputFileNames.end(),
2856-
[TypeName](const std::string &FileName) {
2857-
llvm::outs() << TypeName << ": \"" << FileName << '"';
2858-
},
2859-
[] { llvm::outs() << ", "; });
2858+
interleave(
2859+
OutputFileNames.begin(), OutputFileNames.end(),
2860+
[TypeName](StringRef FileName) {
2861+
llvm::outs() << TypeName << ": \"" << FileName << '"';
2862+
},
2863+
[] { llvm::outs() << ", "; });
28602864

28612865
file_types::forAllTypes([&J](file_types::ID Ty) {
28622866
StringRef AdditionalOutput =

0 commit comments

Comments
 (0)