diff options
| -rw-r--r-- | source/slang/slang.cpp | 35 | ||||
| -rw-r--r-- | source/slang/source-loc.cpp | 10 |
2 files changed, 37 insertions, 8 deletions
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 7ec448ab1..c04aaa044 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -372,16 +372,47 @@ SlangResult CompileRequest::loadFile(String const& path, ISlangBlob** outBlob) RefPtr<Expr> CompileRequest::parseTypeString(TranslationUnitRequest * translationUnit, String typeStr, RefPtr<Scope> scope) { - Slang::RefPtr<Slang::SourceFile> srcFile = sourceManager->createSourceFile(String("type string"), typeStr); + // Create a SourceManager on the stack, so any allocations for 'SourceFile'/'SourceView' etc will be cleaned up + SourceManager localSourceManager; + localSourceManager.initialize(sourceManager); + + Slang::RefPtr<Slang::SourceFile> srcFile(localSourceManager.createSourceFile(String("type string"), typeStr)); + // We'll use a temporary diagnostic sink DiagnosticSink sink; - sink.sourceManager = sourceManager; + sink.sourceManager = &localSourceManager; + + // RAII type to make make sure current SourceManager is restored after parse. + // Use RAII - to make sure everything is reset even if an exception is thrown. + struct ScopeReplaceSourceManager + { + ScopeReplaceSourceManager(CompileRequest* request, SourceManager* replaceManager): + m_request(request), + m_originalSourceManager(request->getSourceManager()) + { + request->setSourceManager(replaceManager); + } + + ~ScopeReplaceSourceManager() + { + m_request->setSourceManager(m_originalSourceManager); + } + + private: + CompileRequest* m_request; + SourceManager* m_originalSourceManager; + }; + + // We need to temporarily replace the SourceManager for this CompileRequest + ScopeReplaceSourceManager scopeReplaceSourceManager(this, &localSourceManager); + auto tokens = preprocessSource( srcFile, &sink, nullptr, Dictionary<String,String>(), translationUnit); + return parseTypeFromSourceFile(translationUnit, tokens, &sink, scope); } diff --git a/source/slang/source-loc.cpp b/source/slang/source-loc.cpp index 905cb46c6..d6b75b909 100644 --- a/source/slang/source-loc.cpp +++ b/source/slang/source-loc.cpp @@ -27,11 +27,9 @@ int SourceView::findEntryIndex(SourceLoc sourceLoc) const int lo = 0; while (lo + 1 < hi) { - int mid = (hi + lo) >> 1; - + const int mid = (hi + lo) >> 1; const Entry& midEntry = m_entries[mid]; SourceLoc::RawValue midValue = midEntry.m_startLoc.getRaw(); - if (midValue <= rawValue) { // The location we seek is at or after this entry @@ -67,8 +65,9 @@ void SourceView::addLineDirective(SourceLoc directiveLoc, StringSlicePool::Handl // We also need to make sure that any lookups for line numbers will // get corrected based on this files location. - // We assume the line number coming in is a line number, NOT an index so the correction needs + 1 - // There is an additional + 1 because we want the NEXT line to be 99 (ie +2 is correct 'fix') + // We assume the line number coming from the directive is a line number, NOT an index, so the correction needs + 1 + // There is an additional + 1 because we want the NEXT line - ie the line after the #line directive, to the specified value + // Taking both into account means +2 is correct 'fix' entry.m_lineAdjust = line - (lineIndex + 2); m_entries.Add(entry); @@ -452,5 +451,4 @@ String SourceManager::getPath(SourceLoc loc, SourceLocType type) } } - } // namespace Slang |
