File: IncludeExpander.cc

package info (click to toggle)
aspectc%2B%2B 1.0pre4~svn.20090918-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 117,308 kB
  • ctags: 410,601
  • sloc: cpp: 1,883,007; ansic: 17,279; sh: 2,190; makefile: 1,088
file content (252 lines) | stat: -rw-r--r-- 8,557 bytes parent folder | download | duplicates (2)
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
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003  The 'ac++' developers (see aspectc.org)
//                                                                
// This program is free software;  you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
// GNU General Public License for more details.                   
//                                                                
// You should have received a copy of the GNU General Public      
// License along with this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            

#include "IncludeExpander.h"
#include "LineDirectiveMgr.h"

#include "Puma/PreTreeNodes.h"
#include "Puma/PreSonIterator.h"
#include "Puma/MacroUnit.h"
#include "Puma/FileUnit.h"
#include "Puma/CProject.h"
#include "Puma/UnitManager.h"
#include "Puma/CCParser.h"
#include "Puma/CTranslationUnit.h"
#include "Puma/Token.h"
#include "Puma/Location.h"

void IncludeExpander::expand (const char *file) {

  // clear the resulting unit
  _unit.List::clear ();

  // preprocess the translation unit
  Unit *unit = _project.unitManager ().getUnit (file, true);

  // return if the unit is empty (strange property of the UnitManager!)
  if (!unit || !unit->first ())
    return;

  // ok, we have a unit to expand
  _root = unit;
  
  // silently run the preprocessor
  CCParser parser;
  CTranslationUnit *tunit = parser.parse (*_root , _project, 2);
  
  _forced_includes = false;
  _depth = 0;
  _ext_depth = 0;
  _files.push (File (_root));

  // search for include and manipulate them -> fills _tree and _nodes
  iterateNodes (tunit->cpp_tree ());

  // cleanup
  delete tunit; 

  // rewind the include stack until we are back at the input file
  rewind (0);

  // write the rest of the main unit
  finish ();
}

// Go through the nodes.
void IncludeExpander::iterateNodes (PreTree* node) {
  PreSonIterator i (node); // the order is important!

  for (i.first (); ! i.isDone (); i.next ())
    i.currentItem ()->accept (*this);
}

// expand tokens from a unit (write them into the result unit)
void IncludeExpander::write (Unit *unit, Token *first, Token *last) {
  ACUnit linedir (_err);
  if (_line_mgr.directive (linedir, unit, first)) {
    linedir << endu;
    linedir.cut (_unit, (Token*)linedir.first (), (Token*)linedir.last ());
  }
  // copy the tokens
  Token *curr = first;
  while (true) {
    // filter out #pragma once
    if (curr->is_directive () && strcmp ("#pragma", curr->text ()) == 0) {
      Token *dir = (Token*)unit->next (curr);
      bool have_pragma_once = false;
      while (dir->is_directive () && strcmp ("#pragma", dir->text ()) != 0) {
        if (strcmp ("once", dir->text ()) == 0) {
          have_pragma_once = true;
          break;
        }
        dir = (Token*)unit->next (dir);
      }
      if (have_pragma_once)
        curr = (Token*)unit->next (dir);
    }
    _unit.append (*(Token*)curr->duplicate ());
    if (curr == last)
      break;
    curr = (Token*)unit->next (curr);
  }
//  List *copy = unit->copy (first, last);
//  copy->cut (_unit, (Token*)copy->first (), (Token*)copy->last ());
//  delete copy;
}

// write a string into the unit
void IncludeExpander::write (const char *str) {
  ACUnit str_unit (_err);
  str_unit << str << endu;
  str_unit.cut (_unit, (Token*)str_unit.first (), (Token*)str_unit.last ());
}

// print the rest of the topmost unit
void IncludeExpander::finish () {
  Token *first = (Token*)_files.top ()._next;
  Token *last  = (Token*)_files.top ()._unit->last ();
  if (first)
    write (_files.top ()._unit, first, last);
}

// pop elements from the include stack until 'up_to' is the top
void IncludeExpander::rewind (int up_to) {
  while (_depth > up_to) {
    finish ();
    _files.pop ();
    _depth--;
  }
}

// handle include directive node	
void IncludeExpander::visitPreIncludeDirective_Pre (PreIncludeDirective* node)
{
  // this_unit is the unit where the include directive is located
  Token *this_token = node->startToken ();
  Unit *this_unit   = (Unit*)this_token->belonging_to ();
//  cout << "in " << this_unit->name () << " " << this_token->location () << " "
//    << _project.isBelow (this_unit) << endl;
        
  if (node->is_forced () || (this_unit == _root && node->depth () == 0))
    _forced_includes = false;
  else if (_forced_includes)
    return;
    
  // manipulate project files only (and forced includes)
  if (node->is_forced () || _project.isBelow (this_unit) ||
      this_unit == _root) {

    // include if expanded by preprocessor
    if (node->daughters () == 1) {
      
      Unit *included_unit = ((PreInclSemNode*)node->daughter (0))->unit ();
      // a guarded unit is treaded as if it was not included
      if (((PreInclSemNode*)node->daughter (0))->guarded ()) {
        included_unit = 0;
      }

//      cout << "included: " << included_unit->name () << " " <<
//        _project.isBelow (included_unit) << endl; 

      if (_ext_depth) {
//        cout << "node depth = " << node->depth () << endl;
//        cout << "ext depth  = " << _ext_depth << endl;
        if (node->depth () < _ext_depth) {
          // we are back!
          _ext_depth = 0;
//          cout << "cleared last" << endl;
        }
        else {
          // problem: we are in a project file, which was included by an external
          return;
        }
      }

      // expand includes of project files only
      if (!included_unit || _project.isBelow (included_unit)) {

        if (_depth > node->depth ())
          rewind (node->depth ());

        if (!node->is_forced ()) {
          // advance the current position up the include
          Token *first = _files.top ()._next;
          if (first && first != this_token) {
            Token *last = (Token*)this_unit->prev (this_token);
            write (this_unit, first, last);
          }
          Token *last = node->endToken ();
          // this might be a macro-generated include -> find the correct next tok
          if (last->is_macro_generated ()) {
            last = ((MacroUnit*)last->belonging_to ())->ExpansionEnd (last);
            assert (last && last->belonging_to () == this_unit);
          }
          _files.top ()._next = (Token*)this_unit->next (last);
        }
        
        if (included_unit) {
          // enter the next include file
          _files.push (File (included_unit));
          _depth++;
        }
      }
      else {
        if (node->is_forced ()) {
          // print the rest of nested includes first
          rewind (0);
          _depth = 0;
          
          // if the included unit does not belong to the project we generate an
          // #include directive so that the backend compiler does not have to
          // include the unit in any case
          const char *path = ((FileUnit*)included_unit)->absolutePath ();
          ostringstream generated_include;
          generated_include << "#include \"" << path << '\"' << endl;
          write (generated_include.str ().c_str ());
          _forced_includes = true;
        }
        else {
          // remember that we leave the project now
//          cout << "_last = " << this_unit->name () << endl;
          if (!_ext_depth) {
            _ext_depth = node->depth () + 1; // depth of external includes
//            cout << "ext_depth = " << _ext_depth << endl;
          }
        }
      }
    }
    // include was e.g. in false ifdef branch
    else {
//      cout << "no daughter" << endl;
    }
  }
  else {
    // in external file!
    if (node->daughters () == 1) {
      Unit *included_unit = ((PreInclSemNode*)node->daughter (0))->unit ();
      if (included_unit && _project.isBelow (included_unit)) {
        // oops, an external file includes an internal => error!
        _err << sev_error << this_token->location ()
             << "internal file '" << included_unit->name ()
             << "' is included by external '" << this_unit->name () << "'"
             << endMessage;
        return;
      }
    }
  }
}