File: PumaPreprocessor.cc

package info (click to toggle)
aspectc%2B%2B 1%3A2.2%2Bgit20181008-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,704 kB
  • sloc: cpp: 110,629; ansic: 7,644; sh: 2,192; makefile: 1,317; pascal: 634; python: 402; xml: 349
file content (156 lines) | stat: -rw-r--r-- 4,939 bytes parent folder | download | duplicates (6)
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
// 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 "PumaPreprocessor.h"
#include "IncludeGraph.h"
#include "IntroductionUnit.h"

using namespace Puma;
using namespace std;


PumaPreprocessor::PumaPreprocessor (const string &tunit_name, ACConfig &conf, CProject &project) :
  _tunit_name (tunit_name), _conf (conf), _project (project), _local_units(_project.err ()),
  _cpp (&_project.err (), &_project.unitManager (), &_local_units, cout),
  _provider(_cpp), _recorded_tokens (0) {

  // scan file
  _unit = _project.scanFile (tunit_name.c_str());
  if (!_unit)
    return;

  // prepare C preprocessor
  _stream.push (_unit);
  _project.unitManager ().init ();
  _cpp.macroManager ()->init (_unit->name ());
  _cpp.stream (&_stream);
  _cpp.configure (_project.config ());
  _provider.init ();

  // fetch the first token
  locate_token ();

}

// return the include graph *after* preprocessing the translation unit
void PumaPreprocessor::get_include_graph (IncludeGraph &include_graph) {
  // finally parse the preprocessor syntax tree in order to create an
  // include graph of the original source code
  include_graph.init (_cpp.syntaxTree());
}

Puma::ErrorStream &PumaPreprocessor::err () const {
  return _project.err ();
}

bool PumaPreprocessor::keywords_enabled (Puma::Token *tok) const {
  if (!tok)
    return false;

  Unit *unit = tok->unit ();
  while (unit && unit->isMacroExp ()) {
    unit = ((MacroUnit*)unit)->CallingUnit ();
  }

  if (!unit->isFile ()) // TODO: is that case possible?
    return false;

  if (!_project.isBelow(unit))
    return false;

  size_t len = strlen (unit->name ());
  if (len > 2 && strcmp (unit->name () + (len - 3), ".ah") == 0)
    return true;

  return _conf.keywords();
}

ACToken PumaPreprocessor::next_token () {
  _provider.next ();
  ACToken result = locate_token ();
  if (_recorded_tokens && result != ACToken::TOK_EOF)
    _recorded_tokens->append(*result.get ()->duplicate());
  return result;
}

ACToken PumaPreprocessor::curr_token () {
  Token *token = _provider.current ();
  return ACToken (token, keywords_enabled (token));
}

ACToken PumaPreprocessor::look_ahead (int n) {
  TokenProvider::State state = _provider.get_state ();
  while (n > 0) {
    _provider.next ();
    locate_token ();
    n--;
  }
  Token *token = _provider.current ();
  _provider.set_state (state);
  return ACToken (token, keywords_enabled(token));
}

ACToken PumaPreprocessor::locate_token () {
  Token *token;
  while ((token = _provider.current ()) && ! token->is_core ()) {
// TODO: handle directives here?
//    if (token->is_directive ())
//      handle_directive ();
//    else
      _provider.next ();
  }
  return ACToken (token, keywords_enabled(token));
}

// functions to record the token stream

void PumaPreprocessor::start_recording () {
  if (!_recorded_tokens) {
    _recorded_tokens = new Unit;
//    _recorded_tokens->name(((Unit*)_jpm.source_unit(curr_token()))->name ());
    _recorded_tokens->append(*curr_token().get ()->duplicate());
  }
}

Unit *PumaPreprocessor::stop_recording () {
  Unit *result = _recorded_tokens;
  _recorded_tokens = 0;
  return result;
}

FileUnit *PumaPreprocessor::source_unit (ACToken token) {
  Unit *unit   = token.unit ();
  while (unit && unit->isMacroExp ()) {
    unit = ((MacroUnit*)unit)->CallingUnit ();
  }
  // if the 'insert unit' is an 'introduction unit', find the intro target unit
  IntroductionUnit *intro_unit = IntroductionUnit::cast (unit);
  if (intro_unit) unit = intro_unit->final_target_unit ();

  // at this point we must have reached a file unit
  if (!unit->isFile ())
    return 0;

  return (FileUnit*)unit;
}

// check whether a token is located within the files of the project
bool PumaPreprocessor::is_in_project(ACToken token) const {
  FileUnit *funit = source_unit(token);
  return funit && _project.isBelow(funit);
}