aboutsummaryrefslogtreecommitdiff
path: root/Driver/PrintPreprocessedOutput.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-02-06 05:56:11 +0000
committerChris Lattner <sabre@nondot.org>2009-02-06 05:56:11 +0000
commit59076ab80b67774ee9e90d9d906c7877cb99f07b (patch)
treef5019881fc3b7fd2ca86f73ab398b6959707f6e7 /Driver/PrintPreprocessedOutput.cpp
parentd532fa0ebf9b831c8e7f5a7e0e125955d431060b (diff)
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
Diffstat (limited to 'Driver/PrintPreprocessedOutput.cpp')
-rw-r--r--Driver/PrintPreprocessedOutput.cpp89
1 files 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(),
"<predefines>"));
- 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.