From b0524a0b18eec9d5e5c3f6ce30b6cecdd02a6306 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 20 Jan 2015 17:18:09 +0200 Subject: Diagnostic infrastructure revamp --- build/diagnostics | 310 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 282 insertions(+), 28 deletions(-) (limited to 'build/diagnostics') diff --git a/build/diagnostics b/build/diagnostics index 98f481f..f85d118 100644 --- a/build/diagnostics +++ b/build/diagnostics @@ -5,20 +5,17 @@ #ifndef BUILD_DIAGNOSTICS #define BUILD_DIAGNOSTICS -#include #include +#include #include +#include +#include +#include #include - -#include +#include namespace build { - // Throw this exception to terminate the build. The handler should - // assume that the diagnostics has already been issued. - // - class error: public std::exception {}; - // Print process commmand line. // void @@ -30,39 +27,296 @@ namespace build print_process (args.data ()); } - // Call a function if there is an exception. + // Throw this exception to terminate the build. The handler should + // assume that the diagnostics has already been issued. + // + class failed: public std::exception {}; + + // Trace verbosity level. // - template - struct exception_guard; + // 1 - command lines to update explicit targets (e.g., .o) + // 2 - command lines to update implicit targets (e.g., .d) + // 3 - things didn't work out (e.g., rule did not match) + // 4 - additional information + // 5 - more additional information + // + extern std::uint8_t verb; + + template inline void level1 (const F& f) {if (verb >= 1) f ();} + template inline void level2 (const F& f) {if (verb >= 2) f ();} + template inline void level3 (const F& f) {if (verb >= 3) f ();} + template inline void level4 (const F& f) {if (verb >= 4) f ();} + template inline void level5 (const F& f) {if (verb >= 5) f ();} + + // Diagnostic facility, base infrastructure (potentially reusable). + // + extern std::ostream* diag_stream; + + template struct diag_prologue; + template struct diag_mark; - template - inline exception_guard> - make_exception_guard (F f, A&&... a) + struct diag_record; + + typedef void (*diag_epilogue) (const diag_record&); + + struct diag_record { - return exception_guard> ( - std::move (f), std::forward_as_tuple (a...)); - } + template + friend const diag_record& + operator<< (const diag_record& r, const T& x) + { + r.os_ << x; + return r; + } + + diag_record (): empty_ (true), epilogue_ (nullptr) {} + + template + explicit + diag_record (const diag_prologue& p) + : empty_ (true), epilogue_ (nullptr) { *this << p;} + + template + explicit + diag_record (const diag_mark& m) + : empty_ (true), epilogue_ (nullptr) { *this << m;} + + ~diag_record () noexcept(false); + + void + append (diag_epilogue e) const + { + if (e != nullptr) + { + assert (epilogue_ == nullptr); // No multiple epilogues support. + epilogue_ = e; + } + + if (empty_) + empty_ = false; + else + os_ << "\n "; + } + + // Move constructible-only type. + // + /* + @@ libstdc++ doesn't yet have the ostringstream move support. + + diag_record (diag_record&& r) + : os_ (std::move (r.os_)) + { + empty_ = r.empty_; + r.empty_ = true; + + epilogue_ = r.epilogue_; + r.epilogue_ = nullptr; + } + */ + + diag_record (diag_record&& r) + { + empty_ = r.empty_; + epilogue_ = r.epilogue_; + + if (!empty_) + { + os_ << r.os_.str (); + + r.empty_ = true; + r.epilogue_ = nullptr; + } + } + + diag_record& operator= (diag_record&&) = delete; + + diag_record (const diag_record&) = delete; + diag_record& operator= (const diag_record&) = delete; + + private: + mutable bool empty_; + mutable std::ostringstream os_; + mutable diag_epilogue epilogue_; + }; + + template + struct diag_prologue: B + { + diag_prologue (diag_epilogue e = nullptr): B (), epilogue_ (e) {} + + template + diag_prologue (A&&... a) + : B (std::forward (a)...), epilogue_ (nullptr) {} + + template + diag_prologue (diag_epilogue e, A&&... a) + : B (std::forward (a)...), epilogue_ (e) {} + + template + diag_record + operator<< (const T& x) const + { + diag_record r; + r.append (epilogue_); + B::operator() (r); + r << x; + return r; + } + + friend const diag_record& + operator<< (const diag_record& r, const diag_prologue& p) + { + r.append (p.epilogue_); + p (r); + return r; + } + + private: + diag_epilogue epilogue_; + }; - template - struct exception_guard> + template + struct diag_mark: B { - typedef std::tuple T; + diag_mark (): B () {} - exception_guard (F f, T a): f_ (std::move (f)), a_ (std::move (a)) {} - ~exception_guard () + template + diag_mark (A&&... a): B (std::forward (a)...) {} + + template + diag_record + operator<< (const T& x) const { - if (std::uncaught_exception ()) - call (std::index_sequence_for ()); + return B::operator() () << x; } + friend const diag_record& + operator<< (const diag_record& r, const diag_mark& m) + { + return r << m (); + } + }; + + // Diagnostic facility, project specifics. + // + struct simple_prologue_base + { + explicit + simple_prologue_base (const char* type, const char* name) + : type_ (type), name_ (name) {} + + void + operator() (const diag_record& r) const; + private: - template + const char* type_; + const char* name_; + }; + typedef diag_prologue simple_prologue; + + struct location + { + location () {} + location (const char* f, std::uint64_t l, std::uint64_t c) + : file (f), line (l), column (c) {} + + const char* file; + std::uint64_t line; + std::uint64_t column; + }; + + struct location_prologue_base + { + location_prologue_base (const char* type, + const char* name, + const location& l) + : type_ (type), name_ (name), loc_ (l) {} + void - call (std::index_sequence) {f_ (std::get (a_)...);} + operator() (const diag_record& r) const; + + private: + const char* type_; + const char* name_; + const location& loc_; + }; + typedef diag_prologue location_prologue; + + struct basic_mark_base + { + explicit + basic_mark_base (const char* type, const char* name = nullptr) + : type_ (type), name_ (name) {} + + simple_prologue + operator() () const + { + return simple_prologue (type_, name_); + } + + location_prologue + operator() (const location& l) const + { + return location_prologue (type_, name_, l); + } + + template + location_prologue + operator() (const L& l) const + { + return location_prologue (type_, name_, get_location (l)); + } - F f_; - T a_; + private: + const char* type_; + const char* name_; }; + typedef diag_mark basic_mark; + + extern const basic_mark error; + extern const basic_mark warn; + extern const basic_mark info; + extern const basic_mark text; + + struct trace_mark_base: basic_mark_base + { + explicit + trace_mark_base (const char* name): basic_mark_base ("trace", name) {} + }; + typedef diag_mark trace_mark; + + typedef trace_mark tracer; + + template + struct fail_mark_base + { + simple_prologue + operator() () const + { + return simple_prologue (&epilogue, "error", nullptr); + } + + location_prologue + operator() (const location& l) const + { + return location_prologue (&epilogue, "error", nullptr, l); + } + + template + location_prologue + operator() (const L& l) const + { + return location_prologue (&epilogue, "error", nullptr, get_location (l)); + } + + static void + epilogue (const diag_record&) {throw E ();} + }; + + template + using fail_mark = diag_mark>; + + extern const fail_mark fail; } #endif // BUILD_DIAGNOSTICS -- cgit v1.1