Repository navigation
fix: escape trigraphs in C and C++ string literals [patch] - #164
Merged
Merged
Conversation
C up to C17, and C++ before C++17, replace trigraphs such as ??! and ??/ inside string literals, so "what??!" compiled with -std=c11 printed what| and ??/ became a backslash escaping the next character. CFamilyGenerator now overrides EscapeString to write the second ? of every ?? pair as \?, which is a question mark in every C and C++ standard and breaks every trigraph without touching a lone ?. EscapeString becomes virtual for this; the other languages keep the shared escaping, since \? is not an escape in Go, Rust or JavaScript. Fixes #107 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NfxXtpcxX8hkLYrePfHkV4
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #107
What was wrong
C up to C17, and C++ before C++17, replace trigraphs inside string literals.
"what??!"compiled withgcc -std=c11printedwhat|.??/became a backslash that escaped the next character, which could swallow the closing quote.Change
CFamilyGeneratornow overridesEscapeString. After the shared escaping, it writes the second?of every??pair as\?.\?is a question mark in every C and C++ standard, so the string's value doesn't change.???!becomes?\?\?!.?(for example"ok?") is written as it was.LanguageGeneratorBase.EscapeStringis nowprotected virtualso the C family can override it.\?is not an escape in Go, Rust or JavaScript.EscapeString, so they are covered too.Tests
New file:
Coder.Test/Languages/StringLiteralTrigraphTests.cs.CFamilyBreaksEveryTrigraph(C and C++): pins the exact literal written for every trigraph, a???!run and a lone?, and asserts that no??is left.OtherLanguagesWriteQuestionMarksAsIs(C#, JavaScript, Python, Go and Rust): question marks are written unchanged.CompiledWithTrigraphsOn_TheStringsKeepTheirValue:"what??!"and"a??/b".-std=c11 -Wall -Wextra -pedantic -Werror=trigraphsand runs it.ToolchainHarnessas the existing compile tests, and is inconclusive when no C compiler is on the path.With the
CFamilyGeneratorchange reverted, three tests fail: bothCFamilyBreaksEveryTrigraphrows and the compile-and-run test. The full suite passes: 1070 passed, 0 skipped.🤖 Generated with Claude Code
https://claude.ai/code/session_01NfxXtpcxX8hkLYrePfHkV4
Generated by Claude Code