summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/pluginhandler.cxx
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@suse.cz>2013-02-02 17:45:18 +0100
committerLuboš Luňák <l.lunak@suse.cz>2013-02-02 22:59:44 +0100
commita7c3adb773e5b69601716bda181cc481090a4d59 (patch)
tree1ffbb7bb5863a316058553ba941be5b9facc7e1b /compilerplugins/clang/pluginhandler.cxx
parentb8f932b6a5c6ace05df975f82e682f10804dad5d (diff)
avoid having to manuall modify sources when adding a new clang plugin
Now each one registers in its .cxx file. Change-Id: I811c0d4400c2bdccc1c287269378d7e8ad8743ce
Diffstat (limited to 'compilerplugins/clang/pluginhandler.cxx')
-rw-r--r--compilerplugins/clang/pluginhandler.cxx87
1 files changed, 64 insertions, 23 deletions
diff --git a/compilerplugins/clang/pluginhandler.cxx b/compilerplugins/clang/pluginhandler.cxx
index fd2c62e58caf..35d881ec52f2 100644
--- a/compilerplugins/clang/pluginhandler.cxx
+++ b/compilerplugins/clang/pluginhandler.cxx
@@ -19,39 +19,80 @@
namespace loplugin
{
+struct PluginData
+ {
+ Plugin* (*create)( ASTContext&, Rewriter& );
+ Plugin* object;
+ const char* optionName;
+ bool isRewriter;
+ };
+
+const int MAX_PLUGINS = 100;
+static PluginData plugins[ MAX_PLUGINS ];
+static int pluginCount = 0;
+static bool pluginObjectsCreated = false;
+
PluginHandler::PluginHandler( ASTContext& context, const vector< string >& args )
: rewriter( context.getSourceManager(), context.getLangOpts())
- , args( args )
- , bodyNotInBlock( context )
- , lclStaticFix( context, rewriter )
- , postfixIncrementFix( context, rewriter )
- , removeForwardStringDecl( context, rewriter )
- , salLogAreas( context )
- , unusedVariableCheck( context )
{
+ bool wasCreated = false;
+ for( int i = 0;
+ i < pluginCount;
+ ++i )
+ {
+ bool create = false;
+ if( args.empty()) // no args -> create non-writer plugins
+ create = !plugins[ i ].isRewriter;
+ else // create only the given plugin(s)
+ {
+ if( find( args.begin(), args.end(), plugins[ i ].optionName ) != args.end())
+ create = true;
+ }
+ if( create )
+ {
+ plugins[ i ].object = plugins[ i ].create( context, rewriter );
+ wasCreated = true;
+ }
+ }
+ pluginObjectsCreated = true;
+ if( !args.empty() && !wasCreated )
+ {
+ DiagnosticsEngine& diag = context.getDiagnostics();
+ diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Fatal,
+ "unknown plugin tool %0 [loplugin]" )) << args.front();
+ }
+ }
+
+PluginHandler::~PluginHandler()
+ {
+ for( int i = 0;
+ i < pluginCount;
+ ++i )
+ if( plugins[ i ].object != NULL )
+ delete plugins[ i ].object;
+ }
+
+void PluginHandler::registerPlugin( Plugin* (*create)( ASTContext&, Rewriter& ), const char* optionName, bool isRewriter )
+ {
+ assert( !pluginObjectsCreated );
+ assert( pluginCount < MAX_PLUGINS );
+ plugins[ pluginCount ].create = create;
+ plugins[ pluginCount ].object = NULL;
+ plugins[ pluginCount ].optionName = optionName;
+ plugins[ pluginCount ].isRewriter = isRewriter;
+ ++pluginCount;
}
void PluginHandler::HandleTranslationUnit( ASTContext& context )
{
if( context.getDiagnostics().hasErrorOccurred())
return;
- if( isArg( "lclstaticfix" ))
- lclStaticFix.run();
- else if( isArg( "postfixincrementfix" ))
- postfixIncrementFix.run();
- else if( isArg( "removeforwardstringdecl" ))
- removeForwardStringDecl.run();
- else if( args.empty())
- {
- bodyNotInBlock.run();
- salLogAreas.run();
- unusedVariableCheck.run();
- }
- else
+ for( int i = 0;
+ i < pluginCount;
+ ++i )
{
- DiagnosticsEngine& diag = context.getDiagnostics();
- diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Fatal,
- "unknown plugin tool %0 [loplugin]" )) << args.front();
+ if( plugins[ i ].object != NULL )
+ plugins[ i ].object->run();
}
for( Rewriter::buffer_iterator it = rewriter.buffer_begin();
it != rewriter.buffer_end();