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
|
|
|
|
|
|
|
#include <clang/AST/ASTConsumer.h>
|
|
|
|
#include <clang/AST/ASTContext.h>
|
2012-10-15 15:36:25 +02:00
|
|
|
#include <clang/Basic/FileManager.h>
|
2012-10-05 18:17:13 +02:00
|
|
|
#include <clang/Frontend/CompilerInstance.h>
|
|
|
|
#include <clang/Frontend/FrontendAction.h>
|
|
|
|
#include <clang/Frontend/FrontendPluginRegistry.h>
|
2012-12-23 23:02:39 +01:00
|
|
|
#include <stdio.h>
|
2012-12-26 15:42:51 +01:00
|
|
|
#include <unistd.h>
|
2012-10-05 18:17:13 +02:00
|
|
|
|
2012-10-09 16:21:33 +02:00
|
|
|
#include "bodynotinblock.hxx"
|
2012-10-15 15:36:25 +02:00
|
|
|
#include "lclstaticfix.hxx"
|
2012-10-15 23:29:36 +02:00
|
|
|
#include "postfixincrementfix.hxx"
|
2013-01-03 20:23:51 +01:00
|
|
|
#include "removeforwardstringdecl.hxx"
|
2012-10-13 17:38:58 +02:00
|
|
|
#include "sallogareas.hxx"
|
2012-10-09 14:50:19 +02:00
|
|
|
#include "unusedvariablecheck.hxx"
|
|
|
|
|
2012-10-05 18:17:13 +02:00
|
|
|
namespace loplugin
|
|
|
|
{
|
|
|
|
|
2012-10-09 16:27:25 +02:00
|
|
|
Plugin::Plugin( ASTContext& context )
|
|
|
|
: context( context )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
|
|
|
|
{
|
|
|
|
DiagnosticsEngine& diag = context.getDiagnostics();
|
2012-10-09 16:28:13 +02:00
|
|
|
#if 0
|
|
|
|
// 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;
|
2012-10-09 16:28:13 +02:00
|
|
|
#endif
|
2012-10-13 17:38:58 +02:00
|
|
|
if( loc.isValid())
|
|
|
|
return diag.Report( loc, diag.getCustomDiagID( level, message ));
|
|
|
|
else
|
|
|
|
return diag.Report( diag.getCustomDiagID( level, message ));
|
2012-10-09 16:27:25 +02:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:39:49 +02:00
|
|
|
bool Plugin::ignoreLocation( SourceLocation loc )
|
|
|
|
{
|
2013-01-03 20:15:21 +01:00
|
|
|
SourceLocation expansionLoc = context.getSourceManager().getExpansionLoc( loc );
|
|
|
|
if( context.getSourceManager().isInSystemHeader( expansionLoc ))
|
|
|
|
return true;
|
|
|
|
bool invalid;
|
|
|
|
const char* bufferName = context.getSourceManager().getBufferName( expansionLoc, &invalid );
|
|
|
|
if( invalid )
|
|
|
|
return true;
|
|
|
|
if( strncmp( bufferName, OUTDIR, strlen( OUTDIR )) == 0
|
|
|
|
|| strncmp( bufferName, WORKDIR, strlen( WORKDIR )) == 0
|
|
|
|
|| strncmp( bufferName, BUILDDIR, strlen( BUILDDIR )) == 0
|
|
|
|
|| strncmp( bufferName, SRCDIR, strlen( SRCDIR )) == 0 )
|
|
|
|
return false; // ok
|
|
|
|
return true;
|
2012-10-09 16:39:49 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 17:34:13 +02:00
|
|
|
|
|
|
|
RewritePlugin::RewritePlugin( ASTContext& context, Rewriter& rewriter )
|
|
|
|
: Plugin( context )
|
|
|
|
, 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 )
|
|
|
|
{
|
2012-12-20 23:08:52 +01:00
|
|
|
return removeText( SourceRange( Start, Start.getLocWithOffset( Length )), opts );
|
2012-10-15 17:34:13 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:08:52 +01:00
|
|
|
bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts )
|
2012-10-15 17:34:13 +02:00
|
|
|
{
|
2012-12-20 23:08:52 +01:00
|
|
|
if( opts.RemoveWholeStatement )
|
|
|
|
{
|
|
|
|
if( !adjustForWholeStatement( &range ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
}
|
2012-10-15 17:34:13 +02:00
|
|
|
if( rewriter.RemoveText( range, opts ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-20 23:08:52 +01:00
|
|
|
bool RewritePlugin::adjustForWholeStatement( SourceRange* range )
|
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;
|
|
|
|
const char* endBuf = SM.getCharacterData( range->getEnd(), &invalid );
|
|
|
|
if( invalid )
|
|
|
|
return false;
|
|
|
|
const char* startSpacePos = startBuf;
|
|
|
|
// do not skip \n here, RemoveLineIfEmpty can take care of that
|
|
|
|
--startSpacePos;
|
|
|
|
while( startSpacePos >= fileBuf && ( *startSpacePos == ' ' || *startSpacePos == '\t' ))
|
|
|
|
--startSpacePos;
|
|
|
|
const char* semiPos = strchr( endBuf, ';' );
|
|
|
|
if( semiPos == NULL )
|
|
|
|
return false;
|
|
|
|
*range = SourceRange( range->getBegin().getLocWithOffset( startSpacePos - startBuf + 1 ),
|
|
|
|
range->getEnd().getLocWithOffset( semiPos - endBuf + 1 ));
|
2012-10-15 17:34:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr )
|
|
|
|
{
|
|
|
|
if( rewriter.ReplaceText( Start, OrigLength, NewStr ))
|
|
|
|
return reportEditFailure( Start );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
|
|
|
|
{
|
|
|
|
if( rewriter.ReplaceText( range, NewStr ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange )
|
|
|
|
{
|
|
|
|
if( rewriter.ReplaceText( range, replacementRange ))
|
|
|
|
return reportEditFailure( range.getBegin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewritePlugin::reportEditFailure( SourceLocation loc )
|
|
|
|
{
|
|
|
|
DiagnosticsEngine& diag = context.getDiagnostics();
|
|
|
|
diag.Report( loc, diag.getCustomDiagID( DiagnosticsEngine::Warning,
|
|
|
|
"cannot perform source modification (macro expansion involved?) [loplugin]" ));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-05 18:17:13 +02:00
|
|
|
/**
|
|
|
|
Class that manages all LO modules.
|
|
|
|
*/
|
|
|
|
class PluginHandler
|
|
|
|
: public ASTConsumer
|
|
|
|
{
|
|
|
|
public:
|
2012-10-15 15:36:25 +02:00
|
|
|
explicit PluginHandler( ASTContext& context, const vector< string >& args )
|
2012-10-05 18:17:13 +02:00
|
|
|
: rewriter( context.getSourceManager(), context.getLangOpts())
|
2012-10-15 15:36:25 +02:00
|
|
|
, args( args )
|
2012-10-09 16:21:33 +02:00
|
|
|
, bodyNotInBlock( context )
|
2012-10-15 15:36:25 +02:00
|
|
|
, lclStaticFix( context, rewriter )
|
2012-10-15 23:29:36 +02:00
|
|
|
, postfixIncrementFix( context, rewriter )
|
2013-01-03 20:23:51 +01:00
|
|
|
, removeForwardStringDecl( context, rewriter )
|
2012-10-13 17:38:58 +02:00
|
|
|
, salLogAreas( context )
|
2012-10-09 14:50:19 +02:00
|
|
|
, unusedVariableCheck( context )
|
2012-10-05 18:17:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void HandleTranslationUnit( ASTContext& context )
|
|
|
|
{
|
|
|
|
if( context.getDiagnostics().hasErrorOccurred())
|
|
|
|
return;
|
2012-10-15 15:36:25 +02:00
|
|
|
if( isArg( "lclstaticfix" ))
|
|
|
|
lclStaticFix.run();
|
2012-10-15 23:29:36 +02:00
|
|
|
else if( isArg( "postfixincrementfix" ))
|
|
|
|
postfixIncrementFix.run();
|
2013-01-03 20:23:51 +01:00
|
|
|
else if( isArg( "removeforwardstringdecl" ))
|
|
|
|
removeForwardStringDecl.run();
|
2012-10-15 15:36:25 +02:00
|
|
|
else if( args.empty())
|
|
|
|
{
|
|
|
|
bodyNotInBlock.run();
|
|
|
|
salLogAreas.run();
|
|
|
|
unusedVariableCheck.run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DiagnosticsEngine& diag = context.getDiagnostics();
|
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Fatal,
|
|
|
|
"unknown plugin tool %0 [loplugin]" )) << args.front();
|
|
|
|
}
|
|
|
|
for( Rewriter::buffer_iterator it = rewriter.buffer_begin();
|
|
|
|
it != rewriter.buffer_end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
const FileEntry* e = context.getSourceManager().getFileEntryForID( it->first );
|
2013-01-03 20:15:21 +01:00
|
|
|
DiagnosticsEngine& diag = context.getDiagnostics();
|
|
|
|
/* Check where the file actually is, and warn about cases where modification
|
|
|
|
most probably doesn't matter (generated files in workdir).
|
|
|
|
The order here is important, as OUTDIR and WORKDIR are often in SRCDIR/BUILDDIR,
|
|
|
|
and BUILDDIR is sometimes in SRCDIR. */
|
|
|
|
string modifyFile;
|
|
|
|
if( strncmp( e->getName(), OUTDIR, strlen( OUTDIR )) == 0 )
|
|
|
|
{
|
|
|
|
/* Try to find a matching file for a file in solver/ (include files
|
|
|
|
are usually included from there rather than from the source dir) if possible. */
|
|
|
|
if( strncmp( e->getName(), OUTDIR "/inc/", strlen( OUTDIR ) + strlen( "/inc/" )) == 0 )
|
|
|
|
{
|
|
|
|
string filename( e->getName());
|
|
|
|
int modulePos = strlen( OUTDIR ) + strlen( "/inc/" );
|
2013-01-05 22:19:10 +01:00
|
|
|
size_t moduleEnd = filename.find( '/', modulePos );
|
2013-01-03 20:15:21 +01:00
|
|
|
if( moduleEnd != string::npos )
|
|
|
|
{
|
|
|
|
modifyFile = SRCDIR "/" + filename.substr( modulePos, moduleEnd - modulePos )
|
|
|
|
+ "/inc/" + filename.substr( modulePos );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( modifyFile.empty())
|
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Warning,
|
|
|
|
"modified source in solver/ : %0 [loplugin]" )) << e->getName();
|
|
|
|
}
|
|
|
|
else if( strncmp( e->getName(), WORKDIR, strlen( WORKDIR )) == 0 )
|
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Warning,
|
|
|
|
"modified source in workdir/ : %0 [loplugin]" )) << e->getName();
|
2013-01-03 22:57:46 +01:00
|
|
|
else if( strcmp( SRCDIR, BUILDDIR ) != 0 && strncmp( e->getName(), BUILDDIR, strlen( BUILDDIR )) == 0 )
|
2013-01-03 20:15:21 +01:00
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Warning,
|
|
|
|
"modified source in build dir : %0 [loplugin]" )) << e->getName();
|
|
|
|
else if( strncmp( e->getName(), SRCDIR, strlen( SRCDIR )) == 0 )
|
|
|
|
; // ok
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Warning,
|
|
|
|
"modified source in unknown location, not modifying : %0 [loplugin]" )) << e->getName();
|
|
|
|
continue; // --->
|
|
|
|
}
|
|
|
|
if( modifyFile.empty())
|
|
|
|
modifyFile = e->getName();
|
|
|
|
char* filename = new char[ modifyFile.length() + 100 ];
|
|
|
|
sprintf( filename, "%s.new.%d", modifyFile.c_str(), getpid());
|
2012-10-15 15:36:25 +02:00
|
|
|
string error;
|
2012-12-23 23:02:39 +01:00
|
|
|
bool ok = false;
|
|
|
|
raw_fd_ostream ostream( filename, error );
|
|
|
|
if( error.empty())
|
|
|
|
{
|
|
|
|
it->second.write( ostream );
|
|
|
|
ostream.close();
|
2013-01-03 20:15:21 +01:00
|
|
|
if( !ostream.has_error() && rename( filename, modifyFile.c_str()) == 0 )
|
2012-12-23 23:02:39 +01:00
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
ostream.clear_error();
|
|
|
|
unlink( filename );
|
|
|
|
if( !ok )
|
2012-10-15 15:36:25 +02:00
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Error,
|
2013-01-03 20:15:21 +01:00
|
|
|
"cannot write modified source to %0 (%1) [loplugin]" )) << modifyFile << error;
|
2012-12-23 23:02:39 +01:00
|
|
|
delete[] filename;
|
2012-10-15 15:36:25 +02:00
|
|
|
}
|
2012-10-05 18:17:13 +02:00
|
|
|
}
|
|
|
|
private:
|
2012-10-15 15:36:25 +02:00
|
|
|
bool isArg( const char* arg ) const
|
|
|
|
{
|
|
|
|
return find( args.begin(), args.end(), arg ) != args.end();
|
|
|
|
}
|
2012-10-05 18:17:13 +02:00
|
|
|
Rewriter rewriter;
|
2012-10-15 15:36:25 +02:00
|
|
|
vector< string > args;
|
2012-10-09 16:21:33 +02:00
|
|
|
BodyNotInBlock bodyNotInBlock;
|
2012-10-15 15:36:25 +02:00
|
|
|
LclStaticFix lclStaticFix;
|
2012-10-15 23:29:36 +02:00
|
|
|
PostfixIncrementFix postfixIncrementFix;
|
2013-01-03 20:23:51 +01:00
|
|
|
RemoveForwardStringDecl removeForwardStringDecl;
|
2012-10-13 17:38:58 +02:00
|
|
|
SalLogAreas salLogAreas;
|
2012-10-09 14:50:19 +02:00
|
|
|
UnusedVariableCheck unusedVariableCheck;
|
2012-10-05 18:17:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
The Clang plugin class, just forwards to PluginHandler.
|
|
|
|
*/
|
|
|
|
class LibreOfficeAction
|
|
|
|
: public PluginASTAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ASTConsumer* CreateASTConsumer( CompilerInstance& Compiler, StringRef InFile )
|
|
|
|
{
|
2012-10-15 15:36:25 +02:00
|
|
|
return new PluginHandler( Compiler.getASTContext(), _args );
|
2012-10-05 18:17:13 +02:00
|
|
|
}
|
2012-10-15 15:36:25 +02:00
|
|
|
virtual bool ParseArgs( const CompilerInstance& CI, const vector< string >& args )
|
2012-10-05 18:17:13 +02:00
|
|
|
{
|
2012-10-15 15:36:25 +02:00
|
|
|
_args = args;
|
2012-10-05 18:17:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-15 15:36:25 +02:00
|
|
|
private:
|
|
|
|
vector< string > _args;
|
2012-10-05 18:17:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static FrontendPluginRegistry::Add< loplugin::LibreOfficeAction > X( "loplugin", "LibreOffice compile check plugin" );
|