blob: bd4f859521202232be557c720f390c95045b9a08 [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"
Jordan Roseccf43ca2012-06-06 17:25:21 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000017#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000018#include "clang/Sema/SemaConsumer.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000019#include "clang/Sema/ExternalSemaSource.h"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000020#include "clang/AST/ASTConsumer.h"
Benjamin Kramer1ea8e092012-07-04 17:04:04 +000021#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000022#include "clang/AST/DeclCXX.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000023#include "clang/AST/ExternalASTSource.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000024#include "clang/AST/Stmt.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000025#include "clang/Parse/Parser.h"
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremenek750028b2011-03-18 02:06:53 +000027#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwindb714922009-08-24 13:25:12 +000028#include <cstdio>
29
Chris Lattner73709ed2006-08-17 06:28:25 +000030using namespace clang;
31
Chris Lattner73709ed2006-08-17 06:28:25 +000032//===----------------------------------------------------------------------===//
33// Public interface to the file
34//===----------------------------------------------------------------------===//
35
Chris Lattner75e0c8c2007-09-15 22:56:56 +000036/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner66918ee2009-03-28 04:13:34 +000037/// the file is parsed. This inserts the parsed decls into the translation unit
38/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000039///
Ted Kremenek062115a2009-01-28 04:29:29 +000040void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregor54feb842009-04-14 16:27:31 +000041 ASTContext &Ctx, bool PrintStats,
Douglas Gregor69f74f82011-08-25 22:30:56 +000042 TranslationUnitKind TUKind,
Erik Verbruggen6e922512012-04-12 10:11:59 +000043 CodeCompleteConsumer *CompletionConsumer,
44 bool SkipFunctionBodies) {
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000045
Dylan Noblesmithe2778992012-02-05 02:12:40 +000046 OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
Douglas Gregor69f74f82011-08-25 22:30:56 +000047 TUKind,
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000048 CompletionConsumer));
Ted Kremenek750028b2011-03-18 02:06:53 +000049
50 // Recover resources if we crash before exiting this method.
Erik Verbruggen6e922512012-04-12 10:11:59 +000051 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
Ted Kremenek750028b2011-03-18 02:06:53 +000052
Erik Verbruggen6e922512012-04-12 10:11:59 +000053 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000054}
55
Erik Verbruggen6e922512012-04-12 10:11:59 +000056void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +000057 // Collect global stats on Decls/Stmts (until we have a module streamer).
58 if (PrintStats) {
Daniel Dunbar62905572012-03-05 21:42:49 +000059 Decl::EnableStatistics();
60 Stmt::EnableStatistics();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000061 }
Ted Kremenek062115a2009-01-28 04:29:29 +000062
Chandler Carruthb4836ea2011-07-06 16:21:37 +000063 // Also turn on collection of stats inside of the Sema object.
64 bool OldCollectStats = PrintStats;
65 std::swap(OldCollectStats, S.CollectStats);
66
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000067 ASTConsumer *Consumer = &S.getASTConsumer();
John McCall0af2b7c2010-08-12 21:23:27 +000068
Erik Verbruggen6e922512012-04-12 10:11:59 +000069 OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
70 SkipFunctionBodies));
Ted Kremenek4c9d46b2011-03-22 01:15:17 +000071 Parser &P = *ParseOP.get();
72
73 PrettyStackTraceParserEntry CrashInfo(P);
74
75 // Recover resources if we crash before exiting this method.
76 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
Erik Verbruggen6e922512012-04-12 10:11:59 +000077 CleanupParser(ParseOP.get());
Ted Kremenek4c9d46b2011-03-22 01:15:17 +000078
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000079 S.getPreprocessor().EnterMainSourceFile();
John McCall112fd082010-08-12 21:39:05 +000080 P.Initialize();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000081 S.Initialize();
Jordan Roseccf43ca2012-06-06 17:25:21 +000082
83 // C11 6.9p1 says translation units must have at least one top-level
84 // declaration. C++ doesn't have this restriction. We also don't want to
85 // complain if we have a precompiled header, although technically if the PCH
86 // is empty we should still emit the (pedantic) diagnostic.
Chris Lattner5bbb3c82009-03-29 16:50:03 +000087 Parser::DeclGroupPtrTy ADecl;
Meador Ingecdc00572012-06-19 18:17:30 +000088 ExternalASTSource *External = S.getASTContext().getExternalSource();
89 if (External)
90 External->StartTranslationUnit(Consumer);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000091
Meador Ingecdc00572012-06-19 18:17:30 +000092 if (P.ParseTopLevelDecl(ADecl)) {
93 if (!External && !S.getLangOpts().CPlusPlus)
94 P.Diag(diag::ext_empty_translation_unit);
Jordan Roseccf43ca2012-06-06 17:25:21 +000095 } else {
Meador Ingecdc00572012-06-19 18:17:30 +000096 do {
Jordan Roseccf43ca2012-06-06 17:25:21 +000097 // If we got a null return and something *was* parsed, ignore it. This
98 // is due to a top-level semicolon, an action override, or a parse error
99 // skipping something.
Meador Ingecdc00572012-06-19 18:17:30 +0000100 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
101 return;
102 } while (!P.ParseTopLevelDecl(ADecl));
Jordan Roseccf43ca2012-06-06 17:25:21 +0000103 }
Meador Ingecdc00572012-06-19 18:17:30 +0000104
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +0000105 // Process any TopLevelDecls generated by #pragma weak.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000106 for (SmallVector<Decl*,2>::iterator
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000107 I = S.WeakTopLevelDecls().begin(),
108 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynnd963a492009-07-31 02:52:19 +0000109 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000110
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000111 Consumer->HandleTranslationUnit(S.getASTContext());
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000112
113 std::swap(OldCollectStats, S.CollectStats);
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000114 if (PrintStats) {
Chandler Carruth3c147a72011-07-04 05:32:14 +0000115 llvm::errs() << "\nSTATISTICS:\n";
Chris Lattner4afeded2008-02-06 00:15:02 +0000116 P.getActions().PrintStats();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000117 S.getASTContext().PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000118 Decl::PrintStats();
119 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +0000120 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000121 }
122}