Skip to content

Commit adcd026

Browse files
committed
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.
1 parent 5eaf44f commit adcd026

File tree

895 files changed

+3319
-3014
lines changed

Some content is hidden

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

895 files changed

+3319
-3014
lines changed

clang-tools-extra/clang-change-namespace/ChangeNamespace.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ inline std::string
2323
joinNamespaces(const llvm::SmallVectorImpl<StringRef> &Namespaces) {
2424
if (Namespaces.empty())
2525
return "";
26-
std::string Result = Namespaces.front();
26+
std::string Result(Namespaces.front());
2727
for (auto I = Namespaces.begin() + 1, E = Namespaces.end(); I != E; ++I)
2828
Result += ("::" + *I).str();
2929
return Result;
@@ -184,7 +184,7 @@ void addReplacementOrDie(
184184
const SourceManager &SM,
185185
std::map<std::string, tooling::Replacements> *FileToReplacements) {
186186
const auto R = createReplacement(Start, End, ReplacementText, SM);
187-
auto Err = (*FileToReplacements)[R.getFilePath()].add(R);
187+
auto Err = (*FileToReplacements)[std::string(R.getFilePath())].add(R);
188188
if (Err)
189189
llvm_unreachable(llvm::toString(std::move(Err)).c_str());
190190
}
@@ -213,18 +213,18 @@ std::string getShortestQualifiedNameInNamespace(llvm::StringRef DeclName,
213213
DeclName = DeclName.ltrim(':');
214214
NsName = NsName.ltrim(':');
215215
if (DeclName.find(':') == llvm::StringRef::npos)
216-
return DeclName;
216+
return std::string(DeclName);
217217

218218
auto NsNameSplitted = splitSymbolName(NsName);
219219
auto DeclNsSplitted = splitSymbolName(DeclName);
220220
llvm::StringRef UnqualifiedDeclName = DeclNsSplitted.pop_back_val();
221221
// If the Decl is in global namespace, there is no need to shorten it.
222222
if (DeclNsSplitted.empty())
223-
return UnqualifiedDeclName;
223+
return std::string(UnqualifiedDeclName);
224224
// If NsName is the global namespace, we can simply use the DeclName sans
225225
// leading "::".
226226
if (NsNameSplitted.empty())
227-
return DeclName;
227+
return std::string(DeclName);
228228

229229
if (NsNameSplitted.front() != DeclNsSplitted.front()) {
230230
// The DeclName must be fully-qualified, but we still need to decide if a
@@ -233,7 +233,7 @@ std::string getShortestQualifiedNameInNamespace(llvm::StringRef DeclName,
233233
// to avoid conflict.
234234
if (llvm::is_contained(NsNameSplitted, DeclNsSplitted.front()))
235235
return ("::" + DeclName).str();
236-
return DeclName;
236+
return std::string(DeclName);
237237
}
238238
// Since there is already an overlap namespace, we know that `DeclName` can be
239239
// shortened, so we reduce the longest common prefix.
@@ -711,7 +711,7 @@ void ChangeNamespaceTool::moveOldNamespace(
711711
MoveNs.InsertionOffset = SM.getFileOffset(SM.getSpellingLoc(InsertionLoc));
712712
MoveNs.FID = SM.getFileID(Start);
713713
MoveNs.SourceMgr = Result.SourceManager;
714-
MoveNamespaces[SM.getFilename(Start)].push_back(MoveNs);
714+
MoveNamespaces[std::string(SM.getFilename(Start))].push_back(MoveNs);
715715
}
716716

717717
// Removes a class forward declaration from the code in the moved namespace and
@@ -762,7 +762,7 @@ void ChangeNamespaceTool::moveClassForwardDeclaration(
762762
InsertForwardDeclaration InsertFwd;
763763
InsertFwd.InsertionOffset = Insertion.getOffset();
764764
InsertFwd.ForwardDeclText = Insertion.getReplacementText().str();
765-
InsertFwdDecls[Insertion.getFilePath()].push_back(InsertFwd);
765+
InsertFwdDecls[std::string(Insertion.getFilePath())].push_back(InsertFwd);
766766
}
767767

768768
// Replaces a qualified symbol (in \p DeclCtx) that refers to a declaration \p
@@ -816,7 +816,7 @@ void ChangeNamespaceTool::replaceQualifiedSymbolInDeclContext(
816816
->getQualifiedNameAsString())) {
817817
FromDeclNameRef = FromDeclNameRef.drop_front(2);
818818
if (FromDeclNameRef.size() < ReplaceName.size())
819-
ReplaceName = FromDeclNameRef;
819+
ReplaceName = std::string(FromDeclNameRef);
820820
}
821821
}
822822
// Checks if there is any namespace alias declarations that can shorten the

clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ llvm::ErrorOr<std::vector<std::string>> GetWhiteListedSymbolPatterns() {
9191
llvm::StringRef Content = File.get()->getBuffer();
9292
Content.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
9393
for (auto Line : Lines)
94-
Patterns.push_back(Line.trim());
94+
Patterns.push_back(std::string(Line.trim()));
9595
return Patterns;
9696
}
9797

clang-tools-extra/clang-doc/Representation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
286286
if (SourceRoot.empty())
287287
// If no SourceRoot was provided the current path is used as the default
288288
llvm::sys::fs::current_path(SourceRootDir);
289-
this->SourceRoot = SourceRootDir.str();
289+
this->SourceRoot = std::string(SourceRootDir.str());
290290
if (!RepositoryUrl.empty()) {
291-
this->RepositoryUrl = RepositoryUrl;
291+
this->RepositoryUrl = std::string(RepositoryUrl);
292292
if (!RepositoryUrl.empty() && RepositoryUrl.find("http://") != 0 &&
293293
RepositoryUrl.find("https://") != 0)
294294
this->RepositoryUrl->insert(0, "https://");

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ int main(int argc, const char **argv) {
232232
llvm::sys::path::native(AssetsPath, IndexJS);
233233
llvm::sys::path::append(IndexJS, "index.js");
234234
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
235-
DefaultStylesheet.str());
235+
std::string(DefaultStylesheet.str()));
236236
CDCtx.FilesToCopy.emplace_back(IndexJS.str());
237237
}
238238

