Skip to content

Commit

Permalink
Pulled module support and some unused methods out of SourceManager/So…
Browse files Browse the repository at this point in the history
…urceLocation
  • Loading branch information
lerno authored and bvdberg committed Nov 27, 2018
1 parent 88e2053 commit 43da279
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 244 deletions.
56 changes: 1 addition & 55 deletions c2c/Clang/DiagnosticRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,20 @@ void DiagnosticRenderer::emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
return;

if (IncludeLoc.isValid())
emitIncludeStackRecursively(IncludeLoc);
else {
emitModuleBuildStack(Loc.getManager());
emitImportStack(Loc);
}
if (IncludeLoc.isValid()) emitIncludeStackRecursively(IncludeLoc);
}

/// Helper to recursively walk up the include stack and print each layer
/// on the way back down.
void DiagnosticRenderer::emitIncludeStackRecursively(FullSourceLoc Loc) {
if (Loc.isInvalid()) {
emitModuleBuildStack(Loc.getManager());
return;
}

PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc);
if (PLoc.isInvalid())
return;

// If this source location was imported from a module, print the module
// import stack rather than the
// FIXME: We want submodule granularity here.
std::pair<FullSourceLoc, StringRef> Imported = Loc.getModuleImportLoc();
if (!Imported.second.empty()) {
// This location was imported by a module. Emit the module import stack.
emitImportStackRecursively(Imported.first, Imported.second);
return;
}

// Emit the other include frames first.
emitIncludeStackRecursively(
Expand All @@ -216,45 +201,6 @@ void DiagnosticRenderer::emitIncludeStackRecursively(FullSourceLoc Loc) {
emitIncludeLocation(Loc, PLoc);
}

/// Emit the module import stack associated with the current location.
void DiagnosticRenderer::emitImportStack(FullSourceLoc Loc) {
if (Loc.isInvalid()) {
emitModuleBuildStack(Loc.getManager());
return;
}

std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc();
emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second);
}

/// Helper to recursively walk up the import stack and print each layer
/// on the way back down.
void DiagnosticRenderer::emitImportStackRecursively(FullSourceLoc Loc,
StringRef ModuleName) {
if (ModuleName.empty()) {
return;
}

PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc);

// Emit the other import frames first.
std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc();
emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second);

// Emit the inclusion text/note.
emitImportLocation(Loc, PLoc, ModuleName);
}

/// Emit the module build stack, for cases where a module is (re-)built
/// on demand.
void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
ModuleBuildStack Stack = SM.getModuleBuildStack();
for (const auto &I : Stack) {
emitBuildingModuleLocation(I.second, I.second.getPresumedLoc(
DiagOpts->ShowPresumedLoc),
I.first);
}
}

/// A recursive function to trace all possible backtrace locations
/// to match the \p CaretLocFileID.
Expand Down
5 changes: 1 addition & 4 deletions c2c/Clang/DiagnosticRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ class DiagnosticRenderer {
void emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
DiagnosticsEngine::Level Level);
void emitIncludeStackRecursively(FullSourceLoc Loc);
void emitImportStack(FullSourceLoc Loc);
void emitImportStackRecursively(FullSourceLoc Loc, StringRef ModuleName);
void emitModuleBuildStack(const SourceManager &SM);
void emitCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
void emitCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
ArrayRef<CharSourceRange> Ranges, ArrayRef<FixItHint> Hints);
void emitSingleMacroExpansion(FullSourceLoc Loc,
DiagnosticsEngine::Level Level,
Expand Down
9 changes: 0 additions & 9 deletions c2c/Clang/SourceLocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,6 @@ FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
}

std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
if (!isValid())
return std::make_pair(FullSourceLoc(), StringRef());

std::pair<SourceLocation, StringRef> ImportLoc =
SrcMgr->getModuleImportLoc(*this);
return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
ImportLoc.second);
}

unsigned FullSourceLoc::getFileOffset() const {
assert(isValid());
Expand Down
3 changes: 1 addition & 2 deletions c2c/Clang/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ class FullSourceLoc : public SourceLocation {
PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
FullSourceLoc getImmediateMacroCallerLoc() const;
std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
unsigned getFileOffset() const;
unsigned getFileOffset() const;

unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
Expand Down
74 changes: 0 additions & 74 deletions c2c/Clang/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,33 +359,6 @@ void SourceManager::clearIDTables() {
createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1);
}

void SourceManager::initializeForReplay(const SourceManager &Old) {
assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");

auto CloneContentCache = [&](const ContentCache *Cache) -> ContentCache * {
auto *Clone = new (ContentCacheAlloc.Allocate<ContentCache>()) ContentCache;
Clone->OrigEntry = Cache->OrigEntry;
Clone->ContentsEntry = Cache->ContentsEntry;
Clone->BufferOverridden = Cache->BufferOverridden;
Clone->IsSystemFile = Cache->IsSystemFile;
Clone->IsTransient = Cache->IsTransient;
Clone->replaceBuffer(Cache->getRawBuffer(), /*DoNotFree*/true);
return Clone;
};

// Ensure all SLocEntries are loaded from the external source.
for (unsigned I = 0, N = Old.LoadedSLocEntryTable.size(); I != N; ++I)
if (!Old.SLocEntryLoaded[I])
Old.loadSLocEntry(I, nullptr);

// Inherit any content cache data from the old source manager.
for (auto &FileInfo : Old.FileInfos) {
SrcMgr::ContentCache *&Slot = FileInfos[FileInfo.first];
if (Slot)
continue;
Slot = CloneContentCache(FileInfo.second);
}
}

/// getOrCreateContentCache - Create or return a cached ContentCache for the
/// specified file.
Expand Down Expand Up @@ -620,53 +593,6 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
}

llvm::MemoryBuffer *SourceManager::getMemoryBufferForFile(const FileEntry *File,
bool *Invalid) {
const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
assert(IR && "getOrCreateContentCache() cannot return NULL");
return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
}

void SourceManager::overrideFileContents(const FileEntry *SourceFile,
llvm::MemoryBuffer *Buffer,
bool DoNotFree) {
const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
assert(IR && "getOrCreateContentCache() cannot return NULL");

const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true;

getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile);
}

void SourceManager::overrideFileContents(const FileEntry *SourceFile,
const FileEntry *NewFile) {
assert(SourceFile->getSize() == NewFile->getSize() &&
"Different sizes, use the FileManager to create a virtual file with "
"the correct size");
assert(FileInfos.count(SourceFile) == 0 &&
"This function should be called at the initialization stage, before "
"any parsing occurs.");
getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile;
}

void SourceManager::disableFileContentsOverride(const FileEntry *File) {
if (!isFileOverridden(File))
return;

const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(nullptr);
const_cast<SrcMgr::ContentCache *>(IR)->ContentsEntry = IR->OrigEntry;

assert(OverriddenFilesInfo);
OverriddenFilesInfo->OverriddenFiles.erase(File);
OverriddenFilesInfo->OverriddenFilesWithBuffer.erase(File);
}

void SourceManager::setFileIsTransient(const FileEntry *File) {
const SrcMgr::ContentCache *CC = getOrCreateContentCache(File);
const_cast<SrcMgr::ContentCache *>(CC)->IsTransient = true;
}

StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
bool MyInvalid = false;
Expand Down
108 changes: 8 additions & 100 deletions c2c/Clang/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,6 @@ class InBeforeInTUCacheEntry {
}
};

/// The stack used when building modules on demand, which is used
/// to provide a link between the source managers of the different compiler
/// instances.
using ModuleBuildStack = ArrayRef<std::pair<std::string, FullSourceLoc>>;

/// This class handles loading and caching of source files into memory.
///
/// This object owns the MemoryBuffer objects for all of the loaded
Expand Down Expand Up @@ -730,15 +725,6 @@ class SourceManager : public RefCountedBase<SourceManager> {
mutable llvm::DenseMap<FileID, std::unique_ptr<MacroArgsMap>>
MacroArgsCacheMap;

/// The stack of modules being built, which is used to detect
/// cycles in the module dependency graph as modules are being built, as
/// well as to describe why we're rebuilding a particular module.
///
/// There is no way to set this value from the command line. If we ever need
/// to do so (e.g., if on-demand module construction moves out-of-process),
/// we can add a cc1-level option to do so.
SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;

public:
SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
bool UserFilesAreVolatile = false);
Expand All @@ -748,40 +734,16 @@ class SourceManager : public RefCountedBase<SourceManager> {

void clearIDTables();

/// Initialize this source manager suitably to replay the compilation
/// described by \p Old. Requires that \p Old outlive \p *this.
void initializeForReplay(const SourceManager &Old);

DiagnosticsEngine &getDiagnostics() const { return Diag; }
DiagnosticsEngine &getDiagnostics() const { return Diag; }

FileManager &getFileManager() const { return FileMgr; }

/// Set true if the SourceManager should report the original file name
/// for contents of files that were overridden by other files. Defaults to
/// true.
void setOverridenFilesKeepOriginalName(bool value) {
OverridenFilesKeepOriginalName = value;
}

/// True if non-system source files should be treated as volatile
/// True if non-system source files should be treated as volatile
/// (likely to change while trying to use them).
bool userFilesAreVolatile() const { return UserFilesAreVolatile; }

/// Retrieve the module build stack.
ModuleBuildStack getModuleBuildStack() const {
return StoredModuleBuildStack;
}

/// Set the module build stack.
void setModuleBuildStack(ModuleBuildStack stack) {
StoredModuleBuildStack.clear();
StoredModuleBuildStack.append(stack.begin(), stack.end());
}

/// Push an entry to the module build stack.
void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
}

