summaryrefslogtreecommitdiffstats
path: root/source/core/slang-writer.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2018-12-12 08:57:48 -0500
committerGitHub <noreply@github.com>2018-12-12 08:57:48 -0500
commit49ed6b60d662906f290578f802f80b0ead1a2b9d (patch)
treee47050f6508a4b3a4d38b756e9b3c53e0d159507 /source/core/slang-writer.cpp
parent62d3e387774255be4d507cca045ac97dabac9970 (diff)
Running tests in slang-test process (#740)
* First pass at having an interface to write text to that can be replaced. Simplifed and made more rigerous the interface used to write formatted strings. * Added AppContext to simplify setting up and parsing around of streams. * Added more simplified way to get the std error/out from AppContext. * Work in progress using dll for tools to speed up testing. * First pass at ISlangWriter interface. * Added support for writing VaArgs. Added NullWriter. * Use ISlangWriter for output. * Use ISlangWriter for output - replacing OutputCallback. Make IRDump go to ISlangWriter * SlangWriterTargetType -> SlangWriterChannel Improvements around AppContext * Shared library working with slang-reflection-test. * Dll testing working for render-test. * Include va_list definintion from header. * Fix errors from clang. * Fix typo for linux. * Added -usexes option * Fix typo. * Fix arguments problem on linux. * Fix typo for linux. * Add windows tool shared library projects. * Fix warning from x86 win build. Fix signed warning from slang-test/main.cpp * First attempt at getting premake to work on travis, and run tests. * Try moving build out into script. * Invoke bash scripts so they don't have to be executable. * Drive configuration/tests from env parameters set by travis * Try using source to run travis tests. * Remove the build.linux directory - but doing so will overwrite Makefile. * Made -fno-delete-null-pointer-checks gcc only. * Try to fix warning from -fno-delete-null-pointer-checks * Turn of warnings for unknown switches. * Try to make premake choose the correct tooling. * Disabled missing braces warning. * Disable -Wundefined-var-template on clang. * -Wunused-function disabled for clang. * Fix typo due to SlangBool. * Remove this nullptr tests. * "-Wno-unused-private-field" for clang. * Added "-Wno-undefined-bool-conversion" * Add DominatorList::end fix. * Split scripts into travis_build.sh travis_test.sh * Fix gcc/clang template pre-declaration issue around QualType. * Fix premake to build such that pthread correctly links with slang-glslang
Diffstat (limited to 'source/core/slang-writer.cpp')
-rw-r--r--source/core/slang-writer.cpp152
1 files changed, 152 insertions, 0 deletions
diff --git a/source/core/slang-writer.cpp b/source/core/slang-writer.cpp
new file mode 100644
index 000000000..4c5df0c8a
--- /dev/null
+++ b/source/core/slang-writer.cpp
@@ -0,0 +1,152 @@
+#include "slang-writer.h"
+
+#include "platform.h"
+#include "slang-string-util.h"
+
+// Includes to allow us to control console
+// output when writing assembly dumps.
+#include <fcntl.h>
+#ifdef _WIN32
+# include <io.h>
+#else
+# include <unistd.h>
+#endif
+
+#include <stdarg.h>
+
+namespace Slang
+{
+static const Guid IID_ISlangWriter = SLANG_UUID_ISlangWriter;
+static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!! WriterHelper !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+SlangResult WriterHelper::print(const char* format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ SlangResult res = m_writer->writeVaList(format, args);
+
+ if (res == SLANG_E_NOT_IMPLEMENTED)
+ {
+ StringBuilder builder;
+ StringUtil::append(format, args, builder);
+
+ // Write if there is anything to write
+ res = (builder.Length()) ? m_writer->write(builder.Buffer(), builder.Length()) : SLANG_OK;
+ }
+
+ va_end(args);
+ return res;
+}
+
+SlangResult WriterHelper::put(const char* text)
+{
+ return m_writer->write(text, ::strlen(text));
+}
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!! BaseWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+ISlangUnknown* BaseWriter::getInterface(const Guid& guid)
+{
+ return (guid == IID_ISlangUnknown || guid == IID_ISlangWriter) ? static_cast<ISlangWriter*>(this) : nullptr;
+}
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!! CallbackWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+SlangResult CallbackWriter::write(const char* chars, size_t numChars)
+{
+ if (numChars > 0)
+ {
+ // Make sure zero terminated
+ StringBuilder builder;
+ builder.Append(chars, numChars);
+
+ m_callback(builder.Buffer(), (void*)m_data);
+ }
+
+ return SLANG_OK;
+}
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!! FileWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+FileWriter::~FileWriter()
+{
+ if ((m_flags & WriterFlag::IsUnowned) == 0)
+ {
+ fclose(m_file);
+ }
+}
+
+SlangResult FileWriter::writeVaList(const char* format, va_list args)
+{
+ // http://www.cplusplus.com/reference/cstdio/vfprintf/
+ ::vfprintf(m_file, format, args);
+
+ if (m_flags & WriterFlag::AutoFlush)
+ {
+ ::fflush(m_file);
+ }
+
+ return SLANG_OK;
+}
+
+SlangResult FileWriter::write(const char* text, size_t numChars)
+{
+ const size_t numWritten = ::fwrite(text, sizeof(char), numChars, m_file);
+ if (m_flags & WriterFlag::AutoFlush)
+ {
+ ::fflush(m_file);
+ }
+ return numChars == numWritten ? SLANG_OK : SLANG_FAIL;
+}
+
+void FileWriter::flush()
+{
+ ::fflush(m_file);
+}
+
+/* static */bool FileWriter::isConsole(FILE* file)
+{
+ const int stdoutFileDesc = _fileno(file);
+ return _isatty(stdoutFileDesc) != 0;
+}
+
+SlangResult FileWriter::setMode(SlangWriterMode mode)
+{
+ switch (mode)
+ {
+ case SLANG_WRITER_MODE_BINARY:
+ {
+#ifdef _WIN32
+ int stdoutFileDesc = _fileno(m_file);
+ _setmode(stdoutFileDesc, _O_BINARY);
+ return SLANG_OK;
+#else
+ break;
+#endif
+ }
+ default: break;
+ }
+ return SLANG_FAIL;
+}
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!! StringWriter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+SlangResult StringWriter::writeVaList(const char* format, va_list args)
+{
+ StringUtil::append(format, args, *m_builder);
+ return SLANG_OK;
+}
+
+SlangResult StringWriter::write(const char* chars, size_t numChars)
+{
+ if (numChars > 0)
+ {
+ m_builder->Append(chars, numChars);
+ }
+ return SLANG_OK;
+}
+
+}
+