forked from OSchip/llvm-project
[clang][DeclPrinter] Pass Context into StmtPrinter whenever possible
ASTContext were only passed to the StmtPrinter in some places, while it is always available in DeclPrinter. The context is used by StmtPrinter to better print statements in some cases, like printing constants as written. Differential Revision: https://reviews.llvm.org/D97043
This commit is contained in:
parent
8f63cf5da3
commit
7c9c0a87c8
|
@ -523,7 +523,7 @@ class Foo {})cpp";
|
|||
)cpp",
|
||||
[](HoverInfo &HI) {
|
||||
HI.Name = "result";
|
||||
HI.Definition = "static constexpr int result = 1 + 2";
|
||||
HI.Definition = "static constexpr int result = a + b";
|
||||
HI.Kind = index::SymbolKind::StaticProperty;
|
||||
HI.Type = "const int";
|
||||
HI.NamespaceScope = "";
|
||||
|
|
|
@ -341,7 +341,8 @@ void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
|
|||
SimpleInit = Init;
|
||||
|
||||
if (SimpleInit)
|
||||
SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
|
||||
SimpleInit->printPretty(Out, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
else {
|
||||
for (unsigned I = 0; I != NumArgs; ++I) {
|
||||
assert(Args[I] != nullptr && "Expected non-null Expr");
|
||||
|
@ -350,7 +351,8 @@ void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
|
|||
|
||||
if (I)
|
||||
Out << ", ";
|
||||
Args[I]->printPretty(Out, nullptr, Policy, Indentation);
|
||||
Args[I]->printPretty(Out, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -568,13 +570,14 @@ void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
|
|||
}
|
||||
|
||||
static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
|
||||
PrintingPolicy &Policy,
|
||||
unsigned Indentation) {
|
||||
PrintingPolicy &Policy, unsigned Indentation,
|
||||
const ASTContext &Context) {
|
||||
std::string Proto = "explicit";
|
||||
llvm::raw_string_ostream EOut(Proto);
|
||||
if (ES.getExpr()) {
|
||||
EOut << "(";
|
||||
ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation);
|
||||
ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
EOut << ")";
|
||||
}
|
||||
EOut << " ";
|
||||
|
@ -616,7 +619,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
|
|||
if (D->isConsteval()) Out << "consteval ";
|
||||
ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D);
|
||||
if (ExplicitSpec.isSpecified())
|
||||
printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation);
|
||||
printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation, Context);
|
||||
}
|
||||
|
||||
PrintingPolicy SubPolicy(Policy);
|
||||
|
@ -720,7 +723,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
|
|||
Proto += "(";
|
||||
llvm::raw_string_ostream EOut(Proto);
|
||||
FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
|
||||
Indentation);
|
||||
Indentation, "\n", &Context);
|
||||
EOut.flush();
|
||||
Proto += EOut.str();
|
||||
Proto += ")";
|
||||
|
@ -744,7 +747,8 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
|
|||
|
||||
if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
|
||||
Out << " requires ";
|
||||
TrailingRequiresClause->printPretty(Out, nullptr, SubPolicy, Indentation);
|
||||
TrailingRequiresClause->printPretty(Out, nullptr, SubPolicy, Indentation,
|
||||
"\n", &Context);
|
||||
}
|
||||
} else {
|
||||
Ty.print(Out, Policy, Proto);
|
||||
|
@ -776,7 +780,8 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
|
|||
Out << ' ';
|
||||
|
||||
if (D->getBody())
|
||||
D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
|
||||
D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation, "\n",
|
||||
&Context);
|
||||
} else {
|
||||
if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
|
||||
Out << " {}";
|
||||
|
@ -821,7 +826,8 @@ void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
|
|||
|
||||
if (D->isBitField()) {
|
||||
Out << " : ";
|
||||
D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
|
||||
D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
}
|
||||
|
||||
Expr *Init = D->getInClassInitializer();
|
||||
|
@ -830,7 +836,7 @@ void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
|
|||
Out << " ";
|
||||
else
|
||||
Out << " = ";
|
||||
Init->printPretty(Out, nullptr, Policy, Indentation);
|
||||
Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
|
||||
}
|
||||
prettyPrintAttributes(D);
|
||||
}
|
||||
|
@ -895,7 +901,7 @@ void DeclPrinter::VisitVarDecl(VarDecl *D) {
|
|||
PrintingPolicy SubPolicy(Policy);
|
||||
SubPolicy.SuppressSpecifiers = false;
|
||||
SubPolicy.IncludeTagDefinition = false;
|
||||
Init->printPretty(Out, nullptr, SubPolicy, Indentation);
|
||||
Init->printPretty(Out, nullptr, SubPolicy, Indentation, "\n", &Context);
|
||||
if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
|
||||
Out << ")";
|
||||
}
|
||||
|
@ -909,7 +915,8 @@ void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
|
|||
|
||||
void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
|
||||
Out << "__asm (";
|
||||
D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
|
||||
D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
Out << ")";
|
||||
}
|
||||
|
||||
|
@ -920,10 +927,11 @@ void DeclPrinter::VisitImportDecl(ImportDecl *D) {
|
|||
|
||||
void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
|
||||
Out << "static_assert(";
|
||||
D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
|
||||
D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
if (StringLiteral *SL = D->getMessage()) {
|
||||
Out << ", ";
|
||||
SL->printPretty(Out, nullptr, Policy, Indentation);
|
||||
SL->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
|
||||
}
|
||||
Out << ")";
|
||||
}
|
||||
|
@ -1110,8 +1118,8 @@ void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
|
|||
Visit(TD);
|
||||
else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
|
||||
Out << "concept " << Concept->getName() << " = " ;
|
||||
Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy,
|
||||
Indentation);
|
||||
Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, Indentation,
|
||||
"\n", &Context);
|
||||
Out << ";";
|
||||
}
|
||||
}
|
||||
|
@ -1271,7 +1279,8 @@ void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
|
|||
|
||||
if (OMD->getBody() && !Policy.TerseOutput) {
|
||||
Out << ' ';
|
||||
OMD->getBody()->printPretty(Out, nullptr, Policy);
|
||||
OMD->getBody()->printPretty(Out, nullptr, Policy, Indentation, "\n",
|
||||
&Context);
|
||||
}
|
||||
else if (Policy.PolishForDeclaration)
|
||||
Out << ';';
|
||||
|
@ -1651,7 +1660,7 @@ void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
|
|||
Out << " : ";
|
||||
D->getType().print(Out, Policy);
|
||||
Out << " : ";
|
||||
D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
|
||||
D->getCombiner()->printPretty(Out, nullptr, Policy, 0, "\n", &Context);
|
||||
Out << ")";
|
||||
if (auto *Init = D->getInitializer()) {
|
||||
Out << " initializer(";
|
||||
|
@ -1665,7 +1674,7 @@ void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
|
|||
case OMPDeclareReductionDecl::CallInit:
|
||||
break;
|
||||
}
|
||||
Init->printPretty(Out, nullptr, Policy, 0);
|
||||
Init->printPretty(Out, nullptr, Policy, 0, "\n", &Context);
|
||||
if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
|
||||
Out << ")";
|
||||
Out << ")";
|
||||
|
@ -1693,7 +1702,7 @@ void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
|
|||
}
|
||||
|
||||
void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
|
||||
D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
|
||||
D->getInit()->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
|
||||
}
|
||||
|
||||
void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
|
||||
|
@ -1727,6 +1736,7 @@ void DeclPrinter::VisitNonTypeTemplateParmDecl(
|
|||
|
||||
if (NTTP->hasDefaultArgument()) {
|
||||
Out << " = ";
|
||||
NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, Indentation);
|
||||
NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, Indentation,
|
||||
"\n", &Context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||
#include "clang/ASTMatchers/ASTMatchers.h"
|
||||
#include "clang/Tooling/Tooling.h"
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
|
@ -1438,3 +1439,11 @@ TEST(DeclPrinter, TestObjCCategoryImplInvalidInterface) {
|
|||
namedDecl(hasName("Extension")).bind("id"),
|
||||
"@implementation <<error-type>>(Extension)\n@end", /*AllowError=*/true));
|
||||
}
|
||||
|
||||
TEST(DeclPrinter, VarDeclWithInitializer) {
|
||||
ASSERT_TRUE(PrintedDeclCXX17Matches(
|
||||
"int a = 0x15;", namedDecl(hasName("a")).bind("id"), "int a = 21"));
|
||||
ASSERT_TRUE(PrintedDeclCXX17Matches(
|
||||
"int a = 0x15;", namedDecl(hasName("a")).bind("id"), "int a = 0x15",
|
||||
[](PrintingPolicy &Policy) { Policy.ConstantsAsWritten = true; }));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue