-
Notifications
You must be signed in to change notification settings - Fork 6
/
Macro.cpp
371 lines (294 loc) · 10 KB
/
Macro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
cpp2cxx is an open source software distributed under terms of the
Apache2.0 licence.
Copyrights remain with the original copyright holders.
Use of this material is by permission and/or license.
Copyright [2012] Aditya Kumar, Andrew Sutton, Bjarne Stroustrup
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Macro.h"
#include "MacroStat.h"
#include "RlParser.h"
#include "ExceptionHandler.h"
#include "vector_utils.hpp"
#include "debug.h"
#include <cctype> //for islower
PPMacro::PPMacro(std::ostream& log_file)
:identifier(boost::wave::T_UNKNOWN),
m_cat(MacroCategory::none),
logFile(log_file),
use_case_set(false)
{ m_stat = new MacroStat; }
PPMacro::~PPMacro()
{ /*delete m_stat;*/ }
void PPMacro::set_identifier(token_type const& tok)
{
identifier = tok;
// std::cout<<"identifier: "<<(*it).get_value()<<std::endl;
}
void PPMacro::put_tokens(std::vector<token_type> const& vec_tokens)
{
//macro_tokens will have atleast one element
macro_tokens = vec_tokens;
DEBUG_MACRO_CLASS(
std::cout<<"Tokens for Macro: "<<macro_tokens.begin()->get_value()<<"\n\t";
std::for_each(macro_tokens.begin(),macro_tokens.end(),
[](token_type tok) {
std::cout<<tok.get_value();
});
std::cout<<"\n";);
}
void PPMacro::set_identifier_parameters(token_type const& tok,
unsigned int parameter_count)
{
//token mytoken(it);
//std::cout<<"identifier_parameter iD: "<<tok.get_name()<<std::endl;
identifier_parameters.push_back(std::make_pair(tok,parameter_count));
}
void PPMacro::set_identifier_str(std::string str)
{
identifier_str = str;
// std::cout<<"iden_string: "<<identifier_str<<std::endl;
}
void PPMacro::set_replacement_list(token_type tok)
{
rep_list.set_replacement_list(tok);
//ReplacementList.push_back(*it);
// std::cout<<"ReplacementList: "<<(*it).get_value()<<std::endl;
}
void PPMacro::set_replacement_list_str(std::string str, RlParser & rl_parser)
{
rep_list.set_replacement_list_str(str, identifier_parameters);
// std::cout<<"repl_string: "<<replacement_list_str<<std::endl;
logFile<<" - log: parsing replacement list: '"
<<str << "' of macro: "
<<identifier.get_value()<<"\n";
set_replacement_list_category(rl_parser);
}
void PPMacro::set_operation(PPOperation op)
{
operation = op;
// std::cout<<"operations: "<<(*it).get_value()<<std::endl;
}
void PPMacro::set_macro_category(MacroCategory cat)
{
m_cat = cat;
// std::cout<<"MacroCategory = "<<m_cat<<std::endl;
}
void PPMacro::set_replacement_list_category(RlParser & rl_parser)
{
rep_list.set_replacement_list_category(rl_parser);
}
void PPMacro::set_conditional_category(CondCategory ccat)
{
condCat = ccat;
}
void PPMacro::set_macro_scope_category(MacroScopeCategory scat)
{
m_scat = scat;
}
void PPMacro::set_use_case(std::pair<token_iterator, token_iterator>& token_iter_range)
{
if(!use_case_set)
use_case = token_iter_range;
}
//capturing only the first macro invocation args
/// @brief if there is a mismatch in the number of arguments
/// then demacrofication is not done for that macro
void PPMacro::set_use_case_string(std::vector<std::string>& vec_string)
{
if(vec_string.size() != identifier_parameters.size()){
logFile<<" - log: definition and invocation does not have equal number of args,";
logFile<<" perhaps multiple definition of same macro.\n";
logFile<<" - log: Not demacrofying: "<<identifier_str<<"\n";
rep_list.get_replacement_list_token_type().reject_type = true;
}
/* assert((vec_string.size() == identifier_parameters.size())
&& "definition and invocation does not have equal number of args");
*/
if(!use_case_set){
invoArgs = vec_string;
using namespace general_utilities;
DEBUG_MACRO_USE_CASE(std::cout<<"Use case args for macro: "
<< identifier_str << ":" <<invoArgs<<"\n";);
use_case_set = true;
}
}
void PPMacro::AnalyzeIdentifier() const
{
bool hasProblems = false;
std::string warning_msg;
std::stringstream strm;
//warning_msg = "Analyzing...\t";
warning_msg = " - line number: ";
strm << identifier.get_position().get_line();
warning_msg += strm.str();
warning_msg += "\t: ";
warning_msg += identifier_str;
warning_msg += "\n";
if(HasLowerCase()) {
warning_msg += " - warning: lower case letters:\n";
hasProblems = true;
}
if(HasLeadingUnderscore()) {
warning_msg += " - warning: leading underscore(s):\n";
hasProblems = true;
}
if(hasProblems) {
logFile<<warning_msg;
throw ExceptionHandler(warning_msg);
}
/*
else
throw ExceptionHandler(warning_msg + "No Problems!\n");
*/
}
bool PPMacro::HasLowerCase() const
{
unsigned int i = 0;
while(i<identifier_str.length()) {
if(islower(identifier_str[i]))
return true;
i++;
}
return false;
}
bool PPMacro::HasLeadingUnderscore() const
{
return identifier_str.compare(0,1,"_") == 0;
}
token_type const PPMacro::get_identifier() const
{ return identifier; }
std::vector<token_type> const& PPMacro::get_tokens() const
{ return macro_tokens; }
int PPMacro::get_num_tokens() const
{ return macro_tokens.size(); }
std::string const& PPMacro::get_identifier_str() const
{ return identifier_str; }
void PPMacro::dump() const
{ std::cout << identifier_str; }
std::string PPMacro::get_replacement_list_str() const
{ return rep_list.get_replacement_list_str(); }
std::string const& PPMacro::get_formatted_replacement_list_str() const
{ return rep_list.get_formatted_replacement_list_str(); }
std::string const& PPMacro::get_replacement_list_str_with_comments() const
{ return rep_list.get_replacement_list_str_with_comments(); }
std::vector<std::pair<token_type,unsigned int> > const&
PPMacro::get_identifier_parameters() const
{ return identifier_parameters; }
ReplacementList& PPMacro::get_replacement_list()
{ return rep_list; }
ReplacementList const& PPMacro::get_replacement_list() const
{ return rep_list; }
PPOperation PPMacro::get_operation() const
{ return operation; }
MacroCategory PPMacro::get_macro_category() const
{ return m_cat; }
RlDCat
PPMacro::get_replacement_list_dependency_category() const
{ return rep_list.get_replacement_list_dependency_category(); }
RlCCat
PPMacro::get_replacement_list_closure_category() const
{ return rep_list.get_replacement_list_closure_category(); }
CondCategory PPMacro::get_conditional_category() const
{ return condCat; }
std::list<token_type>
PPMacro::get_replacement_list_idlist() const
{ return rep_list.get_replacement_list_idlist(); }
std::list<token_type>
PPMacro::get_replacement_list_dep_idlist() const
{
//will contain dependent identifiers in the ReplacementList
//i.e. those which donot occur as function arguments
std::list<token_type> dep_idlist;
if(m_cat == MacroCategory::null_define)
return dep_idlist;
dep_idlist = rep_list.get_replacement_list_idlist();
if(m_cat == MacroCategory::object_like)
return dep_idlist;
//iterator over function_like PPMacro's arguments
std::vector<std::pair<token_type,unsigned int> >::const_iterator iter_args;
//iterator for the ReplacementList tokens
std::list<token_type>::iterator iter_rl_idlist = dep_idlist.begin();
//remove all those identifiers which are in the function argument
iter_args = identifier_parameters.begin();
for(;iter_args != identifier_parameters.end();iter_args++) {
iter_rl_idlist = dep_idlist.begin();
for( ; iter_rl_idlist != dep_idlist.end(); iter_rl_idlist++) {
if(iter_args->first == *iter_rl_idlist) {
//remove the identifiers which are in the function argument
dep_idlist.erase(iter_rl_idlist);
//since the iterators will be invalidated after deletion
iter_rl_idlist = dep_idlist.begin();
}
}
}
return dep_idlist;
}
std::pair<token_iterator,token_iterator>
PPMacro::get_use_case() const
{
return use_case;
}
std::vector<std::string> const& PPMacro::get_use_case_string() const
{
return invoArgs;
}
MacroScopeCategory PPMacro::get_macro_scope_category() const
{
return m_scat;
}
//comparison is based on the token id's to determine their equivalence
bool PPMacro::IsEquivalent(std::pair<token_iterator, token_iterator> token_iter_range) const
{
return true;
/*
typedef std::vector<token_type>::const_iterator tok_iter;
token_iterator first = token_iter_range.first;
token_iterator second = token_iter_range.second;
tok_iter this_mac_tok_iter = macro_tokens.begin();
tok_iter this_mac_tok_iter_end = macro_tokens.end();
for( ;(this_mac_tok_iter!=this_mac_tok_iter_end) && (first != second);
++first,++this_mac_tok_iter) {
#ifdef DEBUG_MACRO_CLASS
std::cout << "Comparing: " << boost::wave::token_id(*first)
<< "\tand\t" << boost::wave::token_id(*this_mac_tok_iter) << "\n";
#endif
if(boost::wave::token_id(*first) != boost::wave::token_id(*this_mac_tok_iter))
return false;
}
return true;
*/
}
void PPMacro::set_macro_stat()
{
m_stat->id_string = identifier_str;
m_stat->rep_list = rep_list.get_replacement_list_str();
m_stat->m_cat = m_cat;
m_stat->rl_ccat = rep_list.get_replacement_list_closure_category();
m_stat->rl_dcat = rep_list.get_replacement_list_dependency_category();
}
MacroStat const* PPMacro::get_macro_stat()
{
return m_stat;
}
bool PPMacro::operator==(PPMacro const& mac) const
{ //return this->identifier == mac.identifier;
return (this->identifier_str.compare(mac.identifier_str) == 0);
}
/*bool PPMacro::operator==(token_type const& tok) const
{
return (this->identifier == tok);
}*/
bool PPMacro::operator<(PPMacro const& mac) const
{
return (this->identifier_str.compare(mac.identifier_str) < 0);
}