[cfe-dev] how to use clang::ento::ProgramState::contains | static analyzer

Alexander Droste alexander.ra.droste at googlemail.com
Sun Apr 19 12:16:38 PDT 2015


In case anyone else is starting to use the analyzer and stumbles across 
this. There is no "contains" function for map like stated in the checker 
dev manual:
http://clang-analyzer.llvm.org/checker_dev_manual.html.
To check if the key is contained in a map one can simply use
get<YourMapName>. If get returns null the key is not contained.

This pattern is for example used in SimpleStreamChecker.cpp
like this:

const StreamState *SS = State->get<StreamMap>(FileDesc);
// if SS is null the key is not contained
if (SS && SS->isClosed()) {
     reportDoubleClose(FileDesc, Call, C);
     return;
}

-Alex

On 10.04.15 22:39, Alexander Droste wrote:
> Meanwhile I tried calling "contains" on the other available containers
> set & list after registering them with (REGISTER_SET_WITH_PROGRAMSTATE,
> REGISTER_LIST_...). For those types "contains" works without any problems.
>
> Is this a bug regarding map and the "contains" function? It seems like
> it. To reproduce the compile error it is sufficient to register a map with:
>
> REGISTER_MAP_WITH_PROGRAMSTATE(MapName, KeyType, ValueType)
> and then call
> // CheckerContext &context
> context.getState()->contains<MapName>(KeyVal);
>
> This should produce the compile error stated in my previous post.
>
>
> On 04.04.15 15:34, Alexander Droste wrote:
>> Hey everyone,
>>
>> while having a look at the SimpleStreamChecker example
>> in the checkers folder of the static analyzer I can't figure
>> out how to use the contains function.
>>
>> I would expect it to work like the get function so it could be called
>> like this:
>>
>> //...
>> SymbolRef FileDesc = Call.getArgSVal(0).getAsSymbol();
>> ProgramStateRef State = C.getState();
>> bool b = State->contains<StreamMap>(FileDesc);
>>
>> But trying to compile I get an error saying:
>> no member named 'Contains' in'clang::ento::ProgramStateTrait<(anonymous
>> namespace)::StreamMap>'...
>>
>> Is there another function I have to implement to get this working?
>>
>> --Alex



More information about the cfe-dev mailing list