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

Marshall Clow mclow.lists at gmail.com
Mon Feb 4 16:55:45 PST 2013

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

More information about the cfe-dev mailing list