summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-parameter-binding.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2019-07-29 13:21:32 -0700
committerGitHub <noreply@github.com>2019-07-29 13:21:32 -0700
commit9f33d3d4eb81afde1b4445a4251979eeb5436e7f (patch)
tree6eb52affab80294535a9f9837bbfb8d9ad4db4b5 /source/slang/slang-parameter-binding.cpp
parentade2c39fa3675504ed135ef8abe7b53cfd06ee84 (diff)
Add an attribute to disable the overlapping-bindings warning (#1005)
Currently if the user gives two global shader parameters conflicting bindings, they get a warning diagnostic: ```hlsl Texture2D a : register(t0); Texture2D b : register(t0); // WARNING: overlapping bindings ``` This change adds a way to locally disable that warning using an attribute: ```hlsl [allow("overlapping-bindings")] Texture2D a : register(t0); [allow("overlapping-bindings")] Texture2D b : register(t0); // OK ``` Note that as a policy decision, the implementation requires `[allow("overlapping-bindings")]` on both declarations in order to disable the warning, under the assumption that the behavior should be strictly opt-in, and not silently affect a programmer who adds a new shader parameter with no knowledge or expectation of possible overlap. The `[allow(...)]` attribute is intended to be a fairly generally mechanism for disabling optional diagnostics within certain scopes (e.g., for the body of a function definition), but as implemented in this change it is quite restrictive: * Only the single name `"overlapping-bindings"` will be recognized, and this name cannot be used with, e.g., a `-W` flag on the command line to enable/disable the same diagnostic, or turn it into an error. Adding more cases would be easy enough, but wiring it up to command-line flags could be trickier. * Only the code that checks for parameter binding overlap is currently checking for `[allow(...)]` attributes, so it is not "wired up" to enable/disable any others. Doing this systematically would ideally involve something in `diagnose()`, but there could be complications to a systematic approach (finding the AST node(s) to use when searching for `[allow(...)]`. On gotcha here is that versions of Slang without this feature will error out on the `[allow(...)]` attribute since they don't understand it, and if we add future diagnostics that it covers then old compiler versions will (as written) error out on a diagnostic they haven't heard of rather than just assume the `[allow(...)]` attribute doesn't apply to them. These kinds of issues can and should be addressed in future changes.
Diffstat (limited to 'source/slang/slang-parameter-binding.cpp')
-rw-r--r--source/slang/slang-parameter-binding.cpp42
1 files changed, 38 insertions, 4 deletions
diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp
index d5efc3515..dc99f55e2 100644
--- a/source/slang/slang-parameter-binding.cpp
+++ b/source/slang/slang-parameter-binding.cpp
@@ -784,6 +784,25 @@ static UInt allocateUnusedSpaces(
return context->shared->usedSpaces.Allocate(nullptr, count);
}
+static bool shouldDisableDiagnostic(
+ Decl* decl,
+ DiagnosticInfo const& diagnosticInfo)
+{
+ for( auto dd = decl; dd; dd = dd->ParentDecl )
+ {
+ for( auto modifier : dd->modifiers )
+ {
+ auto allowAttr = as<AllowAttribute>(modifier);
+ if(!allowAttr)
+ continue;
+
+ if(allowAttr->diagnostic == &diagnosticInfo)
+ return true;
+ }
+ }
+ return false;
+}
+
static void addExplicitParameterBinding(
ParameterBindingContext* context,
RefPtr<ParameterInfo> parameterInfo,
@@ -842,11 +861,26 @@ static void addExplicitParameterBinding(
auto paramA = parameterInfo->varLayouts[0]->varDecl.getDecl();
auto paramB = overlappedVarLayout->varDecl.getDecl();
- getSink(context)->diagnose(paramA, Diagnostics::parameterBindingsOverlap,
- getReflectionName(paramA),
- getReflectionName(paramB));
+ auto& diagnosticInfo = Diagnostics::parameterBindingsOverlap;
+
+ // If *both* of the shader parameters declarations agree
+ // that overlapping bindings should be allowed, then we
+ // will not emit a diagnostic. Otherwise, we will warn
+ // the user because such overlapping bindings are likely
+ // to indicate a programming error.
+ //
+ if(shouldDisableDiagnostic(paramA, diagnosticInfo)
+ && shouldDisableDiagnostic(paramB, diagnosticInfo))
+ {
+ }
+ else
+ {
+ getSink(context)->diagnose(paramA, diagnosticInfo,
+ getReflectionName(paramA),
+ getReflectionName(paramB));
- getSink(context)->diagnose(paramB, Diagnostics::seeDeclarationOf, getReflectionName(paramB));
+ getSink(context)->diagnose(paramB, Diagnostics::seeDeclarationOf, getReflectionName(paramB));
+ }
}
}
}