]> granicus.if.org Git - re2c/commitdiff
Forbid copying of 're2c::Substr'.
authorUlya Trofimovich <skvadrik@gmail.com>
Thu, 14 May 2015 17:20:13 +0000 (18:20 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Thu, 14 May 2015 17:20:13 +0000 (18:20 +0100)
Removed useless methods (most of them became useless after
're2c::Str' removal) and obsolete autoconf check for 'strndup'.

Removed 'Scanner::token' methods. They used
'Scanner::check_token_length', which was pretty useless:
1. checking for the lower should have always succeed, because
   'Scanner::tok' is always set to buffer start in 'Scanner::fill'
   and if the token was too long, it's start will be lost anyway.
2. checking for the upper bound could fail if re2c dev passed some
   trash into it, but any normal function would do so and this is
   no particular reason to have special runtime checks here.
Now substrings are constructed in lexer, where all the lengths
and bounds are easier to verify from lexing context (for re2c dev).

re2c/Makefile.am
re2c/bootstrap/scanner_lex.cc
re2c/configure.ac
re2c/src/dfa/actions.cc
re2c/src/parse/scanner.cc
re2c/src/parse/scanner.h
re2c/src/parse/scanner_lex.re
re2c/src/util/substr.cc [deleted file]
re2c/src/util/substr.h

index 47134af8771c3661ed6fe7faa9605e9ae2c2c5bd..a5bfd0a3beb4d6772b49adb9592509aa4d56cf00 100644 (file)
@@ -94,8 +94,7 @@ SRC = \
        $(srcdir)/src/mbo_getopt.cc \
        $(srcdir)/src/parse/input.cc \
        $(srcdir)/src/parse/scanner.cc \
-       $(srcdir)/src/util/range.cc \
-       $(srcdir)/src/util/substr.cc
+       $(srcdir)/src/util/range.cc
 # omit SRC_PARSER here; include it in EXTRA_DIST instead
 # (automake generates standard build rules for all YACC-ish
 # sources, they will conflict with our custom build rule).
index 9f35bbb21bc1d325da6e68c226e7f16988bb1ced..311c78edba051fb94978934f77562ddcc87f2f6c 100644 (file)
@@ -1,4 +1,4 @@
-/* Generated by re2c 0.14.1.dev on Thu May 14 13:56:12 2015*/
+/* Generated by re2c 0.14.1.dev on Thu May 14 17:19:41 2015*/
 #include <stdlib.h>
 #include <string.h>
 #include <iostream>
@@ -948,13 +948,14 @@ yy164:
                                        } else {
                                                /* Add one char in front and one behind instead of 's or "s */
                                                cur = cursor;
+                                               SubStr s (tok, cur - tok);
                                                if (bCaseInsensitive || bCaseInverted)
                                                {
-                                                       yylval.regexp = strToCaseInsensitiveRE(token());
+                                                       yylval.regexp = strToCaseInsensitiveRE (s);
                                                }
                                                else
                                                {
-                                                       yylval.regexp = strToRE(token());
+                                                       yylval.regexp = strToRE (s);
                                                }
                                                return STRING;
                                        }
@@ -1166,7 +1167,8 @@ yy191:
        ++YYCURSOR;
        {
                                        cur = cursor;
-                                       yylval.regexp = ranToRE(token());
+                                       SubStr s (tok, cur - tok);
+                                       yylval.regexp = ranToRE (s);
                                        return RANGE;
                                }
 yy193:
@@ -1179,7 +1181,8 @@ yy194:
        ++YYCURSOR;
        {
                                        cur = cursor;
-                                       yylval.regexp = invToRE(token());
+                                       SubStr s (tok, cur - tok);
+                                       yylval.regexp = invToRE (s);
                                        return RANGE;
                                }
 yy196:
@@ -1278,13 +1281,14 @@ yy218:
        ++YYCURSOR;
        {
                                        cur = cursor;
+                                       SubStr s (tok + 1, cur - tok - 2);
                                        if (bCaseInverted)
                                        {
-                                               yylval.regexp = strToRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToRE (s);
                                        }
                                        else
                                        {
-                                               yylval.regexp = strToCaseInsensitiveRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToCaseInsensitiveRE (s);
                                        }
                                        return STRING;
                                }
