]> granicus.if.org Git - clang/commitdiff
start implementation of a macro rewriter, this is currently just stubbed out.
authorChris Lattner <sabre@nondot.org>
Thu, 8 May 2008 06:52:13 +0000 (06:52 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 8 May 2008 06:52:13 +0000 (06:52 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@50845 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/RewriteMacros.cpp [new file with mode: 0644]
Driver/clang.cpp
Driver/clang.h
clang.xcodeproj/project.pbxproj

diff --git a/Driver/RewriteMacros.cpp b/Driver/RewriteMacros.cpp
new file mode 100644 (file)
index 0000000..4f75e8b
--- /dev/null
@@ -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
+  
+}
+
+
+
index 96f6dde1ec964058ac39afac3f37b2bc45820a4a..e3362eccd9ede58e769f2a183fa51917cb966dc5 100644 (file)
@@ -63,6 +63,7 @@ Stats("print-stats",
 
 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 @@ ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
              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 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
     DoPrintPreprocessedInput(PP, OutputFile);
     ClearSourceMgr = true;
     break;
-    
+      
   case ParseNoop:                    // -parse-noop
     ParseFile(PP, new MinimalAction(PP.getIdentifierTable()));
     ClearSourceMgr = true;
@@ -1232,6 +1235,11 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
   case ParseSyntaxOnly:              // -fsyntax-only
     Consumer = new ASTConsumer();
     break;
+      
+  case RewriteMacros:
+    RewriteMacrosInInput(PP, OutputFile);
+    ClearSourceMgr = true;
+    break;
   }
   
   if (Consumer) {
index 97a948056605b831a243818e0b750547619ab2a5..00f80d362fc79f290e5f1e425ede7f0138b3085b 100644 (file)
@@ -29,6 +29,9 @@ class SourceManager;
 /// 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 &);
index 15df031d1d961056815b7ce5140ec2dc27067110..d0753a3e51b30eb3f05d7eff57d7e11b35b30c08 100644 (file)
                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 */; };
                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>"; };
                                3574BC2A0D9B531D00DF491A /* HTMLDiagnostics.cpp */,
                                72D16C210D9975EA00E6DA4A /* HTMLPrint.cpp */,
                                035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */,
+                               DEA0EBD90DD2D3C8007A02A9 /* RewriteMacros.cpp */,
                                DEB0AEBA0C2087AB00718A22 /* TextDiagnostics.cpp */,
                                DEB0AEB80C2087A700718A22 /* TextDiagnostics.h */,
                                F0226FD00C18084500141F42 /* TextDiagnosticPrinter.cpp */,
                                DECAB0D00DB3C84200E13CCB /* RewriteRope.cpp in Sources */,
                                035611E20DB40C8100D2EF2A /* RewriteObjC.cpp in Sources */,
                                35EFEFB60DB67ED60020783D /* GRTransferFuncs.cpp in Sources */,
+                               DEA0EBDA0DD2D3C8007A02A9 /* RewriteMacros.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };