From 9a9733091cc7c9628e445313785d561deb229072 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 27 Aug 2018 12:33:35 -0700 Subject: Add basic support for #pragma once (#630) * Improve diagnostic messages for function redefinition The front-end was using internal "not implemented" errors instead of friendly user-facing errors to handle: * Redefinition of a function (same signature and both have bodies) * Multiple function declarations/definitions with the same parameter signature, but differnet return types This change simply turns both of these into reasonably friendly errors that explain what went wrong and point to the previous definition/declaration as appropriate. * Add support for detecting #pragma directives and handling them The logic here mirrors what was set up for preprocessor directives, just for "sub-directives" in this case. The only case here is the default one, which now reports a warning for directives we don't understand. * Add basic support for #pragma once Fixes #494 The approach here is simplistic in the extreme. When we see a `#pragma once` directive, we put the current file path (the location of the `#pragma` directive, as reported by our source manager) into a set, and then any paths in that set are ignored by subsequent `#include` directives. This should work for simple cases of `#pragma once`, but it is likely to fail in a variety of cases because our filesystem layer currently makes no attempt to normalize/canonicalize paths. Improving the robustness of the solution is left to future work. This change includes a simple test case to confirm that a second `#include` of a file with a `#pragma once` is successfully ignored. --- source/slang/check.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 95a958495..42bcfe4b5 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -1876,7 +1876,7 @@ namespace Slang } return true; } - + bool validateAttribute(RefPtr attr) { if(auto numThreadsAttr = attr.As()) @@ -1939,7 +1939,7 @@ namespace Slang entryPointAttr->stage = stage; } - else if ((attr.As()) || + else if ((attr.As()) || (attr.As()) || (attr.As()) || (attr.As()) || @@ -1964,7 +1964,7 @@ namespace Slang // Has no args SLANG_ASSERT(attr->args.Count() == 0); } - else + else { if(attr->args.Count() == 0) { @@ -3639,8 +3639,9 @@ namespace Slang auto prevResultType = GetResultType(prevFuncDeclRef); if (!resultType->Equals(prevResultType)) { - // Bad dedeclaration - getSink()->diagnose(funcDecl, Diagnostics::unimplemented, "redeclaration has a different return type"); + // Bad redeclaration + getSink()->diagnose(funcDecl, Diagnostics::functionRedeclarationWithDifferentReturnType, funcDecl->getName(), resultType, prevResultType); + getSink()->diagnose(prevFuncDecl, Diagnostics::seePreviousDeclarationOf, funcDecl->getName()); // Don't bother emitting other errors at this point break; @@ -3671,7 +3672,8 @@ namespace Slang if (funcDecl->Body && prevFuncDecl->Body) { // Redefinition - getSink()->diagnose(funcDecl, Diagnostics::unimplemented, "function redefinition"); + getSink()->diagnose(funcDecl, Diagnostics::functionRedefinition, funcDecl->getName()); + getSink()->diagnose(prevFuncDecl, Diagnostics::seePreviousDefinitionOf, funcDecl->getName()); // Don't bother emitting other errors break; @@ -8379,7 +8381,7 @@ namespace Slang // TODO: We currently do minimal checking here, but this is the // right place to perform the following validation checks: // - + // * Are the function input/output parameters and result type // all valid for the chosen stage? (e.g., there shouldn't be // an `OutputStream` type in a vertex shader signature) -- cgit v1.2.3