[cfe-dev] Static Checker: getting svals for a struct field value

Alexey Sidorin via cfe-dev cfe-dev at lists.llvm.org
Mon May 9 14:15:46 PDT 2016


Hello Raymond,

1. You can get the Store with ProgramState::getStore() method().
2. To process an SVal representing a region, you may use 
SVal::getAsRegion() method. But I'm not sure what you really need here, 
could you explain more detailedly?
3. Your approach for FieldRegion will work, but it is better to search 
for a FieldDecl in your RecordDecl first and only then get its 
FieldRegion. FieldDecl has getName() method allowing doing this, so your 
code will look like:

for (const auto *F : structRD->fields()) {
   if (F->getName == "fieldName") {

     FieldRegion *FR = MRMgr.getFieldRegion(F, structReg);

     size = State->getSVal(FR);

}
}


where State is of ProgramStateRef type.


09.05.2016 21:55, McDowell, Raymond C. via cfe-dev пишет:
>
> I am new to building Clang static checkers and need some help.  I am 
> implementing a PreCall callback function that checks calls to a 
> certain family of functions (from a local library) that takes a struct 
> as an argument.  I see how to get the SVal and Expr for the argument 
> using CallEvent::getArgSVal() and CallEvent::getArgExpr().  What I’m 
> struggling with is how to go from the SVal for the struct to an SVal 
> for one of its fields.  In particular, how do I get the memory region 
> associated with the struct value?  Once I have that, I think I know 
> how to go from there, e.g.:
>
> RecordType *structTy = structReg->getValueType()->getAsStructureType();
>
> assert(structTy && “Value is not a structure”);
>
> RecordDecl *structRD = structTy ->getDecl()->getDefinition();
>
> assert(structRD && “structure has no definition”);
>
> for (const auto *F : structRD->fields()) {
>
>     FieldRegion *FR = MRMgr.getFieldRegion(F, structReg);
>
>     if (FR->getDec()->getName() == “fieldName”)
>
>         size = StoreMgr.getBinding(store, loc::MemRegionVal(FR));
>
> }
>
> But how do I get the memory region structReg?  And how do I get the 
> Store store?
>
> Thanks!
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160510/45effb43/attachment.html>


More information about the cfe-dev mailing list