[cfe-commits] r50845 - in /cfe/trunk: Driver/RewriteMacros.cpp Driver/clang.cpp Driver/clang.h clang.xcodeproj/project.pbxproj

Chris Lattner sabre at nondot.org
Wed May 7 23:52:13 PDT 2008


Author: lattner
Date: Thu May  8 01:52:13 2008
New Revision: 50845

URL: http://llvm.org/viewvc/llvm-project?rev=50845&view=rev
Log:
start implementation of a macro rewriter, this is currently just stubbed out.

Added:
    cfe/trunk/Driver/RewriteMacros.cpp
Modified:
    cfe/trunk/Driver/clang.cpp
    cfe/trunk/Driver/clang.h
    cfe/trunk/clang.xcodeproj/project.pbxproj

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

==============================================================================
--- cfe/trunk/Driver/RewriteMacros.cpp (added)
+++ cfe/trunk/Driver/RewriteMacros.cpp Thu May  8 01:52:13 2008
@@ -0,0 +1,76 @@
+//===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This code rewrites macro invocations into their expansions.  This gives you
+// a macro expanded file that retains comments and #includes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/Rewrite/Rewriter.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Streams.h"
+#include "llvm/System/Path.h"
+#include <fstream>
+using namespace clang;
+
+/// RewriteMacrosInInput - Implement -rewrite-macros mode.
+void clang::RewriteMacrosInInput(Preprocessor &PP,
+                                 const std::string &OutFileName) {
+  SourceManager &SM = PP.getSourceManager();
+  
+  Rewriter Rewrite;
+  Rewrite.setSourceMgr(SM);
+
+#if 0
+  
+  // Get the ID and start/end of the main file.
+  unsigned MainFileID = SM.getMainFileID();
+  const llvm::MemoryBuffer *MainBuf = SM.getBuffer(MainFileID);
+  const char *MainFileStart = MainBuf->getBufferStart();
+  const char *MainFileEnd = MainBuf->getBufferEnd();
+ 
+  
+  // Create the output file.
+  
+  std::ostream *OutFile;
+  if (OutFileName == "-") {
+    OutFile = llvm::cout.stream();
+  } else if (!OutFileName.empty()) {
+    OutFile = new std::ofstream(OutFileName.c_str(), 
+                                std::ios_base::binary|std::ios_base::out);
+  } else if (InFileName == "-") {
+    OutFile = llvm::cout.stream();
+  } else {
+    llvm::sys::Path Path(InFileName);
+    Path.eraseSuffix();
+    Path.appendSuffix("cpp");
+    OutFile = new std::ofstream(Path.toString().c_str(), 
+                                std::ios_base::binary|std::ios_base::out);
+  }
+
+  // Get the buffer corresponding to MainFileID.  If we haven't changed it, then
+  // we are done.
+  if (const RewriteBuffer *RewriteBuf = 
+      Rewrite.getRewriteBufferFor(MainFileID)) {
+    //printf("Changed:\n");
+    *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
+  } else {
+    fprintf(stderr, "No changes\n");
+  }
+  // Emit metadata.
+  *OutFile << ResultStr;
+#endif
+  
+}
+
+
+

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

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Thu May  8 01:52:13 2008
@@ -63,6 +63,7 @@
 
 enum ProgActions {
   RewriteObjC,                  // ObjC->C Rewriter.
+  RewriteMacros,                // Expand macros but not #includes.
   HTMLTest,                     // HTML displayer testing stuff.
   EmitLLVM,                     // Emit a .ll file.
   EmitBC,                       // Emit a .bc file.
@@ -136,7 +137,9 @@
              clEnumValN(SerializeAST, "serialize",
                         "Build ASTs and emit .ast file"),
              clEnumValN(RewriteObjC, "rewrite-objc",
-                        "Playground for the code rewriter"),                            
+                        "Rewrite ObjC into C (code rewriter example)"),
+             clEnumValN(RewriteMacros, "rewrite-macros",
+                        "Expand macros without full preprocessing"),
              clEnumValEnd));
 
 
@@ -1218,7 +1221,7 @@
     DoPrintPreprocessedInput(PP, OutputFile);
     ClearSourceMgr = true;
     break;
-    
+      
   case ParseNoop:                    // -parse-noop
     ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
     ClearSourceMgr = true;
@@ -1232,6 +1235,11 @@
   case ParseSyntaxOnly:              // -fsyntax-only
     Consumer = new ASTConsumer();
     break;
+      
+  case RewriteMacros:
+    RewriteMacrosInInput(PP, OutputFile);
+    ClearSourceMgr = true;
+    break;
   }
   
   if (Consumer) {

Modified: cfe/trunk/Driver/clang.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.h?rev=50845&r1=50844&r2=50845&view=diff

==============================================================================
--- cfe/trunk/Driver/clang.h (original)
+++ cfe/trunk/Driver/clang.h Thu May  8 01:52:13 2008
@@ -29,6 +29,9 @@
 /// DoPrintPreprocessedInput - Implement -E mode.
 void DoPrintPreprocessedInput(Preprocessor &PP, const std::string& OutFile);
 
+/// RewriteMacrosInInput - Implement -rewrite-macros mode.
+void RewriteMacrosInInput(Preprocessor &PP, const std::string& OutFile);
+  
 /// CreatePrintParserActionsAction - Return the actions implementation that
 /// implements the -parse-print-callbacks option.
 MinimalAction *CreatePrintParserActionsAction(IdentifierTable &);

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=50845&r1=50844&r2=50845&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Thu May  8 01:52:13 2008
@@ -124,6 +124,7 @@
 		DE928B7F0C0A615600231DA4 /* CodeGenModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE928B7E0C0A615600231DA4 /* CodeGenModule.cpp */; };
 		DE928B810C0A615B00231DA4 /* CodeGenFunction.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE928B800C0A615B00231DA4 /* CodeGenFunction.h */; };
 		DE928B830C0A616000231DA4 /* CodeGenFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE928B820C0A616000231DA4 /* CodeGenFunction.cpp */; };
+		DEA0EBDA0DD2D3C8007A02A9 /* RewriteMacros.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DEA0EBD90DD2D3C8007A02A9 /* RewriteMacros.cpp */; };
 		DEAEE98B0A5A2B970045101B /* MultipleIncludeOpt.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEAEE98A0A5A2B970045101B /* MultipleIncludeOpt.h */; };
 		DEAEED4B0A5AF89A0045101B /* NOTES.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEAEED4A0A5AF89A0045101B /* NOTES.txt */; };
 		DEB0AEB90C2087A700718A22 /* TextDiagnostics.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEB0AEB80C2087A700718A22 /* TextDiagnostics.h */; };
@@ -413,6 +414,7 @@
 		DE928B7E0C0A615600231DA4 /* CodeGenModule.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = CodeGenModule.cpp; path = lib/CodeGen/CodeGenModule.cpp; sourceTree = "<group>"; };
 		DE928B800C0A615B00231DA4 /* CodeGenFunction.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = CodeGenFunction.h; path = lib/CodeGen/CodeGenFunction.h; sourceTree = "<group>"; };
 		DE928B820C0A616000231DA4 /* CodeGenFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = CodeGenFunction.cpp; path = lib/CodeGen/CodeGenFunction.cpp; sourceTree = "<group>"; };
+		DEA0EBD90DD2D3C8007A02A9 /* RewriteMacros.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RewriteMacros.cpp; path = Driver/RewriteMacros.cpp; sourceTree = "<group>"; };
 		DEAEE98A0A5A2B970045101B /* MultipleIncludeOpt.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = MultipleIncludeOpt.h; sourceTree = "<group>"; };
 		DEAEED4A0A5AF89A0045101B /* NOTES.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = NOTES.txt; sourceTree = "<group>"; };
 		DEB0AEB80C2087A700718A22 /* TextDiagnostics.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = TextDiagnostics.h; path = Driver/TextDiagnostics.h; sourceTree = "<group>"; };
@@ -735,6 +737,7 @@
 				3574BC2A0D9B531D00DF491A /* HTMLDiagnostics.cpp */,
 				72D16C210D9975EA00E6DA4A /* HTMLPrint.cpp */,
 				035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */,
+				DEA0EBD90DD2D3C8007A02A9 /* RewriteMacros.cpp */,
 				DEB0AEBA0C2087AB00718A22 /* TextDiagnostics.cpp */,
 				DEB0AEB80C2087A700718A22 /* TextDiagnostics.h */,
 				F0226FD00C18084500141F42 /* TextDiagnosticPrinter.cpp */,
@@ -1073,6 +1076,7 @@
 				DECAB0D00DB3C84200E13CCB /* RewriteRope.cpp in Sources */,
 				035611E20DB40C8100D2EF2A /* RewriteObjC.cpp in Sources */,
 				35EFEFB60DB67ED60020783D /* GRTransferFuncs.cpp in Sources */,
+				DEA0EBDA0DD2D3C8007A02A9 /* RewriteMacros.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};





More information about the cfe-commits mailing list