diff --git a/shadercompiler/CMakeLists.txt b/shadercompiler/CMakeLists.txt index cabee075c..0d48454d3 100644 --- a/shadercompiler/CMakeLists.txt +++ b/shadercompiler/CMakeLists.txt @@ -72,6 +72,7 @@ set(_TEST_SOURCES tests/ForwardDeclarationTest.cpp tests/GlobalInputTest.cpp tests/MetalConversionTest.cpp + tests/PreprocessorTest.cpp tests/RayTracingTest.cpp tests/ShaderCompilerTest.cpp tests/TesingUtils.h diff --git a/shadercompiler/ParserState.cpp b/shadercompiler/ParserState.cpp index 9b0e83af5..0a06457ff 100644 --- a/shadercompiler/ParserState.cpp +++ b/shadercompiler/ParserState.cpp @@ -289,6 +289,25 @@ ParserState::~ParserState() extern PreprocessorScanResult GetPreprocessorToken( ParserState& state, PreprocessorToken& token ); extern bool ConvertToScannerToken( ParserState& state, const PreprocessorToken& ppToken, ScannerToken& token ); +// Builds a macro argument's text from its tokens. The tokens are already macro-expanded in the +// caller's context and can come from different buffers (source file, macro bodies, other +// arguments), so the argument can't be a range of the source text. +InlineString ParserState::JoinTokens( const std::vector& tokens ) +{ + std::string text; + for( auto& token : tokens ) + { + if( !text.empty() ) + { + text += ' '; + } + text.append( token.string.start, token.string.end ); + } + char* string = AllocateString( text.length() + 1 ); + memcpy( string, text.c_str(), text.length() + 1 ); + return MakeInlineString( string, string + text.length() ); +} + bool ParserState::ParseMacroArguments( std::vector& arguments ) { std::vector argument; @@ -318,10 +337,10 @@ bool ParserState::ParseMacroArguments( std::vector& arguments ) { if( parenDepth == 0 ) { - arguments.push_back( MakeInlineString( argument.front().string.start, argument.back().string.end ) ); + arguments.push_back( JoinTokens( argument ) ); argument.clear(); + continue; } - continue; } else if( *token.string.start == '(' ) { @@ -331,7 +350,7 @@ bool ParserState::ParseMacroArguments( std::vector& arguments ) { if( parenDepth == 0 ) { - arguments.push_back( MakeInlineString( argument.front().string.start, argument.back().string.end ) ); + arguments.push_back( JoinTokens( argument ) ); return true; } else diff --git a/shadercompiler/ParserState.h b/shadercompiler/ParserState.h index 73cdc73d7..ae70ebbb6 100644 --- a/shadercompiler/ParserState.h +++ b/shadercompiler/ParserState.h @@ -270,6 +270,7 @@ class ParserState void ShowMessageImpl( const FileLocation& location, int32_t errorCode, va_list args ); bool FindConcatOperator( size_t depth ); bool ParseMacroArguments( std::vector& arguments ); + InlineString JoinTokens( const std::vector& tokens ); void AddIntrinsics(); struct FileContents diff --git a/shadercompiler/Preprocessor.y b/shadercompiler/Preprocessor.y index 13fcbc314..24a11f314 100644 --- a/shadercompiler/Preprocessor.y +++ b/shadercompiler/Preprocessor.y @@ -174,7 +174,7 @@ shift_expression(A) ::= shift_expression(B) PP_LEFT_OP additive_expression(D). shift_expression(A) ::= shift_expression(B) PP_RIGHT_OP additive_expression(D). { - A = B << D; + A = B >> D; } diff --git a/shadercompiler/tests/PreprocessorTest.cpp b/shadercompiler/tests/PreprocessorTest.cpp new file mode 100644 index 000000000..a8af75edb --- /dev/null +++ b/shadercompiler/tests/PreprocessorTest.cpp @@ -0,0 +1,160 @@ +// Copyright © 2026 CCP ehf. + +#include "gtest/gtest.h" +#include "EffectCompilerMetal.h" +#include "EffectCompilerDX12.h" +#include "EffectData.h" +#include "TesingUtils.h" + +#if _WIN32 +using DefaultCompiler = EffectCompilerDX12; +#else +using DefaultCompiler = EffectCompilerMetal; +#endif + +namespace +{ + +// A minimal effect. The first %s is placed before the shaders (macros, #if checks), and the +// second is the expression the pixel shader returns. +const char* s_effect = R"SRC( +%s + +float4 p0; + +float4 vs(): SV_Position +{ + return float4( 0.0, 0.0, 0.0, 1.0 ); +} + +float4 ps(): SV_Target +{ + return %s; +} + +technique t0 +{ + pass p0 + { + vertexshader = compile vs_3_0 vs(); + pixelshader = compile ps_3_0 ps(); + } +} +)SRC"; + +std::string Effect( const char* prelude, const char* result = "float4( 0.0, 0.0, 0.0, 1.0 )" ) +{ + return FormatString( s_effect, prelude, result ); +} + +} + +// Guards the tests below: they rely on #error and bad code failing the compile. +TEST( Preprocessor, ErrorDirectiveFailsCompile ) +{ + EXPECT_FALSE( Compiles( Effect( "#error \"expected\"\n" ).c_str() ) ); +} + +TEST( Preprocessor, UndeclaredNameFailsCompile ) +{ + EXPECT_FALSE( Compiles( Effect( "", "UNDECLARED_NAME" ).c_str() ) ); +} + +TEST( Preprocessor, NestedCallInLastArgument ) +{ + const char* prelude = R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +#if FMA( 2, 3, FMA( 1, 1, 1 ) ) != 8 +#error "wrong expansion" +#endif +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); +} + +TEST( Preprocessor, NestedCallInFirstArgument ) +{ + const char* prelude = R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +#if FMA( FMA( 1, 1, 1 ), 3, 1 ) != 7 +#error "wrong expansion" +#endif +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); +} + +TEST( Preprocessor, ObjectLikeMacroAtStartOfArgument ) +{ + const char* prelude = R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +#define ONE 1 +#if FMA( 2, 3, ONE + 1 ) != 8 +#error "wrong expansion" +#endif +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); +} + +TEST( Preprocessor, ObjectLikeMacroAtEndOfArgument ) +{ + const char* prelude = R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +#define ONE 1 +#if FMA( 2, 3, 1 + ONE ) != 8 +#error "wrong expansion" +#endif +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); +} + +TEST( Preprocessor, ParameterPassedOnToAnotherMacro ) +{ + const char* prelude = R"SRC( +#define INNER( x, y ) ( ( x ) + ( y ) ) +#define OUTER( a ) INNER( a, 1 ) +#if OUTER( 5 ) != 6 +#error "wrong expansion" +#endif +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); +} + +TEST( Preprocessor, EmptyArgument ) +{ + const char* prelude = R"SRC( +#define PLUS_ONE( x ) x 1 +#if PLUS_ONE() != 1 +#error "wrong expansion" +#endif +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); +} + +TEST( Preprocessor, NestedCallInShaderCode ) +{ + const char* prelude = "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n"; + const char* result = "float4( FMA( -p0.x, p0.x, FMA( -p0.y, p0.y, float( 1.0 ) ) ).xxxx )"; + auto data = Compile( Effect( prelude, result ).c_str() ); + ASSERT_EQ( data.techniques.size(), 1u ); + EXPECT_EQ( data.techniques[0].passes[0].stages[1].source.find( "FMA" ), std::string::npos ); +} + +TEST( Preprocessor, CommaInsideParenthesesInArgument ) +{ + const char* prelude = "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n"; + const char* result = "float4( FMA( float2( p0.x, p0.y ), float2( 2.0, 3.0 ), p0.zw ), 0.0, 1.0 )"; + auto data = Compile( Effect( prelude, result ).c_str() ); + ASSERT_EQ( data.techniques.size(), 1u ); + EXPECT_EQ( data.techniques[0].passes[0].stages[1].source.find( "FMA" ), std::string::npos ); +} + +TEST( Preprocessor, FunctionLikeMacroNameWithoutParenthesesIsNotExpanded ) +{ + // A function-like macro's name not followed by '(' stays an identifier. + const char* prelude = R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +static const float FMA = 0.5; +)SRC"; + auto data = Compile( Effect( prelude, "float4( FMA, FMA, FMA, 1.0 )" ).c_str() ); + ASSERT_EQ( data.techniques.size(), 1u ); + EXPECT_NE( data.techniques[0].passes[0].stages[1].source.find( "FMA" ), std::string::npos ); +}