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 | |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 37 | ASTContext Context(PP.getLangOptions(), PP.getSourceManager(), |
| 38 | 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 | |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 41 | TranslationUnit TU(Context); |
Ted Kremenek | 225a14c | 2008-06-16 18:00:42 +0000 | [diff] [blame^] | 42 | TU.SetOwnsDecls(false); |
| 43 | |
Eli Friedman | 3a02bdc | 2008-05-27 04:23:47 +0000 | [diff] [blame] | 44 | |
| 45 | Sema S(PP, Context, *Consumer); |
| 46 | Parser P(PP, S); |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 47 | PP.EnterMainSourceFile(); |
| 48 | |
| 49 | // Initialize the parser. |
| 50 | P.Initialize(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 79b50cb | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 52 | Consumer->InitializeTU(TU); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 54 | Parser::DeclTy *ADecl; |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 56 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 57 | // If we got a null return and something *was* parsed, ignore it. This |
| 58 | // is due to a top-level semicolon, an action override, or a parse error |
| 59 | // skipping something. |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 60 | if (ADecl) { |
| 61 | Decl* D = static_cast<Decl*>(ADecl); |
| 62 | TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl. |
| 63 | Consumer->HandleTopLevelDecl(D); |
| 64 | } |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 65 | }; |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 67 | if (PrintStats) { |
| 68 | fprintf(stderr, "\nSTATISTICS:\n"); |
Chris Lattner | 988f2ad | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 69 | P.getActions().PrintStats(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 70 | Context.PrintStats(); |
| 71 | Decl::PrintStats(); |
| 72 | Stmt::PrintStats(); |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 73 | Consumer->PrintStats(); |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 74 | |
| 75 | Decl::CollectingStats(false); |
| 76 | Stmt::CollectingStats(false); |
| 77 | } |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 78 | |
| 79 | delete Consumer; |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 80 | } |