edubart / nelua-lang

Minimal, efficient, statically-typed and meta-programmable systems programming language heavily inspired by Lua, which compiles to C and native code.
https://nelua.io
MIT License
1.99k stars 64 forks source link

Clarification about warning(s) behavior #196

Closed stefanos82 closed 1 year ago

stefanos82 commented 1 year ago

I was playing with overview.nelua and have noticed the usage of fallthrough.

I went to https://nelua.io/overview/#fallthrough and run the following example with nelua -SV.

It threw the following warning:

/home/stefanos/.cache/nelua/fallthrough.c:102:7: warning: this statement may fall through [-Wimplicit-fallthrough=]
  102 |       nelua_print_1(((nlstring){(uint8_t*)"1", 1}));
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/stefanos/.cache/nelua/fallthrough.c:105:5: note: here
  105 |     case 2: {
      |     ^~~~
/home/stefanos/.cache/nelua/fallthrough 
1
2

I opened the generated file and saw this /* fallthrough */ comment in generated code:

int nelua_main(int argc, char** argv) {
  switch(fallthrough_a) {
    case 1: {
      nelua_print_1(((nlstring){(uint8_t*)"1", 1}));
      /* fallthrough */
    }
    case 2: {
      nelua_print_1(((nlstring){(uint8_t*)"2", 1}));
      break;
    }
  }
  return 0;
}

For some reason GCC does not respect it.

Now, if I add a macro condition to validate GCC's existence and its version is greater than or equal to 5, I use GCC's attribute instead:

int nelua_main(int argc, char** argv) {
  switch(fallthrough_a) {
    case 1: {
      nelua_print_1(((nlstring){(uint8_t*)"1", 1}));
#if defined(__GNUC__) && __GNUC__ >= 5
    __attribute__ ((fallthrough));
#endif
      /* fallthrough */
    }
    case 2: {
      nelua_print_1(((nlstring){(uint8_t*)"2", 1}));
      break;
    }
  }
  return 0;
}

This way it compiles without any complain.

Do you have any idea why GCC does not respect the fallthrough comment?

edubart commented 1 year ago

What is your GCC version?

stefanos82 commented 1 year ago

gcc version 12.2.0 (Debian 12.2.0-3)

Update: with Debian clang version 14.0.6-2 works as expected.

Is it a GCC bug?

edubart commented 1 year ago

For GCC, seems like fallthrough comment must come right before the case, but there is a } between, I will provide a fix.

edubart commented 1 year ago

Fixed in https://github.com/edubart/nelua-lang/commit/91f23a2432032db49fc7d5606b3b2005bc0d64fb

stefanos82 commented 1 year ago

Nice! Now it works as expected.

By the way, I got these lto warning messages during compilation procedure with make optimized-nelua-lua:

make --no-print-directory CFLAGS="-march=native -O3 -flto -fno-plt -fno-stack-protector -fprofile-generate=pgo" clean-nelualua default
rm -f nelua-lua
gcc \
    -DMAXRECLEVEL=400 -DLUA_USE_RPMALLOC -DNDEBUG -DLUA_USE_LINUX  \
    -Isrc/lua  \
    -march=native -O3 -flto -fno-plt -fno-stack-protector -fprofile-generate=pgo  \
    src/hasher.c src/lfs.c src/luainit.c src/onelua.c src/sys.c src/lpeglabel/lpcap.c src/lpeglabel/lpcode.c src/lpeglabel/lpprint.c src/lpeglabel/lptree.c src/lpeglabel/lpvm.c src/srpmalloc/srpmalloc.c  \
    -o nelua-lua \
    -Wl,-E  -lm -ldl 
lto-wrapper: warning: using serial compilation of 13 LTRANS jobs
lto-wrapper: note: see the ‘-flto’ option documentation for more information
./nelua -qb tests/all_test.nelua
make --no-print-directory CFLAGS="-march=native -O3 -flto -fno-plt -fno-stack-protector -fprofile-use=pgo" clean-nelualua default
rm -f nelua-lua
gcc \
    -DMAXRECLEVEL=400 -DLUA_USE_RPMALLOC -DNDEBUG -DLUA_USE_LINUX  \
    -Isrc/lua  \
    -march=native -O3 -flto -fno-plt -fno-stack-protector -fprofile-use=pgo  \
    src/hasher.c src/lfs.c src/luainit.c src/onelua.c src/sys.c src/lpeglabel/lpcap.c src/lpeglabel/lpcode.c src/lpeglabel/lpprint.c src/lpeglabel/lptree.c src/lpeglabel/lpvm.c src/srpmalloc/srpmalloc.c  \
    -o nelua-lua \
    -Wl,-E  -lm -ldl 
lto-wrapper: warning: using serial compilation of 6 LTRANS jobs
lto-wrapper: note: see the ‘-flto’ option documentation for more information
rm -rf pgo
edubart commented 1 year ago

Thanks for also reporting that, recent GCC started to require parameters in -flto, I've pushed a fix in https://github.com/edubart/nelua-lang/commit/d0d38599cfe89094a3c9244f7919fc91a37f2760