Skip to content

Commit 7d251fe

Browse files
committed
Translate C lexer tests to C unit tests
This allows us to get rid of a bunch of exports and ctypes wrappers. Signed-off-by: Omar Sandoval <[email protected]>
1 parent 04ee3c3 commit 7d251fe

File tree

8 files changed

+354
-410
lines changed

8 files changed

+354
-410
lines changed

libdrgn/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ libdrgnimpl_la_SOURCES = $(ARCH_DEFS_PYS:_defs.py=.c) \
4545
binary_search_tree.h \
4646
bitops.h \
4747
c_keywords.inc \
48+
c_lexer.h \
4849
cfi.c \
4950
cfi.h \
5051
cityhash.h \
@@ -214,7 +215,8 @@ examples_load_debug_info_LDADD = libdrgnimpl.la
214215
# Python unit tests instead.
215216
TESTS = $(check_PROGRAMS)
216217

217-
check_PROGRAMS = tests/lexer
218+
check_PROGRAMS = tests/language_c \
219+
tests/lexer
218220

219221
EXTRA_DIST += $(addsuffix .c.in,$(check_PROGRAMS))
220222

@@ -230,6 +232,9 @@ test_cflags = $(AM_CFLAGS) $(check_CFLAGS)
230232
test_cppflags = $(AM_CPPFLAGS) -iquote $(srcdir)/tests
231233
test_ldadd = $(check_LIBS) libdrgnimpl.la
232234

235+
tests_language_c_CFLAGS = $(test_cflags)
236+
tests_language_c_CPPFLAGS = $(test_cppflags)
237+
tests_language_c_LDADD = $(test_ldadd)
233238
tests_lexer_CFLAGS = $(test_cflags)
234239
tests_lexer_CPPFLAGS = $(test_cppflags)
235240
tests_lexer_LDADD = $(test_ldadd)

libdrgn/c_lexer.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
#ifndef DRGN_C_LEXER_H
5+
#define DRGN_C_LEXER_H
6+
7+
#include <stdbool.h>
8+
9+
#include "lexer.h"
10+
11+
// These definitions are only exposed for testing purposes.
12+
13+
// This obviously incomplete since we only handle the tokens we care about.
14+
enum {
15+
C_TOKEN_EOF = -1,
16+
MIN_KEYWORD_TOKEN,
17+
MIN_SPECIFIER_TOKEN = MIN_KEYWORD_TOKEN,
18+
C_TOKEN_VOID = MIN_SPECIFIER_TOKEN,
19+
C_TOKEN_CHAR,
20+
C_TOKEN_SHORT,
21+
C_TOKEN_INT,
22+
C_TOKEN_LONG,
23+
C_TOKEN_SIGNED,
24+
C_TOKEN_UNSIGNED,
25+
C_TOKEN_BOOL,
26+
C_TOKEN_FLOAT,
27+
C_TOKEN_DOUBLE,
28+
C_TOKEN_COMPLEX,
29+
MAX_SPECIFIER_TOKEN = C_TOKEN_COMPLEX,
30+
MIN_QUALIFIER_TOKEN,
31+
C_TOKEN_CONST = MIN_QUALIFIER_TOKEN,
32+
C_TOKEN_RESTRICT,
33+
C_TOKEN_VOLATILE,
34+
C_TOKEN_ATOMIC,
35+
MAX_QUALIFIER_TOKEN = C_TOKEN_ATOMIC,
36+
C_TOKEN_STRUCT,
37+
C_TOKEN_UNION,
38+
C_TOKEN_CLASS,
39+
C_TOKEN_ENUM,
40+
MAX_KEYWORD_TOKEN = C_TOKEN_ENUM,
41+
C_TOKEN_LPAREN,
42+
C_TOKEN_RPAREN,
43+
C_TOKEN_LBRACKET,
44+
C_TOKEN_RBRACKET,
45+
C_TOKEN_ASTERISK,
46+
C_TOKEN_DOT,
47+
C_TOKEN_NUMBER,
48+
C_TOKEN_IDENTIFIER,
49+
C_TOKEN_TEMPLATE_ARGUMENTS,
50+
C_TOKEN_COLON,
51+
};
52+
53+
struct drgn_c_family_lexer {
54+
struct drgn_lexer lexer;
55+
bool cpp;
56+
};
57+
58+
struct drgn_error *drgn_c_family_lexer_func(struct drgn_lexer *lexer,
59+
struct drgn_token *token);
60+
61+
#endif /* DRGN_C_LEXER_H */

libdrgn/language_c.c

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "array.h"
1414
#include "bitops.h"
15+
#include "c_lexer.h"
1516
#include "error.h"
1617
#include "language.h" // IWYU pragma: associated
1718
#include "lexer.h"
@@ -1611,53 +1612,8 @@ static struct drgn_error *c_format_object(const struct drgn_object *obj,
16111612
return NULL;
16121613
}
16131614

1614-
/* This obviously incomplete since we only handle the tokens we care about. */
1615-
enum {
1616-
C_TOKEN_EOF = -1,
1617-
MIN_KEYWORD_TOKEN,
1618-
MIN_SPECIFIER_TOKEN = MIN_KEYWORD_TOKEN,
1619-
C_TOKEN_VOID = MIN_SPECIFIER_TOKEN,
1620-
C_TOKEN_CHAR,
1621-
C_TOKEN_SHORT,
1622-
C_TOKEN_INT,
1623-
C_TOKEN_LONG,
1624-
C_TOKEN_SIGNED,
1625-
C_TOKEN_UNSIGNED,
1626-
C_TOKEN_BOOL,
1627-
C_TOKEN_FLOAT,
1628-
C_TOKEN_DOUBLE,
1629-
C_TOKEN_COMPLEX,
1630-
MAX_SPECIFIER_TOKEN = C_TOKEN_COMPLEX,
1631-
MIN_QUALIFIER_TOKEN,
1632-
C_TOKEN_CONST = MIN_QUALIFIER_TOKEN,
1633-
C_TOKEN_RESTRICT,
1634-
C_TOKEN_VOLATILE,
1635-
C_TOKEN_ATOMIC,
1636-
MAX_QUALIFIER_TOKEN = C_TOKEN_ATOMIC,
1637-
C_TOKEN_STRUCT,
1638-
C_TOKEN_UNION,
1639-
C_TOKEN_CLASS,
1640-
C_TOKEN_ENUM,
1641-
MAX_KEYWORD_TOKEN = C_TOKEN_ENUM,
1642-
C_TOKEN_LPAREN,
1643-
C_TOKEN_RPAREN,
1644-
C_TOKEN_LBRACKET,
1645-
C_TOKEN_RBRACKET,
1646-
C_TOKEN_ASTERISK,
1647-
C_TOKEN_DOT,
1648-
C_TOKEN_NUMBER,
1649-
C_TOKEN_IDENTIFIER,
1650-
C_TOKEN_TEMPLATE_ARGUMENTS,
1651-
C_TOKEN_COLON,
1652-
};
1653-
16541615
#include "c_keywords.inc"
16551616

1656-
struct drgn_c_family_lexer {
1657-
struct drgn_lexer lexer;
1658-
bool cpp;
1659-
};
1660-
16611617
struct drgn_error *drgn_c_family_lexer_func(struct drgn_lexer *lexer,
16621618
struct drgn_token *token) {
16631619
const char *p = lexer->p;

libdrgn/lexer.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ struct drgn_error *drgn_lexer_push(struct drgn_lexer *lexer,
125125
struct drgn_error *drgn_lexer_peek(struct drgn_lexer *lexer,
126126
struct drgn_token *token);
127127

128-
/* Exported only for testing. */
129-
struct drgn_error *drgn_c_family_lexer_func(struct drgn_lexer *lexer,
130-
struct drgn_token *token);
131-
132128
/** @} */
133129

134130
#endif /* DRGN_LEXER_H */

libdrgn/python/test.c

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,9 @@
1212
*/
1313

1414
#include "drgnpy.h"
15-
#include "../lexer.h"
1615
#include "../path.h"
1716
#include "../serialize.h"
1817

19-
typeof(drgn_lexer_init) drgn_test_lexer_init;
20-
DRGNPY_PUBLIC void drgn_test_lexer_init(struct drgn_lexer *lexer,
21-
drgn_lexer_func func, const char *str)
22-
{
23-
return drgn_lexer_init(lexer, func, str);
24-
}
25-
26-
typeof(drgn_lexer_deinit) drgn_test_lexer_deinit;
27-
DRGNPY_PUBLIC void drgn_test_lexer_deinit(struct drgn_lexer *lexer)
28-
{
29-
return drgn_lexer_deinit(lexer);
30-
}
31-
32-
typeof(drgn_lexer_pop) drgn_test_lexer_pop;
33-
DRGNPY_PUBLIC struct drgn_error *drgn_test_lexer_pop(struct drgn_lexer *lexer,
34-
struct drgn_token *token)
35-
{
36-
return drgn_lexer_pop(lexer, token);
37-
}
38-
39-
typeof(drgn_lexer_push) drgn_test_lexer_push;
40-
DRGNPY_PUBLIC struct drgn_error *
41-
drgn_test_lexer_push(struct drgn_lexer *lexer, const struct drgn_token *token)
42-
{
43-
return drgn_lexer_push(lexer, token);
44-
}
45-
46-
typeof(drgn_lexer_peek) drgn_test_lexer_peek;
47-
DRGNPY_PUBLIC struct drgn_error *drgn_test_lexer_peek(struct drgn_lexer *lexer,
48-
struct drgn_token *token)
49-
{
50-
return drgn_lexer_peek(lexer, token);
51-
}
52-
53-
typeof(drgn_c_family_lexer_func) drgn_test_lexer_c;
54-
DRGNPY_PUBLIC struct drgn_error *drgn_test_lexer_c(struct drgn_lexer *lexer,
55-
struct drgn_token *token)
56-
{
57-
return drgn_c_family_lexer_func(lexer, token);
58-
}
59-
6018
typeof(path_iterator_next) drgn_test_path_iterator_next;
6119
DRGNPY_PUBLIC bool drgn_test_path_iterator_next(struct path_iterator *it,
6220
const char **component,

0 commit comments

Comments
 (0)