Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/doom-s_sound-pr108867.c

raw
1 /* Reduced from Doom's linuxdoom-1.10/s_sound.c, which is GPLv2 or later. */
2
3 /* { dg-additional-options "-std=gnu17 -fno-analyzer-call-summaries -Wno-analyzer-too-complex -Wno-analyzer-symbol-too-complex" } */
4 /* { dg-require-effective-target size32plus } */
5
6 typedef struct _IO_FILE FILE;
7 extern FILE* stderr;
8 extern int
9 fprintf(FILE* __restrict __stream, const char* __restrict __format, ...);
10 extern int
11 sprintf(char* __restrict __s, const char* __restrict __format, ...)
12 __attribute__((__nothrow__));
13 extern int
14 abs(int __x) __attribute__((__nothrow__, __leaf__)) __attribute__((__const__));
15
16 typedef enum
17 {
18 false,
19 true
20 } boolean;
21
22 typedef unsigned char byte;
23
24 void
25 I_Error(char* error, ...);
26
27 typedef enum
28 {
29 shareware,
30 registered,
31 commercial,
32 /* [...snip...] */
33 } GameMode_t;
34
35 typedef int fixed_t;
36
37 fixed_t
38 FixedMul(fixed_t a, fixed_t b);
39
40 extern fixed_t finesine[5 * 8192 / 4];
41 typedef unsigned angle_t;
42
43 typedef struct mobj_s
44 {
45 /* [...snip...] */
46 fixed_t x;
47 fixed_t y;
48 fixed_t z;
49 /* [...snip...] */
50 angle_t angle;
51 /* [...snip...] */
52 } mobj_t;
53
54 typedef struct player_s
55 {
56 mobj_t* mo;
57 /* [...snip...] */
58 } player_t;
59
60 extern GameMode_t gamemode;
61 extern int gameepisode;
62 extern int gamemap;
63 extern int consoleplayer;
64 extern player_t players[4];
65
66 typedef struct sfxinfo_struct sfxinfo_t;
67
68 struct sfxinfo_struct
69 {
70 /* [...snip...] */
71 int priority;
72 sfxinfo_t* link;
73 int pitch;
74 int volume;
75 void* data;
76 int usefulness;
77 int lumpnum;
78 };
79
80 typedef struct
81 {
82 char* name;
83 int lumpnum;
84 void* data;
85 int handle;
86 } musicinfo_t;
87
88 extern sfxinfo_t S_sfx[];
89
90 extern musicinfo_t S_music[];
91
92 typedef enum
93 {
94 mus_None,
95 mus_e1m1,
96 /* [...snip...] */
97 mus_e1m5,
98 /* [...snip...] */
99 mus_e1m9,
100 /* [...snip...] */
101 mus_e2m4,
102 mus_e2m5,
103 mus_e2m6,
104 mus_e2m7,
105 /* [...snip...] */
106 mus_e3m2,
107 mus_e3m3,
108 mus_e3m4,
109 /* [...snip...] */
110 mus_runnin,
111 /* [...snip...] */
112 NUMMUSIC
113 } musicenum_t;
114
115 typedef enum
116 {
117 /* [...snip...] */
118 sfx_sawup,
119 /* [...snip...] */
120 sfx_sawhit,
121 /* [...snip...] */
122 sfx_itemup,
123 /* [...snip...] */
124 sfx_tink,
125 /* [...snip...] */
126 NUMSFX
127 } sfxenum_t;
128
129
130 void
131 I_SetChannels();
132
133 int
134 I_GetSfxLumpNum(sfxinfo_t* sfxinfo);
135
136 int
137 I_StartSound(int id, int vol, int sep, int pitch, int priority);
138
139 void
140 I_StopSound(int handle);
141 int
142 I_SoundIsPlaying(int handle);
143 void
144 I_UpdateSoundParams(int handle, int vol, int sep, int pitch);
145
146 void
147 I_SetMusicVolume(int volume);
148
149 void
150 I_PauseSong(int handle);
151 void
152 I_ResumeSong(int handle);
153 int
154 I_RegisterSong(void* data);
155
156 void
157 I_PlaySong(int handle, int looping);
158
159 void
160 I_StopSong(int handle);
161
162 void
163 I_UnRegisterSong(int handle);
164 void
165 S_StopSound(void* origin);
166 void
167 S_ChangeMusic(int music_id, int looping);
168 void
169 S_StopMusic(void);
170
171 void
172 S_SetMusicVolume(int volume);
173 void
174 S_SetSfxVolume(int volume);
175
176 void*
177 Z_Malloc(int size, int tag, void* ptr);
178 void
179 Z_ChangeTag2(void* ptr, int tag);
180
181 typedef struct memblock_s
182 {
183 /* [...snip...] */
184 int id;
185 /* [...snip...] */
186 } memblock_t;
187 int
188 M_Random(void);
189 int
190 W_GetNumForName(char* name);
191 void*
192 W_CacheLumpNum(int lump, int tag);
193 angle_t
194 R_PointToAngle2(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2);
195
196 typedef struct
197 {
198 sfxinfo_t* sfxinfo;
199 void* origin;
200 int handle;
201 } channel_t;
202 static channel_t* channels;
203
204 int snd_SfxVolume = 15;
205 int snd_MusicVolume = 15;
206 static boolean mus_paused;
207 static musicinfo_t* mus_playing = 0;
208 int numChannels;
209 static int nextcleanup;
210
211 int
212 S_getChannel(void* origin, sfxinfo_t* sfxinfo);
213
214 int
215 S_AdjustSoundParams(mobj_t* listener,
216 mobj_t* source,
217 int* vol,
218 int* sep,
219 int* pitch);
220 void
221 S_StopChannel(int cnum);
222
223 void
224 S_Init(int sfxVolume, int musicVolume)
225 {
226 int i;
227
228 fprintf(stderr, "S_Init: default sfx volume %d\n", sfxVolume);
229
230 I_SetChannels();
231
232 S_SetSfxVolume(sfxVolume);
233
234 S_SetMusicVolume(musicVolume);
235
236 channels = (channel_t*)Z_Malloc(numChannels * sizeof(channel_t), 1, 0);
237
238 for (i = 0; i < numChannels; i++)
239 channels[i].sfxinfo = 0;
240
241 mus_paused = 0;
242
243 for (i = 1; i < NUMSFX; i++)
244 S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
245 }
246 void
247 S_Start(void)
248 {
249 int cnum;
250 int mnum;
251
252 for (cnum = 0; cnum < numChannels; cnum++)
253 if (channels[cnum].sfxinfo)
254 S_StopChannel(cnum);
255
256 mus_paused = 0;
257
258 if (gamemode == commercial)
259 mnum = mus_runnin + gamemap - 1;
260 else {
261 int spmus[] = {
262
263 mus_e3m4, mus_e3m2, mus_e3m3, mus_e1m5, mus_e2m7,
264 mus_e2m4, mus_e2m6, mus_e2m5, mus_e1m9
265 };
266
267 if (gameepisode < 4)
268 mnum = mus_e1m1 + (gameepisode - 1) * 9 + gamemap - 1;
269 else
270 mnum = spmus[gamemap - 1];
271 }
272
273 S_ChangeMusic(mnum, true);
274
275 nextcleanup = 15;
276 }
277
278 void
279 S_StartSoundAtVolume(void* origin_p, int sfx_id, int volume)
280 {
281
282 int rc;
283 int sep;
284 int pitch;
285 int priority;
286 sfxinfo_t* sfx;
287 int cnum;
288
289 mobj_t* origin = (mobj_t*)origin_p;
290 if (sfx_id < 1 || sfx_id > NUMSFX)
291 I_Error("Bad sfx #: %d", sfx_id);
292
293 sfx = &S_sfx[sfx_id];
294
295 if (sfx->link) {
296 pitch = sfx->pitch;
297 priority = sfx->priority;
298 volume += sfx->volume;
299
300 if (volume < 1)
301 return;
302
303 if (volume > snd_SfxVolume)
304 volume = snd_SfxVolume;
305 } else {
306 pitch = 128;
307 priority = 64;
308 }
309
310 if (origin && origin != players[consoleplayer].mo) {
311 rc = S_AdjustSoundParams(
312 players[consoleplayer].mo, origin, &volume, &sep, &pitch);
313
314 if (origin->x == players[consoleplayer].mo->x &&
315 origin->y == players[consoleplayer].mo->y) {
316 sep = 128;
317 }
318
319 if (!rc)
320 return;
321 } else {
322 sep = 128;
323 }
324
325 if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit) {
326 pitch += 8 - (M_Random() & 15);
327
328 if (pitch < 0)
329 pitch = 0;
330 else if (pitch > 255)
331 pitch = 255;
332 } else if (sfx_id != sfx_itemup && sfx_id != sfx_tink) {
333 pitch += 16 - (M_Random() & 31);
334
335 if (pitch < 0)
336 pitch = 0;
337 else if (pitch > 255)
338 pitch = 255;
339 }
340
341 S_StopSound(origin);
342
343 cnum = S_getChannel(origin, sfx);
344
345 if (cnum < 0)
346 return;
347 if (sfx->lumpnum < 0)
348 sfx->lumpnum = I_GetSfxLumpNum(sfx);
349
350 if (!sfx->data) {
351 fprintf(stderr, "S_StartSoundAtVolume: 16bit and not pre-cached - wtf?\n");
352 }
353
354 if (sfx->usefulness++ < 0)
355 sfx->usefulness = 1;
356
357 channels[cnum].handle = I_StartSound(sfx_id,
358
359 volume,
360 sep,
361 pitch,
362 priority);
363 }
364
365 void
366 S_StartSound(void* origin, int sfx_id)
367 {
368
369 S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
370 }
371
372 void
373 S_StopSound(void* origin)
374 {
375
376 int cnum;
377
378 for (cnum = 0; cnum < numChannels; cnum++) {
379 if (channels[cnum].sfxinfo && channels[cnum].origin == origin) {
380 S_StopChannel(cnum);
381 break;
382 }
383 }
384 }
385 void
386 S_PauseSound(void)
387 {
388 if (mus_playing && !mus_paused) {
389 I_PauseSong(mus_playing->handle);
390 mus_paused = true;
391 }
392 }
393
394 void
395 S_ResumeSound(void)
396 {
397 if (mus_playing && mus_paused) {
398 I_ResumeSong(mus_playing->handle);
399 mus_paused = false;
400 }
401 }
402
403 void
404 S_UpdateSounds(void* listener_p)
405 {
406 int audible;
407 int cnum;
408 int volume;
409 int sep;
410 int pitch;
411 sfxinfo_t* sfx;
412 channel_t* c;
413
414 mobj_t* listener = (mobj_t*)listener_p;
415 for (cnum = 0; cnum < numChannels; cnum++) {
416 c = &channels[cnum];
417 sfx = c->sfxinfo;
418
419 if (c->sfxinfo) {
420 if (I_SoundIsPlaying(c->handle)) {
421
422 volume = snd_SfxVolume;
423 pitch = 128;
424 sep = 128;
425
426 if (sfx->link) {
427 pitch = sfx->pitch;
428 volume += sfx->volume;
429 if (volume < 1) {
430 S_StopChannel(cnum);
431 continue;
432 } else if (volume > snd_SfxVolume) {
433 volume = snd_SfxVolume;
434 }
435 }
436
437 if (c->origin && listener_p != c->origin) {
438 audible =
439 S_AdjustSoundParams(listener, c->origin, &volume, &sep, &pitch);
440
441 if (!audible) {
442 S_StopChannel(cnum);
443 } else
444 I_UpdateSoundParams(c->handle, volume, sep, pitch);
445 }
446 } else {
447
448 S_StopChannel(cnum);
449 }
450 }
451 }
452 }
453
454 void
455 S_SetMusicVolume(int volume)
456 {
457 if (volume < 0 || volume > 127) {
458 I_Error("Attempt to set music volume at %d", volume);
459 }
460
461 I_SetMusicVolume(127);
462 I_SetMusicVolume(volume);
463 snd_MusicVolume = volume;
464 }
465
466 void
467 S_SetSfxVolume(int volume)
468 {
469
470 if (volume < 0 || volume > 127)
471 I_Error("Attempt to set sfx volume at %d", volume);
472
473 snd_SfxVolume = volume;
474 }
475
476 void
477 S_StartMusic(int m_id)
478 {
479 S_ChangeMusic(m_id, false);
480 }
481
482 void
483 S_ChangeMusic(int musicnum, int looping)
484 {
485 musicinfo_t* music;
486 char namebuf[9];
487
488 if ((musicnum <= mus_None) || (musicnum >= NUMMUSIC)) {
489 I_Error("Bad music number %d", musicnum);
490 } else
491 music = &S_music[musicnum];
492
493 /* We don't know that I_Error exits, so actually a false positive;
494 see PR analyzer/108867. */
495
496 if (mus_playing == music) /* { dg-warning "use of uninitialized value 'music'" } */
497 return;
498
499 S_StopMusic();
500
501 /* We shouldn't issue further warnings about 'music' being
502 uninitialized. */
503
504 if (!music->lumpnum) { /* { dg-bogus "use of uninitialized value 'music'" } */
505 sprintf(namebuf, "d_%s", music->name); /* { dg-bogus "use of uninitialized value 'music'" } */
506 music->lumpnum = W_GetNumForName(namebuf); /* { dg-bogus "use of uninitialized value 'music'" } */
507 }
508
509 music->data = (void*)W_CacheLumpNum(music->lumpnum, 3); /* { dg-bogus "use of uninitialized value 'music'" } */
510 music->handle = I_RegisterSong(music->data); /* { dg-bogus "use of uninitialized value 'music'" } */
511
512 I_PlaySong(music->handle, looping); /* { dg-bogus "use of uninitialized value 'music'" } */
513
514 mus_playing = music; /* { dg-bogus "use of uninitialized value 'music'" } */
515 }
516
517 void
518 S_StopMusic(void)
519 {
520 if (mus_playing) {
521 if (mus_paused)
522 I_ResumeSong(mus_playing->handle);
523
524 I_StopSong(mus_playing->handle);
525 I_UnRegisterSong(mus_playing->handle);
526 {
527 if (((memblock_t*)((byte*)(mus_playing->data) - sizeof(memblock_t)))
528 ->id != 0x1d4a11)
529 I_Error("Z_CT at "
530 "s_sound.c"
531 ":%i",
532 699);
533 Z_ChangeTag2(mus_playing->data, 101);
534 };
535 ;
536
537 mus_playing->data = 0;
538 mus_playing = 0;
539 }
540 }
541
542 void
543 S_StopChannel(int cnum)
544 {
545
546 int i;
547 channel_t* c = &channels[cnum];
548
549 if (c->sfxinfo) {
550
551 if (I_SoundIsPlaying(c->handle)) {
552
553 I_StopSound(c->handle);
554 }
555
556 for (i = 0; i < numChannels; i++) {
557 if (cnum != i && c->sfxinfo == channels[i].sfxinfo) {
558 break;
559 }
560 }
561
562 c->sfxinfo->usefulness--;
563
564 c->sfxinfo = 0;
565 }
566 }
567 int
568 S_AdjustSoundParams(mobj_t* listener,
569 mobj_t* source,
570 int* vol,
571 int* sep,
572 int* pitch)
573 {
574 fixed_t approx_dist;
575 fixed_t adx;
576 fixed_t ady;
577 angle_t angle;
578
579 adx = abs(listener->x - source->x);
580 ady = abs(listener->y - source->y);
581
582 approx_dist = adx + ady - ((adx < ady ? adx : ady) >> 1);
583
584 if (gamemap != 8 && approx_dist > (1200 * 0x10000)) {
585 return 0;
586 }
587
588 angle = R_PointToAngle2(listener->x, listener->y, source->x, source->y);
589
590 if (angle > listener->angle)
591 angle = angle - listener->angle;
592 else
593 angle = angle + (0xffffffff - listener->angle);
594
595 angle >>= 19;
596
597 *sep = 128 - (FixedMul((96 * 0x10000), finesine[angle]) >> 16);
598
599 if (approx_dist < (160 * 0x10000)) {
600 *vol = snd_SfxVolume;
601 } else if (gamemap == 8) {
602 if (approx_dist > (1200 * 0x10000))
603 approx_dist = (1200 * 0x10000);
604
605 *vol =
606 15 + ((snd_SfxVolume - 15) * (((1200 * 0x10000) - approx_dist) >> 16)) /
607 (((1200 * 0x10000) - (160 * 0x10000)) >> 16);
608 } else {
609
610 *vol = (snd_SfxVolume * (((1200 * 0x10000) - approx_dist) >> 16)) /
611 (((1200 * 0x10000) - (160 * 0x10000)) >> 16);
612 }
613
614 return (*vol > 0);
615 }
616 int
617 S_getChannel(void* origin, sfxinfo_t* sfxinfo)
618 {
619
620 int cnum;
621
622 channel_t* c;
623
624 for (cnum = 0; cnum < numChannels; cnum++) {
625 if (!channels[cnum].sfxinfo)
626 break;
627 else if (origin && channels[cnum].origin == origin) {
628 S_StopChannel(cnum);
629 break;
630 }
631 }
632
633 if (cnum == numChannels) {
634
635 for (cnum = 0; cnum < numChannels; cnum++)
636 if (channels[cnum].sfxinfo->priority >= sfxinfo->priority) /* { dg-warning "dereference of NULL" } */
637 break;
638
639 if (cnum == numChannels) {
640
641 return -1;
642 } else {
643
644 S_StopChannel(cnum);
645 }
646 }
647
648 c = &channels[cnum];
649
650 c->sfxinfo = sfxinfo;
651 c->origin = origin;
652
653 return cnum;
654 }