blob: 567cfc27630d0911a1d74d92494df1b3a2cafbb7 [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 Lattner556beb72007-09-15 22:56:56 +000015#include "clang/AST/ASTConsumer.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000017#include "clang/AST/TranslationUnit.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "Sema.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Parse/Parser.h"
20using namespace clang;
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022//===----------------------------------------------------------------------===//
23// Public interface to the file
24//===----------------------------------------------------------------------===//
25
Chris Lattner556beb72007-09-15 22:56:56 +000026/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner31e6c7d2007-11-03 06:24:16 +000027/// the file is parsed. This takes ownership of the ASTConsumer and
28/// ultimately deletes it.
Daniel Dunbard3db4012008-10-16 16:54:18 +000029///
30/// \param FreeMemory If false, the memory used for AST elements is
31/// not released.
32void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
33 bool PrintStats, bool FreeMemory) {
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 }
39
Daniel Dunbard3db4012008-10-16 16:54:18 +000040 ASTContext *Context =
41 new ASTContext(PP.getLangOptions(), PP.getSourceManager(),
42 PP.getTargetInfo(),
43 PP.getIdentifierTable(), PP.getSelectorTable());
44 TranslationUnit *TU = new TranslationUnit(*Context);
45 Sema S(PP, *Context, *Consumer);
Eli Friedman80f33462008-05-27 04:23:47 +000046 Parser P(PP, S);
Chris Lattnera0e328f2008-02-06 00:15:02 +000047 PP.EnterMainSourceFile();
48
49 // Initialize the parser.
50 P.Initialize();
Chris Lattner556beb72007-09-15 22:56:56 +000051
Daniel Dunbard3db4012008-10-16 16:54:18 +000052 Consumer->InitializeTU(*TU);
Chris Lattner556beb72007-09-15 22:56:56 +000053
Chris Lattnera0e328f2008-02-06 00:15:02 +000054 Parser::DeclTy *ADecl;
Ted Kremenek27f8a282008-05-20 00:43:19 +000055
Chris Lattnera0e328f2008-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 Kremenek27f8a282008-05-20 00:43:19 +000060 if (ADecl) {
61 Decl* D = static_cast<Decl*>(ADecl);
Daniel Dunbard3db4012008-10-16 16:54:18 +000062 TU->AddTopLevelDecl(D); // TranslationUnit now owns the Decl.
Ted Kremenek27f8a282008-05-20 00:43:19 +000063 Consumer->HandleTopLevelDecl(D);
64 }
Chris Lattnera0e328f2008-02-06 00:15:02 +000065 };
Ted Kremenekc87190d2008-07-02 18:32:45 +000066
Daniel Dunbard3db4012008-10-16 16:54:18 +000067 Consumer->HandleTranslationUnit(*TU);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000068
Chris Lattner556beb72007-09-15 22:56:56 +000069 if (PrintStats) {
70 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000071 P.getActions().PrintStats();
Daniel Dunbard3db4012008-10-16 16:54:18 +000072 Context->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000073 Decl::PrintStats();
74 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000075 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000076
77 Decl::CollectingStats(false);
78 Stmt::CollectingStats(false);
79 }
Daniel Dunbard3db4012008-10-16 16:54:18 +000080
81 if (FreeMemory) {
82 delete TU;
83 delete Context;
84 }
Chris Lattner556beb72007-09-15 22:56:56 +000085}