blob: 5b7adaf42e7085adf7db1f859a618411d2584a19 [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
Logan Chien9207a2e2011-10-21 15:39:28 +080028DiagnosticBuffer::DiagnosticBuffer()
Logan Chienc86623e2011-10-22 18:22:25 +080029 : mSOS(new llvm::raw_string_ostream(mDiags)) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030}
31
Logan Chien9207a2e2011-10-21 15:39:28 +080032DiagnosticBuffer::DiagnosticBuffer(DiagnosticBuffer const &src)
Logan Chienc86623e2011-10-22 18:22:25 +080033 : clang::DiagnosticConsumer(src),
34 mDiags(src.mDiags),
35 mSOS(new llvm::raw_string_ostream(mDiags)) {
Logan Chien9207a2e2011-10-21 15:39:28 +080036}
37
38DiagnosticBuffer::~DiagnosticBuffer() {
39}
40
41void DiagnosticBuffer::HandleDiagnostic(
Stephen Hinese67239d2012-02-24 15:08:36 -080042 clang::DiagnosticsEngine::Level DiagLevel,
43 clang::Diagnostic const &Info) {
Logan Chien9207a2e2011-10-21 15:39:28 +080044 clang::SourceLocation const &SrcLoc = Info.getLocation();
45
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070046 // 100 is enough for storing general diagnosis message
47 llvm::SmallString<100> Buf;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
Loganbe274822011-02-16 22:02:54 +080049 if (SrcLoc.isValid()) {
50 SrcLoc.print(*mSOS, Info.getSourceManager());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 (*mSOS) << ": ";
52 }
53
54 switch (DiagLevel) {
Logan Chien9207a2e2011-10-21 15:39:28 +080055 case clang::DiagnosticsEngine::Note: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 (*mSOS) << "note: ";
57 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058 }
Logan Chien9207a2e2011-10-21 15:39:28 +080059 case clang::DiagnosticsEngine::Warning: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 (*mSOS) << "warning: ";
61 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070062 }
Logan Chien9207a2e2011-10-21 15:39:28 +080063 case clang::DiagnosticsEngine::Error: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 (*mSOS) << "error: ";
65 break;
66 }
Logan Chien9207a2e2011-10-21 15:39:28 +080067 case clang::DiagnosticsEngine::Fatal: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 (*mSOS) << "fatal: ";
69 break;
70 }
71 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080072 slangAssert(0 && "Diagnostic not handled during diagnostic buffering!");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 }
74 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 Info.FormatDiagnostic(Buf);
77 (*mSOS) << Buf.str() << '\n';
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078}
79
Logan Chien9207a2e2011-10-21 15:39:28 +080080clang::DiagnosticConsumer *
81DiagnosticBuffer::clone(clang::DiagnosticsEngine &Diags) const {
82 return new DiagnosticBuffer(*this);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070083}
Stephen Hinese639eb52010-11-08 19:27:20 -080084
85} // namespace slang