44 int rescan_macro_status;
45 std::string macro_expand_filename;
49 token_container currentTokSeq;
57 typedef std::map<std::string,ROSEAttributesList*> attribute_map_type;
58 attribute_map_type currentMapOfAttributes;
60 token_container* skippedTokenStream;
72 template <
typename TokenT>
bool found_include_directive(TokenT directive, std::string relname, std::string absname );
82 template <
typename TokenT,
typename ContainerT>
84 found_directive(TokenT
const& directive, ContainerT
const& expression,
bool expression_value)
87 PreprocessingInfo::DirectiveType rose_typeid;
88 using namespace boost::wave;
89 token_id wave_typeid = token_id(directive);
95 rose_typeid = PreprocessingInfo::CpreprocessorDefineDeclaration;
98 rose_typeid = PreprocessingInfo::CpreprocessorIfdefDeclaration;
101 rose_typeid = PreprocessingInfo::CpreprocessorIfndefDeclaration;
104 rose_typeid = PreprocessingInfo::CpreprocessorIfDeclaration;
109 rose_typeid = PreprocessingInfo::CpreprocessorElifDeclaration;
112 rose_typeid = PreprocessingInfo::CpreprocessorElseDeclaration;
115 rose_typeid = PreprocessingInfo::CpreprocessorEndifDeclaration;
118 rose_typeid = PreprocessingInfo::CplusplusStyleComment;
121 rose_typeid = PreprocessingInfo::C_StyleComment;
124 rose_typeid = PreprocessingInfo::CpreprocessorErrorDeclaration;
127 rose_typeid = PreprocessingInfo::CpreprocessorLineDeclaration;
130 rose_typeid = PreprocessingInfo::CpreprocessorUndefDeclaration;
133 rose_typeid = PreprocessingInfo::CpreprocessorWarningDeclaration;
139 std::cout <<
"Token to include directive: " << directive.get_value() << std::endl;
140 rose_typeid = PreprocessingInfo::CpreprocessorIncludeDeclaration;
150 ROSE_ASSERT(
false==
true);
151 rose_typeid = PreprocessingInfo::CpreprocessorUnknownDeclaration;
157 std::cout <<
"THE FOUND DIRECTIVE IS: " << directive.get_value().c_str() << std::endl;
158 std::cout <<
"THE FOUND DEF IS: " << boost::wave::util::impl::as_string(expression) << std::endl;
161 std::string filename(directive.get_position().get_file().c_str());
162 token_list_container tokListCont;
164 copy (expression.begin(), expression.end(),
165 inserter(tokListCont, tokListCont.end()));
169 if(currentMapOfAttributes.find(filename)==currentMapOfAttributes.end())
171 currentMapOfAttributes.find(filename)->second->addElement(*(
new PreprocessingInfo(directive,tokListCont,expression_value,rose_typeid,PreprocessingInfo::before)));
177 template <
typename TokenT,
typename ContainerT>
179 update_token(TokenT
const& token, ContainerT
const& stream,
bool )
182 std::string filename(token.get_position().get_file().c_str());
184 ROSE_ASSERT(currentMapOfAttributes.find(filename) != currentMapOfAttributes.end());
186 std::vector<PreprocessingInfo*>& infos = currentMapOfAttributes.find(filename)->second->getList();
187 for(std::vector<PreprocessingInfo*>::reverse_iterator i = infos.rbegin(); i != infos.rend(); ++i)
190 if ((*(*i)->get_token_stream())[0] == token && (*(*i)->get_token_stream())[0].get_position() == token.get_position())
193 for (
typename ContainerT::const_iterator item = stream.begin(); item != stream.end(); ++item)
195 (*i)->push_back_token_stream(*item);
200 ROSE_ASSERT(!
"Token to update not found!");
211 template <
typename TokenT>
216 PreprocessingInfo::DirectiveType rose_typeid;
217 using namespace boost::wave;
218 token_id wave_typeid = token_id(directive);
224 rose_typeid = PreprocessingInfo::CpreprocessorIfdefDeclaration;
227 rose_typeid = PreprocessingInfo::CpreprocessorIfndefDeclaration;
230 rose_typeid = PreprocessingInfo::CpreprocessorIfDeclaration;
237 rose_typeid = PreprocessingInfo::CpreprocessorDefineDeclaration;
240 rose_typeid = PreprocessingInfo::CpreprocessorElifDeclaration;
243 rose_typeid = PreprocessingInfo::CpreprocessorElseDeclaration;
246 rose_typeid = PreprocessingInfo::CpreprocessorEndifDeclaration;
249 rose_typeid = PreprocessingInfo::CplusplusStyleComment;
252 rose_typeid = PreprocessingInfo::C_StyleComment;
255 rose_typeid = PreprocessingInfo::CpreprocessorErrorDeclaration;
258 rose_typeid = PreprocessingInfo::CpreprocessorLineDeclaration;
261 rose_typeid = PreprocessingInfo::CpreprocessorUndefDeclaration;
264 rose_typeid = PreprocessingInfo::CpreprocessorWarningDeclaration;
270 std::cout <<
"Token to include directive: " << directive.get_value() << std::endl;
271 rose_typeid = PreprocessingInfo::CpreprocessorIncludeDeclaration;
273 case boost::wave::T_PP_HHEADER_NEXT:
275 std::cout <<
"Token to include next directive: " << directive.get_value() << std::endl;
276 rose_typeid = PreprocessingInfo::CpreprocessorIncludeNextDeclaration;
293 std::cout <<
"Error: Unknown preprocessor declaration found : " << directive.get_value().c_str() << std::endl;
294 std::cout << boost::wave::get_token_name(wave_typeid) <<
" " << directive.get_position().get_file().c_str() <<
" " << directive.get_position().get_line()
295 <<
" " << directive.get_position().get_column() << std::endl;
299 rose_typeid = PreprocessingInfo::CpreprocessorUnknownDeclaration;
307 token_container currentTokSeq2;
308 currentTokSeq2.push_back(directive);
309 std::string filename(directive.get_position().get_file().c_str());
310 if(currentMapOfAttributes.find(filename)==currentMapOfAttributes.end())
312 currentMapOfAttributes.find(filename)->second->addElement(*(
new PreprocessingInfo(currentTokSeq2,rose_typeid,PreprocessingInfo::before)));
329 template<
typename TokenT,
typename ParametersT,
typename DefinitionT>
330 PreprocessingInfo* build_preprocessingInfo_macro_def(TokenT& macro_name,
bool is_functionlike,
331 ParametersT& parameters, DefinitionT &definition,
bool is_predefined){
336 macro_def->is_functionlike = is_functionlike;
337 macro_def->is_predefined = is_predefined;
339 macro_def->macro_name = macro_name;
340 macro_def->paramaters = parameters;
342 copy (definition.begin(), definition.end(),
343 inserter(macro_def->definition, macro_def->definition.end()));
346 return preprocMacroDef;
354 template<
typename TokenT,
typename ParametersT,
typename DefinitionT>
355 void defined_macro(TokenT& macro_name,
bool is_functionlike,
356 ParametersT& parameters, DefinitionT &definition,
bool is_predefined){
357 if(skippedTokenStream != NULL){
359 if(macro_name.get_position().get_file().size()!=0){
360 if(macro_name.get_position().get_file()!=
"<built-in>")
361 skipped_token(macro_name,
true);
363 std::cout <<
"SKIPPED BECAUSE FILE IS NULL: " << macro_name.get_value().c_str() << std::endl;
375 PreprocessingInfo* preprocMacroDef = build_preprocessingInfo_macro_def(macro_name, is_functionlike, parameters, definition, is_predefined);
376 ROSE_ASSERT(preprocMacroDef != NULL);
378 Sg_File_Info* file_info = preprocMacroDef->get_file_info();
379 ROSE_ASSERT(file_info != NULL);
381 std::string filename = file_info->get_filenameString();
385 ROSE_ASSERT(filename !=
"");
387 if(currentMapOfAttributes.find(filename)==currentMapOfAttributes.end())
390 std::cout <<
"DONE adding to map" << std::endl;
391 currentMapOfAttributes.find(filename)->second->addElement(*preprocMacroDef);
393 std::cout <<
"Before mapKey" << std::endl;
395 std::pair<std::string,int> mapKey(filename,macro_name.get_position().get_line());
397 std::cout <<
"After mapKey" << std::endl;
399 std::cout <<
"ASXXX Defining macro: " << macro_name.get_value().c_str() << std::endl;
400 std::cout <<
"at " << filename <<
" l" << mapKey.second << std::endl;
409 if( defMap.find(mapKey) == defMap.end() )
410 defMap[mapKey]=preprocMacroDef;
419 template<
typename TokenT,
typename ContainerT>
420 bool expanding_function_like_macro(TokenT
const ¯odef, std::vector<TokenT>
const &formal_args,
421 ContainerT
const &definition, TokenT
const ¯ocall, std::vector<ContainerT>
const &arguments){
422 if(skippedTokenStream != NULL)
423 skipped_token(macrodef,
true);
442 if(rescan_macro_status==0){
446 std::cout <<
"DEFINITION: " << boost::wave::util::impl::as_string(definition);
448 std::cout <<
"\n MACRO CALL: " << macrocall.get_value() << std::endl;
451 ROSE_ASSERT(macro_call_to_expand == NULL);
453 macro_call_to_expand->is_functionlike =
true;
454 macro_call_to_expand->macro_call = macrocall;
459 std::pair<std::string,int> mapKey;
461 if(macrodef.get_position().get_file()!=
"<built-in>"){
462 mapKey.first = string(macrodef.get_position().get_file().c_str());
463 mapKey.second = macrodef.get_position().get_line();
464 ROSE_ASSERT( defMap.find(mapKey) != defMap.end() );
465 macro_call_to_expand->macro_def = defMap[mapKey];
469 bool is_function_like =
true;
470 bool is_predefined =
false;
471 macro_call_to_expand->macro_def = build_preprocessingInfo_macro_def(macrodef, is_function_like, formal_args, definition, is_predefined);
475 std::cout <<
"Did not find: " << macrodef.get_value().c_str() <<
" " << boost::wave::util::impl::as_string(definition) << std::endl;
476 std::cout <<
"in " << mapKey.first <<
" l " << mapKey.second << std::endl;
481 typedef typename std::vector<ContainerT>::const_iterator vec_call_iterator_t;
485 vec_call_iterator_t it = arguments.begin();
486 vec_call_iterator_t it_end = arguments.end();
492 std::cout <<
"ARGUMENTS:\n";
494 while (it != it_end ){
496 std::cout << boost::wave::util::impl::as_string(*it);
498 std::list<token_type> tk;
500 copy (it->begin(), it->end(),
501 inserter(tk, tk.end()));
503 macro_call_to_expand->arguments.push_back(tk);
510 ++rescan_macro_status;
514 typename testType::iterator it2 = test.begin();
515 typename testType::iterator it_end2 = test.end();
517 while (it2 != it_end2 ){
518 std::cout<< boost::wave::util::impl::as_string(*it2);
538 template<
typename TokenT,
typename ContainerT>
539 bool expanding_object_like_macro(TokenT
const ¯o, ContainerT
const &definition, TokenT
const ¯ocall){
540 if(skippedTokenStream != NULL)
541 skipped_token(macro,
true);
556 if(rescan_macro_status==0){
557 ROSE_ASSERT(macro_call_to_expand == NULL);
559 std::cout <<
"DEFINITION: " << boost::wave::util::impl::as_string(definition);
563 macro_call_to_expand->is_functionlike =
false;
564 macro_call_to_expand->macro_call = macrocall;
566 std::pair<std::string,int> mapKey;
568 if( (macro.get_position().get_file().size() != 0 ) && (macro.get_position().get_file()!=
"<built-in>")
569 && (macro.get_position().get_file()!=
"<command line>") ){
570 mapKey.first = string(macro.get_position().get_file().c_str());
571 mapKey.second = macro.get_position().get_line();
573 if( defMap.find(mapKey) == defMap.end() ){
574 std::cout <<
"Did not find: " << macro.get_value().c_str() <<
" " <<
575 macrocall.get_value().c_str() <<
" " << boost::wave::util::impl::as_string(definition) << std::endl;
576 std::cout <<
"in " << mapKey.first <<
" l " << mapKey.second << std::endl;
579 ROSE_ASSERT( defMap.find(mapKey) != defMap.end() );
580 macro_call_to_expand->macro_def = defMap[mapKey];
585 bool is_function_like =
false;
586 bool is_predefined =
false;
588 token_container parameters;
589 macro_call_to_expand->macro_def = build_preprocessingInfo_macro_def(macro, is_function_like, parameters, definition, is_predefined);
594 std::cout <<
"\n MACRO CALL: " << macrocall.get_value() << std::endl;
597 ++rescan_macro_status;
610 template <
typename ContainerT>
611 void expanded_macro(ContainerT
const&) {}
624 template <
typename ContainerT>
625 void rescanned_macro(ContainerT
const &result)
629 --rescan_macro_status;
630 ROSE_ASSERT( rescan_macro_status >= 0 );
632 std::cout <<
"Rescanned macro: " << boost::wave::util::impl::as_string(result) << std::endl;
634 if(rescan_macro_status==0){
635 ROSE_ASSERT(macro_call_to_expand != NULL);
637 copy (result.begin(), result.end(),
638 inserter(macro_call_to_expand->expanded_macro, macro_call_to_expand->expanded_macro.end()));
642 std::string filename(macro_call_to_expand->macro_call.get_position().get_file().c_str());
643 if(currentMapOfAttributes.find(filename)==currentMapOfAttributes.end())
645 currentMapOfAttributes.find(filename)->second->addElement(*(
new PreprocessingInfo(macro_call_to_expand,PreprocessingInfo::before)));
646 macro_call_to_expand = NULL;
657 template<
typename TokenIterator,
typename DirectiveType>
660 using result_type = bool;
661 result_type operator()(TokenIterator node, DirectiveType directive)
const {
662 using namespace boost::wave;
663 result_type returnValue =
false;
665 token_id wave_typeid = token_id(node);
666 if (wave_typeid == directive) {
681 template<
typename TokenIterator,
typename DirectiveType>
684 using result_type = bool;
685 result_type operator()(TokenIterator node, std::list<DirectiveType> directiveList)
const{
686 using namespace boost::wave;
687 result_type returnValue =
false;
689 token_id wave_typeid = token_id(node);
690 if (std::find(directiveList.begin(),directiveList.end(), wave_typeid) != directiveList.end()) {
699 template <
typename TokenT>
701 skipped_token(TokenT
const& token,
bool last_skipped =
false)
712 if(skippedTokenStream == NULL)
713 skippedTokenStream =
new token_container();
715 if(last_skipped ==
false)
718 std::cout <<
"Pushed Skipped Token: " << token.get_value().c_str() << std::endl;
719 skippedTokenStream->push_back(token);
723 skippedTokenStream->push_back(token);
727 std::cout <<
"Pushed Skipped Token: " << token.get_value().c_str() << std::endl;
728 std::cout <<
"Popping Skipped Tokens: " << boost::wave::util::impl::as_string(*skippedTokenStream).c_str() << std::endl;
730 std::string filename(skippedTokenStream->begin()->get_position().get_file().c_str());
731 if (currentMapOfAttributes.find(filename) == currentMapOfAttributes.end())
733 currentMapOfAttributes.find(filename)->second->addElement(*(
new PreprocessingInfo(*skippedTokenStream, PreprocessingInfo::CSkippedToken, PreprocessingInfo::before)));
735 ROSE_ASSERT(skippedTokenStream != NULL);
736 delete skippedTokenStream;
737 skippedTokenStream = NULL;
740 std::cout <<
"SKIPPED TOKEN: " << token.get_value().c_str() << std::endl;
743 inline void flush_token_stream()
745 if (skippedTokenStream == NULL || skippedTokenStream->begin() == skippedTokenStream->end())
748 std::string filename(skippedTokenStream->begin()->get_position().get_file().c_str());
749 if (currentMapOfAttributes.find(filename) == currentMapOfAttributes.end())
751 currentMapOfAttributes.find(filename)->second->addElement(*(
new PreprocessingInfo(*skippedTokenStream, PreprocessingInfo::CSkippedToken, PreprocessingInfo::before)));
752 delete skippedTokenStream;
753 skippedTokenStream = NULL;
756 template <
typename ContextT,
typename TokenT>
758 may_skip_whitespace(ContextT
const&, TokenT& token,
bool&)
760 using namespace boost::wave;
763 token_id
id = token_id(token);
764 bool skip =
id == T_PP_ELSE ||
id == T_PP_ELIF ||
id == T_PP_ENDIF;
765 using namespace boost::wave;
768 switch(token_id(token))
770 case T_PP_DEFINE: std::cout <<
"Skip White: #define\n";
break;
771 case T_PP_IF: std::cout <<
"Skip White: #if\n";
break;
772 case T_PP_IFDEF: std::cout <<
"Skip White: #ifdef\n";
break;
773 case T_PP_IFNDEF: std::cout <<
"Skip White: #ifndef\n";
break;
774 case T_PP_ELSE: std::cout <<
"Skip White: #else\n";
break;
775 case T_PP_ELIF: std::cout <<
"Skip White: #elif\n";
break;
776 case T_PP_ENDIF: std::cout <<
"Skip White: #endif\n";
break;
777 case T_PP_ERROR: std::cout <<
"Skip White: #error\n";
break;
778 case T_PP_LINE: std::cout <<
"Skip White: #line\n";
break;
779 case T_PP_PRAGMA: std::cout <<
"Skip White: #pragma\n";
break;
780 case T_PP_UNDEF: std::cout <<
"Skip White: #undef\n";
break;
781 case T_PP_WARNING: std::cout <<
"Skip White: #warning\n";
break;
782 case T_PP_INCLUDE: std::cout <<
"Skip White: #include \"...\"\n";
break;
783 case T_PP_QHEADER: std::cout <<
"Skip White: #include <...>\n";
break;
784 case T_PP_HHEADER: std::cout <<
"Skip White: #include ...\n";
break;
785 default: std::cout <<
"Skip White: <something else (" << token.get_value() <<
")>\n";
break;
790 skipped_token(token,
true);
794 if (skippedTokenStream != NULL)
798 std::cout <<
"Whitespace makes us pop skipped tokens: " << boost::wave::util::impl::as_string(*skippedTokenStream).c_str() << std::endl;
800 flush_token_stream();
803 std::cout <<
"Token stream is null?" << std::endl;
807 std::cout <<
"MAX_SKIP_WHITESPACE: " << token.get_value().c_str() << std::endl;
819 void attach_line_to_macro_call();
822 template<
typename IteratorT>
823 void attach_line_to_macro_call(std::vector<IteratorT*> vec){
826 template <
typename StringT,
typename IteratorT>
828 as_string(IteratorT it, IteratorT end)
831 for (; it != end; ++it)
833 result += (*it).get_value();