]> granicus.if.org Git - clang/commitdiff
Teach the rewriter how to respect the -o option.
authorChris Lattner <sabre@nondot.org>
Sat, 22 Mar 2008 00:08:40 +0000 (00:08 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 22 Mar 2008 00:08:40 +0000 (00:08 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48669 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/ASTConsumers.h
Driver/RewriteTest.cpp
Driver/clang.cpp
test/Rewriter/objc-ivar-receiver-1.m

index 9c8ae4064c030e6ba79bdd1ff5d048240cbd39e5..c72f95ec73fcc4d0bdda9ffcf1c26621326559ee 100644 (file)
@@ -50,6 +50,7 @@ ASTConsumer* CreateCFRefChecker(Diagnostic &Diags,
                                 const std::string& FunctionName); 
 
 ASTConsumer *CreateCodeRewriterTest(const std::string& InFile,
+                                    const std::string& OutFile,
                                     Diagnostic &Diags,
                                     const LangOptions &LOpts);
 
index 239fa2d0266da16c9f8799593336829759e32966..89de81bb38f183de21303fb64db92702f330f107 100644 (file)
@@ -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;
 }
 
 //===----------------------------------------------------------------------===//
index b2852e2cd903e28b18c0aaa723ba26563920b94a..f3b8765ea8377026013624106bd88888dd0a5621 100644 (file)
@@ -1044,7 +1044,7 @@ static ASTConsumer* CreateASTConsumer(const std::string& InFile,
       return CreateASTSerializer(InFile, OutputFile, Diag, LangOpts);
       
     case RewriteTest:
-      return CreateCodeRewriterTest(InFile, Diag, LangOpts);
+      return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
   }
 }
 
index 27287dc275e2d48c08ecbe38fe482947e0d76b2b..7b01124135a677ebd3e65120cbcb5009ee42e6e3 100644 (file)
@@ -1,5 +1,5 @@
 // RUN: clang -rewrite-test %s 
-// RUN: clang -rewrite-test %s | grep 'newInv->_container'
+// RUN: clang -rewrite-test %s -o - | grep 'newInv->_container'
 
 @interface NSMutableArray 
 - (void)addObject:(id)addObject;