libreoffice/compilerplugins/clang/removeforwardstringdecl.cxx
Luboš Luňák 153a69cad2 pass around CompilerInstance rather than ASTContext
It's possible to get the latter from the former, and the former
is useful for other things too (access to the preprocessor, for example).

Change-Id: I708d709129fd3a35bf7c63da4de09c2e696b382d
2013-03-28 18:01:00 +01:00

76 lines
2.3 KiB
C++

/*
* 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"
/*
This is a rewriter.
Remove all forward declarations of rtl strings. I.e. 'namespace rtl { class OUString; }' etc.
*/
namespace loplugin
{
RemoveForwardStringDecl::RemoveForwardStringDecl( CompilerInstance& compiler, Rewriter& rewriter )
: RewritePlugin( compiler, rewriter )
{
}
void RemoveForwardStringDecl::run()
{
TraverseDecl( compiler.getASTContext().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;
}
static Plugin::Registration< RemoveForwardStringDecl > X( "removeforwardstringdecl" );
} // namespace