c++abstract-syntax-treeclang-tidyaccess-specifier

How to get the access specifier for a clang::CXXMethodDecl?


I would like to know whether my C++ method is public, protected or private when writing a clang-tidy check. That seems to be a very simple task. But I could not figure out how to solve this, as clang::CXXMethodDecl doesn't provide a method for getting the access specifier. How can I reach it?


Solution

  • C++ access specifiers for declarations are stored in the Decl superclass that CXXMethodDecl inherits, and retrievable by calling getAccess().

    An example clang-tidy check that uses getAccess is VirtualClassDestructorCheck.cpp, which has this usage:

      const CXXDestructorDecl *Destructor = Node.getDestructor();
      if (!Destructor)
        return false;
    
      return (((Destructor->getAccess() == AccessSpecifier::AS_public) &&
               Destructor->isVirtual()) ||
              ((Destructor->getAccess() == AccessSpecifier::AS_protected) &&
               !Destructor->isVirtual()));
    

    (@Botje already answered in a comment. I am just putting the information in the proper box.)