blob: 448556092f69c39427dae39621cb53774fd2a277 [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 Lattner37dee042009-04-19 05:30:08 +000015#include "Sema.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000016#include "clang/Sema/SemaConsumer.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000017#include "clang/AST/ASTConsumer.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Parse/Parser.h"
Chris Lattner37dee042009-04-19 05:30:08 +000021#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
Chris Lattner4b009652007-07-25 00:24:17 +000024//===----------------------------------------------------------------------===//
25// Public interface to the file
26//===----------------------------------------------------------------------===//
27
Chris Lattner1cc01712007-09-15 22:56:56 +000028/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner07b08c02009-03-28 04:13:34 +000029/// the file is parsed. This inserts the parsed decls into the translation unit
30/// held by Ctx.
Daniel Dunbar4efedde2008-10-16 16:54:18 +000031///
Ted Kremenek88eebed2009-01-28 04:29:29 +000032void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregore0d5c562009-04-14 16:27:31 +000033 ASTContext &Ctx, bool PrintStats,
34 bool CompleteTranslationUnit) {
Chris Lattner1cc01712007-09-15 22:56:56 +000035 // Collect global stats on Decls/Stmts (until we have a module streamer).
36 if (PrintStats) {
37 Decl::CollectingStats(true);
38 Stmt::CollectingStats(true);
39 }
Ted Kremenek88eebed2009-01-28 04:29:29 +000040
Douglas Gregore0d5c562009-04-14 16:27:31 +000041 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit);
Eli Friedman3a02bdc2008-05-27 04:23:47 +000042 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
Chris Lattner07b08c02009-03-28 04:13:34 +000048 Consumer->Initialize(Ctx);
Chris Lattner1cc01712007-09-15 22:56:56 +000049
Douglas Gregor87887da2009-04-20 15:53:59 +000050 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
51 SC->InitializeSema(S);
52
Douglas Gregor631f6c62009-04-14 00:24:19 +000053 if (Ctx.getExternalSource())
54 Ctx.getExternalSource()->StartTranslationUnit(Consumer);
55
Chris Lattnera17991f2009-03-29 16:50:03 +000056 Parser::DeclGroupPtrTy ADecl;
Ted Kremenekafdf8112008-05-20 00:43:19 +000057
Chris Lattner988f2ad2008-02-06 00:15:02 +000058 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
59 // If we got a null return and something *was* parsed, ignore it. This
60 // is due to a top-level semicolon, an action override, or a parse error
61 // skipping something.
Chris Lattnera17991f2009-03-29 16:50:03 +000062 if (ADecl)
63 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattner988f2ad2008-02-06 00:15:02 +000064 };
Ted Kremenekecf79512008-07-02 18:32:45 +000065
Chris Lattner07b08c02009-03-28 04:13:34 +000066 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000067
Chris Lattner1cc01712007-09-15 22:56:56 +000068 if (PrintStats) {
69 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000070 P.getActions().PrintStats();
Chris Lattner07b08c02009-03-28 04:13:34 +000071 Ctx.PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000072 Decl::PrintStats();
73 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000074 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000075
76 Decl::CollectingStats(false);
77 Stmt::CollectingStats(false);
78 }
79}