blob: f7703b1bfd8a048551be479b509725c03d7b2461 [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"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000015#include "clang/AST/ASTConsumer.h"
Benjamin Kramer1ea8e092012-07-04 17:04:04 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000017#include "clang/AST/ExternalASTSource.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/AST/Stmt.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000020#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/CodeCompleteConsumer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Sema.h"
23#include "clang/Sema/SemaConsumer.h"
Gabor Horvath207e7b12018-02-10 14:04:45 +000024#include "clang/Sema/TemplateInstCallback.h"
Ted Kremenek750028b2011-03-18 02:06:53 +000025#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwindb714922009-08-24 13:25:12 +000026#include <cstdio>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000027#include <memory>
Torok Edwindb714922009-08-24 13:25:12 +000028
Chris Lattner73709ed2006-08-17 06:28:25 +000029using namespace clang;
30
Nico Weberd0b91de2012-11-27 21:57:34 +000031namespace {
32
Nico Weber63627452015-08-07 17:48:57 +000033/// Resets LLVM's pretty stack state so that stack traces are printed correctly
34/// when there are nested CrashRecoveryContexts and the inner one recovers from
35/// a crash.
36class ResetStackCleanup
37 : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
38 const void> {
39public:
40 ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
41 : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
42 Context, Top) {}
43 void recoverResources() override {
44 llvm::RestorePrettyStackState(resource);
45 }
46};
47
Nico Weberd0b91de2012-11-27 21:57:34 +000048/// If a crash happens while the parser is active, an entry is printed for it.
49class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
50 const Parser &P;
51public:
52 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
Craig Topper2b07f022014-03-12 05:09:18 +000053 void print(raw_ostream &OS) const override;
Nico Weberd0b91de2012-11-27 21:57:34 +000054};
55
56/// If a crash happens while the parser is active, print out a line indicating
57/// what the current token is.
58void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
59 const Token &Tok = P.getCurToken();
60 if (Tok.is(tok::eof)) {
61 OS << "<eof> parser at end of file\n";
62 return;
63 }
64
65 if (Tok.getLocation().isInvalid()) {
66 OS << "<unknown> parser at unknown location\n";
67 return;
68 }
69
70 const Preprocessor &PP = P.getPreprocessor();
71 Tok.getLocation().print(OS, PP.getSourceManager());
Nick Lewycky966b1992013-03-25 21:24:30 +000072 if (Tok.isAnnotation()) {
73 OS << ": at annotation token\n";
74 } else {
75 // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
76 // allocate memory.
77 bool Invalid = false;
78 const SourceManager &SM = P.getPreprocessor().getSourceManager();
79 unsigned Length = Tok.getLength();
80 const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
81 if (Invalid) {
82 OS << ": unknown current parser token\n";
83 return;
84 }
85 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
86 }
Nico Weberd0b91de2012-11-27 21:57:34 +000087}
88
89} // namespace
90
Chris Lattner73709ed2006-08-17 06:28:25 +000091//===----------------------------------------------------------------------===//
92// Public interface to the file
93//===----------------------------------------------------------------------===//
94
Chris Lattner75e0c8c2007-09-15 22:56:56 +000095/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner66918ee2009-03-28 04:13:34 +000096/// the file is parsed. This inserts the parsed decls into the translation unit
97/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000098///
Ted Kremenek062115a2009-01-28 04:29:29 +000099void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregor54feb842009-04-14 16:27:31 +0000100 ASTContext &Ctx, bool PrintStats,
Douglas Gregor69f74f82011-08-25 22:30:56 +0000101 TranslationUnitKind TUKind,
Erik Verbruggen6e922512012-04-12 10:11:59 +0000102 CodeCompleteConsumer *CompletionConsumer,
103 bool SkipFunctionBodies) {
Ted Kremenekc93cf2e2011-03-18 03:44:21 +0000104
Ahmed Charlesb8984322014-03-07 20:03:18 +0000105 std::unique_ptr<Sema> S(
106 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
Ted Kremenek750028b2011-03-18 02:06:53 +0000107
108 // Recover resources if we crash before exiting this method.
Erik Verbruggen6e922512012-04-12 10:11:59 +0000109 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
Fangrui Song6907ce22018-07-30 19:24:48 +0000110
Erik Verbruggen6e922512012-04-12 10:11:59 +0000111 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000112}
113
Erik Verbruggen6e922512012-04-12 10:11:59 +0000114void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000115 // Collect global stats on Decls/Stmts (until we have a module streamer).
116 if (PrintStats) {
Daniel Dunbar62905572012-03-05 21:42:49 +0000117 Decl::EnableStatistics();
118 Stmt::EnableStatistics();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000119 }
Ted Kremenek062115a2009-01-28 04:29:29 +0000120
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000121 // Also turn on collection of stats inside of the Sema object.
122 bool OldCollectStats = PrintStats;
123 std::swap(OldCollectStats, S.CollectStats);
124
Gabor Horvath207e7b12018-02-10 14:04:45 +0000125 // Initialize the template instantiation observer chain.
126 // FIXME: See note on "finalize" below.
127 initialize(S.TemplateInstCallbacks, S);
128
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000129 ASTConsumer *Consumer = &S.getASTConsumer();
John McCall0af2b7c2010-08-12 21:23:27 +0000130
Ahmed Charlesb8984322014-03-07 20:03:18 +0000131 std::unique_ptr<Parser> ParseOP(
132 new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
Ted Kremenek4c9d46b2011-03-22 01:15:17 +0000133 Parser &P = *ParseOP.get();
134
Nico Weber63627452015-08-07 17:48:57 +0000135 llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
136 CleanupPrettyStack(llvm::SavePrettyStackState());
Ted Kremenek4c9d46b2011-03-22 01:15:17 +0000137 PrettyStackTraceParserEntry CrashInfo(P);
138
139 // Recover resources if we crash before exiting this method.
140 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
Erik Verbruggen6e922512012-04-12 10:11:59 +0000141 CleanupParser(ParseOP.get());
Ted Kremenek4c9d46b2011-03-22 01:15:17 +0000142
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000143 S.getPreprocessor().EnterMainSourceFile();
Meador Ingecdc00572012-06-19 18:17:30 +0000144 ExternalASTSource *External = S.getASTContext().getExternalSource();
145 if (External)
146 External->StartTranslationUnit(Consumer);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000147
Mike Rice58df1af2018-09-11 17:10:44 +0000148 // If a PCH through header is specified that does not have an include in
149 // the source, or a PCH is being created with #pragma hdrstop with nothing
150 // after the pragma, there won't be any tokens or a Lexer.
151 bool HaveLexer = S.getPreprocessor().getCurrentLexer();
152
153 if (HaveLexer) {
154 P.Initialize();
155 Parser::DeclGroupPtrTy ADecl;
156 for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF;
157 AtEOF = P.ParseTopLevelDecl(ADecl)) {
158 // If we got a null return and something *was* parsed, ignore it. This
159 // is due to a top-level semicolon, an action override, or a parse error
160 // skipping something.
161 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
162 return;
163 }
Jordan Roseccf43ca2012-06-06 17:25:21 +0000164 }
Meador Ingecdc00572012-06-19 18:17:30 +0000165
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +0000166 // Process any TopLevelDecls generated by #pragma weak.
Yaron Kerenc6328f12014-12-17 16:56:54 +0000167 for (Decl *D : S.WeakTopLevelDecls())
168 Consumer->HandleTopLevelDecl(DeclGroupRef(D));
Fangrui Song6907ce22018-07-30 19:24:48 +0000169
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000170 Consumer->HandleTranslationUnit(S.getASTContext());
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000171
Gabor Horvath207e7b12018-02-10 14:04:45 +0000172 // Finalize the template instantiation observer chain.
173 // FIXME: This (and init.) should be done in the Sema class, but because
174 // Sema does not have a reliable "Finalize" function (it has a
175 // destructor, but it is not guaranteed to be called ("-disable-free")).
176 // So, do the initialization above and do the finalization here:
177 finalize(S.TemplateInstCallbacks, S);
178
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000179 std::swap(OldCollectStats, S.CollectStats);
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000180 if (PrintStats) {
Chandler Carruth3c147a72011-07-04 05:32:14 +0000181 llvm::errs() << "\nSTATISTICS:\n";
Mike Rice58df1af2018-09-11 17:10:44 +0000182 if (HaveLexer) P.getActions().PrintStats();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000183 S.getASTContext().PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000184 Decl::PrintStats();
185 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +0000186 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000187 }
188}