diff options
author | Chris Lattner <sabre@nondot.org> | 2008-03-22 00:08:40 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-03-22 00:08:40 +0000 |
commit | c68ab77068d1aef6c31f18e941b79201be0f71f3 (patch) | |
tree | 7eca8b5b178dcb310df55a080fc112118f5fc4e4 /Driver/RewriteTest.cpp | |
parent | 21fbe0d9eaf7feac7b7593a3917a8b555eaec714 (diff) |
Teach the rewriter how to respect the -o option.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48669 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Driver/RewriteTest.cpp')
-rw-r--r-- | Driver/RewriteTest.cpp | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index 239fa2d026..89de81bb38 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -23,7 +23,9 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/CommandLine.h" +#include "llvm/System/Path.h" #include <sstream> +#include <fstream> using namespace clang; using llvm::utostr; @@ -81,6 +83,8 @@ namespace { // Needed for header files being rewritten bool IsHeader; + std::ostream &OutFile; + static const int OBJC_ABI_VERSION =7 ; public: void Initialize(ASTContext &context); @@ -89,8 +93,9 @@ namespace { // Top Level Driver code. virtual void HandleTopLevelDecl(Decl *D); void HandleDeclInMainFile(Decl *D); - RewriteTest(bool isHeader, Diagnostic &D, const LangOptions &LOpts) : - Diags(D), LangOpts(LOpts) { + RewriteTest(bool isHeader, std::ostream &outFile, + Diagnostic &D, const LangOptions &LOpts) + : Diags(D), LangOpts(LOpts), OutFile(outFile) { IsHeader = isHeader; RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, "rewriting sub-expression within a macro (may not be correct)"); @@ -231,9 +236,28 @@ static bool IsHeaderFile(const std::string &Filename) { } ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, + const std::string& OutFile, Diagnostic &Diags, const LangOptions &LOpts) { - return new RewriteTest(IsHeaderFile(InFile), Diags, LOpts); + // Create the output file. + + std::ostream *Out; + if (OutFile == "-") { + Out = llvm::cout.stream(); + } else if (!OutFile.empty()) { + Out = new std::ofstream(OutFile.c_str(), + std::ios_base::binary|std::ios_base::out); + } else if (InFile == "-") { + Out = llvm::cout.stream(); + } else { + llvm::sys::Path Path(InFile); + Path.eraseSuffix(); + Path.appendSuffix("cpp"); + Out = new std::ofstream(Path.toString().c_str(), + std::ios_base::binary|std::ios_base::out); + } + + return new RewriteTest(IsHeaderFile(InFile), *Out, Diags, LOpts); } void RewriteTest::Initialize(ASTContext &context) { @@ -429,13 +453,12 @@ RewriteTest::~RewriteTest() { if (const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(MainFileID)) { //printf("Changed:\n"); - std::string S(RewriteBuf->begin(), RewriteBuf->end()); - printf("%s\n", S.c_str()); + OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); } else { - printf("No changes\n"); + fprintf(stderr, "No changes\n"); } // Emit metadata. - printf("%s", ResultStr.c_str()); + OutFile << ResultStr; } //===----------------------------------------------------------------------===// |