[cfe-dev] Getting the Bindings of MatchFinder::MatchResult

Manuel Klimek klimek at google.com
Tue Aug 21 09:13:19 PDT 2012


On Fri, Aug 17, 2012 at 12:02 PM, Jens Weller <JensWeller at gmx.de> wrote:
> Hi,
>
> wasn't sure how to name this one, I have a nice idea:
> Building a Counting/Stats/Reporting Callback for ast_matchers.
>
> Background: I think this would be kind of useful, beeing able to run matchers against a class that can report what places in code are found. Without changing them.
> This gives me as a Tool Author the option of displaying to the user, where changes will be made to the code, or maybe even what changes.
>
> I thought this is an easy one, and once I'm done, I'll post it to the list.
> Well, counting is easy. But actually I want to know, where are those changes, which Files are affected. So, I'd like to have an vector<string> to record all found places.
>
> Now, as this Reporting Callback runs against matchers it does not know, and as far as I've understood, the Bindings of BoundNodes are userdefined strings. Which aren't exposed to me. I can query them, but actually I'd need in this case either access to them, or, maybe even better, some exposed information, maybe SourceLocation start,end would be already enough. I'd like to be able to report things like File, FileLocation (line, column) etc.

Well, you can for example hand the id's into the callback class via
the constructor.

> Would like to hear some thoughts on the idea and possible solutions.

I think in principle this is a nice idea, but currently I don't see a
strong need for putting that level of abstraction into a central place
- I'd like to see some more evidence that this is widely used.

For exploratory use cases I'd be more interested in seeing a dynamic
approach with a tool that can parse a matcher expression and then give
you all the locations where it matches.

Cheers,
/Manuel

> This is my Code sofar:
>
> namespace {
> class ReportingCallback : public MatchFinder::MatchCallback {
>  public:
>     ReportingCallback(){}
>   virtual void run(const MatchFinder::MatchResult &Result)
>     {
>         str.str("");//reset
>         const Stmt* stmt = Result.Nodes.getStmtAs<Stmt>("id");
>         const Decl* decl = Result.Nodes.getDeclAs<Decl>("id");
>
>         str << "Found Match in " << Result.SourceManager->getFilename(Result.SourceManager->?);
>         matches.push_back(str.str());
>   }
>  private:
>     std::vector<std::string> matches;
>     std::stringstream str;
> };
> } // end anonymous namespace
>
> kind regards,
>
> Jens Weller
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list