clang-tools-extra/clang-include-fixer/InMemorySymbolIndex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ namespace include_fixer {
1616
InMemorySymbolIndex::InMemorySymbolIndex(
1717
const std::vector<SymbolAndSignals> &Symbols) {
1818
for (const auto &Symbol : Symbols)
19-
LookupTable[Symbol.Symbol.getName()].push_back(Symbol);
19+
LookupTable[std::string(Symbol.Symbol.getName())].push_back(Symbol);
2020
}
2121

2222
std::vector<SymbolAndSignals>
2323
InMemorySymbolIndex::search(llvm::StringRef Identifier) {
24-
auto I = LookupTable.find(Identifier);
24+
auto I = LookupTable.find(std::string(Identifier));
2525
if (I != LookupTable.end())
2626
return I->second;
2727
return {};

clang-tools-extra/clang-include-fixer/IncludeFixer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ std::string IncludeFixerSemaSource::minimizeInclude(
302302
StringRef Include, const clang::SourceManager &SourceManager,
303303
clang::HeaderSearch &HeaderSearch) const {
304304
if (!MinimizeIncludePaths)
305-
return Include;
305+
return std::string(Include);
306306

307307
// Get the FileEntry for the include.
308308
StringRef StrippedInclude = Include.trim("\"<>");
@@ -311,7 +311,7 @@ std::string IncludeFixerSemaSource::minimizeInclude(
311311
// If the file doesn't exist return the path from the database.
312312
// FIXME: This should never happen.
313313
if (!Entry)
314-
return Include;
314+
return std::string(Include);
315315

316316
bool IsSystem = false;
317317
std::string Suggestion =
@@ -352,7 +352,8 @@ IncludeFixerSemaSource::query(StringRef Query, StringRef ScopedQualifiers,
352352
if (!GenerateDiagnostics && !QuerySymbolInfos.empty()) {
353353
if (ScopedQualifiers == QuerySymbolInfos.front().ScopedQualifiers &&
354354
Query == QuerySymbolInfos.front().RawIdentifier) {
355-
QuerySymbolInfos.push_back({Query.str(), ScopedQualifiers, Range});
355+
QuerySymbolInfos.push_back(
356+
{Query.str(), std::string(ScopedQualifiers), Range});
356357
}
357358
return {};
358359
}
@@ -367,7 +368,8 @@ IncludeFixerSemaSource::query(StringRef Query, StringRef ScopedQualifiers,
367368
CI->getSourceManager().getLocForStartOfFile(
368369
CI->getSourceManager().getMainFileID()));
369370

370-
QuerySymbolInfos.push_back({Query.str(), ScopedQualifiers, Range});
371+
QuerySymbolInfos.push_back(
372+
{Query.str(), std::string(ScopedQualifiers), Range});
371373

372374
// Query the symbol based on C++ name Lookup rules.
373375
// Firstly, lookup the identifier with scoped namespace contexts;

clang-tools-extra/clang-include-fixer/IncludeFixer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ class IncludeFixerSemaSource : public clang::ExternalSemaSource {
9292
GenerateDiagnostics(GenerateDiagnostics) {}
9393

9494
void setCompilerInstance(CompilerInstance *CI) { this->CI = CI; }
95-
void setFilePath(StringRef FilePath) { this->FilePath = FilePath; }
95+
void setFilePath(StringRef FilePath) {
96+
this->FilePath = std::string(FilePath);
97+
}
9698

9799
/// Callback for incomplete types. If we encounter a forward declaration we
98100
/// have the fully qualified name ready. Just query that.

clang-tools-extra/clang-include-fixer/IncludeFixerContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ std::string createQualifiedNameForReplacement(
2929
// No need to add missing qualifiers if SymbolIdentifier has a global scope
3030
// operator "::".
3131
if (RawSymbolName.startswith("::"))
32-
return RawSymbolName;
32+
return std::string(RawSymbolName);
3333

3434
std::string QualifiedName = MatchedSymbol.getQualifiedName();
3535

clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ void FindAllSymbols::run(const MatchFinder::MatchResult &Result) {
251251

252252
const SourceManager *SM = Result.SourceManager;
253253
if (auto Symbol = CreateSymbolInfo(ND, *SM, Collector)) {
254-
Filename = SM->getFileEntryForID(SM->getMainFileID())->getName();
254+
Filename =
255+
std::string(SM->getFileEntryForID(SM->getMainFileID())->getName());
255256
FileSymbols[*Symbol] += Signals;
256257
}
257258
}

clang-tools-extra/clang-include-fixer/find-all-symbols/HeaderMapCollector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class HeaderMapCollector {
2929

3030
void addHeaderMapping(llvm::StringRef OrignalHeaderPath,
3131
llvm::StringRef MappingHeaderPath) {
32-
HeaderMappingTable[OrignalHeaderPath] = MappingHeaderPath;
32+
HeaderMappingTable[OrignalHeaderPath] = std::string(MappingHeaderPath);
3333
};
3434

3535
/// Check if there is a mapping from \p Header or a regex pattern that matches

0 commit comments

Comments
 (0)