From 5fde038b1a6b3c8b335cd5b380c3ee8d15403052 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 31 Mar 2021 13:11:49 -0400 Subject: Support for __LINE__ and __FILE__ in preprocessor (#1772) * #include an absolute path didn't work - because paths were taken to always be relative. * First pass support for __LINE__ and __FILE__. * Test include handling with __FILE__ Fix diagnostic compare when input is empty. * Fix some issues in preprocessor handling of special macros like __LINE__ Add a more complex test. * Use CONCAT2 in tests, because preprocessor doesn't quite get parameter expansion correct. * Make __FILE__ and __LINE__ behave more like Clang/Gcc. * A test for preprocessor bug. * Fix __LINE__ and __FILE__ in macro expansion, should be initiating location. * Fix some comments. * Small tidy up around builtin macros. * Small improvements for macro type names. Escape found paths. --- source/core/slang-char-util.cpp | 5 +++ source/core/slang-char-util.h | 4 +++ source/core/slang-string-util.cpp | 72 +++++++++++++++++++++++++++++++++++++++ source/core/slang-string-util.h | 3 ++ 4 files changed, 84 insertions(+) (limited to 'source/core') diff --git a/source/core/slang-char-util.cpp b/source/core/slang-char-util.cpp index 53dd98541..298f9b75f 100644 --- a/source/core/slang-char-util.cpp +++ b/source/core/slang-char-util.cpp @@ -40,6 +40,11 @@ static const CharUtil::CharFlagMap _calcCharFlagsMap() map.flags[size_t('\t')] |= Flag::HorizontalWhitespace; } + { + map.flags[size_t('\n')] |= Flag::VerticalWhitespace; + map.flags[size_t('\r')] |= Flag::VerticalWhitespace; + } + return map; } diff --git a/source/core/slang-char-util.h b/source/core/slang-char-util.h index 810cde0b1..2434697d4 100644 --- a/source/core/slang-char-util.h +++ b/source/core/slang-char-util.h @@ -17,6 +17,7 @@ struct CharUtil Digit = 0x04, ///< 0-9 HorizontalWhitespace = 0x08, ///< Whitespace that can appear horizontally (ie excluding CR/LF) HexDigit = 0x10, ///< 0-9, a-f, A-F + VerticalWhitespace = 0x20, ///< \n \r }; }; @@ -24,6 +25,8 @@ struct CharUtil SLANG_FORCE_INLINE static bool isLower(char c) { return c >= 'a' && c <= 'z'; } SLANG_FORCE_INLINE static bool isUpper(char c) { return c >= 'A' && c <= 'Z'; } SLANG_FORCE_INLINE static bool isHorizontalWhitespace(char c) { return c == ' ' || c == '\t'; } + SLANG_FORCE_INLINE static bool isVerticalWhitespace(char c) { return c == '\n' || c == '\r'; } + SLANG_FORCE_INLINE static bool isWhitespace(char c) { return (getFlags(c) & (Flag::HorizontalWhitespace | Flag::VerticalWhitespace)) != 0; } /// True if it's alpha SLANG_FORCE_INLINE static bool isAlpha(char c) { return (getFlags(c) & (Flag::Upper | Flag::Lower)) != 0; } @@ -38,6 +41,7 @@ struct CharUtil /// Given a character return the upper case equivalent SLANG_FORCE_INLINE static char toUpper(char c) { return (c >= 'a' && c <= 'z') ? (c -'a' + 'A') : c; } + struct CharFlagMap { Flags flags[0x100]; diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp index 6ce75f3f0..b4c767c87 100644 --- a/source/core/slang-string-util.cpp +++ b/source/core/slang-string-util.cpp @@ -445,4 +445,76 @@ SLANG_FORCE_INLINE static bool _isDigit(char c) return SLANG_OK; } +static char _getHexChar(int v) +{ + return (v <= 9) ? char(v + '0') : char(v - 10 + 'A'); +} + +static char _getEscapedChar(char c) +{ + switch (c) + { + case '\b': return 'b'; + case '\f': return 'f'; + case '\n': return 'n'; + case '\r': return 'r'; + case '\a': return 'a'; + case '\t': return 't'; + case '\v': return 'v'; + case '\'': return '\''; + case '\"': return '"'; + case '\\': return '\\'; + default: return 0; + } +} + +/* static */void StringUtil::appendEscaped(const UnownedStringSlice& slice, StringBuilder& out) +{ + const char* start = slice.begin(); + const char* cur = start; + const char*const end = slice.end(); + + for (; cur < end; ++cur) + { + const char c = *cur; + const char escapedChar = _getEscapedChar(c); + + if (escapedChar) + { + // Flush + if (start < cur) + { + out.append(start, end); + } + out.appendChar('\\'); + out.appendChar(escapedChar); + + start = cur + 1; + } + else if ( c < ' ' || c > 126) + { + // Flush + if (start < cur) + { + out.append(start, end); + } + + char buf[5] = "\\0x0"; + + buf[3] = _getHexChar((int(c) >> 4) & 0xf); + buf[4] = _getHexChar(c & 0xf); + + out.append(buf, buf + 4); + + start = cur + 1; + } + } + + if (start < end) + { + out.append(start, end); + } +} + + } // namespace Slang diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h index 4672fa1d0..2850c3b4f 100644 --- a/source/core/slang-string-util.h +++ b/source/core/slang-string-util.h @@ -97,6 +97,9 @@ struct StringUtil /// Convert in to int. Returns SLANG_FAIL on error static SlangResult parseInt(const UnownedStringSlice& in, Int& outValue); + /// Takes slice and adds C++/C type escaping for special characters (like '\', '"' and if not ascii will write out as hex sequence) + /// Does not append double quotes around the output + static void appendEscaped(const UnownedStringSlice& slice, StringBuilder& out); }; /* A helper class that allows parsing of lines from text with iteration. Uses StringUtil::extractLine for the actual underlying implementation. */ -- cgit v1.2.3