Skip to content

Commit 6e35db0

Browse files
committed
[clang][NFC] Refactor PredefinedExpr::IdentKind
This patch converts `PredefinedExpr::IdentKind` into a scoped enum in namespace scope, making it eligible for forward declaring. This is useful in certain contexts, such as `preferred_type` annotations on bit-fields.
1 parent 13956fd commit 6e35db0

File tree

9 files changed

+81
-79
lines changed

9 files changed

+81
-79
lines changed

clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ void LambdaFunctionNameCheck::registerPPCallbacks(
8282

8383
void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
8484
const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>("E");
85-
if (E->getIdentKind() != PredefinedExpr::Func &&
86-
E->getIdentKind() != PredefinedExpr::Function) {
85+
if (E->getIdentKind() != PredefinedIdentKind::Func &&
86+
E->getIdentKind() != PredefinedIdentKind::Function) {
8787
// We don't care about other PredefinedExprs.
8888
return;
8989
}

clang/include/clang/AST/Expr.h

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,19 @@ class StringLiteral final
19991999
}
20002000
};
20012001

2002+
enum class PredefinedIdentKind {
2003+
Func,
2004+
Function,
2005+
LFunction, // Same as Function, but as wide string.
2006+
FuncDName,
2007+
FuncSig,
2008+
LFuncSig, // Same as FuncSig, but as wide string
2009+
PrettyFunction,
2010+
/// The same as PrettyFunction, except that the
2011+
/// 'virtual' keyword is omitted for virtual member functions.
2012+
PrettyFunctionNoVirtual
2013+
};
2014+
20022015
/// [C99 6.4.2.2] - A predefined identifier such as __func__.
20032016
class PredefinedExpr final
20042017
: public Expr,
@@ -2010,22 +2023,7 @@ class PredefinedExpr final
20102023
// "Stmt *" for the predefined identifier. It is present if and only if
20112024
// hasFunctionName() is true and is always a "StringLiteral *".
20122025

2013-
public:
2014-
enum IdentKind {
2015-
Func,
2016-
Function,
2017-
LFunction, // Same as Function, but as wide string.
2018-
FuncDName,
2019-
FuncSig,
2020-
LFuncSig, // Same as FuncSig, but as wide string
2021-
PrettyFunction,
2022-
/// The same as PrettyFunction, except that the
2023-
/// 'virtual' keyword is omitted for virtual member functions.
2024-
PrettyFunctionNoVirtual
2025-
};
2026-
2027-
private:
2028-
PredefinedExpr(SourceLocation L, QualType FNTy, IdentKind IK,
2026+
PredefinedExpr(SourceLocation L, QualType FNTy, PredefinedIdentKind IK,
20292027
bool IsTransparent, StringLiteral *SL);
20302028

20312029
explicit PredefinedExpr(EmptyShell Empty, bool HasFunctionName);
@@ -2045,15 +2043,15 @@ class PredefinedExpr final
20452043
/// If IsTransparent, the PredefinedExpr is transparently handled as a
20462044
/// StringLiteral.
20472045
static PredefinedExpr *Create(const ASTContext &Ctx, SourceLocation L,
2048-
QualType FNTy, IdentKind IK, bool IsTransparent,
2049-
StringLiteral *SL);
2046+
QualType FNTy, PredefinedIdentKind IK,
2047+
bool IsTransparent, StringLiteral *SL);
20502048

20512049
/// Create an empty PredefinedExpr.
20522050
static PredefinedExpr *CreateEmpty(const ASTContext &Ctx,
20532051
bool HasFunctionName);
20542052

2055-
IdentKind getIdentKind() const {
2056-
return static_cast<IdentKind>(PredefinedExprBits.Kind);
2053+
PredefinedIdentKind getIdentKind() const {
2054+
return static_cast<PredefinedIdentKind>(PredefinedExprBits.Kind);
20572055
}
20582056

20592057
bool isTransparent() const { return PredefinedExprBits.IsTransparent; }
@@ -2073,12 +2071,13 @@ class PredefinedExpr final
20732071
: nullptr;
20742072
}
20752073

