blob: 364b07291006d877ca6af22e8d74a5159ccc7854 [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
Chris Lattnere91c1342008-02-06 00:23:21 +000014#include "clang/Sema/ParseAST.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/ASTContext.h"
Chris Lattner556beb72007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "Sema.h"
18#include "clang/Parse/Action.h"
19#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 Lattner31e6c7d2007-11-03 06:24:16 +000027/// the file is parsed. This takes ownership of the ASTConsumer and
28/// ultimately deletes it.
Ted Kremenek95041a22007-12-19 22:51:13 +000029void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner556beb72007-09-15 22:56:56 +000030 // Collect global stats on Decls/Stmts (until we have a module streamer).
31 if (PrintStats) {
32 Decl::CollectingStats(true);
33 Stmt::CollectingStats(true);
34 }
35
36 ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
Steve Naroff68d331a2007-09-27 14:38:14 +000037 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner556beb72007-09-15 22:56:56 +000038
Chris Lattner2ae34ed2008-02-06 00:46:58 +000039 Parser P(PP, *new Sema(PP, Context, *Consumer));
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
Ted Kremenek95041a22007-12-19 22:51:13 +000045 Consumer->Initialize(Context);
Chris Lattner556beb72007-09-15 22:56:56 +000046
Chris Lattnera0e328f2008-02-06 00:15:02 +000047 Parser::DeclTy *ADecl;
48 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
49 // If we got a null return and something *was* parsed, ignore it. This
50 // is due to a top-level semicolon, an action override, or a parse error
51 // skipping something.
52 if (ADecl)
53 Consumer->HandleTopLevelDecl(static_cast<Decl*>(ADecl));
54 };
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000055
Chris Lattner556beb72007-09-15 22:56:56 +000056 if (PrintStats) {
57 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000058 P.getActions().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000059 Context.PrintStats();
60 Decl::PrintStats();
61 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000062 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000063
64 Decl::CollectingStats(false);
65 Stmt::CollectingStats(false);
66 }
Chris Lattner31e6c7d2007-11-03 06:24:16 +000067
68 delete Consumer;
Chris Lattner556beb72007-09-15 22:56:56 +000069}