pass around CompilerInstance rather than ASTContext

It's possible to get the latter from the former, and the former
is useful for other things too (access to the preprocessor, for example).

Change-Id: I708d709129fd3a35bf7c63da4de09c2e696b382d
This commit is contained in:
Luboš Luňák 2013-03-21 16:42:10 +01:00
parent 9ab15ecc4f
commit 153a69cad2
15 changed files with 67 additions and 65 deletions

View File

@ -28,14 +28,14 @@ For example:
Here either both statements should be inside {} or the second statement in indented wrong.
*/
BodyNotInBlock::BodyNotInBlock( ASTContext& context )
: Plugin( context )
BodyNotInBlock::BodyNotInBlock( CompilerInstance& compiler )
: Plugin( compiler )
{
}
void BodyNotInBlock::run()
{
TraverseDecl( context.getTranslationUnitDecl());
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool BodyNotInBlock::VisitFunctionDecl( FunctionDecl* declaration )
@ -110,9 +110,9 @@ void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, c
if( it != parents[ parent_pos ]->child_end())
{
bool invalid1, invalid2;
unsigned bodyColumn = context.getSourceManager()
unsigned bodyColumn = compiler.getSourceManager()
.getPresumedColumnNumber( body->getLocStart(), &invalid1 );
unsigned nextStatementColumn = context.getSourceManager()
unsigned nextStatementColumn = compiler.getSourceManager()
.getPresumedColumnNumber( (*it)->getLocStart(), &invalid2 );
if( invalid1 || invalid2 )
return;

View File

@ -21,7 +21,7 @@ class BodyNotInBlock
, public Plugin
{
public:
explicit BodyNotInBlock( ASTContext& context );
explicit BodyNotInBlock( CompilerInstance& compiler );
virtual void run();
bool VisitFunctionDecl( FunctionDecl* declaration );
private:

View File

@ -21,9 +21,9 @@ class LiteralAlternative:
public RecursiveASTVisitor<LiteralAlternative>, public loplugin::Plugin
{
public:
explicit LiteralAlternative(ASTContext & context): Plugin(context) {}
explicit LiteralAlternative(CompilerInstance & compiler): Plugin(compiler) {}
virtual void run() { TraverseDecl(context.getTranslationUnitDecl()); }
virtual void run() { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
bool VisitCallExpr(CallExpr * expr);
};
@ -65,7 +65,7 @@ bool LiteralAlternative::VisitCallExpr(CallExpr * expr) {
// better to handle that at the level of non-expanded macros instead,
// but I have not found out how to do that yet anyway):
APSInt res;
if (expr->getArg(1)->isIntegerConstantExpr(res, context)) {
if (expr->getArg(1)->isIntegerConstantExpr(res, compiler.getASTContext())) {
Expr const * arg0 = expr->getArg(0)->IgnoreParenImpCasts();
StringLiteral const * lit = dyn_cast<StringLiteral>(arg0);
bool match = false;
@ -82,7 +82,7 @@ bool LiteralAlternative::VisitCallExpr(CallExpr * expr) {
subs->getBase()->IgnoreParenImpCasts());
match = lit != nullptr
&& subs->getIdx()->isIntegerConstantExpr(
res, context)
res, compiler.getASTContext())
&& res == 0;
}
}

View File

@ -20,20 +20,20 @@ Base classes for plugin actions.
namespace loplugin
{
Plugin::Plugin( ASTContext& context )
: context( context )
Plugin::Plugin( CompilerInstance& compiler )
: compiler( compiler )
{
}
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
{
return report( level, message, context, loc );
return report( level, message, compiler, loc );
}
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, ASTContext& context,
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, CompilerInstance& compiler,
SourceLocation loc )
{
DiagnosticsEngine& diag = context.getDiagnostics();
DiagnosticsEngine& diag = compiler.getDiagnostics();
#if 0
// Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
if( level == DiagnosticsEngine::Warning && diag.getWarningsAsErrors())
@ -50,10 +50,10 @@ DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef mess
bool Plugin::ignoreLocation( SourceLocation loc )
{
SourceLocation expansionLoc = context.getSourceManager().getExpansionLoc( loc );
if( context.getSourceManager().isInSystemHeader( expansionLoc ))
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))
return true;
const char* bufferName = context.getSourceManager().getPresumedLoc( expansionLoc ).getFilename();
const char* bufferName = compiler.getSourceManager().getPresumedLoc( expansionLoc ).getFilename();
if( bufferName == NULL )
return true;
if( strncmp( bufferName, OUTDIR, strlen( OUTDIR )) == 0
@ -64,15 +64,15 @@ bool Plugin::ignoreLocation( SourceLocation loc )
return true;
}
void Plugin::registerPlugin( Plugin* (*create)( ASTContext&, Rewriter& ), const char* optionName, bool isRewriter )
void Plugin::registerPlugin( Plugin* (*create)( CompilerInstance&, Rewriter& ), const char* optionName, bool isRewriter )
{
PluginHandler::registerPlugin( create, optionName, isRewriter );
}
/////
RewritePlugin::RewritePlugin( ASTContext& context, Rewriter& rewriter )
: Plugin( context )
RewritePlugin::RewritePlugin( CompilerInstance& compiler, Rewriter& rewriter )
: Plugin( compiler )
, rewriter( rewriter )
{
}

View File

@ -17,6 +17,7 @@
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/FileManager.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Frontend/CompilerInstance.h>
#if __clang_major__ < 3 || __clang_major__ == 3 && __clang_minor__ < 2
#include <clang/Rewrite/Rewriter.h>
@ -40,21 +41,21 @@ namespace loplugin
class Plugin
{
public:
explicit Plugin( ASTContext& context );
explicit Plugin( CompilerInstance& compiler );
virtual ~Plugin();
virtual void run() = 0;
template< typename T > class Registration;
DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc = SourceLocation());
static DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message,
ASTContext& context, SourceLocation loc = SourceLocation());
CompilerInstance& compiler, SourceLocation loc = SourceLocation());
protected:
bool ignoreLocation( SourceLocation loc );
bool ignoreLocation( const Decl* decl );
bool ignoreLocation( const Stmt* stmt );
ASTContext& context;
CompilerInstance& compiler;
private:
static void registerPlugin( Plugin* (*create)( ASTContext&, Rewriter& ), const char* optionName, bool isRewriter );
template< typename T > static Plugin* createHelper( ASTContext& context, Rewriter& rewriter );
static void registerPlugin( Plugin* (*create)( CompilerInstance&, Rewriter& ), const char* optionName, bool isRewriter );
template< typename T > static Plugin* createHelper( CompilerInstance& compiler, Rewriter& rewriter );
enum { isRewriter = false };
};
@ -67,7 +68,7 @@ class RewritePlugin
: public Plugin
{
public:
explicit RewritePlugin( ASTContext& context, Rewriter& rewriter );
explicit RewritePlugin( CompilerInstance& compiler, Rewriter& rewriter );
protected:
// This enum allows passing just 'RemoveLineIfEmpty' to functions below.
enum RemoveLineIfEmpty_t { RemoveLineIfEmpty };
@ -109,7 +110,7 @@ class RewritePlugin
Rewriter& rewriter;
private:
template< typename T > friend class Plugin::Registration;
template< typename T > static Plugin* createHelper( ASTContext& context, Rewriter& rewriter );
template< typename T > static Plugin* createHelper( CompilerInstance& compiler, Rewriter& rewriter );
enum { isRewriter = true };
bool reportEditFailure( SourceLocation loc );
bool adjustForWholeStatement( SourceRange* range );
@ -136,7 +137,7 @@ class Plugin::Registration
class RegistrationCreate
{
public:
template< typename T, bool > static T* create( ASTContext& context, Rewriter& rewriter );
template< typename T, bool > static T* create( CompilerInstance& compiler, Rewriter& rewriter );
};
/////
@ -159,15 +160,15 @@ bool Plugin::ignoreLocation( const Stmt* stmt )
}
template< typename T >
Plugin* Plugin::createHelper( ASTContext& context, Rewriter& )
Plugin* Plugin::createHelper( CompilerInstance& compiler, Rewriter& )
{
return new T( context );
return new T( compiler );
}
template< typename T >
Plugin* RewritePlugin::createHelper( ASTContext& context, Rewriter& rewriter )
Plugin* RewritePlugin::createHelper( CompilerInstance& compiler, Rewriter& rewriter )
{
return new T( context, rewriter );
return new T( compiler, rewriter );
}
template< typename T >

View File

@ -25,7 +25,7 @@ namespace loplugin
struct PluginData
{
Plugin* (*create)( ASTContext&, Rewriter& );
Plugin* (*create)( CompilerInstance&, Rewriter& );
Plugin* object;
const char* optionName;
bool isRewriter;
@ -36,9 +36,9 @@ static PluginData plugins[ MAX_PLUGINS ];
static int pluginCount = 0;
static bool pluginObjectsCreated = false;
PluginHandler::PluginHandler( ASTContext& context, const vector< string >& args )
: context( context )
, rewriter( context.getSourceManager(), context.getLangOpts())
PluginHandler::PluginHandler( CompilerInstance& compiler, const vector< string >& args )
: compiler( compiler )
, rewriter( compiler.getSourceManager(), compiler.getLangOpts())
, scope( "mainfile" )
{
bool wasPlugin = false;
@ -95,11 +95,11 @@ void PluginHandler::createPlugin( const string& name )
if( name.empty()) // no plugin given -> create non-writer plugins
{
if( !plugins[ i ].isRewriter )
plugins[ i ].object = plugins[ i ].create( context, rewriter );
plugins[ i ].object = plugins[ i ].create( compiler, rewriter );
}
else if( plugins[ i ].optionName == name )
{
plugins[ i ].object = plugins[ i ].create( context, rewriter );
plugins[ i ].object = plugins[ i ].create( compiler, rewriter );
return;
}
}
@ -107,7 +107,7 @@ void PluginHandler::createPlugin( const string& name )
report( DiagnosticsEngine::Fatal, "unknown plugin tool %0" ) << name;
}
void PluginHandler::registerPlugin( Plugin* (*create)( ASTContext&, Rewriter& ), const char* optionName, bool isRewriter )
void PluginHandler::registerPlugin( Plugin* (*create)( CompilerInstance&, Rewriter& ), const char* optionName, bool isRewriter )
{
assert( !pluginObjectsCreated );
assert( pluginCount < MAX_PLUGINS );
@ -120,7 +120,7 @@ void PluginHandler::registerPlugin( Plugin* (*create)( ASTContext&, Rewriter& ),
DiagnosticBuilder PluginHandler::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
{
return Plugin::report( level, message, context, loc );
return Plugin::report( level, message, compiler, loc );
}
void PluginHandler::HandleTranslationUnit( ASTContext& context )
@ -218,7 +218,7 @@ void PluginHandler::HandleTranslationUnit( ASTContext& context )
ASTConsumer* LibreOfficeAction::CreateASTConsumer( CompilerInstance& Compiler, StringRef )
{
return new PluginHandler( Compiler.getASTContext(), _args );
return new PluginHandler( Compiler, _args );
}
bool LibreOfficeAction::ParseArgs( const CompilerInstance&, const vector< string >& args )

View File

@ -26,15 +26,15 @@ class PluginHandler
: public ASTConsumer
{
public:
PluginHandler( ASTContext& context, const vector< string >& args );
PluginHandler( CompilerInstance& compiler, const vector< string >& args );
virtual ~PluginHandler();
virtual void HandleTranslationUnit( ASTContext& context );
static void registerPlugin( Plugin* (*create)( ASTContext&, Rewriter& ), const char* optionName, bool isRewriter );
static void registerPlugin( Plugin* (*create)( CompilerInstance&, Rewriter& ), const char* optionName, bool isRewriter );
private:
void handleOption( const string& option );
void createPlugin( const string& name );
DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc = SourceLocation());
ASTContext& context;
CompilerInstance& compiler;
Rewriter rewriter;
string scope;
};

View File

@ -19,14 +19,14 @@ Change all postfix ++ operators of non-trivial types to prefix if possible.
namespace loplugin
{
PostfixIncrementFix::PostfixIncrementFix( ASTContext& context, Rewriter& rewriter )
: RewritePlugin( context, rewriter )
PostfixIncrementFix::PostfixIncrementFix( CompilerInstance& compiler, Rewriter& rewriter )
: RewritePlugin( compiler, rewriter )
{
}
void PostfixIncrementFix::run()
{
TraverseDecl( context.getTranslationUnitDecl());
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool PostfixIncrementFix::VisitFunctionDecl( FunctionDecl* declaration )

View File

@ -21,7 +21,7 @@ class PostfixIncrementFix
, public RewritePlugin
{
public:
explicit PostfixIncrementFix( ASTContext& context, Rewriter& rewriter );
explicit PostfixIncrementFix( CompilerInstance& compiler, Rewriter& rewriter );
virtual void run();
bool VisitFunctionDecl( FunctionDecl* declaration );
private:

View File

@ -19,14 +19,14 @@ Remove all forward declarations of rtl strings. I.e. 'namespace rtl { class OUSt
namespace loplugin
{
RemoveForwardStringDecl::RemoveForwardStringDecl( ASTContext& context, Rewriter& rewriter )
: RewritePlugin( context, rewriter )
RemoveForwardStringDecl::RemoveForwardStringDecl( CompilerInstance& compiler, Rewriter& rewriter )
: RewritePlugin( compiler, rewriter )
{
}
void RemoveForwardStringDecl::run()
{
TraverseDecl( context.getTranslationUnitDecl());
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool RemoveForwardStringDecl::VisitNamespaceDecl( NamespaceDecl* declaration )

View File

@ -21,7 +21,7 @@ class RemoveForwardStringDecl
, public RewritePlugin
{
public:
explicit RemoveForwardStringDecl( ASTContext& context, Rewriter& rewriter );
explicit RemoveForwardStringDecl( CompilerInstance& compiler, Rewriter& rewriter );
virtual void run();
bool VisitNamespaceDecl( NamespaceDecl* declaration );
private:

View File

@ -25,8 +25,8 @@ report if the area is not listed there. The fix is either use a proper area or a
if appropriate.
*/
SalLogAreas::SalLogAreas( ASTContext& context )
: Plugin( context )
SalLogAreas::SalLogAreas( CompilerInstance& compiler )
: Plugin( compiler )
{
}
@ -34,7 +34,7 @@ void SalLogAreas::run()
{
inFunction = NULL;
lastSalDetailLogStreamMacro = SourceLocation();
TraverseDecl( context.getTranslationUnitDecl());
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool SalLogAreas::VisitFunctionDecl( FunctionDecl* function )
@ -59,7 +59,7 @@ bool SalLogAreas::VisitCallExpr( CallExpr* call )
// The SAL_DETAIL_LOG_STREAM macro expands to two calls to sal::detail::log(),
// so do not warn repeatedly about the same macro (the area->getLocStart() of all the calls
// from the same macro should be the same).
SourceLocation expansionLocation = context.getSourceManager().getExpansionLoc( call->getLocStart());
SourceLocation expansionLocation = compiler.getSourceManager().getExpansionLoc( call->getLocStart());
if( expansionLocation == lastSalDetailLogStreamMacro )
return true;
lastSalDetailLogStreamMacro = expansionLocation;
@ -74,14 +74,15 @@ bool SalLogAreas::VisitCallExpr( CallExpr* call )
}
if( inFunction->getQualifiedNameAsString() == "sal::detail::log" )
return true; // This function only forwards to sal_detail_log, so ok.
if( call->getArg( 1 )->isNullPointerConstant( context, Expr::NPC_ValueDependentIsNotNull ) != Expr::NPCK_NotNull )
if( call->getArg( 1 )->isNullPointerConstant( compiler.getASTContext(),
Expr::NPC_ValueDependentIsNotNull ) != Expr::NPCK_NotNull )
{ // If the area argument is a null pointer, that is allowed only for SAL_DEBUG.
const SourceManager& source = context.getSourceManager();
const SourceManager& source = compiler.getSourceManager();
for( SourceLocation loc = call->getLocStart();
loc.isMacroID();
loc = source.getImmediateExpansionRange( loc ).first )
{
StringRef inMacro = Lexer::getImmediateMacroName( loc, source, context.getLangOpts());
StringRef inMacro = Lexer::getImmediateMacroName( loc, source, compiler.getLangOpts());
if( inMacro == "SAL_DEBUG" )
return true; // ok
}

View File

@ -23,7 +23,7 @@ class SalLogAreas
, public Plugin
{
public:
explicit SalLogAreas( ASTContext& context );
explicit SalLogAreas( CompilerInstance& compiler );
virtual void run();
bool VisitFunctionDecl( FunctionDecl* function );
bool VisitCallExpr( CallExpr* call );

View File

@ -30,14 +30,14 @@ SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
that cannot be edited there is a manual list below.
*/
UnusedVariableCheck::UnusedVariableCheck( ASTContext& context )
: Plugin( context )
UnusedVariableCheck::UnusedVariableCheck( CompilerInstance& compiler )
: Plugin( compiler )
{
}
void UnusedVariableCheck::run()
{
TraverseDecl( context.getTranslationUnitDecl());
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool UnusedVariableCheck::VisitVarDecl( VarDecl* var )

View File

@ -21,7 +21,7 @@ class UnusedVariableCheck
, public Plugin
{
public:
explicit UnusedVariableCheck( ASTContext& context );
explicit UnusedVariableCheck( CompilerInstance& compiler );
virtual void run();
bool VisitVarDecl( VarDecl* var );
};