Skip to content

Commit

Permalink
debug: Print location info for all nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
balayette committed Apr 25, 2019
1 parent c9f5b4d commit 717fec1
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions clang-tools-extra/clang-sexpression/ClangSexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ static std::shared_ptr<llvm::raw_ostream> DebugOutputStream;

class SexpVisitor {
public:
void printType(QualType t) {
void printType(QualType t, const SourceRange &range) {
locationDebug(range);
*OutputStream << "\"" << t.getAsString() << "\"";
}

Expand All @@ -80,6 +81,7 @@ class SexpVisitor {

void VisitDecl(Decl *decl) {
*OutputStream << '(' << decl->getDeclKindName() << ' ';
locationDebug(decl->getSourceRange());

if (decl->hasBody())
DispatchStmt(decl->getBody());
Expand All @@ -91,10 +93,12 @@ class SexpVisitor {
if (!f->hasBody())
return;

locationDebug(f->getSourceRange());
*OutputStream << "(Function " << f->getNameAsString() << ' ';

printType(f->getType());
printType(f->getType(), f->getSourceRange());
*OutputStream << "(FunctionParameters ";
locationDebug(f->getSourceRange());
for (auto param : f->parameters())
DispatchDecl((ParmVarDecl *)param);
*OutputStream << ')';
Expand All @@ -106,37 +110,43 @@ class SexpVisitor {

void VisitFunctionTemplate(FunctionTemplateDecl *ft) {
*OutputStream << "(FunctionTemplate " << ft->getNameAsString() << ' ';
locationDebug(ft->getSourceRange());
VisitFunction(ft->getTemplatedDecl());
*OutputStream << ')';
}

void VisitParmVar(ParmVarDecl *p) {
*OutputStream << "(ParmVarDecl " << p->getNameAsString() << ' ';
printType(p->getType());
locationDebug(p->getSourceRange());
printType(p->getType(), p->getSourceRange());
*OutputStream << ')';
}

void VisitTypedef(TypedefDecl *td) {
*OutputStream << "(Typedef " << td->getNameAsString() << ' ';
printType(td->getUnderlyingType());
locationDebug(td->getSourceRange());
printType(td->getUnderlyingType(), td->getSourceRange());
*OutputStream << ')';
}

void VisitRecord(RecordDecl *rd) {
*OutputStream << "(Record " << rd->getNameAsString() << ' ';
locationDebug(rd->getSourceRange());
for (auto *field : rd->fields())
DispatchDecl(field);
*OutputStream << ')';
}

void VisitField(FieldDecl *fd) {
*OutputStream << '(' << fd->getNameAsString() << ' ';
printType(fd->getType());
locationDebug(fd->getSourceRange());
printType(fd->getType(), fd->getSourceRange());
*OutputStream << ')';
}

void VisitEnum(EnumDecl *ed) {
*OutputStream << "(Enum " << ed->getNameAsString();
locationDebug(ed->getSourceRange());
for (auto *field : ed->enumerators())
DispatchDecl(field);
*OutputStream << ')';
Expand All @@ -145,12 +155,14 @@ class SexpVisitor {
void VisitEnumConstant(EnumConstantDecl *ecd) {
*OutputStream << '(' << ecd->getNameAsString() << ' '
<< ecd->getInitVal().getExtValue() << ')';
locationDebug(ecd->getSourceRange());
}

void VisitVar(VarDecl *vd) {
*OutputStream << "(VarDecl " << vd->getNameAsString() << ' ';
locationDebug(vd->getSourceRange());
DispatchStmt(vd->getInit());
printType(vd->getType());
printType(vd->getType(), vd->getSourceRange());
*OutputStream << ')';
}

Expand All @@ -165,14 +177,17 @@ class SexpVisitor {
else
*OutputStream << "CXXRecordDecl";

locationDebug(cxxd->getSourceRange());
*OutputStream << ' ' << cxxd->getNameAsString() << ' ';

*OutputStream << "(CXXRecordFields ";
locationDebug(cxxd->getSourceRange());
for (auto *field : cxxd->fields())
DispatchDecl(field);
*OutputStream << ')';

*OutputStream << "(CXXRecordMethods ";
locationDebug(cxxd->getSourceRange());
for (auto *method : cxxd->methods())
DispatchDecl(method);
*OutputStream << ')';
Expand All @@ -182,9 +197,11 @@ class SexpVisitor {

void VisitCXXMethod(CXXMethodDecl *cxxmd) {
*OutputStream << "(CXXMethod " << cxxmd->getNameAsString() << ' ';
locationDebug(cxxmd->getSourceRange());

printType(cxxmd->getType());
printType(cxxmd->getType(), cxxmd->getSourceRange());
*OutputStream << "(CXXMethodParameters";
locationDebug(cxxmd->getSourceRange());
for (auto *param : cxxmd->parameters())
DispatchDecl((ParmVarDecl *)param);
*OutputStream << ')';
Expand Down Expand Up @@ -212,10 +229,7 @@ class SexpVisitor {
if (_sourceManager->getFilename(decl->getLocation()).endswith(".hh"))
return;

if (DebugOutput) {
decl->getSourceRange().print(*DebugOutputStream, *_sourceManager);
*DebugOutputStream << '\n';
}
locationDebug(decl->getSourceRange());

switch (decl->getKind()) {
DISPATCH_DECL(Function)
Expand Down Expand Up @@ -253,6 +267,7 @@ class SexpVisitor {
void VisitGCCAsmStmt(GCCAsmStmt *as) {
*OutputStream << "(GCCAsmStmt ";
*OutputStream << '"' << as->getAsmString()->getString() << '"';
locationDebug(as->getSourceRange());
*OutputStream << ')';
}

Expand All @@ -264,10 +279,7 @@ class SexpVisitor {
if (!range.getBegin().isValid())
return;

if (DebugOutput) {
range.print(*DebugOutputStream, *_sourceManager);
*DebugOutputStream << '\n';
}
locationDebug(range);

if (_sourceManager->isInSystemMacro(range.getBegin())) {
// Don't expand system macros, but print them as is.
Expand Down Expand Up @@ -302,6 +314,7 @@ class SexpVisitor {
void VisitCompoundAssignOperator(CompoundAssignOperator *cao) {
*OutputStream << "(CompoundAssignOperator " << cao->getOpcodeStr().data()
<< ' ';
locationDebug(cao->getSourceRange());
RECURSE_CHILDREN_STMT(cao);
*OutputStream << ')';
}
Expand All @@ -310,15 +323,18 @@ class SexpVisitor {
*OutputStream << "(MemberExpr ";
RECURSE_CHILDREN_STMT(me);
*OutputStream << me->getMemberDecl()->getNameAsString() << ')';
locationDebug(me->getMemberDecl()->getSourceRange());
}

void VisitDeclStmt(DeclStmt *stmt) {
if (stmt->isSingleDecl()) {
*OutputStream << "(DeclStmt ";
if (auto *var = dyn_cast<VarDecl>(stmt->getSingleDecl()))
DispatchDecl(var);
else
else {
*OutputStream << stmt->getSingleDecl()->getDeclKindName();
locationDebug(stmt->getSingleDecl()->getSourceRange());
}

*OutputStream << ')';
}
Expand All @@ -327,13 +343,15 @@ class SexpVisitor {
void VisitIntegerLiteral(IntegerLiteral *i) {
*OutputStream << "(IntegerLiteral " << i->getValue().getLimitedValue()
<< ' ';
printType(i->getType());
locationDebug(i->getSourceRange());
printType(i->getType(), i->getSourceRange());
*OutputStream << ")";
}

void VisitStringLiteral(StringLiteral *s) {
*OutputStream << "(StringLiteral ";
s->outputString(*OutputStream);
locationDebug(s->getSourceRange());
*OutputStream << ")";
}

Expand All @@ -344,12 +362,14 @@ class SexpVisitor {
void VisitUnaryOperator(UnaryOperator *op) {
*OutputStream << "(UnaryOperator "
<< UnaryOperator::getOpcodeStr(op->getOpcode()).data() << ' ';
locationDebug(op->getSourceRange());
DispatchStmt(op->getSubExpr());
*OutputStream << ')';
}

void VisitBinaryOperator(BinaryOperator *op) {
*OutputStream << "(BinaryOperator " << op->getOpcodeStr().data() << ' ';
locationDebug(op->getSourceRange());

DispatchStmt(op->getLHS());
DispatchStmt(op->getRHS());
Expand All @@ -368,6 +388,18 @@ class SexpVisitor {
void setSourceManager(SourceManager *SM) { _sourceManager = SM; }

private:
void locationDebug(const SourceRange &range, int count = 1) {
if (!DebugOutput)
return;

for (int i = 0; i < count; i++) {
range.getBegin().print(*DebugOutputStream, *_sourceManager);
*DebugOutputStream << ' ';
range.getEnd().print(*DebugOutputStream, *_sourceManager);
*DebugOutputStream << '\n';
}
}

SourceManager *_sourceManager;
};

Expand All @@ -376,8 +408,10 @@ class SexpConsumer : public ASTConsumer {
virtual void HandleTranslationUnit(ASTContext &Context) {
_visitor.setSourceManager(&Context.getSourceManager());

if (DebugOutput)
*DebugOutputStream << _file << "\n";
if (DebugOutput) {
*DebugOutputStream << _file << ":begin " << _file << ":end\n";
*DebugOutputStream << _file << ":begin " << _file << ":end\n";
}

*OutputStream << "(TranslationUnit \"" << _file << "\" ";
_visitor.VisitTranslationUnit(Context.getTranslationUnitDecl());
Expand Down Expand Up @@ -420,7 +454,7 @@ int main(int argc, const char **argv) {
DebugOutputStream =
std::make_shared<llvm::raw_fd_ostream>(DebugOutputFile, err);
else
DebugOutputStream.reset(&llvm::errs(), [](llvm::raw_ostream *a) {});
DebugOutputStream.reset(&llvm::outs(), [](llvm::raw_ostream *a) {});
}

if (OutputFile != "-")
Expand Down

0 comments on commit 717fec1

Please sign in to comment.