[cfe-commits] r63922 - /cfe/trunk/Driver/PrintPreprocessedOutput.cpp

Chris Lattner sabre at nondot.org
Thu Feb 5 21:56:11 PST 2009


Author: lattner
Date: Thu Feb  5 23:56:11 2009
New Revision: 63922

URL: http://llvm.org/viewvc/llvm-project?rev=63922&view=rev
Log:
factor some code out into a helper function.

Modified:
    cfe/trunk/Driver/PrintPreprocessedOutput.cpp

Modified: cfe/trunk/Driver/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/PrintPreprocessedOutput.cpp?rev=63922&r1=63921&r2=63922&view=diff

==============================================================================
--- cfe/trunk/Driver/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/Driver/PrintPreprocessedOutput.cpp Thu Feb  5 23:56:11 2009
@@ -502,6 +502,48 @@
   }
 }
 
+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 @@
   
   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 @@
          !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.





More information about the cfe-commits mailing list