Skip to content

Commit

Permalink
Fix micro expansion issue for __LINE__. (#3942)
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe committed Apr 13, 2024
1 parent b937207 commit 31c704f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion source/slang/slang-preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,11 @@ struct ExpansionInputStream : InputStream

TokenType peekRawTokenType() { return peekRawToken().type; }

void setInitiatingMacroSourceLoc(SourceLoc loc)
{
m_initiatingMacroInvocationLoc = loc;
m_isInExpansion = true;
}
private:
/// The base stream that macro expansion is being applied to
InputStream* m_base = nullptr;
Expand All @@ -868,6 +873,10 @@ struct ExpansionInputStream : InputStream
/// nested macro invocations might be in flight.
SourceLoc m_initiatingMacroInvocationLoc;

/// Whether this ExpansionStream is created in the middle of
/// another macro expansion.
bool m_isInExpansion = false;

/// One token of lookahead
Token m_lookaheadToken;

Expand Down Expand Up @@ -1429,7 +1438,7 @@ void ExpansionInputStream::_maybeBeginMacroInvocation()
// invocation location for things like `__LINE__` uses inside
// of macro bodies.
//
if(activeStream == m_base)
if(!m_isInExpansion && activeStream == m_base)
{
m_initiatingMacroInvocationLoc = token.loc;
}
Expand Down Expand Up @@ -2110,6 +2119,7 @@ void MacroInvocation::_initCurrentOpStream()
// applies macro expansion to them.
//
ExpansionInputStream* expansion = new ExpansionInputStream(m_preprocessor, stream);
expansion->setInitiatingMacroSourceLoc(m_initiatingMacroInvocationLoc);
m_currentOpStreams.push(expansion);
}
break;
Expand Down
14 changes: 14 additions & 0 deletions tests/preprocessor/line-macro.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK): -output-using-type

//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer : register(u0);

#define T(x) x
#define LL T(__LINE__)

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
// CHECK: 13
outputBuffer[0] = LL;
}

0 comments on commit 31c704f

Please sign in to comment.