Skip to content
Open
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
2 changes: 1 addition & 1 deletion Transforms/RenameTransforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class RenameTransform : public Transform {
if (QN.size() == 0) {
return false;
}

// special handling for TagDecl
if (auto T = llvm::dyn_cast<clang::TagDecl>(D)) {
auto KN = T->getKindName();
Expand Down
50 changes: 45 additions & 5 deletions Transforms/TypeRenameTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ void TypeRenameTransform::collectRenameDecls(DeclContext *DC, bool topLevel)
collectRenameDecls(RD);
}
}
else if (auto D = dyn_cast<NamespaceDecl>(*I)) {
std::string newName;
if (nameMatches(D, newName)) {
renameLocation(L, newName);
}
}

// descend into the next level (namespace, etc.)
if (auto innerDC = dyn_cast<DeclContext>(*I)) {
Expand Down Expand Up @@ -211,6 +217,11 @@ void TypeRenameTransform::processDeclContext(DeclContext *DC, bool topLevel)
processTypeLoc(TSI->getTypeLoc());
}
}
// else if (auto D = dyn_cast<NamespaceDecl>(*I)) {
// if (auto TSI = D->getTypeSourceInfo()) {
// processTypeLoc(TSI->getTypeLoc());
// }
// }
else if (auto D = dyn_cast<ObjCMethodDecl>(*I)) {
// if no type source info, it's a void f(void) function
auto TSI = D->getResultTypeSourceInfo();
Expand Down Expand Up @@ -282,6 +293,12 @@ void TypeRenameTransform::processDeclContext(DeclContext *DC, bool topLevel)
renameLocation(D->getLocation(), newName);
}
}
else if (auto D = dyn_cast<UsingDirectiveDecl>(*I)) {
std::string newName;
if (nameMatches(D->getNominatedNamespace(), newName, true)) {
renameLocation(D->getLocation(), newName);
}
}

// descend into the next level (namespace, etc.)
if (auto innerDC = dyn_cast<DeclContext>(*I)) {
Expand Down Expand Up @@ -413,7 +430,7 @@ void TypeRenameTransform::processFunctionDecl(FunctionDecl *D)
if (BL.isValid() && P->getLocation() != BL &&
nameMatches(P, newName, true)) {

// can't use renameLocation since this is a tricy case
// can't use renameLocation since this is a tricky case

// need to use raw_identifier because Lexer::findLocationAfterToken
// performs a raw lexing
Expand Down Expand Up @@ -639,7 +656,7 @@ void TypeRenameTransform::processTypeLoc(TypeLoc TL, bool forceRewriteMacro)
}


default:
default:
break;
}

Expand All @@ -654,12 +671,35 @@ void TypeRenameTransform::processQualifierLoc(NestedNameSpecifierLoc NNSL,
while (NNSL) {
auto NNS = NNSL.getNestedNameSpecifier();
auto NNSK = NNS->getKind();
if (NNSK == NestedNameSpecifier::TypeSpec || NNSK == NestedNameSpecifier::TypeSpecWithTemplate) {
if (NNSK == NestedNameSpecifier::TypeSpec ||
NNSK == NestedNameSpecifier::TypeSpecWithTemplate) {
processTypeLoc(NNSL.getTypeLoc(), forceRewriteMacro);
}
else if (NNSK == NestedNameSpecifier::Namespace) {
std::string newName;
if (nameMatches(NNS->getAsNamespace(), newName, true)) {
renameLocation(NNSL.getLocalBeginLoc(), newName);
}
processTypeLoc(NNSL.getTypeLoc(), forceRewriteMacro);
}

else if (NNSK == NestedNameSpecifier::NamespaceAlias) {
std::string newName;
if (nameMatches(NNS->getAsNamespaceAlias(), newName, true)) {
renameLocation(NNSL.getLocalBeginLoc(), newName);
}
processTypeLoc(NNSL.getTypeLoc(), forceRewriteMacro);
}
// else {
// auto srcrange = NNSL.getSourceRange();
// llvm::errs() << indent()
// << "==> ??? (" << ii << ") "
// << " range=[" << range(srcrange)
// << "\n";
// break;
// }

NNSL = NNSL.getPrefix();
}
}
}

void TypeRenameTransform::processParmVarDecl(ParmVarDecl *P)
Expand Down
53 changes: 53 additions & 0 deletions tests/NamespaceRename/foo.orig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "foo.h"

namespace SampleNameSpace {
int Foo::counter = 0;
};

void SampleNameSpace::Foo::doNothing() {
}

using namespace SampleNameSpace;

void Foo::wasteCycle() {
Foo a(this);
for (int b = 0; b < 10; b++) {
Foo c(this);
Foo d(this);
c.setX(b);
d = c;
}
}

Foo test() {
Foo a = Foo::Foo();
return Foo();
}

Foo test(Foo *a) {
return Foo(a);
}

::SampleNameSpace::Foo globalFoo;

#define TT Foo

int main() {
class Bar {
public:
Foo a;
void test() {
a.setX(10);
}
};

Foo a = test();
TT c = a;

Bar b;

b.test();

typedef Bar toto_t;
return a.getX();
}
38 changes: 38 additions & 0 deletions tests/NamespaceRename/foo.orig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

namespace SampleNameSpace {
class Foo;

struct FooNode {
Foo *next;
};

class Foo {
private:
static int counter;
int x;
Foo *next;
bool ownsNext;
public:
Foo() : x(0), next(0), ownsNext(false) {}
Foo(Foo *n) : x(0), next(n), ownsNext(false) {}
Foo(int px) : x(px), next(new Foo()), ownsNext(true) {}

~Foo() {
if (ownsNext) {
delete next;
}
}

int getX() const;
void setX(int newX) {
x = newX;
}

void wasteCycle();
void doNothing();
};

inline int Foo::getX() const {
return x;
}
};
28 changes: 28 additions & 0 deletions tests/NamespaceRename/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

set -e

/bin/cp foo.orig.h foo.h
/bin/cp foo.orig.cpp foo.cpp
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS:STRING=ON .

clang-refactor < test.yml

set +e
diff -urN foo.orig.h foo.h
sc=$?
if [[ "$sc" == "0" ]]; then
set -e
echo ":: ERR (no changes)"
exit 1
fi
diff -urN foo.orig.cpp foo.cpp
sc=$?
if [[ "$sc" == "0" ]]; then
set -e
echo ":: ERR (no changes)"
exit 1
fi

echo ":: OK"
## EOF ##
8 changes: 8 additions & 0 deletions tests/NamespaceRename/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
Transforms:
TypeRename:
Types:
- SampleNameSpace: Ns
- class SampleNameSpace::Foo: Foobar
- toto_t: bar_t