Skip to content

Commit 3e6ce58

Browse files
committed
[clang][NFC] Refactor StringLiteral::StringKind
This patch converts `StringLiteral::StringKind` to a scoped enum in namespace scope. This enabled forward-declarations of this enum where necessary, e.g. for `preferred_type` annotation for bit-fields.
1 parent c0a7391 commit 3e6ce58

File tree

13 files changed

+94
-76
lines changed

13 files changed

+94
-76
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,15 @@ class ImaginaryLiteral : public Expr {
17861786
}
17871787
};
17881788

1789+
enum class StringLiteralKind {
1790+
Ordinary,
1791+
Wide,
1792+
UTF8,
1793+
UTF16,
1794+
UTF32,
1795+
Unevaluated
1796+
};
1797+
17891798
/// StringLiteral - This represents a string literal expression, e.g. "foo"
17901799
/// or L"bar" (wide strings). The actual string data can be obtained with
17911800
/// getBytes() and is NOT null-terminated. The length of the string data is
@@ -1825,7 +1834,7 @@ class StringLiteral final
18251834
/// * An array of getByteLength() char used to store the string data.
18261835

18271836
public:
1828-
enum StringKind { Ordinary, Wide, UTF8, UTF16, UTF32, Unevaluated };
1837+
// enum StringKind { Ordinary, Wide, UTF8, UTF16, UTF32, Unevaluated };
18291838

18301839
private:
18311840
unsigned numTrailingObjects(OverloadToken<unsigned>) const { return 1; }
@@ -1849,7 +1858,7 @@ class StringLiteral final
18491858
}
18501859

18511860
/// Build a string literal.
1852-
StringLiteral(const ASTContext &Ctx, StringRef Str, StringKind Kind,
1861+
StringLiteral(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind,
18531862
bool Pascal, QualType Ty, const SourceLocation *Loc,
18541863
unsigned NumConcatenated);
18551864

@@ -1858,7 +1867,8 @@ class StringLiteral final
18581867
unsigned CharByteWidth);
18591868

18601869
/// Map a target and string kind to the appropriate character width.
1861-
static unsigned mapCharByteWidth(TargetInfo const &Target, StringKind SK);
1870+
static unsigned mapCharByteWidth(TargetInfo const &Target,
1871+
StringLiteralKind SK);
18621872

