clang: lib/Parse/ParseAST.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
25#include "llvm/Support/CrashRecoveryContext.h"
26#include "llvm/Support/TimeProfiler.h"
27#include
28#include
29
30using namespace clang;
31
32namespace {
33
34
35
36
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
50class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
52public:
53 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
54 void print(raw_ostream &OS) const override;
55};
56
57
58
59void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
60 const Token &Tok = P.getCurToken();
61 if (Tok.is(tok::eof)) {
62 OS << " parser at end of file\n";
63 return;
64 }
65
67 OS << " parser at unknown location\n";
68 return;
69 }
70
74 OS << ": at annotation token\n";
75 } else {
76
77
79 const SourceManager &SM = P.getPreprocessor().getSourceManager();
80 unsigned Length = Tok.getLength();
83 OS << ": unknown current parser token\n";
84 return;
85 }
86 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
87 }
88}
89
90}
91
92
93
94
95
96
97
98
99
104 bool SkipFunctionBodies) {
105
106 std::unique_ptr S(
107 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
108
109
110 llvm::CrashRecoveryContextCleanupRegistrar CleanupSema(S.get());
111
112 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
113}
114
116
117 if (PrintStats) {
120 }
121
122
123 bool OldCollectStats = PrintStats;
125
126
127
129
131
132 std::unique_ptr ParseOP(
134 Parser &P = *ParseOP.get();
135
136 llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
137 CleanupPrettyStack(llvm::SavePrettyStackState());
138 PrettyStackTraceParserEntry CrashInfo(P);
139
140
141 llvm::CrashRecoveryContextCleanupRegistrar
142 CleanupParser(ParseOP.get());
143
147 External->StartTranslationUnit(Consumer);
148
149
150
151
153
154 if (HaveLexer) {
155 llvm::TimeTraceScope TimeScope("Frontend", [&]() {
156 llvm::TimeTraceMetadata M;
157 if (llvm::isTimeTraceVerbose()) {
159 if (const auto *FE = SM.getFileEntryForID(SM.getMainFileID()))
160 M.File = FE->tryGetRealPathName();
161 }
162 return M;
163 });
164 P.Initialize();
168 S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
169
170 for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;
171 AtEOF = P.ParseTopLevelDecl(ADecl, ImportState)) {
172
173
174
176 return;
177 }
178 }
179
180
183
185
186
187
188
189
190
192
194 if (PrintStats) {
195 llvm::errs() << "\nSTATISTICS:\n";
196 if (HaveLexer) P.getActions().PrintStats();
201 }
202}
Defines the clang::ASTContext interface.
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &ASTCtx, QualType Ty)
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
virtual void PrintStats()
PrintStats - If desired, print any statistics.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any.
Abstract interface for a consumer of code-completion information.
Decl - This represents one declaration (or definition), e.g.
static void EnableStatistics()
RAII object that enters a new expression evaluation context.
Abstract interface for external sources of AST nodes.
Wrapper for void* pointer.
Parser - This implements a parser for the C family of languages.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
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.
Preprocessor & getPreprocessor() const
ASTContext & getASTContext() const
SmallVectorImpl< Decl * > & WeakTopLevelDecls()
WeakTopLevelDeclDecls - access to #pragma weak-generated Decls.
ASTConsumer & getASTConsumer() const
bool CollectStats
Flag indicating whether or not to collect detailed statistics.
std::vector< std::unique_ptr< TemplateInstantiationCallback > > TemplateInstCallbacks
The template instantiation callbacks to trace or track instantiations (objects can be chained).
SourceManager & getSourceManager() const
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
void print(raw_ostream &OS, const SourceManager &SM) const
This class handles loading and caching of source files into memory.
static void EnableStatistics()
Token - This structure provides full information about a lexed token.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
unsigned getLength() const
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)) {....
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
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.
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.
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
Diagnostic wrappers for TextAPI types for error reporting.