1

I'm working on a custom checker for the clang static analyzer that checks for incorrect use of CPython APIs. I've made some progress, but I'm stuck: how can I get a clang::QualType value given the name of a type?

For example, I'd like to write something like this:

QualType ty = findTheTypeNamed("Py_ssize_t");

I've spent time looking at the code for clang::ASTContext and clang::QualType, but I'm lost.

How can I get a clang::QualType from the name of a type?

Brad Larsen
  • 995
  • 1
  • 12
  • 20
  • 1
    I don't have a great answer, but if you have the CompilerInstance, you can create a matcher that will let you specify the name. Or you can set up your own recursive visitor: http://clang.llvm.org/docs/RAVFrontendAction.html. To set up a matcher, you can look at some awful code I wrote here: https://github.com/xaxxon/v8toolkit/blob/master/tools/class_parser.cpp#L2935 – xaxxon Sep 20 '16 at 07:39
  • 1
    Here's a question/answer with more details on doing the recursive traversal yourself: http://stackoverflow.com/questions/30805595/how-to-traverse-clang-ast-manually Unfortunately, I think there may not be a "quick" answer. Maybe do a full traversal (or very generic matcher) and do a one-time cache of all the names => qualtype mappings ? – xaxxon Sep 20 '16 at 07:41

1 Answers1

1

The asString narrowing matcher turns a string into a qualtype.

Here is the associated documentation :

Matches if the matched type is represented by the given string.

Given
  class Y { public: void x(); };
  void z() { Y* y; y->x(); }
cxxMemberCallExpr(on(hasType(asString("class Y *"))))
  matches y->x()
Nestor Demeure
  • 509
  • 6
  • 11