Nicer display of unprintable source, and fix caret display for non-ascii text

Unprintable source in diagnostics is transformed to a printable form and then
displayed with reversed colors if possible. Unprintable characters are
displayed as <U+NNNN> while bytes that do not represent valid characters are
shown as <XX>.

Column adjustments to diagnostic carets, highlighted ranges, and fixups are
made both for characters escaped as above and for characters which are
printable but take up more than a single column.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154980 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/ConvertUTF.h b/include/clang/Basic/ConvertUTF.h
index ec6b973..7fb5874 100644
--- a/include/clang/Basic/ConvertUTF.h
+++ b/include/clang/Basic/ConvertUTF.h
@@ -151,9 +151,9 @@
 ConversionResult ConvertUTF32toUTF16 (
   const UTF32** sourceStart, const UTF32* sourceEnd,
   UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
+#endif
 
 Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
-#endif
 
 Boolean isLegalUTF8String(const UTF8 *source, const UTF8 *sourceEnd);
 
diff --git a/include/clang/Frontend/TextDiagnostic.h b/include/clang/Frontend/TextDiagnostic.h
index 519d3b6..314003b 100644
--- a/include/clang/Frontend/TextDiagnostic.h
+++ b/include/clang/Frontend/TextDiagnostic.h
@@ -18,6 +18,8 @@
 
 #include "clang/Frontend/DiagnosticRenderer.h"
 
+struct SourceColumnMap;
+
 namespace clang {
 
 /// \brief Class to encapsulate the logic for formatting and printing a textual
@@ -103,15 +105,16 @@
                            SmallVectorImpl<CharSourceRange>& Ranges,
                            ArrayRef<FixItHint> Hints);
 
+  void emitSnippet(StringRef SourceLine);
+
   void highlightRange(const CharSourceRange &R,
                       unsigned LineNo, FileID FID,
-                      const std::string &SourceLine,
+                      const SourceColumnMap &map,
                       std::string &CaretLine);
+
   std::string buildFixItInsertionLine(unsigned LineNo,
-                                      const char *LineStart,
-                                      const char *LineEnd,
+                                      const SourceColumnMap &map,
                                       ArrayRef<FixItHint> Hints);
-  void expandTabs(std::string &SourceLine, std::string &CaretLine);
   void emitParseableFixits(ArrayRef<FixItHint> Hints);
 };
 
diff --git a/lib/Frontend/TextDiagnostic.cpp b/lib/Frontend/TextDiagnostic.cpp
index 9f5dcb4..3c35ab2 100644
--- a/lib/Frontend/TextDiagnostic.cpp
+++ b/lib/Frontend/TextDiagnostic.cpp
@@ -10,13 +10,17 @@
 #include "clang/Frontend/TextDiagnostic.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/ConvertUTF.h"
 #include "clang/Frontend/DiagnosticOptions.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Locale.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
 #include <algorithm>
+
 using namespace clang;
 
 static const enum raw_ostream::Colors noteColor =
@@ -36,23 +40,268 @@
 /// \brief Number of spaces to indent when word-wrapping.
 const unsigned WordWrapIndentation = 6;
 
+int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) {
+  int bytes = 0;
+  while (0<i) {
+    if (SourceLine[--i]=='\t')
+      break;
+    ++bytes;
+  }
+  return bytes;
+}
+
+/// \brief returns a printable representation of first item from input range
+///
+/// This function returns a printable representation of the next item in a line
+///  of source. If the next byte begins a valid and printable character, that
+///  character is returned along with 'true'.
+///
+/// Otherwise, if the next byte begins a valid, but unprintable character, a
+///  printable, escaped representation of the character is returned, along with
+///  'false'. Otherwise a printable, escaped representation of the next byte
+///  is returned along with 'false'.
+///
+/// \note The index is updated to be used with a subsequent call to
+///        printableTextForNextCharacter.
+///
+/// \param SourceLine The line of source
+/// \param i Pointer to byte index,
+/// \param TabStop used to expand tabs
+/// \return pair(printable text, 'true' iff original text was printable)
+///
+std::pair<SmallString<16>,bool>
+printableTextForNextCharacter(StringRef SourceLine, size_t *i,
+                              unsigned TabStop) {
+  assert(i && "i must not be null");
+  assert(*i<SourceLine.size() && "must point to a valid index");
+  
+  if (SourceLine[*i]=='\t') {
+    assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
+           "Invalid -ftabstop value");
+    unsigned col = bytesSincePreviousTabOrLineBegin(SourceLine, *i);
+    unsigned NumSpaces = TabStop - col%TabStop;
+    assert(0 < NumSpaces && NumSpaces <= TabStop
+           && "Invalid computation of space amt");
+    ++(*i);
+
+    SmallString<16> expandedTab;
+    expandedTab.assign(NumSpaces, ' ');
+    return std::make_pair(expandedTab, true);
+  }
+
+  // FIXME: this data is copied from the private implementation of ConvertUTF.h
+  static const char trailingBytesForUTF8[256] = {
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
+  };
+
+  unsigned char const *begin, *end;
+  begin = reinterpret_cast<unsigned char const *>(&*(SourceLine.begin() + *i));
+  end = begin + SourceLine.size();
+  
+  if (isLegalUTF8Sequence(begin, end)) {
+    UTF32 c;
+    UTF32 *cptr = &c;
+    unsigned char const *original_begin = begin;
+    char trailingBytes = trailingBytesForUTF8[(unsigned char)SourceLine[*i]];
+    unsigned char const *cp_end = begin+trailingBytes+1;
+
+    ConversionResult res = ConvertUTF8toUTF32(&begin, cp_end, &cptr, cptr+1,
+                                              strictConversion);
+    assert(conversionOK==res);
+    assert(0 < begin-original_begin
+           && "we must be further along in the string now");
+    *i += begin-original_begin;
+
+    if (!llvm::sys::locale::isPrint(c)) {
+      // If next character is valid UTF-8, but not printable
+      SmallString<16> expandedCP("<U+>");
+      while (c) {
+        expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(c%16));
+        c/=16;
+      }
+      while (expandedCP.size() < 8)
+        expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(0));
+      return std::make_pair(expandedCP, false);
+    }
+
+    // If next character is valid UTF-8, and printable
+    return std::make_pair(SmallString<16>(original_begin, cp_end), true);
+
+  }
+
+  // If next byte is not valid UTF-8 (and therefore not printable)
+  SmallString<16> expandedByte("<XX>");
+  unsigned char byte = SourceLine[*i];
+  expandedByte[1] = llvm::hexdigit(byte / 16);
+  expandedByte[2] = llvm::hexdigit(byte % 16);
+  ++(*i);
+  return std::make_pair(expandedByte, false);
+}
+
+void expandTabs(std::string &SourceLine, unsigned TabStop) {
+  size_t i = SourceLine.size();
+  while (i>0) {
+    i--;
+    if (SourceLine[i]!='\t')
+      continue;
+    size_t tmp_i = i;
+    std::pair<SmallString<16>,bool> res
+      = printableTextForNextCharacter(SourceLine, &tmp_i, TabStop);
+    SourceLine.replace(i, 1, res.first.c_str());
+  }
+}
+
+/// This function takes a raw source line and produces a mapping from the bytes
+///  of the printable representation of the line to the columns those printable
+///  characters will appear at (numbering the first column as 0).
+///
+/// If a byte 'i' corresponds to muliple columns (e.g. the byte contains a tab
+///  character) then the the array will map that byte to the first column the
+///  tab appears at and the next value in the map will have been incremented
+///  more than once.
+///
+/// If a byte is the first in a sequence of bytes that together map to a single
+///  entity in the output, then the array will map that byte to the appropriate
+///  column while the subsequent bytes will be -1.
+///
+/// The last element in the array does not correspond to any byte in the input
+///  and instead is the number of columns needed to display the source
+///
+/// example: (given a tabstop of 8)
+///
+///    "a \t \u3042" -> {0,1,2,8,9,-1,-1,11}
+///
+///  (\u3042 is represented in UTF-8 by three bytes and takes two columns to
+///   display)
+void byteToColumn(StringRef SourceLine, unsigned TabStop,
+                         SmallVectorImpl<int> &out) {
+  out.clear();
+
+  if (SourceLine.empty()) {
+    out.resize(1u,0);
+    return;
+  }
+  
+  out.resize(SourceLine.size()+1, -1);
+
+  int columns = 0;
+  size_t i = 0;
+  while (i<SourceLine.size()) {
+    out[i] = columns;
+    std::pair<SmallString<16>,bool> res
+      = printableTextForNextCharacter(SourceLine, &i, TabStop);
+    columns += llvm::sys::locale::columnWidth(res.first);
+  }
+  out.back() = columns;
+}
+
+/// This function takes a raw source line and produces a mapping from columns
+///  to the byte of the source line that produced the character displaying at
+///  that column. This is the inverse of the mapping produced by byteToColumn()
+///
+/// The last element in the array is the number of bytes in the source string
+///
+/// example: (given a tabstop of 8)
+///
+///    "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7}
+///
+///  (\u3042 is represented in UTF-8 by three bytes and takes two columns to
+///   display)
+void columnToByte(StringRef SourceLine, unsigned TabStop,
+                         SmallVectorImpl<int> &out) {
+  out.clear();
+
+  if (SourceLine.empty()) {
+    out.resize(1u, 0);
+    return;
+  }
+
+  int columns = 0;
+  size_t i = 0;
+  while (i<SourceLine.size()) {
+    out.resize(columns+1, -1);
+    out.back() = i;
+    std::pair<SmallString<16>,bool> res
+      = printableTextForNextCharacter(SourceLine, &i, TabStop);
+    columns += llvm::sys::locale::columnWidth(res.first);
+  }
+  out.resize(columns+1, -1);
+  out.back() = i;
+}
+
+struct SourceColumnMap {
+  SourceColumnMap(StringRef SourceLine, unsigned TabStop)
+  : m_SourceLine(SourceLine) {
+    
+    ::byteToColumn(SourceLine, TabStop, m_byteToColumn);
+    ::columnToByte(SourceLine, TabStop, m_columnToByte);
+    
+    assert(m_byteToColumn.size()==SourceLine.size()+1);
+    assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size());
+    assert(m_byteToColumn.size()
+           == static_cast<unsigned>(m_columnToByte.back()+1));
+    assert(static_cast<unsigned>(m_byteToColumn.back()+1)
+           == m_columnToByte.size());
+  }
+  int columns() const { return m_byteToColumn.back(); }
+  int bytes() const { return m_columnToByte.back(); }
+  int byteToColumn(int n) const {
+    assert(0<=n && n<static_cast<int>(m_byteToColumn.size()));
+    return m_byteToColumn[n];
+  }
+  int columnToByte(int n) const {
+    assert(0<=n && n<static_cast<int>(m_columnToByte.size()));
+    return m_columnToByte[n];
+  }
+  StringRef getSourceLine() const {
+    return m_SourceLine;
+  }
+  
+private:
+  const std::string m_SourceLine;
+  SmallVector<int,200> m_byteToColumn;
+  SmallVector<int,200> m_columnToByte;
+};
+
+// used in assert in selectInterestingSourceRegion()
+namespace {
+struct char_out_of_range {
+  const char lower,upper;
+  char_out_of_range(char lower, char upper) :
+    lower(lower), upper(upper) {}
+  bool operator()(char c) { return c < lower || upper < c; }
+};
+}
+
 /// \brief When the source code line we want to print is too long for
 /// the terminal, select the "interesting" region.
 static void selectInterestingSourceRegion(std::string &SourceLine,
                                           std::string &CaretLine,
                                           std::string &FixItInsertionLine,
-                                          unsigned EndOfCaretToken,
-                                          unsigned Columns) {
-  unsigned MaxSize = std::max(SourceLine.size(),
-                              std::max(CaretLine.size(), 
-                                       FixItInsertionLine.size()));
-  if (MaxSize > SourceLine.size())
-    SourceLine.resize(MaxSize, ' ');
-  if (MaxSize > CaretLine.size())
-    CaretLine.resize(MaxSize, ' ');
-  if (!FixItInsertionLine.empty() && MaxSize > FixItInsertionLine.size())
-    FixItInsertionLine.resize(MaxSize, ' ');
-    
+                                          unsigned Columns,
+                                          const SourceColumnMap &map) {
+  unsigned MaxColumns = std::max<unsigned>(map.columns(),
+                                           std::max(CaretLine.size(),
+                                                    FixItInsertionLine.size()));
+  // if the number of columns is less than the desired number we're done
+  if (MaxColumns <= Columns)
+    return;
+
+  // no special characters allowed in CaretLine or FixItInsertionLine
+  assert(CaretLine.end() ==
+         std::find_if(CaretLine.begin(), CaretLine.end(),
+         char_out_of_range(' ','~')));
+  assert(FixItInsertionLine.end() ==
+         std::find_if(FixItInsertionLine.begin(), FixItInsertionLine.end(),
+         char_out_of_range(' ','~')));
+
   // Find the slice that we need to display the full caret line
   // correctly.
   unsigned CaretStart = 0, CaretEnd = CaretLine.size();
@@ -64,10 +313,8 @@
     if (!isspace(CaretLine[CaretEnd - 1]))
       break;
 
-  // Make sure we don't chop the string shorter than the caret token
-  // itself.
-  if (CaretEnd < EndOfCaretToken)
-    CaretEnd = EndOfCaretToken;
+  // caret has already been inserted into CaretLine so the above whitespace
+  // check is guaranteed to include the caret
 
   // If we have a fix-it line, make sure the slice includes all of the
   // fix-it information.
@@ -81,10 +328,8 @@
       if (!isspace(FixItInsertionLine[FixItEnd - 1]))
         break;
 
-    if (FixItStart < CaretStart)
-      CaretStart = FixItStart;
-    if (FixItEnd > CaretEnd)
-      CaretEnd = FixItEnd;
+    CaretStart = std::min(FixItStart, CaretStart);
+    CaretEnd = std::max(FixItEnd, CaretEnd);
   }
 
   // CaretLine[CaretStart, CaretEnd) contains all of the interesting
