blob: b27734745ae3856ebedf82b3460d29c84c297728 [file] [log] [blame]
Chris Lattnere91c1342008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnere91c1342008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnere91c1342008-02-06 00:23:21 +000014#include "clang/Sema/ParseAST.h"
Chris Lattner498603d2009-04-19 05:30:08 +000015#include "Sema.h"
Chris Lattner556beb72007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000017#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Stmt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Parse/Parser.h"
Chris Lattner498603d2009-04-19 05:30:08 +000020#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023//===----------------------------------------------------------------------===//
24// Public interface to the file
25//===----------------------------------------------------------------------===//
26
Chris Lattner556beb72007-09-15 22:56:56 +000027/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000028/// the file is parsed. This inserts the parsed decls into the translation unit
29/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000030///
Ted Kremenek46157b52009-01-28 04:29:29 +000031void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000032 ASTContext &Ctx, bool PrintStats,
33 bool CompleteTranslationUnit) {
Chris Lattner556beb72007-09-15 22:56:56 +000034 // Collect global stats on Decls/Stmts (until we have a module streamer).
35 if (PrintStats) {
36 Decl::CollectingStats(true);
37 Stmt::CollectingStats(true);
38 }
Ted Kremenek46157b52009-01-28 04:29:29 +000039
Douglas Gregorf807fe02009-04-14 16:27:31 +000040 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit);
Eli Friedman80f33462008-05-27 04:23:47 +000041 Parser P(PP, S);
Chris Lattnera0e328f2008-02-06 00:15:02 +000042 PP.EnterMainSourceFile();
43
44 // Initialize the parser.
45 P.Initialize();
Chris Lattner556beb72007-09-15 22:56:56 +000046
Chris Lattner3599dbe2009-03-28 04:13:34 +000047 Consumer->Initialize(Ctx);
Chris Lattner556beb72007-09-15 22:56:56 +000048
Douglas Gregorfdd01722009-04-14 00:24:19 +000049 if (Ctx.getExternalSource())
50 Ctx.getExternalSource()->StartTranslationUnit(Consumer);
51
Chris Lattner682bf922009-03-29 16:50:03 +000052 Parser::DeclGroupPtrTy ADecl;
Ted Kremenek27f8a282008-05-20 00:43:19 +000053
Chris Lattnera0e328f2008-02-06 00:15:02 +000054 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
55 // If we got a null return and something *was* parsed, ignore it. This
56 // is due to a top-level semicolon, an action override, or a parse error
57 // skipping something.
Chris Lattner682bf922009-03-29 16:50:03 +000058 if (ADecl)
59 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattnera0e328f2008-02-06 00:15:02 +000060 };
Ted Kremenekc87190d2008-07-02 18:32:45 +000061
Chris Lattner3599dbe2009-03-28 04:13:34 +000062 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000063
Chris Lattner556beb72007-09-15 22:56:56 +000064 if (PrintStats) {
65 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000066 P.getActions().PrintStats();
Chris Lattner3599dbe2009-03-28 04:13:34 +000067 Ctx.PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000068 Decl::PrintStats();
69 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000070 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000071
72 Decl::CollectingStats(false);
73 Stmt::CollectingStats(false);
74 }
75}