Skip to content

Commit 7ef93b2

Browse files
committed
[Basic] Move copyCString to Basic/StringExtras.
Also, use StringRef.copy() instead of copyString().
1 parent 5ab84ae commit 7ef93b2

File tree

8 files changed

+41
-50
lines changed

8 files changed

+41
-50
lines changed

include/swift/Basic/StringExtras.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ namespace swift {
5252
/// Determine the part of speech for the given word.
5353
PartOfSpeech getPartOfSpeech(StringRef word);
5454

55+
/// Copy \p string to \p Allocator and return it as a null terminated C
56+
/// string.
57+
const char *copyCString(StringRef string, llvm::BumpPtrAllocator &Allocator);
58+
5559
/// Scratch space used for returning a set of StringRefs.
5660
class StringScratchSpace {
5761
llvm::BumpPtrAllocator Allocator;
5862

5963
public:
60-
StringRef copyString(StringRef string);
64+
StringRef copyString(StringRef string) { return string.copy(Allocator); }
6165

6266
llvm::BumpPtrAllocator &getAllocator() { return Allocator; }
6367
};

include/swift/IDE/CodeCompletion.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ std::string removeCodeCompletionTokens(StringRef Input,
6464
StringRef TokenName,
6565
unsigned *CompletionOffset);
6666

67-
StringRef copyString(llvm::BumpPtrAllocator &Allocator,
68-
StringRef Str);
69-
70-
const char *copyCString(llvm::BumpPtrAllocator &Allocator,
71-
StringRef Str);
72-
7367
template <typename T>
7468
ArrayRef<T> copyArray(llvm::BumpPtrAllocator &Allocator,
7569
ArrayRef<T> Arr) {

lib/Basic/StringExtras.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,14 @@ StringRef swift::matchLeadingTypeName(StringRef name,
457457
return nameMismatch.getRestOfStr();
458458
}
459459

460-
StringRef StringScratchSpace::copyString(StringRef string) {
461-
void *memory = Allocator.Allocate(string.size(), alignof(char));
460+
const char *swift::copyCString(StringRef string,
461+
llvm::BumpPtrAllocator &Allocator) {
462+
if (string.empty())
463+
return "";
464+
char *memory = Allocator.Allocate<char>(string.size() + 1);
462465
memcpy(memory, string.data(), string.size());
463-
return StringRef(static_cast<char *>(memory), string.size());
466+
memory[string.size()] = '\0';
467+
return memory;
464468
}
465469

466470
void InheritedNameSet::add(StringRef name) {

lib/IDE/CodeCompletion.cpp

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,6 @@ std::string swift::ide::removeCodeCompletionTokens(
111111
return CleanFile;
112112
}
113113

114-
llvm::StringRef swift::ide::copyString(llvm::BumpPtrAllocator &Allocator,
115-
llvm::StringRef Str) {
116-
char *Buffer = Allocator.Allocate<char>(Str.size());
117-
std::copy(Str.begin(), Str.end(), Buffer);
118-
return llvm::StringRef(Buffer, Str.size());
119-
}
120-
121-
const char *swift::ide::copyCString(llvm::BumpPtrAllocator &Allocator,
122-
llvm::StringRef Str) {
123-
char *Buffer = Allocator.Allocate<char>(Str.size() + 1);
124-
std::copy(Str.begin(), Str.end(), Buffer);
125-
Buffer[Str.size()] = '\0';
126-
return Buffer;
127-
}
128-
129114
CodeCompletionString::CodeCompletionString(ArrayRef<Chunk> Chunks) {
130115
std::uninitialized_copy(Chunks.begin(), Chunks.end(),
131116
getTrailingObjects<Chunk>());
@@ -729,7 +714,7 @@ void CodeCompletionResultBuilder::withNestedGroup(
729714

730715
void CodeCompletionResultBuilder::addChunkWithText(
731716
CodeCompletionString::Chunk::ChunkKind Kind, StringRef Text) {
732-
addChunkWithTextNoCopy(Kind, copyString(*Sink.Allocator, Text));
717+
addChunkWithTextNoCopy(Kind, Text.copy(*Sink.Allocator));
733718
}
734719

735720
void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
@@ -1172,7 +1157,7 @@ void CodeCompletionResultBuilder::addTypeAnnotation(Type T, PrintOptions PO,
11721157
}
11731158

11741159
StringRef CodeCompletionContext::copyString(StringRef Str) {
1175-
return ::copyString(*CurrentResults.Allocator, Str);
1160+
return Str.copy(*CurrentResults.Allocator);
11761161
}
11771162

11781163
bool shouldCopyAssociatedUSRForDecl(const ValueDecl *VD) {
@@ -1214,7 +1199,7 @@ ArrayRef<StringRef> copyAssociatedUSRs(llvm::BumpPtrAllocator &Allocator,
12141199
}
12151200

12161201
if (!Ignored)
1217-
USRs.push_back(copyString(Allocator, SS));
1202+
USRs.push_back(SS.str().copy(Allocator));
12181203
});
12191204

12201205
if (!USRs.empty())
@@ -1314,7 +1299,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
13141299
if (!getContextFreeCompletionDiagnostics(ContextFreeNotRecReason, VD,
13151300
severity, messageOS)) {
13161301
ContextFreeDiagnosticSeverity = severity;
1317-
ContextFreeDiagnosticMessage = copyString(*Sink.Allocator, message);
1302+
ContextFreeDiagnosticMessage = message.str().copy(*Sink.Allocator);
13181303
}
13191304
}
13201305
}
@@ -1330,11 +1315,12 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
13301315
ModuleName = Sink.LastModule.second;
13311316
} else {
13321317
if (auto *C = CurrentModule.dyn_cast<const clang::Module *>()) {
1333-
ModuleName = copyString(*Sink.Allocator, C->getFullModuleName());
1318+
ModuleName = StringRef(C->getFullModuleName()).copy(*Sink.Allocator);
13341319
} else {
1335-
ModuleName = copyString(
1336-
*Sink.Allocator,
1337-
CurrentModule.get<const swift::ModuleDecl *>()->getName().str());
1320+
ModuleName = CurrentModule.get<const swift::ModuleDecl *>()
1321+
->getName()
1322+
.str()
1323+
.copy(*Sink.Allocator);
13381324
}
13391325
Sink.LastModule.first = CurrentModule.getOpaqueValue();
13401326
Sink.LastModule.second = ModuleName;
@@ -1343,7 +1329,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
13431329

13441330
ContextFreeResult = ContextFreeCodeCompletionResult::createDeclResult(
13451331
*Sink.Allocator, CCS, AssociatedDecl, ModuleName,
1346-
copyString(*Sink.Allocator, BriefDocComment),
1332+
BriefDocComment.copy(*Sink.Allocator),
13471333
copyAssociatedUSRs(*Sink.Allocator, AssociatedDecl), ResultType,
13481334
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
13491335
ContextFreeDiagnosticMessage);
@@ -1353,14 +1339,14 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
13531339
case CodeCompletionResultKind::Keyword:
13541340
ContextFreeResult = ContextFreeCodeCompletionResult::createKeywordResult(
13551341
*Sink.Allocator, KeywordKind, CCS,
1356-
copyString(*Sink.Allocator, BriefDocComment), ResultType);
1342+
BriefDocComment.copy(*Sink.Allocator), ResultType);
13571343
break;
13581344
case CodeCompletionResultKind::BuiltinOperator:
13591345
case CodeCompletionResultKind::Pattern:
13601346
ContextFreeResult =
13611347
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
13621348
*Sink.Allocator, Kind, CCS, CodeCompletionOperatorKind::None,
1363-
copyString(*Sink.Allocator, BriefDocComment), ResultType,
1349+
BriefDocComment.copy(*Sink.Allocator), ResultType,
13641350
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
13651351
ContextFreeDiagnosticMessage);
13661352
break;
@@ -1383,7 +1369,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
13831369
if (!getContextualCompletionDiagnostics(ContextualNotRecReason, VD,
13841370
severity, messageOS)) {
13851371
ContextualDiagnosticSeverity = severity;
1386-
ContextualDiagnosticMessage = copyString(*Sink.Allocator, message);
1372+
ContextualDiagnosticMessage = message.str().copy(*Sink.Allocator);
13871373
}
13881374
}
13891375
}

