[cfe-dev] More extensive unused variable checks

Daniel B Mosesson dmosess1 at binghamton.edu
Tue Apr 16 06:43:50 PDT 2013


I was thinking about an set of attributes for STL containers (just to
make this manageable) that would identify the operations for the
container classes as either read or write (or both, as in the case of
indexers).
Is there a reason not to use attributes for this purpose?

The the same logic that is used in for load/store instructions could
potentially be applied, assuming that the abstractions that the
container provides are not violated.

Special cases:

1. The `insert` method of vector<T> throws a bit of a wrinkle into
matters (though I am confident that list of updated indices should be
relatively easy to maintain).
2. The `data()` member function (where applicable) will probably make
all further attempts an analysis fruitless.
3. The bitset `to_ulong()`,`to_string()`,`any()` and `none()`
functions should mark all bits both written and read
4. The deque<T>, queue<T> and stack<T> have issues similar to vector,
and can be resolved similarly
5. For forward_list<T> and list<T> I am not aware of a reasonably
efficient way of keeping track of dead store, unless no reading
methods are called at all

Notes:

1. Even though swap technically does a read, it should not count as a
read for this purpose
2. If clear occurs before all elements have been read, those elements
that have been stored should be considered dead stores
3. Iterators should probably be considered as reading all data

In short, a totally unused container is very easy to find and deal
with, but doing the individual element dead store elimination is
possible, but harder (at least for some containers).

On Mon, Apr 15, 2013 at 1:17 AM, David Blaikie <dblaikie at gmail.com> wrote:
> On Tue, Apr 9, 2013 at 3:14 PM, Daniel B Mosesson
> <dmosess1 at binghamton.edu> wrote:
>> Is there any work that is being done on making the unused variable
>> warning more extensive?
>>
>> for example:
>>
>> #include<vector>
>> void test() {
>>     std::vector<int> a;
>>     a.push_back(1);
>> }
>> int main() {
>>     test();
>>     return 0;
>> }
>>
>> Is there any work on saying that test() is a function that will never
>> do anything, and therefore can be optimized out (and throw a warning
>> that says that `a` is unused?
>>
>> I was thinking that perhaps there was a to achieve this with
>> generalized attributes on the containers and their functions to
>> determine if an operation was a "load" or "store" type of function,
>> and then objects that only call one of those two types of instructions
>> would throw a warning (either unused and can be optimized out, or will
>> cause undefined behavior).
>>
>> While I would expect this to be rather straightforward when it comes
>> to containers (assuming that the possibility of optimizing out
>> individual entries in the container is excluded), it would be also
>> interesting to see if a similar idea can be applied to other parts of
>> the STL.
>>
>> If there is no work that is being done on this already, is this
>> something that is
>> 1) useful
>
> Yes
>
>> 2) relatively simple
>
> So far as I know, no. As you say, this would probably require some
> attribute scheme to have a reasonable chance of quality. Did you have
> some other ideas in mind about how you might overcome this?
>
> - David



More information about the cfe-dev mailing list