Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch #1

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions clang/labs/lab1/kruglov_alexey/RenameID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,30 @@ class RenameVisitor : public clang::RecursiveASTVisitor<RenameVisitor> {

if (Name == OldName) {
Rewriter.ReplaceText(Func->getNameInfo().getSourceRange(), NewName);
Rewriter.overwriteChangedFiles();
}
if (Func->getReturnType().getAsString() == OldName ||
Func->getReturnType().getAsString() == OldName + " *" ||
Func->getReturnType().getAsString() == OldName + " &") {
Rewriter.ReplaceText(Func->getFunctionTypeLoc().getBeginLoc(),
OldName.size(), NewName);
}

if (!Func->param_empty()) {
for (auto IVar = Func->param_begin(); IVar != Func->param_end(); IVar++) {
auto Var = static_cast<clang::VarDecl *>(*IVar);
Name = Var->getNameAsString();
auto TypeLoc = Var->getTypeSourceInfo()->getTypeLoc();

if (Name == OldName) {
Rewriter.ReplaceText(Var->getLocation(), Name.size(), NewName);
}

if ((TypeLoc.getType().getAsString() == OldName ||
TypeLoc.getType().getAsString() == OldName + " *" ||
TypeLoc.getType().getAsString() == OldName + " &")) {
Rewriter.ReplaceText(TypeLoc.getBeginLoc(), OldName.size(), NewName);
}
}
}
return true;
}
Expand All @@ -31,25 +54,34 @@ class RenameVisitor : public clang::RecursiveASTVisitor<RenameVisitor> {

if (Name == OldName) {
Rewriter.ReplaceText(Expr->getNameInfo().getSourceRange(), NewName);
Rewriter.overwriteChangedFiles();
}

return true;
}

