OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/coverage.h" | 5 #include "vm/coverage.h" |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 | 8 |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 while (tkit.CurrentTokenKind() != Token::kEOS) { | 34 while (tkit.CurrentTokenKind() != Token::kEOS) { |
35 (*map)[tkit.CurrentPosition()] = cur_line; | 35 (*map)[tkit.CurrentPosition()] = cur_line; |
36 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { | 36 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { |
37 cur_line++; | 37 cur_line++; |
38 } | 38 } |
39 tkit.Advance(); | 39 tkit.Advance(); |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 | 43 |
44 static inline void PrintJSONPreamble(JSONObject* jsobj) { | |
45 jsobj->AddProperty("type", "CodeCoverage"); | |
46 jsobj->AddProperty("id", "coverage"); | |
47 } | |
48 | |
49 | |
44 void CodeCoverage::CompileAndAdd(const Function& function, | 50 void CodeCoverage::CompileAndAdd(const Function& function, |
45 const JSONArray& hits_arr, | 51 const JSONArray& hits_arr, |
46 const GrowableArray<intptr_t>& pos_to_line) { | 52 const GrowableArray<intptr_t>& pos_to_line) { |
47 Isolate* isolate = Isolate::Current(); | 53 Isolate* isolate = Isolate::Current(); |
48 if (!function.HasCode()) { | 54 if (!function.HasCode()) { |
49 // If the function should not be compiled or if the compilation failed, | 55 // If the function should not be compiled or if the compilation failed, |
50 // then just skip this method. | 56 // then just skip this method. |
51 // TODO(iposva): Maybe we should skip synthesized methods in general too. | 57 // TODO(iposva): Maybe we should skip synthesized methods in general too. |
52 if (function.is_abstract() || function.IsRedirectingFactory()) { | 58 if (function.is_abstract() || function.IsRedirectingFactory()) { |
53 return; | 59 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 ASSERT(test_line == line); | 101 ASSERT(test_line == line); |
96 #endif | 102 #endif |
97 hits_arr.AddValue(line); | 103 hits_arr.AddValue(line); |
98 hits_arr.AddValue(ic_data->AggregateCount()); | 104 hits_arr.AddValue(ic_data->AggregateCount()); |
99 } | 105 } |
100 } | 106 } |
101 } | 107 } |
102 } | 108 } |
103 | 109 |
104 | 110 |
105 void CodeCoverage::PrintClass(const Class& cls, const JSONArray& jsarr) { | 111 void CodeCoverage::PrintClass(const Class& cls, |
112 const JSONArray& jsarr, | |
113 const Script& script_filter) { | |
106 Isolate* isolate = Isolate::Current(); | 114 Isolate* isolate = Isolate::Current(); |
107 Array& functions = Array::Handle(cls.functions()); | 115 Array& functions = Array::Handle(cls.functions()); |
108 ASSERT(!functions.IsNull()); | 116 ASSERT(!functions.IsNull()); |
109 Function& function = Function::Handle(); | 117 Function& function = Function::Handle(); |
110 Script& script = Script::Handle(); | 118 Script& script = Script::Handle(); |
111 String& saved_url = String::Handle(); | 119 String& saved_url = String::Handle(); |
112 String& url = String::Handle(); | 120 String& url = String::Handle(); |
113 GrowableArray<intptr_t> pos_to_line; | 121 GrowableArray<intptr_t> pos_to_line; |
114 int i = 0; | 122 int i = 0; |
115 while (i < functions.Length()) { | 123 while (i < functions.Length()) { |
116 HANDLESCOPE(isolate); | 124 HANDLESCOPE(isolate); |
117 function ^= functions.At(i); | 125 function ^= functions.At(i); |
126 script = function.script(); | |
127 if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { | |
128 i++; | |
129 continue; | |
130 } | |
131 saved_url = script.url(); | |
132 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | |
118 JSONObject jsobj(&jsarr); | 133 JSONObject jsobj(&jsarr); |
119 script = function.script(); | |
120 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | |
121 saved_url = script.url(); | |
122 jsobj.AddProperty("source", saved_url.ToCString()); | 134 jsobj.AddProperty("source", saved_url.ToCString()); |
123 jsobj.AddProperty("script", script); | 135 jsobj.AddProperty("script", script); |
124 JSONArray hits_arr(&jsobj, "hits"); | 136 JSONArray hits_arr(&jsobj, "hits"); |
125 | 137 |
126 // We stay within this loop while we are seeing functions from the same | 138 // We stay within this loop while we are seeing functions from the same |
127 // source URI. | 139 // source URI. |
128 while (i < functions.Length()) { | 140 while (i < functions.Length()) { |
129 function ^= functions.At(i); | 141 function ^= functions.At(i); |
130 script = function.script(); | 142 script = function.script(); |
131 url = script.url(); | 143 url = script.url(); |
(...skipping 13 matching lines...) Expand all Loading... | |
145 GrowableObjectArray& closures = | 157 GrowableObjectArray& closures = |
146 GrowableObjectArray::Handle(cls.closures()); | 158 GrowableObjectArray::Handle(cls.closures()); |
147 if (!closures.IsNull()) { | 159 if (!closures.IsNull()) { |
148 i = 0; | 160 i = 0; |
149 pos_to_line.Clear(); | 161 pos_to_line.Clear(); |
150 // We need to keep rechecking the length of the closures array, as handling | 162 // We need to keep rechecking the length of the closures array, as handling |
151 // a closure potentially adds new entries to the end. | 163 // a closure potentially adds new entries to the end. |
152 while (i < closures.Length()) { | 164 while (i < closures.Length()) { |
153 HANDLESCOPE(isolate); | 165 HANDLESCOPE(isolate); |
154 function ^= closures.At(i); | 166 function ^= closures.At(i); |
167 script = function.script(); | |
168 if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { | |
169 i++; | |
170 continue; | |
171 } | |
172 saved_url = script.url(); | |
173 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | |
155 JSONObject jsobj(&jsarr); | 174 JSONObject jsobj(&jsarr); |
156 script = function.script(); | |
157 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | |
158 saved_url = script.url(); | |
159 jsobj.AddProperty("source", saved_url.ToCString()); | 175 jsobj.AddProperty("source", saved_url.ToCString()); |
160 jsobj.AddProperty("script", script); | 176 jsobj.AddProperty("script", script); |
161 JSONArray hits_arr(&jsobj, "hits"); | 177 JSONArray hits_arr(&jsobj, "hits"); |
162 | 178 |
163 // We stay within this loop while we are seeing functions from the same | 179 // We stay within this loop while we are seeing functions from the same |
164 // source URI. | 180 // source URI. |
165 while (i < closures.Length()) { | 181 while (i < closures.Length()) { |
166 function ^= closures.At(i); | 182 function ^= closures.At(i); |
167 script = function.script(); | 183 script = function.script(); |
168 url = script.url(); | 184 url = script.url(); |
169 if (!url.Equals(saved_url)) { | 185 if (!url.Equals(saved_url)) { |
170 pos_to_line.Clear(); | 186 pos_to_line.Clear(); |
171 break; | 187 break; |
172 } | 188 } |
173 CompileAndAdd(function, hits_arr, pos_to_line); | 189 CompileAndAdd(function, hits_arr, pos_to_line); |
174 i++; | 190 i++; |
175 } | 191 } |
176 } | 192 } |
177 } | 193 } |
178 } | 194 } |
179 | 195 |
180 | 196 |
197 void CodeCoverage::PrintJSONForClass(const Class& cls, | |
198 JSONStream* stream) { | |
199 Isolate* isolate = Isolate::Current(); | |
200 JSONObject coverage(stream); | |
201 PrintJSONPreamble(&coverage); | |
202 { | |
203 JSONArray jsarr(&coverage, "coverage"); | |
204 if (cls.EnsureIsFinalized(isolate) == Error::null()) { | |
205 // Only classes that have been finalized do have a meaningful list of | |
206 // functions. | |
207 PrintClass(cls, jsarr, Script::Handle()); | |
208 } | |
209 } | |
210 } | |
211 | |
212 | |
213 void CodeCoverage::PrintJSONForLibrary(const Library& lib, | |
214 JSONStream* stream) { | |
215 Isolate* isolate = Isolate::Current(); | |
216 Class& cls = Class::Handle(); | |
217 JSONObject coverage(stream); | |
218 PrintJSONPreamble(&coverage); | |
219 { | |
220 JSONArray jsarr(&coverage, "coverage"); | |
221 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | |
222 while (it.HasNext()) { | |
223 cls = it.GetNextClass(); | |
224 if (cls.EnsureIsFinalized(isolate) == Error::null()) { | |
225 // Only classes that have been finalized do have a meaningful list of | |
226 // functions. | |
227 PrintClass(cls, jsarr, Script::Handle()); | |
228 } | |
229 } | |
230 } | |
231 } | |
232 | |
233 | |
234 void CodeCoverage::PrintJSONForScript(const String& script_name, | |
235 JSONStream* stream) { | |
236 // Unless there's a backref to the script's library we need to search through | |
237 // all libraries. | |
238 Isolate* isolate = Isolate::Current(); | |
239 const GrowableObjectArray& libs = GrowableObjectArray::Handle( | |
240 isolate, isolate->object_store()->libraries()); | |
241 Library& lib = Library::Handle(); | |
242 Class& cls = Class::Handle(); | |
243 JSONObject coverage(stream); | |
244 PrintJSONPreamble(&coverage); | |
245 { | |
246 JSONArray jsarr(&coverage, "coverage"); | |
247 Script& script = Script::Handle(); | |
248 for (int i = 0; i < libs.Length(); i++) { | |
249 lib ^= libs.At(i); | |
250 const Array& loaded_scripts = Array::Handle(lib.LoadedScripts()); | |
251 intptr_t num_scripts = loaded_scripts.Length(); | |
252 bool found = false; | |
253 for (intptr_t i = 0; i < num_scripts; i++) { | |
Cutch
2014/06/23 16:23:11
Factor this code into a utility method?
FindLibra
Michael Lippautz (Google)
2014/06/24 17:51:07
Done.
Added
static RawScript* Script::FindByUrl
| |
254 script ^= loaded_scripts.At(i); | |
255 if (script_name.CompareTo(String::Handle(script.url())) == 0) { | |
256 found = true; | |
257 break; | |
258 } | |
259 } | |
260 if (!found) { | |
261 continue; | |
262 } | |
263 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | |
Cutch
2014/06/23 16:23:11
Just call PrintJSONForLibrary instead of duplicati
Michael Lippautz (Google)
2014/06/24 17:51:07
Done.
| |
264 while (it.HasNext()) { | |
265 cls = it.GetNextClass(); | |
266 if (cls.EnsureIsFinalized(isolate) == Error::null()) { | |
267 // Only classes that have been finalized do have a meaningful list of | |
268 // functions. | |
269 PrintClass(cls, jsarr, script); | |
270 } | |
271 } | |
272 } | |
273 } | |
274 } | |
275 | |
276 | |
181 void CodeCoverage::Write(Isolate* isolate) { | 277 void CodeCoverage::Write(Isolate* isolate) { |
182 if (FLAG_coverage_dir == NULL) { | 278 if (FLAG_coverage_dir == NULL) { |
183 return; | 279 return; |
184 } | 280 } |
185 | 281 |
186 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); | 282 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); |
187 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); | 283 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); |
188 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); | 284 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); |
189 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { | 285 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { |
190 return; | 286 return; |
(...skipping 30 matching lines...) Expand all Loading... | |
221 { | 317 { |
222 JSONArray jsarr(&coverage, "coverage"); | 318 JSONArray jsarr(&coverage, "coverage"); |
223 for (int i = 0; i < libs.Length(); i++) { | 319 for (int i = 0; i < libs.Length(); i++) { |
224 lib ^= libs.At(i); | 320 lib ^= libs.At(i); |
225 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | 321 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
226 while (it.HasNext()) { | 322 while (it.HasNext()) { |
227 cls = it.GetNextClass(); | 323 cls = it.GetNextClass(); |
228 if (cls.EnsureIsFinalized(isolate) == Error::null()) { | 324 if (cls.EnsureIsFinalized(isolate) == Error::null()) { |
229 // Only classes that have been finalized do have a meaningful list of | 325 // Only classes that have been finalized do have a meaningful list of |
230 // functions. | 326 // functions. |
231 PrintClass(cls, jsarr); | 327 PrintClass(cls, jsarr, Script::Handle()); |
232 } | 328 } |
233 } | 329 } |
234 } | 330 } |
235 } | 331 } |
236 } | 332 } |
237 | 333 |
238 | 334 |
239 } // namespace dart | 335 } // namespace dart |
OLD | NEW |