aboutsummaryrefslogtreecommitdiffstats
path: root/bruiser
diff options
context:
space:
mode:
authoruser1 <user1@ubuntu>2017-04-14 19:31:35 +0000
committeruser1 <user1@ubuntu>2017-04-14 19:31:35 +0000
commit19129256402d71d41fbacaaf2fcea8cc64cb9fba (patch)
tree14ca6221c2c79fee2897c224fe5b0d4242cb6206 /bruiser
parentadded (simple, not necessarily general) translation of memset() (diff)
parentadded some more experimental stuff. right now bruiser can only hijack the main. (diff)
downloadmutator-19129256402d71d41fbacaaf2fcea8cc64cb9fba.tar.gz
mutator-19129256402d71d41fbacaaf2fcea8cc64cb9fba.zip
Merge branch 'master' of https://github.com/bloodstalker/mutator.git
Diffstat (limited to '')
-rw-r--r--bruiser/bruiser.cpp225
-rw-r--r--bruiser/bruiser.h50
2 files changed, 266 insertions, 9 deletions
diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp
index a7df7ca..439605b 100644
--- a/bruiser/bruiser.cpp
+++ b/bruiser/bruiser.cpp
@@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*
/*standard headers*/
#include <string>
#include <iostream>
-#include <cassert>
/*LLVM headers*/
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
@@ -49,32 +48,239 @@ using namespace clang::driver;
using namespace clang::tooling;
/**********************************************************************************************************************/
/*global vars*/
-
static llvm::cl::OptionCategory BruiserCategory("Empty");
/**********************************************************************************************************************/
+/*the implementation of the bruiser logger.*/
+bruiser::BruiserReport::BruiserReport ()
+{
+ BruiserLog.open("bruiser.log");
+}
+
+bruiser::BruiserReport::~BruiserReport()
+{
+ BruiserLog.close();
+}
+
+bool bruiser::BruiserReport::PrintToLog(std::string __in_arg)
+{
+ BruiserLog << __in_arg << "\n";
+ return !BruiserLog.bad();
+}
+/**********************************************************************************************************************/
+bruiser::TypeInfo::TypeInfo(const clang::ast_type_traits::DynTypedNode* __dtn) : DTN(__dtn) {};
+
+bruiser::TypeInfo::~TypeInfo() {};
+
+const clang::Type* bruiser::TypeInfo::getTypeInfo(clang::ASTContext* __astc)
+{
+ const clang::Expr* EXP = DTN->get<clang::Expr>();
+
+ const clang::Type* TP = EXP->getType().getTypePtr();
+
+ return __astc->getCanonicalType(TP);
+}
+/**********************************************************************************************************************/
+class AbstractMatcherHandler : public virtual MatchFinder::MatchCallback
+{
+ public:
+ AbstractMatcherHandler (Rewriter &Rewrite) : R(Rewrite) {}
+
+ public:
+ virtual void run(const MatchFinder::MatchResult &MR)
+ {
+
+ }
+
+ private:
+ Rewriter &R;
+};
+/**********************************************************************************************************************/
+class MatcherHandlerLVL0 : public AbstractMatcherHandler
+{
+ public:
+ explicit MatcherHandlerLVL0 (Rewriter &Rewrite) : AbstractMatcherHandler(Rewrite) {}
+
+ virtual void run(const MatchFinder::MatchResult &MR) override
+ {
+
+ }
+
+ private:
+};
+/**********************************************************************************************************************/
+class NameFinder
+{
+ public:
+ NameFinder () {}
+
+ class runDeclRefExprMatcher
+ {
+ public:
+ runDeclRefExprMatcher (Rewriter &__rwrt) : LVL0Proto(__rwrt), __rwrt(__rwrt) {}
+
+ virtual void runMatcher(const StringRef __sr, ASTContext &__ctx)
+ {
+ Matcher.addMatcher(declRefExpr(to(namedDecl(hasName(__sr.str())))).bind("declrefexpbyname"), &LVL0Proto);
+ Matcher.matchAST(__ctx);
+ }
+
+ private:
+ MatchFinder Matcher;
+ MatcherHandlerLVL0 LVL0Proto;
+ Rewriter __rwrt;
+ StringRef __sr;
+ };
+
+ class runNamedDeclMatcher
+ {
+ public:
+ runNamedDeclMatcher (Rewriter &__rwrt) : LVL0Proto(__rwrt), __rwrt(__rwrt) {}
+
+ virtual void runMatcher(const StringRef __sr, ASTContext &__ctx)
+ {
+ Matcher.addMatcher(declRefExpr(to(namedDecl(hasName(__sr.str())))).bind("nameddeclbyname"), &LVL0Proto);
+ Matcher.matchAST(__ctx);
+ }
+
+ private:
+ MatchFinder Matcher;
+ MatcherHandlerLVL0 LVL0Proto;
+ Rewriter __rwrt;
+ StringRef __sr;
+ };
+
+ private:
+};
+/**********************************************************************************************************************/
+/**********************************************************************************************************************/
+/**********************************************************************************************************************/
+class IfBreaker : public MatchFinder::MatchCallback
+{
+ public:
+ IfBreaker (Rewriter &Rewrite) : Rewrite(Rewrite) {}
+
+ virtual void run(const MatchFinder::MatchResult &MR)
+ {
+ if (MR.Nodes.getNodeAs<clang::UnaryOperator>("uno") != nullptr)
+ {
+ const clang::UnaryOperator* UO = MR.Nodes.getNodeAs<clang::UnaryOperator>("uno");
+
+ SourceLocation SL = UO->getLocStart();
+ CheckSLValidity(SL);
+ SL = Devi::SourceLocationHasMacro(SL, Rewrite);
+
+ const Expr* EXP = UO->getSubExpr();
+ const ast_type_traits::DynTypedNode DynNode = ast_type_traits::DynTypedNode::create<clang::Expr>(*EXP);
+ bruiser::TypeInfo TIProto(&DynNode);
+
+ const clang::Type* CTP = TIProto.getTypeInfo(MR.Context);
+
+ }
+
+ if (MR.Nodes.getNodeAs<clang::BinaryOperator>("dous") != nullptr)
+ {
+ const clang::BinaryOperator* BO = MR.Nodes.getNodeAs<clang::BinaryOperator>("dous");
+
+ SourceLocation SL = BO->getLocStart();
+ CheckSLValidity(SL);
+ SL = Devi::SourceLocationHasMacro(SL, Rewrite);
+
+ const Expr* LHS = BO->getLHS();
+ const Expr* RHS = BO->getRHS();
+
+ QualType LQT = LHS->getType();
+ QualType RQT = RHS->getType();
+
+ const clang::Type* LTP = LQT.getTypePtr();
+ const clang::Type* RTP = RQT.getTypePtr();
+
+ const clang::Type* CLTP = MR.Context->getCanonicalType(LTP);
+ const clang::Type* CRTP = MR.Context->getCanonicalType(RTP);
+ }
+ }
+
+ private:
+ Rewriter &Rewrite;
+ MatchFinder Matcher;
+};
+/**********************************************************************************************************************/
+/**
+ * @brief Hijacks the main main and replaces it with bruiser's main.
+ */
+class MainWrapper : public MatchFinder::MatchCallback
+{
+public:
+ MainWrapper (Rewriter &Rewrite) : Rewrite(Rewrite) {}
+
+ virtual void run(const MatchFinder::MatchResult &MR)
+ {
+ if (MR.Nodes.getNodeAs<clang::FunctionDecl>("mainwrapper") != nullptr)
+ {
+ const FunctionDecl* FD = MR.Nodes.getNodeAs<clang::FunctionDecl>("mainwrapper");
+
+ SourceLocation SL = FD->getLocStart();
+ CheckSLValidity(SL);
+ SL = Devi::SourceLocationHasMacro(SL, Rewrite);
+
+ SourceLocation SLE = FD->getLocEnd();
+ CheckSLValidity(SLE);
+ SLE = Devi::SourceLocationHasMacro(SLE, Rewrite);
+
+ SourceRange SR(SL, SLE);
+
+ std::string MainSig = Rewrite.getRewrittenText(SR);
+
+ size_t mainbegin = MainSig.find("main");
+
+ StringRef __sr("sub_main");
+
+ Rewrite.ReplaceText(SL.getLocWithOffset(mainbegin), 4U, __sr);
+
+ Rewrite.InsertTextAfter(SLE.getLocWithOffset(1U), StringRef("\n\nint main(int argc, const char **argv)\n{\n\tsub_main(argc, argv);\n}"));
+
+ //ruiseRep << "changed main main's name.\n"
+ }
+ }
+
+ private:
+ Rewriter &Rewrite;
+};
+/**********************************************************************************************************************/
/**********************************************************************************************************************/
/**********************************************************************************************************************/
class MyASTConsumer : public ASTConsumer {
public:
- MyASTConsumer(Rewriter &R) {}
+ MyASTConsumer(Rewriter &R) : HIfBreaker(R), HMainWrapper(R)
+ {}
+
+ void HandleTranslationUnit(ASTContext &Context) override
+ {
+ Matcher.addMatcher(ifStmt(hasDescendant(expr(anyOf(unaryOperator().bind("uno"), binaryOperator().bind("dous"))))), &HIfBreaker);
+
+ Matcher.addMatcher(functionDecl(hasName("main")).bind("mainwrapper"), &HMainWrapper);
- void HandleTranslationUnit(ASTContext &Context) override {
Matcher.matchAST(Context);
}
private:
+ IfBreaker HIfBreaker;
+ MainWrapper HMainWrapper;
MatchFinder Matcher;
+ Rewriter R;
};
/**********************************************************************************************************************/
-class MyFrontendAction : public ASTFrontendAction {
+class MyFrontendAction : public ASTFrontendAction
+{
public:
MyFrontendAction() {}
- void EndSourceFileAction() override {
+ void EndSourceFileAction() override
+ {
TheRewriter.getEditBuffer(TheRewriter.getSourceMgr().getMainFileID()).write(llvm::outs());
}
- std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef file) override {
+ std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef file) override
+ {
TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
return llvm::make_unique<MyASTConsumer>(TheRewriter);
}
@@ -84,10 +290,13 @@ private:
};
/**********************************************************************************************************************/
/*Main*/
-int main(int argc, const char **argv) {
+int main(int argc, const char **argv)
+{
CommonOptionsParser op(argc, argv, BruiserCategory);
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
+ bruiser::BruiserReport BruiseRep;
+
return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}
/*last line interntionally left blank.*/
diff --git a/bruiser/bruiser.h b/bruiser/bruiser.h
index cf22e5e..fcfcaec 100644
--- a/bruiser/bruiser.h
+++ b/bruiser/bruiser.h
@@ -27,9 +27,57 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*
/*included modules*/
/*project headers*/
/*standard library headers*/
+#include <string>
+#include <fstream>
+/*clang headers*/
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/Type.h"
+#include "clang/AST/ASTTypeTraits.h"
+/*llvm headers*/
+
+/**********************************************************************************************************************/
+/*using*/
+using namespace llvm;
+using namespace clang;
+/**********************************************************************************************************************/
+namespace bruiser
+{
+/**********************************************************************************************************************/
+/**
+ * @brief This class hanhles the logging for bruiser.
+ */
+class BruiserReport
+{
+ public:
+ BruiserReport ();
+
+ ~BruiserReport();
+
+ bool PrintToLog(std::string __in_arg);
+
+ private:
+ std::ofstream BruiserLog;
+};
+/**********************************************************************************************************************/
+/**
+ * @brief looks through types.
+ */
+class TypeInfo
+{
+ public:
+ explicit TypeInfo(const clang::ast_type_traits::DynTypedNode* __dtn);
+
+ ~TypeInfo();
+
+ const clang::Type* getTypeInfo(clang::ASTContext* __astc);
+
+ private:
+ const clang::ast_type_traits::DynTypedNode* DTN;
+};
/**********************************************************************************************************************/
-/*externals*/
/**********************************************************************************************************************/
+} // end of namespace bruiser
#endif
/*last line intentionally left balnk.*/