1 //===- AnnotateFunctions.cpp ----------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Example clang plugin which adds an annotation to every function. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Frontend/FrontendPluginRegistry.h" 15 #include "clang/AST/AST.h" 16 #include "clang/AST/ASTConsumer.h" 17 using namespace clang; 18 19 namespace { 20 21 class AnnotateFunctionsConsumer : public ASTConsumer { 22 public: 23 bool HandleTopLevelDecl(DeclGroupRef DG) override { 24 for (auto D : DG) 25 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 26 FD->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(), 27 "example_annotation")); 28 return true; 29 } 30 }; 31 32 class AnnotateFunctionsAction : public PluginASTAction { 33 public: 34 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 35 llvm::StringRef) override { 36 return llvm::make_unique<AnnotateFunctionsConsumer>(); 37 } 38 39 bool ParseArgs(const CompilerInstance &CI, 40 const std::vector<std::string> &args) override { 41 return true; 42 } 43 44 PluginASTAction::ActionType getActionType() override { 45 return AddBeforeMainAction; 46 } 47 }; 48 49 } 50 51 static FrontendPluginRegistry::Add<AnnotateFunctionsAction> 52 X("annotate-fns", "annotate functions"); 53