Right parenthesis of a condition.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Right parenthesis of a condition.

Joerg Sonnenberger via cfe-dev
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,


--
Dorian Haglund

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Right parenthesis of a condition.

Joerg Sonnenberger via cfe-dev
Hi Dorian,

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

Regards, Alexey K

2018-02-28 12:11 GMT+03:00 Dorian Haglund via cfe-dev <[hidden email]>:
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,


--
Dorian Haglund

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




--

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Right parenthesis of a condition.

Joerg Sonnenberger via cfe-dev
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


2018-02-28 10:21 GMT+01:00 Alexey Knyshev <[hidden email]>:
Hi Dorian,

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

Regards, Alexey K

2018-02-28 12:11 GMT+03:00 Dorian Haglund via cfe-dev <[hidden email]>:
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,


--
Dorian Haglund

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




--



--
Dorian Haglund

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Right parenthesis of a condition.

Joerg Sonnenberger via cfe-dev
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

2018-02-28 14:08 GMT+03:00 Dorian Haglund <[hidden email]>:
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


2018-02-28 10:21 GMT+01:00 Alexey Knyshev <[hidden email]>:
Hi Dorian,

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

Regards, Alexey K

2018-02-28 12:11 GMT+03:00 Dorian Haglund via cfe-dev <[hidden email]>:
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,


--
Dorian Haglund

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev




--



--
Dorian Haglund



--

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev