[llvm] r357858 - [llvm-objdump] Fix split of source lines; don't ltrim source lines
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 7 03:16:47 PDT 2019
Author: maskray
Date: Sun Apr 7 03:16:46 2019
New Revision: 357858
URL: http://llvm.org/viewvc/llvm-project?rev=357858&view=rev
Log:
[llvm-objdump] Fix split of source lines; don't ltrim source lines
If the file does not end with a newline, it may be dropped. Fix the
splitting algorithm.
Also delete an unnecessary SourceCache lookup.
Modified:
llvm/trunk/test/tools/llvm-objdump/X86/source-interleave-x86_64.ll
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
Modified: llvm/trunk/test/tools/llvm-objdump/X86/source-interleave-x86_64.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/source-interleave-x86_64.ll?rev=357858&r1=357857&r2=357858&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/source-interleave-x86_64.ll (original)
+++ llvm/trunk/test/tools/llvm-objdump/X86/source-interleave-x86_64.ll Sun Apr 7 03:16:46 2019
@@ -7,10 +7,8 @@
; RUN: llvm-objdump -dS %t.o >%t3
; RUN: cmp %t0 %t1
; RUN: cmp %t2 %t3
-; RUN: FileCheck --input-file %t0 --check-prefix="LINES" %t.ll
-; RUN: FileCheck --input-file %t1 --check-prefix="LINES" %t.ll
-; RUN: FileCheck --input-file %t2 --check-prefix="SOURCE" %t.ll
-; RUN: FileCheck --input-file %t3 --check-prefix="SOURCE" %t.ll
+; RUN: FileCheck --check-prefix=LINES %t.ll < %t0
+; RUN: FileCheck --check-prefix=SOURCE --strict-whitespace %t.ll < %t2
; ModuleID = 'source-interleave-x86_64.bc'
source_filename = "source-interleave-x86_64.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
@@ -79,3 +77,4 @@ attributes #1 = { nounwind readnone }
; SOURCE: main:
; SOURCE-NEXT: ; int main() {
+; SOURCE: ; int *b = &a;
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=357858&r1=357857&r2=357858&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Sun Apr 7 03:16:46 2019
@@ -522,17 +522,17 @@ bool SourcePrinter::cacheSource(const DI
Buffer = std::move(*BufferOrError);
}
// Chomp the file to get lines
- size_t BufferSize = Buffer->getBufferSize();
- const char *BufferStart = Buffer->getBufferStart();
- for (const char *Start = BufferStart, *End = BufferStart;
- End < BufferStart + BufferSize; End++)
- if (*End == '\n' || End == BufferStart + BufferSize - 1 ||
- (*End == '\r' && *(End + 1) == '\n')) {
- LineCache[LineInfo.FileName].push_back(StringRef(Start, End - Start));
- if (*End == '\r')
- End++;
- Start = End + 1;
+ const char *BufferStart = Buffer->getBufferStart(),
+ *BufferEnd = Buffer->getBufferEnd();
+ std::vector<StringRef> &Lines = LineCache[LineInfo.FileName];
+ const char *Start = BufferStart;
+ for (const char *I = BufferStart; I != BufferEnd; ++I)
+ if (*I == '\n') {
+ Lines.emplace_back(Start, I - Start - (BufferStart < I && I[-1] == '\r'));
+ Start = I + 1;
}
+ if (Start < BufferEnd)
+ Lines.emplace_back(Start, BufferEnd - Start);
SourceCache[LineInfo.FileName] = std::move(Buffer);
return true;
}
@@ -560,16 +560,12 @@ void SourcePrinter::printSourceLine(raw_
if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
if (!cacheSource(LineInfo))
return;
- auto FileBuffer = SourceCache.find(LineInfo.FileName);
- if (FileBuffer != SourceCache.end()) {
- auto LineBuffer = LineCache.find(LineInfo.FileName);
- if (LineBuffer != LineCache.end()) {
- if (LineInfo.Line > LineBuffer->second.size())
- return;
- // Vector begins at 0, line numbers are non-zero
- OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim()
- << "\n";
- }
+ auto LineBuffer = LineCache.find(LineInfo.FileName);
+ if (LineBuffer != LineCache.end()) {
+ if (LineInfo.Line > LineBuffer->second.size())
+ return;
+ // Vector begins at 0, line numbers are non-zero
+ OS << Delimiter << LineBuffer->second[LineInfo.Line - 1] << '\n';
}
}
OldLineInfo = LineInfo;
More information about the llvm-commits
mailing list