Debian Codegraph @

src/complexity.h

raw
1
2 /*
3 * This file is part of Complexity.
4 * Complexity Copyright (c) 2011-2020 by Bruce Korb - all rights reserved
5 *
6 * Complexity is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Complexity is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef COMPLEXITY_H_GUARD
21 #define COMPLEXITY_H_GUARD
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "char-types.h"
34 #include "opts.h"
35
36 #ifndef NUL
37 # define NUL '\0'
38 #endif
39
40 #ifndef NL
41 # define NL '\n'
42 #endif
43
44 #ifndef CR
45 # define CR '\r'
46 #endif
47
48 #ifndef HT
49 # define HT '\t'
50 #endif
51
52 #ifndef VT
53 # define VT '\v'
54 #endif
55
56 #ifndef FF
57 # define FF '\f'
58 #endif
59
60 #define BSLASH '\\'
61 #define FSLASH '/'
62 #define DQUOT '"'
63 #define SQUOT '\''
64
65 #define TOKEN_TABLE \
66 _Ttbl_( 0, TKN_EMPTY) \
67 _Ttbl_( 1, TKN_EOF) \
68 _Ttbl_( 2, TKN_NAME) \
69 _Ttbl_( 3, TKN_NUMBER) \
70 _Ttbl_( 4, TKN_REL_OP) \
71 _Ttbl_( 5, TKN_ARITH_OP) \
72 _Ttbl_( 6, TKN_LOGIC_AND) \
73 _Ttbl_( 7, TKN_LOGIC_OR) \
74 _Ttbl_( 8, TKN_ASSIGN) \
75 _Ttbl_( 9, TKN_ELLIPSIS) \
76 \
77 _Ttbl_( 11, TKN_KW_CASE) \
78 _Ttbl_( 12, TKN_KW_DEFAULT) \
79 _Ttbl_( 13, TKN_KW_DO) \
80 _Ttbl_( 14, TKN_KW_ELSE) \
81 _Ttbl_( 15, TKN_KW_FOR) \
82 _Ttbl_( 16, TKN_KW_GOTO) \
83 _Ttbl_( 17, TKN_KW_IF) \
84 _Ttbl_( 18, TKN_KW_SWITCH) \
85 _Ttbl_( 19, TKN_KW_WHILE) \
86 _Ttbl_( 20, TKN_KW_EXTERN) \
87 \
88 _Ttbl_('(', TKN_LIT_OPNPAREN) \
89 _Ttbl_(')', TKN_LIT_CLSPAREN) \
90 _Ttbl_(',', TKN_LIT_COMMA) \
91 _Ttbl_(':', TKN_LIT_COLON) \
92 _Ttbl_(';', TKN_LIT_SEMI) \
93 _Ttbl_('?', TKN_LIT_QUESTION) \
94 _Ttbl_('[', TKN_LIT_OPNBRACK) \
95 _Ttbl_(']', TKN_LIT_CLSBRACK) \
96 _Ttbl_('{', TKN_LIT_OBRACE) \
97 _Ttbl_('}', TKN_LIT_CBRACE)
98
99 #define _Ttbl_(_v, _n) _n = _v,
100 typedef enum { TOKEN_TABLE TOKEN_MAX } token_t;
101 #undef _Ttbl_
102
103 #define RES_WORD_TABLE \
104 _Ktbl_(case, TKN_KW_CASE) \
105 _Ktbl_(default, TKN_KW_DEFAULT) \
106 _Ktbl_(do, TKN_KW_DO) \
107 _Ktbl_(else, TKN_KW_ELSE) \
108 _Ktbl_(extern, TKN_KW_EXTERN) \
109 _Ktbl_(for, TKN_KW_FOR) \
110 _Ktbl_(goto, TKN_KW_GOTO) \
111 _Ktbl_(if, TKN_KW_IF) \
112 _Ktbl_(switch, TKN_KW_SWITCH) \
113 _Ktbl_(while, TKN_KW_WHILE)
114
115 typedef struct {
116 FILE * fs_fp;
117 char const * fs_fname;
118 char const * fs_text;
119 char const * fs_scan;
120 bool fs_bol; //!< Beginning Of Line
121 token_t last_tkn;
122 char const * tkn_text;
123 size_t tkn_len;
124 int tkn_line;
125 int cur_line;
126 int nc_line;
127 } fstate_t;
128
129 typedef struct {
130 int st_line_ct;
131 int st_nc_line_ct;
132 int ln_st;
133 int ncln_st;
134 int goto_ct;
135 int proc_line;
136 int st_colon_need;
137 int st_depth_warned;
138 score_t score;
139 char * st_end;
140 fstate_t * st_fstate;
141 char pname[256];
142 } state_t;
143
144 static inline void state_init(state_t * st, fstate_t * fs)
145 {
146 *st = (state_t) {
147 .ln_st = fs->cur_line,
148 .st_fstate = fs,
149 .st_nc_line_ct = fs->nc_line
150 };
151
152 memcpy(st->pname, fs->tkn_text, fs->tkn_len);
153 }
154
155 #define MAX_SCORE 999999
156
157 extern score_t penalty;
158 extern score_t subexp_penalty;
159 extern score_t scaling;
160
161 extern token_t
162 next_token(fstate_t * fs);
163
164 extern void
165 unget_token(fstate_t * fs);
166
167 extern bool
168 find_proc_start(fstate_t * fs);
169
170 extern void
171 do_column_totals(void);
172
173 extern void
174 decomment_file(char const * fname, FILE * fp);
175
176 extern void
177 initialize(int argc, char ** argv);
178
179 extern void
180 do_summary(complexity_exit_code_t);
181
182 extern void
183 score_proc(state_t * score);
184
185 #endif /* COMPLEXITY_H_GUARD */
186 /*
187 * Local Variables:
188 * mode: C
189 * c-file-style: "stroustrup"
190 * indent-tabs-mode: nil
191 * End:
192 * end of complexity.h */