diff options
Diffstat (limited to 'lib/AST/StmtPrinter.cpp')
-rw-r--r-- | lib/AST/StmtPrinter.cpp | 102 |
1 files changed, 71 insertions, 31 deletions
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index ae726e387107..46802d765e1f 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -1,9 +1,8 @@ //===- StmtPrinter.cpp - Printing implementation for Stmt ASTs ------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -37,6 +36,7 @@ #include "clang/Basic/CharInfo.h" #include "clang/Basic/ExpressionTraits.h" #include "clang/Basic/IdentifierTable.h" +#include "clang/Basic/JsonSupport.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/Lambda.h" #include "clang/Basic/OpenMPKinds.h" @@ -414,12 +414,15 @@ void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { if (Node->isVolatile()) OS << "volatile "; + if (Node->isAsmGoto()) + OS << "goto "; + OS << "("; VisitStringLiteral(Node->getAsmString()); // Outputs if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || - Node->getNumClobbers() != 0) + Node->getNumClobbers() != 0 || Node->getNumLabels() != 0) OS << " : "; for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { @@ -439,7 +442,8 @@ void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { } // Inputs - if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) + if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0 || + Node->getNumLabels() != 0) OS << " : "; for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { @@ -459,7 +463,7 @@ void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { } // Clobbers - if (Node->getNumClobbers() != 0) + if (Node->getNumClobbers() != 0 || Node->getNumLabels()) OS << " : "; for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { @@ -469,6 +473,16 @@ void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { VisitStringLiteral(Node->getClobberStringLiteral(i)); } + // Labels + if (Node->getNumLabels() != 0) + OS << " : "; + + for (unsigned i = 0, e = Node->getNumLabels(); i != e; ++i) { + if (i != 0) + OS << ", "; + OS << Node->getLabelName(i); + } + OS << ");"; if (Policy.IncludeNewlines) OS << NL; } @@ -906,6 +920,10 @@ void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective( // Expr printing methods. //===----------------------------------------------------------------------===// +void StmtPrinter::VisitSourceLocExpr(SourceLocExpr *Node) { + OS << Node->getBuiltinStr() << "()"; +} + void StmtPrinter::VisitConstantExpr(ConstantExpr *Node) { PrintExpr(Node->getSubExpr()); } @@ -1262,15 +1280,15 @@ void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { OS << "_Generic("; PrintExpr(Node->getControllingExpr()); - for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { + for (const GenericSelectionExpr::Association &Assoc : Node->associations()) { OS << ", "; - QualType T = Node->getAssocType(i); + QualType T = Assoc.getType(); if (T.isNull()) OS << "default"; else T.print(OS, Policy); OS << ": "; - PrintExpr(Node->getAssocExpr(i)); + PrintExpr(Assoc.getAssociationExpr()); } OS << ")"; } @@ -1604,21 +1622,14 @@ void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { // C++ void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { - const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { - "", -#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ - Spelling, -#include "clang/Basic/OperatorKinds.def" - }; - OverloadedOperatorKind Kind = Node->getOperator(); if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { if (Node->getNumArgs() == 1) { - OS << OpStrings[Kind] << ' '; + OS << getOperatorSpelling(Kind) << ' '; PrintExpr(Node->getArg(0)); } else { PrintExpr(Node->getArg(0)); - OS << ' ' << OpStrings[Kind]; + OS << ' ' << getOperatorSpelling(Kind); } } else if (Kind == OO_Arrow) { PrintExpr(Node->getArg(0)); @@ -1638,11 +1649,11 @@ void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { PrintExpr(Node->getArg(1)); OS << ']'; } else if (Node->getNumArgs() == 1) { - OS << OpStrings[Kind] << ' '; + OS << getOperatorSpelling(Kind) << ' '; PrintExpr(Node->getArg(0)); } else if (Node->getNumArgs() == 2) { PrintExpr(Node->getArg(0)); - OS << ' ' << OpStrings[Kind] << ' '; + OS << ' ' << getOperatorSpelling(Kind) << ' '; PrintExpr(Node->getArg(1)); } else { llvm_unreachable("unknown overloaded operator"); @@ -1692,6 +1703,14 @@ void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { VisitCXXNamedCastExpr(Node); } +void StmtPrinter::VisitBuiltinBitCastExpr(BuiltinBitCastExpr *Node) { + OS << "__builtin_bit_cast("; + Node->getTypeInfoAsWritten()->getType().print(OS, Policy); + OS << ", "; + PrintExpr(Node->getSubExpr()); + OS << ")"; +} + void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { OS << "typeid("; if (Node->isTypeOperand()) { @@ -1896,13 +1915,22 @@ void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { llvm_unreachable("VLA type in explicit captures."); } + if (C->isPackExpansion()) + OS << "..."; + if (Node->isInitCapture(C)) PrintExpr(C->getCapturedVar()->getInit()); } OS << ']'; + if (!Node->getExplicitTemplateParameters().empty()) { + Node->getTemplateParameterList()->print( + OS, Node->getLambdaClass()->getASTContext(), + /*OmitTemplateKW*/true); + } + if (Node->hasExplicitParameters()) { - OS << " ("; + OS << '('; CXXMethodDecl *Method = Node->getCallOperator(); NeedComma = false; for (const auto *P : Method->parameters()) { @@ -1937,9 +1965,11 @@ void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { } // Print the body. - CompoundStmt *Body = Node->getBody(); OS << ' '; - PrintStmt(Body); + if (Policy.TerseOutput) + OS << "{}"; + else + PrintRawCompoundStmt(Node->getBody()); } void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { @@ -1969,10 +1999,11 @@ void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { if (E->isParenTypeId()) OS << "("; std::string TypeS; - if (Expr *Size = E->getArraySize()) { + if (Optional<Expr *> Size = E->getArraySize()) { llvm::raw_string_ostream s(TypeS); s << '['; - Size->printPretty(s, Helper, Policy); + if (*Size) + (*Size)->printPretty(s, Helper, Policy); s << ']'; } E->getAllocatedType().print(OS, Policy, TypeS); @@ -2380,12 +2411,21 @@ void Stmt::dumpPretty(const ASTContext &Context) const { printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts())); } -void Stmt::printPretty(raw_ostream &OS, PrinterHelper *Helper, +void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper, const PrintingPolicy &Policy, unsigned Indentation, - StringRef NL, - const ASTContext *Context) const { - StmtPrinter P(OS, Helper, Policy, Indentation, NL, Context); - P.Visit(const_cast<Stmt*>(this)); + StringRef NL, const ASTContext *Context) const { + StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context); + P.Visit(const_cast<Stmt *>(this)); +} + +void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper, + const PrintingPolicy &Policy, bool AddQuotes) const { + std::string Buf; + llvm::raw_string_ostream TempOut(Buf); + + printPretty(TempOut, Helper, Policy); + + Out << JsonFormat(TempOut.str(), AddQuotes); } //===----------------------------------------------------------------------===// |