Skip to content

[FIX] Fix return value of main() if program failed #139

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

Merged
merged 2 commits into from
Dec 5, 2022

Conversation

knst
Copy link
Contributor

@knst knst commented Nov 25, 2022

If *.cpp2 file contains any syntax error or in case if wrong arguments are specified, the cppfront will return success (main returns 0).

This patch propagates error code as a result of cppfront in case of wrong arguments or in case of syntax error in at least one input file.

It may allow to write scripts that runs build of resulting C++ output or any other steps such as [example for Bash]:
cppfront main.cpp && echo OK

@hsutter hsutter self-assigned this Nov 25, 2022
@hsutter
Copy link
Owner

hsutter commented Nov 25, 2022

Thanks -- this seems useful, but -1 and -2 seem to be nonportable values... should it just be EXIT_FAILURE?

The C and C++ standards talk about 0, EXIT_FAILURE, or EXIT_SUCCESS, which are implementation-defined but a quick Godbolt test shows they appear to often be 0 and 1 respectively on the major compilers on at least the OSes being used here: https://godbolt.org/z/EchfErqYs

You can also return from main, or call exit, with your own value but it may be translated... See: https://en.cppreference.com/w/c/program/exit. Pasting related sample doc material from cppreference since I have them open:

If exit_code is 0 or EXIT_SUCCESS, an implementation-defined status indicating successful termination is returned. If exit_code is EXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.

Both EXIT_SUCCESS and the value zero indicate successful program execution status (see std::exit), although it is not required that EXIT_SUCCESS equals zero.

See this Godbolt example for exit(-2) and return -2;... https://godbolt.org/z/f66zPKnjW

  • MSVC on Windows: MSVC generates mov eax, 0xfffffffe, the OS reports Program returned: 4294967294
  • Gcc and Clang: Compilers both generate mov eax, -2, the OS reports Program returned: 254

So perhaps just return EXIT_FAILURE on failure... thanks!

@knst knst changed the title [FIX] Fix return value of main() in program failed [FIX] Fix return value of main() if program failed Nov 26, 2022
@filipsajdak
Copy link
Contributor

I have used EXIT_SUCCESS and EXIT_FAILURE as it is described on cppreference.

Copy link

@wingunder wingunder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be complete, change line 2668 to return EXIT_SUCCESS;

@hsutter hsutter merged commit a1be8e0 into hsutter:main Dec 5, 2022
@hsutter
Copy link
Owner

hsutter commented Dec 5, 2022

Looks good, thanks!

Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
[FIX] Fix return value of main() if program failed
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.

5 participants