Menu ▾ ▴

#4209 __STDC_HOSTED__ reports a hosted implementation for SDCC's documented freestanding model

closed-fixed
None
Front-end
5
2026-11-07
2026-10-23
No

1: Sample code that reproduces the problem.

int hosted = __STDC_HOSTED__;

2: Exact command used to run SDCC on this sample code.

sdcc -mz80 --std=c2y -E bug.c

3: SDCC version tested.

SDCC 4.6.4 #17068 (Linux), tested 23 October 2026.

4: Observed and expected behaviour.

The preprocessed declaration is int hosted = 1;.

Section 3.1 of the published SDCC 4.6.4 manual describes its intended implementation model as freestanding. Under that model, STDC_HOSTED should expand to 0. N3886 6.10.10.2 defines this macro as 1 for a hosted implementation and 0 otherwise. Reporting 1 makes environment-dependent source code select its hosted branch instead.

The report is about consistency with SDCC's documented freestanding model. It does not infer that hosted startup constraints apply to SDCC merely because the preprocessor currently reports 1. The driver/model contract and predefined macro should agree.

References: SDCC Compiler User Guide, section 3.1, page 25; N3886, 6.10.10.2.

5: Additional checks.

The default result is 1 on Z80, MCS51, STM8, PDK14 and MOS65C02 in both C23 and C2Y (10 checks). The same commands with -Wp-ffreestanding produce 0 in all ten configurations. These commands do not use --nostdinc, so the result is not an artefact of disabling standard-header lookup. The explicit preprocessor flag demonstrates a way to obtain the expected value.

GCC 13.3.0 reports 1 in its default hosted mode and 0 with -ffreestanding, as expected. GCC 16.2 and Clang were not tested.

Related

Wiki: SDCC 4.7.0 Release

Discussion

  • Christopher Bazley

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -26,8 +26,6 @@
    
     5: Additional checks.
    
    -The default result is 1 on Z80, MCS51, STM8, PDK14 and MOS65C02 in both C23 and C2Y (10 checks). The same commands with `-Wp-ffreestanding` produce 0 in all ten configurations. These commands do not use --nostdinc, so the result is not an artefact of disabling standard-header lookup. The exact commands and outputs are in hosted-model-checks.json. The explicit preprocessor flag demonstrates a way to obtain the expected value; no compiler fix is included.
    +The default result is 1 on Z80, MCS51, STM8, PDK14 and MOS65C02 in both C23 and C2Y (10 checks). The same commands with `-Wp-ffreestanding` produce 0 in all ten configurations. These commands do not use --nostdinc, so the result is not an artefact of disabling standard-header lookup. The explicit preprocessor flag demonstrates a way to obtain the expected value.
    
    -GCC 13.3.0 reports 1 in its default hosted mode and 0 with -ffreestanding, as expected. GCC 16.2 and Clang were not tested.
    -
    -The candidate c2y-freestanding-hosted-macro.c checks __STDC_HOSTED__ == 0 only when compiling with SDCC. Its static assertion currently fails on all five targets, producing warning 215. No diagnostic is expected once the reported macro agrees with the documented model. It does not require a hosted GCC configuration to report zero.
    +GCC 13.3.0 reports 1 in its default hosted mode and 0 with `-ffreestanding`, as expected. GCC 16.2 and Clang were not tested.
    
     
  • Philipp Klaus Krause

    Maybe this broke at one of the sdcpp rebases on newer upstream cpp from GCC? @felixs?

     
  • Maarten Brock

    Maarten Brock - 2026-10-24

    Or has it ever worked?

    I just tried arm-none-eabi-gcc 11.2..0 and it also reports to be hosted by default.

    bug-4209.c

    #if __STDC_HOSTED__ == 1
    llkjasdkj
    #endif
    
    $ arm-none-eabi-gcc -c bug-4209.c 
    bug-4209.c:2:1: error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input
        2 | llkjasdkj
          | ^~~~~~~~~
    

    Note that this compiler is used to compile e.g. U-boot, the bootloader to start Linux on many embedded platforms.

     
    • Maarten Brock

      Maarten Brock - 2026-10-24

      Arguing that this is dependent on the used C library and newlib-nano clearly states it is not hosted, I retried with arm-none-eabi-gcc --specs=nano.specs, but that changes nothing.

       
  • Christopher Bazley

    The fix for this appears to be trivial. I am attaching it for review. I assume it still needs approval from another developer according to policy.

     
    • Maarten Brock

      Maarten Brock - 2026-11-05

      Looks good to me.

       
  • Christopher Bazley

    • status: open --> closed-fixed
    • assigned_to: Christopher Bazley
     
  • Christopher Bazley

    Thanks for the review. Fixed by [r17142].

     

    Related

    Commit: [r17142]

  • Philipp Klaus Krause

    • status: closed-fixed --> pending-fixed
     
  • Philipp Klaus Krause

    There is an issue with the test case, looks like the regression test infrastructure has problems parsing the header and/or indentifying the test functions:

    philipp@notebook7:~/sdcc-trunk/sdcc/support/regression$ make test-ucz80
    ................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................Empty function list in ./../tests/bug-4209.c!
    ...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
    populated MakeList...
    
     
  • Christopher Bazley

    • status: pending-fixed --> closed-fixed
     
  • Christopher Bazley

    Removed a colon from the opening comment in the test file. [r17150]

     

    Related

    Commit: [r17150]


Log in to post a comment.