Skip to content

Commit 5baab4c

Browse files
committed
[Support] Add convenience functions to WithColor. NFC.
Create convenience functions for printing error, warning and note to stdout. Previously we had similar functions being used in dsymutil, but given that this pattern is so common it makes sense to make it available globally. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330091 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 851a760 commit 5baab4c

File tree

8 files changed

+78
-88
lines changed

8 files changed

+78
-88
lines changed

include/llvm/Support/WithColor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class WithColor {
4141

4242
raw_ostream &get() { return OS; }
4343
operator raw_ostream &() { return OS; }
44+
45+
/// Convenience method for printing "error: " to stderr.
46+
static raw_ostream &error();
47+
48+
/// Convenience method for printing "warning: " to stderr.
49+
static raw_ostream &warning();
50+
51+
/// Convenience method for printing "note: " to stderr.
52+
static raw_ostream &note();
4453
};
4554

4655
} // end namespace llvm

lib/Support/WithColor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ WithColor::WithColor(raw_ostream &OS, HighlightColor Color) : OS(OS) {
5959
}
6060
}
6161

62+
raw_ostream &WithColor::error() {
63+
return WithColor(errs(), HighlightColor::Error).get() << "error: ";
64+
}
65+
66+
raw_ostream &WithColor::warning() {
67+
return WithColor(errs(), HighlightColor::Warning).get() << "warning: ";
68+
}
69+
70+
raw_ostream &WithColor::note() {
71+
return WithColor(errs(), HighlightColor::Note).get() << "note: ";
72+
}
73+
6274
WithColor::~WithColor() {
6375
if (colorsEnabled(OS))
6476
OS.resetColor();

tools/dsymutil/DebugMap.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "DebugMap.h"
1111
#include "BinaryHolder.h"
12-
#include "ErrorReporting.h"
1312
#include "llvm/ADT/Optional.h"
1413
#include "llvm/ADT/SmallString.h"
1514
#include "llvm/ADT/StringMap.h"
@@ -23,6 +22,7 @@
2322
#include "llvm/Support/Format.h"
2423
#include "llvm/Support/MemoryBuffer.h"
2524
#include "llvm/Support/Path.h"
25+
#include "llvm/Support/WithColor.h"
2626
#include "llvm/Support/YAMLTraits.h"
2727
#include "llvm/Support/raw_ostream.h"
2828
#include <algorithm>
@@ -63,10 +63,9 @@ void DebugMapObject::print(raw_ostream &OS) const {
6363
Entries.reserve(Symbols.getNumItems());
6464
for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
6565
Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
66-
llvm::sort(Entries.begin(), Entries.end(),
67-
[](const Entry &LHS, const Entry &RHS) {
68-
return LHS.first < RHS.first;
69-
});
66+
llvm::sort(
67+
Entries.begin(), Entries.end(),
68+
[](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
7069
for (const auto &Sym : Entries) {
7170
if (Sym.second.ObjectAddress)
7271
OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
@@ -243,7 +242,8 @@ MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
243242
sys::path::append(Path, Filename);
244243
auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
245244
if (auto EC = ErrOrObjectFiles.getError()) {
246-
warn_ostream() << "Unable to open " << Path << " " << EC.message() << '\n';
245+
WithColor::warning() << "Unable to open " << Path << " " << EC.message()
246+
<< '\n';
247247
} else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
248248
// Rewrite the object file symbol addresses in the debug map. The YAML
249249
// input is mainly used to test dsymutil without requiring binaries

tools/dsymutil/DwarfLinker.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "BinaryHolder.h"
1111
#include "DebugMap.h"
12-
#include "ErrorReporting.h"
1312
#include "MachOUtils.h"
1413
#include "NonRelocatableStringpool.h"
1514
#include "dsymutil.h"
@@ -79,6 +78,7 @@
7978
#include "llvm/Support/TargetRegistry.h"
8079
#include "llvm/Support/ThreadPool.h"
8180
#include "llvm/Support/ToolOutputFile.h"
81+
#include "llvm/Support/WithColor.h"
8282
#include "llvm/Support/raw_ostream.h"
8383
#include "llvm/Target/TargetMachine.h"
8484
#include "llvm/Target/TargetOptions.h"
@@ -587,15 +587,15 @@ static bool inFunctionScope(CompileUnit &U, unsigned Idx) {
587587
} // namespace
588588

589589
void warn(Twine Warning, Twine Context) {
590-
warn_ostream() << Warning + "\n";
590+
WithColor::warning() << Warning + "\n";
591591
if (!Context.isTriviallyEmpty())
592-
note_ostream() << Twine("while processing ") + Context + "\n";
592+
WithColor::note() << Twine("while processing ") + Context + "\n";
593593
}
594594

595595
bool error(Twine Error, Twine Context) {
596-
error_ostream() << Error + "\n";
596+
WithColor::error() << Error + "\n";
597597
if (!Context.isTriviallyEmpty())
598-
note_ostream() << Twine("while processing ") + Context + "\n";
598+
WithColor::note() << Twine("while processing ") + Context + "\n";
599599
return false;
600600
}
601601

@@ -2178,7 +2178,7 @@ void DwarfLinker::reportWarning(const Twine &Warning, const DebugMapObject &DMO,
21782178
DumpOpts.RecurseDepth = 0;
21792179
DumpOpts.Verbose = Options.Verbose;
21802180

2181-
note_ostream() << " in DIE:\n";
2181+
WithColor::note() << " in DIE:\n";
21822182
DIE->dump(errs(), 6 /* Indent */, DumpOpts);
21832183
}
21842184

@@ -3999,9 +3999,9 @@ Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath,
39993999
// cache has expired and was pruned by clang. A more adventurous
40004000
// dsymutil would invoke clang to rebuild the module now.
40014001
if (!ModuleCacheHintDisplayed) {
4002-
note_ostream() << "The clang module cache may have expired since "
4003-
"this object file was built. Rebuilding the "
4004-
"object file will rebuild the module cache.\n";
4002+
WithColor::note() << "The clang module cache may have expired since "
4003+
"this object file was built. Rebuilding the "
4004+
"object file will rebuild the module cache.\n";
40054005
ModuleCacheHintDisplayed = true;
40064006
}
40074007
} else if (isArchive) {
@@ -4010,12 +4010,13 @@ Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath,
40104010
// was built on a different machine. We don't want to discourage module
40114011
// debugging for convenience libraries within a project though.
40124012
if (!ArchiveHintDisplayed) {
4013-
note_ostream() << "Linking a static library that was built with "
4014-
"-gmodules, but the module cache was not found. "
4015-
"Redistributable static libraries should never be "
4016-
"built with module debugging enabled. The debug "
4017-
"experience will be degraded due to incomplete "
4018-
"debug information.\n";
4013+
WithColor::note()
4014+
<< "Linking a static library that was built with "
4015+
"-gmodules, but the module cache was not found. "
4016+
"Redistributable static libraries should never be "
4017+
"built with module debugging enabled. The debug "
4018+
"experience will be degraded due to incomplete "
4019+
"debug information.\n";
40194020
ArchiveHintDisplayed = true;
40204021
}
40214022
}
@@ -4248,10 +4249,10 @@ bool DwarfLinker::link(const DebugMap &Map) {
42484249
Stat.getLastModificationTime() !=
42494250
sys::TimePoint<>(LinkContext.DMO.getTimestamp())) {
42504251
// Not using the helper here as we can easily stream TimePoint<>.
4251-
warn_ostream() << "Timestamp mismatch for " << File << ": "
4252-
<< Stat.getLastModificationTime() << " and "
4253-
<< sys::TimePoint<>(LinkContext.DMO.getTimestamp())
4254-
<< "\n";
4252+
WithColor::warning()
4253+
<< "Timestamp mismatch for " << File << ": "
4254+
<< Stat.getLastModificationTime() << " and "
4255+
<< sys::TimePoint<>(LinkContext.DMO.getTimestamp()) << "\n";
42554256
continue;
42564257
}
42574258

tools/dsymutil/ErrorReporting.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

tools/dsymutil/MachODebugMapParser.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
#include "BinaryHolder.h"
1111
#include "DebugMap.h"
12-
#include "ErrorReporting.h"
1312
#include "MachOUtils.h"
1413
#include "llvm/ADT/Optional.h"
1514
#include "llvm/Object/MachO.h"
1615
#include "llvm/Support/Path.h"
16+
#include "llvm/Support/WithColor.h"
1717
#include "llvm/Support/raw_ostream.h"
1818

1919
namespace {
@@ -102,9 +102,10 @@ class MachODebugMapParser {
102102
StringRef BinaryPath);
103103

104104
void Warning(const Twine &Msg, StringRef File = StringRef()) {
105-
warn_ostream() << "("
106-
<< MachOUtils::getArchName(Result->getTriple().getArchName())
107-
<< ") " << File << " " << Msg << "\n";
105+
WithColor::warning() << "("
106+
<< MachOUtils::getArchName(
107+
Result->getTriple().getArchName())
108+
<< ") " << File << " " << Msg << "\n";
108109

109110
if (PaperTrailWarnings) {
110111
if (!File.empty())

tools/dsymutil/MachOUtils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "MachOUtils.h"
1111
#include "BinaryHolder.h"
1212
#include "DebugMap.h"
13-
#include "ErrorReporting.h"
1413
#include "NonRelocatableStringpool.h"
1514
#include "dsymutil.h"
1615
#include "llvm/MC/MCAsmLayout.h"
@@ -21,6 +20,7 @@
2120
#include "llvm/Object/MachO.h"
2221
#include "llvm/Support/FileUtilities.h"
2322
#include "llvm/Support/Program.h"
23+
#include "llvm/Support/WithColor.h"
2424
#include "llvm/Support/raw_ostream.h"
2525

2626
namespace llvm {
@@ -39,15 +39,15 @@ static bool runLipo(StringRef SDKPath, SmallVectorImpl<const char *> &Args) {
3939
Path = sys::findProgramByName("lipo");
4040

4141
if (!Path) {
42-
error_ostream() << "lipo: " << Path.getError().message() << "\n";
42+
WithColor::error() << "lipo: " << Path.getError().message() << "\n";
4343
return false;
4444
}
4545

4646
std::string ErrMsg;
4747
int result =
4848
sys::ExecuteAndWait(*Path, Args.data(), nullptr, {}, 0, 0, &ErrMsg);
4949
if (result) {
50-
error_ostream() << "lipo: " << ErrMsg << "\n";
50+
WithColor::error() << "lipo: " << ErrMsg << "\n";
5151
return false;
5252
}
5353

@@ -64,8 +64,8 @@ bool generateUniversalBinary(SmallVectorImpl<ArchAndFilename> &ArchFiles,
6464
StringRef From(ArchFiles.front().Path);
6565
if (sys::fs::rename(From, OutputFileName)) {
6666
if (std::error_code EC = sys::fs::copy_file(From, OutputFileName)) {
67-
error_ostream() << "while copying " << From << " to " << OutputFileName
68-
<< ": " << EC.message() << "\n";
67+
WithColor::error() << "while copying " << From << " to "
68+
<< OutputFileName << ": " << EC.message() << "\n";
6969
return false;
7070
}
7171
sys::fs::remove(From);

0 commit comments

Comments
 (0)