blob: 87574460cc6eb0f75f46084471201638bf857ad7 [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#include "slang_diagnostic_buffer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07003#include "llvm/ADT/SmallString.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07004
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07005#include "clang/Basic/SourceManager.h"
6#include "clang/Basic/SourceLocation.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07007
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07008using namespace slang;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07009
10DiagnosticBuffer::DiagnosticBuffer() : mSOS(NULL) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070011 mSOS = new llvm::raw_string_ostream(mDiags);
12 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070013}
14
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070015void DiagnosticBuffer::HandleDiagnostic(clang::Diagnostic::Level DiagLevel,
16 const clang::DiagnosticInfo &Info) {
17 const clang::FullSourceLoc &FSLoc = Info.getLocation();
18 // 100 is enough for storing general diagnosis message
19 llvm::SmallString<100> Buf;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070021 if (FSLoc.isValid()) {
22 FSLoc.print(*mSOS, FSLoc.getManager());
23 (*mSOS) << ": ";
24 }
25
26 switch (DiagLevel) {
27 case clang::Diagnostic::Note: {
28 (*mSOS) << "note: ";
29 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070031 case clang::Diagnostic::Warning: {
32 (*mSOS) << "warning: ";
33 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035 case clang::Diagnostic::Error: {
36 (*mSOS) << "error: ";
37 break;
38 }
39 case clang::Diagnostic::Fatal: {
40 (*mSOS) << "fatal: ";
41 break;
42 }
43 default: {
44 assert(0 && "Diagnostic not handled during diagnostic buffering!");
45 }
46 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070047
48
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 Info.FormatDiagnostic(Buf);
50 (*mSOS) << Buf.str() << '\n';
Shih-wei Liao462aefd2010-06-04 15:32:04 -070051
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053}
54
55DiagnosticBuffer::~DiagnosticBuffer() {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080056 delete mSOS;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058}