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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-07 14:04:32 +08:00
|
|
|
#include "ASTConsumers.h"
|
2007-12-19 05:34:28 +08:00
|
|
|
#include "clang/AST/TranslationUnit.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"
|
2007-08-22 05:42:03 +08:00
|
|
|
#include "clang/AST/CFG.h"
|
2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2008-01-17 02:18:48 +08:00
|
|
|
#include "clang/Analysis/Analyses/GRConstants.h"
|
2007-09-07 07:00:42 +08:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2007-11-29 05:32:21 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2007-12-24 09:52:34 +08:00
|
|
|
#include <fstream>
|
2007-11-28 05:46:50 +08:00
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// DeclPrinter - Utility class for printing top-level decls.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DeclPrinter {
|
|
|
|
public:
|
2007-11-29 05:32:21 +08:00
|
|
|
std::ostream& Out;
|
2007-08-09 06:51:59 +08:00
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
|
2007-11-29 05:32:21 +08:00
|
|
|
DeclPrinter() : Out(*llvm::cerr.stream()) {}
|
2007-11-28 05:46:50 +08:00
|
|
|
|
2008-01-03 05:04:16 +08:00
|
|
|
void PrintDecl(Decl *D);
|
2007-11-28 05:46:50 +08:00
|
|
|
void PrintFunctionDeclStart(FunctionDecl *FD);
|
|
|
|
void PrintTypeDefDecl(TypedefDecl *TD);
|
2008-01-12 15:05:38 +08:00
|
|
|
void PrintLinkageSpec(LinkageSpecDecl *LS);
|
2008-01-08 03:49:32 +08:00
|
|
|
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);
|
2007-11-28 05:46:50 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-01-03 05:04:16 +08:00
|
|
|
void DeclPrinter:: PrintDecl(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
|
|
|
|
if (FD->getBody()) {
|
|
|
|
Out << ' ';
|
|
|
|
FD->getBody()->printPretty(Out);
|
|
|
|
Out << '\n';
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (isa<ObjCMethodDecl>(D)) {
|
2008-01-03 05:04:16 +08:00
|
|
|
// Do nothing, methods definitions are printed in
|
2008-01-08 03:49:32 +08:00
|
|
|
// PrintObjCImplementationDecl.
|
2008-01-03 05:04:16 +08:00
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
|
|
|
|
PrintObjCInterfaceDecl(OID);
|
|
|
|
} else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
|
|
|
|
PrintObjCProtocolDecl(PID);
|
|
|
|
} else if (ObjCForwardProtocolDecl *OFPD =
|
|
|
|
dyn_cast<ObjCForwardProtocolDecl>(D)) {
|
2008-01-03 05:04:16 +08:00
|
|
|
Out << "@protocol ";
|
|
|
|
for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
|
2008-01-08 03:49:32 +08:00
|
|
|
const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
|
2008-01-03 05:04:16 +08:00
|
|
|
if (i) Out << ", ";
|
|
|
|
Out << D->getName();
|
|
|
|
}
|
|
|
|
Out << ";\n";
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCImplementationDecl *OID =
|
|
|
|
dyn_cast<ObjCImplementationDecl>(D)) {
|
|
|
|
PrintObjCImplementationDecl(OID);
|
|
|
|
} else if (ObjCCategoryImplDecl *OID =
|
|
|
|
dyn_cast<ObjCCategoryImplDecl>(D)) {
|
|
|
|
PrintObjCCategoryImplDecl(OID);
|
|
|
|
} else if (ObjCCategoryDecl *OID =
|
|
|
|
dyn_cast<ObjCCategoryDecl>(D)) {
|
|
|
|
PrintObjCCategoryDecl(OID);
|
|
|
|
} else if (ObjCCompatibleAliasDecl *OID =
|
|
|
|
dyn_cast<ObjCCompatibleAliasDecl>(D)) {
|
|
|
|
PrintObjCCompatibleAliasDecl(OID);
|
|
|
|
} else if (isa<ObjCClassDecl>(D)) {
|
2008-01-03 05:04:16 +08:00
|
|
|
Out << "@class [printing todo]\n";
|
|
|
|
} else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
|
|
|
|
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
|
|
|
|
Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
|
2008-01-12 15:05:38 +08:00
|
|
|
} else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
|
|
|
|
PrintLinkageSpec(LSD);
|
2008-01-03 05:04:16 +08:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown decl type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
|
2007-06-23 08:39:57 +08:00
|
|
|
bool HasBody = FD->getBody();
|
|
|
|
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << '\n';
|
2007-08-26 12:02:13 +08:00
|
|
|
|
|
|
|
switch (FD->getStorageClass()) {
|
|
|
|
default: assert(0 && "Unknown storage class");
|
|
|
|
case FunctionDecl::None: break;
|
2007-11-29 05:32:21 +08:00
|
|
|
case FunctionDecl::Extern: Out << "extern "; break;
|
|
|
|
case FunctionDecl::Static: Out << "static "; break;
|
2007-08-26 12:02:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->isInline())
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "inline ";
|
2007-08-26 12:02:13 +08:00
|
|
|
|
2007-06-23 08:39:57 +08:00
|
|
|
std::string Proto = FD->getName();
|
2007-12-04 05:43:25 +08:00
|
|
|
const FunctionType *AFT = FD->getType()->getAsFunctionType();
|
2007-06-23 08:39:57 +08:00
|
|
|
|
2007-12-04 05:43:25 +08:00
|
|
|
if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
|
2007-06-23 08:39:57 +08: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 05:32:21 +08:00
|
|
|
Out << Proto;
|
2007-06-23 08:39:57 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
if (!FD->getBody())
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << ";\n";
|
2007-08-09 06:51:59 +08:00
|
|
|
// Doesn't print the body.
|
2007-06-23 08:39:57 +08:00
|
|
|
}
|
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
|
2007-06-23 08:39:57 +08:00
|
|
|
std::string S = TD->getName();
|
|
|
|
TD->getUnderlyingType().getAsStringInternal(S);
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "typedef " << S << ";\n";
|
2007-06-23 08:39:57 +08:00
|
|
|
}
|
|
|
|
|
2008-01-12 15:05:38 +08:00
|
|
|
void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
|
|
|
|
const char *l;
|
|
|
|
if (LS->getLanguage() == LinkageSpecDecl::lang_c)
|
|
|
|
l = "C";
|
|
|
|
else if (LS->getLanguage() == LinkageSpecDecl::lang_cxx)
|
|
|
|
l = "C++";
|
|
|
|
else assert(0 && "unknown language in linkage specification");
|
|
|
|
Out << "extern \"" << l << "\" { ";
|
|
|
|
PrintDecl(LS->getDecl());
|
|
|
|
Out << "}\n";
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
|
2007-11-11 04:59:13 +08:00
|
|
|
if (OMD->isInstance())
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "\n- ";
|
2007-11-11 04:59:13 +08:00
|
|
|
else
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "\n+ ";
|
2007-11-11 04:59:13 +08:00
|
|
|
if (!OMD->getResultType().isNull())
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << '(' << OMD->getResultType().getAsString() << ") ";
|
2007-11-11 04:59:13 +08:00
|
|
|
// FIXME: just print original selector name!
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << OMD->getSelector().getName();
|
2007-11-11 04:59:13 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < OMD->getNumParams(); i++) {
|
|
|
|
ParmVarDecl *PDecl = OMD->getParamDecl(i);
|
2007-11-29 05:32:21 +08:00
|
|
|
// FIXME: selector is missing here!
|
|
|
|
Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
|
2007-11-11 04:59:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
|
2007-11-11 04:59:13 +08:00
|
|
|
std::string I = OID->getName();
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *SID = OID->getSuperClass();
|
2007-11-29 05:32:21 +08:00
|
|
|
|
|
|
|
if (SID)
|
|
|
|
Out << "@implementation " << I << " : " << SID->getName();
|
2007-11-11 04:59:13 +08:00
|
|
|
else
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@implementation " << I;
|
2007-11-11 04:59:13 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
|
2007-12-12 15:46:12 +08:00
|
|
|
E = OID->instmeth_end(); I != E; ++I) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *OMD = *I;
|
|
|
|
PrintObjCMethodDecl(OMD);
|
2007-11-11 04:59:13 +08:00
|
|
|
if (OMD->getBody()) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << ' ';
|
|
|
|
OMD->getBody()->printPretty(Out);
|
|
|
|
Out << '\n';
|
2007-11-11 04:59:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
|
2007-12-12 15:46:12 +08:00
|
|
|
E = OID->classmeth_end(); I != E; ++I) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *OMD = *I;
|
|
|
|
PrintObjCMethodDecl(OMD);
|
2007-11-11 04:59:13 +08:00
|
|
|
if (OMD->getBody()) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << ' ';
|
|
|
|
OMD->getBody()->printPretty(Out);
|
|
|
|
Out << '\n';
|
2007-11-11 04:59:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@end\n";
|
2007-11-11 04:59:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
|
2007-10-09 07:06:41 +08:00
|
|
|
std::string I = OID->getName();
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl *SID = OID->getSuperClass();
|
2007-11-29 05:32:21 +08:00
|
|
|
|
|
|
|
if (SID)
|
|
|
|
Out << "@interface " << I << " : " << SID->getName();
|
2007-10-09 07:06:41 +08:00
|
|
|
else
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@interface " << I;
|
|
|
|
|
2007-10-09 07:06:41 +08:00
|
|
|
// Protocols?
|
|
|
|
int count = OID->getNumIntfRefProtocols();
|
2007-11-29 05:32:21 +08:00
|
|
|
|
2007-10-09 07:06:41 +08:00
|
|
|
if (count > 0) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl **refProtocols = OID->getReferencedProtocols();
|
2007-10-09 07:06:41 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
|
2007-10-09 07:06:41 +08:00
|
|
|
}
|
2007-11-29 05:32:21 +08:00
|
|
|
|
2007-10-09 07:06:41 +08:00
|
|
|
if (count > 0)
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << ">\n";
|
2007-10-09 07:06:41 +08:00
|
|
|
else
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << '\n';
|
2007-10-27 00:29:12 +08:00
|
|
|
|
2007-12-12 15:56:42 +08:00
|
|
|
if (OID->getNumInstanceVariables() > 0) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << '{';
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
|
2007-12-12 15:56:42 +08:00
|
|
|
E = OID->ivar_end(); I != E; ++I) {
|
|
|
|
Out << '\t' << (*I)->getType().getAsString()
|
|
|
|
<< ' ' << (*I)->getName() << ";\n";
|
2007-10-27 00:29:12 +08:00
|
|
|
}
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "}\n";
|
2007-10-27 00:29:12 +08:00
|
|
|
}
|
2007-11-07 06:01:00 +08:00
|
|
|
|
|
|
|
int NumProperties = OID->getNumPropertyDecl();
|
|
|
|
if (NumProperties > 0) {
|
|
|
|
for (int i = 0; i < NumProperties; i++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCPropertyDecl *PDecl = OID->getPropertyDecl()[i];
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@property";
|
2008-01-08 03:49:32 +08:00
|
|
|
if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
|
2007-11-07 06:01:00 +08:00
|
|
|
bool first = true;
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << " (";
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readonly) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "readonly";
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "getter = "
|
|
|
|
<< PDecl->getGetterName()->getName();
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "setter = "
|
|
|
|
<< PDecl->getSetterName()->getName();
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "assign";
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readwrite) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "readwrite";
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "retain";
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "copy";
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2008-01-10 09:43:14 +08:00
|
|
|
if (PDecl->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_nonatomic) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << (first ? ' ' : ',') << "nonatomic";
|
2007-11-07 06:01:00 +08:00
|
|
|
first = false;
|
|
|
|
}
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << " )";
|
2007-11-07 06:01:00 +08:00
|
|
|
}
|
2007-11-29 05:32:21 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIvarDecl **IDecl = PDecl->getPropertyDecls();
|
2007-11-29 05:32:21 +08:00
|
|
|
|
|
|
|
Out << ' ' << IDecl[0]->getType().getAsString()
|
|
|
|
<< ' ' << IDecl[0]->getName();
|
|
|
|
|
|
|
|
for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
|
|
|
|
Out << ", " << IDecl[j]->getName();
|
2007-11-07 06:01:00 +08:00
|
|
|
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << ";\n";
|
2007-11-07 06:01:00 +08:00
|
|
|
}
|
|
|
|
}
|
2007-11-29 05:32:21 +08:00
|
|
|
|
|
|
|
Out << "@end\n";
|
2007-09-11 04:51:04 +08:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@protocol " << PID->getName() << '\n';
|
2007-10-09 02:53:38 +08:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@implementation "
|
|
|
|
<< PID->getClassInterface()->getName()
|
|
|
|
<< '(' << PID->getName() << ");\n";
|
|
|
|
|
2007-10-09 02:53:38 +08:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@interface "
|
|
|
|
<< PID->getClassInterface()->getName()
|
|
|
|
<< '(' << PID->getName() << ");\n";
|
2007-10-09 02:53:38 +08:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "@compatibility_alias " << AID->getName()
|
|
|
|
<< ' ' << AID->getClassInterface()->getName() << ";\n";
|
2007-10-12 07:42:27 +08:00
|
|
|
}
|
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTPrinter - Pretty-printer of ASTs
|
|
|
|
|
2007-09-16 07:02:28 +08:00
|
|
|
namespace {
|
2007-11-28 05:46:50 +08:00
|
|
|
class ASTPrinter : public ASTConsumer, public DeclPrinter {
|
|
|
|
public:
|
2007-11-29 05:32:21 +08:00
|
|
|
ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
|
2007-11-28 05:46:50 +08:00
|
|
|
|
2007-09-16 07:02:28 +08:00
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
2008-01-03 05:04:16 +08:00
|
|
|
PrintDecl(D);
|
2007-06-23 08:39:57 +08:00
|
|
|
}
|
2007-09-16 07:02:28 +08:00
|
|
|
};
|
2007-06-23 08:39:57 +08:00
|
|
|
}
|
2007-08-09 06:51:59 +08:00
|
|
|
|
2007-11-29 05:32:21 +08:00
|
|
|
ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
|
|
|
|
return new ASTPrinter(out);
|
|
|
|
}
|
2007-11-28 05:46:50 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTDumper - Low-level dumper of ASTs
|
2007-09-16 07:02:28 +08:00
|
|
|
|
|
|
|
namespace {
|
2007-11-28 05:46:50 +08:00
|
|
|
class ASTDumper : public ASTConsumer, public DeclPrinter {
|
2007-09-16 07:02:28 +08:00
|
|
|
SourceManager *SM;
|
|
|
|
public:
|
2007-11-29 05:32:21 +08:00
|
|
|
ASTDumper() : DeclPrinter() {}
|
2007-11-28 05:46:50 +08:00
|
|
|
|
2007-12-20 06:51:13 +08:00
|
|
|
void Initialize(ASTContext &Context) {
|
2007-12-12 05:27:55 +08:00
|
|
|
SM = &Context.getSourceManager();
|
2007-09-16 07:02:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
|
|
|
|
if (FD->getBody()) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << '\n';
|
|
|
|
// FIXME: convert dumper to use std::ostream?
|
2007-09-16 07:02:28 +08:00
|
|
|
FD->getBody()->dumpAll(*SM);
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << '\n';
|
2007-09-16 07:02:28 +08:00
|
|
|
}
|
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
|
|
|
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "Read objc interface '" << OID->getName() << "'\n";
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "Read objc protocol '" << OPD->getName() << "'\n";
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "Read objc category '" << OCD->getName() << "'\n";
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (isa<ObjCForwardProtocolDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "Read objc fwd protocol decl\n";
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (isa<ObjCClassDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
Out << "Read objc fwd class decl\n";
|
2007-10-07 02:52:10 +08:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown decl type!");
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 07:02:28 +08:00
|
|
|
};
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
2007-09-16 07:02:28 +08:00
|
|
|
ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
|
|
|
|
|
2007-11-28 05:46:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ASTViewer - AST Visualization
|
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
namespace {
|
|
|
|
class ASTViewer : public ASTConsumer {
|
|
|
|
SourceManager *SM;
|
|
|
|
public:
|
2007-12-20 06:51:13 +08:00
|
|
|
void Initialize(ASTContext &Context) {
|
2007-12-12 05:27:55 +08:00
|
|
|
SM = &Context.getSourceManager();
|
2007-09-20 05:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2007-11-29 05:32:21 +08:00
|
|
|
DeclPrinter().PrintFunctionDeclStart(FD);
|
2007-09-20 05:29:43 +08:00
|
|
|
|
|
|
|
if (FD->getBody()) {
|
2007-11-29 05:32:21 +08:00
|
|
|
llvm::cerr << '\n';
|
2007-09-20 05:29:43 +08:00
|
|
|
FD->getBody()->viewAST();
|
2007-11-29 05:32:21 +08:00
|
|
|
llvm::cerr << '\n';
|
2007-09-20 05:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
|
|
|
|
|
|
|
|
|
2007-09-08 07:47:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
|
|
|
|
// the CFGs for all function definitions.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-09-16 07:21:08 +08:00
|
|
|
class CFGVisitor : public ASTConsumer {
|
2007-09-08 07:47:56 +08:00
|
|
|
public:
|
2007-09-16 07:21:08 +08:00
|
|
|
// CFG Visitor interface to be implemented by subclass.
|
2007-09-08 07:47:56 +08:00
|
|
|
virtual void VisitCFG(CFG& C) = 0;
|
|
|
|
virtual bool printFuncDeclStart() { return true; }
|
2007-09-16 07:21:08 +08:00
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D);
|
2007-09-08 07:47:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-09-16 07:21:08 +08:00
|
|
|
void CFGVisitor::HandleTopLevelDecl(Decl *D) {
|
|
|
|
FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
|
|
|
|
if (!FD || !FD->getBody())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (printFuncDeclStart()) {
|
2007-11-29 05:32:21 +08:00
|
|
|
DeclPrinter().PrintFunctionDeclStart(FD);
|
|
|
|
llvm::cerr << '\n';
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
2007-09-16 07:21:08 +08:00
|
|
|
|
2007-09-18 01:10:02 +08:00
|
|
|
CFG *C = CFG::buildCFG(FD->getBody());
|
|
|
|
VisitCFG(*C);
|
|
|
|
delete C;
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
|
|
|
|
2007-09-08 07:47:56 +08: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 07:21:08 +08:00
|
|
|
virtual void VisitCFG(CFG &C) {
|
|
|
|
if (UseGraphviz)
|
|
|
|
C.viewCFG();
|
|
|
|
else
|
|
|
|
C.dump();
|
|
|
|
}
|
2007-09-08 07:47:56 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-06 08:17:54 +08:00
|
|
|
|
2007-09-16 07:21:08 +08:00
|
|
|
ASTConsumer *clang::CreateCFGDumper(bool ViewGraphs) {
|
|
|
|
return new CFGDumper(ViewGraphs);
|
2007-09-06 08:17:54 +08:00
|
|
|
}
|
|
|
|
|
2007-09-08 07:47:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalyzeLiveVariables - perform live variable analysis and dump results
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LivenessVisitor : public CFGVisitor {
|
2007-09-16 07:21:08 +08:00
|
|
|
SourceManager *SM;
|
2007-09-08 07:47:56 +08:00
|
|
|
public:
|
2007-12-20 06:51:13 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
2007-12-12 05:27:55 +08:00
|
|
|
SM = &Context.getSourceManager();
|
2007-09-16 07:21:08 +08:00
|
|
|
}
|
|
|
|
|
2007-09-08 07:47:56 +08:00
|
|
|
virtual void VisitCFG(CFG& C) {
|
2007-10-02 04:33:52 +08:00
|
|
|
LiveVariables L(C);
|
2007-09-08 07:47:56 +08:00
|
|
|
L.runOnCFG(C);
|
2007-09-25 12:31:27 +08:00
|
|
|
L.dumpBlockLiveness(*SM);
|
2007-09-07 07:00:42 +08:00
|
|
|
}
|
2007-09-08 07:47:56 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-07 07:00:42 +08:00
|
|
|
|
2007-09-16 07:21:08 +08:00
|
|
|
ASTConsumer *clang::CreateLiveVarAnalyzer() {
|
|
|
|
return new LivenessVisitor();
|
2007-09-07 07:00:42 +08:00
|
|
|
}
|
|
|
|
|
2007-09-08 07:47:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-18 04:49:30 +08:00
|
|
|
// DeadStores - run checker to locate dead stores in a function
|
2007-09-08 07:47:56 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DeadStoreVisitor : public CFGVisitor {
|
2007-09-16 07:21:08 +08:00
|
|
|
Diagnostic &Diags;
|
|
|
|
ASTContext *Ctx;
|
2007-09-08 07:47:56 +08:00
|
|
|
public:
|
2007-09-16 07:21:08 +08:00
|
|
|
DeadStoreVisitor(Diagnostic &diags) : Diags(diags) {}
|
2007-12-20 06:51:13 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
2007-09-16 07:21:08 +08:00
|
|
|
Ctx = &Context;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); }
|
2007-09-08 07:54:15 +08:00
|
|
|
virtual bool printFuncDeclStart() { return false; }
|
2007-09-08 07:47:56 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-08-09 06:51:59 +08:00
|
|
|
|
2007-09-16 07:21:08 +08:00
|
|
|
ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
|
|
|
|
return new DeadStoreVisitor(Diags);
|
2007-09-08 07:47:56 +08:00
|
|
|
}
|
2007-09-17 03:46:59 +08:00
|
|
|
|
2007-09-18 04:49:30 +08: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) {}
|
2007-12-20 06:51:13 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
2007-09-18 04:49:30 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-01-09 02:04:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-17 02:18:48 +08:00
|
|
|
// GRConstants - Perform intra-procedural, path-sensitive constant propagation.
|
2008-01-09 02:04:06 +08:00
|
|
|
|
|
|
|
namespace {
|
2008-01-17 02:18:48 +08:00
|
|
|
class GRConstantsVisitor : public CFGVisitor {
|
2008-01-24 10:02:54 +08:00
|
|
|
ASTContext* Ctx;
|
2008-01-09 02:04:06 +08:00
|
|
|
public:
|
2008-01-24 10:02:54 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) { Ctx = &Context; }
|
2008-01-09 02:04:06 +08:00
|
|
|
|
|
|
|
virtual void VisitCFG(CFG& C) {
|
2008-01-24 10:02:54 +08:00
|
|
|
RunGRConstants(C, *Ctx);
|
2008-01-09 02:04:06 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-01-24 10:02:54 +08:00
|
|
|
ASTConsumer* clang::CreateGRConstants() {
|
2008-01-17 02:18:48 +08:00
|
|
|
return new GRConstantsVisitor();
|
2008-01-09 02:04:06 +08:00
|
|
|
}
|
|
|
|
|
2007-09-17 03:46:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLVM Emitter
|
|
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-11-01 04:01:01 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2007-09-17 03:46:59 +08:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
|
|
|
#include "llvm/Module.h"
|
2007-11-01 04:01:01 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2007-12-24 09:52:34 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2007-09-17 03:46:59 +08:00
|
|
|
|
|
|
|
namespace {
|
2007-12-24 09:52:34 +08:00
|
|
|
class CodeGenerator : public ASTConsumer {
|
2007-09-17 03:46:59 +08:00
|
|
|
Diagnostic &Diags;
|
2007-11-01 04:01:01 +08:00
|
|
|
const llvm::TargetData *TD;
|
2007-09-17 03:46:59 +08:00
|
|
|
ASTContext *Ctx;
|
2007-11-28 13:34:05 +08:00
|
|
|
const LangOptions &Features;
|
2007-12-24 09:52:34 +08:00
|
|
|
protected:
|
|
|
|
llvm::Module *M;
|
2007-11-14 02:16:41 +08:00
|
|
|
CodeGen::CodeGenModule *Builder;
|
2007-09-17 03:46:59 +08:00
|
|
|
public:
|
2007-12-24 09:52:34 +08:00
|
|
|
CodeGenerator(Diagnostic &diags, const LangOptions &LO)
|
2007-11-28 13:34:05 +08:00
|
|
|
: Diags(diags)
|
|
|
|
, Features(LO) {}
|
2007-12-20 06:51:13 +08:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
2007-09-17 03:46:59 +08:00
|
|
|
Ctx = &Context;
|
|
|
|
M = new llvm::Module("foo");
|
2007-11-01 04:01:01 +08:00
|
|
|
M->setTargetTriple(Ctx->Target.getTargetTriple());
|
2007-12-14 01:34:31 +08:00
|
|
|
M->setDataLayout(Ctx->Target.getTargetDescription());
|
2007-11-01 04:01:01 +08:00
|
|
|
TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
|
2007-12-02 09:40:18 +08:00
|
|
|
Builder = CodeGen::Init(Context, Features, *M, *TD, Diags);
|
2007-09-17 03:46:59 +08: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);
|
2008-01-12 15:05:38 +08:00
|
|
|
} else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
|
|
|
|
CodeGen::CodeGenLinkageSpec(Builder, LSD);
|
2007-09-17 03:46:59 +08:00
|
|
|
} else {
|
2007-11-18 05:21:01 +08:00
|
|
|
assert(isa<TypeDecl>(D) && "Only expected type decls here");
|
2007-09-17 03:46:59 +08:00
|
|
|
// don't codegen for now, eventually pass down for debug info.
|
2007-12-20 07:49:37 +08:00
|
|
|
//std::cerr << "Read top-level typedef decl: '"
|
|
|
|
// << D->getName() << "'\n";
|
2007-09-17 03:46:59 +08:00
|
|
|
}
|
|
|
|
}
|
2007-12-24 09:52:34 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LLVMEmitter : public CodeGenerator {
|
|
|
|
public:
|
|
|
|
LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
|
|
|
|
: CodeGenerator(diags,LO) {}
|
|
|
|
|
2007-09-17 03:46:59 +08:00
|
|
|
~LLVMEmitter() {
|
|
|
|
CodeGen::Terminate(Builder);
|
|
|
|
|
|
|
|
// Print the generated code.
|
2007-12-14 01:50:11 +08:00
|
|
|
M->print(llvm::cout.stream());
|
2007-09-17 03:46:59 +08:00
|
|
|
delete M;
|
|
|
|
}
|
2007-12-24 09:52:34 +08:00
|
|
|
};
|
|
|
|
}
|
2007-09-17 03:46:59 +08:00
|
|
|
|
2007-12-20 07:49:37 +08:00
|
|
|
ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags,
|
|
|
|
const LangOptions &Features) {
|
2007-11-28 13:34:05 +08:00
|
|
|
return new LLVMEmitter(Diags, Features);
|
2007-09-17 03:46:59 +08:00
|
|
|
}
|
|
|
|
|
2007-12-24 09:52:34 +08:00
|
|
|
namespace {
|
|
|
|
class BCWriter : public CodeGenerator {
|
|
|
|
public:
|
|
|
|
std::ostream& Out;
|
|
|
|
|
|
|
|
BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO)
|
|
|
|
: CodeGenerator(diags,LO)
|
|
|
|
, Out(*out) {}
|
|
|
|
|
|
|
|
~BCWriter() {
|
|
|
|
CodeGen::Terminate(Builder);
|
|
|
|
llvm::WriteBitcodeToFile(M, Out);
|
|
|
|
delete M;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateBCWriter(const std::string& InFile,
|
|
|
|
const std::string& OutputFile,
|
|
|
|
Diagnostic &Diags,
|
|
|
|
const LangOptions &Features) {
|
|
|
|
std::string FileName = OutputFile;
|
2007-12-24 11:23:55 +08:00
|
|
|
|
|
|
|
std::ostream *Out;
|
2007-12-25 04:59:36 +08:00
|
|
|
if (OutputFile == "-")
|
2007-12-25 04:56:07 +08:00
|
|
|
Out = llvm::cout.stream();
|
|
|
|
else if (!OutputFile.size()) {
|
2007-12-25 04:59:36 +08:00
|
|
|
if (InFile == "-")
|
|
|
|
Out = llvm::cout.stream();
|
|
|
|
else {
|
|
|
|
llvm::sys::Path Path(InFile);
|
|
|
|
Path.eraseSuffix();
|
|
|
|
Path.appendSuffix("bc");
|
|
|
|
FileName = Path.toString();
|
2007-12-25 07:49:33 +08:00
|
|
|
Out = new std::ofstream(FileName.c_str(),
|
|
|
|
std::ios_base::binary|std::ios_base::out);
|
2007-12-25 04:59:36 +08:00
|
|
|
}
|
2007-12-24 11:23:55 +08:00
|
|
|
} else {
|
2007-12-25 07:49:33 +08:00
|
|
|
Out = new std::ofstream(FileName.c_str(),
|
|
|
|
std::ios_base::binary|std::ios_base::out);
|
2007-12-24 09:52:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new BCWriter(Out, Diags, Features);
|
|
|
|
}
|
|
|
|
|
2007-12-13 08:37:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AST Serializer
|
|
|
|
|
|
|
|
namespace {
|
2007-12-20 07:49:37 +08:00
|
|
|
|
|
|
|
class ASTSerializer : public ASTConsumer {
|
|
|
|
protected:
|
|
|
|
Diagnostic &Diags;
|
|
|
|
TranslationUnit TU;
|
|
|
|
public:
|
|
|
|
ASTSerializer(Diagnostic& diags, const LangOptions& LO)
|
|
|
|
: Diags(diags), TU(LO) {}
|
|
|
|
|
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
TU.setContext(&Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
2007-12-13 08:37:31 +08:00
|
|
|
|
2007-12-20 07:49:37 +08:00
|
|
|
TU.AddTopLevelDecl(D);
|
|
|
|
}
|
|
|
|
};
|
2007-12-13 08:37:31 +08:00
|
|
|
|
2007-12-20 07:49:37 +08:00
|
|
|
class SingleFileSerializer : public ASTSerializer {
|
|
|
|
const llvm::sys::Path FName;
|
|
|
|
public:
|
|
|
|
SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
|
|
|
|
const LangOptions &LO)
|
|
|
|
: ASTSerializer(diags,LO), FName(F) {}
|
|
|
|
|
|
|
|
~SingleFileSerializer() {
|
|
|
|
EmitASTBitcodeFile(TU,FName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BuildSerializer : public ASTSerializer {
|
|
|
|
llvm::sys::Path EmitDir;
|
|
|
|
public:
|
|
|
|
BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
|
|
|
|
const LangOptions &LO)
|
|
|
|
: ASTSerializer(diags,LO), EmitDir(dir) {}
|
|
|
|
|
2007-12-20 08:34:58 +08:00
|
|
|
~BuildSerializer() {
|
|
|
|
SourceManager& SourceMgr = TU.getASTContext()->getSourceManager();
|
|
|
|
unsigned ID = SourceMgr.getMainFileID();
|
|
|
|
assert (ID && "MainFileID not set!");
|
|
|
|
const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
|
|
|
|
assert (FE && "No FileEntry for main file.");
|
|
|
|
|
|
|
|
// FIXME: This is not portable to Windows.
|
|
|
|
// FIXME: This logic should probably be moved elsewhere later.
|
|
|
|
|
2007-12-21 03:47:16 +08:00
|
|
|
llvm::sys::Path FName(EmitDir);
|
2007-12-20 08:34:58 +08:00
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
buf.reserve(strlen(FE->getName())+100);
|
|
|
|
|
|
|
|
sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
|
2007-12-21 03:47:16 +08:00
|
|
|
FName.appendComponent(&buf[0]);
|
|
|
|
FName.createDirectoryOnDisk(true);
|
|
|
|
if (!FName.canWrite() || !FName.isDirectory()) {
|
2007-12-20 08:34:58 +08:00
|
|
|
assert (false && "Could not create 'device' serialization directory.");
|
|
|
|
return;
|
|
|
|
}
|
2007-12-21 03:47:16 +08:00
|
|
|
|
2007-12-20 08:34:58 +08:00
|
|
|
sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
|
2007-12-21 03:47:16 +08:00
|
|
|
FName.appendComponent(&buf[0]);
|
|
|
|
EmitASTBitcodeFile(TU,FName);
|
|
|
|
|
|
|
|
// Now emit the sources.
|
2007-12-20 08:34:58 +08:00
|
|
|
|
|
|
|
}
|
2007-12-20 07:49:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-12-13 08:37:31 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2007-12-20 06:24:34 +08:00
|
|
|
ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
|
2007-12-20 07:49:37 +08:00
|
|
|
const std::string& OutputFile,
|
2007-12-13 08:37:31 +08:00
|
|
|
Diagnostic &Diags,
|
|
|
|
const LangOptions &Features) {
|
2007-12-20 01:25:59 +08:00
|
|
|
|
2007-12-20 07:49:37 +08:00
|
|
|
if (OutputFile.size()) {
|
2007-12-20 08:34:58 +08:00
|
|
|
if (InFile == "-") {
|
|
|
|
llvm::cerr <<
|
|
|
|
"error: Cannot use --serialize with -o for source read from STDIN.\n";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 07:49:37 +08:00
|
|
|
// The user specified an AST-emission directory. Determine if the path
|
|
|
|
// is absolute.
|
|
|
|
llvm::sys::Path EmitDir(OutputFile);
|
|
|
|
|
|
|
|
if (!EmitDir.isAbsolute()) {
|
|
|
|
llvm::cerr <<
|
|
|
|
"error: Output directory for --serialize must be an absolute path.\n";
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the directory if it does not exist.
|
|
|
|
EmitDir.createDirectoryOnDisk(true);
|
|
|
|
if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
|
|
|
|
llvm::cerr <<
|
|
|
|
"error: Could not create output directory for --serialize.\n";
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 08:34:58 +08:00
|
|
|
// FIXME: We should probably only allow using BuildSerializer when
|
|
|
|
// the ASTs come from parsed source files, and not from .ast files.
|
2007-12-20 07:49:37 +08:00
|
|
|
return new BuildSerializer(EmitDir, Diags, Features);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user did not specify an output directory for serialized ASTs.
|
|
|
|
// Serialize the translation to a single file whose name is the same
|
|
|
|
// as the input file with the ".ast" extension appended.
|
2007-12-20 01:25:59 +08:00
|
|
|
|
2007-12-20 07:49:37 +08:00
|
|
|
llvm::sys::Path FName(InFile.c_str());
|
2007-12-20 08:34:58 +08:00
|
|
|
FName.appendSuffix("ast");
|
2007-12-20 07:49:37 +08:00
|
|
|
return new SingleFileSerializer(FName, Diags, Features);
|
2007-12-13 08:37:31 +08:00
|
|
|
}
|