Fix a couple of bugs, add some new cool stuff.

1. Fix a todo in Parser::ParseTag, to recover better.  On code like
   that in test/Sema/decl-invalid.c it causes us to return a single
   error instead of multiple.
2. Fix an error in Sema::ParseDeclarator, where it would crash if the
   declarator didn't have an identifier.  Instead, diagnose the problem.
3. Start adding infrastructure to track the range of locations covered
   by a declspec or declarator.  This is mostly implemented for declspec,
   but could be improved, it is missing for declarator.

Thanks to Neil for pointing out this crash.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@40482 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/LLVMCodegen.cpp b/Driver/LLVMCodegen.cpp
new file mode 100644
index 0000000..b969ff8
--- /dev/null
+++ b/Driver/LLVMCodegen.cpp
@@ -0,0 +1,70 @@
+//===--- 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.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);
+}
+