]> granicus.if.org Git - clang/commitdiff
switch the llvm emitter to ASTConsumer interface.
authorChris Lattner <sabre@nondot.org>
Sun, 16 Sep 2007 19:46:59 +0000 (19:46 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 16 Sep 2007 19:46:59 +0000 (19:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42013 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/ASTStreamers.cpp
Driver/ASTStreamers.h
Driver/LLVMCodegen.cpp [deleted file]
Driver/clang.cpp
clang.xcodeproj/project.pbxproj

index 3c67d9524a84391c13a2dddbc700ace1ac2413db..7ef3001e3ba410a25c337b6f69a8445263fec4e8 100644 (file)
@@ -232,3 +232,57 @@ namespace {
 ASTConsumer *clang::CreateDeadStoreChecker(Diagnostic &Diags) {
   return new DeadStoreVisitor(Diags);
 }
+
+//===----------------------------------------------------------------------===//
+// LLVM Emitter
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/CodeGen/ModuleBuilder.h"
+#include "llvm/Module.h"
+#include <iostream>
+
+namespace {
+  class LLVMEmitter : public ASTConsumer {
+    Diagnostic &Diags;
+    llvm::Module *M;
+    ASTContext *Ctx;
+    CodeGen::BuilderTy *Builder;
+  public:
+    LLVMEmitter(Diagnostic &diags) : Diags(diags) {}
+    virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
+      Ctx = &Context;
+      M = new llvm::Module("foo");
+      Builder = CodeGen::Init(Context, *M);
+    }
+    
+    virtual void HandleTopLevelDecl(Decl *D) {
+      // If an error occurred, stop code generation, but continue parsing and
+      // semantic analysis (to ensure all warnings and errors are emitted).
+      if (Diags.hasErrorOccurred())
+        return;
+      
+      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+        CodeGen::CodeGenFunction(Builder, FD);
+      } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
+        CodeGen::CodeGenGlobalVar(Builder, FVD);
+      } else {
+        assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
+        // don't codegen for now, eventually pass down for debug info.
+        //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
+      }
+    }
+    
+    ~LLVMEmitter() {
+      CodeGen::Terminate(Builder);
+      
+      // Print the generated code.
+      M->print(std::cout);
+      delete M;
+    }
+  }; 
+} // end anonymous namespace
+
+ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags) {
+  return new LLVMEmitter(Diags);
+}
+
index 60b23ef70d332502f9a52dc12db9fea4644b9448..d9ea3bdcf3ca0e9bdce660c793a5487d3d4f0b4a 100644 (file)
@@ -24,6 +24,7 @@ ASTConsumer *CreateASTDumper();
 ASTConsumer *CreateCFGDumper(bool ViewGraphs = false);
 ASTConsumer *CreateLiveVarAnalyzer();
 ASTConsumer *CreateDeadStoreChecker(Diagnostic &Diags);
+ASTConsumer *CreateLLVMEmitter(Diagnostic &Diags);
 
 } // end clang namespace
 
diff --git a/Driver/LLVMCodegen.cpp b/Driver/LLVMCodegen.cpp
deleted file mode 100644 (file)
index 95d23f5..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-//===--- LLVMCodegen.cpp - Emit LLVM Code from ASTs -----------------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file was developed by Chris Lattner and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This builds an AST and converts it to LLVM Code.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang.h"
-#include "clang/CodeGen/ModuleBuilder.h"
-#include "clang/Sema/ASTStreamer.h"
-#include "clang/AST/AST.h"
-#include "clang/Lex/Preprocessor.h"
-#include "clang/Basic/Diagnostic.h"
-#include "llvm/Module.h"
-#include <iostream>
-using namespace clang;
-
-//===----------------------------------------------------------------------===//
-// LLVM Emission
-//===----------------------------------------------------------------------===//
-
-void clang::EmitLLVMFromASTs(Preprocessor &PP, unsigned MainFileID,
-                             bool PrintStats) {
-  Diagnostic &Diags = PP.getDiagnostics();
-  // Create the streamer to read the file.
-  ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
-                     PP.getIdentifierTable());
-  ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
-  
-  // Create the module to codegen into.
-  llvm::Module M("foo");
-  
-  CodeGen::BuilderTy *Builder = CodeGen::Init(Context, M);
-  
-  while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
-    // If an error occurred, stop code generation, but continue parsing and
-    // semantic analysis (to ensure all warnings and errors are emitted).
-    if (Diags.hasErrorOccurred())
-      continue;
-    
-    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-      CodeGen::CodeGenFunction(Builder, FD);
-    } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
-      CodeGen::CodeGenGlobalVar(Builder, FVD);
-    } else {
-      assert(isa<TypedefDecl>(D) && "Only expected typedefs here");
-      // don't codegen for now, eventually pass down for debug info.
-      //std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
-    }
-  }
-  
-  if (PrintStats) {
-    std::cerr << "\nSTATISTICS:\n";
-    CodeGen::PrintStats(Builder);
-    ASTStreamer_PrintStats(Streamer);
-    Context.PrintStats();
-  }
-  
-  CodeGen::Terminate(Builder);
-  ASTStreamer_Terminate(Streamer);
-  
-  // Print the generated code.
-  M.print(std::cout);
-}
-
index 7301354c176d14c22618f51599d45c7db67cc6ae..405180671823972d2584d1e0f1901362ac0d2aff 100644 (file)
@@ -870,9 +870,11 @@ static void ProcessInputFile(Preprocessor &PP, unsigned MainFileID,
     ParseAST(PP, MainFileID, *C.get(), Stats);
     break;
   }
-  case EmitLLVM:
-    EmitLLVMFromASTs(PP, MainFileID, Stats);
+  case EmitLLVM: {
+    std::auto_ptr<ASTConsumer> C(CreateLLVMEmitter(PP.getDiagnostics()));
+    ParseAST(PP, MainFileID, *C.get(), Stats);
     break;
+  }
   case ParseASTCheck:
     exit(CheckDiagnostics(PP, MainFileID));
     break;
index 578a156700ed1489889d20145e390f4d1a7f49b6..985c974dbac0c78174d74fb26b33b9b9a36a7acf 100644 (file)
@@ -70,7 +70,6 @@
                DE6954640C5121BD00A5826B /* Token.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE6954630C5121BD00A5826B /* Token.h */; };
                DE75ED290B044DC90020CF81 /* ASTContext.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE75ED280B044DC90020CF81 /* ASTContext.h */; };
                DE75EDF10B06880E0020CF81 /* Type.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE75EDF00B06880E0020CF81 /* Type.cpp */; };
-               DE927FFD0C055DE900231DA4 /* LLVMCodegen.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE927FFC0C055DE900231DA4 /* LLVMCodegen.cpp */; };
                DE928B130C05659200231DA4 /* ModuleBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE928B120C05659200231DA4 /* ModuleBuilder.cpp */; };
                DE928B200C0565B000231DA4 /* ModuleBuilder.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE928B1F0C0565B000231DA4 /* ModuleBuilder.h */; };
                DE928B7D0C0A615100231DA4 /* CodeGenModule.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE928B7C0C0A615100231DA4 /* CodeGenModule.h */; };
                35AE0F680C9B4CC200CC1279 /* UnintializedValues.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UnintializedValues.cpp; path = Analysis/UnintializedValues.cpp; sourceTree = "<group>"; };
                84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
                84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
-               8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+               8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
                DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
                DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
                DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };
                DE6954630C5121BD00A5826B /* Token.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = Token.h; sourceTree = "<group>"; };
                DE75ED280B044DC90020CF81 /* ASTContext.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ASTContext.h; path = clang/AST/ASTContext.h; sourceTree = "<group>"; };
                DE75EDF00B06880E0020CF81 /* Type.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Type.cpp; path = AST/Type.cpp; sourceTree = "<group>"; };
-               DE927FFC0C055DE900231DA4 /* LLVMCodegen.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = LLVMCodegen.cpp; path = Driver/LLVMCodegen.cpp; sourceTree = "<group>"; };
                DE928B120C05659200231DA4 /* ModuleBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ModuleBuilder.cpp; path = CodeGen/ModuleBuilder.cpp; sourceTree = "<group>"; };
                DE928B1F0C0565B000231DA4 /* ModuleBuilder.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ModuleBuilder.h; path = clang/CodeGen/ModuleBuilder.h; sourceTree = "<group>"; };
                DE928B7C0C0A615100231DA4 /* CodeGenModule.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = CodeGenModule.h; path = CodeGen/CodeGenModule.h; sourceTree = "<group>"; };
                                DED67AEF0B6DB92F00AAD4A3 /* PPCBuiltins.def */,
                                DED67AED0B6DB92A00AAD4A3 /* X86Builtins.def */,
                                DEC82DC30C32D50A00BAC245 /* DiagChecker.cpp */,
-                               DE927FFC0C055DE900231DA4 /* LLVMCodegen.cpp */,
                                DE5932CF0AD60FF400BC794C /* PrintParserCallbacks.cpp */,
                                DE5932D00AD60FF400BC794C /* PrintPreprocessedOutput.cpp */,
                                DED627020AE0C51D001E80A4 /* Targets.cpp */,
                                DE67E7170C020EE400F66BC5 /* Sema.cpp in Sources */,
                                DE67E71A0C020F4F00F66BC5 /* ASTStreamer.cpp in Sources */,
                                DE06756C0C051CFE00EBBFD8 /* ParseExprCXX.cpp in Sources */,
-                               DE927FFD0C055DE900231DA4 /* LLVMCodegen.cpp in Sources */,
                                DE928B130C05659200231DA4 /* ModuleBuilder.cpp in Sources */,
                                DE928B7F0C0A615600231DA4 /* CodeGenModule.cpp in Sources */,
                                DE928B830C0A616000231DA4 /* CodeGenFunction.cpp in Sources */,