2013-09-21 14:42:35 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-10-05 18:17:13 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-15 15:36:25 +02:00
|
|
|
#include "plugin.hxx"
|
2012-10-05 18:17:13 +02:00
|
|
|
|
2012-10-15 15:36:25 +02:00
|
|
|
#include <clang/Basic/FileManager.h>
|
2013-08-14 16:39:11 +02:00
|
|
|
#include <clang/Lex/Lexer.h>
|
2012-10-05 18:17:13 +02:00
|
|
|
|
2013-02-02 16:35:47 +01:00
|
|
|
#include "pluginhandler.hxx"
|
2012-10-09 14:50:19 +02:00
|
|
|
|
2013-02-02 18:34:12 +01:00
|
|
|
/*
|
|
|
|
Base classes for plugin actions.
|
|
|
|
*/
|
2012-10-05 18:17:13 +02:00
|
|
|
namespace loplugin
|
|
|
|
{
|
|
|
|
|
2013-03-21 16:42:10 +01:00
|
|
|
Plugin::Plugin( CompilerInstance& compiler )
|
|
|
|
: compiler( compiler )
|
2012-10-09 16:27:25 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
|
2013-02-02 19:31:24 +01:00
|
|
|
{
|
2013-03-21 16:42:10 +01:00
|
|
|
return report( level, message, compiler, loc );
|
2013-02-02 19:31:24 +01:00
|
|
|
}
|
|
|
|
|
2013-03-21 16:42:10 +01:00
|
|
|
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, CompilerInstance& compiler,
|
2013-02-02 19:31:24 +01:00
|
|
|
SourceLocation loc )
|
2012-10-09 16:27:25 +02:00
|
|
|
{
|
2013-03-21 16:42:10 +01:00
|
|
|
DiagnosticsEngine& diag = compiler.getDiagnostics();
|
2012-10-09 16:28:13 +02:00
|
|
|
// Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
|
2012-10-09 16:27:25 +02:00
|
|
|
if( level == DiagnosticsEngine::Warning && diag.getWarningsAsErrors())
|
|
|
|
level = DiagnosticsEngine::Error;
|
|
|
|
if( level == DiagnosticsEngine::Error && diag.getErrorsAsFatal())
|
|
|
|
level = DiagnosticsEngine::Fatal;
|
2013-02-02 19:38:56 +01:00
|
|
|
string fullMessage = ( message + " [loplugin]" ).str();
|
2012-10-13 17:38:58 +02:00
|
|
|
if( loc.isValid())
|
2013-02-02 19:38:56 +01:00
|
|
|
return diag.Report( loc, diag.getCustomDiagID( level, fullMessage ));
|
2012-10-13 17:38:58 +02:00
|
|
|
else
|
2013-02-02 19:38:56 +01:00
|
|
|
return diag.Report( diag.getCustomDiagID( level, fullMessage ));
|
2012-10-09 16:27:25 +02:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:39:49 +02:00
|
|
|
bool Plugin::ignoreLocation( SourceLocation loc )
|
|
|
|
{
|
2013-03-21 16:42:10 +01:00
|
|
|
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
|
|
|
|
if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))
|
2013-01-03 20:15:21 +01:00
|
|
|
return true;
|
2013-03-21 16:42:10 +01:00
|
|
|
const char* bufferName = compiler.getSourceManager().getPresumedLoc( expansionLoc ).getFilename();
|
2014-01-10 11:07:55 +01:00
|
|
|
if( bufferName == NULL
|
|
|
|
|| strncmp( bufferName, WORKDIR, strlen( WORKDIR )) == 0 )
|
2013-01-03 20:15:21 +01:00
|
|
|
return true;
|
2014-01-10 11:07:55 +01:00
|
|
|
if( strncmp( bufferName, BUILDDIR, strlen( BUILDDIR )) == 0
|
2013-05-06 15:14:35 +02:00
|
|
|
|| strncmp( bufferName, SRCDIR, strlen( SRCDIR )) == 0 )
|
|
|
|
return false; // ok
|
|
|
|
return true;
|
2012-10-09 16:39:49 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 17:57:45 +02:00
|
|
|
void Plugin::registerPlugin( Plugin* (*create)( CompilerInstance&, Rewriter& ), const char* optionName, bool isRewriter, bool isPPCallback )
|
2013-02-02 17:45:18 +01:00
|
|
|
{
|
2013-08-06 17:57:45 +02:00
|
|
|
PluginHandler::registerPlugin( create, optionName, isRewriter, isPPCallback );
|
2013-02-02 17:45:18 +01:00
|
|
|
}
|
|
|
|
|
2013-06-19 23:55:47 +02:00
|
|
|
unordered_map< const Stmt*, const Stmt* > Plugin::parents;
|
|
|
|
|
|
|
|
const Stmt* Plugin::parentStmt( const Stmt* stmt )
|
|
|
|
{
|
|
|
|
if( parents.empty())
|
|
|
|
buildParents( compiler );
|
|
|
|
assert( parents.count( stmt ) == 1 );
|
|
|
|
return parents[ stmt ];
|
|
|
|
}
|
|
|
|
|
|
|
|
Stmt* Plugin::parentStmt( Stmt* stmt )
|
|
|
|
{
|
|
|
|
if( parents.empty())
|
|
|
|
buildParents( compiler );
|
|
|
|
assert( parents.count( stmt ) == 1 );
|
|
|
|
return const_cast< Stmt* >( parents[ stmt ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class ParentBuilder
|
|
|
|
: public RecursiveASTVisitor< ParentBuilder >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool VisitFunctionDecl( const FunctionDecl* function );
|
|
|
|
void walk( const Stmt* stmt );
|
|
|
|
unordered_map< const Stmt*, const Stmt* >* parents;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ParentBuilder::VisitFunctionDecl( const FunctionDecl* function )
|
|
|
|
{
|
|
|
|
// if( ignoreLocation( declaration ))
|
|
|
|
// return true; ???
|
2013-07-15 19:55:41 +02:00
|
|
|
if( function->doesThisDeclarationHaveABody())
|
|
|
|
{
|
|
|
|
const Stmt* body = function->getBody();
|
|
|
|
(*parents)[ body ] = NULL; // no parent
|
|
|
|
walk( body );
|
|
|
|
}
|
|
|
|
if( const CXXConstructorDecl* ctor = dyn_cast< CXXConstructorDecl >( function ))
|
|
|
|
{
|
|
|
|
for( CXXConstructorDecl::init_const_iterator it = ctor->init_begin();
|
|
|
|
it != ctor->init_end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
const Expr* init_expression = (*it)->getInit();
|
|
|
|
(*parents)[ init_expression ] = NULL;
|
|
|
|
walk( init_expression );
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 23:55:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParentBuilder::walk( const Stmt* stmt )
|
|
|
|
{
|
|
|
|
for( ConstStmtIterator it = stmt->child_begin();
|
|
|
|
it != stmt->child_end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
if( *it != NULL )
|
|
|
|
{
|
|
|
|
(*parents)[ *it ] = stmt;
|
|
|
|
walk( *it );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Plugin::buildParents( CompilerInstance& compiler )
|
|
|
|
{
|
|
|
|
assert( parents.empty());
|
|
|
|
ParentBuilder builder;
|
|
|
|
builder.parents = &parents;
|
|
|
|
builder.TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
|
2013-08-14 18:43:01 +02:00
|
|
|
SourceLocation Plugin::locationAfterToken( SourceLocation location )
|
|
|
|
{
|
|
|
|
return Lexer::getLocForEndOfToken( location, 0, compiler.getSourceManager(), compiler.getLangOpts());
|
|
|
|
}
|
|
|
|
|
2013-02-02 17:45:18 +01:00
|
|
|
/////
|
2012-10-15 17:34:13 +02:00
|
|
|
|
2013-03-21 16:42:10 +01:00
|
|
|
RewritePlugin::RewritePlugin( CompilerInstance& compiler, Rewriter& rewriter )
|
|
|
|
: Plugin( compiler )
|
2012-10-15 17:34:13 +02:00
|
|
|
, rewriter( rewriter )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAfter, bool indentNewLines )
|
|
|
|
{
|
|
|
|
if( rewriter.InsertText( Loc, Str, InsertAfter, indentNewLines ))
|
|
|
|
return reportEditFailure( Loc );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str )
|
|
|
|
{
|
|
|
|
if( rewriter.InsertTextAfter( Loc, Str ))
|
|
|
|
return reportEditFailure( Loc );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str )
|
|
|
|
{
|
|
|
|
if( rewriter.InsertTextAfterToken( Loc, Str ))
|
|
|
|
return reportEditFailure( Loc );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str )
|
|
|
|
{
|
|
|
|
if( rewriter.InsertTextBefore( Loc, Str ))
|
|
|
|
return reportEditFailure( Loc );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::removeText( SourceLocation Start, unsigned Length, RewriteOptions opts )
|
|
|
|
{
|
2013-08-14 16:39:11 +02:00
|
|
|
CharSourceRange range( SourceRange( Start, Start.getLocWithOffset( Length )), false );
|
|
|
|
return removeText( range, opts );
|
2012-10-15 17:34:13 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:08:52 +01:00
|
|
|
bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts )
|
2013-08-14 16:39:11 +02:00
|
|
|
{
|
|
|
|
return removeText( CharSourceRange( range, true ), opts );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::removeText( CharSourceRange range, RewriteOptions opts )
|
2012-10-15 17:34:13 +02:00
|
|
|
{
|
2013-08-20 19:42:16 +02:00
|
|
|
if( rewriter.getRangeSize( range, opts ) == -1 )
|
|
|
|
return reportEditFailure( range.getBegin());
|
2013-05-06 16:41:27 +02:00
|
|
|
if( removals.find( range.getBegin()) != removals.end())
|
|
|
|
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", range.getBegin());
|
|
|
|
removals.insert( range.getBegin());
|
2013-08-14 16:39:11 +02:00
|
|
|
if( opts.flags & RemoveWholeStatement || opts.flags & RemoveAllWhitespace )
|
2012-12-20 23:08:52 +01:00
|
|
|
{
|
2013-08-14 16:39:11 +02:00
|
|
|
if( !adjustRangeForOptions( &range, opts ))
|
2012-12-20 23:08:52 +01:00
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
}
|
2012-10-15 17:34:13 +02:00
|
|
|
if( rewriter.RemoveText( range, opts ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-14 16:39:11 +02:00
|
|
|
bool RewritePlugin::adjustRangeForOptions( CharSourceRange* range, RewriteOptions opts )
|
2012-10-15 17:34:13 +02:00
|
|
|
{
|
2012-12-20 23:08:52 +01:00
|
|
|
SourceManager& SM = rewriter.getSourceMgr();
|
|
|
|
SourceLocation fileStartLoc = SM.getLocForStartOfFile( SM.getFileID( range->getBegin()));
|
|
|
|
if( fileStartLoc.isInvalid())
|
|
|
|
return false;
|
|
|
|
bool invalid = false;
|
|
|
|
const char* fileBuf = SM.getCharacterData( fileStartLoc, &invalid );
|
|
|
|
if( invalid )
|
|
|
|
return false;
|
|
|
|
const char* startBuf = SM.getCharacterData( range->getBegin(), &invalid );
|
|
|
|
if( invalid )
|
|
|
|
return false;
|
2013-08-14 16:39:11 +02:00
|
|
|
SourceLocation locationEnd = range->getEnd();
|
|
|
|
if( range->isTokenRange())
|
2013-08-14 18:43:01 +02:00
|
|
|
locationEnd = locationAfterToken( locationEnd );
|
2013-08-14 16:39:11 +02:00
|
|
|
const char* endBuf = SM.getCharacterData( locationEnd, &invalid );
|
2012-12-20 23:08:52 +01:00
|
|
|
if( invalid )
|
|
|
|
return false;
|
2013-08-14 16:39:11 +02:00
|
|
|
const char* startPos = startBuf;
|
|
|
|
--startPos;
|
|
|
|
while( startPos >= fileBuf && ( *startPos == ' ' || *startPos == '\t' ))
|
|
|
|
--startPos;
|
|
|
|
if( startPos >= fileBuf && *startPos == '\n' )
|
|
|
|
startPos = startBuf - 1; // do not remove indentation whitespace (RemoveLineIfEmpty can do that)
|
|
|
|
const char* endPos = endBuf;
|
|
|
|
while( *endPos == ' ' || *endPos == '\t' )
|
|
|
|
++endPos;
|
|
|
|
if( opts.flags & RemoveWholeStatement )
|
|
|
|
{
|
|
|
|
if( *endPos == ';' )
|
|
|
|
++endPos;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*range = CharSourceRange( SourceRange( range->getBegin().getLocWithOffset( startPos - startBuf + 1 ),
|
|
|
|
locationEnd.getLocWithOffset( endPos - endBuf )), false );
|
2012-10-15 17:34:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr )
|
|
|
|
{
|
2013-05-06 16:41:27 +02:00
|
|
|
if( OrigLength != 0 && removals.find( Start ) != removals.end())
|
|
|
|
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", Start );
|
|
|
|
removals.insert( Start );
|
2012-10-15 17:34:13 +02:00
|
|
|
if( rewriter.ReplaceText( Start, OrigLength, NewStr ))
|
|
|
|
return reportEditFailure( Start );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
|
|
|
|
{
|
2013-08-20 19:42:16 +02:00
|
|
|
if( rewriter.getRangeSize( range ) == -1 )
|
|
|
|
return reportEditFailure( range.getBegin());
|
2013-05-06 16:41:27 +02:00
|
|
|
if( removals.find( range.getBegin()) != removals.end())
|
|
|
|
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin());
|
|
|
|
removals.insert( range.getBegin());
|
2012-10-15 17:34:13 +02:00
|
|
|
if( rewriter.ReplaceText( range, NewStr ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange )
|
|
|
|
{
|
2013-08-20 19:42:16 +02:00
|
|
|
if( rewriter.getRangeSize( range ) == -1 )
|
|
|
|
return reportEditFailure( range.getBegin());
|
2013-05-06 16:41:27 +02:00
|
|
|
if( removals.find( range.getBegin()) != removals.end())
|
|
|
|
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin());
|
|
|
|
removals.insert( range.getBegin());
|
2012-10-15 17:34:13 +02:00
|
|
|
if( rewriter.ReplaceText( range, replacementRange ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::reportEditFailure( SourceLocation loc )
|
|
|
|
{
|
2013-02-02 19:38:56 +01:00
|
|
|
report( DiagnosticsEngine::Warning, "cannot perform source modification (macro expansion involved?)", loc );
|
2012-10-15 17:34:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-05 18:17:13 +02:00
|
|
|
} // namespace
|
2013-09-21 14:42:35 +01:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|