blob: 87a37f97273da33eefaac6743ad3afe1ffaabd12 [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 Hinese639eb52010-11-08 19:27:20 -080024namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070025
26DiagnosticBuffer::DiagnosticBuffer() : mSOS(NULL) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070027 mSOS = new llvm::raw_string_ostream(mDiags);
28 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070029}
30
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070031void DiagnosticBuffer::HandleDiagnostic(clang::Diagnostic::Level DiagLevel,
32 const clang::DiagnosticInfo &Info) {
33 const clang::FullSourceLoc &FSLoc = Info.getLocation();
34 // 100 is enough for storing general diagnosis message
35 llvm::SmallString<100> Buf;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070037 if (FSLoc.isValid()) {
38 FSLoc.print(*mSOS, FSLoc.getManager());
39 (*mSOS) << ": ";
40 }
41
42 switch (DiagLevel) {
43 case clang::Diagnostic::Note: {
44 (*mSOS) << "note: ";
45 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047 case clang::Diagnostic::Warning: {
48 (*mSOS) << "warning: ";
49 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 case clang::Diagnostic::Error: {
52 (*mSOS) << "error: ";
53 break;
54 }
55 case clang::Diagnostic::Fatal: {
56 (*mSOS) << "fatal: ";
57 break;
58 }
59 default: {
60 assert(0 && "Diagnostic not handled during diagnostic buffering!");
61 }
62 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070063
64
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 Info.FormatDiagnostic(Buf);
66 (*mSOS) << Buf.str() << '\n';
Shih-wei Liao462aefd2010-06-04 15:32:04 -070067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069}
70
71DiagnosticBuffer::~DiagnosticBuffer() {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080072 delete mSOS;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074}
Stephen Hinese639eb52010-11-08 19:27:20 -080075
76} // namespace slang