[cfe-dev] (no subject)

Aitor San Juan aitor.sj at opendeusto.es
Thu May 1 09:53:35 PDT 2014


Thanks for the clarifications for cases #2 and #3.

For #1, you are right: there's an ImplicitCastExpr wrapping the
StringLiteral in the AST. I think this approach is cleaner than casting in
#2 and #3, so using what you suggested works like a charm:

const Expr *arg = (CE->getArg(0))->IgnoreParenImpCasts();

Thanks a lot!


2014-05-01 18:12 GMT+02:00 Jordan Rose <jordan_rose at apple.com>:

> The analyzer traffics in const Stmt * and const Expr * everywhere, so your
> temporary variable for #2 and #3 should be "const StringLiteral *SL" (or
> just "auto *SL" now that we're using C++11).
>
> The probable reason why this doesn't work is because there's probably an
> ImplicitCastExpr wrapping the StringLiteral, handling the decay from
> char[N] to char* (or const char *, or whatever). Depending on what you want
> to do, you may want to use Expr::IgnoreParenImpCasts to "look through"
> these kinds of filters.
>
> Jordan
>
>
> On May 1, 2014, at 4:31 , Aitor San Juan <aitor.sj at opendeusto.es> wrote:
>
> > Hello,
> >
> > In a checker I want to test whether an argument to a function call is a
> String literal, but I'm a bit stuck. Could anybody shed a bit of light on
> this?
> >
> > 1. This way seems to not work (although no compile-time error), but I
> don't understand why:
> >
> > const Expr *arg = CE->getArg(0); // CE is a CallExpr
> > if ((arg != NULL) && (clang::isa<clang::StringLiteral>(arg))){
> > ...
> >
> > 2. The following does not compile:
> >
> > const Expr *arg = CE->getArg(0); // CE is a CallExpr
> > if (StringLiteral *SL = dyn_cast<StringLiteral>(arg)) {
> >
> > invalid conversion from ‘llvm::cast_retty<clang::StringLiteral, const
> clang::Expr*>::ret_type {aka const clang::StringLiteral*}’ to
> ‘clang::StringLiteral*’ [-fpermissive]
> >     if (StringLiteral *SL = dyn_cast<StringLiteral>(arg)) {
> >
>    ^
> > 3. The following does not compile, however there's a non-const getArg()
> method:
> >
> > Expr *arg = CE->getArg(0); // CE is a CallExpr
> > if (StringLiteral *SL = dyn_cast<StringLiteral>(arg)) {
> >
> > error: invalid conversion from ‘const clang::Expr*’ to ‘clang::Expr*’
> [-fpermissive]
> >     Expr *arg = CE->getArg(0);
> >                             ^
> >
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140501/b0153407/attachment.html>


More information about the cfe-dev mailing list