You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nuttx.apache.org by Daniel Appiagyei <da...@braincorp.com.INVALID> on 2023/09/16 00:47:08 UTC

Enforcing C++ compatibility with C code in CI/CD

Hi,
I'm following up on an issue
<https://github.com/apache/nuttx/issues/10556#issuecomment-1712717696> I
opened about a C header file not being compatible with a C++ compiler
because of it using reserved C++ keywords. I'm fixing this issue for header
files but wanted to know if we want to endorse C++ compatibility for our C
source files as well.

GCC has the -Wc++-compat
<https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wc_002b_002b-compat>
flag
which we could enable as an error instead of a warning. Here's an example
of it catching us using the reserved keyword 'or':
```
misc/lib_fnmatch.c:193:19: warning: identifier "or" is a special operator
name in C++ [-Wc++-compat]
```
Aside from reserved keywords being used, C++ doesn't support *implicit
*conversions
from void* to other types. The majority of nuttx code I've seen so far
*explicitly* casts anyway, so I don't see requiring explicit casts being a
major hurdle. Refactoring our codebase to be c++ compliant would therefore
mainly be find/replace for reserved keywords and adding explicit casts
(straightforward). I can do the work and it would only take a couple weeks.

My questions are these:
*Does NuttX want to officially agree to and enforce that*:
1. all C headers and source file be compatible with C++, or
2. all C headers be compatible *and* only C++ source files with 'extern
"C"' in them be compatible

Secondly, if we decide on one of the above rulings, do we want to add the
ruling to our C coding style guidelines
<https://nuttx.apache.org/docs/latest/contributing/coding_style.html#general-conventions>
?

Thanks,
Daniel


<bo...@braincorporation.com>

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Daniel Appiagyei <da...@braincorp.com.INVALID>.
No rush at all, thanks

On Mon, Oct 9, 2023 at 8:23 PM Xiang Xiao <xi...@gmail.com> wrote:

> Let' me find some time to write a test, please wait for a moment.
>
> On Tue, Oct 10, 2023 at 2:53 AM Daniel Appiagyei
> <da...@braincorp.com.invalid> wrote:
>
> > Good solution.
> > I'm not too knowledgeable about makefiles, but if you could set that part
> > up then I can do the work to make the files compatible?
> >
> > On Sun, Oct 8, 2023 at 10:44 PM Xiang Xiao <xi...@gmail.com>
> > wrote:
> >
> > > We can add a simple test like this:
> > >
> > >    1. Create a context target in Makefile to generate a .cxx file which
> > >    list and include all header files in nuttx/include
> > >    2. Add the generated file to CXXSRCS
> > >
> > > If we just want to ensure the public nuttx header files are compatible
> > with
> > > c++ syntax.
> > >
> > > On Mon, Oct 9, 2023 at 9:47 AM Daniel Appiagyei
> > > <da...@braincorp.com.invalid> wrote:
> > >
> > > > Late follow up, sorry
> > > >
> > > > Thanks guys, I do understand that only headers need to be compatible,
> > > > however, the GCC check I mentioned above does not discriminate
> between
> > > > headers and source files. If we don't have some sort of automated
> check
> > > for
> > > > C++ compatibility, then we will run into issues in the future like I
> > > > mentioned above. The nuttx style guide mentions that all C code must
> be
> > > C89
> > > > compliant. As long as C++ compatibility doesn't interfere with that
> > > > standard then we won't be losing much.
> > > > Are there any other ideas on how to automatically enforce headers
> being
> > > c++
> > > > comaptible?
> > > > Best,
> > > > Daniel
> > > >
> > > > On Sun, Sep 17, 2023 at 12:56 PM Petro Karashchenko <
> > > > petro.karashchenko@gmail.com> wrote:
> > > >
> > > > > Since C++ most likely will never be used in the kernel I see that
> > > > > compatibility border should stay in the "include" folder in nuttx
> > repo.
> > > > The
> > > > > nuttx-apps headers should be C++ compatible as we can mix things at
> > > > > application level.
> > > > >
> > > >
> > > >
> > > > --
> > > >
> > > >
> > > > *Daniel Appiagyei | Embedded Software Engineer *Email:
> > > > daniel.appiagyei@braincorp.com
> > > > <bo...@braincorporation.com>*Brain*
> > > > * Corp™ *10182 Telesis Ct, Suite 100
> > > > San Diego, CA 92121
> > > >
> > > > (858)-689-7600
> > > > www.braincorp.com
> > > >
> > >
> >
> >
> > --
> >
> >
> > *Daniel Appiagyei | Embedded Software Engineer *Email:
> > daniel.appiagyei@braincorp.com
> > <bo...@braincorporation.com>*Brain*
> > * Corp™ *10182 Telesis Ct, Suite 100
> > San Diego, CA 92121
> >
> > (858)-689-7600
> > www.braincorp.com
> >
>

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Xiang Xiao <xi...@gmail.com>.
Let' me find some time to write a test, please wait for a moment.

On Tue, Oct 10, 2023 at 2:53 AM Daniel Appiagyei
<da...@braincorp.com.invalid> wrote:

