@@ -97,6 +97,9 @@ typedef std::pair<Declarator *, TemplateArgs *> SimpleTemplateIDType;
9797
9898using namespace ispc ;
9999
100+ // This macro is defined to be used in printer directives later.
101+ #define SAFE_ACCESS (ptr, expr ) ((ptr) ? (ptr)->expr : " nullptr" )
102+
100103#define UNIMPLEMENTED \
101104 Error (yylloc, " Unimplemented parser functionality %s:%d" , \
102105 __FILE__, __LINE__);
@@ -315,85 +318,89 @@ struct ForeachDimension {
315318// e.g., tests/lit-tests/2599.ispc
316319
317320// Print semantic values for debugging (under --yydebug)
318- %printer { fprintf(yyo, " %s" , $$-> c_str ()); } <stringVal>
321+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, c_str() )); } <stringVal>
319322%printer { fprintf(yyo, " %s" , $$); } <constCharPtr>
320323%printer { fprintf(yyo, " %" PRIu64, $$); } <intVal>
321- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <expr>
322- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <exprList>
323- %printer { fprintf(yyo, " %s" , $$ ? $$-> GetString ().c_str() : "nullptr" ); } <stmt>
324- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <declaration>
324+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <expr>
325+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <exprList>
326+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str()) ); } <stmt>
327+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <declaration>
325328%printer {
326329 fprintf (yyo, " <" );
327330 for (auto &i : *$$) {
328- fprintf (yyo, " %s:" , i-> GetString ().c_str ());
331+ fprintf (yyo, " %s:" , SAFE_ACCESS (i, GetString ().c_str () ));
329332 }
330333 fprintf (yyo, " >" );
331334} <declarationList>
332- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <declarator>
335+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <declarator>
333336%printer {
334337 fprintf (yyo, " <" );
335338 for (auto &i : *$$) {
336- fprintf (yyo, " %s," , i-> GetString ().c_str ());
339+ fprintf (yyo, " %s," , SAFE_ACCESS (i, GetString ().c_str () ));
337340 }
338341 fprintf (yyo, " >" );
339342} <declarators>
340343%printer {
341344 fprintf (yyo, " <" );
342345 for (auto &i : *$$) {
343- fprintf (yyo, " %s," , i-> GetString ().c_str ());
346+ fprintf (yyo, " %s," , SAFE_ACCESS (i, GetString ().c_str () ));
344347 }
345348 fprintf (yyo, " >" );
346349} <structDeclaratorList>
347- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <structDeclaration>
350+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <structDeclaration>
348351%printer {
349352 fprintf (yyo, " <" );
350353 for (auto &i : *$$) {
351- fprintf (yyo, " %s," , i-> GetString ().c_str ());
354+ fprintf (yyo, " %s," , SAFE_ACCESS (i, GetString ().c_str () ));
352355 }
353356 fprintf (yyo, " >" );
354357} <structDeclarationList>
355358%printer {
356359 fprintf (yyo, " <" );
357360 for (auto &i : *$$) {
358- fprintf (yyo, " %s," , i-> name .c_str ());
361+ fprintf (yyo, " %s," , SAFE_ACCESS (i, name.c_str () ));
359362 }
360363 fprintf (yyo, " >" );
361364} <symbolList>
362- %printer { fprintf(yyo, " %s" , $$-> name.c_str()); } <symbol>
363- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <enumType>
364- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <type>
365+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, name.c_str() )); } <symbol>
366+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <enumType>
367+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <type>
365368%printer {
366369 fprintf (yyo, " <" );
367370 for (auto &i : *$$) {
368- fprintf (yyo, " %s," , i.first -> GetString ().c_str ());
371+ fprintf (yyo, " %s," , SAFE_ACCESS ( i.first , GetString ().c_str () ));
369372 }
370373 fprintf (yyo, " >" );
371374} <typeList>
372- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <atomicType>
375+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <atomicType>
373376%printer { fprintf(yyo, " %s" , DeclSpecs::GetTypeQualifiersString($$).c_str()); } <typeQualifier>
374- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <storageClass>
375- %printer { fprintf(yyo, " %s:" , $$-> GetString ().c_str()); } <declSpecs>
376- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <attributeList>
377- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <attr>
378- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <attrArg>
379- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <pragmaAttributes>
377+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <storageClass>
378+ %printer { fprintf(yyo, " %s:" , SAFE_ACCESS($$, GetString().c_str() )); } <declSpecs>
379+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <attributeList>
380+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <attr>
381+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <attrArg>
382+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <pragmaAttributes>
380383%printer { fprintf(yyo, " %s" , $$->sym->name.c_str()); } <foreachDimension>
381384%printer {
382385 fprintf (yyo, " <" );
383386 for (auto &i : *$$) {
384- fprintf (yyo, " %s," , i->sym ->name .c_str ());
387+ if (i && i->sym ) {
388+ fprintf (yyo, " %s," , i->sym ->name .c_str ());
389+ } else {
390+ fprintf (yyo, " nullptr" );
391+ }
385392 }
386393 fprintf (yyo, " >" );
387394} <foreachDimensionList>
388- %printer { fprintf(yyo, " %s" , $$-> first.c_str()); } <declspecPair>
395+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, first.c_str() )); } <declspecPair>
389396%printer {
390397 fprintf (yyo, " <" );
391398 for (auto &i : *$$) {
392399 fprintf (yyo, " %s," , i.first .c_str ());
393400 }
394401 fprintf (yyo, " >" );
395402} <declspecList>
396- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <templateArg>
403+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <templateArg>
397404%printer {
398405 fprintf (yyo, " <" );
399406 for (auto &i : *$$) {
@@ -402,23 +409,29 @@ struct ForeachDimension {
402409 fprintf (yyo, " >" );
403410} <templateArgs>
404411%printer {
405- fprintf (yyo, " %s: " , $$->first->name.c_str());
412+ if ($$ && $$->first) {
413+ fprintf (yyo, " %s: " , $$->first ->name .c_str ());
414+ }
406415 fprintf (yyo, " <" );
407- for (auto &i : *$$->second) {
408- fprintf (yyo, " %s," , i.GetString ().c_str ());
416+ if ($$ && $$->second) {
417+ for (auto &i : *$$->second ) {
418+ fprintf (yyo, " %s," , i.GetString ().c_str ());
419+ }
409420 }
410421 fprintf (yyo, " >" );
411422} <simpleTemplateID>
412- %printer { fprintf(yyo, " %s" , $$-> GetString ().c_str()); } <templateTypeParm>
413- %printer { fprintf(yyo, " %s" , $$-> GetName ().c_str()); } <templateParm>
423+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetString().c_str() )); } <templateTypeParm>
424+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, GetName().c_str() )); } <templateParm>
414425%printer {
415426 fprintf (yyo, " <" );
416- for (size_t i = 0 ; i < $$->GetCount (); ++i) {
417- fprintf (yyo, " %s," , (*$$)[i]->GetName ().c_str ());
427+ if ($$) {
428+ for (size_t i = 0 ; i < $$->GetCount (); ++i) {
429+ fprintf (yyo, " %s," , SAFE_ACCESS ((*$$)[i], GetName ().c_str ()));
430+ }
418431 }
419432 fprintf (yyo, " >" );
420433} <templateParmList>
421- %printer { fprintf(yyo, " %s" , $$-> name.c_str()); } <functionTemplateSym>
434+ %printer { fprintf(yyo, " %s" , SAFE_ACCESS($$, name.c_str() )); } <functionTemplateSym>
422435
423436%start translation_unit
424437%%
0 commit comments