clang 8.0.0

ParseAST.cpp
Go to the documentation of this file.
1 //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the clang::ParseAST method.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Parse/ParseAST.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ExternalASTSource.h"
18 #include "clang/AST/Stmt.h"
19 #include "clang/Parse/ParseDiagnostic.h"
20 #include "clang/Parse/Parser.h"
21 #include "clang/Sema/CodeCompleteConsumer.h"
22 #include "clang/Sema/Sema.h"
23 #include "clang/Sema/SemaConsumer.h"
24 #include "clang/Sema/TemplateInstCallback.h"
25 #include "llvm/Support/CrashRecoveryContext.h"
26 #include <cstdio>
27 #include <memory>
28
29 using namespace clang;
30
31 namespace {
32
33 /// Resets LLVM's pretty stack state so that stack traces are printed correctly
34 /// when there are nested CrashRecoveryContexts and the inner one recovers from
35 /// a crash.
36 class ResetStackCleanup
37 : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
38 const void> {
39 public:
40 ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
41 : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
42 Context, Top) {}
43 void recoverResources() override {
44 llvm::RestorePrettyStackState(resource);
45 }
46 };
47
48 /// If a crash happens while the parser is active, an entry is printed for it.
49 class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
50 const Parser &P;
51 public:
52 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
53 void print(raw_ostream &OS) const override;
54 };
55
56 /// If a crash happens while the parser is active, print out a line indicating
57 /// what the current token is.
58 void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
59 const Token &Tok = P.getCurToken();
60 if (Tok.is(tok::eof)) {
61 OS << "<eof> parser at end of file\n";
62 return;
63 }
64
65 if (Tok.getLocation().isInvalid()) {
66 OS << "<unknown> parser at unknown location\n";
67 return;
68 }
69
70 const Preprocessor &PP = P.getPreprocessor();
71 Tok.getLocation().print(OS, PP.getSourceManager());
72 if (Tok.isAnnotation()) {
73 OS << ": at annotation token\n";
74 } else {
75 // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
76 // allocate memory.
77 bool Invalid = false;
78 const SourceManager &SM = P.getPreprocessor().getSourceManager();
79 unsigned Length = Tok.getLength();
80 const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
81 if (Invalid) {
82 OS << ": unknown current parser token\n";
83 return;
84 }
85 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
86 }
87 }
88
89 } // namespace
90
91 //===----------------------------------------------------------------------===//
92 // Public interface to the file
93 //===----------------------------------------------------------------------===//
94
95 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
96 /// the file is parsed. This inserts the parsed decls into the translation unit
97 /// held by Ctx.
98 ///
99 void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
100 ASTContext &Ctx, bool PrintStats,
101 TranslationUnitKind TUKind,
102 CodeCompleteConsumer *CompletionConsumer,
103 bool SkipFunctionBodies) {
104
105 std::unique_ptr<Sema> S(
106 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
107
108 // Recover resources if we crash before exiting this method.
109 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
110
111 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
112 }
113
114 void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
115 // Collect global stats on Decls/Stmts (until we have a module streamer).
116 if (PrintStats) {
117 Decl::EnableStatistics();
118 Stmt::EnableStatistics();
119 }
120
121 // Also turn on collection of stats inside of the Sema object.
122 bool OldCollectStats = PrintStats;
123 std::swap(OldCollectStats, S.CollectStats);
124
125 // Initialize the template instantiation observer chain.
126 // FIXME: See note on "finalize" below.
127 initialize(S.TemplateInstCallbacks, S);
128
129 ASTConsumer *Consumer = &S.getASTConsumer();
130
131 std::unique_ptr<Parser> ParseOP(
132 new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
133 Parser &P = *ParseOP.get();
134
135 llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
136 CleanupPrettyStack(llvm::SavePrettyStackState());
137 PrettyStackTraceParserEntry CrashInfo(P);
138
139 // Recover resources if we crash before exiting this method.
140 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
141 CleanupParser(ParseOP.get());
142
143 S.getPreprocessor().EnterMainSourceFile();
144 ExternalASTSource *External = S.getASTContext().getExternalSource();
145 if (External)
146 External->StartTranslationUnit(Consumer);
147
148 // If a PCH through header is specified that does not have an include in
149 // the source, or a PCH is being created with #pragma hdrstop with nothing
150 // after the pragma, there won't be any tokens or a Lexer.
151 bool HaveLexer = S.getPreprocessor().getCurrentLexer();
152
153 if (HaveLexer) {
154 P.Initialize();
155 Parser::DeclGroupPtrTy ADecl;
156 for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF;
157 AtEOF = P.ParseTopLevelDecl(ADecl)) {
158 // If we got a null return and something *was* parsed, ignore it. This
159 // is due to a top-level semicolon, an action override, or a parse error
160 // skipping something.
161 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
162 return;
163 }
164 }
165
166 // Process any TopLevelDecls generated by #pragma weak.
167 for (Decl *D : S.WeakTopLevelDecls())
168 Consumer->HandleTopLevelDecl(DeclGroupRef(D));
169
170 Consumer->HandleTranslationUnit(S.getASTContext());
171
172 // Finalize the template instantiation observer chain.
173 // FIXME: This (and init.) should be done in the Sema class, but because
174 // Sema does not have a reliable "Finalize" function (it has a
175 // destructor, but it is not guaranteed to be called ("-disable-free")).
176 // So, do the initialization above and do the finalization here:
177 finalize(S.TemplateInstCallbacks, S);
178
179 std::swap(OldCollectStats, S.CollectStats);
180 if (PrintStats) {
181 llvm::errs() << "\nSTATISTICS:\n";
182 if (HaveLexer) P.getActions().PrintStats();
183 S.getASTContext().PrintStats();
184 Decl::PrintStats();
185 Stmt::PrintStats();
186 Consumer->PrintStats();
187 }
188 }
ASTContext.h
Defines the clang::ASTContext interface.
SemaConsumer.h
clang::OpaquePtr::get
PtrTy get() const
Definition: Ownership.h:81
clang::Sema::WeakTopLevelDecls
SmallVectorImpl< Decl * > & WeakTopLevelDecls()
WeakTopLevelDeclDecls - access to #pragma weak-generated Decls.
Definition: Sema.h:1393
clang::Parser::Initialize
void Initialize()
Initialize - Warm up the parser.
Definition: Parser.cpp:438
clang::ASTConsumer
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:34
clang::Sema::TemplateInstCallbacks
std::vector< std::unique_ptr< TemplateInstantiationCallback > > TemplateInstCallbacks
The template instantiation callbacks to trace or track instantiations (objects can be chained)...
Definition: Sema.h:7343
llvm
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
Parser.h
clang::Token::is
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:95
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
P
StringRef P
Definition: ASTMatchersInternal.cpp:439
clang::SourceManager::getCharacterData
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer...
Definition: SourceManager.cpp:1094
CodeCompleteConsumer.h
clang::OpaquePtr
Wrapper for void* pointer.
Definition: Ownership.h:51
clang::Parser
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:58
clang::finalize
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
Definition: TemplateInstCallback.h:53
clang::ASTConsumer::PrintStats
virtual void PrintStats()
PrintStats - If desired, print any statistics.
Definition: ASTConsumer.h:133
Sema.h
clang::Token::isAnnotation
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:118
ParseDiagnostic.h
clang::SourceLocation::print
void print(raw_ostream &OS, const SourceManager &SM) const
Definition: SourceLocation.cpp:44
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
clang::format::Tok
const FormatToken & Tok
Definition: BreakableToken.h:244
clang::Sema::PrintStats
void PrintStats() const
Print out statistics about the semantic analysis.
Definition: Sema.cpp:424
ExternalASTSource.h
clang::driver::tools::arm::ReadTPMode::Invalid
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:278
clang::Parser::getActions
Sema & getActions() const
Definition: Parser.h:375
clang::Preprocessor::EnterMainSourceFile
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc...
Definition: Preprocessor.cpp:526
clang::Token::getLocation
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:124
clang::ASTConsumer::HandleTranslationUnit
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: ASTConsumer.h:67
Stmt.h
clang::Sema::getPreprocessor
Preprocessor & getPreprocessor() const
Definition: Sema.h:1237
ASTConsumer.h
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::Preprocessor::getSourceManager
SourceManager & getSourceManager() const
Definition: Preprocessor.h:819
clang::ExternalASTSource
Abstract interface for external sources of AST nodes.
Definition: ExternalASTSource.h:63
clang::ASTContext::PrintStats
void PrintStats() const
Definition: ASTContext.cpp:925
clang::Sema::getASTContext
ASTContext & getASTContext() const
Definition: Sema.h:1238
clang::Stmt::PrintStats
static void PrintStats()
Definition: Stmt.cpp:87
TemplateInstCallback.h
clang::ParseAST
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:99
clang::Sema::CollectStats
bool CollectStats
Flag indicating whether or not to collect detailed statistics.
Definition: Sema.h:330
clang::Preprocessor::getCurrentLexer
PreprocessorLexer * getCurrentLexer() const
Return the current lexer being lexed from.
Definition: Preprocessor.h:887
clang::Parser::ParseTopLevelDecl
bool ParseTopLevelDecl(DeclGroupPtrTy &Result)
ParseTopLevelDecl - Parse one top-level declaration.
Definition: Parser.cpp:546
clang::Sema::getASTConsumer
ASTConsumer & getASTConsumer() const
Definition: Sema.h:1239
ParseAST.h
clang::CodeCompleteConsumer
Abstract interface for a consumer of code-completion information.
Definition: CodeCompleteConsumer.h:999
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::ExternalASTSource::StartTranslationUnit
virtual void StartTranslationUnit(ASTConsumer *Consumer)
Function that will be invoked when we begin parsing a new translation unit involving this external AS...
Definition: ExternalASTSource.cpp:71
clang::DeclGroupRef
Definition: DeclGroup.h:52
clang::ASTContext::getExternalSource
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any...
Definition: ASTContext.h:1083
clang::Token::getLength
unsigned getLength() const
Definition: Token.h:127
clang::comments::tok::eof
Definition: CommentLexer.h:34
clang::SourceLocation::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:109
clang::TranslationUnitKind
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:362
clang::initialize
void initialize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
Definition: TemplateInstCallback.h:44
clang::Decl::PrintStats
static void PrintStats()
Definition: DeclBase.cpp:169
clang::ASTConsumer::HandleTopLevelDecl
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:19
clang::Decl::EnableStatistics
static void EnableStatistics()
Definition: DeclBase.cpp:165
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::Parser::ParseFirstTopLevelDecl
bool ParseFirstTopLevelDecl(DeclGroupPtrTy &Result)
Parse the first top-level declaration in a translation unit.
Definition: Parser.cpp:529
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:125
clang::Stmt::EnableStatistics
static void EnableStatistics()
Definition: Stmt.cpp:117