Use shared DebugData for when using source locations from comments.

This commit is contained in:
Christian Parpart 2021-07-08 16:38:16 +02:00
parent 5e4868d5d6
commit f129a3498c
2 changed files with 24 additions and 7 deletions

View File

@ -128,21 +128,20 @@ void Parser::fetchSourceLocationFromComment()
auto const end = toInt(matchResult[4].str()); auto const end = toInt(matchResult[4].str());
auto const commentLocation = m_scanner->currentCommentLocation(); auto const commentLocation = m_scanner->currentCommentLocation();
m_locationOverride = SourceLocation{}; m_debugDataOverride = DebugData::create();
if (!sourceIndex || !start || !end) if (!sourceIndex || !start || !end)
m_errorReporter.syntaxError(6367_error, commentLocation, "Invalid value in source location mapping. Could not parse location specification."); 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))) 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."); 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. 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<unsigned>(*sourceIndex)))) else if (!(sourceIndex >= 0 && m_charStreamMap->count(static_cast<unsigned>(*sourceIndex))))
m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src."); m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src.");
else else
{ {
shared_ptr<CharStream> charStream = m_charStreamMap->at(static_cast<unsigned>(*sourceIndex)); shared_ptr<CharStream> charStream = m_charStreamMap->at(static_cast<unsigned>(*sourceIndex));
solAssert(charStream, ""); solAssert(charStream, "");
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, charStream});
m_locationOverride = SourceLocation{*start, *end, charStream};
} }
} }
} }
@ -371,7 +370,7 @@ variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
{ {
case Token::Identifier: case Token::Identifier:
{ {
Identifier identifier{DebugData::create(currentLocation()), YulString{currentLiteral()}}; Identifier identifier{createDebugData(), YulString{currentLiteral()}};
advance(); advance();
return identifier; return identifier;
} }
@ -402,7 +401,7 @@ variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
} }
Literal literal{ Literal literal{
DebugData::create(currentLocation()), createDebugData(),
kind, kind,
YulString{currentLiteral()}, YulString{currentLiteral()},
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType

View File

@ -59,6 +59,7 @@ public:
ParserBase(_errorReporter), ParserBase(_errorReporter),
m_dialect(_dialect), m_dialect(_dialect),
m_locationOverride{_locationOverride ? *_locationOverride : langutil::SourceLocation{}}, m_locationOverride{_locationOverride ? *_locationOverride : langutil::SourceLocation{}},
m_debugDataOverride{},
m_useSourceLocationFrom{ m_useSourceLocationFrom{
_locationOverride ? _locationOverride ?
UseSourceLocationFrom::LocationOverride : UseSourceLocationFrom::LocationOverride :
@ -76,6 +77,7 @@ public:
ParserBase(_errorReporter), ParserBase(_errorReporter),
m_dialect(_dialect), m_dialect(_dialect),
m_charStreamMap{std::move(_charStreamMap)}, m_charStreamMap{std::move(_charStreamMap)},
m_debugDataOverride{DebugData::create()},
m_useSourceLocationFrom{UseSourceLocationFrom::Comments} m_useSourceLocationFrom{UseSourceLocationFrom::Comments}
{} {}
@ -96,11 +98,26 @@ protected:
void fetchSourceLocationFromComment(); void fetchSourceLocationFromComment();
/// Creates a DebugData object with the correct source location set.
std::shared_ptr<DebugData const> 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. /// Creates an inline assembly node with the current source location.
template <class T> T createWithLocation() const template <class T> T createWithLocation() const
{ {
T r; T r;
r.debugData = DebugData::create(currentLocation()); r.debugData = createDebugData();
return r; return r;
} }
@ -129,6 +146,7 @@ private:
std::optional<std::map<unsigned, std::shared_ptr<langutil::CharStream>>> m_charStreamMap; std::optional<std::map<unsigned, std::shared_ptr<langutil::CharStream>>> m_charStreamMap;
langutil::SourceLocation m_locationOverride; langutil::SourceLocation m_locationOverride;
std::shared_ptr<DebugData const> m_debugDataOverride;
UseSourceLocationFrom m_useSourceLocationFrom = UseSourceLocationFrom::Scanner; UseSourceLocationFrom m_useSourceLocationFrom = UseSourceLocationFrom::Scanner;
ForLoopComponent m_currentForLoopComponent = ForLoopComponent::None; ForLoopComponent m_currentForLoopComponent = ForLoopComponent::None;
bool m_insideFunction = false; bool m_insideFunction = false;