convenience functions for source rewriters
Change-Id: I36e2b49bc615db0b12b03ffa755fa51acc6830a0
This commit is contained in:
@@ -22,7 +22,7 @@ namespace loplugin
|
||||
{
|
||||
|
||||
LclStaticFix::LclStaticFix( ASTContext& context, Rewriter& rewriter )
|
||||
: Plugin( context ), rewriter( rewriter )
|
||||
: RewritePlugin( context, rewriter )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -46,12 +46,7 @@ bool LclStaticFix::VisitFunctionDecl( FunctionDecl* declaration )
|
||||
return true;
|
||||
if( name.compare( 0, 4, "lcl_" ) != 0 )
|
||||
return true;
|
||||
if( rewriter.InsertText( declaration->getLocStart(), "static " ))
|
||||
{ // the logic is backwards, true here meant it failed, so report
|
||||
report( DiagnosticsEngine::Warning,
|
||||
"cannot fix lcl_ function (result of macro expansion?) [loplugin]",
|
||||
declaration->getLocStart());
|
||||
}
|
||||
insertText( declaration->getLocStart(), "static " );
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -18,14 +18,12 @@ namespace loplugin
|
||||
|
||||
class LclStaticFix
|
||||
: public RecursiveASTVisitor< LclStaticFix >
|
||||
, public Plugin
|
||||
, public RewritePlugin
|
||||
{
|
||||
public:
|
||||
explicit LclStaticFix( ASTContext& context, Rewriter& rewriter );
|
||||
void run();
|
||||
bool VisitFunctionDecl( FunctionDecl* declaration );
|
||||
private:
|
||||
Rewriter& rewriter;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
@@ -52,6 +52,92 @@ bool Plugin::ignoreLocation( SourceLocation loc )
|
||||
return context.getSourceManager().isInSystemHeader( context.getSourceManager().getExpansionLoc( loc ));
|
||||
}
|
||||
|
||||
|
||||
RewritePlugin::RewritePlugin( ASTContext& context, Rewriter& rewriter )
|
||||
: Plugin( context )
|
||||
, rewriter( rewriter )
|
||||
{
|
||||
}
|
||||
|
||||
bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAfter, bool indentNewLines )
|
||||
{
|
||||
if( rewriter.InsertText( Loc, Str, InsertAfter, indentNewLines ))
|
||||
return reportEditFailure( Loc );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str )
|
||||
{
|
||||
if( rewriter.InsertTextAfter( Loc, Str ))
|
||||
return reportEditFailure( Loc );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str )
|
||||
{
|
||||
if( rewriter.InsertTextAfterToken( Loc, Str ))
|
||||
return reportEditFailure( Loc );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str )
|
||||
{
|
||||
if( rewriter.InsertTextBefore( Loc, Str ))
|
||||
return reportEditFailure( Loc );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::removeText( SourceLocation Start, unsigned Length, RewriteOptions opts )
|
||||
{
|
||||
if( rewriter.RemoveText( Start, Length, opts ))
|
||||
return reportEditFailure( Start );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::removeText( CharSourceRange range, RewriteOptions opts )
|
||||
{
|
||||
if( rewriter.RemoveText( range, opts ))
|
||||
return reportEditFailure( range.getBegin());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts )
|
||||
{
|
||||
if( rewriter.RemoveText( range, opts ))
|
||||
return reportEditFailure( range.getBegin());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr )
|
||||
{
|
||||
if( rewriter.ReplaceText( Start, OrigLength, NewStr ))
|
||||
return reportEditFailure( Start );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
|
||||
{
|
||||
if( rewriter.ReplaceText( range, NewStr ))
|
||||
return reportEditFailure( range.getBegin());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange )
|
||||
{
|
||||
if( rewriter.ReplaceText( range, replacementRange ))
|
||||
return reportEditFailure( range.getBegin());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RewritePlugin::reportEditFailure( SourceLocation loc )
|
||||
{
|
||||
DiagnosticsEngine& diag = context.getDiagnostics();
|
||||
diag.Report( loc, diag.getCustomDiagID( DiagnosticsEngine::Warning,
|
||||
"cannot perform source modification (macro expansion involved?) [loplugin]" ));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Class that manages all LO modules.
|
||||
*/
|
||||
|
@@ -39,7 +39,25 @@ class RewritePlugin
|
||||
public:
|
||||
explicit RewritePlugin( ASTContext& context, Rewriter& rewriter );
|
||||
protected:
|
||||
typedef Rewriter::RewriteOptions RewriteOptions;
|
||||
// These following insert/remove/replaceText functions map to functions
|
||||
// in clang::Rewriter, with two differences:
|
||||
// - they (more intuitively) return false on failure rather than true
|
||||
// - they report a warning when the change cannot be done
|
||||
bool insertText( SourceLocation Loc, StringRef Str,
|
||||
bool InsertAfter = true, bool indentNewLines = false );
|
||||
bool insertTextAfter( SourceLocation Loc, StringRef Str );
|
||||
bool insertTextAfterToken( SourceLocation Loc, StringRef Str );
|
||||
bool insertTextBefore( SourceLocation Loc, StringRef Str );
|
||||
bool removeText( SourceLocation Start, unsigned Length, RewriteOptions opts = RewriteOptions());
|
||||
bool removeText( CharSourceRange range, RewriteOptions opts = RewriteOptions());
|
||||
bool removeText( SourceRange range, RewriteOptions opts = RewriteOptions());
|
||||
bool replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr );
|
||||
bool replaceText( SourceRange range, StringRef NewStr );
|
||||
bool replaceText( SourceRange range, SourceRange replacementRange );
|
||||
Rewriter& rewriter;
|
||||
private:
|
||||
bool reportEditFailure( SourceLocation loc );
|
||||
};
|
||||
|
||||
inline
|
||||
|
Reference in New Issue
Block a user