@@ -92,62 +337,72 @@
   // number of columns we have, try to grow the slice to encompass
   // more context.
 
-  // If the end of the interesting region comes before we run out of
-  // space in the terminal, start at the beginning of the line.
-  if (Columns > 3 && CaretEnd < Columns - 3)
-    CaretStart = 0;
+  unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart,
+                                                             map.columns()));
+  unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd,
+                                                           map.columns()));
+
+  unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart
+    - (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart));
+
+  char const *front_ellipse = "  ...";
+  char const *front_space   = "     ";
+  char const *back_ellipse = "...";
+  unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse);
 
   unsigned TargetColumns = Columns;
-  if (TargetColumns > 8)
-    TargetColumns -= 8; // Give us extra room for the ellipses.
-  unsigned SourceLength = SourceLine.size();
-  while ((CaretEnd - CaretStart) < TargetColumns) {
+  // Give us extra room for the ellipses
+  //  and any of the caret line that extends past the source
+  if (TargetColumns > ellipses_space+CaretColumnsOutsideSource)
+    TargetColumns -= ellipses_space+CaretColumnsOutsideSource;
+
+  while (SourceStart>0 || SourceEnd<SourceLine.size()) {
     bool ExpandedRegion = false;
-    // Move the start of the interesting region left until we've
-    // pulled in something else interesting.
-    if (CaretStart == 1)
-      CaretStart = 0;
-    else if (CaretStart > 1) {
-      unsigned NewStart = CaretStart - 1;
+
+    if (SourceStart>0) {
+      unsigned NewStart = SourceStart-1;
 
       // Skip over any whitespace we see here; we're looking for
       // another bit of interesting text.
-      while (NewStart && isspace(SourceLine[NewStart]))
+      while (NewStart &&
+             (map.byteToColumn(NewStart)==-1 || isspace(SourceLine[NewStart])))
         --NewStart;
 
       // Skip over this bit of "interesting" text.
-      while (NewStart && !isspace(SourceLine[NewStart]))
+      while (NewStart &&
+             (map.byteToColumn(NewStart)!=-1 && !isspace(SourceLine[NewStart])))
         --NewStart;
 
       // Move up to the non-whitespace character we just saw.
       if (NewStart)
         ++NewStart;
 
-      // If we're still within our limit, update the starting
-      // position within the source/caret line.
-      if (CaretEnd - NewStart <= TargetColumns) {
-        CaretStart = NewStart;
+      unsigned NewColumns = map.byteToColumn(SourceEnd) -
+                              map.byteToColumn(NewStart);
+      if (NewColumns <= TargetColumns) {
+        SourceStart = NewStart;
         ExpandedRegion = true;
       }
     }
 
-    // Move the end of the interesting region right until we've
-    // pulled in something else interesting.
-    if (CaretEnd != SourceLength) {
-      assert(CaretEnd < SourceLength && "Unexpected caret position!");
-      unsigned NewEnd = CaretEnd;
+    if (SourceEnd<SourceLine.size()) {
+      unsigned NewEnd = SourceEnd+1;
 
       // Skip over any whitespace we see here; we're looking for
       // another bit of interesting text.
-      while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1]))
+      while (NewEnd<SourceLine.size() &&
+             (map.byteToColumn(NewEnd)==-1 || isspace(SourceLine[NewEnd])))
         ++NewEnd;
 
       // Skip over this bit of "interesting" text.
-      while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1]))
+      while (NewEnd<SourceLine.size() &&
+             (map.byteToColumn(NewEnd)!=-1 && !isspace(SourceLine[NewEnd])))
         ++NewEnd;
 
