blob: 8fd14d17d1a64e99d1d28786722c537ad02dc14b [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/ASTContext.h"
Chris Lattner556beb72007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "Sema.h"
18#include "clang/Parse/Action.h"
19#include "clang/Parse/Parser.h"
20using namespace clang;
21
Chris Lattner556beb72007-09-15 22:56:56 +000022ASTConsumer::~ASTConsumer() {}
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024//===----------------------------------------------------------------------===//
25// Public interface to the file
26//===----------------------------------------------------------------------===//
27
Chris Lattner556beb72007-09-15 22:56:56 +000028/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner31e6c7d2007-11-03 06:24:16 +000029/// the file is parsed. This takes ownership of the ASTConsumer and
30/// ultimately deletes it.
Ted Kremenek95041a22007-12-19 22:51:13 +000031void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner556beb72007-09-15 22:56:56 +000032 // Collect global stats on Decls/Stmts (until we have a module streamer).
33 if (PrintStats) {
34 Decl::CollectingStats(true);
35 Stmt::CollectingStats(true);
36 }
37
38 ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
Steve Naroff68d331a2007-09-27 14:38:14 +000039 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner556beb72007-09-15 22:56:56 +000040
Chris Lattner2ae34ed2008-02-06 00:46:58 +000041 Parser P(PP, *new Sema(PP, Context, *Consumer));
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
Ted Kremenek95041a22007-12-19 22:51:13 +000047 Consumer->Initialize(Context);
Chris Lattner556beb72007-09-15 22:56:56 +000048
Chris Lattnera0e328f2008-02-06 00:15:02 +000049 Parser::DeclTy *ADecl;
50 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.
54 if (ADecl)
55 Consumer->HandleTopLevelDecl(static_cast<Decl*>(ADecl));
56 };
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000057
Chris Lattner556beb72007-09-15 22:56:56 +000058 if (PrintStats) {
59 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000060 P.getActions().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000061 Context.PrintStats();
62 Decl::PrintStats();
63 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000064 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000065
66 Decl::CollectingStats(false);
67 Stmt::CollectingStats(false);
68 }
Chris Lattner31e6c7d2007-11-03 06:24:16 +000069
70 delete Consumer;
Chris Lattner556beb72007-09-15 22:56:56 +000071}