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>
|
|
|
|
|
#include <clang/Rewrite/Rewriter.h>
|
|
|
|
|
|
2012-10-09 16:21:33 +02:00
|
|
|
#include "bodynotinblock.hxx"
|
2012-10-15 15:36:25 +02:00
|
|
|
#include "lclstaticfix.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 )
|
|
|
|
|
{
|
|
|
|
|
return context.getSourceManager().isInSystemHeader( context.getSourceManager().getExpansionLoc( loc ));
|
|
|
|
|
}
|
|
|
|
|
|
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-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();
|
|
|
|
|
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 );
|
|
|
|
|
string filename = std::string( e->getName()) + ".new";
|
|
|
|
|
string error;
|
|
|
|
|
// TODO If there will be actually plugins also modifying headers,
|
|
|
|
|
// race conditions should be avoided here.
|
|
|
|
|
raw_fd_ostream ostream( filename.c_str(), error );
|
|
|
|
|
DiagnosticsEngine& diag = context.getDiagnostics();
|
|
|
|
|
if( !error.empty())
|
|
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Error,
|
|
|
|
|
"cannot write modified source to %0 (%1) [loplugin]" )) << filename << error;
|
|
|
|
|
else
|
|
|
|
|
diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Note,
|
|
|
|
|
"modified source %0 [loplugin]" )) << filename;
|
|
|
|
|
it->second.write( ostream );
|
|
|
|
|
}
|
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-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" );
|