-      if (NewEnd - CaretStart <= TargetColumns) {
-        CaretEnd = NewEnd;
+      unsigned NewColumns = map.byteToColumn(NewEnd) -
+                              map.byteToColumn(SourceStart);
+      if (NewColumns <= TargetColumns) {
+        SourceEnd = NewEnd;
         ExpandedRegion = true;
       }
     }
@@ -156,21 +411,41 @@
       break;
   }
 
+  CaretStart = map.byteToColumn(SourceStart);
+  CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
+
   // [CaretStart, CaretEnd) is the slice we want. Update the various
   // output lines to show only this slice, with two-space padding
   // before the lines so that it looks nicer.
-  if (CaretEnd < SourceLine.size())
-    SourceLine.replace(CaretEnd, std::string::npos, "...");
-  if (CaretEnd < CaretLine.size())
-    CaretLine.erase(CaretEnd, std::string::npos);
-  if (FixItInsertionLine.size() > CaretEnd)
-    FixItInsertionLine.erase(CaretEnd, std::string::npos);
 
-  if (CaretStart > 2) {
-    SourceLine.replace(0, CaretStart, "  ...");
-    CaretLine.replace(0, CaretStart, "     ");
-    if (FixItInsertionLine.size() >= CaretStart)
-      FixItInsertionLine.replace(0, CaretStart, "     ");
+  assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 &&
+         SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1);
+  assert(SourceStart <= SourceEnd);
+  assert(CaretStart <= CaretEnd);
+
+  unsigned BackColumnsRemoved
+    = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd);
+  unsigned FrontColumnsRemoved = CaretStart;
+  unsigned ColumnsKept = CaretEnd-CaretStart;
+
+  // We checked up front that the line needed truncation
+  assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns);
+
+  // The line needs some trunctiona, and we'd prefer to keep the front
+  //  if possible, so remove the back
+  if (BackColumnsRemoved)
+    SourceLine.replace(SourceEnd, std::string::npos, back_ellipse);
+
+  // If that's enough then we're done
+  if (FrontColumnsRemoved+ColumnsKept <= Columns)
+    return;
+
+  // Otherwise remove the front as well
+  if (FrontColumnsRemoved) {
+    SourceLine.replace(0, SourceStart, front_ellipse);
+    CaretLine.replace(0, CaretStart, front_space);
+    if (!FixItInsertionLine.empty())
+      FixItInsertionLine.replace(0, CaretStart, front_space);
   }
 }
 
@@ -596,19 +871,30 @@
   // length as the line of source code.
   std::string CaretLine(LineEnd-LineStart, ' ');
 
+  const SourceColumnMap sourceColMap(SourceLine, DiagOpts.TabStop);
+
   // Highlight all of the characters covered by Ranges with ~ characters.
   for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(),
                                                   E = Ranges.end();
        I != E; ++I)
-    highlightRange(*I, LineNo, FID, SourceLine, CaretLine);
+    highlightRange(*I, LineNo, FID, sourceColMap, CaretLine);
 
   // Next, insert the caret itself.
-  if (ColNo-1 < CaretLine.size())
-    CaretLine[ColNo-1] = '^';
-  else
-    CaretLine.push_back('^');
+  ColNo = sourceColMap.byteToColumn(ColNo-1);
+  if (CaretLine.size()<ColNo+1)
+    CaretLine.resize(ColNo+1, ' ');
+  CaretLine[ColNo] = '^';
 
-  expandTabs(SourceLine, CaretLine);
+  std::string FixItInsertionLine = buildFixItInsertionLine(LineNo,
+                                                           sourceColMap,
+                                                           Hints);
+
+  // If the source line is too long for our terminal, select only the
+  // "interesting" source region within that line.
+  unsigned Columns = DiagOpts.MessageLength;
+  if (Columns)
+    selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
+                                  Columns, sourceColMap);
 
   // If we are in -fdiagnostics-print-source-range-info mode, we are trying
   // to produce easily machine parsable output.  Add a space before the
