[cfe-dev] TextDiagnostic: Tab handling and newlines in FixIt lines

Firat Kasmis via cfe-dev cfe-dev at lists.llvm.org
Wed Nov 2 11:34:04 PDT 2016


Hi,

I am just struggling around with tabs and new lines in TextDiagnostic.cpp.
Right now, I am writing a clang-tidy check to transform

int a, b, c;
int k1 = 0, k2 = 0;

into

int a;
int b;
int c;

int k1 = 0;
int k2 = 0;

and came across some issues.


1. buildFixItInsertionLine ignores lines with \n or \r. Is there any
specific reason not to show these lines (despite that everything is called
Line and not Lines and probably not checked for Lines)?
2. I am getting odd results for lines containing multiple tabs:


Diag << FixItHint::CreateInsertion(..., "Hallo\tTab\tTab\tX"); // gets
fired twice (for a,b,c and k1,k2 line)

==>

Hallo...Tab.....Tab.....X
Hallo.......Tab.....Tab.....X


Seems to be that

static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i)
{
  int bytes = 0;
  while (0<i) {
    if (SourceLine[--i]=='\t')
      break;
    ++bytes;
  }
  return bytes;
}

is guilty. As far as I can see, the prefix-decrement should be a postfix,
thus SourceLine[i--]. Now the result is as expected:

Hallo........Tab........Tab........X
Hallo........Tab........Tab........X

Now, splitting up the declaration statement and keeping indentation, as
Rewriter::InsertText(..indentNewLines=true) does, works well.

3. Are there any tests for TextDiagnostic.cpp? I could find one.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161102/31ebb651/attachment.html>


More information about the cfe-dev mailing list