diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2012-12-08 12:42:30 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2012-12-08 12:42:30 +0000 |
commit | 0f584640dce8f9d10e802b1e323a594f7850a465 (patch) | |
tree | 35d18fa8f15af0be311fad2365851f775f9c72fc | |
parent | 6e399b42b5356d8ee59eb5dc4da8c89a0d0b7ae1 (diff) |
Escape % in the TextDiagnosticBuffer so they aren't interpreted twice when fed into the diagnostic formatting machinery again.
Fixes PR14543.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169677 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Frontend/TextDiagnosticBuffer.cpp | 26 | ||||
-rw-r--r-- | test/Driver/unknown-arg.c | 4 |
2 files changed, 26 insertions, 4 deletions
diff --git a/lib/Frontend/TextDiagnosticBuffer.cpp b/lib/Frontend/TextDiagnosticBuffer.cpp index 57105f15a3..039475a2e0 100644 --- a/lib/Frontend/TextDiagnosticBuffer.cpp +++ b/lib/Frontend/TextDiagnosticBuffer.cpp @@ -42,17 +42,37 @@ void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level, } } +/// \brief Escape diagnostic texts to avoid problems when they are fed into the +/// diagnostic formatter a second time. +static StringRef escapeDiag(StringRef Str, SmallVectorImpl<char> &Buf) { + size_t Pos = Str.find('%'); + if (Pos == StringRef::npos) + return Str; + + // We found a '%'. Replace this and all following '%' with '%%'. + Buf.clear(); + Buf.append(Str.data(), Str.data() + Pos); + for (size_t I = Pos, E = Str.size(); I != E; ++I) { + if (Str[I] == '%') + Buf.push_back('%'); + Buf.push_back(Str[I]); + } + + return StringRef(Buf.data(), Buf.size()); +} + void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const { + SmallVector<char, 64> Buf; // FIXME: Flush the diagnostics in order. for (const_iterator it = err_begin(), ie = err_end(); it != ie; ++it) Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, - it->second.c_str())); + escapeDiag(it->second, Buf))); for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it) Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning, - it->second.c_str())); + escapeDiag(it->second, Buf))); for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it) Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note, - it->second.c_str())); + escapeDiag(it->second, Buf))); } DiagnosticConsumer *TextDiagnosticBuffer::clone(DiagnosticsEngine &) const { diff --git a/test/Driver/unknown-arg.c b/test/Driver/unknown-arg.c index 5d0f7afc51..0fab8a50b9 100644 --- a/test/Driver/unknown-arg.c +++ b/test/Driver/unknown-arg.c @@ -1,4 +1,6 @@ -// RUN: not %clang_cc1 %s -cake-is-lie 2> %t.log +// RUN: not %clang_cc1 %s -cake-is-lie -%0 -%d 2> %t.log // RUN: FileCheck %s -input-file=%t.log // CHECK: unknown argument +// CHECK: unknown argument +// CHECK: unknown argument |