blob: fc35096827280c8db4d3da1a583c6c9a1cec637a [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 Lattner1cc01712007-09-15 22:56:56 +000015#include "clang/AST/ASTConsumer.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000016#include "clang/AST/Stmt.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"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Parse/Parser.h"
20using namespace clang;
21
Chris Lattner4b009652007-07-25 00:24:17 +000022//===----------------------------------------------------------------------===//
23// Public interface to the file
24//===----------------------------------------------------------------------===//
25
Chris Lattner1cc01712007-09-15 22:56:56 +000026/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner8593cbf2007-11-03 06:24:16 +000027/// the file is parsed. This takes ownership of the ASTConsumer and
28/// ultimately deletes it.
Ted Kremenek50aab982008-08-08 02:46:37 +000029void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner1cc01712007-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
Ted Kremenek842126e2008-06-04 15:55:15 +000036 ASTContext Context(PP.getLangOptions(), PP.getSourceManager(),
37 PP.getTargetInfo(),
Steve Naroff4ed9d662007-09-27 14:38:14 +000038 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner1cc01712007-09-15 22:56:56 +000039
Ted Kremenek842126e2008-06-04 15:55:15 +000040 TranslationUnit TU(Context);
Eli Friedman3a02bdc2008-05-27 04:23:47 +000041 Sema S(PP, Context, *Consumer);
42 Parser P(PP, S);
Chris Lattner988f2ad2008-02-06 00:15:02 +000043 PP.EnterMainSourceFile();
44
45 // Initialize the parser.
46 P.Initialize();
Chris Lattner1cc01712007-09-15 22:56:56 +000047
Ted Kremenek79b50cb2008-05-31 20:11:04 +000048 Consumer->InitializeTU(TU);
Chris Lattner1cc01712007-09-15 22:56:56 +000049
Chris Lattner988f2ad2008-02-06 00:15:02 +000050 Parser::DeclTy *ADecl;
Ted Kremenekafdf8112008-05-20 00:43:19 +000051
Chris Lattner988f2ad2008-02-06 00:15:02 +000052 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
53 // If we got a null return and something *was* parsed, ignore it. This
54 // is due to a top-level semicolon, an action override, or a parse error
55 // skipping something.
Ted Kremenekafdf8112008-05-20 00:43:19 +000056 if (ADecl) {
57 Decl* D = static_cast<Decl*>(ADecl);
58 TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl.
59 Consumer->HandleTopLevelDecl(D);
60 }
Chris Lattner988f2ad2008-02-06 00:15:02 +000061 };
Ted Kremenekecf79512008-07-02 18:32:45 +000062
63 Consumer->HandleTranslationUnit(TU);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000064
Chris Lattner1cc01712007-09-15 22:56:56 +000065 if (PrintStats) {
66 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000067 P.getActions().PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000068 Context.PrintStats();
69 Decl::PrintStats();
70 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000071 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000072
73 Decl::CollectingStats(false);
74 Stmt::CollectingStats(false);
75 }
76}