mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-19 19:45:40 -04:00

This required a couple of yaks to be shaved: 1. MatchingOpeningBlockLineIndex was misused to also store the closing index; instead, use a second variable, as this doesn't work correctly for "} else {". 2. We needed to change the API of AffectedRangeManager to not use iterators; we always passed in begin / end for the whole container before, so there was no mismatch in generality. 3. We need an extra check to discontinue formatting at the top level, as we now sometimes change the indent of the closing brace, but want to bail out immediately afterwards, for example: void f() { if (a) { } void g(); Previously: void f() { if (a) { } void g(); Now: void f() { if (a) { } void g(); Differential Revision: https://reviews.llvm.org/D45726 llvm-svn: 330573
157 lines
5.4 KiB
C++
157 lines
5.4 KiB
C++
//===--- AffectedRangeManager.cpp - Format C++ code -----------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
///
|
|
/// \file
|
|
/// \brief This file implements AffectRangeManager class.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "AffectedRangeManager.h"
|
|
|
|
#include "FormatToken.h"
|
|
#include "TokenAnnotator.h"
|
|
|
|
namespace clang {
|
|
namespace format {
|
|
|
|
bool AffectedRangeManager::computeAffectedLines(
|
|
SmallVectorImpl<AnnotatedLine *> &Lines) {
|
|
SmallVectorImpl<AnnotatedLine *>::iterator I = Lines.begin();
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E = Lines.end();
|
|
bool SomeLineAffected = false;
|
|
const AnnotatedLine *PreviousLine = nullptr;
|
|
while (I != E) {
|
|
AnnotatedLine *Line = *I;
|
|
Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
|
|
|
|
// If a line is part of a preprocessor directive, it needs to be formatted
|
|
// if any token within the directive is affected.
|
|
if (Line->InPPDirective) {
|
|
FormatToken *Last = Line->Last;
|
|
SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
|
|
while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
|
|
Last = (*PPEnd)->Last;
|
|
++PPEnd;
|
|
}
|
|
|
|
if (affectsTokenRange(*Line->First, *Last,
|
|
/*IncludeLeadingNewlines=*/false)) {
|
|
SomeLineAffected = true;
|
|
markAllAsAffected(I, PPEnd);
|
|
}
|
|
I = PPEnd;
|
|
continue;
|
|
}
|
|
|
|
if (nonPPLineAffected(Line, PreviousLine, Lines))
|
|
SomeLineAffected = true;
|
|
|
|
PreviousLine = Line;
|
|
++I;
|
|
}
|
|
return SomeLineAffected;
|
|
}
|
|
|
|
bool AffectedRangeManager::affectsCharSourceRange(
|
|
const CharSourceRange &Range) {
|
|
for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
|
|
E = Ranges.end();
|
|
I != E; ++I) {
|
|
if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
|
|
!SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool AffectedRangeManager::affectsTokenRange(const FormatToken &First,
|
|
const FormatToken &Last,
|
|
bool IncludeLeadingNewlines) {
|
|
SourceLocation Start = First.WhitespaceRange.getBegin();
|
|
if (!IncludeLeadingNewlines)
|
|
Start = Start.getLocWithOffset(First.LastNewlineOffset);
|
|
SourceLocation End = Last.getStartOfNonWhitespace();
|
|
End = End.getLocWithOffset(Last.TokenText.size());
|
|
CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
|
|
return affectsCharSourceRange(Range);
|
|
}
|
|
|
|
bool AffectedRangeManager::affectsLeadingEmptyLines(const FormatToken &Tok) {
|
|
CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
|
|
Tok.WhitespaceRange.getBegin(),
|
|
Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
|
|
return affectsCharSourceRange(EmptyLineRange);
|
|
}
|
|
|
|
void AffectedRangeManager::markAllAsAffected(
|
|
SmallVectorImpl<AnnotatedLine *>::iterator I,
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E) {
|
|
while (I != E) {
|
|
(*I)->Affected = true;
|
|
markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
|
|
++I;
|
|
}
|
|
}
|
|
|
|
bool AffectedRangeManager::nonPPLineAffected(
|
|
AnnotatedLine *Line, const AnnotatedLine *PreviousLine,
|
|
SmallVectorImpl<AnnotatedLine *> &Lines) {
|
|
bool SomeLineAffected = false;
|
|
Line->ChildrenAffected = computeAffectedLines(Line->Children);
|
|
if (Line->ChildrenAffected)
|
|
SomeLineAffected = true;
|
|
|
|
// Stores whether one of the line's tokens is directly affected.
|
|
bool SomeTokenAffected = false;
|
|
// Stores whether we need to look at the leading newlines of the next token
|
|
// in order to determine whether it was affected.
|
|
bool IncludeLeadingNewlines = false;
|
|
|
|
// Stores whether the first child line of any of this line's tokens is
|
|
// affected.
|
|
bool SomeFirstChildAffected = false;
|
|
|
|
for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
|
|
// Determine whether 'Tok' was affected.
|
|
if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
|
|
SomeTokenAffected = true;
|
|
|
|
// Determine whether the first child of 'Tok' was affected.
|
|
if (!Tok->Children.empty() && Tok->Children.front()->Affected)
|
|
SomeFirstChildAffected = true;
|
|
|
|
IncludeLeadingNewlines = Tok->Children.empty();
|
|
}
|
|
|
|
// Was this line moved, i.e. has it previously been on the same line as an
|
|
// affected line?
|
|
bool LineMoved = PreviousLine && PreviousLine->Affected &&
|
|
Line->First->NewlinesBefore == 0;
|
|
|
|
bool IsContinuedComment =
|
|
Line->First->is(tok::comment) && Line->First->Next == nullptr &&
|
|
Line->First->NewlinesBefore < 2 && PreviousLine &&
|
|
PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
|
|
|
|
bool IsAffectedClosingBrace =
|
|
Line->First->is(tok::r_brace) &&
|
|
Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
|
|
Lines[Line->MatchingOpeningBlockLineIndex]->Affected;
|
|
|
|
if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
|
|
IsContinuedComment || IsAffectedClosingBrace) {
|
|
Line->Affected = true;
|
|
SomeLineAffected = true;
|
|
}
|
|
return SomeLineAffected;
|
|
}
|
|
|
|
} // namespace format
|
|
} // namespace clang
|