blob: bbca5515216c365a5785fa2cf45e0155c274a026 [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
Chris Lattner9ecd26a2009-03-28 01:37:17 +000028/// the file is parsed. This inserts the parsed decls into TU.
Daniel Dunbard3db4012008-10-16 16:54:18 +000029///
Ted Kremenek46157b52009-01-28 04:29:29 +000030void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Chris Lattner9ecd26a2009-03-28 01:37:17 +000031 TranslationUnit &TU, 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 Lattner9ecd26a2009-03-28 01:37:17 +000038 Sema S(PP, TU.getContext(), *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 Lattner7bb0da02009-03-28 02:18:25 +000045 Consumer->Initialize(TU.getContext());
Chris Lattner556beb72007-09-15 22:56:56 +000046
Chris Lattnera0e328f2008-02-06 00:15:02 +000047 Parser::DeclTy *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) {
54 Decl* D = static_cast<Decl*>(ADecl);
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 Lattner9ecd26a2009-03-28 01:37:17 +000059 Consumer->HandleTranslationUnit(TU);
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 Lattner9ecd26a2009-03-28 01:37:17 +000064 TU.getContext().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}