Skip to content

Commit

Permalink
Merge pull request #1274 from stan-dev/fix/printed-filename
Browse files Browse the repository at this point in the history
More consistently print requested file name in errors
  • Loading branch information
WardBrian authored Dec 12, 2022
2 parents e7d5dc3 + 694f3a2 commit 2044426
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 65 deletions.
20 changes: 11 additions & 9 deletions src/middle/Location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,23 @@ let context_to_string (context_cb : unit -> string list) (loc : t) :

let empty = {filename= ""; line_num= 0; col_num= 0; included_from= None}

(** If printed_filename is passed, it will replace the filename printed for
this Location.t and all recursively included ones.
(**
Format the location for error messaging.
If printed_filename is passed, it replaces the highest-level name and
leaves the filenames of included files intact.
*)
let rec to_string ?printed_filename ?(print_file = true) ?(print_line = true)
loc =
let open Format in
let filename =
match printed_filename with None -> loc.filename | Some f -> f in
let file = if print_file then sprintf "'%s', " filename else "" in
let line = if print_line then sprintf "line %d, " loc.line_num else "" in
let incl =
let incl, filename =
match loc.included_from with
| Some loc2 ->
sprintf ", included from\n%s" (to_string ?printed_filename loc2)
| None -> "" in
( sprintf ", included from\n%s" (to_string ?printed_filename loc2)
, loc.filename )
| None -> ("", Option.value ~default:loc.filename printed_filename) in
let file = if print_file then sprintf "'%s', " filename else "" in
let line = if print_line then sprintf "line %d, " loc.line_num else "" in
sprintf "%s%scolumn %d%s" file line loc.col_num incl

let compare loc1 loc2 =
Expand Down
18 changes: 10 additions & 8 deletions src/stanc/stanc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,17 @@ let get_ast_or_exit ?printed_filename ?(print_warnings = true)
(Warnings.pp_warnings ?printed_filename) Fmt.stderr warnings ;
match res with
| Result.Ok ast -> ast
| Result.Error err -> Errors.pp Fmt.stderr err ; exit 1
| Result.Error err ->
Errors.pp ?printed_filename Fmt.stderr err ;
exit 1

let type_ast_or_exit ast =
let type_ast_or_exit ?printed_filename ast =
match Typechecker.check_program ast with
| Result.Ok (p, warns) ->
Warnings.pp_warnings Fmt.stderr warns ;
Warnings.pp_warnings ?printed_filename Fmt.stderr warns ;
p
| Result.Error error ->
Errors.pp_semantic_error Fmt.stderr error ;
Errors.pp_semantic_error ?printed_filename Fmt.stderr error ;
exit 1

(*
Expand All @@ -280,14 +282,16 @@ let print_or_write data =
else print_endline data

let use_file filename =
let printed_filename =
match !filename_for_msg with "" -> None | s -> Some s in
let ast =
get_ast_or_exit filename
get_ast_or_exit ?printed_filename filename
~print_warnings:(not !canonicalize_settings.deprecations)
~bare_functions:!bare_functions in
(* must be before typecheck to fix up deprecated syntax which gets rejected *)
let ast = Canonicalize.repair_syntax ast !canonicalize_settings in
Debugging.ast_logger ast ;
let typed_ast = type_ast_or_exit ast in
let typed_ast = type_ast_or_exit ?printed_filename ast in
let canonical_ast =
Canonicalize.canonicalize_program typed_ast !canonicalize_settings in
if !pretty_print_program then
Expand All @@ -298,8 +302,6 @@ let use_file filename =
if !print_info_json then (
print_endline (Info.info canonical_ast) ;
exit 0 ) ;
let printed_filename =
match !filename_for_msg with "" -> None | s -> Some s in
if not !canonicalize_settings.deprecations then
Warnings.pp_warnings Fmt.stderr ?printed_filename
(Deprecation_analysis.collect_warnings typed_ast) ;
Expand Down
36 changes: 0 additions & 36 deletions test/integration/cli-args/dune
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
(rule
(targets filename_error.output)
(deps
(package stanc)
filename_error.stan)
(action
(with-stdout-to
%{targets}
(run
%{bin:run_bin_on_args}
"%{bin:stanc} --filename-in-msg=foo.stan "
filename_error.stan))))

(rule
(alias runtest)
(action
(diff filename_error.expected filename_error.output)))

(rule
(targets filename_good.output)
(deps
(package stanc)
filename_good.stan)
(action
(with-stdout-to
%{targets}
(run
%{bin:run_bin_on_args}
"%{bin:stanc} --filename-in-msg=foo.stan --print-cpp "
filename_good.stan))))

(rule
(alias runtest)
(action
(diff filename_good.expected filename_good.output)))

(rule
(targets notfound.output)
(deps
Expand Down
35 changes: 35 additions & 0 deletions test/integration/cli-args/filename-in-msg/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(rule
(targets filename_error.output)
(deps
(package stanc)
(:stanfiles filename_error.stan filename_syntax_error.stan includes.stan))
(action
(with-stdout-to
%{targets}
(run
%{bin:run_bin_on_args}
"%{bin:stanc} --filename-in-msg=foo.stan --include-paths=. "
%{stanfiles}))))

(rule
(alias runtest)
(action
(diff filename_error.expected filename_error.output)))

(rule
(targets filename_good.output)
(deps
(package stanc)
filename_good.stan)
(action
(with-stdout-to
%{targets}
(run
%{bin:run_bin_on_args}
"%{bin:stanc} --filename-in-msg=foo.stan --print-cpp "
filename_good.stan))))

(rule
(alias runtest)
(action
(diff filename_good.expected filename_good.output)))
30 changes: 30 additions & 0 deletions test/integration/cli-args/filename-in-msg/filename_error.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
$ ../../../../../install/default/bin/stanc --filename-in-msg=foo.stan --include-paths=. filename_error.stan
Semantic error in 'foo.stan', line 3, column 17 to column 18:
-------------------------------------------------
1: transformed data {
2: real p;
3: real q = p + a;
^
4: }
-------------------------------------------------

Identifier 'a' not in scope.
$ ../../../../../install/default/bin/stanc --filename-in-msg=foo.stan --include-paths=. filename_syntax_error.stan
Syntax error in 'foo.stan', line 2, column 4 to column 5, parsing error:
-------------------------------------------------
1: model {
2: rea x = 3;
^
-------------------------------------------------

Ill-formed statement or expression. A statement or expression could be expected here.
$ ../../../../../install/default/bin/stanc --filename-in-msg=foo.stan --include-paths=. includes.stan
Syntax error in './filename_syntax_error.stan', line 2, column 4, included from
'foo.stan', line 6, column 0, parsing error:
-------------------------------------------------
1: model {
2: rea x = 3;
^
-------------------------------------------------

Ill-formed statement or expression. A statement or expression could be expected here.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$ ../../../../install/default/bin/stanc --filename-in-msg=foo.stan --print-cpp filename_good.stan
$ ../../../../../install/default/bin/stanc --filename-in-msg=foo.stan --print-cpp filename_good.stan

// Code generated by %%NAME%% %%VERSION%%
#include <stan/model/model_header.hpp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model {
rea x = 3;
6 changes: 6 additions & 0 deletions test/integration/cli-args/filename-in-msg/includes.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

data {

}

#include "filename_syntax_error.stan"
11 changes: 0 additions & 11 deletions test/integration/cli-args/filename_error.expected

This file was deleted.

0 comments on commit 2044426

Please sign in to comment.