lib/IDE/CodeCompletionCache.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/IDE/CodeCompletionCache.h"
1414
#include "swift/Basic/Cache.h"
15+
#include "swift/Basic/StringExtras.h"
1516
#include "llvm/ADT/APInt.h"
1617
#include "llvm/ADT/DenseMap.h"
1718
#include "llvm/ADT/Hashing.h"
@@ -165,8 +166,11 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
165166
}
166167

167168
const char *p = strings + index;
168-
auto size = read32le(p);
169-
auto str = copyString(*V.Allocator, StringRef(p, size));
169+
size_t size = read32le(p);
170+
StringRef str = StringRef(p, size);
171+
// Import the string to the allocator. Use null-terminated string to avoid
172+
// potential copies in clients.
173+
str = StringRef(copyCString(str, *V.Allocator), size);
170174
knownStrings[index] = str;
171175
return str;
172176
};

lib/IDE/CodeCompletionResultPrinter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/IDE/CodeCompletionResultPrinter.h"
1414
#include "swift/AST/ASTPrinter.h"
1515
#include "swift/Basic/LLVM.h"
16+
#include "swift/Basic/StringExtras.h"
1617
#include "swift/IDE/CodeCompletion.h"
1718
#include "swift/Markup/XMLUtils.h"
1819
#include "llvm/Support/raw_ostream.h"
@@ -541,9 +542,7 @@ StringRef swift::ide::getCodeCompletionResultFilterName(
541542
SmallString<32> buf;
542543
llvm::raw_svector_ostream OS(buf);
543544
printCodeCompletionResultFilterName(Str, OS);
544-
size_t size = buf.size();
545-
char *storage = Allocator.Allocate<char>(size + 1);
546-
memcpy(storage, buf.data(), size);
547-
storage[size] = '\0';
548-
return {storage, size};
545+
// Use 'copyCString' to get a null terminated StringRef just in case clients
546+
// need it.
547+
return {copyCString(buf, Allocator), buf.size()};
549548
}

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
126126

127127
auto addCompletion = [&](CustomCompletionInfo customCompletion) {
128128
using Chunk = CodeCompletionString::Chunk;
129-
auto nameCopy = copyString(sink.allocator, customCompletion.Name);
129+
auto nameCopy = StringRef(customCompletion.Name).copy(sink.allocator);
130130
auto chunk = Chunk::createWithText(Chunk::ChunkKind::Text, 0, nameCopy);
131131
auto *completionString =
132132
CodeCompletionString::create(sink.allocator, chunk);
@@ -1187,7 +1187,7 @@ Completion *CompletionBuilder::finish() {
11871187
}
11881188

11891189
auto *result = new (sink.allocator)
1190-
Completion(*newBase, copyString(sink.allocator, description));
1190+
Completion(*newBase, description.str().copy(sink.allocator));
11911191
result->moduleImportDepth = moduleImportDepth;
11921192
result->popularityFactor = popularityFactor;
11931193
result->opaqueCustomKind = customKind;

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ static StringRef getModuleName(const ValueDecl *VD,
760760
static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
761761
if (auto ClangNode = VD->getClangNode()) {
762762
if (const auto *ClangMod = Importer->getClangOwningModule(ClangNode))
763-
return copyString(Allocator, ClangMod->getFullModuleName());
763+
return StringRef(ClangMod->getFullModuleName()).copy(Allocator);
764764
return "";
765765
}
766766

@@ -824,7 +824,7 @@ struct DeclInfo {
824824

825825
static StringRef copyAndClearString(llvm::BumpPtrAllocator &Allocator,
826826
SmallVectorImpl<char> &Str) {
827-
auto Ref = copyString(Allocator, StringRef(Str.data(), Str.size()));
827+
auto Ref = StringRef(Str.data(), Str.size()).copy(Allocator);
828828
Str.clear();
829829
return Ref;
830830
}
@@ -998,7 +998,7 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
998998
SmallVector<ParentInfo, 4> Parents;
999999
for (auto &Component : PathComponents) {
10001000
SwiftLangSupport::printUSR(Component.VD, OS);
1001-
Parents.emplace_back(copyString(Allocator, Component.Title),
1001+
Parents.emplace_back(Component.Title.str().copy(Allocator),
10021002
Component.Kind,
10031003
copyAndClearString(Allocator, Buffer));
10041004
};
@@ -1009,7 +1009,7 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
10091009
SmallVector<ParentInfo, 4> FIParents;
10101010
for (auto &Component: FI.ParentContexts) {
10111011
SwiftLangSupport::printUSR(Component.VD, OS);
1012-
FIParents.emplace_back(copyString(Allocator, Component.Title),
1012+
FIParents.emplace_back(Component.Title.str().copy(Allocator),
10131013
Component.Kind,
10141014
copyAndClearString(Allocator, Buffer));
10151015
}

0 commit comments

Comments
 (0)