ginko 0.0.8

A device-tree source parser and analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
use crate::dts::analysis::{Analysis, AnalysisContext};
use crate::dts::ast::{DtsFile, Include, Reference};
use crate::dts::data::HasSource;
use crate::dts::error_codes::SeverityMap;
use crate::dts::reader::ByteReader;
use crate::dts::tokens::Lexer;
use crate::dts::visitor::ItemAtCursor;
use crate::dts::{Diagnostic, FileType, HasSpan, Parser, ParserContext, Position, Severity, Span};
use itertools::Itertools;
use std::collections::HashMap;
use std::iter::empty;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{fs, io};

#[derive(Default)]
pub struct ProjectFile {
    pub(crate) parser_diagnostics: Vec<Diagnostic>,
    pub(crate) analysis_diagnostics: Vec<Diagnostic>,
    pub(crate) file: Option<DtsFile>,
    pub(crate) context: Option<AnalysisContext>,
    pub(crate) file_type: FileType,
    pub(crate) source: String,
}

impl ProjectFile {
    pub fn parsed(
        diagnostics: Vec<Diagnostic>,
        file: DtsFile,
        file_type: FileType,
        source: String,
    ) -> ProjectFile {
        ProjectFile {
            parser_diagnostics: diagnostics,
            file: Some(file),
            file_type,
            context: None,
            source,
            analysis_diagnostics: vec![],
        }
    }

    pub fn unrecoverable(err: Diagnostic, source: String, file_type: FileType) -> ProjectFile {
        ProjectFile {
            parser_diagnostics: vec![err],
            analysis_diagnostics: vec![],
            file: None,
            source,
            context: None,
            file_type,
        }
    }

    pub fn analysis_context(&self) -> Option<&AnalysisContext> {
        self.context.as_ref()
    }

    pub fn diagnostics(&self) -> impl Iterator<Item = &Diagnostic> {
        self.parser_diagnostics
            .iter()
            .chain(&self.analysis_diagnostics)
    }

    pub fn has_errors(&self, severity_map: &SeverityMap) -> bool {
        self.diagnostics()
            .any(|diagnostic| diagnostic.severity(severity_map) == Severity::Error)
    }

    pub fn source(&self) -> &String {
        &self.source
    }
}

#[derive(Default)]
pub struct Project {
    files: HashMap<PathBuf, ProjectFile>,
    pub include_paths: Vec<PathBuf>,
    pub severities: SeverityMap,
}

impl Project {
    pub fn set_include_paths(&mut self, include_paths: Vec<String>) {
        self.include_paths = include_paths
            .iter()
            .map(|path| dunce::canonicalize(path).unwrap_or_default())
            .collect::<Vec<PathBuf>>();
    }

    pub fn add_file(&mut self, file_name: String) -> Result<(), io::Error> {
        let file_name = dunce::canonicalize(file_name)?;
        let content = fs::read_to_string(file_name.clone())?;
        let file_ending = FileType::from(file_name.as_path());
        self.add_file_with_text(file_name, content, file_ending);
        Ok(())
    }

    /// Adds a file to the project with already given text.
    /// Re-evaluates the file, if the file is already present.
    /// Does not re-evaluate dependencies, if they are cached.
    ///
    /// # Parameters
    /// * file_name: The name of the file.
    /// * text: The contents of the file.
    /// * file_type: Defines how the file should be analyzed.
    ///
    /// # Panics
    /// If `file_name` does not point to a valid file.
    pub fn add_file_with_text(&mut self, file_name: PathBuf, text: String, file_type: FileType) {
        let file_name = dunce::canonicalize(file_name).expect("File must be present");
        // First step: Parse file and all dependencies.
        // Dependencies are cached.
        self.parse_file(file_name.clone(), text, file_type);

        let keys = self.compute_key_order();

        let mut analysis = Analysis::new();
        for key in &keys {
            let proj_file = self.files.get(key).unwrap();
            let result = if let Some(file) = &proj_file.file {
                analysis.analyze_file(file, proj_file.file_type, self)
            } else {
                continue;
            };
            let proj_file = self.files.get_mut(key).unwrap();
            proj_file.context = Some(result.context);
            proj_file.analysis_diagnostics = result.diagnostics;
        }
    }

