diff options
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/Makefile-clang.mk | 1 | ||||
-rw-r--r-- | compilerplugins/clang/plugin.cxx | 5 | ||||
-rw-r--r-- | compilerplugins/clang/removeforwardstringdecl.cxx | 76 | ||||
-rw-r--r-- | compilerplugins/clang/removeforwardstringdecl.hxx | 34 |
4 files changed, 116 insertions, 0 deletions
diff --git a/compilerplugins/Makefile-clang.mk b/compilerplugins/Makefile-clang.mk index 6031fe194f86..e652e44e22d4 100644 --- a/compilerplugins/Makefile-clang.mk +++ b/compilerplugins/Makefile-clang.mk @@ -14,6 +14,7 @@ CLANGSRC= \ bodynotinblock.cxx \ lclstaticfix.cxx \ postfixincrementfix.cxx \ + removeforwardstringdecl.cxx \ sallogareas.cxx \ unusedvariablecheck.cxx \ diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx index 22bac0cdfe8c..348386f7aae3 100644 --- a/compilerplugins/clang/plugin.cxx +++ b/compilerplugins/clang/plugin.cxx @@ -22,6 +22,7 @@ #include "bodynotinblock.hxx" #include "lclstaticfix.hxx" #include "postfixincrementfix.hxx" +#include "removeforwardstringdecl.hxx" #include "sallogareas.hxx" #include "unusedvariablecheck.hxx" @@ -192,6 +193,7 @@ class PluginHandler , bodyNotInBlock( context ) , lclStaticFix( context, rewriter ) , postfixIncrementFix( context, rewriter ) + , removeForwardStringDecl( context, rewriter ) , salLogAreas( context ) , unusedVariableCheck( context ) { @@ -204,6 +206,8 @@ class PluginHandler lclStaticFix.run(); else if( isArg( "postfixincrementfix" )) postfixIncrementFix.run(); + else if( isArg( "removeforwardstringdecl" )) + removeForwardStringDecl.run(); else if( args.empty()) { bodyNotInBlock.run(); @@ -292,6 +296,7 @@ class PluginHandler BodyNotInBlock bodyNotInBlock; LclStaticFix lclStaticFix; PostfixIncrementFix postfixIncrementFix; + RemoveForwardStringDecl removeForwardStringDecl; SalLogAreas salLogAreas; UnusedVariableCheck unusedVariableCheck; }; diff --git a/compilerplugins/clang/removeforwardstringdecl.cxx b/compilerplugins/clang/removeforwardstringdecl.cxx new file mode 100644 index 000000000000..45891d5d7fb3 --- /dev/null +++ b/compilerplugins/clang/removeforwardstringdecl.cxx @@ -0,0 +1,76 @@ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +#include "removeforwardstringdecl.hxx" + +#include <clang/AST/ASTContext.h> +#include <clang/Basic/SourceManager.h> + +/* +This is a rewriter. + +Remove all forward declarations of rtl strings. I.e. 'namespace rtl { class OUString; }' etc. +*/ + +namespace loplugin +{ + +RemoveForwardStringDecl::RemoveForwardStringDecl( ASTContext& context, Rewriter& rewriter ) + : RewritePlugin( context, rewriter ) + { + } + +void RemoveForwardStringDecl::run() + { + TraverseDecl( context.getTranslationUnitDecl()); + } + +bool RemoveForwardStringDecl::VisitNamespaceDecl( NamespaceDecl* declaration ) + { + if( ignoreLocation( declaration )) + return true; + if( declaration->getQualifiedNameAsString() != "rtl" ) + return true; + bool canRemove = true; + for( NamespaceDecl::decl_iterator it = declaration->decls_begin(); + it != declaration->decls_end(); + ++it ) + { + if( *it != NULL ) + { + if( !tryRemoveStringForwardDecl( *it )) + canRemove = false; + } + } + if( canRemove ) // contained only forward decls that we removed + removeText( declaration->getSourceRange(), RemoveLineIfEmpty ); + return true; + } + +bool RemoveForwardStringDecl::tryRemoveStringForwardDecl( const Decl* decl ) + { + const CXXRecordDecl* classdecl = dyn_cast< CXXRecordDecl >( decl ); + if( classdecl == NULL ) + return false; + if( !classdecl->isFreeStanding() || classdecl->isCompleteDefinition()) + return false; // not a simple forward declaration + if( classdecl->getName() == "OString" || classdecl->getName() == "OUString" + || classdecl->getName() == "OStringBuffer" || classdecl->getName() == "OUStringBuffer" + || classdecl->getName() == "OStringHash" || classdecl->getName() == "OUStringHash" + || classdecl->getName() == "OStringLiteral" || classdecl->getName() == "OUStringLiteral" ) + { + removeText( SourceRange( classdecl->getOuterLocStart(), classdecl->getLocEnd()), + RemoveLineIfEmpty | RemoveWholeStatement ); + return true; + } + return false; + } + +} // namespace diff --git a/compilerplugins/clang/removeforwardstringdecl.hxx b/compilerplugins/clang/removeforwardstringdecl.hxx new file mode 100644 index 000000000000..7f40e371817d --- /dev/null +++ b/compilerplugins/clang/removeforwardstringdecl.hxx @@ -0,0 +1,34 @@ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +#ifndef REMOVEFORWARDSTRINGDECL_H +#define REMOVEFORWARDSTRINGDECL_H + +#include "plugin.hxx" + +namespace loplugin +{ + +class RemoveForwardStringDecl + : public RecursiveASTVisitor< RemoveForwardStringDecl > + , public RewritePlugin + { + public: + explicit RemoveForwardStringDecl( ASTContext& context, Rewriter& rewriter ); + void run(); + bool VisitNamespaceDecl( NamespaceDecl* declaration ); + private: + bool tryRemoveStringForwardDecl( const Decl* decl ); + }; + +} // namespace + +#endif // REMOVEFORWARDSTRINGDECL_H + |