bool VisitVarDecl(clang::VarDecl *Var) {
bool VisitDeclStmt(clang::DeclStmt *Stmt) {
auto IVar = Stmt->decl_begin();
auto Var = static_cast<clang::VarDecl *>(*IVar);
std::string Name = Var->getNameAsString();
auto TypeLoc = Var->getTypeSourceInfo()->getTypeLoc();

if (Name == OldName) {
Rewriter.ReplaceText(Var->getLocation(), Name.size(), NewName);
Rewriter.overwriteChangedFiles();
}

if (Var->getType().getAsString() == OldName ||
Var->getType().getAsString() == OldName + " *" ||
Var->getType().getAsString() == OldName + " &") {
Rewriter.ReplaceText(Var->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
OldName.size(), NewName);
Rewriter.overwriteChangedFiles();
if ((TypeLoc.getType().getAsString() == OldName ||
TypeLoc.getType().getAsString() == OldName + " *" ||
TypeLoc.getType().getAsString() == OldName + " &")) {
Rewriter.ReplaceText(TypeLoc.getBeginLoc(), OldName.size(), NewName);
}

for (IVar++; IVar != Stmt->decl_end(); IVar++) {

Var = static_cast<clang::VarDecl *>(*IVar);
Name = Var->getNameAsString();
if (Name == OldName) {
Rewriter.ReplaceText(Var->getLocation(), Name.size(), NewName);
}
}

return true;
Expand All @@ -65,9 +97,8 @@ class RenameVisitor : public clang::RecursiveASTVisitor<RenameVisitor> {
if (RecordDestr)
Rewriter.ReplaceText(RecordDestr->getLocation(), Name.size() + 1,
"~" + NewName);

Rewriter.overwriteChangedFiles();
}

return true;
}

Expand All @@ -77,25 +108,27 @@ class RenameVisitor : public clang::RecursiveASTVisitor<RenameVisitor> {
if (Name == OldName) {
Rewriter.ReplaceText(NewExpr->getExprLoc(), Name.size() + 4,
"new " + NewName);
Rewriter.overwriteChangedFiles();
}

return true;
}

bool OverwriteChangedFiles() { return Rewriter.overwriteChangedFiles(); }
};

class RenameIDConsumer : public clang::ASTConsumer {
protected:
RenameVisitor visitor;

public:
explicit RenameIDConsumer(clang::CompilerInstance &CI,
std::string OldName,
explicit RenameIDConsumer(clang::CompilerInstance &CI, std::string OldName,
std::string NewName)
: visitor(clang::Rewriter(CI.getSourceManager(), CI.getLangOpts()),
OldName, NewName) {}

void HandleTranslationUnit(clang::ASTContext &Context) override {
visitor.TraverseDecl(Context.getTranslationUnitDecl());
visitor.OverwriteChangedFiles();
}
};

Expand All @@ -107,12 +140,12 @@ class RenameIDPlugin : public clang::PluginASTAction {
protected:
bool ParseArgs(const clang::CompilerInstance &CI,
const std::vector<std::string> &args) override {
if (args[0].find("OldName=") == std::string::npos || args[1].find("NewName=") == std::string::npos){
if (args[0].find("OldName=") != 0 || args[1].find("NewName=") != 0) {
llvm::errs() << "Error in parameters input.\n"
"Format of input:\n"
"OldName='MyOldName'\n"
"NewName='MyNewName'\n";
return true;
return false;
}

OldName = args[0].substr(args[0].find("=") + 1);
Expand Down
28 changes: 27 additions & 1 deletion clang/test/lab1/kruglov_alexey/RenameID_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
// VAR-NEXT: return *new_var;
// VAR-NEXT: }

// RUN: %clang_cc1 -load %llvmshlibdir/RenameIDPlugin%pluginext\
// RUN: -add-plugin RenameID\
// RUN: -plugin-arg-RenameID OldName=int\
// RUN: -plugin-arg-RenameID NewName=long %t/rename_type.cpp
// RUN: FileCheck %s < %t/rename_type.cpp --check-prefix=TYPE

// TYPE: long* func(long x, long y) {
// TYPE-NEXT: long *a, *c;
// TYPE-NEXT: long b = 2, d, e;
// TYPE-NEXT: a = &b;
// TYPE-NEXT: a = b + a;
// TYPE-NEXT: a++;
// TYPE-NEXT: return a;
// TYPE-NEXT: }

// RUN: %clang_cc1 -load %llvmshlibdir/RenameIDPlugin%pluginext\
// RUN: -add-plugin RenameID\
// RUN: -plugin-arg-RenameID OldName=func\
Expand Down Expand Up @@ -52,6 +67,7 @@
// CLASS-NEXT: NewClass() {}
// CLASS-NEXT: NewClass(int a, int b): a(a), b(b) {}
// CLASS-NEXT: NewClass(NewClass &b) {}
// CLASS-NEXT: NewClass func(NewClass c){ return c; };
// CLASS-NEXT: ~NewClass();
// CLASS-NEXT: };
// CLASS: void func(NewClass c) {
Expand All @@ -62,7 +78,7 @@

// RUN: %clang_cc1 -load %llvmshlibdir/RenameIDPlugin%pluginext\
// RUN: -add-plugin RenameID\
// RUN: -plugin-arg-RenameID OldClass\
// RUN: -plugin-arg-RenameID SomeOldName=SomeOldName\
// RUN: -plugin-arg-RenameID NewName=NewClass \
// RUN: 2>&1 | FileCheck %s --check-prefix=ERROR

Expand All @@ -80,6 +96,15 @@ int func() {
a++;
return *a;
}
//--- rename_type.cpp
int* func(int x, int y) {
int *a, *c;
int b = 2, d, e;
a = &b;
a = b + a;
a++;
return a;
}
//--- rename_func.cpp
int func(int b) {
return 1 + 1;
Expand All @@ -101,6 +126,7 @@ class OldClass{
OldClass() {}
OldClass(int a, int b): a(a), b(b) {}
OldClass(OldClass &b) {}
OldClass func(OldClass c){ return c; };
~OldClass();
};
void func(OldClass c) {
Expand Down
Loading