From: helly Date: Wed, 30 Mar 2005 23:23:06 +0000 (+0000) Subject: - Change to two pass generation which allows to avoid warnings about unused X-Git-Tag: 0.13.6~673 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=111175d7f88b0e5054ba7c69f77a178a7b2d1911;p=re2c - Change to two pass generation which allows to avoid warnings about unused labels. We first generate virtually everything and store which labels are used and then generate the stuff while only writing used labels. --- diff --git a/code.cc b/code.cc index 13198019..26982b14 100644 --- a/code.cc +++ b/code.cc @@ -260,6 +260,7 @@ void genGoTo(std::ostream &o, State *from, State *to, bool & readCh) o << "\tgoto yy" << to->label << ";\n"; ++oline; + vUsedLabels.append(to->label); } void genIf(std::ostream &o, char *cmp, uint v, bool &readCh) @@ -715,7 +716,10 @@ void Go::genGoto(std::ostream &o, State *from, State *next, bool &readCh) void State::emit(std::ostream &o, bool &readCh) { - o << "yy" << label << ":"; + if (vUsedLabels.contains(label)) + { + o << "yy" << label << ":"; + } /* o << "\nfprintf(stderr, \"<" << label << ">\");\n";*/ action->emit(o, readCh); } @@ -947,6 +951,38 @@ void DFA::split(State *s) s->go.span[0].to = move; } +class null_stream: public std::ostream +{ +public: + null_stream() + : std::ostream(&ns) + { + } + + null_stream& put(char_type) + { + // nothing to do + return *this; + } + + null_stream& write(const char_type *, std::streamsize) + { + // nothing to do + return *this; + } + +protected: + class null_streambuf: public std::streambuf + { + public: + null_streambuf() + : std::streambuf() + { + } + }; + null_streambuf ns; +}; + void DFA::emit(std::ostream &o) { static uint label = 0; @@ -1114,6 +1150,7 @@ void DFA::emit(std::ostream &o) o << "\tgoto yy" << label << ";\n"; ++oline; + vUsedLabels.append(label); (void) new Enter(head, label++); for (s = head; s; s = s->next) @@ -1121,6 +1158,17 @@ void DFA::emit(std::ostream &o) s->label = label++; } + null_stream noWhere; + + unsigned int nOrgOline = oline; + for (s = head; s; s = s->next) + { + bool readCh = false; + s->emit(noWhere, readCh); + s->go.genGoto(noWhere, s, s->next, readCh); + } + oline = nOrgOline; + for (s = head; s; s = s->next) { bool readCh = false; diff --git a/globals.h b/globals.h index 8a59104d..39f6f1db 100644 --- a/globals.h +++ b/globals.h @@ -3,6 +3,30 @@ #define _globals_h #include "basics.h" +#include +#include + +template +class label_list: protected std::list<_Ty> +{ +public: + label_list() + : std::list<_Ty>() + { + } + + void append(const _Ty &val) + { + push_back(val); + sort(); + unique(); + } + + bool contains(const _Ty &val) + { + return std::find(begin(), end(), val) != end(); + } +}; namespace re2c { @@ -18,6 +42,8 @@ extern uchar ebc2asc[256]; extern uchar *xlat, *talx; +extern label_list vUsedLabels; + } // end namespace re2c #endif diff --git a/main.cc b/main.cc index 78452626..77c7747a 100644 --- a/main.cc +++ b/main.cc @@ -21,6 +21,7 @@ char *outputFileName = 0; bool sFlag = false; bool bFlag = false; unsigned int oline = 1; +label_list vUsedLabels; using namespace std;