Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
cet::includer Class Reference

#include <includer.h>

Classes

struct  frame
 
struct  posinfo
 

Public Types

using const_iterator = std::string::const_iterator
 

Public Member Functions

 includer (std::string const &filename, cet::filepath_maker &abs_filename)
 
 includer (std::istream &is, cet::filepath_maker &abs_filename)
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
std::string whereis (const_iterator const &it) const
 
std::string highlighted_whereis (const_iterator const &it) const
 
std::string src_whereis (const_iterator const &it) const
 

Private Types

using uint = std::string::size_type
 

Private Member Functions

void include (int including_framenum, std::string const &filename, cet::filepath_maker &abs_filename)
 
void include (std::istream &is, cet::filepath_maker &abs_filename)
 
std::string backtrace (uint from_frame) const
 
void debug () const
 
posinfo get_posinfo (const_iterator const &it) const
 

Private Attributes

std::string text_ {}
 
std::vector< frameframes_
 
std::vector< std::stringrecursionStack_ {}
 

Detailed Description

Definition at line 23 of file includer.h.

Member Typedef Documentation

using cet::includer::const_iterator = std::string::const_iterator

Definition at line 27 of file includer.h.

using cet::includer::uint = std::string::size_type
private

Definition at line 24 of file includer.h.

Constructor & Destructor Documentation

includer::includer ( std::string const &  filename,
cet::filepath_maker abs_filename 
)
explicit

Definition at line 96 of file includer.cc.

98  : frames_{frame(0, begin_string(), 0, text_.size())}
99 {
100  include(0, filename, policy_filename);
101  frames_.emplace_back(0, end_string(), 0, text_.size());
102 }
void include(int including_framenum, std::string const &filename, cet::filepath_maker &abs_filename)
Definition: includer.cc:186
string filename
Definition: train.py:213
std::string text_
Definition: includer.h:86
std::vector< frame > frames_
Definition: includer.h:87
includer::includer ( std::istream &  is,
cet::filepath_maker abs_filename 
)
explicit

Definition at line 104 of file includer.cc.

105  : frames_{frame(0, begin_string(), 0, text_.size())}
106 {
107  include(is, policy_filename);
108  frames_.emplace_back(0, end_string(), 0, text_.size());
109 }
void include(int including_framenum, std::string const &filename, cet::filepath_maker &abs_filename)
Definition: includer.cc:186
std::string text_
Definition: includer.h:86
std::vector< frame > frames_
Definition: includer.h:87

Member Function Documentation

std::string includer::backtrace ( uint  from_frame) const
private

Definition at line 323 of file includer.cc.

324 {
325  std::ostringstream result;
326  // append the backtrace:
327  for (uint k = from_frame; k != 0u; k = frames_[k].including_framenum) {
328  result << "\nincluded from line " << frames_[k].starting_linenum
329  << " of file \"" << frames_[k].filename << '\"';
330  }
331  return result.str();
332 }
static QCString result
std::vector< frame > frames_
Definition: includer.h:87
unsigned uint
Definition: qglobal.h:351
const_iterator cet::includer::begin ( ) const
inline

Definition at line 35 of file includer.h.

36  {
37  return text_.begin();
38  }
std::string text_
Definition: includer.h:86
const_iterator cet::includer::cbegin ( ) const
inline

Definition at line 45 of file includer.h.

46  {
47  return text_.cbegin();
48  }
std::string text_
Definition: includer.h:86
const_iterator cet::includer::cend ( ) const
inline

Definition at line 50 of file includer.h.

51  {
52  return text_.cend();
53  }
std::string text_
Definition: includer.h:86
void includer::debug ( ) const
private

Definition at line 335 of file includer.cc.

336 {
337  std::ostringstream result;
338 
339  result << "\nframe[" << 0 << "] " << frames_[0].including_framenum << " "
340  << frames_[0].starting_linenum << " " << frames_[0].starting_textpos
341  << " " << frames_[0].filename << '\n';
342 
343  for (uint k = 1u; k != frames_.size(); ++k) {
344  uint starting_textpos = frames_[k - 1u].starting_textpos;
345  result << "\nframe[" << k << "] " << frames_[k].including_framenum << " "
346  << frames_[k].starting_linenum << " " << frames_[k].starting_textpos
347  << " " << frames_[k].filename << '\n'
348  << text_.substr(starting_textpos,
349  frames_[k].starting_textpos - starting_textpos);
350  }
351  std::cerr << result.str();
352 }
static QCString result
std::string text_
Definition: includer.h:86
std::vector< frame > frames_
Definition: includer.h:87
unsigned uint
Definition: qglobal.h:351
const_iterator cet::includer::end ( ) const
inline

Definition at line 40 of file includer.h.

41  {
42  return text_.end();
43  }
std::string text_
Definition: includer.h:86
includer::posinfo includer::get_posinfo ( const_iterator const &  it) const
private

Definition at line 112 of file includer.cc.

