summaryrefslogtreecommitdiff
path: root/tests/diagnostics/int-literal.slang
blob: b3713ec49bdc11e7e885284a18d4b235b060ffd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//DIAGNOSTIC_TEST:SIMPLE:

int doSomething(int a)
{
    // No warning, literal will be interpreted as 64 bit.
    uint64_t c0 = 0x800000000;
    
    // No warning as top bits are just ignored
    int c1 = -1ll;
    
    int c2 = int(-1u);
    
    // Should sign extend 
    int c3 = 0x80000000;
    
    // No warning, hex literal will be interpreted as an unsigned 64 integer then signed with negative operator.
    int64_t c4 = -0xfffffffff;
    
    a += (int)c0 + c1 + c2;
    
    int64_t b = 0;

    // Ok
    b += 0x800000000ll;
    
    uint64_t c5 = -2ull;

    // Warning, integer literal is too large for signed 64 bit, must be interpreted as unsigned.
    uint64_t d0 = 18446744073709551615;

    // Warning, integer literal is too small for signed 64 bit, must be interpreted as unsigned.
    uint64_t d1 = -9223372036854775809;

    // This is INT64_MIN and valid negative signed integer, but warning will be emitted as negative(-) is scanned
    // separately in the lexer, and the positive literal portion will emit a warning.
    // The final value will still be correctly set as INT64_MIN.
    //
    // To not have this warning the lexer must scan the negative operator and number together.
    uint64_t d2 = -9223372036854775808;

    // Warning, integer literal is too large for signed 64 bit, must be interpreted as unsigned.
    int x4 = 0xFFFFFFFFFFFFFFFF;
    
    return a + int(b);
}