mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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.
This commit is contained in:
@@ -18,15 +18,34 @@
|
||||
#include "clang/Basic/FileManager.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include "llvm/ADT/DenseSet.h"
|
||||
#include "llvm/ADT/TinyPtrVector.h"
|
||||
#include "llvm/Support/SourceMgr.h"
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace swift {
|
||||
class SourceFile;
|
||||
}
|
||||
|
||||
namespace llvm {
|
||||
template <> struct PointerLikeTypeTraits<swift::SourceFile *> {
|
||||
public:
|
||||
static inline swift::SourceFile *getFromVoidPointer(void *P) {
|
||||
return (swift::SourceFile *)P;
|
||||
}
|
||||
static inline void *getAsVoidPointer(swift::SourceFile *S) {
|
||||
return (void *)S;
|
||||
}
|
||||
enum { NumLowBitsAvailable = /*swift::DeclContextAlignInBits=*/ 3 };
|
||||
};
|
||||
}
|
||||
|
||||
namespace swift {
|
||||
|
||||
class CustomAttr;
|
||||
class DeclContext;
|
||||
class SourceFile;
|
||||
|
||||
/// Augments a buffer that was created specifically to hold generated source
|
||||
/// code with the reasons for it being generated.
|
||||
@@ -123,6 +142,13 @@ private:
|
||||
/// is an unfortunate hack needed to allow for correct re-lexing.
|
||||
llvm::DenseSet<SourceLoc> RegexLiteralStartLocs;
|
||||
|
||||
/// Mapping from each buffer ID to the source files that describe it
|
||||
/// semantically.
|
||||
llvm::DenseMap<
|
||||
unsigned,
|
||||
llvm::TinyPtrVector<SourceFile *>
|
||||
> bufferIDToSourceFiles;
|
||||
|
||||
std::map<const char *, VirtualFile> VirtualFiles;
|
||||
mutable std::pair<const char *, const VirtualFile*> CachedVFile = {nullptr, nullptr};
|
||||
|
||||
@@ -323,6 +349,13 @@ public:
|
||||
/// Adds a memory buffer to the SourceManager, taking ownership of it.
|
||||
unsigned addNewSourceBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer);
|
||||
|
||||
/// Record the source file as having the given buffer ID.
|
||||
void recordSourceFile(unsigned bufferID, SourceFile *sourceFile);
|
||||
|
||||
/// Retrieve the source files for the given buffer ID.
|
||||
llvm::TinyPtrVector<SourceFile *>
|
||||
getSourceFilesForBufferID(unsigned bufferID) const;
|
||||
|
||||
/// Add a \c #sourceLocation-defined virtual file region of \p Length.
|
||||
void createVirtualFile(SourceLoc Loc, StringRef Name, int LineOffset,
|
||||
unsigned Length);
|
||||
|
||||
Reference in New Issue
Block a user