Skip to content

Commit

Permalink
Re-commit r321223, which adds a printing policy to the ASTDumper.
Browse files Browse the repository at this point in the history
This allows you to dump C++ code that spells bool instead of _Bool, leaves off the elaborated type specifiers when printing struct or class names, and other C-isms.

Fixes the -Wreorder issue and fixes the ast-dump-color.cpp test.

llvm-svn: 321310
  • Loading branch information
AaronBallman committed Dec 21, 2017
1 parent 50db8a2 commit 8c20828
Show file tree
Hide file tree
Showing 18 changed files with 2,016 additions and 1,998 deletions.
14 changes: 6 additions & 8 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -976,16 +976,14 @@ class QualType {
return LHS.Value != RHS.Value;
}

std::string getAsString() const {
return getAsString(split());
static std::string getAsString(SplitQualType split,
const PrintingPolicy &Policy) {
return getAsString(split.Ty, split.Quals, Policy);
}
static std::string getAsString(const Type *ty, Qualifiers qs,
const PrintingPolicy &Policy);

static std::string getAsString(SplitQualType split) {
return getAsString(split.Ty, split.Quals);
}

static std::string getAsString(const Type *ty, Qualifiers qs);

std::string getAsString() const;
std::string getAsString(const PrintingPolicy &Policy) const;

void print(raw_ostream &OS, const PrintingPolicy &Policy,
Expand Down
46 changes: 30 additions & 16 deletions clang/lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ namespace {
const CommandTraits *Traits;
const SourceManager *SM;

/// The policy to use for printing; can be defaulted.
PrintingPolicy PrintPolicy;

/// Pending[i] is an action to dump an entity at level i.
llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;

Expand Down Expand Up @@ -207,12 +210,17 @@ namespace {
public:
ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
const SourceManager *SM)
: OS(OS), Traits(Traits), SM(SM),
ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
: ASTDumper(OS, Traits, SM,
SM && SM->getDiagnostics().getShowColors()) {}

ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
const SourceManager *SM, bool ShowColors)
: OS(OS), Traits(Traits), SM(SM), ShowColors(ShowColors) {}
: ASTDumper(OS, Traits, SM, ShowColors, LangOptions()) {}
ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
const SourceManager *SM, bool ShowColors,
const PrintingPolicy &PrintPolicy)
: OS(OS), Traits(Traits), SM(SM), PrintPolicy(PrintPolicy),
ShowColors(ShowColors) {}

void setDeserialize(bool D) { Deserialize = D; }

Expand Down Expand Up @@ -646,13 +654,13 @@ void ASTDumper::dumpBareType(QualType T, bool Desugar) {
ColorScope Color(*this, TypeColor);

SplitQualType T_split = T.split();
OS << "'" << QualType::getAsString(T_split) << "'";
OS << "'" << QualType::getAsString(T_split, PrintPolicy) << "'";

if (Desugar && !T.isNull()) {
// If the type is sugared, also dump a (shallow) desugared type.
SplitQualType D_split = T.getSplitDesugaredType();
if (T_split != D_split)
OS << ":'" << QualType::getAsString(D_split) << "'";
OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'";
}
}

Expand Down Expand Up @@ -1187,12 +1195,12 @@ void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {

if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
if (MD->size_overridden_methods() != 0) {
auto dumpOverride =
[=](const CXXMethodDecl *D) {
SplitQualType T_split = D->getType().split();
OS << D << " " << D->getParent()->getName() << "::"
<< D->getNameAsString() << " '" << QualType::getAsString(T_split) << "'";
};
auto dumpOverride = [=](const CXXMethodDecl *D) {
SplitQualType T_split = D->getType().split();
OS << D << " " << D->getParent()->getName()
<< "::" << D->getNameAsString() << " '"
<< QualType::getAsString(T_split, PrintPolicy) << "'";
};

dumpChild([=] {
auto Overrides = MD->overridden_methods();
Expand Down Expand Up @@ -2682,15 +2690,19 @@ LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS) const {
LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }

LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize) const {
ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
&getASTContext().getSourceManager());
const ASTContext &Ctx = getASTContext();
const SourceManager &SM = Ctx.getSourceManager();
ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &SM,
SM.getDiagnostics().getShowColors(), Ctx.getPrintingPolicy());
P.setDeserialize(Deserialize);
P.dumpDecl(this);
}

LLVM_DUMP_METHOD void Decl::dumpColor() const {
ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
&getASTContext().getSourceManager(), /*ShowColors*/true);
const ASTContext &Ctx = getASTContext();
ASTDumper P(llvm::errs(), &Ctx.getCommentCommandTraits(),
&Ctx.getSourceManager(), /*ShowColors*/ true,
Ctx.getPrintingPolicy());
P.dumpDecl(this);
}

Expand All @@ -2705,7 +2717,9 @@ LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
while (!DC->isTranslationUnit())
DC = DC->getParent();
ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
const SourceManager &SM = Ctx.getSourceManager();
ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager(),
SM.getDiagnostics().getShowColors(), Ctx.getPrintingPolicy());
P.setDeserialize(Deserialize);
P.dumpLookups(this, DumpDecls);
}
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,16 +1712,20 @@ void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
OS << ' ';
}

std::string QualType::getAsString() const {
return getAsString(split(), LangOptions());
}

std::string QualType::getAsString(const PrintingPolicy &Policy) const {
std::string S;
getAsStringInternal(S, Policy);
return S;
}

std::string QualType::getAsString(const Type *ty, Qualifiers qs) {
std::string QualType::getAsString(const Type *ty, Qualifiers qs,
const PrintingPolicy &Policy) {
std::string buffer;
LangOptions options;
getAsStringInternal(ty, qs, buffer, PrintingPolicy(options));
getAsStringInternal(ty, qs, buffer, Policy);
return buffer;
}

Expand Down
Loading

0 comments on commit 8c20828

Please sign in to comment.