]> granicus.if.org Git - clang/commitdiff
factor some code out into a helper function.
authorChris Lattner <sabre@nondot.org>
Fri, 6 Feb 2009 05:56:11 +0000 (05:56 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 6 Feb 2009 05:56:11 +0000 (05:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63922 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/PrintPreprocessedOutput.cpp

index 370adc72c44acd8db9f91328ab558136af12a8a8..8b73c14d4d802f44679e76353e3ab5df28fbe789 100644 (file)
@@ -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.