From: Argyrios Kyrtzidis Date: Mon, 26 Sep 2011 08:01:50 +0000 (+0000) Subject: Associate the macro arguments location map with a FileID instead X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fb3612ef197cb8532c05f33889ec1aed7c26e5cb;p=clang Associate the macro arguments location map with a FileID instead of a ContentCache, since multiple FileIDs can have the same ContentCache but the expanded macro arguments locations will be different. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140521 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index 2c6b8cde18..6aea0f95ac 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -104,11 +104,6 @@ namespace SrcMgr { /// if SourceLineCache is non-null. unsigned NumLines; - /// \brief Lazily computed map of macro argument chunks to their expanded - /// source location. - typedef std::map MacroArgsMap; - MacroArgsMap *MacroArgsCache; - /// getBuffer - Returns the memory buffer for the associated content. /// /// \param Diag Object through which diagnostics will be emitted if the @@ -166,11 +161,11 @@ namespace SrcMgr { ContentCache(const FileEntry *Ent = 0) : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), - SourceLineCache(0), NumLines(0), MacroArgsCache(0) {} + SourceLineCache(0), NumLines(0) {} ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), - SourceLineCache(0), NumLines(0), MacroArgsCache(0) {} + SourceLineCache(0), NumLines(0) {} ~ContentCache(); @@ -178,14 +173,13 @@ namespace SrcMgr { /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory /// is not transferred, so this is a logical error. ContentCache(const ContentCache &RHS) - : Buffer(0, false), SourceLineCache(0), MacroArgsCache(0) + : Buffer(0, false), SourceLineCache(0) { OrigEntry = RHS.OrigEntry; ContentsEntry = RHS.ContentsEntry; assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 && - RHS.MacroArgsCache == 0 - && "Passed ContentCache object cannot own a buffer."); + "Passed ContentCache object cannot own a buffer."); NumLines = RHS.NumLines; } @@ -571,6 +565,12 @@ class SourceManager : public llvm::RefCountedBase { // Cache for the "fake" buffer used for error-recovery purposes. mutable llvm::MemoryBuffer *FakeBufferForRecovery; + /// \brief Lazily computed map of macro argument chunks to their expanded + /// source location. + typedef std::map MacroArgsMap; + + mutable llvm::DenseMap MacroArgsCacheMap; + // SourceManager doesn't support copy construction. explicit SourceManager(const SourceManager&); void operator=(const SourceManager&); @@ -1301,7 +1301,7 @@ private: std::pair getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, unsigned Offset) const; - void computeMacroArgsCache(SrcMgr::ContentCache *Content, FileID FID) const; + void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const; friend class ASTReader; friend class ASTWriter; diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 410d4dfbeb..0c27e996c7 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -39,7 +39,6 @@ using llvm::MemoryBuffer; ContentCache::~ContentCache() { if (shouldFreeBuffer()) delete Buffer.getPointer(); - delete MacroArgsCache; } /// getSizeBytesMapped - Returns the number of bytes actually mapped for this @@ -389,6 +388,11 @@ SourceManager::~SourceManager() { } delete FakeBufferForRecovery; + + for (llvm::DenseMap::iterator + I = MacroArgsCacheMap.begin(),E = MacroArgsCacheMap.end(); I!=E; ++I) { + delete I->second; + } } void SourceManager::clearIDTables() { @@ -1503,13 +1507,13 @@ SourceLocation SourceManager::translateLineCol(FileID FID, /// 0 -> SourceLocation() /// 100 -> Expanded macro arg location /// 110 -> SourceLocation() -void SourceManager::computeMacroArgsCache(ContentCache *Content, +void SourceManager::computeMacroArgsCache(MacroArgsMap *&CachePtr, FileID FID) const { - assert(!Content->MacroArgsCache); assert(!FID.isInvalid()); + assert(!CachePtr); - Content->MacroArgsCache = new ContentCache::MacroArgsMap(); - ContentCache::MacroArgsMap &MacroArgsCache = *Content->MacroArgsCache; + CachePtr = new MacroArgsMap(); + MacroArgsMap &MacroArgsCache = *CachePtr; // Initially no macro argument chunk is present. MacroArgsCache.insert(std::make_pair(0, SourceLocation())); @@ -1566,7 +1570,7 @@ void SourceManager::computeMacroArgsCache(ContentCache *Content, // previous chunks, we only need to find where the ending of the new macro // chunk is mapped to and update the map with new begin/end mappings. - ContentCache::MacroArgsMap::iterator I= MacroArgsCache.upper_bound(EndOffs); + MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs); --I; SourceLocation EndOffsMappedLoc = I->second; MacroArgsCache[BeginOffs] = SourceLocation::getMacroLoc(Entry.getOffset()); @@ -1594,15 +1598,12 @@ SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const { if (FID.isInvalid()) return Loc; - ContentCache *Content - = const_cast(getSLocEntry(FID).getFile().getContentCache()); - if (!Content->MacroArgsCache) - computeMacroArgsCache(Content, FID); - - assert(Content->MacroArgsCache); - assert(!Content->MacroArgsCache->empty()); - ContentCache::MacroArgsMap::iterator - I = Content->MacroArgsCache->upper_bound(Offset); + MacroArgsMap *&MacroArgsCache = MacroArgsCacheMap[FID]; + if (!MacroArgsCache) + computeMacroArgsCache(MacroArgsCache, FID); + + assert(!MacroArgsCache->empty()); + MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset); --I; unsigned MacroArgBeginOffs = I->first; @@ -1720,13 +1721,12 @@ void SourceManager::PrintStats() const { << "B of Sloc address space used.\n"; unsigned NumLineNumsComputed = 0; - unsigned NumMacroArgsComputed = 0; unsigned NumFileBytesMapped = 0; for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ NumLineNumsComputed += I->second->SourceLineCache != 0; - NumMacroArgsComputed += I->second->MacroArgsCache != 0; NumFileBytesMapped += I->second->getSizeBytesMapped(); } + unsigned NumMacroArgsComputed = MacroArgsCacheMap.size(); llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " << NumLineNumsComputed << " files with line #'s computed, "