blob: 6b70cfe98c3d0b1bc492a76441d761ffe857fc42 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
zonr6315f762010-10-05 15:35:14 +080017#include "slang_diagnostic_buffer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Stephen Hinese639eb52010-11-08 19:27:20 -080019#include "clang/Basic/SourceLocation.h"
20#include "clang/Basic/SourceManager.h"
21
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include "llvm/ADT/SmallString.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070023
Stephen Hines6e6578a2011-02-07 18:05:48 -080024#include "slang_assert.h"
25
Stephen Hinese639eb52010-11-08 19:27:20 -080026namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070027
28DiagnosticBuffer::DiagnosticBuffer() : mSOS(NULL) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070029 mSOS = new llvm::raw_string_ostream(mDiags);
30 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070031}
32
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070033void DiagnosticBuffer::HandleDiagnostic(clang::Diagnostic::Level DiagLevel,
34 const clang::DiagnosticInfo &Info) {
Loganbe274822011-02-16 22:02:54 +080035 const clang::SourceLocation &SrcLoc = Info.getLocation();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070036 // 100 is enough for storing general diagnosis message
37 llvm::SmallString<100> Buf;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070038
Loganbe274822011-02-16 22:02:54 +080039 if (SrcLoc.isValid()) {
40 SrcLoc.print(*mSOS, Info.getSourceManager());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070041 (*mSOS) << ": ";
42 }
43
44 switch (DiagLevel) {
45 case clang::Diagnostic::Note: {
46 (*mSOS) << "note: ";
47 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 case clang::Diagnostic::Warning: {
50 (*mSOS) << "warning: ";
51 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053 case clang::Diagnostic::Error: {
54 (*mSOS) << "error: ";
55 break;
56 }
57 case clang::Diagnostic::Fatal: {
58 (*mSOS) << "fatal: ";
59 break;
60 }
61 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080062 slangAssert(0 && "Diagnostic not handled during diagnostic buffering!");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070063 }
64 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070065
66
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 Info.FormatDiagnostic(Buf);
68 (*mSOS) << Buf.str() << '\n';
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071}
72
73DiagnosticBuffer::~DiagnosticBuffer() {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080074 delete mSOS;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070076}
Stephen Hinese639eb52010-11-08 19:27:20 -080077
78} // namespace slang