From 1dd69783bb88d18537558039cf2804fa5daab70e Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Fri, 9 Mar 2018 15:22:42 +0000 Subject: [PATCH] [dsymutil] Unify error handling and add color We improved the handling of errors and warnings in dwarfdump's verifier in rL314498. This patch does the same thing for dsymutil. Differential revision: https://reviews.llvm.org/D44052 llvm-svn: 327137 --- llvm/test/tools/dsymutil/X86/module-warnings.test | 2 +- llvm/test/tools/dsymutil/X86/swift-ast-x86_64.test | 2 +- llvm/tools/dsymutil/DwarfLinker.cpp | 57 ++++++++++++++++------ llvm/tools/dsymutil/dsymutil.h | 4 +- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/llvm/test/tools/dsymutil/X86/module-warnings.test b/llvm/test/tools/dsymutil/X86/module-warnings.test index 3240a26..453e90e 100644 --- a/llvm/test/tools/dsymutil/X86/module-warnings.test +++ b/llvm/test/tools/dsymutil/X86/module-warnings.test @@ -42,7 +42,7 @@ # STATIC: warning: {{.*}}Bar.pcm: # STATIC: note: Linking a static library # STATIC: warning: {{.*}}Foo.pcm: -# STATIC-NOT: note: +# STATIC-NOT: warning: --- triple: 'x86_64-apple-darwin' diff --git a/llvm/test/tools/dsymutil/X86/swift-ast-x86_64.test b/llvm/test/tools/dsymutil/X86/swift-ast-x86_64.test index 98d3850..daee1cd 100644 --- a/llvm/test/tools/dsymutil/X86/swift-ast-x86_64.test +++ b/llvm/test/tools/dsymutil/X86/swift-ast-x86_64.test @@ -16,4 +16,4 @@ READOBJ: |SWIFTMODULE DATA| READOBJ-NEXT: |.| RUN: llvm-dsymutil -oso-prepend-path %p/.. %p/../Inputs/swift-ast.macho.x86_64 -no-output -verbose 2>&1 | FileCheck %s --check-prefix=TIMESTAMP -TIMESTAMP: Warning: Timestamp mismatch +TIMESTAMP: warning: Timestamp mismatch diff --git a/llvm/tools/dsymutil/DwarfLinker.cpp b/llvm/tools/dsymutil/DwarfLinker.cpp index 6acf305..04b5210 100644 --- a/llvm/tools/dsymutil/DwarfLinker.cpp +++ b/llvm/tools/dsymutil/DwarfLinker.cpp @@ -76,6 +76,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" @@ -564,8 +565,32 @@ static bool inFunctionScope(CompileUnit &U, unsigned Idx) { return false; } +static raw_ostream &error_ostream() { + return WithColor(errs(), HighlightColor::Error).get() << "error: "; +} + +static raw_ostream &warn_ostream() { + return WithColor(errs(), HighlightColor::Warning).get() << "warning: "; +} + +static raw_ostream ¬e_ostream() { + return WithColor(errs(), HighlightColor::Note).get() << "note: "; +} } // end anonymous namespace +void warn(Twine Warning, Twine Context) { + warn_ostream() << Warning + "\n"; + if (!Context.isTriviallyEmpty()) + note_ostream() << Twine("while processing ") + Context + ":\n"; +} + +bool error(Twine Error, Twine Context) { + error_ostream() << Error + "\n"; + if (!Context.isTriviallyEmpty()) + note_ostream() << Twine("while processing ") + Context + ":\n"; + return false; +} + void CompileUnit::markEverythingAsKept() { unsigned Idx = 0; @@ -2092,7 +2117,7 @@ void DwarfLinker::reportWarning(const Twine &Warning, DumpOpts.RecurseDepth = 0; DumpOpts.Verbose = Options.Verbose; - errs() << " in DIE:\n"; + note_ostream() << " in DIE:\n"; DIE->dump(errs(), 6 /* Indent */, DumpOpts); } @@ -3882,9 +3907,9 @@ Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath, // cache has expired and was pruned by clang. A more adventurous // dsymutil would invoke clang to rebuild the module now. if (!ModuleCacheHintDisplayed) { - errs() << "note: The clang module cache may have expired since this " - "object file was built. Rebuilding the object file will " - "rebuild the module cache.\n"; + note_ostream() << "The clang module cache may have expired since " + "this object file was built. Rebuilding the " + "object file will rebuild the module cache.\n"; ModuleCacheHintDisplayed = true; } } else if (isArchive) { @@ -3893,11 +3918,12 @@ Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath, // was built on a different machine. We don't want to discourage module // debugging for convenience libraries within a project though. if (!ArchiveHintDisplayed) { - errs() << "note: Linking a static library that was built with " - "-gmodules, but the module cache was not found. " - "Redistributable static libraries should never be built " - "with module debugging enabled. The debug experience will " - "be degraded due to incomplete debug information.\n"; + note_ostream() << "Linking a static library that was built with " + "-gmodules, but the module cache was not found. " + "Redistributable static libraries should never be " + "built with module debugging enabled. The debug " + "experience will be degraded due to incomplete " + "debug information.\n"; ArchiveHintDisplayed = true; } } @@ -3921,7 +3947,7 @@ Error DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath, (Filename + ": Clang modules are expected to have exactly 1 compile unit.\n") .str(); - errs() << Err; + error(Err); return make_error(Err, inconvertibleErrorCode()); } // FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is @@ -4034,15 +4060,16 @@ bool DwarfLinker::link(const DebugMap &Map) { continue; } sys::fs::file_status Stat; - if (auto errc = sys::fs::status(File, Stat)) { - errs() << "Warning: " << errc.message() << "\n"; + if (auto Err = sys::fs::status(File, Stat)) { + warn(Err.message()); continue; } if (!Options.NoTimestamp && Stat.getLastModificationTime() != sys::TimePoint<>(Obj->getTimestamp())) { - errs() << "Warning: Timestamp mismatch for " << File << ": " - << Stat.getLastModificationTime() << " and " - << sys::TimePoint<>(Obj->getTimestamp()) << "\n"; + // Not using the helper here as we can easily stream TimePoint<>. + warn_ostream() << "Timestamp mismatch for " << File << ": " + << Stat.getLastModificationTime() << " and " + << sys::TimePoint<>(Obj->getTimestamp()) << "\n"; continue; } diff --git a/llvm/tools/dsymutil/dsymutil.h b/llvm/tools/dsymutil/dsymutil.h index 709a480..4d7ea47 100644 --- a/llvm/tools/dsymutil/dsymutil.h +++ b/llvm/tools/dsymutil/dsymutil.h @@ -70,8 +70,8 @@ bool dumpStab(StringRef InputFile, ArrayRef Archs, bool linkDwarf(raw_fd_ostream &OutFile, const DebugMap &DM, const LinkOptions &Options); -void warn(const Twine &Warning, const Twine &Context); -bool error(const Twine &Error, const Twine &Context); +void warn(Twine Warning, Twine Context = {}); +bool error(Twine Error, Twine Context = {}); } // end namespace dsymutil } // end namespace llvm -- 2.7.4