    /// Computes the order in which files must be analyzed.
    /// inefficient at the moment at O(n^3)
    fn compute_key_order(&self) -> Vec<PathBuf> {
        let mut map: HashMap<_, Vec<_>> = HashMap::new();
        for (path, file) in &self.files {
            let Some(dts_file) = &file.file else { continue };
            map.entry(path.clone()).or_default();
            let includes = dts_file
                .elements
                .iter()
                .filter_map(|el| el.as_include())
                .map(|incl| incl.path())
                .collect_vec();
            for include in includes.into_iter().flatten() {
                map.entry(include).or_default().push(path.clone())
            }
        }
        // This is very inefficient. Probably there is a better way.
        let mut current_order = self.files.keys().cloned().collect_vec();
        for (key, value) in &map {
            let key_idx = current_order.iter().position(|r| r == key).unwrap();
            for v in value {
                let value_idx = current_order.iter().position(|r| r == v).unwrap();
                if key_idx > value_idx {
                    current_order.swap(key_idx, value_idx)
                }
            }
        }
        current_order
    }

    pub fn remove_file(&mut self, path: &Path) {
        if let Ok(path) = dunce::canonicalize(path) {
            self.files.remove(&path);
        }
    }

    pub fn get_diagnostics(&self, path: &Path) -> Box<dyn Iterator<Item = &Diagnostic> + '_> {
        let Some(file) = self.get_file(path) else {
            return Box::new(empty());
        };
        Box::new(file.diagnostics())
    }

    pub fn all_diagnostics(&self) -> impl Iterator<Item = &Diagnostic> {
        self.files.values().flat_map(|file| file.diagnostics())
    }

    pub fn files(&self) -> impl Iterator<Item = &Path> {
        self.files.keys().map(|key| key.as_path())
    }

    pub fn project_files(&self) -> impl Iterator<Item = &ProjectFile> {
        self.files.values()
    }

    pub fn get_analysis(&self, path: &Path) -> Option<&AnalysisContext> {
        match self.get_file(path) {
            None => None,
            Some(project) => project.context.as_ref(),
        }
    }

    #[cfg(test)]
    pub fn assert_no_diagnostics(&self) {
        let diagnostics = self
            .files
            .values()
            .flat_map(|file| file.diagnostics())
            .collect_vec();
        if diagnostics.is_empty() {
            return;
        }
        for diag in diagnostics {
            println!("{diag:?}");
        }
        panic!("Found diagnostics")
    }

    pub fn find_at_pos<'a>(&'a self, path: &Path, position: &Position) -> Option<ItemAtCursor<'a>> {
        let file = match self.get_file(path).and_then(|file| file.file.as_ref()) {
            None => return None,
            Some(file) => file,
        };
        file.item_at_cursor(position)
    }

    pub fn document_reference(&self, path: &Path, reference: &Reference) -> Option<String> {
        let referenced = self.get_analysis(path)?.get_referenced(reference)?;
        Some(format!("Node {}", referenced.name.name.clone()))
    }

    pub fn get_node_position(
        &self,
        path: &Path,
        reference: &Reference,
    ) -> Option<(Span, Arc<Path>)> {
        let referenced = self.get_analysis(path)?.get_referenced(reference)?;
        Some((referenced.name.span(), referenced.name.source()))
    }

    pub fn get_root(&self, path: &Path) -> Option<&DtsFile> {
        match self.get_file(path) {
            Some(ProjectFile {
                file: Some(file), ..
            }) => Some(file),
            _ => None,
        }
    }

    fn parse_file(&mut self, file_name: PathBuf, text: String, file_type: FileType) {
        let reader = ByteReader::from_string(text.clone());
        let lexer = Lexer::new(reader, file_name.clone().into());
        // add the file's directory to the include paths to allow local includes
        let mut include_paths = self.include_paths.clone();
        if let Some(parent) = file_name.parent() {
            include_paths.insert(0, parent.into());
        }
        let mut parser = Parser::new(lexer, ParserContext { include_paths });
        match parser.file() {
            Ok(file) => {
                // insert dummy file to be defined so that no cyclic dependency can occur.
                self.files.insert(file_name.clone(), ProjectFile::default());
                file.elements
                    .iter()
                    .filter_map(|primary| primary.as_include())
                    .for_each(|include| self.parse_included_file(&mut parser.diagnostics, include));
                self.files.insert(
                    file_name,
                    ProjectFile::parsed(parser.diagnostics, file, file_type, text),
                );
            }
            Err(err) => {
                self.files
                    .insert(file_name, ProjectFile::unrecoverable(err, text, file_type));
            }
        };
    }

    fn parse_included_file(&mut self, diagnostics: &mut Vec<Diagnostic>, include: &Include) {
        let canonicalized_path = match include.path() {
            Ok(path) => path,
            Err(err) => {
                diagnostics.push(Diagnostic::io_error(include.span(), include.source(), err));
                return;
            }
        };
        // Avoids duplicate insertion and cyclic dependencies
        if self.files.contains_key(&canonicalized_path) {
            return;
        }
        match fs::read_to_string(&canonicalized_path) {
            Ok(text) => {
                let typ = FileType::from(canonicalized_path.as_path());
                self.parse_file(canonicalized_path, text, typ);
            }
            Err(err) => {
                diagnostics.push(Diagnostic::io_error(include.span(), include.source(), err))
            }
        }
    }

    pub fn get_file(&self, path: &Path) -> Option<&ProjectFile> {
        match dunce::canonicalize(path) {
            Ok(path) => self.files.get(&path),
            Err(_) => None,
        }
    }
}

