2007-06-23 00:39:57 +00:00
|
|
|
//===--- ASTStreamers.cpp - ASTStreamer Drivers ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Bill Wendling and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// ASTStreamer drivers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ASTStreamers.h"
|
|
|
|
#include "clang/AST/AST.h"
|
2007-08-21 21:42:03 +00:00
|
|
|
#include "clang/AST/CFG.h"
|
2007-09-06 00:17:54 +00:00
|
|
|
#include "clang/Analysis/LiveVariables.h"
|
2007-09-06 23:00:42 +00:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2007-06-23 00:39:57 +00:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Sema/ASTStreamer.h"
|
2007-08-08 22:51:59 +00:00
|
|
|
using namespace clang;
|
2007-06-23 00:39:57 +00:00
|
|
|
|
|
|
|
void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
|
|
|
|
// collect global stats on Decls/Stmts (until we have a module streamer)
|
|
|
|
if (Stats) {
|
|
|
|
Decl::CollectingStats(true);
|
|
|
|
Stmt::CollectingStats(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
|
|
|
|
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
|
|
|
|
|
|
|
|
while (ASTStreamer_ReadTopLevelDecl(Streamer))
|
|
|
|
/* keep reading */;
|
|
|
|
|
|
|
|
if (Stats) {
|
|
|
|
fprintf(stderr, "\nSTATISTICS:\n");
|
|
|
|
ASTStreamer_PrintStats(Streamer);
|
|
|
|
Context.PrintStats();
|
|
|
|
Decl::PrintStats();
|
|
|
|
Stmt::PrintStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTStreamer_Terminate(Streamer);
|
|
|
|
}
|
|
|
|
|
2007-08-08 22:51:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void PrintFunctionDeclStart(FunctionDecl *FD) {
|
2007-06-23 00:39:57 +00:00
|
|
|
bool HasBody = FD->getBody();
|
|
|
|
|
2007-08-26 04:02:13 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
switch (FD->getStorageClass()) {
|
|
|
|
default: assert(0 && "Unknown storage class");
|
|
|
|
case FunctionDecl::None: break;
|
|
|
|
case FunctionDecl::Extern: fprintf(stderr, "extern "); break;
|
|
|
|
case FunctionDecl::Static: fprintf(stderr, "static "); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->isInline())
|
|
|
|
fprintf(stderr, "inline ");
|
|
|
|
|
2007-06-23 00:39:57 +00:00
|
|
|
std::string Proto = FD->getName();
|
|
|
|
FunctionType *AFT = cast<FunctionType>(FD->getType());
|
|
|
|
|
|
|
|
if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
|
|
|
|
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-08-26 04:02:13 +00:00
|
|
|
fprintf(stderr, "%s", Proto.c_str());
|
2007-06-23 00:39:57 +00:00
|
|
|
|
2007-08-08 22:51:59 +00:00
|
|
|
if (!FD->getBody())
|
2007-06-23 00:39:57 +00:00
|
|
|
fprintf(stderr, ";\n");
|
2007-08-08 22:51:59 +00:00
|
|
|
// Doesn't print the body.
|
2007-06-23 00:39:57 +00:00
|
|
|
}
|
|
|
|
|
2007-08-08 22:51:59 +00:00
|
|
|
static void PrintTypeDefDecl(TypedefDecl *TD) {
|
2007-06-23 00:39:57 +00:00
|
|
|
std::string S = TD->getName();
|
|
|
|
TD->getUnderlyingType().getAsStringInternal(S);
|
|
|
|
fprintf(stderr, "typedef %s;\n", S.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
|
|
|
|
ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
|
|
|
|
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
|
|
|
|
|
|
|
|
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2007-08-08 22:51:59 +00:00
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
|
|
|
|
if (FD->getBody()) {
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
FD->getBody()->dumpPretty();
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2007-06-23 00:39:57 +00:00
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Stats) {
|
|
|
|
fprintf(stderr, "\nSTATISTICS:\n");
|
|
|
|
ASTStreamer_PrintStats(Streamer);
|
|
|
|
Context.PrintStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTStreamer_Terminate(Streamer);
|
|
|
|
}
|
2007-08-08 22:51:59 +00:00
|
|
|
|
|
|
|
void clang::DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
|
|
|
|
ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
|
|
|
|
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
|
|
|
|
|
|
|
|
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
|
|
|
|
if (FD->getBody()) {
|
|
|
|
fprintf(stderr, "\n");
|
2007-08-30 00:40:08 +00:00
|
|
|
FD->getBody()->dumpAll(PP.getSourceManager());
|
2007-08-08 22:51:59 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
PrintTypeDefDecl(TD);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Stats) {
|
|
|
|
fprintf(stderr, "\nSTATISTICS:\n");
|
|
|
|
ASTStreamer_PrintStats(Streamer);
|
|
|
|
Context.PrintStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTStreamer_Terminate(Streamer);
|
|
|
|
}
|
|
|
|
|
2007-09-07 23:47:56 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFGVisitor & VisitCFGs - Boilerplate interface and logic to visit
|
|
|
|
// the CFGs for all function definitions.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CFGVisitor {
|
|
|
|
public:
|
|
|
|
virtual ~CFGVisitor() {}
|
|
|
|
virtual void VisitCFG(CFG& C) = 0;
|
|
|
|
virtual bool printFuncDeclStart() { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static void VisitCFGs(CFGVisitor& Visitor, Preprocessor& PP,
|
|
|
|
unsigned MainFileID, bool Stats) {
|
|
|
|
|
|
|
|
bool printFDecl = Visitor.printFuncDeclStart();
|
2007-08-21 21:42:03 +00:00
|
|
|
ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
|
|
|
|
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
|
|
|
|
|
|
|
|
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
|
2007-09-07 23:47:56 +00:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
2007-08-21 21:42:03 +00:00
|
|
|
if (FD->getBody()) {
|
2007-09-07 23:47:56 +00:00
|
|
|
|
|
|
|
if (printFDecl) {
|
|
|
|
PrintFunctionDeclStart(FD);
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
}
|
|
|
|
|
2007-08-29 21:56:09 +00:00
|
|
|
if (CFG* C = CFG::buildCFG(FD->getBody())) {
|
2007-09-07 23:47:56 +00:00
|
|
|
Visitor.VisitCFG(*C);
|
|
|
|
delete C;
|
2007-08-29 21:56:09 +00:00
|
|
|
}
|
2007-08-21 21:42:03 +00:00
|
|
|
else
|
2007-09-07 23:47:56 +00:00
|
|
|
fprintf(stderr," Error processing CFG.\n");
|
2007-08-21 21:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Stats) {
|
|
|
|
fprintf(stderr, "\nSTATISTICS:\n");
|
|
|
|
ASTStreamer_PrintStats(Streamer);
|
|
|
|
Context.PrintStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTStreamer_Terminate(Streamer);
|
|
|
|
}
|
|
|
|
|
2007-09-07 23:47:56 +00: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) {}
|
|
|
|
|
|
|
|
virtual void VisitCFG(CFG& C) {
|
|
|
|
if (UseGraphviz) C.viewCFG();
|
|
|
|
else C.dump();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-06 00:17:54 +00:00
|
|
|
|
2007-09-07 23:47:56 +00:00
|
|
|
void clang::DumpCFGs(Preprocessor &PP, unsigned MainFileID,
|
|
|
|
bool Stats, bool use_graphviz) {
|
|
|
|
CFGDumper Visitor(use_graphviz);
|
|
|
|
VisitCFGs(Visitor,PP,MainFileID,Stats);
|
2007-09-06 00:17:54 +00:00
|
|
|
}
|
|
|
|
|
2007-09-07 23:47:56 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AnalyzeLiveVariables - perform live variable analysis and dump results
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LivenessVisitor : public CFGVisitor {
|
|
|
|
Preprocessor& PP;
|
|
|
|
public:
|
|
|
|
LivenessVisitor(Preprocessor& pp) : PP(pp) {}
|
|
|
|
|
|
|
|
virtual void VisitCFG(CFG& C) {
|
|
|
|
LiveVariables L;
|
|
|
|
L.runOnCFG(C);
|
2007-09-10 17:36:42 +00:00
|
|
|
L.dumpBlockLiveness(PP.getSourceManager());
|
|
|
|
L.dumpVarLiveness(PP.getSourceManager());
|
2007-09-06 23:00:42 +00:00
|
|
|
}
|
2007-09-07 23:47:56 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-06 23:00:42 +00:00
|
|
|
|
2007-09-07 23:47:56 +00:00
|
|
|
void clang::AnalyzeLiveVariables(Preprocessor &PP, unsigned MainFileID) {
|
|
|
|
LivenessVisitor Visitor(PP);
|
|
|
|
VisitCFGs(Visitor,PP,MainFileID,false);
|
2007-09-06 23:00:42 +00:00
|
|
|
}
|
|
|
|
|
2007-09-07 23:47:56 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RunDeadStores - run checker to locate dead stores in a function
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DeadStoreVisitor : public CFGVisitor {
|
|
|
|
Preprocessor& PP;
|
|
|
|
public:
|
|
|
|
DeadStoreVisitor(Preprocessor& pp) : PP(pp) {}
|
|
|
|
virtual void VisitCFG(CFG& C) { CheckDeadStores(C,PP); }
|
2007-09-07 23:54:15 +00:00
|
|
|
virtual bool printFuncDeclStart() { return false; }
|
2007-09-07 23:47:56 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-08-08 22:51:59 +00:00
|
|
|
|
2007-09-07 23:47:56 +00:00
|
|
|
void clang::RunDeadStoresCheck(Preprocessor &PP,unsigned MainFileID,bool Stats){
|
|
|
|
DeadStoreVisitor Visitor(PP);
|
|
|
|
VisitCFGs(Visitor,PP,MainFileID,Stats);
|
|
|
|
}
|