@@ -62,11 +62,6 @@ static cl::opt<bool>
62
62
DisableDebugInfoPrinting (" disable-debug-info-print" , cl::Hidden,
63
63
cl::desc (" Disable debug info printing" ));
64
64
65
- static cl::opt<bool > UnknownLocations (
66
- " use-unknown-locations" , cl::Hidden,
67
- cl::desc (" Make an absence of debug location information explicit." ),
68
- cl::init(false ));
69
-
70
65
static cl::opt<bool >
71
66
GenerateGnuPubSections (" generate-gnu-dwarf-pub-sections" , cl::Hidden,
72
67
cl::desc (" Generate GNU-style pubnames and pubtypes" ),
@@ -81,6 +76,13 @@ namespace {
81
76
enum DefaultOnOff { Default, Enable, Disable };
82
77
}
83
78
79
+ static cl::opt<DefaultOnOff> UnknownLocations (
80
+ " use-unknown-locations" , cl::Hidden,
81
+ cl::desc (" Make an absence of debug location information explicit." ),
82
+ cl::values(clEnumVal(Default, " At top of block or after label" ),
83
+ clEnumVal(Enable, " In all cases" ), clEnumVal(Disable, " Never" )),
84
+ cl::init(Default));
85
+
84
86
static cl::opt<DefaultOnOff>
85
87
DwarfAccelTables (" dwarf-accel-tables" , cl::Hidden,
86
88
cl::desc (" Output prototype dwarf accelerator tables." ),
@@ -1010,31 +1012,73 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {
1010
1012
if (MI->isDebugValue () || MI->isCFIInstruction ())
1011
1013
return ;
1012
1014
const DebugLoc &DL = MI->getDebugLoc ();
1013
- if (DL == PrevInstLoc)
1015
+ // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1016
+ // the last line number actually emitted, to see if it was line 0.
1017
+ unsigned LastAsmLine =
1018
+ Asm->OutStreamer ->getContext ().getCurrentDwarfLoc ().getLine ();
1019
+
1020
+ if (DL == PrevInstLoc) {
1021
+ // If we have an ongoing unspecified location, nothing to do here.
1022
+ if (!DL)
1023
+ return ;
1024
+ // We have an explicit location, same as the previous location.
1025
+ // But we might be coming back to it after a line 0 record.
1026
+ if (LastAsmLine == 0 && DL.getLine () != 0 ) {
1027
+ // Reinstate the source location but not marked as a statement.
1028
+ const MDNode *Scope = DL.getScope ();
1029
+ recordSourceLine (DL.getLine (), DL.getCol (), Scope, /* Flags=*/ 0 );
1030
+ }
1014
1031
return ;
1032
+ }
1015
1033
1016
1034
if (!DL) {
1017
1035
// We have an unspecified location, which might want to be line 0.
1018
- if (UnknownLocations) {
1019
- PrevInstLoc = DL;
1020
- recordSourceLine (0 , 0 , nullptr , 0 );
1036
+ // If we have already emitted a line-0 record, don't repeat it.
1037
+ if (LastAsmLine == 0 )
1038
+ return ;
1039
+ // If user said Don't Do That, don't do that.
1040
+ if (UnknownLocations == Disable)
1041
+ return ;
1042
+ // See if we have a reason to emit a line-0 record now.
1043
+ // Reasons to emit a line-0 record include:
1044
+ // - User asked for it (UnknownLocations).
1045
+ // - Instruction has a label, so it's referenced from somewhere else,
1046
+ // possibly debug information; we want it to have a source location.
1047
+ // - Instruction is at the top of a block; we don't want to inherit the
1048
+ // location from the physically previous (maybe unrelated) block.
1049
+ if (UnknownLocations == Enable || PrevLabel ||
1050
+ (PrevInstBB && PrevInstBB != MI->getParent ())) {
1051
+ // Preserve the file number, if we can, to save space in the line table.
1052
+ // Do not update PrevInstLoc, it remembers the last non-0 line.
1053
+ // FIXME: Also preserve the column number, to save more space?
1054
+ const MDNode *Scope = PrevInstLoc ? PrevInstLoc.getScope () : nullptr ;
1055
+ recordSourceLine (0 , 0 , Scope, 0 );
1021
1056
}
1022
1057
return ;
1023
1058
}
1024
1059
1025
- // We have a new, explicit location.
1060
+ // We have an explicit location, different from the previous location.
1061
+ // Don't repeat a line-0 record, but otherwise emit the new location.
1062
+ // (The new location might be an explicit line 0, which we do emit.)
1063
+ if (DL.getLine () == 0 && LastAsmLine == 0 )
1064
+ return ;
1026
1065
unsigned Flags = 0 ;
1027
- PrevInstLoc = DL;
1028
1066
if (DL == PrologEndLoc) {
1029
1067
Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
1030
1068
PrologEndLoc = DebugLoc ();
1031
1069
}
1032
- if (DL.getLine () !=
1033
- Asm->OutStreamer ->getContext ().getCurrentDwarfLoc ().getLine ())
1070
+ // If the line changed, we call that a new statement; unless we went to
1071
+ // line 0 and came back, in which case it is not a new statement.
1072
+ unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine () : LastAsmLine;
1073
+ if (DL.getLine () && DL.getLine () != OldLine)
1034
1074
Flags |= DWARF2_FLAG_IS_STMT;
1035
1075
1036
1076
const MDNode *Scope = DL.getScope ();
1037
1077
recordSourceLine (DL.getLine (), DL.getCol (), Scope, Flags);
1078
+
1079
+ // If we're not at line 0, remember this location.
1080
+ if (DL.getLine ())
1081
+ PrevInstLoc = DL;
1038
1082
}
1039
1083
1040
1084
static DebugLoc findPrologueEndLoc (const MachineFunction *MF) {
0 commit comments