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 | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/Stmt.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" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Parser.h" |
| 20 | using namespace clang; |
| 21 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | // Public interface to the file |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 26 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 8593cbf | 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 | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 29 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) { |
Chris Lattner | 1cc0171 | 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 | |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 36 | ASTContext Context(PP.getLangOptions(), PP.getSourceManager(), |
| 37 | 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 | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 40 | TranslationUnit TU(Context); |
Eli Friedman | 3a02bdc | 2008-05-27 04:23:47 +0000 | [diff] [blame] | 41 | Sema S(PP, Context, *Consumer); |
| 42 | Parser P(PP, S); |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 43 | PP.EnterMainSourceFile(); |
| 44 | |
| 45 | // Initialize the parser. |
| 46 | P.Initialize(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 79b50cb | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 48 | Consumer->InitializeTU(TU); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 50 | Parser::DeclTy *ADecl; |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 52 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 53 | // If we got a null return and something *was* parsed, ignore it. This |
| 54 | // is due to a top-level semicolon, an action override, or a parse error |
| 55 | // skipping something. |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 56 | if (ADecl) { |
| 57 | Decl* D = static_cast<Decl*>(ADecl); |
| 58 | TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl. |
| 59 | Consumer->HandleTopLevelDecl(D); |
| 60 | } |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 61 | }; |
Ted Kremenek | ecf7951 | 2008-07-02 18:32:45 +0000 | [diff] [blame] | 62 | |
| 63 | Consumer->HandleTranslationUnit(TU); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 65 | if (PrintStats) { |
| 66 | fprintf(stderr, "\nSTATISTICS:\n"); |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 67 | P.getActions().PrintStats(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 68 | Context.PrintStats(); |
| 69 | Decl::PrintStats(); |
| 70 | Stmt::PrintStats(); |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 71 | Consumer->PrintStats(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 72 | |
| 73 | Decl::CollectingStats(false); |
| 74 | Stmt::CollectingStats(false); |
| 75 | } |
| 76 | } |