Skip to content

Commit

Permalink
Refactors Codebase
Browse files Browse the repository at this point in the history
Most of the cruft of earlier designs should be eliminated by this commit.

- Moves deduction system out of main
- Splits Types.h/cpp into FuncCalls and FuncEntries
- Removes Debugging printfs, such that in quiet mode no debugging
  information should be seen
- Removes unneeded header includes; As a side effect the header-include test is
  now only subjected to stl headers and not llvm headers, making it run
  considerably faster (though it may miss some edge cases)
  • Loading branch information
fundamental committed May 28, 2012
1 parent ee91509 commit c3a26bb
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 218 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Expand Up @@ -21,8 +21,8 @@ add_definitions (-fno-rtti)
set (CMAKE_CXX_COMPILER clang++)

add_definitions (-DVERSION="${sfpv_VERSION_MAJOR}.${sfpv_VERSION_MINOR}")
add_executable (sfpv src/Errors.cpp src/GraphBuilder.cpp
src/TranslationUnit.cpp src/Types.cpp src/main.cpp)
add_executable (sfpv src/Errors.cpp src/GraphBuilder.cpp src/Deductions.cpp
src/TranslationUnit.cpp src/FuncCalls.cpp src/FuncEntries.cpp src/main.cpp)
target_link_libraries (sfpv ${CLANG_LIBRARIES} ${LLVM_LIBRARIES})

######################################################################
Expand Down
155 changes: 155 additions & 0 deletions src/Deductions.cpp
@@ -0,0 +1,155 @@
#include "Deductions.h"
#include "Errors.h"
#include "GraphBuilder.h"
#include "FuncCalls.h"
#include "FuncEntries.h"
#include "TranslationUnit.h"

#include <clang/AST/Decl.h>
#include <clang/AST/Expr.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <string>
#include <vector>
#include <cstdio>

#pragma clang diagnostic ignored "-Wc++11-extensions"

typedef CallPair Deduction;
class DeductionsImpl
{
public:
void print_path(std::string func, GraphBuilder *gb);
int length(void) const;
void print(void) const;
bool rt(std::string fname) const;
void deduce(Deduction d);
void perform_deductions(GraphBuilder *gb);
void find_all(GraphBuilder *gb);
int find_inconsistent(GraphBuilder *gb);
std::vector<Deduction> d_list;
};

Deductions::Deductions(GraphBuilder *gb)
:impl(new DeductionsImpl)
{
impl->find_all(gb);
}

Deductions::~Deductions(void)
{
delete impl;
}

void Deductions::print(void)
{
impl->print();
}

int Deductions::find_inconsistent(GraphBuilder *gb)
{
return impl->find_inconsistent(gb);
}

void DeductionsImpl::print_path(std::string func, GraphBuilder *gb)
{
for(Deduction d: d_list) {
if(d.second == func) {
clang::DiagnosticsEngine *diag = d.TU->getDiagEng();
if(d.CE) {
std::string str = d.CE->getDirectCallee()->getQualifiedNameAsString();
diag->Report(d.CE->getLocStart(), error_realtime_saftey_trace) << str;
} else
diag->Report(error_realtime_safety_class) << d.second << d.first;

print_path(d.first, gb);
return;
}
}

FuncEntry f = gb->getFunctions()[func];
clang::DiagnosticsEngine *diag = f.TU->getDiagEng();
diag->Report(f.FDECL->getLocation(), error_realtime_saftey_trace_end) << f.FDECL->getQualifiedNameAsString();
}

int DeductionsImpl::length(void) const
{
return d_list.size();
}

void DeductionsImpl::print(void) const
{
for(Deduction d: d_list)
printf("%30s :=> %30s\n", d.first.c_str(), d.second.c_str());
}

bool DeductionsImpl::rt(std::string fname) const
{
for(Deduction d: d_list)
if(d.second == fname)
return true;
return false;
}

void DeductionsImpl::deduce(Deduction d)
{
d_list.push_back(d);
}

