implement pretty printing of offsetof

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41615 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-08-30 17:59:59 +00:00
Родитель d3fb6adc88
Коммит 704fe35d13
3 изменённых файлов: 36 добавлений и 2 удалений

Просмотреть файл

@ -64,6 +64,9 @@ namespace {
return OS;
}
bool PrintOffsetOfDesignator(Expr *E);
void VisitUnaryOffsetOf(UnaryOperator *Node);
void VisitStmt(Stmt *Node);
#define STMT(N, CLASS, PARENT) \
void Visit##CLASS(CLASS *Node);
@ -429,8 +432,33 @@ void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
if (Node->isPostfix())
OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
}
bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
if (isa<CompoundLiteralExpr>(E)) {
// Base case, print the type and comma.
OS << E->getType().getAsString() << ", ";
return true;
} else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
PrintOffsetOfDesignator(ASE->getLHS());
OS << "[";
PrintExpr(ASE->getRHS());
OS << "]";
return false;
} else {
MemberExpr *ME = cast<MemberExpr>(E);
bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
return false;
}
}
void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
OS << "__builtin_offsetof(";
PrintOffsetOfDesignator(Node->getSubExpr());
OS << ")";
}
void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
OS << Node->getArgumentType().getAsString() << ")";

Просмотреть файл

@ -1748,6 +1748,8 @@ Sema::ExprResult Sema::ParseBuiltinOffsetOf(SourceLocation BuiltinLoc,
Res->getType().getAsString());
}
// FIXME: C++: Verify that operator[] isn't overloaded.
// C99 6.5.2.1p1
Expr *Idx = static_cast<Expr*>(OC.U.E);
if (!Idx->getType()->isIntegerType())
@ -1772,6 +1774,10 @@ Sema::ExprResult Sema::ParseBuiltinOffsetOf(SourceLocation BuiltinLoc,
return Diag(BuiltinLoc, diag::err_typecheck_no_member,
OC.U.IdentInfo->getName(),
SourceRange(OC.LocStart, OC.LocEnd));
// FIXME: C++: Verify that MemberDecl isn't a static field.
// FIXME: Verify that MemberDecl isn't a bitfield.
Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
}

Просмотреть файл

@ -95,7 +95,7 @@ public:
case UnaryOperator::Real: DISPATCH(UnaryReal, UnaryOperator);
case UnaryOperator::Imag: DISPATCH(UnaryImag, UnaryOperator);
case UnaryOperator::Extension: DISPATCH(UnaryExtension, UnaryOperator);
case UnaryOperator::OffsetOf: DISPATCH(UnaryExtension, UnaryOperator);
case UnaryOperator::OffsetOf: DISPATCH(UnaryOffsetOf, UnaryOperator);
}
}