[cfe-dev] Proposal: A "Const tool" for clang

Laszlo Nagy rizsotto.mailinglist at gmail.com
Tue Feb 5 02:04:51 PST 2013


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 at 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 at 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 at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130205/8ea30b85/attachment.html>


More information about the cfe-dev mailing list