[cfe-commits] r46791 - in /cfe/trunk: Driver/ASTConsumers.cpp Driver/ASTConsumers.h Driver/clang.cpp test/CodeGen/bitfield.c test/CodeGen/merge-statics.c test/CodeGen/struct-x86-darwin.c test/Lexer/11-27-2007-FloatLiterals.c

Chris Lattner sabre at nondot.org
Tue Feb 5 17:42:26 PST 2008


Author: lattner
Date: Tue Feb  5 19:42:25 2008
New Revision: 46791

URL: http://llvm.org/viewvc/llvm-project?rev=46791&view=rev
Log:
pull .ll and .bc writing out of the ASTConsumer destructors into some top
level code in clang.  This is a cleanup, but does implement "-o" for 
-emit-llvm.  One effect of this is that "clang foo.c -emit-llvm" will now
emit into foo.ll instead of stdout.  Use "clang foo.c -emit-llvm -o -" or 
"clang < foo.c -emit-llvm" to get the old behavior.


Modified:
    cfe/trunk/Driver/ASTConsumers.cpp
    cfe/trunk/Driver/ASTConsumers.h
    cfe/trunk/Driver/clang.cpp
    cfe/trunk/test/CodeGen/bitfield.c
    cfe/trunk/test/CodeGen/merge-statics.c
    cfe/trunk/test/CodeGen/struct-x86-darwin.c
    cfe/trunk/test/Lexer/11-27-2007-FloatLiterals.c

Modified: cfe/trunk/Driver/ASTConsumers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTConsumers.cpp?rev=46791&r1=46790&r2=46791&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTConsumers.cpp (original)
+++ cfe/trunk/Driver/ASTConsumers.cpp Tue Feb  5 19:42:25 2008
@@ -22,8 +22,6 @@
 #include "clang/Analysis/Analyses/GRConstants.h"
 #include "clang/Analysis/LocalCheckers.h"
 #include "llvm/Support/Streams.h"
-#include <fstream>
-
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -607,15 +605,20 @@
     ASTContext *Ctx;
     const LangOptions &Features;
   protected:
-    llvm::Module *M;
+    llvm::Module *&M;
     CodeGen::CodeGenModule *Builder;
   public:
-    CodeGenerator(Diagnostic &diags, const LangOptions &LO)
-      : Diags(diags)
-      , Features(LO) {}
+    CodeGenerator(Diagnostic &diags, const LangOptions &LO,
+                  llvm::Module *&DestModule)
+      : Diags(diags), Features(LO), M(DestModule) {}
+    
+    ~CodeGenerator() {
+      CodeGen::Terminate(Builder);
+    }
+    
     virtual void Initialize(ASTContext &Context) {
       Ctx = &Context;
-      M = new llvm::Module("foo");
+      
       M->setTargetTriple(Ctx->Target.getTargetTriple());
       M->setDataLayout(Ctx->Target.getTargetDescription());
       TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
@@ -641,70 +644,10 @@
   };
 }
 
-namespace {
-  class LLVMEmitter : public CodeGenerator {
-  public:
-    LLVMEmitter(Diagnostic &diags, const LangOptions &LO)
-    : CodeGenerator(diags,LO) {}
-
-    ~LLVMEmitter() {
-      CodeGen::Terminate(Builder);
-      
-      // Print the generated code.
-      M->print(llvm::cout.stream());
-      delete M;
-    }
-  };
-}
-
-ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags, 
-                                      const LangOptions &Features) {
-  return new LLVMEmitter(Diags, Features);
-}
-
-namespace {
-  class BCWriter : public CodeGenerator {
-  public:
-    std::ostream& Out;
-
-    BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO)
-    : CodeGenerator(diags,LO)
-    , Out(*out) {}
-
-    ~BCWriter() {
-      CodeGen::Terminate(Builder);
-      llvm::WriteBitcodeToFile(M, Out);
-      delete M;
-    }
-  };
-}
-
-ASTConsumer *clang::CreateBCWriter(const std::string& InFile,
-                                   const std::string& OutputFile,
-                                   Diagnostic &Diags,
-                                   const LangOptions &Features) {
-  std::string FileName = OutputFile;
-  
-  std::ostream *Out;
-  if (OutputFile == "-")
-    Out = llvm::cout.stream();
-  else if (!OutputFile.size()) {
-    if (InFile == "-")
-      Out = llvm::cout.stream();
-    else {
-      llvm::sys::Path Path(InFile);
-      Path.eraseSuffix();
-      Path.appendSuffix("bc");
-      FileName = Path.toString();
-      Out = new std::ofstream(FileName.c_str(), 
-                              std::ios_base::binary|std::ios_base::out);
-    }
-  } else {
-    Out = new std::ofstream(FileName.c_str(), 
-                            std::ios_base::binary|std::ios_base::out);
-  }
-
-  return new BCWriter(Out, Diags, Features);
+ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags, 
+                                      const LangOptions &Features,
+                                      llvm::Module *&DestModule) {
+  return new CodeGenerator(Diags, Features, DestModule);
 }
 
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/Driver/ASTConsumers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTConsumers.h?rev=46791&r1=46790&r2=46791&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTConsumers.h (original)
+++ cfe/trunk/Driver/ASTConsumers.h Tue Feb  5 19:42:25 2008
@@ -16,8 +16,10 @@
 
 #include <iosfwd>
 
-namespace llvm { namespace sys { class Path; }}
-
+namespace llvm {
+  class Module;
+  namespace sys { class Path; }
+}
 namespace clang {
 
 class ASTConsumer;
@@ -41,12 +43,9 @@
   
 ASTConsumer *CreateGRConstants();
 
-ASTConsumer *CreateLLVMEmitter(Diagnostic &Diags, const LangOptions &Features);
-
-ASTConsumer *CreateBCWriter(const std::string& InFile,
-                            const std::string& OutFile,
-                            Diagnostic &Diags,
-                            const LangOptions &LOpts);
+  
+ASTConsumer *CreateLLVMCodeGen(Diagnostic &Diags, const LangOptions &Features,
+                               llvm::Module *&DestModule);
 
 ASTConsumer *CreateCodeRewriterTest(const std::string& InFile,
                                     Diagnostic &Diags);

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

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Tue Feb  5 19:42:25 2008
@@ -34,13 +34,16 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/Module.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Signals.h"
 #include "llvm/Config/config.h"
 #include "llvm/ADT/OwningPtr.h"
 #include <memory>
+#include <fstream>
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -930,7 +933,8 @@
 ///  parsed from source files as well as those deserialized from Bitcode.
 static ASTConsumer* CreateASTConsumer(const std::string& InFile,
                                       Diagnostic& Diag, FileManager& FileMgr, 
-                                      const LangOptions& LangOpts) {
+                                      const LangOptions& LangOpts,
+                                      llvm::Module *&DestModule) {
   switch (ProgAction) {
     default:
       return NULL;
@@ -964,10 +968,9 @@
       return CreateSerializationTest(Diag, FileMgr, LangOpts);
       
     case EmitLLVM:
-      return CreateLLVMEmitter(Diag, LangOpts);
-
     case EmitBC:
-      return CreateBCWriter(InFile, OutputFile, Diag, LangOpts);
+      DestModule = new llvm::Module(InFile);
+      return CreateLLVMCodeGen(Diag, LangOpts, DestModule);
 
     case SerializeAST:
       // FIXME: Allow user to tailor where the file is written.
@@ -985,13 +988,15 @@
 
   ASTConsumer* Consumer = NULL;
   bool ClearSourceMgr = false;
+  llvm::Module *CodeGenModule = 0;
   
   switch (ProgAction) {
   default:
     Consumer = CreateASTConsumer(InFile,
                                  PP.getDiagnostics(),
                                  PP.getFileManager(),
-                                 PP.getLangOptions());
+                                 PP.getLangOptions(),
+                                 CodeGenModule);
     
     if (!Consumer) {      
       fprintf(stderr, "Unexpected program action!\n");
@@ -1050,6 +1055,41 @@
     // This deletes Consumer.
     ParseAST(PP, Consumer, Stats);
   }
+
+  // If running the code generator, finish up now.
+  if (CodeGenModule) {
+    std::ostream *Out;
+    if (OutputFile == "-") {
+      Out = llvm::cout.stream();
+    } else if (!OutputFile.empty()) {
+      Out = new std::ofstream(OutputFile.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();
+      if (ProgAction == EmitLLVM)
+        Path.appendSuffix("ll");
+      else if (ProgAction == EmitBC)
+        Path.appendSuffix("bc");
+      else
+        assert(0 && "Unknown action");
+      Out = new std::ofstream(Path.toString().c_str(), 
+                              std::ios_base::binary|std::ios_base::out);
+    }
+    
+    if (ProgAction == EmitLLVM) {
+      CodeGenModule->print(*Out);
+    } else {
+      assert(ProgAction == EmitBC);
+      llvm::WriteBitcodeToFile(CodeGenModule, *Out);
+    }
+    
+    if (Out != llvm::cout.stream())
+      delete Out;
+    delete CodeGenModule;
+  }
   
   if (Stats) {
     fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
@@ -1093,8 +1133,10 @@
   
   // Observe that we use the source file name stored in the deserialized
   // translation unit, rather than InFile.
+  llvm::Module *DestModule;
   llvm::OwningPtr<ASTConsumer>
-    Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOpts()));
+    Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOpts(),
+                               DestModule));
   
   if (!Consumer) {      
     fprintf(stderr, "Unsupported program action with serialized ASTs!\n");
@@ -1103,6 +1145,7 @@
   
   Consumer->Initialize(*TU->getContext());
   
+  // FIXME: We need to inform Consumer about completed TagDecls as well.
   for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
     Consumer->HandleTopLevelDecl(*I);
 }

Modified: cfe/trunk/test/CodeGen/bitfield.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/bitfield.c?rev=46791&r1=46790&r2=46791&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/bitfield.c (original)
+++ cfe/trunk/test/CodeGen/bitfield.c Tue Feb  5 19:42:25 2008
@@ -1,4 +1,4 @@
-// RUN: clang %s -emit-llvm > %t1
+// RUN: clang %s -emit-llvm -o - > %t1
 // RUN: grep "shl i32 %tmp, 19" %t1 &&
 // RUN: grep "ashr i32 %tmp1, 19" %t1 &&
 // RUN: grep "shl i16 %tmp4, 1" %t1 &&

Modified: cfe/trunk/test/CodeGen/merge-statics.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/merge-statics.c?rev=46791&r1=46790&r2=46791&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/merge-statics.c (original)
+++ cfe/trunk/test/CodeGen/merge-statics.c Tue Feb  5 19:42:25 2008
@@ -1,4 +1,4 @@
-// RUN: clang %s -emit-llvm | grep internal | count 1
+// RUN: clang < %s -emit-llvm | grep internal | count 1
 
 // The two decls for 'a' should merge into one llvm GlobalVariable.
 

Modified: cfe/trunk/test/CodeGen/struct-x86-darwin.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/struct-x86-darwin.c?rev=46791&r1=46790&r2=46791&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/struct-x86-darwin.c (original)
+++ cfe/trunk/test/CodeGen/struct-x86-darwin.c Tue Feb  5 19:42:25 2008
@@ -1,4 +1,4 @@
-// RUN: clang %s -emit-llvm > %t1
+// RUN: clang < %s -emit-llvm > %t1
 // Run  grep "STest1 = type { i32, \[4 x i16\], double }" %t1 &&
 // RUN: grep "STest2 = type { i16, i16, i32, i32 }" %t1 &&
 // RUN: grep "STest3 = type { i8, i8, i16, i32 }" %t1 &&

Modified: cfe/trunk/test/Lexer/11-27-2007-FloatLiterals.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/11-27-2007-FloatLiterals.c?rev=46791&r1=46790&r2=46791&view=diff

==============================================================================
--- cfe/trunk/test/Lexer/11-27-2007-FloatLiterals.c (original)
+++ cfe/trunk/test/Lexer/11-27-2007-FloatLiterals.c Tue Feb  5 19:42:25 2008
@@ -1,5 +1,5 @@
-// RUN: clang %s -emit-llvm 2>&1 | grep 0x3BFD83C940000000 | count 2
-// RUN: clang %s -emit-llvm 2>&1 | grep 2.000000e+32 | count 2
+// RUN: clang %s -emit-llvm -o - | grep 0x3BFD83C940000000 | count 2
+// RUN: clang %s -emit-llvm -o - | grep 2.000000e+32 | count 2
 
 float  F  = 1e-19f;
 double D  = 2e32;





More information about the cfe-commits mailing list