Closed jetX11 closed 3 days ago
Works fine with int
instead of bool
.
Thanks, will fix.
Fixed.
After all updates and upgrades (v up, v upgrade, ...), unfortunately the issue persists! Apparently, the wrong is with
//~ #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!
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;
@jetX11 v install c2v
to upate c2v
I should make v translate
update it automatically.
@medvednikov Thanks a lot. It works fine. Well done!
Describe the bug
(Though I tested it in a more realistic code) In a trivial example, the code
is translated to
while obviously it must be
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:
using v compiler:
$ v translate foo.c
The result foo.v :
Expected Behavior
Why? the translated code is
return value == false
It should be
return value == 2
Current Behavior
Running:
$ v translate foo.c
Output:
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