[cfe-dev] Finding out if an assignment is within If statement's predicate

Kirill Bobyrev via cfe-dev cfe-dev at lists.llvm.org
Sat Jul 30 14:20:22 PDT 2016


> On 30 Jul 2016, at 22:34, Himanshu <himanshu at utexas.edu> wrote:
> 
> Hi Kirill, 
> 
> I am not interested only in assignments within if condition, but just want to treat them differently.
That was exactly my point…

Just see some clang-tidy checks for examples.
> Specifically, I want to add a function/macro 
> after each assignment to an instance of a certain struct type. 
> Hence, I want an approach that can identify all the '=' assignments to that type, and if they are within if conditions then use a function call to <function_for_if_assign>(.... ) otherwise call <function_for_stmt_assign>(...). 
> 
> --
> Himanshu
> 
> On Sat, Jul 30, 2016 at 1:10 PM, Kirill Bobyrev via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
> Treating nodes of a single type differently depending on their specifics might be much easier with AST Matchers.
> 
> What you’re looking for is hasConditionVariableStatement [http://clang.llvm.org/docs/LibASTMatchersReference.html#hasConditionVariableStatement0Anchor <http://clang.llvm.org/docs/LibASTMatchersReference.html#hasConditionVariableStatement0Anchor>].
> 
> Generally, try to use RecursiveASTVisitor when what you want is exactly node traversal, i.e. almost no dispatching. Otherwise, use AST Matchers. They’re easy to use and yet very powerful.
> 
>> Kirill Bobyrev 
> 
>> On 30 Jul 2016, at 19:45, Himanshu via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
>> 
>> Hi All, 
>> 
>> In my VisitBinaryOperator I want to treat assignments in IfStmt's predicate check part differently 
>> than the assignments in its body. For example: 
>> 
>> if( (*ptrI = malloc(sizeof(int) * 10)) == NULL)  // => this assignment is within predicate 
>>         msg = "success"; // assignment in body (treat it differently than the above)
>> else
>>         msg = "fail"; // assignment in body
>> 
>> 
>> Here, I would like to handle the ptrI = malloc assignment differently than the other two assignments of 'msg'. I tries setting a flag in VisitIfStmt, but that can't distinguish between the two different kind of assignments. 
>> 
>> Thanks!
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <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/20160730/bf8611ca/attachment-0001.html>


More information about the cfe-dev mailing list