1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
15 Replaces all calls to the deprecated O(U)String::valueOf() .
25 : public loplugin::FilteringRewritePlugin
< ConvertValueOf
>
28 explicit ConvertValueOf( CompilerInstance
& compiler
, Rewriter
& rewriter
);
29 virtual void run() override
;
30 bool VisitCallExpr( const CallExpr
* call
);
32 void removeCast( const Expr
* arg
);
35 ConvertValueOf::ConvertValueOf( CompilerInstance
& compiler
, Rewriter
& rewriter
)
36 : FilteringRewritePlugin( compiler
, rewriter
)
40 void ConvertValueOf::run()
42 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
45 bool ConvertValueOf::VisitCallExpr( const CallExpr
* call
)
47 if( ignoreLocation( call
))
49 // Using getDirectCallee() here means that we find only calls
50 // that call the function directly (i.e. not using a pointer, for example).
51 // Use getCallee() to include also those :
52 // if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( call->getCalleeDecl()))
53 if( const FunctionDecl
* func
= call
->getDirectCallee())
55 // Optimize, getQualifiedNameAsString() is reportedly expensive,
56 // so first check fast details like number of arguments or the (unqualified)
57 // name before checking the fully qualified name.
58 // See FunctionDecl for all the API about the function.
59 if( func
->getIdentifier() != NULL
60 && ( func
->getName() == "valueOf" ))
62 string qualifiedName
= func
->getQualifiedNameAsString();
63 if( qualifiedName
== "rtl::OString::valueOf" )
65 // Further checks about arguments. Check mainly ParmVarDecl, VarDecl,
66 // ValueDecl and QualType for Clang API details.
67 string arg0
= func
->getParamDecl( 0 )->getType().getAsString();
68 if( arg0
== "sal_Bool" )
69 replaceText( call
->getCallee()->getSourceRange(), "OString::boolean" );
72 replaceText( call
->getCallee()->getSourceRange(), "OString::number" );
73 removeCast( call
->getArg( 0 ));
76 if( qualifiedName
== "rtl::OUString::valueOf" )
78 // Further checks about arguments. Check mainly ParmVarDecl, VarDecl,
79 // ValueDecl and QualType for Clang API details.
80 string arg0
= func
->getParamDecl( 0 )->getType().getAsString();
81 if( arg0
== "sal_Bool" )
82 replaceText( call
->getCallee()->getSourceRange(), "OUString::boolean" );
83 else if( arg0
== "sal_Unicode" )
84 replaceText( call
->getCallee()->getSourceRange(), "OUString" );
87 replaceText( call
->getCallee()->getSourceRange(), "OUString::number" );
88 removeCast( call
->getArg( 0 ));
96 void ConvertValueOf::removeCast( const Expr
* arg
)
98 arg
= arg
->IgnoreImpCasts();
99 if( const ExplicitCastExpr
* cast
= dyn_cast
< ExplicitCastExpr
>( arg
))
101 // Explicit casts don't seem to actually always change the type (integer promotion
102 // takes place first?), so remove also preceding implicit casts:
106 // |-CallExpr 0x1a84f20 <line:6:5, col:16> 'void'
107 // | |-ImplicitCastExpr 0x1a84f08 <col:5> 'void (*)(int)' <FunctionToPointerDecay>
108 // | | `-DeclRefExpr 0x1a84eb8 <col:5> 'void (int)' lvalue Function 0x1a58900 'f' 'void (int)'
109 // | `-CXXFunctionalCastExpr 0x1a84e90 <col:8, col:15> 'int' functional cast to int <NoOp>
110 // | `-ImplicitCastExpr 0x1a84e78 <col:13> 'int' <IntegralCast>
111 // | `-ImplicitCastExpr 0x1a84e60 <col:13> 'char' <LValueToRValue>
112 // | `-DeclRefExpr 0x1a58b88 <col:13> 'char' lvalue Var 0x1a58ab0 'a' 'char'
113 const Expr
* castFrom
= cast
->getSubExpr()->IgnoreImpCasts();
114 if( cast
->getType()->isIntegerType() && castFrom
->getType()->isIntegerType())
116 string fromType
= castFrom
->getType().getAsString();
117 if( fromType
!= "sal_Bool" && fromType
!= "bool" && fromType
!= "sal_Unicode" )
119 if( const CXXFunctionalCastExpr
* funcCast
= dyn_cast
< CXXFunctionalCastExpr
>( cast
))
121 removeText( CharSourceRange::getCharRange( funcCast
->getLocStart(),
122 compiler
.getSourceManager().getExpansionLoc( funcCast
->getSubExpr()->getLocStart())));
123 removeText( CharSourceRange::getCharRange( locationAfterToken(
124 compiler
.getSourceManager().getExpansionLoc( funcCast
->getSubExpr()->getLocEnd())),
125 locationAfterToken( funcCast
->getLocEnd())));
127 else if( const CXXNamedCastExpr
* namedCast
= dyn_cast
< CXXNamedCastExpr
>( cast
))
129 removeText( CharSourceRange::getCharRange( namedCast
->getLocStart(),
130 compiler
.getSourceManager().getExpansionLoc( namedCast
->getSubExpr()->getLocStart())));
131 removeText( CharSourceRange::getCharRange( locationAfterToken(
132 compiler
.getSourceManager().getExpansionLoc( namedCast
->getSubExpr()->getLocEnd())),
133 locationAfterToken( namedCast
->getLocEnd())));
135 else if( const CStyleCastExpr
* cCast
= dyn_cast
< CStyleCastExpr
>( cast
))
136 removeText( SourceRange( cCast
->getLocStart(), cCast
->getRParenLoc()));
144 static Plugin::Registration
< ConvertValueOf
> X( "convertvalueof" );
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */