diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-08-15 18:38:59 -0400 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-08-15 15:38:59 -0700 |
| commit | 77fe3dd74446fb07dd0de31114f7033f111ef868 (patch) | |
| tree | 835044f955d4f2afc02894448864321fca3344df | |
| parent | 44506607215247be4bf33c1a629bf39971bc966f (diff) | |
A more convoluted #pragma once file identity test, using relative paths. (#1021)
* A more convoluted #pragma once file identity test, using relative paths.
* Fix bug with passing - to slang as a command line option causes a crash.
Ability to set file-system to use on command line.
#pragma once tests try with 'normal' and 'read-file' only versions
* OSFileSystem -> OSFileSystemExt
LoadFileOSFileSystem -> OSFileSystem
Implemented OSFileSystem like OSFileSystemExt as as singleton.
Fixes to comments.
| -rw-r--r-- | source/slang/slang-diagnostic-defs.h | 2 | ||||
| -rw-r--r-- | source/slang/slang-file-system.cpp | 27 | ||||
| -rw-r--r-- | source/slang/slang-file-system.h | 36 | ||||
| -rw-r--r-- | source/slang/slang-options.cpp | 25 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 8 | ||||
| -rw-r--r-- | tests/preprocessor/file-identity/b.h | 11 | ||||
| -rw-r--r-- | tests/preprocessor/file-identity/c.h | 11 | ||||
| -rw-r--r-- | tests/preprocessor/file-identity/sub-folder/file-identity.slang | 13 | ||||
| -rw-r--r-- | tests/preprocessor/pragma-once.slang | 1 |
9 files changed, 116 insertions, 18 deletions
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index 802c89f8b..c1c207fd7 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -69,6 +69,8 @@ DIAGNOSTIC( 14, Error, unknownProfile, "unknown profile '$0'"); DIAGNOSTIC( 15, Error, unknownStage, "unknown stage '$0'"); DIAGNOSTIC( 16, Error, unknownPassThroughTarget, "unknown pass-through target '$0'"); DIAGNOSTIC( 17, Error, unknownCommandLineOption, "unknown command-line option '$0'"); +DIAGNOSTIC( 18, Error, unknownFileSystemOption, "unknown file-system option '$0'"); + DIAGNOSTIC( 20, Error, entryPointsNeedToBeAssociatedWithTranslationUnits, "when using multiple source files, entry points must be specified after their corresponding source file(s)"); DIAGNOSTIC( 21, Error, expectedArgumentForOption, "expected an argument for command-line option '$0'"); diff --git a/source/slang/slang-file-system.cpp b/source/slang/slang-file-system.cpp index f8c423e84..975d35f2c 100644 --- a/source/slang/slang-file-system.cpp +++ b/source/slang/slang-file-system.cpp @@ -36,9 +36,18 @@ static SlangResult _calcCombinedPath(SlangPathType fromPathType, const char* fro return SLANG_OK; } -/* !!!!!!!!!!!!!!!!!!!!!!!!!! IncludeFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! OSFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ -/* static */OSFileSystem OSFileSystem::s_singleton; +/* static */OSFileSystem OSFileSystem::s_singleton; + +ISlangUnknown* OSFileSystem::getInterface(const Guid& guid) +{ + return (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem ) ? static_cast<ISlangFileSystem*>(this) : nullptr; +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!! OSFileSystemExt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + +/* static */OSFileSystemExt OSFileSystemExt::s_singleton; template <typename T> static ISlangFileSystemExt* _getInterface(T* ptr, const Guid& guid) @@ -46,7 +55,7 @@ static ISlangFileSystemExt* _getInterface(T* ptr, const Guid& guid) return (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem || guid == IID_ISlangFileSystemExt) ? static_cast<ISlangFileSystemExt*>(ptr) : nullptr; } -ISlangUnknown* OSFileSystem::getInterface(const Guid& guid) +ISlangUnknown* OSFileSystemExt::getInterface(const Guid& guid) { return _getInterface(this, guid); } @@ -62,13 +71,13 @@ static String _fixPathDelimiters(const char* pathIn) #endif } -SlangResult OSFileSystem::getFileUniqueIdentity(const char* pathIn, ISlangBlob** outUniqueIdentity) +SlangResult OSFileSystemExt::getFileUniqueIdentity(const char* pathIn, ISlangBlob** outUniqueIdentity) { // By default we use the canonical path to uniquely identify a file return getCanonicalPath(pathIn, outUniqueIdentity); } -SlangResult OSFileSystem::getCanonicalPath(const char* path, ISlangBlob** outCanonicalPath) +SlangResult OSFileSystemExt::getCanonicalPath(const char* path, ISlangBlob** outCanonicalPath) { String canonicalPath; SLANG_RETURN_ON_FAIL(Path::getCanonical(_fixPathDelimiters(path), canonicalPath)); @@ -76,25 +85,25 @@ SlangResult OSFileSystem::getCanonicalPath(const char* path, ISlangBlob** outCan return SLANG_OK; } -SlangResult OSFileSystem::getSimplifiedPath(const char* pathIn, ISlangBlob** outSimplifiedPath) +SlangResult OSFileSystemExt::getSimplifiedPath(const char* pathIn, ISlangBlob** outSimplifiedPath) { String simplifiedPath = Path::simplify(_fixPathDelimiters(pathIn)); *outSimplifiedPath = StringUtil::createStringBlob(simplifiedPath).detach(); return SLANG_OK; } -SlangResult OSFileSystem::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) +SlangResult OSFileSystemExt::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) { // Don't need to fix delimiters - because combine path handles both path delimiter types return _calcCombinedPath(fromPathType, fromPath, path, pathOut); } -SlangResult SLANG_MCALL OSFileSystem::getPathType(const char* pathIn, SlangPathType* pathTypeOut) +SlangResult SLANG_MCALL OSFileSystemExt::getPathType(const char* pathIn, SlangPathType* pathTypeOut) { return Path::getPathType(_fixPathDelimiters(pathIn), pathTypeOut); } -SlangResult OSFileSystem::loadFile(char const* pathIn, ISlangBlob** outBlob) +SlangResult OSFileSystemExt::loadFile(char const* pathIn, ISlangBlob** outBlob) { // Default implementation that uses the `core` libraries facilities for talking to the OS filesystem. // diff --git a/source/slang/slang-file-system.h b/source/slang/slang-file-system.h index 1d747d73f..d91d8ff65 100644 --- a/source/slang/slang-file-system.h +++ b/source/slang/slang-file-system.h @@ -11,7 +11,8 @@ namespace Slang { -class OSFileSystem : public ISlangFileSystemExt +// Implementation of ISlangFileSystemExt for the OS +class OSFileSystemExt : public ISlangFileSystemExt { public: // ISlangUnknown @@ -55,11 +56,40 @@ public: private: /// Make so not constructible - OSFileSystem() {} - virtual ~OSFileSystem() {} + OSFileSystemExt() {} + virtual ~OSFileSystemExt() {} ISlangUnknown* getInterface(const Guid& guid); + static OSFileSystemExt s_singleton; +}; + +// Implementation of ISlangFileSystem for the OS (ie only has simplified interface of just loadFile) +class OSFileSystem : public ISlangFileSystem +{ +public: + // ISlangUnknown + SLANG_IUNKNOWN_QUERY_INTERFACE + SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; } + SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; } + + // ISlangFileSystem + virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile( + char const* path, + ISlangBlob** outBlob) SLANG_OVERRIDE + { + // Can just use OsFileSystemExt impl + return OSFileSystemExt::getSingleton()->loadFile(path, outBlob); + } + + static ISlangFileSystem* getSingleton() { return &s_singleton; } + +private: + /// Make so not constructible + OSFileSystem() {} + virtual ~OSFileSystem() {} + + ISlangUnknown* getInterface(const Guid& guid); static OSFileSystem s_singleton; }; diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 81fb468eb..5918f1551 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -8,6 +8,8 @@ #include "slang-compiler.h" #include "slang-profile.h" +#include "slang-file-system.h" + #include <assert.h> namespace Slang { @@ -632,7 +634,7 @@ struct OptionsParser SLANG_RETURN_ON_FAIL(tryReadCommandLineArgument(sink, arg, &argCursor, argEnd, name)); addSharedLibraryPath(SharedLibraryType::Fxc, name); } - else if (argStr[1] == 'D') + else if (argStr.getLength() >= 2 && argStr[1] == 'D') { // The value to be defined might be part of the same option, as in: // -DFOO @@ -677,7 +679,7 @@ struct OptionsParser ""); } } - else if (argStr[1] == 'I') + else if (argStr.getLength() >= 2 && argStr[1] == 'I') { // The value to be defined might be part of the same option, as in: // -IFOO @@ -803,6 +805,25 @@ struct OptionsParser { requestImpl->getBackEndReq()->useUnknownImageFormatAsDefault = true; } + else if (argStr == "-file-system") + { + String name; + SLANG_RETURN_ON_FAIL(tryReadCommandLineArgument(sink, arg, &argCursor, argEnd, name)); + + if (name == "default") + { + spSetFileSystem(compileRequest, nullptr); + } + else if (name == "load-file") + { + spSetFileSystem(compileRequest, OSFileSystem::getSingleton()); + } + else + { + sink->diagnose(SourceLoc(), Diagnostics::unknownFileSystemOption, name); + return SLANG_FAIL; + } + } else if (argStr == "--") { // The `--` option causes us to stop trying to parse options, diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index f0570132f..eef0dff6f 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -2365,17 +2365,17 @@ void Linkage::setFileSystem(ISlangFileSystem* inFileSystem) // Set the fileSystem fileSystem = inFileSystem; - // Set up fileSystemExt appropriately + // If nullptr passed in set up default if (inFileSystem == nullptr) { - fileSystemExt = new Slang::CacheFileSystem(Slang::OSFileSystem::getSingleton()); + fileSystemExt = new Slang::CacheFileSystem(Slang::OSFileSystemExt::getSingleton()); } else { - // See if we have the interface + // See if we have the full ISlangFileSystemExt interface, if we do just use it inFileSystem->queryInterface(IID_ISlangFileSystemExt, (void**)fileSystemExt.writeRef()); - // If not wrap with WrapFileSytem that keeps the old behavior + // If not wrap with CacheFileSystem that emulates ISlangFileSystemExt from the ISlangFileSystem interface if (!fileSystemExt) { // Construct a wrapper to emulate the extended interface behavior diff --git a/tests/preprocessor/file-identity/b.h b/tests/preprocessor/file-identity/b.h new file mode 100644 index 000000000..ba131c653 --- /dev/null +++ b/tests/preprocessor/file-identity/b.h @@ -0,0 +1,11 @@ +#pragma once + +#include "c.h" + +#ifdef B_H +# error "Shouldn't be included twice" +#endif + +#define B_H + +float foo(float x) { return x; }
\ No newline at end of file diff --git a/tests/preprocessor/file-identity/c.h b/tests/preprocessor/file-identity/c.h new file mode 100644 index 000000000..eb6cd1c42 --- /dev/null +++ b/tests/preprocessor/file-identity/c.h @@ -0,0 +1,11 @@ +#pragma once + +#include "b.h" + +#ifdef C_H +# error "c.h shouldn't be included twice" +#endif + +#define C_H + +float bar(float x) { return x; }
\ No newline at end of file diff --git a/tests/preprocessor/file-identity/sub-folder/file-identity.slang b/tests/preprocessor/file-identity/sub-folder/file-identity.slang new file mode 100644 index 000000000..590b32a70 --- /dev/null +++ b/tests/preprocessor/file-identity/sub-folder/file-identity.slang @@ -0,0 +1,13 @@ +//TEST(smoke):SIMPLE: +//TEST(smoke):SIMPLE: -file-system load-file + + +#include "../b.h" +#include "../c.h" + +#include "./../b.h" + +float test(float x) +{ + return foo(x) + bar(x); +}
\ No newline at end of file diff --git a/tests/preprocessor/pragma-once.slang b/tests/preprocessor/pragma-once.slang index 43ddee680..ef43ea265 100644 --- a/tests/preprocessor/pragma-once.slang +++ b/tests/preprocessor/pragma-once.slang @@ -1,4 +1,5 @@ //TEST(smoke):SIMPLE: +//TEST(smoke):SIMPLE: -file-system load-file // Test support for `#pragma once` |