#[cfg(test)]
// For some reason, this fails under windows with error "The system cannot find the file specified. (os error 2)"
#[cfg(not(windows))]
mod tests {
    use crate::dts::error_codes::ErrorCode;
    use crate::dts::test::Code;
    use crate::dts::tokens::TokenKind;
    use crate::dts::{ast, Diagnostic, HasSpan, ItemAtCursor, Project};
    use assert_matches::assert_matches;
    use itertools::Itertools;
    use std::fs;
    use std::fs::File;
    use std::io::Write;
    use std::path::{Path, PathBuf};
    use tempfile::tempdir;

    struct TempDir {
        pub inner: tempfile::TempDir,
    }

    impl TempDir {
        pub fn new() -> TempDir {
            let dir = tempdir().expect("Cannot create temporary directory");
            TempDir { inner: dir }
        }

        pub fn add_file(
            &self,
            name: impl AsRef<Path>,
            content: impl AsRef<str>,
        ) -> (Code, PathBuf) {
            let code = Code::new(content.as_ref());
            let file_path = self.inner.path().join(name);

            fs::write(&file_path, code.code()).expect("Cannot write to file");
            (code, file_path)
        }

        pub fn new_file(&self, name: impl AsRef<Path>) -> (File, PathBuf) {
            let file_path = self.inner.path().join(name);
            (
                File::create(&file_path).expect("Cannot create temporary file"),
                file_path,
            )
        }
    }