void DeductionsImpl::perform_deductions(GraphBuilder *gb)
{
FuncEntries &entries = gb->getFunctions();
for(auto pair:entries) {
FuncEntry e = pair.second;
if(!e.realtime_p() && !rt(e.name))
continue;

//Deduce all children must be realtime
for(CallPair c:gb->getCalls()) {
if(c.first == e.name && !entries[c.second].realtime_p()
&& !rt(c.second))
deduce(c);
}
}
}

void DeductionsImpl::find_all(GraphBuilder *gb)
{
int len = 0;
do {
len = length();
perform_deductions(gb);
} while(len != length());
}

int DeductionsImpl::find_inconsistent(GraphBuilder *gb)
{
int result = 0; //aka all is safe
for(auto pair: gb->getFunctions()) {
FuncEntry e = pair.second;
if(e.not_realtime_p() && rt(e.name)) {
clang::DiagnosticsEngine *diag = e.TU->getDiagEng();
diag->Report(e.FDECL->getLocation(), error_realtime_saftey_violation)
<< e.FDECL->getQualifiedNameAsString();

print_path(e.name, gb);
result |= 2;
}
if(!e.defined_p() && !e.realtime_p() && rt(e.name)) {
//Check for function calls [virtual methods]
for(auto call : gb->getCalls())
if(call.first==e.name)
goto next;

clang::DiagnosticsEngine *diag = e.TU->getDiagEng();
diag->Report(e.FDECL->getLocation(), warnn_realtime_saftey_unknown)
<< e.FDECL->getQualifiedNameAsString();

print_path(e.name, gb);
result |= 1;
}
next:
;
}
return result;
}

13 changes: 13 additions & 0 deletions src/Deductions.h
@@ -0,0 +1,13 @@
class GraphBuilder;

class Deductions
{
public:
Deductions(GraphBuilder *gb);
~Deductions(void);
void print(void);
int find_inconsistent(GraphBuilder *gb);
private:
class DeductionsImpl *impl;
};

42 changes: 42 additions & 0 deletions src/FuncCalls.cpp
@@ -0,0 +1,42 @@
#include "FuncCalls.h"
#include <cstdio>

#pragma clang diagnostic ignored "-Wc++11-extensions"

struct FuncCallsImpl
{
std::set<CallPair> cpair;
};

FuncCalls::FuncCalls(void)
{
impl = new FuncCallsImpl;
}

FuncCalls::~FuncCalls(void)
{
delete impl;
}

void FuncCalls::add(std::string f1, std::string f2, class TranslationUnit *tu, class clang::CallExpr *ce)
{
impl->cpair.insert(CallPair(f1,f2,tu,ce));
}

void FuncCalls::print(void) const
{
for(CallPair c:impl->cpair)
printf("%30s :=> %30s\n", c.first.c_str(), c.second.c_str());
}

typedef std::set<CallPair>::iterator sitr;

sitr FuncCalls::begin(void)
{
return impl->cpair.begin();
}

sitr FuncCalls::end(void)
{
return impl->cpair.end();
}
37 changes: 37 additions & 0 deletions src/FuncCalls.h
@@ -0,0 +1,37 @@
#include <utility>
#include <string>
#include <set>

namespace clang
{
class CallExpr;
}

class CallPair : public std::pair<std::string,std::string>
{
public:
CallPair(void)
:std::pair<std::string,std::string>("",""),TU(NULL), CE(NULL)
{}

CallPair(std::string a, std::string b, class TranslationUnit *tu, class clang::CallExpr *ce)
:std::pair<std::string,std::string>(a,b),TU(tu), CE(ce)
{}
class TranslationUnit *TU;
class clang::CallExpr *CE;
};

