blob: bcff1c8cfbc75376f893df72a77f515717038464 [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"
Ted Kremenekafdf8112008-05-20 00:43:19 +000018#include "clang/AST/TranslationUnit.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
Chris Lattner988f2ad2008-02-06 00:15:02 +000048 Parser::DeclTy *ADecl;
Ted Kremenekafdf8112008-05-20 00:43:19 +000049
Chris Lattner988f2ad2008-02-06 00:15:02 +000050 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
51 // If we got a null return and something *was* parsed, ignore it. This
52 // is due to a top-level semicolon, an action override, or a parse error
53 // skipping something.
Ted Kremenekafdf8112008-05-20 00:43:19 +000054 if (ADecl) {
55 Decl* D = static_cast<Decl*>(ADecl);
Ted Kremenekafdf8112008-05-20 00:43:19 +000056 Consumer->HandleTopLevelDecl(D);
57 }
Chris Lattner988f2ad2008-02-06 00:15:02 +000058 };
Ted Kremenekecf79512008-07-02 18:32:45 +000059
Chris Lattner07b08c02009-03-28 04:13:34 +000060 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000061
Chris Lattner1cc01712007-09-15 22:56:56 +000062 if (PrintStats) {
63 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000064 P.getActions().PrintStats();
Chris Lattner07b08c02009-03-28 04:13:34 +000065 Ctx.PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000066 Decl::PrintStats();
67 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000068 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000069
70 Decl::CollectingStats(false);
71 Stmt::CollectingStats(false);
72 }
73}