[cfe-commits] r41991 - in /cfe/trunk: Driver/ASTStreamers.cpp Driver/ASTStreamers.h Driver/DiagChecker.cpp Driver/clang.cpp Sema/ASTStreamer.cpp include/clang/AST/ASTConsumer.h include/clang/Sema/ASTStreamer.h

Chris Lattner sabre at nondot.org
Sat Sep 15 15:56:57 PDT 2007


Author: lattner
Date: Sat Sep 15 17:56:56 2007
New Revision: 41991

URL: http://llvm.org/viewvc/llvm-project?rev=41991&view=rev
Log:
add a new ASTConsumer consumer to simplify stuff in the driver.
Switch -parse-ast over to it.

Added:
    cfe/trunk/include/clang/AST/ASTConsumer.h
Modified:
    cfe/trunk/Driver/ASTStreamers.cpp
    cfe/trunk/Driver/ASTStreamers.h
    cfe/trunk/Driver/DiagChecker.cpp
    cfe/trunk/Driver/clang.cpp
    cfe/trunk/Sema/ASTStreamer.cpp
    cfe/trunk/include/clang/Sema/ASTStreamer.h

Modified: cfe/trunk/Driver/ASTStreamers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTStreamers.cpp?rev=41991&r1=41990&r2=41991&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTStreamers.cpp (original)
+++ cfe/trunk/Driver/ASTStreamers.cpp Sat Sep 15 17:56:56 2007
@@ -20,32 +20,6 @@
 #include "clang/Sema/ASTStreamer.h"
 using namespace clang;
 
-void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
-  // collect global stats on Decls/Stmts (until we have a module streamer)
-  if (Stats) {
-    Decl::CollectingStats(true);
-    Stmt::CollectingStats(true);
-  }
-
-  ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
-                     PP.getIdentifierTable());
-  ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
-
-  while (ASTStreamer_ReadTopLevelDecl(Streamer))
-    /* keep reading */;
-
-  if (Stats) {
-    fprintf(stderr, "\nSTATISTICS:\n");
-    ASTStreamer_PrintStats(Streamer);
-    Context.PrintStats();
-    Decl::PrintStats();
-    Stmt::PrintStats();
-  }
-  
-  ASTStreamer_Terminate(Streamer);
-}
-
-
 
 static void PrintFunctionDeclStart(FunctionDecl *FD) {
   bool HasBody = FD->getBody();

Modified: cfe/trunk/Driver/ASTStreamers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTStreamers.h?rev=41991&r1=41990&r2=41991&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTStreamers.h (original)
+++ cfe/trunk/Driver/ASTStreamers.h Sat Sep 15 17:56:56 2007
@@ -19,8 +19,8 @@
 class Preprocessor;
 class FunctionDecl;
 class TypedefDecl;
+class ASTConsumer;
 
-void BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
 void PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
 void DumpASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
 

Modified: cfe/trunk/Driver/DiagChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/DiagChecker.cpp?rev=41991&r1=41990&r2=41991&view=diff

==============================================================================
--- cfe/trunk/Driver/DiagChecker.cpp (original)
+++ cfe/trunk/Driver/DiagChecker.cpp Sat Sep 15 17:56:56 2007
@@ -14,6 +14,8 @@
 #include "clang.h"
 #include "ASTStreamers.h"
 #include "TextDiagnosticBuffer.h"
+#include "clang/Sema/ASTStreamer.h"
+#include "clang/AST/ASTConsumer.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Preprocessor.h"
 using namespace clang;
@@ -223,8 +225,12 @@
 
 /// CheckDiagnostics - Implement the -parse-ast-check diagnostic verifier.
 bool clang::CheckDiagnostics(Preprocessor &PP, unsigned MainFileID) {
-  // Parse the specified input file.
-  BuildASTs(PP, MainFileID, false);
+  // Parse the specified input file, building ASTs and performing sema, but
+  // doing nothing else.
+{
+  ASTConsumer NullConsumer;
+  ParseAST(PP, MainFileID, NullConsumer);
+}
 
   // Gather the set of expected diagnostics.
   DiagList ExpectedErrors, ExpectedWarnings;

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

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Sat Sep 15 17:56:56 2007
@@ -26,6 +26,8 @@
 #include "ASTStreamers.h"
 #include "TextDiagnosticBuffer.h"
 #include "TextDiagnosticPrinter.h"
+#include "clang/Sema/ASTStreamer.h"
+#include "clang/AST/ASTConsumer.h"
 #include "clang/Parse/Parser.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Basic/FileManager.h"
@@ -51,7 +53,7 @@
   ParseASTPrint,                // Parse ASTs and print them.
   ParseASTDump,                 // Parse ASTs and dump them.
   ParseASTCheck,                // Parse ASTs and check diagnostics.
-  ParseAST,                     // Parse ASTs.
+  BuildAST,                     // Parse ASTs.
   ParseCFGDump,                 // Parse ASTS. Build CFGs. Print CFGs.
   ParseCFGView,                 // Parse ASTS. Build CFGs. View CFGs.
   AnalysisLiveVariables,        // Print results of live-variable analysis.
@@ -80,7 +82,7 @@
                         "Run parser and perform semantic analysis"),
              clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
                         "Run parser and print each callback invoked"),
-             clEnumValN(ParseAST, "parse-ast",
+             clEnumValN(BuildAST, "parse-ast",
                         "Run parser and build ASTs"),
              clEnumValN(ParseASTPrint, "parse-ast-print",
                         "Run parser, build ASTs, then print ASTs"),
@@ -837,9 +839,11 @@
     ClearSourceMgr = true;
     break;
   case ParseSyntaxOnly:              // -fsyntax-only
-  case ParseAST:
-    BuildASTs(PP, MainFileID, Stats);
+  case BuildAST: {
+    ASTConsumer NullConsumer;
+    ParseAST(PP, MainFileID, NullConsumer, Stats);
     break;
+  }
   case ParseASTPrint:
     PrintASTs(PP, MainFileID, Stats);
     break;

Modified: cfe/trunk/Sema/ASTStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/ASTStreamer.cpp?rev=41991&r1=41990&r2=41991&view=diff

==============================================================================
--- cfe/trunk/Sema/ASTStreamer.cpp (original)
+++ cfe/trunk/Sema/ASTStreamer.cpp Sat Sep 15 17:56:56 2007
@@ -13,11 +13,14 @@
 
 #include "clang/Sema/ASTStreamer.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTConsumer.h"
 #include "Sema.h"
 #include "clang/Parse/Action.h"
 #include "clang/Parse/Parser.h"
 using namespace clang;
 
+ASTConsumer::~ASTConsumer() {}
+
 namespace {
   class ASTStreamer {
     Parser P;
@@ -84,6 +87,39 @@
 // Public interface to the file
 //===----------------------------------------------------------------------===//
 
+/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
+/// the file is parsed.
+void clang::ParseAST(Preprocessor &PP, unsigned MainFileID, 
+                     ASTConsumer &Consumer, bool PrintStats) {
+  // Collect global stats on Decls/Stmts (until we have a module streamer).
+  if (PrintStats) {
+    Decl::CollectingStats(true);
+    Stmt::CollectingStats(true);
+  }
+  
+  ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
+                     PP.getIdentifierTable());
+  
+  ASTStreamer Streamer(PP, Context, MainFileID);
+  
+  Consumer.Initialize(Context, MainFileID);
+  
+  while (Decl *D = Streamer.ReadTopLevelDecl())
+    Consumer.HandleTopLevelDecl(D);
+  
+  if (PrintStats) {
+    fprintf(stderr, "\nSTATISTICS:\n");
+    Streamer.PrintStats();
+    Context.PrintStats();
+    Decl::PrintStats();
+    Stmt::PrintStats();
+    Consumer.PrintStats();
+    
+    Decl::CollectingStats(false);
+    Stmt::CollectingStats(false);
+  }
+}
+
 /// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
 /// and FileID.
 ASTStreamerTy *clang::ASTStreamer_Init(Preprocessor &pp, ASTContext &ctxt,

Added: cfe/trunk/include/clang/AST/ASTConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTConsumer.h?rev=41991&view=auto

==============================================================================
--- cfe/trunk/include/clang/AST/ASTConsumer.h (added)
+++ cfe/trunk/include/clang/AST/ASTConsumer.h Sat Sep 15 17:56:56 2007
@@ -0,0 +1,44 @@
+//===--- ASTConsumer.h - Abstract interface for reading ASTs ----*- C++ -*-===//
+//
+//                     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 file defines the ASTConsumer class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_ASTCONSUMER_H
+#define LLVM_CLANG_AST_ASTCONSUMER_H
+
+namespace clang {
+  class ASTContext;
+  
+/// ASTConsumer - This is an abstract interface that should be implemented by
+/// clients that read ASTs.  This abstraction layer allows the client to be
+/// independent of the AST producer (e.g. parser vs AST dump file reader, etc).
+class ASTConsumer {
+public:
+  virtual ~ASTConsumer();
+  
+  /// Initialize - This is called to initialize the consumer, providing the
+  /// ASTContext and the file ID of the primary file.
+  virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
+  }
+  
+  /// HandleTopLevelDecl - Handle the specified top-level declaration.
+  ///
+  virtual void HandleTopLevelDecl(Decl *D) {
+  }
+  
+  /// PrintStats - If desired, print any statistics.
+  virtual void PrintStats() {
+  }
+};
+
+} // end namespace clang.
+
+#endif

Modified: cfe/trunk/include/clang/Sema/ASTStreamer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/ASTStreamer.h?rev=41991&r1=41990&r2=41991&view=diff

==============================================================================
--- cfe/trunk/include/clang/Sema/ASTStreamer.h (original)
+++ cfe/trunk/include/clang/Sema/ASTStreamer.h Sat Sep 15 17:56:56 2007
@@ -18,6 +18,13 @@
   class Preprocessor;
   class ASTContext;
   class Decl;
+  class ASTConsumer;
+  
+  /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
+  /// the file is parsed.  This does not take ownership of the ASTConsumer.
+  void ParseAST(Preprocessor &pp, unsigned MainFileID, 
+                ASTConsumer &C, bool PrintStats = false);
+  
   
   /// ASTStreamerTy - This is an opaque type used to reference ASTStreamer
   /// objects.





More information about the cfe-commits mailing list