class FuncCalls
{
public:
FuncCalls(void);
~FuncCalls(void);

void print(void) const;
void add(std::string f1, std::string f2, class TranslationUnit *tu, class clang::CallExpr *ce);
typedef std::set<CallPair>::iterator sitr;
sitr begin(void);
sitr end(void);
private:
struct FuncCallsImpl *impl;
};
42 changes: 1 addition & 41 deletions src/Types.cpp → src/FuncEntries.cpp
@@ -1,7 +1,6 @@
#include "Types.h"
#include "FuncEntries.h"
#include <cstdio>
#include <map>
#include <set>

//C++11 extentions are useful, but setting the standard to be C++11 breaks
//compilation, so lets just sweep these errors under a rug for now
Expand Down Expand Up @@ -60,42 +59,3 @@ miterator FuncEntries::end(void)
{
return impl->fmap.end();
}

struct FcallsImpl
{
std::set<CallPair> cpair;
};

Fcalls::Fcalls(void)
{
impl = new FcallsImpl;
}

Fcalls::~Fcalls(void)
{
delete impl;
}

void Fcalls::add(std::string f1, std::string f2, class TranslationUnit *tu, class clang::CallExpr *ce)
{
impl->cpair.insert(CallPair(f1,f2,tu,ce));
}

void Fcalls::print(void) const
{
for(CallPair c:impl->cpair)
printf("%30s :=> %30s\n", c.first.c_str(), c.second.c_str());
}

typedef std::set<CallPair>::iterator sitr;

sitr Fcalls::begin(void)
{
return impl->cpair.begin();
}

sitr Fcalls::end(void)
{
return impl->cpair.end();
}

30 changes: 0 additions & 30 deletions src/Types.h → src/FuncEntries.h
Expand Up @@ -6,24 +6,8 @@

namespace clang {
class FunctionDecl;
class CallExpr;
}

class CallPair : public std::pair<std::string,std::string>
{
public:
CallPair(void)
:std::pair<std::string,std::string>("",""),TU(NULL), CE(NULL)
{}

CallPair(std::string a, std::string b, class TranslationUnit *tu, class clang::CallExpr *ce)
:std::pair<std::string,std::string>(a,b),TU(tu), CE(ce)
{}
class TranslationUnit *TU;
class clang::CallExpr *CE;
};


class FuncEntry
{
public:
Expand Down Expand Up @@ -72,18 +56,4 @@ class FuncEntries
class FuncEntriesImpl *impl;
};

class Fcalls
{
public:
Fcalls(void);
~Fcalls(void);

void print(void) const;
void add(std::string f1, std::string f2, class TranslationUnit *tu, class clang::CallExpr *ce);
typedef std::set<CallPair>::iterator sitr;
sitr begin(void);
sitr end(void);
private:
struct FcallsImpl *impl;
};
#endif
12 changes: 6 additions & 6 deletions src/GraphBuilder.cpp
@@ -1,18 +1,18 @@
#include "GraphBuilder.h"
#include "FuncEntries.h"
#include "FuncCalls.h"
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/StmtVisitor.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <cstdio>
#include <iostream>
#include <err.h>

#pragma clang diagnostic ignored "-Wc++11-extensions"
#define CHECK(x) do{if(!x) errx(1, "expected '%s'", #x);}while(0)

class GraphStmtHelper :public clang::StmtVisitor<GraphStmtHelper>
{
GraphBuilderImpl *gbi;
std::string caller;

public:
GraphStmtHelper(GraphBuilderImpl *_gbi, std::string _caller)
:gbi(_gbi), caller(_caller)
Expand Down Expand Up @@ -68,7 +68,7 @@ class GraphBuilderImpl

TopDeclConsumer consume;
FuncEntries fe;
Fcalls fc;
FuncCalls fc;
class TranslationUnit *current_tu;
};

Expand Down Expand Up @@ -150,7 +150,7 @@ FuncEntries &GraphBuilder::getFunctions(void) const
return impl->fe;
}

Fcalls &GraphBuilder::getCalls(void) const
FuncCalls &GraphBuilder::getCalls(void) const
{
return impl->fc;
}
Expand Down

0 comments on commit c3a26bb

Please sign in to comment.