[clang] 6e35db0 - [clang][NFC] Refactor `PredefinedExpr::IdentKind`
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 5 04:39:13 PST 2023
Author: Vlad Serebrennikov
Date: 2023-11-05T15:39:05+03:00
New Revision: 6e35db0694b2cb80ffe0c7edfed3090f9ede4805
URL: https://github.com/llvm/llvm-project/commit/6e35db0694b2cb80ffe0c7edfed3090f9ede4805
DIFF: https://github.com/llvm/llvm-project/commit/6e35db0694b2cb80ffe0c7edfed3090f9ede4805.diff
LOG: [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.
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
clang/include/clang/AST/Expr.h
clang/include/clang/Sema/Sema.h
clang/lib/AST/Expr.cpp
clang/lib/AST/StmtProfile.cpp
clang/lib/AST/VTableBuilder.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/TreeTransform.h
clang/lib/Serialization/ASTWriterStmt.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
index a01b7f5a4ee6e68..5260a8b4ecb0bad 100644
--- a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
@@ -82,8 +82,8 @@ void LambdaFunctionNameCheck::registerPPCallbacks(
void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>("E");
- if (E->getIdentKind() != PredefinedExpr::Func &&
- E->getIdentKind() != PredefinedExpr::Function) {
+ if (E->getIdentKind() != PredefinedIdentKind::Func &&
+ E->getIdentKind() != PredefinedIdentKind::Function) {
// We don't care about other PredefinedExprs.
return;
}
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 7e8e183afc5d3cf..c2691f80dc54a7e 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1999,6 +1999,19 @@ class StringLiteral final
}
};
+enum class PredefinedIdentKind {
+ Func,
+ Function,
+ LFunction, // Same as Function, but as wide string.
+ FuncDName,
+ FuncSig,
+ LFuncSig, // Same as FuncSig, but as wide string
+ PrettyFunction,
+ /// The same as PrettyFunction, except that the
+ /// 'virtual' keyword is omitted for virtual member functions.
+ PrettyFunctionNoVirtual
+};
+
/// [C99 6.4.2.2] - A predefined identifier such as __func__.
class PredefinedExpr final
: public Expr,
@@ -2010,22 +2023,7 @@ class PredefinedExpr final
// "Stmt *" for the predefined identifier. It is present if and only if
// hasFunctionName() is true and is always a "StringLiteral *".
-public:
- enum IdentKind {
- Func,
- Function,
- LFunction, // Same as Function, but as wide string.
- FuncDName,
- FuncSig,
- LFuncSig, // Same as FuncSig, but as wide string
- PrettyFunction,
- /// The same as PrettyFunction, except that the
- /// 'virtual' keyword is omitted for virtual member functions.
- PrettyFunctionNoVirtual
- };
-
-private:
- PredefinedExpr(SourceLocation L, QualType FNTy, IdentKind IK,
+ PredefinedExpr(SourceLocation L, QualType FNTy, PredefinedIdentKind IK,
bool IsTransparent, StringLiteral *SL);
explicit PredefinedExpr(EmptyShell Empty, bool HasFunctionName);
@@ -2045,15 +2043,15 @@ class PredefinedExpr final
/// If IsTransparent, the PredefinedExpr is transparently handled as a
/// StringLiteral.
static PredefinedExpr *Create(const ASTContext &Ctx, SourceLocation L,
- QualType FNTy, IdentKind IK, bool IsTransparent,
- StringLiteral *SL);
+ QualType FNTy, PredefinedIdentKind IK,
+ bool IsTransparent, StringLiteral *SL);
/// Create an empty PredefinedExpr.
static PredefinedExpr *CreateEmpty(const ASTContext &Ctx,
bool HasFunctionName);
- IdentKind getIdentKind() const {
- return static_cast<IdentKind>(PredefinedExprBits.Kind);
+ PredefinedIdentKind getIdentKind() const {
+ return static_cast<PredefinedIdentKind>(PredefinedExprBits.Kind);
}
bool isTransparent() const { return PredefinedExprBits.IsTransparent; }
@@ -2073,12 +2071,13 @@ class PredefinedExpr final
: nullptr;
}
- static StringRef getIdentKindName(IdentKind IK);
+ static StringRef getIdentKindName(PredefinedIdentKind IK);
StringRef getIdentKindName() const {
return getIdentKindName(getIdentKind());
}
- static std::string ComputeName(IdentKind IK, const Decl *CurrentDecl);
+ static std::string ComputeName(PredefinedIdentKind IK,
+ const Decl *CurrentDecl);
SourceLocation getBeginLoc() const { return getLocation(); }
SourceLocation getEndLoc() const { return getLocation(); }
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8a35cbe3e9502b6..24f209e95736a67 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5723,8 +5723,7 @@ class Sema final {
// __FUNCTION__) are replaced (expanded) with string-literal Tokens.
std::vector<Token> ExpandFunctionLocalPredefinedMacros(ArrayRef<Token> Toks);
- ExprResult BuildPredefinedExpr(SourceLocation Loc,
- PredefinedExpr::IdentKind IK);
+ ExprResult BuildPredefinedExpr(SourceLocation Loc, PredefinedIdentKind IK);
ExprResult ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind);
ExprResult ActOnIntegerConstant(SourceLocation Loc, uint64_t Val);
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index eccff0699d21a2a..74a9c49a795b4fc 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -604,10 +604,11 @@ std::string SYCLUniqueStableNameExpr::ComputeName(ASTContext &Context,
return Out.str();
}
-PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentKind IK,
- bool IsTransparent, StringLiteral *SL)
+PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy,
+ PredefinedIdentKind IK, bool IsTransparent,
+ StringLiteral *SL)
: Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary) {
- PredefinedExprBits.Kind = IK;
+ PredefinedExprBits.Kind = llvm::to_underlying(IK);
assert((getIdentKind() == IK) &&
"IdentKind do not fit in PredefinedExprBitfields!");
bool HasFunctionName = SL != nullptr;
@@ -625,7 +626,7 @@ PredefinedExpr::PredefinedExpr(EmptyShell Empty, bool HasFunctionName)
}
PredefinedExpr *PredefinedExpr::Create(const ASTContext &Ctx, SourceLocation L,
- QualType FNTy, IdentKind IK,
+ QualType FNTy, PredefinedIdentKind IK,
bool IsTransparent, StringLiteral *SL) {
bool HasFunctionName = SL != nullptr;
void *Mem = Ctx.Allocate(totalSizeToAlloc<Stmt *>(HasFunctionName),
@@ -640,23 +641,23 @@ PredefinedExpr *PredefinedExpr::CreateEmpty(const ASTContext &Ctx,
return new (Mem) PredefinedExpr(EmptyShell(), HasFunctionName);
}
-StringRef PredefinedExpr::getIdentKindName(PredefinedExpr::IdentKind IK) {
+StringRef PredefinedExpr::getIdentKindName(PredefinedIdentKind IK) {
switch (IK) {
- case Func:
+ case PredefinedIdentKind::Func:
return "__func__";
- case Function:
+ case PredefinedIdentKind::Function:
return "__FUNCTION__";
- case FuncDName:
+ case PredefinedIdentKind::FuncDName:
return "__FUNCDNAME__";
- case LFunction:
+ case PredefinedIdentKind::LFunction:
return "L__FUNCTION__";
- case PrettyFunction:
+ case PredefinedIdentKind::PrettyFunction:
return "__PRETTY_FUNCTION__";
- case FuncSig:
+ case PredefinedIdentKind::FuncSig:
return "__FUNCSIG__";
- case LFuncSig:
+ case PredefinedIdentKind::LFuncSig:
return "L__FUNCSIG__";
- case PrettyFunctionNoVirtual:
+ case PredefinedIdentKind::PrettyFunctionNoVirtual:
break;
}
llvm_unreachable("Unknown ident kind for PredefinedExpr");
@@ -664,10 +665,11 @@ StringRef PredefinedExpr::getIdentKindName(PredefinedExpr::IdentKind IK) {
// FIXME: Maybe this should use DeclPrinter with a special "print predefined
// expr" policy instead.
-std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
+std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
+ const Decl *CurrentDecl) {
ASTContext &Context = CurrentDecl->getASTContext();
- if (IK == PredefinedExpr::FuncDName) {
+ if (IK == PredefinedIdentKind::FuncDName) {
if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
std::unique_ptr<MangleContext> MC;
MC.reset(Context.createMangleContext());
@@ -712,15 +714,17 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
return std::string(Out.str());
}
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
- if (IK != PrettyFunction && IK != PrettyFunctionNoVirtual &&
- IK != FuncSig && IK != LFuncSig)
+ if (IK != PredefinedIdentKind::PrettyFunction &&
+ IK != PredefinedIdentKind::PrettyFunctionNoVirtual &&
+ IK != PredefinedIdentKind::FuncSig &&
+ IK != PredefinedIdentKind::LFuncSig)
return FD->getNameAsString();
SmallString<256> Name;
llvm::raw_svector_ostream Out(Name);
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
- if (MD->isVirtual() && IK != PrettyFunctionNoVirtual)
+ if (MD->isVirtual() && IK != PredefinedIdentKind::PrettyFunctionNoVirtual)
Out << "virtual ";
if (MD->isStatic())
Out << "static ";
@@ -752,7 +756,8 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
if (FD->hasWrittenPrototype())
FT = dyn_cast<FunctionProtoType>(AFT);
- if (IK == FuncSig || IK == LFuncSig) {
+ if (IK == PredefinedIdentKind::FuncSig ||
+ IK == PredefinedIdentKind::LFuncSig) {
switch (AFT->getCallConv()) {
case CC_C: POut << "__cdecl "; break;
case CC_X86StdCall: POut << "__stdcall "; break;
@@ -777,7 +782,8 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
if (FT->isVariadic()) {
if (FD->getNumParams()) POut << ", ";
POut << "...";
- } else if ((IK == FuncSig || IK == LFuncSig ||
+ } else if ((IK == PredefinedIdentKind::FuncSig ||
+ IK == PredefinedIdentKind::LFuncSig ||
!Context.getLangOpts().CPlusPlus) &&
!Decl->getNumParams()) {
POut << "void";
@@ -902,7 +908,8 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
return std::string(Name);
}
- if (isa<TranslationUnitDecl>(CurrentDecl) && IK == PrettyFunction) {
+ if (isa<TranslationUnitDecl>(CurrentDecl) &&
+ IK == PredefinedIdentKind::PrettyFunction) {
// __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
return "top level";
}
@@ -2282,8 +2289,8 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
case SourceLocIdentKind::FuncSig: {
const auto *CurDecl = dyn_cast<Decl>(Context);
const auto Kind = getIdentKind() == SourceLocIdentKind::Function
- ? PredefinedExpr::Function
- : PredefinedExpr::FuncSig;
+ ? PredefinedIdentKind::Function
+ : PredefinedIdentKind::FuncSig;
return MakeStringLiteral(
CurDecl ? PredefinedExpr::ComputeName(Kind, CurDecl) : std::string(""));
}
@@ -2317,7 +2324,7 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
Value.getStructField(F->getFieldIndex()) = MakeStringLiteral(
CurDecl && !isa<TranslationUnitDecl>(CurDecl)
? StringRef(PredefinedExpr::ComputeName(
- PredefinedExpr::PrettyFunction, CurDecl))
+ PredefinedIdentKind::PrettyFunction, CurDecl))
: "");
} else if (Name == "_M_line") {
llvm::APSInt IntVal = Ctx.MakeIntValue(PLoc.getLine(), F->getType());
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index ce3381781ff68fa..6510fa369d78eb6 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -1328,7 +1328,7 @@ void StmtProfiler::VisitSYCLUniqueStableNameExpr(
void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
VisitExpr(S);
- ID.AddInteger(S->getIdentKind());
+ ID.AddInteger(llvm::to_underlying(S->getIdentKind()));
}
void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
diff --git a/clang/lib/AST/VTableBuilder.cpp b/clang/lib/AST/VTableBuilder.cpp
index cce0a507e8077d8..69861c65f6e3ac9 100644
--- a/clang/lib/AST/VTableBuilder.cpp
+++ b/clang/lib/AST/VTableBuilder.cpp
@@ -1956,9 +1956,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
case VTableComponent::CK_FunctionPointer: {
const CXXMethodDecl *MD = Component.getFunctionDecl();
- std::string Str =
- PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
- MD);
+ std::string Str = PredefinedExpr::ComputeName(
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
Out << Str;
if (MD->isPure())
Out << " [pure]";
@@ -2036,9 +2035,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
case VTableComponent::CK_UnusedFunctionPointer: {
const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
- std::string Str =
- PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
- MD);
+ std::string Str = PredefinedExpr::ComputeName(
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
Out << "[unused] " << Str;
if (MD->isPure())
Out << " [pure]";
@@ -2115,9 +2113,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
for (const auto &I : Thunks) {
const CXXMethodDecl *MD = I.first;
- std::string MethodName =
- PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
- MD);
+ std::string MethodName = PredefinedExpr::ComputeName(
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
}
@@ -2181,9 +2178,8 @@ void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
continue;
MD = MD->getCanonicalDecl();
- std::string MethodName =
- PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
- MD);
+ std::string MethodName = PredefinedExpr::ComputeName(
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
GlobalDecl GD(DD, Dtor_Complete);
@@ -3177,7 +3173,7 @@ void VFTableBuilder::dumpLayout(raw_ostream &Out) {
// FIXME: Figure out how to print the real thunk type, since they can
//
diff er in the return type.
std::string Str = PredefinedExpr::ComputeName(
- PredefinedExpr::PrettyFunctionNoVirtual, MD);
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
Out << Str;
if (MD->isPure())
Out << " [pure]";
@@ -3232,7 +3228,7 @@ void VFTableBuilder::dumpLayout(raw_ostream &Out) {
for (const auto &I : Thunks) {
const CXXMethodDecl *MD = I.first;
std::string MethodName = PredefinedExpr::ComputeName(
- PredefinedExpr::PrettyFunctionNoVirtual, MD);
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
}
@@ -3664,7 +3660,7 @@ void MicrosoftVTableContext::dumpMethodLocations(
assert(hasVtableSlot(MD));
std::string MethodName = PredefinedExpr::ComputeName(
- PredefinedExpr::PrettyFunctionNoVirtual, MD);
+ PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
if (isa<CXXDestructorDecl>(MD)) {
IndicesMap[I.second] = MethodName + " [scalar deleting]";
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index b9ff45ecac8fdbf..432e4285e8a01d6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1884,24 +1884,24 @@ ExprResult Sema::CreateGenericSelectionExpr(
ContainsUnexpandedParameterPack, ResultIndex);
}
-static PredefinedExpr::IdentKind getPredefinedExprKind(tok::TokenKind Kind) {
+static PredefinedIdentKind getPredefinedExprKind(tok::TokenKind Kind) {
switch (Kind) {
default:
llvm_unreachable("unexpected TokenKind");
case tok::kw___func__:
- return PredefinedExpr::Func; // [C99 6.4.2.2]
+ return PredefinedIdentKind::Func; // [C99 6.4.2.2]
case tok::kw___FUNCTION__:
- return PredefinedExpr::Function;
+ return PredefinedIdentKind::Function;
case tok::kw___FUNCDNAME__:
- return PredefinedExpr::FuncDName; // [MS]
+ return PredefinedIdentKind::FuncDName; // [MS]
case tok::kw___FUNCSIG__:
- return PredefinedExpr::FuncSig; // [MS]
+ return PredefinedIdentKind::FuncSig; // [MS]
case tok::kw_L__FUNCTION__:
- return PredefinedExpr::LFunction; // [MS]
+ return PredefinedIdentKind::LFunction; // [MS]
case tok::kw_L__FUNCSIG__:
- return PredefinedExpr::LFuncSig; // [MS]
+ return PredefinedIdentKind::LFuncSig; // [MS]
case tok::kw___PRETTY_FUNCTION__:
- return PredefinedExpr::PrettyFunction; // [GNU]
+ return PredefinedIdentKind::PrettyFunction; // [GNU]
}
}
@@ -3716,7 +3716,7 @@ static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source,
}
ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc,
- PredefinedExpr::IdentKind IK) {
+ PredefinedIdentKind IK) {
Decl *currentDecl = getPredefinedExprDecl(CurContext);
if (!currentDecl) {
Diag(Loc, diag::ext_predef_outside_function);
@@ -3734,7 +3734,8 @@ ExprResult Sema::BuildPredefinedExpr(SourceLocation Loc,
unsigned Length = Str.length();
llvm::APInt LengthI(32, Length + 1);
- if (IK == PredefinedExpr::LFunction || IK == PredefinedExpr::LFuncSig) {
+ if (IK == PredefinedIdentKind::LFunction ||
+ IK == PredefinedIdentKind::LFuncSig) {
ResTy =
Context.adjustStringLiteralBaseType(Context.WideCharTy.withConst());
SmallString<32> RawChars;
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 47a90321af68dc1..279bc29815980ec 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -2620,8 +2620,7 @@ class TreeTransform {
///
/// By default, performs semantic analysis to build the new expression.
/// Subclasses may override this routine to provide
diff erent behavior.
- ExprResult RebuildPredefinedExpr(SourceLocation Loc,
- PredefinedExpr::IdentKind IK) {
+ ExprResult RebuildPredefinedExpr(SourceLocation Loc, PredefinedIdentKind IK) {
return getSema().BuildPredefinedExpr(Loc, IK);
}
diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp
index 04f4ea9ce1f523d..78b1df3748ad8ae 100644
--- a/clang/lib/Serialization/ASTWriterStmt.cpp
+++ b/clang/lib/Serialization/ASTWriterStmt.cpp
@@ -595,7 +595,8 @@ void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
bool HasFunctionName = E->getFunctionName() != nullptr;
Record.push_back(HasFunctionName);
- Record.push_back(E->getIdentKind()); // FIXME: stable encoding
+ Record.push_back(
+ llvm::to_underlying(E->getIdentKind())); // FIXME: stable encoding
Record.push_back(E->isTransparent());
Record.AddSourceLocation(E->getLocation());
if (HasFunctionName)
More information about the cfe-commits
mailing list