1 //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the clang::ParseAST method.
12 //===----------------------------------------------------------------------===//
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 "llvm/ADT/OwningPtr.h"
25 #include "llvm/Support/CrashRecoveryContext.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // Public interface to the file
32 //===----------------------------------------------------------------------===//
34 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
35 /// the file is parsed. This inserts the parsed decls into the translation unit
38 void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
39 ASTContext &Ctx, bool PrintStats,
40 bool CompleteTranslationUnit,
41 CodeCompleteConsumer *CompletionConsumer) {
43 llvm::OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
44 CompleteTranslationUnit,
47 // Recover resources if we crash before exiting this method.
48 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleaupSema(S.get());
50 ParseAST(*S.get(), PrintStats);
53 void clang::ParseAST(Sema &S, bool PrintStats) {
54 // Collect global stats on Decls/Stmts (until we have a module streamer).
56 Decl::CollectingStats(true);
57 Stmt::CollectingStats(true);
60 // Also turn on collection of stats inside of the Sema object.
61 bool OldCollectStats = PrintStats;
62 std::swap(OldCollectStats, S.CollectStats);
64 ASTConsumer *Consumer = &S.getASTConsumer();
66 llvm::OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S));
67 Parser &P = *ParseOP.get();
69 PrettyStackTraceParserEntry CrashInfo(P);
71 // Recover resources if we crash before exiting this method.
72 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
73 CleaupParser(ParseOP.get());
75 S.getPreprocessor().EnterMainSourceFile();
79 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
80 External->StartTranslationUnit(Consumer);
82 Parser::DeclGroupPtrTy ADecl;
84 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
85 // If we got a null return and something *was* parsed, ignore it. This
86 // is due to a top-level semicolon, an action override, or a parse error
87 // skipping something.
89 Consumer->HandleTopLevelDecl(ADecl.get());
91 // Check for any pending objective-c implementation decl.
92 while ((ADecl = P.FinishPendingObjCActions()))
93 Consumer->HandleTopLevelDecl(ADecl.get());
95 // Process any TopLevelDecls generated by #pragma weak.
96 for (llvm::SmallVector<Decl*,2>::iterator
97 I = S.WeakTopLevelDecls().begin(),
98 E = S.WeakTopLevelDecls().end(); I != E; ++I)
99 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
101 Consumer->HandleTranslationUnit(S.getASTContext());
103 std::swap(OldCollectStats, S.CollectStats);
105 llvm::errs() << "\nSTATISTICS:\n";
106 P.getActions().PrintStats();
107 S.getASTContext().PrintStats();
110 Consumer->PrintStats();