Skip to content

Commit f3594f8

Browse files
Merge branch 'topic/#1668' into 'master'
Recover when failing to parse preprocessing options See merge request eng/ide/ada_language_server!2025
2 parents d3261c7 + f77bb01 commit f3594f8

File tree

7 files changed

+176
-15
lines changed

7 files changed

+176
-15
lines changed

source/ada/lsp-ada_handlers-file_readers.adb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ package body LSP.Ada_Handlers.File_Readers is
166166
end if;
167167
end if;
168168

169-
-- If we have preprocessing data, use LAL's API to preoprocess the file.
169+
-- If we have preprocessing data, use LAL's API to preprocess the file.
170170
-- Otherwise, just decode the contents of the document/file.
171171

172172
if Self.Preprocessing_Data /= No_Preprocessor_Data then

source/ada/lsp-ada_handlers-project_loading.adb

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with GPR2.Reporter;
3434
pragma Warnings (Off, "unit ""GPR2.Build.Source.Sets"" is not referenced");
3535
with GPR2.Build.Source.Sets;
3636

37+
with Langkit_Support.Errors;
3738
with Libadalang.Preprocessing;
3839
with Libadalang.Project_Provider;
3940
with LSP.Ada_Contexts;
@@ -558,20 +559,42 @@ package body LSP.Ada_Handlers.Project_Loading is
558559
-- and create the file reader which will preprocess the files
559560
-- accordingly.
560561

561-
Libadalang.Preprocessing.Extract_Preprocessor_Data_From_Project
562-
(Tree => Self.Project_Tree,
563-
Project => View,
564-
Default_Config => Default_Config,
565-
File_Configs => File_Configs);
566-
567-
Libadalang.Preprocessing.Iterate
568-
(Default_Config => Default_Config,
569-
File_Configs => File_Configs,
570-
Process => Set_Line_Mode'Access);
571-
572-
Reader.Preprocessing_Data :=
573-
Libadalang.Preprocessing.Create_Preprocessor_Data
574-
(Default_Config, File_Configs);
562+
begin
563+
Libadalang.Preprocessing.Extract_Preprocessor_Data_From_Project
564+
(Tree => Self.Project_Tree,
565+
Project => View,
566+
Default_Config => Default_Config,
567+
File_Configs => File_Configs);
568+
569+
Libadalang.Preprocessing.Iterate
570+
(Default_Config => Default_Config,
571+
File_Configs => File_Configs,
572+
Process => Set_Line_Mode'Access);
573+
574+
Reader.Preprocessing_Data :=
575+
Libadalang.Preprocessing.Create_Preprocessor_Data
576+
(Default_Config, File_Configs);
577+
exception
578+
-- Fallback to a degraded mode when failing to parse preprocessing
579+
-- options.
580+
when E : Langkit_Support.Errors.Syntax_Error =>
581+
Self.Send_Messages
582+
(Show => True,
583+
Messages =>
584+
["Failed to load preprocessing options: please ensure"
585+
& " your preprocessing definitions are syntactically correct."],
586+
Severity => LSP.Enumerations.Error,
587+
File => GNATCOLL.VFS.No_File);
588+
Tracer.Trace_Exception
589+
(Error => E,
590+
Message =>
591+
"Failed to parse preprocessing options for Context Id: "
592+
& (C.Id)
593+
& ", fallback to a degraded mode without support for "
594+
& "preprocessing directives");
595+
Reader.Preprocessing_Data :=
596+
Libadalang.Preprocessing.No_Preprocessor_Data;
597+
end;
575598

576599
C.Initialize
577600
(Reader,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project Default is
2+
package Compiler is
3+
for Switches ("Ada") use
4+
("-gnatep=" & Default'Project_Dir & "prep.def");
5+
end Compiler;
6+
end Default;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
with Ada.Text_IO;
2+
procedure Main is
3+
Var : constant Integer := 10;
4+
begin
5+
# if VERSION = "1" then
6+
Ada.Text_IO.Put_Line ("Version 1");
7+
# else
8+
Ada.Text_IO.Put_Line ("Version 2");
9+
# end if;
10+
Ada.Text_IO.Put_Line ("Var = " & Var'Img);
11+
end Main;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-DVERSION=1
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
[
2+
{
3+
"comment": [
4+
"This test checks that we fallback to a degraded mode when failing to parse ",
5+
"preprocessing options, warning the user via a message when it's the case."
6+
]
7+
},
8+
{
9+
"start": {
10+
"cmd": ["${ALS}"]
11+
}
12+
},
13+
{
14+
"send": {
15+
"request": {
16+
"jsonrpc": "2.0",
17+
"id": 1,
18+
"method": "initialize",
19+
"params": {
20+
"rootUri": "$URI{.}",
21+
"initializationOptions": {
22+
"ada": {
23+
"projectFile": "$URI{default.gpr}",
24+
"showNotificationsOnErrors": true
25+
}
26+
},
27+
"capabilities": {}
28+
}
29+
},
30+
"wait": []
31+
}
32+
},
33+
{
34+
"send": {
35+
"request": {
36+
"jsonrpc": "2.0",
37+
"method": "initialized"
38+
},
39+
"wait": [
40+
{
41+
"method": "window/showMessage",
42+
"params": {
43+
"type": 1,
44+
"message": "Failed to load preprocessing options: please ensure your preprocessing definitions are syntactically correct."
45+
}
46+
}
47+
]
48+
}
49+
},
50+
{
51+
"send": {
52+
"request": {
53+
"jsonrpc": "2.0",
54+
"method": "textDocument/didOpen",
55+
"params": {
56+
"textDocument": {
57+
"uri": "$URI{main.adb}",
58+
"languageId": "Ada",
59+
"version": 0,
60+
"text": "with Ada.Text_IO;\nprocedure Main is\n Var : constant Integer := 10;\nbegin\n # if VERSION = \"1\" then\n Ada.Text_IO.Put_Line (\"Version 1\");\n # else\n Ada.Text_IO.Put_Line (\"Version 2\");\n # end if;\n Ada.Text_IO.Put_Line (\"Var = \" & Var'Img);\nend Main;\n"
61+
}
62+
}
63+
},
64+
"wait": []
65+
}
66+
},
67+
{
68+
"comment": "Check that basic navigation still works in degraded mode",
69+
"send": {
70+
"request": {
71+
"jsonrpc": "2.0",
72+
"id": 7,
73+
"method": "textDocument/definition",
74+
"params": {
75+
"textDocument": {
76+
"uri": "$URI{main.adb}"
77+
},
78+
"position": {
79+
"line": 9,
80+
"character": 36
81+
}
82+
}
83+
},
84+
"wait": [
85+
{
86+
"id": 7,
87+
"result": {
88+
"uri": "$URI{main.adb}",
89+
"range": {
90+
"start": {
91+
"line": 2,
92+
"character": 4
93+
},
94+
"end": {
95+
"line": 2,
96+
"character": 7
97+
}
98+
}
99+
}
100+
}
101+
]
102+
}
103+
},
104+
{
105+
"send": {
106+
"request": {
107+
"jsonrpc": "2.0",
108+
"id": 5,
109+
"method": "shutdown"
110+
},
111+
"wait": []
112+
}
113+
},
114+
{
115+
"stop": {
116+
"exit_code": 0
117+
}
118+
}
119+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: 'als.1668.bad_preprocessing_options'

0 commit comments

Comments
 (0)