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" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/Stmt.h" |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/TranslationUnit.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "Sema.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
Daniel Dunbar | d6a1c5d | 2008-10-27 22:01:05 +0000 | [diff] [blame^] | 27 | /// the file is parsed. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 28 | /// |
| 29 | /// \param FreeMemory If false, the memory used for AST elements is |
| 30 | /// not released. |
| 31 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
| 32 | bool PrintStats, bool FreeMemory) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 33 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 34 | if (PrintStats) { |
| 35 | Decl::CollectingStats(true); |
| 36 | Stmt::CollectingStats(true); |
| 37 | } |
| 38 | |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 39 | ASTContext *Context = |
| 40 | new ASTContext(PP.getLangOptions(), PP.getSourceManager(), |
| 41 | PP.getTargetInfo(), |
| 42 | PP.getIdentifierTable(), PP.getSelectorTable()); |
| 43 | TranslationUnit *TU = new TranslationUnit(*Context); |
| 44 | Sema S(PP, *Context, *Consumer); |
Eli Friedman | 80f3346 | 2008-05-27 04:23:47 +0000 | [diff] [blame] | 45 | Parser P(PP, S); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 46 | PP.EnterMainSourceFile(); |
| 47 | |
| 48 | // Initialize the parser. |
| 49 | P.Initialize(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 51 | Consumer->InitializeTU(*TU); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 52 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 53 | Parser::DeclTy *ADecl; |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 54 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 55 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 56 | // If we got a null return and something *was* parsed, ignore it. This |
| 57 | // is due to a top-level semicolon, an action override, or a parse error |
| 58 | // skipping something. |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 59 | if (ADecl) { |
| 60 | Decl* D = static_cast<Decl*>(ADecl); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 61 | TU->AddTopLevelDecl(D); // TranslationUnit now owns the Decl. |
Ted Kremenek | 27f8a28 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 62 | Consumer->HandleTopLevelDecl(D); |
| 63 | } |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 64 | }; |
Ted Kremenek | c87190d | 2008-07-02 18:32:45 +0000 | [diff] [blame] | 65 | |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 66 | Consumer->HandleTranslationUnit(*TU); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 68 | if (PrintStats) { |
| 69 | fprintf(stderr, "\nSTATISTICS:\n"); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 70 | P.getActions().PrintStats(); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 71 | Context->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 72 | Decl::PrintStats(); |
| 73 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 74 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 75 | |
| 76 | Decl::CollectingStats(false); |
| 77 | Stmt::CollectingStats(false); |
| 78 | } |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 79 | |
| 80 | if (FreeMemory) { |
| 81 | delete TU; |
| 82 | delete Context; |
| 83 | } |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 84 | } |