@@ -130,6 +130,29 @@ int goto_instrument_parse_optionst::doit()
130130 return 0 ;
131131 }
132132
133+ if (cmdline.isset (" save-code-statistics" ))
134+ {
135+ // Here we only early-check for requirements and we handle the option later.
136+ const std::string out_json_file_pathname=
137+ cmdline.get_value (" save-code-statistics" );
138+ if (fileutl_is_directory (out_json_file_pathname))
139+ {
140+ error () << " The path-name '" << out_json_file_pathname
141+ << " 'passed to the option '--save-code-statistics' "
142+ " represents an existing directory."
143+ << eom;
144+ return 12 ;
145+ }
146+ if (fileutl_parse_extension_in_pathname (out_json_file_pathname)!=" .json" )
147+ {
148+ error () << " The file of the path-name '" << out_json_file_pathname
149+ << " 'passed to the option '--save-code-statistics' does "
150+ " not have '.json' extension."
151+ << eom;
152+ return 13 ;
153+ }
154+ }
155+
133156 eval_verbosity ();
134157
135158 try
@@ -747,23 +770,22 @@ int goto_instrument_parse_optionst::doit()
747770 stats.extend (goto_model);
748771 const std::string out_json_file_pathname=
749772 cmdline.get_value (" save-code-statistics" );
750- if ( fileutl_is_directory (out_json_file_pathname))
751- {
752- error () << " The path-name ' " << cmdline. args [ 1 ]
753- << " 'passed to the option '--save-code-statistics' "
754- " represents an existing directory. "
755- << eom;
756- return 12 ;
757- }
758- if (fileutl_parse_extension_in_pathname (out_json_file_pathname)!= " .json " )
773+ INVARIANT (! fileutl_is_directory (out_json_file_pathname),
774+ " The early check passed so the JSON file indeed should not be "
775+ " a directory. " );
776+ INVARIANT (
777+ fileutl_parse_extension_in_pathname (out_json_file_pathname)== " .json " ,
778+ " The early check passed so the JSON file indeed should have "
779+ " '.json' extension. " ) ;
780+ std::ofstream ofile (out_json_file_pathname);
781+ if (!ofile )
759782 {
760- error () << " The file of the path-name '" << cmdline. args [ 1 ]
761- << " 'passed to the option '--save-code-statistics' does "
762- " not have '.json' extension ."
783+ error () << " Failed to open the JSON file '" << out_json_file_pathname
784+ << " ' passed to the option '--save-code-statistics' "
785+ << " for writing ."
763786 << eom;
764787 return 13 ;
765788 }
766- std::ofstream ofile (out_json_file_pathname);
767789 ofile << to_json (stats);
768790 }
769791
0 commit comments