18631873
/// Set one of the string literal token.
18641874
void setStrTokenLoc(unsigned TokNum, SourceLocation L) {
@@ -1870,13 +1880,13 @@ class StringLiteral final
18701880
/// This is the "fully general" constructor that allows representation of
18711881
/// strings formed from multiple concatenated tokens.
18721882
static StringLiteral *Create(const ASTContext &Ctx, StringRef Str,
1873-
StringKind Kind, bool Pascal, QualType Ty,
1883+
StringLiteralKind Kind, bool Pascal, QualType Ty,
18741884
const SourceLocation *Loc,
18751885
unsigned NumConcatenated);
18761886

18771887
/// Simple constructor for string literals made from one token.
18781888
static StringLiteral *Create(const ASTContext &Ctx, StringRef Str,
1879-
StringKind Kind, bool Pascal, QualType Ty,
1889+
StringLiteralKind Kind, bool Pascal, QualType Ty,
18801890
SourceLocation Loc) {
18811891
return Create(Ctx, Str, Kind, Pascal, Ty, &Loc, 1);
18821892
}
@@ -1918,16 +1928,16 @@ class StringLiteral final
19181928
unsigned getLength() const { return *getTrailingObjects<unsigned>(); }
19191929
unsigned getCharByteWidth() const { return StringLiteralBits.CharByteWidth; }
19201930

1921-
StringKind getKind() const {
1922-
return static_cast<StringKind>(StringLiteralBits.Kind);
1931+
StringLiteralKind getKind() const {
1932+
return static_cast<StringLiteralKind>(StringLiteralBits.Kind);
19231933
}
19241934

1925-
bool isOrdinary() const { return getKind() == Ordinary; }
1926-
bool isWide() const { return getKind() == Wide; }
1927-
bool isUTF8() const { return getKind() == UTF8; }
1928-
bool isUTF16() const { return getKind() == UTF16; }
1929-
bool isUTF32() const { return getKind() == UTF32; }
1930-
bool isUnevaluated() const { return getKind() == Unevaluated; }
1935+
bool isOrdinary() const { return getKind() == StringLiteralKind::Ordinary; }
1936+
bool isWide() const { return getKind() == StringLiteralKind::Wide; }
1937+
bool isUTF8() const { return getKind() == StringLiteralKind::UTF8; }
1938+
bool isUTF16() const { return getKind() == StringLiteralKind::UTF16; }
1939+
bool isUTF32() const { return getKind() == StringLiteralKind::UTF32; }
1940+
bool isUnevaluated() const { return getKind() == StringLiteralKind::Unevaluated; }
19311941
bool isPascal() const { return StringLiteralBits.IsPascal; }
19321942

19331943
bool containsNonAscii() const {

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12221,7 +12221,7 @@ ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const {
1222112221
StringLiteral *&Result = StringLiteralCache[Key];
1222212222
if (!Result)
1222312223
Result = StringLiteral::Create(
12224-
*this, Key, StringLiteral::Ordinary,
12224+
*this, Key, StringLiteralKind::Ordinary,
1222512225
/*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()),
1222612226
SourceLocation());
1222712227
return Result;

clang/lib/AST/Expr.cpp

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,23 +1060,23 @@ double FloatingLiteral::getValueAsApproximateDouble() const {
10601060
}
10611061

10621062
unsigned StringLiteral::mapCharByteWidth(TargetInfo const &Target,
1063-
StringKind SK) {
1063+
StringLiteralKind SK) {
10641064
unsigned CharByteWidth = 0;
10651065
switch (SK) {
1066-
case Ordinary:
1067-
case UTF8:
1066+
case StringLiteralKind::Ordinary:
1067+
case StringLiteralKind::UTF8:
10681068
CharByteWidth = Target.getCharWidth();
10691069
break;
1070-
case Wide:
1070+
case StringLiteralKind::Wide:
10711071
CharByteWidth = Target.getWCharWidth();
10721072
break;
1073-
case UTF16:
1073+
case StringLiteralKind::UTF16:
10741074
CharByteWidth = Target.getChar16Width();
10751075
break;
1076-
case UTF32:
1076+
case StringLiteralKind::UTF32:
10771077
CharByteWidth = Target.getChar32Width();
10781078
break;
1079-
case Unevaluated:
1079+
case StringLiteralKind::Unevaluated:
10801080
return sizeof(char); // Host;
10811081
}
10821082
assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
@@ -1087,17 +1087,17 @@ unsigned StringLiteral::mapCharByteWidth(TargetInfo const &Target,
10871087
}
10881088

10891089
StringLiteral::StringLiteral(const ASTContext &Ctx, StringRef Str,
1090-
StringKind Kind, bool Pascal, QualType Ty,
1090+
StringLiteralKind Kind, bool Pascal, QualType Ty,
10911091
const SourceLocation *Loc,
10921092
unsigned NumConcatenated)
10931093
: Expr(StringLiteralClass, Ty, VK_LValue, OK_Ordinary) {
10941094

10951095
unsigned Length = Str.size();
10961096

1097-
StringLiteralBits.Kind = Kind;
1097+
StringLiteralBits.Kind = llvm::to_underlying(Kind);
10981098
StringLiteralBits.NumConcatenated = NumConcatenated;
10991099

1100-
if (Kind != StringKind::Unevaluated) {
1100+
if (Kind != StringLiteralKind::Unevaluated) {
11011101
assert(Ctx.getAsConstantArrayType(Ty) &&
11021102
"StringLiteral must be of constant array type!");
11031103
unsigned CharByteWidth = mapCharByteWidth(Ctx.getTargetInfo(), Kind);
@@ -1152,8 +1152,8 @@ StringLiteral::StringLiteral(EmptyShell Empty, unsigned NumConcatenated,
11521152
}
11531153

11541154
StringLiteral *StringLiteral::Create(const ASTContext &Ctx, StringRef Str,
1155-
StringKind Kind, bool Pascal, QualType Ty,
1156-
const SourceLocation *Loc,
1155+
StringLiteralKind Kind, bool Pascal,
1156+
QualType Ty, const SourceLocation *Loc,
11571157
unsigned NumConcatenated) {
11581158
void *Mem = Ctx.Allocate(totalSizeToAlloc<unsigned, SourceLocation, char>(
11591159
1, NumConcatenated, Str.size()),
@@ -1175,13 +1175,21 @@ StringLiteral *StringLiteral::CreateEmpty(const ASTContext &Ctx,
11751175

11761176
void StringLiteral::outputString(raw_ostream &OS) const {
11771177
switch (getKind()) {
1178-
case Unevaluated:
1179-
case Ordinary:
1178+
case StringLiteralKind::Unevaluated:
1179+
case StringLiteralKind::Ordinary:
11801180
break; // no prefix.
1181-
case Wide: OS << 'L'; break;
1182-
case UTF8: OS << "u8"; break;
1183-
case UTF16: OS << 'u'; break;
1184-
case UTF32: OS << 'U'; break;
1181+
case StringLiteralKind::Wide:
1182+
OS << 'L';
1183+
break;
1184+
case StringLiteralKind::UTF8:
1185+
OS << "u8";
1186+
break;
1187+
case StringLiteralKind::UTF16:
1188+
OS << 'u';
1189+
break;
1190+
case StringLiteralKind::UTF32:
1191+
OS << 'U';
1192+
break;
11851193
}
11861194
OS << '"';
11871195
static const char Hex[] = "0123456789ABCDEF";
@@ -1195,8 +1203,8 @@ void StringLiteral::outputString(raw_ostream &OS) const {
11951203

11961204
// Convert UTF-16 surrogate pairs back to codepoints before rendering.
11971205
// Leave invalid surrogates alone; we'll use \x for those.
1198-
if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
1199-
Char <= 0xdbff) {
1206+
if (getKind() == StringLiteralKind::UTF16 && I != N - 1 &&
1207+
Char >= 0xd800 && Char <= 0xdbff) {
12001208
uint32_t Trail = getCodeUnit(I + 1);
12011209
if (Trail >= 0xdc00 && Trail <= 0xdfff) {
12021210
Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
@@ -1208,7 +1216,7 @@ void StringLiteral::outputString(raw_ostream &OS) const {
12081216
// If this is a wide string, output characters over 0xff using \x
12091217
// escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
12101218
// codepoint: use \x escapes for invalid codepoints.
1211-
if (getKind() == Wide ||
1219+
if (getKind() == StringLiteralKind::Wide ||
12121220
(Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
12131221
// FIXME: Is this the best way to print wchar_t?
12141222
OS << "\\x";
@@ -1285,9 +1293,9 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
12851293
const LangOptions &Features,
12861294
const TargetInfo &Target, unsigned *StartToken,
12871295
unsigned *StartTokenByteOffset) const {
1288-
assert((getKind() == StringLiteral::Ordinary ||
1289-
getKind() == StringLiteral::UTF8 ||
1290-
getKind() == StringLiteral::Unevaluated) &&
1296+
assert((getKind() == StringLiteralKind::Ordinary ||
1297+
getKind() == StringLiteralKind::UTF8 ||
1298+
getKind() == StringLiteralKind::Unevaluated) &&
12911299
"Only narrow string literals are currently supported");
12921300

12931301
// Loop over all of the tokens in this string until we find the one that

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9043,7 +9043,7 @@ class PointerExprEvaluator
90439043
CharTy, Size, nullptr, ArraySizeModifier::Normal, 0);
90449044

90459045
StringLiteral *SL =
9046-
StringLiteral::Create(Info.Ctx, ResultStr, StringLiteral::Ordinary,
9046+
StringLiteral::Create(Info.Ctx, ResultStr, StringLiteralKind::Ordinary,
90479047
/*Pascal*/ false, ArrayTy, E->getLocation());
90489048

90499049
evaluateLValue(SL, Result);

clang/lib/AST/StmtProfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) {
13711371
void StmtProfiler::VisitStringLiteral(const StringLiteral *S) {
13721372
VisitExpr(S);
13731373
ID.AddString(S->getBytes());
1374-
ID.AddInteger(S->getKind());
1374+
ID.AddInteger(llvm::to_underlying(S->getKind()));
13751375
}
13761376

13771377
void StmtProfiler::VisitParenExpr(const ParenExpr *S) {

clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ namespace {
601601
QualType StrType = Context->getConstantArrayType(
602602
Context->CharTy, llvm::APInt(32, Str.size() + 1), nullptr,
603603
ArraySizeModifier::Normal, 0);
604-
return StringLiteral::Create(*Context, Str, StringLiteral::Ordinary,
604+
return StringLiteral::Create(*Context, Str, StringLiteralKind::Ordinary,
605605
/*Pascal=*/false, StrType, SourceLocation());
606606
}
607607
};

clang/lib/Frontend/Rewrite/RewriteObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ namespace {
500500
QualType StrType = Context->getConstantArrayType(
501501
Context->CharTy, llvm::APInt(32, Str.size() + 1), nullptr,
502502
ArraySizeModifier::Normal, 0);
503-
return StringLiteral::Create(*Context, Str, StringLiteral::Ordinary,
503+
return StringLiteral::Create(*Context, Str, StringLiteralKind::Ordinary,
504504
/*Pascal=*/false, StrType, SourceLocation());
505505
}
506506
};

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10130,7 +10130,7 @@ class FormatStringLiteral {
1013010130
unsigned getLength() const { return FExpr->getLength() - Offset; }
1013110131
unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); }
1013210132

10133-
StringLiteral::StringKind getKind() const { return FExpr->getKind(); }
10133+
StringLiteralKind getKind() const { return FExpr->getKind(); }
1013410134

1013510135
QualType getType() const { return FExpr->getType(); }
1013610136

clang/lib/Sema/SemaExpr.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ ExprResult Sema::ActOnUnevaluatedStringLiteral(ArrayRef<Token> StringToks) {
19691969
StringTokLocs.push_back(Tok.getLocation());
19701970

19711971
StringLiteral *Lit = StringLiteral::Create(
1972-
Context, Literal.GetString(), StringLiteral::Unevaluated, false, {},
1972+
Context, Literal.GetString(), StringLiteralKind::Unevaluated, false, {},
19731973
&StringTokLocs[0], StringTokLocs.size());
19741974

19751975
if (!Literal.getUDSuffix().empty()) {
@@ -2055,28 +2055,28 @@ Sema::ActOnStringLiteral(ArrayRef<Token> StringToks, Scope *UDLScope) {
20552055
StringTokLocs.push_back(Tok.getLocation());
20562056

20572057
QualType CharTy = Context.CharTy;
2058-
StringLiteral::StringKind Kind = StringLiteral::Ordinary;
2058+
StringLiteralKind Kind = StringLiteralKind::Ordinary;
20592059
if (Literal.isWide()) {
20602060
CharTy = Context.getWideCharType();
2061-
Kind = StringLiteral::Wide;
2061+
Kind = StringLiteralKind::Wide;
20622062
} else if (Literal.isUTF8()) {
20632063
if (getLangOpts().Char8)
20642064
CharTy = Context.Char8Ty;
2065-
Kind = StringLiteral::UTF8;
2065+
Kind = StringLiteralKind::UTF8;
20662066
} else if (Literal.isUTF16()) {
20672067
CharTy = Context.Char16Ty;
2068-
Kind = StringLiteral::UTF16;
2068+
Kind = StringLiteralKind::UTF16;
20692069
} else if (Literal.isUTF32()) {
20702070
CharTy = Context.Char32Ty;
2071-
Kind = StringLiteral::UTF32;
2071+
Kind = StringLiteralKind::UTF32;
20722072
} else if (Literal.isPascal()) {
20732073
CharTy = Context.UnsignedCharTy;
20742074
}
20752075

20762076
// Warn on initializing an array of char from a u8 string literal; this
20772077
// becomes ill-formed in C++2a.
20782078
if (getLangOpts().CPlusPlus && !getLangOpts().CPlusPlus20 &&
2079-
!getLangOpts().Char8 && Kind == StringLiteral::UTF8) {
2079+
!getLangOpts().Char8 && Kind == StringLiteralKind::UTF8) {
20802080
Diag(StringTokLocs.front(), diag::warn_cxx20_compat_utf8_string);
20812081

20822082
// Create removals for all 'u8' prefixes in the string literal(s). This
@@ -3743,14 +3743,14 @@ ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc,
37433743
ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr,
37443744
ArraySizeModifier::Normal,
37453745
/*IndexTypeQuals*/ 0);
3746-
SL = StringLiteral::Create(Context, RawChars, StringLiteral::Wide,
3746+
SL = StringLiteral::Create(Context, RawChars, StringLiteralKind::Wide,
37473747
/*Pascal*/ false, ResTy, Loc);
37483748
} else {
37493749
ResTy = Context.adjustStringLiteralBaseType(Context.CharTy.withConst());
37503750
ResTy = Context.getConstantArrayType(ResTy, LengthI, nullptr,
37513751
ArraySizeModifier::Normal,
37523752
/*IndexTypeQuals*/ 0);
3753-
SL = StringLiteral::Create(Context, Str, StringLiteral::Ordinary,
3753+
SL = StringLiteral::Create(Context, Str, StringLiteralKind::Ordinary,
37543754
/*Pascal*/ false, ResTy, Loc);
37553755
}
37563756
}
@@ -4006,7 +4006,7 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
40064006
llvm::APInt(32, Length + 1), nullptr, ArraySizeModifier::Normal, 0);
40074007
Expr *Lit =
40084008
StringLiteral::Create(Context, StringRef(TokSpelling.data(), Length),
4009-
StringLiteral::Ordinary,
4009+
StringLiteralKind::Ordinary,
40104010
/*Pascal*/ false, StrTy, &TokLoc, 1);
40114011
return BuildLiteralOperatorCall(R, OpNameInfo, Lit, TokLoc);
40124012
}

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4105,20 +4105,20 @@ Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
41054105
// explicit appropriate pointer target type (C++ 4.2p2).
41064106
if (!ToPtrType->getPointeeType().hasQualifiers()) {
41074107
switch (StrLit->getKind()) {
4108-
case StringLiteral::UTF8:
4109-
case StringLiteral::UTF16:
4110-
case StringLiteral::UTF32:
4111-
// We don't allow UTF literals to be implicitly converted
4112-
break;
4113-
case StringLiteral::Ordinary:
4114-
return (ToPointeeType->getKind() == BuiltinType::Char_U ||
4115-
ToPointeeType->getKind() == BuiltinType::Char_S);
4116-
case StringLiteral::Wide:
4117-
return Context.typesAreCompatible(Context.getWideCharType(),
4118-
QualType(ToPointeeType, 0));
4119-
case StringLiteral::Unevaluated:
4120-
assert(false && "Unevaluated string literal in expression");
4121-
break;
4108+
case StringLiteralKind::UTF8:
4109+
case StringLiteralKind::UTF16:
4110+
case StringLiteralKind::UTF32:
4111+
// We don't allow UTF literals to be implicitly converted
4112+
break;
4113+
case StringLiteralKind::Ordinary:
4114+
return (ToPointeeType->getKind() == BuiltinType::Char_U ||
4115+
ToPointeeType->getKind() == BuiltinType::Char_S);
4116+
case StringLiteralKind::Wide:
4117+
return Context.typesAreCompatible(Context.getWideCharType(),
4118+
QualType(ToPointeeType, 0));
4119+
case StringLiteralKind::Unevaluated:
4120+
assert(false && "Unevaluated string literal in expression");
4121+
break;
41224122
}
41234123
}
41244124
}

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
7171
QualType StrTy = Context.getConstantArrayType(
7272
CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1), nullptr,
7373
CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
74-
S = StringLiteral::Create(Context, StrBuf, StringLiteral::Ordinary,
74+
S = StringLiteral::Create(Context, StrBuf, StringLiteralKind::Ordinary,
7575
/*Pascal=*/false, StrTy, &StrLocs[0],
7676
StrLocs.size());
7777
}

0 commit comments

Comments
 (0)