Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ASTStreamer.cpp - Provide streaming interface to ASTs ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTStreamer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Sema/ASTStreamer.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "Sema.h" |
| 18 | #include "clang/Parse/Action.h" |
| 19 | #include "clang/Parse/Parser.h" |
| 20 | using namespace clang; |
| 21 | |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 22 | ASTConsumer::~ASTConsumer() {} |
| 23 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Public interface to the file |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 28 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 29 | /// the file is parsed. This takes ownership of the ASTConsumer and |
| 30 | /// ultimately deletes it. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 31 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 32 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 33 | if (PrintStats) { |
| 34 | Decl::CollectingStats(true); |
| 35 | Stmt::CollectingStats(true); |
| 36 | } |
| 37 | |
| 38 | ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(), |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 39 | PP.getIdentifierTable(), PP.getSelectorTable()); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame^] | 41 | Parser P(PP, *new Sema(PP, Context)); |
| 42 | PP.EnterMainSourceFile(); |
| 43 | |
| 44 | // Initialize the parser. |
| 45 | P.Initialize(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 47 | Consumer->Initialize(Context); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame^] | 49 | Parser::DeclTy *ADecl; |
| 50 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 51 | // If we got a null return and something *was* parsed, ignore it. This |
| 52 | // is due to a top-level semicolon, an action override, or a parse error |
| 53 | // skipping something. |
| 54 | if (ADecl) |
| 55 | Consumer->HandleTopLevelDecl(static_cast<Decl*>(ADecl)); |
| 56 | }; |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 58 | if (PrintStats) { |
| 59 | fprintf(stderr, "\nSTATISTICS:\n"); |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame^] | 60 | P.getActions().PrintStats(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 61 | Context.PrintStats(); |
| 62 | Decl::PrintStats(); |
| 63 | Stmt::PrintStats(); |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 64 | Consumer->PrintStats(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 65 | |
| 66 | Decl::CollectingStats(false); |
| 67 | Stmt::CollectingStats(false); |
| 68 | } |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 69 | |
| 70 | delete Consumer; |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 71 | } |