[cfe-dev] [Clang-tidy] Detecting if a catch was done by const ref

Richard via cfe-dev cfe-dev at lists.llvm.org
Sun Mar 11 23:12:22 PDT 2018


[Please reply *only* to the list and do not include my email directly
in the To: or Cc: of your reply; otherwise I will not see your reply.
Thanks.]

In article <CAHCaCkKdVLA-Mvg6i8BLAURRAeFfq5_GSGc6NmY3bEjRPpXq3Q at mail.gmail.com>,
    Matthieu Brucher via cfe-dev <cfe-dev at lists.llvm.org> writes:

> 2018-03-11 21:26 GMT+00:00 Richard via cfe-dev <cfe-dev at lists.llvm.org>:
> 
> > Have you tried prototyping with clang-query?
> >
> 
> I know how to use it to create the query, but that's it. And even then, I
> can only do basic query, as I don't know where to find documentation on the
> specifics.
> It's possible to use for "dematching"? The ideal would be to select all
> catch statements that are not const&, but there are no example anywhere on
> that kind of selection.

I confess that I find the type related matchers confusing and I'm
never quite sure how to use them.  If you look at the matcher
reference <http://clang.llvm.org/docs/LibASTMatchersReference.html>
you'll see a matcher for isExceptionVariable which is how you match
variable declarations in a catch statement.  (Maybe the matcher name
here isn't the best.)

	c:\tmp
	> copy con: tmp.cpp
	#include <stdexcept>

	void f()
	{
	  try {
		g();
	  } catch (std::exception &e) {
		throw;
	  }
	}
	^Z
			1 file(s) copied.

	c:\tmp
	> clang-query tmp.cpp --
	clang-query> match varDecl(isExceptionVariable())

	Match #1:

	c:\tmp\tmp.cpp:9:12: note: "root" binds here
	  } catch (std::exception &e) {
			   ^~~~~~~~~~~~~~~~~
	1 match.
	clang-query>

The part that always confuses me about the matchers is how to modify
matchers that find statements or declarations to qualify the matches
by aspects of the associated type.

Here we want to narrow the varDecl() so that the type of the variable
is reference but not reference to const.  Frankly, I have never
figured out how to do that in clang-query and I'd love for someone to
enlighten me :)
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
            The Terminals Wiki <http://terminals-wiki.org>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list