tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
fix(cadre-celeste): keep raw strings, digit separators and header names intact in ABI fingerprints
- Commit
18825fb2f8ceac0d7dbf1a95eb1afa80e4a1132a- Author
- Leitwolf11 ·
- Parents
e425aeec1f51
Changes
.../AbiFingerprint.cs | 106 ++++++++++++++++++++- .../CadreCelestePersistenceAndAbiTests.cs | 23 +++++ 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs b/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs index 5bafcb2..a42cb78 100644 --- a/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs +++ b/src/Progmasoft.Tazel.CadreCeleste/AbiFingerprint.cs @@ -85,7 +85,19 @@ public static class AbiFingerprint pendingSpace = false; } - if (ch is '"' or '\'') + if (ch == '"' && IsRawStringStart(source, i)) + { + CopyRawStringLiteral(source, ref i, sb); + return i; + } + + if (ch == '<' && IsIncludeLine(sb)) + { + CopyHeaderName(source, ref i, sb); + return i; + } + + if (ch == '"' || (ch == '\'' && !IsDigitSeparator(source, i))) { CopyQuotedLiteral(source, ref i, ch, sb); return i; @@ -95,6 +107,98 @@ public static class AbiFingerprint return i + 1; } + /// <summary> + /// Whether the <c>"</c> at <paramref name="quote"/> opens a C++ raw string literal: it follows <c>R</c>, itself + /// preceded by an encoding prefix (<c>u8</c>, <c>u</c>, <c>U</c>, <c>L</c>) or by no identifier character. + /// </summary> + private static bool IsRawStringStart(string source, int quote) + { + if (quote == 0 || source[quote - 1] != 'R') + { + return false; + } + + var start = quote - 1; + while (start > 0 && IsIdentifierChar(source[start - 1])) + { + start--; + } + + return source.AsSpan(start, quote - start) is "R" or "u8R" or "uR" or "UR" or "LR"; + } + + /// <summary> + /// Whether the <c>'</c> at <paramref name="quote"/> is a C++14 digit separator: it lies inside a pp-number, a + /// token that starts with a digit, and is followed by a digit or letter. + /// </summary> + private static bool IsDigitSeparator(string source, int quote) + { + if (quote == 0 || quote + 1 >= source.Length || !char.IsAsciiLetterOrDigit(source[quote + 1])) + { + return false; + } + + var start = quote; + while (start > 0 && (IsIdentifierChar(source[start - 1]) || source[start - 1] is '\'' or '.')) + { + start--; + } + + return start < quote && char.IsAsciiDigit(source[start]); + } + + private static bool IsIdentifierChar(char c) => char.IsAsciiLetterOrDigit(c) || c == '_'; + + /// <summary>Whether the normalized line so far is <c>#include</c>, <c>#include_next</c> or <c>#import</c>.</summary> + private static bool IsIncludeLine(StringBuilder sb) + { + var lineStart = sb.Length; + while (lineStart > 0 && sb[lineStart - 1] != '\n') + { + lineStart--; + } + + var line = sb.ToString(lineStart, sb.Length - lineStart).Replace(" ", string.Empty, StringComparison.Ordinal); + return line is "#include" or "#include_next" or "#import"; + } + + /// <summary>Copies a <c><header-name></c> verbatim, so <c>//</c> or spaces inside it are kept.</summary> + private static void CopyHeaderName(string source, ref int i, StringBuilder sb) + { + while (i < source.Length && source[i] is not ('>' or '\n')) + { + sb.Append(source[i++]); + } + + if (i < source.Length && source[i] == '>') + { + sb.Append(source[i++]); + } + } + + /// <summary> + /// Copies a raw string literal <c>"delim( ... )delim"</c> verbatim: nothing inside it is a comment, whitespace in it + /// is significant, and a <c>"</c> inside it does not end it. + /// </summary> + private static void CopyRawStringLiteral(string source, ref int i, StringBuilder sb) + { + var open = source.IndexOf('(', i + 1); + if (open < 0 || open - (i + 1) > 16) + { + // Not a well-formed raw string (the delimiter is at most 16 characters): copy the rest unchanged, which can + // only make the fingerprint more sensitive, never less. + sb.Append(source, i, source.Length - i); + i = source.Length; + return; + } + + var terminator = ")" + source[(i + 1)..open] + "\""; + var close = source.IndexOf(terminator, open + 1, StringComparison.Ordinal); + var end = close < 0 ? source.Length : close + terminator.Length; + sb.Append(source, i, end - i); + i = end; + } + private static bool NeedsSpaceSeparator(StringBuilder sb) => sb.Length > 0 && sb[^1] != '\n'; private static void AppendNewlineIfNeeded(StringBuilder sb) diff --git a/tests/Progmasoft.Tazel.Tests/CadreCelestePersistenceAndAbiTests.cs b/tests/Progmasoft.Tazel.Tests/CadreCelestePersistenceAndAbiTests.cs index 1e5b78e..98ae820 100644 --- a/tests/Progmasoft.Tazel.Tests/CadreCelestePersistenceAndAbiTests.cs +++ b/tests/Progmasoft.Tazel.Tests/CadreCelestePersistenceAndAbiTests.cs @@ -122,6 +122,29 @@ public sealed class CadreCelestePersistenceAndAbiTests Assert.Equal(3, downstreamBuilds); } + [Theory] + [InlineData("auto s = R\"(say \"hi there\")\";", "auto s = R\"(say \"hi there\")\";")] + [InlineData("auto s = R\"(a // not a comment)\";", "auto s = R\"(a // still not a comment)\";")] + [InlineData("auto s = u8R\"x(a \"b\" )\" c)x\";", "auto s = u8R\"x(a \"b\" )\" c)x\";")] + [InlineData("int n = 1'000; auto s = \"a b\";", "int n = 1'000; auto s = \"a b\";")] + [InlineData("int n = 0x1'F; char c = 'x'; auto s = \"p q\";", "int n = 0x1'F; char c = 'x'; auto s = \"p q\";")] + [InlineData("#include <dir//a.h>", "#include <dir//b.h>")] + [InlineData("#import <dir/x y.h>", "#import <dir/x y.h>")] + public void AbiHeaderTokenHashSeesChangesInsideLiteralsAndHeaderNames(string before, string after) + { + Assert.NotEqual(AbiFingerprint.HashHeaderTokens(before), AbiFingerprint.HashHeaderTokens(after)); + } + + [Theory] + [InlineData("int n = 1'000; // note", "int n = 1'000; /* other note */")] + [InlineData("char c = 'a'; int x;", "char c = 'a'; int x; // trailing")] + [InlineData("#include <a/b.h> // why", "#include <a/b.h>")] + [InlineData("auto s = R\"(x)\"; // c", "auto s = R\"(x)\";")] + public void AbiHeaderTokenHashIgnoresCommentsAndSpacingAroundLiterals(string before, string after) + { + Assert.Equal(AbiFingerprint.HashHeaderTokens(before), AbiFingerprint.HashHeaderTokens(after)); + } + [Fact] public void ExportedSymbolFingerprintIsOrderIndependentAndSensitiveToSignatureChanges() {