blob: 9199179af74dac48ec1cba3f1c7a701fafd4e84f [file] [log] [blame]
Chris Lattner0bed6ec2008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner0bed6ec2008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner0bed6ec2008-02-06 00:23:21 +000014#include "clang/Sema/ParseAST.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "clang/AST/ASTContext.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Ted Kremenekafdf8112008-05-20 00:43:19 +000017#include "clang/AST/TranslationUnit.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "Sema.h"
19#include "clang/Parse/Action.h"
20#include "clang/Parse/Parser.h"
21using namespace clang;
22
Chris Lattner4b009652007-07-25 00:24:17 +000023//===----------------------------------------------------------------------===//
24// Public interface to the file
25//===----------------------------------------------------------------------===//
26
Chris Lattner1cc01712007-09-15 22:56:56 +000027/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner8593cbf2007-11-03 06:24:16 +000028/// the file is parsed. This takes ownership of the ASTConsumer and
29/// ultimately deletes it.
Ted Kremenek17861c52007-12-19 22:51:13 +000030void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner1cc01712007-09-15 22:56:56 +000031 // Collect global stats on Decls/Stmts (until we have a module streamer).
32 if (PrintStats) {
33 Decl::CollectingStats(true);
34 Stmt::CollectingStats(true);
35 }
36
37 ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
Steve Naroff4ed9d662007-09-27 14:38:14 +000038 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner1cc01712007-09-15 22:56:56 +000039
Ted Kremenekafdf8112008-05-20 00:43:19 +000040 TranslationUnit TU(Context, PP.getLangOptions());
Eli Friedman3a02bdc2008-05-27 04:23:47 +000041
42 Sema S(PP, Context, *Consumer);
43 Parser P(PP, S);
Chris Lattner988f2ad2008-02-06 00:15:02 +000044 PP.EnterMainSourceFile();
45
46 // Initialize the parser.
47 P.Initialize();
Chris Lattner1cc01712007-09-15 22:56:56 +000048
Ted Kremenek79b50cb2008-05-31 20:11:04 +000049 Consumer->InitializeTU(TU);
Chris Lattner1cc01712007-09-15 22:56:56 +000050
Chris Lattner988f2ad2008-02-06 00:15:02 +000051 Parser::DeclTy *ADecl;
Ted Kremenekafdf8112008-05-20 00:43:19 +000052
Chris Lattner988f2ad2008-02-06 00:15:02 +000053 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
54 // If we got a null return and something *was* parsed, ignore it. This
55 // is due to a top-level semicolon, an action override, or a parse error
56 // skipping something.
Ted Kremenekafdf8112008-05-20 00:43:19 +000057 if (ADecl) {
58 Decl* D = static_cast<Decl*>(ADecl);
59 TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl.
60 Consumer->HandleTopLevelDecl(D);
61 }
Chris Lattner988f2ad2008-02-06 00:15:02 +000062 };
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000063
Chris Lattner1cc01712007-09-15 22:56:56 +000064 if (PrintStats) {
65 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000066 P.getActions().PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000067 Context.PrintStats();
68 Decl::PrintStats();
69 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000070 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000071
72 Decl::CollectingStats(false);
73 Stmt::CollectingStats(false);
74 }
Chris Lattner8593cbf2007-11-03 06:24:16 +000075
76 delete Consumer;
Chris Lattner1cc01712007-09-15 22:56:56 +000077}