Start adding support for printing out parser callbacks and adding ast building

llvm-svn: 38933
diff --git a/clang/Driver/PrintParserCallbacks.cpp b/clang/Driver/PrintParserCallbacks.cpp
new file mode 100644
index 0000000..9f10fb5
--- /dev/null
+++ b/clang/Driver/PrintParserCallbacks.cpp
@@ -0,0 +1,57 @@
+//===--- PrintParserActions.cpp - Implement -parse-print-callbacks mode ---===//
+//
+//                     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 code simply runs the preprocessor on the input file and prints out the
+// result.  This is the traditional behavior of the -E option.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/Lex/IdentifierTable.h"
+#include "clang/Parse/Action.h"
+#include "clang/Parse/Declarations.h"
+#include <iostream>
+
+using namespace llvm;
+using namespace clang;
+
+namespace {
+  class ParserPrintActions : public EmptyAction {
+    
+    /// ParseDeclarator - This callback is invoked when a declarator is parsed and
+    /// 'Init' specifies the initializer if any.  This is for things like:
+    /// "int X = 4" or "typedef int foo".
+    virtual void ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
+                                 ExprTy *Init) {
+      std::cout << "ParseDeclarator ";
+      if (IdentifierInfo *II = D.getIdentifier()) {
+        std::cout << "'" << II->getName() << "'";
+      } else {
+        std::cout << "<anon>";
+      }
+      std::cout << "\n";
+      
+      // Pass up to EmptyActions so that the symbol table is maintained right.
+      EmptyAction::ParseDeclarator(Loc, S, D, Init);
+    }
+    
+    /// PopScope - This callback is called immediately before the specified scope
+    /// is popped and deleted.
+    virtual void PopScope(SourceLocation Loc, Scope *S) {
+      std::cout << "PopScope\n";
+      
+      // Pass up to EmptyActions so that the symbol table is maintained right.
+      EmptyAction::PopScope(Loc, S);
+    }
+  };
+}
+
+Action *llvm::clang::CreatePrintParserActionsAction() {
+  return new ParserPrintActions();
+}
diff --git a/clang/Driver/clang.cpp b/clang/Driver/clang.cpp
index 7fcf8f2..d736e48 100644
--- a/clang/Driver/clang.cpp
+++ b/clang/Driver/clang.cpp
@@ -23,6 +23,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang.h"
+#include "clang/AST/AST.h"
 #include "clang/Parse/Parser.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/FileManager.h"
@@ -47,9 +48,10 @@
 Stats("stats", cl::desc("Print performance metrics and statistics"));
 
 enum ProgActions {
+  ParseSyntaxOnly,              // Parse and perform semantic analysis.
+  ParsePrintASTs,               // Parse and print raw ASTs.
   ParsePrintCallbacks,          // Parse and print each callback.
   ParseNoop,                    // Parse with noop callbacks.
-  ParseSyntaxOnly,              // Parse and perform semantic analysis.
   RunPreprocessorOnly,          // Just lex, no output.
   PrintPreprocessedInput,       // -E mode.
   DumpTokens                    // Token dump mode.
@@ -57,7 +59,7 @@
 
 static cl::opt<ProgActions> 
 ProgAction(cl::desc("Choose output type:"), cl::ZeroOrMore,
-           cl::init(ParseSyntaxOnly),
+           cl::init(ParsePrintASTs),
            cl::values(
              clEnumValN(RunPreprocessorOnly, "Eonly",
                         "Just run preprocessor, no output (for timings)"),
@@ -65,13 +67,14 @@
                         "Run preprocessor, emit preprocessed file"),
              clEnumValN(DumpTokens, "dumptokens",
                         "Run preprocessor, dump internal rep of tokens"),
-             clEnumValN(ParseSyntaxOnly, "fsyntax-only",
-                        "Run parser and perform semantic analysis"),
-             clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
-                        "Run parser and print each callback invoked"),
              clEnumValN(ParseNoop, "parse-noop",
                         "Run parser with noop callbacks (for timings)"),
-             // TODO: NULL PARSER.
+             clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
+                        "Run parser and print each callback invoked"),
+             clEnumValN(ParsePrintASTs, "parse-print-ast",
+                        "Run parser and print raw ASTs"),
+             clEnumValN(ParseSyntaxOnly, "fsyntax-only",
+                        "Run parser and perform semantic analysis"),
              clEnumValEnd));
 
 
@@ -748,8 +751,13 @@
     break;
 
   case ParseNoop:                    // -parse-noop
+    ParseFile(PP, new EmptyAction(), MainFileID);
+    break;
   case ParsePrintCallbacks:
-    //ParseFile(PP, new ParserPrintActions(PP), MainFileID);
+    ParseFile(PP, CreatePrintParserActionsAction(), MainFileID);
+    break;
+  case ParsePrintASTs:
+    // 
     break;
   case ParseSyntaxOnly:              // -fsyntax-only
     ParseFile(PP, new EmptyAction(), MainFileID);
diff --git a/clang/Driver/clang.h b/clang/Driver/clang.h
index b81b9aa..f997d61 100644
--- a/clang/Driver/clang.h
+++ b/clang/Driver/clang.h
@@ -18,11 +18,16 @@
 namespace clang {
 class Preprocessor;
 class LangOptions;
+class Action;
 
 /// DoPrintPreprocessedInput - Implement -E mode.
 void DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
                               LangOptions &Options);
 
+/// CreatePrintParserActionsAction - Return the actions implementation that
+/// implements the -parse-print-callbacks option.
+Action *CreatePrintParserActionsAction();
+
 }  // end namespace clang
 }  // end namespace llvm