My Project
comment.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 
19 #ifndef __CPUI_COMMENT__
20 #define __CPUI_COMMENT__
21 
22 #include "address.hh"
23 
24 class FlowBlock;
25 class PcodeOp;
26 class Funcdata;
27 
37 class Comment {
38  friend class CommentDatabaseInternal;
39  uint4 type;
40  Address funcaddr;
41  Address addr;
42  int4 uniq;
43  string text;
44 public:
46  enum comment_type {
47  user1 = 1,
48  user2 = 2,
49  user3 = 4,
50  header = 8,
51  warning = 16,
53  };
54  Comment(uint4 tp,const Address &fad,
55  const Address &ad,int4 uq,const string &txt);
56  Comment(void) {}
57  uint4 getType(void) const { return type; }
58  const Address &getFuncAddr(void) const { return funcaddr; }
59  const Address &getAddr(void) const { return addr; }
60  int4 getUniq(void) const { return uniq; }
61  const string &getText(void) const { return text; }
62  void saveXml(ostream &s) const;
63  void restoreXml(const Element *el,const AddrSpaceManager *manage);
64  static uint4 encodeCommentType(const string &name);
65  static string decodeCommentType(uint4 val);
66 };
67 
72 struct CommentOrder {
73  bool operator()(const Comment *a,const Comment *b) const;
74 };
75 
76 typedef set<Comment *,CommentOrder> CommentSet;
77 
85 public:
86  CommentDatabase(void) {}
87  virtual ~CommentDatabase(void) {}
88  virtual void clear(void)=0;
89 
96  virtual void clearType(const Address &fad,uint4 tp)=0;
97 
104  virtual void addComment(uint4 tp,const Address &fad,
105  const Address &ad,const string &txt)=0;
106 
116  virtual bool addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad,const string &txt)=0;
117 
121  virtual void deleteComment(Comment *com)=0;
122 
127  virtual CommentSet::const_iterator beginComment(const Address &fad) const=0;
128 
133  virtual CommentSet::const_iterator endComment(const Address &fad) const=0;
134 
139  virtual void saveXml(ostream &s) const=0;
140 
145  virtual void restoreXml(const Element *el,const AddrSpaceManager *manage)=0;
146 };
147 
148 
155  CommentSet commentset;
156 public:
158  virtual ~CommentDatabaseInternal(void);
159  virtual void clear(void);
160  virtual void clearType(const Address &fad,uint4 tp);
161  virtual void addComment(uint4 tp,const Address &fad,
162  const Address &ad,const string &txt);
163  virtual bool addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad,const string &txt);
164  virtual void deleteComment(Comment *com);
165  virtual CommentSet::const_iterator beginComment(const Address &fad) const;
166  virtual CommentSet::const_iterator endComment(const Address &fad) const;
167  virtual void saveXml(ostream &s) const;
168  virtual void restoreXml(const Element *el,const AddrSpaceManager *manage);
169 };
170 
189 public:
190  enum {
191  header_basic = 0,
192  header_unplaced = 1
193  };
194 private:
196  struct Subsort {
197  int4 index;
198  uint4 order;
199  uint4 pos;
200 
205  bool operator<(const Subsort &op2) const {
206  if (index == op2.index) {
207  if (order == op2.order)
208  return (pos < op2.pos);
209  return (order < op2.order);
210  }
211  return (index < op2.index);
212  }
213 
217  void setHeader(uint4 headerType) {
218  index = -1; // -1 indicates a header comment
219  order = headerType;
220  }
221 
226  void setBlock(int4 i,uint4 ord) {
227  index = i;
228  order = ord;
229  }
230  };
231  map<Subsort,Comment *> commmap;
232  mutable map<Subsort,Comment *>::const_iterator start;
233  map<Subsort,Comment *>::const_iterator stop;
234  map<Subsort,Comment *>::const_iterator opstop;
235  bool displayUnplacedComments;
236  bool findPosition(Subsort &subsort,Comment *comm,const Funcdata *fd);
237 public:
238  CommentSorter(void) { displayUnplacedComments = false; }
239  void setupFunctionList(uint4 tp,const Funcdata *fd,const CommentDatabase &db,bool displayUnplaced);
240  void setupBlockList(const FlowBlock *bl);
241  void setupOpList(const PcodeOp *op);
242  void setupHeader(uint4 headerType);
243  bool hasNext(void) const { return (start!=opstop); }
244  Comment *getNext(void) const { Comment *res=(*start).second; ++start; return res; }
245 };
246 
247 #endif
The second user defined property.
Definition: comment.hh:48
Description of a control-flow block containing PcodeOps.
Definition: block.hh:60
const Address & getFuncAddr(void) const
Get the address of the function containing the comment.
Definition: comment.hh:58
void restoreXml(const Element *el, const AddrSpaceManager *manage)
Restore the comment from XML.
Definition: comment.cc:55
A class for sorting comments into and within basic blocks.
Definition: comment.hh:188
A manager for different address spaces.
Definition: translate.hh:218
static uint4 encodeCommentType(const string &name)
Convert name string to comment property.
Definition: comment.cc:74
An interface to a container of comments.
Definition: comment.hh:84
Container for data structures associated with a single function.
Definition: funcdata.hh:45
const string & getText(void) const
Get the body of the comment.
Definition: comment.hh:61
virtual ~CommentDatabase(void)
Destructor.
Definition: comment.hh:87
CommentSorter(void)
Constructor.
Definition: comment.hh:238
A comment attached to a specific function and code address.
Definition: comment.hh:37
bool hasNext(void) const
Return true if there are more comments to emit in the current set.
Definition: comment.hh:243
Lowest level operation of the p-code language.
Definition: op.hh:58
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
The comment should be displayed in the function header.
Definition: comment.hh:50
Classes for specifying addresses and other low-level constants.
The first user defined property.
Definition: comment.hh:47
Comment(void)
Constructor for use with restoreXml.
Definition: comment.hh:56
void saveXml(ostream &s) const
Save the comment to an XML stream.
Definition: comment.cc:33
The comment is auto-generated to alert the user.
Definition: comment.hh:51
Comment * getNext(void) const
Advance to the next comment.
Definition: comment.hh:244
An XML element. A node in the DOM tree.
Definition: xml.hh:150
static string decodeCommentType(uint4 val)
Convert comment property to string.
Definition: comment.cc:94
Compare two Comment pointers.
Definition: comment.hh:72
The comment is auto-generated and should be in the header.
Definition: comment.hh:52
comment_type
Possible properties associated with a comment.
Definition: comment.hh:46
uint4 getType(void) const
Get the properties associated with the comment.
Definition: comment.hh:57
int4 getUniq(void) const
Get the sub-sorting index.
Definition: comment.hh:60
The third user defined property.
Definition: comment.hh:49
set< Comment *, CommentOrder > CommentSet
A set of comments sorted by function and address.
Definition: comment.hh:76
CommentDatabase(void)
Constructor.
Definition: comment.hh:86
const Address & getAddr(void) const
Get the address to which the instruction is attached.
Definition: comment.hh:59
An in-memory implementation of the CommentDatabase API.
Definition: comment.hh:154