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

The formatter adds unnecessary tabs #17

Closed
keyhr opened this issue Jan 9, 2022 · 8 comments
Closed

The formatter adds unnecessary tabs #17

keyhr opened this issue Jan 9, 2022 · 8 comments

Comments

@keyhr
Copy link
Collaborator

keyhr commented Jan 9, 2022

Problem

According to norm, (III.3 functions in en.norm.pdf)

Each function must have a single tabulation between its return type and its name.

But this formatter inserts multiple tabulations as follows (2 tabs between int and main):

Screen Shot 2022-01-09 at 13 12 05

Expected formats:
Screen Shot 2022-01-09 at 13 13 00

@cacharle
Copy link
Collaborator

cacharle commented Jan 9, 2022

I'll look into it.

Is it just the function in .c files or are the function prototypes and struct, union, etc.. in header files also affected?

Ok, just read the norm.pdf and it's only function, all the rest stays the same even global variable if I'm not mistaken.

cacharle referenced this issue in cacharle/c_formatter_42 Jan 9, 2022
…er to ignore function declaration (broken with function typedef)
@cacharle
Copy link
Collaborator

cacharle commented Jan 9, 2022

I pushed a quick fix, please try it and tell me where it breaks.

$ pip3 uninstall c_formatter_42
$ git clone https://github.com/cacharle/c_formatter_42
$ cd c_formatter_42
$ pip3 install -e .

$ python -m c_formatter_42 < [your_file]  # I guess the vim extension should keep working if you use it

I already found a bug where typedef on function are ignored by the alignment.

Also, It's not clear to me what should be aligned and what should only have one tab (I ran the norm on global variable with one tab and it passed).

Aligned

  • prototype
  • typedef
  • struct, union, enum
  • member of struct, ...
  • local variables of functions

Not Aligned (one tab between type and name)

  • function declaration
  • global variable

Tell me if I'm missing something.

@keyhr
Copy link
Collaborator Author

keyhr commented Jan 9, 2022

Thank you for your quick response, and I'm sorry that I didn't check the previous issue and file a duplicated one.

I tried to run the formatter but got an error. What I missed? (I don't use python often and need your help)

Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/root/c_formatter_42/c_formatter_42/__main__.py", line 61, in <module>
    main()
  File "/root/c_formatter_42/c_formatter_42/__main__.py", line 42, in main
    print(run_all(content), end="")
  File "/root/c_formatter_42/c_formatter_42/run.py", line 27, in run_all
    content = clang_format(content)
  File "/root/c_formatter_42/c_formatter_42/formatters/clang_format.py", line 75, in clang_format
    raise RuntimeError(f"clang-format error: {err.decode()}")
RuntimeError: clang-format error: qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or directory

Alignment

This is the entire norm rules about indentation:

  1. Each variable declaration must be indented on the same column for its scope.
  2. Each function must have a single tabulation between its return type and its name.
  3. Add a tabulation when declaring a struct, enum or union.
  4. When declaring a struct, union or enum with a typedef, all indentation rules apply.
  5. Typedef name must be preceded by a tab.
  6. You must indent all structures’ names on the same column for their scope.

Rule 1 above is applied to global vars just like other variable declarations. (I ran norminette on not-aligned global vars and confirm that it returned a norm error.)

@cacharle
Copy link
Collaborator

cacharle commented Jan 9, 2022

You should install clang-format on your distribution so that the clang-format executable used by c_formatter_42 has the correct dependencies installed.

c_formatter_42 is using it's own clang-format executable located in c_formatter_42/data because different OS had different versions of it which caused issues (#7)

You're missing /lib64/ld-linux-x86-64.so.2 which is a dependency of clang-format I guess.

A way to fix this issue for future user would be to have our own clang-format dependencies next to the excecutable and link against those instead of the one of the system.

@cacharle
Copy link
Collaborator

cacharle commented Jan 9, 2022

And thank you for clarifying the alignment rules for me, I'll try to implement those.

@keyhr
Copy link
Collaborator Author

keyhr commented Jan 9, 2022

I checked the clang-format installation, but it's been already installed.

$ clang-format --version
clang-format version 10.0.0-4ubuntu1

Is the version old? I'll try to install newer version tomorrow.


I successfully ran on the host environment (M1Mac) (I've been using ubuntu 20.04 docker container).
The new clone worked well on the function declaration. (Inserting single tabulation between the return type and the name)

$ clang-format --version
clang-format version 13.0.0

Thanks very much for working on this, even though you're not 42 student any more..

@keyhr
Copy link
Collaborator Author

keyhr commented Jan 10, 2022

The error '/lib64/ld-linux-x86-64.so.2': No such file or directory seems to be happening because the clang-format binary attempts to link to the x86-64-native ld, while the running ubuntu is a native Arm image.
lib64-ld-linux-x86-64-so-2-no-such-file-or-directory-error - Stack Overflow

This error does not occurs when I use the one from PyPI.

@cacharle
Copy link
Collaborator

cacharle commented Jan 10, 2022 via email

@keyhr keyhr closed this as completed Jan 27, 2022
@keyhr keyhr mentioned this issue Jan 2, 2023
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