convert conditional expression to string

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

convert conditional expression to string

Oleg Smolsky via cfe-dev
Hi all,

How can I get string representations of conditional expressions of IfStmt?
The problem is as follows:
For example;
=============
/* source code */
if( x < 10) …
=============

“IS->getCond()->getSourceRange()” represents only “x <“. It seems quite strange.

Thanks,
Kihong
_______________________________________________
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: convert conditional expression to string

Oleg Smolsky via cfe-dev
Hi Kihong,
The problem in your particular case is that the begin location of
integer literal is reported as the end location (should we fix it in AST)?
As a workaround, you can try to use Lexer::getLocForEndOfToken() to get
the token end location for the location reported by getLocEnd().

02.12.2018 8:36, Kihong Heo via cfe-dev пишет:

> Hi all,
>
> How can I get string representations of conditional expressions of IfStmt?
> The problem is as follows:
> For example;
> =============
> /* source code */
> if( x < 10) …
> =============
>
> “IS->getCond()->getSourceRange()” represents only “x <“. It seems quite strange.
>
> Thanks,
> Kihong
> _______________________________________________
> 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: convert conditional expression to string

Oleg Smolsky via cfe-dev
On Sat, 1 Dec 2018, 22:16 Alexey Sidorin via cfe-dev <[hidden email] wrote:
Hi Kihong,
The problem in your particular case is that the begin location of
integer literal is reported as the end location (should we fix it in AST)?

No, that's just how clang represents source ranges: source ranges are inclusive and source locations are the locations of tokens. So the start and end location of an integer literal should be the same, and the user needs to find the end of the last token themselves.

As a workaround, you can try to use Lexer::getLocForEndOfToken() to get
the token end location for the location reported by getLocEnd().

02.12.2018 8:36, Kihong Heo via cfe-dev пишет:
> Hi all,
>
> How can I get string representations of conditional expressions of IfStmt?
> The problem is as follows:
> For example;
> =============
> /* source code */
> if( x < 10) …
> =============
>
> “IS->getCond()->getSourceRange()” represents only “x <“. It seems quite strange.
>
> Thanks,
> Kihong
> _______________________________________________
> 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

_______________________________________________
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: convert conditional expression to string

Oleg Smolsky via cfe-dev
Thanks.

It seems Expr::getSourceRange() returns the range of the first token.
Then, in this particular case, what is the most common way to get the last token of the expression? How can I get “x < 10”?

Best,
Kihong

2018. 12. 2. 오후 1:30, Richard Smith <[hidden email]> 작성:

On Sat, 1 Dec 2018, 22:16 Alexey Sidorin via cfe-dev <[hidden email] wrote:
Hi Kihong,
The problem in your particular case is that the begin location of 
integer literal is reported as the end location (should we fix it in AST)?

No, that's just how clang represents source ranges: source ranges are inclusive and source locations are the locations of tokens. So the start and end location of an integer literal should be the same, and the user needs to find the end of the last token themselves.

As a workaround, you can try to use Lexer::getLocForEndOfToken() to get 
the token end location for the location reported by getLocEnd().

02.12.2018 8:36, Kihong Heo via cfe-dev пишет:

> Hi all,
>
> How can I get string representations of conditional expressions of IfStmt?
> The problem is as follows:
> For example;
> =============
> /* source code */
> if( x < 10) …
> =============
>
> “IS->getCond()->getSourceRange()” represents only “x <“. It seems quite strange.
>
> Thanks,
> Kihong
> _______________________________________________
> 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


_______________________________________________
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: convert conditional expression to string

Oleg Smolsky via cfe-dev
Hello!

There is a cool tutorial how to work with Clang Tidy:
https://youtu.be/1S2A0VWGOws (by Daniel Jasper and Manuel Klimek)

I have already implemented this based on the video.

Clang Tidy version:

static StringRef exprToStr(const Expr *E,
                           const MatchFinder::MatchResult &Result) {
  return Lexer::getSourceText(
      CharSourceRange::getTokenRange(E->getSourceRange()),
      *Result.SourceManager, Result.Context->getLangOpts(), 0);
}


Static Analyzer version: 

static StringRef exprToStr(const Expr *E, BugReporterContext &BRC) {
  return Lexer::getSourceText(
      CharSourceRange::getTokenRange(E->getSourceRange()),
      BRC.getSourceManager(), BRC.getASTContext().getLangOpts(), 0);
}

2018. dec. 6. 5:39 ezt írta ("Kihong Heo via cfe-dev" <[hidden email]>):
Thanks.

It seems Expr::getSourceRange() returns the range of the first token.
Then, in this particular case, what is the most common way to get the last token of the expression? How can I get “x < 10”?

Best,
Kihong

2018. 12. 2. 오후 1:30, Richard Smith <[hidden email]> 작성:

On Sat, 1 Dec 2018, 22:16 Alexey Sidorin via cfe-dev <[hidden email] wrote:
Hi Kihong,
The problem in your particular case is that the begin location of 
integer literal is reported as the end location (should we fix it in AST)?

No, that's just how clang represents source ranges: source ranges are inclusive and source locations are the locations of tokens. So the start and end location of an integer literal should be the same, and the user needs to find the end of the last token themselves.

As a workaround, you can try to use Lexer::getLocForEndOfToken() to get 
the token end location for the location reported by getLocEnd().

02.12.2018 8:36, Kihong Heo via cfe-dev пишет:

> Hi all,
>
> How can I get string representations of conditional expressions of IfStmt?
> The problem is as follows:
> For example;
> =============
> /* source code */
> if( x < 10) …
> =============
>
> “IS->getCond()->getSourceRange()” represents only “x <“. It seems quite strange.
>
> Thanks,
> Kihong
> _______________________________________________
> 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

_______________________________________________
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