AnnotateFunctions.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 in
  11. // translation units that start with #pragma enable_annotate.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Frontend/FrontendPluginRegistry.h"
  15. #include "clang/AST/AST.h"
  16. #include "clang/AST/ASTConsumer.h"
  17. #include "clang/Lex/Preprocessor.h"
  18. #include "clang/Lex/LexDiagnostic.h"
  19. using namespace clang;
  20. namespace {
  21. static bool EnableAnnotate = false;
  22. static bool HandledDecl = false;
  23. class AnnotateFunctionsConsumer : public ASTConsumer {
  24. public:
  25. bool HandleTopLevelDecl(DeclGroupRef DG) override {
  26. HandledDecl = true;
  27. if (!EnableAnnotate)
  28. return true;
  29. for (auto D : DG)
  30. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
  31. FD->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(),
  32. "example_annotation"));
  33. return true;
  34. }
  35. };
  36. class AnnotateFunctionsAction : public PluginASTAction {
  37. public:
  38. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  39. llvm::StringRef) override {
  40. return llvm::make_unique<AnnotateFunctionsConsumer>();
  41. }
  42. bool ParseArgs(const CompilerInstance &CI,
  43. const std::vector<std::string> &args) override {
  44. return true;
  45. }
  46. PluginASTAction::ActionType getActionType() override {
  47. return AddBeforeMainAction;
  48. }
  49. };
  50. class PragmaAnnotateHandler : public PragmaHandler {
  51. public:
  52. PragmaAnnotateHandler() : PragmaHandler("enable_annotate") { }
  53. void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
  54. Token &PragmaTok) override {
  55. Token Tok;
  56. PP.LexUnexpandedToken(Tok);
  57. if (Tok.isNot(tok::eod))
  58. PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
  59. if (HandledDecl) {
  60. DiagnosticsEngine &D = PP.getDiagnostics();
  61. unsigned ID = D.getCustomDiagID(
  62. DiagnosticsEngine::Error,
  63. "#pragma enable_annotate not allowed after declarations");
  64. D.Report(PragmaTok.getLocation(), ID);
  65. }
  66. EnableAnnotate = true;
  67. }
  68. };
  69. }
  70. static FrontendPluginRegistry::Add<AnnotateFunctionsAction>
  71. X("annotate-fns", "annotate functions");
  72. static PragmaHandlerRegistry::Add<PragmaAnnotateHandler>
  73. Y("enable_annotate","enable annotation");