Skip to content

Commit d9c68a3

Browse files
committed
Modified how wchar_t string constants are communicated so that their char width can be inferred.
1 parent d611e27 commit d9c68a3

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

clang/tools/libclang/PathogenExtensions.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,15 @@ enum class PathogenConstantValueKind : int
680680
enum class PathogenStringConstantKind : int
681681
{
682682
Ascii,
683+
//! Never actually used. We replace this with the more appropriate UTF equivalent with WideCharBit set instead.
683684
WideChar,
684685
Utf8,
685686
Utf16,
686-
Utf32
687+
Utf32,
688+
//! When combined with one of the UTF values, indicates that the constant was originally a wchar_t string.
689+
WideCharBit = 1 << 31,
687690
};
691+
PATHOGEN_FLAGS(PathogenStringConstantKind);
688692
static_assert((int)PathogenStringConstantKind::Ascii == StringLiteral::Ascii, "ASCII string kinds must match.");
689693
static_assert((int)PathogenStringConstantKind::WideChar == StringLiteral::Wide, "Wide character string kinds must match.");
690694
static_assert((int)PathogenStringConstantKind::Utf8 == StringLiteral::UTF8, "UTF8 string kinds must match.");
@@ -703,7 +707,7 @@ struct PathogenConstantValueInfo
703707
bool HasUndefinedBehavior;
704708
PathogenConstantValueKind Kind;
705709
//! If Kind is UnsignedInteger, SignedInteger, or FloatingPoint: This is the size of the value in bits
706-
//! If Kind is String: This is one of PathogenStringConstantKind
710+
//! If Kind is String: This is one of PathogenStringConstantKind, potentially with WideCharBit set in the case of wchar_t.
707711
//! If Kind is Unknown, this is the Clang kind (APValue::ValueKind)
708712
int SubKind;
709713
//! The value of the constant
@@ -807,7 +811,27 @@ PATHOGEN_EXPORT bool pathogen_ComputeConstantValue(CXCursor cursor, PathogenCons
807811
{
808812
const StringLiteral* stringLiteral = (const StringLiteral*)lValueExpr;
809813
info->Kind = PathogenConstantValueKind::String;
810-
info->SubKind = (int)stringLiteral->getKind();
814+
PathogenStringConstantKind* stringKind = (PathogenStringConstantKind*)&info->SubKind;
815+
*stringKind = (PathogenStringConstantKind)stringLiteral->getKind();
816+
817+
if (*stringKind == PathogenStringConstantKind::WideChar)
818+
{
819+
switch (stringLiteral->getCharByteWidth())
820+
{
821+
case 1:
822+
*stringKind = PathogenStringConstantKind::Utf8 | PathogenStringConstantKind::WideCharBit;
823+
break;
824+
case 2:
825+
*stringKind = PathogenStringConstantKind::Utf16 | PathogenStringConstantKind::WideCharBit;
826+
break;
827+
case 4:
828+
*stringKind = PathogenStringConstantKind::Utf32 | PathogenStringConstantKind::WideCharBit;
829+
break;
830+
default:
831+
assert(false && "wchar_t string literal has an unexpected char width.");
832+
break;
833+
}
834+
}
811835

812836
PathogenConstantString* string = (PathogenConstantString*)malloc(sizeof(PathogenConstantString) + stringLiteral->getByteLength() - 1);
813837
string->SizeBytes = stringLiteral->getByteLength();

0 commit comments

Comments
 (0)