source: tests/array-container/dimexpr-match.hfa @ 65ef0cd

Last change on this file since 65ef0cd was f02f546, checked in by Michael Brooks <mlbrooks@…>, 17 months ago

Implement new rules for array dimension expression matching.

Core changes:
src/ResolvExpr/Unify.cc: add sense of "these two expressions unify"
src/InitTweak/GenInit.cc: make hoisting happen more often
tests/array-container/*: reconfigure the array-dimension test to use non-"classic" expectation rules

Misc contributors and noise:
libcfa/src/parseargs.cfa: ,ake a parameter, that's used as a length expression, constant (example of an array user following new rules)
src/ResolvExpr/ResolveTypeof.h: make fixArrayType public
src/Validate/GenericParameter.cpp: do the array-type desugaring in both AST corners that forall-variables can be found (not just in one of them)
tests/.expect/typedefRedef-ERR1.txt: old one was "expecting" a bug, that new array rules handle correctly

  • Property mode set to 100644
File size: 12.2 KB
Line 
1
2
3//  Framework for the dimexpr-match tests.
4//
5//  These tests show whether an expression of type like `float (*)[25]`
6//  can be used in a context expecting a type like `float (*)[rand()]`.
7//
8//  A test includes this header and defines the function `runTests`.
9//  The `runTests` body is made with macros defined here.
10//
11//  Normally, drive a test either from test.py or ./dimexpr-match-detail.sh.
12//  Direct usage is for making small repro cases, while troubleshooting a problem.
13//
14//  Direct usage is:
15//
16//      compiler=gcc -x c           # pick one
17//      compiler=$fa
18//
19//      test=dimexpr-match-c.cfa    # pick one
20//      test=dimexpr-match-cfa.cfa
21//
22//      $compiler $test
23//      ./a.out
24//      ./a.out -cmd4skip | sed -E -n 's/skip.*\| *//p'
25//
26//  Expect the above compiler invocation to report success.
27//  Expect the first a.out invocation to print a mix of "done" and "skipped" messages.
28//  Assume the second a.out invocation (through sed) prints several compiler CLI invocations.  For each...
29//  Expect that running the current compiler invocation reports an error.
30//  So far, this is what dimexpr-match-detail.sh does.
31//
32//  The macro logic in here is hard to read.
33//  A specific test's TRY_COMPAT definition is easy to read.
34//
35//  An error message from an unexpected rejection is hard to read, because the message blames a macro.
36//  To diagnose one, first obtain the failing case's coordinates by ruuning:
37//      $compiler $test -E -P -DEMIT_COORDS > temp.cfa  # just preprocessor
38//      $compiler temp.cfa                              # will fail, want message
39//  Open temp.cfa and chase an error detail's line number, to find the failing code.
40//  Look at the pragma-message above the failing line, and read off the failure's -D coordinates.
41//  Isolate this case by running:                              ... copied from pragma message ...............................................................................................
42//      $compiler $test -E -P -DEMIT_MINIMAL -DINCLUDE_MINIMAL -DARRANGEMENT__OPTIN -DARRANGEMENT_PTRPARM_---- -DCASE_MANUAL -DCASE_FORMATION_L=--- -DCASE_FORMATION_R=--- -DCASE_EQUALITY=-- > mycase.cfa  # template
43//      $compiler $test -E -P -DEMIT_MINIMAL -DINCLUDE_MINIMAL -DARRANGEMENT__OPTIN -DARRANGEMENT_PTRPARM_CALL -DCASE_MANUAL -DCASE_FORMATION_L=LIT -DCASE_FORMATION_R=LIT -DCASE_EQUALITY=NE > mycase.cfa  # example
44//      $compiler mycase.cfa
45//  The content of mycase.cfa is easy to read.
46
47#ifdef __cforall
48    extern "C" {
49        int strcmp (const char* str1, const char* str2);
50    }
51    #define NULL 0p
52#else
53    #include <stdio.h>
54    #include <string.h>
55#endif
56
57#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
58#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
59
60#define DO_PRAGMA(x) _Pragma (#x)
61#define DO_MESSAGE(x) DO_PRAGMA(message #x)
62
63#ifdef EMIT_COORDS
64#define MAYPRINT(...)
65#define MAYDELIMIT( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME ) \
66    DO_MESSAGE(-DARRANGEMENT__OPTIN -DARRANGEMENT_ ## ARR_NAME -DCASE_MANUAL -DCASE_FORMATION_L=KL -DCASE_FORMATION_R=KR -DCASE_EQUALITY=ISEQ)
67#else
68#define MAYPRINT(...) printf(__VA_ARGS__);
69#define MAYDELIMIT( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME )
70#endif
71
72#ifdef EMIT_MINIMAL
73#undef MAYPRINT
74#define MAYPRINT(...)
75#endif
76
77#define GRP_K_LIT STA
78#define GRP_K_ENU STA
79#define GRP_K_CPR DYN
80#define GRP_K_DIM DYN
81#define GRP_K_MUT UNS
82#define GRP_K( K ) GRP_K_ ## K
83
84#define MKV7_LIT 7
85#define MKV7_ENU enu7
86#define MKV7_CPR cpr7
87#define MKV7_DIM dim7
88#define MKV7_MUT mut7
89#define MKV7( K ) MKV7_ ## K
90
91#define MKV42_LIT 42
92#define MKV42_ENU enu42
93#define MKV42_CPR cpr42
94#define MKV42_DIM dim42
95#define MKV42_MUT mut42
96#define MKV42( K ) MKV42_ ## K
97
98#define MKLV( LK ) MKV7( LK )
99
100#define MKRV_EQ( KR ) MKV7(KR)
101#define MKRV_NE( KR ) MKV42(KR)
102#define MKRV( KR, ISEQ ) MKRV_ ## ISEQ( KR )
103
104#ifdef __cforall
105#define RULE_PFX RULE_CF_
106#else
107#define RULE_PFX RULE_C_
108#endif
109
110#define X_CASE_2( KL, KR, ISEQ, FL, FR, ARR_NAME ) \
111    MAY_TRY_COMPAT( \
112        CAT(CAT(CAT(CAT(CAT(RULE_PFX, ISEQ), _), FL), _), FR), \
113        MKLV( KL ), \
114        MKRV( KR, ISEQ ), \
115        KL, KR, FL, FR, ISEQ, ARR_NAME \
116    )
117
118#define X_CASE( KL, KR, ISEQ, ARR_NAME ) X_CASE_2( KL, KR, ISEQ, GRP_K(KL), GRP_K(KR), ARR_NAME )
119
120#ifdef ERRS
121#define MAY_TRY_COMPAT_REJ( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME ) \
122    _Pragma("message \"Expect fail\"") \
123    MAY_TRY_COMPAT_ACC( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME )
124#else
125#define MAY_TRY_COMPAT_REJ( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME ) \
126    MAYPRINT("skip %s %s %s, L=%s, R=%s",  #FL, #ISEQ, #FR, #LV, #RV ) \
127    if (cmd4skip) { \
128        MAYPRINT(" | -DERRS -Werror %s -DARRANGEMENT__OPTIN -DARRANGEMENT_%s -DCASE_MANUAL -DCASE_FORMATION_L=%s -DCASE_FORMATION_R=%s -DCASE_EQUALITY=%s",  __FILE__, #ARR_NAME, #KL, #KR, #ISEQ ) \
129    } \
130    MAYPRINT("\n")
131#endif
132
133#define MAY_TRY_COMPAT_ACC( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME ) \
134    MAYDELIMIT( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME ) \
135    TRY_COMPAT(LV, RV) \
136    MAYPRINT("done %s %s %s, L=%s, R=%s\n",  #FL, #ISEQ, #FR, #LV, #RV )
137
138#define MAY_TRY_COMPAT(EXPECT, LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME) \
139    CAT(MAY_TRY_COMPAT_, EXPECT( LV, RV, KL, KR, FL, FR, ISEQ, ARR_NAME ))
140
141#define TCXXX(Q) Q
142#define TCXX(Q) #Q
143
144#define QUOTE(str) #str
145#define EXPAND_AND_QUOTE(str) QUOTE(str)
146#define TRY_COMPAT_E EXPAND_AND_QUOTE(TRY_COMPAT(__L__,__R__))
147
148#if ! defined __cforall
149#define SUPPRESS_FORALL_N
150#endif
151
152#if defined SUPPRESS_FORALL_N_ON_CLASSIC && defined CFA_IS_CLASSIC
153#define SUPPRESS_FORALL_N
154#endif
155
156/*
157
158RULE macros define the expected outcome:
159Given a compiler's functionality level and a  test case, do we expect the compiler to accept or reject the case?
160
161Group STA: Statically stable
162    - literal
163    - enumeration
164
165Group DYN: Dynamically stable
166    - parameter that is a constant
167    - static constant global
168
169Group UNS: potentially Unstable
170    - variable
171    - parameter that is a constant reference to constant
172    - function return
173    - extern constant (safe and annoying here; must be here if you can link it to something that changes)
174
175C rules
176    - Reject a pair from group STA if it has two different values
177    - otherwise, accept
178
179CFA "classic" rules (what CFA actually does, before Mike brings in CFA rules)
180    - same as C, except
181    - reject STA/non-STA crossing
182
183CFA rules ("preview" only)
184    - accept a pair from group STA if it has the same value
185    - accept a pair from group DYN if it is written out the same (e.g. refers to same variable)
186    - otherwise, reject
187        (notably, reject all group UNS)
188        (notably, reject any group crossing)
189
190The RULE_* definitions below encode the summary above into the test harness's format.
191This format is: tabled function to {ACC,REJ} from {STA,DYN,UNS}^2 x {EQ,NE}
192
193*/
194
195#define RULE_C_EQ_STA_STA  ACC
196#define RULE_C_EQ_STA_DYN  ACC
197#define RULE_C_EQ_STA_UNS  ACC
198#define RULE_C_EQ_DYN_STA  ACC
199#define RULE_C_EQ_DYN_DYN  ACC
200#define RULE_C_EQ_DYN_UNS  ACC
201#define RULE_C_EQ_UNS_STA  ACC
202#define RULE_C_EQ_UNS_DYN  ACC
203#define RULE_C_EQ_UNS_UNS  ACC
204
205#define RULE_C_NE_STA_STA  REJ
206#define RULE_C_NE_STA_DYN  ACC
207#define RULE_C_NE_STA_UNS  ACC
208#define RULE_C_NE_DYN_STA  ACC
209#define RULE_C_NE_DYN_DYN  ACC
210#define RULE_C_NE_DYN_UNS  ACC
211#define RULE_C_NE_UNS_STA  ACC
212#define RULE_C_NE_UNS_DYN  ACC
213#define RULE_C_NE_UNS_UNS  ACC
214
215#ifdef CFA_IS_CLASSIC
216
217#define RULE_CF_EQ_STA_STA  ACC
218#define RULE_CF_EQ_STA_DYN  REJ
219#define RULE_CF_EQ_STA_UNS  REJ
220#define RULE_CF_EQ_DYN_STA  REJ
221#define RULE_CF_EQ_DYN_DYN  ACC
222#define RULE_CF_EQ_DYN_UNS  ACC
223#define RULE_CF_EQ_UNS_STA  REJ
224#define RULE_CF_EQ_UNS_DYN  ACC
225#define RULE_CF_EQ_UNS_UNS  ACC
226
227#define RULE_CF_NE_STA_STA  REJ
228#define RULE_CF_NE_STA_DYN  REJ
229#define RULE_CF_NE_STA_UNS  REJ
230#define RULE_CF_NE_DYN_STA  REJ
231#define RULE_CF_NE_DYN_DYN  ACC
232#define RULE_CF_NE_DYN_UNS  ACC
233#define RULE_CF_NE_UNS_STA  REJ
234#define RULE_CF_NE_UNS_DYN  ACC
235#define RULE_CF_NE_UNS_UNS  ACC
236
237#else
238
239#define RULE_CF_EQ_STA_STA  ACC
240#define RULE_CF_EQ_STA_DYN  REJ
241#define RULE_CF_EQ_STA_UNS  REJ
242#define RULE_CF_EQ_DYN_STA  REJ
243#define RULE_CF_EQ_DYN_DYN  ACC
244#define RULE_CF_EQ_DYN_UNS  REJ
245#define RULE_CF_EQ_UNS_STA  REJ
246#define RULE_CF_EQ_UNS_DYN  REJ
247#define RULE_CF_EQ_UNS_UNS  REJ
248
249#define RULE_CF_NE_STA_STA  REJ
250#define RULE_CF_NE_STA_DYN  REJ
251#define RULE_CF_NE_STA_UNS  REJ
252#define RULE_CF_NE_DYN_STA  REJ
253#define RULE_CF_NE_DYN_DYN  REJ
254#define RULE_CF_NE_DYN_UNS  REJ
255#define RULE_CF_NE_UNS_STA  REJ
256#define RULE_CF_NE_UNS_DYN  REJ
257#define RULE_CF_NE_UNS_UNS  REJ
258
259#endif
260
261#ifndef ARRANGEMENT__OPTIN
262#define ARRANGEMENT_PTRVAR_INIT 1
263#define ARRANGEMENT_PTRPARM_CALL 1
264#define ARRANGEMENT_PTRVAR_ASGN 1
265#define ARRANGEMENT_REFVAR_INIT 1
266#define ARRANGEMENT_REFPARM_CALL 1
267#define ARRANGEMENT_REFVAR_ASGN 1
268#define ARRANGEMENT_CALLZIP 1
269#endif
270
271#define EMIT_SKIP( ARR_NAME ) \
272    MAYPRINT( "---- " #ARR_NAME " skipped\n" )
273
274#define MAY_EMIT_ARRANGEMENT_PTRVAR_INIT( ARR_NAME ) EMIT_SKIP( ARR_NAME )
275#define MAY_EMIT_ARRANGEMENT_PTRPARM_CALL( ARR_NAME ) EMIT_SKIP( ARR_NAME )
276#define MAY_EMIT_ARRANGEMENT_PTRVAR_ASGN( ARR_NAME ) EMIT_SKIP( ARR_NAME )
277#define MAY_EMIT_ARRANGEMENT_REFVAR_INIT( ARR_NAME ) EMIT_SKIP( ARR_NAME )
278#define MAY_EMIT_ARRANGEMENT_REFPARM_CALL( ARR_NAME ) EMIT_SKIP( ARR_NAME )
279#define MAY_EMIT_ARRANGEMENT_REFVAR_ASGN( ARR_NAME ) EMIT_SKIP( ARR_NAME )
280#define MAY_EMIT_ARRANGEMENT_CALLZIP( ARR_NAME ) EMIT_SKIP( ARR_NAME )
281
282#define MAY_EMIT_1( ARR_NAME ) \
283    MAYPRINT( "---- " #ARR_NAME ":   " TRY_COMPAT_E "\n" ) \
284    RUNCASES( ARR_NAME )
285
286#ifndef CASE_MANUAL
287  #define RUNCASES( ARR_NAME ) \
288    X_CASE( LIT, LIT, EQ, ARR_NAME ) \
289    X_CASE( LIT, LIT, NE, ARR_NAME ) \
290    X_CASE( LIT, ENU, EQ, ARR_NAME ) \
291    X_CASE( LIT, ENU, NE, ARR_NAME ) \
292    X_CASE( LIT, CPR, NE, ARR_NAME ) \
293    X_CASE( LIT, MUT, NE, ARR_NAME ) \
294    X_CASE( ENU, ENU, EQ, ARR_NAME ) \
295    X_CASE( ENU, ENU, NE, ARR_NAME ) \
296    X_CASE( ENU, LIT, EQ, ARR_NAME ) \
297    X_CASE( ENU, LIT, NE, ARR_NAME ) \
298    X_CASE( ENU, CPR, NE, ARR_NAME ) \
299    X_CASE( ENU, MUT, NE, ARR_NAME ) \
300    X_CASE( CPR, CPR, EQ, ARR_NAME ) \
301    X_CASE( CPR, CPR, NE, ARR_NAME ) \
302    X_CASE( CPR, LIT, NE, ARR_NAME ) \
303    X_CASE( CPR, ENU, NE, ARR_NAME ) \
304    X_CASE( CPR, MUT, NE, ARR_NAME ) \
305    X_CASE( MUT, MUT, EQ, ARR_NAME ) \
306    X_CASE( MUT, MUT, NE, ARR_NAME ) \
307    X_CASE( MUT, LIT, NE, ARR_NAME ) \
308    X_CASE( MUT, ENU, NE, ARR_NAME ) \
309    X_CASE( MUT, CPR, NE, ARR_NAME ) \
310    RUNCASES_FORALL_N( ARR_NAME )
311
312  #ifndef SUPPRESS_FORALL_N
313  #define RUNCASES_FORALL_N( ARR_NAME ) \
314    X_CASE( LIT, DIM, NE, ARR_NAME ) \
315    X_CASE( ENU, DIM, NE, ARR_NAME ) \
316    X_CASE( CPR, DIM, NE, ARR_NAME ) \
317    X_CASE( DIM, DIM, EQ, ARR_NAME ) \
318    X_CASE( DIM, DIM, NE, ARR_NAME ) \
319    X_CASE( DIM, LIT, NE, ARR_NAME ) \
320    X_CASE( DIM, ENU, NE, ARR_NAME ) \
321    X_CASE( DIM, CPR, NE, ARR_NAME ) \
322    X_CASE( DIM, MUT, NE, ARR_NAME ) \
323    X_CASE( MUT, DIM, NE, ARR_NAME )
324  #else
325  #define RUNCASES_FORALL_N( ARR_NAME )
326  #endif
327
328#else
329    #ifndef CASE_FORMATION_L
330    #error CASE_FORMATION_L not specified along with CASE_MANUAL
331    #endif
332    #ifndef CASE_FORMATION_R
333    #error CASE_FORMATION_R not specified along with CASE_MANUAL
334    #endif
335    #ifndef CASE_EQUALITY
336    #error CASE_EQUALITY not specified along with CASE_MANUAL
337    #endif
338    #define RUNCASES( ARR_NAME ) X_CASE( CASE_FORMATION_L, CASE_FORMATION_R, CASE_EQUALITY, ARR_NAME )
339#endif
340
341#define ARRANGEMENT( ARR_NAME ) CAT(MAY_EMIT_, CAT(ARRANGEMENT_, ARR_NAME)) ( ARR_NAME )
342
343
344#if defined SUPPRESS_FORALL_N
345
346    #define DECLN_runTests                   \
347        void runTests(                       \
348            const int cpr7, const int cpr42, \
349            int cmd4skip                     \
350        )
351#else
352
353    forall( [N] ) struct quicktag {};
354
355    #define DECLN_runTests                   \
356        forall( [dim7], [dim42] )            \
357        void runTests(                       \
358            const int cpr7, const int cpr42, \
359            quicktag(dim7), quicktag(dim42), \
360            int cmd4skip                     \
361        )
362#endif
363
364DECLN_runTests;
365
366int main(int argc, const char *argv[]) {
367
368    int cmd4skip = 0;
369
370    if (argc > 1 && strcmp("-cmd4skip", argv[1]) == 0) {
371        cmd4skip = 1;
372    }
373
374  #if defined SUPPRESS_FORALL_N
375    runTests(7, 42, cmd4skip);
376  #else
377    quicktag(7) tag7;
378    quicktag(42) tag42;
379    runTests(7, 42, tag7, tag42, cmd4skip);
380  #endif
381
382    return 0;
383}
Note: See TracBrowser for help on using the repository browser.