blob: fdd7d0f151a62f6258e420fce87667c58bf81bbe [file] [log] [blame]
Chris Lattnere91c1342008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnere91c1342008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Parse/ParseAST.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000016#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregore7785042009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000018#include "clang/Sema/ExternalSemaSource.h"
Chris Lattner556beb72007-09-15 22:56:56 +000019#include "clang/AST/ASTConsumer.h"
John McCall384aff82010-08-25 07:42:41 +000020#include "clang/AST/DeclCXX.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Stmt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Parse/Parser.h"
Ted Kremenek934a5712011-03-18 03:44:21 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremenek965fe842011-03-18 02:06:53 +000025#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000026#include <cstdio>
27
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030//===----------------------------------------------------------------------===//
31// Public interface to the file
32//===----------------------------------------------------------------------===//
33
Chris Lattner556beb72007-09-15 22:56:56 +000034/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000035/// the file is parsed. This inserts the parsed decls into the translation unit
36/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000037///
Ted Kremenek46157b52009-01-28 04:29:29 +000038void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000039 ASTContext &Ctx, bool PrintStats,
Douglas Gregor467dc882011-08-25 22:30:56 +000040 TranslationUnitKind TUKind,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +000041 CodeCompleteConsumer *CompletionConsumer) {
Ted Kremenek934a5712011-03-18 03:44:21 +000042
43 llvm::OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
Douglas Gregor467dc882011-08-25 22:30:56 +000044 TUKind,
Ted Kremenek934a5712011-03-18 03:44:21 +000045 CompletionConsumer));
Ted Kremenek965fe842011-03-18 02:06:53 +000046
47 // Recover resources if we crash before exiting this method.
Ted Kremenek614f96a2011-03-22 01:15:17 +000048 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleaupSema(S.get());
Ted Kremenek965fe842011-03-18 02:06:53 +000049
Ted Kremenek934a5712011-03-18 03:44:21 +000050 ParseAST(*S.get(), PrintStats);
Douglas Gregor46ea32a2010-08-12 22:51:45 +000051}
52
53void clang::ParseAST(Sema &S, bool PrintStats) {
Chris Lattner556beb72007-09-15 22:56:56 +000054 // Collect global stats on Decls/Stmts (until we have a module streamer).
55 if (PrintStats) {
56 Decl::CollectingStats(true);
57 Stmt::CollectingStats(true);
58 }
Ted Kremenek46157b52009-01-28 04:29:29 +000059
Chandler Carruth5d989942011-07-06 16:21:37 +000060 // Also turn on collection of stats inside of the Sema object.
61 bool OldCollectStats = PrintStats;
62 std::swap(OldCollectStats, S.CollectStats);
63
Douglas Gregor46ea32a2010-08-12 22:51:45 +000064 ASTConsumer *Consumer = &S.getASTConsumer();
John McCalle783d002010-08-12 21:23:27 +000065
Ted Kremenek614f96a2011-03-22 01:15:17 +000066 llvm::OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S));
67 Parser &P = *ParseOP.get();
68
69 PrettyStackTraceParserEntry CrashInfo(P);
70
71 // Recover resources if we crash before exiting this method.
72 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
73 CleaupParser(ParseOP.get());
74
Douglas Gregor46ea32a2010-08-12 22:51:45 +000075 S.getPreprocessor().EnterMainSourceFile();
John McCalld69fd7f2010-08-12 21:39:05 +000076 P.Initialize();
Douglas Gregor46ea32a2010-08-12 22:51:45 +000077 S.Initialize();
78
79 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
Douglas Gregor668c1a42009-04-21 22:25:48 +000080 External->StartTranslationUnit(Consumer);
Douglas Gregor46ea32a2010-08-12 22:51:45 +000081
Chris Lattner682bf922009-03-29 16:50:03 +000082 Parser::DeclGroupPtrTy ADecl;
Douglas Gregor46ea32a2010-08-12 22:51:45 +000083
Chris Lattnera0e328f2008-02-06 00:15:02 +000084 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
John McCalld69fd7f2010-08-12 21:39:05 +000085 // 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.
Chris Lattner682bf922009-03-29 16:50:03 +000088 if (ADecl)
John McCall2b5289b2010-08-23 07:28:44 +000089 Consumer->HandleTopLevelDecl(ADecl.get());
Chris Lattnera0e328f2008-02-06 00:15:02 +000090 };
Fariborz Jahanian63e963c2009-11-16 18:57:01 +000091 // Check for any pending objective-c implementation decl.
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000092 while ((ADecl = P.FinishPendingObjCActions()))
John McCall2b5289b2010-08-23 07:28:44 +000093 Consumer->HandleTopLevelDecl(ADecl.get());
Douglas Gregor46ea32a2010-08-12 22:51:45 +000094
Daniel Dunbarec2a4ed2009-12-01 21:57:20 +000095 // Process any TopLevelDecls generated by #pragma weak.
Chris Lattner5f9e2722011-07-23 10:55:15 +000096 for (SmallVector<Decl*,2>::iterator
Douglas Gregor46ea32a2010-08-12 22:51:45 +000097 I = S.WeakTopLevelDecls().begin(),
98 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +000099 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000100
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000101 Consumer->HandleTranslationUnit(S.getASTContext());
Chandler Carruth5d989942011-07-06 16:21:37 +0000102
103 std::swap(OldCollectStats, S.CollectStats);
Chris Lattner556beb72007-09-15 22:56:56 +0000104 if (PrintStats) {
Chandler Carruthcd92a652011-07-04 05:32:14 +0000105 llvm::errs() << "\nSTATISTICS:\n";
Chris Lattnera0e328f2008-02-06 00:15:02 +0000106 P.getActions().PrintStats();
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000107 S.getASTContext().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000108 Decl::PrintStats();
109 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000110 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000111 }
112}