blob: 5678ece0c822ccc02ce95daaece8b0f48a319703 [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"
Chris Lattner556beb72007-09-15 22:56:56 +000015#include "clang/AST/ASTConsumer.h"
Benjamin Kramer478851c2012-07-04 17:04:04 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000018#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/CodeCompleteConsumer.h"
23#include "clang/Sema/ExternalSemaSource.h"
24#include "clang/Sema/Sema.h"
25#include "clang/Sema/SemaConsumer.h"
Ted Kremenek934a5712011-03-18 03:44:21 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremenek965fe842011-03-18 02:06:53 +000027#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000028#include <cstdio>
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Nico Weberf88836d2012-11-27 21:57:34 +000032namespace {
33
34/// If a crash happens while the parser is active, an entry is printed for it.
35class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
36 const Parser &P;
37public:
38 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
39 virtual void print(raw_ostream &OS) const;
40};
41
42/// If a crash happens while the parser is active, print out a line indicating
43/// what the current token is.
44void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
45 const Token &Tok = P.getCurToken();
46 if (Tok.is(tok::eof)) {
47 OS << "<eof> parser at end of file\n";
48 return;
49 }
50
51 if (Tok.getLocation().isInvalid()) {
52 OS << "<unknown> parser at unknown location\n";
53 return;
54 }
55
56 const Preprocessor &PP = P.getPreprocessor();
57 Tok.getLocation().print(OS, PP.getSourceManager());
Nick Lewyckycdd81e52013-03-25 21:24:30 +000058 if (Tok.isAnnotation()) {
59 OS << ": at annotation token\n";
60 } else {
61 // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
62 // allocate memory.
63 bool Invalid = false;
64 const SourceManager &SM = P.getPreprocessor().getSourceManager();
65 unsigned Length = Tok.getLength();
66 const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
67 if (Invalid) {
68 OS << ": unknown current parser token\n";
69 return;
70 }
71 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
72 }
Nico Weberf88836d2012-11-27 21:57:34 +000073}
74
75} // namespace
76
Reid Spencer5f016e22007-07-11 17:01:13 +000077//===----------------------------------------------------------------------===//
78// Public interface to the file
79//===----------------------------------------------------------------------===//
80
Chris Lattner556beb72007-09-15 22:56:56 +000081/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000082/// the file is parsed. This inserts the parsed decls into the translation unit
83/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000084///
Ted Kremenek46157b52009-01-28 04:29:29 +000085void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000086 ASTContext &Ctx, bool PrintStats,
Douglas Gregor467dc882011-08-25 22:30:56 +000087 TranslationUnitKind TUKind,
Erik Verbruggen6a91d382012-04-12 10:11:59 +000088 CodeCompleteConsumer *CompletionConsumer,
89 bool SkipFunctionBodies) {
Ted Kremenek934a5712011-03-18 03:44:21 +000090
Nico Weberf88836d2012-11-27 21:57:34 +000091 OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
Ted Kremenek965fe842011-03-18 02:06:53 +000092
93 // Recover resources if we crash before exiting this method.
Erik Verbruggen6a91d382012-04-12 10:11:59 +000094 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
Ted Kremenek965fe842011-03-18 02:06:53 +000095
Erik Verbruggen6a91d382012-04-12 10:11:59 +000096 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
Douglas Gregor46ea32a2010-08-12 22:51:45 +000097}
98
Erik Verbruggen6a91d382012-04-12 10:11:59 +000099void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
Chris Lattner556beb72007-09-15 22:56:56 +0000100 // Collect global stats on Decls/Stmts (until we have a module streamer).
101 if (PrintStats) {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000102 Decl::EnableStatistics();
103 Stmt::EnableStatistics();
Chris Lattner556beb72007-09-15 22:56:56 +0000104 }
Ted Kremenek46157b52009-01-28 04:29:29 +0000105
Chandler Carruth5d989942011-07-06 16:21:37 +0000106 // Also turn on collection of stats inside of the Sema object.
107 bool OldCollectStats = PrintStats;
108 std::swap(OldCollectStats, S.CollectStats);
109
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000110 ASTConsumer *Consumer = &S.getASTConsumer();
John McCalle783d002010-08-12 21:23:27 +0000111
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000112 OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
113 SkipFunctionBodies));
Ted Kremenek614f96a2011-03-22 01:15:17 +0000114 Parser &P = *ParseOP.get();
115
116 PrettyStackTraceParserEntry CrashInfo(P);
117
118 // Recover resources if we crash before exiting this method.
119 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000120 CleanupParser(ParseOP.get());
Ted Kremenek614f96a2011-03-22 01:15:17 +0000121
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000122 S.getPreprocessor().EnterMainSourceFile();
John McCalld69fd7f2010-08-12 21:39:05 +0000123 P.Initialize();
Jordan Rosed73ef132012-06-06 17:25:21 +0000124
125 // C11 6.9p1 says translation units must have at least one top-level
126 // declaration. C++ doesn't have this restriction. We also don't want to
127 // complain if we have a precompiled header, although technically if the PCH
128 // is empty we should still emit the (pedantic) diagnostic.
Chris Lattner682bf922009-03-29 16:50:03 +0000129 Parser::DeclGroupPtrTy ADecl;
Meador Inge9416d422012-06-19 18:17:30 +0000130 ExternalASTSource *External = S.getASTContext().getExternalSource();
131 if (External)
132 External->StartTranslationUnit(Consumer);
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000133
Meador Inge9416d422012-06-19 18:17:30 +0000134 if (P.ParseTopLevelDecl(ADecl)) {
135 if (!External && !S.getLangOpts().CPlusPlus)
136 P.Diag(diag::ext_empty_translation_unit);
Jordan Rosed73ef132012-06-06 17:25:21 +0000137 } else {
Meador Inge9416d422012-06-19 18:17:30 +0000138 do {
Jordan Rosed73ef132012-06-06 17:25:21 +0000139 // If we got a null return and something *was* parsed, ignore it. This
140 // is due to a top-level semicolon, an action override, or a parse error
141 // skipping something.
Meador Inge9416d422012-06-19 18:17:30 +0000142 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
Chad Rosiereb4c01e2012-12-21 19:43:33 +0000143 return;
Meador Inge9416d422012-06-19 18:17:30 +0000144 } while (!P.ParseTopLevelDecl(ADecl));
Jordan Rosed73ef132012-06-06 17:25:21 +0000145 }
Meador Inge9416d422012-06-19 18:17:30 +0000146
Daniel Dunbarec2a4ed2009-12-01 21:57:20 +0000147 // Process any TopLevelDecls generated by #pragma weak.
Craig Topper09d19ef2013-07-04 03:08:24 +0000148 for (SmallVectorImpl<Decl *>::iterator
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000149 I = S.WeakTopLevelDecls().begin(),
150 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +0000151 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000152
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000153 Consumer->HandleTranslationUnit(S.getASTContext());
Chandler Carruth5d989942011-07-06 16:21:37 +0000154
155 std::swap(OldCollectStats, S.CollectStats);
Chris Lattner556beb72007-09-15 22:56:56 +0000156 if (PrintStats) {
Chandler Carruthcd92a652011-07-04 05:32:14 +0000157 llvm::errs() << "\nSTATISTICS:\n";
Chris Lattnera0e328f2008-02-06 00:15:02 +0000158 P.getActions().PrintStats();
Douglas Gregor46ea32a2010-08-12 22:51:45 +0000159 S.getASTContext().PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000160 Decl::PrintStats();
161 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000162 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000163 }
164}