v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
annotations.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "clang.h"
4 
5 #include <regex>
6 #include <iostream>
7 #include <fmt/ostream.h>
8 
9 
10 
11 class Annotations {
12  set <string> annotations;
13 
14  void get_annotations_for_decl(const Decl * decl_to_check) {
15  if (!decl_to_check) { return; }
16  for (auto attr : decl_to_check->getAttrs()) {
17  AnnotateAttr *annotation = dyn_cast<AnnotateAttr>(attr);
18  if (annotation) {
19  auto attribute_attr = dyn_cast<AnnotateAttr>(attr);
20  auto annotation_string = attribute_attr->getAnnotation().str();
21  //if (print_logging) cerr << "Got annotation " << annotation_string << endl;
22  annotations.emplace(annotation->getAnnotation().str());
23  }
24  }
25  }
26 
27 
28 public:
29 
30  Annotations(const Decl *decl_to_check) {
31  get_annotations_for_decl(decl_to_check);
32  }
33 
34 
35  Annotations(const CXXMethodDecl *decl_to_check) {
36  get_annotations_for_decl(decl_to_check);
37 
38  }
39 
40  Annotations() = default;
41 
42  const vector <string> get() const {
43  std::vector <string> results;
44 
45  for (auto &annotation : annotations) {
46  results.push_back(annotation);
47  }
48  return results;
49  }
50 
51  std::vector <string> get_regex(const string &regex_string) const {
52  auto re = regex(regex_string);
53  std::vector <string> results;
54 
55  for (auto &annotation : annotations) {
56  std::smatch matches;
57  if (std::regex_match(annotation, matches, re)) {
58  // printf("GOT %d MATCHES\n", (int)matches.size());
59  if (matches.size() > 1) {
60  results.emplace_back(matches[1]);
61  }
62  }
63  }
64  return results;
65  }
66 
67  bool has(const std::string &target) const {
68  return std::find(annotations.begin(), annotations.end(), target) != annotations.end();
69  }
70 
71  void merge(const Annotations &other) {
72  cerr << fmt::format("Merging in {} annotations onto {} existing ones", other.get().size(), this->get().size())
73  << endl;
74  annotations.insert(other.annotations.begin(), other.annotations.end());
75  }
76 
77 
78  // holds a list of templates and associated annotations. These annotations will be merged with classes created
79  // from the template. This allows metadata associated with all instantiations of a template
80  static map<const ClassTemplateDecl *, Annotations> annotations_for_class_templates;
81 
82  // any annotations on 'using' statements should be applied to the actual CXXRecordDecl being aliased (the right side)
83  static map<const CXXRecordDecl *, Annotations> annotations_for_record_decls;
84 
85 
86  // if a template instantiation is named with a 'using' statement, use that alias for the type isntead of the template/class name itself
87  // this stops them all from being named the same thing - aka CppFactory, CppFactory, ... instead of MyThingFactory, MyOtherThingFactory, ...
88  static map<const CXXRecordDecl *, string> names_for_record_decls;
89 
90  Annotations(const CXXRecordDecl *decl_to_check);
91 };
static map< const ClassTemplateDecl *, Annotations > annotations_for_class_templates
Definition: annotations.h:80
::std::string string
Definition: gtest-port.h:1097
const vector< string > get() const
Definition: annotations.h:42
void merge(const Annotations &other)
Definition: annotations.h:71
Annotations(const Decl *decl_to_check)
Definition: annotations.h:30
std::vector< string > get_regex(const string &regex_string) const
Definition: annotations.h:51
bool has(const std::string &target) const
Definition: annotations.h:67
static map< const CXXRecordDecl *, string > names_for_record_decls
Definition: annotations.h:88
static map< const CXXRecordDecl *, Annotations > annotations_for_record_decls
Definition: annotations.h:83
Annotations(const CXXMethodDecl *decl_to_check)
Definition: annotations.h:35
Annotations()=default