summaryrefslogtreecommitdiffstats
path: root/source/slang/preprocessor.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2018-08-27 12:33:35 -0700
committerGitHub <noreply@github.com>2018-08-27 12:33:35 -0700
commit9a9733091cc7c9628e445313785d561deb229072 (patch)
treec15aca8a54dc36ab5c1671591b86ae7e1895b382 /source/slang/preprocessor.cpp
parent6a8ad6eb4cab72c18de48762768e04d08b60a21c (diff)
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.
Diffstat (limited to 'source/slang/preprocessor.cpp')
-rw-r--r--source/slang/preprocessor.cpp105
1 files changed, 102 insertions, 3 deletions
diff --git a/source/slang/preprocessor.cpp b/source/slang/preprocessor.cpp
index 3ec44fc28..71c6fd4bb 100644
--- a/source/slang/preprocessor.cpp
+++ b/source/slang/preprocessor.cpp
@@ -197,6 +197,11 @@ struct Preprocessor
// The translation unit that is being parsed
TranslationUnitRequest* translationUnit;
+ // Any paths that have issued `#pragma once` directives to
+ // stop them from being included again.
+ HashSet<String> pragmaOncePaths;
+
+
TranslationUnitRequest* getTranslationUnit()
{
return translationUnit;
@@ -1608,6 +1613,12 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context)
// a switch of input stream
expectEndOfDirective(context);
+ // Check whether we've previously included this file and seen a `#pragma once` directive
+ if(context->preprocessor->pragmaOncePaths.Contains(foundPath))
+ {
+ return;
+ }
+
// Push the new file onto our stack of input streams
// TODO(tfoley): check if we have made our include stack too deep
@@ -1818,12 +1829,100 @@ static void HandleLineDirective(PreprocessorDirectiveContext* context)
inputStream->primaryStream->lexer.startOverridingSourceLocations(newLoc);
}
+#define SLANG_PRAGMA_DIRECTIVE_CALLBACK(NAME) \
+ void NAME(PreprocessorDirectiveContext* context, Token subDirectiveToken)
+
+// Callback interface used by `#pragma` directives
+typedef SLANG_PRAGMA_DIRECTIVE_CALLBACK((*PragmaDirectiveCallback));
+
+SLANG_PRAGMA_DIRECTIVE_CALLBACK(handleUnknownPragmaDirective)
+{
+ GetSink(context)->diagnose(subDirectiveToken, Diagnostics::unknownPragmaDirectiveIgnored, subDirectiveToken.getName());
+ SkipToEndOfLine(context);
+ return;
+}
+
+SLANG_PRAGMA_DIRECTIVE_CALLBACK(handlePragmaOnceDirective)
+{
+ // We need to identify the path of the file we are preprocessing,
+ // so that we can avoid including it again.
+ //
+ // Note: for now we are doing a very simplistic check where
+ // we use the raw file path as the key for our duplicate checking.
+ //
+ // TODO: a more refined implementation should probably apply Unicode
+ // normalization and case-folding to the path, and then use that
+ // plus a hash of the file contents to determine whether things
+ // represent the "same" file.
+ //
+ // TODO: even for our simplistic implementation, we need to add
+ // logic to deal with `../` segments in path names to detect
+ // trivial cases of the "same" path.
+ //
+ auto directiveLoc = GetDirectiveLoc(context);
+ auto expandedDirectiveLoc = context->preprocessor->translationUnit->compileRequest->getSourceManager()->expandSourceLoc(directiveLoc);
+ String pathIssuedFrom = expandedDirectiveLoc.getSpellingPath();
+
+ context->preprocessor->pragmaOncePaths.Add(pathIssuedFrom);
+}
+
+// Information about a specific `#pragma` directive
+struct PragmaDirective
+{
+ // name of the directive
+ char const* name;
+
+ // Callback to handle the directive
+ PragmaDirectiveCallback callback;
+};
+
+// A simple array of all the `#pragma` directives we know how to handle.
+static const PragmaDirective kPragmaDirectives[] =
+{
+ { "once", &handlePragmaOnceDirective },
+
+ { NULL, NULL },
+};
+
+static const PragmaDirective kUnknownPragmaDirective = {
+ NULL, &handleUnknownPragmaDirective,
+};
+
+// Look up the `#pragma` directive with the given name.
+static PragmaDirective const* findPragmaDirective(String const& name)
+{
+ char const* nameStr = name.Buffer();
+ for (int ii = 0; kPragmaDirectives[ii].name; ++ii)
+ {
+ if (strcmp(kPragmaDirectives[ii].name, nameStr) != 0)
+ continue;
+
+ return &kPragmaDirectives[ii];
+ }
+
+ return &kUnknownPragmaDirective;
+}
+
// Handle a `#pragma` directive
static void HandlePragmaDirective(PreprocessorDirectiveContext* context)
{
- // TODO(tfoley): figure out which pragmas to parse,
- // and which to pass along
- SkipToEndOfLine(context);
+ // Try to read the sub-directive name.
+ Token subDirectiveToken = PeekRawToken(context);
+
+ // The sub-directive had better be an identifier
+ if (subDirectiveToken.type != TokenType::Identifier)
+ {
+ GetSink(context)->diagnose(GetDirectiveLoc(context), Diagnostics::expectedPragmaDirectiveName);
+ SkipToEndOfLine(context);
+ return;
+ }
+ AdvanceRawToken(context);
+
+ // Look up the handler for the sub-directive.
+ PragmaDirective const* subDirective = findPragmaDirective(subDirectiveToken.getName()->text);
+
+ // Apply the sub-directive-specific callback
+ (subDirective->callback)(context, subDirectiveToken);
}
// Handle a `#version` directive