![]() |
|
This example plugin converts all procedural if statements to case statements.
The makefile for building the plugin (for linux based systems) follows (Makefile-example2):
#/*----------------------------------------------------------------------------- # * Copyright (c) 1997-2009 Mark Hummel DBA Raquette Software. # * All rights reserved # * # * This file contains source code written by Raquette Software, # * 68 Stewart Street, Franklin MA 02038. It may not be used without # * express written permission. The expression of the information # * contained herein is protected under federal copyright laws and # * all copying without permission is prohibited and may be subject # * to criminal penalties. The Author assumes no responsibility for # * errors, omissions, or damages caused by the use of these programs # * or from use of the information contained herein. # * # *----------------------------------------------------------------------------- # */ # # Extract installation flags and paths for vrq # VRQ_VERSION = $(shell vrq --version|head -n 1|cut -c 5-|cut -d , -f1) PLUGIN_DIR = $(shell vrq --pkglibdir) INCLUDE_DIR = $(shell vrq --includedir) CXXFLAGS = $(shell vrq --cflags) LDFLAGS = $(shell vrq --ldflags) # # name of plugin # PLUGIN=example2 # # Build object file # all: $(PLUGIN).so $(PLUGIN).o: $(PLUGIN).cc $(PLUGIN).h $(CXX) -I . -I $(INCLUDE_DIR) $(CXXFLAGS) \ -DVRQ_VERSION=\"$(VRQ_VERSION)\" -c $< -fPIC -DPIC -o $@ # # Build DLL # $(PLUGIN).so: $(PLUGIN).o $(CXX) $(LDFLAGS) -shared $< -Wl,-soname -Wl,$@ -o $@ # # Install plugin # install: $(PLUGIN).so install -c '$<' $(PLUGIN_DIR) # # Print installation flags and paths # env: echo "VRQ_VERSION: $(VRQ_VERSION)" echo "PLUGIN_DIR: $(PLUGIN_DIR)" echo "INCLUDE_DIR: $(INCLUDE_DIR)" echo "CXXFLAGS: $(CXXFLAGS)" echo "LDFLAGS: $(LDFLAGS)" # # remove built files # clean: -rm $(PLUGIN).o $(PLUGIN).so
The header file for the plugin is below (example2.h):
/*-----------------------------------------------------------------------------
* Copyright (c) 1997-2009 Mark Hummel DBA Raquette Software.
* All rights reserved
*
* This file contains source code written by Raquette Software,
* 68 Stewart Street, Franklin MA 02038. It may not be used without
* express written permission. The expression of the information
* contained herein is protected under federal copyright laws and
* all copying without permission is prohibited and may be subject
* to criminal penalties. The Author assumes no responsibility for
* errors, omissions, or damages caused by the use of these programs
* or from use of the information contained herein.
*
*-----------------------------------------------------------------------------
*/
/******************************************************************************
*
*
* example2.h
* - example plugin
* -
*
******************************************************************************
*/
#ifndef EXAMPLE2_H
#define EXAMPLE2_H
/*
* include vrq headers
*/
#include "plugin.h"
#include <map>
/*
* Plugin tools implement a subclass of CBackend.
*/
class CExample2 : public CBackend
{
public:
/*
* Methods
*/
CExample2( void );
virtual char* GetToolName( void );
virtual char* GetToolDescription( void );
virtual int AcceptAllPlusArgs( void );
virtual int HideTool();
virtual int IgnoreVrqComments();
virtual int ResolveModules();
virtual int ResolveInstance( CModule*, CInstance* );
virtual void Activate();
virtual void Process( list<CElement>& inputList,
list<CElement>& outputList );
};
#endif // EXAMPLE2_H
The source for the plugin follows (example2.cc):
/*-----------------------------------------------------------------------------
* Copyright (c) 1997-2009 Mark Hummel DBA Raquette Software.
* All rights reserved
*
* This file contains source code written by Raquette Software,
* 68 Stewart Street, Franklin MA 02038. It may not be used without
* express written permission. The expression of the information
* contained herein is protected under federal copyright laws and
* all copying without permission is prohibited and may be subject
* to criminal penalties. The Author assumes no responsibility for
* errors, omissions, or damages caused by the use of these programs
* or from use of the information contained herein.
*
*-----------------------------------------------------------------------------
*/
/******************************************************************************
*
*
* example2.cc
* - methods for example plugin
*
******************************************************************************
*/
#include "example2.h"
/************************************************
CreateToolInstance
- entry point for dll. Vrq will
call this entry point (must be C code)
to create and initialize the tool.
If tool creation fails, return NULL and
vrq will ignore DLL.
************************************************/
extern "C" {
CBackend* CreateToolInstance()
{
/*
* Only initialize the tool if it was built for
* the running version of vrq.
*/
if( strcmp( VRQ_VERSION, VrqVersionString() ) ) {
return NULL;
}
/*
* Create tool and return it.
*/
return new CExample2();
}
}
/************************************************
Constructor
- Initialize tool
************************************************/
CExample2::CExample2( )
{
}
/************************************************
AcceptAllPlusArgs
- return TRUE if tool accepts arbitrary
plusargs.
************************************************/
int CExample2::AcceptAllPlusArgs( void )
{
/*
* Tool does not support arbitary plusargs
*/
return FALSE;
}
/************************************************
HideTool
- return TRUE if tool should be hidden
from help system. This allows a tool
to be functional and hidden.
***********************************************/
int CExample2::HideTool()
{
return FALSE;
}
/************************************************
IgnoreVrqComments
- return TRUE if tool requires lexer to
treat vrq translate_on/off pragmas
as pure comments.
***********************************************/
int CExample2::IgnoreVrqComments()
{
return FALSE;
}
/************************************************
ResolveModules
- return TRUE if tool requires all
module references to be resolved.
This is done by search specified
library search paths.
***********************************************/
int CExample2::ResolveModules()
{
return FALSE;
}
/************************************************
ResolveInstance
- return TRUE if tool requires the
given module reference to be resolved.
This is allows a tool to implement a
fine grain filter on what must be resolved.
Note this only called if ResolvedModules
returns TRUE.
***********************************************/
int CExample2::ResolveInstance( CModule*, CInstance* )
{
return FALSE;
}
/************************************************
Activate
- This routine is called before a tool
is invoked. It is only called once
and will only be called if the tool
will be used in a pipeline.
***********************************************/
void CExample2::Activate()
{
}
/************************************************
GetToolName
- return name of tool
************************************************/
char* CExample2::GetToolName( void )
{
static char toolName[] = "example2";
return toolName;
}
/************************************************
GetToolDescription
- return description of tool. This
text is used in vrq help text, man pages, etc.
************************************************/
char* CExample2::GetToolDescription( void )
{
static char toolDescription[] = "Example plugin that converts if statements"
" to case statements";
return toolDescription;
}
/************************************************
ProcessIfStatements
- callback routine used to convert if
statements to case statements.
***********************************************/
CNode* ProcessIfStatements( CNode* n, void* arg )
{
switch( n->GetOp() ) {
}
return n;
}
/************************************************
Process
- process a compilation unit
************************************************/
void CExample2::Process( list<CElement>& inputList,
list<CElement>& outputList )
{
/*
* process each compilation unit, pass on modified trees to next filter.
*/
list<CElement>::iterator ptr;
for( ptr = inputList.begin(); ptr != inputList.end(); ++ptr ) {
/*
* Transverse the entire code tree for the given compilation
* unit. For each node ProcessIfStatements will be called to
* convert if to case
*/
CNode* code = ptr->Code();
code = code->PostSubVisit1( ProcessIfStatements, NULL );
outputList.push_back( CElement( ptr->Filename(), !ptr->Filename(), code ) );
}
}
1.7.1