[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-dev
Subject: [cfe-dev] Proposal: A "Const tool" for clang
From: Marshall Clow <mclow.lists () gmail ! com>
Date: 2013-02-05 0:55:45
Message-ID: EBCF0474-3FCB-4F7F-BC09-CA3EAEFBA581 () gmail ! com
[Download RAW message or body]
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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic