diff --git a/source/ShaderWriter/Writer.cpp b/source/ShaderWriter/Writer.cpp index f787eaa2..ad45ba95 100644 --- a/source/ShaderWriter/Writer.cpp +++ b/source/ShaderWriter/Writer.cpp @@ -166,7 +166,7 @@ namespace sdw void ShaderWriter::pushScope() { - m_builder->pushScope( getStmtCache().makeContainer() ); + m_builder->pushScope( getStmtCache().makeCompound() ); } void ShaderWriter::popScope() diff --git a/test/Common.cpp b/test/Common.cpp index cfb2e6a0..9a330730 100644 --- a/test/Common.cpp +++ b/test/Common.cpp @@ -668,22 +668,22 @@ namespace test } } - void reportFailure( char const * const error - , char const * const function + void reportFailure( std::string_view error + , std::string_view function , int line , TestCounts & testCounts ) { - testCounts.reportFailure( error, function, line ); + testCounts.reportFailure( error.data(), function.data(), line ); } - void reportFailure( char const * const error - , char const * const callerFunction + void reportFailure( std::string_view error + , std::string_view callerFunction , int callerLine - , char const * const calleeFunction + , std::string_view calleeFunction , int calleeLine , TestCounts & testCounts ) { - testCounts.reportFailure( error, callerFunction, callerLine, calleeFunction, calleeLine ); + testCounts.reportFailure( error.data(), callerFunction.data(), callerLine, calleeFunction.data(), calleeLine ); } //********************************************************************************************* diff --git a/test/Common.hpp b/test/Common.hpp index 6289dcb8..b8a8d2eb 100644 --- a/test/Common.hpp +++ b/test/Common.hpp @@ -538,13 +538,40 @@ namespace test class Exception : public std::runtime_error { - using std::runtime_error::runtime_error; - public: + explicit Exception( char const * const message + , char const * const function + , int32_t line ) + : std::runtime_error{ message } + , m_function{ function } + , m_line{ line } + { + } + + explicit Exception( std::string const & message + , char const * const function + , int32_t line ) + : Exception{ message.c_str(), function, line } + { + } + std::string getText()const { return what(); } + + std::string getFunction()const + { + return m_function; + } + + int32_t getLine()const + { + return m_line; + } + + std::string m_function; + int32_t m_line; }; struct TestStringStreams @@ -789,45 +816,17 @@ namespace test void beginTest( TestCounts & testCounts , std::string name ); void endTest( TestCounts & testCounts ); - void reportFailure( char const * const error - , char const * const function + void reportFailure( std::string_view error + , std::string_view function , int line , TestCounts & testCounts ); - void reportFailure( char const * const error - , char const * const callerFunction + void reportFailure( std::string_view error + , std::string_view callerFunction , int callerLine - , char const * const calleeFunction + , std::string_view calleeFunction , int calleeLine , TestCounts & testCounts ); - inline void reportFailure( std::string_view error - , char const * const function - , int line - , TestCounts & testCounts ) - { - reportFailure( error.data(), function, line, testCounts ); - } - - inline void reportFailure( std::string_view error - , char const * const callerFunction - , int callerLine - , char const * const calleeFunction - , int calleeLine - , TestCounts & testCounts ) - { - reportFailure( error.data(), callerFunction, callerLine, calleeFunction, calleeLine, testCounts ); - } - - inline void reportFailure( std::string_view error - , std::string const & callerFunction - , int callerLine - , char const * const calleeFunction - , int calleeLine - , TestCounts & testCounts ) - { - reportFailure( error.data(), callerFunction.c_str(), callerLine, calleeFunction, calleeLine, testCounts ); - } - # define astTestSuiteMain( testName )\ static test::TestResults launch##testName( test::TestSuite & suite, test::TestCounts & testCounts ) @@ -945,13 +944,13 @@ namespace test testCounts.incTest();\ if ( !( x ) )\ {\ - throw test::Exception{ "\n Value: " + toString( x ) };\ + throw test::Exception{ testConcatStr2( x, " failed." ), __FUNCTION__, __LINE__ };\ }\ testCounts.flushErrors();\ }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( testConcatStr2( x, " failed:" ) + exc.getText(), __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( testConcatStr2( x, " failed:" ) + exc.getText(), exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ @@ -964,7 +963,7 @@ namespace test testCounts.incTest();\ if ( !( x ) )\ {\ - throw test::Exception{ "\n Value: " + toString( x ) };\ + throw test::Exception{ testConcatStr2( x, " failed." ), __FUNCTION__, __LINE__ };\ } #define astEndRequire\ @@ -972,11 +971,11 @@ namespace test }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( testConcatStr2( x, " failed:" ) + exc.getText(), __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( exc.getText(), exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ - test::reportFailure( testConcatStr2( x, " failed." ), __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( "Unknown unhandled exception.", __FUNCTION__, __LINE__, testCounts );\ } #define astCheck( x )\ @@ -1000,13 +999,13 @@ namespace test testCounts.incTest();\ if ( !( ( x ) == ( y ) ) )\ {\ - throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ) };\ + throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ), __FUNCTION__, __LINE__ };\ }\ testCounts.flushErrors();\ }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( astTestConcatStr4( x, " == ", y, " failed:" ) + exc.getText(), __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( astTestConcatStr4( x, " == ", y, " failed:" ) + exc.getText(), exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ @@ -1019,13 +1018,13 @@ namespace test testCounts.incTest();\ if ( ( x ) == ( y ) )\ {\ - throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ) };\ + throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ), __FUNCTION__, __LINE__ };\ }\ testCounts.flushErrors();\ }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( astTestConcatStr4( x, " != ", y, " failed:" ) + exc.getText(), __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( astTestConcatStr4( x, " != ", y, " failed:" ) + exc.getText(), exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ @@ -1077,13 +1076,13 @@ namespace test testCounts.incTest();\ if ( !( x ) )\ {\ - throw test::Exception{ "\n Value: " + toString( x ) };\ + throw test::Exception{ "\n Value: " + toString( x ), __FUNCTION__, __LINE__ };\ }\ testCounts.flushErrors();\ }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( testConcatStr2( x, " failed:" ) + exc.getText(), f, l, __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( testConcatStr2( x, " failed:" ) + exc.getText(), f, l, exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ @@ -1111,13 +1110,13 @@ namespace test testCounts.incTest();\ if ( !( ( x ) == ( y ) ) )\ {\ - throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ) };\ + throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ), __FUNCTION__, __LINE__ };\ }\ testCounts.flushErrors();\ }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( astTestConcatStr4( x, " == ", y, " failed:" ) + exc.getText(), f, l, __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( astTestConcatStr4( x, " == ", y, " failed:" ) + exc.getText(), f, l, exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ @@ -1130,13 +1129,13 @@ namespace test testCounts.incTest();\ if ( ( x ) == ( y ) )\ {\ - throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ) };\ + throw test::Exception{ "\n LHS: " + toString( x ) + "\n RHS: " + toString( y ), __FUNCTION__, __LINE__ };\ }\ testCounts.flushErrors();\ }\ catch ( test::Exception & exc )\ {\ - test::reportFailure( astTestConcatStr4( x, " != ", y, " failed:" ) + exc.getText(), f, l, __FUNCTION__, __LINE__, testCounts );\ + test::reportFailure( astTestConcatStr4( x, " != ", y, " failed:" ) + exc.getText(), f, l, exc.getFunction(), exc.getLine(), testCounts );\ }\ catch ( ... )\ {\ diff --git a/test/ShaderWriter/TestWriterControlStatements.cpp b/test/ShaderWriter/TestWriterControlStatements.cpp index 92597828..b1b545b5 100644 --- a/test/ShaderWriter/TestWriterControlStatements.cpp +++ b/test/ShaderWriter/TestWriterControlStatements.cpp @@ -849,6 +849,70 @@ namespace , testCounts, CurrentCompilers ); astTestEnd(); } + + void testAnonymousScope( test::sdw_test::TestCounts & testCounts ) + { + astTestBegin( "testAnonymousScope" ); + sdw::ShaderArray shaders; + { + sdw::ComputeWriter writer{ &testCounts.allocator }; + auto i = writer.declSharedVariable< sdw::UInt >( "i" ); + writer.implementMain( 32u, [&]( sdw::ComputeIn in ) + { + if (auto scope = makeScope( writer ) ) + { + i = in.globalInvocationID.x(); + } + } ); + test::writeShader( writer + , testCounts, CurrentCompilers ); + shaders.emplace_back( std::move( writer.getShader() ) ); + } + test::validateShaders( shaders + , testCounts, CurrentCompilers ); + astTestEnd(); + } + + void testNestedAnonymousScopes( test::sdw_test::TestCounts & testCounts ) + { + astTestBegin( "testNestedAnonymousScopes" ); + sdw::ShaderArray shaders; + { + sdw::ComputeWriter writer{ &testCounts.allocator }; + auto i = writer.declSharedVariable< sdw::UInt >( "i" ); + auto j = writer.declSharedVariable< sdw::UInt >( "j" ); + auto k = writer.declSharedVariable< sdw::UInt >( "k" ); + auto l = writer.declSharedVariable< sdw::UInt >( "l" ); + writer.implementMain( 32u, [&]( sdw::ComputeIn in ) + { + if (auto scope1 = makeScope( writer ) ) + { + l = in.localInvocationIndex; + if ( auto scope2 = makeScope( writer ) ) + { + k = in.globalInvocationID.z(); + if ( auto scope3 = makeScope( writer ) ) + { + j = in.globalInvocationID.y(); + if ( auto scope4 = makeScope( writer ) ) + { + i = in.globalInvocationID.x(); + } + j += in.globalInvocationID.y(); + } + k += in.globalInvocationID.z(); + } + l += in.localInvocationIndex; + } + } ); + test::writeShader( writer + , testCounts, CurrentCompilers ); + shaders.emplace_back( std::move( writer.getShader() ) ); + } + test::validateShaders( shaders + , testCounts, CurrentCompilers ); + astTestEnd(); + } } sdwTestSuiteMain( TestWriterControlStatements ) @@ -884,6 +948,8 @@ sdwTestSuiteMain( TestWriterControlStatements ) testConstSwitch0( testCounts ); testConstSwitch1( testCounts ); testConstSwitchDefault( testCounts ); + testAnonymousScope( testCounts ); + testNestedAnonymousScopes( testCounts ); sdwTestSuiteEnd(); } diff --git a/test/ShaderWriter/TestWriterIncrement.cpp b/test/ShaderWriter/TestWriterIncrement.cpp index 7414b0bd..a2be2cb6 100644 --- a/test/ShaderWriter/TestWriterIncrement.cpp +++ b/test/ShaderWriter/TestWriterIncrement.cpp @@ -169,7 +169,7 @@ namespace } ROF; astCheckEqual( writer.getBuilder().getContainer()->size(), 1u ); - astBeginRequire( writer.getBuilder().getContainer()->back()->getKind() == stmt::Kind::eContainer ); + astBeginRequire( writer.getBuilder().getContainer()->back()->getKind() == stmt::Kind::eCompound ); astCheckEqual( static_cast< stmt::Container const & >( *writer.getBuilder().getContainer()->back() ).size(), 1u ); astBeginRequire( static_cast< stmt::Container const & >( *writer.getBuilder().getContainer()->back() ).back()->getKind() == stmt::Kind::eFor ); astCheckEqual( static_cast< stmt::For const & >( *static_cast< stmt::Container const & >( *writer.getBuilder().getContainer()->back() ).back() ).size(), 1u );