AnnotateFunctions.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "clang/Frontend/FrontendPluginRegistry.h"
  14. #include "clang/AST/AST.h"
  15. #include "clang/AST/ASTConsumer.h"
  16. using namespace clang;
  17. namespace {
  18. class AnnotateFunctionsConsumer : public ASTConsumer {
  19. public:
  20. bool HandleTopLevelDecl(DeclGroupRef DG) override {
  21. for (auto D : DG)
  22. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
  23. FD->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(),
  24. "example_annotation"));
  25. return true;
  26. }
  27. };
  28. class AnnotateFunctionsAction : public PluginASTAction {
  29. public:
  30. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  31. llvm::StringRef) override {
  32. return llvm::make_unique<AnnotateFunctionsConsumer>();
  33. }
  34. bool ParseArgs(const CompilerInstance &CI,
  35. const std::vector<std::string> &args) override {
  36. return true;
  37. }
  38. PluginASTAction::ActionType getActionType() override {
  39. return AddBeforeMainAction;
  40. }
  41. };
  42. }
  43. static FrontendPluginRegistry::Add<AnnotateFunctionsAction>
  44. X("annotate-fns", "annotate functions");