blob: 67af285315016dd5374b7c70a698012c6503fc6a [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
Daniel Dunbarac6372a2008-10-27 22:01:05 +000027/// the file is parsed.
Daniel Dunbar4efedde2008-10-16 16:54:18 +000028///
29/// \param FreeMemory If false, the memory used for AST elements is
30/// not released.
31void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
32 bool PrintStats, bool FreeMemory) {
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 }
38
Daniel Dunbar4efedde2008-10-16 16:54:18 +000039 ASTContext *Context =
40 new ASTContext(PP.getLangOptions(), PP.getSourceManager(),
41 PP.getTargetInfo(),
Steve Naroff207b9ec2009-01-27 23:20:32 +000042 PP.getIdentifierTable(), PP.getSelectorTable(),
43 FreeMemory);
Daniel Dunbar4efedde2008-10-16 16:54:18 +000044 TranslationUnit *TU = new TranslationUnit(*Context);
45 Sema S(PP, *Context, *Consumer);
Eli Friedman3a02bdc2008-05-27 04:23:47 +000046 Parser P(PP, S);
Chris Lattner988f2ad2008-02-06 00:15:02 +000047 PP.EnterMainSourceFile();
48
49 // Initialize the parser.
50 P.Initialize();
Chris Lattner1cc01712007-09-15 22:56:56 +000051
Daniel Dunbar4efedde2008-10-16 16:54:18 +000052 Consumer->InitializeTU(*TU);
Chris Lattner1cc01712007-09-15 22:56:56 +000053
Chris Lattner988f2ad2008-02-06 00:15:02 +000054 Parser::DeclTy *ADecl;
Ted Kremenekafdf8112008-05-20 00:43:19 +000055
Chris Lattner988f2ad2008-02-06 00:15:02 +000056 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
57 // If we got a null return and something *was* parsed, ignore it. This
58 // is due to a top-level semicolon, an action override, or a parse error
59 // skipping something.
Ted Kremenekafdf8112008-05-20 00:43:19 +000060 if (ADecl) {
61 Decl* D = static_cast<Decl*>(ADecl);
Ted Kremenekafdf8112008-05-20 00:43:19 +000062 Consumer->HandleTopLevelDecl(D);
63 }
Chris Lattner988f2ad2008-02-06 00:15:02 +000064 };
Ted Kremenekecf79512008-07-02 18:32:45 +000065
Daniel Dunbar4efedde2008-10-16 16:54:18 +000066 Consumer->HandleTranslationUnit(*TU);
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();
Daniel Dunbar4efedde2008-10-16 16:54:18 +000071 Context->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 }
Daniel Dunbar4efedde2008-10-16 16:54:18 +000079
80 if (FreeMemory) {
81 delete TU;
82 delete Context;
83 }
Chris Lattner1cc01712007-09-15 22:56:56 +000084}