[PATCH] D126651: [clang-diff] Fix getStmtValue when dealing with wide chars
Kaining Zhong via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 1 06:34:11 PDT 2022
PRESIDENT810 updated this revision to Diff 433375.
PRESIDENT810 added a comment.
Refactored some code and add support of U16 & U32 characters, as well as tests for them.
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
@@ -51,6 +51,30 @@
return 0;
}
+ const wchar_t *fooWide(int i) {
+ if (i == 0)
+ // CHECK: StringLiteral: foo(
+ return L"foo";
+ // CHECK-NOT: ImplicitCastExpr
+ return 0;
+ }
+
+ const char16_t *fooU16(int i) {
+ if (i == 0)
+ // CHECK: StringLiteral: foo(
+ return u"foo";
+ // CHECK-NOT: ImplicitCastExpr
+ return 0;
+ }
+
+ const char32_t *fooU32(int i) {
+ if (i == 0)
+ // CHECK: StringLiteral: foo(
+ return U"foo";
+ // CHECK-NOT: ImplicitCastExpr
+ return 0;
+ }
+
// 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,39 @@
}
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 auto *temp =
+ reinterpret_cast<const wchar_t *>(String->getBytes().data());
+ std::wstring wstr(temp, wsize);
+ std::string str;
+ if (!convertWideToUTF8(wstr, str))
+ return "";
+ return str;
+ }
+ if (String->isUTF16()) {
+ unsigned int usize = String->getByteLength() / String->getCharByteWidth();
+ const auto *temp =
+ reinterpret_cast<const unsigned short *>(String->getBytes().data());
+ ArrayRef<UTF16> u16str(temp, usize);
+ std::string str;
+ if (!convertUTF16ToUTF8String(u16str, str))
+ return "";
+ return str;
+ }
+ if (String->isUTF32()) {
+ unsigned int usize = String->getByteLength() / String->getCharByteWidth();
+ const auto *temp =
+ reinterpret_cast<const unsigned int *>(String->getBytes().data());
+ ArrayRef<UTF32> u32str(temp, usize);
+ std::string str;
+ if (!convertUTF32ToUTF8String(u32str, 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.433375.patch
Type: text/x-patch
Size: 2748 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220601/b55c0fdf/attachment-0001.bin>
More information about the cfe-commits
mailing list