2076-
static StringRef getIdentKindName(IdentKind IK);
2074+
static StringRef getIdentKindName(PredefinedIdentKind IK);
20772075
StringRef getIdentKindName() const {
20782076
return getIdentKindName(getIdentKind());
20792077
}
20802078

2081-
static std::string ComputeName(IdentKind IK, const Decl *CurrentDecl);
2079+
static std::string ComputeName(PredefinedIdentKind IK,
2080+
const Decl *CurrentDecl);
20822081

20832082
SourceLocation getBeginLoc() const { return getLocation(); }
20842083
SourceLocation getEndLoc() const { return getLocation(); }

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5723,8 +5723,7 @@ class Sema final {
57235723
// __FUNCTION__) are replaced (expanded) with string-literal Tokens.
57245724
std::vector<Token> ExpandFunctionLocalPredefinedMacros(ArrayRef<Token> Toks);
57255725

5726-
ExprResult BuildPredefinedExpr(SourceLocation Loc,
5727-
PredefinedExpr::IdentKind IK);
5726+
ExprResult BuildPredefinedExpr(SourceLocation Loc, PredefinedIdentKind IK);
57285727
ExprResult ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind);
57295728
ExprResult ActOnIntegerConstant(SourceLocation Loc, uint64_t Val);
57305729

clang/lib/AST/Expr.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,11 @@ std::string SYCLUniqueStableNameExpr::ComputeName(ASTContext &Context,
604604
return Out.str();
605605
}
606606

607-
PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentKind IK,
608-
bool IsTransparent, StringLiteral *SL)
607+
PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy,
608+
PredefinedIdentKind IK, bool IsTransparent,
609+
StringLiteral *SL)
609610
: Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary) {
610-
PredefinedExprBits.Kind = IK;
611+
PredefinedExprBits.Kind = llvm::to_underlying(IK);
611612
assert((getIdentKind() == IK) &&
612613
"IdentKind do not fit in PredefinedExprBitfields!");
613614
bool HasFunctionName = SL != nullptr;
@@ -625,7 +626,7 @@ PredefinedExpr::PredefinedExpr(EmptyShell Empty, bool HasFunctionName)
625626
}
626627

