Right parenthesis of a condition.

Hello,

I’m using the clang library to monitor (among other things) the if statements in my program.

i need to get the location of the right parenthesis of the condition of the if (and while) statements.

I’ve tried things such as:

auto condLocEnd = statement->getCond()->getLocEnd();
auto condLoc = Lexer::findLocationAfterToken(condLocEnd, tok::r_paren, sm, LangOptions(), false);

Unfortunately condLoc is invalid when the condition contains a macro. Eg: if(p == NULL). I can’t figure out how to get the location of the right parenthesis.

Do you know how to get it ? If it is not possible, could you explain why ?

Best regards,

Hi Dorian,

Hope it would be helpful:
https://stackoverflow.com/a/24223347

Regards, Alexey K

Thanks for your help Alexey.

It made me realize that I forgot a very important step of my problem:

Before giving the file to be monitored to clang, we preprocess the source with gcc.

Which means that is if the original source file /tmp/test.c is:

#include <stdlib.h>

int main()
{
int* a = 0;
if (a != NULL)
return 1;
return 0;
}

Clang would receive (I only pasted the last 15 lines) :

2 “/tmp/test.c” 2

3 “/tmp/test.c”

int main()
{
int* a = 0;
if (a !=

6 “/tmp/test.c” 3 4

((void *)0)

6 “/tmp/test.c”

)
return 1;
return 0;
}

In that case, I would like to get the location of the enclosing parenthesis for the ‘IfStmt’ (the one just before the ‘return 1;’). I tried to toy with the lexer to get it but with no success.
Do you have any idea ?

Best regards

What you get is RAW (preprocessed) source file with original line mappings. Clang should know how to deal with such marked files but probably you have to tweak something (flags or whatever). Anyway, I’m not expect in clang default build pipeline ( preprocessing => compilation => linking) so probably someone with such experience could shed a bit more light on it.

Regards, Alexey K