blob: 4bcb478e8905dda586042b2f581b6bcf1614c1fd [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
Ted Kremenek88eebed2009-01-28 04:29:29 +000014#include <llvm/ADT/OwningPtr.h>
Chris Lattner0bed6ec2008-02-06 00:23:21 +000015#include "clang/Sema/ParseAST.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Stmt.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 Lattner07b08c02009-03-28 04:13:34 +000027/// the file is parsed. This inserts the parsed decls into the translation unit
28/// held by Ctx.
Daniel Dunbar4efedde2008-10-16 16:54:18 +000029///
Ted Kremenek88eebed2009-01-28 04:29:29 +000030void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Chris Lattner07b08c02009-03-28 04:13:34 +000031 ASTContext &Ctx, bool PrintStats) {
Chris Lattner1cc01712007-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 Kremenek88eebed2009-01-28 04:29:29 +000037
Chris Lattner07b08c02009-03-28 04:13:34 +000038 Sema S(PP, Ctx, *Consumer);
Eli Friedman3a02bdc2008-05-27 04:23:47 +000039 Parser P(PP, S);
Chris Lattner988f2ad2008-02-06 00:15:02 +000040 PP.EnterMainSourceFile();
41
42 // Initialize the parser.
43 P.Initialize();
Chris Lattner1cc01712007-09-15 22:56:56 +000044
Chris Lattner07b08c02009-03-28 04:13:34 +000045 Consumer->Initialize(Ctx);
Chris Lattner1cc01712007-09-15 22:56:56 +000046
Chris Lattnera17991f2009-03-29 16:50:03 +000047 Parser::DeclGroupPtrTy ADecl;
Ted Kremenekafdf8112008-05-20 00:43:19 +000048
Chris Lattner988f2ad2008-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.
Chris Lattnera17991f2009-03-29 16:50:03 +000053 if (ADecl)
54 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattner988f2ad2008-02-06 00:15:02 +000055 };
Ted Kremenekecf79512008-07-02 18:32:45 +000056
Chris Lattner07b08c02009-03-28 04:13:34 +000057 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000058
Chris Lattner1cc01712007-09-15 22:56:56 +000059 if (PrintStats) {
60 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000061 P.getActions().PrintStats();
Chris Lattner07b08c02009-03-28 04:13:34 +000062 Ctx.PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000063 Decl::PrintStats();
64 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000065 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000066
67 Decl::CollectingStats(false);
68 Stmt::CollectingStats(false);
69 }
70}