Skip to content

Commit f129a34

Browse files
Use shared DebugData for when using source locations from comments.
1 parent 5e4868d commit f129a34

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

libyul/AsmParser.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,20 @@ void Parser::fetchSourceLocationFromComment()
128128
auto const end = toInt(matchResult[4].str());
129129

130130
auto const commentLocation = m_scanner->currentCommentLocation();
131-
m_locationOverride = SourceLocation{};
131+
m_debugDataOverride = DebugData::create();
132132
if (!sourceIndex || !start || !end)
133133
m_errorReporter.syntaxError(6367_error, commentLocation, "Invalid value in source location mapping. Could not parse location specification.");
134134
else if (!((*start < 0 && *end < 0) || (*start >= 0 && *start <= *end)))
135135
m_errorReporter.syntaxError(5798_error, commentLocation, "Invalid value in source location mapping. Start offset larger than end offset.");
136136
else if (sourceIndex == -1 && (0 <= *start && *start <= *end)) // Use source index -1 to indicate original source.
137-
m_locationOverride = SourceLocation{*start, *end, ParserBase::currentLocation().source};
137+
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, ParserBase::currentLocation().source});
138138
else if (!(sourceIndex >= 0 && m_charStreamMap->count(static_cast<unsigned>(*sourceIndex))))
139139
m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src.");
140140
else
141141
{
142142
shared_ptr<CharStream> charStream = m_charStreamMap->at(static_cast<unsigned>(*sourceIndex));
143143
solAssert(charStream, "");
144-
145-
m_locationOverride = SourceLocation{*start, *end, charStream};
144+
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, charStream});
146145
}
147146
}
148147
}
@@ -371,7 +370,7 @@ variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
371370
{
372371
case Token::Identifier:
373372
{
374-
Identifier identifier{DebugData::create(currentLocation()), YulString{currentLiteral()}};
373+
Identifier identifier{createDebugData(), YulString{currentLiteral()}};
375374
advance();
376375
return identifier;
377376
}
@@ -402,7 +401,7 @@ variant<Literal, Identifier> Parser::parseLiteralOrIdentifier()
402401
}
403402

404403
Literal literal{
405-
DebugData::create(currentLocation()),
404+
createDebugData(),
406405
kind,
407406
YulString{currentLiteral()},
408407
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType

libyul/AsmParser.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Parser: public langutil::ParserBase
5959
ParserBase(_errorReporter),
6060
m_dialect(_dialect),
6161
m_locationOverride{_locationOverride ? *_locationOverride : langutil::SourceLocation{}},
62+
m_debugDataOverride{},
6263
m_useSourceLocationFrom{
6364
_locationOverride ?
6465
UseSourceLocationFrom::LocationOverride :
@@ -76,6 +77,7 @@ class Parser: public langutil::ParserBase
7677
ParserBase(_errorReporter),
7778
m_dialect(_dialect),
7879
m_charStreamMap{std::move(_charStreamMap)},
80+
m_debugDataOverride{DebugData::create()},
7981
m_useSourceLocationFrom{UseSourceLocationFrom::Comments}
8082
{}
8183

@@ -96,11 +98,26 @@ class Parser: public langutil::ParserBase
9698

9799
void fetchSourceLocationFromComment();
98100

101+
/// Creates a DebugData object with the correct source location set.
102+
std::shared_ptr<DebugData const> createDebugData() const
103+
{
104+
switch (m_useSourceLocationFrom)
105+
{
106+
case UseSourceLocationFrom::Scanner:
107+
return DebugData::create(ParserBase::currentLocation());
108+
case UseSourceLocationFrom::LocationOverride:
109+
return DebugData::create(m_locationOverride);
110+
case UseSourceLocationFrom::Comments:
111+
return m_debugDataOverride;
112+
}
113+
solAssert(false, "");
114+
}
115+
99116
/// Creates an inline assembly node with the current source location.
100117
template <class T> T createWithLocation() const
101118
{
102119
T r;
103-
r.debugData = DebugData::create(currentLocation());
120+
r.debugData = createDebugData();
104121
return r;
105122
}
106123

@@ -129,6 +146,7 @@ class Parser: public langutil::ParserBase
129146

130147
std::optional<std::map<unsigned, std::shared_ptr<langutil::CharStream>>> m_charStreamMap;
131148
langutil::SourceLocation m_locationOverride;
149+
std::shared_ptr<DebugData const> m_debugDataOverride;
132150
UseSourceLocationFrom m_useSourceLocationFrom = UseSourceLocationFrom::Scanner;
133151
ForLoopComponent m_currentForLoopComponent = ForLoopComponent::None;
134152
bool m_insideFunction = false;

0 commit comments

Comments
 (0)