vcg.l 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* gcc-10 -fanalyzer vcg.flex.c:4433:1: warning: leak of ‘<unknown>’ [CWE-401] [-Wanalyzer-malloc-leak]
  2. * This does happen with older gcc versions, then use at least gcc version 11.2
  3. */
  4. /*
  5. * Copyright 2021
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * These are the four essential freedoms with GNU GPL software:
  21. * 1: freedom to run the program, for any purpose
  22. * 2: freedom to study how the program works, and change it to make it do what you wish
  23. * 3: freedom to redistribute copies to help your Free Software friends
  24. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  25. * , ,
  26. * / \
  27. * ((__-^^-,-^^-__))
  28. * `-_---' `---_-'
  29. * `--|o` 'o|--'
  30. * \ ` /
  31. * ): :(
  32. * :o_o:
  33. * "-"
  34. *
  35. * SPDX-License-Identifier: GPL-3.0+
  36. * License-Filename: LICENSE
  37. */
  38. /* lex the vcg data generated with gcc -fcallgraph-info option which is a subset of vcg language */
  39. %{
  40. #include "config.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <zlib.h>
  44. #include "splay-tree.h"
  45. #include "vcg.yy.h"
  46. #include "vcg.tab.h"
  47. #include "vcgus.h"
  48. #include "dpmem.h"
  49. /* use GNU GCC compiler builtin strlen */
  50. #undef YY_NEED_STRLEN
  51. /* increase read buffer */
  52. #undef YY_READ_BUF_SIZE
  53. /* #define YY_READ_BUF_SIZE (16*1024) */
  54. /* fread() with 128kb at once is fastest on Linux */
  55. #define YY_READ_BUF_SIZE (128*1024)
  56. /* Size of default input buffer. Do not tune. */
  57. #undef YY_BUF_SIZE
  58. #define YY_BUF_SIZE 16384
  59. /* at malloc error there will be a memory leak but it does also
  60. * #define YY_FATAL_ERROR(msg) exit(0)
  61. */
  62. /* char *p is used in flex coe */
  63. static char *ppointer = NULL;
  64. static char *qpointer = NULL;
  65. static char *tmpp = NULL;
  66. char *vcglaststring = NULL;
  67. static gzFile vcgzin = (gzFile)0;
  68. /* gzfread cannot be used because of older zlib in the dll */
  69. #undef YY_INPUT
  70. #define YY_INPUT(buf,result,max_size) do { \
  71. if ( (result = gzread(vcgzin, (char*)buf, (sizeof(char) * max_size) )) == 0) { \
  72. int estatus = 0; \
  73. const char *es = gzerror (vcgzin, &estatus); \
  74. if (estatus == Z_BUF_ERROR) { \
  75. YY_FATAL_ERROR( "gzread() in vcg.l flex scanner failed"); \
  76. } else { \
  77. if (estatus) { \
  78. printf ("%s(): zlib error status %d %s in vcg.l\n",__func__,(int)estatus,es); \
  79. } \
  80. } \
  81. } } while (0);
  82. /* own yyalloc
  83. * void *yyalloc (size_t n) { return(calloc(1,n)); }
  84. * void yyfree (void *ptr) { if (ptr) { free (ptr); } return; }
  85. * void *yyrealloc (void *ptr, size_t n) { return (realloc(ptr,n)); }
  86. */
  87. %}
  88. /* use own yyalloc
  89. * %option noyyalloc
  90. * %option noyyrealloc
  91. * %option noyyfree
  92. */
  93. %option noinput
  94. %option nounput
  95. %option noyywrap
  96. %option 8bit
  97. %option never-interactive
  98. %option yylineno
  99. %option noread
  100. ISTR [^\\\"]|\\.|\\\n
  101. STR \"({ISTR}*)\"
  102. CCS \/\*[^\*]*\*+([^\*\/][^\*]*\*+)*\/
  103. CCS0 \/\*
  104. CCS1 \*\/
  105. CCE \/\/[^\n]*
  106. %%
  107. {CCS} { /* c-comment style */ /* lexer does update yylineno */ }
  108. {CCS0} { /* start of c comment but no end of c comment */ }
  109. {CCS1} { /* end of c comment but no start of c comment */ }
  110. {CCE} { /* c++ comment style */ /* lexer does update yylineno */ }
  111. [\f ]+ { /* skip form feed chars and spaces */ }
  112. [\t] { /* skip tabs */ }
  113. [\n] { /* skip new line */ /* lexer does update yylineno */ }
  114. [\r] { /* skip carriage return */ }
  115. ":" { return (VCG_COLON); }
  116. "{" { return (VCG_BO); }
  117. "}" { return (VCG_BC); }
  118. "graph" { return (VCG_GRAPH); }
  119. "edge" { return (VCG_EDGE); }
  120. "ellipse" { return (VCG_ELLIPSE); }
  121. "label" { return (VCG_LABEL); }
  122. "node" { return (VCG_NODE); }
  123. "shape" { return (VCG_SHAPE); }
  124. "sourcename" { return (VCG_SOURCENAME); }
  125. "targetname" { return (VCG_TARGETNAME); }
  126. "title" { return (VCG_TITLE); }
  127. {STR} {
  128. if (yyleng == 2) {
  129. /* string is "" */
  130. vcglaststring = vcg_uniqstr ("");
  131. return (VCG_STRING);
  132. }
  133. /* copy and filter the text, and clear last " */
  134. yytext[yyleng-1] = 0;
  135. /* one byte to much but that is later on corrected */
  136. tmpp = (char *) dp_calloc (1, (yyleng+1));
  137. ppointer = yytext;
  138. ppointer++; /* skip first " */
  139. qpointer = tmpp;
  140. while (*ppointer)
  141. {
  142. if ((*ppointer) =='\\') {
  143. if((*(ppointer+1)) == 0) {
  144. *qpointer = '\\';
  145. qpointer++;
  146. ppointer++; /* "\" as last char */
  147. } else if ((*(ppointer+1)) =='\n') {
  148. ppointer = ppointer + 2; /* skip "\\n" sequence */
  149. } else if ((*(ppointer+1)) == 'n') {
  150. *qpointer = '\n'; /* */
  151. qpointer++;
  152. ppointer = ppointer + 2;
  153. } else if ((*(ppointer+1)) == '"') {
  154. *qpointer = '"'; /* */
  155. qpointer++;
  156. ppointer = ppointer + 2;
  157. } else if ((*(ppointer+1)) == '\\') {
  158. *qpointer = '\\';
  159. qpointer++;
  160. ppointer = ppointer + 2;
  161. } else {
  162. *qpointer = *ppointer; /* copy other esc sequences */
  163. ppointer++;
  164. qpointer++;
  165. }
  166. } else {
  167. *qpointer = *ppointer; /* copy regular chars */
  168. ppointer++;
  169. qpointer++;
  170. }
  171. }
  172. /* do a strdup() */
  173. ppointer = vcg_uniqstr (tmpp);
  174. vcglaststring = ppointer;
  175. tmpp = dp_free (tmpp);
  176. ppointer = NULL;
  177. qpointer = NULL;
  178. if (ppointer) {}
  179. if (qpointer) {}
  180. return (VCG_STRING);
  181. }
  182. . { return ((int)yytext[0]); }
  183. %%
  184. /* */
  185. void vcg_lex_init (gzFile f, int debugflag)
  186. {
  187. vcglineno=1;
  188. yyin = (FILE *)0;
  189. vcgzin = (gzFile)f;
  190. /* activate debug in lexer */
  191. yy_flex_debug = debugflag;
  192. /* activate debug in vcg parser */
  193. vcgdebug = debugflag;
  194. return;
  195. }
  196. /* */
  197. void vcg_lex_clear (void)
  198. {
  199. vcglineno=1;
  200. yylex_destroy();
  201. yy_flex_debug = 0;
  202. vcgzin = (gzFile)0;
  203. yyin = (FILE *)0;
  204. return;
  205. }
  206. void vcg_yydebug (int debugflag)
  207. {
  208. /* activate debug in lexer */
  209. vcg_flex_debug = debugflag;
  210. /* activate debug in vcg parser */
  211. vcgdebug = debugflag;
  212. return;
  213. }
  214. /* end. */