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/clang/unittests/AST/ASTImporterFixtures.cpp b/clang/unittests/AST/ASTImporterFixtures.cpp
index 80fcd1f..36732ee 100644
--- a/clang/unittests/AST/ASTImporterFixtures.cpp
+++ b/clang/unittests/AST/ASTImporterFixtures.cpp
@@ -41,7 +41,7 @@
ASTImporterTestBase::TU::TU(StringRef Code, StringRef FileName, ArgVector Args,
ImporterConstructor C,
ASTImporter::ODRHandlingType ODRHandling)
- : Code(Code), FileName(FileName),
+ : Code(std::string(Code)), FileName(std::string(FileName)),
Unit(tooling::buildASTFromCodeWithArgs(this->Code, Args, this->FileName)),
TUDecl(Unit->getASTContext().getTranslationUnitDecl()), Creator(C),
ODRHandling(ODRHandling) {
@@ -118,7 +118,7 @@
return;
ArgVector ToArgs = getArgVectorForLanguage(ToLang);
// Source code must be a valid live buffer through the tests lifetime.
- ToCode = ToSrcCode;
+ ToCode = std::string(ToSrcCode);
// Build the AST from an empty file.
ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, FileName);
ToAST->enableSourceFileDiagnostics();
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 3e8f804..8c3dc6c 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -115,8 +115,8 @@
const BindableMatcher<NodeType> &VerificationMatcher) {
return testImport(
FromCode, FromArgs, ToCode, ToArgs, Verifier,
- translationUnitDecl(
- has(namedDecl(hasName(DeclToImportID)).bind(DeclToImportID))),
+ translationUnitDecl(has(namedDecl(hasName(std::string(DeclToImportID)))
+ .bind(DeclToImportID))),
VerificationMatcher);
}
diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp
index 5a0a804..3bb17ab 100644
--- a/clang/unittests/AST/DeclPrinterTest.cpp
+++ b/clang/unittests/AST/DeclPrinterTest.cpp
@@ -108,12 +108,9 @@
StringRef ExpectedPrinted,
PrintingPolicyModifier PolicyModifier = nullptr) {
std::vector<std::string> Args(1, "-std=c++98");
- return PrintedDeclMatches(Code,
- Args,
- namedDecl(hasName(DeclName)).bind("id"),
- ExpectedPrinted,
- "input.cc",
- PolicyModifier);
+ return PrintedDeclMatches(
+ Code, Args, namedDecl(hasName(std::string(DeclName))).bind("id"),
+ ExpectedPrinted, "input.cc", PolicyModifier);
}
::testing::AssertionResult
@@ -133,11 +130,9 @@
StringRef DeclName,
StringRef ExpectedPrinted) {
std::vector<std::string> Args(1, "-std=c++11");
- return PrintedDeclMatches(Code,
- Args,
- namedDecl(hasName(DeclName)).bind("id"),
- ExpectedPrinted,
- "input.cc");
+ return PrintedDeclMatches(
+ Code, Args, namedDecl(hasName(std::string(DeclName))).bind("id"),
+ ExpectedPrinted, "input.cc");
}
::testing::AssertionResult PrintedDeclCXX11Matches(
diff --git a/clang/unittests/AST/NamedDeclPrinterTest.cpp b/clang/unittests/AST/NamedDeclPrinterTest.cpp
index a5c3e19..d5077b8 100644
--- a/clang/unittests/AST/NamedDeclPrinterTest.cpp
+++ b/clang/unittests/AST/NamedDeclPrinterTest.cpp
@@ -112,47 +112,45 @@
PrintedNamedDeclCXX98Matches(StringRef Code, StringRef DeclName,
StringRef ExpectedPrinted) {
std::vector<std::string> Args(1, "-std=c++98");
- return PrintedNamedDeclMatches(Code,
- Args,
- /*SuppressUnwrittenScope*/ false,
- namedDecl(hasName(DeclName)).bind("id"),
- ExpectedPrinted,
- "input.cc");
+ return PrintedNamedDeclMatches(
+ Code, Args,
+ /*SuppressUnwrittenScope*/ false,
+ namedDecl(hasName(std::string(DeclName))).bind("id"), ExpectedPrinted,
+ "input.cc");
}
::testing::AssertionResult
PrintedWrittenNamedDeclCXX11Matches(StringRef Code, StringRef DeclName,
StringRef ExpectedPrinted) {
std::vector<std::string> Args(1, "-std=c++11");
- return PrintedNamedDeclMatches(Code,
- Args,
- /*SuppressUnwrittenScope*/ true,
- namedDecl(hasName(DeclName)).bind("id"),
- ExpectedPrinted,
- "input.cc");
+ return PrintedNamedDeclMatches(
+ Code, Args,
+ /*SuppressUnwrittenScope*/ true,
+ namedDecl(hasName(std::string(DeclName))).bind("id"), ExpectedPrinted,
+ "input.cc");
}
::testing::AssertionResult
PrintedWrittenPropertyDeclObjCMatches(StringRef Code, StringRef DeclName,
StringRef ExpectedPrinted) {
std::vector<std::string> Args{"-std=c++11", "-xobjective-c++"};
- return PrintedNamedDeclMatches(Code,
- Args,
- /*SuppressUnwrittenScope*/ true,
- objcPropertyDecl(hasName(DeclName)).bind("id"),
- ExpectedPrinted,
- "input.m");
+ return PrintedNamedDeclMatches(
+ Code, Args,
+ /*SuppressUnwrittenScope*/ true,
+ objcPropertyDecl(hasName(std::string(DeclName))).bind("id"),
+ ExpectedPrinted, "input.m");
}
::testing::AssertionResult
PrintedNestedNameSpecifierMatches(StringRef Code, StringRef DeclName,
StringRef ExpectedPrinted) {
std::vector<std::string> Args{"-std=c++11"};
- return PrintedDeclMatches(Code, Args, namedDecl(hasName(DeclName)).bind("id"),
- ExpectedPrinted, "input.cc",
- [](llvm::raw_ostream &Out, const NamedDecl *D) {
- D->printNestedNameSpecifier(Out);
- });
+ return PrintedDeclMatches(
+ Code, Args, namedDecl(hasName(std::string(DeclName))).bind("id"),
+ ExpectedPrinted, "input.cc",
+ [](llvm::raw_ostream &Out, const NamedDecl *D) {
+ D->printNestedNameSpecifier(Out);
+ });
}
} // unnamed namespace
diff --git a/clang/unittests/AST/StmtPrinterTest.cpp b/clang/unittests/AST/StmtPrinterTest.cpp
index 080c18b..76195af 100644
--- a/clang/unittests/AST/StmtPrinterTest.cpp
+++ b/clang/unittests/AST/StmtPrinterTest.cpp
@@ -34,7 +34,7 @@
enum class StdVer { CXX98, CXX11, CXX14, CXX17, CXX2a };
DeclarationMatcher FunctionBodyMatcher(StringRef ContainingFunction) {
- return functionDecl(hasName(ContainingFunction),
+ return functionDecl(hasName(std::string(ContainingFunction)),
has(compoundStmt(has(stmt().bind("id")))));
}