summaryrefslogtreecommitdiff
path: root/llvm/lib/DebugInfo/LogicalView/Core/LVLine.cpp
blob: c3810d282abc0d714ddd84cc3b0906e97c4cf07a (plain)
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
//===-- LVLine.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This implements the LVLine class.
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/LogicalView/Core/LVLine.h"
#include "llvm/DebugInfo/LogicalView/Core/LVCompare.h"
#include "llvm/DebugInfo/LogicalView/Core/LVReader.h"

using namespace llvm;
using namespace llvm::logicalview;

#define DEBUG_TYPE "Line"

namespace {
const char *const KindBasicBlock = "BasicBlock";
const char *const KindDiscriminator = "Discriminator";
const char *const KindEndSequence = "EndSequence";
const char *const KindEpilogueBegin = "EpilogueBegin";
const char *const KindLineDebug = "Line";
const char *const KindLineSource = "Code";
const char *const KindNewStatement = "NewStatement";
const char *const KindPrologueEnd = "PrologueEnd";
const char *const KindUndefined = "Undefined";
const char *const KindAlwaysStepInto = "AlwaysStepInto"; // CodeView
const char *const KindNeverStepInto = "NeverStepInto";   // CodeView
} // end anonymous namespace

//===----------------------------------------------------------------------===//
// Logical line.
//===----------------------------------------------------------------------===//
// Return a string representation for the line kind.
const char *LVLine::kind() const {
  const char *Kind = KindUndefined;
  if (getIsLineDebug())
    Kind = KindLineDebug;
  else if (getIsLineAssembler())
    Kind = KindLineSource;
  return Kind;
}

LVLineDispatch LVLine::Dispatch = {
    {LVLineKind::IsBasicBlock, &LVLine::getIsBasicBlock},
    {LVLineKind::IsDiscriminator, &LVLine::getIsDiscriminator},
    {LVLineKind::IsEndSequence, &LVLine::getIsEndSequence},
    {LVLineKind::IsLineDebug, &LVLine::getIsLineDebug},
    {LVLineKind::IsLineAssembler, &LVLine::getIsLineAssembler},
    {LVLineKind::IsNewStatement, &LVLine::getIsNewStatement},
    {LVLineKind::IsEpilogueBegin, &LVLine::getIsEpilogueBegin},
    {LVLineKind::IsPrologueEnd, &LVLine::getIsPrologueEnd},
    {LVLineKind::IsAlwaysStepInto, &LVLine::getIsAlwaysStepInto},
    {LVLineKind::IsNeverStepInto, &LVLine::getIsNeverStepInto}};

// String used as padding for printing elements with no line number.
std::string LVLine::noLineAsString(bool ShowZero) const {
  if (options().getInternalNone())
    return LVObject::noLineAsString(ShowZero);
  return (ShowZero || options().getAttributeZero()) ? ("    0   ")
                                                    : ("    -   ");
}

void LVLine::markMissingParents(const LVLines *References,
                                const LVLines *Targets) {
  if (!(References && Targets))
    return;

  LLVM_DEBUG({
    dbgs() << "\n[LVLine::markMissingParents]\n";
    for (const LVLine *Reference : *References)
      dbgs() << "References: "
             << "Kind = " << formattedKind(Reference->kind()) << ", "
             << "Line = " << Reference->getLineNumber() << "\n";
    for (const LVLine *Target : *Targets)
      dbgs() << "Targets   : "
             << "Kind = " << formattedKind(Target->kind()) << ", "
             << "Line = " << Target->getLineNumber() << "\n";
  });

  for (LVLine *Reference : *References) {
    LLVM_DEBUG({
      dbgs() << "Search Reference: Line = " << Reference->getLineNumber()
             << "\n";
    });
    if (!Reference->findIn(Targets))
      Reference->markBranchAsMissing();
  }
}

LVLine *LVLine::findIn(const LVLines *Targets) const {
  if (!Targets)
    return nullptr;

  LLVM_DEBUG({
    dbgs() << "\n[LVLine::findIn]\n"
           << "Reference: "
           << "Level = " << getLevel() << ", "
           << "Kind = " << formattedKind(kind()) << ", "
           << "Line = " << getLineNumber() << "\n";
    for (const LVLine *Target : *Targets)
      dbgs() << "Target   : "
             << "Level = " << Target->getLevel() << ", "
             << "Kind = " << formattedKind(Target->kind()) << ", "
             << "Line = " << Target->getLineNumber() << "\n";
  });

  for (LVLine *Line : *Targets)
    if (equals(Line))
      return Line;

  return nullptr;
}

bool LVLine::equals(const LVLine *Line) const {
  return LVElement::equals(Line);
}

bool LVLine::equals(const LVLines *References, const LVLines *Targets) {
  if (!References && !Targets)
    return true;
  if (References && Targets && References->size() == Targets->size()) {
    for (const LVLine *Reference : *References)
      if (!Reference->findIn(Targets))
        return false;
    return true;
  }
  return false;
}

void LVLine::report(LVComparePass Pass) {
  getComparator().printItem(this, Pass);
}

void LVLine::print(raw_ostream &OS, bool Full) const {
  if (getReader().doPrintLine(this)) {
    getReaderCompileUnit()->incrementPrintedLines();
    LVElement::print(OS, Full);
    printExtra(OS, Full);
  }
}

//===----------------------------------------------------------------------===//
// DWARF line record.
//===----------------------------------------------------------------------===//
std::string LVLineDebug::statesInfo(bool Formatted) const {
  // Returns the DWARF extra qualifiers.
  std::string String;
  raw_string_ostream Stream(String);

  std::string Separator = Formatted ? " " : "";
  if (getIsNewStatement()) {
    Stream << Separator << "{" << KindNewStatement << "}";
    Separator = " ";
  }
  if (getIsDiscriminator()) {
    Stream << Separator << "{" << KindDiscriminator << "}";
    Separator = " ";
  }
  if (getIsBasicBlock()) {
    Stream << Separator << "{" << KindBasicBlock << "}";
    Separator = " ";
  }
  if (getIsEndSequence()) {
    Stream << Separator << "{" << KindEndSequence << "}";
    Separator = " ";
  }
  if (getIsEpilogueBegin()) {
    Stream << Separator << "{" << KindEpilogueBegin << "}";
    Separator = " ";
  }
  if (getIsPrologueEnd()) {
    Stream << Separator << "{" << KindPrologueEnd << "}";
    Separator = " ";
  }
  if (getIsAlwaysStepInto()) {
    Stream << Separator << "{" << KindAlwaysStepInto << "}";
    Separator = " ";
  }
  if (getIsNeverStepInto()) {
    Stream << Separator << "{" << KindNeverStepInto << "}";
    Separator = " ";
  }

  return String;
}

bool LVLineDebug::equals(const LVLine *Line) const {
  if (!LVLine::equals(Line))
    return false;
  return getFilenameIndex() == Line->getFilenameIndex();
}

void LVLineDebug::printExtra(raw_ostream &OS, bool Full) const {
  OS << formattedKind(kind());

  if (options().getAttributeQualifier()) {
    // The qualifier includes the states information and the source filename
    // that contains the line element.
    OS << statesInfo(/*Formatted=*/true);
    OS << " " << formattedName(getPathname());
  }
  OS << "\n";
}

//===----------------------------------------------------------------------===//
// Assembler line extracted from the ELF .text section.
//===----------------------------------------------------------------------===//
bool LVLineAssembler::equals(const LVLine *Line) const {
  return LVLine::equals(Line);
}

void LVLineAssembler::printExtra(raw_ostream &OS, bool Full) const {
  OS << formattedKind(kind());
  OS << " " << formattedName(getName());
  OS << "\n";
}