> Good solution.
> I'm not too knowledgeable about makefiles, but if you could set that part
> up then I can do the work to make the files compatible?
>
> On Sun, Oct 8, 2023 at 10:44 PM Xiang Xiao <xi...@gmail.com>
> wrote:
>
> > We can add a simple test like this:
> >
> >    1. Create a context target in Makefile to generate a .cxx file which
> >    list and include all header files in nuttx/include
> >    2. Add the generated file to CXXSRCS
> >
> > If we just want to ensure the public nuttx header files are compatible
> with
> > c++ syntax.
> >
> > On Mon, Oct 9, 2023 at 9:47 AM Daniel Appiagyei
> > <da...@braincorp.com.invalid> wrote:
> >
> > > Late follow up, sorry
> > >
> > > Thanks guys, I do understand that only headers need to be compatible,
> > > however, the GCC check I mentioned above does not discriminate between
> > > headers and source files. If we don't have some sort of automated check
> > for
> > > C++ compatibility, then we will run into issues in the future like I
> > > mentioned above. The nuttx style guide mentions that all C code must be
> > C89
> > > compliant. As long as C++ compatibility doesn't interfere with that
> > > standard then we won't be losing much.
> > > Are there any other ideas on how to automatically enforce headers being
> > c++
> > > comaptible?
> > > Best,
> > > Daniel
> > >
> > > On Sun, Sep 17, 2023 at 12:56 PM Petro Karashchenko <
> > > petro.karashchenko@gmail.com> wrote:
> > >
> > > > Since C++ most likely will never be used in the kernel I see that
> > > > compatibility border should stay in the "include" folder in nuttx
> repo.
> > > The
> > > > nuttx-apps headers should be C++ compatible as we can mix things at
> > > > application level.
> > > >
> > >
> > >
> > > --
> > >
> > >
> > > *Daniel Appiagyei | Embedded Software Engineer *Email:
> > > daniel.appiagyei@braincorp.com
> > > <bo...@braincorporation.com>*Brain*
> > > * Corp™ *10182 Telesis Ct, Suite 100
> > > San Diego, CA 92121
> > >
> > > (858)-689-7600
> > > www.braincorp.com
> > >
> >
>
>
> --
>
>
> *Daniel Appiagyei | Embedded Software Engineer *Email:
> daniel.appiagyei@braincorp.com
> <bo...@braincorporation.com>*Brain*
> * Corp™ *10182 Telesis Ct, Suite 100
> San Diego, CA 92121
>
> (858)-689-7600
> www.braincorp.com
>

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Daniel Appiagyei <da...@braincorp.com.INVALID>.
Good solution.
I'm not too knowledgeable about makefiles, but if you could set that part
up then I can do the work to make the files compatible?

On Sun, Oct 8, 2023 at 10:44 PM Xiang Xiao <xi...@gmail.com>
wrote:

> We can add a simple test like this:
>
>    1. Create a context target in Makefile to generate a .cxx file which
>    list and include all header files in nuttx/include
>    2. Add the generated file to CXXSRCS
>
> If we just want to ensure the public nuttx header files are compatible with
> c++ syntax.
>
> On Mon, Oct 9, 2023 at 9:47 AM Daniel Appiagyei
> <da...@braincorp.com.invalid> wrote:
>
> > Late follow up, sorry
> >
> > Thanks guys, I do understand that only headers need to be compatible,
> > however, the GCC check I mentioned above does not discriminate between
> > headers and source files. If we don't have some sort of automated check
> for
> > C++ compatibility, then we will run into issues in the future like I
> > mentioned above. The nuttx style guide mentions that all C code must be
> C89
> > compliant. As long as C++ compatibility doesn't interfere with that
> > standard then we won't be losing much.
> > Are there any other ideas on how to automatically enforce headers being
> c++
> > comaptible?
> > Best,
> > Daniel
> >
> > On Sun, Sep 17, 2023 at 12:56 PM Petro Karashchenko <
> > petro.karashchenko@gmail.com> wrote:
> >
> > > Since C++ most likely will never be used in the kernel I see that
> > > compatibility border should stay in the "include" folder in nuttx repo.
> > The
> > > nuttx-apps headers should be C++ compatible as we can mix things at
> > > application level.
> > >
> >
> >
> > --
> >
> >
> > *Daniel Appiagyei | Embedded Software Engineer *Email:
> > daniel.appiagyei@braincorp.com
> > <bo...@braincorporation.com>*Brain*
> > * Corp™ *10182 Telesis Ct, Suite 100
> > San Diego, CA 92121
> >
> > (858)-689-7600
> > www.braincorp.com
> >
>


-- 


*Daniel Appiagyei | Embedded Software Engineer *Email:
daniel.appiagyei@braincorp.com
<bo...@braincorporation.com>*Brain*
* Corp™ *10182 Telesis Ct, Suite 100
San Diego, CA 92121

(858)-689-7600
www.braincorp.com

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Xiang Xiao <xi...@gmail.com>.
We can add a simple test like this:

   1. Create a context target in Makefile to generate a .cxx file which
   list and include all header files in nuttx/include
   2. Add the generated file to CXXSRCS

If we just want to ensure the public nuttx header files are compatible with
c++ syntax.

On Mon, Oct 9, 2023 at 9:47 AM Daniel Appiagyei
<da...@braincorp.com.invalid> wrote:

> Late follow up, sorry
>
> Thanks guys, I do understand that only headers need to be compatible,
> however, the GCC check I mentioned above does not discriminate between
> headers and source files. If we don't have some sort of automated check for
> C++ compatibility, then we will run into issues in the future like I
> mentioned above. The nuttx style guide mentions that all C code must be C89
> compliant. As long as C++ compatibility doesn't interfere with that
> standard then we won't be losing much.
> Are there any other ideas on how to automatically enforce headers being c++
> comaptible?
> Best,
> Daniel
>
> On Sun, Sep 17, 2023 at 12:56 PM Petro Karashchenko <
> petro.karashchenko@gmail.com> wrote:
>
> > Since C++ most likely will never be used in the kernel I see that
> > compatibility border should stay in the "include" folder in nuttx repo.
> The
> > nuttx-apps headers should be C++ compatible as we can mix things at
> > application level.
> >
>
>
> --
>
>
> *Daniel Appiagyei | Embedded Software Engineer *Email:
> daniel.appiagyei@braincorp.com
> <bo...@braincorporation.com>*Brain*
> * Corp™ *10182 Telesis Ct, Suite 100
> San Diego, CA 92121
>
> (858)-689-7600
> www.braincorp.com
>

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Daniel Appiagyei <da...@braincorp.com.INVALID>.
Late follow up, sorry

Thanks guys, I do understand that only headers need to be compatible,
however, the GCC check I mentioned above does not discriminate between
headers and source files. If we don't have some sort of automated check for
C++ compatibility, then we will run into issues in the future like I
mentioned above. The nuttx style guide mentions that all C code must be C89
compliant. As long as C++ compatibility doesn't interfere with that
standard then we won't be losing much.
Are there any other ideas on how to automatically enforce headers being c++
comaptible?
Best,
Daniel

On Sun, Sep 17, 2023 at 12:56 PM Petro Karashchenko <
petro.karashchenko@gmail.com> wrote:

> Since C++ most likely will never be used in the kernel I see that
> compatibility border should stay in the "include" folder in nuttx repo. The
> nuttx-apps headers should be C++ compatible as we can mix things at
> application level.
>


-- 


*Daniel Appiagyei | Embedded Software Engineer *Email:
daniel.appiagyei@braincorp.com
<bo...@braincorporation.com>*Brain*
* Corp™ *10182 Telesis Ct, Suite 100
San Diego, CA 92121

(858)-689-7600
www.braincorp.com

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Petro Karashchenko <pe...@gmail.com>.
Since C++ most likely will never be used in the kernel I see that
compatibility border should stay in the "include" folder in nuttx repo. The
nuttx-apps headers should be C++ compatible as we can mix things at
application level.

Re: Enforcing C++ compatibility with C code in CI/CD

Posted by Xiang Xiao <xi...@gmail.com>.
I think at least the c header file should be compatible with c++ compiler,
but the c source file could use sme incompatible language features.

On Sat, Sep 16, 2023 at 8:47 AM Daniel Appiagyei
<da...@braincorp.com.invalid> wrote:

> Hi,
> I'm following up on an issue
> <https://github.com/apache/nuttx/issues/10556#issuecomment-1712717696> I
> opened about a C header file not being compatible with a C++ compiler
> because of it using reserved C++ keywords. I'm fixing this issue for header
> files but wanted to know if we want to endorse C++ compatibility for our C
> source files as well.
>
> GCC has the -Wc++-compat
> <
> https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wc_002b_002b-compat
> >
> flag
> which we could enable as an error instead of a warning. Here's an example
> of it catching us using the reserved keyword 'or':
> ```
> misc/lib_fnmatch.c:193:19: warning: identifier "or" is a special operator
> name in C++ [-Wc++-compat]
> ```
> Aside from reserved keywords being used, C++ doesn't support *implicit
> *conversions
> from void* to other types. The majority of nuttx code I've seen so far
> *explicitly* casts anyway, so I don't see requiring explicit casts being a
> major hurdle. Refactoring our codebase to be c++ compliant would therefore
> mainly be find/replace for reserved keywords and adding explicit casts
> (straightforward). I can do the work and it would only take a couple weeks.
>
> My questions are these:
> *Does NuttX want to officially agree to and enforce that*:
> 1. all C headers and source file be compatible with C++, or
> 2. all C headers be compatible *and* only C++ source files with 'extern
> "C"' in them be compatible
>
> Secondly, if we decide on one of the above rulings, do we want to add the
> ruling to our C coding style guidelines
> <
> https://nuttx.apache.org/docs/latest/contributing/coding_style.html#general-conventions
> >
> ?
>
> Thanks,
> Daniel
>
>
> <bo...@braincorporation.com>
>