diff options
| author | venkataram-nv <vedavamadath@nvidia.com> | 2024-07-18 17:43:19 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-18 17:43:19 -0700 |
| commit | 6e7c726658c775e97578e7a9dd99d23b819870bd (patch) | |
| tree | 67d00d82a910068ba0da0cf230909eade09735a4 /tests | |
| parent | 831d79654996b3d1e9af3ca0cc580f71997eb346 (diff) | |
Warnings for uninitialized fields in constructors (#4680)
* Detect uninitialized fields in constructors
* Reachability check for early returns
* Specialized warnings for synthesized default initializers
* Handling quirks with constructors
* Addressing review comments
* Ignore synthesized constructors if they are not used
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/compute/struct-default-init.slang | 6 | ||||
| -rw-r--r-- | tests/diagnostics/uninitialized-fields.slang | 108 | ||||
| -rw-r--r-- | tests/language-feature/struct-field-initializers/struct-field-initializer.slang | 4 |
3 files changed, 113 insertions, 5 deletions
diff --git a/tests/compute/struct-default-init.slang b/tests/compute/struct-default-init.slang index 50933ae84..dc0e0218a 100644 --- a/tests/compute/struct-default-init.slang +++ b/tests/compute/struct-default-init.slang @@ -3,9 +3,9 @@ struct Test { - int a; + int a = 0; int b = 1; - int c; + int c = 0; int d = 1 + 1; } @@ -37,4 +37,4 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) int outVal = test(inVal); outputBuffer[tid] = outVal; -}
\ No newline at end of file +} diff --git a/tests/diagnostics/uninitialized-fields.slang b/tests/diagnostics/uninitialized-fields.slang new file mode 100644 index 000000000..29e065d2b --- /dev/null +++ b/tests/diagnostics/uninitialized-fields.slang @@ -0,0 +1,108 @@ +//TEST:SIMPLE(filecheck=CHK): -target spirv + +extern static const int flag; + +struct None +{ + int a; + int b; + + __init() + { + a = 0; + b = 0; + } +} + +struct Simple +{ + int value; + Simple *next; + + __init(int v, Simple* ptr) + { + value = v; + //CHK-DAG: warning 41020: exiting constructor without initializing field 'next' + } +} + +struct Conditional +{ + int field; + int another_field; + + __init() + { + field = 0; + if (flag == 0) + //CHK-DAG: warning 41020: exiting constructor without initializing field 'another_field' + return; + + another_field = 0; + } +}; + +struct Partial +{ + int inline = 0; + int body = 0; + + __init(int x) + { + body = x; + } +} + +// Warnings here should be a bit different +struct Implicit +{ + //CHK-DAG: warning 41021: default initializer for 'Implicit' will not initialize field 'a' + int a; + int b = 1; + //CHK-DAG: warning 41021: default initializer for 'Implicit' will not initialize field 'c' + int c; + int d = 1 + 1; +} + +int using_implicit(int x) +{ + Implicit impl; + impl.a = x; + return impl.c; +} + +struct UnusedImplicit +{ + // Shouldn't warn here, since its never used + int a; + int b = 1; + int c; + int d = 1 + 1; +} + +struct Pass +{ + int x = 0; + int y = 0; +} + +struct Impl +{ + float x; + + __init(float val) + { + x = val; + } + + __init() + { + float val = 2.0; + + // Shouldn't trigger a warning here + return Impl(val); + } +} + +//CHK-NOT: warning 41020 +//CHK-NOT: warning 41021 diff --git a/tests/language-feature/struct-field-initializers/struct-field-initializer.slang b/tests/language-feature/struct-field-initializers/struct-field-initializer.slang index 0a03644aa..7c4eeeef4 100644 --- a/tests/language-feature/struct-field-initializers/struct-field-initializer.slang +++ b/tests/language-feature/struct-field-initializers/struct-field-initializer.slang @@ -11,7 +11,7 @@ static int myThree = 1+2; struct DefaultStructNoInit { - int data0; + int data0 = 0; int data1 = myTwo; int data2 = 2; }; @@ -57,4 +57,4 @@ void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) && withInit2.data1 == 4 && withInit2.data2 == 4 ; -}
\ No newline at end of file +} |
