vlang / c2v

C/C++ to V translator
GNU General Public License v3.0
226 stars 31 forks source link

c2v Translation Issue on `return value == 2;` #187

Closed jetX11 closed 3 days ago

jetX11 commented 6 days ago

Describe the bug

(Though I tested it in a more realistic code) In a trivial example, the code

bool foo(int value)
{
    return value == 2;
}

is translated to

fn foo(value int) bool {
    return value == false
}

while obviously it must be

fn foo(value int) bool {
    return value == 2
}

Testing with values other than 2 (e.g., 0, 1, 3, ...), results in the same code with either true or false in the place of that value.

Reproduction Steps

The C source code:

/* foo.c */
#include <stdbool.h>

bool foo(int value)
{
    return value == 2;
}

using v compiler:

$ v translate foo.c

The result foo.v :

@[translated]
module main

fn foo(value int) bool {
    return value == false
}

Expected Behavior

Why? the translated code is return value == false

It should be

return value == 2

Current Behavior

Running:

$ v translate foo.c

Output:

C to V translator 0.4.1
  translating /home/user/projects/V/vcp/foo.c ... #include <stdbool.h>

bool foo(int value)
{
    return value == 2;
}

path=/home/user/projects/V/vcp/foo.c
main loop 1
1FN DECL c_name="foo" cur_file="/home/user/projects/V/vcp/foo.c" node.location.file="/home/user/projects/V/vcp/foo.c"
Reformatted file: /home/user/projects/V/vcp/foo.v
 c2v translate_file() took    17 ms ; output .v file: foo.v
Translated   1 files in    18 ms.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 864845b

Environment details (OS name and version, etc.)

V full version: V 0.4.8 988f8b4.864845b OS: linux, Ubuntu 24.04.1 LTS Processor: 20 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i7-12700K

getwd: /home/user/projects/V/vcp vexe: /home/user/v/v vexe mtime: 2024-11-17 10:43:06

vroot: OK, value: /home/user/v VMODULES: OK, value: /home/user/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.43.0 Git vroot status: weekly.2024.42-270-g864845bc (41 commit(s) behind V master) .git/config present: true

CC version: cc (Ubuntu 13.2.0-23ubuntu4) 13.2.0 emcc version: N/A thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

Huly®: V_0.6-21365

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

medvednikov commented 4 days ago

Works fine with int instead of bool.

Thanks, will fix.

medvednikov commented 3 days ago

Fixed.

jetX11 commented 2 days ago

After all updates and upgrades (v up, v upgrade, ...), unfortunately the issue persists! Apparently, the wrong is with which messes with defacto C bool type and V bool type. v2c apparently works perfect with C99 standard _Bool type:

//~ #include    <stdbool.h>

typedef     int     BOOL;
#define     FALSE   0
#define     TRUE    (!FALSE)

#define CONSTVAL    2

//~ bool foo(int value)
//~ {
    //~ return value == CONSTVAL;
//~ }

_Bool foo_1(int value)
{
    return value == CONSTVAL;
}

BOOL foo_2(int value)
{
    return value == CONSTVAL;
}

BOOL foo_3(int value)
{
    return value == CONSTVAL? TRUE : FALSE;
}

(stdbool.h stuff are commented out) translates to (perfect code)

@[translated]
module main

//~ #include    <stdbool.h>
type BOOL = int

//~ bool foo(int value)
//~ {
//~ return value == CONSTVAL;
//~ }
fn foo_1(value int) bool {
    return value == 2
}

fn foo_2(value int) BOOL {
    return value == 2
}

fn foo_3(value int) BOOL {
    return if value == 2 { (!0) } else { 0 }
}

but including stdbool.h in code:

#include    <stdbool.h>

typedef     int     BOOL;
#define     FALSE   0
#define     TRUE    (!FALSE)

#define CONSTVAL    2

bool foo(int value)
{
    return value == CONSTVAL;
}

_Bool foo_1(int value)
{
    return value == CONSTVAL;
}

BOOL foo_2(int value)
{
    return value == CONSTVAL;
}

BOOL foo_3(int value)
{
    return value == CONSTVAL? TRUE : FALSE;
}

translates to odd code:

@[translated]
module main

type BOOL = int

fn foo(value int) bool {
    return value == false
}

fn foo_1(value int) bool {
    return value == false
}

fn foo_2(value int) BOOL {
    return value == 2
}

fn foo_3(value int) BOOL {
    return if value == 2 { (!0) } else { 0 }
}

Especially, the odd difference is foo_1 is noticeable!

jetX11 commented 2 days ago

Apparently, the line

#define    bool    _Bool

(in the header file "stdbool.h") is the source of issue. It works fine if it is changed to the better compiler-aware statement

typedef     _Bool    bool;
medvednikov commented 1 day ago

@jetX11 v install c2v to upate c2v

medvednikov commented 1 day ago

I should make v translate update it automatically.

jetX11 commented 12 hours ago

@medvednikov Thanks a lot. It works fine. Well done!