diff options
Diffstat (limited to 'clang')
-rw-r--r-- | clang/Driver/RewriteMacros.cpp | 76 | ||||
-rw-r--r-- | clang/Driver/clang.cpp | 12 | ||||
-rw-r--r-- | clang/Driver/clang.h | 3 | ||||
-rw-r--r-- | clang/clang.xcodeproj/project.pbxproj | 4 |
4 files changed, 93 insertions, 2 deletions
diff --git a/clang/Driver/RewriteMacros.cpp b/clang/Driver/RewriteMacros.cpp new file mode 100644 index 00000000000..4f75e8b267e --- /dev/null +++ b/clang/Driver/RewriteMacros.cpp @@ -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 + +} + + + diff --git a/clang/Driver/clang.cpp b/clang/Driver/clang.cpp index 96f6dde1ec9..e3362eccd9e 100644 --- a/clang/Driver/clang.cpp +++ b/clang/Driver/clang.cpp @@ -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) { diff --git a/clang/Driver/clang.h b/clang/Driver/clang.h index 97a94805660..00f80d362fc 100644 --- a/clang/Driver/clang.h +++ b/clang/Driver/clang.h @@ -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 &); diff --git a/clang/clang.xcodeproj/project.pbxproj b/clang/clang.xcodeproj/project.pbxproj index 15df031d1d9..d0753a3e51b 100644 --- a/clang/clang.xcodeproj/project.pbxproj +++ b/clang/clang.xcodeproj/project.pbxproj @@ -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; }; |