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
#![allow(renamed_and_removed_lints, unused_doc_comments)]
use raw::{Ident, Type, Version};
use std::{fmt, io};
use std::error::Error as StdError;
use std::path::PathBuf;
use std::result::Result as StdResult;
error_chain! {
foreign_links {
Io(io::Error) ;
Json(::serde_json::Error) #[cfg(feature="serde_json")] ;
}
errors {
UnknownFileType(magic: u32) {
description("unknown file type")
display("unknown file type, magic 0x{:08x} not recognized", magic)
}
VersionMismatch(expected: Version, actual: Version) {
description("version mismatch")
display("version mismatch, existing graph has \"{}\", incoming file has \"{}\"", expected, actual)
}
Eof {
description("encountered EOF record")
}
UnknownTag(tag: u32) {
description("unknown record")
display("unknown record, tag 0x{:08x} not recognized", tag)
}
UnsupportedAttr(kind: &'static str, raw_flag: u32) {
description("unsupported flags")
display("unsupported {} flags 0x{:x}", kind, raw_flag)
}
UnsupportedVersion(version: u32) {
description("unsupported gcov version")
display("unsupported gcov version 0x{:08x}", version)
}
MissingFunction(file_checksum: u32, ident: Ident) {
description("missing function")
display("function from *.gcda cannot be found in the *.gcno (checksum: {}, ident: {})", file_checksum, ident)
}
RecordWithoutFunction {
description("encountered a record without the corresponding function")
}
CountsMismatch(kind: &'static str, ty: Type, expected: usize, actual: usize) {
description("counts mismatch")
display("{0} counts mismatch on *.{3}, expecting {1} {0}, received {2} {0}", kind, expected, actual, ty)
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Location {
None,
Cursor(u64),
RecordIndex(usize),
File(PathBuf),
}
impl Location {
pub fn wrap<T, E: Into<Error>, F: FnOnce() -> StdResult<T, E>>(self, f: F) -> Result<T> {
f().map_err(|e| self.wrap_error(e))
}
pub fn wrap_error<E: Into<Error>>(self, e: E) -> Error {
let mut error = e.into();
if self != Location::None {
let cause = Box::new(AtError {
location: self,
cause: error.1.next_error,
});
error.1.next_error = Some(cause);
}
error
}
}
#[derive(Debug)]
struct AtError {
location: Location,
cause: Option<Box<StdError + Send + 'static>>,
}
impl fmt::Display for AtError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.location {
Location::Cursor(cursor) => write!(fmt, "reading at file position {0} (0x{0:x})", cursor),
Location::RecordIndex(index) => write!(fmt, "parsing record index #{}", index),
Location::File(ref path) => write!(fmt, "parsing file {}", path.display()),
Location::None => write!(fmt, "nothing"),
}
}
}
impl StdError for AtError {
fn description(&self) -> &str {
"<error context>"
}
fn cause(&self) -> Option<&StdError> {
self.cause.as_ref().map(|e| -> &StdError { &**e })
}
}
pub trait IsEof {
fn is_eof(&self) -> bool;
}
impl IsEof for ErrorKind {
fn is_eof(&self) -> bool {
match *self {
ErrorKind::Io(ref e) => e.is_eof(),
ErrorKind::Eof => true,
_ => false,
}
}
}
impl IsEof for Error {
fn is_eof(&self) -> bool {
self.kind().is_eof()
}
}
impl IsEof for io::Error {
fn is_eof(&self) -> bool {
self.kind() == io::ErrorKind::UnexpectedEof
}
}