Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_diagnostic_buffer.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include "clang/Basic/SourceLocation.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 23 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 24 | #include "slang_assert.h" |
| 25 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 26 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 28 | DiagnosticBuffer::DiagnosticBuffer() |
Logan Chien | c86623e | 2011-10-22 18:22:25 +0800 | [diff] [blame] | 29 | : mSOS(new llvm::raw_string_ostream(mDiags)) { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 32 | DiagnosticBuffer::DiagnosticBuffer(DiagnosticBuffer const &src) |
Logan Chien | c86623e | 2011-10-22 18:22:25 +0800 | [diff] [blame] | 33 | : clang::DiagnosticConsumer(src), |
| 34 | mDiags(src.mDiags), |
| 35 | mSOS(new llvm::raw_string_ostream(mDiags)) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DiagnosticBuffer::~DiagnosticBuffer() { |
| 39 | } |
| 40 | |
| 41 | void DiagnosticBuffer::HandleDiagnostic( |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 42 | clang::DiagnosticsEngine::Level DiagLevel, |
| 43 | clang::Diagnostic const &Info) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 44 | clang::SourceLocation const &SrcLoc = Info.getLocation(); |
| 45 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 46 | // 100 is enough for storing general diagnosis message |
| 47 | llvm::SmallString<100> Buf; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 48 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 49 | if (SrcLoc.isValid()) { |
| 50 | SrcLoc.print(*mSOS, Info.getSourceManager()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 51 | (*mSOS) << ": "; |
| 52 | } |
| 53 | |
| 54 | switch (DiagLevel) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 55 | case clang::DiagnosticsEngine::Note: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | (*mSOS) << "note: "; |
| 57 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 58 | } |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 59 | case clang::DiagnosticsEngine::Warning: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 60 | (*mSOS) << "warning: "; |
| 61 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 62 | } |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 63 | case clang::DiagnosticsEngine::Error: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 64 | (*mSOS) << "error: "; |
| 65 | break; |
| 66 | } |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 67 | case clang::DiagnosticsEngine::Fatal: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 68 | (*mSOS) << "fatal: "; |
| 69 | break; |
| 70 | } |
| 71 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 72 | slangAssert(0 && "Diagnostic not handled during diagnostic buffering!"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 75 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 76 | Info.FormatDiagnostic(Buf); |
| 77 | (*mSOS) << Buf.str() << '\n'; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 80 | clang::DiagnosticConsumer * |
| 81 | DiagnosticBuffer::clone(clang::DiagnosticsEngine &Diags) const { |
| 82 | return new DiagnosticBuffer(*this); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 84 | |
| 85 | } // namespace slang |