* gcc.dg/vmx/unpack.c: Use dg-additional-options rather than
[official-gcc.git] / gcc / tree-browser.c
bloba73131d6b6d3cbf1526f42a1c3f6bbc3f7ce603a
1 /* Tree browser.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "alias.h"
25 #include "tree.h"
26 #include "options.h"
27 #include "tree-pretty-print.h"
28 #include "print-tree.h"
30 #define TB_OUT_FILE stdout
31 #define TB_IN_FILE stdin
32 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
33 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
35 /* Structures for handling Tree Browser's commands. */
36 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
37 enum TB_Comm_code {
38 #include "tree-browser.def"
39 TB_UNUSED_COMMAND
41 #undef DEFTBCODE
42 typedef enum TB_Comm_code TB_CODE;
44 struct tb_command {
45 const char *help_msg;
46 const char *comm_text;
47 size_t comm_len;
48 TB_CODE comm_code;
51 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
52 static const struct tb_command tb_commands[] =
54 #include "tree-browser.def"
56 #undef DEFTBCODE
58 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
59 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
60 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
61 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
64 /* Next structure is for parsing TREE_CODEs. */
65 struct tb_tree_code {
66 enum tree_code code;
67 const char *code_string;
68 size_t code_string_len;
71 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
72 #define END_OF_BASE_TREE_CODES \
73 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
74 static const struct tb_tree_code tb_tree_codes[] =
76 #include "all-tree.def"
78 #undef DEFTREECODE
79 #undef END_OF_BASE_TREE_CODES
81 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
82 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
83 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
86 /* Function declarations. */
88 static long TB_getline (char **, long *, FILE *);
89 static TB_CODE TB_get_command (char *);
90 static enum tree_code TB_get_tree_code (char *);
91 static tree find_node_with_code (tree *, int *, void *);
92 static tree store_child_info (tree *, int *, void *);
93 static void TB_update_up (tree);
94 static tree TB_current_chain_node (tree);
95 static tree TB_prev_expr (tree);
96 static tree TB_next_expr (tree);
97 static tree TB_up_expr (tree);
98 static tree TB_first_in_bind (tree);
99 static tree TB_last_in_bind (tree);
100 static tree TB_history_prev (void);
102 /* FIXME: To be declared in a .h file. */
103 void browse_tree (tree);
105 /* Hashtable helpers. */
106 struct tree_upper_hasher : nofree_ptr_hash<tree_node>
108 static inline bool equal (const value_type &, const compare_type &);
111 inline bool
112 tree_upper_hasher::equal (const value_type &parent, const compare_type &node)
114 if (parent == NULL || node == NULL)
115 return 0;
117 if (EXPR_P (parent))
119 int n = TREE_OPERAND_LENGTH (parent);
120 int i;
121 for (i = 0; i < n; i++)
122 if (node == TREE_OPERAND (parent, i))
123 return true;
125 return false;
128 /* Static variables. */
129 static hash_table<tree_upper_hasher> *TB_up_ht;
130 static vec<tree, va_gc> *TB_history_stack;
131 static int TB_verbose = 1;
134 /* Entry point in the Tree Browser. */
136 void
137 browse_tree (tree begin)
139 tree head;
140 TB_CODE tbc = TB_UNUSED_COMMAND;
141 ssize_t rd;
142 char *input = NULL;
143 long input_size = 0;
145 fprintf (TB_OUT_FILE, "\nTree Browser\n");
147 #define TB_SET_HEAD(N) do { \
148 vec_safe_push (TB_history_stack, N); \
149 head = N; \
150 if (TB_verbose) \
151 if (head) \
153 print_generic_expr (TB_OUT_FILE, head, 0); \
154 fprintf (TB_OUT_FILE, "\n"); \
156 } while (0)
158 TB_SET_HEAD (begin);
160 /* Store in a hashtable information about previous and upper statements. */
162 TB_up_ht = new hash_table<tree_upper_hasher> (1023);
163 TB_update_up (head);
166 while (24)
168 fprintf (TB_OUT_FILE, "TB> ");
169 rd = TB_getline (&input, &input_size, TB_IN_FILE);
171 if (rd == -1)
172 /* EOF. */
173 goto ret;
175 if (rd != 1)
176 /* Get a new command. Otherwise the user just pressed enter, and thus
177 she expects the last command to be reexecuted. */
178 tbc = TB_get_command (input);
180 switch (tbc)
182 case TB_UPDATE_UP:
183 TB_update_up (head);
184 break;
186 case TB_MAX:
187 if (head && (INTEGRAL_TYPE_P (head)
188 || TREE_CODE (head) == REAL_TYPE
189 || TREE_CODE (head) == FIXED_POINT_TYPE))
190 TB_SET_HEAD (TYPE_MAX_VALUE (head));
191 else
192 TB_WF;
193 break;
195 case TB_MIN:
196 if (head && (INTEGRAL_TYPE_P (head)
197 || TREE_CODE (head) == REAL_TYPE
198 || TREE_CODE (head) == FIXED_POINT_TYPE))
199 TB_SET_HEAD (TYPE_MIN_VALUE (head));
200 else
201 TB_WF;
202 break;
204 case TB_ELT:
205 if (head && TREE_CODE (head) == TREE_VEC)
207 /* This command takes another argument: the element number:
208 for example "elt 1". */
209 TB_NIY;
211 else if (head && TREE_CODE (head) == VECTOR_CST)
213 /* This command takes another argument: the element number:
214 for example "elt 1". */
215 TB_NIY;
217 else
218 TB_WF;
219 break;
221 case TB_VALUE:
222 if (head && TREE_CODE (head) == TREE_LIST)
223 TB_SET_HEAD (TREE_VALUE (head));
224 else
225 TB_WF;
226 break;
228 case TB_PURPOSE:
229 if (head && TREE_CODE (head) == TREE_LIST)
230 TB_SET_HEAD (TREE_PURPOSE (head));
231 else
232 TB_WF;
233 break;
235 case TB_IMAG:
236 if (head && TREE_CODE (head) == COMPLEX_CST)
237 TB_SET_HEAD (TREE_IMAGPART (head));
238 else
239 TB_WF;
240 break;
242 case TB_REAL:
243 if (head && TREE_CODE (head) == COMPLEX_CST)
244 TB_SET_HEAD (TREE_REALPART (head));
245 else
246 TB_WF;
247 break;
249 case TB_BLOCK:
250 if (head && TREE_CODE (head) == BIND_EXPR)
251 TB_SET_HEAD (TREE_OPERAND (head, 2));
252 else
253 TB_WF;
254 break;
256 case TB_SUBBLOCKS:
257 if (head && TREE_CODE (head) == BLOCK)
258 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
259 else
260 TB_WF;
261 break;
263 case TB_SUPERCONTEXT:
264 if (head && TREE_CODE (head) == BLOCK)
265 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
266 else
267 TB_WF;
268 break;
270 case TB_VARS:
271 if (head && TREE_CODE (head) == BLOCK)
272 TB_SET_HEAD (BLOCK_VARS (head));
273 else if (head && TREE_CODE (head) == BIND_EXPR)
274 TB_SET_HEAD (TREE_OPERAND (head, 0));
275 else
276 TB_WF;
277 break;
279 case TB_REFERENCE_TO_THIS:
280 if (head && TYPE_P (head))
281 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
282 else
283 TB_WF;
284 break;
286 case TB_POINTER_TO_THIS:
287 if (head && TYPE_P (head))
288 TB_SET_HEAD (TYPE_POINTER_TO (head));
289 else
290 TB_WF;
291 break;
293 case TB_BASETYPE:
294 if (head && TREE_CODE (head) == OFFSET_TYPE)
295 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
296 else
297 TB_WF;
298 break;
300 case TB_ARG_TYPES:
301 if (head && (TREE_CODE (head) == FUNCTION_TYPE
302 || TREE_CODE (head) == METHOD_TYPE))
303 TB_SET_HEAD (TYPE_ARG_TYPES (head));
304 else
305 TB_WF;
306 break;
308 case TB_METHOD_BASE_TYPE:
309 if (head && (TREE_CODE (head) == FUNCTION_TYPE
310 || TREE_CODE (head) == METHOD_TYPE)
311 && TYPE_METHOD_BASETYPE (head))
312 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
313 else
314 TB_WF;
315 break;
317 case TB_FIELDS:
318 if (head && (TREE_CODE (head) == RECORD_TYPE
319 || TREE_CODE (head) == UNION_TYPE
320 || TREE_CODE (head) == QUAL_UNION_TYPE))
321 TB_SET_HEAD (TYPE_FIELDS (head));
322 else
323 TB_WF;
324 break;
326 case TB_DOMAIN:
327 if (head && TREE_CODE (head) == ARRAY_TYPE)
328 TB_SET_HEAD (TYPE_DOMAIN (head));
329 else
330 TB_WF;
331 break;
333 case TB_VALUES:
334 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
335 TB_SET_HEAD (TYPE_VALUES (head));
336 else
337 TB_WF;
338 break;
340 case TB_ARG_TYPE:
341 if (head && TREE_CODE (head) == PARM_DECL)
342 TB_SET_HEAD (DECL_ARG_TYPE (head));
343 else
344 TB_WF;
345 break;
347 case TB_INITIAL:
348 if (head && DECL_P (head))
349 TB_SET_HEAD (DECL_INITIAL (head));
350 else
351 TB_WF;
352 break;
354 case TB_RESULT:
355 if (head && DECL_P (head))
356 TB_SET_HEAD (DECL_RESULT_FLD (head));
357 else
358 TB_WF;
359 break;
361 case TB_ARGUMENTS:
362 if (head && DECL_P (head))
363 TB_SET_HEAD (DECL_ARGUMENTS (head));
364 else
365 TB_WF;
366 break;
368 case TB_ABSTRACT_ORIGIN:
369 if (head && DECL_P (head))
370 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
371 else if (head && TREE_CODE (head) == BLOCK)
372 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
373 else
374 TB_WF;
375 break;
377 case TB_ATTRIBUTES:
378 if (head && DECL_P (head))
379 TB_SET_HEAD (DECL_ATTRIBUTES (head));
380 else if (head && TYPE_P (head))
381 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
382 else
383 TB_WF;
384 break;
386 case TB_CONTEXT:
387 if (head && DECL_P (head))
388 TB_SET_HEAD (DECL_CONTEXT (head));
389 else if (head && TYPE_P (head)
390 && TYPE_CONTEXT (head))
391 TB_SET_HEAD (TYPE_CONTEXT (head));
392 else
393 TB_WF;
394 break;
396 case TB_OFFSET:
397 if (head && TREE_CODE (head) == FIELD_DECL)
398 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
399 else
400 TB_WF;
401 break;
403 case TB_BIT_OFFSET:
404 if (head && TREE_CODE (head) == FIELD_DECL)
405 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
406 else
407 TB_WF;
408 break;
410 case TB_UNIT_SIZE:
411 if (head && DECL_P (head))
412 TB_SET_HEAD (DECL_SIZE_UNIT (head));
413 else if (head && TYPE_P (head))
414 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
415 else
416 TB_WF;
417 break;
419 case TB_SIZE:
420 if (head && DECL_P (head))
421 TB_SET_HEAD (DECL_SIZE (head));
422 else if (head && TYPE_P (head))
423 TB_SET_HEAD (TYPE_SIZE (head));
424 else
425 TB_WF;
426 break;
428 case TB_TYPE:
429 if (head && TREE_TYPE (head))
430 TB_SET_HEAD (TREE_TYPE (head));
431 else
432 TB_WF;
433 break;
435 case TB_DECL_SAVED_TREE:
436 if (head && TREE_CODE (head) == FUNCTION_DECL
437 && DECL_SAVED_TREE (head))
438 TB_SET_HEAD (DECL_SAVED_TREE (head));
439 else
440 TB_WF;
441 break;
443 case TB_BODY:
444 if (head && TREE_CODE (head) == BIND_EXPR)
445 TB_SET_HEAD (TREE_OPERAND (head, 1));
446 else
447 TB_WF;
448 break;
450 case TB_CHILD_0:
451 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
452 TB_SET_HEAD (TREE_OPERAND (head, 0));
453 else
454 TB_WF;
455 break;
457 case TB_CHILD_1:
458 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
459 TB_SET_HEAD (TREE_OPERAND (head, 1));
460 else
461 TB_WF;
462 break;
464 case TB_CHILD_2:
465 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
466 TB_SET_HEAD (TREE_OPERAND (head, 2));
467 else
468 TB_WF;
469 break;
471 case TB_CHILD_3:
472 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
473 TB_SET_HEAD (TREE_OPERAND (head, 3));
474 else
475 TB_WF;
476 break;
478 case TB_PRINT:
479 if (head)
480 debug_tree (head);
481 else
482 TB_WF;
483 break;
485 case TB_PRETTY_PRINT:
486 if (head)
488 print_generic_stmt (TB_OUT_FILE, head, 0);
489 fprintf (TB_OUT_FILE, "\n");
491 else
492 TB_WF;
493 break;
495 case TB_SEARCH_NAME:
497 break;
499 case TB_SEARCH_CODE:
501 enum tree_code code;
502 char *arg_text;
504 arg_text = strchr (input, ' ');
505 if (arg_text == NULL)
507 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
508 break;
510 code = TB_get_tree_code (arg_text + 1);
512 /* Search in the subtree a node with the given code. */
514 tree res;
516 res = walk_tree (&head, find_node_with_code, &code, NULL);
517 if (res == NULL_TREE)
519 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
521 else
523 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
524 TB_SET_HEAD (res);
527 break;
530 #define TB_MOVE_HEAD(FCT) do { \
531 if (head) \
533 tree t; \
534 t = FCT (head); \
535 if (t) \
536 TB_SET_HEAD (t); \
537 else \
538 TB_WF; \
540 else \
541 TB_WF; \
542 } while (0)
544 case TB_FIRST:
545 TB_MOVE_HEAD (TB_first_in_bind);
546 break;
548 case TB_LAST:
549 TB_MOVE_HEAD (TB_last_in_bind);
550 break;
552 case TB_UP:
553 TB_MOVE_HEAD (TB_up_expr);
554 break;
556 case TB_PREV:
557 TB_MOVE_HEAD (TB_prev_expr);
558 break;
560 case TB_NEXT:
561 TB_MOVE_HEAD (TB_next_expr);
562 break;
564 case TB_HPREV:
565 /* This command is a little bit special, since it deals with history
566 stack. For this reason it should keep the "head = ..." statement
567 and not use TB_MOVE_HEAD. */
568 if (head)
570 tree t;
571 t = TB_history_prev ();
572 if (t)
574 head = t;
575 if (TB_verbose)
577 print_generic_expr (TB_OUT_FILE, head, 0);
578 fprintf (TB_OUT_FILE, "\n");
581 else
582 TB_WF;
584 else
585 TB_WF;
586 break;
588 case TB_CHAIN:
589 /* Don't go further if it's the last node in this chain. */
590 if (head && TREE_CODE (head) == BLOCK)
591 TB_SET_HEAD (BLOCK_CHAIN (head));
592 else if (head && TREE_CHAIN (head))
593 TB_SET_HEAD (TREE_CHAIN (head));
594 else
595 TB_WF;
596 break;
598 case TB_FUN:
599 /* Go up to the current function declaration. */
600 TB_SET_HEAD (current_function_decl);
601 fprintf (TB_OUT_FILE, "Current function declaration.\n");
602 break;
604 case TB_HELP:
605 /* Display a help message. */
607 int i;
608 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
609 for (i = 0; i < TB_UNUSED_COMMAND; i++)
611 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
614 break;
616 case TB_VERBOSE:
617 if (TB_verbose == 0)
619 TB_verbose = 1;
620 fprintf (TB_OUT_FILE, "Verbose on.\n");
622 else
624 TB_verbose = 0;
625 fprintf (TB_OUT_FILE, "Verbose off.\n");
627 break;
629 case TB_EXIT:
630 case TB_QUIT:
631 /* Just exit from this function. */
632 goto ret;
634 default:
635 TB_NIY;
639 ret:;
640 delete TB_up_ht;
641 TB_up_ht = NULL;
642 return;
646 /* Search the first node in this BIND_EXPR. */
648 static tree
649 TB_first_in_bind (tree node)
651 tree t;
653 if (node == NULL_TREE)
654 return NULL_TREE;
656 while ((t = TB_prev_expr (node)))
657 node = t;
659 return node;
662 /* Search the last node in this BIND_EXPR. */
664 static tree
665 TB_last_in_bind (tree node)
667 tree t;
669 if (node == NULL_TREE)
670 return NULL_TREE;
672 while ((t = TB_next_expr (node)))
673 node = t;
675 return node;
678 /* Search the parent expression for this node. */
680 static tree
681 TB_up_expr (tree node)
683 tree res;
684 if (node == NULL_TREE)
685 return NULL_TREE;
687 res = TB_up_ht->find (node);
688 return res;
691 /* Search the previous expression in this BIND_EXPR. */
693 static tree
694 TB_prev_expr (tree node)
696 node = TB_current_chain_node (node);
698 if (node == NULL_TREE)
699 return NULL_TREE;
701 node = TB_up_expr (node);
702 if (node && TREE_CODE (node) == COMPOUND_EXPR)
703 return node;
704 else
705 return NULL_TREE;
708 /* Search the next expression in this BIND_EXPR. */
710 static tree
711 TB_next_expr (tree node)
713 node = TB_current_chain_node (node);
715 if (node == NULL_TREE)
716 return NULL_TREE;
718 node = TREE_OPERAND (node, 1);
719 return node;
722 static tree
723 TB_current_chain_node (tree node)
725 if (node == NULL_TREE)
726 return NULL_TREE;
728 if (TREE_CODE (node) == COMPOUND_EXPR)
729 return node;
731 node = TB_up_expr (node);
732 if (node)
734 if (TREE_CODE (node) == COMPOUND_EXPR)
735 return node;
737 node = TB_up_expr (node);
738 if (TREE_CODE (node) == COMPOUND_EXPR)
739 return node;
742 return NULL_TREE;
745 /* For each node store in its children nodes that the current node is their
746 parent. This function is used by walk_tree. */
748 static tree
749 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
750 void *data ATTRIBUTE_UNUSED)
752 tree node;
753 tree_node **slot;
755 node = *tp;
757 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
758 if (EXPR_P (node))
760 int n = TREE_OPERAND_LENGTH (node);
761 int i;
762 for (i = 0; i < n; i++)
764 tree op = TREE_OPERAND (node, i);
765 slot = TB_up_ht->find_slot (op, INSERT);
766 *slot = node;
770 /* Never stop walk_tree. */
771 return NULL_TREE;
774 /* Update information about upper expressions in the hash table. */
776 static void
777 TB_update_up (tree node)
779 while (node)
781 walk_tree (&node, store_child_info, NULL, NULL);
783 /* Walk function's body. */
784 if (TREE_CODE (node) == FUNCTION_DECL)
785 if (DECL_SAVED_TREE (node))
786 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
788 /* Walk rest of the chain. */
789 node = TREE_CHAIN (node);
791 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
794 /* Parse the input string for determining the command the user asked for. */
796 static TB_CODE
797 TB_get_command (char *input)
799 unsigned int mn, size_tok;
800 int comp;
801 char *space;
803 space = strchr (input, ' ');
804 if (space != NULL)
805 size_tok = strlen (input) - strlen (space);
806 else
807 size_tok = strlen (input) - 1;
809 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
811 if (size_tok != TB_COMMAND_LEN (mn))
812 continue;
814 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
815 if (comp == 0)
816 /* Here we just determined the command. If this command takes
817 an argument, then the argument is determined later. */
818 return TB_COMMAND_CODE (mn);
821 /* Not a valid command. */
822 return TB_UNUSED_COMMAND;
825 /* Parse the input string for determining the tree code. */
827 static enum tree_code
828 TB_get_tree_code (char *input)
830 unsigned int mn, size_tok;
831 int comp;
832 char *space;
834 space = strchr (input, ' ');
835 if (space != NULL)
836 size_tok = strlen (input) - strlen (space);
837 else
838 size_tok = strlen (input) - 1;
840 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
842 if (size_tok != TB_TREE_CODE_LEN (mn))
843 continue;
845 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
846 if (comp == 0)
848 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
849 return TB_TREE_CODE (mn);
853 /* This isn't a valid code. */
854 return LAST_AND_UNUSED_TREE_CODE;
857 /* Find a node with a given code. This function is used as an argument to
858 walk_tree. */
860 static tree
861 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
862 void *data)
864 enum tree_code *code;
865 code = (enum tree_code *) data;
866 if (*code == TREE_CODE (*tp))
867 return *tp;
869 return NULL_TREE;
872 /* Returns a pointer to the last visited node. */
874 static tree
875 TB_history_prev (void)
877 if (!vec_safe_is_empty (TB_history_stack))
879 tree last = TB_history_stack->last ();
880 TB_history_stack->pop ();
881 return last;
883 return NULL_TREE;
886 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
887 (and null-terminate it). *LINEPTR is a pointer returned from malloc
888 (or NULL), pointing to *N characters of space. It is realloc'd as
889 necessary. Returns the number of characters read (not including the
890 null terminator), or -1 on error or EOF.
891 This function comes from sed (and is supposed to be a portable version
892 of getline). */
894 static long
895 TB_getline (char **lineptr, long *n, FILE *stream)
897 char *line, *p;
898 long size, copy;
900 if (lineptr == NULL || n == NULL)
902 errno = EINVAL;
903 return -1;
906 if (ferror (stream))
907 return -1;
909 /* Make sure we have a line buffer to start with. */
910 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
912 #ifndef MAX_CANON
913 #define MAX_CANON 256
914 #endif
915 line = (char *) xrealloc (*lineptr, MAX_CANON);
916 if (line == NULL)
917 return -1;
918 *lineptr = line;
919 *n = MAX_CANON;
922 line = *lineptr;
923 size = *n;
925 copy = size;
926 p = line;
928 while (1)
930 long len;
932 while (--copy > 0)
934 register int c = getc (stream);
935 if (c == EOF)
936 goto lose;
937 else if ((*p++ = c) == '\n')
938 goto win;
941 /* Need to enlarge the line buffer. */
942 len = p - line;
943 size *= 2;
944 line = (char *) xrealloc (line, size);
945 if (line == NULL)
946 goto lose;
947 *lineptr = line;
948 *n = size;
949 p = line + len;
950 copy = size - len;
953 lose:
954 if (p == *lineptr)
955 return -1;
957 /* Return a partial line since we got an error in the middle. */
958 win:
959 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
960 if (p - 2 >= *lineptr && p[-2] == '\r')
961 p[-2] = p[-1], --p;
962 #endif
963 *p = '\0';
964 return p - *lineptr;