src
unreachable.cc
Go to the documentation of this file.
1 #include "src/util/c99_stdint.h"
2 #include <map>
3 #include <set>
4 #include <utility>
5 
6 #include "src/conf/warn.h"
7 #include "src/globals.h"
8 #include "src/ir/rule_rank.h"
9 #include "src/ir/skeleton/path.h"
11 #include "src/parse/rules.h"
12 
13 namespace re2c
14 {
15 
17 {
18  if (!reachable.empty ())
19  {
20  return;
21  }
22  else if (end ())
23  {
24  reachable.insert (rule);
25  }
26  else if (loop < 2)
27  {
28  local_inc _ (loop);
29  for (arcs_t::iterator i = arcs.begin (); i != arcs.end (); ++i)
30  {
31  i->first->calc_reachable ();
32  reachable.insert (i->first->reachable.begin (), i->first->reachable.end ());
33  }
34  }
35 }
36 
38 {
40  for (uint32_t i = 0; i < nodes_count; ++i)
41  {
42  const rule_rank_t r1 = nodes[i].rule.rank;
43  const std::set<rule_t> & rs = nodes[i].reachable;
44  for (std::set<rule_t>::const_iterator j = rs.begin (); j != rs.end (); ++j)
45  {
46  const rule_rank_t r2 = j->rank;
47  if (r1 == r2 || r2.is_none ())
48  {
49  rules[r1].reachable = true;
50  }
51  else
52  {
53  rules[r1].shadow.insert (r2);
54  }
55  }
56  }
57 
58  // warn about unreachable rules:
59  // - rules that are shadowed by other rules, e.g. rule '[a]' is shadowed by '[a] [^]'
60  // - infinite rules that consume infinitely many characters and fail on YYFILL, e.g. '[^]*'
61  // - rules that contain never-matching link, e.g. '[]' with option '--empty-class match-none'
62  // default rule '*' should not be reported
63  for (rules_t::const_iterator i = rules.begin (); i != rules.end (); ++i)
64  {
65  const rule_rank_t r = i->first;
66  if (!r.is_none () && !r.is_def () && !rules[r].reachable)
67  {
68  warn.unreachable_rule (cond, i->second, rules);
69  }
70  }
71 }
72 
73 } // namespace re2c
Node * nodes
Definition: skeleton.h:114
void calc_reachable()
Definition: unreachable.cc:16
Warn warn
Definition: warn.cc:11
rule_t rule
Definition: skeleton.h:78
bool is_def() const
Definition: rule_rank.cc:42
arcs_t arcs
Definition: skeleton.h:70
uint8_t loop
Definition: skeleton.h:75
const std::string cond
Definition: skeleton.h:110
std::set< rule_t > reachable
Definition: skeleton.h:89
const size_t nodes_count
Definition: skeleton.h:113
bool end() const
Definition: skeleton.cc:67
void unreachable_rule(const std::string &cond, const rule_info_t &rule, const rules_t &rules)
Definition: warn.cc:164
rules_t rules
Definition: skeleton.h:116
bool is_none() const
Definition: rule_rank.cc:37
void warn_unreachable_rules()
Definition: unreachable.cc:37
Definition: bitmap.cc:10
rule_rank_t rank
Definition: path.h:14