debug.c revision 1.1 1 /* $NetBSD: debug.c,v 1.1 2021/07/31 18:16:42 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Roland Illig <rillig (at) NetBSD.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 __RCSID("$NetBSD: debug.c,v 1.1 2021/07/31 18:16:42 rillig Exp $");
39 #endif
40
41 #include "lint1.h"
42
43
44 #ifdef DEBUG
45
46 static int debug_indentation = 0;
47
48
49 void __printflike(1, 2)
50 debug_printf(const char *fmt, ...)
51 {
52 va_list va;
53
54 va_start(va, fmt);
55 vfprintf(stdout, fmt, va);
56 va_end(va);
57 }
58
59 void
60 debug_indent(void)
61 {
62
63 debug_printf("%*s", 2 * debug_indentation, "");
64 }
65
66 void
67 debug_indent_inc(void)
68 {
69
70 debug_indentation++;
71 }
72
73 void
74 debug_indent_dec(void)
75 {
76
77 debug_indentation--;
78 }
79
80 void
81 (debug_enter)(const char *func)
82 {
83
84 printf("%*s+ %s\n", 2 * debug_indentation++, "", func);
85 }
86
87 void __printflike(1, 2)
88 debug_step(const char *fmt, ...)
89 {
90 va_list va;
91
92 debug_indent();
93 va_start(va, fmt);
94 vfprintf(stdout, fmt, va);
95 va_end(va);
96 printf("\n");
97 }
98
99 void
100 (debug_leave)(const char *func)
101 {
102
103 printf("%*s- %s\n", 2 * --debug_indentation, "", func);
104 }
105
106 void
107 debug_node(const tnode_t *tn, int indent)
108 {
109 op_t op;
110
111 if (tn == NULL) {
112 printf("%*s" "null\n", indent, "");
113 return;
114 }
115
116 op = tn->tn_op;
117 printf("%*s%s with type '%s'%s%s",
118 2 * indent, "",
119 op == CVT && !tn->tn_cast ? "convert" : modtab[op].m_name,
120 type_name(tn->tn_type), tn->tn_lvalue ? ", lvalue" : "",
121 tn->tn_parenthesized ? ", parenthesized" : "");
122
123 if (op == NAME)
124 printf(" %s\n", tn->tn_sym->s_name);
125 else if (op == CON && is_floating(tn->tn_type->t_tspec))
126 printf(", value %Lg", tn->tn_val->v_ldbl);
127 else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
128 printf(", value %llu\n", (unsigned long long)tn->tn_val->v_quad);
129 else if (op == CON && is_integer(tn->tn_type->t_tspec))
130 printf(", value %lld\n", (long long)tn->tn_val->v_quad);
131 else if (op == CON)
132 printf(", unknown value\n");
133 else if (op == STRING)
134 printf(", length %zu\n", tn->tn_string->st_len);
135 else {
136 printf("\n");
137
138 debug_node(tn->tn_left, indent + 1);
139 if (modtab[op].m_binary || tn->tn_right != NULL)
140 debug_node(tn->tn_right, indent + 1);
141 }
142 }
143
144 #endif
145