[PATCH] Keep track of the operator location of MemberExprs

Kim Gräsman kim.grasman at gmail.com
Mon Mar 2 11:51:27 PST 2015


Hi Joe,

I was just looking at some code in our tool that works around the lack
of the operator location, so I would welcome this!

I can't speak to the quality of the change, but the comments on our
code makes me think you're missing some tests:

// Unfortunately member_expr doesn't expose the location of the .  or
// ->.  If the base is implicit, there is no . or ->, and we just
// return the member loc.  Otherwise, we have to guess if the entire
// member-expression (all of 'b.m') is in a macro or not.  We look at
// getMemberLoc(), the start of the member ('m') , and
// getBase()->getLocEnd(), the end of the base ('b').  If they're both
// on the same line of the same file, then the . or -> must be there
// too, and return that as the location.  Otherwise, we assume that
// one or the other is in a macro, but the . or -> is not, and use the
// instantiation (not spelling) location of the macro.

- Tests with macros would be good, e.g.

   CALL_INSIDE_MACRO();
   CALL_INSIDE_MACRO_X(x);
   x.METHOD();
   THING.method();
   x.CALL_METHOD;

- I'm not entirely sure what the comment means by "if the base is
implicit", but I think it's refering to implicit this-calls or even
implicit base calls, e.g.

  struct Base {
    void foo() {}
    void bar() {
       foo();  // implicit this
    }
  };

  struct Derived : public Base {
    void baz() {
       bar();  // implicit base
    }
  }

- Kim

On Mon, Mar 2, 2015 at 4:12 PM, Joe Ranieri <joe at alacatialabs.com> wrote:
> While working on a refactoring tool, I ran into the problem not having
> the operator location on MemberExpr. clang-modernize also has this
> problem for its loop converter and tries to use the lexer to determine
> where the operator was. In my testing this didn't always give back the
> correct answer, so the attached patch makes clang retain the source
> location of the -> or . operator in the MemberExpr.
>
> -- Joe Ranieri
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>



More information about the cfe-commits mailing list