r197856 - Fix getCustomDiagID() usage in CodeGen and TextDiagnosticBuffer

Alp Toker alp at nuanti.com
Fri Dec 20 21:20:03 PST 2013


Author: alp
Date: Fri Dec 20 23:20:03 2013
New Revision: 197856

URL: http://llvm.org/viewvc/llvm-project?rev=197856&view=rev
Log:
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.

Modified:
    cfe/trunk/lib/CodeGen/CodeGenAction.cpp
    cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenAction.cpp?rev=197856&r1=197855&r2=197856&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenAction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenAction.cpp Fri Dec 20 23:20:03 2013
@@ -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();

Modified: cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp?rev=197856&r1=197855&r2=197856&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticBuffer.cpp Fri Dec 20 23:20:03 2013
@@ -42,36 +42,19 @@ void TextDiagnosticBuffer::HandleDiagnos
   }
 }
 
-/// \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;
 }
 





More information about the cfe-commits mailing list