Search code examples
clanglibtooling

Clang tooling, check if final or override


Is there a "clang way" to check if CXXMethodDecl is specified with override or final keywords?

I can check it by std::string::find(" override") for the string representing CXXMethodDecl, but this way looks a little ugly.


Solution

  • It turned out that that final and override are hidden in the attributes of the clang::Decl. So the underlying code checks method for final.

    bool FinalReplacer::VisitCXXMethodDecl(CXXMethodDecl *methodDecl) {
        auto pos = find_if(methodDecl->attr_begin(), methodDecl->attr_end(), [](Attr *a) {
            return (a->getKind() == attr::Kind::Final);
        });
        if (pos != methodDecl->attr_end()) {
            //Do something here.
        }
        return true;
    }
    

    Similarly for override attribute is attr::Kind::Override.

    A complete list of attributes for clang::Decl can be found in clang/Basic/AttrList.inc