move some code to a common base
Change-Id: Ife306c69054dfcc20b1339b88a4e14e5333ced71
This commit is contained in:
@@ -12,8 +12,6 @@
|
||||
|
||||
#include <clang/Basic/SourceManager.h>
|
||||
|
||||
using namespace clang;
|
||||
|
||||
namespace loplugin
|
||||
{
|
||||
|
||||
@@ -23,21 +21,10 @@ but are not inside a compound statement and thus the second one is unrelated.
|
||||
*/
|
||||
|
||||
BodyNotInBlock::BodyNotInBlock( ASTContext& context )
|
||||
: context( context )
|
||||
: Plugin( context )
|
||||
{
|
||||
}
|
||||
|
||||
DiagnosticBuilder BodyNotInBlock::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
|
||||
{
|
||||
// Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
|
||||
DiagnosticsEngine& diag = context.getDiagnostics();
|
||||
if( level == DiagnosticsEngine::Warning && diag.getWarningsAsErrors())
|
||||
level = DiagnosticsEngine::Error;
|
||||
if( level == DiagnosticsEngine::Error && diag.getErrorsAsFatal())
|
||||
level = DiagnosticsEngine::Fatal;
|
||||
return diag.Report( loc, diag.getCustomDiagID( level, message ));
|
||||
}
|
||||
|
||||
void BodyNotInBlock::run()
|
||||
{
|
||||
TraverseDecl( context.getTranslationUnitDecl());
|
||||
|
@@ -11,27 +11,23 @@
|
||||
#ifndef BODYNOTINBLOCK_H
|
||||
#define BODYNOTINBLOCK_H
|
||||
|
||||
#include <clang/AST/RecursiveASTVisitor.h>
|
||||
|
||||
using namespace clang;
|
||||
#include "compileplugin.hxx"
|
||||
|
||||
namespace loplugin
|
||||
{
|
||||
|
||||
typedef std::vector< const Stmt* > StmtParents;
|
||||
|
||||
class BodyNotInBlock
|
||||
: public RecursiveASTVisitor< BodyNotInBlock >
|
||||
, public Plugin
|
||||
{
|
||||
public:
|
||||
explicit BodyNotInBlock( ASTContext& context );
|
||||
void run();
|
||||
bool VisitFunctionDecl( FunctionDecl* declaration );
|
||||
private:
|
||||
typedef std::vector< const Stmt* > StmtParents;
|
||||
void traverseStatement( const Stmt* stmt, StmtParents& parents );
|
||||
void checkBody( const Stmt* body, const StmtParents& parents, int stmtType );
|
||||
DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc );
|
||||
ASTContext& context;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
@@ -25,6 +25,22 @@ using namespace clang;
|
||||
namespace loplugin
|
||||
{
|
||||
|
||||
Plugin::Plugin( ASTContext& context )
|
||||
: context( context )
|
||||
{
|
||||
}
|
||||
|
||||
DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
|
||||
{
|
||||
// Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
|
||||
DiagnosticsEngine& diag = context.getDiagnostics();
|
||||
if( level == DiagnosticsEngine::Warning && diag.getWarningsAsErrors())
|
||||
level = DiagnosticsEngine::Error;
|
||||
if( level == DiagnosticsEngine::Error && diag.getErrorsAsFatal())
|
||||
level = DiagnosticsEngine::Fatal;
|
||||
return diag.Report( loc, diag.getCustomDiagID( level, message ));
|
||||
}
|
||||
|
||||
/**
|
||||
Class that manages all LO modules.
|
||||
*/
|
||||
|
@@ -11,4 +11,22 @@
|
||||
#ifndef COMPILEPLUGIN_H
|
||||
#define COMPILEPLUGIN_H
|
||||
|
||||
#include <clang/AST/RecursiveASTVisitor.h>
|
||||
|
||||
using namespace clang;
|
||||
|
||||
namespace loplugin
|
||||
{
|
||||
|
||||
class Plugin
|
||||
{
|
||||
public:
|
||||
explicit Plugin( ASTContext& context );
|
||||
protected:
|
||||
DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc );
|
||||
ASTContext& context;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // COMPILEPLUGIN_H
|
||||
|
@@ -12,8 +12,6 @@
|
||||
|
||||
#include <clang/Basic/SourceManager.h>
|
||||
|
||||
using namespace clang;
|
||||
|
||||
namespace loplugin
|
||||
{
|
||||
|
||||
@@ -31,7 +29,7 @@ that cannot be edited there is a manual list below.
|
||||
*/
|
||||
|
||||
UnusedVariableCheck::UnusedVariableCheck( ASTContext& context )
|
||||
: context( context )
|
||||
: Plugin( context )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -40,17 +38,6 @@ void UnusedVariableCheck::run()
|
||||
TraverseDecl( context.getTranslationUnitDecl());
|
||||
}
|
||||
|
||||
DiagnosticBuilder UnusedVariableCheck::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
|
||||
{
|
||||
// Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
|
||||
DiagnosticsEngine& diag = context.getDiagnostics();
|
||||
if( level == DiagnosticsEngine::Warning && diag.getWarningsAsErrors())
|
||||
level = DiagnosticsEngine::Error;
|
||||
if( level == DiagnosticsEngine::Error && diag.getErrorsAsFatal())
|
||||
level = DiagnosticsEngine::Fatal;
|
||||
return diag.Report( loc, diag.getCustomDiagID( level, message ));
|
||||
}
|
||||
|
||||
bool UnusedVariableCheck::VisitNamedDecl( NamedDecl* declaration )
|
||||
{
|
||||
// TODO also LO header files? or a subdir?
|
||||
|
@@ -11,23 +11,19 @@
|
||||
#ifndef UNUSEDVARIABLECHECK_H
|
||||
#define UNUSEDVARIABLECHECK_H
|
||||
|
||||
#include <clang/AST/RecursiveASTVisitor.h>
|
||||
|
||||
using namespace clang;
|
||||
#include "compileplugin.hxx"
|
||||
|
||||
namespace loplugin
|
||||
{
|
||||
|
||||
class UnusedVariableCheck
|
||||
: public RecursiveASTVisitor< UnusedVariableCheck >
|
||||
, public Plugin
|
||||
{
|
||||
public:
|
||||
explicit UnusedVariableCheck( ASTContext& context );
|
||||
void run();
|
||||
bool VisitNamedDecl( NamedDecl* declaration );
|
||||
private:
|
||||
DiagnosticBuilder report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc );
|
||||
ASTContext& context;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
Reference in New Issue
Block a user