1 | /*
|
---|
2 | * TOPPERS Software
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
4 | *
|
---|
5 | * Copyright (C) 2007-2008 by TAKAGI Nobuhisa
|
---|
6 | *
|
---|
7 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
8 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
9 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
10 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
11 | * \¦C±Ìpð¨æÑºLÌ³ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
12 | * XR[hÉÜÜêĢ鱯D
|
---|
13 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
14 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
15 | gip
|
---|
16 | * Ò}j
|
---|
17 | AÈÇjÉCãLÌì \¦C±Ìpð¨æÑºL
|
---|
18 | * Ì³ÛØKèðfÚ·é±ÆD
|
---|
19 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
20 | * pūȢ`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
21 | * ÆD
|
---|
22 | * (a) Äzzɺ¤hL
|
---|
23 | gipÒ}j
|
---|
24 | AÈÇjÉCãLÌ
|
---|
25 | * ì \¦C±Ìpð¨æÑºLÌ³ÛØKèðfÚ·é±ÆD
|
---|
26 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
27 | * ñ·é±ÆD
|
---|
28 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
29 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
30 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
31 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
32 | * ÆÓ·é±ÆD
|
---|
33 | *
|
---|
34 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
35 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
36 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
37 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
38 | * ÌÓCðíÈ¢D
|
---|
39 | *
|
---|
40 | */
|
---|
41 | #include <cstring>
|
---|
42 | #include <algorithm>
|
---|
43 | #include <iostream>
|
---|
44 | #include <iterator>
|
---|
45 | #include "toppers/io.hpp"
|
---|
46 | #include "toppers/cpp.hpp"
|
---|
47 | #include "toppers/global.hpp"
|
---|
48 | #include "toppers/diagnostics.hpp"
|
---|
49 | #include "toppers/oil/preprocess.hpp"
|
---|
50 | #include <boost/spirit.hpp>
|
---|
51 |
|
---|
52 | namespace toppers
|
---|
53 | {
|
---|
54 | namespace oil
|
---|
55 | {
|
---|
56 |
|
---|
57 | /*!
|
---|
58 | * \brief ÃIAPI `INCLUDE'ÌWJ
|
---|
59 | * \param[in] in üÍt@CÌàe
|
---|
60 | * \param[out] out oÍt@CÌàe
|
---|
61 | * \param[in] codeset ¶R[h
|
---|
62 | * \param[out] dependencies ˶ÖWÌi[æiNULLÌêÍi[µÈ¢j
|
---|
63 | * \param[out] onces #pragma onceªLq³êÄ¢½t@C¼Ìi[æiNULLÌêÍi[µÈ¢j
|
---|
64 | *
|
---|
65 | * `INCLUDE' ÌøÍAÊITRON dlÌàÌÆÍÙÈèAñdøpÅÍÜ¸É #include w߯
|
---|
66 | * ¯¶`®ðp¢éB
|
---|
67 | */
|
---|
68 | void expand_include( text const& in, text& out, codeset_t codeset, std::set< std::string >* dependencies, std::set< std::string >* onces )
|
---|
69 | {
|
---|
70 | typedef text::container::const_iterator const_row_iterator;
|
---|
71 | typedef std::string::size_type size_type;
|
---|
72 | size_type const npos = std::string::npos;
|
---|
73 | const_row_iterator first( in.begin().get_row() ), last( in.end().get_row() );
|
---|
74 |
|
---|
75 | out.set_line( first->line.file, first->line.line );
|
---|
76 | for ( const_row_iterator iter( first ); iter != last; ++iter )
|
---|
77 | {
|
---|
78 | std::string const& buf = iter->buf;
|
---|
79 | char state = 0;
|
---|
80 |
|
---|
81 | for ( size_type i = 0, n = buf.size(); i != n; ++i )
|
---|
82 | {
|
---|
83 | char c = buf[i];
|
---|
84 | if ( c == '\'' || c == '\"' )
|
---|
85 | {
|
---|
86 | if ( state == 0 )
|
---|
87 | {
|
---|
88 | state = c; // ¶iñjeJn
|
---|
89 | }
|
---|
90 | else if ( buf[i - 1] != '\\' ) // \' Ü½Í \" ÅÍÈ¢
|
---|
91 | {
|
---|
92 | state = 0; // ¶iñjeI¹
|
---|
93 | }
|
---|
94 | else if ( ( codeset == shift_jis ) && ( i >= 2 ) && is_lead< shift_jis >( buf[i - 2] ) )
|
---|
95 | {
|
---|
96 | state = 0;
|
---|
97 | }
|
---|
98 | out.push_back( c );
|
---|
99 | }
|
---|
100 | else if ( state != 0 ) // ¶iñjeà
|
---|
101 | {
|
---|
102 | out.push_back( c );
|
---|
103 | }
|
---|
104 | else
|
---|
105 | {
|
---|
106 | using namespace boost::spirit;
|
---|
107 | std::string headername;
|
---|
108 | if ( iter == last )
|
---|
109 | {
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | text::const_iterator iter2( iter, i );
|
---|
113 | if ( iter2 == in.end() )
|
---|
114 | {
|
---|
115 | break;
|
---|
116 | }
|
---|
117 | parse_info< text::const_iterator > info
|
---|
118 | = parse( iter2, in.end(),
|
---|
119 | ( str_p( "#include" ) >> *space_p
|
---|
120 | >> '\"' >> ( +( anychar_p - '\"' ) )[ assign( headername ) ] >> '\"'
|
---|
121 | ) ); // sÔª¸êéÌÅAXLbvp[TÍgpµÈ¢
|
---|
122 | if ( info.hit )
|
---|
123 | {
|
---|
124 | std::vector< std::string > include_paths = get_global< std::vector< std::string > >( "include-path" );
|
---|
125 | std::string hname = search_include_file( include_paths.begin(), include_paths.end(), headername );
|
---|
126 | if ( hname.empty() ) // wb_¼ª©Â©çÈ¢
|
---|
127 | {
|
---|
128 | error( iter->line, _( "cannot open file `%1%\'" ), headername );
|
---|
129 | }
|
---|
130 | else if ( onces == 0 || onces->find( hname ) == onces->end() )
|
---|
131 | {
|
---|
132 | if ( dependencies != 0 )
|
---|
133 | {
|
---|
134 | dependencies->insert( hname );
|
---|
135 | }
|
---|
136 | out.push_back( ' ' ); // _~[ð}üµÄ¨©È¢ÆsÔª¸êé
|
---|
137 | preprocess( hname, out, codeset, dependencies, onces ); // wb_¼Åwè³ê½t@CÉεÄOðÄAIÉs¤
|
---|
138 | iter = info.stop.get_row();
|
---|
139 | i = info.stop.get_col() - 1;
|
---|
140 | if ( iter != last )
|
---|
141 | {
|
---|
142 | out.set_line( iter->line.file, iter->line.line );
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | if ( iter2.get_col() == 0 )
|
---|
149 | {
|
---|
150 | info = parse( iter2, in.end(),
|
---|
151 | ( *( space_p - eol_p ) >>
|
---|
152 | ch_p( '#' ) >> *space_p >> "pragma" >> *space_p
|
---|
153 | >> "once"
|
---|
154 | >> *( space_p - eol_p ) >> eol_p ) );
|
---|
155 | if ( info.hit )
|
---|
156 | {
|
---|
157 | if ( onces != 0 )
|
---|
158 | {
|
---|
159 | onces->insert( iter2.line().file );
|
---|
160 | }
|
---|
161 | out.push_back( ' ' );
|
---|
162 | iter = info.stop.get_row();
|
---|
163 | if ( iter != last )
|
---|
164 | {
|
---|
165 | out.set_line( iter->line.file, iter->line.line );
|
---|
166 | }
|
---|
167 | --iter; // CNg³êéÌÅ¢Á½ñß·B
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | if ( parse( iter2, in.end(),
|
---|
172 | ( str_p( "#include" ) >> *space_p >> '\"' >> +( anychar_p - ')' ) >> '\"') ).hit )
|
---|
173 | {
|
---|
174 | warning( iter->line, _( "probably, %1% argument of `%2%\' is illegal" ), _( "1st" ), "INCLUDE" );
|
---|
175 | }
|
---|
176 | out.push_back( c );
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*!
|
---|
184 | * \brief RtBM
|
---|
185 | [Vt@CÌO
|
---|
186 | * \param[in] file RtBM
|
---|
187 | [Vt@C¼
|
---|
188 | * \param[out] result OãÌàe
|
---|
189 | * \param[in] codeset ¶R[h
|
---|
190 | * \param[out] dependencies ˶ÖWÌi[æiNULLÌêÍi[µÈ¢j
|
---|
191 | * \param[out] onces #pragma onceªLq³êÄ¢½t@C¼Ìi[æiNULLÌêÍi[µÈ¢j
|
---|
192 | */
|
---|
193 | void preprocess( std::string const& file, text& result, codeset_t codeset, std::set< std::string >* dependencies, std::set< std::string >* onces )
|
---|
194 | {
|
---|
195 | std::string buf;
|
---|
196 | read( file, buf );
|
---|
197 | if ( buf.empty() )
|
---|
198 | {
|
---|
199 | return;
|
---|
200 | }
|
---|
201 | text txt;
|
---|
202 | txt.set_line( file, 1 );
|
---|
203 | remove_comment( buf.begin(), buf.end(), std::back_inserter( txt ), codeset );
|
---|
204 | expand_include( txt, result, codeset, dependencies, onces );
|
---|
205 | }
|
---|
206 |
|
---|
207 | }
|
---|
208 | }
|
---|