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 17#include "slang_diagnostic_buffer.h" 18 19#include "clang/Basic/SourceLocation.h" 20#include "clang/Basic/SourceManager.h" 21 22#include "llvm/ADT/SmallString.h" 23 24#include "slang_assert.h" 25 26namespace slang { 27 28DiagnosticBuffer::DiagnosticBuffer() 29 : mSOS(new llvm::raw_string_ostream(mDiags)) { 30} 31 32DiagnosticBuffer::DiagnosticBuffer(DiagnosticBuffer const &src) 33 : clang::DiagnosticConsumer(src), 34 mDiags(src.mDiags), 35 mSOS(new llvm::raw_string_ostream(mDiags)) { 36} 37 38DiagnosticBuffer::~DiagnosticBuffer() { 39} 40 41void DiagnosticBuffer::HandleDiagnostic( 42 clang::DiagnosticsEngine::Level DiagLevel, 43 clang::Diagnostic const &Info) { 44 45 clang::SourceLocation const &SrcLoc = Info.getLocation(); 46 47 // 100 is enough for storing general diagnosis message 48 llvm::SmallString<100> Buf; 49 50 if (SrcLoc.isValid()) { 51 SrcLoc.print(*mSOS, Info.getSourceManager()); 52 (*mSOS) << ": "; 53 } 54 55 switch (DiagLevel) { 56 case clang::DiagnosticsEngine::Note: { 57 (*mSOS) << "note: "; 58 break; 59 } 60 case clang::DiagnosticsEngine::Warning: { 61 (*mSOS) << "warning: "; 62 break; 63 } 64 case clang::DiagnosticsEngine::Error: { 65 (*mSOS) << "error: "; 66 break; 67 } 68 case clang::DiagnosticsEngine::Fatal: { 69 (*mSOS) << "fatal: "; 70 break; 71 } 72 default: { 73 slangAssert(0 && "Diagnostic not handled during diagnostic buffering!"); 74 } 75 } 76 77 Info.FormatDiagnostic(Buf); 78 (*mSOS) << Buf.str() << '\n'; 79} 80 81clang::DiagnosticConsumer * 82DiagnosticBuffer::clone(clang::DiagnosticsEngine &Diags) const { 83 return new DiagnosticBuffer(*this); 84} 85 86} // namespace slang 87