@@ -1307,13 +1311,14 @@ yy223:
        ++YYCURSOR;
        {
                                        cur = cursor;
+                                       SubStr s (tok + 1, cur - tok - 2);
                                        if (bCaseInsensitive || bCaseInverted)
                                        {
-                                               yylval.regexp = strToCaseInsensitiveRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToCaseInsensitiveRE (s);
                                        }
                                        else
                                        {
-                                               yylval.regexp = strToRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToRE (s);
                                        }
                                        return STRING;
                                }
@@ -2185,7 +2190,7 @@ yy351:
 yy352:
        {
                                        cur = cursor;
-                                       yylval.number = atoi(token().to_string().c_str());
+                                       yylval.number = atoi(std::string (tok, cur - tok).c_str());
                                        iscfg = 0;
                                        return NUMBER;
                                }
@@ -2429,7 +2434,7 @@ yy379:
 yy380:
        {
                                        cur = cursor;
-                                       cline = atoi(token().to_string().c_str());
+                                       cline = atoi(std::string (tok, cur - tok).c_str());
                                        goto sourceline; 
                                }
 yy381:
@@ -2465,7 +2470,7 @@ yy387:
        ++YYCURSOR;
        {
                                        cur = cursor;
-                                       escape (in.file_name, token(1, cur - tok - 2).to_string());
+                                       escape (in.file_name, std::string (tok + 1, cur - tok - 2));
                                        goto sourceline; 
                                }
 }
index 86baefa2565f47630b8b2cc2edde42a636a1f01e..6137fc097982ea2eb37ad6f585533459c7357b12 100644 (file)
@@ -50,7 +50,7 @@ AC_CHECK_SIZEOF([0i8],       [], [[]])
 
 
 # checks for library functions
-AC_CHECK_FUNCS([strdup strndup])
+AC_CHECK_FUNCS([strdup])
 
 
 AC_CONFIG_FILES([\
index b3d576eb597db69f0ab6db22313ecb990d11f04e..4a533f4b2b96a131b2346bc8b67469be41cb4487 100644 (file)
@@ -757,7 +757,7 @@ RegExp * Scanner::matchSymbol(uint32_t c) const
                return new MatchOp(new Range(c, c + 1));
 }
 
-RegExp * Scanner::strToRE(SubStr s) const
+RegExp * Scanner::strToRE (SubStr & s) const
 {
        if (s.len == 0)
                return new NullOp;
@@ -770,7 +770,7 @@ RegExp * Scanner::strToRE(SubStr s) const
        return re;
 }
 
-RegExp * Scanner::strToCaseInsensitiveRE(SubStr s) const
+RegExp * Scanner::strToCaseInsensitiveRE (SubStr & s) const
 {
        if (s.len == 0)
                return new NullOp;
@@ -828,7 +828,7 @@ RegExp * Scanner::matchSymbolRange(Range * r) const
                return new MatchOp(r);
 }
 
-RegExp * Scanner::ranToRE(SubStr s) const
+RegExp * Scanner::ranToRE (SubStr & s) const
 {
        s.len -= 2;
        s.str += 1;
@@ -839,7 +839,7 @@ RegExp * Scanner::ranToRE(SubStr s) const
        return matchSymbolRange(mkRange(s));
 }
 
-RegExp * Scanner::invToRE(SubStr s) const
+RegExp * Scanner::invToRE (SubStr & s) const
 {
        s.len -= 3;
        s.str += 2;
index 168d5a770e64604320e33aa4800f9347228af2aa..4a4a2b0c0518d6975beb6d4a3984f0ce8a446b3d 100644 (file)
@@ -1,7 +1,13 @@
 #include <stdarg.h>
+#include <string.h>
 
 #include "scanner.h"
 
+// used by Scanner::fatal_at and Scanner::fatalf
+#if defined(_MSC_VER) && !defined(vsnprintf)
+#    define vsnprintf _vsnprintf
+#endif
+
 namespace re2c {
 
 void Scanner::config(const std::string & cfg, int num)
@@ -442,14 +448,6 @@ Scanner::~Scanner()
        }
 }
 
-void Scanner::check_token_length(char *pos, uint32_t len) const
-{
-       if (pos < bot || pos + len > top)
-       {
-               fatal("Token exceeds limit");
-       }
-}
-
 void Scanner::reuse()
 {
        next_label = 0;
index 9031a9c6feb752b0a10629a64d52f0ae8dd95657..88e81d902059f771bca20af339bbb96e90f45974 100644 (file)
@@ -83,24 +83,19 @@ public:
        void config(const std::string &, int);
        void config(const std::string &, const std::string &);
 
-       void check_token_length(char *pos, uint32_t len) const;
-       SubStr token() const;
-       SubStr token(uint32_t start, uint32_t len) const;
-
        uint32_t unescape(SubStr &s) const;
        std::string& unescape(SubStr& str_in, std::string& str_out) const;
 
-       Range * mkRange(SubStr &s) const;
-       Range * getRange(SubStr &s) const;
-       RegExp * matchSymbol(uint32_t c) const;
-       RegExp * matchSymbolRange(Range * r) const;
-       RegExp * strToName(SubStr s) const;
-       RegExp * strToRE(SubStr s) const;
-       RegExp * strToCaseInsensitiveRE(SubStr s) const;
-       RegExp * ranToRE(SubStr s) const;
-       RegExp * invToRE(SubStr s) const;
-       RegExp * mkDot() const;
-       RegExp * mkDefault() const;
+       Range * mkRange (SubStr & s) const;
+       Range * getRange (SubStr & s) const;
+       RegExp * matchSymbol (uint32_t c) const;
+       RegExp * matchSymbolRange (Range * r) const;
+       RegExp * strToRE (SubStr & s) const;
+       RegExp * strToCaseInsensitiveRE (SubStr & s) const;
+       RegExp * ranToRE (SubStr & s) const;
+       RegExp * invToRE (SubStr & s) const;
+       RegExp * mkDot () const;
+       RegExp * mkDefault () const;
 
        FORBID_COPY (Scanner);
 };
@@ -130,18 +125,6 @@ inline void Scanner::fatal(const char *msg) const
        fatal(0, msg);
 }
 
-inline SubStr Scanner::token() const
-{
-       check_token_length(tok, cur - tok);
-       return SubStr(tok, cur - tok);
-}
-
-inline SubStr Scanner::token(uint32_t start, uint32_t len) const
-{
-       check_token_length(tok + start, len);
-       return SubStr(tok + start, len);
-}
-
 } // end namespace re2c
 
 #endif
index 869ee04d1b3dcedc6ba33fee92aad283774cd900..8a5c24c14c867fae003ccc795eadc14551a55da9 100644 (file)
@@ -254,26 +254,28 @@ scan:
 
        dstring         {
                                        cur = cursor;
+                                       SubStr s (tok + 1, cur - tok - 2);
                                        if (bCaseInsensitive || bCaseInverted)
                                        {
-                                               yylval.regexp = strToCaseInsensitiveRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToCaseInsensitiveRE (s);
                                        }
                                        else
                                        {
-                                               yylval.regexp = strToRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToRE (s);
                                        }
                                        return STRING;
                                }
 
        sstring         {
                                        cur = cursor;
+                                       SubStr s (tok + 1, cur - tok - 2);
                                        if (bCaseInverted)
                                        {
-                                               yylval.regexp = strToRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToRE (s);
                                        }
                                        else
                                        {
-                                               yylval.regexp = strToCaseInsensitiveRE(token(1, cur - tok - 2));
+                                               yylval.regexp = strToCaseInsensitiveRE (s);
                                        }
                                        return STRING;
                                }
@@ -287,13 +289,15 @@ scan:
 
        istring         {
                                        cur = cursor;
-                                       yylval.regexp = invToRE(token());
+                                       SubStr s (tok, cur - tok);
+                                       yylval.regexp = invToRE (s);
                                        return RANGE;
                                }
 
        cstring         {
                                        cur = cursor;
-                                       yylval.regexp = ranToRE(token());
+                                       SubStr s (tok, cur - tok);
+                                       yylval.regexp = ranToRE (s);
                                        return RANGE;
                                }
 
@@ -384,13 +388,14 @@ scan:
                                        } else {
                                                /* Add one char in front and one behind instead of 's or "s */
                                                cur = cursor;
+                                               SubStr s (tok, cur - tok);
                                                if (bCaseInsensitive || bCaseInverted)
                                                {
-                                                       yylval.regexp = strToCaseInsensitiveRE(token());
+                                                       yylval.regexp = strToCaseInsensitiveRE (s);
                                                }
                                                else
                                                {
-                                                       yylval.regexp = strToRE(token());
+                                                       yylval.regexp = strToRE (s);
                                                }
                                                return STRING;
                                        }
