blob: 43736e32ac55888df5f8a8057d0d40e8541c97ad [file] [log] [blame]
Chris Lattner8082d872008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Chris Lattner73709ed2006-08-17 06:28:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner73709ed2006-08-17 06:28:25 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8082d872008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Chris Lattner73709ed2006-08-17 06:28:25 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner8082d872008-02-06 00:23:21 +000014#include "clang/Sema/ParseAST.h"
Steve Naroff2c055d22007-02-28 19:32:13 +000015#include "clang/AST/ASTContext.h"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000017#include "clang/AST/TranslationUnit.h"
Chris Lattnerddd6fc82006-11-10 04:58:55 +000018#include "Sema.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000019#include "clang/Parse/Action.h"
20#include "clang/Parse/Parser.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000021using namespace clang;
22
Chris Lattner73709ed2006-08-17 06:28:25 +000023//===----------------------------------------------------------------------===//
24// Public interface to the file
25//===----------------------------------------------------------------------===//
26
Chris Lattner75e0c8c2007-09-15 22:56:56 +000027/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner376cdaf2007-11-03 06:24:16 +000028/// the file is parsed. This takes ownership of the ASTConsumer and
29/// ultimately deletes it.
Ted Kremenekdcb5e382008-08-08 02:46:37 +000030void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +000031 // Collect global stats on Decls/Stmts (until we have a module streamer).
32 if (PrintStats) {
33 Decl::CollectingStats(true);
34 Stmt::CollectingStats(true);
35 }
36
Ted Kremenekb147ad12008-06-04 15:55:15 +000037 ASTContext Context(PP.getLangOptions(), PP.getSourceManager(),
38 PP.getTargetInfo(),
Steve Narofff73590d2007-09-27 14:38:14 +000039 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner75e0c8c2007-09-15 22:56:56 +000040
Ted Kremenekb147ad12008-06-04 15:55:15 +000041 TranslationUnit TU(Context);
Ted Kremenek34f664d2008-06-16 18:00:42 +000042 TU.SetOwnsDecls(false);
43
Eli Friedmanac0285a2008-05-27 04:23:47 +000044
45 Sema S(PP, Context, *Consumer);
46 Parser P(PP, S);
Chris Lattner4afeded2008-02-06 00:15:02 +000047 PP.EnterMainSourceFile();
48
49 // Initialize the parser.
50 P.Initialize();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000051
Ted Kremenek380df932008-05-31 20:11:04 +000052 Consumer->InitializeTU(TU);
Chris Lattner75e0c8c2007-09-15 22:56:56 +000053
Chris Lattner4afeded2008-02-06 00:15:02 +000054 Parser::DeclTy *ADecl;
Ted Kremenekce20e8f2008-05-20 00:43:19 +000055
Chris Lattner4afeded2008-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 Kremenekce20e8f2008-05-20 00:43:19 +000060 if (ADecl) {
61 Decl* D = static_cast<Decl*>(ADecl);
62 TU.AddTopLevelDecl(D); // TranslationUnit now owns the Decl.
63 Consumer->HandleTopLevelDecl(D);
64 }
Chris Lattner4afeded2008-02-06 00:15:02 +000065 };
Ted Kremenekc3b30342008-07-02 18:32:45 +000066
67 Consumer->HandleTranslationUnit(TU);
Fariborz Jahanian99e96b02007-10-26 19:46:17 +000068
Chris Lattner75e0c8c2007-09-15 22:56:56 +000069 if (PrintStats) {
70 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner4afeded2008-02-06 00:15:02 +000071 P.getActions().PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000072 Context.PrintStats();
73 Decl::PrintStats();
74 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +000075 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000076
77 Decl::CollectingStats(false);
78 Stmt::CollectingStats(false);
79 }
80}