Skip to content
Browse files

* Restructure directories so MethodMove is under Transforms

* Centralize tests
* Refactor MethodMove to use framework
  • Loading branch information...
1 parent f6d1c2c commit c1a61feb7ff2f3797dfe5b2ec6924d66b0ec04b5 Thomas Minor committed
View
13 .gitignore
@@ -1,8 +1,17 @@
+*~
+
+# Test results
+tests/*/*_out.cpp
+tests/*/*_out.h
+
+# Built binaries
+*.a
+Build
+
+# CMake generated files
CMakeCache.txt
CMakeFiles
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
-*.a
-refactorial
View
31 CMakeLists.txt
@@ -1,28 +1,37 @@
-CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
+CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
+PROJECT (Refactorial)
+
+SET(CMAKE_DISABLE_SOURCE_CHANGES ON)
+SET(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
SET(CMAKE_BUILD_TYPE None)
SET(CMAKE_C_COMPILER clang)
SET(CMAKE_CXX_COMPILER clang++)
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_LINKER_FLAGS} -g -fno-rtti -fcolor-diagnostics -Wno-c++11-extensions")
+SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_LINKER_FLAGS} -g -fno-rtti")
IF(APPLE)
- SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_LINKER_FLAGS} -std=c++0x -stdlib=libc++ -fno-rtti -all_load")
- SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -stdlib=libc++ -fno-rtti -all_load")
+ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ --std=c++0x")
+ SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -stdlib=libc++")
ELSE(APPLE)
- SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_LINKER_FLAGS} --std=gnu++0x -fno-rtti")
- SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-rtti")
+ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11")
ENDIF(APPLE)
-PROJECT (Refactorial)
-
FIND_PACKAGE(LLVM REQUIRED)
ADD_DEFINITIONS(${LLVM_DEFINITIONS})
-INCLUDE_DIRECTORIES(${LLVM_INCLUDE_DIRS})
+INCLUDE_DIRECTORIES(${LLVM_INCLUDE_DIRS} .)
LINK_DIRECTORIES(${LLVM_LIBRARY_DIRS})
LLVM_MAP_COMPONENTS_TO_LIBRARIES(REQ_LLVM_LIBRARIES native)
SET(CLANG_LIBRARIES clangAnalysis clangAST clangBasic clangDriver clangEdit clangFrontend clangLex clangParse clangRewrite clangSema clangSerialization clangTooling)
-ADD_SUBDIRECTORY (Transforms)
+SET(Transforms_sources Transforms.cpp Accessors.cpp MethodMove/MethodMove.cpp)
+
+FOREACH(arg ${Transforms_sources})
+ LIST(APPEND sources "Transforms/${arg}")
+ENDFOREACH(arg ${Transforms_sources})
+
+SET(sources ${sources} main.cpp)
-ADD_EXECUTABLE (refactorial main.cpp)
-TARGET_LINK_LIBRARIES (refactorial ${REQ_LLVM_LIBRARIES} ${CLANG_LIBRARIES} Transforms)
+ADD_EXECUTABLE (refactorial ${sources} )
+TARGET_LINK_LIBRARIES (refactorial ${REQ_LLVM_LIBRARIES} ${CLANG_LIBRARIES})
View
27 MethodMove/Makefile
@@ -1,27 +0,0 @@
-USEDLIBS = -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMTarget \
- -lLLVMCore -lLLVMMC -lLLVMObject -lLLVMSupport -lclangFrontend \
- -lclangEdit -lclangParse -lclangSema -lclangAnalysis -lclangAST \
- -lclangLex -lclangBasic -lclangDriver -lclangSerialization -lLLVMMC \
- -lLLVMSupport -lclangRewrite
-
-LDFLAGS = -g -O0 `llvm-config --ldflags`
-CXXFLAGS = -g -O0 `llvm-config --cxxflags` -fno-rtti
-
-LIBS = $(USEDLIBS)
-
-CC=clang
-CXX=clang++
-LD=clang++
-
-OUTPUT = MethodMove
-OBJS = MethodMove.o
-
-$(OUTPUT): $(OBJS)
- $(CXX) -o $@ $(LDFLAGS) $^ $(LIBS)
-
-clean:
- rm -f $(OUTPUT)
- rm -f $(OBJS)
- rm -f *~
-
-.PHONY: clean
View
336 MethodMove/MethodMove.cpp
@@ -1,336 +0,0 @@
-
-// based on https://github.com/loarabia/Clang-tutorial
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <set>
-
-#include "llvm/Support/Host.h"
-#include "llvm/Support/raw_ostream.h"
-
-#include "clang/Frontend/CompilerInstance.h"
-#include "clang/Basic/TargetOptions.h"
-#include "clang/Basic/TargetInfo.h"
-#include "clang/Basic/FileManager.h"
-#include "clang/Basic/SourceManager.h"
-#include "clang/Lex/Preprocessor.h"
-#include "clang/Lex/Lexer.h"
-#include "clang/Basic/Diagnostic.h"
-#include "clang/AST/RecursiveASTVisitor.h"
-#include "clang/AST/ASTConsumer.h"
-#include "clang/Parse/ParseAST.h"
-#include "clang/Rewrite/Rewriters.h"
-#include "clang/Rewrite/Rewriter.h"
-
-using namespace clang;
-
-class MyRecursiveASTVisitor : public RecursiveASTVisitor<MyRecursiveASTVisitor>
-{
-public:
- std::set<FileID> rewriteIDSet;
- Rewriter Rewrite;
- CompilerInstance *ci;
-
- static void PrintRange(const SourceRange& R, const SourceManager& SM) {
- llvm::outs() << "(";
- R.getBegin().print(llvm::outs(), SM);
- llvm::outs() << " -- ";
- R.getEnd().print(llvm::outs(), SM);
- llvm::outs() << ")";
- }
-
- bool TraverseDecl(Decl *D)
- {
- if (isa<NamespaceDecl>(D)) {
- NamespaceDecl* N = static_cast<NamespaceDecl*>(D);
- llvm::outs() << "Namespace: " << N->getDeclName().getAsString() << "\n";
- }
- else if (isa<CXXRecordDecl>(D)) {
- CXXRecordDecl* R = static_cast<CXXRecordDecl*>(D);
- llvm::outs() << "CXXRecordDecl: " << R->getDeclName().getAsString() << "\n";
- }
- else if (isa<CXXMethodDecl>(D)) {
- CXXMethodDecl* M = static_cast<CXXMethodDecl*>(D);
- llvm::outs() << "Method: " << M->getDeclName().getAsString();
-
- FullSourceLoc FSL(M->getNameInfo().getBeginLoc(), ci->getSourceManager());
- const FileEntry* F = ci->getSourceManager().getFileEntryForID(FSL.getFileID());
- llvm::outs() << "in: " << F->getName() << ", ";
-
- std::string fn(F->getName());
- if (fn.find("/usr") != std::string::npos) {
- return true;
- }
-
-
- std::string capturedSource;
- llvm::raw_string_ostream sst(capturedSource);
-
-
- // llvm::outs() << ", nameInfo: " << M->getNameInfo().getAsString();
- // llvm::outs() << ", beginLoc: ";
- // M->getNameInfo().getBeginLoc().print(llvm::outs(), ci->getSourceManager());
- // llvm::outs() << ", endLoc: ";
- // M->getNameInfo().getEndLoc().print(llvm::outs(), ci->getSourceManager());
-
- llvm::outs() << ", qualifier: " << M->getTypeQualifiers();
- llvm::outs() << ", return type: " << M->getResultType().getAsString();
- llvm::outs() << ", constExpr? " << M->isConstexpr();
-
- sst << M->getResultType().getAsString();
- sst << " ";
-
- // TODO: Namespace?
- sst << M->getParent()->getDeclName().getAsString();
- sst << "::";
- sst << M->getDeclName().getAsString();
- sst << "(";
-
- for (auto I = M->param_begin(), E = M->param_end(); I != E; ) {
- llvm::outs() << ", param ";
- PrintRange((*I)->getSourceRange(), ci->getSourceManager());
-
- const char* cdataBegin = ci->getSourceManager().getCharacterData((*I)->getSourceRange().getBegin());
- const char* cdataEnd = ci->getSourceManager().getCharacterData((*I)->getSourceRange().getEnd());
- std::string code(cdataBegin, cdataEnd - cdataBegin + 1);
- sst << code;
-
- ++I;
- if (I != E) {
- sst << ", ";
- }
- }
-
- sst << ")";
-
- // insert the const qualifier
- if (M->getTypeQualifiers() & Qualifiers::Const) {
- sst << " const";
- }
-
- if (isa<CXXConstructorDecl>(D)) {
- auto C = static_cast<CXXConstructorDecl*>(D);
-
- if (C->init_begin() != C->init_end()) {
-
- sst << " : ";
-
- // grab the initializers
- for (auto I = C->init_begin(), E = C->init_end(); I != E; ) {
- llvm::outs() << ", init'er ";
- PrintRange((*I)->getSourceRange(), ci->getSourceManager());
-
- const char* cdataBegin = ci->getSourceManager().getCharacterData((*I)->getSourceRange().getBegin());
- const char* cdataEnd = ci->getSourceManager().getCharacterData((*I)->getSourceRange().getEnd());
- std::string code(cdataBegin, cdataEnd - cdataBegin + 1);
- sst << code;
-
- ++I;
- if (I != E) {
- sst << ", ";
- }
-
- }
-
- }
- }
-
- SourceRange R = M->getSourceRange();
-
- llvm::outs() << ", body? " << M->hasBody() << ", from: ";
-
- R.getBegin().print(llvm::outs(), ci->getSourceManager());
-
- llvm::outs() << ", to: ";
- R.getEnd().print(llvm::outs(), ci->getSourceManager());
-
- llvm::outs() << "\n";
-
- if (M->hasBody()) {
- Stmt *S = M->getBody();
- SourceLocation LBL = S->getLocStart();
- SourceLocation RBL = S->getLocEnd();
-
- FullSourceLoc FSL(LBL, ci->getSourceManager());
- const FileEntry* F = ci->getSourceManager().getFileEntryForID(FSL.getFileID());
- llvm::outs() << "in: " << F->getName() << ", ";
-
- std::string fn(F->getName());
- if (fn.rfind(".h") != std::string::npos && fn.find("/usr") == std::string::npos) {
-
-
- const char* cdataBegin = ci->getSourceManager().getCharacterData(LBL);
- const char* cdataEnd = ci->getSourceManager().getCharacterData(RBL);
- std::string code(cdataBegin, cdataEnd - cdataBegin + 1);
- sst << code;
- sst << "\n";
-
- SourceLocation B = LBL;
-
- if (M->isInlineSpecified()) {
- B = R.getBegin();
- Rewrite.RemoveText(SourceRange(B, RBL));
- }
- else {
- Rewrite.ReplaceText(SourceRange(B, RBL), ";");
- }
-
- rewriteIDSet.insert(FSL.getFileID());
-
- llvm::outs() << "compound stmt, from : ";
-
- LBL.print(llvm::outs(), ci->getSourceManager());
- llvm::outs() << ", to: ";
- RBL.print(llvm::outs(), ci->getSourceManager());
- llvm::outs() << "\n\n";
-
- // write the code
- llvm::outs() << "captured source code: " << sst.str() << "\n";
-
- rewriteIDSet.insert(ci->getSourceManager().getMainFileID());
- auto leof = ci->getSourceManager().getLocForEndOfFile(ci->getSourceManager().getMainFileID());
- capturedSource.insert(0, "\n\n// ----\n");
- Rewrite.InsertText(leof, capturedSource);
- }
-
- }
-
- }
-
- bool traverseResult = RecursiveASTVisitor<MyRecursiveASTVisitor>::TraverseDecl(D);
-
- return traverseResult;
- }
-};
-
-
-class MyASTConsumer : public ASTConsumer {
-public:
- MyASTConsumer(const char *f);
- virtual bool HandleTopLevelDecl(DeclGroupRef d);
-
- MyRecursiveASTVisitor rv;
-};
-
-
-MyASTConsumer::MyASTConsumer(const char *f)
-{
- rv.ci = new CompilerInstance();
-
- // set to C++
- LangOptions& lo = rv.ci->getLangOpts();
- lo.CPlusPlus = 1;
- lo.CPlusPlus0x = 1;
-
- rv.ci->createDiagnostics(0, NULL);
-
- TargetOptions to;
- to.Triple = llvm::sys::getDefaultTargetTriple();
- TargetInfo *pti = TargetInfo::CreateTargetInfo(rv.ci->getDiagnostics(), to);
- rv.ci->setTarget(pti);
-
- rv.ci->createFileManager();
- rv.ci->createSourceManager(rv.ci->getFileManager());
- rv.ci->createPreprocessor();
- rv.ci->getPreprocessorOpts().UsePredefines = true;
-
- rv.ci->setASTConsumer(this);
-
- rv.ci->createASTContext();
-
-
- // Initialize rewriter
- rv.Rewrite.setSourceMgr(rv.ci->getSourceManager(), rv.ci->getLangOpts());
-
- const FileEntry *pFile = rv.ci->getFileManager().getFile(f);
- rv.ci->getSourceManager().createMainFileID(pFile);
- rv.ci->getDiagnosticClient().BeginSourceFile(rv.ci->getLangOpts(),
- &rv.ci->getPreprocessor());
-
- // Convert <file>.c to <file_out>.c
-
-
-
- ParseAST(rv.ci->getPreprocessor(), this, rv.ci->getASTContext());
- rv.ci->getDiagnosticClient().EndSourceFile();
-
-
- for (std::set<FileID>::iterator i = rv.rewriteIDSet.begin(), e = rv.rewriteIDSet.end(); i != e; ++i) {
- const FileEntry* F = rv.ci->getSourceManager().getFileEntryForID(*i);
-
- std::string outName(F->getName());
- size_t ext = outName.rfind(".");
- if (ext == std::string::npos) {
- ext = outName.length();
- }
-
- outName.insert(ext, "_out");
-
- llvm::errs() << "Output to: " << outName << "\n";
- std::string OutErrorInfo;
-
- llvm::raw_fd_ostream outFile(outName.c_str(), OutErrorInfo, 0);
-
-
- if (OutErrorInfo.empty())
- {
- // Now output rewritten source code
- const RewriteBuffer *RewriteBuf = rv.Rewrite.getRewriteBufferFor(*i);
- outFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
- }
- else
- {
- llvm::errs() << "Cannot open " << outName << " for writing\n";
- }
-
- outFile.close();
- }
-
-
- delete rv.ci;
-}
-
-bool MyASTConsumer::HandleTopLevelDecl(DeclGroupRef d)
-{
- llvm::outs() << "HandleTopLevelDecl: ";
-
- if (d.isSingleDecl()) {
- clang::Decl* D = d.getSingleDecl();
- llvm::outs() << "single: " << D->getDeclKindName() << "\n";
- }
- else {
- llvm::outs() << "group" << "\n";
- }
-
- typedef DeclGroupRef::iterator iter;
-
- for (iter b = d.begin(), e = d.end(); b != e; ++b)
- {
-
- rv.TraverseDecl(*b);
- }
-
- return true; // keep going
-}
-
-
-int main(int argc, char *argv[])
-{
- struct stat sb;
-
- if (argc != 2) {
- llvm::errs() << "usage: MethodMove <filename>\n";
- return 1;
- }
-
- if (stat(argv[1], &sb) == -1) {
- perror(argv[1]);
- exit(EXIT_FAILURE);
- }
-
- MyASTConsumer *astConsumer = new MyASTConsumer(argv[1]);
- (void)astConsumer;
-
- return 0;
-}
View
68 MethodMove/tests/foo.h
@@ -1,68 +0,0 @@
-namespace MyNameSpace {
- class Foo {
- private:
- int a;
- int b;
- public:
- Foo()
- {
- a = 5;
- b = 4;
- }
-
- Foo(int x) : a(x) { b = 3; }
-
- Foo(int* x) : a(*x), b(*x + 1) {}
-
- Foo(int x, int y);
-
- virtual ~Foo() {
- a = 0;
- }
-
- virtual void pure() = 0;
- virtual void pure(int x) = 0;
- virtual int pureWithReturn() = 0;
-
- void test() {
- a = 6;
- }
-
- void test(int x);
- void test(int x, int y);
-
- int testConstFunc(int x) const {
- return a + x;
- }
-
- void inlineTest();
- void inlineTest(int x);
-
- virtual void vtest() {
- }
-
- virtual void vtest(int x);
-
- const int getA() {
- return a;
- }
-
- static void StaticMethod() {
- }
- };
-
- class Bar : Foo {
- public:
- virtual void vtest() override {
- }
- };
-
- inline void Foo::inlineTest() {
- a = 7;
- }
-};
-
-inline void MyNameSpace::Foo::inlineTest(int x) {
- a = 8;
-}
-
View
41 Transforms/Accessors.cpp
@@ -5,33 +5,26 @@
using namespace clang;
-class AccessorsTransform : public Transform {
- Sema *sema;
+DEFINE_TRANSFORM_BEGIN(Accessors)
public:
- std::string getName() override {return "AccessorsTransform"; }
- void InitializeSema(Sema &sema) override {
- this->sema = &sema;
- }
+void HandleTranslationUnit(ASTContext &Ctx) override {
+ const TranslationUnitDecl *topLevel = Ctx.getTranslationUnitDecl();
- void HandleTranslationUnit(ASTContext &Ctx) override {
- const TranslationUnitDecl *topLevel = Ctx.getTranslationUnitDecl();
-
- printNamespaceFunctions(topLevel);
- }
- void printNamespaceFunctions(const DeclContext *ns_decl) const
- {
- for(auto subdecl = ns_decl->decls_begin(); subdecl != ns_decl->decls_end(); subdecl++) {
- if(const RecordDecl *rc_decl = dyn_cast<RecordDecl>(*subdecl)) {
- llvm::errs() << rc_decl->getQualifiedNameAsString() << "\n";
- for(auto member = rc_decl->field_begin(); member != rc_decl->field_end(); member++) {
- llvm::errs() << "\t" << member->getType().getAsString() << " " << member->getQualifiedNameAsString() << "\n";
- }
+ printNamespaceFunctions(topLevel);
+}
+void printNamespaceFunctions(const DeclContext *ns_decl) const
+{
+ for(auto subdecl = ns_decl->decls_begin(); subdecl != ns_decl->decls_end(); subdecl++) {
+ if(const RecordDecl *rc_decl = dyn_cast<RecordDecl>(*subdecl)) {
+ llvm::errs() << rc_decl->getQualifiedNameAsString() << "\n";
+ for(auto member = rc_decl->field_begin(); member != rc_decl->field_end(); member++) {
+ llvm::errs() << "\t" << member->getType().getAsString() << " " << member->getQualifiedNameAsString() << "\n";
}
- //recurse into inner contexts
- if(const DeclContext *inner_dc_decl = dyn_cast<DeclContext>(*subdecl))
- printNamespaceFunctions(inner_dc_decl);
}
+ //recurse into inner contexts
+ if(const DeclContext *inner_dc_decl = dyn_cast<DeclContext>(*subdecl))
+ printNamespaceFunctions(inner_dc_decl);
}
-};
+}
+DEFINE_TRANSFORM_END(Accessors);
-REGISTER_TRANSFORM(AccessorsTransform);
View
1 Transforms/CMakeLists.txt
@@ -1 +0,0 @@
-add_library (Transforms Transforms.cpp Accessors.cpp ClassRenameTransform.cpp)
View
240 Transforms/MethodMove/MethodMove.cpp
@@ -0,0 +1,240 @@
+#include "Transforms/Transforms.h"
+
+#include <clang/AST/Decl.h>
+#include <clang/Rewrite/Rewriter.h>
+#include <clang/Sema/Sema.h>
+#include <clang/Basic/SourceManager.h>
+
+using namespace clang;
+
+DEFINE_TRANSFORM_BEGIN(MethodMove)
+public:
+virtual void HandleTranslationUnit(ASTContext &ctx) override;
+virtual bool HandleTopLevelDecl(DeclGroupRef d) override;
+bool TraverseDecl(Decl *D);
+
+static void PrintRange(const SourceRange& R, const SourceManager& SM) {
+ llvm::outs() << "(";
+ R.getBegin().print(llvm::outs(), SM);
+ llvm::outs() << " -- ";
+ R.getEnd().print(llvm::outs(), SM);
+ llvm::outs() << ")";
+}
+DEFINE_TRANSFORM_END(MethodMove);
+
+bool MethodMoveTransform::TraverseDecl(Decl *D)
+{
+ if (NamespaceDecl* N = dyn_cast<NamespaceDecl>(D)) {
+ llvm::outs() << "Namespace: " << N->getDeclName().getAsString() << "\n";
+ }
+ else if (CXXRecordDecl* R = dyn_cast<CXXRecordDecl>(D)) {
+ llvm::outs() << "CXXRecordDecl: " << R->getDeclName().getAsString() << "\n";
+ }
+ else if (CXXMethodDecl* M = dyn_cast<CXXMethodDecl>(D)) {
+ llvm::outs() << "Method: " << M->getDeclName().getAsString();
+
+ FullSourceLoc FSL(M->getNameInfo().getBeginLoc(), rewriter.getSourceMgr());
+ const FileEntry* F = rewriter.getSourceMgr().getFileEntryForID(FSL.getFileID());
+ llvm::outs() << "in: " << F->getName() << ", ";
+
+ std::string fn(F->getName());
+ if (fn.find("/usr") != std::string::npos) {
+ return true;
+ }
+
+
+ std::string capturedSource;
+ llvm::raw_string_ostream sst(capturedSource);
+
+
+ // llvm::outs() << ", nameInfo: " << M->getNameInfo().getAsString();
+ // llvm::outs() << ", beginLoc: ";
+ // M->getNameInfo().getBeginLoc().print(llvm::outs(), ci->getSourceManager());
+ // llvm::outs() << ", endLoc: ";
+ // M->getNameInfo().getEndLoc().print(llvm::outs(), ci->getSourceManager());
+
+ llvm::outs() << ", qualifier: " << M->getTypeQualifiers();
+ llvm::outs() << ", return type: " << M->getResultType().getAsString();
+ llvm::outs() << ", constExpr? " << M->isConstexpr();
+
+ sst << M->getResultType().getAsString();
+ sst << " ";
+
+ // TODO: Namespace?
+ sst << M->getParent()->getDeclName().getAsString();
+ sst << "::";
+ sst << M->getDeclName().getAsString();
+ sst << "(";
+
+ for (auto I = M->param_begin(), E = M->param_end(); I != E; ) {
+ llvm::outs() << ", param ";
+ PrintRange((*I)->getSourceRange(), rewriter.getSourceMgr());
+
+ const char* cdataBegin = rewriter.getSourceMgr().getCharacterData((*I)->getSourceRange().getBegin());
+ const char* cdataEnd = rewriter.getSourceMgr().getCharacterData((*I)->getSourceRange().getEnd());
+ std::string code(cdataBegin, cdataEnd - cdataBegin + 1);
+ sst << code;
+
+ ++I;
+ if (I != E) {
+ sst << ", ";
+ }
+ }
+
+ sst << ")";
+
+ // insert the const qualifier
+ if (M->getTypeQualifiers() & Qualifiers::Const) {
+ sst << " const";
+ }
+
+ if (CXXConstructorDecl* C = dyn_cast<CXXConstructorDecl>(D)) {
+ if (C->init_begin() != C->init_end()) {
+
+ sst << " : ";
+
+ // grab the initializers
+ for (auto I = C->init_begin(), E = C->init_end(); I != E; ) {
+ llvm::outs() << ", init'er ";
+ PrintRange((*I)->getSourceRange(), rewriter.getSourceMgr());
+
+ const char* cdataBegin = rewriter.getSourceMgr().getCharacterData((*I)->getSourceRange().getBegin());
+ const char* cdataEnd = rewriter.getSourceMgr().getCharacterData((*I)->getSourceRange().getEnd());
+ std::string code(cdataBegin, cdataEnd - cdataBegin + 1);
+ sst << code;
+
+ ++I;
+ if (I != E) {
+ sst << ", ";
+ }
+
+ }
+
+ }
+ }
+
+ SourceRange R = M->getSourceRange();
+
+ llvm::outs() << ", body? " << M->hasBody() << ", from: ";
+
+ R.getBegin().print(llvm::outs(), rewriter.getSourceMgr());
+
+ llvm::outs() << ", to: ";
+ R.getEnd().print(llvm::outs(), rewriter.getSourceMgr());
+
+ llvm::outs() << "\n";
+
+ if (M->hasBody()) {
+ Stmt *S = M->getBody();
+ SourceLocation LBL = S->getLocStart();
+ SourceLocation RBL = S->getLocEnd();
+
+ FullSourceLoc FSL(LBL, rewriter.getSourceMgr());
+ const FileEntry* F = rewriter.getSourceMgr().getFileEntryForID(FSL.getFileID());
+ llvm::outs() << "in: " << F->getName() << ", ";
+
+ std::string fn(F->getName());
+ if (fn.rfind(".h") != std::string::npos && fn.find("/usr") == std::string::npos) {
+
+
+ const char* cdataBegin = rewriter.getSourceMgr().getCharacterData(LBL);
+ const char* cdataEnd = rewriter.getSourceMgr().getCharacterData(RBL);
+ std::string code(cdataBegin, cdataEnd - cdataBegin + 1);
+ sst << code;
+ sst << "\n";
+
+ SourceLocation B = LBL;
+
+ if (M->isInlineSpecified()) {
+ B = R.getBegin();
+ rewriter.RemoveText(SourceRange(B, RBL));
+ }
+ else {
+ rewriter.ReplaceText(SourceRange(B, RBL), ";");
+ }
+
+ llvm::outs() << "compound stmt, from : ";
+
+ LBL.print(llvm::outs(), rewriter.getSourceMgr());
+ llvm::outs() << ", to: ";
+ RBL.print(llvm::outs(), rewriter.getSourceMgr());
+ llvm::outs() << "\n\n";
+
+ // write the code
+ llvm::outs() << "captured source code: " << sst.str() << "\n";
+
+ auto leof = rewriter.getSourceMgr().getLocForEndOfFile(rewriter.getSourceMgr().getMainFileID());
+ capturedSource.insert(0, "\n\n// ----\n");
+ rewriter.InsertText(leof, capturedSource);
+ }
+
+ }
+
+ }
+
+ bool traverseResult = true;
+ if(DeclContext *DC = dyn_cast<DeclContext>(D)) {
+ for(auto subdecl = DC->decls_begin(); traverseResult && subdecl != DC->decls_end(); ++subdecl) {
+ traverseResult = traverseResult && TraverseDecl(*subdecl);
+ }
+ }
+
+ return traverseResult;
+}
+
+void MethodMoveTransform::HandleTranslationUnit(ASTContext &ctx)
+{
+ for (Rewriter::buffer_iterator iter = rewriter.buffer_begin(); iter != rewriter.buffer_end(); iter++) {
+ const FileEntry* F = rewriter.getSourceMgr().getFileEntryForID(iter->first);
+
+ std::string outName(F->getName());
+ size_t ext = outName.rfind(".");
+ if (ext == std::string::npos) {
+ ext = outName.length();
+ }
+
+ outName.insert(ext, "_out");
+
+ llvm::errs() << "Output to: " << outName << "\n";
+ std::string OutErrorInfo;
+
+ llvm::raw_fd_ostream outFile(outName.c_str(), OutErrorInfo, 0);
+
+
+ if (OutErrorInfo.empty())
+ {
+ // Now output rewritten source code
+ const RewriteBuffer *RewriteBuf = &iter->second;
+ outFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
+ }
+ else
+ {
+ llvm::errs() << "Cannot open " << outName << " for writing\n";
+ }
+
+ outFile.close();
+ }
+}
+
+bool MethodMoveTransform::HandleTopLevelDecl(DeclGroupRef d)
+{
+ llvm::outs() << "HandleTopLevelDecl: ";
+
+ if (d.isSingleDecl()) {
+ clang::Decl* D = d.getSingleDecl();
+ llvm::outs() << "single: " << D->getDeclKindName() << "\n";
+ }
+ else {
+ llvm::outs() << "group" << "\n";
+ }
+
+ typedef DeclGroupRef::iterator iter;
+
+ for (iter b = d.begin(), e = d.end(); b != e; ++b)
+ {
+
+ TraverseDecl(*b);
+ }
+
+ return true; // keep going
+}
View
0 MethodMove/README.markdown → Transforms/MethodMove/README.markdown
File renamed without changes.
View
39 Transforms/Transforms.cpp
@@ -2,59 +2,56 @@
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
-#include <clang/Lex/Preprocessor.h>
using namespace clang;
using namespace clang::tooling;
TransformRegistry &TransformRegistry::get()
{
- static TransformRegistry instance;
- return instance;
+ static TransformRegistry instance;
+ return instance;
}
void TransformRegistry::add(transform_creator creator)
{
- m_transforms.push_back(creator);
+ m_transforms.push_back(creator);
}
TransformRegistry::iterator TransformRegistry::begin()
{
- return m_transforms.begin();
+ return m_transforms.begin();
}
TransformRegistry::iterator TransformRegistry::end()
{
- return m_transforms.end();
+ return m_transforms.end();
}
TransformRegistration::TransformRegistration(transform_creator creator)
{
- TransformRegistry::get().add(creator);
+ TransformRegistry::get().add(creator);
}
class TransformAction : public clang::ASTFrontendAction {
private:
- transform_creator tcreator;
+ transform_creator tcreator;
public:
- TransformAction(transform_creator creator) {tcreator = creator;}
+ TransformAction(transform_creator creator) {tcreator = creator;}
protected:
- ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) override {
- Transform *T = tcreator();
- T->setCompilerInstance(&CI);
- return T;
- }
-
- virtual bool BeginInvocation(CompilerInstance &CI) override {
- CI.getHeaderSearchOpts().AddPath("/usr/local/lib/clang/3.2/include", clang::frontend::System, false, false, false);
- return true;
- }
+ ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) override {
+ return tcreator();
+ }
+
+ virtual bool BeginInvocation(CompilerInstance &CI) override {
+ CI.getHeaderSearchOpts().AddPath("/usr/local/lib/clang/3.2/include", clang::frontend::System, false, false, false);
+ return true;
+ }
};
TransformFactory::TransformFactory(transform_creator creator) {
- tcreator = creator;
+ tcreator = creator;
}
FrontendAction *TransformFactory::create() {
- return new TransformAction(tcreator);
+ return new TransformAction(tcreator);
}
View
60 Transforms/Transforms.h
@@ -1,6 +1,8 @@
#include <string>
#include <vector>
+#include <clang/Rewrite/Rewriter.h>
+#include <clang/Sema/Sema.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/Tooling/Tooling.h>
#include <clang/Frontend/CompilerInstance.h>
@@ -10,47 +12,55 @@ class clang::CompilerInstance;
class Transform : public clang::SemaConsumer
{
protected:
- clang::CompilerInstance *compilerInstance;
-public:
- Transform() : compilerInstance(0) {}
- virtual std::string getName() = 0;
- virtual void setCompilerInstance(clang::CompilerInstance *CI) {
- compilerInstance = CI;
- }
+ clang::Sema *sema;
+ clang::Rewriter rewriter;
+ virtual std::string getName() = 0;
+ virtual void InitializeSema(clang::Sema &s) override {sema = &s; rewriter.setSourceMgr(this->sema->getSourceManager(), sema->getLangOpts());}
+
};
template <typename T> Transform* transform_factory()
{
- return new T;
+ return new T;
}
typedef Transform* (*transform_creator)(void);
class TransformRegistry
{
-private:
- std::vector<transform_creator> m_transforms;
-public:
- typedef std::vector<transform_creator>::iterator iterator;
- static TransformRegistry& get();
- void add(transform_creator);
- iterator begin();
- iterator end();
+ private:
+ std::vector<transform_creator> m_transforms;
+ public:
+ typedef std::vector<transform_creator>::iterator iterator;
+ static TransformRegistry& get();
+ void add(transform_creator);
+ iterator begin();
+ iterator end();
};
class TransformRegistration
{
-public:
- TransformRegistration(transform_creator);
+ public:
+ TransformRegistration(transform_creator);
};
-#define REGISTER_TRANSFORM(transform) \
- TransformRegistration _transform_registration_ ## transform(&transform_factory<transform>);
+#define REGISTER_TRANSFORM(transform) \
+ TransformRegistration _transform_registration_ \
+ ## transform(&transform_factory<transform>)
+
+#define DEFINE_TRANSFORM_BEGIN(transform) \
+ class transform##Transform : public Transform { \
+public: \
+ std::string getName() override {return #transform "Transform"; }
+
+#define DEFINE_TRANSFORM_END(transform) \
+ }; \
+ REGISTER_TRANSFORM(transform##Transform)
class TransformFactory : public clang::tooling::FrontendActionFactory {
-private:
- transform_creator tcreator;
-public:
- TransformFactory(transform_creator creator);
- clang::FrontendAction *create() override;
+ private:
+ transform_creator tcreator;
+ public:
+ TransformFactory(transform_creator creator);
+ clang::FrontendAction *create() override;
};
View
10 tests/MethodMove/CMakeLists.txt
@@ -0,0 +1,10 @@
+CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
+
+PROJECT (MethodMove_test)
+
+SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+SET(CMAKE_BUILD_TYPE None)
+SET(CMAKE_C_COMPILER clang)
+SET(CMAKE_CXX_COMPILER clang++)
+
+ADD_LIBRARY (foo foo.cpp)
View
10 MethodMove/tests/foo.cpp → tests/MethodMove/foo.cpp
@@ -1,17 +1,17 @@
#include "foo.h"
namespace MyNameSpace {
- Foo::Foo(int x, int y) {
- a = x + y;
- }
+ Foo::Foo(int x, int y) {
+ a = x + y;
+ }
};
void MyNameSpace::Foo::test(int x) {
- a = x * 2;
+ a = x * 2;
}
using namespace MyNameSpace;
void Foo::test(int x, int y) {
- a = x + y;
+ a = x + y;
}
View
67 tests/MethodMove/foo.h
@@ -0,0 +1,67 @@
+namespace MyNameSpace {
+ class Foo {
+ private:
+ int a;
+ int b;
+ public:
+ Foo() {
+ a = 5;
+ b = 4;
+ }
+
+ Foo(int x) : a(x) { b = 3; }
+
+ Foo(int* x) : a(*x), b(*x + 1) {}
+
+ Foo(int x, int y);
+
+ virtual ~Foo() {
+ a = 0;
+ }
+
+ virtual void pure() = 0;
+ virtual void pure(int x) = 0;
+ virtual int pureWithReturn() = 0;
+
+ void test() {
+ a = 6;
+ }
+
+ void test(int x);
+ void test(int x, int y);
+
+ int testConstFunc(int x) const {
+ return a + x;
+ }
+
+ void inlineTest();
+ void inlineTest(int x);
+
+ virtual void vtest() {
+ }
+
+ virtual void vtest(int x);
+
+ const int getA() {
+ return a;
+ }
+
+ static void StaticMethod() {
+ }
+ };
+
+ class Bar : Foo {
+ public:
+ virtual void vtest() override {
+ }
+ };
+
+ inline void Foo::inlineTest() {
+ a = 7;
+ }
+};
+
+inline void MyNameSpace::Foo::inlineTest(int x) {
+ a = 8;
+}
+

0 comments on commit c1a61fe

Please sign in to comment.
Something went wrong with that request. Please try again.