blob: bca0fa7ba501f1475a3537be961bdb74a512f73f [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
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/ParseAST.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000016#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000018#include "clang/Sema/ExternalSemaSource.h"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000019#include "clang/AST/ASTConsumer.h"
John McCall28a0cf72010-08-25 07:42:41 +000020#include "clang/AST/DeclCXX.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000021#include "clang/AST/ExternalASTSource.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Stmt.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000023#include "clang/Parse/Parser.h"
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremenek750028b2011-03-18 02:06:53 +000025#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwindb714922009-08-24 13:25:12 +000026#include <cstdio>
27
Chris Lattner73709ed2006-08-17 06:28:25 +000028using namespace clang;
29
Chris Lattner73709ed2006-08-17 06:28:25 +000030//===----------------------------------------------------------------------===//
31// Public interface to the file
32//===----------------------------------------------------------------------===//
33
Chris Lattner75e0c8c2007-09-15 22:56:56 +000034/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner66918ee2009-03-28 04:13:34 +000035/// the file is parsed. This inserts the parsed decls into the translation unit
36/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000037///
Ted Kremenek062115a2009-01-28 04:29:29 +000038void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregor54feb842009-04-14 16:27:31 +000039 ASTContext &Ctx, bool PrintStats,
Douglas Gregor2436e712009-09-17 21:32:03 +000040 bool CompleteTranslationUnit,
Daniel Dunbar242ea9a2009-11-13 08:58:20 +000041 CodeCompleteConsumer *CompletionConsumer) {
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000042
43 llvm::OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
44 CompleteTranslationUnit,
45 CompletionConsumer));
Ted Kremenek750028b2011-03-18 02:06:53 +000046
47 // Recover resources if we crash before exiting this method.
48 llvm::CrashRecoveryContextCleanupRegistrar
49 SemaCleanupInCrash(llvm::CrashRecoveryContextCleanup::
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000050 create<Sema>(S.get()));
Ted Kremenek750028b2011-03-18 02:06:53 +000051
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000052 ParseAST(*S.get(), PrintStats);
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000053}
54
55void clang::ParseAST(Sema &S, bool PrintStats) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +000056 // Collect global stats on Decls/Stmts (until we have a module streamer).
57 if (PrintStats) {
58 Decl::CollectingStats(true);
59 Stmt::CollectingStats(true);
60 }
Ted Kremenek062115a2009-01-28 04:29:29 +000061
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000062 ASTConsumer *Consumer = &S.getASTConsumer();
John McCall0af2b7c2010-08-12 21:23:27 +000063
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000064 Parser P(S.getPreprocessor(), S);
65 S.getPreprocessor().EnterMainSourceFile();
John McCall112fd082010-08-12 21:39:05 +000066 P.Initialize();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000067 S.Initialize();
68
69 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
Douglas Gregora868bbd2009-04-21 22:25:48 +000070 External->StartTranslationUnit(Consumer);
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000071
Chris Lattner5bbb3c82009-03-29 16:50:03 +000072 Parser::DeclGroupPtrTy ADecl;
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000073
Chris Lattner4afeded2008-02-06 00:15:02 +000074 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
John McCall112fd082010-08-12 21:39:05 +000075 // If we got a null return and something *was* parsed, ignore it. This
76 // is due to a top-level semicolon, an action override, or a parse error
77 // skipping something.
Chris Lattner5bbb3c82009-03-29 16:50:03 +000078 if (ADecl)
John McCall3e56fd42010-08-23 07:28:44 +000079 Consumer->HandleTopLevelDecl(ADecl.get());
Chris Lattner4afeded2008-02-06 00:15:02 +000080 };
Fariborz Jahanian9290ede2009-11-16 18:57:01 +000081 // Check for any pending objective-c implementation decl.
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +000082 while ((ADecl = P.FinishPendingObjCActions()))
John McCall3e56fd42010-08-23 07:28:44 +000083 Consumer->HandleTopLevelDecl(ADecl.get());
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000084
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +000085 // Process any TopLevelDecls generated by #pragma weak.
Ryan Flynnd963a492009-07-31 02:52:19 +000086 for (llvm::SmallVector<Decl*,2>::iterator
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000087 I = S.WeakTopLevelDecls().begin(),
88 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynnd963a492009-07-31 02:52:19 +000089 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000090
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000091 Consumer->HandleTranslationUnit(S.getASTContext());
92
Chris Lattner75e0c8c2007-09-15 22:56:56 +000093 if (PrintStats) {
94 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner4afeded2008-02-06 00:15:02 +000095 P.getActions().PrintStats();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000096 S.getASTContext().PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000097 Decl::PrintStats();
98 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +000099 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000100 }
101}