[Libreoffice-commits] dev-tools.git: clang/README clang/rename.cxx clang/testclass.cxx
Miklos Vajna
vmiklos at collabora.co.uk
Mon Jan 25 00:13:34 PST 2016
clang/README | 13 +++-
clang/rename.cxx | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++--
clang/testclass.cxx | 42 +++++++++++++++
3 files changed, 185 insertions(+), 8 deletions(-)
New commits:
commit 09aa5939e4fffbe574e7c01d5f13f37627045cce
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date: Mon Jan 25 09:13:08 2016 +0100
clang: handle rename of classes
diff --git a/clang/README b/clang/README
index 948e223..7676418 100644
--- a/clang/README
+++ b/clang/README
@@ -4,7 +4,7 @@
This tool is similar to clang-rename, though there are a number of differences.
-Pros:
+Pros wrt. data members:
- old name can be a simple qualified name, no need to manually specify a byte
offset
@@ -12,10 +12,15 @@ Pros:
- handles macros, even nested ones
- comes with a wrapper to do fully automatic rewriting
-Cons:
+Details:
-- handles only rename of data members and member functions so far
-- only tested with clang 3.5/3.7
+- handles rename of:
+ - data members
+ - member functions
+ - classes
+- tested with clang
+ - 3.5
+ - 3.7
== Hello world
diff --git a/clang/rename.cxx b/clang/rename.cxx
index 902c6d5..8ce4c66 100644
--- a/clang/rename.cxx
+++ b/clang/rename.cxx
@@ -76,10 +76,33 @@ public:
*/
bool VisitVarDecl(clang::VarDecl* pDecl)
{
- std::string aName = pDecl->getQualifiedNameAsString();
- const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
- if (it != mrRewriter.getNameMap().end())
- mrRewriter.ReplaceText(pDecl->getLocation(), pDecl->getNameAsString().length(), it->second);
+ {
+ std::string aName = pDecl->getQualifiedNameAsString();
+ const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
+ if (it != mrRewriter.getNameMap().end())
+ mrRewriter.ReplaceText(pDecl->getLocation(), pDecl->getNameAsString().length(), it->second);
+ }
+
+ /*
+ * C* pC = 0;
+ * ^ Handles this.
+ */
+ clang::QualType pType = pDecl->getType();
+ const clang::RecordDecl* pRecordDecl = pType->getPointeeCXXRecordDecl();
+ if (pRecordDecl)
+ {
+ std::string aName = pRecordDecl->getNameAsString();
+ const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
+ if (it != mrRewriter.getNameMap().end())
+ {
+ clang::SourceLocation aLocation = pDecl->getTypeSpecStartLoc();
+ if (maHandledLocations.find(aLocation) == maHandledLocations.end())
+ {
+ mrRewriter.ReplaceText(aLocation, pRecordDecl->getNameAsString().length(), it->second);
+ maHandledLocations.insert(aLocation);
+ }
+ }
+ }
return true;
}
@@ -107,6 +130,38 @@ public:
mrRewriter.ReplaceText(pInitializer->getSourceLocation(), pFieldDecl->getNameAsString().length(), it->second);
}
}
+
+ std::string aName = pDecl->getNameAsString();
+ const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
+ if (it != mrRewriter.getNameMap().end())
+ {
+ /*
+ * Foo::Foo(...) {}
+ * ^~~ Handles this.
+ */
+ {
+ clang::SourceLocation aLocation = pDecl->getLocStart();
+ if (maHandledLocations.find(aLocation) == maHandledLocations.end())
+ {
+ mrRewriter.ReplaceText(aLocation, pDecl->getNameAsString().length(), it->second);
+ maHandledLocations.insert(aLocation);
+ }
+ }
+
+ /*
+ * Foo::Foo(...) {}
+ * ^~~ Handles this.
+ */
+ {
+ clang::SourceLocation aLocation = pDecl->getLocation();
+ if (maHandledLocations.find(aLocation) == maHandledLocations.end())
+ {
+ mrRewriter.ReplaceText(aLocation, pDecl->getNameAsString().length(), it->second);
+ maHandledLocations.insert(aLocation);
+ }
+ }
+ }
+
return true;
}
@@ -207,6 +262,81 @@ public:
}
return true;
}
+
+ // Class names.
+
+ /*
+ * class C <- Handles this.
+ * {
+ * };
+ */
+ bool VisitCXXRecordDecl(const clang::CXXRecordDecl* pDecl)
+ {
+ std::string aName = pDecl->getQualifiedNameAsString();
+ const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
+ if (it != mrRewriter.getNameMap().end())
+ {
+ clang::SourceLocation aLocation = pDecl->getLocation();
+ if (maHandledLocations.find(aLocation) == maHandledLocations.end())
+ {
+ mrRewriter.ReplaceText(aLocation, pDecl->getNameAsString().length(), it->second);
+ maHandledLocations.insert(aLocation);
+ }
+ }
+ return true;
+ }
+
+ /*
+ * ... new C(...); <- Handles this.
+ */
+ bool VisitCXXConstructExpr(const clang::CXXConstructExpr* pExpr)
+ {
+ if (const clang::CXXConstructorDecl* pDecl = pExpr->getConstructor())
+ {
+ std::string aName = pDecl->getNameAsString();
+ const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
+ if (it != mrRewriter.getNameMap().end())
+ {
+ clang::SourceLocation aLocation = pExpr->getLocation();
+ if (maHandledLocations.find(aLocation) == maHandledLocations.end())
+ {
+ mrRewriter.ReplaceText(aLocation, pDecl->getNameAsString().length(), it->second);
+ maHandledLocations.insert(aLocation);
+ }
+ }
+ }
+ return true;
+ }
+
+ /*
+ * ... static_cast<const C*>(...) ...;
+ * ^ Handles this...
+ *
+ * ... static_cast<const C&>(...) ...;
+ * ^ ... and this.
+ */
+ bool VisitCXXStaticCastExpr(clang::CXXStaticCastExpr* pExpr)
+ {
+ clang::QualType pType = pExpr->getType();
+ const clang::RecordDecl* pDecl = pType->getPointeeCXXRecordDecl();
+ if (!pDecl)
+ pDecl = pType->getAsCXXRecordDecl();
+ if (pDecl)
+ {
+ std::string aName = pDecl->getNameAsString();
+ const std::map<std::string, std::string>::const_iterator it = mrRewriter.getNameMap().find(aName);
+ if (it != mrRewriter.getNameMap().end())
+ {
+ clang::SourceLocation aLocation = pExpr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
+ if (maHandledLocations.find(aLocation) == maHandledLocations.end())
+ {
+ mrRewriter.ReplaceText(aLocation, pDecl->getNameAsString().length(), it->second);
+ maHandledLocations.insert(aLocation);
+ }
+ }
+ }
+ return true;
+ }
};
class RenameASTConsumer : public clang::ASTConsumer
diff --git a/clang/testclass.cxx b/clang/testclass.cxx
new file mode 100644
index 0000000..c95b0ae
--- /dev/null
+++ b/clang/testclass.cxx
@@ -0,0 +1,42 @@
+class S
+{
+};
+
+class B
+{
+public:
+ B(int, int)
+ {
+ };
+
+ B(int, S&)
+ {
+ }
+};
+
+class SdrEdgeLineDeltaAnzItem: public B {
+public:
+ SdrEdgeLineDeltaAnzItem(int nVal=0): B(0,nVal) {}
+ SdrEdgeLineDeltaAnzItem(S& rIn);
+ virtual B* Clone() const
+ {
+ return new SdrEdgeLineDeltaAnzItem(*this);
+ }
+
+ int GetValue() const
+ {
+ return 0;
+ }
+};
+
+SdrEdgeLineDeltaAnzItem::SdrEdgeLineDeltaAnzItem(S& rIn): B(0,rIn) {}
+
+int main()
+{
+ SdrEdgeLineDeltaAnzItem* pItem = 0;
+ static_cast<const SdrEdgeLineDeltaAnzItem&>(*pItem).GetValue();
+ int nValue = static_cast<const SdrEdgeLineDeltaAnzItem*>( 0 )->GetValue();
+ return nValue;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
More information about the Libreoffice-commits
mailing list