Amazing that there are still issues with the fields of anonymous struct/unions..
[clang.git] / lib / Parse / ParseAST.cpp
blobd02787941b0dc168cd9109587f0b91b4b236a39e
1 //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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 <cstdio>
26 using namespace clang;
28 static 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;
35 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
36 if (!RD || RD->isImplicit() || RD->isDependentType() ||
37 RD->isInvalidDecl() || !RD->getDefinition())
38 continue;
40 // FIXME: Do we really need to hard code this?
41 if (RD->getQualifiedNameAsString() == "__va_list_tag")
42 continue;
44 C.DumpRecordLayout(RD, llvm::errs());
48 //===----------------------------------------------------------------------===//
49 // Public interface to the file
50 //===----------------------------------------------------------------------===//
52 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
53 /// the file is parsed. This inserts the parsed decls into the translation unit
54 /// held by Ctx.
55 ///
56 void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
57 ASTContext &Ctx, bool PrintStats,
58 bool CompleteTranslationUnit,
59 CodeCompleteConsumer *CompletionConsumer) {
60 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
61 ParseAST(S, PrintStats);
64 void clang::ParseAST(Sema &S, bool PrintStats) {
65 // Collect global stats on Decls/Stmts (until we have a module streamer).
66 if (PrintStats) {
67 Decl::CollectingStats(true);
68 Stmt::CollectingStats(true);
71 ASTConsumer *Consumer = &S.getASTConsumer();
73 Parser P(S.getPreprocessor(), S);
74 S.getPreprocessor().EnterMainSourceFile();
75 P.Initialize();
76 S.Initialize();
78 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
79 External->StartTranslationUnit(Consumer);
81 Parser::DeclGroupPtrTy ADecl;
83 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
84 // 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.
87 if (ADecl)
88 Consumer->HandleTopLevelDecl(ADecl.get());
90 // Check for any pending objective-c implementation decl.
91 while ((ADecl = P.FinishPendingObjCActions()))
92 Consumer->HandleTopLevelDecl(ADecl.get());
94 // Process any TopLevelDecls generated by #pragma weak.
95 for (llvm::SmallVector<Decl*,2>::iterator
96 I = S.WeakTopLevelDecls().begin(),
97 E = S.WeakTopLevelDecls().end(); I != E; ++I)
98 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
100 // Dump record layouts, if requested.
101 if (S.getLangOptions().DumpRecordLayouts)
102 DumpRecordLayouts(S.getASTContext());
104 Consumer->HandleTranslationUnit(S.getASTContext());
106 if (PrintStats) {
107 fprintf(stderr, "\nSTATISTICS:\n");
108 P.getActions().PrintStats();
109 S.getASTContext().PrintStats();
110 Decl::PrintStats();
111 Stmt::PrintStats();
112 Consumer->PrintStats();