Skip to main content

cxx2flow_lib/
error.rs

1#![allow(unused_assignments)]
2
3use miette::{Diagnostic, NamedSource, SourceSpan};
4use petgraph::graph::NodeIndex;
5use thiserror::Error;
6
7use crate::graph::Graph;
8
9#[derive(Debug, Error, Diagnostic)]
10pub enum Error {
11    #[error("transparent")]
12    #[diagnostic(
13        code(cxx2flow::tree_sitter),
14        help("error with tree_sitter parsing library")
15    )]
16    TreeSitter(#[from] tree_sitter::LanguageError),
17
18    #[error("transparent")]
19    #[diagnostic(code(cxx2flow::io), help("error with reading/writing file"))]
20    Io(#[from] std::io::Error),
21
22    #[error("transparent")]
23    #[diagnostic(code(cxx2flow::utf8), help("error with UTF-8 decoding"))]
24    UTF8(#[from] std::str::Utf8Error),
25
26    #[error("transparent")]
27    #[diagnostic(code(cxx2flow::from_utf8), help("error with UTF-8 decoding"))]
28    FromUTF8(#[from] std::string::FromUtf8Error),
29
30    #[error("transparent")]
31    #[diagnostic(
32        code(cxx2flow::hashchain_insert_failed),
33        help("hashchain insert failed")
34    )]
35    InsertFailed(#[from] hash_chain::Error),
36
37    #[error("target function not found")]
38    #[diagnostic(
39        code(cxx2flow::target_function_not_found),
40        help("maybe you have a typo, or source code is incomplete, \nplease check your input")
41    )]
42    FunctionNotFound {
43        #[source_code]
44        src: String,
45        #[label("this is the name of your target function")]
46        range: SourceSpan,
47    },
48
49    #[error("declarator not found")]
50    #[diagnostic(
51        code(cxx2flow::declarator_not_found),
52        help("maybe source code is incomplete, \nplease check your input")
53    )]
54    DeclaratorNotFound,
55
56    #[error("child not found")]
57    #[diagnostic(
58        code(cxx2flow::child_not_found),
59        help("maybe source code is incomplete, \nplease check your input")
60    )]
61    ChildNotFound,
62
63    #[error("treesitter parse failed")]
64    #[diagnostic(code(cxx2flow::treesitter_parse_failed))]
65    TreesitterParseFailed,
66
67    #[diagnostic(
68        code(cxx2flow::garbage_token),
69        help("garbage token found in AST\nthis might be a bug, please report it to the author")
70    )]
71    #[error("garbage token {0}")]
72    GarbageToken(&'static str),
73
74    #[diagnostic(
75        code(cxx2flow::unexpected_continue),
76        help("maybe you have a continue in a wrong place(e.g. out of a loop)")
77    )]
78    #[error("unexpected continue")]
79    UnexpectedContinue {
80        #[source_code]
81        src: NamedSource<String>,
82        #[label("unexpected continue statement here")]
83        range: SourceSpan,
84    },
85
86    #[diagnostic(
87        code(cxx2flow::unexpected_break),
88        help("maybe you have a break in a wrong place(e.g. out of a loop/switch)")
89    )]
90    #[error("unexpected break")]
91    UnexpectedBreak {
92        #[source_code]
93        src: NamedSource<String>,
94        #[label("unexpected break statement here")]
95        range: SourceSpan,
96    },
97
98    #[diagnostic(
99        code(cxx2flow::unexpected_dummy_graph),
100        help(
101            "dummy node found in the flow graph\nthis might be a bug, please report it to the author"
102        )
103    )]
104    #[error("unexpected dummy graph node {:?}", petgraph::dot::Dot::new(.graph))]
105    UnexpectedDummyGraphNode { graph: Graph },
106
107    #[error("unexpected dummy ast node")]
108    #[diagnostic(
109        code(cxx2flow::unexpected_dummy_ast),
110        help("dummy node found in the ast\nthis might be a bug, please report it to the author")
111    )]
112    UnexpectedDummyAstNode {
113        #[source_code]
114        src: NamedSource<String>,
115        #[label("dummy ast node here")]
116        range: SourceSpan,
117    },
118
119    #[error("unexpected outgoing edge: {node_index:?}, neighbors: {neighbors:?}, graph: {:?}", petgraph::dot::Dot::new(.graph))]
120    #[diagnostic(
121        code(cxx2flow::unexpected_outgoing_nodes),
122        help(
123            "usually, every dummy node only has one outgoing edge, but this node has zero or more than one outgoing edges\nthis might be a bug, please report it to the author"
124        )
125    )]
126    UnexpectedOutgoingEdges {
127        node_index: NodeIndex,
128        neighbors: Vec<NodeIndex>,
129        graph: Graph,
130    },
131}
132
133pub type Result<T> = std::result::Result<T, Error>;