@@ -619,23 +905,12 @@
     CaretLine = ' ' + CaretLine;
   }
 
-  std::string FixItInsertionLine = buildFixItInsertionLine(LineNo,
-                                                           LineStart, LineEnd,
-                                                           Hints);
-
-  // If the source line is too long for our terminal, select only the
-  // "interesting" source region within that line.
-  unsigned Columns = DiagOpts.MessageLength;
-  if (Columns && SourceLine.size() > Columns)
-    selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
-                                  CaretEndColNo, Columns);
-
   // Finally, remove any blank spaces from the end of CaretLine.
   while (CaretLine[CaretLine.size()-1] == ' ')
     CaretLine.erase(CaretLine.end()-1);
 
   // Emit what we have computed.
-  OS << SourceLine << '\n';
+  emitSnippet(SourceLine);
 
   if (DiagOpts.ShowColors)
     OS.changeColor(caretColor, true);
@@ -658,13 +933,49 @@
   emitParseableFixits(Hints);
 }
 
+void TextDiagnostic::emitSnippet(StringRef line)
+{
+  if (line.empty())
+    return;
+
+  size_t i = 0;
+  
+  std::string to_print;
+  bool print_reversed = false;
+  
+  while (i<line.size()) {
+    std::pair<SmallString<16>,bool> res
+        = printableTextForNextCharacter(line, &i, DiagOpts.TabStop);
+    bool was_printable = res.second;
+    
+    if (DiagOpts.ShowColors
+        && was_printable==print_reversed) {
+      if (print_reversed)
+        OS.reverseColor();
+      OS << to_print;
+      to_print.clear();
+      if (DiagOpts.ShowColors)
+        OS.resetColor();
+    }
+    
+    print_reversed = !was_printable;
+    to_print += res.first.str();
+  }
+  
+  if (print_reversed && DiagOpts.ShowColors)
+    OS.reverseColor();
+  OS << to_print;
+  if (print_reversed && DiagOpts.ShowColors)
+    OS.resetColor();
+  
+  OS << '\n';
+}
+
 /// \brief Highlight a SourceRange (with ~'s) for any characters on LineNo.
 void TextDiagnostic::highlightRange(const CharSourceRange &R,
                                     unsigned LineNo, FileID FID,
-                                    const std::string &SourceLine,
+                                    const SourceColumnMap &map,
                                     std::string &CaretLine) {
-  assert(CaretLine.size() == SourceLine.size() &&
-         "Expect a correspondence between source and caret line!");
   if (!R.isValid()) return;
 
   SourceLocation Begin = SM.getExpansionLoc(R.getBegin());
@@ -694,7 +1005,7 @@
   }
 
   // Compute the column number of the end.
