From bd815f02d846a50e16dab67e6512db2a6215c41f Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 21 Jan 2019 16:41:54 -0500 Subject: Feature/file unique identity (#789) * * Fix memory bug around expanding va_args - needed buffer to have space for terminating 0 * Fix problem with FileWriter defaults being globals, as memory they allocate, will only be freed after return from main - work around by making StdWriters RefObject derived, and kept in scope such the writers are destroyed before checks for leaks is found * Added SimplifyPathAndHash mode for CacheFileSystem - will simplify the path and see if simplified path is in cache before reading file (limiting amout of underlying file requests) * * Added calcReplaceChar * Renamed DefaultFileSystem to OSFileSystem * Made OSFileSystem convert windows \ to / on linux * Simplified logic for caching in CacheFileSystem. * Added pragma-once-c to add extra test, but also so there is an 'include' directory in preprocessor tests. * Small fixes in pragma once test. * Simplified cache handling path, so that paths/simplified paths area always added. * Improve naming of methods for different caches. * Removed references to 'canonicalPath' and made 'uniqueIdentity' * * Re-add support for canonicalPath to ISlangFileSystem -> not for uniqueIdentifier but as a way to display 'canonicalPath' * Added peliminary support for being able to display verbose paths in a diagnostic * Added 'clearCache' support * Added verbose path support to SourceManager (now needs a ISlangFileSystemExt to do this) * Added support for '-verbose-path' option to slangc and slang-test. --- source/slang/preprocessor.cpp | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'source/slang/preprocessor.cpp') diff --git a/source/slang/preprocessor.cpp b/source/slang/preprocessor.cpp index c038737b7..4ccb5d50e 100644 --- a/source/slang/preprocessor.cpp +++ b/source/slang/preprocessor.cpp @@ -197,10 +197,9 @@ struct Preprocessor // The translation unit that is being parsed TranslationUnitRequest* translationUnit; - // Any paths that have issued `#pragma once` directives to + // The unique identities of any paths that have issued `#pragma once` directives to // stop them from being included again. - HashSet pragmaOncePaths; - + HashSet pragmaOnceUniqueIdentities; TranslationUnitRequest* getTranslationUnit() { @@ -1610,10 +1609,10 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context) return; } - // We must have a canonical path to be compare - if (!filePathInfo.hasCanonicalPath()) + // We must have a uniqueIdentity to be compare + if (!filePathInfo.hasUniqueIdentity()) { - GetSink(context)->diagnose(pathToken.loc, Diagnostics::noCanonicalPath, path); + GetSink(context)->diagnose(pathToken.loc, Diagnostics::noUniqueIdentity, path); return; } @@ -1623,7 +1622,7 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context) expectEndOfDirective(context); // Check whether we've previously included this file and seen a `#pragma once` directive - if(context->preprocessor->pragmaOncePaths.Contains(filePathInfo.canonicalPath)) + if(context->preprocessor->pragmaOnceUniqueIdentities.Contains(filePathInfo.uniqueIdentity)) { return; } @@ -1633,7 +1632,7 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context) auto sourceManager = context->preprocessor->getCompileRequest()->getSourceManager(); // See if this an already loaded source file - SourceFile* sourceFile = sourceManager->findSourceFileRecursively(filePathInfo.canonicalPath); + SourceFile* sourceFile = sourceManager->findSourceFileRecursively(filePathInfo.uniqueIdentity); // If not create a new one, and add to the list of known source files if (!sourceFile) { @@ -1645,7 +1644,7 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context) } sourceFile = sourceManager->createSourceFileWithBlob(filePathInfo, foundSourceBlob); - sourceManager->addSourceFile(filePathInfo.canonicalPath, sourceFile); + sourceManager->addSourceFile(filePathInfo.uniqueIdentity, sourceFile); } // This is a new parse (even if it's a pre-existing source file), so create a new SourceUnit @@ -1873,29 +1872,19 @@ 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. - // + // We are using the 'uniqueIdentity' as determined by the ISlangFileSystemEx interface to determine file identities. + auto directiveLoc = GetDirectiveLoc(context); auto issuedFromPathInfo = context->preprocessor->translationUnit->compileRequest->getSourceManager()->getPathInfo(directiveLoc, SourceLocType::Actual); - // Must have a canonical path for a #pragma once to work - if (!issuedFromPathInfo.hasCanonicalPath()) + // Must have uniqueIdentity for a #pragma once to work + if (!issuedFromPathInfo.hasUniqueIdentity()) { GetSink(context)->diagnose(subDirectiveToken, Diagnostics::pragmaOnceIgnored); return; } - context->preprocessor->pragmaOncePaths.Add(issuedFromPathInfo.canonicalPath); + context->preprocessor->pragmaOnceUniqueIdentities.Add(issuedFromPathInfo.uniqueIdentity); } // Information about a specific `#pragma` directive -- cgit v1.2.3