diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index ce25724f9..05b3c4b1c 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -128,21 +128,20 @@ void Parser::fetchSourceLocationFromComment() auto const end = toInt(matchResult[4].str()); auto const commentLocation = m_scanner->currentCommentLocation(); - m_locationOverride = SourceLocation{}; + m_debugDataOverride = DebugData::create(); if (!sourceIndex || !start || !end) m_errorReporter.syntaxError(6367_error, commentLocation, "Invalid value in source location mapping. Could not parse location specification."); else if (!((*start < 0 && *end < 0) || (*start >= 0 && *start <= *end))) m_errorReporter.syntaxError(5798_error, commentLocation, "Invalid value in source location mapping. Start offset larger than end offset."); else if (sourceIndex == -1 && (0 <= *start && *start <= *end)) // Use source index -1 to indicate original source. - m_locationOverride = SourceLocation{*start, *end, ParserBase::currentLocation().source}; + m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, ParserBase::currentLocation().source}); else if (!(sourceIndex >= 0 && m_charStreamMap->count(static_cast(*sourceIndex)))) m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src."); else { shared_ptr charStream = m_charStreamMap->at(static_cast(*sourceIndex)); solAssert(charStream, ""); - - m_locationOverride = SourceLocation{*start, *end, charStream}; + m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, charStream}); } } } @@ -371,7 +370,7 @@ variant Parser::parseLiteralOrIdentifier() { case Token::Identifier: { - Identifier identifier{DebugData::create(currentLocation()), YulString{currentLiteral()}}; + Identifier identifier{createDebugData(), YulString{currentLiteral()}}; advance(); return identifier; } @@ -402,7 +401,7 @@ variant Parser::parseLiteralOrIdentifier() } Literal literal{ - DebugData::create(currentLocation()), + createDebugData(), kind, YulString{currentLiteral()}, kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType diff --git a/libyul/AsmParser.h b/libyul/AsmParser.h index ed4d249d5..fc0c9bb64 100644 --- a/libyul/AsmParser.h +++ b/libyul/AsmParser.h @@ -59,6 +59,7 @@ public: ParserBase(_errorReporter), m_dialect(_dialect), m_locationOverride{_locationOverride ? *_locationOverride : langutil::SourceLocation{}}, + m_debugDataOverride{}, m_useSourceLocationFrom{ _locationOverride ? UseSourceLocationFrom::LocationOverride : @@ -76,6 +77,7 @@ public: ParserBase(_errorReporter), m_dialect(_dialect), m_charStreamMap{std::move(_charStreamMap)}, + m_debugDataOverride{DebugData::create()}, m_useSourceLocationFrom{UseSourceLocationFrom::Comments} {} @@ -96,11 +98,26 @@ protected: void fetchSourceLocationFromComment(); + /// Creates a DebugData object with the correct source location set. + std::shared_ptr createDebugData() const + { + switch (m_useSourceLocationFrom) + { + case UseSourceLocationFrom::Scanner: + return DebugData::create(ParserBase::currentLocation()); + case UseSourceLocationFrom::LocationOverride: + return DebugData::create(m_locationOverride); + case UseSourceLocationFrom::Comments: + return m_debugDataOverride; + } + solAssert(false, ""); + } + /// Creates an inline assembly node with the current source location. template T createWithLocation() const { T r; - r.debugData = DebugData::create(currentLocation()); + r.debugData = createDebugData(); return r; } @@ -129,6 +146,7 @@ private: std::optional>> m_charStreamMap; langutil::SourceLocation m_locationOverride; + std::shared_ptr m_debugDataOverride; UseSourceLocationFrom m_useSourceLocationFrom = UseSourceLocationFrom::Scanner; ForLoopComponent m_currentForLoopComponent = ForLoopComponent::None; bool m_insideFunction = false;