2008-02-06 08:23:21 +08:00
|
|
|
//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
|
2006-08-17 14:28:25 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2006-08-17 14:28:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-02-06 08:23:21 +08:00
|
|
|
// This file implements the clang::ParseAST method.
|
2006-08-17 14:28:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Parse/ParseAST.h"
|
2007-09-16 06:56:56 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-07-05 01:04:04 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-04-14 08:24:19 +08:00
|
|
|
#include "clang/AST/ExternalASTSource.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2006-08-17 14:28:25 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
2018-02-10 22:04:45 +08:00
|
|
|
#include "clang/Sema/TemplateInstCallback.h"
|
2011-03-18 10:06:53 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2019-03-30 16:42:48 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2009-08-24 21:25:12 +08:00
|
|
|
#include <cstdio>
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2009-08-24 21:25:12 +08:00
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2012-11-28 05:57:34 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-08-08 01:48:57 +08: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-28 05:57:34 +08: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 13:09:18 +08:00
|
|
|
void print(raw_ostream &OS) const override;
|
2012-11-28 05:57:34 +08: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-26 05:24:30 +08: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-28 05:57:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public interface to the file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-09-16 06:56:56 +08:00
|
|
|
/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
|
2009-03-28 12:13:34 +08:00
|
|
|
/// the file is parsed. This inserts the parsed decls into the translation unit
|
|
|
|
/// held by Ctx.
|
2008-10-17 00:54:18 +08:00
|
|
|
///
|
2009-01-28 12:29:29 +08:00
|
|
|
void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
|
2009-04-15 00:27:31 +08:00
|
|
|
ASTContext &Ctx, bool PrintStats,
|
2011-08-26 06:30:56 +08:00
|
|
|
TranslationUnitKind TUKind,
|
2012-04-12 18:11:59 +08:00
|
|
|
CodeCompleteConsumer *CompletionConsumer,
|
|
|
|
bool SkipFunctionBodies) {
|
2011-03-18 11:44:21 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<Sema> S(
|
|
|
|
new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
|
2011-03-18 10:06:53 +08:00
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
2012-04-12 18:11:59 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-04-12 18:11:59 +08:00
|
|
|
ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
|
2010-08-13 06:51:45 +08:00
|
|
|
}
|
|
|
|
|
2012-04-12 18:11:59 +08:00
|
|
|
void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
|
2007-09-16 06:56:56 +08:00
|
|
|
// Collect global stats on Decls/Stmts (until we have a module streamer).
|
|
|
|
if (PrintStats) {
|
2012-03-06 05:42:49 +08:00
|
|
|
Decl::EnableStatistics();
|
|
|
|
Stmt::EnableStatistics();
|
2007-09-16 06:56:56 +08:00
|
|
|
}
|
2009-01-28 12:29:29 +08:00
|
|
|
|
2011-07-07 00:21:37 +08:00
|
|
|
// Also turn on collection of stats inside of the Sema object.
|
|
|
|
bool OldCollectStats = PrintStats;
|
|
|
|
std::swap(OldCollectStats, S.CollectStats);
|
|
|
|
|
2018-02-10 22:04:45 +08:00
|
|
|
// Initialize the template instantiation observer chain.
|
|
|
|
// FIXME: See note on "finalize" below.
|
|
|
|
initialize(S.TemplateInstCallbacks, S);
|
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
ASTConsumer *Consumer = &S.getASTConsumer();
|
2010-08-13 05:39:05 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<Parser> ParseOP(
|
|
|
|
new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
|
2011-03-22 09:15:17 +08:00
|
|
|
Parser &P = *ParseOP.get();
|
|
|
|
|
2015-08-08 01:48:57 +08:00
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
|
|
|
|
CleanupPrettyStack(llvm::SavePrettyStackState());
|
2011-03-22 09:15:17 +08:00
|
|
|
PrettyStackTraceParserEntry CrashInfo(P);
|
|
|
|
|
|
|
|
// Recover resources if we crash before exiting this method.
|
|
|
|
llvm::CrashRecoveryContextCleanupRegistrar<Parser>
|
2012-04-12 18:11:59 +08:00
|
|
|
CleanupParser(ParseOP.get());
|
2011-03-22 09:15:17 +08:00
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
S.getPreprocessor().EnterMainSourceFile();
|
2012-06-20 02:17:30 +08:00
|
|
|
ExternalASTSource *External = S.getASTContext().getExternalSource();
|
|
|
|
if (External)
|
|
|
|
External->StartTranslationUnit(Consumer);
|
2011-11-18 08:26:59 +08:00
|
|
|
|
2018-09-12 01:10:44 +08:00
|
|
|
// If a PCH through header is specified that does not have an include in
|
|
|
|
// the source, or a PCH is being created with #pragma hdrstop with nothing
|
|
|
|
// after the pragma, there won't be any tokens or a Lexer.
|
|
|
|
bool HaveLexer = S.getPreprocessor().getCurrentLexer();
|
|
|
|
|
|
|
|
if (HaveLexer) {
|
2019-03-30 16:42:48 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("Frontend", StringRef(""));
|
2018-09-12 01:10:44 +08:00
|
|
|
P.Initialize();
|
|
|
|
Parser::DeclGroupPtrTy ADecl;
|
|
|
|
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-07 01:25:21 +08:00
|
|
|
}
|
2012-06-20 02:17:30 +08:00
|
|
|
|
2009-12-02 05:57:20 +08:00
|
|
|
// Process any TopLevelDecls generated by #pragma weak.
|
2014-12-18 00:56:54 +08:00
|
|
|
for (Decl *D : S.WeakTopLevelDecls())
|
|
|
|
Consumer->HandleTopLevelDecl(DeclGroupRef(D));
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-08-13 06:51:45 +08:00
|
|
|
Consumer->HandleTranslationUnit(S.getASTContext());
|
2011-07-07 00:21:37 +08:00
|
|
|
|
2018-02-10 22:04:45 +08: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-07 00:21:37 +08:00
|
|
|
std::swap(OldCollectStats, S.CollectStats);
|
2007-09-16 06:56:56 +08:00
|
|
|
if (PrintStats) {
|
2011-07-04 13:32:14 +08:00
|
|
|
llvm::errs() << "\nSTATISTICS:\n";
|
2018-09-12 01:10:44 +08:00
|
|
|
if (HaveLexer) P.getActions().PrintStats();
|
2010-08-13 06:51:45 +08:00
|
|
|
S.getASTContext().PrintStats();
|
2007-09-16 06:56:56 +08:00
|
|
|
Decl::PrintStats();
|
|
|
|
Stmt::PrintStats();
|
2007-11-03 14:24:16 +08:00
|
|
|
Consumer->PrintStats();
|
2007-09-16 06:56:56 +08:00
|
|
|
}
|
|
|
|
}
|