Skip to content

Commit

Permalink
handle slang parsing errors more elegantly with assign op inside layo…
Browse files Browse the repository at this point in the history
…ut (needed since future commit will be using same code; will be less hard to deal with code conflicts)
  • Loading branch information
ArielG-NV committed Mar 11, 2024
1 parent a4372d0 commit 3f1731a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
44 changes: 24 additions & 20 deletions source/slang/slang-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7760,6 +7760,12 @@ namespace Slang

static NodeBase* parseLayoutModifier(Parser* parser, void* /*userData*/)
{
#define readElseErrorIfNextTokenIsNotAssign(modToCheck) \
{ \
if (parser->tokenReader.peekTokenType() != TokenType::OpAssign) { parser->diagnose(modToCheck->loc, Diagnostics::unexpectedTokenExpectedTokenType, parser->tokenReader.peekTokenType(), TokenType::OpAssign); break;} \
else { parser->ReadToken(TokenType::OpAssign); } \
}

ModifierListBuilder listBuilder;

GLSLLayoutLocalSizeAttribute* numThreadsAttrib = nullptr;
Expand Down Expand Up @@ -7817,7 +7823,7 @@ namespace Slang
listBuilder.add(attr);
}

parser->ReadToken(TokenType::OpAssign);
readElseErrorIfNextTokenIsNotAssign(attr);

// If the token asked for is not returned found will put in recovering state, and return token found
Token valToken = parser->ReadToken(TokenType::IntegerLiteral);
Expand Down Expand Up @@ -7859,28 +7865,24 @@ namespace Slang
modifier->keywordName = nameAndLoc.name;
modifier->loc = nameAndLoc.loc;

if (parser->tokenReader.peekTokenType() == TokenType::OpAssign)

// Special handling for GLSLLayoutModifier
if (auto glslModifier = as<GLSLParsedLayoutModifier>(modifier))
{
// Special handling for GLSLLayoutModifier
if (auto glslModifier = as<GLSLParsedLayoutModifier>(modifier))
{
parser->ReadToken(TokenType::OpAssign);
glslModifier->valToken = parser->ReadToken(TokenType::IntegerLiteral);
}
//Special handling for GLSLOffsetLayoutAttribute to add to the byte offset tracker at a binding location
else if (auto glslOffset = as<GLSLOffsetLayoutAttribute>(modifier))
readElseErrorIfNextTokenIsNotAssign(modifier);
glslModifier->valToken = parser->ReadToken(TokenType::IntegerLiteral);
}
//Special handling for GLSLOffsetLayoutAttribute to add to the byte offset tracker at a binding location
else if (auto glslOffset = as<GLSLOffsetLayoutAttribute>(modifier))
{
if (auto binding = listBuilder.find<GLSLBindingAttribute>())
{
if (auto binding = listBuilder.find<GLSLBindingAttribute>())
{
parser->ReadToken(TokenType::OpAssign);
glslOffset->offset = int64_t(getIntegerLiteralValue(parser->ReadToken(TokenType::IntegerLiteral)));
parser->setBindingOffset(binding->binding, glslOffset->offset);
}
else
{
parser->diagnose(modifier->loc, Diagnostics::missingLayoutBindingModifier);
}
readElseErrorIfNextTokenIsNotAssign(modifier);
glslOffset->offset = int64_t(getIntegerLiteralValue(parser->ReadToken(TokenType::IntegerLiteral)));
parser->setBindingOffset(binding->binding, glslOffset->offset);
}
else
parser->diagnose(modifier->loc, Diagnostics::missingLayoutBindingModifier);
}

listBuilder.add(modifier);
Expand All @@ -7899,6 +7901,8 @@ namespace Slang
listBuilder.add(parser->astBuilder->create<GLSLLayoutModifierGroupEnd>());

return listBuilder.getFirst();

#undef readElseErrorIfNextTokenIsNotAssign
}

static NodeBase* parseBuiltinTypeModifier(Parser* parser, void* /*userData*/)
Expand Down
13 changes: 13 additions & 0 deletions tests/glsl-intrinsic/atomic/atomicErrorTest1.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target glsl -DTARGET_GLSL
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target spirv -emit-spirv-directly -DTARGET_SPIRV
#version 430

// CHECK: error 20001

//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):name=one
layout(binding = 1, offset = ) uniform atomic_uint one;

void computeMain()
{

}
13 changes: 13 additions & 0 deletions tests/glsl-intrinsic/atomic/atomicErrorTest2.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target glsl -DTARGET_GLSL
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target spirv -emit-spirv-directly -DTARGET_SPIRV
#version 430

// CHECK: error 20001

//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):name=one
layout(binding 1) uniform atomic_uint one;

void computeMain()
{

}
13 changes: 13 additions & 0 deletions tests/glsl-intrinsic/atomic/atomicErrorTest3.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target glsl -DTARGET_GLSL
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target spirv -emit-spirv-directly -DTARGET_SPIRV
#version 430

// CHECK: error 20001

//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):name=one
layout(offset ) uniform atomic_uint one;

void computeMain()
{

}
13 changes: 13 additions & 0 deletions tests/glsl-intrinsic/atomic/atomicErrorTest4.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target glsl -DTARGET_GLSL
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target spirv -emit-spirv-directly -DTARGET_SPIRV
#version 430

// CHECK: error 20001

//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):name=one
layout(binding = 1, offset) uniform atomic_uint one;

void computeMain()
{

}

0 comments on commit 3f1731a

Please sign in to comment.