diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/slang-cpp-extractor/diagnostic-defs.h | 1 | ||||
| -rw-r--r-- | tools/slang-cpp-extractor/file-util.cpp | 90 | ||||
| -rw-r--r-- | tools/slang-embed/slang-embed.cpp | 20 | ||||
| -rw-r--r-- | tools/slang-test/slang-test-main.cpp | 118 |
4 files changed, 104 insertions, 125 deletions
diff --git a/tools/slang-cpp-extractor/diagnostic-defs.h b/tools/slang-cpp-extractor/diagnostic-defs.h index 34b444206..ba43f1844 100644 --- a/tools/slang-cpp-extractor/diagnostic-defs.h +++ b/tools/slang-cpp-extractor/diagnostic-defs.h @@ -23,6 +23,7 @@ DIAGNOSTIC(-1, Note, seeOpen, "see open $0") DIAGNOSTIC(-1, Note, commandLine, "Command line: $0") DIAGNOSTIC(1, Error, cannotOpenFile, "cannot open file '$0'.") +DIAGNOSTIC(1, Error, errorAccessingFile, "error accessing file '$0'.") DIAGNOSTIC(1, Error, extractorFailed, "C++ Extractor failed") DIAGNOSTIC(1, Error, internalError, "Unknown internal error in C++ Extractor, aborted!") diff --git a/tools/slang-cpp-extractor/file-util.cpp b/tools/slang-cpp-extractor/file-util.cpp index 1d37df650..e73ba7f55 100644 --- a/tools/slang-cpp-extractor/file-util.cpp +++ b/tools/slang-cpp-extractor/file-util.cpp @@ -5,60 +5,80 @@ namespace CppExtract { using namespace Slang; -/* static */SlangResult FileUtil::readAllText(const Slang::String& fileName, DiagnosticSink* sink, String& outRead) +namespace { // anonymous +struct DiagnosticReporter { - try - { - StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); - outRead = reader.ReadToEnd(); - } - catch (const IOException&) + SlangResult report(SlangResult res) { - if (sink) + if (SLANG_FAILED(res)) { - sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName); + if (m_sink) + { + if (res == SLANG_E_CANNOT_OPEN) + { + m_sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, m_filename); + } + else + { + m_sink->diagnose(SourceLoc(), CPPDiagnostics::errorAccessingFile, m_filename); + } + } } - return SLANG_FAIL; + return res; } - catch (...) + + DiagnosticReporter(const String& filename, DiagnosticSink* sink) : + m_filename(filename), + m_sink(sink) { - if (sink) - { - sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName); - } - return SLANG_FAIL; } + DiagnosticSink* m_sink; + String m_filename; +}; + +} // anonymous + +/* static */SlangResult FileUtil::readAllText(const Slang::String& fileName, DiagnosticSink* sink, String& outRead) +{ + DiagnosticReporter reporter(fileName, sink); + + RefPtr<FileStream> stream = new FileStream; + SLANG_RETURN_ON_FAIL(reporter.report(stream->init(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite))); + + StreamReader reader; + SLANG_RETURN_ON_FAIL(reporter.report(reader.init(stream))); + SLANG_RETURN_ON_FAIL(reporter.report(reader.readToEnd(outRead))); + return SLANG_OK; } /* static */SlangResult FileUtil::writeAllText(const Slang::String& fileName, DiagnosticSink* sink, const UnownedStringSlice& text) { - try - { - if (File::exists(fileName)) - { - String existingText; + // TODO(JS): + // There is an optimization/behavior here,that checks if the contents has changed. It only writes if it hasn't + // That might not be what you want (both because of extra work of read, the file modified stamp or other reasons, file is write only etc) + // NOTE! That this also does the work of the comparison after it is decoded, but the *bytes* might actually be different. - if (readAllText(fileName, nullptr, existingText) == SLANG_OK) - { - if (existingText == text) - return SLANG_OK; - } - } - StreamWriter writer(new FileStream(fileName, FileMode::Create)); - writer.Write(text); - } - catch (const IOException&) + if (File::exists(fileName)) { - if (sink) + String existingText; + if (SLANG_SUCCEEDED(readAllText(fileName, nullptr, existingText))) { - sink->diagnose(SourceLoc(), CPPDiagnostics::cannotOpenFile, fileName); + if (existingText == text) + return SLANG_OK; } - return SLANG_FAIL; } - return SLANG_OK; + DiagnosticReporter reporter(fileName, sink); + + RefPtr<FileStream> stream = new FileStream; + SLANG_RETURN_ON_FAIL(reporter.report(stream->init(fileName, FileMode::Create))); + + StreamWriter writer; + SLANG_RETURN_ON_FAIL(reporter.report(writer.init(stream))); + SLANG_RETURN_ON_FAIL(reporter.report(writer.write(text))) + return SLANG_OK; } /* static */ void FileUtil::indent(Index indentCount, StringBuilder& out) diff --git a/tools/slang-embed/slang-embed.cpp b/tools/slang-embed/slang-embed.cpp index 17b71d216..4a81c1b95 100644 --- a/tools/slang-embed/slang-embed.cpp +++ b/tools/slang-embed/slang-embed.cpp @@ -19,6 +19,7 @@ #include "../../source/core/slang-string-util.h" #include "../../source/core/slang-io.h" + // Utility to free pointers on scope exit struct ScopedMemory { @@ -86,6 +87,8 @@ struct App bool useNewStringLit = true; void processInputFile(FILE* outputFile, Slang::String inputPath) { + using namespace Slang; + // We open the input file in text mode because we are currently // embedding textual source files. If/when this utility gets // used for binary files another mode could be called for. @@ -94,15 +97,22 @@ struct App // could lead to a difference in the embedded bytes based on // the line ending convention of the host platform) // - Slang::StreamReader streamReader(inputPath); - while (!streamReader.IsEnd()) + + String contents; + { + auto res = File::readAllText(inputPath, contents); + SLANG_ASSERT(SLANG_SUCCEEDED(res)); + } + + LineParser lineReader(contents.getUnownedSlice()); + + for (auto line : lineReader) { - auto line = streamReader.ReadLine(); - Slang::String trimedLine = line.trimStart(); + auto trimedLine = line.trimStart(); if (trimedLine.startsWith("#include")) { auto fileName = - Slang::StringUtil::getAtInSplit(trimedLine.getUnownedSlice(), ' ', 1); + Slang::StringUtil::getAtInSplit(trimedLine, ' ', 1); if (fileName[0] == '<') goto normalProcess; fileName = Slang::UnownedStringSlice(fileName.begin() + 1, fileName.end() - 1); diff --git a/tools/slang-test/slang-test-main.cpp b/tools/slang-test/slang-test-main.cpp index 4f96c3a6a..6fb270c35 100644 --- a/tools/slang-test/slang-test-main.cpp +++ b/tools/slang-test/slang-test-main.cpp @@ -436,14 +436,8 @@ static SlangResult _gatherTestsForFile( outTestList->tests.clear(); String fileContents; - try - { - fileContents = Slang::File::readAllText(filePath); - } - catch (const Slang::IOException&) - { - return SLANG_FAIL; - } + + SLANG_RETURN_ON_FAIL(Slang::File::readAllText(filePath, fileContents)); // Walk through the lines of the file, looking for test commands char const* cursor = fileContents.begin(); @@ -1168,13 +1162,9 @@ TestResult runDocTest(TestContext* context, TestInput& input) String expectedOutputPath = outputStem + ".expected"; String expectedOutput; - try - { - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } + + // TODO(JS): Might want to check the result code.. + Slang::File::readAllText(expectedOutputPath, expectedOutput); // If no expected output file was found, then we // expect everything to be empty @@ -1258,14 +1248,9 @@ TestResult runSimpleTest(TestContext* context, TestInput& input) String expectedOutputPath = outputStem + ".expected"; String expectedOutput; - try - { - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } - + + Slang::File::readAllText(expectedOutputPath, expectedOutput); + // If no expected output file was found, then we // expect everything to be empty if (expectedOutput.getLength() == 0) @@ -1298,15 +1283,7 @@ TestResult runSimpleTest(TestContext* context, TestInput& input) SlangResult _readText(const UnownedStringSlice& path, String& out) { - try - { - out = Slang::File::readAllText(path); - } - catch (const Slang::IOException&) - { - return SLANG_FAIL; - } - return SLANG_OK; + return Slang::File::readAllText(path, out); } static SlangResult _readExpected(const UnownedStringSlice& stem, String& out) @@ -1540,13 +1517,8 @@ TestResult runReflectionTest(TestContext* context, TestInput& input) String expectedOutputPath = outputStem + ".expected"; String expectedOutput; - try - { - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } + + Slang::File::readAllText(expectedOutputPath, expectedOutput); // If no expected output file was found, then we // expect everything to be empty @@ -1581,14 +1553,9 @@ String getExpectedOutput(String const& outputStem) { String expectedOutputPath = outputStem + ".expected"; String expectedOutput; - try - { - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } - + + Slang::File::readAllText(expectedOutputPath, expectedOutput); + // If no expected output file was found, then we // expect everything to be empty if (expectedOutput.getLength() == 0) @@ -1727,15 +1694,10 @@ static TestResult runCPPCompilerSharedLibrary(TestContext* context, TestInput& i { // Read the expected String expectedOutput; - try - { - String expectedOutputPath = outputStem + ".expected"; - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } - + + String expectedOutputPath = outputStem + ".expected"; + Slang::File::readAllText(expectedOutputPath, expectedOutput); + // Compare if they are the same if (!StringUtil::areLinesEqual(actualOutput.getUnownedSlice(), expectedOutput.getUnownedSlice())) { @@ -1867,15 +1829,10 @@ static TestResult runCPPCompilerExecute(TestContext* context, TestInput& input) { // Read the expected String expectedOutput; - try - { - String expectedOutputPath = outputStem + ".expected"; - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } - + + String expectedOutputPath = outputStem + ".expected"; + Slang::File::readAllText(expectedOutputPath, expectedOutput); + // Compare if they are the same if (!StringUtil::areLinesEqual(actualOutput.getUnownedSlice(), expectedOutput.getUnownedSlice())) { @@ -1960,11 +1917,8 @@ TestResult runCrossCompilerTest(TestContext* context, TestInput& input) { expectedOutput = getOutput(expectedExeRes); String expectedOutputPath = outputStem + ".expected"; - try - { - Slang::File::writeAllText(expectedOutputPath, expectedOutput); - } - catch (const Slang::IOException&) + + if (SLANG_FAILED(Slang::File::writeAllText(expectedOutputPath, expectedOutput))) { return TestResult::Fail; } @@ -2045,14 +1999,12 @@ TestResult generateHLSLBaseline( String expectedOutput = getOutput(exeRes); String expectedOutputPath = outputStem + ".expected"; - try - { - Slang::File::writeAllText(expectedOutputPath, expectedOutput); - } - catch (const Slang::IOException&) + + if (SLANG_FAILED(Slang::File::writeAllText(expectedOutputPath, expectedOutput))) { return TestResult::Fail; } + return TestResult::Pass; } @@ -2126,14 +2078,8 @@ static TestResult _runHLSLComparisonTest( String actualOutput = actualOutputBuilder.ProduceString(); String expectedOutput; - try - { - expectedOutput = Slang::File::readAllText(expectedOutputPath); - } - catch (const Slang::IOException&) - { - } - + Slang::File::readAllText(expectedOutputPath, expectedOutput); + TestResult result = TestResult::Pass; // If no expected output file was found, then we @@ -2543,8 +2489,10 @@ TestResult runComputeComparisonImpl(TestContext* context, TestInput& input, cons printf("referenceOutput %s not found.\n", referenceOutputFile.getBuffer()); return TestResult::Fail; } - auto actualOutputContent = File::readAllText(actualOutputFile); - auto referenceOutputContent = File::readAllText(referenceOutputFile); + String actualOutputContent, referenceOutputContent; + + File::readAllText(actualOutputFile, actualOutputContent); + File::readAllText(referenceOutputFile, referenceOutputContent); if (SLANG_FAILED(_compareWithType(actualOutputContent.getUnownedSlice(), referenceOutputContent.getUnownedSlice()))) { |
