Skip to content

Commit 6f88c22

Browse files
committed
Maintain a buffer ID -> source file(s) mapping in the source manager
Now that every source file has a buffer ID, introduce the reverse mapping so clients can find the source file(s) in their module that reference that buffer ID.
1 parent 0bd5195 commit 6f88c22

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@
1818
#include "clang/Basic/FileManager.h"
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/DenseSet.h"
21+
#include "llvm/ADT/TinyPtrVector.h"
2122
#include "llvm/Support/SourceMgr.h"
2223
#include <map>
2324
#include <optional>
2425
#include <vector>
2526

27+
namespace swift {
28+
class SourceFile;
29+
}
30+
31+
namespace llvm {
32+
template <> struct PointerLikeTypeTraits<swift::SourceFile *> {
33+
public:
34+
static inline swift::SourceFile *getFromVoidPointer(void *P) {
35+
return (swift::SourceFile *)P;
36+
}
37+
static inline void *getAsVoidPointer(swift::SourceFile *S) {
38+
return (void *)S;
39+
}
40+
enum { NumLowBitsAvailable = /*swift::DeclContextAlignInBits=*/ 3 };
41+
};
42+
}
43+
2644
namespace swift {
2745

2846
class CustomAttr;
2947
class DeclContext;
48+
class SourceFile;
3049

3150
/// Augments a buffer that was created specifically to hold generated source
3251
/// code with the reasons for it being generated.
@@ -123,6 +142,13 @@ class SourceManager {
123142
/// is an unfortunate hack needed to allow for correct re-lexing.
124143
llvm::DenseSet<SourceLoc> RegexLiteralStartLocs;
125144

145+
/// Mapping from each buffer ID to the source files that describe it
146+
/// semantically.
147+
llvm::DenseMap<
148+
unsigned,
149+
llvm::TinyPtrVector<SourceFile *>
150+
> bufferIDToSourceFiles;
151+
126152
std::map<const char *, VirtualFile> VirtualFiles;
127153
mutable std::pair<const char *, const VirtualFile*> CachedVFile = {nullptr, nullptr};
128154

@@ -323,6 +349,13 @@ class SourceManager {
323349
/// Adds a memory buffer to the SourceManager, taking ownership of it.
324350
unsigned addNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer);
325351

352+
/// Record the source file as having the given buffer ID.
353+
void recordSourceFile(unsigned bufferID, SourceFile *sourceFile);
354+
355+
/// Retrieve the source files for the given buffer ID.
356+
llvm::TinyPtrVector<SourceFile *>
357+
getSourceFilesForBufferID(unsigned bufferID) const;
358+
326359
/// Add a \c #sourceLocation-defined virtual file region of \p Length.
327360
void createVirtualFile(SourceLoc Loc, StringRef Name, int LineOffset,
328361
unsigned Length);

lib/AST/Module.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,6 +3446,8 @@ SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
34463446
(void)problem;
34473447
}
34483448

3449+
M.getASTContext().SourceMgr.recordSourceFile(bufferID, this);
3450+
34493451
if (Kind == SourceFileKind::MacroExpansion ||
34503452
Kind == SourceFileKind::DefaultArgument)
34513453
M.addAuxiliaryFile(*this);

lib/Basic/SourceLoc.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ SourceManager::getIDForBufferIdentifier(StringRef BufIdentifier) const {
215215
return It->second;
216216
}
217217

218+
void SourceManager::recordSourceFile(unsigned bufferID, SourceFile *sourceFile){
219+
bufferIDToSourceFiles[bufferID].push_back(sourceFile);
220+
}
221+
222+
llvm::TinyPtrVector<SourceFile *>
223+
SourceManager::getSourceFilesForBufferID(unsigned bufferID) const {
224+
auto found = bufferIDToSourceFiles.find(bufferID);
225+
if (found == bufferIDToSourceFiles.end())
226+
return { };
227+
228+
return found->second;
229+
}
230+
218231
SourceManager::~SourceManager() {
219232
for (auto &generated : GeneratedSourceInfos) {
220233
free((void*)generated.second.onDiskBufferCopyFileName.data());

0 commit comments

Comments
 (0)