    #[test]
    pub fn file_with_includes() {
        let mut project = Project::default();
        let temp_dir = TempDir::new();
        let (_, path1) = temp_dir.add_file("tests-include.dtsi", "");
        project
            .add_file(path1.clone().into_os_string().into_string().unwrap())
            .expect("Cannot add file");

        let (_, file2) = temp_dir.add_file(
            "tests-file.dts",
            format!(
                r#"
/dts-v1/;

/include/ "{}"
"#,
                path1.display()
            ),
        );
        project
            .add_file(file2.clone().into_os_string().into_string().unwrap())
            .expect("Cannot add file");
        assert!(project.get_file(&path1).is_some());
        assert!(project.get_file(&file2).is_some());
        assert_eq!(project.get_diagnostics(&path1).next(), None);
        assert_eq!(project.get_diagnostics(&file2).next(), None);
        assert!(project.get_root(&path1).is_some());
        assert!(project.get_analysis(&path1).is_some());
        assert!(project.get_analysis(&file2).is_some());
    }

    #[test]
    pub fn cross_file_references() {
        let mut project = Project::default();
        let temp_dir = TempDir::new();
        let (code1, file1) = temp_dir.add_file(
            "tests-include.dtsi",
            r#"
/ {
    some_node: node_a {
        // ...
    };
};
"#,
        );
        let (code2, file2) = temp_dir.add_file(
            "tests-file.dts",
            format!(
                r#"
/dts-v1/;

/include/ "{}"

&some_node {{
}};

&{{/node_a}} {{
}};
"#,
                file1.display()
            ),
        );

        project
            .add_file(file2.clone().into_os_string().into_string().unwrap())
            .expect("Unexpected IO error");

        project.assert_no_diagnostics();

        let substr = code2.s1("&some_node");
        let item = project
            .find_at_pos(&file2, &substr.span().start())
            .expect("Found no item");
        let ItemAtCursor::Reference(reference) = item else {
            panic!("Found non-node at cursor")
        };
        assert_eq!(reference, &ast::Reference::Label("some_node".to_owned()));
        match project.get_node_position(&file2, reference) {
            Some((span, path)) => {
                assert_eq!(span, code1.s1("node_a").span());
                assert_eq!(
                    path.to_path_buf(),
                    dunce::canonicalize(&file1).expect("File does not exist")
                );
            }
            None => panic!("References does not reference nodes"),
        }

        let substr = code2.s1("&{/node_a}");
        let item = project
            .find_at_pos(&file2, &substr.span().start())
            .expect("Found no item");
        let ItemAtCursor::Reference(reference) = item else {
            panic!("Found non-node at cursor")
        };
        assert_eq!(reference, &ast::Reference::Path("/node_a".into()));
        match project.get_node_position(&file2, reference) {
            Some((span, path)) => {
                assert_eq!(span, code1.s1("node_a").span());
                assert_eq!(
                    path.to_path_buf(),
                    dunce::canonicalize(&file1).expect("File does not exist")
                );
            }
            None => panic!("References does not reference nodes"),
        }
    }

