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