From fb7710ae467dab933bd7ee4ab9f2f2f05f650a20 Mon Sep 17 00:00:00 2001 From: ccpisheden Date: Fri, 27 Nov 2026 14:24:55 +0100 Subject: [PATCH 1/2] Fix macro expansion bugs in the shader compiler preprocessor - Macro arguments are built from their expanded tokens instead of a range of source text, which spanned unrelated buffers when an argument started or ended in a macro. Nested calls left parameter names unexpanded, and an object-like macro at the start of an argument could expand forever. - Commas inside parentheses in an argument are kept in the argument. - Empty macro arguments no longer read from an empty token list. - '>>' in #if conditions shifted left. - Add preprocessor tests to ShaderCompilerTest. Co-Authored-By: Claude Opus 5.5 --- shadercompiler/CMakeLists.txt | 1 + shadercompiler/ParserState.cpp | 25 +++- shadercompiler/ParserState.h | 1 + shadercompiler/Preprocessor.y | 2 +- shadercompiler/tests/PreprocessorTest.cpp | 153 ++++++++++++++++++++++ 5 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 shadercompiler/tests/PreprocessorTest.cpp diff --git a/shadercompiler/CMakeLists.txt b/shadercompiler/CMakeLists.txt index 0f843d4ca..cb49f5c7f 100644 --- a/shadercompiler/CMakeLists.txt +++ b/shadercompiler/CMakeLists.txt @@ -71,6 +71,7 @@ set(_TEST_SOURCES tests/Bindless.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 3e604aec0..3b2c63546 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..9352d772d --- /dev/null +++ b/shadercompiler/tests/PreprocessorTest.cpp @@ -0,0 +1,153 @@ +// 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 ) +{ + EXPECT_TRUE( Compiles( Effect( R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +#if FMA( 2, 3, FMA( 1, 1, 1 ) ) != 8 +#error "wrong expansion" +#endif +)SRC" ).c_str() ) ); +} + +TEST( Preprocessor, NestedCallInFirstArgument ) +{ + EXPECT_TRUE( Compiles( Effect( R"SRC( +#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) ) +#if FMA( FMA( 1, 1, 1 ), 3, 1 ) != 7 +#error "wrong expansion" +#endif +)SRC" ).c_str() ) ); +} + +TEST( Preprocessor, ObjectLikeMacroAtStartOfArgument ) +{ + EXPECT_TRUE( Compiles( Effect( 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" ).c_str() ) ); +} + +TEST( Preprocessor, ObjectLikeMacroAtEndOfArgument ) +{ + EXPECT_TRUE( Compiles( Effect( 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" ).c_str() ) ); +} + +TEST( Preprocessor, ParameterPassedOnToAnotherMacro ) +{ + EXPECT_TRUE( Compiles( Effect( R"SRC( +#define INNER( x, y ) ( ( x ) + ( y ) ) +#define OUTER( a ) INNER( a, 1 ) +#if OUTER( 5 ) != 6 +#error "wrong expansion" +#endif +)SRC" ).c_str() ) ); +} + +TEST( Preprocessor, EmptyArgument ) +{ + EXPECT_TRUE( Compiles( Effect( R"SRC( +#define PLUS_ONE( x ) x 1 +#if PLUS_ONE() != 1 +#error "wrong expansion" +#endif +)SRC" ).c_str() ) ); +} + +TEST( Preprocessor, NestedCallInShaderCode ) +{ + auto data = Compile( Effect( + "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n", + "float4( FMA( -p0.x, p0.x, FMA( -p0.y, p0.y, float( 1.0 ) ) ).xxxx )" ).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 ) +{ + auto data = Compile( Effect( + "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n", + "float4( FMA( float2( p0.x, p0.y ), float2( 2.0, 3.0 ), p0.zw ), 0.0, 1.0 )" ).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. + auto data = Compile( Effect( + "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n" + "static const float FMA = 0.5;\n", + "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 ); +} From a91eeac9771b409d26396058237af03f8de2eaae Mon Sep 17 00:00:00 2001 From: ccpisheden Date: Fri, 27 Nov 2026 14:51:22 +0100 Subject: [PATCH 2/2] Format preprocessor tests for clang-format Co-Authored-By: Claude Opus 5.5 --- shadercompiler/tests/PreprocessorTest.cpp | 51 +++++++++++++---------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/shadercompiler/tests/PreprocessorTest.cpp b/shadercompiler/tests/PreprocessorTest.cpp index 9352d772d..a8af75edb 100644 --- a/shadercompiler/tests/PreprocessorTest.cpp +++ b/shadercompiler/tests/PreprocessorTest.cpp @@ -62,81 +62,87 @@ TEST( Preprocessor, UndeclaredNameFailsCompile ) TEST( Preprocessor, NestedCallInLastArgument ) { - EXPECT_TRUE( Compiles( Effect( R"SRC( + 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" ).c_str() ) ); +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); } TEST( Preprocessor, NestedCallInFirstArgument ) { - EXPECT_TRUE( Compiles( Effect( R"SRC( + 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" ).c_str() ) ); +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); } TEST( Preprocessor, ObjectLikeMacroAtStartOfArgument ) { - EXPECT_TRUE( Compiles( Effect( R"SRC( + 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" ).c_str() ) ); +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); } TEST( Preprocessor, ObjectLikeMacroAtEndOfArgument ) { - EXPECT_TRUE( Compiles( Effect( R"SRC( + 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" ).c_str() ) ); +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); } TEST( Preprocessor, ParameterPassedOnToAnotherMacro ) { - EXPECT_TRUE( Compiles( Effect( R"SRC( + 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" ).c_str() ) ); +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); } TEST( Preprocessor, EmptyArgument ) { - EXPECT_TRUE( Compiles( Effect( R"SRC( + const char* prelude = R"SRC( #define PLUS_ONE( x ) x 1 #if PLUS_ONE() != 1 #error "wrong expansion" #endif -)SRC" ).c_str() ) ); +)SRC"; + EXPECT_TRUE( Compiles( Effect( prelude ).c_str() ) ); } TEST( Preprocessor, NestedCallInShaderCode ) { - auto data = Compile( Effect( - "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n", - "float4( FMA( -p0.x, p0.x, FMA( -p0.y, p0.y, float( 1.0 ) ) ).xxxx )" ).c_str() ); + 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 ) { - auto data = Compile( Effect( - "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n", - "float4( FMA( float2( p0.x, p0.y ), float2( 2.0, 3.0 ), p0.zw ), 0.0, 1.0 )" ).c_str() ); + 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 ); } @@ -144,10 +150,11 @@ TEST( Preprocessor, CommaInsideParenthesesInArgument ) TEST( Preprocessor, FunctionLikeMacroNameWithoutParenthesesIsNotExpanded ) { // A function-like macro's name not followed by '(' stays an identifier. - auto data = Compile( Effect( - "#define FMA( x, y, z ) ( ( x ) * ( y ) + ( z ) )\n" - "static const float FMA = 0.5;\n", - "float4( FMA, FMA, FMA, 1.0 )" ).c_str() ); + 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 ); }