From 088644c21c015eb60a41a59f417990023f1f4ef8 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 20 Apr 2023 13:42:20 -0400 Subject: Improve SourceMap coverage/testing (#2818) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP around more value like behavior for SourceMap/StringPool. * Use default impls on SourceMap ctor/assignment. * Handle comparison of SourceMaps. Some small fixes. Expand unit tests. --- source/compiler-core/slang-source-map.cpp | 99 +++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'source/compiler-core/slang-source-map.cpp') diff --git a/source/compiler-core/slang-source-map.cpp b/source/compiler-core/slang-source-map.cpp index 7a6e368a4..43727838f 100644 --- a/source/compiler-core/slang-source-map.cpp +++ b/source/compiler-core/slang-source-map.cpp @@ -35,6 +35,105 @@ void SourceMap::swapWith(ThisType& rhs) m_slicePool.swapWith(rhs.m_slicePool); } +static bool _areEqual(const List& a, const List& b, const List& bToAMap) +{ + const auto count = a.getCount(); + if (count != b.getCount()) + { + return false; + } + + const auto* as = a.getBuffer(); + const auto* bs = a.getBuffer(); + + for (Index i = 0; i < count; ++i) + { + if (StringSlicePool::asIndex(as[i]) != bToAMap[StringSlicePool::asIndex(bs[i])]) + { + return false; + } + } + + return true; +} + +static bool _areEqual(const SourceMap::Entry& a, const SourceMap::Entry& b, const List& bToAMap) +{ + return a.generatedColumn == b.generatedColumn && + a.sourceLine == b.sourceLine && + a.sourceColumn == b.sourceColumn && + a.sourceFileIndex == bToAMap[b.sourceFileIndex] && + a.nameIndex == bToAMap[b.nameIndex]; +} + +static bool _areEqual(const List& a, const List&b, const List& bToAMap) +{ + const auto count = a.getCount(); + if (count != b.getCount()) + { + return false; + } + + for (Index i = 0; i < count; ++i) + { + if (!_areEqual(a[i], b[i], bToAMap)) + { + return false; + } + } + + return true; +} + +bool SourceMap::operator==(const ThisType& rhs) const +{ + if (this == &rhs) + { + return true; + } + + if (m_file != rhs.m_file || + m_sourceRoot != rhs.m_sourceRoot || + m_lineStarts != rhs.m_lineStarts) + { + return false; + } + + if (m_slicePool == rhs.m_slicePool) + { + // If the slice pools are the same we can just compare indices directly + return m_sources == rhs.m_sources && + m_sourcesContent == rhs.m_sourcesContent && + m_names == rhs.m_names && + m_lineEntries == rhs.m_lineEntries; + } + else + { + // Otherwise we need to remap the indices + // Maps a pool handle from the rhs source map to the + List rhsMap; + + Count count = rhs.m_slicePool.getSlicesCount(); + + rhsMap.setCount(count); + + const auto startIndex = rhs.m_slicePool.getFirstAddedIndex(); + + // Work out the map + for (Index i = 0; i < startIndex; ++i) + { + const auto rhsSlice = rhs.m_slicePool.getSlice(StringSlicePool::Handle(i)); + rhsMap[i] = (i < startIndex) ? i : m_slicePool.findIndex(rhsSlice); + } + + // Do the comparison taking into account the mapping. + return _areEqual(m_sources, rhs.m_sources, rhsMap) && + _areEqual(m_sourcesContent, rhs.m_sourcesContent, rhsMap) && + _areEqual(m_names, rhs.m_names, rhsMap) && + _areEqual(m_lineEntries, rhs.m_lineEntries, rhsMap); + } +} + void SourceMap::advanceToLine(Index nextLineIndex) { const Count currentLineIndex = getGeneratedLineCount() - 1; -- cgit v1.2.3