[clang] [clang-tools-extra] [llvm] [Support][JSON] Add safeUTF8() helper to sanitize UTF-8 for JSON values (PR #212949)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 30 01:10:53 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- clang-tools-extra/clang-doc/JSONGenerator.cpp clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp llvm/include/llvm/Support/JSON.h llvm/lib/Support/JSON.cpp llvm/unittests/Support/JSONTest.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/unittests/Support/JSONTest.cpp b/llvm/unittests/Support/JSONTest.cpp
index c39164ee9..3b1407fbe 100644
--- a/llvm/unittests/Support/JSONTest.cpp
+++ b/llvm/unittests/Support/JSONTest.cpp
@@ -279,13 +279,18 @@ TEST(JSONTest, SafeUTF8) {
   // Invalid UTF-8: safeUTF8 returns a Value with U+FFFD substitutions.
   for (auto [Invalid, Expected] :
        std::vector<std::pair<const char *, const char *>>{
-           {"lone trailing \x81\x82 bytes", "lone trailing \xEF\xBF\xBD\xEF\xBF\xBD bytes"},
-           {"missing trailing \xD0 bytes", "missing trailing \xEF\xBF\xBD bytes"},
+           {"lone trailing \x81\x82 bytes",
+            "lone trailing \xEF\xBF\xBD\xEF\xBF\xBD bytes"},
+           {"missing trailing \xD0 bytes",
+            "missing trailing \xEF\xBF\xBD bytes"},
            {"truncated character \xD0", "truncated character \xEF\xBF\xBD"},
            {"not \xC1\x80 the \xE0\x9f\xBF shortest \xF0\x83\x83\x83 encoding",
-            "not \xEF\xBF\xBD\xEF\xBF\xBD the \xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD shortest \xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD encoding"},
+            "not \xEF\xBF\xBD\xEF\xBF\xBD the "
+            "\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD shortest "
+            "\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD encoding"},
            {"surrogate \xED\xA0\x80 invalid \xF4\x90\x80\x80",
-            "surrogate \xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD invalid \xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD"}}) {
+            "surrogate \xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD invalid "
+            "\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD"}}) {
     Value V = safeUTF8(Invalid);
     ASSERT_EQ(V.kind(), Value::String);
     EXPECT_EQ(*V.getAsString(), Expected);

``````````

</details>


https://github.com/llvm/llvm-project/pull/212949


More information about the cfe-commits mailing list