Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong formatting with C99 compound literal #22

Closed
Zeta611 opened this issue Apr 10, 2021 · 3 comments
Closed

Wrong formatting with C99 compound literal #22

Zeta611 opened this issue Apr 10, 2021 · 3 comments

Comments

@Zeta611
Copy link

Zeta611 commented Apr 10, 2021

I've been using original CWEB to try literate programming for the first time.
Everything was fine, until I noticed a strange formatting when using C99 compound literal syntax.
Then I tried CWEB 4.2, and the problem was not resolved.

Here is a MWE that demonstrates the problem:

@* Hello CWEB.
@c
#include <stdio.h>

struct T {
	int a;
};

struct T tok(void) {
	return (struct T){.a = 1}; // C99 compound literal
}

int main(void)
{
	struct T = tok();
	return 0;
}

This is the output PDF build with cweave and pdftex.

In the meantime, I can try manually breaking the lines with @;, but I can't see how to manually indent/unindent wrongly formatted code.

@ascherer
Copy link
Owner

This particular example can be fixed with

return @[(struct T){.a = 1}@]; // C99 compound literal

which works with CWEB 3.6 (May 2005) and up.

@ascherer
Copy link
Owner

Here's a larger example with a few more tweaks:

@* C/99 Compound Literals.

@c
@<\#|include|s@>@;
@<Structure@>@;
@<Declaration@>@;
@<Definition@>@;
@<Output@>@;
@<Main@>@;

@ @<\#|inc...@>=
#include <stdio.h> /* |printf| */
#include <string.h> /* |memcpy| */

@ @<Struct...@>=
struct T
{
	int a;
	char* b;
} t2;

@ @<Decl...@>=
void g(const struct T* t);

@ @d bufsiz 10

@<Def...@>=
void f(void)
{
@t\2\2@>
	int *x = @[(int [bufsiz])@t @>{42}@];

	t2 = @[(struct T) {43, "world"}@];
	g( @[&(struct T)@t @>{@, .b="hello", .a=47 @,}@] );
	g( @[&(struct T) {43, "bye"}@] );
@t\2\2@>
	memcpy(x, @[(int[])@t @>{1, 2, 3}@], 3*sizeof(int));
	for (int i=0; i<bufsiz; ++i)
		printf("x[%d]==%d\n", i, x[i]);
}

@ @<Out...@>=
void g(const struct T* t)
{
	printf("struct T { a=%d, b=%s }\n", t->a, t->b);
}

@ @<Main@>=
int main(void)
{
	f();
}

@Zeta611
Copy link
Author

Zeta611 commented Apr 15, 2021

Thank you for the detailed example, especially about the indentation tweak!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants