[cfe-commits] r38658 - in /cfe/cfe/trunk/Driver: PrintPreprocessedOutput.cpp clang.cpp clang.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:23:35 PDT 2007


Author: sabre
Date: Wed Jul 11 11:23:35 2007
New Revision: 38658

URL: http://llvm.org/viewvc/llvm-project?rev=38658&view=rev
Log:
Split the -E mode printer out to a separate .cpp file.

Added:
    cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp   (with props)
    cfe/cfe/trunk/Driver/clang.h   (with props)
Modified:
    cfe/cfe/trunk/Driver/clang.cpp

Added: cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp?rev=38658&view=auto

==============================================================================
--- cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp (added)
+++ cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp Wed Jul 11 11:23:35 2007
@@ -0,0 +1,206 @@
+//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This code simply runs the preprocessor on the input file and prints out the
+// result.  This is the traditional behavior of the -E option.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/Pragma.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/Support/CommandLine.h"
+#include <iostream>
+using namespace llvm;
+using namespace clang;
+
+static cl::opt<bool>
+DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode"));
+
+static unsigned EModeCurLine;
+static std::string EModeCurFilename;
+static Preprocessor *EModePP;
+static bool EmodeEmittedTokensOnThisLine;
+static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir;
+
+static void MoveToLine(unsigned LineNo) {
+  // If this line is "close enough" to the original line, just print newlines,
+  // otherwise print a #line directive.
+  if (LineNo-EModeCurLine < 8) {
+    for (; EModeCurLine != LineNo; ++EModeCurLine)
+      std::cout << "\n";
+  } else {
+    if (EmodeEmittedTokensOnThisLine) {
+      std::cout << "\n";
+      EmodeEmittedTokensOnThisLine = false;
+    }
+    
+    EModeCurLine = LineNo;
+    if (DisableLineMarkers) return;
+    
+    std::cout << "# " << LineNo << " " << EModeCurFilename;
+    
+    if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
+      std::cout << " 3";
+    else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
+      std::cout << " 3 4";
+    std::cout << "\n";
+  } 
+}
+
+/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
+/// it invokes this handler.  Update our conception of the current 
+static void HandleFileChange(SourceLocation Loc,
+                             Preprocessor::FileChangeReason Reason,
+                             DirectoryLookup::DirType FileType) {
+  SourceManager &SourceMgr = EModePP->getSourceManager();
+  
+  // Unless we are exiting a #include, make sure to skip ahead to the line the
+  // #include directive was at.
+  if (Reason == Preprocessor::EnterFile) {
+    SourceLocation IncludeLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
+    MoveToLine(SourceMgr.getLineNumber(IncludeLoc));
+  } else if (Reason == Preprocessor::SystemHeaderPragma) {
+    MoveToLine(SourceMgr.getLineNumber(Loc));
+    
+    // TODO GCC emits the # directive for this directive on the line AFTER the
+    // directive and emits a bunch of spaces that aren't needed.  Emulate this
+    // strange behavior.
+  }
+  
+  EModeCurLine = SourceMgr.getLineNumber(Loc);
+  EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
+  EmodeFileType = FileType;
+  
+  if (EmodeEmittedTokensOnThisLine) {
+    std::cout << "\n";
+    EmodeEmittedTokensOnThisLine = false;
+  }
+  
+  if (DisableLineMarkers) return;
+  
+  std::cout << "# " << EModeCurLine << " " << EModeCurFilename;
+  switch (Reason) {
+    case Preprocessor::EnterFile:
+      std::cout << " 1";
+      break;
+    case Preprocessor::ExitFile:
+      std::cout << " 2";
+      break;
+    case Preprocessor::SystemHeaderPragma: break;
+    case Preprocessor::RenameFile: break;
+  }
+  
+  if (FileType == DirectoryLookup::SystemHeaderDir)
+    std::cout << " 3";
+  else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
+    std::cout << " 3 4";
+  
+  std::cout << "\n";
+}
+
+static void HandleIdent(SourceLocation Loc, const std::string &Val) {
+  SourceManager &SourceMgr = EModePP->getSourceManager();
+  MoveToLine(SourceMgr.getLineNumber(Loc));
+  
+  std::cout << "#ident " << Val;
+  EmodeEmittedTokensOnThisLine = true;
+}
+
+/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
+/// is called for the first token on each new line.
+static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
+  // Figure out what line we went to and insert the appropriate number of
+  // newline characters.
+  unsigned LineNo = PP.getSourceManager().getLineNumber(Tok.getLocation());
+  
+  // Move to the specified line.
+  MoveToLine(LineNo);
+  
+  
+  // Print out space characters so that the first token on a line is
+  // indented for easy reading.
+  unsigned ColNo = 
+    PP.getSourceManager().getColumnNumber(Tok.getLocation());
+  
+  // This hack prevents stuff like:
+  // #define HASH #
+  // HASH define foo bar
+  // From having the # character end up at column 1, which makes it so it
+  // is not handled as a #define next time through the preprocessor if in
+  // -fpreprocessed mode.
+  if (ColNo <= 1 && Tok.getKind() == tok::hash)
+    std::cout << ' ';
+  
+  // Otherwise, indent the appropriate number of spaces.
+  for (; ColNo > 1; --ColNo)
+    std::cout << ' ';
+}
+
+struct UnknownPragmaHandler : public PragmaHandler {
+  const char *Prefix;
+  UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
+  virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
+    // Figure out what line we went to and insert the appropriate number of
+    // newline characters.
+    MoveToLine(PP.getSourceManager().getLineNumber(PragmaTok.getLocation()));
+    std::cout << Prefix;
+    
+    // Read and print all of the pragma tokens.
+    while (PragmaTok.getKind() != tok::eom) {
+      if (PragmaTok.hasLeadingSpace())
+        std::cout << ' ';
+      std::cout << PP.getSpelling(PragmaTok);
+      PP.LexUnexpandedToken(PragmaTok);
+    }
+    std::cout << "\n";
+  }
+};
+
+
+/// DoPrintPreprocessedInput - This implements -E mode.
+void clang::DoPrintPreprocessedInput(Preprocessor &PP) {
+  LexerToken Tok;
+  char Buffer[256];
+  EModeCurLine = 0;
+  EModeCurFilename = "\"<uninit>\"";
+  PP.setFileChangeHandler(HandleFileChange);
+  PP.setIdentHandler(HandleIdent);
+  EModePP = &PP;
+  EmodeEmittedTokensOnThisLine = false;
+  
+  PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
+  PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
+  do {
+    PP.Lex(Tok);
+    
+    // If this token is at the start of a line.  Emit the \n and indentation.
+    // FIXME: this shouldn't use the isAtStartOfLine flag.  This should use a
+    // "newline callback" from the lexer.
+    // FIXME: For some tests, this fails just because there is no col# info from
+    // macro expansions!
+    if (Tok.isAtStartOfLine()) {
+      HandleFirstTokOnLine(Tok, PP);
+    } else if (Tok.hasLeadingSpace()) {
+      std::cout << ' ';
+    }
+    
+    if (Tok.getLength() < 256) {
+      unsigned Len = PP.getSpelling(Tok, Buffer);
+      Buffer[Len] = 0;
+      std::cout << Buffer;
+    } else {
+      std::cout << PP.getSpelling(Tok);
+    }
+    EmodeEmittedTokensOnThisLine = true;
+  } while (Tok.getKind() != tok::eof);
+  std::cout << "\n";
+}
+

Propchange: cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: cfe/cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.cpp?rev=38658&r1=38657&r2=38658&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/clang.cpp (original)
+++ cfe/cfe/trunk/Driver/clang.cpp Wed Jul 11 11:23:35 2007
@@ -22,8 +22,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang.h"
 #include "clang/Lex/Preprocessor.h"
-#include "clang/Lex/Pragma.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceBuffer.h"
@@ -591,193 +591,6 @@
 
 
 //===----------------------------------------------------------------------===//
-// Preprocessed output mode.
-//===----------------------------------------------------------------------===//
-
-static cl::opt<bool>
-DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode"));
-
-static unsigned EModeCurLine;
-static std::string EModeCurFilename;
-static Preprocessor *EModePP;
-static bool EmodeEmittedTokensOnThisLine;
-static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir;
-
-static void MoveToLine(unsigned LineNo) {
-  // If this line is "close enough" to the original line, just print newlines,
-  // otherwise print a #line directive.
-  if (LineNo-EModeCurLine < 8) {
-    for (; EModeCurLine != LineNo; ++EModeCurLine)
-      std::cout << "\n";
-  } else {
-    if (EmodeEmittedTokensOnThisLine) {
-      std::cout << "\n";
-      EmodeEmittedTokensOnThisLine = false;
-    }
-
-    EModeCurLine = LineNo;
-    if (DisableLineMarkers) return;
-    
-    std::cout << "# " << LineNo << " " << EModeCurFilename;
-
-    if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
-      std::cout << " 3";
-    else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
-      std::cout << " 3 4";
-    std::cout << "\n";
-  } 
-}
-
-/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
-/// it invokes this handler.  Update our conception of the current 
-static void HandleFileChange(SourceLocation Loc,
-                             Preprocessor::FileChangeReason Reason,
-                             DirectoryLookup::DirType FileType) {
-  SourceManager &SourceMgr = EModePP->getSourceManager();
-
-  // Unless we are exiting a #include, make sure to skip ahead to the line the
-  // #include directive was at.
-  if (Reason == Preprocessor::EnterFile) {
-    SourceLocation IncludeLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
-    MoveToLine(SourceMgr.getLineNumber(IncludeLoc));
-  } else if (Reason == Preprocessor::SystemHeaderPragma) {
-    MoveToLine(SourceMgr.getLineNumber(Loc));
-    
-    // TODO GCC emits the # directive for this directive on the line AFTER the
-    // directive and emits a bunch of spaces that aren't needed.  Emulate this
-    // strange behavior.
-  }
-  
-  EModeCurLine = SourceMgr.getLineNumber(Loc);
-  EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
-  EmodeFileType = FileType;
-  
-  if (EmodeEmittedTokensOnThisLine) {
-    std::cout << "\n";
-    EmodeEmittedTokensOnThisLine = false;
-  }
-  
-  if (DisableLineMarkers) return;
-  
-  std::cout << "# " << EModeCurLine << " " << EModeCurFilename;
-  switch (Reason) {
-  case Preprocessor::EnterFile:
-    std::cout << " 1";
-    break;
-  case Preprocessor::ExitFile:
-    std::cout << " 2";
-    break;
-  case Preprocessor::SystemHeaderPragma: break;
-  case Preprocessor::RenameFile: break;
-  }
-  
-  if (FileType == DirectoryLookup::SystemHeaderDir)
-    std::cout << " 3";
-  else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
-    std::cout << " 3 4";
-    
-  std::cout << "\n";
-}
-
-static void HandleIdent(SourceLocation Loc, const std::string &Val) {
-  SourceManager &SourceMgr = EModePP->getSourceManager();
-  MoveToLine(SourceMgr.getLineNumber(Loc));
-
-  std::cout << "#ident " << Val;
-  EmodeEmittedTokensOnThisLine = true;
-}
-
-/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
-/// is called for the first token on each new line.
-static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
-  // Figure out what line we went to and insert the appropriate number of
-  // newline characters.
-  unsigned LineNo = PP.getSourceManager().getLineNumber(Tok.getLocation());
-  
-  // Move to the specified line.
-  MoveToLine(LineNo);
-
-  
-  // Print out space characters so that the first token on a line is
-  // indented for easy reading.
-  unsigned ColNo = 
-    PP.getSourceManager().getColumnNumber(Tok.getLocation());
-  
-  // This hack prevents stuff like:
-  // #define HASH #
-  // HASH define foo bar
-  // From having the # character end up at column 1, which makes it so it
-  // is not handled as a #define next time through the preprocessor if in
-  // -fpreprocessed mode.
-  if (ColNo <= 1 && Tok.getKind() == tok::hash)
-    std::cout << ' ';
-  
-  // Otherwise, indent the appropriate number of spaces.
-  for (; ColNo > 1; --ColNo)
-    std::cout << ' ';
-}
-
-struct UnknownPragmaHandler : public PragmaHandler {
-  const char *Prefix;
-  UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
-  virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
-    // Figure out what line we went to and insert the appropriate number of
-    // newline characters.
-    MoveToLine(PP.getSourceManager().getLineNumber(PragmaTok.getLocation()));
-    std::cout << Prefix;
-    
-    // Read and print all of the pragma tokens.
-    while (PragmaTok.getKind() != tok::eom) {
-      if (PragmaTok.hasLeadingSpace())
-        std::cout << ' ';
-      std::cout << PP.getSpelling(PragmaTok);
-      PP.LexUnexpandedToken(PragmaTok);
-    }
-    std::cout << "\n";
-  }
-};
-
-
-/// DoPrintPreprocessedInput - This implements -E mode.
-void DoPrintPreprocessedInput(Preprocessor &PP) {
-  LexerToken Tok;
-  char Buffer[256];
-  EModeCurLine = 0;
-  EModeCurFilename = "\"<uninit>\"";
-  PP.setFileChangeHandler(HandleFileChange);
-  PP.setIdentHandler(HandleIdent);
-  EModePP = &PP;
-  EmodeEmittedTokensOnThisLine = false;
-  
-  PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
-  PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
-  do {
-    PP.Lex(Tok);
-
-    // If this token is at the start of a line.  Emit the \n and indentation.
-    // FIXME: this shouldn't use the isAtStartOfLine flag.  This should use a
-    // "newline callback" from the lexer.
-    // FIXME: For some tests, this fails just because there is no col# info from
-    // macro expansions!
-    if (Tok.isAtStartOfLine()) {
-      HandleFirstTokOnLine(Tok, PP);
-    } else if (Tok.hasLeadingSpace()) {
-      std::cout << ' ';
-    }
-    
-    if (Tok.getLength() < 256) {
-      unsigned Len = PP.getSpelling(Tok, Buffer);
-      Buffer[Len] = 0;
-      std::cout << Buffer;
-    } else {
-      std::cout << PP.getSpelling(Tok);
-    }
-    EmodeEmittedTokensOnThisLine = true;
-  } while (Tok.getKind() != tok::eof);
-  std::cout << "\n";
-}
-
-//===----------------------------------------------------------------------===//
 // Main driver
 //===----------------------------------------------------------------------===//
 

Added: cfe/cfe/trunk/Driver/clang.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.h?rev=38658&view=auto

==============================================================================
--- cfe/cfe/trunk/Driver/clang.h (added)
+++ cfe/cfe/trunk/Driver/clang.h Wed Jul 11 11:23:35 2007
@@ -0,0 +1,27 @@
+//===--- clang.h - C-Language Front-end -----------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This is the header file that pulls together the top-level driver.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_CLANG_H
+#define LLVM_CLANG_CLANG_H
+
+namespace llvm {
+namespace clang {
+class Preprocessor;    
+
+/// DoPrintPreprocessedInput - Implement -E mode.
+void DoPrintPreprocessedInput(Preprocessor &PP);
+
+}  // end namespace clang
+}  // end namespace llvm
+
+#endif

Propchange: cfe/cfe/trunk/Driver/clang.h

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/Driver/clang.h

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision





More information about the cfe-commits mailing list