Path: blob/main/contrib/llvm-project/clang/lib/Parse/ParseAST.cpp
35234 views
//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the clang::ParseAST method.9//10//===----------------------------------------------------------------------===//1112#include "clang/Parse/ParseAST.h"13#include "clang/AST/ASTConsumer.h"14#include "clang/AST/ASTContext.h"15#include "clang/AST/ExternalASTSource.h"16#include "clang/AST/Stmt.h"17#include "clang/Parse/ParseDiagnostic.h"18#include "clang/Parse/Parser.h"19#include "clang/Sema/CodeCompleteConsumer.h"20#include "clang/Sema/EnterExpressionEvaluationContext.h"21#include "clang/Sema/Sema.h"22#include "clang/Sema/SemaConsumer.h"23#include "clang/Sema/TemplateInstCallback.h"24#include "llvm/Support/CrashRecoveryContext.h"25#include "llvm/Support/TimeProfiler.h"26#include <cstdio>27#include <memory>2829using namespace clang;3031namespace {3233/// Resets LLVM's pretty stack state so that stack traces are printed correctly34/// when there are nested CrashRecoveryContexts and the inner one recovers from35/// a crash.36class ResetStackCleanup37: public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,38const void> {39public:40ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)41: llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(42Context, Top) {}43void recoverResources() override {44llvm::RestorePrettyStackState(resource);45}46};4748/// If a crash happens while the parser is active, an entry is printed for it.49class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {50const Parser &P;51public:52PrettyStackTraceParserEntry(const Parser &p) : P(p) {}53void print(raw_ostream &OS) const override;54};5556/// If a crash happens while the parser is active, print out a line indicating57/// what the current token is.58void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {59const Token &Tok = P.getCurToken();60if (Tok.is(tok::eof)) {61OS << "<eof> parser at end of file\n";62return;63}6465if (Tok.getLocation().isInvalid()) {66OS << "<unknown> parser at unknown location\n";67return;68}6970const Preprocessor &PP = P.getPreprocessor();71Tok.getLocation().print(OS, PP.getSourceManager());72if (Tok.isAnnotation()) {73OS << ": at annotation token\n";74} else {75// Do the equivalent of PP.getSpelling(Tok) except for the parts that would76// allocate memory.77bool Invalid = false;78const SourceManager &SM = P.getPreprocessor().getSourceManager();79unsigned Length = Tok.getLength();80const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);81if (Invalid) {82OS << ": unknown current parser token\n";83return;84}85OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";86}87}8889} // namespace9091//===----------------------------------------------------------------------===//92// Public interface to the file93//===----------------------------------------------------------------------===//9495/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as96/// the file is parsed. This inserts the parsed decls into the translation unit97/// held by Ctx.98///99void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,100ASTContext &Ctx, bool PrintStats,101TranslationUnitKind TUKind,102CodeCompleteConsumer *CompletionConsumer,103bool SkipFunctionBodies) {104105std::unique_ptr<Sema> S(106new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));107108// Recover resources if we crash before exiting this method.109llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());110111ParseAST(*S.get(), PrintStats, SkipFunctionBodies);112}113114void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {115// Collect global stats on Decls/Stmts (until we have a module streamer).116if (PrintStats) {117Decl::EnableStatistics();118Stmt::EnableStatistics();119}120121// Also turn on collection of stats inside of the Sema object.122bool OldCollectStats = PrintStats;123std::swap(OldCollectStats, S.CollectStats);124125// Initialize the template instantiation observer chain.126// FIXME: See note on "finalize" below.127initialize(S.TemplateInstCallbacks, S);128129ASTConsumer *Consumer = &S.getASTConsumer();130131std::unique_ptr<Parser> ParseOP(132new Parser(S.getPreprocessor(), S, SkipFunctionBodies));133Parser &P = *ParseOP.get();134135llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>136CleanupPrettyStack(llvm::SavePrettyStackState());137PrettyStackTraceParserEntry CrashInfo(P);138139// Recover resources if we crash before exiting this method.140llvm::CrashRecoveryContextCleanupRegistrar<Parser>141CleanupParser(ParseOP.get());142143S.getPreprocessor().EnterMainSourceFile();144ExternalASTSource *External = S.getASTContext().getExternalSource();145if (External)146External->StartTranslationUnit(Consumer);147148// If a PCH through header is specified that does not have an include in149// the source, or a PCH is being created with #pragma hdrstop with nothing150// after the pragma, there won't be any tokens or a Lexer.151bool HaveLexer = S.getPreprocessor().getCurrentLexer();152153if (HaveLexer) {154llvm::TimeTraceScope TimeScope("Frontend", [&]() {155llvm::TimeTraceMetadata M;156if (llvm::isTimeTraceVerbose()) {157const SourceManager &SM = S.getSourceManager();158if (const auto *FE = SM.getFileEntryForID(SM.getMainFileID()))159M.File = FE->tryGetRealPathName();160}161return M;162});163P.Initialize();164Parser::DeclGroupPtrTy ADecl;165Sema::ModuleImportState ImportState;166EnterExpressionEvaluationContext PotentiallyEvaluated(167S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);168169for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;170AtEOF = P.ParseTopLevelDecl(ADecl, ImportState)) {171// If we got a null return and something *was* parsed, ignore it. This172// is due to a top-level semicolon, an action override, or a parse error173// skipping something.174if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))175return;176}177}178179// Process any TopLevelDecls generated by #pragma weak.180for (Decl *D : S.WeakTopLevelDecls())181Consumer->HandleTopLevelDecl(DeclGroupRef(D));182183Consumer->HandleTranslationUnit(S.getASTContext());184185// Finalize the template instantiation observer chain.186// FIXME: This (and init.) should be done in the Sema class, but because187// Sema does not have a reliable "Finalize" function (it has a188// destructor, but it is not guaranteed to be called ("-disable-free")).189// So, do the initialization above and do the finalization here:190finalize(S.TemplateInstCallbacks, S);191192std::swap(OldCollectStats, S.CollectStats);193if (PrintStats) {194llvm::errs() << "\nSTATISTICS:\n";195if (HaveLexer) P.getActions().PrintStats();196S.getASTContext().PrintStats();197Decl::PrintStats();198Stmt::PrintStats();199Consumer->PrintStats();200}201}202203204