[PATCH] D126651: [clang-diff] Fix getStmtValue when dealing with wide, UTF16 UTF32 chars

Johannes Altmanninger via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 11:25:47 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe80748ff8840: [clang-diff] Fix assertion error when dealing with wide strings (authored by PRESIDENT810, committed by johannes).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126651/new/

https://reviews.llvm.org/D126651

Files:
  clang/lib/Tooling/ASTDiff/ASTDiff.cpp
  clang/test/Tooling/clang-diff-ast.cpp


Index: clang/test/Tooling/clang-diff-ast.cpp
===================================================================
--- clang/test/Tooling/clang-diff-ast.cpp
+++ clang/test/Tooling/clang-diff-ast.cpp
@@ -47,6 +47,12 @@
     if (i == 0)
       // CHECK: StringLiteral: foo(
       return "foo";
+    // CHECK: StringLiteral: wide(
+    (void)L"wide";
+    // CHECK: StringLiteral: utf-16(
+    (void)u"utf-16";
+    // CHECK: StringLiteral: utf-32(
+    (void)U"utf-32";
     // CHECK-NOT: ImplicitCastExpr
     return 0;
   }
Index: clang/lib/Tooling/ASTDiff/ASTDiff.cpp
===================================================================
--- clang/lib/Tooling/ASTDiff/ASTDiff.cpp
+++ clang/lib/Tooling/ASTDiff/ASTDiff.cpp
@@ -16,6 +16,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/ADT/PriorityQueue.h"
+#include "llvm/Support/ConvertUTF.h"
 
 #include <limits>
 #include <memory>
@@ -463,8 +464,29 @@
   }
   if (auto *D = dyn_cast<DeclRefExpr>(S))
     return getRelativeName(D->getDecl(), getEnclosingDeclContext(AST, S));
-  if (auto *String = dyn_cast<StringLiteral>(S))
+  if (auto *String = dyn_cast<StringLiteral>(S)) {
+    if (String->isWide() || String->isUTF16() || String->isUTF32()) {
+      std::string UTF8Str;
+      unsigned int NumChars = String->getLength();
+      const char *Bytes = String->getBytes().data();
+      if (String->isWide()) {
+        const auto *Chars = reinterpret_cast<const wchar_t *>(Bytes);
+        if (!convertWideToUTF8({Chars, NumChars}, UTF8Str))
+          return "";
+      } else if (String->isUTF16()) {
+        const auto *Chars = reinterpret_cast<const UTF16 *>(Bytes);
+        if (!convertUTF16ToUTF8String({Chars, NumChars}, UTF8Str))
+          return "";
+      } else {
+        assert(String->isUTF32() && "Unsupported string encoding.");
+        const auto *Chars = reinterpret_cast<const UTF32 *>(Bytes);
+        if (!convertUTF32ToUTF8String({Chars, NumChars}, UTF8Str))
+          return "";
+      }
+      return UTF8Str;
+    }
     return std::string(String->getString());
+  }
   if (auto *B = dyn_cast<CXXBoolLiteralExpr>(S))
     return B->getValue() ? "true" : "false";
   return "";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126651.434898.patch
Type: text/x-patch
Size: 2203 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220607/c28c6bf0/attachment.bin>


More information about the cfe-commits mailing list