2007-10-07 10:04:32 +04:00
|
|
|
//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-10-07 10:04:32 +04:00
|
|
|
// This file was developed by Chris Lattner and is distributed under the
|
2007-07-11 21:01:13 +04:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-10-07 10:04:32 +04:00
|
|
|
// AST Consumer Implementations.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-07 10:04:32 +04:00
|
|
|
#include "ASTConsumers.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/AST/AST.h"
|
2007-09-16 03:02:28 +04:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2007-08-22 01:42:03 +04:00
|
|
|
#include "clang/AST/CFG.h"
|
2007-09-06 04:17:54 +04:00
|
|
|
#include "clang/Analysis/LiveVariables.h"
|
2007-09-07 03:00:42 +04:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2007-11-29 00:32:21 +03:00
|
|
|
#include "llvm/Support/Streams.h"
|
2007-11-28 00:46:50 +03:00
|
|
|
|
2007-08-09 02:51:59 +04:00
|
|
|
using namespace clang;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// DeclPrinter - Utility class for printing top-level decls.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DeclPrinter {
|
|
|
|
public:
|
2007-11-29 00:32:21 +03:00
|
|
|
std::ostream& Out;
|
2007-08-09 02:51:59 +04:00
|
|
|
|
2007-11-29 00:32:21 +03:00
|
|
|
DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
|
|
|
|
DeclPrinter() : Out(*llvm::cerr.stream()) {}
|
2007-11-28 00:46:50 +03:00
|
|
|
|
|
|
|
void PrintFunctionDeclStart(FunctionDecl *FD);
|
|
|
|
void PrintTypeDefDecl(TypedefDecl *TD);
|
|
|
|
void PrintObjcMethodDecl(ObjcMethodDecl *OMD);
|
|
|
|
void PrintObjcImplementationDecl(ObjcImplementationDecl *OID);
|
|
|
|
void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID);
|
|
|
|
void PrintObjcProtocolDecl(ObjcProtocolDecl *PID);
|
|
|
|
void PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID);
|
|
|
|
void PrintObjcCategoryDecl(ObjcCategoryDecl *PID);
|
|
|
|
void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
|
2007-07-11 21:01:13 +04:00
|
|
|
bool HasBody = FD->getBody();
|
|
|
|
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '\n';
|
2007-08-26 08:02:13 +04:00
|
|
|
|
|
|
|
switch (FD->getStorageClass()) {
|
|
|
|
default: assert(0 && "Unknown storage class");
|
|
|
|
case FunctionDecl::None: break;
|
2007-11-29 00:32:21 +03:00
|
|
|
case FunctionDecl::Extern: Out << "extern "; break;
|
|
|
|
case FunctionDecl::Static: Out << "static "; break;
|
2007-08-26 08:02:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->isInline())
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "inline ";
|
2007-08-26 08:02:13 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
std::string Proto = FD->getName();
|
2007-12-04 00:43:25 +03:00
|
|
|
const FunctionType *AFT = FD->getType()->getAsFunctionType();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-12-04 00:43:25 +03:00
|
|
|
if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Proto += "(";
|
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
|
|
|
|
if (i) Proto += ", ";
|
|
|
|
std::string ParamStr;
|
|
|
|
if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
|
|
|
|
|
|
|
|
FT->getArgType(i).getAsStringInternal(ParamStr);
|
|
|
|
Proto += ParamStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FT->isVariadic()) {
|
|
|
|
if (FD->getNumParams()) Proto += ", ";
|
|
|
|
Proto += "...";
|
|
|
|
}
|
|
|
|
Proto += ")";
|
|
|
|
} else {
|
|
|
|
assert(isa<FunctionTypeNoProto>(AFT));
|
|
|
|
Proto += "()";
|
|
|
|
}
|
|
|
|
|
|
|
|
AFT->getResultType().getAsStringInternal(Proto);
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << Proto;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-09 02:51:59 +04:00
|
|
|
if (!FD->getBody())
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ";\n";
|
2007-08-09 02:51:59 +04:00
|
|
|
// Doesn't print the body.
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
|
2007-07-11 21:01:13 +04:00
|
|
|
std::string S = TD->getName();
|
|
|
|
TD->getUnderlyingType().getAsStringInternal(S);
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "typedef " << S << ";\n";
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) {
|
2007-11-10 23:59:13 +03:00
|
|
|
if (OMD->isInstance())
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "\n- ";
|
2007-11-10 23:59:13 +03:00
|
|
|
else
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "\n+ ";
|
2007-11-10 23:59:13 +03:00
|
|
|
if (!OMD->getResultType().isNull())
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '(' << OMD->getResultType().getAsString() << ") ";
|
2007-11-10 23:59:13 +03:00
|
|
|
// FIXME: just print original selector name!
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << OMD->getSelector().getName();
|
2007-11-10 23:59:13 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < OMD->getNumParams(); i++) {
|
|
|
|
ParmVarDecl *PDecl = OMD->getParamDecl(i);
|
2007-11-29 00:32:21 +03:00
|
|
|
// FIXME: selector is missing here!
|
|
|
|
Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
|
2007-11-10 23:59:13 +03:00
|
|
|
std::string I = OID->getName();
|
|
|
|
ObjcInterfaceDecl *SID = OID->getSuperClass();
|
2007-11-29 00:32:21 +03:00
|
|
|
|
|
|
|
if (SID)
|
|
|
|
Out << "@implementation " << I << " : " << SID->getName();
|
2007-11-10 23:59:13 +03:00
|
|
|
else
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@implementation " << I;
|
2007-11-10 23:59:13 +03:00
|
|
|
|
2007-12-12 10:46:12 +03:00
|
|
|
for (ObjcImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
|
|
|
|
E = OID->instmeth_end(); I != E; ++I) {
|
|
|
|
ObjcMethodDecl *OMD = *I;
|
|
|
|
PrintObjcMethodDecl(OMD);
|
2007-11-10 23:59:13 +03:00
|
|
|
if (OMD->getBody()) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ' ';
|
|
|
|
OMD->getBody()->printPretty(Out);
|
|
|
|
Out << '\n';
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-12 10:46:12 +03:00
|
|
|
for (ObjcImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
|
|
|
|
E = OID->classmeth_end(); I != E; ++I) {
|
|
|
|
ObjcMethodDecl *OMD = *I;
|
|
|
|
PrintObjcMethodDecl(OMD);
|
2007-11-10 23:59:13 +03:00
|
|
|
if (OMD->getBody()) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ' ';
|
|
|
|
OMD->getBody()->printPretty(Out);
|
|
|
|
Out << '\n';
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@end\n";
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) {
|
2007-10-09 03:06:41 +04:00
|
|
|
std::string I = OID->getName();
|
|
|
|
ObjcInterfaceDecl *SID = OID->getSuperClass();
|
2007-11-29 00:32:21 +03:00
|
|
|
|
|
|
|
if (SID)
|
|
|
|
Out << "@interface " << I << " : " << SID->getName();
|
2007-10-09 03:06:41 +04:00
|
|
|
else
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@interface " << I;
|
|
|
|
|
2007-10-09 03:06:41 +04:00
|
|
|
// Protocols?
|
|
|
|
int count = OID->getNumIntfRefProtocols();
|
2007-11-29 00:32:21 +03:00
|
|
|
|
2007-10-09 03:06:41 +04:00
|
|
|
if (count > 0) {
|
|
|
|
ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols();
|
|
|
|
for (int i = 0; i < count; i++)
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
|
2007-10-09 03:06:41 +04:00
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
|
2007-10-09 03:06:41 +04:00
|
|
|
if (count > 0)
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ">\n";
|
2007-10-09 03:06:41 +04:00
|
|
|
else
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '\n';
|
2007-10-26 20:29:12 +04:00
|
|
|
|
2007-12-12 10:56:42 +03:00
|
|
|
if (OID->getNumInstanceVariables() > 0) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '{';
|
2007-12-12 10:56:42 +03:00
|
|
|
for (ObjcInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
|
|
|
|
E = OID->ivar_end(); I != E; ++I) {
|
|
|
|
Out << '\t' << (*I)->getType().getAsString()
|
|
|
|
<< ' ' << (*I)->getName() << ";\n";
|
2007-10-26 20:29:12 +04:00
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "}\n";
|
2007-10-26 20:29:12 +04:00
|
|
|
}
|
2007-11-07 01:01:00 +03:00
|
|
|
|
|
|
|
int NumProperties = OID->getNumPropertyDecl();
|
|
|
|
if (NumProperties > 0) {
|
|
|
|
for (int i = 0; i < NumProperties; i++) {
|
|
|
|
ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i];
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@property";
|
2007-11-07 01:01:00 +03:00
|
|
|
if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) {
|
|
|
|
bool first = true;
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << " (";
|
2007-11-07 01:01:00 +03:00
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "readonly";
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "getter = "
|
|
|
|
<< PDecl->getGetterName()->getName();
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "setter = "
|
|
|
|
<< PDecl->getSetterName()->getName();
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "assign";
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "readwrite";
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "retain";
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "copy";
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic)
|
|
|
|
{
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << (first ? ' ' : ',') << "nonatomic";
|
2007-11-07 01:01:00 +03:00
|
|
|
first = false;
|
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << " )";
|
2007-11-07 01:01:00 +03:00
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
|
2007-11-07 01:01:00 +03:00
|
|
|
ObjcIvarDecl **IDecl = PDecl->getPropertyDecls();
|
2007-11-29 00:32:21 +03:00
|
|
|
|
|
|
|
Out << ' ' << IDecl[0]->getType().getAsString()
|
|
|
|
<< ' ' << IDecl[0]->getName();
|
|
|
|
|
|
|
|
for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
|
|
|
|
Out << ", " << IDecl[j]->getName();
|
2007-11-07 01:01:00 +03:00
|
|
|
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ";\n";
|
2007-11-07 01:01:00 +03:00
|
|
|
}
|
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
|
|
|
|
Out << "@end\n";
|
2007-09-11 00:51:04 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@protocol " << PID->getName() << '\n';
|
2007-10-08 22:53:38 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@implementation "
|
|
|
|
<< PID->getClassInterface()->getName()
|
|
|
|
<< '(' << PID->getName() << ");\n";
|
|
|
|
|
2007-10-08 22:53:38 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@interface "
|
|
|
|
<< PID->getClassInterface()->getName()
|
|
|
|
<< '(' << PID->getName() << ");\n";
|
2007-10-08 22:53:38 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@compatibility_alias " << AID->getName()
|
|
|
|
<< ' ' << AID->getClassInterface()->getName() << ";\n";
|
2007-10-12 03:42:27 +04:00
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTPrinter - Pretty-printer of ASTs
|
|
|
|
|
2007-09-16 03:02:28 +04:00
|
|
|
namespace {
|
2007-11-28 00:46:50 +03:00
|
|
|
class ASTPrinter : public ASTConsumer, public DeclPrinter {
|
|
|
|
public:
|
2007-11-29 00:32:21 +03:00
|
|
|
ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
|
2007-11-28 00:46:50 +03:00
|
|
|
|
2007-09-16 03:02:28 +04:00
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
|
|
|
|
if (FD->getBody()) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ' ';
|
|
|
|
FD->getBody()->printPretty(Out);
|
|
|
|
Out << '\n';
|
2007-09-16 03:02:28 +04:00
|
|
|
}
|
2007-11-14 02:48:03 +03:00
|
|
|
} else if (isa<ObjcMethodDecl>(D)) {
|
|
|
|
// Do nothing, methods definitions are printed in
|
|
|
|
// PrintObjcImplementationDecl.
|
2007-09-16 03:02:28 +04:00
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
2007-10-06 22:52:10 +04:00
|
|
|
} else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
|
|
|
|
PrintObjcInterfaceDecl(OID);
|
2007-10-08 22:53:38 +04:00
|
|
|
} else if (ObjcProtocolDecl *PID = dyn_cast<ObjcProtocolDecl>(D)) {
|
|
|
|
PrintObjcProtocolDecl(PID);
|
2007-10-06 22:52:10 +04:00
|
|
|
} else if (ObjcForwardProtocolDecl *OFPD =
|
|
|
|
dyn_cast<ObjcForwardProtocolDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@protocol ";
|
2007-10-06 22:52:10 +04:00
|
|
|
for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
|
|
|
|
const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
|
2007-11-29 00:32:21 +03:00
|
|
|
if (i) Out << ", ";
|
|
|
|
Out << D->getName();
|
2007-10-06 22:52:10 +04:00
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << ";\n";
|
2007-10-06 22:52:10 +04:00
|
|
|
} else if (ObjcImplementationDecl *OID =
|
|
|
|
dyn_cast<ObjcImplementationDecl>(D)) {
|
2007-11-10 23:59:13 +03:00
|
|
|
PrintObjcImplementationDecl(OID);
|
2007-10-08 22:53:38 +04:00
|
|
|
} else if (ObjcCategoryImplDecl *OID =
|
|
|
|
dyn_cast<ObjcCategoryImplDecl>(D)) {
|
|
|
|
PrintObjcCategoryImplDecl(OID);
|
|
|
|
} else if (ObjcCategoryDecl *OID =
|
|
|
|
dyn_cast<ObjcCategoryDecl>(D)) {
|
|
|
|
PrintObjcCategoryDecl(OID);
|
2007-10-12 03:42:27 +04:00
|
|
|
} else if (ObjcCompatibleAliasDecl *OID =
|
|
|
|
dyn_cast<ObjcCompatibleAliasDecl>(D)) {
|
|
|
|
PrintObjcCompatibleAliasDecl(OID);
|
2007-10-06 22:52:10 +04:00
|
|
|
} else if (isa<ObjcClassDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@class [printing todo]\n";
|
2007-11-29 01:54:11 +03:00
|
|
|
} else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
|
2007-10-08 22:53:38 +04:00
|
|
|
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
|
2007-10-06 22:52:10 +04:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown decl type!");
|
2007-08-09 02:51:59 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-09-16 03:02:28 +04:00
|
|
|
};
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-08-09 02:51:59 +04:00
|
|
|
|
2007-11-29 00:32:21 +03:00
|
|
|
ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
|
|
|
|
return new ASTPrinter(out);
|
|
|
|
}
|
2007-11-28 00:46:50 +03:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTDumper - Low-level dumper of ASTs
|
2007-09-16 03:02:28 +04:00
|
|
|
|
|
|
|
namespace {
|
2007-11-28 00:46:50 +03:00
|
|
|
class ASTDumper : public ASTConsumer, public DeclPrinter {
|
2007-09-16 03:02:28 +04:00
|
|
|
SourceManager *SM;
|
|
|
|
public:
|
2007-11-29 00:32:21 +03:00
|
|
|
ASTDumper() : DeclPrinter() {}
|
2007-11-28 00:46:50 +03:00
|
|
|
|
2007-09-16 03:02:28 +04:00
|
|
|
void Initialize(ASTContext &Context, unsigned MainFileID) {
|
2007-12-12 00:27:55 +03:00
|
|
|
SM = &Context.getSourceManager();
|
2007-09-16 03:02:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
|
|
|
|
if (FD->getBody()) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '\n';
|
|
|
|
// FIXME: convert dumper to use std::ostream?
|
2007-09-16 03:02:28 +04:00
|
|
|
FD->getBody()->dumpAll(*SM);
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '\n';
|
2007-09-16 03:02:28 +04:00
|
|
|
}
|
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
|
|
|
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
|
2007-10-06 22:52:10 +04:00
|
|
|
} else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc interface '" << OID->getName() << "'\n";
|
2007-10-14 21:03:01 +04:00
|
|
|
} else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc protocol '" << OPD->getName() << "'\n";
|
2007-10-14 21:03:01 +04:00
|
|
|
} else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc category '" << OCD->getName() << "'\n";
|
2007-10-06 22:52:10 +04:00
|
|
|
} else if (isa<ObjcForwardProtocolDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc fwd protocol decl\n";
|
2007-10-14 21:03:01 +04:00
|
|
|
} else if (isa<ObjcClassDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc fwd class decl\n";
|
2007-10-06 22:52:10 +04:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown decl type!");
|
2007-08-09 02:51:59 +04:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 03:02:28 +04:00
|
|
|
};
|
2007-08-09 02:51:59 +04:00
|
|
|
}
|
|
|
|
|
2007-09-16 03:02:28 +04:00
|
|
|
ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTViewer - AST Visualization
|
|
|
|
|
2007-09-20 01:29:43 +04:00
|
|
|
namespace {
|
|
|
|
class ASTViewer : public ASTConsumer {
|
|
|
|
SourceManager *SM;
|
|
|
|
public:
|
|
|
|
void Initialize(ASTContext &Context, unsigned MainFileID) {
|
2007-12-12 00:27:55 +03:00
|
|
|
SM = &Context.getSourceManager();
|
2007-09-20 01:29:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
DeclPrinter().PrintFunctionDeclStart(FD);
|
2007-09-20 01:29:43 +04:00
|
|
|
|
|
|
|
if (FD->getBody()) {
|
2007-11-29 00:32:21 +03:00
|
|
|
llvm::cerr << '\n';
|
2007-09-20 01:29:43 +04:00
|
|
|
FD->getBody()->viewAST();
|
2007-11-29 00:32:21 +03:00
|
|
|
llvm::cerr << '\n';
|
2007-09-20 01:29:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
|
|
|
|
|
|
|
|
|
2007-09-08 03:47:56 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
|
|
|
|
// the CFGs for all function definitions.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
class CFGVisitor : public ASTConsumer {
|
2007-09-08 03:47:56 +04:00
|
|
|
public:
|
2007-09-16 03:21:08 +04:00
|
|
|
// CFG Visitor interface to be implemented by subclass.
|
2007-09-08 03:47:56 +04:00
|
|
|
virtual void VisitCFG(CFG& C) = 0;
|
|
|
|
virtual bool printFuncDeclStart() { return true; }
|
2007-09-16 03:21:08 +04:00
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D);
|
2007-09-08 03:47:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
void CFGVisitor::HandleTopLevelDecl(Decl *D) {
|
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
|
|
|
|
if (!FD || !FD->getBody())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (printFuncDeclStart()) {
|
2007-11-29 00:32:21 +03:00
|
|
|
DeclPrinter().PrintFunctionDeclStart(FD);
|
|
|
|
llvm::cerr << '\n';
|
2007-08-22 01:42:03 +04:00
|
|
|
}
|
2007-09-16 03:21:08 +04:00
|
|
|
|
2007-09-17 21:10:02 +04:00
|
|
|
CFG *C = CFG::buildCFG(FD->getBody());
|
|
|
|
VisitCFG(*C);
|
|
|
|
delete C;
|
2007-08-22 01:42:03 +04:00
|
|
|
}
|
|
|
|
|
2007-09-08 03:47:56 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DumpCFGs - Dump CFGs to stderr or visualize with Graphviz
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CFGDumper : public CFGVisitor {
|
|
|
|
const bool UseGraphviz;
|
|
|
|
public:
|
|
|
|
CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {}
|
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
virtual void VisitCFG(CFG &C) {
|
|
|
|
if (UseGraphviz)
|
|
|
|
C.viewCFG();
|
|
|
|
else
|
|
|
|
C.dump();
|
|
|
|
}
|
2007-09-08 03:47:56 +04:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-06 04:17:54 +04:00
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
|
|
|
|
return new CFGDumper(ViewGraphs);
|
2007-09-06 04:17:54 +04:00
|
|
|
}
|
|
|
|
|
2007-09-08 03:47:56 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalyzeLiveVariables - perform live variable analysis and dump results
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LivenessVisitor : public CFGVisitor {
|
2007-09-16 03:21:08 +04:00
|
|
|
SourceManager *SM;
|
2007-09-08 03:47:56 +04:00
|
|
|
public:
|
2007-09-16 03:21:08 +04:00
|
|
|
virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
|
2007-12-12 00:27:55 +03:00
|
|
|
SM = &Context.getSourceManager();
|
2007-09-16 03:21:08 +04:00
|
|
|
}
|
|
|
|
|
2007-09-08 03:47:56 +04:00
|
|
|
virtual void VisitCFG(CFG& C) {
|
2007-10-02 00:33:52 +04:00
|
|
|
LiveVariables L(C);
|
2007-09-08 03:47:56 +04:00
|
|
|
L.runOnCFG(C);
|
2007-09-25 08:31:27 +04:00
|
|
|
L.dumpBlockLiveness(*SM);
|
2007-09-07 03:00:42 +04:00
|
|
|
}
|
2007-09-08 03:47:56 +04:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-07 03:00:42 +04:00
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
ASTConsumer *clang::CreateLiveVarAnalyzer() {
|
|
|
|
return new LivenessVisitor();
|
2007-09-07 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2007-09-08 03:47:56 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-18 00:49:30 +04:00
|
|
|
// DeadStores - run checker to locate dead stores in a function
|
2007-09-08 03:47:56 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DeadStoreVisitor : public CFGVisitor {
|
2007-09-16 03:21:08 +04:00
|
|
|
Diagnostic &Diags;
|
|
|
|
ASTContext *Ctx;
|
2007-09-08 03:47:56 +04:00
|
|
|
public:
|
2007-09-16 03:21:08 +04:00
|
|
|
DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
|
|
|
|
virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
|
|
|
|
Ctx = &Context;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
|
2007-09-08 03:54:15 +04:00
|
|
|
virtual bool printFuncDeclStart() { return false; }
|
2007-09-08 03:47:56 +04:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-08-09 02:51:59 +04:00
|
|
|
|
2007-09-16 03:21:08 +04:00
|
|
|
ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
|
|
|
|
return new DeadStoreVisitor(Diags);
|
2007-09-08 03:47:56 +04:00
|
|
|
}
|
2007-09-16 23:46:59 +04:00
|
|
|
|
2007-09-18 00:49:30 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Unitialized Values - run checker to flag potential uses of uninitalized
|
|
|
|
// variables.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class UninitValsVisitor : public CFGVisitor {
|
|
|
|
Diagnostic &Diags;
|
|
|
|
ASTContext *Ctx;
|
|
|
|
public:
|
|
|
|
UninitValsVisitor(Diagnostic &diags) : Diags(diags) {}
|
|
|
|
virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
|
|
|
|
Ctx = &Context;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); }
|
|
|
|
virtual bool printFuncDeclStart() { return false; }
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) {
|
|
|
|
return new UninitValsVisitor(Diags);
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:46:59 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLVM Emitter
|
|
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-10-31 23:01:01 +03:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2007-09-16 23:46:59 +04:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
|
|
|
#include "llvm/Module.h"
|
2007-10-31 23:01:01 +03:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2007-09-16 23:46:59 +04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LLVMEmitter : public ASTConsumer {
|
|
|
|
Diagnostic &Diags;
|
|
|
|
llvm::Module *M;
|
2007-10-31 23:01:01 +03:00
|
|
|
const llvm::TargetData *TD;
|
2007-09-16 23:46:59 +04:00
|
|
|
ASTContext *Ctx;
|
2007-11-28 08:34:05 +03:00
|
|
|
const LangOptions &Features;
|
2007-11-13 21:16:41 +03:00
|
|
|
CodeGen::CodeGenModule *Builder;
|
2007-09-16 23:46:59 +04:00
|
|
|
public:
|
2007-11-28 08:34:05 +03:00
|
|
|
LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
|
|
|
|
: Diags(diags)
|
|
|
|
, Features(LO) {}
|
2007-09-16 23:46:59 +04:00
|
|
|
virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
|
|
|
|
Ctx = &Context;
|
|
|
|
M = new llvm::Module("foo");
|
2007-10-31 23:01:01 +03:00
|
|
|
M->setTargetTriple(Ctx->Target.getTargetTriple());
|
|
|
|
TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
|
2007-12-02 04:40:18 +03:00
|
|
|
Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
|
2007-09-16 23:46:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
// If an error occurred, stop code generation, but continue parsing and
|
|
|
|
// semantic analysis (to ensure all warnings and errors are emitted).
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
CodeGen::CodeGenFunction(Builder, FD);
|
|
|
|
} else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
|
|
|
|
CodeGen::CodeGenGlobalVar(Builder, FVD);
|
|
|
|
} else {
|
2007-11-18 00:21:01 +03:00
|
|
|
assert(isa<TypeDecl>(D) && "Only expected type decls here");
|
2007-09-16 23:46:59 +04:00
|
|
|
// don't codegen for now, eventually pass down for debug info.
|
|
|
|
//std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~LLVMEmitter() {
|
|
|
|
CodeGen::Terminate(Builder);
|
|
|
|
|
|
|
|
// Print the generated code.
|
|
|
|
M->print(std::cout);
|
|
|
|
delete M;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-11-28 08:34:05 +03:00
|
|
|
ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags, const LangOptions &Features) {
|
|
|
|
return new LLVMEmitter(Diags, Features);
|
2007-09-16 23:46:59 +04:00
|
|
|
}
|
|
|
|
|