@@ -580,7 +585,7 @@ value:
 /*!re2c
        number          {
                                        cur = cursor;
-                                       yylval.number = atoi(token().to_string().c_str());
+                                       yylval.number = atoi(std::string (tok, cur - tok).c_str());
                                        iscfg = 0;
                                        return NUMBER;
                                }
@@ -614,12 +619,12 @@ sourceline:
 /*!re2c        
        lineno          {
                                        cur = cursor;
-                                       cline = atoi(token().to_string().c_str());
+                                       cline = atoi(std::string (tok, cur - tok).c_str());
                                        goto sourceline; 
                                }
        dstring         {
                                        cur = cursor;
-                                       escape (in.file_name, token(1, cur - tok - 2).to_string());
+                                       escape (in.file_name, std::string (tok + 1, cur - tok - 2));
                                        goto sourceline; 
                                }
        "\n"                    {
diff --git a/re2c/src/util/substr.cc b/re2c/src/util/substr.cc
deleted file mode 100644 (file)
index 9bf4a41..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/* $Id$ */
-#include <string.h>
-#include <stdlib.h>
-
-#include "src/globals.h"
-#include "src/util/substr.h"
-
-#ifndef HAVE_STRNDUP
-
-char *strndup(const char *str, size_t len)
-{
-       char * ret = (char*)malloc(len + 1);
-       
-       memcpy(ret, str, len);
-       ret[len] = '\0';
-       return ret;
-}
-
-#endif
-
-namespace re2c
-{
-
-void SubStr::out(std::ostream& o) const
-{
-       o.write(str, len);
-}
-
-bool operator==(const SubStr &s1, const SubStr &s2)
-{
-       return (bool) (s1.len == s2.len && memcmp(s1.str, s2.str, s1.len) == 0);
-}
-
-} // end namespace re2c
index 4f6f5683b75c02c1b85832f5b0233b130028bc4d..7e6ca64fbea0fccc461404b7f065269ff6bc802e 100644 (file)
@@ -1,13 +1,8 @@
-/* $Id$ */
-#ifndef _substr_h
-#define _substr_h
+#ifndef __SUBSTR__
+#define __SUBSTR__
 
-#include <iostream>
-#include <string>
-#include <string.h>
-
-#include "config.h"
 #include "src/util/c99_stdint.h"
+#include "src/util/forbid_copy.h"
 
 namespace re2c
 {
@@ -19,71 +14,19 @@ public:
        const char * const org;
        uint32_t len;
 
-public:
-       friend bool operator==(const SubStr &, const SubStr &);
-       SubStr(const uint8_t *, uint32_t);
-       SubStr(const char*, uint32_t);
-       explicit SubStr(const char*);
-       SubStr(const SubStr&);
-       virtual ~SubStr();
-       void out(std::ostream&) const;
-       std::string to_string() const;
-       uint32_t ofs() const;
-
-private:
-       SubStr & operator = (const SubStr &);
+       inline SubStr (const char * s, uint32_t l)
+               : str (s)
+               , org (s)
+               , len (l)
+       {}
+       inline uint32_t ofs () const
+       {
+               return str - org;
+       }
+
+       FORBID_COPY (SubStr);
 };
 
-inline std::ostream& operator<<(std::ostream& o, const SubStr &s)
-{
-       s.out(o);
-       return o;
-}
-
-inline std::ostream& operator<<(std::ostream& o, const SubStr* s)
-{
-       return o << *s;
-}
-
-inline SubStr::SubStr(const uint8_t *s, uint32_t l)
-               : str((char*)s), org((char*)s), len(l)
-{ }
-
-inline SubStr::SubStr(const char *s, uint32_t l)
-               : str(s), org(s), len(l)
-{ }
-
-inline SubStr::SubStr(const char *s)
-               : str(s), org(s), len(strlen(s))
-{ }
-
-inline SubStr::SubStr(const SubStr &s)
-               : str(s.str), org(s.str), len(s.len)
-{ }
-
-inline SubStr::~SubStr()
-{ }
-
-inline std::string SubStr::to_string() const
-{
-       return str && len ? std::string(str, len) : std::string();
-}
-
-inline uint32_t SubStr::ofs() const
-{
-       return str - org;
-}
-
-} // end namespace re2c
-
-#ifndef HAVE_STRNDUP
-
-char *strndup(const char *str, size_t len);
-
-#endif
-
-#if defined(_MSC_VER) && !defined(vsnprintf)
-#define vsnprintf _vsnprintf
-#endif
+} // namespace re2c
 
-#endif
+#endif // __SUBSTR__