• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  //===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
2  //
3  //                     The LLVM Compiler Infrastructure
4  //
5  // This file is distributed under the University of Illinois Open Source
6  // License. See LICENSE.TXT for details.
7  //
8  //===----------------------------------------------------------------------===//
9  //
10  // This is a concrete diagnostic client, which buffers the diagnostic messages.
11  //
12  //===----------------------------------------------------------------------===//
13  
14  #include "clang/Frontend/TextDiagnosticBuffer.h"
15  #include "llvm/ADT/SmallString.h"
16  #include "llvm/Support/ErrorHandling.h"
17  using namespace clang;
18  
19  /// HandleDiagnostic - Store the errors, warnings, and notes that are
20  /// reported.
21  ///
HandleDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)22  void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
23                                              const Diagnostic &Info) {
24    // Default implementation (Warnings/errors count).
25    DiagnosticConsumer::HandleDiagnostic(Level, Info);
26  
27    SmallString<100> Buf;
28    Info.FormatDiagnostic(Buf);
29    switch (Level) {
30    default: llvm_unreachable(
31                           "Diagnostic not handled during diagnostic buffering!");
32    case DiagnosticsEngine::Note:
33      Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
34      break;
35    case DiagnosticsEngine::Warning:
36      Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
37      break;
38    case DiagnosticsEngine::Error:
39    case DiagnosticsEngine::Fatal:
40      Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
41      break;
42    }
43  }
44  
45  /// \brief Escape diagnostic texts to avoid problems when they are fed into the
46  /// diagnostic formatter a second time.
escapeDiag(StringRef Str,SmallVectorImpl<char> & Buf)47  static StringRef escapeDiag(StringRef Str, SmallVectorImpl<char> &Buf) {
48    size_t Pos = Str.find('%');
49    if (Pos == StringRef::npos)
50      return Str;
51  
52    // We found a '%'. Replace this and all following '%' with '%%'.
53    Buf.clear();
54    Buf.append(Str.data(), Str.data() + Pos);
55    for (size_t I = Pos, E = Str.size(); I != E; ++I) {
56      if (Str[I] == '%')
57        Buf.push_back('%');
58      Buf.push_back(Str[I]);
59    }
60  
61    return StringRef(Buf.data(), Buf.size());
62  }
63  
FlushDiagnostics(DiagnosticsEngine & Diags) const64  void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
65    SmallVector<char, 64> Buf;
66    // FIXME: Flush the diagnostics in order.
67    for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
68      Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error,
69                                         escapeDiag(it->second, Buf)));
70    for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
71      Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning,
72                                         escapeDiag(it->second, Buf)));
73    for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
74      Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
75                                         escapeDiag(it->second, Buf)));
76  }
77  
clone(DiagnosticsEngine &) const78  DiagnosticConsumer *TextDiagnosticBuffer::clone(DiagnosticsEngine &) const {
79    return new TextDiagnosticBuffer();
80  }
81