//===- AnnotateFunctions.cpp ----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Attribute plugin to mark a virtual method as ``call_super``, subclasses must // call it in the overridden method. // // This example shows that attribute plugins combined with ``PluginASTAction`` // in Clang can do some of the same things which Java Annotations do. // // Unlike the other attribute plugin examples, this one does not attach an // attribute AST node to the declaration AST node. Instead, it keeps a separate // list of attributed declarations, which may be faster than using // ``Decl::getAttr<T>()`` in some cases. The disadvantage of this approach is // that the attribute is not part of the AST, which means that dumping the AST // will lose the attribute information, pretty printing the AST won't write the // attribute back out to source, and AST matchers will not be able to match // against the attribute on the declaration. // //===----------------------------------------------------------------------===// #include "clang/AST/ASTContext.h" #include "clang/AST/Attr.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/Frontend/FrontendPluginRegistry.h" #include "clang/Sema/ParsedAttr.h" #include "clang/Sema/Sema.h" #include "clang/Sema/SemaDiagnostic.h" #include "llvm/ADT/SmallPtrSet.h" usingnamespaceclang; namespace { // Cached methods which are marked as 'call_super'. llvm::SmallPtrSet<const CXXMethodDecl *, 16> MarkedMethods; bool isMarkedAsCallSuper(const CXXMethodDecl *D) { … } class MethodUsageVisitor : public RecursiveASTVisitor<MethodUsageVisitor> { … }; class CallSuperVisitor : public RecursiveASTVisitor<CallSuperVisitor> { … }; class CallSuperConsumer : public ASTConsumer { … }; class CallSuperAction : public PluginASTAction { … }; struct CallSuperAttrInfo : public ParsedAttrInfo { … }; } // namespace static FrontendPluginRegistry::Add<CallSuperAction> X("call_super_plugin", "clang plugin, checks every overridden virtual " "function whether called this function or not."); static ParsedAttrInfoRegistry::Add<CallSuperAttrInfo> Y("call_super_attr", "Attr plugin to define 'call_super' attribute");