blob: bcff1c8cfbc75376f893df72a77f515717038464 [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
Ted Kremenek062115a2009-01-28 04:29:29 +000014#include <llvm/ADT/OwningPtr.h>
Chris Lattner8082d872008-02-06 00:23:21 +000015#include "clang/Sema/ParseAST.h"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000017#include "clang/AST/Stmt.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000018#include "clang/AST/TranslationUnit.h"
Chris Lattnerddd6fc82006-11-10 04:58:55 +000019#include "Sema.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000020#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 Lattner66918ee2009-03-28 04:13:34 +000028/// the file is parsed. This inserts the parsed decls into the translation unit
29/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000030///
Ted Kremenek062115a2009-01-28 04:29:29 +000031void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Chris Lattner66918ee2009-03-28 04:13:34 +000032 ASTContext &Ctx, bool PrintStats) {
Chris Lattner75e0c8c2007-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 Kremenek062115a2009-01-28 04:29:29 +000038
Chris Lattner66918ee2009-03-28 04:13:34 +000039 Sema S(PP, Ctx, *Consumer);
Eli Friedmanac0285a2008-05-27 04:23:47 +000040 Parser P(PP, S);
Chris Lattner4afeded2008-02-06 00:15:02 +000041 PP.EnterMainSourceFile();
42
43 // Initialize the parser.
44 P.Initialize();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000045
Chris Lattner66918ee2009-03-28 04:13:34 +000046 Consumer->Initialize(Ctx);
Chris Lattner75e0c8c2007-09-15 22:56:56 +000047
Chris Lattner4afeded2008-02-06 00:15:02 +000048 Parser::DeclTy *ADecl;
Ted Kremenekce20e8f2008-05-20 00:43:19 +000049
Chris Lattner4afeded2008-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 Kremenekce20e8f2008-05-20 00:43:19 +000054 if (ADecl) {
55 Decl* D = static_cast<Decl*>(ADecl);
Ted Kremenekce20e8f2008-05-20 00:43:19 +000056 Consumer->HandleTopLevelDecl(D);
57 }
Chris Lattner4afeded2008-02-06 00:15:02 +000058 };
Ted Kremenekc3b30342008-07-02 18:32:45 +000059
Chris Lattner66918ee2009-03-28 04:13:34 +000060 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanian99e96b02007-10-26 19:46:17 +000061
Chris Lattner75e0c8c2007-09-15 22:56:56 +000062 if (PrintStats) {
63 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner4afeded2008-02-06 00:15:02 +000064 P.getActions().PrintStats();
Chris Lattner66918ee2009-03-28 04:13:34 +000065 Ctx.PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000066 Decl::PrintStats();
67 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +000068 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000069
70 Decl::CollectingStats(false);
71 Stmt::CollectingStats(false);
72 }
73}