0

Why isn't the astyle program formatting my c source file?

Background: I copy-and-pasted a c source file from gitlab and for some reason, the resulting file was jumbled. I was hoping to be able to run the file through astyle and come away with a nicely formatted file, but my various attempts have had no effect. Each attempt has resulted in a message saying, "Unchanged ecl-hello-world.c"

Here's the malformed source file:

#include <stdio.h> #include <ecl/ecl.h> int main (int argc, char **argv) {   /* Initialize ECL */   cl_boot(argc, argv);   /* Initialize the library we linked in. Each library    * has to be initialized. It is best if all libraries    * are joined using ASDF:MAKE-BUILD.    */   extern void init_lib_HELLO_LISP(cl_object);   ecl_init_module(NULL, init_lib_HELLO_LISP);   cl_object result= cl_eval(c_string_to_object("(hello-lisp)"));   ecl_print(result, ECL_T);   cl_object my_fun = cl_eval(c_string_to_object("(lambda (x) (1+ x))"));   ecl_print(my_fun, ECL_T);   result=cl_funcall(2, my_fun, ecl_make_fixnum(8));   ecl_print(result, ECL_T);   ecl_terpri(ECL_T);   cl_shutdown();   return 0; }

Here are the invocations of astyle I've tried:

astyle ecl-hello-world.c

astyle --style=allman --indent=spaces=4 ecl-hello-world.c

astyle --style=linux ecl-hello-world.c

3
  • Don't know astyle; maybe it doesn't do what it says on the tin. You might try GNU ident, which has been around for decades, and works. Nov 23, 2017 at 2:24
  • See my answer for a possible explanation. @JamesK.Lowden Although that code is invalid it's possible to make source code formatters fail - that is they take code that works and the output fails to compile. GNU indent included. In this case that single line of code is invalid however and it seems that's why (didn't try it with indent though).
    – Pryftan
    Jan 30, 2018 at 23:55
  • Possibility: it thinks it's a text file? Since it's not formed like a C source file? Trying astyle on a short text file has the same result.
    – Pryftan
    Jan 31, 2018 at 0:25

1 Answer 1

1

The problem is that a C pre-processor statement has to be on a line by itself; you cannot have two #includes on the same line and you cannot have your main definition after the #include.

When I tried it the way you had it pasted I got the same error message you did (even though I knew a compiler would throw a fit). After adding a line break after each #include it did format it. So unless you only simplified that for pasting it in that's probably why; certainly it didn't work until after I made that change.

Edit:

I just tried it with GNU indent too. It also fails. The answer seems to truly be fix the first part of the file. Why is it that it doesn't detect and do that itself? Good question. Is there a way to get it to work aside from formatting the file itself a bit first? I don't know but it seems no.

2
  • 2
    Well, I believe that the OP knows that the code is invalid; the question is: Since the meaning/intent is clear, why doesn’t astyle try harder to understand it and fix it? And, I guess, the answer is: Because it wasn’t designed to do that. Jan 30, 2018 at 23:59
  • 1
    @G-Man Well he/she does state it's invalid yes. But that is why it doesn't format it, seemingly. It's actually pretty easy to mess with source code formatters even making it so the output fails to compile (when the original does compile). But here it seems that all he/she would have to do (it seems) is to add the newlines. Btw indent also fails to format it.
    – Pryftan
    Jan 31, 2018 at 0:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .