@@ -588,11 +588,49 @@ static void ReadPrologFiles(Preprocessor &PP, std::vector<char> &Buf) {
588
588
// Preprocessed output mode.
589
589
// ===----------------------------------------------------------------------===//
590
590
591
+ // / HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
592
+ // / is called for the first token on each new line.
593
+ static void HandleFirstTokOnLine (LexerToken &Tok, Preprocessor &PP,
594
+ unsigned &CurLine) {
595
+ // Figure out what line we went to and insert the appropriate number of
596
+ // newline characters.
597
+ unsigned LineNo = PP.getSourceManager ().getLineNumber (Tok.getLocation ());
598
+
599
+ // If this line is "close enough" to the original line, just print newlines,
600
+ // otherwise print a #line directive.
601
+ if (LineNo-CurLine < 8 ) {
602
+ for (; CurLine != LineNo; ++CurLine)
603
+ std::cout << " \n " ;
604
+ } else {
605
+ // FIXME: filename too.
606
+ std::cout << " \n # " << LineNo << " \n " ;
607
+ CurLine = LineNo;
608
+ }
609
+
610
+ // Print out space characters so that the first token on a line is
611
+ // indented for easy reading.
612
+ unsigned ColNo =
613
+ PP.getSourceManager ().getColumnNumber (Tok.getLocation ());
614
+
615
+ // This hack prevents stuff like:
616
+ // #define HASH #
617
+ // HASH define foo bar
618
+ // From having the # character end up at column 1, which makes it so it
619
+ // is not handled as a #define next time through the preprocessor if in
620
+ // -fpreprocessed mode.
621
+ if (ColNo <= 1 && Tok.getKind () == tok::hash)
622
+ std::cout << ' ' ;
623
+
624
+ // Otherwise, indent the appropriate number of spaces.
625
+ for (; ColNo > 1 ; --ColNo)
626
+ std::cout << ' ' ;
627
+ }
628
+
591
629
// / DoPrintPreprocessedInput - This implements -E mode.
592
630
void DoPrintPreprocessedInput (Preprocessor &PP) {
593
631
LexerToken Tok;
594
632
char Buffer[256 ];
595
- bool isFirstToken = true ;
633
+ unsigned CurLine = 1 ;
596
634
do {
597
635
PP.Lex (Tok);
598
636
@@ -602,29 +640,10 @@ void DoPrintPreprocessedInput(Preprocessor &PP) {
602
640
// FIXME: For some tests, this fails just because there is no col# info from
603
641
// macro expansions!
604
642
if (Tok.isAtStartOfLine ()) {
605
- if (!isFirstToken)
606
- std::cout << " \n " ;
607
- // Print out space characters so that the first token on a line is
608
- // indented for easy reading.
609
- unsigned ColNo =
610
- PP.getSourceManager ().getColumnNumber (Tok.getLocation ());
611
-
612
- // This hack prevents stuff like:
613
- // #define HASH #
614
- // HASH define foo bar
615
- // From having the # character end up at column 1, which makes it so it
616
- // is not handled as a #define next time through the preprocessor if in
617
- // -fpreprocessed mode.
618
- if (ColNo <= 1 && Tok.getKind () == tok::hash)
619
- std::cout << ' ' ;
620
-
621
- for (; ColNo > 1 ; --ColNo)
622
- std::cout << ' ' ;
623
-
643
+ HandleFirstTokOnLine (Tok, PP, CurLine);
624
644
} else if (Tok.hasLeadingSpace ()) {
625
645
std::cout << ' ' ;
626
646
}
627
- isFirstToken = false ;
628
647
629
648
if (Tok.getLength () < 256 ) {
630
649
unsigned Len = PP.getSpelling (Tok, Buffer);
0 commit comments