113 {
114  // locate the frame corresponding to the given iterator:
115  uint textpos = it - text_.begin();
116  uint framenum;
117  for (framenum = 1u; framenum != frames_.size(); ++framenum)
118  if (textpos < frames_[framenum].starting_textpos)
119  break;
120  frame const& this_frame = frames_[--framenum];
121 
122  // determine the line number within the corresponding file:
123  uint delta_line_num{};
124  if (this_frame.starting_textpos != textpos) {
125  auto const this_frame_begin = this_frame.nl_positions.cbegin();
126  auto const this_frame_end = this_frame.nl_positions.cend();
127  auto const lower =
128  std::upper_bound(this_frame_begin, this_frame_end, textpos);
129  delta_line_num = std::distance(this_frame_begin, lower);
130  }
131  uint const linenum = this_frame.starting_linenum + delta_line_num;
132 
133  // determine the character position within the corresponding line:
134  uint const newlinepos =
135  textpos == 0u ? std::string::npos : text_.find_last_of('\n', textpos - 1u);
136  uint const charpos = newlinepos == std::string::npos ?
137  textpos + 1 :
138  textpos - text_.find_last_of('\n', textpos - 1u);
139  return {textpos, linenum, charpos, framenum};
140 }
std::string text_
Definition: includer.h:86
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
std::vector< frame > frames_
Definition: includer.h:87
unsigned uint
Definition: qglobal.h:351
std::string includer::highlighted_whereis ( const_iterator const &  it) const

Definition at line 155 of file includer.cc.

156 {
157  posinfo const pos{get_posinfo(it)};
158  // prepare the current information:
159  std::ostringstream result;
160  result << "line " << pos.linenum << ", character " << pos.charpos
161  << ", of file \"" << frames_[pos.framenum].filename << '\"'
162  << backtrace(frames_[pos.framenum].including_framenum) << "\n\n";
163  auto cp0 = pos.charpos - 1;
164  uint eol = text_.find_first_of('\n', pos.textpos);
165  result << text_.substr(pos.textpos - cp0,
166  (eol == std::string::npos) ?
167  std::string::npos :
168  (eol - (pos.textpos - cp0)))
169  << "\n";
170  result << std::string(cp0, ' ') << "^";
171 
172  return result.str();
173 }
static QCString result
std::string string
Definition: nybbler.cc:12
std::string backtrace(uint from_frame) const
Definition: includer.cc:323
#define eol
std::string text_
Definition: includer.h:86
posinfo get_posinfo(const_iterator const &it) const
Definition: includer.cc:112
std::vector< frame > frames_
Definition: includer.h:87
unsigned uint
Definition: qglobal.h:351
void includer::include ( int  including_framenum,
std::string const &  filename,
cet::filepath_maker abs_filename 
)
private

Definition at line 186 of file includer.cc.

189 {
190  static std::string const inc_lit = std::string("#include");
191  static uint const min_sz = inc_lit.size() + 3u;
192 
193  // expand filename to obtain, per policy, path to file:
194  bool const use_cin = (filename == "-");
195  std::string const filepath = use_cin ? filename : policy_filename(filename);
196  std::string const canonical_filepath =
197  use_cin ? canonicalizePath(filepath) : filepath;
198 
199  // check for recursive #inclusion:
200  if (std::find(recursionStack_.crbegin(),
201  recursionStack_.crend(),
202  canonical_filepath) != recursionStack_.crend()) {
203  throw inc_exception(recursive)
204  << filename << " => " << filepath << backtrace(frames_.size() - 1u);
205  } else {
206  // Record opening of file.
207  recursionStack_.emplace_back(canonical_filepath);
208  }
209 
210  // open the #included file:
211  std::ifstream ifs;
212  if (!use_cin)
213  ifs.open(filepath.c_str(), std::ifstream::in);
214  std::istream& f = use_cin ? std::cin : ifs;
215  if (!f)
216  throw inc_exception(cant_open)
217  << filename << " => " << filepath << backtrace(frames_.size() - 1u);
218 
219  int const starting_linenum = 1;
220  frame new_frame(including_framenum, filepath, starting_linenum, text_.size());
221 
222  int linenum = 0;
223  // iterate over each line of the input file:
224  for (auto& line : getlines(f)) {
225  ++linenum;
226  if (line.find(inc_lit) != 0) { // ordinary line (not an #include)
227  text_.append(line).append(1, '\n');
228  new_frame.nl_positions.push_back(text_.size()); // Record newline
229  continue;
230  }
231 
232  // save buffered text:
233  frames_.push_back(new_frame);
234 
235  // record this #include's place:
236  new_frame.starting_linenum = linenum;
237  new_frame.starting_textpos = text_.size();
238  frames_.push_back(new_frame);
239 
240  // validate the rest of the #include line's syntax:
241  trim_right(line, " \t\r\n");
242  if (line.size() <= min_sz // too short?
243  || line[8] != ' ' // missing separator?
244  || line[9] != '\"' || line.end()[-1] != '\"' // missing either quote?
245  )
246  throw inc_exception(malformed)
247  << line << "\n at line " << linenum << " of file " << filepath;
248 
249  // process the #include:
250  std::string nextfilename(line.substr(min_sz - 1u, line.size() - min_sz));
251  include(frames_.size() - 1u, nextfilename, policy_filename);
252 
253  // prepare to resume where we left off:
254  new_frame.starting_linenum = linenum + 1;
255  new_frame.starting_textpos = text_.size();
256  } // for
257 
258  // save final buffered text:
259  frames_.push_back(new_frame);
260 
261  // Done with this file.
262  recursionStack_.pop_back();
263 }
std::string string
Definition: nybbler.cc:12
std::string & trim_right(std::string &source, std::string const &t=" ")
Definition: trim.h:33
void include(int including_framenum, std::string const &filename, cet::filepath_maker &abs_filename)
Definition: includer.cc:186
std::string backtrace(uint from_frame) const
Definition: includer.cc:323
string filename
Definition: train.py:213
std::string text_
Definition: includer.h:86
std::vector< std::string > recursionStack_
Definition: includer.h:88
string filepath
Definition: train.py:371
void line(double t, double *p, double &x, double &y, double &z)
std::vector< frame > frames_
Definition: includer.h:87
unsigned uint
Definition: qglobal.h:351
void includer::include ( std::istream &  is,
cet::filepath_maker abs_filename 
)
private

