[cfe-dev] Get the specialized class name and function name

zxhuan via cfe-dev cfe-dev at lists.llvm.org
Wed Feb 26 23:05:19 PST 2020


Hey guys,

I am new to clang and I am trying to get the specialized name of template
classes and functions. For example, the std::map container is an STL class
with template parameters. map<string, int> should be expanded as something
similar to std::map<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int>. I have written a
simple clang tool to get the function name and the class name of functions,
here is a piece of code:

map<string, set<string>> func_set;

DeclarationMatcher methodMatcher = cxxMethodDecl().bind("methods");

class MethodPrinter : public MatchFinder::MatchCallback {
public :
  virtual void run(const MatchFinder::MatchResult &Result) {
    if (const CXXMethodDecl *md =
Result.Nodes.getNodeAs<clang::CXXMethodDecl>("methods")) {
    string f_name = md->getNameAsString();
    string c_name = md->getParent()->getNameAsString();

    if (auto tsd =
dyn_cast<ClassTemplateSpecializationDecl>(md->getParent()))
    c_name = tsd->getNameAsString();

    func_set[c_name].insert(f_name);
    }
  }
};

This piece of code is getting me started, but not quite there. The
getNameAsString() method just gives a very simple name, without the
template parameters specialized. I am wondering if there is a way to do
that.

Any help is appreciated!

Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200227/dd0c850a/attachment.html>


More information about the cfe-dev mailing list