blob: 5346a59ae1ae6de0c330d4a85a23ad55f668d0c7 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTStreamer.cpp - Provide streaming interface to ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTStreamer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/ASTStreamer.h"
15#include "clang/AST/ASTContext.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000016#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "Sema.h"
18#include "clang/Parse/Action.h"
19#include "clang/Parse/Parser.h"
20using namespace clang;
21
Chris Lattner1cc01712007-09-15 22:56:56 +000022ASTConsumer::~ASTConsumer() {}
23
Chris Lattner4b009652007-07-25 00:24:17 +000024//===----------------------------------------------------------------------===//
25// Public interface to the file
26//===----------------------------------------------------------------------===//
27
Chris Lattner1cc01712007-09-15 22:56:56 +000028/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner8593cbf2007-11-03 06:24:16 +000029/// the file is parsed. This takes ownership of the ASTConsumer and
30/// ultimately deletes it.
Ted Kremenek17861c52007-12-19 22:51:13 +000031void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Chris Lattner1cc01712007-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 Naroff4ed9d662007-09-27 14:38:14 +000039 PP.getIdentifierTable(), PP.getSelectorTable());
Chris Lattner1cc01712007-09-15 22:56:56 +000040
Chris Lattner988f2ad2008-02-06 00:15:02 +000041 Parser P(PP, *new Sema(PP, Context));
42 PP.EnterMainSourceFile();
43
44 // Initialize the parser.
45 P.Initialize();
Chris Lattner1cc01712007-09-15 22:56:56 +000046
Ted Kremenek17861c52007-12-19 22:51:13 +000047 Consumer->Initialize(Context);
Chris Lattner1cc01712007-09-15 22:56:56 +000048
Chris Lattner988f2ad2008-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 Jahanianf185aef2007-10-26 19:46:17 +000057
Chris Lattner1cc01712007-09-15 22:56:56 +000058 if (PrintStats) {
59 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner988f2ad2008-02-06 00:15:02 +000060 P.getActions().PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000061 Context.PrintStats();
62 Decl::PrintStats();
63 Stmt::PrintStats();
Chris Lattner8593cbf2007-11-03 06:24:16 +000064 Consumer->PrintStats();
Chris Lattner1cc01712007-09-15 22:56:56 +000065
66 Decl::CollectingStats(false);
67 Stmt::CollectingStats(false);
68 }
Chris Lattner8593cbf2007-11-03 06:24:16 +000069
70 delete Consumer;
Chris Lattner1cc01712007-09-15 22:56:56 +000071}