blob: bb5acb0ee8dabe012bea4bff7ba2adcf16b9d3c4 [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"
Douglas Gregor631f6c62009-04-14 00:24:19 +000017#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Stmt.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "Sema.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#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 Lattner07b08c02009-03-28 04:13:34 +000028/// the file is parsed. This inserts the parsed decls into the translation unit
29/// held by Ctx.
Daniel Dunbar4efedde2008-10-16 16:54:18 +000030///
Ted Kremenek88eebed2009-01-28 04:29:29 +000031void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Chris Lattner07b08c02009-03-28 04:13:34 +000032 ASTContext &Ctx, bool PrintStats) {
Chris Lattner1cc01712007-09-15 22:56:56 +000033 // Collect global stats on Decls/Stmts (until we have a module streamer).
34 if (PrintStats) {
35 Decl::CollectingStats(true);
36 Stmt::CollectingStats(true);
37 }
Ted Kremenek88eebed2009-01-28 04:29:29 +000038
Chris Lattner07b08c02009-03-28 04:13:34 +000039 Sema S(PP, Ctx, *Consumer);
Eli Friedman3a02bdc2008-05-27 04:23:47 +000040 Parser P(PP, S);
Chris Lattner988f2ad2008-02-06 00:15:02 +000041 PP.EnterMainSourceFile();
42
43 // Initialize the parser.
44 P.Initialize();
Chris Lattner1cc01712007-09-15 22:56:56 +000045
Chris Lattner07b08c02009-03-28 04:13:34 +000046 Consumer->Initialize(Ctx);
Chris Lattner1cc01712007-09-15 22:56:56 +000047
Douglas Gregor631f6c62009-04-14 00:24:19 +000048 if (Ctx.getExternalSource())
49 Ctx.getExternalSource()->StartTranslationUnit(Consumer);
50
Chris Lattnera17991f2009-03-29 16:50:03 +000051 Parser::DeclGroupPtrTy 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.
Chris Lattnera17991f2009-03-29 16:50:03 +000057 if (ADecl)
58 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattner988f2ad2008-02-06 00:15:02 +000059 };
Ted Kremenekecf79512008-07-02 18:32:45 +000060
Chris Lattner07b08c02009-03-28 04:13:34 +000061 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000062
Chris Lattner1cc01712007-09-15 22:56:56 +000063 if (PrintStats) {
64 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000065 P.getActions().PrintStats();
Chris Lattner07b08c02009-03-28 04:13:34 +000066 Ctx.PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000067 Decl::PrintStats();
68 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000069 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000070
71 Decl::CollectingStats(false);
72 Stmt::CollectingStats(false);
73 }
74}