From d765e9cb519e809988409bc34042d6e33fdacd29 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Mon, 14 Sep 2015 23:11:51 +0100 Subject: [PATCH] The generated '--skeleton' program now warns about undefined control flow. --- re2c/src/codegen/skeleton/generate_code.cc | 17 ++++++++++++++++- re2c/src/codegen/skeleton/skeleton.cc | 10 +++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/re2c/src/codegen/skeleton/generate_code.cc b/re2c/src/codegen/skeleton/generate_code.cc index 785bfdf4..7d849229 100644 --- a/re2c/src/codegen/skeleton/generate_code.cc +++ b/re2c/src/codegen/skeleton/generate_code.cc @@ -28,6 +28,12 @@ static void exact_uint (OutputFile & o, size_t width) void Skeleton::emit_prolog (OutputFile & o, uint32_t maxfill) const { + const uint32_t default_rule = sizeof_key == 1 + ? UINT8_MAX + : sizeof_key == 2 + ? UINT16_MAX + : UINT32_MAX; + o << "\n" << "#include "; o << "\n" << "#include // malloc, free"; o << "\n" << "#include // memset"; @@ -68,6 +74,15 @@ void Skeleton::emit_prolog (OutputFile & o, uint32_t maxfill) const o << "\n" << indString << "const long len_act = *cursor - token;"; o << "\n" << indString << "const long len_exp = (long) match_length (keys, i);"; o << "\n" << indString << "const YYKEYTYPE rule_exp = rule (keys, i);"; + o << "\n" << indString << "if (rule_exp == " << default_rule << ")"; + o << "\n" << indString << "{"; + o << "\n" << indString << indString << "fprintf"; + o << "\n" << indString << indString << indString << "( stderr"; + o << "\n" << indString << indString << indString << ", \"warning: control flow is undefined for input\""; + o << "\n" << indString << indString << indString << indString << "\" at position %ld, rerun re2c with '-W'\\n\""; + o << "\n" << indString << indString << indString << ", pos"; + o << "\n" << indString << indString << indString << ");"; + o << "\n" << indString << "}"; o << "\n" << indString << "if (len_act == len_exp && rule_act == rule_exp)"; o << "\n" << indString << "{"; o << "\n" << indString << indString << "*cursor = token + length (keys, i);"; @@ -77,7 +92,7 @@ void Skeleton::emit_prolog (OutputFile & o, uint32_t maxfill) const o << "\n" << indString << "{"; o << "\n" << indString << indString << "fprintf"; o << "\n" << indString << indString << indString << "( stderr"; - o << "\n" << indString << indString << indString << ", \"error at position %ld (iteration %u):\\n\""; + o << "\n" << indString << indString << indString << ", \"error: at position %ld (iteration %u):\\n\""; o << "\n" << indString << indString << indString << indString << "\"\\texpected: match length %ld, rule %u\\n\""; o << "\n" << indString << indString << indString << indString << "\"\\tactual: match length %ld, rule %u\\n\""; o << "\n" << indString << indString << indString << ", pos"; diff --git a/re2c/src/codegen/skeleton/skeleton.cc b/re2c/src/codegen/skeleton/skeleton.cc index 68cbc8b6..616ada98 100644 --- a/re2c/src/codegen/skeleton/skeleton.cc +++ b/re2c/src/codegen/skeleton/skeleton.cc @@ -60,7 +60,7 @@ Skeleton::Skeleton (const DFA & dfa) , line (dfa.line) , nodes_count (dfa.nStates + 1) // +1 for default state , nodes (new Node [nodes_count]) - , sizeof_key (0) + , sizeof_key (4) { Node * n; @@ -103,18 +103,14 @@ Skeleton::Skeleton (const DFA & dfa) // initialize size of key const uint32_t max = std::max (maxlen, maxrule); - if (max <= UINT8_MAX) + if (max < UINT8_MAX) { sizeof_key = 1; } - else if (max <= UINT16_MAX) + else if (max < UINT16_MAX) { sizeof_key = 2; } - else - { - sizeof_key = 4; - } } Skeleton::~Skeleton () -- 2.40.0