From 5f632cd204b7a85f3a97b6c316c5a34f9fc8193e Mon Sep 17 00:00:00 2001 From: Ronan Date: Fri, 25 Apr 2025 00:48:37 +0200 Subject: Implemented #pragma warning (#6748) * Implemented #pragma warning Based on https://learn.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-170 * Make #pragma warning work with #includes. - SourceLoc are not sorted by inclusion order. - Construct a mapping from SourceLoc to "absolute locations" that are sorted by inclusion order (roughly represents a location in a raw file with all #include resolved). - The absolute location can be used in the pragma warning timeline * Added preprocessor #pragma warning tests. - Fixed #pragma warning (push / pop) SourceLoc - Fixed unused directiveLoc in #pragma warning parsing * #pragma warning: Added some comments and fixed some typos * Cleaned #pragma warning preprocessor implementation. --------- Co-authored-by: Yong He --- source/compiler-core/slang-lexer.cpp | 37 ++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'source/compiler-core/slang-lexer.cpp') diff --git a/source/compiler-core/slang-lexer.cpp b/source/compiler-core/slang-lexer.cpp index 048c266ca..c2ee5cd69 100644 --- a/source/compiler-core/slang-lexer.cpp +++ b/source/compiler-core/slang-lexer.cpp @@ -374,9 +374,14 @@ static void _lexIdentifier(Lexer* lexer) } } -static SourceLoc _getSourceLoc(Lexer* lexer) +static SourceLoc _getSourceLoc(const Lexer& lexer, const char* it) { - return lexer->m_startLoc + (lexer->m_cursor - lexer->m_begin); + return lexer.m_startLoc + (it - lexer.m_begin); +} + +static SourceLoc _getSourceLoc(const Lexer* lexer) +{ + return _getSourceLoc(*lexer, lexer->m_cursor); } static void _lexDigits(Lexer* lexer, int base) @@ -1857,4 +1862,32 @@ TokenList Lexer::lexAllTokens() return UnownedStringSlice(in.begin() + offset, in.begin() + offset + tok.charsCount); } +SourceLoc Lexer::findNextLineEnd(SourceLoc from, UInt& lineCount) const +{ + const char* it = m_begin + (from.getRaw() - m_startLoc.getRaw()); + if (it >= m_begin && it < m_end) + { + while (it != m_end) + { + const char c = *it; + if (c == '\n' || c == '\r') + { + const char next = ((it + 1) == m_end) ? char(kEOF) : *(it + 1); + if ((next ^ c) == ('\n' ^ '\r')) + { + ++it; + } + --lineCount; + if (lineCount == 0) + { + SourceLoc res = _getSourceLoc(*this, it); + return res; + } + } + ++it; + } + } + return {}; +} + } // namespace Slang -- cgit v1.2.3