-  unsigned EndColNo = CaretLine.size();
+  unsigned EndColNo = map.getSourceLine().size();
   if (EndLineNo == LineNo) {
     EndColNo = SM.getExpansionColumnNumber(End);
     if (EndColNo) {
@@ -714,15 +1025,17 @@
   // Check that a token range does not highlight only whitespace.
   if (R.isTokenRange()) {
     // Pick the first non-whitespace column.
-    while (StartColNo < SourceLine.size() &&
-           (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
+    while (StartColNo < map.getSourceLine().size() &&
+           (map.getSourceLine()[StartColNo] == ' ' ||
+            map.getSourceLine()[StartColNo] == '\t'))
       ++StartColNo;
 
     // Pick the last non-whitespace column.
-    if (EndColNo > SourceLine.size())
-      EndColNo = SourceLine.size();
+    if (EndColNo > map.getSourceLine().size())
+      EndColNo = map.getSourceLine().size();
     while (EndColNo-1 &&
-           (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
+           (map.getSourceLine()[EndColNo-1] == ' ' ||
+            map.getSourceLine()[EndColNo-1] == '\t'))
       --EndColNo;
 
     // If the start/end passed each other, then we are trying to highlight a
@@ -731,15 +1044,24 @@
     assert(StartColNo <= EndColNo && "Trying to highlight whitespace??");
   }
 
+  assert(StartColNo <= map.getSourceLine().size() && "Invalid range!");
+  assert(EndColNo <= map.getSourceLine().size() && "Invalid range!");
+
   // Fill the range with ~'s.
-  for (unsigned i = StartColNo; i < EndColNo; ++i)
-    CaretLine[i] = '~';
+  StartColNo = map.byteToColumn(StartColNo);
+  EndColNo = map.byteToColumn(EndColNo);
+
+  assert(StartColNo <= EndColNo && "Invalid range!");
+  if (CaretLine.size() < EndColNo)
+    CaretLine.resize(EndColNo,' ');
+  std::fill(CaretLine.begin()+StartColNo,CaretLine.begin()+EndColNo,'~');
 }
 
-std::string TextDiagnostic::buildFixItInsertionLine(unsigned LineNo,
-                                                    const char *LineStart,
-                                                    const char *LineEnd,
-                                                    ArrayRef<FixItHint> Hints) {
+std::string TextDiagnostic::buildFixItInsertionLine(
+  unsigned LineNo,
+  const SourceColumnMap &map,
+  ArrayRef<FixItHint> Hints) {
+
   std::string FixItInsertionLine;
   if (Hints.empty() || !DiagOpts.ShowFixits)
     return FixItInsertionLine;
@@ -755,13 +1077,32 @@
         // Insert the new code into the line just below the code
         // that the user wrote.
         unsigned HintColNo
-          = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
+          = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
+        // hint must start inside the source or right at the end
+        assert(HintColNo<static_cast<unsigned>(map.bytes())+1);
+        HintColNo = map.byteToColumn(HintColNo);
+
+        // FIXME: if the fixit includes tabs or other characters that do not
+        //  take up a single column per byte when displayed then
+        //  I->CodeToInsert.size() is not a column number and we're mixing
+        //  units (columns + bytes). We should get printable versions
+        //  of each fixit before using them.
         unsigned LastColumnModified
-          = HintColNo - 1 + I->CodeToInsert.size();
+          = HintColNo + I->CodeToInsert.size();
+
+        if (LastColumnModified > static_cast<unsigned>(map.bytes())) {
+          unsigned LastExistingColumn = map.byteToColumn(map.bytes());
+          unsigned AddedColumns = LastColumnModified-LastExistingColumn;
+          LastColumnModified = LastExistingColumn + AddedColumns;
+        } else {
+          LastColumnModified = map.byteToColumn(LastColumnModified);
+        }
+
         if (LastColumnModified > FixItInsertionLine.size())
           FixItInsertionLine.resize(LastColumnModified, ' ');
+        assert(HintColNo+I->CodeToInsert.size() <= FixItInsertionLine.size());
         std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(),
-                  FixItInsertionLine.begin() + HintColNo - 1);
+                  FixItInsertionLine.begin() + HintColNo);
       } else {
         FixItInsertionLine.clear();
         break;
@@ -769,72 +1110,11 @@
     }
   }
 
-  if (FixItInsertionLine.empty())
-    return FixItInsertionLine;
-
-  // Now that we have the entire fixit line, expand the tabs in it.
-  // Since we don't want to insert spaces in the middle of a word,
-  // find each word and the column it should line up with and insert
-  // spaces until they match.
-  unsigned FixItPos = 0;
-  unsigned LinePos = 0;
-  unsigned TabExpandedCol = 0;
-  unsigned LineLength = LineEnd - LineStart;
-
-  while (FixItPos < FixItInsertionLine.size() && LinePos < LineLength) {
-    // Find the next word in the FixIt line.
-    while (FixItPos < FixItInsertionLine.size() &&
-           FixItInsertionLine[FixItPos] == ' ')
-      ++FixItPos;
-    unsigned CharDistance = FixItPos - TabExpandedCol;
-
-    // Walk forward in the source line, keeping track of
-    // the tab-expanded column.
-    for (unsigned I = 0; I < CharDistance; ++I, ++LinePos)
-      if (LinePos >= LineLength || LineStart[LinePos] != '\t')
-        ++TabExpandedCol;
-      else
-        TabExpandedCol =
-          (TabExpandedCol/DiagOpts.TabStop + 1) * DiagOpts.TabStop;
-
-    // Adjust the fixit line to match this column.
-    FixItInsertionLine.insert(FixItPos, TabExpandedCol-FixItPos, ' ');
-    FixItPos = TabExpandedCol;
-
-    // Walk to the end of the word.
-    while (FixItPos < FixItInsertionLine.size() &&
-           FixItInsertionLine[FixItPos] != ' ')
-      ++FixItPos;
-  }
+  expandTabs(FixItInsertionLine, DiagOpts.TabStop);
 
   return FixItInsertionLine;
 }
 
-void TextDiagnostic::expandTabs(std::string &SourceLine,
-                                std::string &CaretLine) {
-  // Scan the source line, looking for tabs.  If we find any, manually expand
-  // them to spaces and update the CaretLine to match.
-  for (unsigned i = 0; i != SourceLine.size(); ++i) {
-    if (SourceLine[i] != '\t') continue;
-
-    // Replace this tab with at least one space.
-    SourceLine[i] = ' ';
-
-    // Compute the number of spaces we need to insert.
-    unsigned TabStop = DiagOpts.TabStop;
-    assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
-           "Invalid -ftabstop value");
-    unsigned NumSpaces = ((i+TabStop)/TabStop * TabStop) - (i+1);
-    assert(NumSpaces < TabStop && "Invalid computation of space amt");
-
-    // Insert spaces into the SourceLine.
-    SourceLine.insert(i+1, NumSpaces, ' ');
-
-    // Insert spaces or ~'s into CaretLine.
-    CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
-  }
-}
-
 void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints) {
   if (!DiagOpts.ShowParseableFixits)
     return;
diff --git a/test/Misc/message-length.c b/test/Misc/message-length.c
index 3e69b6a..a6f4f44 100644
--- a/test/Misc/message-length.c
+++ b/test/Misc/message-length.c
@@ -27,9 +27,8 @@
 
 #pragma STDC CX_LIMITED_RANGE    // some long comment text and a brace, eh {}
 
-
 // CHECK: FILE:23:78
-// CHECK: {{^  ...// some long comment text and a brace, eh {} }}
+// CHECK: {{^  ...// some long comment text and a brace, eh {}}}
 
 struct A { int x; };
 void h(struct A *a) {
diff --git a/test/Misc/unprintable.c b/test/Misc/unprintable.c
new file mode 100644
index 0000000..860503e
--- /dev/null
+++ b/test/Misc/unprintable.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s 2>&1 | FileCheck -strict-whitespace %s
+
+int main() {
+    int i;
+    if((i==/*👿*/1));
+
+// CHECK: {{^    if\(\(i==/\*<U\+1F47F>\*/1\)\);}}
+
+// CHECK: {{^        ~\^~~~~~~~~~~~~~~~}}
+// CHECK: {{^       ~ \^               ~}}
+
+    /* 👿 */ "👿berhund";
+
+// CHECK: {{^    /\* <U\+1F47F> \*/ "<U\+1F47F>berhund";}}
+// CHECK: {{^                    \^~~~~~~~~~~~~~~~~~}}
+}
\ No newline at end of file
diff --git a/test/Misc/wrong-encoding.c b/test/Misc/wrong-encoding.c
new file mode 100644
index 0000000..bd1cf3d
--- /dev/null
+++ b/test/Misc/wrong-encoding.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck -strict-whitespace %s
+
+void foo() {
+
+  "§Ã"; // ø
+// CHECK: {{^  "<A7><C3>"; // <F8>}}
+// CHECK: {{^  \^}}
+
+  /* þ« */ const char *d = "¥";
+
+// CHECK: {{^  /\* <FE><AB> \*/ const char \*d = "<A5>";}}
+// CHECK: {{^                                 \^}}
+
+// CHECK: {{^  "<A7><C3>"; // <F8>}}
+// CHECK: {{^  \^~~~~~~~~~}}
+}