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

Kaining Zhong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 30 06:48:02 PDT 2022


PRESIDENT810 created this revision.
PRESIDENT810 added reviewers: klimek, arphaman, johannes.
Herald added a project: All.
PRESIDENT810 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This fixes https://github.com/llvm/llvm-project/issues/55771.
Directly using StringLiteral::getString for wide string is not currently supported; therefore in ASTDiff, getStmtValue will fail when asserting that the StringLiteral has a width of 1. This patch will convert wide string to utf-8 string.


Repository:
  rG LLVM Github Monorepo

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
@@ -51,6 +51,12 @@
     return 0;
   }
 
+  // CHECK: CXXMethodDecl: :bar(const wchar_t *()
+  const wchar_t *bar() {
+    // CHECK: StringLiteral: bar(
+    return L"bar";
+  }
+
   // CHECK: AccessSpecDecl: public(
 public:
   int not_initialized;
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,19 @@
   }
   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()) {
+      unsigned int wsize = String->getByteLength() / String->getCharByteWidth();
+      const wchar_t *temp =
+          reinterpret_cast<const wchar_t *>(String->getBytes().data());
+      std::wstring wstr(temp);
+      std::string str;
+      if (!convertWideToUTF8(wstr.substr(0, wsize), str))
+        return "";
+      return str;
+    }
     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.432912.patch
Type: text/x-patch
Size: 1629 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220530/d650b26b/attachment.bin>


More information about the cfe-commits mailing list