From 59076ab80b67774ee9e90d9d906c7877cb99f07b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 6 Feb 2009 05:56:11 +0000 Subject: [PATCH] factor some code out into a helper function. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63922 91177308-0d34-0410-b5e6-96231b3b80d8 --- Driver/PrintPreprocessedOutput.cpp | 89 ++++++++++++++++-------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/Driver/PrintPreprocessedOutput.cpp b/Driver/PrintPreprocessedOutput.cpp index 370adc72c4..8b73c14d4d 100644 --- a/Driver/PrintPreprocessedOutput.cpp +++ b/Driver/PrintPreprocessedOutput.cpp @@ -502,6 +502,48 @@ bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok, } } +static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, + PrintPPOutputPPCallbacks *Callbacks, + llvm::raw_ostream &OS) { + char Buffer[256]; + Token PrevTok; + while (1) { + + // If this token is at the start of a line, emit newlines if needed. + if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { + // done. + } else if (Tok.hasLeadingSpace() || + // If we haven't emitted a token on this line yet, PrevTok isn't + // useful to look at and no concatenation could happen anyway. + (Callbacks->hasEmittedTokensOnThisLine() && + // Don't print "-" next to "-", it would form "--". + Callbacks->AvoidConcat(PrevTok, Tok))) { + OS << ' '; + } + + if (IdentifierInfo *II = Tok.getIdentifierInfo()) { + OS.write(II->getName(), II->getLength()); + } else if (Tok.isLiteral() && !Tok.needsCleaning() && + Tok.getLiteralData()) { + OS.write(Tok.getLiteralData(), Tok.getLength()); + } else if (Tok.getLength() < 256) { + const char *TokPtr = Buffer; + unsigned Len = PP.getSpelling(Tok, TokPtr); + OS.write(TokPtr, Len); + } else { + std::string S = PP.getSpelling(Tok); + OS.write(&S[0], S.size()); + } + Callbacks->SetEmittedTokensOnThisLine(); + + if (Tok.is(tok::eof)) break; + + PrevTok = Tok; + PP.Lex(Tok); + } +} + + /// DoPrintPreprocessedInput - This implements -E mode. /// void clang::DoPrintPreprocessedInput(Preprocessor &PP, @@ -522,15 +564,14 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, OS.SetBufferSize(64*1024); - - Token Tok, PrevTok; - char Buffer[256]; - PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, OS); - PP.setPPCallbacks(Callbacks); - + Token Tok; + PrintPPOutputPPCallbacks *Callbacks; + Callbacks = new PrintPPOutputPPCallbacks(PP, OS); PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); + PP.setPPCallbacks(Callbacks); + // After we have configured the preprocessor, enter the main file. // Start parsing the specified input file. @@ -545,40 +586,8 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(), "")); - while (1) { - - // If this token is at the start of a line, emit newlines if needed. - if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { - // done. - } else if (Tok.hasLeadingSpace() || - // If we haven't emitted a token on this line yet, PrevTok isn't - // useful to look at and no concatenation could happen anyway. - (Callbacks->hasEmittedTokensOnThisLine() && - // Don't print "-" next to "-", it would form "--". - Callbacks->AvoidConcat(PrevTok, Tok))) { - OS << ' '; - } - - if (IdentifierInfo *II = Tok.getIdentifierInfo()) { - OS.write(II->getName(), II->getLength()); - } else if (Tok.isLiteral() && !Tok.needsCleaning() && - Tok.getLiteralData()) { - OS.write(Tok.getLiteralData(), Tok.getLength()); - } else if (Tok.getLength() < 256) { - const char *TokPtr = Buffer; - unsigned Len = PP.getSpelling(Tok, TokPtr); - OS.write(TokPtr, Len); - } else { - std::string S = PP.getSpelling(Tok); - OS.write(&S[0], S.size()); - } - Callbacks->SetEmittedTokensOnThisLine(); - - if (Tok.is(tok::eof)) break; - - PrevTok = Tok; - PP.Lex(Tok); - } + // Read all the preprocessed tokens, printing them out to the stream. + PrintPreprocessedTokens(PP, Tok, Callbacks, OS); OS << '\n'; // Flush the ostream. -- 2.40.0