00001 //===-- basic/Diagnostic.h ------------------------------------ -*- C++ -*-===// 00002 // 00003 // This file is distributed under the MIT license. See LICENSE.txt for details. 00004 // 00005 // Copyright (C) 2008, Stephen Wilson 00006 // 00007 //===----------------------------------------------------------------------===// 00008 00009 #ifndef COMMA_BASIC_DIAGNOSTIC_HDR_GUARD 00010 #define COMMA_BASIC_DIAGNOSTIC_HDR_GUARD 00011 00012 #include "comma/basic/Location.h" 00013 #include <iostream> 00014 #include <sstream> 00015 00016 namespace comma { 00017 00018 namespace diag { 00019 00020 enum Kind { 00021 #define DIAGNOSTIC(ENUM, KIND, FORMAT) ENUM, 00022 #include "comma/basic/Diagnostic.def" 00023 #undef DIAGNOSTIC 00024 00025 LAST_UNUSED_DIAGNOSTIC_KIND 00026 }; 00027 00028 } // End diag namespace. 00029 00030 00031 class DiagnosticStream { 00032 00033 public: 00034 DiagnosticStream(std::ostream &stream); 00035 00036 DiagnosticStream &initialize(const SourceLocation &loc, const char *format); 00037 00038 DiagnosticStream &operator<<(const std::string &string); 00039 00040 DiagnosticStream &operator<<(const char *string); 00041 00042 DiagnosticStream &operator<<(int n); 00043 00044 DiagnosticStream &operator<<(char c); 00045 00046 private: 00047 void emitFormatComponent(); 00048 00049 std::ostream &stream; 00050 unsigned position; 00051 std::ostringstream message; 00052 const char *format; 00053 }; 00054 00055 class Diagnostic { 00056 00057 public: 00058 // Creates a diagnostic object with the reporting stream defaulting to 00059 // std::cerr; 00060 Diagnostic() : diagstream(std::cerr) { } 00061 00062 // Creates a diagnostic object with the given output stream serving as the 00063 // default stream to which messages are delivered. 00064 Diagnostic(std::ostream &stream) : diagstream(stream) { } 00065 00066 DiagnosticStream &report(const SourceLocation &loc, diag::Kind kind); 00067 00068 const char *getDiagnosticFormat(diag::Kind); 00069 00070 private: 00071 DiagnosticStream diagstream; 00072 00073 static void initializeMessages(); 00074 00075 static const char *messages[diag::LAST_UNUSED_DIAGNOSTIC_KIND]; 00076 }; 00077 00078 } // End comma namespace. 00079 00080 #endif