//===--------------------------------------------------------------------===//
// MainFileID creation and querying methods.
Expand Down Expand Up @@ -874,40 +836,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
SourceLocation TokenStart,
SourceLocation TokenEnd);

/// Retrieve the memory buffer associated with the given file.
///
/// \param Invalid If non-NULL, will be set \c true if an error
/// occurs while retrieving the memory buffer.
llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
bool *Invalid = nullptr);

/// Override the contents of the given source file by providing an
/// already-allocated buffer.
///
/// \param SourceFile the source file whose contents will be overridden.
///
/// \param Buffer the memory buffer whose contents will be used as the
/// data in the given source file.
///
/// \param DoNotFree If true, then the buffer will not be freed when the
/// source manager is destroyed.
void overrideFileContents(const FileEntry *SourceFile,
llvm::MemoryBuffer *Buffer, bool DoNotFree);
void overrideFileContents(const FileEntry *SourceFile,
std::unique_ptr<llvm::MemoryBuffer> Buffer) {
overrideFileContents(SourceFile, Buffer.release(), /*DoNotFree*/ false);
}

/// Override the given source file with another one.
///
/// \param SourceFile the source file which will be overridden.
///
/// \param NewFile the file whose contents will be used as the
/// data instead of the contents of the given source file.
void overrideFileContents(const FileEntry *SourceFile,
const FileEntry *NewFile);

/// Returns true if the file contents have been overridden.
/// Returns true if the file contents have been overridden.
bool isFileOverridden(const FileEntry *File) const {
if (OverriddenFilesInfo) {
if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
Expand All @@ -919,22 +849,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
return false;
}

/// Disable overridding the contents of a file, previously enabled
/// with #overrideFileContents.
///
/// This should be called before parsing has begun.
void disableFileContentsOverride(const FileEntry *File);

/// Specify that a file is transient.
void setFileIsTransient(const FileEntry *SourceFile);

/// Specify that all files that are read during this compilation are
/// transient.
void setAllFilesAreTransient(bool Transient) {
FilesAreTransient = Transient;
}

//===--------------------------------------------------------------------===//
//===--------------------------------------------------------------------===//
// FileID manipulation methods.
//===--------------------------------------------------------------------===//

Expand Down Expand Up @@ -985,16 +901,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
return Content->OrigEntry;
}

/// Returns the FileEntry record for the provided SLocEntry.
const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
{
const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
if (!Content)
return nullptr;
return Content->OrigEntry;
}

/// Return a StringRef to the source buffer data for the
/// Return a StringRef to the source buffer data for the
/// specified FileID.
///
/// \param FID The file ID whose contents will be returned.
Expand Down Expand Up @@ -1024,7 +932,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
}

//===--------------------------------------------------------------------===//
//===--------------------------------------------------------------------===//
// SourceLocation manipulation methods.
//===--------------------------------------------------------------------===//

Expand All @@ -1051,7 +959,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
return StringRef();
}

/// Return the source location corresponding to the first byte of
/// Return the source location corresponding to the first byte of
/// the specified file.
SourceLocation getLocForStartOfFile(FileID FID) const {
bool Invalid = false;
Expand Down Expand Up @@ -1101,7 +1009,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
return ExternalSLocEntries->getModuleImportLoc(FID.ID);
}

/// Given a SourceLocation object \p Loc, return the expansion
/// Given a SourceLocation object \p Loc, return the expansion
/// location referenced by the ID.
SourceLocation getExpansionLoc(SourceLocation Loc) const {
// Handle the non-mapped case inline, defer to out of line code to handle
Expand Down

0 comments on commit 43da279

Please sign in to comment.