Skip to content

Commit

Permalink
Merge branch 'topic/gnatcheck_worker_errors' into 'master'
Browse files Browse the repository at this point in the history
Enhance GNATcheck driver error messages (emitting and parsing)

Closes #151

See merge request eng/libadalang/langkit-query-language!123
  • Loading branch information
HugoGGuerrier committed Nov 21, 2023
2 parents 516c429 + 1fe6ca3 commit b6bfc7f
Show file tree
Hide file tree
Showing 33 changed files with 90 additions and 23 deletions.
30 changes: 22 additions & 8 deletions lkql_checker/lalcheck/gnatcheck-compiler.adb
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ package body Gnatcheck.Compiler is

procedure Format_Error is
begin
Error ("Unexpected format of message:");
Error_No_Tool_Name (Msg);
Error ("Unparsable line: " & Msg);
Errors := True;
end Format_Error;

Expand All @@ -271,8 +270,9 @@ package body Gnatcheck.Compiler is
end if;

-- We assume the following format of the message:
-- filename:line:column: <message body>
--
-- filename:line:column: <message body>
-- If this format is violated we display the line as unparasable.

Idx := Index (Msg (Idx .. Msg'Last), ":");

Expand All @@ -284,11 +284,6 @@ package body Gnatcheck.Compiler is
File_Idx := Idx;
SF := File_Find (Msg (First_Idx .. Idx - 1), Use_Short_Name => True);

if not Present (SF) or else Source_Info (SF) = Ignore_Unit then
-- This source should be ignored
return;
end if;

Word_End := Index (Msg (Idx + 1 .. Msg'Last), ":");

if Word_End = 0 then
Expand Down Expand Up @@ -320,6 +315,12 @@ package body Gnatcheck.Compiler is
return;
end;

-- Test if the provided sources is present and is not ignored
if not Present (SF) or else Source_Info (SF) = Ignore_Unit then
-- This source should be ignored
return;
end if;

Idx := Word_End + 2;

-- A gnatcheck message emitted by a child process via --subprocess
Expand Down Expand Up @@ -478,6 +479,12 @@ package body Gnatcheck.Compiler is
end if;
end;
end if;
elsif Line_Len >= 20
and then Line (1 .. 20) = "WORKER_FATAL_ERROR: "
then
Error ("error raised by the worker: " & Line (21 .. Line_Len));
Errors := True;
return;
else
Analyze_Line (Line (1 .. Line_Len));
end if;
Expand Down Expand Up @@ -1405,6 +1412,13 @@ package body Gnatcheck.Compiler is
end if;
end loop;

-- Test if the worker executable exists
if Worker = null then
Error ("cannot locate the worker executable: "
& Base_Name (Worker_Command));
raise Fatal_Error;
end if;

if Prj /= "" then
Num_Args := @ + 1;
Args (Num_Args) := new String'("-P" & Prj);
Expand Down
3 changes: 0 additions & 3 deletions lkql_checker/lalcheck/gnatcheck-diagnoses.adb
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,6 @@ package body Gnatcheck.Diagnoses is
end if;

case Error_Messages_Storage.Element (Position).Diagnosis_Kind is
when Not_A_Diagnosis =>
pragma Assert (False);
null;
when Rule_Violation =>
if Error_Messages_Storage.Element (Position).Justification =
Null_Unbounded_String
Expand Down
3 changes: 1 addition & 2 deletions lkql_checker/lalcheck/gnatcheck-diagnoses.ads
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ package Gnatcheck.Diagnoses is
-----------------------

type Diagnosis_Kinds is
(Not_A_Diagnosis,
Rule_Violation,
(Rule_Violation,
-- Corresponds to all rule diagnoses, including compiler checks
Exemption_Warning,
-- Warnings generated for Annotate pragmas used to implement rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ public void initSources() {
if (sourceFile.isFile()) {
this.specifiedSourceFiles.add(sourceFile.getAbsolutePath());
} else {
System.err.println("Source file '" + file + "' not found");
this.getDiagnosticEmitter()
.emitMissingFile(null, file, !this.keepGoingOnMissingFile(), this);
}
}
}
Expand Down Expand Up @@ -567,6 +568,12 @@ public void initSources() {
Libadalang.ProjectManager.create(
projectFileName, this.getScenarioVars(), "", "");

// Test if there is any diagnostic in the project manager
if (!this.projectManager.getDiagnostics().isEmpty()) {
throw LKQLRuntimeException.fromMessage(
"Error(s) during project opening: " + this.projectManager.getDiagnostics());
}

final String subprojectName = this.env.getOptions().get(LKQLLanguage.subprojectFile);
final String[] subprojects =
subprojectName.isEmpty() ? null : new String[] {subprojectName};
Expand Down Expand Up @@ -616,7 +623,8 @@ public void initSources() {

// We should not get any scenario variable if we are being run without a project file.
if (this.getScenarioVars().length != 0) {
System.err.println("Scenario variable specifications require a project file");
throw LKQLRuntimeException.fromMessage(
"Scenario variable specifications require a project file");
}

this.adaContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static LKQLRuntimeException fromMessage(String message, Locatable locatio
*/
@CompilerDirectives.TruffleBoundary
public static LKQLRuntimeException fromMessage(String message) {
return new LKQLRuntimeException("Error: " + message);
return new LKQLRuntimeException(message);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ else if (fieldValue instanceof LKQLNode lkqlNode) {
+ childField.getName()
+ " for "
+ this.getClass().getSimpleName());
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,7 @@ protected int executeScript(Context.Builder contextBuilder) {
executable.executeVoid(true);
return 0;
} catch (Exception e) {
if (this.args.verbose) {
e.printStackTrace();
} else {
System.err.println(e.getMessage());
}
System.out.println("WORKER_FATAL_ERROR: " + e.getMessage());
return 0;
}
}
Expand Down
4 changes: 3 additions & 1 deletion testsuite/drivers/gnatcheck_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class GnatcheckDriver(BaseDriver):
- ``profile-time``: Enable the time profiling or not as a boolean
- ``pre_python``/``post_python``: Python code to be executed
before/after the test
- ``worker``: Provide a custom worker for the GNATcheck run
.. NOTE:: In practice, the above allows several different ways to express
the same test, which dis not ideal. It was necessary to transition
Expand Down Expand Up @@ -172,7 +173,8 @@ def run(self) -> None:
gnatcheck_env["LKQL_RULES_PATH"] = getattr(
self.env, "gnatcheck_rules_path", ""
)
gnatcheck_env["GNATCHECK_WORKER"] = " ".join(
custom_worker = self.test_env.get('worker', None)
gnatcheck_env["GNATCHECK_WORKER"] = custom_worker or " ".join(
self.gnatcheck_worker_exe
)

Expand Down
4 changes: 4 additions & 0 deletions testsuite/tests/gnatcheck_errors/invalid_worker/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
procedure Main is
begin
null;
end Name;
3 changes: 3 additions & 0 deletions testsuite/tests/gnatcheck_errors/invalid_worker/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gnatcheck: Unparsable line: Not a message
gnatcheck: Unparsable line: Message: Should appear
gnatcheck: Unparsable line: --files-from=/gnatcheck-files1.TMP --rules-from=/gnatcheck-rules0.TMP
7 changes: 7 additions & 0 deletions testsuite/tests/gnatcheck_errors/invalid_worker/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
driver: gnatcheck
format: brief
worker: "echo Not a message\nnot_a_file.adb:01:01: Should not appear\nMessage: Should appear\n"
input_sources:
- main.adb
rules:
- +RGOTO_Statements
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
procedure Main is
begin
Put_Line ("Main");
end Main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project Prj is
for Excluded_Source_Files use ("invalid.adb");
end Prj;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gnatcheck: error raised by the worker: Error(s) during project opening: [prj.gpr:2:36: unknown file "invalid.adb"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
driver: gnatcheck
format: brief
project: prj.gpr
rules:
- +RGOTO_Statements
3 changes: 3 additions & 0 deletions testsuite/tests/gnatcheck_errors/missing_source_dirs/prj.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project Prj is
for Source_Dirs use ("invalid_source_dirs");
end Prj;
4 changes: 4 additions & 0 deletions testsuite/tests/gnatcheck_errors/missing_source_dirs/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gnatcheck: prj.gpr:2:26: error: "/invalid_source_dirs" is not a valid directory
gnatcheck: prj.gpr: warning: there are no sources of language "Ada" in this project
try "gnatcheck --help" for more information.
>>>program returned status code 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
driver: gnatcheck
format: brief
project: prj.gpr
rules:
- +RGOTO_Statements
4 changes: 4 additions & 0 deletions testsuite/tests/gnatcheck_errors/missing_worker/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
procedure Main is
begin
null;
end Name;
2 changes: 2 additions & 0 deletions testsuite/tests/gnatcheck_errors/missing_worker/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gnatcheck: cannot locate the worker executable: i_dont_exist
>>>program returned status code 2
7 changes: 7 additions & 0 deletions testsuite/tests/gnatcheck_errors/missing_worker/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
driver: gnatcheck
format: brief
worker: i_dont_exist
input_sources:
- main.adb
rules:
- +RGOTO_Statements

0 comments on commit b6bfc7f

Please sign in to comment.