summaryrefslogtreecommitdiffstats
path: root/tests/preprocessor
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-26 10:11:00 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-26 12:06:54 -0700
commit6e99b81c98f8c76444563d959536073befc7d8ca (patch)
tree84bb72a43c0a9725e7ada37f7df369e80ee6a62f /tests/preprocessor
parent7d3bfe403362b294cc2a1f2607d51dfcd447aafd (diff)
Make `#import` work with preprocessor macros
With this change, there is now a meaningful semantic difference between `__import` and `#import`. An `__import` compiles the target file in a fresh environment, only providing it any macro definitions passed via command line or API. Any macros defined in the imported file are not made visible at the import site. One can think of an `__import` as a bit like `using namespace` in C++. A `#import` will tokenize the input in the same preprocessor environment as the importing file, and any macros defined along the way will be visible in the parent file. It is a *bit* like a `#include` with two big differences: - The imported code is always parsed as Slang, and as its own module with default flags, etc. (so semantic checks are on even if we are in "rewriter" mode). It is pulled into the outer namespace just as for `__import`. - A given file will only get `#import`ed once for a translation unit, so it behaves a bit like there is an implicit `#pragma once` in the target file
Diffstat (limited to 'tests/preprocessor')
-rw-r--r--tests/preprocessor/import.hlsl18
-rw-r--r--tests/preprocessor/import.slang.h12
2 files changed, 30 insertions, 0 deletions
diff --git a/tests/preprocessor/import.hlsl b/tests/preprocessor/import.hlsl
new file mode 100644
index 000000000..486023678
--- /dev/null
+++ b/tests/preprocessor/import.hlsl
@@ -0,0 +1,18 @@
+//TEST:SIMPLE:-profile vs_5_0
+
+// Confirm that `#import` interacts with preprocessor as expected
+
+// Here is a macro that flows from parent to child file
+#define FOO float
+
+// Here we import the child file
+#import "import.slang.h"
+
+// Here we use a macro that flows the other way (child->parent)
+BAR g( FOO x ) { return f(x); }
+
+// Here we confirm that importing the file again is a no-op
+#import "import.slang.h"
+
+void main()
+{}
diff --git a/tests/preprocessor/import.slang.h b/tests/preprocessor/import.slang.h
new file mode 100644
index 000000000..a97a199f0
--- /dev/null
+++ b/tests/preprocessor/import.slang.h
@@ -0,0 +1,12 @@
+// Confirm that `#import` interacts with preprocessor as expected
+
+// We add a guard to ensure that this file isn't imported more than once
+#ifdef BAR
+#error File imported more than one!
+#endif
+
+// Here we use a macro from the parent file
+FOO f( FOO y ) { return y; }
+
+// Here is a macro that flows from child to parent
+#define BAR float