Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shadercompiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions shadercompiler/ParserState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PreprocessorToken>& 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<InlineString>& arguments )
{
std::vector<PreprocessorToken> argument;
Expand Down Expand Up @@ -318,10 +337,10 @@ bool ParserState::ParseMacroArguments( std::vector<InlineString>& 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 == '(' )
{
Expand All @@ -331,7 +350,7 @@ bool ParserState::ParseMacroArguments( std::vector<InlineString>& arguments )
{
if( parenDepth == 0 )
{
arguments.push_back( MakeInlineString( argument.front().string.start, argument.back().string.end ) );
arguments.push_back( JoinTokens( argument ) );
return true;
}
else
Expand Down
1 change: 1 addition & 0 deletions shadercompiler/ParserState.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<InlineString>& arguments );
InlineString JoinTokens( const std::vector<PreprocessorToken>& tokens );
void AddIntrinsics();

struct FileContents
Expand Down
2 changes: 1 addition & 1 deletion shadercompiler/Preprocessor.y
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
160 changes: 160 additions & 0 deletions shadercompiler/tests/PreprocessorTest.cpp
Original file line number Diff line number Diff line change
@@ -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<DefaultCompiler>( Effect( "#error \"expected\"\n" ).c_str() ) );
}

TEST( Preprocessor, UndeclaredNameFailsCompile )
{
EXPECT_FALSE( Compiles<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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<DefaultCompiler>( 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 );
}