[ADT] Make StringRef's std::string conversion operator explicit

This has the same behavior as converting std::string_view to
std::string. This is an expensive conversion, so explicit conversions
are helpful for avoiding unneccessary string copies.
This commit is contained in:
Benjamin Kramer 2020-01-28 23:30:02 +01:00
parent 076da521f3
commit 777180a32b
13 changed files with 23 additions and 21 deletions

View File

@ -594,7 +594,8 @@ protected:
std::string ReadName;
LookupRequest Req;
Req.IDs.insert(TestSymbol.ID);
Target.lookup(Req, [&](const Symbol &S) { ReadName = S.Name; });
Target.lookup(Req,
[&](const Symbol &S) { ReadName = std::string(S.Name); });
// The index was rebuild if the name is up to date.
return ReadName == VersionStorage.back();
}

View File

@ -332,7 +332,7 @@ TEST(DiagnosticTest, LongFixMessages) {
n]] = 10; // error-ok
}
)cpp");
TU.Code = Source.code();
TU.Code = std::string(Source.code());
EXPECT_THAT(TU.build().getDiagnostics(),
ElementsAre(WithFix(
Fix(Source.range(), "ident", "change 'ide\\…' to 'ident'"))));
@ -846,7 +846,7 @@ TEST(DiagsInHeaders, DiagInsideHeader) {
void foo() {})cpp");
Annotations Header("[[no_type_spec]]; // error-ok");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
TU.AdditionalFiles = {{"a.h", std::string(Header.code())}};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(AllOf(
Diag(Main.range(), "in included file: C++ requires a "
@ -953,7 +953,7 @@ TEST(DiagsInHeaders, OnlyErrorOrFatal) {
[[no_type_spec]]; // error-ok
int x = 5/0;)cpp");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
TU.AdditionalFiles = {{"a.h", std::string(Header.code())}};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(AllOf(
Diag(Main.range(), "in included file: C++ requires "
@ -969,7 +969,7 @@ TEST(DiagsInHeaders, FromNonWrittenSources) {
int x = 5/0;
int b = [[FOO]]; // error-ok)cpp");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
TU.AdditionalFiles = {{"a.h", std::string(Header.code())}};
TU.ExtraArgs = {"-DFOO=NOOO"};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(AllOf(
@ -988,7 +988,7 @@ TEST(DiagsInHeaders, ErrorFromMacroExpansion) {
#define X foo
X;)cpp");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
TU.AdditionalFiles = {{"a.h", std::string(Header.code())}};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(
Diag(Main.range(), "in included file: use of undeclared "
@ -1005,7 +1005,7 @@ TEST(DiagsInHeaders, ErrorFromMacroArgument) {
#define X(arg) arg
X(foo);)cpp");
TestTU TU = TestTU::withCode(Main.code());
TU.AdditionalFiles = {{"a.h", Header.code()}};
TU.AdditionalFiles = {{"a.h", std::string(Header.code())}};
EXPECT_THAT(TU.build().getDiagnostics(),
UnorderedElementsAre(
Diag(Main.range(), "in included file: use of undeclared "

View File

@ -296,7 +296,7 @@ std::string segment(llvm::StringRef Text) {
}
// this is a no-op hack so clang-format will vertically align our testcases.
llvm::StringRef returns(llvm::StringRef Text) { return Text; }
std::string returns(llvm::StringRef Text) { return std::string(Text); }
TEST(FuzzyMatch, Segmentation) {
EXPECT_THAT(segment("std::basic_string"), //

View File

@ -113,7 +113,7 @@ void checkHighlightings(llvm::StringRef Code,
TU.ExtraArgs.push_back("-std=c++2a");
for (auto File : AdditionalFiles)
TU.AdditionalFiles.insert({File.first, File.second});
TU.AdditionalFiles.insert({File.first, std::string(File.second)});
auto AST = TU.build();
EXPECT_EQ(Code, annotate(Test.code(), getSemanticHighlightings(AST)));

View File

@ -389,10 +389,10 @@ TEST(SourceCodeTests, CollectWords) {
// this is a comment
std::string getSomeText() { return "magic word"; }
)cpp");
std::set<std::string> ActualWords(Words.keys().begin(), Words.keys().end());
std::set<std::string> ExpectedWords = {"define", "fizz", "buzz", "this",
"comment", "string", "some", "text",
"return", "magic", "word"};
std::set<StringRef> ActualWords(Words.keys().begin(), Words.keys().end());
std::set<StringRef> ExpectedWords = {"define", "fizz", "buzz", "this",
"comment", "string", "some", "text",
"return", "magic", "word"};
EXPECT_EQ(ActualWords, ExpectedWords);
}

View File

@ -1117,7 +1117,7 @@ TEST(FindReferences, NoQueryForLocalSymbols) {
TEST(GetNonLocalDeclRefs, All) {
struct Case {
llvm::StringRef AnnotatedCode;
std::vector<llvm::StringRef> ExpectedDecls;
std::vector<std::string> ExpectedDecls;
} Cases[] = {
{
// VarDecl and ParamVarDecl

View File

@ -47,7 +47,7 @@ void HandlePacket(MockServer &server,
StringRef response) {
StringExtractorGDBRemote request;
ASSERT_EQ(PacketResult::Success, server.GetPacket(request));
ASSERT_THAT(request.GetStringRef(), expected);
ASSERT_THAT(std::string(request.GetStringRef()), expected);
ASSERT_EQ(PacketResult::Success, server.SendPacket(response));
}

View File

@ -15,7 +15,7 @@ using namespace lldb_private;
static std::string to_string(const EventDataBytes &E) {
StreamString S;
E.Dump(&S);
return S.GetString();
return std::string(S.GetString());
}
TEST(EventTest, DumpEventDataBytes) {

View File

@ -157,7 +157,7 @@ TEST(ScalarTest, ExtractBitfield) {
template <typename T> static std::string ScalarGetValue(T value) {
StreamString stream;
Scalar(value).GetValue(&stream, false);
return stream.GetString();
return std::string(stream.GetString());
}
TEST(ScalarTest, GetValue) {

View File

@ -96,7 +96,8 @@ Expected<JThreadsInfo> JThreadsInfo::create(StringRef Response,
ArrayRef<RegisterInfo> RegInfos) {
JThreadsInfo jthreads_info;
StructuredData::ObjectSP json = StructuredData::ParseJSON(Response);
StructuredData::ObjectSP json =
StructuredData::ParseJSON(std::string(Response));
StructuredData::Array *array = json->GetAsArray();
if (!array)
return make_parsing_error("JThreadsInfo: JSON array");

View File

@ -30,6 +30,6 @@ std::string TestBase::getLogFileName() {
sys::path::append(DirStr, std::string("server-") +
test_info->test_case_name() + "-" +
test_info->name() + ".log");
return DirStr.str();
return std::string(DirStr.str());
}

View File

@ -36,7 +36,7 @@ public:
static std::string getInferiorPath(llvm::StringRef Name) {
llvm::SmallString<64> Path(LLDB_TEST_INFERIOR_PATH);
llvm::sys::path::append(Path, Name + LLDB_TEST_INFERIOR_SUFFIX);
return Path.str();
return std::string(Path.str());
}
static std::string getLogFileName();

View File

@ -272,7 +272,7 @@ namespace llvm {
/// @name Type Conversions
/// @{
operator std::string() const { return str(); }
explicit operator std::string() const { return str(); }
#if __cplusplus > 201402L
operator std::string_view() const {