From c9b47f9ba822e69349dffe2b7c1c694a57e279fa Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Fri, 16 Mar 2012 22:31:42 +0000 Subject: [PATCH] Escape % in diagnostic message when compiling LLVM IR. % is a common character in IR so we'd crash on almost any malformed IR. The diagnostic formatter expects a formatting directive when it sees an unescaped %. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152956 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenAction.cpp | 12 +++++++++++- test/Frontend/ir-support-errors.ll | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp index 6a184e0ef9..dd32167b84 100644 --- a/lib/CodeGen/CodeGenAction.cpp +++ b/lib/CodeGen/CodeGenAction.cpp @@ -23,6 +23,7 @@ #include "llvm/Module.h" #include "llvm/Pass.h" #include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/SmallString.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Support/IRReader.h" #include "llvm/Support/MemoryBuffer.h" @@ -393,8 +394,17 @@ void CodeGenAction::ExecuteAction() { StringRef Msg = Err.getMessage(); if (Msg.startswith("error: ")) Msg = Msg.substr(7); + + // Escape '%', which is interpreted as a format character. + llvm::SmallString<128> EscapedMessage; + for (unsigned i = 0, e = Msg.size(); i != e; ++i) { + if (Msg[i] == '%') + EscapedMessage += '%'; + EscapedMessage += Msg[i]; + } + unsigned DiagID = CI.getDiagnostics().getCustomDiagID( - DiagnosticsEngine::Error, Msg); + DiagnosticsEngine::Error, EscapedMessage); CI.getDiagnostics().Report(Loc, DiagID); return; diff --git a/test/Frontend/ir-support-errors.ll b/test/Frontend/ir-support-errors.ll index 98227d46f7..cb5913cd3a 100644 --- a/test/Frontend/ir-support-errors.ll +++ b/test/Frontend/ir-support-errors.ll @@ -3,6 +3,6 @@ target triple = "x86_64-apple-darwin10" define i32 @f0() nounwind ssp { -; CHECK: {{.*}}ir-support-errors.ll:7:16: error: expected value token - ret i32 x +; CHECK: {{.*}}ir-support-errors.ll:7:16: error: use of undefined value '%x' + ret i32 %x } -- 2.40.0