Skip to content

Commit

Permalink
[clang][NFC] Refactor SourceLocExpr::IdentKind
Browse files Browse the repository at this point in the history
This patch converts `SourceLocExpr::IdentKind` into a scoped enum at namespace scope, making it eligible to be forward-declared. This is needed by `preferred_type` annotations on bit-fields.
  • Loading branch information
Endilll committed Nov 4, 2023
1 parent 83888a5 commit 99e7e7a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 67 deletions.
44 changes: 22 additions & 22 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4735,6 +4735,16 @@ class VAArgExpr : public Expr {
}
};

enum class SourceLocIdentKind {
Function,
FuncSig,
File,
FileName,
Line,
Column,
SourceLocStruct
};

/// Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(),
/// __builtin_FUNCTION(), __builtin_FUNCSIG(), __builtin_FILE(),
/// __builtin_FILE_NAME() or __builtin_source_location().
Expand All @@ -4743,19 +4753,9 @@ class SourceLocExpr final : public Expr {
DeclContext *ParentContext;

public:
enum IdentKind {
Function,
FuncSig,
File,
FileName,
Line,
Column,
SourceLocStruct
};

SourceLocExpr(const ASTContext &Ctx, IdentKind Type, QualType ResultTy,
SourceLocation BLoc, SourceLocation RParenLoc,
DeclContext *Context);
SourceLocExpr(const ASTContext &Ctx, SourceLocIdentKind Type,
QualType ResultTy, SourceLocation BLoc,
SourceLocation RParenLoc, DeclContext *Context);

/// Build an empty call expression.
explicit SourceLocExpr(EmptyShell Empty) : Expr(SourceLocExprClass, Empty) {}
Expand All @@ -4768,20 +4768,20 @@ class SourceLocExpr final : public Expr {
/// Return a string representing the name of the specific builtin function.
StringRef getBuiltinStr() const;

IdentKind getIdentKind() const {
return static_cast<IdentKind>(SourceLocExprBits.Kind);
SourceLocIdentKind getIdentKind() const {
return static_cast<SourceLocIdentKind>(SourceLocExprBits.Kind);
}

bool isIntType() const {
switch (getIdentKind()) {
case File:
case FileName:
case Function:
case FuncSig:
case SourceLocStruct:
case SourceLocIdentKind::File:
case SourceLocIdentKind::FileName:
case SourceLocIdentKind::Function:
case SourceLocIdentKind::FuncSig:
case SourceLocIdentKind::SourceLocStruct:
return false;
case Line:
case Column:
case SourceLocIdentKind::Line:
case SourceLocIdentKind::Column:
return true;
}
llvm_unreachable("unknown source location expression kind");
Expand Down
7 changes: 3 additions & 4 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -6086,14 +6086,13 @@ class Sema final {

// __builtin_LINE(), __builtin_FUNCTION(), __builtin_FUNCSIG(),
// __builtin_FILE(), __builtin_COLUMN(), __builtin_source_location()
ExprResult ActOnSourceLocExpr(SourceLocExpr::IdentKind Kind,
ExprResult ActOnSourceLocExpr(SourceLocIdentKind Kind,
SourceLocation BuiltinLoc,
SourceLocation RPLoc);

// Build a potentially resolved SourceLocExpr.
ExprResult BuildSourceLocExpr(SourceLocExpr::IdentKind Kind,
QualType ResultTy, SourceLocation BuiltinLoc,
SourceLocation RPLoc,
ExprResult BuildSourceLocExpr(SourceLocIdentKind Kind, QualType ResultTy,
SourceLocation BuiltinLoc, SourceLocation RPLoc,
DeclContext *ParentContext);

// __null
Expand Down
34 changes: 17 additions & 17 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2196,31 +2196,31 @@ bool BinaryOperator::isNullPointerArithmeticExtension(ASTContext &Ctx,
return true;
}

SourceLocExpr::SourceLocExpr(const ASTContext &Ctx, IdentKind Kind,
SourceLocExpr::SourceLocExpr(const ASTContext &Ctx, SourceLocIdentKind Kind,
QualType ResultTy, SourceLocation BLoc,
SourceLocation RParenLoc,
DeclContext *ParentContext)
: Expr(SourceLocExprClass, ResultTy, VK_PRValue, OK_Ordinary),
BuiltinLoc(BLoc), RParenLoc(RParenLoc), ParentContext(ParentContext) {
SourceLocExprBits.Kind = Kind;
SourceLocExprBits.Kind = llvm::to_underlying(Kind);
setDependence(ExprDependence::None);
}

StringRef SourceLocExpr::getBuiltinStr() const {
switch (getIdentKind()) {
case File:
case SourceLocIdentKind::File:
return "__builtin_FILE";
case FileName:
case SourceLocIdentKind::FileName:
return "__builtin_FILE_NAME";
case Function:
case SourceLocIdentKind::Function:
return "__builtin_FUNCTION";
case FuncSig:
case SourceLocIdentKind::FuncSig:
return "__builtin_FUNCSIG";
case Line:
case SourceLocIdentKind::Line:
return "__builtin_LINE";
case Column:
case SourceLocIdentKind::Column:
return "__builtin_COLUMN";
case SourceLocStruct:
case SourceLocIdentKind::SourceLocStruct:
return "__builtin_source_location";
}
llvm_unreachable("unexpected IdentKind!");
Expand Down Expand Up @@ -2255,34 +2255,34 @@ APValue SourceLocExpr::EvaluateInContext(const ASTContext &Ctx,
};

switch (getIdentKind()) {
case SourceLocExpr::FileName: {
case SourceLocIdentKind::FileName: {
// __builtin_FILE_NAME() is a Clang-specific extension that expands to the
// the last part of __builtin_FILE().
SmallString<256> FileName;
clang::Preprocessor::processPathToFileName(
FileName, PLoc, Ctx.getLangOpts(), Ctx.getTargetInfo());
return MakeStringLiteral(FileName);
}
case SourceLocExpr::File: {
case SourceLocIdentKind::File: {
SmallString<256> Path(PLoc.getFilename());
clang::Preprocessor::processPathForFileMacro(Path, Ctx.getLangOpts(),
Ctx.getTargetInfo());
return MakeStringLiteral(Path);
}
case SourceLocExpr::Function:
case SourceLocExpr::FuncSig: {
case SourceLocIdentKind::Function:
case SourceLocIdentKind::FuncSig: {
const auto *CurDecl = dyn_cast<Decl>(Context);
const auto Kind = getIdentKind() == SourceLocExpr::Function
const auto Kind = getIdentKind() == SourceLocIdentKind::Function
? PredefinedExpr::Function
: PredefinedExpr::FuncSig;
return MakeStringLiteral(
CurDecl ? PredefinedExpr::ComputeName(Kind, CurDecl) : std::string(""));
}
case SourceLocExpr::Line:
case SourceLocIdentKind::Line:
return APValue(Ctx.MakeIntValue(PLoc.getLine(), Ctx.UnsignedIntTy));
case SourceLocExpr::Column:
case SourceLocIdentKind::Column:
return APValue(Ctx.MakeIntValue(PLoc.getColumn(), Ctx.UnsignedIntTy));
case SourceLocExpr::SourceLocStruct: {
case SourceLocIdentKind::SourceLocStruct: {
// Fill in a std::source_location::__impl structure, by creating an
// artificial file-scoped CompoundLiteralExpr, and returning a pointer to
// that.
Expand Down
16 changes: 8 additions & 8 deletions clang/lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2820,22 +2820,22 @@ ExprResult Parser::ParseBuiltinPrimaryExpression() {
SkipUntil(tok::r_paren, StopAtSemi);
return ExprError();
}
SourceLocExpr::IdentKind Kind = [&] {
SourceLocIdentKind Kind = [&] {
switch (T) {
case tok::kw___builtin_FILE:
return SourceLocExpr::File;
return SourceLocIdentKind::File;
case tok::kw___builtin_FILE_NAME:
return SourceLocExpr::FileName;
return SourceLocIdentKind::FileName;
case tok::kw___builtin_FUNCTION:
return SourceLocExpr::Function;
return SourceLocIdentKind::Function;
case tok::kw___builtin_FUNCSIG:
return SourceLocExpr::FuncSig;
return SourceLocIdentKind::FuncSig;
case tok::kw___builtin_LINE:
return SourceLocExpr::Line;
return SourceLocIdentKind::Line;
case tok::kw___builtin_COLUMN:
return SourceLocExpr::Column;
return SourceLocIdentKind::Column;
case tok::kw___builtin_source_location:
return SourceLocExpr::SourceLocStruct;
return SourceLocIdentKind::SourceLocStruct;
default:
llvm_unreachable("invalid keyword");
}
Expand Down
19 changes: 9 additions & 10 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17542,25 +17542,25 @@ static CXXRecordDecl *LookupStdSourceLocationImpl(Sema &S, SourceLocation Loc) {
return ImplDecl;
}

ExprResult Sema::ActOnSourceLocExpr(SourceLocExpr::IdentKind Kind,
ExprResult Sema::ActOnSourceLocExpr(SourceLocIdentKind Kind,
SourceLocation BuiltinLoc,
SourceLocation RPLoc) {
QualType ResultTy;
switch (Kind) {
case SourceLocExpr::File:
case SourceLocExpr::FileName:
case SourceLocExpr::Function:
case SourceLocExpr::FuncSig: {
case SourceLocIdentKind::File:
case SourceLocIdentKind::FileName:
case SourceLocIdentKind::Function:
case SourceLocIdentKind::FuncSig: {
QualType ArrTy = Context.getStringLiteralArrayType(Context.CharTy, 0);
ResultTy =
Context.getPointerType(ArrTy->getAsArrayTypeUnsafe()->getElementType());
break;
}
case SourceLocExpr::Line:
case SourceLocExpr::Column:
case SourceLocIdentKind::Line:
case SourceLocIdentKind::Column:
ResultTy = Context.UnsignedIntTy;
break;
case SourceLocExpr::SourceLocStruct:
case SourceLocIdentKind::SourceLocStruct:
if (!StdSourceLocationImplDecl) {
StdSourceLocationImplDecl =
LookupStdSourceLocationImpl(*this, BuiltinLoc);
Expand All @@ -17575,8 +17575,7 @@ ExprResult Sema::ActOnSourceLocExpr(SourceLocExpr::IdentKind Kind,
return BuildSourceLocExpr(Kind, ResultTy, BuiltinLoc, RPLoc, CurContext);
}

ExprResult Sema::BuildSourceLocExpr(SourceLocExpr::IdentKind Kind,
QualType ResultTy,
ExprResult Sema::BuildSourceLocExpr(SourceLocIdentKind Kind, QualType ResultTy,
SourceLocation BuiltinLoc,
SourceLocation RPLoc,
DeclContext *ParentContext) {
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -3587,8 +3587,8 @@ class TreeTransform {
///
/// By default, performs semantic analysis to build the new expression.
/// Subclasses may override this routine to provide different behavior.
ExprResult RebuildSourceLocExpr(SourceLocExpr::IdentKind Kind,
QualType ResultTy, SourceLocation BuiltinLoc,
ExprResult RebuildSourceLocExpr(SourceLocIdentKind Kind, QualType ResultTy,
SourceLocation BuiltinLoc,
SourceLocation RPLoc,
DeclContext *ParentContext) {
return getSema().BuildSourceLocExpr(Kind, ResultTy, BuiltinLoc, RPLoc,
Expand Down Expand Up @@ -12115,7 +12115,7 @@ TreeTransform<Derived>::TransformCXXMemberCallExpr(CXXMemberCallExpr *E) {

template <typename Derived>
ExprResult TreeTransform<Derived>::TransformSourceLocExpr(SourceLocExpr *E) {
bool NeedRebuildFunc = E->getIdentKind() == SourceLocExpr::Function &&
bool NeedRebuildFunc = E->getIdentKind() == SourceLocIdentKind::Function &&
getSema().CurContext != E->getParentContext();

if (!getDerived().AlwaysRebuild() && !NeedRebuildFunc)
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,7 @@ void ASTStmtReader::VisitSourceLocExpr(SourceLocExpr *E) {
E->ParentContext = readDeclAs<DeclContext>();
E->BuiltinLoc = readSourceLocation();
E->RParenLoc = readSourceLocation();
E->SourceLocExprBits.Kind =
static_cast<SourceLocExpr::IdentKind>(Record.readInt());
E->SourceLocExprBits.Kind = Record.readInt();
}

void ASTStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTWriterStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ void ASTStmtWriter::VisitSourceLocExpr(SourceLocExpr *E) {
Record.AddDeclRef(cast_or_null<Decl>(E->getParentContext()));
Record.AddSourceLocation(E->getBeginLoc());
Record.AddSourceLocation(E->getEndLoc());
Record.push_back(E->getIdentKind());
Record.push_back(llvm::to_underlying(E->getIdentKind()));
Code = serialization::EXPR_SOURCE_LOC;
}

Expand Down

0 comments on commit 99e7e7a

Please sign in to comment.