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/ExternalSemaSource.h"
23 #include "clang/Sema/Sema.h"
24 #include "clang/Sema/SemaConsumer.h"
25 #include "llvm/Support/CrashRecoveryContext.h"
29 using namespace clang;
33 /// If a crash happens while the parser is active, an entry is printed for it.
34 class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
37 PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
38 void print(raw_ostream &OS) const override;
41 /// If a crash happens while the parser is active, print out a line indicating
42 /// what the current token is.
43 void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
44 const Token &Tok = P.getCurToken();
45 if (Tok.is(tok::eof)) {
46 OS << "<eof> parser at end of file\n";
50 if (Tok.getLocation().isInvalid()) {
51 OS << "<unknown> parser at unknown location\n";
55 const Preprocessor &PP = P.getPreprocessor();
56 Tok.getLocation().print(OS, PP.getSourceManager());
57 if (Tok.isAnnotation()) {
58 OS << ": at annotation token\n";
60 // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
63 const SourceManager &SM = P.getPreprocessor().getSourceManager();
64 unsigned Length = Tok.getLength();
65 const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
67 OS << ": unknown current parser token\n";
70 OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
76 //===----------------------------------------------------------------------===//
77 // Public interface to the file
78 //===----------------------------------------------------------------------===//
80 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
81 /// the file is parsed. This inserts the parsed decls into the translation unit
84 void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
85 ASTContext &Ctx, bool PrintStats,
86 TranslationUnitKind TUKind,
87 CodeCompleteConsumer *CompletionConsumer,
88 bool SkipFunctionBodies) {
90 std::unique_ptr<Sema> S(
91 new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
93 // Recover resources if we crash before exiting this method.
94 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
96 ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
99 void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
100 // Collect global stats on Decls/Stmts (until we have a module streamer).
102 Decl::EnableStatistics();
103 Stmt::EnableStatistics();
106 // Also turn on collection of stats inside of the Sema object.
107 bool OldCollectStats = PrintStats;
108 std::swap(OldCollectStats, S.CollectStats);
110 ASTConsumer *Consumer = &S.getASTConsumer();
112 std::unique_ptr<Parser> ParseOP(
113 new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
114 Parser &P = *ParseOP.get();
116 PrettyStackTraceParserEntry CrashInfo(P);
118 // Recover resources if we crash before exiting this method.
119 llvm::CrashRecoveryContextCleanupRegistrar<Parser>
120 CleanupParser(ParseOP.get());
122 S.getPreprocessor().EnterMainSourceFile();
125 // C11 6.9p1 says translation units must have at least one top-level
126 // declaration. C++ doesn't have this restriction. We also don't want to
127 // complain if we have a precompiled header, although technically if the PCH
128 // is empty we should still emit the (pedantic) diagnostic.
129 Parser::DeclGroupPtrTy ADecl;
130 ExternalASTSource *External = S.getASTContext().getExternalSource();
132 External->StartTranslationUnit(Consumer);
134 if (P.ParseTopLevelDecl(ADecl)) {
135 if (!External && !S.getLangOpts().CPlusPlus)
136 P.Diag(diag::ext_empty_translation_unit);
139 // If we got a null return and something *was* parsed, ignore it. This
140 // is due to a top-level semicolon, an action override, or a parse error
141 // skipping something.
142 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
144 } while (!P.ParseTopLevelDecl(ADecl));
147 // Process any TopLevelDecls generated by #pragma weak.
148 for (Decl *D : S.WeakTopLevelDecls())
149 Consumer->HandleTopLevelDecl(DeclGroupRef(D));
151 Consumer->HandleTranslationUnit(S.getASTContext());
153 std::swap(OldCollectStats, S.CollectStats);
155 llvm::errs() << "\nSTATISTICS:\n";
156 P.getActions().PrintStats();
157 S.getASTContext().PrintStats();
160 Consumer->PrintStats();