blob: f2e7322183a92da98cded58bfdbda397a6f1af7d [file] [log] [blame]
John Brawn6c789742016-03-15 12:51:40 +00001//===- 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"
17using namespace clang;
18
19namespace {
20
21class AnnotateFunctionsConsumer : public ASTConsumer {
22public:
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
32class AnnotateFunctionsAction : public PluginASTAction {
33public:
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
51static FrontendPluginRegistry::Add<AnnotateFunctionsAction>
52X("annotate-fns", "annotate functions");