blob: 099171e56fbbeb07079c7965ab5373b0391d63ce [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
Chris Lattner8082d872008-02-06 00:23:21 +000014#include "clang/Sema/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"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000020#include "clang/AST/ExternalASTSource.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Stmt.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000022#include "clang/Parse/Parser.h"
Torok Edwindb714922009-08-24 13:25:12 +000023#include <cstdio>
24
Chris Lattner73709ed2006-08-17 06:28:25 +000025using namespace clang;
26
Daniel Dunbar8ee67602010-04-08 02:59:56 +000027static void DumpRecordLayouts(ASTContext &C) {
28 for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end();
29 I != E; ++I) {
30 const RecordType *RT = dyn_cast<RecordType>(*I);
31 if (!RT)
32 continue;
33
34 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
35 if (!RD || RD->isImplicit() || RD->isDependentType() ||
36 RD->isInvalidDecl() || !RD->getDefinition())
37 continue;
38
39 // FIXME: Do we really need to hard code this?
40 if (RD->getQualifiedNameAsString() == "__va_list_tag")
41 continue;
42
43 C.DumpRecordLayout(RD, llvm::errs());
44 }
45}
46
Chris Lattner73709ed2006-08-17 06:28:25 +000047//===----------------------------------------------------------------------===//
48// Public interface to the file
49//===----------------------------------------------------------------------===//
50
Chris Lattner75e0c8c2007-09-15 22:56:56 +000051/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner66918ee2009-03-28 04:13:34 +000052/// the file is parsed. This inserts the parsed decls into the translation unit
53/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000054///
Ted Kremenek062115a2009-01-28 04:29:29 +000055void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregor54feb842009-04-14 16:27:31 +000056 ASTContext &Ctx, bool PrintStats,
Douglas Gregor2436e712009-09-17 21:32:03 +000057 bool CompleteTranslationUnit,
Daniel Dunbar242ea9a2009-11-13 08:58:20 +000058 CodeCompleteConsumer *CompletionConsumer) {
Douglas Gregorad814e22010-08-12 20:50:39 +000059 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
60 ParseAST(S, PrintStats);
61}
62
63void clang::ParseAST(Sema &S, bool PrintStats) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +000064 // Collect global stats on Decls/Stmts (until we have a module streamer).
65 if (PrintStats) {
66 Decl::CollectingStats(true);
67 Stmt::CollectingStats(true);
68 }
Ted Kremenek062115a2009-01-28 04:29:29 +000069
Douglas Gregorad814e22010-08-12 20:50:39 +000070 Parser P(S.getPreprocessor(), S);
71 S.getPreprocessor().EnterMainSourceFile();
72
73 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
Douglas Gregora868bbd2009-04-21 22:25:48 +000074 External->StartTranslationUnit(Consumer);
Douglas Gregorad814e22010-08-12 20:50:39 +000075
Chris Lattner5bbb3c82009-03-29 16:50:03 +000076 Parser::DeclGroupPtrTy ADecl;
Douglas Gregorad814e22010-08-12 20:50:39 +000077
Chris Lattner4afeded2008-02-06 00:15:02 +000078 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
Douglas Gregorad814e22010-08-12 20:50:39 +000079 // If we got a null return and something *was* parsed, ignore it. This
80 // is due to a top-level semicolon, an action override, or a parse error
81 // skipping something.
Chris Lattner5bbb3c82009-03-29 16:50:03 +000082 if (ADecl)
83 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattner4afeded2008-02-06 00:15:02 +000084 };
Fariborz Jahanian9290ede2009-11-16 18:57:01 +000085 // Check for any pending objective-c implementation decl.
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +000086 while ((ADecl = P.FinishPendingObjCActions()))
Fariborz Jahanian9290ede2009-11-16 18:57:01 +000087 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Douglas Gregorad814e22010-08-12 20:50:39 +000088
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +000089 // Process any TopLevelDecls generated by #pragma weak.
Ryan Flynnd963a492009-07-31 02:52:19 +000090 for (llvm::SmallVector<Decl*,2>::iterator
Douglas Gregorad814e22010-08-12 20:50:39 +000091 I = S.WeakTopLevelDecls().begin(),
92 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynnd963a492009-07-31 02:52:19 +000093 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregorad814e22010-08-12 20:50:39 +000094
Daniel Dunbar8ee67602010-04-08 02:59:56 +000095 // Dump record layouts, if requested.
96 if (PP.getLangOptions().DumpRecordLayouts)
97 DumpRecordLayouts(Ctx);
Douglas Gregorad814e22010-08-12 20:50:39 +000098
Chris Lattner66918ee2009-03-28 04:13:34 +000099 Consumer->HandleTranslationUnit(Ctx);
Douglas Gregorad814e22010-08-12 20:50:39 +0000100
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000101 if (PrintStats) {
102 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner4afeded2008-02-06 00:15:02 +0000103 P.getActions().PrintStats();
Chris Lattner66918ee2009-03-28 04:13:34 +0000104 Ctx.PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000105 Decl::PrintStats();
106 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +0000107 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000108 }
109}