    #[test]
    pub fn cyclic_import_error() {
        let temp_dir = TempDir::new();
        let (mut file1, path1) = temp_dir.new_file("test.dts");
        let (mut file2, path2) = temp_dir.new_file("tests-include.dtsi");

        write!(file1, r#"/dts-v1/; /include/ "{}""#, path2.display())
            .expect("Cannot write to file 2");
        write!(file2, r#"/include/ "{}""#, path1.display()).expect("Cannot write to file1");

        let mut project = Project::default();
        project
            .add_file(path1.into_os_string().into_string().unwrap())
            .expect("Cannot add file to project");

        let diag = project.all_diagnostics().cloned().collect_vec();
        // This is explicitly vague. There should be some error somewhere,
        // but the exact location is not perfect at the moment.
        // This is because the error only occurs in one file while it should occur in all
        // files affected by the cyclic include.
        assert_matches!(
            &diag[..],
            &[Diagnostic {
                kind: ErrorCode::CyclicDependencyError,
                ..
            }]
        );
    }

    #[test]
    pub fn file_with_multiple_includes() {
        let mut project = Project::default();
        let temp_dir = TempDir::new();
        let (_, file1) = temp_dir.add_file("test1.dtsi", "");
        project
            .add_file(file1.clone().into_os_string().into_string().unwrap())
            .expect("Cannot add file");
        let (_, file2) = temp_dir.add_file("test2.dtsi", "");
        project
            .add_file(file2.clone().into_os_string().into_string().unwrap())
            .expect("Cannot add file");
        let (_, file3) = temp_dir.add_file(
            "test.dts",
            format!(
                r#"
/dts-v1/;

/include/ "{}"
/include/ "{}"
"#,
                file1.display(),
                file2.display()
            ),
        );

        project
            .add_file(file3.into_os_string().into_string().unwrap())
            .expect("Unexpected IO error");
        project.assert_no_diagnostics();
    }

    #[test]
    pub fn file_with_nested_includes() {
        let mut project = Project::default();
        let temp_dir = TempDir::new();
        let (_, file1) = temp_dir.add_file("tests-include1.dtsi", "");
        let (_, file2) = temp_dir.add_file(
            "tests-include2.dtsi",
            format!(r#"/include/ "{}""#, file1.display()).as_str(),
        );
        let (_, file3) = temp_dir.add_file(
            "test.dts",
            format!(
                r#"
/dts-v1/;

/include/ "{}"
"#,
                file2.display()
            ),
        );

        project
            .add_file(file3.clone().into_os_string().into_string().unwrap())
            .expect("Unexpected IO error");

        project.assert_no_diagnostics();

        assert!(project.get_file(&file1).is_some());
        assert!(project.get_file(&file2).is_some());
        assert!(project.get_file(&file3).is_some());
    }

    #[test]
    pub fn error_in_included_file_add_include_before_dts() {
        let mut project = Project::default();
        let temp_dir = TempDir::new();
        let (code1, file1) = temp_dir.add_file("error.dtsi", "/ {}"); // missing semicolon
        let (code2, file2) = temp_dir.add_file(
            "test.dts",
            format!(
                r#"
/dts-v1/;

/include/ "{}"
"#,
                file1.display()
            ),
        );
        project
            .add_file(file2.clone().into_os_string().into_string().unwrap())
            .expect("Cannot add file to project");

        assert!(project.get_file(&file1).is_some());
        assert!(project.get_file(&file2).is_some());
        assert_eq!(
            project.get_diagnostics(&file1).cloned().collect_vec(),
            vec![Diagnostic::expected(
                code1.s1("}").end().as_span(),
                dunce::canonicalize(&file1)
                    .expect("Cannot canonicalize")
                    .into(),
                &[TokenKind::Semicolon]
            )]
        );
        assert_eq!(
            project.get_diagnostics(&file2).cloned().collect_vec(),
            vec![Diagnostic::new(
                code2
                    .s1(format!(r#"/include/ "{}""#, file1.display()).as_str())
                    .span(),
                dunce::canonicalize(file2)
                    .expect("Cannot canonicalize")
                    .into(),
                ErrorCode::ErrorsInInclude,
                "Included file contains errors"
            )]
        );
    }

    #[test]
    pub fn file_with_include_paths_includes() {
        let includes_dir = TempDir::new();
        let (_, file1) = includes_dir.add_file("tests-include1.dtsi", "");
        let another_includes_dir = TempDir::new();
        let (_, file2) = another_includes_dir.add_file("tests-include2.dtsi", "");

        let temp_dir = TempDir::new();
        let (_, file3) = temp_dir.add_file(
            "test.dts",
            r#"
/dts-v1/;

/include/ "tests-include1.dtsi"
/include/ "tests-include2.dtsi"
"#,
        );

        let mut project = Project::default();
        let include_paths = vec![
            includes_dir.inner.path().display().to_string(),
            another_includes_dir.inner.path().display().to_string(),
        ];

        project.set_include_paths(include_paths);

        project
            .add_file(file3.clone().into_os_string().into_string().unwrap())
            .expect("Unexpected IO error");

        project.assert_no_diagnostics();

        assert!(project.get_file(&file1).is_some());
        assert!(project.get_file(&file2).is_some());
        assert!(project.get_file(&file3).is_some());
    }
}