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 TranslationUnitKind TUKind,
41 CodeCompleteConsumer *CompletionConsumer,
42 bool SkipFunctionBodies) {
44 OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
48 // Recover resources if we crash before exiting this method.
49 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
51 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
54 void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
55 // Collect global stats on Decls/Stmts (until we have a module streamer).
57 Decl::EnableStatistics();
58 Stmt::EnableStatistics();
61 // Also turn on collection of stats inside of the Sema object.
62 bool OldCollectStats = PrintStats;
63 std::swap(OldCollectStats, S.CollectStats);
65 ASTConsumer *Consumer = &S.getASTConsumer();
67 OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
69 Parser &P = *ParseOP.get();
71 PrettyStackTraceParserEntry CrashInfo(P);
73 // Recover resources if we crash before exiting this method.
74 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
75 CleanupParser(ParseOP.get());
77 S.getPreprocessor().EnterMainSourceFile();
81 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
82 External->StartTranslationUnit(Consumer);
85 Parser::DeclGroupPtrTy ADecl;
87 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
88 // If we got a null return and something *was* parsed, ignore it. This
89 // is due to a top-level semicolon, an action override, or a parse error
90 // skipping something.
92 if (!Consumer->HandleTopLevelDecl(ADecl.get())) {
102 // Process any TopLevelDecls generated by #pragma weak.
103 for (SmallVector<Decl*,2>::iterator
104 I = S.WeakTopLevelDecls().begin(),
105 E = S.WeakTopLevelDecls().end(); I != E; ++I)
106 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
108 Consumer->HandleTranslationUnit(S.getASTContext());
110 std::swap(OldCollectStats, S.CollectStats);
112 llvm::errs() << "\nSTATISTICS:\n";
113 P.getActions().PrintStats();
114 S.getASTContext().PrintStats();
117 Consumer->PrintStats();