2008-02-06 00:23:21 +00:00
|
|
|
//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
|
2006-08-17 06:28:25 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:25 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-17 06:28:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-02-06 00:23:21 +00:00
|
|
|
// This file implements the clang::ParseAST method.
|
2006-08-17 06:28:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-20 18:27:03 +00:00
|
|
|
#include "clang/Parse/ParseAST.h"
|
2007-09-15 22:56:56 +00:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-07-04 17:04:04 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-04-14 00:24:19 +00:00
|
|
|
#include "clang/AST/ExternalASTSource.h"
|
2008-08-11 04:54:23 +00:00
|
|
|
#include "clang/AST/Stmt.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2006-08-17 06:28:25 +00:00
|
|
|
#include "clang/Parse/Parser.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
2018-02-10 14:04:45 +00:00
|
|
|
#include "clang/Sema/TemplateInstCallback.h"
|
2011-03-18 02:06:53 +00:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2009-08-24 13:25:12 +00:00
|
|
|
#include <cstdio>
|
2014-03-09 11:36:40 +00:00
|
|
|
#include <memory>
|
2009-08-24 13:25:12 +00:00
|
|
|
|
2018-08-14 16:56:25 +00:00
|
|
|
|
2006-08-17 06:28:25 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
2012-11-27 21:57:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-08-07 17:48:57 +00:00
|
|
|
/// Resets LLVM's pretty stack state so that stack traces are printed correctly
|
|
|
|
/// when there are nested CrashRecoveryContexts and the inner one recovers from
|
|
|
|
/// a crash.
|
|
|
|
class ResetStackCleanup
|
|
|
|
: public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
|
|
|
|
const void> {
|
|
|
|
public:
|
|
|
|
ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
|
|
|
|
: llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
|
|
|
|
Context, Top) {}
|
|
|
|
void recoverResources() override {
|
|
|
|
llvm::RestorePrettyStackState(resource);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-27 21:57:34 +00:00
|
|
|
/// If a crash happens while the parser is active, an entry is printed for it.
|
|
|
|
class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
|
|
|
|
const Parser &P;
|
|
|
|
public:
|
|
|
|
PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
|
2014-03-12 05:09:18 +00:00
|
|
|
void print(raw_ostream &OS) const override;
|
2012-11-27 21:57:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// If a crash happens while the parser is active, print out a line indicating
|
|
|
|
/// what the current token is.
|
|
|
|
void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
|
|
|
|
const Token &Tok = P.getCurToken();
|
|
|
|
if (Tok.is(tok::eof)) {
|
|
|
|
OS << "<eof> parser at end of file\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.getLocation().isInvalid()) {
|
|
|
|
OS << "<unknown> parser at unknown location\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Preprocessor &PP = P.getPreprocessor();
|
|
|
|
Tok.getLocation().print(OS, PP.getSourceManager());
|
2013-03-25 21:24:30 +00:00
|
|
|
if (Tok.isAnnotation()) {
|
|
|
|
OS << ": at annotation token\n";
|
|
|
|
} else {
|
|
|
|
// Do the equivalent of PP.getSpelling(Tok) except for the parts that would
|
|
|
|
// allocate memory.
|
|
|
|
bool Invalid = false;
|
|
|
|
const SourceManager &SM = P.getPreprocessor().getSourceManager();
|
|
|
|
unsigned Length = Tok.getLength();
|
|
|
|
const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
|
|
|
|
if (Invalid) {
|
|
|
|
OS << ": unknown current parser token\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
|
|
|
|
}
|
2012-11-27 21:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2006-08-17 06:28:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public interface to the file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-09-15 22:56:56 +00:00
|
|
|
/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
|
2009-03-28 04:13:34 +00:00
|
|
|
/// the file is parsed. This inserts the parsed decls into the translation unit
|
|
|
|
/// held by Ctx.
|
2008-10-16 16:54:18 +00:00
|
|
|
///
|
2009-01-28 04:29:29 +00:00
|
|
|
void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
|
2009-04-14 16:27:31 +00:00
|
|
|
ASTContext &Ctx, bool PrintStats,
|
2011-08-25 22:30:56 +00:00
|
|
|
TranslationUnitKind TUKind,
|
2012-04-12 10:11:59 +00:00
|
|
|
CodeCompleteConsumer *CompletionConsumer,
|
|
|
|
bool SkipFunctionBodies) {
|
2011-03-18 03:44:21 +00:00
|
|
|
|
2014-03-07 20:03:18 +00:00
|
|
|
std::unique_ptr<Sema> S(
|
|
|
|
new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
|
2011-03-18 02:06:53 +00:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2012-04-12 10:11:59 +00:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-04-12 10:11:59 +00:00
|
|
|
ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
|
2010-08-12 22:51:45 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 10:11:59 +00:00
|
|
|
void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
|
2007-09-15 22:56:56 +00:00
|
|
|
// Collect global stats on Decls/Stmts (until we have a module streamer).
|
|
|
|
if (PrintStats) {
|
2012-03-05 21:42:49 +00:00
|
|
|
Decl::EnableStatistics();
|
|
|
|
Stmt::EnableStatistics();
|
2007-09-15 22:56:56 +00:00
|
|
|
}
|
2009-01-28 04:29:29 +00:00
|
|
|
|
2011-07-06 16:21:37 +00:00
|
|
|
// Also turn on collection of stats inside of the Sema object.
|
|
|
|
bool OldCollectStats = PrintStats;
|
|
|
|
std::swap(OldCollectStats, S.CollectStats);
|
|
|
|
|
2018-02-10 14:04:45 +00:00
|
|
|
// Initialize the template instantiation observer chain.
|
|
|
|
// FIXME: See note on "finalize" below.
|
|
|
|
initialize(S.TemplateInstCallbacks, S);
|
|
|
|
|
2010-08-12 22:51:45 +00:00
|
|
|
ASTConsumer *Consumer = &S.getASTConsumer();
|
2010-08-12 21:39:05 +00:00
|
|
|
|
2014-03-07 20:03:18 +00:00
|
|
|
std::unique_ptr<Parser> ParseOP(
|
|
|
|
new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
|
2011-03-22 01:15:17 +00:00
|
|
|
Parser &P = *ParseOP.get();
|
|
|
|
|
2015-08-07 17:48:57 +00:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
|
|
|
|
CleanupPrettyStack(llvm::SavePrettyStackState());
|
2011-03-22 01:15:17 +00:00
|
|
|
PrettyStackTraceParserEntry CrashInfo(P);
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<Parser>
|
2012-04-12 10:11:59 +00:00
|
|
|
CleanupParser(ParseOP.get());
|
2011-03-22 01:15:17 +00:00
|
|
|
|
2010-08-12 22:51:45 +00:00
|
|
|
S.getPreprocessor().EnterMainSourceFile();
|
2018-07-05 17:22:13 +00:00
|
|
|
if (!S.getPreprocessor().getCurrentLexer()) {
|
|
|
|
// If a PCH through header is specified that does not have an include in
|
|
|
|
// the source, there won't be any tokens or a Lexer.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-12 21:39:05 +00:00
|
|
|
P.Initialize();
|
2012-06-06 17:25:21 +00:00
|
|
|
|
2009-03-29 16:50:03 +00:00
|
|
|
Parser::DeclGroupPtrTy ADecl;
|
2012-06-19 18:17:30 +00:00
|
|
|
ExternalASTSource *External = S.getASTContext().getExternalSource();
|
|
|
|
if (External)
|
|
|
|
External->StartTranslationUnit(Consumer);
|
2011-11-18 00:26:59 +00:00
|
|
|
|
2016-08-26 00:14:38 +00:00
|
|
|
for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF;
|
|
|
|
AtEOF = P.ParseTopLevelDecl(ADecl)) {
|
|
|
|
// If we got a null return and something *was* parsed, ignore it. This
|
|
|
|
// is due to a top-level semicolon, an action override, or a parse error
|
|
|
|
// skipping something.
|
|
|
|
if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
|
|
|
|
return;
|
2012-06-06 17:25:21 +00:00
|
|
|
}
|
2012-06-19 18:17:30 +00:00
|
|
|
|
2009-12-01 21:57:20 +00:00
|
|
|
// Process any TopLevelDecls generated by #pragma weak.
|
2014-12-17 16:56:54 +00:00
|
|
|
for (Decl *D : S.WeakTopLevelDecls())
|
|
|
|
Consumer->HandleTopLevelDecl(DeclGroupRef(D));
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-08-12 22:51:45 +00:00
|
|
|
Consumer->HandleTranslationUnit(S.getASTContext());
|
2011-07-06 16:21:37 +00:00
|
|
|
|
2018-02-10 14:04:45 +00:00
|
|
|
// Finalize the template instantiation observer chain.
|
|
|
|
// FIXME: This (and init.) should be done in the Sema class, but because
|
|
|
|
// Sema does not have a reliable "Finalize" function (it has a
|
|
|
|
// destructor, but it is not guaranteed to be called ("-disable-free")).
|
|
|
|
// So, do the initialization above and do the finalization here:
|
|
|
|
finalize(S.TemplateInstCallbacks, S);
|
|
|
|
|
2011-07-06 16:21:37 +00:00
|
|
|
std::swap(OldCollectStats, S.CollectStats);
|
2007-09-15 22:56:56 +00:00
|
|
|
if (PrintStats) {
|
2011-07-04 05:32:14 +00:00
|
|
|
llvm::errs() << "\nSTATISTICS:\n";
|
2008-02-06 00:15:02 +00:00
|
|
|
P.getActions().PrintStats();
|
2010-08-12 22:51:45 +00:00
|
|
|
S.getASTContext().PrintStats();
|
2007-09-15 22:56:56 +00:00
|
|
|
Decl::PrintStats();
|
|
|
|
Stmt::PrintStats();
|
2007-11-03 06:24:16 +00:00
|
|
|
Consumer->PrintStats();
|
2007-09-15 22:56:56 +00:00
|
|
|
}
|
|
|
|
}
|