Kaydet (Commit) fc809193 authored tarafından Noel Grandin's avatar Noel Grandin

fix loplugin rewriter source range checking

after
    commit 94ab8e43
    Date:   Fri Feb 9 15:28:41 2018 +0200
    improve loplugin rewriter double source modification detection

Change-Id: Ibf0a64fe4cc3dd6bf5ae16672b3d748a842196e4
üst acd16c7e
...@@ -397,9 +397,15 @@ bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAf ...@@ -397,9 +397,15 @@ bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAf
assert( rewriter ); assert( rewriter );
if (wouldRewriteWorkdir(Loc)) if (wouldRewriteWorkdir(Loc))
return false; return false;
SourceRange Range(SourceRange(Loc, Loc.getLocWithOffset(Str.size())));
if( !handler.checkOverlap( Range ) )
{
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", Range.getBegin());
return false;
}
if( rewriter->InsertText( Loc, Str, InsertAfter, indentNewLines )) if( rewriter->InsertText( Loc, Str, InsertAfter, indentNewLines ))
return reportEditFailure( Loc ); return reportEditFailure( Loc );
handler.addSourceModification(SourceRange(Loc, Loc.getLocWithOffset(Str.size()))); handler.addSourceModification(Range);
return true; return true;
} }
...@@ -408,9 +414,15 @@ bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str ) ...@@ -408,9 +414,15 @@ bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str )
assert( rewriter ); assert( rewriter );
if (wouldRewriteWorkdir(Loc)) if (wouldRewriteWorkdir(Loc))
return false; return false;
SourceRange Range(SourceRange(Loc, Loc.getLocWithOffset(Str.size())));
if( !handler.checkOverlap( Range ) )
{
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", Range.getBegin());
return false;
}
if( rewriter->InsertTextAfter( Loc, Str )) if( rewriter->InsertTextAfter( Loc, Str ))
return reportEditFailure( Loc ); return reportEditFailure( Loc );
handler.addSourceModification(SourceRange(Loc, Loc.getLocWithOffset(Str.size()))); handler.addSourceModification(Range);
return true; return true;
} }
...@@ -419,9 +431,15 @@ bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str ) ...@@ -419,9 +431,15 @@ bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str )
assert( rewriter ); assert( rewriter );
if (wouldRewriteWorkdir(Loc)) if (wouldRewriteWorkdir(Loc))
return false; return false;
SourceRange Range(SourceRange(Loc, Loc.getLocWithOffset(Str.size())));
if( !handler.checkOverlap( Range ) )
{
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", Range.getBegin());
return false;
}
if( rewriter->InsertTextAfterToken( Loc, Str )) if( rewriter->InsertTextAfterToken( Loc, Str ))
return reportEditFailure( Loc ); return reportEditFailure( Loc );
handler.addSourceModification(SourceRange(Loc, Loc.getLocWithOffset(Str.size()))); handler.addSourceModification(Range);
return true; return true;
} }
...@@ -430,9 +448,15 @@ bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str ) ...@@ -430,9 +448,15 @@ bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str )
assert( rewriter ); assert( rewriter );
if (wouldRewriteWorkdir(Loc)) if (wouldRewriteWorkdir(Loc))
return false; return false;
SourceRange Range(SourceRange(Loc, Loc.getLocWithOffset(Str.size())));
if( !handler.checkOverlap( Range ) )
{
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", Range.getBegin());
return false;
}
if( rewriter->InsertTextBefore( Loc, Str )) if( rewriter->InsertTextBefore( Loc, Str ))
return reportEditFailure( Loc ); return reportEditFailure( Loc );
handler.addSourceModification(SourceRange(Loc, Loc.getLocWithOffset(Str.size()))); handler.addSourceModification(Range);
return true; return true;
} }
...@@ -457,7 +481,7 @@ bool RewritePlugin::removeText( CharSourceRange range, RewriteOptions opts ) ...@@ -457,7 +481,7 @@ bool RewritePlugin::removeText( CharSourceRange range, RewriteOptions opts )
if( !handler.checkOverlap( range.getAsRange() ) ) if( !handler.checkOverlap( range.getAsRange() ) )
{ {
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", range.getBegin()); report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", range.getBegin());
return true; return false;
} }
if( opts.flags & RemoveWholeStatement || opts.flags & RemoveAllWhitespace ) if( opts.flags & RemoveWholeStatement || opts.flags & RemoveAllWhitespace )
{ {
...@@ -520,7 +544,7 @@ bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, Stri ...@@ -520,7 +544,7 @@ bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, Stri
if( OrigLength != 0 && !handler.checkOverlap( Range ) ) if( OrigLength != 0 && !handler.checkOverlap( Range ) )
{ {
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", Start ); report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", Start );
return true; return false;
} }
if( rewriter->ReplaceText( Start, OrigLength, NewStr )) if( rewriter->ReplaceText( Start, OrigLength, NewStr ))
return reportEditFailure( Start ); return reportEditFailure( Start );
...@@ -538,7 +562,7 @@ bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr ) ...@@ -538,7 +562,7 @@ bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
if( !handler.checkOverlap( range ) ) if( !handler.checkOverlap( range ) )
{ {
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin()); report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin());
return true; return false;
} }
if( rewriter->ReplaceText( range, NewStr )) if( rewriter->ReplaceText( range, NewStr ))
return reportEditFailure( range.getBegin()); return reportEditFailure( range.getBegin());
...@@ -556,7 +580,7 @@ bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange ...@@ -556,7 +580,7 @@ bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange
if( !handler.checkOverlap( range ) ) if( !handler.checkOverlap( range ) )
{ {
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin()); report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin());
return true; return false;
} }
if( rewriter->ReplaceText( range, replacementRange )) if( rewriter->ReplaceText( range, replacementRange ))
return reportEditFailure( range.getBegin()); return reportEditFailure( range.getBegin());
......
...@@ -250,10 +250,17 @@ bool PluginHandler::checkOverlap(SourceRange range) ...@@ -250,10 +250,17 @@ bool PluginHandler::checkOverlap(SourceRange range)
if (p1 <= rPair.second && rPair.first <= p2) if (p1 <= rPair.second && rPair.first <= p2)
return false; return false;
} }
mvModifiedRanges.emplace_back(p1, p2);
return true; return true;
} }
void PluginHandler::addSourceModification(SourceRange range)
{
SourceManager& SM = compiler.getSourceManager();
char const *p1 = SM.getCharacterData( range.getBegin() );
char const *p2 = SM.getCharacterData( range.getEnd() );
mvModifiedRanges.emplace_back(p1, p2);
}
void PluginHandler::HandleTranslationUnit( ASTContext& context ) void PluginHandler::HandleTranslationUnit( ASTContext& context )
{ {
if( context.getDiagnostics().hasErrorOccurred()) if( context.getDiagnostics().hasErrorOccurred())
......
...@@ -60,7 +60,7 @@ public: ...@@ -60,7 +60,7 @@ public:
// If we overlap with a previous area we modified, we cannot perform this change // If we overlap with a previous area we modified, we cannot perform this change
// without corrupting the source // without corrupting the source
bool checkOverlap(SourceRange range); bool checkOverlap(SourceRange range);
bool addSourceModification(SourceRange range); void addSourceModification(SourceRange range);
private: private:
void handleOption( const std::string& option ); void handleOption( const std::string& option );
void createPlugins( std::set< std::string > rewriters ); void createPlugins( std::set< std::string > rewriters );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment