Skip to content

Commit ced6524

Browse files
author
Devang Patel
committed
Extract source location info from DebugInfo.
Add methods to add source location info in a DIE. llvm-svn: 61761
1 parent a1f3441 commit ced6524

File tree

2 files changed

+109
-9
lines changed

2 files changed

+109
-9
lines changed

llvm/include/llvm/Analysis/DebugInfo.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ namespace llvm {
4848
/// not, the debug info is corrupt and we ignore it.
4949
DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
5050

51-
unsigned getVersion() const {
52-
return getUnsignedField(0) & VersionMask;
53-
}
54-
5551
std::string getStringField(unsigned Elt) const;
5652
unsigned getUnsignedField(unsigned Elt) const {
5753
return (unsigned)getUInt64Field(Elt);
@@ -74,6 +70,10 @@ namespace llvm {
7470

7571
GlobalVariable *getGV() const { return GV; }
7672

73+
unsigned getVersion() const {
74+
return getUnsignedField(0) & VersionMask;
75+
}
76+
7777
unsigned getTag() const {
7878
return getUnsignedField(0) & ~VersionMask;
7979
}
@@ -147,7 +147,8 @@ namespace llvm {
147147
public:
148148
explicit DIType(GlobalVariable *GV);
149149
explicit DIType() {}
150-
150+
virtual ~DIType() {}
151+
151152
DIDescriptor getContext() const { return getDescriptorField(1); }
152153
std::string getName() const { return getStringField(2); }
153154
DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
@@ -158,6 +159,16 @@ namespace llvm {
158159
// carry this is just plain insane.
159160
uint64_t getOffsetInBits() const { return getUInt64Field(7); }
160161
unsigned getFlags() const { return getUnsignedField(8); }
162+
163+
virtual std::string getFilename() const {
164+
assert (0 && "Invalid DIDescriptor");
165+
return "";
166+
}
167+
168+
virtual std::string getDirectory() const {
169+
assert (0 && "Invalid DIDescriptor");
170+
return "";
171+
}
161172
};
162173

163174
/// DIBasicType - A basic type, like 'int' or 'float'.
@@ -166,7 +177,7 @@ namespace llvm {
166177
explicit DIBasicType(GlobalVariable *GV);
167178

168179
unsigned getEncoding() const { return getUnsignedField(9); }
169-
std::string getFileName() const { return getStringField(10); }
180+
std::string getFilename() const { return getStringField(10); }
170181
std::string getDirectory() const { return getStringField(11); }
171182
};
172183

@@ -180,7 +191,7 @@ namespace llvm {
180191
explicit DIDerivedType(GlobalVariable *GV);
181192

182193
DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
183-
std::string getFileName() const { return getStringField(10); }
194+
std::string getFilename() const { return getStringField(10); }
184195
std::string getDirectory() const { return getStringField(11); }
185196

186197
/// isDerivedType - Return true if the specified tag is legal for
@@ -202,7 +213,7 @@ namespace llvm {
202213
explicit DICompositeType(GlobalVariable *GV);
203214

204215
DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
205-
std::string getFileName() const { return getStringField(11); }
216+
std::string getFilename() const { return getStringField(11); }
206217
std::string getDirectory() const { return getStringField(12); }
207218

208219
/// isCompositeType - Return true if the specified tag is legal for
@@ -220,7 +231,8 @@ namespace llvm {
220231
explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
221232
: DIDescriptor(GV, RequiredTag) {}
222233
public:
223-
234+
virtual ~DIGlobal() {}
235+
224236
DIDescriptor getContext() const { return getDescriptorField(2); }
225237
std::string getName() const { return getStringField(3); }
226238
std::string getDisplayName() const { return getStringField(4); }
@@ -234,6 +246,16 @@ namespace llvm {
234246
/// compile unit, like 'static' in C.
235247
unsigned isLocalToUnit() const { return getUnsignedField(9); }
236248
unsigned isDefinition() const { return getUnsignedField(10); }
249+
250+
virtual std::string getFilename() const {
251+
assert (0 && "Invalid DIDescriptor");
252+
return "";
253+
}
254+
255+
virtual std::string getDirectory() const {
256+
assert (0 && "Invalid DIDescriptor");
257+
return "";
258+
}
237259
};
238260

239261

llvm/lib/CodeGen/AsmPrinter/DwarfWriter.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,33 @@ class Dwarf {
11231123

11241124
};
11251125

1126+
//===----------------------------------------------------------------------===//
1127+
/// SrcFileInfo - This class is used to track source information.
1128+
///
1129+
class SrcFileInfo {
1130+
unsigned DirectoryID; // Directory ID number.
1131+
std::string Name; // File name (not including directory.)
1132+
public:
1133+
SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1134+
1135+
// Accessors
1136+
unsigned getDirectoryID() const { return DirectoryID; }
1137+
const std::string &getName() const { return Name; }
1138+
1139+
/// operator== - Used by UniqueVector to locate entry.
1140+
///
1141+
bool operator==(const SourceFileInfo &SI) const {
1142+
return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1143+
}
1144+
1145+
/// operator< - Used by UniqueVector to locate entry.
1146+
///
1147+
bool operator<(const SrcFileInfo &SI) const {
1148+
return getDirectoryID() < SI.getDirectoryID() ||
1149+
(getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1150+
}
1151+
};
1152+
11261153
//===----------------------------------------------------------------------===//
11271154
/// DwarfDebug - Emits Dwarf debug directives.
11281155
///
@@ -1136,6 +1163,7 @@ class DwarfDebug : public Dwarf {
11361163
/// CompileUnits - All the compile units involved in this build. The index
11371164
/// of each entry in this vector corresponds to the sources in MMI.
11381165
std::vector<CompileUnit *> CompileUnits;
1166+
DenseMap<GlobalVariable *, CompileUnit *> DW_CUs;
11391167

11401168
/// AbbreviationsSet - Used to uniquely define abbreviations.
11411169
///
@@ -1147,6 +1175,12 @@ class DwarfDebug : public Dwarf {
11471175

11481176
/// ValuesSet - Used to uniquely define values.
11491177
///
1178+
// Directories - Uniquing vector for directories.
1179+
UniqueVector<std::string> Directories;
1180+
1181+
// SourceFiles - Uniquing vector for source files.
1182+
UniqueVector<SrcFileInfo> SrcFiles;
1183+
11501184
FoldingSet<DIEValue> ValuesSet;
11511185

11521186
/// Values - A list of all the unique values in use.
@@ -1416,6 +1450,42 @@ class DwarfDebug : public Dwarf {
14161450
}
14171451
}
14181452

1453+
/// AddSourceLine - Add location information to specified debug information
1454+
/// entry.
1455+
void AddSourceLine(DIE *Die, DIGlobal *G) {
1456+
unsigned FileID = 0;
1457+
unsigned Line = G->getLineNumber();
1458+
if (G->getVersion() < DIDescriptor::Version7) {
1459+
// Version6 or earlier. Use compile unit info to get file id.
1460+
CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1461+
FileID = Unit->getID();
1462+
} else {
1463+
// Version7 or newer, use filename and directory info from DIGlobal
1464+
// directly.
1465+
unsigned DID = Directories.idFor(G->getDirectory());
1466+
FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1467+
}
1468+
AddUInt(Die, DW_AT_decl_file, 0, FileID);
1469+
AddUInt(Die, DW_AT_decl_line, 0, Line);
1470+
}
1471+
1472+
void AddSourceLine(DIE *Die, DIType *G) {
1473+
unsigned FileID = 0;
1474+
unsigned Line = G->getLineNumber();
1475+
if (G->getVersion() < DIDescriptor::Version7) {
1476+
// Version6 or earlier. Use compile unit info to get file id.
1477+
CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1478+
FileID = Unit->getID();
1479+
} else {
1480+
// Version7 or newer, use filename and directory info from DIGlobal
1481+
// directly.
1482+
unsigned DID = Directories.idFor(G->getDirectory());
1483+
FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1484+
}
1485+
AddUInt(Die, DW_AT_decl_file, 0, FileID);
1486+
AddUInt(Die, DW_AT_decl_line, 0, Line);
1487+
}
1488+
14191489
/// AddAddress - Add an address attribute to a die based on the location
14201490
/// provided.
14211491
void AddAddress(DIE *Die, unsigned Attribute,
@@ -2144,6 +2214,14 @@ class DwarfDebug : public Dwarf {
21442214
return Unit;
21452215
}
21462216

2217+
/// FindCompileUnit - Get the compile unit for the given descriptor.
2218+
///
2219+
CompileUnit *FindCompileUnit(DICompileUnit Unit) {
2220+
CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
2221+
assert(DW_Unit && "Missing compile unit.");
2222+
return DW_Unit;
2223+
}
2224+
21472225
/// NewGlobalVariable - Add a new global variable DIE.
21482226
///
21492227
DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {

0 commit comments

Comments
 (0)