627628
PredefinedExpr *PredefinedExpr::Create(const ASTContext &Ctx, SourceLocation L,
628-
QualType FNTy, IdentKind IK,
629+
QualType FNTy, PredefinedIdentKind IK,
629630
bool IsTransparent, StringLiteral *SL) {
630631
bool HasFunctionName = SL != nullptr;
631632
void *Mem = Ctx.Allocate(totalSizeToAlloc<Stmt *>(HasFunctionName),
@@ -640,34 +641,35 @@ PredefinedExpr *PredefinedExpr::CreateEmpty(const ASTContext &Ctx,
640641
return new (Mem) PredefinedExpr(EmptyShell(), HasFunctionName);
641642
}
642643

643-
StringRef PredefinedExpr::getIdentKindName(PredefinedExpr::IdentKind IK) {
644+
StringRef PredefinedExpr::getIdentKindName(PredefinedIdentKind IK) {
644645
switch (IK) {
645-
case Func:
646+
case PredefinedIdentKind::Func:
646647
return "__func__";
647-
case Function:
648+
case PredefinedIdentKind::Function:
648649
return "__FUNCTION__";
649-
case FuncDName:
650+
case PredefinedIdentKind::FuncDName:
650651
return "__FUNCDNAME__";
651-
case LFunction:
652+
case PredefinedIdentKind::LFunction:
652653
return "L__FUNCTION__";
653-
case PrettyFunction:
654+
case PredefinedIdentKind::PrettyFunction:
654655
return "__PRETTY_FUNCTION__";
655-
case FuncSig:
656+
case PredefinedIdentKind::FuncSig:
656657
return "__FUNCSIG__";
657-
case LFuncSig:
658+
case PredefinedIdentKind::LFuncSig:
658659
return "L__FUNCSIG__";
659-
case PrettyFunctionNoVirtual:
660+
case PredefinedIdentKind::PrettyFunctionNoVirtual:
660661
break;
661662
}
662663
llvm_unreachable("Unknown ident kind for PredefinedExpr");
663664
}
664665

665666
// FIXME: Maybe this should use DeclPrinter with a special "print predefined
666667
// expr" policy instead.
667-
std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
668+
std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
669+
const Decl *CurrentDecl) {
668670
ASTContext &Context = CurrentDecl->getASTContext();
669671

670-
if (IK == PredefinedExpr::FuncDName) {
672+
if (IK == PredefinedIdentKind::FuncDName) {
671673
if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
672674
std::unique_ptr<MangleContext> MC;
673675
MC.reset(Context.createMangleContext());
@@ -712,15 +714,17 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
712714
return std::string(Out.str());
713715
}
714716
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
715-
if (IK != PrettyFunction && IK != PrettyFunctionNoVirtual &&
716-
IK != FuncSig && IK != LFuncSig)
717+
if (IK != PredefinedIdentKind::PrettyFunction &&
718+
IK != PredefinedIdentKind::PrettyFunctionNoVirtual &&
719+
IK != PredefinedIdentKind::FuncSig &&
720+
IK != PredefinedIdentKind::LFuncSig)
717721
return FD->getNameAsString();
718722

719723
SmallString<256> Name;
720724
llvm::raw_svector_ostream Out(Name);
721725

722726
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
723-
if (MD->isVirtual() && IK != PrettyFunctionNoVirtual)
727+
if (MD->isVirtual() && IK != PredefinedIdentKind::PrettyFunctionNoVirtual)
724728
Out << "virtual ";
725729
if (MD->isStatic())
726730
Out << "static ";
@@ -752,7 +756,8 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
752756
if (FD->hasWrittenPrototype())
753757
FT = dyn_cast<FunctionProtoType>(AFT);
754758

755-
if (IK == FuncSig || IK == LFuncSig) {
759+
if (IK == PredefinedIdentKind::FuncSig ||
760+
IK == PredefinedIdentKind::LFuncSig) {
756761
switch (AFT->getCallConv()) {
757762
case CC_C: POut << "__cdecl "; break;
758763
case CC_X86StdCall: POut << "__stdcall "; break;
@@ -777,7 +782,8 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
777782
if (FT->isVariadic()) {
778783
if (FD->getNumParams()) POut << ", ";
779784
POut << "...";
780-
} else if ((IK == FuncSig || IK == LFuncSig ||
785+
} else if ((IK == PredefinedIdentKind::FuncSig ||
786+
IK == PredefinedIdentKind::LFuncSig ||
781787
!Context.getLangOpts().CPlusPlus) &&
782788
!Decl->getNumParams()) {
783789
POut << "void";
@@ -902,7 +908,8 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
902908

903909
return std::string(Name);
904910
}
905-
if (isa<TranslationUnitDecl>(CurrentDecl) && IK == PrettyFunction) {
911+
if (isa<TranslationUnitDecl>(CurrentDecl) &&
912+
IK == PredefinedIdentKind::PrettyFunction) {
906913
// __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
907914
return "top level";
908915
}
@@ -2282,8 +2289,8 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
22822289
case SourceLocIdentKind::FuncSig: {
22832290
const auto *CurDecl = dyn_cast<Decl>(Context);
22842291
const auto Kind = getIdentKind() == SourceLocIdentKind::Function
2285-
? PredefinedExpr::Function
2286-
: PredefinedExpr::FuncSig;
2292+
? PredefinedIdentKind::Function
2293+
: PredefinedIdentKind::FuncSig;
22872294
return MakeStringLiteral(
22882295
CurDecl ? PredefinedExpr::ComputeName(Kind, CurDecl) : std::string(""));
22892296
}
@@ -2317,7 +2324,7 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
23172324
Value.getStructField(F->getFieldIndex()) = MakeStringLiteral(
23182325
CurDecl && !isa<TranslationUnitDecl>(CurDecl)
23192326
? StringRef(PredefinedExpr::ComputeName(
2320-
PredefinedExpr::PrettyFunction, CurDecl))
2327+
PredefinedIdentKind::PrettyFunction, CurDecl))
23212328
: "");
23222329
} else if (Name == "_M_line") {
23232330
llvm::APSInt IntVal = Ctx.MakeIntValue(PLoc.getLine(), F->getType());

clang/lib/AST/StmtProfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ void StmtProfiler::VisitSYCLUniqueStableNameExpr(
13281328

13291329
void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
13301330
VisitExpr(S);
1331-
ID.AddInteger(S->getIdentKind());
1331+
ID.AddInteger(llvm::to_underlying(S->getIdentKind()));
13321332
}
13331333

13341334
void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {

clang/lib/AST/VTableBuilder.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,9 +1956,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
19561956
case VTableComponent::CK_FunctionPointer: {
19571957
const CXXMethodDecl *MD = Component.getFunctionDecl();
19581958

1959-
std::string Str =
1960-
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
1961-
MD);
1959+
std::string Str = PredefinedExpr::ComputeName(
1960+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
19621961
Out << Str;
19631962
if (MD->isPure())
19641963
Out << " [pure]";
@@ -2036,9 +2035,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
20362035
case VTableComponent::CK_UnusedFunctionPointer: {
20372036
const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
20382037

2039-
std::string Str =
2040-
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2041-
MD);
2038+
std::string Str = PredefinedExpr::ComputeName(
2039+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
20422040
Out << "[unused] " << Str;
20432041
if (MD->isPure())
20442042
Out << " [pure]";
@@ -2115,9 +2113,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
21152113

21162114
for (const auto &I : Thunks) {
21172115
const CXXMethodDecl *MD = I.first;
2118-
std::string MethodName =
2119-
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2120-
MD);
2116+
std::string MethodName = PredefinedExpr::ComputeName(
2117+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
21212118

21222119
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
21232120
}
@@ -2181,9 +2178,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
21812178
continue;
21822179
MD = MD->getCanonicalDecl();
21832180

2184-
std::string MethodName =
2185-
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2186-
MD);
2181+
std::string MethodName = PredefinedExpr::ComputeName(
2182+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
21872183

21882184
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
21892185
GlobalDecl GD(DD, Dtor_Complete);
@@ -3177,7 +3173,7 @@ void VFTableBuilder::dumpLayout(raw_ostream &Out) {
31773173
// FIXME: Figure out how to print the real thunk type, since they can
31783174
// differ in the return type.
31793175
std::string Str = PredefinedExpr::ComputeName(
3180-
PredefinedExpr::PrettyFunctionNoVirtual, MD);
3176+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
31813177
Out << Str;
31823178
if (MD->isPure())
31833179
Out << " [pure]";
@@ -3232,7 +3228,7 @@ void VFTableBuilder::dumpLayout(raw_ostream &Out) {
32323228
for (const auto &I : Thunks) {
32333229
const CXXMethodDecl *MD = I.first;
32343230
std::string MethodName = PredefinedExpr::ComputeName(
3235-
PredefinedExpr::PrettyFunctionNoVirtual, MD);
3231+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
32363232

32373233
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
32383234
}
@@ -3664,7 +3660,7 @@ void MicrosoftVTableContext::dumpMethodLocations(
36643660
assert(hasVtableSlot(MD));
36653661

36663662
std::string MethodName = PredefinedExpr::ComputeName(
3667-
PredefinedExpr::PrettyFunctionNoVirtual, MD);
3663+
PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
36683664

36693665
if (isa<CXXDestructorDecl>(MD)) {
36703666
IndicesMap[I.second] = MethodName + " [scalar deleting]";

clang/lib/Sema/SemaExpr.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,24 +1884,24 @@ ExprResult Sema::CreateGenericSelectionExpr(
18841884
ContainsUnexpandedParameterPack, ResultIndex);
18851885
}
18861886

1887-
static PredefinedExpr::IdentKind getPredefinedExprKind(tok::TokenKind Kind) {
1887+
static PredefinedIdentKind getPredefinedExprKind(tok::TokenKind Kind) {
18881888
switch (Kind) {
18891889
default:
18901890
llvm_unreachable("unexpected TokenKind");
18911891
case tok::kw___func__:
1892-
return PredefinedExpr::Func; // [C99 6.4.2.2]
1892+
return PredefinedIdentKind::Func; // [C99 6.4.2.2]
18931893
case tok::kw___FUNCTION__:
1894-
return PredefinedExpr::Function;
1894+
return PredefinedIdentKind::Function;
18951895
case tok::kw___FUNCDNAME__:
1896-
return PredefinedExpr::FuncDName; // [MS]
1896+
return PredefinedIdentKind::FuncDName; // [MS]
18971897
case tok::kw___FUNCSIG__:
1898-
return PredefinedExpr::FuncSig; // [MS]
1898+
return PredefinedIdentKind::FuncSig; // [MS]
18991899
case tok::kw_L__FUNCTION__:
1900-
return PredefinedExpr::LFunction; // [MS]
1900+
return PredefinedIdentKind::LFunction; // [MS]
19011901
case tok::kw_L__FUNCSIG__:
1902-
return PredefinedExpr::LFuncSig; // [MS]
1902+
return PredefinedIdentKind::LFuncSig; // [MS]
19031903
case tok::kw___PRETTY_FUNCTION__:
1904-
return PredefinedExpr::PrettyFunction; // [GNU]
1904+
return PredefinedIdentKind::PrettyFunction; // [GNU]
19051905
}
19061906
}
19071907

@@ -3716,7 +3716,7 @@ static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source,
37163716
}
37173717

37183718
ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc,
3719-
PredefinedExpr::IdentKind IK) {
3719+
PredefinedIdentKind IK) {
37203720
Decl *currentDecl = getPredefinedExprDecl(CurContext);
37213721
if (!currentDecl) {
37223722
Diag(Loc, diag::ext_predef_outside_function);
@@ -3734,7 +3734,8 @@ ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc,
37343734
unsigned Length = Str.length();
37353735

37363736
llvm::APInt LengthI(32, Length + 1);
3737-
if (IK == PredefinedExpr::LFunction || IK == PredefinedExpr::LFuncSig) {
3737+
if (IK == PredefinedIdentKind::LFunction ||
3738+
IK == PredefinedIdentKind::LFuncSig) {
37383739
ResTy =
37393740
Context.adjustStringLiteralBaseType(Context.WideCharTy.withConst());
37403741
SmallString<32> RawChars;

clang/lib/Sema/TreeTransform.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,8 +2620,7 @@ class TreeTransform {
26202620
///
26212621
/// By default, performs semantic analysis to build the new expression.
26222622
/// Subclasses may override this routine to provide different behavior.
2623-
ExprResult RebuildPredefinedExpr(SourceLocation Loc,
2624-
PredefinedExpr::IdentKind IK) {
2623+
ExprResult RebuildPredefinedExpr(SourceLocation Loc, PredefinedIdentKind IK) {
26252624
return getSema().BuildPredefinedExpr(Loc, IK);
26262625
}
26272626

clang/lib/Serialization/ASTWriterStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
595595

596596
bool HasFunctionName = E->getFunctionName() != nullptr;
597597
Record.push_back(HasFunctionName);
598-
Record.push_back(E->getIdentKind()); // FIXME: stable encoding
598+
Record.push_back(
599+
llvm::to_underlying(E->getIdentKind())); // FIXME: stable encoding
599600
Record.push_back(E->isTransparent());
600601
Record.AddSourceLocation(E->getLocation());
601602
if (HasFunctionName)

0 commit comments

Comments
 (0)