Skip to content

Commit 3fd92f8

Browse files
committed
Use StringRef in more places to match recent llvm changes.
llvm r283043 and possibly other recent changes switch to use StringRef instead of char* pointers. Update Swift to match. In some cases, this is a clear improvement. It would be good to assess the impact on memory use, particularly for the Filename component of source locations. Note that the change to SILLocation::isNull fixes an apparent bug where the location was treated as null when the filename was *not* null.
1 parent c08a96a commit 3fd92f8

File tree

13 files changed

+41
-47
lines changed

13 files changed

+41
-47
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class SourceManager {
134134
/// Returns the identifier for the buffer with the given ID.
135135
///
136136
/// \p BufferID must be a valid buffer ID.
137-
const char *getIdentifierForBuffer(unsigned BufferID) const;
137+
StringRef getIdentifierForBuffer(unsigned BufferID) const;
138138

139139
/// \brief Returns a SourceRange covering the entire specified buffer.
140140
///
@@ -167,9 +167,9 @@ class SourceManager {
167167
/// location.
168168
///
169169
/// This respects #line directives.
170-
const char *getBufferIdentifierForLoc(SourceLoc Loc) const {
170+
StringRef getBufferIdentifierForLoc(SourceLoc Loc) const {
171171
if (auto VFile = getVirtualFile(Loc))
172-
return VFile->Name.c_str();
172+
return VFile->Name;
173173
else
174174
return getIdentifierForBuffer(findBufferContainingLoc(Loc));
175175
}

include/swift/SIL/SILLocation.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ class SILLocation {
8787
struct DebugLoc {
8888
unsigned Line;
8989
unsigned Column;
90-
const char *Filename;
90+
StringRef Filename;
9191

9292
DebugLoc(unsigned Line = 0, unsigned Column = 0,
93-
const char *Filename = nullptr) : Line(Line), Column(Column),
94-
Filename(Filename) { }
93+
StringRef Filename = StringRef()) : Line(Line), Column(Column),
94+
Filename(Filename) { }
9595
};
9696

9797
protected:
@@ -260,7 +260,7 @@ class SILLocation {
260260
bool isNull() const {
261261
switch (getStorageKind()) {
262262
case ASTNodeKind: return Loc.ASTNode.Primary.isNull();
263-
case DebugInfoKind: return Loc.DebugInfoLoc.Filename;
263+
case DebugInfoKind: return Loc.DebugInfoLoc.Filename.empty();
264264
case SILFileKind: return Loc.SILFileLoc.isInvalid();
265265
default: return true;
266266
}
@@ -405,7 +405,7 @@ class SILLocation {
405405
}
406406

407407
/// Fingerprint a DebugLoc for use in a DenseMap.
408-
typedef std::pair<std::pair<unsigned, unsigned>, const void *> DebugLocKey;
408+
typedef std::pair<std::pair<unsigned, unsigned>, StringRef> DebugLocKey;
409409
struct DebugLocHash : public DebugLocKey {
410410
DebugLocHash(DebugLoc L) : DebugLocKey({{L.Line, L.Column}, L.Filename}) {}
411411
};

lib/AST/ConformanceLookupTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ int ConformanceLookupTable::compareProtocolConformances(
10381038
}
10391039

10401040
// Otherwise, order by buffer identifier.
1041-
return StringRef(ctx.SourceMgr.getIdentifierForBuffer(lhsBuffer))
1041+
return ctx.SourceMgr.getIdentifierForBuffer(lhsBuffer)
10421042
.compare(ctx.SourceMgr.getIdentifierForBuffer(rhsBuffer));
10431043
}
10441044
}

lib/Basic/SourceLoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Optional<unsigned> SourceManager::getIDForBufferIdentifier(
137137
return It->second;
138138
}
139139

140-
const char *SourceManager::getIdentifierForBuffer(unsigned bufferID) const {
140+
StringRef SourceManager::getIdentifierForBuffer(unsigned bufferID) const {
141141
auto *buffer = LLVMSourceMgr.getMemoryBuffer(bufferID);
142142
assert(buffer && "invalid buffer ID");
143143
return buffer->getBufferIdentifier();

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ namespace {
157157
/*null-terminated=*/true);
158158
}
159159

160-
const char *getBufferIdentifier() const override {
161-
return name.c_str();
160+
StringRef getBufferIdentifier() const override {
161+
return name;
162162
}
163163

164164
BufferKind getBufferKind() const override {
@@ -2350,7 +2350,7 @@ StringRef ClangModuleUnit::getFilename() const {
23502350
if (!clangModule)
23512351
return "<imports>";
23522352
if (const clang::FileEntry *F = clangModule->getASTFile())
2353-
if (F->getName())
2353+
if (!F->getName().empty())
23542354
return F->getName();
23552355
return StringRef();
23562356
}

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind,
117117
// location to pull out the source line.
118118
SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
119119
std::pair<unsigned, unsigned> LineAndCol;
120-
const char *BufferID = "<unknown>";
120+
StringRef BufferID = "<unknown>";
121121
std::string LineStr;
122122

123123
if (Loc.isValid()) {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ class JSONFixitWriter : public DiagnosticConsumer {
663663
unsigned BufID = SM.findBufferContainingLoc(Loc);
664664
unsigned Offset = SM.getLocOffsetInBuffer(Loc, BufID);
665665
unsigned Length = Range.getByteLength();
666-
SmallString<200> Path =
667-
StringRef(SM.getIdentifierForBuffer(BufID));
666+
SmallString<200> Path = SM.getIdentifierForBuffer(BufID);
668667

669668
OS << " {\n";
670669
OS << " \"file\": \"";

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ IRGenDebugInfo::IRGenDebugInfo(const IRGenOptions &Opts,
139139
Opts.DebugInfoKind > IRGenDebugInfoKind::LineTables
140140
? llvm::DICompileUnit::FullDebug
141141
: llvm::DICompileUnit::LineTablesOnly);
142-
MainFile = getOrCreateFile(BumpAllocatedString(AbsMainFile).data());
142+
MainFile = getOrCreateFile(BumpAllocatedString(AbsMainFile));
143143

144144
// Because the swift compiler relies on Clang to setup the Module,
145145
// the clang CU is always created first. Several dwarf-reading
@@ -161,22 +161,15 @@ IRGenDebugInfo::IRGenDebugInfo(const IRGenOptions &Opts,
161161
DBuilder.createImportedModule(MainFile, MainModule, 1);
162162
}
163163

164-
static const char *getFilenameFromDC(const DeclContext *DC) {
165-
if (auto LF = dyn_cast<LoadedFile>(DC)) {
166-
// FIXME: Today, the subclasses of LoadedFile happen to return StringRefs
167-
// that are backed by null-terminated strings, but that's certainly not
168-
// guaranteed in the future.
169-
StringRef Fn = LF->getFilename();
170-
assert(((Fn.size() == 0) ||
171-
(Fn.data()[Fn.size()] == '\0')) && "not a C string");
172-
return Fn.data();
173-
}
164+
static StringRef getFilenameFromDC(const DeclContext *DC) {
165+
if (auto LF = dyn_cast<LoadedFile>(DC))
166+
return LF->getFilename();
174167
if (auto SF = dyn_cast<SourceFile>(DC))
175-
return SF->getFilename().data();
168+
return SF->getFilename();
176169
else if (auto M = dyn_cast<Module>(DC))
177-
return M->getModuleFilename().data();
170+
return M->getModuleFilename();
178171
else
179-
return nullptr;
172+
return StringRef();
180173
}
181174

182175
SILLocation::DebugLoc getDeserializedLoc(Pattern *) { return {}; }
@@ -185,7 +178,8 @@ SILLocation::DebugLoc getDeserializedLoc(Stmt *) { return {}; }
185178
SILLocation::DebugLoc getDeserializedLoc(Decl *D) {
186179
SILLocation::DebugLoc L;
187180
const DeclContext *DC = D->getDeclContext()->getModuleScopeContext();
188-
if (const char *Filename = getFilenameFromDC(DC))
181+
StringRef Filename = getFilenameFromDC(DC);
182+
if (!Filename.empty())
189183
L.Filename = Filename;
190184
return L;
191185
}
@@ -412,8 +406,8 @@ llvm::DIScope *IRGenDebugInfo::getOrCreateScope(const SILDebugScope *DS) {
412406
return DScope;
413407
}
414408

415-
llvm::DIFile *IRGenDebugInfo::getOrCreateFile(const char *Filename) {
416-
if (!Filename)
409+
llvm::DIFile *IRGenDebugInfo::getOrCreateFile(StringRef Filename) {
410+
if (Filename.empty())
417411
return MainFile;
418412

419413
if (MainFile) {
@@ -676,7 +670,7 @@ llvm::DISubprogram *IRGenDebugInfo::emitFunction(
676670

677671
// We know that main always comes from MainFile.
678672
if (LinkageName == SWIFT_ENTRY_POINT_FUNCTION) {
679-
if (!L.Filename)
673+
if (L.Filename.empty())
680674
File = MainFile;
681675
Line = 1;
682676
Name = LinkageName;
@@ -759,8 +753,7 @@ void IRGenDebugInfo::emitImport(ImportDecl *D) {
759753

760754
llvm::DIModule *
761755
IRGenDebugInfo::getOrCreateModule(ModuleDecl::ImportedModule M) {
762-
const char *fn = getFilenameFromDC(M.second);
763-
StringRef Path(fn ? fn : "");
756+
StringRef Path = getFilenameFromDC(M.second);
764757
if (M.first.empty()) {
765758
StringRef Name = M.second->getName().str();
766759
return getOrCreateModule(Name, TheCU, Name, Path);

lib/IRGen/IRGenDebugInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class IRGenDebugInfo {
6363

6464
// Various caches.
6565
llvm::DenseMap<const SILDebugScope *, llvm::TrackingMDNodeRef> ScopeCache;
66-
llvm::DenseMap<const char *, llvm::TrackingMDNodeRef> DIFileCache;
66+
llvm::DenseMap<llvm::StringRef, llvm::TrackingMDNodeRef> DIFileCache;
6767
llvm::DenseMap<TypeBase *, llvm::TrackingMDNodeRef> DITypeCache;
6868
llvm::StringMap<llvm::TrackingMDNodeRef> DIModuleCache;
6969
TrackingDIRefMap DIRefMap;
@@ -243,7 +243,7 @@ class IRGenDebugInfo {
243243
llvm::MDNode *createInlinedAt(const SILDebugScope *CallSite);
244244

245245
/// Translate filenames into DIFiles.
246-
llvm::DIFile *getOrCreateFile(const char *Filename);
246+
llvm::DIFile *getOrCreateFile(StringRef Filename);
247247
/// Return a DIType for Ty reusing any DeclContext found in DbgTy.
248248
llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DbgTy);
249249

lib/SIL/SILPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class SILPrinter : public SILVisitor<SILPrinter> {
605605
void printDebugLocRef(SILLocation Loc, const SourceManager &SM,
606606
bool PrintComma = true) {
607607
auto DL = Loc.decodeDebugLoc(SM);
608-
if (DL.Filename) {
608+
if (!DL.Filename.empty()) {
609609
if (PrintComma)
610610
*this << ", ";
611611
*this << "loc " << QuotedString(DL.Filename) << ':' << DL.Line << ':'

lib/Serialization/Serialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,8 +3598,8 @@ class DeclGroupNameContext {
35983598
auto PathOp = VD->getDeclContext()->getParentSourceFile()->getBufferID();
35993599
if (!PathOp.hasValue())
36003600
return NullGroupName;
3601-
StringRef FullPath = StringRef(VD->getASTContext().SourceMgr.
3602-
getIdentifierForBuffer(PathOp.getValue()));
3601+
StringRef FullPath =
3602+
VD->getASTContext().SourceMgr.getIdentifierForBuffer(PathOp.getValue());
36033603
if (!pMap) {
36043604
YamlGroupInputParser Parser(RecordPath);
36053605
if (!Parser.parse()) {

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ FileUnit *SerializedModuleLoader::loadAST(
150150
bool isFramework) {
151151
assert(moduleInputBuffer);
152152

153-
const char *moduleBufferID = moduleInputBuffer->getBufferIdentifier();
154-
const char *moduleDocBufferID = nullptr;
153+
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
154+
StringRef moduleDocBufferID;
155155
if (moduleDocInputBuffer)
156156
moduleDocBufferID = moduleDocInputBuffer->getBufferIdentifier();
157157

@@ -236,9 +236,9 @@ FileUnit *SerializedModuleLoader::loadAST(
236236
break;
237237

238238
case serialization::Status::MalformedDocumentation:
239-
assert(moduleDocBufferID);
239+
assert(!moduleDocBufferID.empty());
240240
Ctx.Diags.diagnose(*diagLoc, diag::serialization_malformed_module,
241-
moduleDocBufferID ? moduleDocBufferID : "");
241+
moduleDocBufferID);
242242
break;
243243

244244
case serialization::Status::MissingDependency: {

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,10 @@ static void expandPlaceholders(llvm::MemoryBuffer *SourceBuf,
14201420
nullptr, 0);
14211421
sourcekitd_request_dictionary_set_uid(Exp, KeyRequest,
14221422
RequestEditorExpandPlaceholder);
1423-
sourcekitd_request_dictionary_set_string(Exp, KeyName,
1424-
SourceBuf->getBufferIdentifier());
1423+
auto SourceBufID = SourceBuf->getBufferIdentifier();
1424+
sourcekitd_request_dictionary_set_stringbuf(Exp, KeyName,
1425+
SourceBufID.data(),
1426+
SourceBufID.size());
14251427
sourcekitd_request_dictionary_set_string(Exp, KeySourceText, "");
14261428
sourcekitd_request_dictionary_set_int64(Exp, KeyOffset, Offset);
14271429
sourcekitd_request_dictionary_set_int64(Exp, KeyLength, Length);

0 commit comments

Comments
 (0)