1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* Based on LLVM/Clang.
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
/*
This is a rewriter.
It changes the SvStream operator<< calls into calls to more explicitly named
methods, which reduces the casting needed, and makes it less likely that we
will accidentally write data to a file using the wrong data-type-size.
TODO we don't currently cope with macro expansion e.g. if the constant on the RHS is a #define
TODO we don't currently cope with code like "(*this) << 1;"
TODO we don't currently cope with code like "aStream << x << endl;" the "endl" parts ends up dangling.
TODO we don't currently cope with custom overloads of operator<< in some of the use-sites.
*/
#include "plugin.hxx"
#include <clang/Lex/Lexer.h>
#include <iostream>
namespace loplugin
{
class SvStreamOutputOperators
: public loplugin::FilteringRewritePlugin< SvStreamOutputOperators >
{
public:
explicit SvStreamOutputOperators( InstantiationData const & data );
virtual void run() override;
bool VisitCallExpr( const CallExpr* call );
private:
SourceLocation after(const SourceLocation& loc);
};
SvStreamOutputOperators::SvStreamOutputOperators( InstantiationData const & data )
: FilteringRewritePlugin( data )
{
}
void SvStreamOutputOperators::run()
{
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool SvStreamOutputOperators::VisitCallExpr( const CallExpr* callExpr )
{
if( ignoreLocation( callExpr ))
return true;
if( callExpr->getNumArgs() < 2 )
return true;
const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( callExpr->getCalleeDecl() );
if ( func == NULL )
return true;
if( func->getNumParams() != 1 )
return true;
string qualifiedName = func->getQualifiedNameAsString();
bool bOutputOperator;
if( qualifiedName == "SvStream::operator<<" )
bOutputOperator = true;
else if( qualifiedName == "SvStream::operator>>" )
bOutputOperator = false;
else
return true;
string arg0 = func->getParamDecl( 0 )->getType().getAsString();
string newIOMethod;
if (bOutputOperator)
{
if( arg0 == "sal_uInt16" )
newIOMethod = "WriteUInt16";
else if( arg0 == "sal_uInt32" )
newIOMethod = "WriteUInt32";
else if( arg0 == "sal_uInt64" )
newIOMethod = "WriteUInt64";
else if( arg0 == "sal_Int16" )
newIOMethod = "WriteInt16";
else if( arg0 == "sal_Int32" )
newIOMethod = "WriteInt32";
else if( arg0 == "sal_Int64" )
newIOMethod = "WriteInt64";
else if( arg0 == "sal_uInt8" )
newIOMethod = "WriteUInt8";
else if( arg0 == "sal_Unicode" )
newIOMethod = "WriteUnicode";
else if( arg0 == "rtl::OString" )
newIOMethod = "WriteOString";
else if( arg0 == "bool" )
newIOMethod = "WriteBool";
else if( arg0 == "signed char" )
newIOMethod = "WriteSChar";
else if( arg0 == "char" )
newIOMethod = "WriteChar";
else if( arg0 == "unsigned char" )
newIOMethod = "WriteUChar";
else if( arg0 == "float" )
newIOMethod = "WriteFloat";
else if( arg0 == "double" )
newIOMethod = "WriteDouble";
else if( arg0 == "const double &" )
newIOMethod = "WriteDouble";
else if( arg0 == "const char *" )
newIOMethod = "WriteCharPtr";
else if( arg0 == "char *" )
newIOMethod = "WriteCharPtr";
else if( arg0 == "const unsigned char *" )
newIOMethod = "WriteUCharPtr";
else if( arg0 == "unsigned char *" )
newIOMethod = "WriteUCharPtr";
else if( arg0 == "class SvStream &" )
newIOMethod = "WriteStream";
else
{
report( DiagnosticsEngine::Warning,
"found call to operator<< that I cannot convert with type: " + arg0,
callExpr->getLocStart());
return true;
}
}
else
{
if( arg0 == "sal_uInt16 &" )
newIOMethod = "ReadUInt16";
else if( arg0 == "sal_uInt32 &" )
newIOMethod = "ReadUInt32";
else if( arg0 == "sal_uInt64 &" )
newIOMethod = "ReadUInt64";
else if( arg0 == "sal_Int16 &" )
newIOMethod = "ReadInt16";
else if( arg0 == "sal_Int32 &" )
newIOMethod = "ReadInt32";
else if( arg0 == "sal_Int64 &" )
newIOMethod = "ReadInt64";
else if( arg0 == "sal_uInt8 &" )
newIOMethod = "ReadUInt8";
else if( arg0 == "signed char &" )
newIOMethod = "ReadSChar";
else if( arg0 == "char &" )
newIOMethod = "ReadChar";
else if( arg0 == "unsigned char &" )
newIOMethod = "ReadUChar";
else if( arg0 == "float &" )
newIOMethod = "ReadFloat";
else if( arg0 == "double &" )
newIOMethod = "ReadDouble";
else if( arg0 == "class SvStream &" )
newIOMethod = "ReadStream";
else
{
report( DiagnosticsEngine::Warning,
"found call to operator>> that I cannot convert with type: " + arg0,
callExpr->getLocStart());
return true;
}
}
// CallExpr overrides the children() method from Stmt, but not the const variant of it, so we need to cast const away.
StmtRange range = const_cast<CallExpr*>(callExpr)->children();
const Stmt* child1 = *range; // ImplicitCastExpr
++range;
const Stmt* child2 = *range; // ImplicitCastExpr
if( dyn_cast_or_null< UnaryOperator >( child2 ) != NULL )
{
// remove the "*" before the stream variable
if( !replaceText( callExpr->getLocStart(), 1, "" ) )
return true;
if( !replaceText( child1->getLocStart().getLocWithOffset(-1), 4, "->" ) )
return true;
}
else
{
if( !replaceText( child1->getLocStart().getLocWithOffset(-1), 4, "." ) )
return true;
}
if( !insertTextBefore( callExpr->getArg( 1 )->getLocStart(), newIOMethod + "( " ) )
return true;
if( !insertTextAfter( after( callExpr->getLocEnd() ), " )" ) )
return true;
//TODO for some reason this is currently removing too much text
// if there was a cast e.g. "r << (sal_Int32) 1", then remove the cast
// const CStyleCastExpr* cast = dyn_cast_or_null< CStyleCastExpr >( callExpr->getArg(1) );
// if (cast != NULL)
// {
// replaceText( SourceRange( cast->getLParenLoc(), cast->getRParenLoc() ), "" );
// }
// if there was already parentheses around the expression, remove them
const ParenExpr* paren = dyn_cast_or_null< ParenExpr >( callExpr->getArg(1) );
if (paren != NULL)
{
if( !replaceText( paren->getLocStart(), 1, "" ) )
return true;
if( !replaceText( paren->getLocEnd(), 1, "" ) )
return true;
}
// report( DiagnosticsEngine::Note, "found", callExpr->getLocStart());
return true;
}
SourceLocation SvStreamOutputOperators::after( const SourceLocation& loc )
{
return Lexer::getLocForEndOfToken( loc, 0, compiler.getASTContext().getSourceManager(), compiler.getASTContext().getLangOpts() );
}
static Plugin::Registration< SvStreamOutputOperators > X( "svstreamoutputoperators" );
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|