Skip to content

Commit

Permalink
Fix a crash when search for files. (#5818)
Browse files Browse the repository at this point in the history
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
  • Loading branch information
csyonghe and expipiplus1 authored Dec 10, 2024
1 parent b0dfb1a commit 89bf795
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/core/slang-string-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ String StringUtil::replaceAll(
StringBuilder builder;
for (Index i = 0; i < text.getLength();)
{
if (i + subStr.getLength() >= text.getLength())
if (i + subStr.getLength() > text.getLength())
{
builder.append(text.subString(i, text.getLength() - i));
break;
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4257,7 +4257,7 @@ SourceFile* Linkage::findFile(Name* name, SourceLoc loc, IncludeSystem& outInclu
// Next, try to find the file of the given name,
// using our ordinary include-handling logic.

auto searchDirs = getSearchDirectories();
auto& searchDirs = getSearchDirectories();
outIncludeSystem = IncludeSystem(&searchDirs, getFileSystemExt(), getSourceManager());

// Get the original path info
Expand Down
3 changes: 3 additions & 0 deletions tests/language-feature/modules/gh-5799/Common/Common.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Common;

__include Common.Test;
8 changes: 8 additions & 0 deletions tests/language-feature/modules/gh-5799/Common/Test.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//#pragma once
#include "Simple.h"
//
//#ifndef HOST_CODE
//implementing Common;
//#endif
implementing Common;

37 changes: 37 additions & 0 deletions tests/language-feature/modules/gh-5799/HelloTriangleFS.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//TEST:SIMPLE(filecheck=CHECK): -target spirv -I $dirname

//CHECK: OpEntryPoint

// shaders.slang

//
// This file provides a simple vertex and fragment shader that can be compiled
// using Slang. This code should also be valid as HLSL, and thus it does not
// use any of the new language features supported by Slang.
//

import Scene.Scene;

// Output of the vertex shader, and input to the fragment shader.
struct CoarseVertex
{
float3 color;
};

// Output of the fragment shader
struct Fragment
{
float4 color;
};


// Fragment Shader

[shader("fragment")]
float4 main(
CoarseVertex coarseVertex : CoarseVertex) : SV_Target
{
float3 fragColor = coarseVertex.color;

return float4(fragColor, 1.0);
}
3 changes: 3 additions & 0 deletions tests/language-feature/modules/gh-5799/Scene/Scene.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Scene;
import Common.Common;

Empty file.
13 changes: 13 additions & 0 deletions tools/slang-test/slang-test-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,17 @@ static SlangResult _extractCommand(const char** ioCursor, UnownedStringSlice& ou
}
}

static void applyMacroSubstitution(String filePath, TestDetails& details)
{
for (auto& arg : details.options.args)
{
arg = StringUtil::replaceAll(
arg.getUnownedSlice(),
toSlice("$dirname"),
Path::getParentDirectory(filePath).getUnownedSlice());
}
}

// Try to read command-line options from the test file itself
static SlangResult _gatherTestsForFile(
TestCategorySet* categorySet,
Expand Down Expand Up @@ -597,6 +608,7 @@ static SlangResult _gatherTestsForFile(
if (command == "TEST")
{
SLANG_RETURN_ON_FAIL(_gatherTestOptions(categorySet, &cursor, testDetails.options));
applyMacroSubstitution(filePath, testDetails);

// See if the type of test needs certain APIs available
const RenderApiFlags testRequiredApis =
Expand All @@ -611,6 +623,7 @@ static SlangResult _gatherTestsForFile(
else if (command == "DIAGNOSTIC_TEST")
{
SLANG_RETURN_ON_FAIL(_gatherTestOptions(categorySet, &cursor, testDetails.options));
applyMacroSubstitution(filePath, testDetails);

// Apply the file wide options
_combineOptions(categorySet, fileOptions, testDetails.options);
Expand Down

0 comments on commit 89bf795

Please sign in to comment.