blob: 3f86c4d2c89bc61f94695c8b6c247900a1fe53f4 [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"
Jordan Rosed73ef132012-06-06 17:25:21 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000017#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregore7785042009-04-20 15:53:59 +000018#include "clang/Sema/SemaConsumer.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000019#include "clang/Sema/ExternalSemaSource.h"
Chris Lattner556beb72007-09-15 22:56:56 +000020#include "clang/AST/ASTConsumer.h"
John McCall384aff82010-08-25 07:42:41 +000021#include "clang/AST/DeclCXX.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000022#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000023#include "clang/AST/Stmt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Parse/Parser.h"
Ted Kremenek934a5712011-03-18 03:44:21 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek965fe842011-03-18 02:06:53 +000026#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000027#include <cstdio>
28
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Reid Spencer5f016e22007-07-11 17:01:13 +000031//===----------------------------------------------------------------------===//
32// Public interface to the file
33//===----------------------------------------------------------------------===//
34
Chris Lattner556beb72007-09-15 22:56:56 +000035/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000036/// the file is parsed. This inserts the parsed decls into the translation unit
37/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000038///
Ted Kremenek46157b52009-01-28 04:29:29 +000039void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000040 ASTContext &Ctx, bool PrintStats,
Douglas Gregor467dc882011-08-25 22:30:56 +000041 TranslationUnitKind TUKind,
Erik Verbruggen6a91d382012-04-12 10:11:59 +000042 CodeCompleteConsumer *CompletionConsumer,
43 bool SkipFunctionBodies) {
Ted Kremenek934a5712011-03-18 03:44:21 +000044
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000045 OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
Douglas Gregor467dc882011-08-25 22:30:56 +000046 TUKind,
Ted Kremenek934a5712011-03-18 03:44:21 +000047 CompletionConsumer));
Ted Kremenek965fe842011-03-18 02:06:53 +000048
49 // Recover resources if we crash before exiting this method.
Erik Verbruggen6a91d382012-04-12 10:11:59 +000050 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
Ted Kremenek965fe842011-03-18 02:06:53 +000051
Erik Verbruggen6a91d382012-04-12 10:11:59 +000052 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
Douglas Gregor46ea32a2010-08-12 22:51:45 +000053}
54
Erik Verbruggen6a91d382012-04-12 10:11:59 +000055void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
Chris Lattner556beb72007-09-15 22:56:56 +000056 // Collect global stats on Decls/Stmts (until we have a module streamer).
57 if (PrintStats) {
Daniel Dunbar02892a62012-03-05 21:42:49 +000058 Decl::EnableStatistics();
59 Stmt::EnableStatistics();
Chris Lattner556beb72007-09-15 22:56:56 +000060 }
Ted Kremenek46157b52009-01-28 04:29:29 +000061
Chandler Carruth5d989942011-07-06 16:21:37 +000062 // Also turn on collection of stats inside of the Sema object.
63 bool OldCollectStats = PrintStats;
64 std::swap(OldCollectStats, S.CollectStats);
65
Douglas Gregor46ea32a2010-08-12 22:51:45 +000066 ASTConsumer *Consumer = &S.getASTConsumer();
John McCalle783d002010-08-12 21:23:27 +000067
Erik Verbruggen6a91d382012-04-12 10:11:59 +000068 OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
69 SkipFunctionBodies));
Ted Kremenek614f96a2011-03-22 01:15:17 +000070 Parser &P = *ParseOP.get();
71
72 PrettyStackTraceParserEntry CrashInfo(P);
73
74 // Recover resources if we crash before exiting this method.
75 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
Erik Verbruggen6a91d382012-04-12 10:11:59 +000076 CleanupParser(ParseOP.get());
Ted Kremenek614f96a2011-03-22 01:15:17 +000077
Douglas Gregor46ea32a2010-08-12 22:51:45 +000078 S.getPreprocessor().EnterMainSourceFile();
John McCalld69fd7f2010-08-12 21:39:05 +000079 P.Initialize();
Douglas Gregor46ea32a2010-08-12 22:51:45 +000080 S.Initialize();
Jordan Rosed73ef132012-06-06 17:25:21 +000081
82 // C11 6.9p1 says translation units must have at least one top-level
83 // declaration. C++ doesn't have this restriction. We also don't want to
84 // complain if we have a precompiled header, although technically if the PCH
85 // is empty we should still emit the (pedantic) diagnostic.
86 bool WarnForEmptyTU = !S.getLangOpts().CPlusPlus;
87 if (ExternalASTSource *External = S.getASTContext().getExternalSource()) {
Douglas Gregor668c1a42009-04-21 22:25:48 +000088 External->StartTranslationUnit(Consumer);
Jordan Rosed73ef132012-06-06 17:25:21 +000089 WarnForEmptyTU = false;
90 }
91
92 // Clang's predefines contain top-level declarations for things like va_list,
93 // making it hard to tell if the /user's/ translation unit has at least one
94 // top-level declaration. So we parse cautiously, looking for a declaration
95 // that doesn't come from our predefines.
96 // Note that ParseTopLevelDecl returns 'true' at EOF.
97 SourceManager &SM = S.getSourceManager();
Chris Lattner682bf922009-03-29 16:50:03 +000098 Parser::DeclGroupPtrTy ADecl;
Jordan Rosed73ef132012-06-06 17:25:21 +000099 while (WarnForEmptyTU && !P.ParseTopLevelDecl(ADecl)) {
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000100 if (ADecl) {
Jordan Rosed73ef132012-06-06 17:25:21 +0000101 if (!Consumer->HandleTopLevelDecl(ADecl.get()))
102 return;
103 if (DeclGroupRef::iterator FirstDecl = ADecl.get().begin()) {
104 SourceLocation DeclLoc = (*FirstDecl)->getLocation();
105 WarnForEmptyTU = SM.isFromPredefines(DeclLoc);
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000106 }
107 }
Jordan Rosed73ef132012-06-06 17:25:21 +0000108 }
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000109
Jordan Rosed73ef132012-06-06 17:25:21 +0000110 // If we ended up seeing EOF before any top-level declarations, emit our
111 // diagnostic. Otherwise, parse the rest of the file normally.
112 if (WarnForEmptyTU) {
113 P.Diag(diag::ext_empty_translation_unit);
114 } else {
115 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
116 // If we got a null return and something *was* parsed, ignore it. This
117 // is due to a top-level semicolon, an action override, or a parse error
118 // skipping something.
119 if (ADecl) {
120 if (!Consumer->HandleTopLevelDecl(ADecl.get()))
121 return;
122 }
123 };
124 }
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000125
Daniel Dunbarec2a4ed2009-12-01 21:57:20 +0000126 // Process any TopLevelDecls generated by #pragma weak.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 for (SmallVector<Decl*,2>::iterator
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000128 I = S.WeakTopLevelDecls().begin(),
129 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +0000130 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000131
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000132 Consumer->HandleTranslationUnit(S.getASTContext());
Chandler Carruth5d989942011-07-06 16:21:37 +0000133
134 std::swap(OldCollectStats, S.CollectStats);
Chris Lattner556beb72007-09-15 22:56:56 +0000135 if (PrintStats) {
Chandler Carruthcd92a652011-07-04 05:32:14 +0000136 llvm::errs() << "\nSTATISTICS:\n";
Chris Lattnera0e328f2008-02-06 00:15:02 +0000137 P.getActions().PrintStats();
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000138 S.getASTContext().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000139 Decl::PrintStats();
140 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000141 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000142 }
143}