2007-10-07 14:04:32 +08:00
|
|
|
//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
|
2007-06-23 08:39:57 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-23 08:39:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-10-07 14:04:32 +08:00
|
|
|
// AST Consumer Implementations.
|
2007-06-23 08:39:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 06:50:54 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2009-09-09 23:08:12 +08:00
|
|
|
#include "clang/Frontend/DocumentXML.h"
|
2009-03-02 14:16:29 +08:00
|
|
|
#include "clang/Frontend/PathDiagnosticClients.h"
|
2008-08-10 02:32:11 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-12-20 08:34:58 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2007-06-23 08:39:57 +08:00
|
|
|
#include "clang/AST/AST.h"
|
2007-09-16 07:02:28 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-03-28 12:27:18 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-09-25 02:54:49 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-05-30 04:38:28 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2008-08-06 02:50:11 +08:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
|
|
|
#include "llvm/Module.h"
|
2009-09-25 07:50:42 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2008-02-19 05:21:23 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-28 12:27:18 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2009-08-24 21:25:12 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
using namespace clang;
|
2007-06-23 08:39:57 +08:00
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-05-30 08:08:05 +08:00
|
|
|
/// ASTPrinter - Pretty-printer and dumper of ASTs
|
2007-11-28 05:46:50 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-05-30 08:08:05 +08:00
|
|
|
class ASTPrinter : public ASTConsumer {
|
|
|
|
llvm::raw_ostream &Out;
|
|
|
|
bool Dump;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
ASTPrinter(llvm::raw_ostream* o = NULL, bool Dump = false)
|
2009-05-30 08:08:05 +08:00
|
|
|
: Out(o? *o : llvm::errs()), Dump(Dump) { }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-30 08:08:05 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &Context) {
|
|
|
|
PrintingPolicy Policy = Context.PrintingPolicy;
|
|
|
|
Policy.Dump = Dump;
|
2009-06-30 10:35:04 +08:00
|
|
|
Context.getTranslationUnitDecl()->print(Out, Policy);
|
2007-06-23 08:39:57 +08:00
|
|
|
}
|
2007-09-16 07:02:28 +08:00
|
|
|
};
|
2009-03-28 13:44:17 +08:00
|
|
|
} // end anonymous namespace
|
2007-08-09 06:51:59 +08:00
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
|
2007-11-29 05:32:21 +08:00
|
|
|
return new ASTPrinter(out);
|
|
|
|
}
|
2007-11-28 05:46:50 +08:00
|
|
|
|
2009-05-22 04:55:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTPrinterXML - XML-printer of ASTs
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ASTPrinterXML : public ASTConsumer {
|
|
|
|
DocumentXML Doc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-22 04:55:50 +08:00
|
|
|
void Initialize(ASTContext &Context) {
|
|
|
|
Doc.initialize(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
|
|
|
Doc.addSubNode("TranslationUnit");
|
2009-09-09 23:08:12 +08:00
|
|
|
for (DeclContext::decl_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
D = Ctx.getTranslationUnitDecl()->decls_begin(),
|
|
|
|
DEnd = Ctx.getTranslationUnitDecl()->decls_end();
|
2009-09-09 23:08:12 +08:00
|
|
|
D != DEnd;
|
2009-05-22 04:55:50 +08:00
|
|
|
++D)
|
|
|
|
Doc.PrintDecl(*D);
|
|
|
|
Doc.toParent();
|
|
|
|
Doc.finalize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) {
|
|
|
|
return new ASTPrinterXML(out ? *out : llvm::outs());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ASTConsumer *clang::CreateASTDumper() {
|
|
|
|
return new ASTPrinter(0, true);
|
2009-04-26 10:02:08 +08:00
|
|
|
}
|
2007-09-16 07:02:28 +08:00
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTViewer - AST Visualization
|
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
namespace {
|
|
|
|
class ASTViewer : public ASTConsumer {
|
2009-05-30 08:08:05 +08:00
|
|
|
ASTContext *Context;
|
2007-09-20 05:29:43 +08:00
|
|
|
public:
|
2007-12-20 06:51:13 +08:00
|
|
|
void Initialize(ASTContext &Context) {
|
2009-05-30 08:08:05 +08:00
|
|
|
this->Context = &Context;
|
2007-09-20 05:29:43 +08:00
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef D) {
|
|
|
|
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
|
|
|
|
HandleTopLevelSingleDecl(*I);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
void HandleTopLevelSingleDecl(Decl *D);
|
2007-09-20 05:29:43 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
|
2009-03-28 13:44:17 +08:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2009-06-30 10:35:04 +08:00
|
|
|
FD->print(llvm::errs());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-12 08:08:48 +08:00
|
|
|
if (Stmt *Body = FD->getBody()) {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << '\n';
|
2009-09-12 08:08:48 +08:00
|
|
|
Body->viewAST();
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << '\n';
|
2009-03-28 13:44:17 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 13:44:17 +08:00
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
2009-06-30 10:35:04 +08:00
|
|
|
MD->print(llvm::errs());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 13:44:17 +08:00
|
|
|
if (MD->getBody()) {
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << '\n';
|
2009-03-28 13:44:17 +08:00
|
|
|
MD->getBody()->viewAST();
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << '\n';
|
2009-03-28 13:44:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
|
|
|
|
|
2009-01-13 09:29:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// DeclContextPrinter - Decl and DeclContext Visualization
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DeclContextPrinter : public ASTConsumer {
|
|
|
|
llvm::raw_ostream& Out;
|
|
|
|
public:
|
|
|
|
DeclContextPrinter() : Out(llvm::errs()) {}
|
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &C) {
|
|
|
|
PrintDeclContext(C.getTranslationUnitDecl(), 4);
|
2009-01-13 09:29:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
|
|
|
|
};
|
2009-03-28 13:44:17 +08:00
|
|
|
} // end anonymous namespace
|
2009-01-13 09:29:24 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
|
2009-01-13 09:29:24 +08:00
|
|
|
unsigned Indentation) {
|
|
|
|
// Print DeclContext name.
|
2009-01-13 21:11:58 +08:00
|
|
|
switch (DC->getDeclKind()) {
|
2009-01-13 09:29:24 +08:00
|
|
|
case Decl::TranslationUnit:
|
|
|
|
Out << "[translation unit] " << DC;
|
|
|
|
break;
|
|
|
|
case Decl::Namespace: {
|
|
|
|
Out << "[namespace] ";
|
2009-02-16 22:29:59 +08:00
|
|
|
const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
|
2009-01-13 09:29:24 +08:00
|
|
|
Out << ND->getNameAsString();
|
|
|
|
break;
|
|
|
|
}
|
2009-01-13 10:41:08 +08:00
|
|
|
case Decl::Enum: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const EnumDecl* ED = cast<EnumDecl>(DC);
|
2009-01-13 10:41:08 +08:00
|
|
|
if (ED->isDefinition())
|
|
|
|
Out << "[enum] ";
|
|
|
|
else
|
|
|
|
Out << "<enum> ";
|
|
|
|
Out << ED->getNameAsString();
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
2009-01-13 10:41:08 +08:00
|
|
|
}
|
2009-01-13 09:29:24 +08:00
|
|
|
case Decl::Record: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const RecordDecl* RD = cast<RecordDecl>(DC);
|
2009-01-13 09:29:24 +08:00
|
|
|
if (RD->isDefinition())
|
|
|
|
Out << "[struct] ";
|
|
|
|
else
|
|
|
|
Out << "<struct> ";
|
|
|
|
Out << RD->getNameAsString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXRecord: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
|
2009-01-13 09:29:24 +08:00
|
|
|
if (RD->isDefinition())
|
|
|
|
Out << "[class] ";
|
|
|
|
else
|
|
|
|
Out << "<class> ";
|
|
|
|
Out << RD->getNameAsString() << " " << DC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
Out << "[objc method]";
|
|
|
|
break;
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
Out << "[objc interface]";
|
|
|
|
break;
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
Out << "[objc category]";
|
|
|
|
break;
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
Out << "[objc protocol]";
|
|
|
|
break;
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
Out << "[objc implementation]";
|
|
|
|
break;
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
Out << "[objc categoryimpl]";
|
|
|
|
break;
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
Out << "[linkage spec]";
|
|
|
|
break;
|
|
|
|
case Decl::Block:
|
|
|
|
Out << "[block]";
|
|
|
|
break;
|
|
|
|
case Decl::Function: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const FunctionDecl* FD = cast<FunctionDecl>(DC);
|
2009-01-13 09:29:24 +08:00
|
|
|
if (FD->isThisDeclarationADefinition())
|
|
|
|
Out << "[function] ";
|
|
|
|
else
|
|
|
|
Out << "<function> ";
|
|
|
|
Out << FD->getNameAsString();
|
2009-01-13 14:25:33 +08:00
|
|
|
// Print the parameters.
|
|
|
|
Out << "(";
|
|
|
|
bool PrintComma = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionDecl::param_const_iterator I = FD->param_begin(),
|
2009-01-13 14:25:33 +08:00
|
|
|
E = FD->param_end(); I != E; ++I) {
|
|
|
|
if (PrintComma)
|
|
|
|
Out << ", ";
|
|
|
|
else
|
|
|
|
PrintComma = true;
|
|
|
|
Out << (*I)->getNameAsString();
|
|
|
|
}
|
|
|
|
Out << ")";
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXMethod: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
|
2009-06-18 06:49:50 +08:00
|
|
|
if (D->isOutOfLine())
|
2009-01-13 09:29:24 +08:00
|
|
|
Out << "[c++ method] ";
|
2009-01-13 11:26:38 +08:00
|
|
|
else if (D->isImplicit())
|
|
|
|
Out << "(c++ method) ";
|
2009-01-13 09:29:24 +08:00
|
|
|
else
|
|
|
|
Out << "<c++ method> ";
|
2009-01-13 11:26:38 +08:00
|
|
|
Out << D->getNameAsString();
|
2009-01-13 14:25:33 +08:00
|
|
|
// Print the parameters.
|
|
|
|
Out << "(";
|
|
|
|
bool PrintComma = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionDecl::param_const_iterator I = D->param_begin(),
|
2009-01-13 14:25:33 +08:00
|
|
|
E = D->param_end(); I != E; ++I) {
|
|
|
|
if (PrintComma)
|
|
|
|
Out << ", ";
|
|
|
|
else
|
|
|
|
PrintComma = true;
|
|
|
|
Out << (*I)->getNameAsString();
|
|
|
|
}
|
|
|
|
Out << ")";
|
2009-01-13 09:29:24 +08:00
|
|
|
|
|
|
|
// Check the semantic DeclContext.
|
2009-02-16 22:29:59 +08:00
|
|
|
const DeclContext* SemaDC = D->getDeclContext();
|
|
|
|
const DeclContext* LexicalDC = D->getLexicalDeclContext();
|
2009-01-13 11:26:38 +08:00
|
|
|
if (SemaDC != LexicalDC)
|
2009-01-13 09:29:24 +08:00
|
|
|
Out << " [[" << SemaDC << "]]";
|
2009-01-13 11:26:38 +08:00
|
|
|
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXConstructor: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
|
2009-06-18 06:49:50 +08:00
|
|
|
if (D->isOutOfLine())
|
2009-01-13 09:29:24 +08:00
|
|
|
Out << "[c++ ctor] ";
|
2009-01-13 11:26:38 +08:00
|
|
|
else if (D->isImplicit())
|
|
|
|
Out << "(c++ ctor) ";
|
2009-01-13 09:29:24 +08:00
|
|
|
else
|
|
|
|
Out << "<c++ ctor> ";
|
2009-01-13 11:26:38 +08:00
|
|
|
Out << D->getNameAsString();
|
2009-01-13 14:25:33 +08:00
|
|
|
// Print the parameters.
|
|
|
|
Out << "(";
|
|
|
|
bool PrintComma = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (FunctionDecl::param_const_iterator I = D->param_begin(),
|
2009-01-13 14:25:33 +08:00
|
|
|
E = D->param_end(); I != E; ++I) {
|
|
|
|
if (PrintComma)
|
|
|
|
Out << ", ";
|
|
|
|
else
|
|
|
|
PrintComma = true;
|
|
|
|
Out << (*I)->getNameAsString();
|
|
|
|
}
|
|
|
|
Out << ")";
|
|
|
|
|
2009-01-13 11:26:38 +08:00
|
|
|
// Check the semantic DC.
|
2009-02-16 22:29:59 +08:00
|
|
|
const DeclContext* SemaDC = D->getDeclContext();
|
|
|
|
const DeclContext* LexicalDC = D->getLexicalDeclContext();
|
2009-01-13 11:26:38 +08:00
|
|
|
if (SemaDC != LexicalDC)
|
|
|
|
Out << " [[" << SemaDC << "]]";
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXDestructor: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
|
2009-06-18 06:49:50 +08:00
|
|
|
if (D->isOutOfLine())
|
2009-01-13 09:29:24 +08:00
|
|
|
Out << "[c++ dtor] ";
|
2009-01-13 11:26:38 +08:00
|
|
|
else if (D->isImplicit())
|
|
|
|
Out << "(c++ dtor) ";
|
2009-01-13 09:29:24 +08:00
|
|
|
else
|
|
|
|
Out << "<c++ dtor> ";
|
2009-01-13 11:26:38 +08:00
|
|
|
Out << D->getNameAsString();
|
|
|
|
// Check the semantic DC.
|
2009-02-16 22:29:59 +08:00
|
|
|
const DeclContext* SemaDC = D->getDeclContext();
|
|
|
|
const DeclContext* LexicalDC = D->getLexicalDeclContext();
|
2009-01-13 11:26:38 +08:00
|
|
|
if (SemaDC != LexicalDC)
|
|
|
|
Out << " [[" << SemaDC << "]]";
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXConversion: {
|
2009-02-16 22:29:59 +08:00
|
|
|
const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
|
2009-06-18 06:49:50 +08:00
|
|
|
if (D->isOutOfLine())
|
2009-01-13 09:29:24 +08:00
|
|
|
Out << "[c++ conversion] ";
|
2009-01-13 11:26:38 +08:00
|
|
|
else if (D->isImplicit())
|
|
|
|
Out << "(c++ conversion) ";
|
2009-01-13 09:29:24 +08:00
|
|
|
else
|
|
|
|
Out << "<c++ conversion> ";
|
2009-01-13 11:26:38 +08:00
|
|
|
Out << D->getNameAsString();
|
|
|
|
// Check the semantic DC.
|
2009-02-16 22:29:59 +08:00
|
|
|
const DeclContext* SemaDC = D->getDeclContext();
|
|
|
|
const DeclContext* LexicalDC = D->getLexicalDeclContext();
|
2009-01-13 11:26:38 +08:00
|
|
|
if (SemaDC != LexicalDC)
|
|
|
|
Out << " [[" << SemaDC << "]]";
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0 && "a decl that inherits DeclContext isn't handled");
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "\n";
|
|
|
|
|
|
|
|
// Print decls in the DeclContext.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
|
2009-01-13 09:29:24 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
for (unsigned i = 0; i < Indentation; ++i)
|
2009-02-11 04:16:46 +08:00
|
|
|
Out << " ";
|
2009-01-13 09:29:24 +08:00
|
|
|
|
|
|
|
Decl::Kind DK = I->getKind();
|
|
|
|
switch (DK) {
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::Enum:
|
|
|
|
case Decl::Record:
|
|
|
|
case Decl::CXXRecord:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::ObjCInterface:
|
2009-09-09 23:08:12 +08:00
|
|
|
case Decl::ObjCCategory:
|
2009-01-13 09:29:24 +08:00
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::Block:
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
{
|
2009-02-16 22:29:59 +08:00
|
|
|
DeclContext* DC = cast<DeclContext>(*I);
|
2009-02-11 04:16:46 +08:00
|
|
|
PrintDeclContext(DC, Indentation+2);
|
2009-01-13 09:29:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::Field: {
|
|
|
|
FieldDecl* FD = cast<FieldDecl>(*I);
|
|
|
|
Out << "<field> " << FD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::Typedef: {
|
|
|
|
TypedefDecl* TD = cast<TypedefDecl>(*I);
|
|
|
|
Out << "<typedef> " << TD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::EnumConstant: {
|
|
|
|
EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
|
|
|
|
Out << "<enum constant> " << ECD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::Var: {
|
|
|
|
VarDecl* VD = cast<VarDecl>(*I);
|
|
|
|
Out << "<var> " << VD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ImplicitParam: {
|
|
|
|
ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
|
|
|
|
Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ParmVar: {
|
|
|
|
ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
|
|
|
|
Out << "<parameter> " << PVD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ObjCProperty: {
|
|
|
|
ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
|
|
|
|
Out << "<objc property> " << OPD->getNameAsString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2009-04-05 10:04:38 +08:00
|
|
|
fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
|
2009-01-13 09:29:24 +08:00
|
|
|
assert(0 && "decl unhandled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
ASTConsumer *clang::CreateDeclContextPrinter() {
|
|
|
|
return new DeclContextPrinter();
|
2009-01-13 09:29:24 +08:00
|
|
|
}
|
|
|
|
|
2009-09-25 02:54:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// RecordLayoutDumper - C++ Record Layout Dumping.
|
|
|
|
namespace {
|
|
|
|
class RecordLayoutDumper : public ASTConsumer {
|
|
|
|
llvm::raw_ostream& Out;
|
|
|
|
|
2009-09-25 07:50:42 +08:00
|
|
|
void PrintOffset(uint64_t Offset, unsigned IndentLevel) {
|
|
|
|
Out << llvm::format("%4d | ", Offset);
|
|
|
|
for (unsigned I = 0; I < IndentLevel * 2; ++I) Out << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumpRecordLayoutOffsets(const CXXRecordDecl *RD, ASTContext &C,
|
|
|
|
uint64_t Offset,
|
|
|
|
unsigned IndentLevel, const char* Description,
|
|
|
|
bool IncludeVirtualBases) {
|
|
|
|
const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
PrintOffset(Offset, IndentLevel);
|
|
|
|
Out << C.getTypeDeclType((CXXRecordDecl *)RD).getAsString();
|
|
|
|
if (Description)
|
|
|
|
Out << ' ' << Description;
|
|
|
|
if (RD->isEmpty())
|
|
|
|
Out << " (empty)";
|
|
|
|
Out << '\n';
|
|
|
|
|
|
|
|
IndentLevel++;
|
|
|
|
|
|
|
|
const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
|
|
|
|
|
|
|
|
// Vtable pointer.
|
|
|
|
if (RD->isDynamicClass() && !PrimaryBase) {
|
|
|
|
PrintOffset(Offset, IndentLevel);
|
|
|
|
Out << '(' << RD->getNameAsString() << " vtable pointer)\n";
|
|
|
|
}
|
|
|
|
// Dump (non-virtual) bases
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2009-10-26 01:03:50 +08:00
|
|
|
assert(!I->getType()->isDependentType() &&
|
|
|
|
"Cannot layout class with dependent bases.");
|
2009-09-25 07:50:42 +08:00
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
|
|
|
|
|
|
|
|
DumpRecordLayoutOffsets(Base, C, BaseOffset, IndentLevel,
|
|
|
|
Base == PrimaryBase ? "(primary base)" : "(base)",
|
|
|
|
/*IncludeVirtualBases=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump fields.
|
|
|
|
uint64_t FieldNo = 0;
|
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I != E; ++I, ++FieldNo) {
|
|
|
|
const FieldDecl *Field = *I;
|
|
|
|
uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
|
|
|
|
|
|
|
|
if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
|
|
|
|
if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
|
|
DumpRecordLayoutOffsets(D, C, FieldOffset, IndentLevel,
|
|
|
|
Field->getNameAsCString(),
|
|
|
|
/*IncludeVirtualBases=*/true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintOffset(FieldOffset, IndentLevel);
|
|
|
|
Out << Field->getType().getAsString() << ' ';
|
|
|
|
Out << Field->getNameAsString() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IncludeVirtualBases)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Dump virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
assert(I->isVirtual() && "Found non-virtual class!");
|
|
|
|
const CXXRecordDecl *VBase =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
|
|
|
|
DumpRecordLayoutOffsets(VBase, C, VBaseOffset, IndentLevel,
|
|
|
|
VBase == PrimaryBase ?
|
|
|
|
"(primary virtual base)" : "(virtual base)",
|
|
|
|
/*IncludeVirtualBases=*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Maybe this could be useful in ASTContext.cpp.
|
2009-09-25 02:54:49 +08:00
|
|
|
void DumpRecordLayout(const CXXRecordDecl *RD, ASTContext &C) {
|
|
|
|
const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
|
|
|
|
|
2009-09-25 07:50:42 +08:00
|
|
|
DumpRecordLayoutOffsets(RD, C, 0, 0, 0,
|
|
|
|
/*IncludeVirtualBases=*/true);
|
2009-09-25 02:54:49 +08:00
|
|
|
Out << " sizeof=" << Info.getSize() / 8;
|
|
|
|
Out << ", dsize=" << Info.getDataSize() / 8;
|
|
|
|
Out << ", align=" << Info.getAlignment() / 8 << '\n';
|
|
|
|
Out << " nvsize=" << Info.getNonVirtualSize() / 8;
|
|
|
|
Out << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
|
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
RecordLayoutDumper() : Out(llvm::errs()) {}
|
|
|
|
|
|
|
|
void HandleTranslationUnit(ASTContext &C) {
|
|
|
|
for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const RecordType *RT = dyn_cast<RecordType>(*I);
|
|
|
|
if (!RT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (!RD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (RD->isImplicit())
|
|
|
|
continue;
|
|
|
|
|
2009-09-25 09:54:38 +08:00
|
|
|
if (RD->isDependentType())
|
|
|
|
continue;
|
|
|
|
|
2009-09-26 09:02:04 +08:00
|
|
|
if (RD->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!RD->getDefinition(C))
|
|
|
|
continue;
|
|
|
|
|
2009-09-25 02:54:49 +08:00
|
|
|
// FIXME: Do we really need to hard code this?
|
|
|
|
if (RD->getQualifiedNameAsString() == "__va_list_tag")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DumpRecordLayout(RD, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
ASTConsumer *clang::CreateRecordLayoutDumper() {
|
|
|
|
return new RecordLayoutDumper();
|
|
|
|
}
|
|
|
|
|
2008-10-24 07:36:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// InheritanceViewer - C++ Inheritance Visualization
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class InheritanceViewer : public ASTConsumer {
|
|
|
|
const std::string clsname;
|
|
|
|
public:
|
|
|
|
InheritanceViewer(const std::string& cname) : clsname(cname) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &C) {
|
2008-10-24 07:36:29 +08:00
|
|
|
for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
|
2009-02-28 09:32:25 +08:00
|
|
|
if (RecordType *T = dyn_cast<RecordType>(*I)) {
|
|
|
|
if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
|
|
|
|
// FIXME: This lookup needs to be generalized to handle namespaces and
|
|
|
|
// (when we support them) templates.
|
|
|
|
if (D->getNameAsString() == clsname) {
|
2009-09-09 23:08:12 +08:00
|
|
|
D->viewInheritance(C);
|
2009-02-28 09:32:25 +08:00
|
|
|
}
|
2008-10-24 07:36:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2008-10-24 07:36:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
|
|
|
|
return new InheritanceViewer(clsname);
|
|
|
|
}
|