blob: 5821436a306fe7398ed440d9b7430903ac20bc98 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is a concrete diagnostic client, which buffers the diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticBuffer.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000015#include "llvm/ADT/SmallString.h"
David Blaikie548f6c82011-09-23 05:57:42 +000016#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Douglas Gregor94b1dd22008-10-24 04:54:22 +000019/// HandleDiagnostic - Store the errors, warnings, and notes that are
20/// reported.
Mike Stump1eb44332009-09-09 15:08:12 +000021///
David Blaikied6471f72011-09-25 23:23:43 +000022void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +000023 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +000024 // Default implementation (Warnings/errors count).
David Blaikie78ad0b92011-09-25 23:39:51 +000025 DiagnosticConsumer::HandleDiagnostic(Level, Info);
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +000026
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000027 SmallString<100> Buf;
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000028 Info.FormatDiagnostic(Buf);
Reid Spencer5f016e22007-07-11 17:01:13 +000029 switch (Level) {
David Blaikieb219cfc2011-09-23 05:06:16 +000030 default: llvm_unreachable(
31 "Diagnostic not handled during diagnostic buffering!");
David Blaikied6471f72011-09-25 23:23:43 +000032 case DiagnosticsEngine::Note:
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000033 Notes.push_back(std::make_pair(Info.getLocation(), Buf.str()));
Douglas Gregor233f74b2008-09-11 02:46:36 +000034 break;
David Blaikied6471f72011-09-25 23:23:43 +000035 case DiagnosticsEngine::Warning:
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000036 Warnings.push_back(std::make_pair(Info.getLocation(), Buf.str()));
Reid Spencer5f016e22007-07-11 17:01:13 +000037 break;
David Blaikied6471f72011-09-25 23:23:43 +000038 case DiagnosticsEngine::Error:
39 case DiagnosticsEngine::Fatal:
Daniel Dunbar4cbe3b62009-11-29 20:58:39 +000040 Errors.push_back(std::make_pair(Info.getLocation(), Buf.str()));
Reid Spencer5f016e22007-07-11 17:01:13 +000041 break;
42 }
43}
Daniel Dunbarc2389552009-11-30 08:41:34 +000044
Benjamin Kramer0f584642012-12-08 12:42:30 +000045/// \brief Escape diagnostic texts to avoid problems when they are fed into the
46/// diagnostic formatter a second time.
47static 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
David Blaikied6471f72011-09-25 23:23:43 +000064void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const {
Benjamin Kramer0f584642012-12-08 12:42:30 +000065 SmallVector<char, 64> Buf;
Daniel Dunbarc2389552009-11-30 08:41:34 +000066 // FIXME: Flush the diagnostics in order.
67 for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it)
David Blaikied6471f72011-09-25 23:23:43 +000068 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error,
Benjamin Kramer0f584642012-12-08 12:42:30 +000069 escapeDiag(it->second, Buf)));
Daniel Dunbarc2389552009-11-30 08:41:34 +000070 for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
David Blaikied6471f72011-09-25 23:23:43 +000071 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning,
Benjamin Kramer0f584642012-12-08 12:42:30 +000072 escapeDiag(it->second, Buf)));
Daniel Dunbarc2389552009-11-30 08:41:34 +000073 for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
David Blaikied6471f72011-09-25 23:23:43 +000074 Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
Benjamin Kramer0f584642012-12-08 12:42:30 +000075 escapeDiag(it->second, Buf)));
Daniel Dunbarc2389552009-11-30 08:41:34 +000076}
Douglas Gregoraee526e2011-09-29 00:38:00 +000077