blob: 6923476aeba0d25f0d1decb53811869e2d535519 [file] [log] [blame]
Chris Lattnere91c1342008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnere91c1342008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek46157b52009-01-28 04:29:29 +000014#include <llvm/ADT/OwningPtr.h>
Chris Lattnere91c1342008-02-06 00:23:21 +000015#include "clang/Sema/ParseAST.h"
Chris Lattner556beb72007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Stmt.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000018#include "clang/AST/TranslationUnit.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "Sema.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Parse/Parser.h"
21using namespace clang;
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023//===----------------------------------------------------------------------===//
24// Public interface to the file
25//===----------------------------------------------------------------------===//
26
Chris Lattner556beb72007-09-15 22:56:56 +000027/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Daniel Dunbard6a1c5d2008-10-27 22:01:05 +000028/// the file is parsed.
Daniel Dunbard3db4012008-10-16 16:54:18 +000029///
Ted Kremenek46157b52009-01-28 04:29:29 +000030/// \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.
33void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
34 TranslationUnit *TU, bool PrintStats) {
Chris Lattner556beb72007-09-15 22:56:56 +000035 // 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 Kremenek46157b52009-01-28 04:29:29 +000040
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 Friedman80f33462008-05-27 04:23:47 +000055 Parser P(PP, S);
Chris Lattnera0e328f2008-02-06 00:15:02 +000056 PP.EnterMainSourceFile();
57
58 // Initialize the parser.
59 P.Initialize();
Chris Lattner556beb72007-09-15 22:56:56 +000060
Daniel Dunbard3db4012008-10-16 16:54:18 +000061 Consumer->InitializeTU(*TU);
Chris Lattner556beb72007-09-15 22:56:56 +000062
Chris Lattnera0e328f2008-02-06 00:15:02 +000063 Parser::DeclTy *ADecl;
Ted Kremenek27f8a282008-05-20 00:43:19 +000064
Chris Lattnera0e328f2008-02-06 00:15:02 +000065 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 Kremenek27f8a282008-05-20 00:43:19 +000069 if (ADecl) {
70 Decl* D = static_cast<Decl*>(ADecl);
Ted Kremenek27f8a282008-05-20 00:43:19 +000071 Consumer->HandleTopLevelDecl(D);
72 }
Chris Lattnera0e328f2008-02-06 00:15:02 +000073 };
Ted Kremenekc87190d2008-07-02 18:32:45 +000074
Daniel Dunbard3db4012008-10-16 16:54:18 +000075 Consumer->HandleTranslationUnit(*TU);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000076
Chris Lattner556beb72007-09-15 22:56:56 +000077 if (PrintStats) {
78 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000079 P.getActions().PrintStats();
Ted Kremenek46157b52009-01-28 04:29:29 +000080 TU->getContext().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000081 Decl::PrintStats();
82 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000083 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000084
85 Decl::CollectingStats(false);
86 Stmt::CollectingStats(false);
87 }
88}