source: tests/array.cfa @ d344a63

Last change on this file since d344a63 was 81e768d, checked in by Michael Brooks <mlbrooks@…>, 3 weeks ago

Fix #276; add support for c-array parameters using dependent lengths.

Without this fix, declarations like

void f( int m, int n, float[m][n] );

would either

  • generate bad C code, with unmangled variable names appearing in the function definition, or
  • refuse to resolve a valid-c call of such a function.

tests/array-collections/c-dependent: add direct tests of such cases
tests/tuplearray: activate and expand cases which were blocked on #276
tests/array: activate case fm5y, which was blocked on #276; [noise] adjust source line numbers in .expect
tests/typedefRedef: expand coverage of "error, an array detail is different" cases; [noise] adjust source line numbers in .expect
tests/functions: [noise] adjust .expect to have resolved array sizes (extra casts) in the diffed code dump

The fix is:

  • (ResolvExpr/ResolveTypeof?, ResolvExpr/Resolver?) Resolve the dimension expressions, where they were missed.
  • (ResolvExpr/Resolver?) Prevent dimension expressions that are bound to other parameters from escaping in the function's type, to where they are out of scope. In the f example above, redact the type shown to callers from void (*)(int, int, float[m][n]) to void (*)(int, int, float[][*]).
  • (ResolvExpr/Unify?) Relax the matching rules for such a type, when used at a call site, letting the patameters wildcard type match with the concrete type in scope at the caller's side.
  • (Validate/ReplaceTypedef?) Apply the former, stricter matching rules to the one place where they are still needed: detecting inconsistent typedefs.
  • Property mode set to 100644
File size: 4.2 KB
Line 
1//                               -*- Mode: C -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// array.cfa -- test array declarations
9//
10// Author           : Peter A. Buhr
11// Created On       : Tue Feb 19 21:18:06 2019
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Sat Jun  5 10:05:51 2021
14// Update Count     : 5
15//
16
17// Tests syntax.  Comments explain semantics.  Test does not show semantics.
18// Mostly illustrates facts about C (with which CFA is being tested to agree).
19// Is a test oracle under `gcc -x c`.
20
21#ifdef ERR1
22#define E1(...) __VA_ARGS__
23#else
24#define E1(...)
25#endif
26
27#ifdef ERR2
28#define E2(...) __VA_ARGS__
29#else
30#define E2(...)
31#endif
32
33#ifdef ERR3
34#define E3(...) __VA_ARGS__
35#else
36#define E3(...)
37#endif
38
39    int a1[0];
40E1( int a2[*];       )
41                                                        #ifndef __cforall
42E1( double a4[3.0];  )                                  // BUG 275: CFA accepts but should reject
43                                                        #endif
44
45    int m1[0][3];
46E1( int m2[*][*];    )
47    int m4[3][3];
48
49    typedef int T;
50
51    int fred(int n) {
52E1(     int a1[];    )
53E1(     int a2[*];   )
54        int a4[3];
55        int T[3];
56        int a5[n];
57    }
58
59    int fred2(int n,
60        int a1[],
61E1(     int a2[*],   )
62        int a4[3],
63        int T[3],
64        int a5[n]
65    ) {}
66
67    int mary( int T[3],                                 // same as: int *T
68              int p1[const 3],                          // same as: int const *p1
69              int p2[static 3],                         // same as T, but length >=3 checked
70              int p3[static const 3]                    // both above: 3 is static, p3 is const
71        ) {
72    }
73
74    // function taking (), returning pointer to array of ints
75    int (*tom())[3] {
76    }
77
78    // function taking (), returning pointer to function of same type as mary
79    int (*(jane)())( int T[3],
80                     int p1[const 3],
81                     int p2[static 3],
82                     int p3[static const 3]
83        ) {
84    }
85
86    // functions returning same exotic pointers, in CFA's non-onion syntax
87    #ifdef __cforall
88    [ * [3] int ] toms_twin(...) {
89    }
90    [ * [int]( [3] int T,
91               [const 3] int p1,
92               [static 3] int p2,
93               [static const 3] int p3
94             )
95    ] janes_twin(...) {
96    }
97    #endif
98
99    // GCC 11+ gives a false warning (-Wvla-parameter) on the valid (C11 ARM p134-135) combination:
100    // declare with type int[*], define with type int[n].
101    // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=100420 suggests the internal representation of
102    // of a[*] is the same as a[0].
103    // https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-vla-parameter explains
104    // the purpose of -Wvla-parameter is to report conflicts between int[] and int[n], which would
105    // understandably also include those between int[42] and int[n].
106    // https://stackoverflow.com/questions/17371645/why-use-an-asterisk-instead-of-an-integer-for-a-vla-array-parameter-of-a-f
107    // explains the declare-*, define-n pattern.
108
109    // To work around the false warning, and keep to this test's purpose of exercising CFA's
110    // handling of exotic C array syntax, what would ideally be demonstrated as a declaration of
111    // fm1, followed by its definition, is instead split into fm1x and fm1y.  And similarly for
112    // fm5.
113
114    int fm1x( int, int, int[][*] );
115    int fm1y( int r, int c, int m[][c] ) {}
116    int fm2( int r, int c, int (*m)[c] ) {}             // same as fm1
117E2( int fm3( int r, int c, int m[][static c] ) {}  )    // that's not static
118E3( int fm4( int r, int c, int m[][] );            )    // m's immediate element type is incomplete
119    int fm5x( int, int, int[*][*] );                    // same as fm1 decl
120    int fm5y( int r, int c, int m[r][c] ) {}            // same as fm1 defn
121
122
123
124int main() {
125    #pragma GCC warning "Preprocessor started"          // force non-empty .expect file, NO TABS!!!
126}
127
128// Local Variables: //
129// tab-width: 4 //
130// compile-command: "cfa array.cfa" //
131// End: //
Note: See TracBrowser for help on using the repository browser.