Make llvm::StringRef to std::string conversions explicit.
This is how it should've been and brings it more in line with
std::string_view. There should be no functional change here.
This is mostly mechanical from a custom clang-tidy check, with a lot of
manual fixups. It uncovers a lot of minor inefficiencies.
This doesn't actually modify StringRef yet, I'll do that in a follow-up.
diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index 9fcb36e..6b90c06 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -160,7 +160,7 @@
static std::string rewritePath(StringRef s) {
if (fs::exists(s))
return relativeToRoot(s);
- return s;
+ return std::string(s);
}
// Reconstructs command line arguments so that so that you can re-run
@@ -199,7 +199,7 @@
os << toString(*arg) << "\n";
}
}
- return data.str();
+ return std::string(data.str());
}
// Find a file by concatenating given paths. If a resulting path
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 43978cd..b3f8984 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -45,7 +45,7 @@
if (f->toStringCache.empty()) {
if (f->archiveName.empty())
- f->toStringCache = f->getName();
+ f->toStringCache = std::string(f->getName());
else
f->toStringCache = (f->archiveName + "(" + f->getName() + ")").str();
}
@@ -222,7 +222,7 @@
// Concatenates arguments to construct a string representing an error location.
static std::string createFileLineMsg(StringRef path, unsigned line) {
- std::string filename = path::filename(path);
+ std::string filename = std::string(path::filename(path));
std::string lineno = ":" + std::to_string(line);
if (filename == path)
return filename + lineno;
@@ -243,7 +243,7 @@
return createFileLineMsg(fileLine->first, fileLine->second);
// File.sourceFile contains STT_FILE symbol, and that is a last resort.
- return file.sourceFile;
+ return std::string(file.sourceFile);
}
std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec,
@@ -1405,7 +1405,7 @@
BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
uint64_t offsetInArchive)
: InputFile(BitcodeKind, mb) {
- this->archiveName = archiveName;
+ this->archiveName = std::string(archiveName);
std::string path = mb.getBufferIdentifier().str();
if (config->thinLTOIndexOnly)
@@ -1607,7 +1607,7 @@
if (path.consume_back(suffix))
return (path + repl).str();
- return path;
+ return std::string(path);
}
template void BitcodeFile::parse<ELF32LE>();
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index a310ba5..7927810 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -200,7 +200,7 @@
ArrayRef<Symbol *> getGlobalSymbols();
ObjFile(MemoryBufferRef m, StringRef archiveName) : ELFFileBase(ObjKind, m) {
- this->archiveName = archiveName;
+ this->archiveName = std::string(archiveName);
}
void parse(bool ignoreComdats = false);
@@ -298,7 +298,7 @@
LazyObjFile(MemoryBufferRef m, StringRef archiveName,
uint64_t offsetInArchive)
: InputFile(LazyObjKind, m), offsetInArchive(offsetInArchive) {
- this->archiveName = archiveName;
+ this->archiveName = std::string(archiveName);
}
static bool classof(const InputFile *f) { return f->kind() == LazyObjKind; }
@@ -341,7 +341,7 @@
class SharedFile : public ELFFileBase {
public:
SharedFile(MemoryBufferRef m, StringRef defaultSoName)
- : ELFFileBase(SharedKind, m), soName(defaultSoName),
+ : ELFFileBase(SharedKind, m), soName(std::string(defaultSoName)),
isNeeded(!config->asNeeded) {}
// This is actually a vector of Elf_Verdef pointers.
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 7c79879c..dc544fa 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -307,7 +307,7 @@
// File->sourceFile contains STT_FILE symbol that contains a
// source file name. If it's missing, we use an object file name.
- std::string srcFile = getFile<ELFT>()->sourceFile;
+ std::string srcFile = std::string(getFile<ELFT>()->sourceFile);
if (srcFile.empty())
srcFile = toString(file);
@@ -338,7 +338,7 @@
//
// path/to/foo.o:(function bar) in archive path/to/bar.a
std::string InputSectionBase::getObjMsg(uint64_t off) {
- std::string filename = file->getName();
+ std::string filename = std::string(file->getName());
std::string archive;
if (!file->archiveName.empty())
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index d8e343c..9067d7c 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -59,9 +59,9 @@
}
static std::string getThinLTOOutputFile(StringRef modulePath) {
- return lto::getThinLTOOutputFile(modulePath,
- config->thinLTOPrefixReplace.first,
- config->thinLTOPrefixReplace.second);
+ return lto::getThinLTOOutputFile(
+ std::string(modulePath), std::string(config->thinLTOPrefixReplace.first),
+ std::string(config->thinLTOPrefixReplace.second));
}
static lto::Config createConfig() {
@@ -97,23 +97,23 @@
c.PTO.SLPVectorization = c.OptLevel > 1;
// Set up a custom pipeline if we've been asked to.
- c.OptPipeline = config->ltoNewPmPasses;
- c.AAPipeline = config->ltoAAPipeline;
+ c.OptPipeline = std::string(config->ltoNewPmPasses);
+ c.AAPipeline = std::string(config->ltoAAPipeline);
// Set up optimization remarks if we've been asked to.
- c.RemarksFilename = config->optRemarksFilename;
- c.RemarksPasses = config->optRemarksPasses;
+ c.RemarksFilename = std::string(config->optRemarksFilename);
+ c.RemarksPasses = std::string(config->optRemarksPasses);
c.RemarksWithHotness = config->optRemarksWithHotness;
- c.RemarksFormat = config->optRemarksFormat;
+ c.RemarksFormat = std::string(config->optRemarksFormat);
- c.SampleProfile = config->ltoSampleProfile;
+ c.SampleProfile = std::string(config->ltoSampleProfile);
c.UseNewPM = config->ltoNewPassManager;
c.DebugPassManager = config->ltoDebugPassManager;
- c.DwoDir = config->dwoDir;
+ c.DwoDir = std::string(config->dwoDir);
c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
- c.CSIRProfile = config->ltoCSProfileFile;
+ c.CSIRProfile = std::string(config->ltoCSProfileFile);
c.RunCSIRInstr = config->ltoCSProfileGenerate;
if (config->emitLLVM) {
@@ -140,7 +140,8 @@
if (config->thinLTOIndexOnly) {
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
backend = lto::createWriteIndexesThinBackend(
- config->thinLTOPrefixReplace.first, config->thinLTOPrefixReplace.second,
+ std::string(config->thinLTOPrefixReplace.first),
+ std::string(config->thinLTOPrefixReplace.second),
config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
} else if (config->thinLTOJobs != -1U) {
backend = lto::createInProcessThinBackend(config->thinLTOJobs);
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index db97943..b5f4644 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -88,7 +88,7 @@
if (!secRef)
secRef = sec;
}
- sec->location = location;
+ sec->location = std::string(location);
return sec;
}
@@ -324,7 +324,7 @@
if (!file)
return "";
if (file->archiveName.empty())
- return file->getName();
+ return std::string(file->getName());
return (file->archiveName + "(" + file->getName() + ")").str();
}
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index f43a660..f020552 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -128,7 +128,7 @@
struct MemoryRegion {
MemoryRegion(StringRef name, uint64_t origin, uint64_t length, uint32_t flags,
uint32_t negFlags)
- : name(name), origin(origin), length(length), flags(flags),
+ : name(std::string(name)), origin(origin), length(length), flags(flags),
negFlags(negFlags) {}
std::string name;
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 93ec066..888bec3 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -761,7 +761,7 @@
break;
// Substitute name[i].
- newName = name;
+ newName = std::string(name);
for (char c = '0'; c <= 'z'; ++c) {
newName[i] = c;
if (const Symbol *s = suggest(newName))
diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index e0ff56f..7453acf 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -64,7 +64,7 @@
}
std::string ScriptLexer::getCurrentLocation() {
- std::string filename = getCurrentMB().getBufferIdentifier();
+ std::string filename = std::string(getCurrentMB().getBufferIdentifier());
return (filename + ":" + Twine(getLineNumber())).str();
}
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index a0d763e..27e1ae1 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -871,11 +871,11 @@
}
if (consume(">"))
- cmd->memoryRegionName = next();
+ cmd->memoryRegionName = std::string(next());
if (consume("AT")) {
expect(">");
- cmd->lmaRegionName = next();
+ cmd->lmaRegionName = std::string(next());
}
if (cmd->lmaExpr && !cmd->lmaRegionName.empty())
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 4956e27..f5d5ca1 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -28,7 +28,7 @@
static std::string demangle(StringRef symName) {
if (elf::config->demangle)
return demangleItanium(symName);
- return symName;
+ return std::string(symName);
}
std::string toString(const elf::Symbol &b) { return demangle(b.getName()); }
diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp
index 7b0824a..597db1f 100644
--- a/lld/ELF/Target.cpp
+++ b/lld/ELF/Target.cpp
@@ -41,7 +41,7 @@
StringRef s = getELFRelocationTypeName(elf::config->emachine, type);
if (s == "Unknown")
return ("Unknown (" + Twine(type) + ")").str();
- return s;
+ return std::string(s);
}
namespace elf {