Replace c cast to c++ style cast

245 views
Skip to first unread message

skeept

unread,
Feb 26, 2016, 3:44:03 PM2/26/16
to vim_use
There is a switch in g++ that warns about c style casts.
I will try to fix those but there are plenty in the project where I am working.

Can someone recommend a function that would allow to do this in vim?

Or outside vim, can anyone recommend a tool that would be useful in doing this?

Thank you!

Dominique Pellé

unread,
Feb 26, 2016, 9:43:56 PM2/26/16
to Vim List
Doing it automatically seems more harmful than useful.

Ideally you should change the code to avoid the cast if possible.
And if the cast is really needed, then you need to chose the
correct c++ cast: static_cast, const_cast, reinterpret_case or dynamic_cast.
It requires thoughts to select the proper cast and so it should not
be automated.

Regards
Dominique

Luc Hermitte

unread,
Feb 26, 2016, 10:09:47 PM2/26/16
to vim use
Hi,

> There is a switch in g++ that warns about c style casts.
> I will try to fix those but there are plenty in the project where I
> am working.
>
> Can someone recommend a function that would allow to do this in vim?

In lh-cpp (*), I have 3 mappings that transform C casts into C++ casts -- and 6 more to cast an expression.

This has to be done manually: you'll have to know if you want a const_cast, a static_cast, or a reinterpret_cast. As Dominique said, it can't really be done automatically. May be, clang could automate many things, but it won't be able to know whether you want to downcast with a dynamic_cast or with a static_cast?

(*) https://github.com/LucHermitte/lh-cpp

HTH,

--
Luc Hermitte

skeept

unread,
Feb 28, 2016, 12:04:38 AM2/28/16
to vim_use
Hi Dominique and Luc,

thanks you for the advice. I agree that it is not a good idea to blindly replace everything. The most common case for me is probably static_cast so I would do that by default.


> In lh-cpp (*), I have 3 mappings that transform C casts into C++ casts -- and 6 more to cast an expression.
>
> This has to be done manually: you'll have to know if you want a const_cast, a static_cast, or a reinterpret_cast. As Dominique said, it can't really be done automatically. May be, clang could automate many things, but it won't be able to know whether you want to downcast with a dynamic_cast or with a static_cast?
>
> (*) https://github.com/LucHermitte/lh-cpp
>


Luc, I did install your library but the mapping provided ,,sc didn't do anything for me.
I did have an error when entering a c++ buffer after loading the plugin with VAM, something like:

line 39:
E227: mapping already exists for <80><fc>^D

I am not sure this is the reason I cannot use the mapping provided in your plugin.


I did write a function and mapping that for now should suit my needs:

function! ToStatic1()
" (type) b -> static_cast<type>(b)
" it is assumed cursor is inside the first cast

let original_keyword = &iskeyword
setlocal iskeyword +=_
setlocal iskeyword +=[
setlocal iskeyword +=]
setlocal iskeyword +=:

normal di(
let @a = @"
normal da(
normal istatic_cast<>
normal h
normal "ap
normal f>l
while 1
"get current char
let c = getline('.')[col('.')-1]
if c == ' '
normal x
else
break
endif
endwhile
echom c
if c != '('
normal i(
normal e
normal a)
endif

let &iskeyword=original_keyword
endfunction

nnoremap <silent> ,a :call ToStatic1()<CR>


Thanks,
Jorge

Luc Hermitte

unread,
Feb 28, 2016, 8:45:16 AM2/28/16
to vim use
Hi Jorge,

> Luc, I did install your library but the mapping provided ,,sc didn't
> do anything for me.

You have to select the C cast expression before hitting ,,sc. Depending on your leaderleader, it could be something else.

> I did have an error when entering a c++ buffer after loading the
> plugin with VAM, something like:
>
> line 39:
> E227: mapping already exists for <80><fc>^D

Unfortunately, I have to idea which key <80><fc> refers to. It's line 39 from which file? Once you identify it you should be able to provide another keybinding in your .vimrc (or may be in a ftplugin with higher priority)


> I am not sure this is the reason I cannot use the mapping provided in
> your plugin.
>
>
> I did write a function and mapping that for now should suit my needs:

It could be simpler:

" From ftplugin/cpp/cpp_snippets.vim
vnoremap <silent> <buffer> <LocalLeader><LocalLeader>sc
\ <c-\><c-n>:'<,'>call <sid>ConvertToCPPCast('static_cast')<cr>
nmap <silent> <buffer> <LocalLeader><LocalLeader>sc viw<LocalLeader><LocalLeader>sc


function! s:ConvertToCPPCast(cast_type) abort
" Extract text to convert
let save_a = @a
silent normal! gv"ay
" Strip the possible brackets around the expression
" matchlist seems to cause an odd error on multiline C cast expressions: it
" have the fucntion called again.
let [all, type, expr ; tail] = matchlist(@a, '\v^\(\_s*(.{-})\_s*\)\_s*(.{-})\_s*$')
let expr = substitute(expr, '\v^\(\s*(.{-})\s*\)$', '\1', '')
"
" Build the C++-casting from the C casting
let new_cast = a:cast_type.'<'.type.'>('.expr.')'
" Do the replacement
silent exe "normal! gvs".new_cast."\<esc>"
let @a = save_a
endfunction


--
Luc Hermitte
Reply all
Reply to author
Forward
0 new messages