blob: 59a04dad64e20f6d6bb85ef1e3b5fb24baf38a43 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "Sema.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Parse/Parser.h"
20using namespace clang;
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022//===----------------------------------------------------------------------===//
23// Public interface to the file
24//===----------------------------------------------------------------------===//
25
Chris Lattner556beb72007-09-15 22:56:56 +000026/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000027/// the file is parsed. This inserts the parsed decls into the translation unit
28/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000029///
Ted Kremenek46157b52009-01-28 04:29:29 +000030void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Chris Lattner3599dbe2009-03-28 04:13:34 +000031 ASTContext &Ctx, bool PrintStats) {
Chris Lattner556beb72007-09-15 22:56:56 +000032 // Collect global stats on Decls/Stmts (until we have a module streamer).
33 if (PrintStats) {
34 Decl::CollectingStats(true);
35 Stmt::CollectingStats(true);
36 }
Ted Kremenek46157b52009-01-28 04:29:29 +000037
Chris Lattner3599dbe2009-03-28 04:13:34 +000038 Sema S(PP, Ctx, *Consumer);
Eli Friedman80f33462008-05-27 04:23:47 +000039 Parser P(PP, S);
Chris Lattnera0e328f2008-02-06 00:15:02 +000040 PP.EnterMainSourceFile();
41
42 // Initialize the parser.
43 P.Initialize();
Chris Lattner556beb72007-09-15 22:56:56 +000044
Chris Lattner3599dbe2009-03-28 04:13:34 +000045 Consumer->Initialize(Ctx);
Chris Lattner556beb72007-09-15 22:56:56 +000046
Chris Lattnerb28317a2009-03-28 19:18:32 +000047 Parser::DeclPtrTy ADecl;
Ted Kremenek27f8a282008-05-20 00:43:19 +000048
Chris Lattnera0e328f2008-02-06 00:15:02 +000049 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
50 // If we got a null return and something *was* parsed, ignore it. This
51 // is due to a top-level semicolon, an action override, or a parse error
52 // skipping something.
Ted Kremenek27f8a282008-05-20 00:43:19 +000053 if (ADecl) {
Chris Lattnerb28317a2009-03-28 19:18:32 +000054 Decl *D = ADecl.getAs<Decl>();
Ted Kremenek27f8a282008-05-20 00:43:19 +000055 Consumer->HandleTopLevelDecl(D);
56 }
Chris Lattnera0e328f2008-02-06 00:15:02 +000057 };
Ted Kremenekc87190d2008-07-02 18:32:45 +000058
Chris Lattner3599dbe2009-03-28 04:13:34 +000059 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000060
Chris Lattner556beb72007-09-15 22:56:56 +000061 if (PrintStats) {
62 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000063 P.getActions().PrintStats();
Chris Lattner3599dbe2009-03-28 04:13:34 +000064 Ctx.PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000065 Decl::PrintStats();
66 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000067 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000068
69 Decl::CollectingStats(false);
70 Stmt::CollectingStats(false);
71 }
72}