clang 19.0.0git
ParseAST.cpp
Go to the documentation of this file.
1//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the clang::ParseAST method.
10//
11//===----------------------------------------------------------------------===//
12
17#include "clang/AST/Stmt.h"
19#include "clang/Parse/Parser.h"
22#include "clang/Sema/Sema.h"
25#include "llvm/Support/CrashRecoveryContext.h"
26#include "llvm/Support/TimeProfiler.h"
27#include <cstdio>
28#include <memory>
29
30using namespace clang;
31
32namespace {
33
34/// Resets LLVM's pretty stack state so that stack traces are printed correctly
35/// when there are nested CrashRecoveryContexts and the inner one recovers from
36/// a crash.
37class ResetStackCleanup
38 : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
39 const void> {
40public:
41 ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
42 : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
43 Context, Top) {}
44 void recoverResources() override {
45 llvm::RestorePrettyStackState(resource);
46 }
47};
48
49/// If a crash happens while the parser is active, an entry is printed for it.
50class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
51 const Parser &P;
52public:
53 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
54 void print(raw_ostream &OS) const override;
55};
56
57/// If a crash happens while the parser is active, print out a line indicating
58/// what the current token is.
59void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
60 const Token &Tok = P.getCurToken();
61 if (Tok.is(tok::eof)) {
62 OS << "<eof> parser at end of file\n";
63 return;
64 }
65
66 if (Tok.getLocation().isInvalid()) {
67 OS << "<unknown> parser at unknown location\n";
68 return;
69 }
70
71 const Preprocessor &PP = P.getPreprocessor();
72 Tok.getLocation().print(OS, PP.getSourceManager());
73 if (Tok.isAnnotation()) {
74 OS << ": at annotation token\n";
75 } else {
76 // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
77 // allocate memory.
78 bool Invalid = false;
79 const SourceManager &SM = P.getPreprocessor().getSourceManager();
80 unsigned Length = Tok.getLength();
81 const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
82 if (Invalid) {
83 OS << ": unknown current parser token\n";
84 return;
85 }
86 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
87 }
88}
89
90} // namespace
91
92//===----------------------------------------------------------------------===//
93// Public interface to the file
94//===----------------------------------------------------------------------===//
95
96/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
97/// the file is parsed. This inserts the parsed decls into the translation unit
98/// held by Ctx.
99///
101 ASTContext &Ctx, bool PrintStats,
102 TranslationUnitKind TUKind,
103 CodeCompleteConsumer *CompletionConsumer,
104 bool SkipFunctionBodies) {
105
106 std::unique_ptr<Sema> S(
107 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
108
109 // Recover resources if we crash before exiting this method.
110 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
111
112 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
113}
114
115void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
116 // Collect global stats on Decls/Stmts (until we have a module streamer).
117 if (PrintStats) {
120 }
121
122 // Also turn on collection of stats inside of the Sema object.
123 bool OldCollectStats = PrintStats;
124 std::swap(OldCollectStats, S.CollectStats);
125
126 // Initialize the template instantiation observer chain.
127 // FIXME: See note on "finalize" below.
129
130 ASTConsumer *Consumer = &S.getASTConsumer();
131
132 std::unique_ptr<Parser> ParseOP(
133 new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
134 Parser &P = *ParseOP.get();
135
136 llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
137 CleanupPrettyStack(llvm::SavePrettyStackState());
138 PrettyStackTraceParserEntry CrashInfo(P);
139
140 // Recover resources if we crash before exiting this method.
141 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
142 CleanupParser(ParseOP.get());
143
146 if (External)
147 External->StartTranslationUnit(Consumer);
148
149 // If a PCH through header is specified that does not have an include in
150 // the source, or a PCH is being created with #pragma hdrstop with nothing
151 // after the pragma, there won't be any tokens or a Lexer.
152 bool HaveLexer = S.getPreprocessor().getCurrentLexer();
153
154 if (HaveLexer) {
155 llvm::TimeTraceScope TimeScope("Frontend");
156 P.Initialize();
158 Sema::ModuleImportState ImportState;
159 EnterExpressionEvaluationContext PotentiallyEvaluated(
160 S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
161
162 for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;
163 AtEOF = P.ParseTopLevelDecl(ADecl, ImportState)) {
164 // If we got a null return and something *was* parsed, ignore it. This
165 // is due to a top-level semicolon, an action override, or a parse error
166 // skipping something.
167 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
168 return;
169 }
170 }
171
172 // Process any TopLevelDecls generated by #pragma weak.
173 for (Decl *D : S.WeakTopLevelDecls())
174 Consumer->HandleTopLevelDecl(DeclGroupRef(D));
175
177
178 // Finalize the template instantiation observer chain.
179 // FIXME: This (and init.) should be done in the Sema class, but because
180 // Sema does not have a reliable "Finalize" function (it has a
181 // destructor, but it is not guaranteed to be called ("-disable-free")).
182 // So, do the initialization above and do the finalization here:
184
185 std::swap(OldCollectStats, S.CollectStats);
186 if (PrintStats) {
187 llvm::errs() << "\nSTATISTICS:\n";
188 if (HaveLexer) P.getActions().PrintStats();
192 Consumer->PrintStats();
193 }
194}
Defines the clang::ASTContext interface.
StringRef P
#define SM(sm)
Definition: Cuda.cpp:82
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:96
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:33
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: ASTConsumer.h:66
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:18
virtual void PrintStats()
PrintStats - If desired, print any statistics.
Definition: ASTConsumer.h:137
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
void PrintStats() const
Definition: ASTContext.cpp:950
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any.
Definition: ASTContext.h:1184
Abstract interface for a consumer of code-completion information.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
static void EnableStatistics()
Definition: DeclBase.cpp:174
static void PrintStats()
Definition: DeclBase.cpp:178
RAII object that enters a new expression evaluation context.
Abstract interface for external sources of AST nodes.
Wrapper for void* pointer.
Definition: Ownership.h:50
PtrTy get() const
Definition: Ownership.h:80
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:54
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
PreprocessorLexer * getCurrentLexer() const
Return the current lexer being lexed from.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
SourceManager & getSourceManager() const
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:426
Preprocessor & getPreprocessor() const
Definition: Sema.h:500
ASTContext & getASTContext() const
Definition: Sema.h:501
SmallVectorImpl< Decl * > & WeakTopLevelDecls()
WeakTopLevelDeclDecls - access to #pragma weak-generated Decls.
Definition: Sema.h:3727
ASTConsumer & getASTConsumer() const
Definition: Sema.h:502
bool CollectStats
Flag indicating whether or not to collect detailed statistics.
Definition: Sema.h:973
std::vector< std::unique_ptr< TemplateInstantiationCallback > > TemplateInstCallbacks
The template instantiation callbacks to trace or track instantiations (objects can be chained).
Definition: Sema.h:10534
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
Definition: Sema.h:8025
void print(raw_ostream &OS, const SourceManager &SM) const
This class handles loading and caching of source files into memory.
static void EnableStatistics()
Definition: Stmt.cpp:131
static void PrintStats()
Definition: Stmt.cpp:101
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:132
unsigned getLength() const
Definition: Token.h:135
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition: Token.h:99
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:121
The JSON file list parser is used to communicate input to InstallAPI.
void ParseAST(Preprocessor &pp, ASTConsumer *C, ASTContext &Ctx, bool PrintStats=false, TranslationUnitKind TUKind=TU_Complete, CodeCompleteConsumer *CompletionConsumer=nullptr, bool SkipFunctionBodies=false)
Parse the entire file specified, notifying the ASTConsumer as the file is parsed.
Definition: ParseAST.cpp:100
void initialize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
@ External
External linkage, which indicates that the entity can be referred to from other translation units.
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:1013
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
YAML serialization mapping.
Definition: Dominators.h:30