Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
3d6fe839
Commit
3d6fe839
authored
Nov 14, 2014
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cdump: handle multi-line preprocessor directives.
Signed-off-by:
Rusty Russell
<
rusty@rustcorp.com.au
>
parent
1e5f5ecc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
2 deletions
+87
-2
ccan/cdump/cdump.c
ccan/cdump/cdump.c
+12
-2
ccan/cdump/test/run-multiline.c
ccan/cdump/test/run-multiline.c
+75
-0
No files found.
ccan/cdump/cdump.c
View file @
3d6fe839
...
...
@@ -16,6 +16,16 @@ static void add_token(struct token **toks, const char *p, size_t len)
(
*
toks
)[
n
].
len
=
len
;
}
static
size_t
to_eol
(
const
char
*
p
)
{
size_t
len
=
strcspn
(
p
,
"
\n
"
);
/* And any \ continuations. */
while
(
p
[
len
]
&&
p
[
len
-
1
]
==
'\\'
)
len
+=
strcspn
(
p
+
len
+
1
,
"
\n
"
)
+
1
;
return
len
;
}
/* Simplified tokenizer: comments and preproc directives removed,
identifiers are a token, others are single char tokens. */
static
struct
token
*
tokenize
(
const
void
*
ctx
,
const
char
*
code
)
...
...
@@ -27,10 +37,10 @@ static struct token *tokenize(const void *ctx, const char *code)
for
(
i
=
0
;
code
[
i
];
i
+=
len
)
{
if
(
code
[
i
]
==
'#'
&&
start_of_line
)
{
/* Preprocessor line. */
len
=
strcspn
(
code
+
i
,
"
\n
"
);
len
=
to_eol
(
code
+
i
);
}
else
if
(
code
[
i
]
==
'/'
&&
code
[
i
+
1
]
==
'/'
)
{
/* One line comment. */
len
=
strcspn
(
code
+
i
,
"
\n
"
);
len
=
to_eol
(
code
+
i
);
if
(
tok_start
!=
-
1U
)
{
add_token
(
&
toks
,
code
+
tok_start
,
i
-
tok_start
);
tok_start
=
-
1U
;
...
...
ccan/cdump/test/run-multiline.c
0 → 100644
View file @
3d6fe839
#include <ccan/cdump/cdump.h>
/* Include the C files directly. */
#include <ccan/cdump/cdump.c>
#include <ccan/tap/tap.h>
int
main
(
void
)
{
struct
cdump_definitions
*
defs
;
const
struct
cdump_type
*
t
;
char
*
ctx
=
tal
(
NULL
,
char
),
*
problems
;
/* This is how many tests you plan to run */
plan_tests
(
30
);
/* Multi-line preprocessor statement. */
defs
=
cdump_extract
(
ctx
,
"#if
\\\n
"
"SOME
\\\n
"
"THING
\n
"
"enum foo { BAR };"
,
&
problems
);
ok1
(
defs
);
ok1
(
tal_parent
(
defs
)
==
ctx
);
ok1
(
strmap_empty
(
&
defs
->
structs
));
ok1
(
strmap_empty
(
&
defs
->
unions
));
t
=
strmap_get
(
&
defs
->
enums
,
"foo"
);
ok1
(
t
);
ok1
(
t
->
kind
==
CDUMP_ENUM
);
ok1
(
streq
(
t
->
name
,
"foo"
));
ok1
(
tal_count
(
t
->
u
.
enum_vals
)
==
1
);
ok1
(
streq
(
t
->
u
.
enum_vals
[
0
].
name
,
"BAR"
));
ok1
(
!
t
->
u
.
enum_vals
[
0
].
value
);
defs
=
cdump_extract
(
ctx
,
"enum foo {
\n
"
"#if
\\\n
"
"SOME
\\\n
"
"THING
\n
"
" BAR };"
,
&
problems
);
ok1
(
defs
);
ok1
(
tal_parent
(
defs
)
==
ctx
);
ok1
(
strmap_empty
(
&
defs
->
structs
));
ok1
(
strmap_empty
(
&
defs
->
unions
));
t
=
strmap_get
(
&
defs
->
enums
,
"foo"
);
ok1
(
t
);
ok1
(
t
->
kind
==
CDUMP_ENUM
);
ok1
(
streq
(
t
->
name
,
"foo"
));
ok1
(
tal_count
(
t
->
u
.
enum_vals
)
==
1
);
ok1
(
streq
(
t
->
u
.
enum_vals
[
0
].
name
,
"BAR"
));
ok1
(
!
t
->
u
.
enum_vals
[
0
].
value
);
/* Multi-line "one-line" comment. */
defs
=
cdump_extract
(
ctx
,
"enum foo {
\n
"
"// Comment
\\\n
"
"SOME
\\\n
"
"THING
\n
"
" BAR };"
,
&
problems
);
ok1
(
defs
);
ok1
(
tal_parent
(
defs
)
==
ctx
);
ok1
(
strmap_empty
(
&
defs
->
structs
));
ok1
(
strmap_empty
(
&
defs
->
unions
));
t
=
strmap_get
(
&
defs
->
enums
,
"foo"
);
ok1
(
t
);
ok1
(
t
->
kind
==
CDUMP_ENUM
);
ok1
(
streq
(
t
->
name
,
"foo"
));
ok1
(
tal_count
(
t
->
u
.
enum_vals
)
==
1
);
ok1
(
streq
(
t
->
u
.
enum_vals
[
0
].
name
,
"BAR"
));
ok1
(
!
t
->
u
.
enum_vals
[
0
].
value
);
/* This exits depending on whether all tests passed */
return
exit_status
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment