Skip to content

Commit 5f4ca2f

Browse files
author
Hemant Kulkarni
committed
llvm-size: Add --totals option
Differential Revision: https://reviews.llvm.org/D24308 llvm-svn: 281233
1 parent aecf9d0 commit 5f4ca2f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

llvm/test/tools/llvm-size/X86/ignore-sections.s

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
22
// RUN: llvm-size -A %t.o | FileCheck --check-prefix="SYSV" %s
3-
// RUN: llvm-size -B %t.o| FileCheck --check-prefix="BSD" %s
3+
// RUN: llvm-size -B -t %t.o| FileCheck --check-prefix="BSD" %s
44

55
.text
66
.zero 4
@@ -26,3 +26,4 @@
2626

2727
// BSD: text data bss dec hex filename
2828
// BSD-NEXT: 4 4 4 12 c {{[ -\(\)_A-Za-z0-9.\\/:]+}}
29+
// BSD-NEXT: 4 4 4 12 c (TOTALS)

llvm/tools/llvm-size/llvm-size.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ static cl::opt<OutputFormatTy> OutputFormatShort(
5252

5353
static bool BerkeleyHeaderPrinted = false;
5454
static bool MoreThanOneFile = false;
55+
static uint64_t TotalObjectText = 0;
56+
static uint64_t TotalObjectData = 0;
57+
static uint64_t TotalObjectBss = 0;
58+
static uint64_t TotalObjectTotal = 0;
5559

5660
cl::opt<bool>
5761
DarwinLongFormat("l", cl::desc("When format is darwin, use long format "
@@ -81,6 +85,14 @@ RadixShort(cl::desc("Print size in radix:"),
8185
clEnumValEnd),
8286
cl::init(decimal));
8387

88+
static cl::opt<bool>
89+
TotalSizes("totals",
90+
cl::desc("Print totals of all objects - Berkeley format only"),
91+
cl::init(false));
92+
93+
static cl::alias TotalSizesShort("t", cl::desc("Short for --totals"),
94+
cl::aliasopt(TotalSizes));
95+
8496
static cl::list<std::string>
8597
InputFilenames(cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
8698

@@ -108,7 +120,7 @@ static bool error(Twine Message) {
108120

109121
// This version of error() prints the archive name and member name, for example:
110122
// "libx.a(foo.o)" after the ToolName before the error message. It sets
111-
// HadError but returns allowing the code to move on to other archive members.
123+
// HadError but returns allowing the code to move on to other archive members.
112124
static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
113125
StringRef ArchitectureName = StringRef()) {
114126
HadError = true;
@@ -136,7 +148,7 @@ static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
136148

137149
// This version of error() prints the file name and which architecture slice it // is from, for example: "foo.o (for architecture i386)" after the ToolName
138150
// before the error message. It sets HadError but returns allowing the code to
139-
// move on to other architecture slices.
151+
// move on to other architecture slices.
140152
static void error(llvm::Error E, StringRef FileName,
141153
StringRef ArchitectureName = StringRef()) {
142154
HadError = true;
@@ -462,6 +474,13 @@ static void printObjectSectionSizes(ObjectFile *Obj) {
462474

463475
total = total_text + total_data + total_bss;
464476

477+
if (TotalSizes) {
478+
TotalObjectText += total_text;
479+
TotalObjectData += total_data;
480+
TotalObjectBss += total_bss;
481+
TotalObjectTotal += total;
482+
}
483+
465484
if (!BerkeleyHeaderPrinted) {
466485
outs() << " text data bss "
467486
<< (Radix == octal ? "oct" : "dec") << " hex filename\n";
@@ -821,6 +840,22 @@ static void printFileSectionSizes(StringRef file) {
821840
outs() << "\n";
822841
}
823842

843+
static void printBerkelyTotals() {
844+
std::string fmtbuf;
845+
raw_string_ostream fmt(fmtbuf);
846+
const char *radix_fmt = getRadixFmt();
847+
fmt << "%#7" << radix_fmt << " "
848+
<< "%#7" << radix_fmt << " "
849+
<< "%#7" << radix_fmt << " ";
850+
outs() << format(fmt.str().c_str(), TotalObjectText, TotalObjectData,
851+
TotalObjectBss);
852+
fmtbuf.clear();
853+
fmt << "%7" << (Radix == octal ? PRIo64 : PRIu64) << " "
854+
<< "%7" PRIx64 " ";
855+
outs() << format(fmt.str().c_str(), TotalObjectTotal, TotalObjectTotal)
856+
<< "(TOTALS)\n";
857+
}
858+
824859
int main(int argc, char **argv) {
825860
// Print a stack trace if we signal out.
826861
sys::PrintStackTraceOnErrorSignal(argv[0]);
@@ -853,6 +888,8 @@ int main(int argc, char **argv) {
853888
MoreThanOneFile = InputFilenames.size() > 1;
854889
std::for_each(InputFilenames.begin(), InputFilenames.end(),
855890
printFileSectionSizes);
891+
if (OutputFormat == berkeley && TotalSizes)
892+
printBerkelyTotals();
856893

857894
if (HadError)
858895
return 1;

0 commit comments

Comments
 (0)