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