Silencing an MSVC warning about loop variable conflicting with a variable from an...
[oota-llvm.git] / lib / CodeGen / AsmPrinter / WinCodeViewLineTables.cpp
index 1cb69e97c059828ed933849bee426050fd715287..6a5c431d4fd18d10c23fe370f33f962d3f2d2d69 100644 (file)
@@ -29,7 +29,7 @@ StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) {
   StringRef Dir = Scope.getDirectory(),
             Filename = Scope.getFilename();
   char *&Result = DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)];
-  if (Result != 0)
+  if (Result)
     return Result;
 
   // Clang emits directory and relative filename info into the IR, but CodeView
@@ -101,10 +101,19 @@ void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL,
   InstrInfo[MCL] = InstrInfoTy(Filename, DL.getLine());
 }
 
-WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *Asm)
-    : Asm(Asm), CurFn(0) {
+WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP)
+    : Asm(nullptr), CurFn(nullptr) {
+  MachineModuleInfo *MMI = AP->MMI;
+
+  // If module doesn't have named metadata anchors or COFF debug section
+  // is not available, skip any debug info related stuff.
+  if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
+      !AP->getObjFileLowering().getCOFFDebugSymbolsSection())
+    return;
+
   // Tell MMI that we have debug info.
-  Asm->MMI->setDebugInfoAvailability(true);
+  MMI->setDebugInfoAvailability(true);
+  Asm = AP;
 }
 
 static void EmitLabelDiff(MCStreamer &Streamer,
@@ -122,9 +131,12 @@ void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) {
   // For each function there is a separate subsection
   // which holds the PC to file:line table.
   const MCSymbol *Fn = Asm->getSymbol(GV);
-  const FunctionInfo &FI = FnDebugInfo[GV];
   assert(Fn);
-  assert(FI.Instrs.size() > 0);
+
+  const FunctionInfo &FI = FnDebugInfo[GV];
+  if (FI.Instrs.empty())
+    return;
+  assert(FI.End && "Don't know where the function ends?");
 
   // PCs/Instructions are grouped into segments sharing the same filename.
   // Pre-calculate the lengths (in instructions) of these segments and store
@@ -159,7 +171,7 @@ void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) {
   EmitLabelDiff(Asm->OutStreamer, Fn, FI.End);
 
   // PC-to-linenumber lookup table:
-  MCSymbol *FileSegmentEnd = 0;
+  MCSymbol *FileSegmentEnd = nullptr;
   for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
     MCSymbol *Instr = FI.Instrs[J];
     assert(InstrInfo.count(Instr));
@@ -204,6 +216,7 @@ void WinCodeViewLineTables::endModule() {
   if (FnDebugInfo.empty())
     return;
 
+  assert(Asm != nullptr);
   Asm->OutStreamer.SwitchSection(
       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
   Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
@@ -251,13 +264,7 @@ void WinCodeViewLineTables::endModule() {
 void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
   assert(!CurFn && "Can't process two functions at once!");
 
-  if (!Asm->MMI->hasDebugInfo())
-    return;
-
-  // Grab the lexical scopes for the function, if we don't have any of those
-  // then we're not going to be able to do anything.
-  LScopes.initialize(*MF);
-  if (LScopes.empty())
+  if (!Asm || !Asm->MMI->hasDebugInfo())
     return;
 
   const Function *GV = MF->getFunction();
@@ -270,20 +277,19 @@ void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
   // for the first instruction of the function, not the last of the prolog?
   DebugLoc PrologEndLoc;
   bool EmptyPrologue = true;
-  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
-       I != E && PrologEndLoc.isUnknown(); ++I) {
-    for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
-         II != IE; ++II) {
-      const MachineInstr *MI = II;
-      if (MI->isDebugValue())
+  for (const auto &MBB : *MF) {
+    if (!PrologEndLoc.isUnknown())
+      break;
+    for (const auto &MI : MBB) {
+      if (MI.isDebugValue())
         continue;
 
       // First known non-DBG_VALUE and non-frame setup location marks
       // the beginning of the function body.
       // FIXME: do we need the first subcondition?
-      if (!MI->getFlag(MachineInstr::FrameSetup) &&
-          (!MI->getDebugLoc().isUnknown())) {
-        PrologEndLoc = MI->getDebugLoc();
+      if (!MI.getFlag(MachineInstr::FrameSetup) &&
+          (!MI.getDebugLoc().isUnknown())) {
+        PrologEndLoc = MI.getDebugLoc();
         break;
       }
       EmptyPrologue = false;
@@ -297,23 +303,29 @@ void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
   }
 }
 
-void WinCodeViewLineTables::endFunction(const MachineFunction *) {
-  if (!CurFn)  // We haven't created any debug info for this function.
+void WinCodeViewLineTables::endFunction(const MachineFunction *MF) {
+  if (!Asm || !CurFn)  // We haven't created any debug info for this function.
     return;
 
-  if (CurFn->Instrs.empty())
-    llvm_unreachable("Can this ever happen?");
-
-  // Define end label for subprogram.
-  MCSymbol *FunctionEndSym = Asm->OutStreamer.getContext().CreateTempSymbol();
-  Asm->OutStreamer.EmitLabel(FunctionEndSym);
-  CurFn->End = FunctionEndSym;
-  CurFn = 0;
+  const Function *GV = MF->getFunction();
+  assert(FnDebugInfo.count(GV));
+  assert(CurFn == &FnDebugInfo[GV]);
+
+  if (CurFn->Instrs.empty()) {
+    FnDebugInfo.erase(GV);
+    VisitedFunctions.pop_back();
+  } else {
+    // Define end label for subprogram.
+    MCSymbol *FunctionEndSym = Asm->OutStreamer.getContext().CreateTempSymbol();
+    Asm->OutStreamer.EmitLabel(FunctionEndSym);
+    CurFn->End = FunctionEndSym;
+  }
+  CurFn = nullptr;
 }
 
 void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) {
   // Ignore DBG_VALUE locations and function prologue.
-  if (MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
+  if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
     return;
   DebugLoc DL = MI->getDebugLoc();
   if (DL == PrevInstLoc || DL.isUnknown())