1 //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the clang::ParseAST method.
12 //===----------------------------------------------------------------------===//
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 "llvm/Support/CrashRecoveryContext.h"
28 using namespace clang;
32 /// Resets LLVM's pretty stack state so that stack traces are printed correctly
33 /// when there are nested CrashRecoveryContexts and the inner one recovers from
35 class ResetStackCleanup
36 : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
39 ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
40 : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
42 void recoverResources() override {
43 llvm::RestorePrettyStackState(resource);
47 /// If a crash happens while the parser is active, an entry is printed for it.
48 class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
51 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
52 void print(raw_ostream &OS) const override;
55 /// If a crash happens while the parser is active, print out a line indicating
56 /// what the current token is.
57 void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
58 const Token &Tok = P.getCurToken();
59 if (Tok.is(tok::eof)) {
60 OS << "<eof> parser at end of file\n";
64 if (Tok.getLocation().isInvalid()) {
65 OS << "<unknown> parser at unknown location\n";
69 const Preprocessor &PP = P.getPreprocessor();
70 Tok.getLocation().print(OS, PP.getSourceManager());
71 if (Tok.isAnnotation()) {
72 OS << ": at annotation token\n";
74 // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
77 const SourceManager &SM = P.getPreprocessor().getSourceManager();
78 unsigned Length = Tok.getLength();
79 const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
81 OS << ": unknown current parser token\n";
84 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
90 //===----------------------------------------------------------------------===//
91 // Public interface to the file
92 //===----------------------------------------------------------------------===//
94 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
95 /// the file is parsed. This inserts the parsed decls into the translation unit
98 void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
99 ASTContext &Ctx, bool PrintStats,
100 TranslationUnitKind TUKind,
101 CodeCompleteConsumer *CompletionConsumer,
102 bool SkipFunctionBodies) {
104 std::unique_ptr<Sema> S(
105 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
107 // Recover resources if we crash before exiting this method.
108 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
110 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
113 void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
114 // Collect global stats on Decls/Stmts (until we have a module streamer).
116 Decl::EnableStatistics();
117 Stmt::EnableStatistics();
120 // Also turn on collection of stats inside of the Sema object.
121 bool OldCollectStats = PrintStats;
122 std::swap(OldCollectStats, S.CollectStats);
124 ASTConsumer *Consumer = &S.getASTConsumer();
126 std::unique_ptr<Parser> ParseOP(
127 new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
128 Parser &P = *ParseOP.get();
130 llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
131 CleanupPrettyStack(llvm::SavePrettyStackState());
132 PrettyStackTraceParserEntry CrashInfo(P);
134 // Recover resources if we crash before exiting this method.
135 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
136 CleanupParser(ParseOP.get());
138 S.getPreprocessor().EnterMainSourceFile();
141 Parser::DeclGroupPtrTy ADecl;
142 ExternalASTSource *External = S.getASTContext().getExternalSource();
144 External->StartTranslationUnit(Consumer);
146 for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF;
147 AtEOF = P.ParseTopLevelDecl(ADecl)) {
148 // If we got a null return and something *was* parsed, ignore it. This
149 // is due to a top-level semicolon, an action override, or a parse error
150 // skipping something.
151 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
155 // Process any TopLevelDecls generated by #pragma weak.
156 for (Decl *D : S.WeakTopLevelDecls())
157 Consumer->HandleTopLevelDecl(DeclGroupRef(D));
159 Consumer->HandleTranslationUnit(S.getASTContext());
161 std::swap(OldCollectStats, S.CollectStats);
163 llvm::errs() << "\nSTATISTICS:\n";
164 P.getActions().PrintStats();
165 S.getASTContext().PrintStats();
168 Consumer->PrintStats();