Home | History | Annotate | Download | only in slang
      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 
     26 namespace slang {
     27 
     28 DiagnosticBuffer::DiagnosticBuffer()
     29   : mSOS(new llvm::raw_string_ostream(mDiags)) {
     30 }
     31 
     32 DiagnosticBuffer::~DiagnosticBuffer() {
     33 }
     34 
     35 void DiagnosticBuffer::HandleDiagnostic(
     36     clang::DiagnosticsEngine::Level DiagLevel,
     37     clang::Diagnostic const &Info) {
     38   clang::SourceLocation const &SrcLoc = Info.getLocation();
     39 
     40   std::string Message;
     41   llvm::raw_string_ostream stream(Message);
     42 
     43   if (SrcLoc.isValid()) {
     44     SrcLoc.print(stream, Info.getSourceManager());
     45     stream << ": ";
     46   }
     47 
     48   switch (DiagLevel) {
     49     case clang::DiagnosticsEngine::Note: {
     50       stream << "note: ";
     51       break;
     52     }
     53     case clang::DiagnosticsEngine::Warning: {
     54       stream << "warning: ";
     55       break;
     56     }
     57     case clang::DiagnosticsEngine::Error: {
     58       stream << "error: ";
     59       break;
     60     }
     61     case clang::DiagnosticsEngine::Fatal: {
     62       stream << "fatal: ";
     63       break;
     64     }
     65     default: {
     66       slangAssert(0 && "Diagnostic not handled during diagnostic buffering!");
     67     }
     68   }
     69   // 100 is enough for storing general diagnosis Message
     70   llvm::SmallString<100> Buf;
     71   Info.FormatDiagnostic(Buf);
     72   stream << Buf.str() << '\n';
     73   stream.flush();
     74 
     75   if (mIncludedMessages.find(Message) == mIncludedMessages.end()) {
     76     mIncludedMessages.insert(Message);
     77     (*mSOS) << Message;
     78   }
     79 }
     80 
     81 }  // namespace slang
     82