coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hash_include.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
6  * *
7  * Redistribution and use in source and binary forms, with or without *
8  * modification, are permitted provided that the following conditions *
9  * are met: *
10  * *
11  * Redistributions of source code must retain the above copyright *
12  * notice, this list of conditions and the following disclaimer. *
13  * *
14  * Redistributions in binary form must reproduce the above copyright *
15  * notice, this list of conditions and the following disclaimer in the *
16  * documentation and/or other materials provided with the distribution. *
17  * *
18  * Neither the name of Mike Kinghan nor the names of its contributors *
19  * may be used to endorse or promote products derived from this software *
20  * without specific prior written permission. *
21  * *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
32  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
33  * DAMAGE. *
34  * *
35  **************************************************************************/
36 #include "hash_include.h"
37 #include "diagnostic.h"
38 #include "citable.h"
39 #include "options.h"
40 #include "io.h"
41 #include "canonical.h"
42 #include "reference.h"
43 #include <iostream>
44 
49 using namespace std;
50 
51 bool hash_include::valid() const
52 {
53  string fn = filename();
54  size_t len = fn.length();
55  bool valid = len > 2U && len == fn.length();
56  if (valid) {
57  valid = ((fn[0] == '<' && fn[len -1] == '>') ||
58  (fn[0] == '\"' && fn[len - 1] == '\"'));
59  }
60  return valid;
61 }
62 
64 {
65  string arg;
66  if (identifier::is_start_char(*chew)) {
67  string id = identifier::read(chew);
68  symbol::locator sloc(id);
69  _ref.reset(new reference(sloc,chew));
70  arg = _ref->invocation();
71  } else {
72  size_t startoff = size_t(chew);
73  size_t endoff = size_t(chew(header_name));
74  if (startoff < endoff) {
75  chew.cursor(startoff);
76  arg = citable(chew,endoff - startoff);
77  } else {
78  arg = canonical<string>(chew);
79  }
80  }
81  return arg;
82 }
83 
85 {
86  if (!line_despatch::cur_line().reportable() ||
88  return;
89  }
90  shared_ptr<reference> ref = symbolic_argument();
91  if (_directive.reported()) {
92  if (ref && ref->callee()->configured() && !ref->reported()) {
93  _directive.set_reported(false);
94  }
95  }
96  if (_directive.reported() && options::list_only_once()) {
97  return;
98  }
100  (options::list_local_includes() && local_header()) ||
101  (options::list_system_includes() && system_header())) {
102  cout << "#include ";
103  if (ref) {
104  cout << _directive.argument() << ": symbolic argument, ";
105  if (ref->callee()->configured()) {
106  if (ref->callee()->self_referential()) {
107  cout << "insoluble, because of circular definitions";
108  } else {
109  cout << "expands as >>" << filename() << "<<";
110  }
111  } else {
112  cout << "insoluble, because unconfigured";
113  }
114 
115  } else {
116  cout << filename();
117  }
118  if (options::list_location()) {
119  cout << ": " << io::in_file_name()
120  << '(' << line_despatch::cur_line().num() << ')';
121  }
122  cout << '\n';
123  _directive.set_reported();
124  }
125 }
126 
127 /* EOF */
static parsed_line & cur_line()
Get a reference to the current output line.
Definition: line_despatch.h:68
static bool list_only_once()
Do we report only the first occurrence of listed items?
Definition: options.h:117
void report()
Report the #include directive.
bool is_start_char(char ch)
Say whether a character can be the first of an identifier.
Definition: identifier.h:52
The symbols command.
Definition: options.h:76
struct symbol::locator encapsulates a symbol table entry.
Definition: symbol.h:79
static bool list_local_includes()
Do we list local #include directives?
Definition: options.h:157
static command_code get_command()
Get the operative coan command code.
Definition: options.cpp:247
static bool list_system_includes()
Do we list system #include directives?
Definition: options.h:153
static std::string in_file_name()
Get the name of the current source file.
Definition: io.h:105
std::string citable(chewer< std::string > &chew, size_t len=std::string::npos)
Make a citable version of length-delimited text.
unsigned num() const
Get the greatest source line number spanned by this line.
Definition: parsed_line.h:70
bool valid() const
Say whether the #include directive is valid.
std::string read(chewer< CharSeq > &chew)
Read an identifier from an chewer<CharSeq>
static bool list_location()
Do we report file and line numbers for listed items?
Definition: options.h:113
chew_mode::header_name const header_name
An exemplar chew_mode::header_name
Definition: chew.h:225
`template struct chewer<CharSeq> is a cursor-like type that is associated with a character-sequence t...
Definition: chew.h:248
template class canonical<What> encapsulates the canonical representation of values of type What...
Definition: canonical.h:82
std::string read(chewer< parse_buffer > &chew)
Read the body of an #include directive.
size_t cursor() const
Get the scanning position.
Definition: chew.h:370