blob: d02787941b0dc168cd9109587f0b91b4b236a39e [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"
Torok Edwinf42e4a62009-08-24 13:25:12 +000024#include <cstdio>
25
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Daniel Dunbarb69eca52010-04-08 02:59:56 +000028static void DumpRecordLayouts(ASTContext &C) {
29 for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
30 I != E; ++I) {
31 const RecordType *RT = dyn_cast<RecordType>(*I);
32 if (!RT)
33 continue;
34
35 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
36 if (!RD || RD->isImplicit() || RD->isDependentType() ||
37 RD->isInvalidDecl() || !RD->getDefinition())
38 continue;
39
40 // FIXME: Do we really need to hard code this?
41 if (RD->getQualifiedNameAsString() == "__va_list_tag")
42 continue;
43
44 C.DumpRecordLayout(RD, llvm::errs());
45 }
46}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048//===----------------------------------------------------------------------===//
49// Public interface to the file
50//===----------------------------------------------------------------------===//
51
Chris Lattner556beb72007-09-15 22:56:56 +000052/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000053/// the file is parsed. This inserts the parsed decls into the translation unit
54/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000055///
Ted Kremenek46157b52009-01-28 04:29:29 +000056void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000057 ASTContext &Ctx, bool PrintStats,
Douglas Gregor81b747b2009-09-17 21:32:03 +000058 bool CompleteTranslationUnit,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +000059 CodeCompleteConsumer *CompletionConsumer) {
Douglas Gregor46ea32a2010-08-12 22:51:45 +000060 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
61 ParseAST(S, PrintStats);
62}
63
64void clang::ParseAST(Sema &S, bool PrintStats) {
Chris Lattner556beb72007-09-15 22:56:56 +000065 // Collect global stats on Decls/Stmts (until we have a module streamer).
66 if (PrintStats) {
67 Decl::CollectingStats(true);
68 Stmt::CollectingStats(true);
69 }
Ted Kremenek46157b52009-01-28 04:29:29 +000070
Douglas Gregor46ea32a2010-08-12 22:51:45 +000071 ASTConsumer *Consumer = &S.getASTConsumer();
John McCalle783d002010-08-12 21:23:27 +000072
Douglas Gregor46ea32a2010-08-12 22:51:45 +000073 Parser P(S.getPreprocessor(), S);
74 S.getPreprocessor().EnterMainSourceFile();
John McCalld69fd7f2010-08-12 21:39:05 +000075 P.Initialize();
Douglas Gregor46ea32a2010-08-12 22:51:45 +000076 S.Initialize();
77
78 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
Douglas Gregor668c1a42009-04-21 22:25:48 +000079 External->StartTranslationUnit(Consumer);
Douglas Gregor46ea32a2010-08-12 22:51:45 +000080
Chris Lattner682bf922009-03-29 16:50:03 +000081 Parser::DeclGroupPtrTy ADecl;
Douglas Gregor46ea32a2010-08-12 22:51:45 +000082
Chris Lattnera0e328f2008-02-06 00:15:02 +000083 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
John McCalld69fd7f2010-08-12 21:39:05 +000084 // If we got a null return and something *was* parsed, ignore it. This
85 // is due to a top-level semicolon, an action override, or a parse error
86 // skipping something.
Chris Lattner682bf922009-03-29 16:50:03 +000087 if (ADecl)
John McCall2b5289b2010-08-23 07:28:44 +000088 Consumer->HandleTopLevelDecl(ADecl.get());
Chris Lattnera0e328f2008-02-06 00:15:02 +000089 };
Fariborz Jahanian63e963c2009-11-16 18:57:01 +000090 // Check for any pending objective-c implementation decl.
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000091 while ((ADecl = P.FinishPendingObjCActions()))
John McCall2b5289b2010-08-23 07:28:44 +000092 Consumer->HandleTopLevelDecl(ADecl.get());
Douglas Gregor46ea32a2010-08-12 22:51:45 +000093
Daniel Dunbarec2a4ed2009-12-01 21:57:20 +000094 // Process any TopLevelDecls generated by #pragma weak.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +000095 for (llvm::SmallVector<Decl*,2>::iterator
Douglas Gregor46ea32a2010-08-12 22:51:45 +000096 I = S.WeakTopLevelDecls().begin(),
97 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +000098 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor46ea32a2010-08-12 22:51:45 +000099
Daniel Dunbarb69eca52010-04-08 02:59:56 +0000100 // Dump record layouts, if requested.
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000101 if (S.getLangOptions().DumpRecordLayouts)
102 DumpRecordLayouts(S.getASTContext());
103
104 Consumer->HandleTranslationUnit(S.getASTContext());
105
Chris Lattner556beb72007-09-15 22:56:56 +0000106 if (PrintStats) {
107 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +0000108 P.getActions().PrintStats();
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000109 S.getASTContext().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000110 Decl::PrintStats();
111 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000112 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000113 }
114}