Definition at line 266 of file includer.cc.

267 {
268  static std::string const inc_lit = std::string("#include");
269  static uint const min_sz = inc_lit.size() + 3u;
270 
271  // expand filename to obtain, per policy, absolute path to file:
272  std::string const filepath = "-";
273 
274  // check the open file:
275  if (!f)
276  throw inc_exception(cant_open)
277  << filepath << backtrace(frames_.size() - 1u);
278 
279  int const starting_linenum = 1;
280  frame new_frame(0, filepath, starting_linenum, text_.size());
281 
282  int linenum = 0;
283  // iterate over each line of the input file:
284  for (auto& line : getlines(f)) {
285  ++linenum;
286  if (line.find(inc_lit) != 0) { // ordinary line (not an #include)
287  text_.append(line).append(1, '\n');
288  new_frame.nl_positions.push_back(text_.size()); // Record newline
289  continue;
290  }
291 
292  // save buffered text:
293  frames_.push_back(new_frame);
294 
295  // record this #include's place:
296  new_frame.starting_linenum = linenum;
297  new_frame.starting_textpos = text_.size();
298  frames_.push_back(new_frame);
299 
300  // validate the rest of the #include line's syntax:
301  trim_right(line, " \t\r\n");
302  if (line.size() <= min_sz // too short?
303  || line[8] != ' ' // missing separator?
304  || line[9] != '\"' || line.end()[-1] != '\"' // missing either quote?
305  )
306  throw inc_exception(malformed)
307  << line << "\n at line " << linenum << " of file " << filepath;
308 
309  // process the #include:
310  std::string nextfilename(line.substr(min_sz - 1u, line.size() - min_sz));
311  include(frames_.size() - 1u, nextfilename, policy_filename);
312 
313  // prepare to resume where we left off:
314  new_frame.starting_linenum = linenum + 1;
315  new_frame.starting_textpos = text_.size();
316  } // for
317 
318  // save final buffered text:
319  frames_.push_back(new_frame);
320 }
std::string string
Definition: nybbler.cc:12
std::string & trim_right(std::string &source, std::string const &t=" ")
Definition: trim.h:33
void include(int including_framenum, std::string const &filename, cet::filepath_maker &abs_filename)
Definition: includer.cc:186
std::string backtrace(uint from_frame) const
Definition: includer.cc:323
std::string text_
Definition: includer.h:86
string filepath
Definition: train.py:371
void line(double t, double *p, double &x, double &y, double &z)
std::vector< frame > frames_
Definition: includer.h:87
unsigned uint
Definition: qglobal.h:351
std::string includer::src_whereis ( const_iterator const &  it) const

Definition at line 176 of file includer.cc.

177 {
178  posinfo const pos{get_posinfo(it)};
179 
180  std::ostringstream result;
181  result << frames_[pos.framenum].filename << ':' << pos.linenum;
182  return result.str();
183 }
static QCString result
posinfo get_posinfo(const_iterator const &it) const
Definition: includer.cc:112
std::vector< frame > frames_
Definition: includer.h:87
std::string includer::whereis ( const_iterator const &  it) const

Definition at line 143 of file includer.cc.

144 {
145  posinfo const pos{get_posinfo(it)};
146  // prepare the current information:
147  std::ostringstream result;
148  result << "line " << pos.linenum << ", character " << pos.charpos
149  << ", of file \"" << frames_[pos.framenum].filename << '\"';
150 
151  return result.str() + backtrace(frames_[pos.framenum].including_framenum);
152 }
static QCString result
std::string backtrace(uint from_frame) const
Definition: includer.cc:323
posinfo get_posinfo(const_iterator const &it) const
Definition: includer.cc:112
std::vector< frame > frames_
Definition: includer.h:87

Member Data Documentation

std::vector<frame> cet::includer::frames_
private

Definition at line 87 of file includer.h.

std::vector<std::string> cet::includer::recursionStack_ {}
private

Definition at line 88 of file includer.h.

std::string cet::includer::text_ {}
private

Definition at line 86 of file includer.h.


The documentation for this class was generated from the following files: