[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-dev
Subject: Re: [cfe-dev] Proposal: A "Const tool" for clang
From: Laszlo Nagy <rizsotto.mailinglist () gmail ! com>
Date: 2013-02-05 10:04:51
Message-ID: CAN=tS+G_Y5EwGoCaYbs_iZhRBpp+4KnfHxThyOtKXH3cciyfpA () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Hi Marshall,
as you noticed I had an attempt to write such a tool. It is published,
contributors are welcome. <https://github.com/rizsotto/Constantine> My
goals were very similar what you are proposing here. I see these points are
the major differences:
- do topological sort on the call graph, and fix function signature in that
order is a good idea, (got smaller problems to fix first.)
- do rewrite the code instead of just print warning would be a great
improvement,
- write this tool separately from LLVM/Clang source tree.
And there is another interesting approach to solve the constness problem. <
https://bitbucket.org/grrussel/constcpp/wiki/Home>
Regards,
Laszlo
On Tue, Feb 5, 2013 at 1:55 AM, Marshall Clow <mclow.lists@gmail.com> wrote:
> This is the first cut of a proposal for a refactoring tool built on clang.
> I have tried to break out what I think are the important issues - but this
> is only a first cut.
>
> The entire proposal is at <
> http://marshall.calepin.co/a-const-tool-for-llvm.html>, only because I
> think it's a lot easier to read with formatting.
>
> Here's the first section:
>
> == Motivation ==
> Adapting an existing code base to use `const` can be a daunting,
> frustrating task. Simply adding "const" to a `Foo &` parameter can have
> repercussions throughout the code base, and may involve adding const to an
> arbitrary number of other parameters (of other functions), or, after making
> changes in several places, finding that you have to back all (or most) of
> your changes out.
>
> Consider the code snippet:
>
> void ncfunc1 ( std::string &str ) { str.append ( ' ' ); }
> int cfunc1 ( std::string &str ) { return str.length(); }
>
> void function1 ( std::string &one, std::string& two ) {
> if ( cfunc1 ( one ) > 10 )
> ncfunc ( two );
> }
>
> * Changing the first parameter to function1 to be `const
> std::string &` also requires changing `cfunc1` to take a `const std::string
> &`
>
> * You cannot change the second parameter because it gets passed to
> `ncfunc1`, which cannot be modified to take a `const std::string &`,
> because it calls the non-const member function `append` using that object.
> (and `std::string::append(char)` cannot be made const, because it is part
> of the standard library)
>
> Broadly speaking, there are three places where const can be added to an
> existing code base:
> * Parameters that are passed by reference or pointer can be passed
> by const pointer or const reference.
> * object methods can be marked as const
> * Return values that are returned by pointer or reference can be
> returned by const reference or const pointer. This may involve adding an
> additional function alongside the original, differing only in the return
> value.
>
>
> Read the whole thing at: <
> http://marshall.calepin.co/a-const-tool-for-llvm.html>, and send me
> comments, please - on list or off.
> If you know of tools that already do this, I'd love to know about them,
> too.
>
> -- Marshall
>
> Marshall Clow Idio Software <mailto:mclow.lists@gmail.com>
>
> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is
> promptly moderated down to (-1, Flamebait).
> -- Yu Suzuki
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev@cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
[Attachment #5 (text/html)]
<div dir="ltr">Hi Marshall,<div><br></div><div style>as you noticed I had an attempt \
to write such a tool. It is published, contributors are welcome. <<a \
href="https://github.com/rizsotto/Constantine">https://github.com/rizsotto/Constantine</a>> \
My goals were very similar what you are proposing here. I see these points are the \
major differences:</div> <div style><br></div><div style>- do topological sort on the \
call graph, and fix function signature in that order is a good idea, (got smaller \
problems to fix first.)</div><div style>- do rewrite the code instead of just print \
warning would be a great improvement,</div> <div style>- write this tool separately \
from LLVM/Clang source tree.</div><div style><br></div><div style>And there is \
another interesting approach to solve the constness problem. <<a \
href="https://bitbucket.org/grrussel/constcpp/wiki/Home">https://bitbucket.org/grrussel/constcpp/wiki/Home</a>></div>
<div style><br></div><div style>Regards,</div><div style>Laszlo</div></div><div \
class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Feb 5, 2013 at 1:55 AM, \
Marshall Clow <span dir="ltr"><<a href="mailto:mclow.lists@gmail.com" \
target="_blank">mclow.lists@gmail.com</a>></span> wrote:<br> <blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">This is the first cut of a proposal for a refactoring tool \
built on clang.<br> I have tried to break out what I think are the important issues - \
but this is only a first cut.<br> <br>
The entire proposal is at <<a \
href="http://marshall.calepin.co/a-const-tool-for-llvm.html" \
target="_blank">http://marshall.calepin.co/a-const-tool-for-llvm.html</a>>, only \
because I think it's a lot easier to read with formatting.<br>
<br>
Here's the first section:<br>
<br>
== Motivation ==<br>
Adapting an existing code base to use `const` can be a daunting, frustrating task. \
Simply adding "const" to a `Foo &` parameter can have repercussions \
throughout the code base, and may involve adding const to an arbitrary number of \
other parameters (of other functions), or, after making changes in several places, \
finding that you have to back all (or most) of your changes out.<br>
<br>
Consider the code snippet:<br>
<br>
void ncfunc1 ( std::string &str ) { str.append ( ' ' ); \
}<br>
int cfunc1 ( std::string &str ) { return str.length(); }<br>
<br>
void function1 ( std::string &one, std::string& two ) {<br>
if ( cfunc1 ( one ) > 10 )<br>
ncfunc ( two );<br>
}<br>
<br>
* Changing the first parameter to function1 to be `const std::string &` \
also requires changing `cfunc1` to take a `const std::string &`<br> <br>
* You cannot change the second parameter because it gets passed to `ncfunc1`, \
which cannot be modified to take a `const std::string &`, because it calls the \
non-const member function `append` using that object. (and \
`std::string::append(char)` cannot be made const, because it is part of the standard \
library)<br>
<br>
Broadly speaking, there are three places where const can be added to an existing code \
base:<br>
* Parameters that are passed by reference or pointer can be passed by const \
pointer or const reference.<br>
* object methods can be marked as const<br>
* Return values that are returned by pointer or reference can be returned by \
const reference or const pointer. This may involve adding an additional function \
alongside the original, differing only in the return value.<br>
<br>
<br>
Read the whole thing at: <<a \
href="http://marshall.calepin.co/a-const-tool-for-llvm.html" \
target="_blank">http://marshall.calepin.co/a-const-tool-for-llvm.html</a>>, and \
send me comments, please - on list or off.<br>
If you know of tools that already do this, I'd love to know about them, too.<br>
<br>
-- Marshall<br>
<br>
Marshall Clow Idio Software <mailto:<a \
href="mailto:mclow.lists@gmail.com">mclow.lists@gmail.com</a>><br> <br>
A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly \
moderated down to (-1, Flamebait).<br>
-- Yu Suzuki<br>
<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" \
target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br> \
</blockquote></div><br></div>
_______________________________________________
cfe-dev mailing list
cfe-dev@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic