Ghidra Decompiler Analysis Engine
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  int4 uniq;
41  Address funcaddr;
42  Address addr;
43  string text;
44  mutable bool emitted;
45 public:
47  enum comment_type {
48  user1 = 1,
49  user2 = 2,
50  user3 = 4,
51  header = 8,
52  warning = 16,
54  };
55  Comment(uint4 tp,const Address &fad,const Address &ad,int4 uq,const string &txt);
56  Comment(void) {}
57  void setEmitted(bool val) const { emitted = val; }
58  bool isEmitted(void) const { return emitted; }
59  uint4 getType(void) const { return type; }
60  const Address &getFuncAddr(void) const { return funcaddr; }
61  const Address &getAddr(void) const { return addr; }
62  int4 getUniq(void) const { return uniq; }
63  const string &getText(void) const { return text; }
64  void saveXml(ostream &s) const;
65  void restoreXml(const Element *el,const AddrSpaceManager *manage);
66  static uint4 encodeCommentType(const string &name);
67  static string decodeCommentType(uint4 val);
68 };
69 
74 struct CommentOrder {
75  bool operator()(const Comment *a,const Comment *b) const;
76 };
77 
78 typedef set<Comment *,CommentOrder> CommentSet;
79 
87 public:
88  CommentDatabase(void) {}
89  virtual ~CommentDatabase(void) {}
90  virtual void clear(void)=0;
91 
98  virtual void clearType(const Address &fad,uint4 tp)=0;
99 
106  virtual void addComment(uint4 tp,const Address &fad,
107  const Address &ad,const string &txt)=0;
108 
118  virtual bool addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad,const string &txt)=0;
119 
123  virtual void deleteComment(Comment *com)=0;
124 
129  virtual CommentSet::const_iterator beginComment(const Address &fad) const=0;
130 
135  virtual CommentSet::const_iterator endComment(const Address &fad) const=0;
136 
141  virtual void saveXml(ostream &s) const=0;
142 
147  virtual void restoreXml(const Element *el,const AddrSpaceManager *manage)=0;
148 };
149 
150 
157  CommentSet commentset;
158 public:
160  virtual ~CommentDatabaseInternal(void);
161  virtual void clear(void);
162  virtual void clearType(const Address &fad,uint4 tp);
163  virtual void addComment(uint4 tp,const Address &fad,
164  const Address &ad,const string &txt);
165  virtual bool addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad,const string &txt);
166  virtual void deleteComment(Comment *com);
167  virtual CommentSet::const_iterator beginComment(const Address &fad) const;
168  virtual CommentSet::const_iterator endComment(const Address &fad) const;
169  virtual void saveXml(ostream &s) const;
170  virtual void restoreXml(const Element *el,const AddrSpaceManager *manage);
171 };
172 
191 public:
192  enum {
195  };
196 private:
198  struct Subsort {
199  int4 index;
200  uint4 order;
201  uint4 pos;
202 
207  bool operator<(const Subsort &op2) const {
208  if (index == op2.index) {
209  if (order == op2.order)
210  return (pos < op2.pos);
211  return (order < op2.order);
212  }
213  return (index < op2.index);
214  }
215 
219  void setHeader(uint4 headerType) {
220  index = -1; // -1 indicates a header comment
221  order = headerType;
222  }
223 
228  void setBlock(int4 i,uint4 ord) {
229  index = i;
230  order = ord;
231  }
232  };
233  map<Subsort,Comment *> commmap;
234  mutable map<Subsort,Comment *>::const_iterator start;
235  map<Subsort,Comment *>::const_iterator stop;
236  map<Subsort,Comment *>::const_iterator opstop;
237  bool displayUnplacedComments;
238  bool findPosition(Subsort &subsort,Comment *comm,const Funcdata *fd);
239 public:
240  CommentSorter(void) { displayUnplacedComments = false; }
241  void setupFunctionList(uint4 tp,const Funcdata *fd,const CommentDatabase &db,bool displayUnplaced);
242  void setupBlockList(const FlowBlock *bl);
243  void setupOpList(const PcodeOp *op);
244  void setupHeader(uint4 headerType);
245  bool hasNext(void) const { return (start!=opstop); }
246  Comment *getNext(void) const { Comment *res=(*start).second; ++start; return res; }
247 };
248 
249 #endif
Comment::encodeCommentType
static uint4 encodeCommentType(const string &name)
Convert name string to comment property.
Definition: comment.cc:73
CommentDatabaseInternal::saveXml
virtual void saveXml(ostream &s) const
Save all comments in the container to an XML stream.
Definition: comment.cc:238
CommentDatabase::addCommentNoDuplicate
virtual bool addCommentNoDuplicate(uint4 tp, const Address &fad, const Address &ad, const string &txt)=0
Add a new comment to the container, making sure there is no duplicate.
CommentDatabaseInternal::clearType
virtual void clearType(const Address &fad, uint4 tp)
Clear all comments matching (one of) the indicated types.
Definition: comment.cc:154
FlowBlock
Description of a control-flow block containing PcodeOps.
Definition: block.hh:60
CommentDatabase::saveXml
virtual void saveXml(ostream &s) const =0
Save all comments in the container to an XML stream.
CommentSorter::setupFunctionList
void setupFunctionList(uint4 tp, const Funcdata *fd, const CommentDatabase &db, bool displayUnplaced)
Collect and sort comments specific to the given function.
Definition: comment.cc:331
CommentDatabase::endComment
virtual CommentSet::const_iterator endComment(const Address &fad) const =0
Get an iterator to the ending of comments for a single function.
Comment::user1
@ user1
The first user defined property.
Definition: comment.hh:48
Comment::Comment
Comment(uint4 tp, const Address &fad, const Address &ad, int4 uq, const string &txt)
Constructor.
Definition: comment.cc:24
CommentDatabaseInternal::beginComment
virtual CommentSet::const_iterator beginComment(const Address &fad) const
Get an iterator to the beginning of comments for a single function.
Definition: comment.cc:224
CommentDatabaseInternal::clear
virtual void clear(void)
Clear all comments from this container.
Definition: comment.cc:144
CommentDatabase::addComment
virtual void addComment(uint4 tp, const Address &fad, const Address &ad, const string &txt)=0
Add a new comment to the container.
CommentDatabase::beginComment
virtual CommentSet::const_iterator beginComment(const Address &fad) const =0
Get an iterator to the beginning of comments for a single function.
Comment::warningheader
@ warningheader
The comment is auto-generated and should be in the header.
Definition: comment.hh:53
CommentSorter::setupBlockList
void setupBlockList(const FlowBlock *bl)
Prepare to walk comments from a single basic block.
Definition: comment.cc:376
CommentDatabaseInternal::CommentDatabaseInternal
CommentDatabaseInternal(void)
Constructor.
Definition: comment.cc:130
CommentSet
set< Comment *, CommentOrder > CommentSet
A set of comments sorted by function and address.
Definition: comment.hh:78
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
PcodeOp
Lowest level operation of the p-code language.
Definition: op.hh:58
CommentDatabase::clear
virtual void clear(void)=0
Clear all comments from this container.
CommentSorter::header_unplaced
@ header_unplaced
Comment that can't be placed in code flow.
Definition: comment.hh:194
Comment::user2
@ user2
The second user defined property.
Definition: comment.hh:49
CommentOrder::operator()
bool operator()(const Comment *a, const Comment *b) const
Comparison operator.
Definition: comment.cc:118
Comment::user3
@ user3
The third user defined property.
Definition: comment.hh:50
CommentDatabaseInternal::restoreXml
virtual void restoreXml(const Element *el, const AddrSpaceManager *manage)
Restore all comments from XML.
Definition: comment.cc:249
Comment::restoreXml
void restoreXml(const Element *el, const AddrSpaceManager *manage)
Restore the comment from XML.
Definition: comment.cc:53
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
Comment::saveXml
void saveXml(ostream &s) const
Save the comment to an XML stream.
Definition: comment.cc:31
CommentSorter::setupHeader
void setupHeader(uint4 headerType)
Prepare to walk comments in the header.
Definition: comment.cc:391
CommentDatabase::clearType
virtual void clearType(const Address &fad, uint4 tp)=0
Clear all comments matching (one of) the indicated types.
CommentOrder
Compare two Comment pointers.
Definition: comment.hh:74
Comment::comment_type
comment_type
Possible properties associated with a comment.
Definition: comment.hh:47
CommentDatabase::deleteComment
virtual void deleteComment(Comment *com)=0
Remove the given Comment object from the container.
CommentSorter
A class for sorting comments into and within basic blocks.
Definition: comment.hh:190
Comment
A comment attached to a specific function and code address.
Definition: comment.hh:37
Comment::decodeCommentType
static string decodeCommentType(uint4 val)
Convert comment property to string.
Definition: comment.cc:93
address.hh
Classes for specifying addresses and other low-level constants.
CommentSorter::header_basic
@ header_basic
Basic header comments.
Definition: comment.hh:193
CommentDatabase
An interface to a container of comments.
Definition: comment.hh:86
CommentDatabase::restoreXml
virtual void restoreXml(const Element *el, const AddrSpaceManager *manage)=0
Restore all comments from XML.
AddrSpaceManager
A manager for different address spaces.
Definition: translate.hh:218
CommentSorter::setupOpList
void setupOpList(const PcodeOp *op)
Establish a p-code landmark within the current set of comments.
Definition: comment.cc:359
CommentDatabaseInternal
An in-memory implementation of the CommentDatabase API.
Definition: comment.hh:156
CommentDatabaseInternal::addCommentNoDuplicate
virtual bool addCommentNoDuplicate(uint4 tp, const Address &fad, const Address &ad, const string &txt)
Add a new comment to the container, making sure there is no duplicate.
Definition: comment.cc:192
Comment::warning
@ warning
The comment is auto-generated to alert the user.
Definition: comment.hh:52
Comment::header
@ header
The comment should be displayed in the function header.
Definition: comment.hh:51
CommentDatabaseInternal::addComment
virtual void addComment(uint4 tp, const Address &fad, const Address &ad, const string &txt)
Add a new comment to the container.
Definition: comment.cc:174
CommentDatabaseInternal::deleteComment
virtual void deleteComment(Comment *com)
Remove the given Comment object from the container.
Definition: comment.cc:217
CommentDatabaseInternal::endComment
virtual CommentSet::const_iterator endComment(const Address &fad) const
Get an iterator to the ending of comments for a single function.
Definition: comment.cc:231