aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2013-01-10 19:49:59 +0000
committerManuel Klimek <klimek@google.com>2013-01-10 19:49:59 +0000
commitf9ea2ed30a8dec954f3d78ca43afc0be914bb1db (patch)
tree8b1e0f498935fa7f577eba357f6f5a1d9d042f2d /lib/Format/Format.cpp
parentfb8e82e9a2e6c89d1ec5f1d0112bfbdcff605954 (diff)
Refactoring the outermost structure of the formatter.
This is the last step of pure shuffling stuff around, the next step will be the actual feature. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172098 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp82
1 files changed, 45 insertions, 37 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 4d281047b8..f22c0ab581 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -1220,55 +1220,63 @@ public:
unsigned PreviousEndOfLineColumn = 0;
for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
E = UnwrappedLines.end();
- I != E; ++I)
- PreviousEndOfLineColumn = formatUnwrappedLine(*I,
- PreviousEndOfLineColumn);
+ I != E; ++I) {
+ const UnwrappedLine &TheLine = *I;
+ if (touchesRanges(TheLine)) {
+ TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex);
+ if (!Annotator.annotate())
+ break;
+ unsigned Indent = formatFirstToken(Annotator.getRootToken(),
+ TheLine.Level, TheLine.InPPDirective,
+ PreviousEndOfLineColumn);
+ bool FitsOnALine = fitsOnALine(Annotator.getRootToken(), Indent,
+ TheLine.InPPDirective);
+ UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
+ FitsOnALine, Annotator.getLineType(),
+ Annotator.getRootToken(), Replaces,
+ StructuralError);
+ PreviousEndOfLineColumn = Formatter.format();
+ } else {
+ // If we did not reformat this unwrapped line, the column at the end of
+ // the last token is unchanged - thus, we can calculate the end of the
+ // last token, and return the result.
+ const FormatToken *Last = getLastLine(TheLine);
+ PreviousEndOfLineColumn =
+ SourceMgr.getSpellingColumnNumber(Last->Tok.getLocation()) +
+ Lex.MeasureTokenLength(Last->Tok.getLocation(), SourceMgr,
+ Lex.getLangOpts()) -
+ 1;
+ }
+ }
return Replaces;
}
private:
- virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
- UnwrappedLines.push_back(TheLine);
+ const FormatToken *getLastLine(const UnwrappedLine &TheLine) {
+ const FormatToken *Last = &TheLine.RootToken;
+ while (!Last->Children.empty())
+ Last = &Last->Children.back();
+ return Last;
}
- unsigned formatUnwrappedLine(const UnwrappedLine &TheLine,
- unsigned PreviousEndOfLineColumn) {
+ bool touchesRanges(const UnwrappedLine &TheLine) {
const FormatToken *First = &TheLine.RootToken;
- const FormatToken *Last = First;
- while (!Last->Children.empty())
- Last = &Last->Children.back();
+ const FormatToken *Last = getLastLine(TheLine);
CharSourceRange LineRange = CharSourceRange::getTokenRange(
First->Tok.getLocation(),
Last->Tok.getLocation());
-
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
- if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
- Ranges[i].getBegin()) ||
- SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
- LineRange.getBegin()))
- continue;
-
- TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex);
- if (!Annotator.annotate())
- break;
- unsigned Indent = formatFirstToken(Annotator.getRootToken(),
- TheLine.Level, TheLine.InPPDirective,
- PreviousEndOfLineColumn);
- bool FitsOnALine = fitsOnALine(Annotator.getRootToken(), Indent,
- TheLine.InPPDirective);
- UnwrappedLineFormatter Formatter(
- Style, SourceMgr, TheLine, Indent, FitsOnALine,
- Annotator.getLineType(), Annotator.getRootToken(), Replaces,
- StructuralError);
- return Formatter.format();
+ if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
+ Ranges[i].getBegin()) &&
+ !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
+ LineRange.getBegin()))
+ return true;
}
- // If we did not reformat this unwrapped line, the column at the end of the
- // last token is unchanged - thus, we can calculate the end of the last
- // token, and return the result.
- return SourceMgr.getSpellingColumnNumber(Last->Tok.getLocation()) +
- Lex.MeasureTokenLength(Last->Tok.getLocation(), SourceMgr,
- Lex.getLangOpts()) -
- 1;
+ return false;
+ }
+
+ virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
+ UnwrappedLines.push_back(TheLine);
}
bool fitsOnALine(const AnnotatedToken &RootToken, unsigned Indent,