const AnnotatedToken &Previous = *State.NextToken->Parent;
if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
- State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
- State.NextToken->FormatTok.TokenLength;
+ // FIXME: Is this correct?
+ int WhitespaceLength =
+ SourceMgr.getSpellingColumnNumber(
+ State.NextToken->FormatTok.WhitespaceRange.getEnd()) -
+ SourceMgr.getSpellingColumnNumber(
+ State.NextToken->FormatTok.WhitespaceRange.getBegin());
+ State.Column += WhitespaceLength + State.NextToken->FormatTok.TokenLength;
if (State.NextToken->Children.empty())
State.NextToken = NULL;
else
virtual FormatToken getNextToken() {
if (GreaterStashed) {
FormatTok.NewlinesBefore = 0;
- FormatTok.WhiteSpaceStart =
+ SourceLocation GreaterLocation =
FormatTok.Tok.getLocation().getLocWithOffset(1);
- FormatTok.WhiteSpaceLength = 0;
+ FormatTok.WhitespaceRange = SourceRange(GreaterLocation, GreaterLocation);
GreaterStashed = false;
return FormatTok;
}
FormatTok = FormatToken();
Lex.LexFromRawLexer(FormatTok.Tok);
StringRef Text = rawTokenText(FormatTok.Tok);
- FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
- if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
+ SourceLocation WhitespaceStart = FormatTok.Tok.getLocation();
+ if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
FormatTok.IsFirst = true;
// Consume and record whitespace until we find a significant token.
+ unsigned WhitespaceLength = 0;
while (FormatTok.Tok.is(tok::unknown)) {
unsigned Newlines = Text.count('\n');
if (Newlines > 0)
- FormatTok.LastNewlineOffset =
- FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1;
+ FormatTok.LastNewlineOffset = WhitespaceLength + Text.rfind('\n') + 1;
unsigned EscapedNewlines = Text.count("\\\n");
FormatTok.NewlinesBefore += Newlines;
FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
- FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
+ WhitespaceLength += FormatTok.Tok.getLength();
- if (FormatTok.Tok.is(tok::eof))
+ if (FormatTok.Tok.is(tok::eof)) {
+ FormatTok.WhitespaceRange =
+ SourceRange(WhitespaceStart,
+ WhitespaceStart.getLocWithOffset(WhitespaceLength));
return FormatTok;
+ }
Lex.LexFromRawLexer(FormatTok.Tok);
Text = rawTokenText(FormatTok.Tok);
}
unsigned i = 0;
while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
// FIXME: ++FormatTok.NewlinesBefore is missing...
- FormatTok.WhiteSpaceLength += 2;
+ WhitespaceLength += 2;
FormatTok.TokenLength -= 2;
i += 2;
}
GreaterStashed = true;
}
+ FormatTok.WhitespaceRange = SourceRange(
+ WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
return FormatTok;
}
} else if (TheLine.Type != LT_Invalid &&
(WasMoved || FormatPPDirective || touchesLine(TheLine))) {
unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
- if (FirstTok.WhiteSpaceStart.isValid() &&
+ if (FirstTok.WhitespaceRange.isValid() &&
// Insert a break even if there is a structural error in case where
// we break apart a line consisting of multiple unwrapped lines.
(FirstTok.NewlinesBefore == 0 || !StructuralError)) {
AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
while (!Tok->Children.empty()) {
if (Tok->Type == TT_PointerOrReference) {
- bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
- bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
+ bool SpacesBefore = Tok->FormatTok.WhitespaceRange.getBegin() !=
+ Tok->FormatTok.WhitespaceRange.getEnd();
+ bool SpacesAfter =
+ Tok->Children[0].FormatTok.WhitespaceRange.getBegin() !=
+ Tok->Children[0].FormatTok.WhitespaceRange.getEnd();
if (SpacesBefore && !SpacesAfter)
++CountBoundToVariable;
else if (!SpacesBefore && SpacesAfter)
if (Tok->Type == TT_TemplateCloser &&
Tok->Parent->Type == TT_TemplateCloser &&
- Tok->FormatTok.WhiteSpaceLength == 0)
+ Tok->FormatTok.WhitespaceRange.getBegin() ==
+ Tok->FormatTok.WhitespaceRange.getEnd())
HasCpp03IncompatibleFormat = true;
Tok = &Tok->Children[0];
}
const FormatToken *First = &TheLine.First.FormatTok;
const FormatToken *Last = &TheLine.Last->FormatTok;
CharSourceRange LineRange = CharSourceRange::getCharRange(
- First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
+ First->WhitespaceRange.getBegin().getLocWithOffset(
+ First->LastNewlineOffset),
Last->Tok.getLocation().getLocWithOffset(Last->TokenLength - 1));
return touchesRanges(LineRange);
}
bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
const FormatToken *First = &TheLine.First.FormatTok;
CharSourceRange LineRange = CharSourceRange::getCharRange(
- First->WhiteSpaceStart,
- First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset));
+ First->WhitespaceRange.getBegin(),
+ First->WhitespaceRange.getBegin().getLocWithOffset(
+ First->LastNewlineOffset));
return touchesRanges(LineRange);
}
/// whitespace characters preceeding it.
struct FormatToken {
FormatToken()
- : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
+ : NewlinesBefore(0), HasUnescapedNewline(false),
LastNewlineOffset(0), TokenLength(0), IsFirst(false),
MustBreakBefore(false), TrailingWhiteSpaceLength(0) {}
/// Token.
bool HasUnescapedNewline;
- /// \brief The location of the start of the whitespace immediately preceeding
- /// the \c Token.
- ///
- /// Used together with \c WhiteSpaceLength to create a \c Replacement.
- SourceLocation WhiteSpaceStart;
-
- /// \brief The length in characters of the whitespace immediately preceeding
- /// the \c Token.
- unsigned WhiteSpaceLength;
+ /// \brief The range of the whitespace immediately preceeding the \c Token.
+ SourceRange WhitespaceRange;
/// \brief The offset just past the last '\n' in this token's leading
/// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
/// This can be different to Tok.getLocation(), which includes leading escaped
/// newlines.
SourceLocation getStartOfNonWhitespace() const {
- return WhiteSpaceStart.getLocWithOffset(WhiteSpaceLength);
+ return WhitespaceRange.getEnd();
}
};
unsigned StartOfTokenColumn,
bool InPPDirective) {
Changes.push_back(Change(
- true, SourceRange(Tok.FormatTok.WhiteSpaceStart,
- Tok.FormatTok.WhiteSpaceStart.getLocWithOffset(
- Tok.FormatTok.WhiteSpaceLength)),
+ true, Tok.FormatTok.WhitespaceRange,
Spaces, StartOfTokenColumn, Newlines, "", "", Tok.FormatTok.Tok.getKind(),
InPPDirective && !Tok.FormatTok.IsFirst));
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
bool InPPDirective) {
- Changes.push_back(Change(
- false,
- SourceRange(Tok.WhiteSpaceStart,
- Tok.WhiteSpaceStart.getLocWithOffset(Tok.WhiteSpaceLength)),
- Tok.WhiteSpaceLength - Tok.NewlinesBefore,
- SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1,
- Tok.NewlinesBefore, "", "", Tok.Tok.getKind(),
- InPPDirective && !Tok.IsFirst));
+ Changes.push_back(
+ Change(false, Tok.WhitespaceRange, /*Spaces=*/0,
+ SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1,
+ Tok.NewlinesBefore, "", "", Tok.Tok.getKind(),
+ InPPDirective && !Tok.IsFirst));
}
void WhitespaceManager::breakToken(const FormatToken &Tok, unsigned Offset,