@@ -447,14 +447,19 @@ class UnwrappedLineFormatter {
447
447
// State already examined with lower penalty.
448
448
continue ;
449
449
450
- addNextStateToQueue (Penalty, Node, /* NewLine=*/ false );
451
- addNextStateToQueue (Penalty, Node, /* NewLine=*/ true );
450
+ FormatDecision LastFormat = Node->State .NextToken ->Decision ;
451
+ if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
452
+ addNextStateToQueue (Penalty, Node, /* NewLine=*/ false );
453
+ if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
454
+ addNextStateToQueue (Penalty, Node, /* NewLine=*/ true );
452
455
}
453
456
454
- if (Queue.empty ())
457
+ if (Queue.empty ()) {
455
458
// We were unable to find a solution, do nothing.
456
459
// FIXME: Add diagnostic?
460
+ DEBUG (llvm::dbgs () << " Could not find a solution.\n " );
457
461
return 0 ;
462
+ }
458
463
459
464
// Reconstruct the solution.
460
465
if (!DryRun)
@@ -815,29 +820,54 @@ class Formatter : public UnwrappedLineConsumer {
815
820
const std::vector<CharSourceRange> &Ranges)
816
821
: Style (Style ), Lex(Lex), SourceMgr(SourceMgr),
817
822
Whitespaces (SourceMgr, Style , inputUsesCRLF(Lex.getBuffer())),
818
- Ranges(Ranges), Encoding(encoding::detectEncoding(Lex.getBuffer())) {
823
+ Ranges(Ranges), UnwrappedLines(1 ),
824
+ Encoding(encoding::detectEncoding(Lex.getBuffer())) {
819
825
DEBUG (llvm::dbgs () << " File encoding: "
820
826
<< (Encoding == encoding::Encoding_UTF8 ? " UTF8"
821
827
: " unknown" )
822
828
<< " \n " );
823
829
}
824
830
825
- virtual ~Formatter () {
826
- for (unsigned i = 0 , e = AnnotatedLines.size (); i != e; ++i) {
827
- delete AnnotatedLines[i];
828
- }
829
- }
830
-
831
831
tooling::Replacements format () {
832
+ tooling::Replacements Result;
832
833
FormatTokenLexer Tokens (Lex, SourceMgr, Style , Encoding);
833
834
834
835
UnwrappedLineParser Parser (Style , Tokens.lex (), *this );
835
836
bool StructuralError = Parser.parse ();
837
+ assert (UnwrappedLines.rbegin ()->empty ());
838
+ for (unsigned Run = 0 , RunE = UnwrappedLines.size (); Run + 1 != RunE;
839
+ ++Run) {
840
+ DEBUG (llvm::dbgs () << " Run " << Run << " ...\n " );
841
+ SmallVector<AnnotatedLine *, 16 > AnnotatedLines;
842
+ for (unsigned i = 0 , e = UnwrappedLines[Run].size (); i != e; ++i) {
843
+ AnnotatedLines.push_back (new AnnotatedLine (UnwrappedLines[Run][i]));
844
+ }
845
+ tooling::Replacements RunResult =
846
+ format (AnnotatedLines, StructuralError, Tokens);
847
+ DEBUG ({
848
+ llvm::dbgs () << " Replacements for run " << Run << " :\n " ;
849
+ for (tooling::Replacements::iterator I = RunResult.begin (),
850
+ E = RunResult.end ();
851
+ I != E; ++I) {
852
+ llvm::dbgs () << I->toString () << " \n " ;
853
+ }
854
+ });
855
+ for (unsigned i = 0 , e = AnnotatedLines.size (); i != e; ++i) {
856
+ delete AnnotatedLines[i];
857
+ }
858
+ Result.insert (RunResult.begin (), RunResult.end ());
859
+ Whitespaces.reset ();
860
+ }
861
+ return Result;
862
+ }
863
+
864
+ tooling::Replacements format (SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
865
+ bool StructuralError, FormatTokenLexer &Tokens) {
836
866
TokenAnnotator Annotator (Style , Tokens.getIdentTable ().get (" in" ));
837
867
for (unsigned i = 0 , e = AnnotatedLines.size (); i != e; ++i) {
838
868
Annotator.annotate (*AnnotatedLines[i]);
839
869
}
840
- deriveLocalStyle ();
870
+ deriveLocalStyle (AnnotatedLines );
841
871
for (unsigned i = 0 , e = AnnotatedLines.size (); i != e; ++i) {
842
872
Annotator.calculateFormattingInformation (*AnnotatedLines[i]);
843
873
}
@@ -920,8 +950,7 @@ class Formatter : public UnwrappedLineConsumer {
920
950
} else {
921
951
// Format the first token if necessary, and notify the WhitespaceManager
922
952
// about the unchanged whitespace.
923
- for (const FormatToken *Tok = TheLine.First ; Tok != NULL ;
924
- Tok = Tok->Next ) {
953
+ for (FormatToken *Tok = TheLine.First ; Tok != NULL ; Tok = Tok->Next ) {
925
954
if (Tok == TheLine.First &&
926
955
(Tok->NewlinesBefore > 0 || Tok->IsFirst )) {
927
956
unsigned LevelIndent = Tok->OriginalColumn ;
@@ -947,6 +976,9 @@ class Formatter : public UnwrappedLineConsumer {
947
976
// last token.
948
977
PreviousLineWasTouched = false ;
949
978
}
979
+ for (FormatToken *Tok = TheLine.First ; Tok != NULL ; Tok = Tok->Next ) {
980
+ Tok->Finalized = true ;
981
+ }
950
982
PreviousLine = *I;
951
983
}
952
984
return Whitespaces.generateReplacements ();
@@ -957,7 +989,8 @@ class Formatter : public UnwrappedLineConsumer {
957
989
return Text.count (' \r ' ) * 2 > Text.count (' \n ' );
958
990
}
959
991
960
- void deriveLocalStyle () {
992
+ void
993
+ deriveLocalStyle (const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
961
994
unsigned CountBoundToVariable = 0 ;
962
995
unsigned CountBoundToType = 0 ;
963
996
bool HasCpp03IncompatibleFormat = false ;
@@ -1229,13 +1262,18 @@ class Formatter : public UnwrappedLineConsumer {
1229
1262
}
1230
1263
1231
1264
virtual void consumeUnwrappedLine (const UnwrappedLine &TheLine) {
1232
- AnnotatedLines.push_back (new AnnotatedLine (TheLine));
1265
+ assert (!UnwrappedLines.empty ());
1266
+ UnwrappedLines.back ().push_back (TheLine);
1267
+ }
1268
+
1269
+ virtual void finishRun () {
1270
+ UnwrappedLines.push_back (SmallVector<UnwrappedLine, 16 >());
1233
1271
}
1234
1272
1235
1273
// / \brief Add a new line and the required indent before the first Token
1236
1274
// / of the \c UnwrappedLine if there was no structural parsing error.
1237
1275
// / Returns the indent level of the \c UnwrappedLine.
1238
- void formatFirstToken (const FormatToken &RootToken,
1276
+ void formatFirstToken (FormatToken &RootToken,
1239
1277
const AnnotatedLine *PreviousLine, unsigned Indent,
1240
1278
bool InPPDirective) {
1241
1279
unsigned Newlines =
@@ -1272,7 +1310,7 @@ class Formatter : public UnwrappedLineConsumer {
1272
1310
SourceManager &SourceMgr;
1273
1311
WhitespaceManager Whitespaces;
1274
1312
std::vector<CharSourceRange> Ranges;
1275
- SmallVector<AnnotatedLine * , 16 > AnnotatedLines ;
1313
+ SmallVector<SmallVector<UnwrappedLine , 16 >, 2 > UnwrappedLines ;
1276
1314
1277
1315
encoding::Encoding Encoding;
1278
1316
bool BinPackInconclusiveFunctions;
0 commit comments