IDA C++ SDK 9.2
Loading...
Searching...
No Matches
ida_highlighter.hpp
Go to the documentation of this file.
1#ifndef IDA_HIGHLIGHTER
2#define IDA_HIGHLIGHTER
3
4#include <expr.hpp>
5
6idaman void ida_export code_highlight_block(void *context, highlighter_cbs_t *highlighter_cbs, const qstring &text);
7
9{
11// returns the length of text to colorize
12// negative values may have special meaning in the future.
13 virtual ssize_t get_text_color(syntax_highlight_style * /*out_color*/, const char * /*str*/) { return 0; }
14// returns true if identifier is colorized
15 virtual bool get_ident_color(syntax_highlight_style * /*out_color*/, const qstring &/*ident*/) { return false; }
16};
18
19#define MLTCMTMASK 0xF
20#define PREPROC_FLAG 0x10
21
22// Common implementation of syntax_highlighter_t used in different parts of IDA
24{
25protected:
26 // keys of keywords_t point to memory from keyword_strings.
27 // once allocated, this buffer won't be moved in the memory.
29 // helper class so that keywords_t gets sorted by string contents, not pointer values.
31 {
32 const char *ptr;
33 plain_char_ptr_t(const char *p = nullptr) : ptr(p) {}
34 bool operator <(const plain_char_ptr_t &r) const
35 {
36 return strcmp(ptr, r.ptr) < 0;
37 }
38 bool operator ==(const plain_char_ptr_t &r) const
39 {
40 return strcmp(ptr, r.ptr) == 0;
41 }
42 };
44 {
48 multicmt_t(const char *_open_multicmt, const char *_close_multicmt) :
49 open_multicmt(_open_multicmt),
50 close_multicmt(_close_multicmt)
51 {}
52 };
53
54 // typedef std::map<plain_char_ptr_t, syntax_highlight_style> keywords_t;
61 //typedef qvector<plain_char_ptr_t> keywords_t;
64
65 qstring open_cmt; // string that opens a regular line comment
67 char literal_closer; // either close_strconst or close_chrconst for the current literal
68
69 // color mappings
74
76
77 // work variables
78 const char *input; // pointer to the start of the input buffer
79 const char *pending; // pointer in the input buffer
80 syntax_highlight_style style = HF_DEFAULT; // current highlighting style
81
83 {
84 for ( const char *p = pending; p != end; ++p )
85 if ( !qisspace(*p) )
86 return true;
87 return false;
88 }
89
90 const char *parse_literal_const(highlighter_cbs_t *highlighter_cbs, const char *ptr, char literal_closer);
91 void flush_output(highlighter_cbs_t *highlighter_cbs, const char *ptr, syntax_highlight_style style);
92 void handle_cmt(highlighter_cbs_t *highlighter_cbs, int mcmt_idx, const char **ptr);
93 void handle_preproc(highlighter_cbs_t *highlighter_cbs, const char **ptr);
94
95public:
96 // if any of the following features is not needed, just zero them out:
97 char open_strconst; // character that opens a string constant
98 char close_strconst; // character that closes a string constant
99 char open_chrconst; // character that closes a character constant
100 char close_chrconst; // character that opens a character constant
101 char escape_char; // backslash
103
105
106 void highlight_block_ex(highlighter_cbs_t *highlighter_cbs, const qstring &text);
108 void set_open_cmt(const char *begin) { open_cmt = begin; }
109 void add_multi_line_comment(const char *begin, const char *end)
110 {
111 multicmt_t &mcmt = multicmts.push_back();
112 mcmt.open_multicmt = begin;
113 mcmt.close_multicmt = end;
114 }
115
117 {
118 keywords_style_t *pair_p = nullptr;
119 for ( int i = 0; i < keywords.size(); i++ )
120 {
121 if ( keywords[i].style == _style )
122 {
123 pair_p = &keywords[i];
124 break;
125 }
126 }
127 if ( pair_p == nullptr )
128 {
129 keywords_style_t &pair = keywords.push_back();
130 pair_p = &pair;
131 pair_p->style = _style;
132 }
133 return pair_p;
134 }
135
136 void add_keyword_to_style(const char *kw, syntax_highlight_style _style)
137 {
138 keywords_style_t *pair_p = get_keyword_style(_style);
139 pair_p->keywords.push_back(kw);
140 }
141
142 void add_keywords(const char *kwstr, syntax_highlight_style _style)
143 {
144 char *ctx;
145 // in order not to allocate every keyword separately, we allocate the whole
146 // kwstr string at once and will just store pointers to it in the map.
147 qstring &mem = keyword_memory.push_back();
148 mem = kwstr;
149 for ( char *kw = qstrtok(mem.begin(), "|", &ctx); kw != nullptr; kw = qstrtok(nullptr, "|", &ctx) )
150 {
151 // there is room for optimization here...
152 bool exists = false;
153 for ( int i = 0; i < keywords.size(); i++ )
154 {
155 auto p = std::find(keywords[i].keywords.begin(), keywords[i].keywords.end(), kw);
156 if ( p != keywords[i].keywords.end() )
157 {
158 // already exists, update the style
159 if ( keywords[i].style != _style )
160 {
161 keywords[i].keywords.erase(p);
162 add_keyword_to_style(kw, _style);
163 }
164 exists = true;
165 break;
166 }
167 }
168 if ( !exists )
169 add_keyword_to_style(kw, _style);
170 }
171 }
172};
173
174
175#endif // IDA_HIGHLIGHTER
iterator begin(void)
Get a pointer to the beginning of the qstring.
Definition pro.h:3177
Reimplementation of vector class from STL.
Definition pro.h:2250
Functions that deal with C-like expressions and built-in IDC language.
syntax_highlight_style
Possible syntax element highlighting style names.
Definition expr.hpp:608
@ HF_PREPROC
Definition expr.hpp:615
@ HF_DEFAULT
Definition expr.hpp:609
@ HF_STRING
Definition expr.hpp:613
@ HF_COMMENT
Definition expr.hpp:614
INLINE THREAD_SAFE bool ida_local qisspace(char c)
Definition pro.h:938
idaman const char * end
Definition pro.h:1001
qvector< ida_syntax_highlighter_helper_t * > ida_syntax_highlighter_helpers_t
Definition ida_highlighter.hpp:17
idaman void ida_export code_highlight_block(void *context, highlighter_cbs_t *highlighter_cbs, const qstring &text)
idaman THREAD_SAFE char *ida_export qstrtok(char *s, const char *delim, char **save_ptr)
Thread-safe version of strtok.
ptrdiff_t ssize_t
Signed size_t - used to check for size overflows when the counter becomes negative.
Definition pro.h:381
_qstring< char > qstring
regular string
Definition pro.h:3694
qvector< qstring > qstrvec_t
vector of strings
Definition pro.h:3697
Definition expr.hpp:627
Definition ida_highlighter.hpp:9
virtual ~ida_syntax_highlighter_helper_t()
Definition ida_highlighter.hpp:10
virtual ssize_t get_text_color(syntax_highlight_style *, const char *)
Definition ida_highlighter.hpp:13
virtual bool get_ident_color(syntax_highlight_style *, const qstring &)
Definition ida_highlighter.hpp:15
Definition ida_highlighter.hpp:56
syntax_highlight_style style
Definition ida_highlighter.hpp:58
qvector< plain_char_ptr_t > keywords
Definition ida_highlighter.hpp:57
Definition ida_highlighter.hpp:44
qstring close_multicmt
Definition ida_highlighter.hpp:46
multicmt_t()
Definition ida_highlighter.hpp:47
multicmt_t(const char *_open_multicmt, const char *_close_multicmt)
Definition ida_highlighter.hpp:48
qstring open_multicmt
Definition ida_highlighter.hpp:45
plain_char_ptr_t(const char *p=nullptr)
Definition ida_highlighter.hpp:33
bool operator==(const plain_char_ptr_t &r) const
Definition ida_highlighter.hpp:38
const char * ptr
Definition ida_highlighter.hpp:32
bool operator<(const plain_char_ptr_t &r) const
Definition ida_highlighter.hpp:34
const char * pending
Definition ida_highlighter.hpp:79
char open_chrconst
Definition ida_highlighter.hpp:99
keywords_t keywords
Definition ida_highlighter.hpp:63
char preprocessor_char
Definition ida_highlighter.hpp:102
const char * parse_literal_const(highlighter_cbs_t *highlighter_cbs, const char *ptr, char literal_closer)
void handle_preproc(highlighter_cbs_t *highlighter_cbs, const char **ptr)
ida_syntax_highlighter_t()
Definition ida_highlighter.hpp:104
void highlight_block_ex(highlighter_cbs_t *highlighter_cbs, const qstring &text)
syntax_highlight_style preprocessor_color
Definition ida_highlighter.hpp:73
multicmtvec_t multicmts
Definition ida_highlighter.hpp:66
syntax_highlight_style text_color
Definition ida_highlighter.hpp:70
syntax_highlight_style comment_color
Definition ida_highlighter.hpp:71
void add_ida_syntax_highlighter_helper(ida_syntax_highlighter_helper_t *sh)
Definition ida_highlighter.hpp:107
qvector< keywords_style_t > keywords_t
Definition ida_highlighter.hpp:60
qstring open_cmt
Definition ida_highlighter.hpp:65
void add_keyword_to_style(const char *kw, syntax_highlight_style _style)
Definition ida_highlighter.hpp:136
char close_chrconst
Definition ida_highlighter.hpp:100
bool pending_nonspaces_present(const char *end)
Definition ida_highlighter.hpp:82
char literal_closer
Definition ida_highlighter.hpp:67
qvector< multicmt_t > multicmtvec_t
Definition ida_highlighter.hpp:62
char escape_char
Definition ida_highlighter.hpp:101
keywords_style_t * get_keyword_style(syntax_highlight_style _style)
Definition ida_highlighter.hpp:116
syntax_highlight_style style
Definition ida_highlighter.hpp:80
char open_strconst
Definition ida_highlighter.hpp:97
qstrvec_t keyword_memory
Definition ida_highlighter.hpp:28
void handle_cmt(highlighter_cbs_t *highlighter_cbs, int mcmt_idx, const char **ptr)
void add_keywords(const char *kwstr, syntax_highlight_style _style)
Definition ida_highlighter.hpp:142
void add_multi_line_comment(const char *begin, const char *end)
Definition ida_highlighter.hpp:109
char close_strconst
Definition ida_highlighter.hpp:98
syntax_highlight_style string_color
Definition ida_highlighter.hpp:72
void set_open_cmt(const char *begin)
Definition ida_highlighter.hpp:108
ida_syntax_highlighter_helpers_t ida_syntax_highlighter_helpers
Definition ida_highlighter.hpp:75
const char * input
Definition ida_highlighter.hpp:78
void flush_output(highlighter_cbs_t *highlighter_cbs, const char *ptr, syntax_highlight_style style)
syntax_highlighter_t(block_highlighter_t *bh=nullptr)
Definition expr.hpp:646