From d2d6a32454a172955c65f9cc35a9b30578835e54 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 12 Jan 2021 10:49:42 -0500 Subject: [PATCH 1/6] Duplication WIP --- clang/include/clang/3C/Duplicate.h | 19 +++++++++ clang/lib/3C/CMakeLists.txt | 1 + clang/lib/3C/Duplicate.cpp | 62 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 clang/include/clang/3C/Duplicate.h create mode 100644 clang/lib/3C/Duplicate.cpp diff --git a/clang/include/clang/3C/Duplicate.h b/clang/include/clang/3C/Duplicate.h new file mode 100644 index 000000000000..691abdd56d7d --- /dev/null +++ b/clang/include/clang/3C/Duplicate.h @@ -0,0 +1,19 @@ +#ifndef LLVM_DUPLCIATE_H +#define LLVM_DUPLCIATE_H + +#include "clang/AST/ASTContext.h" +#include "ProgramInfo.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Stmt.h" +#include "clang/Rewrite/Core/Rewriter.h" + +typedef std::function selector; + +//TODO this should be a bool for error reporting +void create_duplicate(clang::ASTContext &Context, clang::Rewriter &R, + ProgramInfo &Info, + clang::Decl *FD, selector P); + + + +#endif //LLVM_DUPLCIATE_H diff --git a/clang/lib/3C/CMakeLists.txt b/clang/lib/3C/CMakeLists.txt index 6027224db916..0cd1c308c8da 100644 --- a/clang/lib/3C/CMakeLists.txt +++ b/clang/lib/3C/CMakeLists.txt @@ -40,6 +40,7 @@ set( LLVM_LINK_COMPONENTS StructInit.cpp TypeVariableAnalysis.cpp Utils.cpp + Duplicate.cpp ${five_c_source} LINK_LIBS clangAST diff --git a/clang/lib/3C/Duplicate.cpp b/clang/lib/3C/Duplicate.cpp new file mode 100644 index 000000000000..de8f892305a7 --- /dev/null +++ b/clang/lib/3C/Duplicate.cpp @@ -0,0 +1,62 @@ +#include "clang/3C/Duplicate.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/3C/ProgramInfo.h" +#include "clang/AST/Stmt.h" +#include "clang/Rewrite/Core/Rewriter.h" + +using namespace clang; +using namespace llvm; + + +class FindVarDecl : public RecursiveASTVisitor { +public: + explicit FindVarDecl(ASTContext &Context, Rewriter &R, + ProgramInfo &I, selector P) + : Context(Context), R(R), Info(I), P(P) {} + + bool VisitVarDecl(VarDecl *VD) { + if(P(VD)){ + llvm::errs() << "Found vardecl\n"; + addNewVD(VD); + return false; + } else { + return true; + } + } + + //TODO properly gensym the name + //TODO ensure works for multi-decls + std::string createDuplicateString(VarDecl *VD) { + auto CV = Info.getVariable(VD, &Context); + auto Type = + CV.hasValue() ? + CV.getValue() + .mkString(Info.getConstraints().getVariables(), + false) + : VD->getType().getAsString(); + auto TargetName = VD->getNameAsString(); + return ";\n" + Type + " newvarname = " + TargetName + ""; + } + + void addNewVD(VarDecl *VD) { + SourceLocation End = VD->getEndLoc(); + R.InsertTextAfterToken(End, createDuplicateString(VD)); + } + + +private: + ASTContext &Context; + Rewriter &R; + ProgramInfo &Info; + selector P; + +}; + +void create_duplicate(ASTContext &Context, Rewriter &R, + ProgramInfo &Info, + Decl *FD, selector P) { + llvm::errs() << "Hit!\n"; + auto V = FindVarDecl(Context, R, Info, P); + V.TraverseDecl(FD); +} From 4916b8a31cc0b952bd5d262b0d02f04fed143058 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 12 Jan 2021 13:19:03 -0500 Subject: [PATCH 2/6] Variable naming --- clang/lib/3C/Duplicate.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/clang/lib/3C/Duplicate.cpp b/clang/lib/3C/Duplicate.cpp index de8f892305a7..1c7a5bfd92c0 100644 --- a/clang/lib/3C/Duplicate.cpp +++ b/clang/lib/3C/Duplicate.cpp @@ -17,7 +17,6 @@ class FindVarDecl : public RecursiveASTVisitor { bool VisitVarDecl(VarDecl *VD) { if(P(VD)){ - llvm::errs() << "Found vardecl\n"; addNewVD(VD); return false; } else { @@ -25,8 +24,7 @@ class FindVarDecl : public RecursiveASTVisitor { } } - //TODO properly gensym the name - //TODO ensure works for multi-decls + //TODO handle mixed type multi-decl std::string createDuplicateString(VarDecl *VD) { auto CV = Info.getVariable(VD, &Context); auto Type = @@ -36,7 +34,8 @@ class FindVarDecl : public RecursiveASTVisitor { false) : VD->getType().getAsString(); auto TargetName = VD->getNameAsString(); - return ";\n" + Type + " newvarname = " + TargetName + ""; + auto NewName = "__" + TargetName + "_copy"; + return ";\n" + Type + " " + NewName + "= " + TargetName + ""; } void addNewVD(VarDecl *VD) { @@ -56,7 +55,6 @@ class FindVarDecl : public RecursiveASTVisitor { void create_duplicate(ASTContext &Context, Rewriter &R, ProgramInfo &Info, Decl *FD, selector P) { - llvm::errs() << "Hit!\n"; auto V = FindVarDecl(Context, R, Info, P); V.TraverseDecl(FD); } From fc96f232feb0fc3fea6ec81d93c8458c89948682 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Tue, 12 Jan 2021 14:17:49 -0500 Subject: [PATCH 3/6] Cleanup --- clang/include/clang/3C/Duplicate.h | 15 ++++++---- clang/lib/3C/Duplicate.cpp | 46 +++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/clang/include/clang/3C/Duplicate.h b/clang/include/clang/3C/Duplicate.h index 691abdd56d7d..5dac5c356794 100644 --- a/clang/include/clang/3C/Duplicate.h +++ b/clang/include/clang/3C/Duplicate.h @@ -1,18 +1,23 @@ #ifndef LLVM_DUPLCIATE_H #define LLVM_DUPLCIATE_H -#include "clang/AST/ASTContext.h" #include "ProgramInfo.h" +#include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/Stmt.h" #include "clang/Rewrite/Core/Rewriter.h" +#include typedef std::function selector; -//TODO this should be a bool for error reporting -void create_duplicate(clang::ASTContext &Context, clang::Rewriter &R, - ProgramInfo &Info, - clang::Decl *FD, selector P); +// Create a duplicate of a variable. +// ToScan is the context to search for the variable +// P is a predicate that decides which variable to make a duplicate of +// Returns the name of the newly created variable +// or None if no variable matching P is found +Option createDuplicate(clang::ASTContext &Context, + clang::Rewriter &R, ProgramInfo &Info, + clang::Decl *ToScan, selector P); diff --git a/clang/lib/3C/Duplicate.cpp b/clang/lib/3C/Duplicate.cpp index 1c7a5bfd92c0..bd64456cb15b 100644 --- a/clang/lib/3C/Duplicate.cpp +++ b/clang/lib/3C/Duplicate.cpp @@ -2,30 +2,51 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/3C/ProgramInfo.h" -#include "clang/AST/Stmt.h" #include "clang/Rewrite/Core/Rewriter.h" using namespace clang; using namespace llvm; +using namespace std; +// Recursive Visitor class FindVarDecl : public RecursiveASTVisitor { public: explicit FindVarDecl(ASTContext &Context, Rewriter &R, ProgramInfo &I, selector P) : Context(Context), R(R), Info(I), P(P) {} + bool VisitVarDecl(VarDecl *VD) { + // If the selctor matches execute the duplicate function + // and store the new name in the optional if(P(VD)){ - addNewVD(VD); + auto NewName = addDuplicate(VD); + Replacement = { NewName }; return false; } else { return true; } } - //TODO handle mixed type multi-decl - std::string createDuplicateString(VarDecl *VD) { + Option didReplace(void) { + return Replacement; + } + +private: + // Insert the duplicate into the rewriter + // Returns the new var's name + string addDuplicate(VarDecl *VD) { + SourceLocation End = VD->getEndLoc(); + auto Dup = createDuplicateString(VD); + R.InsertTextAfterToken(End, Dup.second); + return Dup.first; + } + + // Create the text for the new variable + // Returns a pair containing the new name + // and the new line containing declaration and assignment + pair createDuplicateString(VarDecl *VD) { auto CV = Info.getVariable(VD, &Context); auto Type = CV.hasValue() ? @@ -35,26 +56,25 @@ class FindVarDecl : public RecursiveASTVisitor { : VD->getType().getAsString(); auto TargetName = VD->getNameAsString(); auto NewName = "__" + TargetName + "_copy"; - return ";\n" + Type + " " + NewName + "= " + TargetName + ""; + return make_pair(NewName, ";\n" + Type + " " + NewName + "= " + TargetName + ""); } - void addNewVD(VarDecl *VD) { - SourceLocation End = VD->getEndLoc(); - R.InsertTextAfterToken(End, createDuplicateString(VD)); - } -private: ASTContext &Context; Rewriter &R; ProgramInfo &Info; + // Predicate for finding the variable to duplicate selector P; - + // Duplicate's name or none if var not found + Option Replacement = {}; }; -void create_duplicate(ASTContext &Context, Rewriter &R, +// Main entrypoint +Option createDuplicate(ASTContext &Context, Rewriter &R, ProgramInfo &Info, Decl *FD, selector P) { auto V = FindVarDecl(Context, R, Info, P); V.TraverseDecl(FD); -} + return V.didReplace(); +} \ No newline at end of file From dd8d0e31786e0786c2f240d16a75ff0314964961 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Wed, 13 Jan 2021 11:15:40 -0500 Subject: [PATCH 4/6] Typo fix pt 1 Co-authored-by: Matt McCutchen (Correct Computation) --- clang/include/clang/3C/Duplicate.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/3C/Duplicate.h b/clang/include/clang/3C/Duplicate.h index 5dac5c356794..d99793c51dfe 100644 --- a/clang/include/clang/3C/Duplicate.h +++ b/clang/include/clang/3C/Duplicate.h @@ -1,5 +1,5 @@ -#ifndef LLVM_DUPLCIATE_H -#define LLVM_DUPLCIATE_H +#ifndef LLVM_DUPLICATE_H +#define LLVM_DUPLICATE_H #include "ProgramInfo.h" #include "clang/AST/ASTContext.h" From a4a4d9f1b3e1cb01e52d1acd3ee9d7e8ef939512 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Wed, 13 Jan 2021 11:15:59 -0500 Subject: [PATCH 5/6] Typo fix pt 2 Co-authored-by: Matt McCutchen (Correct Computation) --- clang/include/clang/3C/Duplicate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/3C/Duplicate.h b/clang/include/clang/3C/Duplicate.h index d99793c51dfe..beb50f30de76 100644 --- a/clang/include/clang/3C/Duplicate.h +++ b/clang/include/clang/3C/Duplicate.h @@ -21,4 +21,4 @@ Option createDuplicate(clang::ASTContext &Context, -#endif //LLVM_DUPLCIATE_H +#endif //LLVM_DUPLICATE_H From 4fab0a57be46d6b1d4946f8ede98421791557684 Mon Sep 17 00:00:00 2001 From: Aaron Eline Date: Fri, 15 Jan 2021 09:54:47 -0500 Subject: [PATCH 6/6] Update clang/lib/3C/Duplicate.cpp typo Co-authored-by: Matt McCutchen (Correct Computation) --- clang/lib/3C/Duplicate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/3C/Duplicate.cpp b/clang/lib/3C/Duplicate.cpp index bd64456cb15b..e653ded1fe02 100644 --- a/clang/lib/3C/Duplicate.cpp +++ b/clang/lib/3C/Duplicate.cpp @@ -18,7 +18,7 @@ class FindVarDecl : public RecursiveASTVisitor { bool VisitVarDecl(VarDecl *VD) { - // If the selctor matches execute the duplicate function + // If the selector matches execute the duplicate function // and store the new name in the optional if(P(VD)){ auto NewName = addDuplicate(VD); @@ -77,4 +77,4 @@ Option createDuplicate(ASTContext &Context, Rewriter &R, auto V = FindVarDecl(Context, R, Info, P); V.TraverseDecl(FD); return V.didReplace(); -} \ No newline at end of file +}