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 clang::SourceLocation const &SrcLoc = Info.getLocation(); 45 46 // 100 is enough for storing general diagnosis message 47 llvm::SmallString<100> Buf; 48 49 if (SrcLoc.isValid()) { 50 SrcLoc.print(*mSOS, Info.getSourceManager()); 51 (*mSOS) << ": "; 52 } 53 54 switch (DiagLevel) { 55 case clang::DiagnosticsEngine::Note: { 56 (*mSOS) << "note: "; 57 break; 58 } 59 case clang::DiagnosticsEngine::Warning: { 60 (*mSOS) << "warning: "; 61 break; 62 } 63 case clang::DiagnosticsEngine::Error: { 64 (*mSOS) << "error: "; 65 break; 66 } 67 case clang::DiagnosticsEngine::Fatal: { 68 (*mSOS) << "fatal: "; 69 break; 70 } 71 default: { 72 slangAssert(0 && "Diagnostic not handled during diagnostic buffering!"); 73 } 74 } 75 76 Info.FormatDiagnostic(Buf); 77 (*mSOS) << Buf.str() << '\n'; 78} 79 80clang::DiagnosticConsumer * 81DiagnosticBuffer::clone(clang::DiagnosticsEngine &Diags) const { 82 return new DiagnosticBuffer(*this); 83} 84 85} // namespace slang 86