LCOV - code coverage report
Current view: top level - src - source-position.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 61 74 82.4 %
Date: 2019-01-20 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright 2016 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/source-position.h"
       6             : #include "src/objects-inl.h"
       7             : #include "src/optimized-compilation-info.h"
       8             : 
       9             : namespace v8 {
      10             : namespace internal {
      11             : 
      12         788 : std::ostream& operator<<(std::ostream& out, const SourcePositionInfo& pos) {
      13         788 :   out << "<";
      14        2364 :   if (!pos.script.is_null() && pos.script->name()->IsString()) {
      15        3152 :     out << String::cast(pos.script->name())->ToCString(DISALLOW_NULLS).get();
      16             :   } else {
      17           0 :     out << "unknown";
      18             :   }
      19         788 :   out << ":" << pos.line + 1 << ":" << pos.column + 1 << ">";
      20         788 :   return out;
      21             : }
      22             : 
      23         788 : std::ostream& operator<<(std::ostream& out,
      24             :                          const std::vector<SourcePositionInfo>& stack) {
      25             :   bool first = true;
      26        2364 :   for (const SourcePositionInfo& pos : stack) {
      27         788 :     if (!first) out << " inlined at ";
      28         788 :     out << pos;
      29             :     first = false;
      30             :   }
      31         788 :   return out;
      32             : }
      33             : 
      34         126 : std::ostream& operator<<(std::ostream& out, const SourcePosition& pos) {
      35         126 :   if (pos.isInlined()) {
      36           0 :     out << "<inlined(" << pos.InliningId() << "):";
      37             :   } else {
      38         126 :     out << "<not inlined:";
      39             :   }
      40         126 :   out << pos.ScriptOffset() << ">";
      41         126 :   return out;
      42             : }
      43             : 
      44         788 : std::vector<SourcePositionInfo> SourcePosition::InliningStack(
      45             :     OptimizedCompilationInfo* cinfo) const {
      46         788 :   SourcePosition pos = *this;
      47             :   std::vector<SourcePositionInfo> stack;
      48         788 :   while (pos.isInlined()) {
      49           0 :     const auto& inl = cinfo->inlined_functions()[pos.InliningId()];
      50           0 :     stack.push_back(SourcePositionInfo(pos, inl.shared_info));
      51           0 :     pos = inl.position.position;
      52             :   }
      53        1576 :   stack.push_back(SourcePositionInfo(pos, cinfo->shared_info()));
      54         788 :   return stack;
      55             : }
      56             : 
      57          48 : std::vector<SourcePositionInfo> SourcePosition::InliningStack(
      58             :     Handle<Code> code) const {
      59             :   Isolate* isolate = code->GetIsolate();
      60             :   Handle<DeoptimizationData> deopt_data(
      61          96 :       DeoptimizationData::cast(code->deoptimization_data()), isolate);
      62          48 :   SourcePosition pos = *this;
      63             :   std::vector<SourcePositionInfo> stack;
      64         134 :   while (pos.isInlined()) {
      65             :     InliningPosition inl =
      66         172 :         deopt_data->InliningPositions()->get(pos.InliningId());
      67             :     Handle<SharedFunctionInfo> function(
      68         172 :         deopt_data->GetInlinedFunction(inl.inlined_function_id), isolate);
      69         172 :     stack.push_back(SourcePositionInfo(pos, function));
      70             :     pos = inl.position;
      71             :   }
      72             :   Handle<SharedFunctionInfo> function(
      73          96 :       SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo()), isolate);
      74          96 :   stack.push_back(SourcePositionInfo(pos, function));
      75          48 :   return stack;
      76             : }
      77             : 
      78         306 : void SourcePosition::Print(std::ostream& out,
      79             :                            SharedFunctionInfo function) const {
      80             :   Script::PositionInfo pos;
      81         306 :   Object source_name;
      82         612 :   if (function->script()->IsScript()) {
      83         612 :     Script script = Script::cast(function->script());
      84         306 :     source_name = script->name();
      85         306 :     script->GetPositionInfo(ScriptOffset(), &pos, Script::WITH_OFFSET);
      86             :   }
      87         306 :   out << "<";
      88         306 :   if (source_name->IsString()) {
      89             :     out << String::cast(source_name)
      90             :                ->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL)
      91           0 :                .get();
      92             :   } else {
      93         306 :     out << "unknown";
      94             :   }
      95         306 :   out << ":" << pos.line + 1 << ":" << pos.column + 1 << ">";
      96         306 : }
      97             : 
      98         557 : void SourcePosition::PrintJson(std::ostream& out) const {
      99         557 :   out << "{ \"scriptOffset\" : " << ScriptOffset() << ", "
     100         557 :       << "  \"inliningId\" : " << InliningId() << "}";
     101         557 : }
     102             : 
     103         306 : void SourcePosition::Print(std::ostream& out, Code code) const {
     104             :   DeoptimizationData deopt_data =
     105         612 :       DeoptimizationData::cast(code->deoptimization_data());
     106         306 :   if (!isInlined()) {
     107             :     SharedFunctionInfo function(
     108         612 :         SharedFunctionInfo::cast(deopt_data->SharedFunctionInfo()));
     109         306 :     Print(out, function);
     110             :   } else {
     111           0 :     InliningPosition inl = deopt_data->InliningPositions()->get(InliningId());
     112           0 :     if (inl.inlined_function_id == -1) {
     113           0 :       out << *this;
     114             :     } else {
     115             :       SharedFunctionInfo function =
     116           0 :           deopt_data->GetInlinedFunction(inl.inlined_function_id);
     117           0 :       Print(out, function);
     118             :     }
     119           0 :     out << " inlined at ";
     120           0 :     inl.position.Print(out, code);
     121             :   }
     122         306 : }
     123             : 
     124        1056 : SourcePositionInfo::SourcePositionInfo(SourcePosition pos,
     125             :                                        Handle<SharedFunctionInfo> f)
     126             :     : position(pos),
     127             :       shared(f),
     128        4224 :       script(f.is_null() || !f->script()->IsScript()
     129             :                  ? Handle<Script>::null()
     130        4224 :                  : handle(Script::cast(f->script()), f->GetIsolate())) {
     131        1056 :   if (!script.is_null()) {
     132             :     Script::PositionInfo info;
     133        1056 :     if (Script::GetPositionInfo(script, pos.ScriptOffset(), &info,
     134             :                                 Script::WITH_OFFSET)) {
     135        1056 :       line = info.line;
     136        1056 :       column = info.column;
     137             :     }
     138             :   }
     139        1056 : }
     140             : 
     141             : }  // namespace internal
     142      183867 : }  // namespace v8

Generated by: LCOV version 1.10