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