ParseAST.cpp revision d9015eee474115c8ecf616a02f3ed30be2b91203
1//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the clang::ParseAST method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/ParseAST.h"
15#include "clang/Sema/Sema.h"
16#include "clang/Sema/CodeCompleteConsumer.h"
17#include "clang/Sema/SemaConsumer.h"
18#include "clang/Sema/ExternalSemaSource.h"
19#include "clang/AST/ASTConsumer.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/ExternalASTSource.h"
22#include "clang/AST/Stmt.h"
23#include "clang/Parse/Parser.h"
24#include <cstdio>
25
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Public interface to the file
30//===----------------------------------------------------------------------===//
31
32/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
33/// the file is parsed.  This inserts the parsed decls into the translation unit
34/// held by Ctx.
35///
36void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
37                     ASTContext &Ctx, bool PrintStats,
38                     bool CompleteTranslationUnit,
39                     CodeCompleteConsumer *CompletionConsumer) {
40  Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
41  ParseAST(S, PrintStats);
42}
43
44void clang::ParseAST(Sema &S, bool PrintStats) {
45  // Collect global stats on Decls/Stmts (until we have a module streamer).
46  if (PrintStats) {
47    Decl::CollectingStats(true);
48    Stmt::CollectingStats(true);
49  }
50
51  ASTConsumer *Consumer = &S.getASTConsumer();
52
53  Parser P(S.getPreprocessor(), S);
54  S.getPreprocessor().EnterMainSourceFile();
55  P.Initialize();
56  S.Initialize();
57
58  if (ExternalASTSource *External = S.getASTContext().getExternalSource())
59    External->StartTranslationUnit(Consumer);
60
61  Parser::DeclGroupPtrTy ADecl;
62
63  while (!P.ParseTopLevelDecl(ADecl)) {  // Not end of file.
64    // If we got a null return and something *was* parsed, ignore it.  This
65    // is due to a top-level semicolon, an action override, or a parse error
66    // skipping something.
67    if (ADecl)
68      Consumer->HandleTopLevelDecl(ADecl.get());
69  };
70  // Check for any pending objective-c implementation decl.
71  while ((ADecl = P.FinishPendingObjCActions()))
72    Consumer->HandleTopLevelDecl(ADecl.get());
73
74  // Process any TopLevelDecls generated by #pragma weak.
75  for (llvm::SmallVector<Decl*,2>::iterator
76       I = S.WeakTopLevelDecls().begin(),
77       E = S.WeakTopLevelDecls().end(); I != E; ++I)
78    Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
79
80  Consumer->HandleTranslationUnit(S.getASTContext());
81
82  if (PrintStats) {
83    fprintf(stderr, "\nSTATISTICS:\n");
84    P.getActions().PrintStats();
85    S.getASTContext().PrintStats();
86    Decl::PrintStats();
87    Stmt::PrintStats();
88    Consumer->PrintStats();
89  }
90}
91