tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] __LINE__ and #line undelying type


From: Christian Jullien
Subject: Re: [Tinycc-devel] __LINE__ and #line undelying type
Date: Fri, 16 Apr 2021 07:11:27 +0200

Even more strange, __LINE__ type changes as it overflows on clang, and gcc:

int
main() {
#line 2147483647
  printf("__LINE__: %s\n", TYPEOF(__LINE__));
#line 3000000000
  printf("__LINE__: %s\n", TYPEOF(__LINE__));
}

jullien@sims4:~ $ gcc -std=c11 foo.c -o foo && ./foo
__LINE__: int
__LINE__: long long

However, tcc (mob on arm32) detects an error when line overflow occurs:

jullien@sims4:~ $ tcc -std=c11 foo.c -o foo && ./foo
foo.c:-2147483648: error: wrong #line format

On Fedora 33 64bits, it is a warning while much greater values (as 3000000000) 
are promoted to long int on tcc.

C standard says nothing about line int overflow so tcc can't be declared as 
wrong.

Now I agree with you that tcc should better handle those cases (as gcc does?)

-----Original Message-----
From: Tinycc-devel [mailto:tinycc-devel-bounces+eligis=orange.fr@nongnu.org] On 
Behalf Of Elijah Stone
Sent: Friday, April 16, 2021 06:42
To: jullien@eligis.com; tinycc-devel@nongnu.org
Subject: Re: [Tinycc-devel] __LINE__ and #line undelying type

On Fri, 16 Apr 2021, Christian Jullien wrote:

> C standard says:
> _ _LINE_ _ The presumed line number (within the current source file) of the 
> current
> source line (an integer constant).
>
> So, it should be a signed integer (same as int on your running architecture 
> which is internally either int32_t or int64_t depending on how int is 
> represented).
> For type safety, it is important that __LINE__ is an int because a lot of 
> code uses this type to call functions as below:
>
> void debug(const char* file, int line, const char* msg) {
>  printf("%s(%d) %s\n", file, line msg);
> }
>
> gcc, clang and tcc all use an int as you can see below, so tcc looks right to 
> me:

You are not testing the right thing.  It is an int literal in any case, 
but tcc allows it to overflow.
Here is a better test:

$ cat tt.c
#line 2147483644
#include <stdio.h>
#define STR2(x) #x
#define STR(x) STR2(x)
int main(void) {
         puts(STR(__LINE__));
}
$ gcc -o tt tt.c && ./tt
2147483648
$ tcc -o tt tt.c && ./tt
-2147483648

  -E

_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]