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-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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"
|
2008-11-04 01:31:48 +03:00
|
|
|
#include "clang/Driver/PathDiagnosticClients.h"
|
2007-12-19 00:34:28 +03:00
|
|
|
#include "clang/AST/TranslationUnit.h"
|
2008-08-09 22:32:11 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-12-20 03:34:58 +03:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.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"
|
2008-08-05 22:50:11 +04:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
|
|
|
#include "llvm/Module.h"
|
2008-10-22 03:54:00 +04:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-02-19 00:21:23 +03:00
|
|
|
#include "llvm/Support/Timer.h"
|
2008-09-13 09:16:45 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-03-31 22:26:32 +04: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:
|
2008-09-13 09:16:45 +04:00
|
|
|
llvm::raw_ostream& Out;
|
2007-08-09 02:51:59 +04:00
|
|
|
|
2008-09-13 09:16:45 +04:00
|
|
|
DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {}
|
|
|
|
DeclPrinter() : Out(llvm::errs()) {}
|
|
|
|
virtual ~DeclPrinter();
|
2007-11-28 00:46:50 +03:00
|
|
|
|
2008-01-03 00:04:16 +03:00
|
|
|
void PrintDecl(Decl *D);
|
2007-11-28 00:46:50 +03:00
|
|
|
void PrintFunctionDeclStart(FunctionDecl *FD);
|
|
|
|
void PrintTypeDefDecl(TypedefDecl *TD);
|
2008-01-12 10:05:38 +03:00
|
|
|
void PrintLinkageSpec(LinkageSpecDecl *LS);
|
2008-01-07 22:49:32 +03: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);
|
2008-04-17 22:25:18 +04:00
|
|
|
void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
|
2008-04-23 04:06:01 +04:00
|
|
|
void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
|
2007-11-28 00:46:50 +03:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-09-13 09:16:45 +04:00
|
|
|
DeclPrinter::~DeclPrinter() {
|
|
|
|
Out.flush();
|
|
|
|
}
|
|
|
|
|
2008-01-03 00:04:16 +03: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-07 22:49:32 +03:00
|
|
|
} else if (isa<ObjCMethodDecl>(D)) {
|
2008-01-03 00:04:16 +03:00
|
|
|
// Do nothing, methods definitions are printed in
|
2008-01-07 22:49:32 +03:00
|
|
|
// PrintObjCImplementationDecl.
|
2008-01-03 00:04:16 +03:00
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
2008-01-07 22:49:32 +03: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 =
|
2008-02-26 00:04:36 +03:00
|
|
|
dyn_cast<ObjCForwardProtocolDecl>(D)) {
|
2008-01-03 00:04:16 +03:00
|
|
|
Out << "@protocol ";
|
|
|
|
for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
|
2008-01-07 22:49:32 +03:00
|
|
|
const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
|
2008-01-03 00:04:16 +03:00
|
|
|
if (i) Out << ", ";
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << D->getNameAsString();
|
2008-01-03 00:04:16 +03:00
|
|
|
}
|
|
|
|
Out << ";\n";
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (ObjCImplementationDecl *OID =
|
2008-02-26 00:04:36 +03:00
|
|
|
dyn_cast<ObjCImplementationDecl>(D)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
PrintObjCImplementationDecl(OID);
|
|
|
|
} else if (ObjCCategoryImplDecl *OID =
|
2008-02-26 00:04:36 +03:00
|
|
|
dyn_cast<ObjCCategoryImplDecl>(D)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
PrintObjCCategoryImplDecl(OID);
|
|
|
|
} else if (ObjCCategoryDecl *OID =
|
2008-02-26 00:04:36 +03:00
|
|
|
dyn_cast<ObjCCategoryDecl>(D)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
PrintObjCCategoryDecl(OID);
|
|
|
|
} else if (ObjCCompatibleAliasDecl *OID =
|
2008-02-26 00:04:36 +03:00
|
|
|
dyn_cast<ObjCCompatibleAliasDecl>(D)) {
|
2008-01-07 22:49:32 +03:00
|
|
|
PrintObjCCompatibleAliasDecl(OID);
|
2008-06-22 01:40:20 +04:00
|
|
|
} else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
|
|
|
|
Out << "@class ";
|
|
|
|
ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
|
|
|
|
for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
|
|
|
|
const ObjCInterfaceDecl *D = ForwardDecls[i];
|
|
|
|
if (i) Out << ", ";
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << D->getNameAsString();
|
2008-06-22 01:40:20 +04:00
|
|
|
}
|
|
|
|
Out << ";\n";
|
2008-12-17 05:04:30 +03:00
|
|
|
} else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
|
|
|
|
Out << "enum " << ED->getNameAsString() << " {\n";
|
|
|
|
for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
|
|
|
|
EEnd = ED->enumerator_end();
|
|
|
|
E != EEnd; ++E)
|
|
|
|
Out << " " << (*E)->getNameAsString() << ",\n";
|
|
|
|
Out << "};\n";
|
2008-01-03 00:04:16 +03:00
|
|
|
} else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "Read top-level tag decl: '" << TD->getNameAsString() << "'\n";
|
2008-01-03 00:04:16 +03:00
|
|
|
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "Read top-level variable decl: '" << SD->getNameAsString() << "'\n";
|
2008-01-12 10:05:38 +03:00
|
|
|
} else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
|
|
|
|
PrintLinkageSpec(LSD);
|
2008-02-08 03:33:21 +03:00
|
|
|
} else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
|
|
|
|
Out << "asm(";
|
|
|
|
AD->getAsmString()->printPretty(Out);
|
|
|
|
Out << ")\n";
|
2008-01-03 00:04:16 +03:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown decl type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-28 00:46:50 +03:00
|
|
|
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;
|
2008-04-15 07:57:09 +04:00
|
|
|
case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; 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
|
|
|
|
2008-11-24 08:29:24 +03:00
|
|
|
std::string Proto = FD->getNameAsString();
|
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;
|
2008-11-24 08:29:24 +03:00
|
|
|
if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
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) {
|
2008-11-24 08:29:24 +03:00
|
|
|
std::string S = TD->getNameAsString();
|
2007-07-11 21:01:13 +04:00
|
|
|
TD->getUnderlyingType().getAsStringInternal(S);
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "typedef " << S << ";\n";
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-01-12 10:05:38 +03:00
|
|
|
void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
|
|
|
|
const char *l;
|
|
|
|
if (LS->getLanguage() == LinkageSpecDecl::lang_c)
|
|
|
|
l = "C";
|
2008-04-08 09:52:18 +04:00
|
|
|
else {
|
|
|
|
assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
|
|
|
|
"unknown language in linkage specification");
|
2008-01-12 10:05:38 +03:00
|
|
|
l = "C++";
|
2008-04-08 09:52:18 +04:00
|
|
|
}
|
2008-12-17 01:23:02 +03:00
|
|
|
|
|
|
|
Out << "extern \"" << l << "\" ";
|
|
|
|
if (LS->hasBraces())
|
|
|
|
Out << "{\n";
|
|
|
|
|
|
|
|
for (LinkageSpecDecl::decl_const_iterator D = LS->decls_begin(),
|
|
|
|
DEnd = LS->decls_end();
|
|
|
|
D != DEnd; ++D)
|
|
|
|
PrintDecl(*D);
|
|
|
|
|
|
|
|
if (LS->hasBraces())
|
|
|
|
Out << "}";
|
|
|
|
Out << "\n";
|
2008-01-12 10:05:38 +03:00
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +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())
|
2008-08-22 10:59:15 +04:00
|
|
|
Out << '(' << OMD->getResultType().getAsString() << ")";
|
2007-11-10 23:59:13 +03:00
|
|
|
|
2008-11-24 06:33:13 +03:00
|
|
|
std::string name = OMD->getSelector().getAsString();
|
2008-08-22 10:59:15 +04:00
|
|
|
std::string::size_type pos, lastPos = 0;
|
2008-03-16 04:07:14 +03:00
|
|
|
for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
|
2007-11-10 23:59:13 +03:00
|
|
|
ParmVarDecl *PDecl = OMD->getParamDecl(i);
|
2007-11-29 00:32:21 +03:00
|
|
|
// FIXME: selector is missing here!
|
2008-08-22 10:59:15 +04:00
|
|
|
pos = name.find_first_of(":", lastPos);
|
|
|
|
Out << " " << name.substr(lastPos, pos - lastPos);
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << ":(" << PDecl->getType().getAsString() << ")"
|
|
|
|
<< PDecl->getNameAsString();
|
2008-08-22 10:59:15 +04:00
|
|
|
lastPos = pos + 1;
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
2008-08-22 10:59:15 +04:00
|
|
|
|
|
|
|
if (OMD->getNumParams() == 0)
|
|
|
|
Out << " " << name;
|
|
|
|
|
|
|
|
if (OMD->isVariadic())
|
|
|
|
Out << ", ...";
|
|
|
|
|
|
|
|
Out << ";";
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
|
2008-11-24 08:29:24 +03:00
|
|
|
std::string I = OID->getNameAsString();
|
2008-01-07 22:49:32 +03:00
|
|
|
ObjCInterfaceDecl *SID = OID->getSuperClass();
|
2007-11-29 00:32:21 +03:00
|
|
|
|
|
|
|
if (SID)
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "@implementation " << I << " : " << SID->getNameAsString();
|
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
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
|
2007-12-12 10:46:12 +03:00
|
|
|
E = OID->instmeth_end(); I != E; ++I) {
|
2008-01-07 22:49:32 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
|
2007-12-12 10:46:12 +03:00
|
|
|
E = OID->classmeth_end(); I != E; ++I) {
|
2008-01-07 22:49:32 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-23 04:06:01 +04:00
|
|
|
for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
|
|
|
|
E = OID->propimpl_end(); I != E; ++I)
|
|
|
|
PrintObjCPropertyImplDecl(*I);
|
|
|
|
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@end\n";
|
2007-11-10 23:59:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
|
2008-11-24 08:29:24 +03:00
|
|
|
std::string I = OID->getNameAsString();
|
2008-01-07 22:49:32 +03:00
|
|
|
ObjCInterfaceDecl *SID = OID->getSuperClass();
|
2007-11-29 00:32:21 +03:00
|
|
|
|
|
|
|
if (SID)
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "@interface " << I << " : " << SID->getNameAsString();
|
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?
|
2008-07-21 22:19:38 +04:00
|
|
|
const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
|
|
|
|
if (!Protocols.empty()) {
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
|
|
|
|
E = Protocols.end(); I != E; ++I)
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
|
2007-10-09 03:06:41 +04:00
|
|
|
}
|
2007-11-29 00:32:21 +03:00
|
|
|
|
2008-07-21 22:19:38 +04:00
|
|
|
if (!Protocols.empty())
|
|
|
|
Out << ">";
|
|
|
|
Out << '\n';
|
2007-10-26 20:29:12 +04:00
|
|
|
|
2008-03-17 00:08:55 +03:00
|
|
|
if (OID->ivar_size() > 0) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << '{';
|
2008-01-07 22:49:32 +03:00
|
|
|
for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
|
2007-12-12 10:56:42 +03:00
|
|
|
E = OID->ivar_end(); I != E; ++I) {
|
|
|
|
Out << '\t' << (*I)->getType().getAsString()
|
2008-11-24 08:29:24 +03:00
|
|
|
<< ' ' << (*I)->getNameAsString() << ";\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
|
|
|
|
2008-04-17 22:25:18 +04:00
|
|
|
for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
|
|
|
|
E = OID->classprop_end(); I != E; ++I)
|
|
|
|
PrintObjCPropertyDecl(*I);
|
2008-05-07 21:43:59 +04:00
|
|
|
bool eol_needed = false;
|
2008-05-07 03:14:25 +04:00
|
|
|
for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
|
|
|
|
E = OID->classmeth_end(); I != E; ++I)
|
2008-05-07 21:43:59 +04:00
|
|
|
eol_needed = true, PrintObjCMethodDecl(*I);
|
2008-05-07 03:14:25 +04:00
|
|
|
|
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
|
|
|
|
E = OID->instmeth_end(); I != E; ++I)
|
2008-05-07 21:43:59 +04:00
|
|
|
eol_needed = true, PrintObjCMethodDecl(*I);
|
2008-05-07 03:14:25 +04:00
|
|
|
|
2008-05-07 21:43:59 +04:00
|
|
|
Out << (eol_needed ? "\n@end\n" : "@end\n");
|
2007-09-11 00:51:04 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "@protocol " << PID->getNameAsString() << '\n';
|
2008-04-17 22:25:18 +04:00
|
|
|
|
|
|
|
for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
|
|
|
|
E = PID->classprop_end(); I != E; ++I)
|
|
|
|
PrintObjCPropertyDecl(*I);
|
|
|
|
Out << "@end\n";
|
2007-10-08 22:53:38 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@implementation "
|
2008-11-24 08:29:24 +03:00
|
|
|
<< PID->getClassInterface()->getNameAsString()
|
|
|
|
<< '(' << PID->getNameAsString() << ");\n";
|
2008-04-23 04:06:01 +04:00
|
|
|
for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
|
|
|
|
E = PID->propimpl_end(); I != E; ++I)
|
|
|
|
PrintObjCPropertyImplDecl(*I);
|
|
|
|
Out << "@end\n";
|
2007-10-08 22:53:38 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "@interface "
|
2008-11-24 08:29:24 +03:00
|
|
|
<< PID->getClassInterface()->getNameAsString()
|
|
|
|
<< '(' << PID->getNameAsString() << ");\n";
|
2008-04-17 01:08:45 +04:00
|
|
|
// Output property declarations.
|
2008-04-17 22:25:18 +04:00
|
|
|
for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
|
|
|
|
E = PID->classprop_end(); I != E; ++I)
|
|
|
|
PrintObjCPropertyDecl(*I);
|
2008-04-17 01:08:45 +04:00
|
|
|
Out << "@end\n";
|
|
|
|
|
2007-10-08 22:53:38 +04:00
|
|
|
// FIXME: implement the rest...
|
|
|
|
}
|
|
|
|
|
2008-01-07 22:49:32 +03:00
|
|
|
void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "@compatibility_alias " << AID->getNameAsString()
|
|
|
|
<< ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
|
2007-10-12 03:42:27 +04:00
|
|
|
}
|
|
|
|
|
2008-04-17 22:25:18 +04:00
|
|
|
/// PrintObjCPropertyDecl - print a property declaration.
|
|
|
|
///
|
|
|
|
void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
|
2008-05-05 22:51:55 +04:00
|
|
|
if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
|
|
|
|
Out << "@required\n";
|
|
|
|
else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
|
|
|
|
Out << "@optional\n";
|
|
|
|
|
2008-04-17 22:25:18 +04:00
|
|
|
Out << "@property";
|
|
|
|
if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
|
|
|
|
bool first = true;
|
|
|
|
Out << " (";
|
|
|
|
if (PDecl->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readonly) {
|
|
|
|
Out << (first ? ' ' : ',') << "readonly";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
|
|
|
|
Out << (first ? ' ' : ',') << "getter = "
|
2008-11-24 06:33:13 +03:00
|
|
|
<< PDecl->getGetterName().getAsString();
|
2008-04-17 22:25:18 +04:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
|
|
|
|
Out << (first ? ' ' : ',') << "setter = "
|
2008-11-24 06:33:13 +03:00
|
|
|
<< PDecl->getSetterName().getAsString();
|
2008-04-17 22:25:18 +04:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
|
|
|
|
Out << (first ? ' ' : ',') << "assign";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readwrite) {
|
|
|
|
Out << (first ? ' ' : ',') << "readwrite";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
|
|
|
|
Out << (first ? ' ' : ',') << "retain";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
|
|
|
|
Out << (first ? ' ' : ',') << "copy";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDecl->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_nonatomic) {
|
|
|
|
Out << (first ? ' ' : ',') << "nonatomic";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
Out << " )";
|
|
|
|
}
|
|
|
|
Out << ' ' << PDecl->getType().getAsString()
|
2008-11-24 08:29:24 +03:00
|
|
|
<< ' ' << PDecl->getNameAsString();
|
2008-04-17 22:25:18 +04:00
|
|
|
|
|
|
|
Out << ";\n";
|
|
|
|
}
|
2008-04-23 04:06:01 +04:00
|
|
|
|
|
|
|
/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
|
|
|
|
/// declaration syntax.
|
|
|
|
///
|
|
|
|
void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
|
2008-08-26 08:47:31 +04:00
|
|
|
if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
|
2008-04-23 04:06:01 +04:00
|
|
|
Out << "\n@synthesize ";
|
|
|
|
else
|
|
|
|
Out << "\n@dynamic ";
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << PID->getPropertyDecl()->getNameAsString();
|
2008-04-23 04:06:01 +04:00
|
|
|
if (PID->getPropertyIvarDecl())
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
|
2008-04-23 04:06:01 +04:00
|
|
|
Out << ";\n";
|
|
|
|
}
|
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:
|
2008-09-13 09:16:45 +04:00
|
|
|
ASTPrinter(llvm::raw_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) {
|
2008-01-03 00:04:16 +03:00
|
|
|
PrintDecl(D);
|
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
|
|
|
|
2008-09-13 09:16:45 +04:00
|
|
|
ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
|
2007-11-29 00:32:21 +03:00
|
|
|
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-12-20 01:51:13 +03:00
|
|
|
void Initialize(ASTContext &Context) {
|
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)) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "Read top-level variable decl: '" << SD->getNameAsString()
|
|
|
|
<< "'\n";
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
|
2008-11-24 08:29:24 +03:00
|
|
|
Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (isa<ObjCForwardProtocolDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc fwd protocol decl\n";
|
2008-01-07 22:49:32 +03:00
|
|
|
} else if (isa<ObjCClassDecl>(D)) {
|
2007-11-29 00:32:21 +03:00
|
|
|
Out << "Read objc fwd class decl\n";
|
2008-02-08 03:33:21 +03:00
|
|
|
} else if (isa<FileScopeAsmDecl>(D)) {
|
|
|
|
Out << "Read file scope asm decl\n";
|
2008-03-14 20:31:00 +03:00
|
|
|
} else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
|
2008-11-24 06:33:13 +03:00
|
|
|
Out << "Read objc method decl: '" << MD->getSelector().getAsString()
|
2008-03-14 20:31:00 +03:00
|
|
|
<< "'\n";
|
2008-05-23 22:50:58 +04:00
|
|
|
if (MD->getBody()) {
|
|
|
|
// FIXME: convert dumper to use std::ostream?
|
|
|
|
MD->getBody()->dumpAll(*SM);
|
|
|
|
Out << '\n';
|
|
|
|
}
|
2008-03-14 20:31:00 +03:00
|
|
|
} else if (isa<ObjCImplementationDecl>(D)) {
|
|
|
|
Out << "Read objc implementation decl\n";
|
2008-10-05 04:31:15 +04:00
|
|
|
} else if (isa<ObjCCategoryImplDecl>(D)) {
|
|
|
|
Out << "Read objc category implementation decl\n";
|
2008-12-17 01:14:15 +03:00
|
|
|
} else if (isa<LinkageSpecDecl>(D)) {
|
|
|
|
Out << "Read linkage spec decl\n";
|
|
|
|
} else {
|
2007-10-06 22:52:10 +04:00
|
|
|
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:
|
2007-12-20 01:51:13 +03:00
|
|
|
void Initialize(ASTContext &Context) {
|
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
|
|
|
}
|
|
|
|
}
|
2008-03-14 20:31:00 +03:00
|
|
|
else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
DeclPrinter().PrintObjCMethodDecl(MD);
|
|
|
|
|
|
|
|
if (MD->getBody()) {
|
|
|
|
llvm::cerr << '\n';
|
|
|
|
MD->getBody()->viewAST();
|
|
|
|
llvm::cerr << '\n';
|
|
|
|
}
|
|
|
|
}
|
2007-09-20 01:29:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
|
|
|
|
|
2008-10-24 03:36:29 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// InheritanceViewer - C++ Inheritance Visualization
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class InheritanceViewer : public ASTConsumer {
|
|
|
|
const std::string clsname;
|
|
|
|
public:
|
|
|
|
InheritanceViewer(const std::string& cname) : clsname(cname) {}
|
|
|
|
|
|
|
|
void HandleTranslationUnit(TranslationUnit& TU) {
|
|
|
|
ASTContext& C = TU.getContext();
|
|
|
|
for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
|
|
|
|
if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
|
|
|
|
CXXRecordDecl* D = T->getDecl();
|
|
|
|
// FIXME: This lookup needs to be generalized to handle namespaces and
|
|
|
|
// (when we support them) templates.
|
2008-11-24 08:29:24 +03:00
|
|
|
if (D->getNameAsString() == clsname) {
|
2008-10-24 23:53:54 +04:00
|
|
|
D->viewInheritance(C);
|
2008-10-24 03:36:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
|
|
|
|
return new InheritanceViewer(clsname);
|
|
|
|
}
|
|
|
|
|
2007-12-13 03:37:31 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AST Serializer
|
|
|
|
|
|
|
|
namespace {
|
2007-12-20 02:49:37 +03:00
|
|
|
|
|
|
|
class ASTSerializer : public ASTConsumer {
|
|
|
|
protected:
|
2008-08-09 22:32:11 +04:00
|
|
|
Diagnostic& Diags;
|
2008-04-23 20:25:39 +04:00
|
|
|
|
2007-12-20 02:49:37 +03:00
|
|
|
public:
|
2008-08-10 23:20:05 +04:00
|
|
|
ASTSerializer(Diagnostic& diags) : Diags(diags) {}
|
2007-12-20 02:49:37 +03:00
|
|
|
};
|
2008-06-10 00:02:51 +04:00
|
|
|
|
2007-12-20 02:49:37 +03:00
|
|
|
class SingleFileSerializer : public ASTSerializer {
|
|
|
|
const llvm::sys::Path FName;
|
|
|
|
public:
|
2008-08-09 22:32:11 +04:00
|
|
|
SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
|
|
|
|
: ASTSerializer(diags), FName(F) {}
|
2007-12-20 02:49:37 +03:00
|
|
|
|
2008-08-10 23:20:05 +04:00
|
|
|
virtual void HandleTranslationUnit(TranslationUnit& TU) {
|
2008-08-09 22:32:11 +04:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
2008-08-10 23:20:05 +04:00
|
|
|
EmitASTBitcodeFile(&TU, FName);
|
2007-12-20 02:49:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BuildSerializer : public ASTSerializer {
|
|
|
|
llvm::sys::Path EmitDir;
|
|
|
|
public:
|
2008-08-09 22:32:11 +04:00
|
|
|
BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
|
|
|
|
: ASTSerializer(diags), EmitDir(dir) {}
|
2007-12-20 02:49:37 +03:00
|
|
|
|
2008-08-10 23:20:05 +04:00
|
|
|
virtual void HandleTranslationUnit(TranslationUnit& TU) {
|
|
|
|
if (Diags.hasErrorOccurred())
|
2008-04-23 20:25:39 +04:00
|
|
|
return;
|
|
|
|
|
2008-08-10 23:20:05 +04:00
|
|
|
SourceManager& SourceMgr = TU.getContext().getSourceManager();
|
2007-12-20 03:34:58 +03:00
|
|
|
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-20 22:47:16 +03:00
|
|
|
llvm::sys::Path FName(EmitDir);
|
2007-12-20 03:34:58 +03:00
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
buf.reserve(strlen(FE->getName())+100);
|
|
|
|
|
|
|
|
sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
|
2007-12-20 22:47:16 +03:00
|
|
|
FName.appendComponent(&buf[0]);
|
|
|
|
FName.createDirectoryOnDisk(true);
|
|
|
|
if (!FName.canWrite() || !FName.isDirectory()) {
|
2007-12-20 03:34:58 +03:00
|
|
|
assert (false && "Could not create 'device' serialization directory.");
|
|
|
|
return;
|
|
|
|
}
|
2007-12-20 22:47:16 +03:00
|
|
|
|
2007-12-20 03:34:58 +03:00
|
|
|
sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
|
2007-12-20 22:47:16 +03:00
|
|
|
FName.appendComponent(&buf[0]);
|
2008-08-10 23:20:05 +04:00
|
|
|
EmitASTBitcodeFile(&TU, FName);
|
2007-12-20 22:47:16 +03:00
|
|
|
|
|
|
|
// Now emit the sources.
|
2007-12-20 03:34:58 +03:00
|
|
|
|
|
|
|
}
|
2007-12-20 02:49:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-12-13 03:37:31 +03:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2007-12-20 01:24:34 +03:00
|
|
|
ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
|
2007-12-20 02:49:37 +03:00
|
|
|
const std::string& OutputFile,
|
2008-06-04 19:55:15 +04:00
|
|
|
Diagnostic &Diags) {
|
2007-12-19 20:25:59 +03:00
|
|
|
|
2007-12-20 02:49:37 +03:00
|
|
|
if (OutputFile.size()) {
|
2007-12-20 03:34:58 +03:00
|
|
|
if (InFile == "-") {
|
|
|
|
llvm::cerr <<
|
|
|
|
"error: Cannot use --serialize with -o for source read from STDIN.\n";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 02:49:37 +03: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 03:34:58 +03:00
|
|
|
// FIXME: We should probably only allow using BuildSerializer when
|
|
|
|
// the ASTs come from parsed source files, and not from .ast files.
|
2008-08-09 22:32:11 +04:00
|
|
|
return new BuildSerializer(EmitDir, Diags);
|
2007-12-20 02:49:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-19 20:25:59 +03:00
|
|
|
|
2007-12-20 02:49:37 +03:00
|
|
|
llvm::sys::Path FName(InFile.c_str());
|
2007-12-20 03:34:58 +03:00
|
|
|
FName.appendSuffix("ast");
|
2008-08-09 22:32:11 +04:00
|
|
|
return new SingleFileSerializer(FName, Diags);
|
2007-12-13 03:37:31 +03:00
|
|
|
}
|