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

C cleanups #107

Merged
Merged

Conversation

ronnychevalier
Copy link
Contributor

Another batch of cleanups for the C code in tectonic/.

They were missed with previous cleanups since they use '#if  0'
(multiple spaces before the 0), and I did a simple git grep 'if 0'
"static" should be first

-Wold-style-declaration
Tectonic does not stop to ask user for input, thus we remove the code
potentially asking the user for a password.
do_encryption is initialized to false, it can only change on the else
case when calling dvi_scan_specials.
Standard C libraries implement the free function that already checks if
NULL is provided. In such case, free does nothing.

For example, see ISO/IEC 9899: "7.20.3.2 The free function"

Done using the following coccinelle semantic patch:
    @@
    expression p;
    @@
    - if (p)
      free(p);
Simplify a common pattern where one free a pointer, then assigns it NULL,
with a single call.

Done using the following coccinelle semantic patch:
    @@
    expression p;
    @@
    - free(p);
    - p = NULL;
    + p = mfree(p);
NULL is the value associated to a non-initialized pointer (it does not
refer to a valid object). It improves readability to uses the NULL
macro instead of a 0.

Done using the following coccinelle patch:
http://coccinelle.lip6.fr/rules/badzero.html
In C, a cast from void * to any other pointer is made implicitly.

Done using the following coccinelle patch:
    @@
    type T;
    @@

    - (T *)
      (\(malloc\|xmalloc\|xcalloc\|calloc\)(...))
We use xmalloc/xcalloc to abort in case an OOM occurs.
It improves readability of the code.

Done with the following coccinelle semantic patchs:
    @@
    expression a, b;
    @@
    - strcmp(a, b) == 0
    + streq_ptr(a, b)
    @@
    expression a, b;
    @@
    - a && streq_ptr(a, b)
    + streq_ptr(a, b)
@ronnychevalier ronnychevalier force-pushed the rc/c-yet-more-cleanups branch from 2b48575 to 1a16c53 Compare June 18, 2017 20:03
@pkgw
Copy link
Collaborator

pkgw commented Jun 20, 2017

Ooh, you've probably been using this for a while, but I wasn't aware of coccinelle until I read your commit messages. Neat! And, thanks for documenting what you've done. I think coccinelle could be extremely useful for some higher-level transforms of the C code ... it probably would have saved me a lot of work in earlier iterations! Hopefully it can help transform the C code that accesses TeX's structures to allow us to get rid of all of the mem[q].hh.u.B0 type constructs.

I see that you've added some inline functions. Are there any portability dangers associated with this? For the time, I only care about vaguely recent Linux, OSX, and Windows, so I don't mind if inline functions are only going to cause problems on old versions of AIX or whatever.

I am not a huge fan of the function name streq_ptr ... is there a special reasoning to the naming? Please note that there is already a #define called STREQ that does the same thing ... if the inline function is nice and portable, I would definitely prefer to switch to it to get type-safety etc. But maybe just call it streq? Is that name used on some OS's?

And for consistency with other functions, I think I might prefer renaming startswith to strstartswith.

@ronnychevalier
Copy link
Contributor Author

ronnychevalier commented Jun 20, 2017 via email

@ronnychevalier ronnychevalier force-pushed the rc/c-yet-more-cleanups branch from e1d5a89 to 06a48c6 Compare June 20, 2017 16:44
@ronnychevalier
Copy link
Contributor Author

Also, what's the purpose of mem[q].hh.u.B0?

@pkgw
Copy link
Collaborator

pkgw commented Jun 21, 2017

  • OK, if inline is specified in C99, I'm happy to use it. There's a part of me that still feels a little uncomfortable using some C99 constructs, but seriously, the standard is almost 20 years old at this point. The discomfort says more about when I started learning C (before 1999!) than it does about what's portable.
  • Thanks for explaining the _ptr suffix. I can't say that it feels especially "right" to me, but it's fine to keep the function with the suffix as-is. For what it's worth I have the impression that systemd has lots of careful, clean C code, so if the suffix is coming from their style that genuinely encourages me to give it a try.
  • Accesses like mem[q].hh.u.B0 are basically accesses to fields of the various structures used by the TeX engine. TeX manages memory manually by manipulating "pointers" (offsets) within its big loosely-typed mem array. Most of these pointers are to data structures with named fields but because of the way the WEB2C translation works, unfortunately the symbolic information was all lost in the translation to C. I'd really like to make all of these references symbolic again, but I have trouble seeing how to make the change incrementally, which means it would be a very big project.

@pkgw pkgw merged commit 6eaf5fb into tectonic-typesetting:master Jun 21, 2017
Mrmaxmeier pushed a commit to Mrmaxmeier/tectonic that referenced this pull request Oct 4, 2019
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

Successfully merging this pull request may close these issues.

2 participants