Get the specialized class name and function name

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Get the specialized class name and function name

suyash singh via cfe-dev
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

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Get the specialized class name and function name

suyash singh via cfe-dev
I don't know the specific way off-hand, but my usual approach to finding such APIs would be to run clang on a source file that produces a warning/error/diagnostic with the name you want - then running that under a debugger to see how it is getting that name

On Wed, Feb 26, 2020 at 11:05 PM zxhuan via cfe-dev <[hidden email]> wrote:
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
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev