Chromium Code Reviews| 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 | |
| 50 void CodeCoverage::CompileAndAdd(const Function& function, | 44 void CodeCoverage::CompileAndAdd(const Function& function, |
| 51 const JSONArray& hits_arr, | 45 const JSONArray& hits_arr, |
| 52 const GrowableArray<intptr_t>& pos_to_line) { | 46 const GrowableArray<intptr_t>& pos_to_line) { |
| 53 Isolate* isolate = Isolate::Current(); | 47 Isolate* isolate = Isolate::Current(); |
| 54 if (!function.HasCode()) { | 48 if (!function.HasCode()) { |
| 55 // If the function should not be compiled or if the compilation failed, | 49 // If the function should not be compiled or if the compilation failed, |
| 56 // then just skip this method. | 50 // then just skip this method. |
| 57 // TODO(iposva): Maybe we should skip synthesized methods in general too. | 51 // TODO(iposva): Maybe we should skip synthesized methods in general too. |
| 58 if (function.is_abstract() || function.IsRedirectingFactory()) { | 52 if (function.is_abstract() || function.IsRedirectingFactory()) { |
| 59 return; | 53 return; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 } | 116 } |
| 123 } | 117 } |
| 124 // Write last hit value if needed. | 118 // Write last hit value if needed. |
| 125 if (last_line != -1) { | 119 if (last_line != -1) { |
| 126 hits_arr.AddValue(last_line); | 120 hits_arr.AddValue(last_line); |
| 127 hits_arr.AddValue(last_count); | 121 hits_arr.AddValue(last_count); |
| 128 } | 122 } |
| 129 } | 123 } |
| 130 | 124 |
| 131 | 125 |
| 132 void CodeCoverage::PrintClass(const Class& cls, | 126 void CodeCoverage::PrintClass(const Library& lib, |
| 127 const Class& cls, | |
| 133 const JSONArray& jsarr, | 128 const JSONArray& jsarr, |
| 134 const Script& script_filter) { | 129 CoverageFilter* filter) { |
| 135 Isolate* isolate = Isolate::Current(); | 130 Isolate* isolate = Isolate::Current(); |
| 136 if (cls.EnsureIsFinalized(isolate) != Error::null()) { | 131 if (cls.EnsureIsFinalized(isolate) != Error::null()) { |
| 137 // Only classes that have been finalized do have a meaningful list of | 132 // Only classes that have been finalized do have a meaningful list of |
| 138 // functions. | 133 // functions. |
| 139 return; | 134 return; |
| 140 } | 135 } |
| 141 Array& functions = Array::Handle(cls.functions()); | 136 Array& functions = Array::Handle(cls.functions()); |
| 142 ASSERT(!functions.IsNull()); | 137 ASSERT(!functions.IsNull()); |
| 143 Function& function = Function::Handle(); | 138 Function& function = Function::Handle(); |
| 144 Script& script = Script::Handle(); | 139 Script& script = Script::Handle(); |
| 145 String& saved_url = String::Handle(); | 140 String& saved_url = String::Handle(); |
| 146 String& url = String::Handle(); | 141 String& url = String::Handle(); |
| 147 GrowableArray<intptr_t> pos_to_line; | 142 GrowableArray<intptr_t> pos_to_line; |
| 148 int i = 0; | 143 int i = 0; |
| 149 while (i < functions.Length()) { | 144 while (i < functions.Length()) { |
| 150 HANDLESCOPE(isolate); | 145 HANDLESCOPE(isolate); |
| 151 function ^= functions.At(i); | 146 function ^= functions.At(i); |
| 152 script = function.script(); | 147 script = function.script(); |
| 153 if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { | 148 if (filter != NULL && !filter->GetCoverage(lib, script, cls, function)) { |
| 154 i++; | 149 i++; |
| 155 continue; | 150 continue; |
| 156 } | 151 } |
| 157 saved_url = script.url(); | 152 saved_url = script.url(); |
| 158 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | 153 ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
| 159 JSONObject jsobj(&jsarr); | 154 JSONObject jsobj(&jsarr); |
| 160 jsobj.AddProperty("source", saved_url.ToCString()); | 155 jsobj.AddProperty("source", saved_url.ToCString()); |
| 161 jsobj.AddProperty("script", script); | 156 jsobj.AddProperty("script", script); |
| 162 JSONArray hits_arr(&jsobj, "hits"); | 157 JSONArray hits_arr(&jsobj, "hits"); |
| 163 | 158 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 184 GrowableObjectArray::Handle(cls.closures()); | 179 GrowableObjectArray::Handle(cls.closures()); |
| 185 if (!closures.IsNull()) { | 180 if (!closures.IsNull()) { |
| 186 i = 0; | 181 i = 0; |
| 187 pos_to_line.Clear(); | 182 pos_to_line.Clear(); |
| 188 // We need to keep rechecking the length of the closures array, as handling | 183 // We need to keep rechecking the length of the closures array, as handling |
| 189 // a closure potentially adds new entries to the end. | 184 // a closure potentially adds new entries to the end. |
| 190 while (i < closures.Length()) { | 185 while (i < closures.Length()) { |
| 191 HANDLESCOPE(isolate); | 186 HANDLESCOPE(isolate); |
| 192 function ^= closures.At(i); | 187 function ^= closures.At(i); |
| 193 script = function.script(); | 188 script = function.script(); |
| 194 if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { | 189 if (filter != NULL && !filter->GetCoverage(lib, script, cls, function)) { |
| 195 i++; | 190 i++; |
| 196 continue; | 191 continue; |
| 197 } | 192 } |
| 198 saved_url = script.url(); | 193 saved_url = script.url(); |
| 199 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | 194 ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
| 200 JSONObject jsobj(&jsarr); | 195 JSONObject jsobj(&jsarr); |
| 201 jsobj.AddProperty("source", saved_url.ToCString()); | 196 jsobj.AddProperty("source", saved_url.ToCString()); |
| 202 jsobj.AddProperty("script", script); | 197 jsobj.AddProperty("script", script); |
| 203 JSONArray hits_arr(&jsobj, "hits"); | 198 JSONArray hits_arr(&jsobj, "hits"); |
| 204 | 199 |
| 205 // We stay within this loop while we are seeing functions from the same | 200 // We stay within this loop while we are seeing functions from the same |
| 206 // source URI. | 201 // source URI. |
| 207 while (i < closures.Length()) { | 202 while (i < closures.Length()) { |
| 208 function ^= closures.At(i); | 203 function ^= closures.At(i); |
| 209 script = function.script(); | 204 script = function.script(); |
| 210 url = script.url(); | 205 url = script.url(); |
| 211 if (!url.Equals(saved_url)) { | 206 if (!url.Equals(saved_url)) { |
| 212 pos_to_line.Clear(); | 207 pos_to_line.Clear(); |
| 213 break; | 208 break; |
| 214 } | 209 } |
| 215 CompileAndAdd(function, hits_arr, pos_to_line); | 210 CompileAndAdd(function, hits_arr, pos_to_line); |
| 216 i++; | 211 i++; |
| 217 } | 212 } |
| 218 } | 213 } |
| 219 } | 214 } |
| 220 } | 215 } |
| 221 | 216 |
| 222 | 217 |
| 223 void CodeCoverage::PrintJSONForClass(const Class& cls, | |
| 224 JSONStream* stream) { | |
| 225 JSONObject coverage(stream); | |
| 226 PrintJSONPreamble(&coverage); | |
| 227 { | |
| 228 JSONArray jsarr(&coverage, "coverage"); | |
| 229 PrintClass(cls, jsarr, Script::Handle()); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 | |
| 234 void CodeCoverage::PrintJSONForLibrary(const Library& lib, | |
| 235 const Script& script_filter, | |
| 236 JSONStream* stream) { | |
| 237 Class& cls = Class::Handle(); | |
| 238 JSONObject coverage(stream); | |
| 239 PrintJSONPreamble(&coverage); | |
| 240 { | |
| 241 JSONArray jsarr(&coverage, "coverage"); | |
| 242 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | |
| 243 while (it.HasNext()) { | |
| 244 cls = it.GetNextClass(); | |
| 245 ASSERT(!cls.IsNull()); | |
| 246 PrintClass(cls, jsarr, script_filter); | |
| 247 } | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 | |
| 252 void CodeCoverage::PrintJSONForScript(const Script& script, | |
| 253 JSONStream* stream) { | |
| 254 Library& lib = Library::Handle(); | |
| 255 lib = script.FindLibrary(); | |
| 256 ASSERT(!lib.IsNull()); | |
| 257 PrintJSONForLibrary(lib, script, stream); | |
| 258 } | |
| 259 | |
| 260 | |
| 261 void CodeCoverage::Write(Isolate* isolate) { | 218 void CodeCoverage::Write(Isolate* isolate) { |
| 262 if (FLAG_coverage_dir == NULL) { | 219 if (FLAG_coverage_dir == NULL) { |
| 263 return; | 220 return; |
| 264 } | 221 } |
| 265 | 222 |
| 266 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); | 223 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); |
| 267 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); | 224 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); |
| 268 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); | 225 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); |
| 269 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { | 226 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { |
| 270 return; | 227 return; |
| 271 } | 228 } |
| 272 | 229 |
| 273 JSONStream stream; | 230 JSONStream stream; |
| 274 PrintJSON(isolate, &stream); | 231 PrintJSON(isolate, &stream, NULL); |
| 275 | 232 |
| 276 const char* format = "%s/dart-cov-%" Pd "-%" Pd ".json"; | 233 const char* format = "%s/dart-cov-%" Pd "-%" Pd ".json"; |
| 277 intptr_t pid = OS::ProcessId(); | 234 intptr_t pid = OS::ProcessId(); |
| 278 intptr_t len = OS::SNPrint(NULL, 0, format, | 235 intptr_t len = OS::SNPrint(NULL, 0, format, |
| 279 FLAG_coverage_dir, pid, isolate->main_port()); | 236 FLAG_coverage_dir, pid, isolate->main_port()); |
| 280 char* filename = Isolate::Current()->current_zone()->Alloc<char>(len + 1); | 237 char* filename = Isolate::Current()->current_zone()->Alloc<char>(len + 1); |
| 281 OS::SNPrint(filename, len + 1, format, | 238 OS::SNPrint(filename, len + 1, format, |
| 282 FLAG_coverage_dir, pid, isolate->main_port()); | 239 FLAG_coverage_dir, pid, isolate->main_port()); |
| 283 void* file = (*file_open)(filename, true); | 240 void* file = (*file_open)(filename, true); |
| 284 if (file == NULL) { | 241 if (file == NULL) { |
| 285 OS::Print("Failed to write coverage file: %s\n", filename); | 242 OS::Print("Failed to write coverage file: %s\n", filename); |
| 286 return; | 243 return; |
| 287 } | 244 } |
| 288 (*file_write)(stream.buffer()->buf(), stream.buffer()->length(), file); | 245 (*file_write)(stream.buffer()->buf(), stream.buffer()->length(), file); |
| 289 (*file_close)(file); | 246 (*file_close)(file); |
| 290 } | 247 } |
| 291 | 248 |
| 292 | 249 |
| 293 void CodeCoverage::PrintJSON(Isolate* isolate, JSONStream* stream) { | 250 void CodeCoverage::PrintJSON(Isolate* isolate, |
| 251 JSONStream* stream, | |
| 252 CoverageFilter* filter) { | |
| 294 const GrowableObjectArray& libs = GrowableObjectArray::Handle( | 253 const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
|
Cutch
2014/06/27 15:07:18
kill all the filter != NULL checks by adding:
cla
Michael Lippautz (Google)
2014/06/27 17:12:24
Done.
| |
| 295 isolate, isolate->object_store()->libraries()); | 254 isolate, isolate->object_store()->libraries()); |
| 296 Library& lib = Library::Handle(); | 255 Library& lib = Library::Handle(); |
| 297 Class& cls = Class::Handle(); | 256 Class& cls = Class::Handle(); |
| 298 JSONObject coverage(stream); | 257 JSONObject coverage(stream); |
| 299 coverage.AddProperty("type", "CodeCoverage"); | 258 coverage.AddProperty("type", "CodeCoverage"); |
| 300 coverage.AddProperty("id", "coverage"); | 259 coverage.AddProperty("id", "coverage"); |
| 301 { | 260 { |
| 302 JSONArray jsarr(&coverage, "coverage"); | 261 JSONArray jsarr(&coverage, "coverage"); |
| 303 for (int i = 0; i < libs.Length(); i++) { | 262 for (int i = 0; i < libs.Length(); i++) { |
| 304 lib ^= libs.At(i); | 263 lib ^= libs.At(i); |
| 305 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | 264 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
| 306 while (it.HasNext()) { | 265 while (it.HasNext()) { |
| 307 cls = it.GetNextClass(); | 266 cls = it.GetNextClass(); |
| 308 ASSERT(!cls.IsNull()); | 267 ASSERT(!cls.IsNull()); |
| 309 PrintClass(cls, jsarr, Script::Handle()); | 268 PrintClass(lib, cls, jsarr, filter); |
| 310 } | 269 } |
| 311 } | 270 } |
| 312 } | 271 } |
| 313 } | 272 } |
| 314 | 273 |
| 315 | 274 |
| 316 } // namespace dart | 275 } // namespace dart |
| OLD | NEW |