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
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
namespace Microsoft.Boogie {
public class XmlSink {
string/*!*/ filename;
[ContractInvariantMethod]
void ObjectInvariant() {
Contract.Invariant(filename != null);
}
[Rep]
XmlWriter wr;
public bool IsOpen {
get {
return wr != null;
}
}
public XmlSink(string filename) {
Contract.Requires(filename != null);
this.filename = filename;
}
/// <summary>
/// Returns null on success, in which case the caller should eventually invoke Close.
/// Returns an error string on failure.
/// </summary>
public string Open() {
//modifies this.*;
Contract.Ensures(IsOpen);
if (wr != null) {
Close();
}
cce.BeginExpose(this);
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
wr = XmlWriter.Create(filename, settings);
wr.WriteStartDocument();
wr.WriteStartElement("boogie");
wr.WriteAttributeString("version", CommandLineOptions.VersionNumber);
wr.WriteAttributeString("commandLine", Environment.CommandLine);
}
cce.EndExpose();
return null; // success
}
public void Close() {
//modifies this.*;
if (wr != null) {
cce.BeginExpose(this);
{
wr.WriteEndDocument();
wr.Close();
wr = null;
}
cce.EndExpose();
}
}
const string DateTimeFormatString = "u";
public void WriteStartMethod(string methodName, DateTime startTime) {
Contract.Requires(methodName != null);
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("method");
wr.WriteAttributeString("name", methodName);
wr.WriteAttributeString("startTime", startTime.ToString(DateTimeFormatString));
}
cce.EndExpose();
}
public void WriteEndMethod(string outcome, DateTime endTime, TimeSpan elapsed) {
Contract.Requires(outcome != null);
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("conclusion");
wr.WriteAttributeString("endTime", endTime.ToString(DateTimeFormatString));
wr.WriteAttributeString("duration", elapsed.TotalSeconds.ToString());
wr.WriteAttributeString("outcome", outcome);
wr.WriteEndElement(); // outcome
wr.WriteEndElement(); // method
}
cce.EndExpose();
}
public void WriteError(string message, IToken errorToken, IToken relatedToken, BlockSeq trace) {
Contract.Requires(errorToken != null);
Contract.Requires(message != null);
Contract.Requires(IsOpen && (trace == null || cce.Owner.Different(this, trace)));
//modifies this.*, errorToken.*, relatedToken.*, trace.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("error");
wr.WriteAttributeString("message", message);
WriteTokenAttributes(errorToken);
if (relatedToken != null) {
wr.WriteStartElement("related");
WriteTokenAttributes(relatedToken);
wr.WriteEndElement();
}
if (trace != null) {
wr.WriteStartElement("trace");
{
foreach (object bo in trace) {
cce.LoopInvariant(wr != null);
Contract.Assume(bo is Block);
Block b = (Block)bo;
wr.WriteStartElement("traceNode");
{
WriteTokenAttributes(b.tok);
wr.WriteAttributeString("label", b.Label);
}
wr.WriteEndElement();
}
wr.WriteEndElement();
}
}
wr.WriteEndElement();
}
cce.EndExpose();
}
#if CCI
public void WriteError(string message, Cci.Node offendingNode, BlockSeq trace) {
Contract.Requires(offendingNode != null);
Contract.Requires(message != null);
Contract.Requires(IsOpen && cce.Owner.Different(this, offendingNode));
Contract.Requires(trace == null || cce.Owner.Different(this, trace));
//modifies this.*, offendingNode.*, trace.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("error");
wr.WriteAttributeString("message", message);
WriteTokenAttributes(offendingNode);
if (trace != null) {
wr.WriteStartElement("trace");
{
foreach (object bo in trace) {
cce.LoopInvariant(wr != null);
Contract.Assume(bo is Block);
Block b = (Block)bo;
wr.WriteStartElement("traceNode");
{
this.WriteTokenAttributes(b.tok);
wr.WriteAttributeString("label", b.Label);
}
wr.WriteEndElement();
}
wr.WriteEndElement();
}
}
wr.WriteEndElement();
}
cce.EndExpose();
}
#endif
[Inside]
private void WriteTokenAttributes(IToken tok) {
Contract.Requires(wr != null && cce.IsPeerConsistent(wr));
//modifies this.0, wr.*;
if (tok != null && tok.filename != null) {
wr.WriteAttributeString("file", tok.filename);
wr.WriteAttributeString("line", tok.line.ToString());
wr.WriteAttributeString("column", tok.col.ToString());
}
}
#if CCI
[Inside]
private void WriteTokenAttributes(Cci.Node node) {
Contract.Requires(node != null);
Contract.Requires(wr != null && cce.IsPeerConsistent(wr));
//modifies this.0, wr.*;
Contract.Assert(wr != null);
if (node.SourceContext.Document != null) {
wr.WriteAttributeString("file", node.SourceContext.Document.Name);
wr.WriteAttributeString("line", node.SourceContext.StartLine.ToString());
wr.WriteAttributeString("column", node.SourceContext.StartColumn.ToString());
}
}
#endif
public void WriteStartInference(string inferenceName) {
Contract.Requires(inferenceName != null);
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("inference");
wr.WriteAttributeString("name", inferenceName);
}
cce.EndExpose();
}
public void WriteEndInference() {
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteEndElement(); // inference
}
cce.EndExpose();
}
public void WriteContractParaAssignment(string varName, string val) {
Contract.Requires(varName != null);
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("assignment");
wr.WriteAttributeString("name", varName);
wr.WriteAttributeString("value", val);
wr.WriteEndElement();
}
cce.EndExpose();
}
public void WriteStartFile(string filename) {
Contract.Requires(filename != null);
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("file");
wr.WriteAttributeString("name", filename);
}
cce.EndExpose();
}
public void WriteEndFile() {
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteEndElement();
}
cce.EndExpose();
}
public void WriteFileFragment(string fragment) {
Contract.Requires(fragment != null);
Contract.Requires(IsOpen);
//modifies this.*;
Contract.Ensures(IsOpen);
Contract.Assert(wr != null);
cce.BeginExpose(this);
{
wr.WriteStartElement("fileFragment");
wr.WriteAttributeString("name", fragment);
wr.WriteEndElement();
}
cce.EndExpose();
}
}
public class XmlFileScope : IDisposable {
[Peer]
[SpecPublic]
XmlSink sink;
[Captured]
public XmlFileScope(XmlSink sink, string filename) {
Contract.Requires(filename != null);
Contract.Requires(sink == null || sink.IsOpen);
//modifies sink.*;
if (sink != null) {
sink.WriteStartFile(filename); // invoke this method while "sink" is still peer consistent
cce.Owner.AssignSame(this, sink);
this.sink = sink;
}
}
public void Dispose() {
if (sink != null) {
Contract.Assume(sink.IsOpen);
sink.WriteEndFile();
}
}
}
}
|