Bug Summary

File:lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp
Location:line 319, column 7
Description:Value stored to 'LastSegmentStart' is never read

Annotated Source Code

1//===-- llvm/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp --*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing line tables info into COFF files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "WinCodeViewLineTables.h"
15#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCSymbol.h"
17#include "llvm/Support/COFF.h"
18
19namespace llvm {
20
21StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) {
22 assert(S)((S) ? static_cast<void> (0) : __assert_fail ("S", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 22, __PRETTY_FUNCTION__))
;
23 assert((isa<DICompileUnit>(S) || isa<DIFile>(S) || isa<DISubprogram>(S) ||(((isa<DICompileUnit>(S) || isa<DIFile>(S) || isa
<DISubprogram>(S) || isa<DILexicalBlockBase>(S)) &&
"Unexpected scope info") ? static_cast<void> (0) : __assert_fail
("(isa<DICompileUnit>(S) || isa<DIFile>(S) || isa<DISubprogram>(S) || isa<DILexicalBlockBase>(S)) && \"Unexpected scope info\""
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 25, __PRETTY_FUNCTION__))
24 isa<DILexicalBlockBase>(S)) &&(((isa<DICompileUnit>(S) || isa<DIFile>(S) || isa
<DISubprogram>(S) || isa<DILexicalBlockBase>(S)) &&
"Unexpected scope info") ? static_cast<void> (0) : __assert_fail
("(isa<DICompileUnit>(S) || isa<DIFile>(S) || isa<DISubprogram>(S) || isa<DILexicalBlockBase>(S)) && \"Unexpected scope info\""
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 25, __PRETTY_FUNCTION__))
25 "Unexpected scope info")(((isa<DICompileUnit>(S) || isa<DIFile>(S) || isa
<DISubprogram>(S) || isa<DILexicalBlockBase>(S)) &&
"Unexpected scope info") ? static_cast<void> (0) : __assert_fail
("(isa<DICompileUnit>(S) || isa<DIFile>(S) || isa<DISubprogram>(S) || isa<DILexicalBlockBase>(S)) && \"Unexpected scope info\""
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 25, __PRETTY_FUNCTION__))
;
26
27 auto *Scope = cast<DIScope>(S);
28 StringRef Dir = Scope->getDirectory(),
29 Filename = Scope->getFilename();
30 char *&Result = DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)];
31 if (Result)
32 return Result;
33
34 // Clang emits directory and relative filename info into the IR, but CodeView
35 // operates on full paths. We could change Clang to emit full paths too, but
36 // that would increase the IR size and probably not needed for other users.
37 // For now, just concatenate and canonicalize the path here.
38 std::string Filepath;
39 if (Filename.find(':') == 1)
40 Filepath = Filename;
41 else
42 Filepath = (Dir + "\\" + Filename).str();
43
44 // Canonicalize the path. We have to do it textually because we may no longer
45 // have access the file in the filesystem.
46 // First, replace all slashes with backslashes.
47 std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
48
49 // Remove all "\.\" with "\".
50 size_t Cursor = 0;
51 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
52 Filepath.erase(Cursor, 2);
53
54 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
55 // path should be well-formatted, e.g. start with a drive letter, etc.
56 Cursor = 0;
57 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
58 // Something's wrong if the path starts with "\..\", abort.
59 if (Cursor == 0)
60 break;
61
62 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
63 if (PrevSlash == std::string::npos)
64 // Something's wrong, abort.
65 break;
66
67 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
68 // The next ".." might be following the one we've just erased.
69 Cursor = PrevSlash;
70 }
71
72 // Remove all duplicate backslashes.
73 Cursor = 0;
74 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
75 Filepath.erase(Cursor, 1);
76
77 Result = strdup(Filepath.c_str());
78 return StringRef(Result);
79}
80
81void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL,
82 const MachineFunction *MF) {
83 const MDNode *Scope = DL.getScope();
84 if (!Scope)
85 return;
86 StringRef Filename = getFullFilepath(Scope);
87
88 // Skip this instruction if it has the same file:line as the previous one.
89 assert(CurFn)((CurFn) ? static_cast<void> (0) : __assert_fail ("CurFn"
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 89, __PRETTY_FUNCTION__))
;
90 if (!CurFn->Instrs.empty()) {
91 const InstrInfoTy &LastInstr = InstrInfo[CurFn->Instrs.back()];
92 if (LastInstr.Filename == Filename && LastInstr.LineNumber == DL.getLine())
93 return;
94 }
95 FileNameRegistry.add(Filename);
96
97 MCSymbol *MCL = Asm->MMI->getContext().createTempSymbol();
98 Asm->OutStreamer->EmitLabel(MCL);
99 CurFn->Instrs.push_back(MCL);
100 InstrInfo[MCL] = InstrInfoTy(Filename, DL.getLine(), DL.getCol());
101}
102
103WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP)
104 : Asm(nullptr), CurFn(nullptr) {
105 MachineModuleInfo *MMI = AP->MMI;
106
107 // If module doesn't have named metadata anchors or COFF debug section
108 // is not available, skip any debug info related stuff.
109 if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
110 !AP->getObjFileLowering().getCOFFDebugSymbolsSection())
111 return;
112
113 // Tell MMI that we have debug info.
114 MMI->setDebugInfoAvailability(true);
115 Asm = AP;
116}
117
118void WinCodeViewLineTables::endModule() {
119 if (FnDebugInfo.empty())
120 return;
121
122 assert(Asm != nullptr)((Asm != nullptr) ? static_cast<void> (0) : __assert_fail
("Asm != nullptr", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 122, __PRETTY_FUNCTION__))
;
123 Asm->OutStreamer->SwitchSection(
124 Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
125 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
126
127 // The COFF .debug$S section consists of several subsections, each starting
128 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
129 // of the payload followed by the payload itself. The subsections are 4-byte
130 // aligned.
131
132 // Emit per-function debug information. This code is extracted into a
133 // separate function for readability.
134 for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I)
135 emitDebugInfoForFunction(VisitedFunctions[I]);
136
137 // This subsection holds a file index to offset in string table table.
138 Asm->OutStreamer->AddComment("File index to string table offset subsection");
139 Asm->EmitInt32(COFF::DEBUG_INDEX_SUBSECTION);
140 size_t NumFilenames = FileNameRegistry.Infos.size();
141 Asm->EmitInt32(8 * NumFilenames);
142 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
143 StringRef Filename = FileNameRegistry.Filenames[I];
144 // For each unique filename, just write its offset in the string table.
145 Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset);
146 // The function name offset is not followed by any additional data.
147 Asm->EmitInt32(0);
148 }
149
150 // This subsection holds the string table.
151 Asm->OutStreamer->AddComment("String table");
152 Asm->EmitInt32(COFF::DEBUG_STRING_TABLE_SUBSECTION);
153 Asm->EmitInt32(FileNameRegistry.LastOffset);
154 // The payload starts with a null character.
155 Asm->EmitInt8(0);
156
157 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
158 // Just emit unique filenames one by one, separated by a null character.
159 Asm->OutStreamer->EmitBytes(FileNameRegistry.Filenames[I]);
160 Asm->EmitInt8(0);
161 }
162
163 // No more subsections. Fill with zeros to align the end of the section by 4.
164 Asm->OutStreamer->EmitFill((-FileNameRegistry.LastOffset) % 4, 0);
165
166 clear();
167}
168
169static void EmitLabelDiff(MCStreamer &Streamer,
170 const MCSymbol *From, const MCSymbol *To,
171 unsigned int Size = 4) {
172 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
173 MCContext &Context = Streamer.getContext();
174 const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context),
175 *ToRef = MCSymbolRefExpr::create(To, Variant, Context);
176 const MCExpr *AddrDelta =
177 MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
178 Streamer.EmitValue(AddrDelta, Size);
179}
180
181void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) {
182 // For each function there is a separate subsection
183 // which holds the PC to file:line table.
184 const MCSymbol *Fn = Asm->getSymbol(GV);
185 assert(Fn)((Fn) ? static_cast<void> (0) : __assert_fail ("Fn", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 185, __PRETTY_FUNCTION__))
;
186
187 const FunctionInfo &FI = FnDebugInfo[GV];
188 if (FI.Instrs.empty())
189 return;
190 assert(FI.End && "Don't know where the function ends?")((FI.End && "Don't know where the function ends?") ? static_cast
<void> (0) : __assert_fail ("FI.End && \"Don't know where the function ends?\""
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 190, __PRETTY_FUNCTION__))
;
191
192 StringRef GVName = GV->getName();
193 StringRef FuncName;
194 if (auto *SP = getDISubprogram(GV))
195 FuncName = SP->getDisplayName();
196
197 // FIXME Clang currently sets DisplayName to "bar" for a C++
198 // "namespace_foo::bar" function, see PR21528. Luckily, dbghelp.dll is trying
199 // to demangle display names anyways, so let's just put a mangled name into
200 // the symbols subsection until Clang gives us what we need.
201 if (GVName.startswith("\01?"))
202 FuncName = GVName.substr(1);
203 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
204 MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(),
205 *SymbolsEnd = Asm->MMI->getContext().createTempSymbol();
206 Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName));
207 Asm->EmitInt32(COFF::DEBUG_SYMBOL_SUBSECTION);
208 EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
209 Asm->OutStreamer->EmitLabel(SymbolsBegin);
210 {
211 MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(),
212 *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol();
213 EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2);
214 Asm->OutStreamer->EmitLabel(ProcSegmentBegin);
215
216 Asm->EmitInt16(COFF::DEBUG_SYMBOL_TYPE_PROC_START);
217 // Some bytes of this segment don't seem to be required for basic debugging,
218 // so just fill them with zeroes.
219 Asm->OutStreamer->EmitFill(12, 0);
220 // This is the important bit that tells the debugger where the function
221 // code is located and what's its size:
222 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
223 Asm->OutStreamer->EmitFill(12, 0);
224 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
225 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
226 Asm->EmitInt8(0);
227 // Emit the function display name as a null-terminated string.
228 Asm->OutStreamer->EmitBytes(FuncName);
229 Asm->EmitInt8(0);
230 Asm->OutStreamer->EmitLabel(ProcSegmentEnd);
231
232 // We're done with this function.
233 Asm->EmitInt16(0x0002);
234 Asm->EmitInt16(COFF::DEBUG_SYMBOL_TYPE_PROC_END);
235 }
236 Asm->OutStreamer->EmitLabel(SymbolsEnd);
237 // Every subsection must be aligned to a 4-byte boundary.
238 Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0);
239
240 // PCs/Instructions are grouped into segments sharing the same filename.
241 // Pre-calculate the lengths (in instructions) of these segments and store
242 // them in a map for convenience. Each index in the map is the sequential
243 // number of the respective instruction that starts a new segment.
244 DenseMap<size_t, size_t> FilenameSegmentLengths;
245 size_t LastSegmentEnd = 0;
246 StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename;
247 for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) {
248 if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename)
249 continue;
250 FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd;
251 LastSegmentEnd = J;
252 PrevFilename = InstrInfo[FI.Instrs[J]].Filename;
253 }
254 FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd;
255
256 // Emit a line table subsection, required to do PC-to-file:line lookup.
257 Asm->OutStreamer->AddComment("Line table subsection for " + Twine(FuncName));
258 Asm->EmitInt32(COFF::DEBUG_LINE_TABLE_SUBSECTION);
259 MCSymbol *LineTableBegin = Asm->MMI->getContext().createTempSymbol(),
260 *LineTableEnd = Asm->MMI->getContext().createTempSymbol();
261 EmitLabelDiff(*Asm->OutStreamer, LineTableBegin, LineTableEnd);
262 Asm->OutStreamer->EmitLabel(LineTableBegin);
263
264 // Identify the function this subsection is for.
265 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
266 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
267 // Insert flags after a 16-bit section index.
268 Asm->EmitInt16(COFF::DEBUG_LINE_TABLES_HAVE_COLUMN_RECORDS);
269
270 // Length of the function's code, in bytes.
271 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
272
273 // PC-to-linenumber lookup table:
274 MCSymbol *FileSegmentEnd = nullptr;
275
276 // The start of the last segment:
277 size_t LastSegmentStart = 0;
278
279 auto FinishPreviousChunk = [&] {
280 if (!FileSegmentEnd)
281 return;
282 for (size_t ColSegI = LastSegmentStart,
283 ColSegEnd = ColSegI + FilenameSegmentLengths[LastSegmentStart];
284 ColSegI != ColSegEnd; ++ColSegI) {
285 unsigned ColumnNumber = InstrInfo[FI.Instrs[ColSegI]].ColumnNumber;
286 Asm->EmitInt16(ColumnNumber); // Start column
287 Asm->EmitInt16(ColumnNumber); // End column
288 }
289 Asm->OutStreamer->EmitLabel(FileSegmentEnd);
290 };
291
292 for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
293 MCSymbol *Instr = FI.Instrs[J];
294 assert(InstrInfo.count(Instr))((InstrInfo.count(Instr)) ? static_cast<void> (0) : __assert_fail
("InstrInfo.count(Instr)", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 294, __PRETTY_FUNCTION__))
;
295
296 if (FilenameSegmentLengths.count(J)) {
297 // We came to a beginning of a new filename segment.
298 FinishPreviousChunk();
299 StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename;
300 assert(FileNameRegistry.Infos.count(CurFilename))((FileNameRegistry.Infos.count(CurFilename)) ? static_cast<
void> (0) : __assert_fail ("FileNameRegistry.Infos.count(CurFilename)"
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 300, __PRETTY_FUNCTION__))
;
301 size_t IndexInStringTable =
302 FileNameRegistry.Infos[CurFilename].FilenameID;
303 // Each segment starts with the offset of the filename
304 // in the string table.
305 Asm->OutStreamer->AddComment(
306 "Segment for file '" + Twine(CurFilename) + "' begins");
307 MCSymbol *FileSegmentBegin = Asm->MMI->getContext().createTempSymbol();
308 Asm->OutStreamer->EmitLabel(FileSegmentBegin);
309 Asm->EmitInt32(8 * IndexInStringTable);
310
311 // Number of PC records in the lookup table.
312 size_t SegmentLength = FilenameSegmentLengths[J];
313 Asm->EmitInt32(SegmentLength);
314
315 // Full size of the segment for this filename, including the prev two
316 // records.
317 FileSegmentEnd = Asm->MMI->getContext().createTempSymbol();
318 EmitLabelDiff(*Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd);
319 LastSegmentStart = J;
Value stored to 'LastSegmentStart' is never read
320 }
321
322 // The first PC with the given linenumber and the linenumber itself.
323 EmitLabelDiff(*Asm->OutStreamer, Fn, Instr);
324 Asm->EmitInt32(InstrInfo[Instr].LineNumber);
325 }
326
327 FinishPreviousChunk();
328 Asm->OutStreamer->EmitLabel(LineTableEnd);
329}
330
331void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
332 assert(!CurFn && "Can't process two functions at once!")((!CurFn && "Can't process two functions at once!") ?
static_cast<void> (0) : __assert_fail ("!CurFn && \"Can't process two functions at once!\""
, "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 332, __PRETTY_FUNCTION__))
;
333
334 if (!Asm || !Asm->MMI->hasDebugInfo())
335 return;
336
337 const Function *GV = MF->getFunction();
338 assert(FnDebugInfo.count(GV) == false)((FnDebugInfo.count(GV) == false) ? static_cast<void> (
0) : __assert_fail ("FnDebugInfo.count(GV) == false", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 338, __PRETTY_FUNCTION__))
;
339 VisitedFunctions.push_back(GV);
340 CurFn = &FnDebugInfo[GV];
341
342 // Find the end of the function prolog.
343 // FIXME: is there a simpler a way to do this? Can we just search
344 // for the first instruction of the function, not the last of the prolog?
345 DebugLoc PrologEndLoc;
346 bool EmptyPrologue = true;
347 for (const auto &MBB : *MF) {
348 if (PrologEndLoc)
349 break;
350 for (const auto &MI : MBB) {
351 if (MI.isDebugValue())
352 continue;
353
354 // First known non-DBG_VALUE and non-frame setup location marks
355 // the beginning of the function body.
356 // FIXME: do we need the first subcondition?
357 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
358 PrologEndLoc = MI.getDebugLoc();
359 break;
360 }
361 EmptyPrologue = false;
362 }
363 }
364 // Record beginning of function if we have a non-empty prologue.
365 if (PrologEndLoc && !EmptyPrologue) {
366 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
367 maybeRecordLocation(FnStartDL, MF);
368 }
369}
370
371void WinCodeViewLineTables::endFunction(const MachineFunction *MF) {
372 if (!Asm || !CurFn) // We haven't created any debug info for this function.
373 return;
374
375 const Function *GV = MF->getFunction();
376 assert(FnDebugInfo.count(GV))((FnDebugInfo.count(GV)) ? static_cast<void> (0) : __assert_fail
("FnDebugInfo.count(GV)", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 376, __PRETTY_FUNCTION__))
;
377 assert(CurFn == &FnDebugInfo[GV])((CurFn == &FnDebugInfo[GV]) ? static_cast<void> (0
) : __assert_fail ("CurFn == &FnDebugInfo[GV]", "/tmp/buildd/llvm-toolchain-snapshot-3.8~svn253036/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp"
, 377, __PRETTY_FUNCTION__))
;
378
379 if (CurFn->Instrs.empty()) {
380 FnDebugInfo.erase(GV);
381 VisitedFunctions.pop_back();
382 } else {
383 CurFn->End = Asm->getFunctionEnd();
384 }
385 CurFn = nullptr;
386}
387
388void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) {
389 // Ignore DBG_VALUE locations and function prologue.
390 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
391 return;
392 DebugLoc DL = MI->getDebugLoc();
393 if (DL == PrevInstLoc || !DL)
394 return;
395 maybeRecordLocation(DL, Asm->MF);
396}
397}