blob: 0f5a1b3ffbd6cf0b26d922c06727008bd2e668ad [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"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000018#include "clang/AST/ExternalASTSource.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000019#include "clang/AST/Stmt.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000021#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-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 Kremenek750028b2011-03-18 02:06:53 +000026#include "llvm/Support/CrashRecoveryContext.h"
Torok Edwindb714922009-08-24 13:25:12 +000027#include <cstdio>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000028#include <memory>
Torok Edwindb714922009-08-24 13:25:12 +000029
Chris Lattner73709ed2006-08-17 06:28:25 +000030using namespace clang;
31
Nico Weberd0b91de2012-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) {}
Craig Topper2b07f022014-03-12 05:09:18 +000039 void print(raw_ostream &OS) const override;
Nico Weberd0b91de2012-11-27 21:57:34 +000040};
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 Lewycky966b1992013-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 Weberd0b91de2012-11-27 21:57:34 +000073}
74
75} // namespace
76
Chris Lattner73709ed2006-08-17 06:28:25 +000077//===----------------------------------------------------------------------===//
78// Public interface to the file
79//===----------------------------------------------------------------------===//
80
Chris Lattner75e0c8c2007-09-15 22:56:56 +000081/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner66918ee2009-03-28 04:13:34 +000082/// the file is parsed. This inserts the parsed decls into the translation unit
83/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000084///
Ted Kremenek062115a2009-01-28 04:29:29 +000085void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregor54feb842009-04-14 16:27:31 +000086 ASTContext &Ctx, bool PrintStats,
Douglas Gregor69f74f82011-08-25 22:30:56 +000087 TranslationUnitKind TUKind,
Erik Verbruggen6e922512012-04-12 10:11:59 +000088 CodeCompleteConsumer *CompletionConsumer,
89 bool SkipFunctionBodies) {
Ted Kremenekc93cf2e2011-03-18 03:44:21 +000090
Ahmed Charlesb8984322014-03-07 20:03:18 +000091 std::unique_ptr<Sema> S(
92 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
Ted Kremenek750028b2011-03-18 02:06:53 +000093
94 // Recover resources if we crash before exiting this method.
Erik Verbruggen6e922512012-04-12 10:11:59 +000095 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
Ted Kremenek750028b2011-03-18 02:06:53 +000096
Erik Verbruggen6e922512012-04-12 10:11:59 +000097 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000098}
99
Erik Verbruggen6e922512012-04-12 10:11:59 +0000100void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000101 // Collect global stats on Decls/Stmts (until we have a module streamer).
102 if (PrintStats) {
Daniel Dunbar62905572012-03-05 21:42:49 +0000103 Decl::EnableStatistics();
104 Stmt::EnableStatistics();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000105 }
Ted Kremenek062115a2009-01-28 04:29:29 +0000106
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000107 // Also turn on collection of stats inside of the Sema object.
108 bool OldCollectStats = PrintStats;
109 std::swap(OldCollectStats, S.CollectStats);
110
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000111 ASTConsumer *Consumer = &S.getASTConsumer();
John McCall0af2b7c2010-08-12 21:23:27 +0000112
Ahmed Charlesb8984322014-03-07 20:03:18 +0000113 std::unique_ptr<Parser> ParseOP(
114 new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
Ted Kremenek4c9d46b2011-03-22 01:15:17 +0000115 Parser &P = *ParseOP.get();
116
117 PrettyStackTraceParserEntry CrashInfo(P);
118
119 // Recover resources if we crash before exiting this method.
120 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
Erik Verbruggen6e922512012-04-12 10:11:59 +0000121 CleanupParser(ParseOP.get());
Ted Kremenek4c9d46b2011-03-22 01:15:17 +0000122
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000123 S.getPreprocessor().EnterMainSourceFile();
John McCall112fd082010-08-12 21:39:05 +0000124 P.Initialize();
Jordan Roseccf43ca2012-06-06 17:25:21 +0000125
126 // C11 6.9p1 says translation units must have at least one top-level
127 // declaration. C++ doesn't have this restriction. We also don't want to
128 // complain if we have a precompiled header, although technically if the PCH
129 // is empty we should still emit the (pedantic) diagnostic.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000130 Parser::DeclGroupPtrTy ADecl;
Meador Ingecdc00572012-06-19 18:17:30 +0000131 ExternalASTSource *External = S.getASTContext().getExternalSource();
132 if (External)
133 External->StartTranslationUnit(Consumer);
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000134
Meador Ingecdc00572012-06-19 18:17:30 +0000135 if (P.ParseTopLevelDecl(ADecl)) {
136 if (!External && !S.getLangOpts().CPlusPlus)
137 P.Diag(diag::ext_empty_translation_unit);
Jordan Roseccf43ca2012-06-06 17:25:21 +0000138 } else {
Meador Ingecdc00572012-06-19 18:17:30 +0000139 do {
Jordan Roseccf43ca2012-06-06 17:25:21 +0000140 // If we got a null return and something *was* parsed, ignore it. This
141 // is due to a top-level semicolon, an action override, or a parse error
142 // skipping something.
Meador Ingecdc00572012-06-19 18:17:30 +0000143 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
Chad Rosier6af2d2e2012-12-21 19:43:33 +0000144 return;
Meador Ingecdc00572012-06-19 18:17:30 +0000145 } while (!P.ParseTopLevelDecl(ADecl));
Jordan Roseccf43ca2012-06-06 17:25:21 +0000146 }
Meador Ingecdc00572012-06-19 18:17:30 +0000147
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +0000148 // Process any TopLevelDecls generated by #pragma weak.
Craig Topper2341c0d2013-07-04 03:08:24 +0000149 for (SmallVectorImpl<Decl *>::iterator
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000150 I = S.WeakTopLevelDecls().begin(),
151 E = S.WeakTopLevelDecls().end(); I != E; ++I)
Ryan Flynnd963a492009-07-31 02:52:19 +0000152 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000153
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000154 Consumer->HandleTranslationUnit(S.getASTContext());
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000155
156 std::swap(OldCollectStats, S.CollectStats);
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000157 if (PrintStats) {
Chandler Carruth3c147a72011-07-04 05:32:14 +0000158 llvm::errs() << "\nSTATISTICS:\n";
Chris Lattner4afeded2008-02-06 00:15:02 +0000159 P.getActions().PrintStats();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000160 S.getASTContext().PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000161 Decl::PrintStats();
162 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +0000163 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000164 }
165}