Kaydet (Commit) 9cb21a33 authored tarafından Luboš Luňák's avatar Luboš Luňák

check for double modifications in compiler plugins

A different way to do 1c0669af2f1f58e6431b5e489ac48a883e242ba7.
Sometimes one piece of code can be represented several times in the AST,
e.g. with default function arguments.

Change-Id: Ic7799fa0bd918a638bdc8ebef69e6aa91d355bdc
üst d870271c
......@@ -110,6 +110,9 @@ bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str )
// given to this overload would not match afterwards.
bool RewritePlugin::removeText( SourceLocation Start, unsigned Length, RewriteOptions opts )
{
if( removals.find( Start ) != removals.end())
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", Start );
removals.insert( Start );
if( opts.RemoveWholeStatement )
{
SourceRange range( Start, Start.getLocWithOffset( Length - 1 ));
......@@ -125,6 +128,9 @@ bool RewritePlugin::removeText( SourceLocation Start, unsigned Length, RewriteOp
bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts )
{
if( removals.find( range.getBegin()) != removals.end())
report( DiagnosticsEngine::Warning, "double code removal, possible plugin error", range.getBegin());
removals.insert( range.getBegin());
if( opts.RemoveWholeStatement )
{
if( !adjustForWholeStatement( &range ))
......@@ -166,6 +172,9 @@ bool RewritePlugin::adjustForWholeStatement( SourceRange* range )
bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr )
{
if( OrigLength != 0 && removals.find( Start ) != removals.end())
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", Start );
removals.insert( Start );
if( rewriter.ReplaceText( Start, OrigLength, NewStr ))
return reportEditFailure( Start );
return true;
......@@ -173,6 +182,9 @@ bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, Stri
bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
{
if( removals.find( range.getBegin()) != removals.end())
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin());
removals.insert( range.getBegin());
if( rewriter.ReplaceText( range, NewStr ))
return reportEditFailure( range.getBegin());
return true;
......@@ -180,6 +192,9 @@ bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange )
{
if( removals.find( range.getBegin()) != removals.end())
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", range.getBegin());
removals.insert( range.getBegin());
if( rewriter.ReplaceText( range, replacementRange ))
return reportEditFailure( range.getBegin());
return true;
......
......@@ -18,6 +18,7 @@
#include <clang/Basic/FileManager.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Frontend/CompilerInstance.h>
#include <set>
#if __clang_major__ < 3 || __clang_major__ == 3 && __clang_minor__ < 2
#include <clang/Rewrite/Rewriter.h>
......@@ -114,6 +115,7 @@ class RewritePlugin
enum { isRewriter = true };
bool reportEditFailure( SourceLocation loc );
bool adjustForWholeStatement( SourceRange* range );
set< SourceLocation > removals;
};
/**
......
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