From 8f0895e0f8257da2fd10b6325931627a9a1792ba Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 11 Nov 2020 09:56:50 -0500 Subject: Include hierarchy output (#1595) * #include an absolute path didn't work - because paths were taken to always be relative. * Improve diagnostic for token pasting. * Token paste location test. * Output include hierarchy. * WIP on includes hierarchy. * Improved include hierarchy output - to handle source files without tokens. Improved test case. * Small comment improvements. Fixed a typo with not returning a reference. * Slight simplification of the ViewInitiatingHierarchy, by adding GetOrAddValue to Dictionary. * Remove the need for ViewInitiatingHierarchy type. * Improve output of path in diagnostic for includes hierarchy. * Remove comment in diagnostic for token-paste-location.slang * Update command line docs to include `-output-includes` Co-authored-by: Yong He --- source/slang/slang-preprocessor.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'source/slang/slang-preprocessor.cpp') diff --git a/source/slang/slang-preprocessor.cpp b/source/slang/slang-preprocessor.cpp index b5861d986..41b2c5bdf 100644 --- a/source/slang/slang-preprocessor.cpp +++ b/source/slang/slang-preprocessor.cpp @@ -1013,10 +1013,12 @@ top: StringBuilder sb; sb << token.getContent(); + Token poundPoundToken; + while (PeekRawTokenType(preprocessor) == TokenType::PoundPound) { // Consume the `##` - AdvanceRawToken(preprocessor); + poundPoundToken = AdvanceRawToken(preprocessor); // Possibly macro-expand the next token MaybeBeginMacroExpansion(preprocessor); @@ -1035,7 +1037,7 @@ top: PathInfo pathInfo = PathInfo::makeTokenPaste(); SourceFile* sourceFile = sourceManager->createSourceFileWithString(pathInfo, sb.ProduceString()); - SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr); + SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, poundPoundToken.getLoc()); Lexer lexer; lexer.initialize(sourceView, GetSink(preprocessor), preprocessor->getNamePool(), sourceManager->getMemoryArena()); @@ -1880,8 +1882,8 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context) sourceManager->addSourceFile(filePathInfo.uniqueIdentity, sourceFile); } - // This is a new parse (even if it's a pre-existing source file), so create a new SourceUnit - SourceView* sourceView = sourceManager->createSourceView(sourceFile, &filePathInfo); + // This is a new parse (even if it's a pre-existing source file), so create a new SourceView + SourceView* sourceView = sourceManager->createSourceView(sourceFile, &filePathInfo, directiveLoc); PreprocessorInputStream* inputStream = CreateInputStreamForSource(context->preprocessor, sourceView); inputStream->parent = context->preprocessor->inputStream; @@ -2411,8 +2413,10 @@ static void DefineMacro( SourceFile* keyFile = sourceManager->createSourceFileWithString(pathInfo, key); SourceFile* valueFile = sourceManager->createSourceFileWithString(pathInfo, value); - SourceView* keyView = sourceManager->createSourceView(keyFile, nullptr); - SourceView* valueView = sourceManager->createSourceView(valueFile, nullptr); + // Note that we don't need to pass a special source loc to identify that these are defined on the command line + // because the PathInfo on the SourceFile, is marked 'command line'. + SourceView* keyView = sourceManager->createSourceView(keyFile, nullptr, SourceLoc::fromRaw(0)); + SourceView* valueView = sourceManager->createSourceView(valueFile, nullptr, SourceLoc::fromRaw(0)); // Use existing `Lexer` to generate a token stream. Lexer lexer; @@ -2546,7 +2550,9 @@ TokenList preprocessSource( } } - SourceView* sourceView = sourceManager->createSourceView(file, nullptr); + // This is the originating source we are compiling - there is no 'initiating' source loc, + // so pass SourceLoc(0) - meaning it has no initiating location. + SourceView* sourceView = sourceManager->createSourceView(file, nullptr, SourceLoc::fromRaw(0)); // create an initial input stream based on the provided buffer preprocessor.inputStream = CreateInputStreamForSource(&preprocessor, sourceView); -- cgit v1.2.3