Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

428 linhas
19 KiB

  1. %%%
  2. %%% General colour conventions mostly follow the same schema
  3. %%% that git uses:
  4. %%%
  5. %%% * cyan: files/lines or locations in general
  6. %%% * bold: important text i.e. the human parts of warnings
  7. %%% this allows quickly 'scanning' over a warning fading
  8. %%% out the cody bits
  9. %%% * red: things that went bad, i.e. the wrong argument in a
  10. %%% call. It allows to quickly catching where in the code
  11. %%% ane error is.
  12. %%% * green: the 'good' stuff, i.e. what was expected as an argument
  13. %%% the 'red vs green' resambles the diff view 'remove vs add'
  14. %%% * blue: argument positions.
  15. -module(rebar_dialyzer_format).
  16. -include("rebar.hrl").
  17. -export([format_warnings/1]).
  18. %% Formats a list of warnings in a nice per file way. Note that we reverse
  19. %% the list at the end to 'undo' the reversal by foldl
  20. format_warnings(Warnings) ->
  21. {_, Res} = lists:foldl(fun format_warning_/2, {undefined, []}, Warnings),
  22. lists:reverse(Res).
  23. %% If the last seen file is and the file of this warning are the same
  24. %% we skip the file header
  25. format_warning_(Warning = {_Tag, {File, Line}, Msg}, {File, Acc}) ->
  26. try
  27. String = message_to_string(Msg),
  28. {File, [lists:flatten(fmt("~!c~4w~!!: ~s", [Line, String])) | Acc]}
  29. catch
  30. Error:Reason ->
  31. ?DEBUG("Failed to pretty format warning: ~p:~p",
  32. [Error, Reason]),
  33. {File, [dialyzer:format_warning(Warning, fullpath) | Acc]}
  34. end;
  35. %% With a new file detencted we also write a file header.
  36. format_warning_(Warning = {_Tag, {File, Line}, Msg}, {_LastFile, Acc}) ->
  37. try
  38. Base = filename:basename(File),
  39. Dir = filename:dirname(File),
  40. Root = filename:rootname(Base),
  41. Ext = filename:extension(Base),
  42. Path = re:replace(Dir, "^.*/_build/", "_build/", [{return, list}]),
  43. Base1 = fmt("~!_c~s~!!~!__~s", [Root, Ext]),
  44. F = fmt("~!__~s", [filename:join(Path, Base1)]),
  45. String = message_to_string(Msg),
  46. {File, [lists:flatten(fmt("~n~s~n~!c~4w~!!: ~s", [F, Line, String])) | Acc]}
  47. catch
  48. Error:Reason ->
  49. ?DEBUG("Failed to pretty format warning: ~p:~p~n~p",
  50. [Error, Reason, erlang:get_stacktrace()]),
  51. {File, [dialyzer:format_warning(Warning, fullpath) | Acc]}
  52. end.
  53. fmt(Fmt) ->
  54. cf:format(Fmt, []).
  55. fmt(Fmt, Args) ->
  56. cf:format(Fmt, Args).
  57. %%-----------------------------------------------------------------------------
  58. %% Message classification and pretty-printing below. Messages appear in
  59. %% categories and in more or less alphabetical ordering within each category.
  60. %%-----------------------------------------------------------------------------
  61. %%----- Warnings for general discrepancies ----------------
  62. message_to_string({apply, [Args, ArgNs, FailReason,
  63. SigArgs, SigRet, Contract]}) ->
  64. fmt("~!^Fun application with arguments ~!!~s ",
  65. [bad_arg(ArgNs, Args)]) ++
  66. call_or_apply_to_string(ArgNs, FailReason, SigArgs, SigRet, Contract);
  67. message_to_string({app_call, [M, F, Args, Culprit, ExpectedType, FoundType]}) ->
  68. fmt("~!^The call~!! ~s:~s~s ~!^requires that"
  69. "~!! ~s ~!^is of type ~!g~s~!^ not ~!r~s",
  70. [M, F, Args, Culprit, ExpectedType, FoundType]);
  71. message_to_string({bin_construction, [Culprit, Size, Seg, Type]}) ->
  72. fmt("~!^Binary construction will fail since the ~!b~s~!^ field~!!"
  73. " ~s~!^ in segment~!! ~s~!^ has type~!! ~s",
  74. [Culprit, Size, Seg, Type]);
  75. message_to_string({call, [M, F, Args, ArgNs, FailReason,
  76. SigArgs, SigRet, Contract]}) ->
  77. fmt("~!^The call~!! ~w:~w~s ", [M, F, bad_arg(ArgNs, Args)]) ++
  78. call_or_apply_to_string(ArgNs, FailReason, SigArgs, SigRet, Contract);
  79. message_to_string({call_to_missing, [M, F, A]}) ->
  80. fmt("~!^Call to missing or unexported function ~!!~w:~w/~w",
  81. [M, F, A]);
  82. message_to_string({exact_eq, [Type1, Op, Type2]}) ->
  83. fmt("~!^The test ~!!~s ~s ~s~!^ can never evaluate to 'true'",
  84. [Type1, Op, Type2]);
  85. message_to_string({fun_app_args, [Args, Type]}) ->
  86. fmt("~!^Fun application with arguments ~!!~s~!^ will fail"
  87. " since the function has type ~!!~s", [Args, Type]);
  88. message_to_string({fun_app_no_fun, [Op, Type, Arity]}) ->
  89. fmt("~!^Fun application will fail since ~!!~s ~!^::~!! ~s"
  90. " is not a function of arity ~!!~w", [Op, Type, Arity]);
  91. message_to_string({guard_fail, []}) ->
  92. "~!^Clause guard cannot succeed.~!!";
  93. message_to_string({guard_fail, [Arg1, Infix, Arg2]}) ->
  94. fmt("~!^Guard test ~!!~s ~s ~s~!^ can never succeed",
  95. [Arg1, Infix, Arg2]);
  96. message_to_string({neg_guard_fail, [Arg1, Infix, Arg2]}) ->
  97. fmt("~!^Guard test not(~!!~s ~s ~s~!^) can never succeed",
  98. [Arg1, Infix, Arg2]);
  99. message_to_string({guard_fail, [Guard, Args]}) ->
  100. fmt("~!^Guard test ~!!~w~s~!^ can never succeed",
  101. [Guard, Args]);
  102. message_to_string({neg_guard_fail, [Guard, Args]}) ->
  103. fmt("~!^Guard test not(~!!~w~s~!^) can never succeed",
  104. [Guard, Args]);
  105. message_to_string({guard_fail_pat, [Pat, Type]}) ->
  106. fmt("~!^Clause guard cannot succeed. The ~!!~s~!^ was matched"
  107. " against the type ~!!~s", [Pat, Type]);
  108. message_to_string({improper_list_constr, [TlType]}) ->
  109. fmt("~!^Cons will produce an improper list"
  110. " since its ~!b2~!!nd~!^ argument is~!! ~s", [TlType]);
  111. message_to_string({no_return, [Type|Name]}) ->
  112. NameString =
  113. case Name of
  114. [] -> fmt("~!^The created fun ");
  115. [F, A] -> fmt("~!^Function ~!r~w/~w ", [F, A])
  116. end,
  117. case Type of
  118. no_match -> fmt("~s~!^has no clauses that will ever match",[NameString]);
  119. only_explicit -> fmt("~s~!^only terminates with explicit exception", [NameString]);
  120. only_normal -> fmt("~s~!^has no local return", [NameString]);
  121. both -> fmt("~s~!^has no local return", [NameString])
  122. end;
  123. message_to_string({record_constr, [RecConstr, FieldDiffs]}) ->
  124. fmt("~!^Record construction ~!!~s~!^ violates the"
  125. " declared type of field ~!!~s", [RecConstr, FieldDiffs]);
  126. message_to_string({record_constr, [Name, Field, Type]}) ->
  127. fmt("~!^Record construction violates the declared type for ~!!#~w{}~!^"
  128. " since ~!!~s~!^ cannot be of type ~!!~s",
  129. [Name, Field, Type]);
  130. message_to_string({record_matching, [String, Name]}) ->
  131. fmt("~!^The ~!!~s~!^ violates the"
  132. " declared type for ~!!#~w{}", [String, Name]);
  133. message_to_string({record_match, [Pat, Type]}) ->
  134. fmt("~!^Matching of ~!!~s~!^ tagged with a record name violates the"
  135. " declared type of ~!!~s", [Pat, Type]);
  136. message_to_string({pattern_match, [Pat, Type]}) ->
  137. fmt("~!^The ~s~!^ can never match the type ~!g~s",
  138. [bad_pat(Pat), Type]);
  139. message_to_string({pattern_match_cov, [Pat, Type]}) ->
  140. fmt("~!^The ~s~!^ can never match since previous"
  141. " clauses completely covered the type ~!g~s",
  142. [bad_pat(Pat), Type]);
  143. message_to_string({unmatched_return, [Type]}) ->
  144. fmt("~!^Expression produces a value of type ~!!~s~!^,"
  145. " but this value is unmatched", [Type]);
  146. message_to_string({unused_fun, [F, A]}) ->
  147. fmt("~!^Function ~!r~w/~w~!!~!^ will never be called", [F, A]);
  148. %%----- Warnings for specs and contracts -------------------
  149. message_to_string({contract_diff, [M, F, _A, Contract, Sig]}) ->
  150. fmt("~!^Type specification ~!!~w:~w~s~!^"
  151. " is not equal to the success typing: ~!!~w:~w~s",
  152. [M, F, Contract, M, F, Sig]);
  153. message_to_string({contract_subtype, [M, F, _A, Contract, Sig]}) ->
  154. fmt("~!^Type specification ~!!~w:~w~s~!^"
  155. " is a subtype of the success typing: ~!!~w:~w~s",
  156. [M, F, Contract, M, F, Sig]);
  157. message_to_string({contract_supertype, [M, F, _A, Contract, Sig]}) ->
  158. fmt("~!^Type specification ~!!~w:~w~s~!^"
  159. " is a supertype of the success typing: ~!!~w:~w~s",
  160. [M, F, Contract, M, F, Sig]);
  161. message_to_string({contract_range, [Contract, M, F, ArgStrings, Line, CRet]}) ->
  162. fmt("~!^The contract ~!!~w:~w~s~!^ cannot be right because the"
  163. " inferred return for ~!!~w~s~!^ on line ~!!~w~!^ is ~!!~s",
  164. [M, F, Contract, F, ArgStrings, Line, CRet]);
  165. message_to_string({invalid_contract, [M, F, A, Sig]}) ->
  166. fmt("~!^Invalid type specification for function~!! ~w:~w/~w."
  167. "~!^ The success typing is~!! ~s", [M, F, A, Sig]);
  168. message_to_string({extra_range, [M, F, A, ExtraRanges, SigRange]}) ->
  169. fmt("~!^The specification for ~!!~w:~w/~w~!^ states that the function"
  170. " might also return ~!!~s~!^ but the inferred return is ~!!~s",
  171. [M, F, A, ExtraRanges, SigRange]);
  172. message_to_string({overlapping_contract, [M, F, A]}) ->
  173. fmt("~!^Overloaded contract for ~!!~w:~w/~w~!^ has overlapping"
  174. " domains; such contracts are currently unsupported and are simply "
  175. "ignored", [M, F, A]);
  176. message_to_string({spec_missing_fun, [M, F, A]}) ->
  177. fmt("~!^Contract for function that does not exist: ~!!~w:~w/~w",
  178. [M, F, A]);
  179. %%----- Warnings for opaque type violations -------------------
  180. message_to_string({call_with_opaque, [M, F, Args, ArgNs, ExpArgs]}) ->
  181. fmt("~!^The call ~!!~w:~w~s~!^ contains ~!!~s~!^ when ~!!~s",
  182. [M, F, bad_arg(ArgNs, Args), form_positions(ArgNs), form_expected(ExpArgs)]);
  183. message_to_string({call_without_opaque, [M, F, Args, [{N,_,_}|_] = ExpectedTriples]}) ->
  184. fmt("~!^The call ~!!~w:~w~s ~!^does not have~!! ~s",
  185. [M, F, bad_arg(N, Args), form_expected_without_opaque(ExpectedTriples)]);
  186. message_to_string({opaque_eq, [Type, _Op, OpaqueType]}) ->
  187. fmt("~!^Attempt to test for equality between a term of type ~!!~s~!^"
  188. " and a term of opaque type ~!!~s", [Type, OpaqueType]);
  189. message_to_string({opaque_guard, [Arg1, Infix, Arg2, ArgNs]}) ->
  190. fmt("~!^Guard test ~!!~s ~s ~s~!^ contains ~!!~s",
  191. [Arg1, Infix, Arg2, form_positions(ArgNs)]);
  192. message_to_string({opaque_guard, [Guard, Args]}) ->
  193. fmt("~!^Guard test ~!!~w~s~!^ breaks the opaqueness of its"
  194. " argument", [Guard, Args]);
  195. message_to_string({opaque_match, [Pat, OpaqueType, OpaqueTerm]}) ->
  196. Term = if OpaqueType =:= OpaqueTerm -> "the term";
  197. true -> OpaqueTerm
  198. end,
  199. fmt("~!^The attempt to match a term of type ~!!~s~!^ against the"
  200. "~!! ~s~!^ breaks the opaqueness of ~!!~s",
  201. [OpaqueType, Pat, Term]);
  202. message_to_string({opaque_neq, [Type, _Op, OpaqueType]}) ->
  203. fmt("~!^Attempt to test for inequality between a term of type ~!!~s"
  204. "~!^ and a term of opaque type ~!!~s", [Type, OpaqueType]);
  205. message_to_string({opaque_type_test, [Fun, Args, Arg, ArgType]}) ->
  206. fmt("~!^The type test ~!!~s~s~!^ breaks the opaqueness of the term "
  207. "~!!~s~s", [Fun, Args, Arg, ArgType]);
  208. message_to_string({opaque_size, [SizeType, Size]}) ->
  209. fmt("~!^The size ~!!~s~!^ breaks the opaqueness of ~!!~s",
  210. [SizeType, Size]);
  211. message_to_string({opaque_call, [M, F, Args, Culprit, OpaqueType]}) ->
  212. fmt("~!^The call ~!!~s:~s~s~!^ breaks the opaqueness of the term~!!"
  213. " ~s :: ~s", [M, F, Args, Culprit, OpaqueType]);
  214. %%----- Warnings for concurrency errors --------------------
  215. message_to_string({race_condition, [M, F, Args, Reason]}) ->
  216. fmt("~!^The call ~!!~w:~w~s ~s", [M, F, Args, Reason]);
  217. %%----- Warnings for behaviour errors --------------------
  218. message_to_string({callback_type_mismatch, [B, F, A, ST, CT]}) ->
  219. fmt("~!^The inferred return type of~!! ~w/~w (~s) ~!^"
  220. "has nothing in common with~!! ~s, ~!^which is the expected"
  221. " return type for the callback of~!! ~w ~!^behaviour",
  222. [F, A, ST, CT, B]);
  223. message_to_string({callback_arg_type_mismatch, [B, F, A, N, ST, CT]}) ->
  224. fmt("~!^The inferred type for the~!! ~s ~!^argument of~!!"
  225. " ~w/~w (~s) ~!^is not a supertype of~!! ~s~!^, which is"
  226. "expected type for this argument in the callback of the~!! ~w "
  227. "~!^behaviour",
  228. [ordinal(N), F, A, ST, CT, B]);
  229. message_to_string({callback_spec_type_mismatch, [B, F, A, ST, CT]}) ->
  230. fmt("~!^The return type ~!!~s~!^ in the specification of ~!!"
  231. "~w/~w~!^ is not a subtype of ~!!~s~!^, which is the expected"
  232. " return type for the callback of ~!!~w~!^ behaviour",
  233. [ST, F, A, CT, B]);
  234. message_to_string({callback_spec_arg_type_mismatch, [B, F, A, N, ST, CT]}) ->
  235. fmt("~!^The specified type for the ~!!~s~!^ argument of ~!!"
  236. "~w/~w (~s)~!^ is not a supertype of ~!!~s~!^, which is"
  237. " expected type for this argument in the callback of the ~!!~w"
  238. "~!^ behaviour", [ordinal(N), F, A, ST, CT, B]);
  239. message_to_string({callback_missing, [B, F, A]}) ->
  240. fmt("~!^Undefined callback function ~!!~w/~w~!^ (behaviour ~!!"
  241. "'~w'~!^)",[F, A, B]);
  242. message_to_string({callback_info_missing, [B]}) ->
  243. fmt("~!^Callback info about the ~!r~w~!!~!^"
  244. " behaviour is not available", [B]);
  245. %%----- Warnings for unknown functions, types, and behaviours -------------
  246. message_to_string({unknown_type, {M, F, A}}) ->
  247. fmt("~!^Unknown type ~!r~w:~w/~w", [M, F, A]);
  248. message_to_string({unknown_function, {M, F, A}}) ->
  249. fmt("~!^Unknown function ~!r~w:~w/~w", [M, F, A]);
  250. message_to_string({unknown_behaviour, B}) ->
  251. fmt("~!^Unknown behaviour ~!r~w", [B]).
  252. %%-----------------------------------------------------------------------------
  253. %% Auxiliary functions below
  254. %%-----------------------------------------------------------------------------
  255. call_or_apply_to_string(ArgNs, FailReason, SigArgs, SigRet,
  256. {IsOverloaded, Contract}) ->
  257. PositionString = form_position_string(ArgNs),
  258. case FailReason of
  259. only_sig ->
  260. case ArgNs =:= [] of
  261. true ->
  262. %% We do not know which argument(s) caused the failure
  263. fmt("~!^will never return since the success typing arguments"
  264. " are ~!!~s", [SigArgs]);
  265. false ->
  266. fmt("~!^will never return since it differs in the~!!"
  267. " ~s ~!^argument from the success typing"
  268. " arguments:~!! ~s",
  269. [PositionString, good_arg(ArgNs, SigArgs)])
  270. end;
  271. only_contract ->
  272. case (ArgNs =:= []) orelse IsOverloaded of
  273. true ->
  274. %% We do not know which arguments caused the failure
  275. fmt("~!^breaks the contract~!! ~s", [good_arg(ArgNs, Contract)]);
  276. false ->
  277. fmt("~!^breaks the contract~!! ~s ~!^in the~!!"
  278. " ~s ~!^argument",
  279. [good_arg(ArgNs, Contract), PositionString])
  280. end;
  281. both ->
  282. fmt("~!^will never return since the success typing is "
  283. "~!!~s ~!^->~!! ~s ~!^and the contract is ~!!~s",
  284. [good_arg(ArgNs, SigArgs), SigRet,
  285. good_arg(ArgNs, Contract)])
  286. end.
  287. form_positions(ArgNs) ->
  288. ArgS = form_position_string(ArgNs),
  289. case ArgNs of
  290. [_] -> fmt("~!^an opaque term as ~!!~s~!^ argument", [ArgS]);
  291. [_,_|_] -> fmt("~!^opaque terms as ~!!~s~!^ arguments", [ArgS])
  292. end.
  293. %% We know which positions N are to blame;
  294. %% the list of triples will never be empty.
  295. form_expected_without_opaque([{N, T, TStr}]) ->
  296. FStr = case erl_types:t_is_opaque(T) of
  297. true ->
  298. "~!^an opaque term of type~!g ~s ~!^as ";
  299. false ->
  300. "~!^a term of type ~!g~s ~!^(with opaque subterms) as "
  301. end ++ form_position_string([N]) ++ "~!^ argument",
  302. fmt(FStr, [TStr]);
  303. form_expected_without_opaque(ExpectedTriples) -> %% TODO: can do much better here
  304. {ArgNs, _Ts, _TStrs} = lists:unzip3(ExpectedTriples),
  305. "opaque terms as " ++ form_position_string(ArgNs) ++ " arguments".
  306. form_expected(ExpectedArgs) ->
  307. case ExpectedArgs of
  308. [T] ->
  309. TS = erl_types:t_to_string(T),
  310. case erl_types:t_is_opaque(T) of
  311. true -> fmt("~!^an opaque term of type ~!!~s~!^ is"
  312. " expected", [TS]);
  313. false -> fmt("~!^a structured term of type ~!!~s~!^ is"
  314. " expected", [TS])
  315. end;
  316. [_,_|_] -> fmt("~!^terms of different types are expected in these"
  317. " positions", [])
  318. end.
  319. form_position_string(ArgNs) ->
  320. case ArgNs of
  321. [] -> "";
  322. [N1] -> ordinal(N1);
  323. [_,_|_] ->
  324. [Last|Prevs] = lists:reverse(ArgNs),
  325. ", " ++ Head = lists:flatten([fmt(", ~s",[ordinal(N)]) ||
  326. N <- lists:reverse(Prevs)]),
  327. Head ++ " and " ++ ordinal(Last)
  328. end.
  329. ordinal(1) -> fmt("~!B1~!!st");
  330. ordinal(2) -> fmt("~!B2~!!nd");
  331. ordinal(3) -> fmt("~!B3~!!rd");
  332. ordinal(N) when is_integer(N) -> fmt("~!B~w~!!th", [N]).
  333. %% Format a pattern ad highlight errorous part in red.
  334. bad_pat("pattern " ++ P) ->
  335. fmt("pattern ~!r~s",[P]);
  336. bad_pat("variable " ++ P) ->
  337. fmt("variable ~!r~s",[P]);
  338. bad_pat(P) ->
  339. fmt("~!r~s",[P]).
  340. bad_arg(N, Args) ->
  341. colour_arg(N, r, Args).
  342. good_arg(N, Args) ->
  343. colour_arg(N, g, Args).
  344. %% colour one or more arg of an argument list, this unparses the args
  345. %% highlights one or more of them and puts them back together.
  346. colour_arg(N, C, Args) when is_integer(N) ->
  347. colour_arg([N], C, Args);
  348. colour_arg(Ns, C, Args) ->
  349. {Args1, Rest} =seperate_args(Args),
  350. Args2 = highlight(Ns, 1, C, Args1),
  351. join_args(Args2) ++ Rest.
  352. highlight([], _N, _C, Rest) ->
  353. Rest;
  354. highlight([N | Nr], N, g, [Arg | Rest]) ->
  355. [fmt("~!g~s", [Arg]) | highlight(Nr, N+1, g, Rest)];
  356. highlight([N | Nr], N, r, [Arg | Rest]) ->
  357. [fmt("~!r~s", [Arg]) | highlight(Nr, N+1, r, Rest)];
  358. highlight(Ns, N, C, [Arg | Rest]) ->
  359. [Arg | highlight(Ns, N + 1, C, Rest)].
  360. %% Arugments to functions and constraints are passed as
  361. %% strings not as data, this function pulls them apart
  362. %% to allow interacting with them seperately and not
  363. %% as one bug chunk of data.
  364. seperate_args([$( | S]) ->
  365. seperate_args([], S, "", []).
  366. %% We strip this space since dialyzer is inconsistant in adding or not adding
  367. %% it ....
  368. seperate_args([], [$,, $\s | R], Arg, Args) ->
  369. seperate_args([], R, [], [lists:reverse(Arg) | Args]);
  370. seperate_args([], [$, | R], Arg, Args) ->
  371. seperate_args([], R, [], [lists:reverse(Arg) | Args]);
  372. seperate_args([], [$) | Rest], Arg, Args) ->
  373. {lists:reverse([lists:reverse(Arg) | Args]), Rest};
  374. seperate_args([C | D], [C | R], Arg, Args) ->
  375. seperate_args(D, R, [C | Arg], Args);
  376. %% Brackets
  377. seperate_args(D, [${ | R], Arg, Args) ->
  378. seperate_args([$}|D], R, [${ | Arg], Args);
  379. seperate_args(D, [$( | R], Arg, Args) ->
  380. seperate_args([$)|D], R, [$( | Arg], Args);
  381. seperate_args(D, [$[ | R], Arg, Args) ->
  382. seperate_args([$]|D], R, [$[ | Arg], Args);
  383. seperate_args(D, [$< | R], Arg, Args) ->
  384. seperate_args([$>|D], R, [$< | Arg], Args);
  385. %% 'strings'
  386. seperate_args(D, [$' | R], Arg, Args) ->
  387. seperate_args([$'|D], R, [$' | Arg], Args);
  388. seperate_args(D, [$" | R], Arg, Args) ->
  389. seperate_args([$"|D], R, [$" | Arg], Args);
  390. seperate_args(D, [C | R], Arg, Args) ->
  391. seperate_args(D, R, [C | Arg], Args).
  392. join_args(Args) ->
  393. [$(, string:join(Args, ", "), $)].