Fix getCustomDiagID() usage in CodeGen and TextDiagnosticBuffer
DiagIDs are a cached resource generally only constructed from compile-time constant or stable format strings. Escaping arbitrary messages and constructing DiagIDs from them didn't make sense. llvm-svn: 197856
This commit is contained in:
parent
9477f9e52f
commit
bc043f27f4
|
|
@ -399,24 +399,15 @@ void CodeGenAction::ExecuteAction() {
|
|||
SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
|
||||
Err.getColumnNo() + 1);
|
||||
|
||||
// Get a custom diagnostic for the error. We strip off a leading
|
||||
// diagnostic code if there is one.
|
||||
// Strip off a leading diagnostic code if there is one.
|
||||
StringRef Msg = Err.getMessage();
|
||||
if (Msg.startswith("error: "))
|
||||
Msg = Msg.substr(7);
|
||||
|
||||
// Escape '%', which is interpreted as a format character.
|
||||
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, "%0");
|
||||
|
||||
unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
|
||||
DiagnosticsEngine::Error, EscapedMessage);
|
||||
|
||||
CI.getDiagnostics().Report(Loc, DiagID);
|
||||
CI.getDiagnostics().Report(Loc, DiagID) << Msg;
|
||||
return;
|
||||
}
|
||||
const TargetOptions &TargetOpts = CI.getTargetOpts();
|
||||
|
|
|
|||
|
|
@ -42,36 +42,19 @@ 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,
|
||||
escapeDiag(it->second, Buf)));
|
||||
Diags.Report(it->first,
|
||||
Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
|
||||
<< it->second;
|
||||
for (const_iterator it = warn_begin(), ie = warn_end(); it != ie; ++it)
|
||||
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Warning,
|
||||
escapeDiag(it->second, Buf)));
|
||||
Diags.Report(it->first,
|
||||
Diags.getCustomDiagID(DiagnosticsEngine::Warning, "%0"))
|
||||
<< it->second;
|
||||
for (const_iterator it = note_begin(), ie = note_end(); it != ie; ++it)
|
||||
Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Note,
|
||||
escapeDiag(it->second, Buf)));
|
||||
Diags.Report(it->first,
|
||||
Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
|
||||
<< it->second;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue