1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Parse/ParseAST.h" |
14 | #include "clang/AST/ASTConsumer.h" |
15 | #include "clang/AST/ASTContext.h" |
16 | #include "clang/AST/ExternalASTSource.h" |
17 | #include "clang/AST/Stmt.h" |
18 | #include "clang/Parse/ParseDiagnostic.h" |
19 | #include "clang/Parse/Parser.h" |
20 | #include "clang/Sema/CodeCompleteConsumer.h" |
21 | #include "clang/Sema/Sema.h" |
22 | #include "clang/Sema/SemaConsumer.h" |
23 | #include "clang/Sema/TemplateInstCallback.h" |
24 | #include "llvm/Support/CrashRecoveryContext.h" |
25 | #include <cstdio> |
26 | #include <memory> |
27 | |
28 | using namespace clang; |
29 | |
30 | namespace { |
31 | |
32 | |
33 | |
34 | |
35 | class ResetStackCleanup |
36 | : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, |
37 | const void> { |
38 | public: |
39 | ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top) |
40 | : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>( |
41 | Context, Top) {} |
42 | void recoverResources() override { |
43 | llvm::RestorePrettyStackState(resource); |
44 | } |
45 | }; |
46 | |
47 | |
48 | class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry { |
49 | const Parser &P; |
50 | public: |
51 | PrettyStackTraceParserEntry(const Parser &p) : P(p) {} |
52 | void print(raw_ostream &OS) const override; |
53 | }; |
54 | |
55 | |
56 | |
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"; |
61 | return; |
62 | } |
63 | |
64 | if (Tok.getLocation().isInvalid()) { |
65 | OS << "<unknown> parser at unknown location\n"; |
66 | return; |
67 | } |
68 | |
69 | const Preprocessor &PP = P.getPreprocessor(); |
70 | Tok.getLocation().print(OS, PP.getSourceManager()); |
71 | if (Tok.isAnnotation()) { |
72 | OS << ": at annotation token\n"; |
73 | } else { |
74 | |
75 | |
76 | bool Invalid = false; |
77 | const SourceManager &SM = P.getPreprocessor().getSourceManager(); |
78 | unsigned Length = Tok.getLength(); |
79 | const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid); |
80 | if (Invalid) { |
81 | OS << ": unknown current parser token\n"; |
82 | return; |
83 | } |
84 | OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n"; |
85 | } |
86 | } |
87 | |
88 | } |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
99 | ASTContext &Ctx, bool PrintStats, |
100 | TranslationUnitKind TUKind, |
101 | CodeCompleteConsumer *CompletionConsumer, |
102 | bool SkipFunctionBodies) { |
103 | |
104 | std::unique_ptr<Sema> S( |
105 | new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer)); |
106 | |
107 | |
108 | llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get()); |
109 | |
110 | ParseAST(*S.get(), PrintStats, SkipFunctionBodies); |
111 | } |
112 | |
113 | void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) { |
114 | |
115 | if (PrintStats) { |
116 | Decl::EnableStatistics(); |
117 | Stmt::EnableStatistics(); |
118 | } |
119 | |
120 | |
121 | bool OldCollectStats = PrintStats; |
122 | std::swap(OldCollectStats, S.CollectStats); |
123 | |
124 | |
125 | |
126 | initialize(S.TemplateInstCallbacks, S); |
127 | |
128 | ASTConsumer *Consumer = &S.getASTConsumer(); |
129 | |
130 | std::unique_ptr<Parser> ParseOP( |
131 | new Parser(S.getPreprocessor(), S, SkipFunctionBodies)); |
132 | Parser &P = *ParseOP.get(); |
133 | |
134 | llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup> |
135 | CleanupPrettyStack(llvm::SavePrettyStackState()); |
136 | PrettyStackTraceParserEntry CrashInfo(P); |
137 | |
138 | |
139 | llvm::CrashRecoveryContextCleanupRegistrar<Parser> |
140 | CleanupParser(ParseOP.get()); |
141 | |
142 | S.getPreprocessor().EnterMainSourceFile(); |
143 | ExternalASTSource *External = S.getASTContext().getExternalSource(); |
144 | if (External) |
145 | External->StartTranslationUnit(Consumer); |
146 | |
147 | |
148 | |
149 | |
150 | bool HaveLexer = S.getPreprocessor().getCurrentLexer(); |
151 | |
152 | if (HaveLexer) { |
153 | P.Initialize(); |
154 | Parser::DeclGroupPtrTy ADecl; |
155 | for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF; |
156 | AtEOF = P.ParseTopLevelDecl(ADecl)) { |
157 | |
158 | |
159 | |
160 | if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get())) |
161 | return; |
162 | } |
163 | } |
164 | |
165 | |
166 | for (Decl *D : S.WeakTopLevelDecls()) |
167 | Consumer->HandleTopLevelDecl(DeclGroupRef(D)); |
168 | |
169 | Consumer->HandleTranslationUnit(S.getASTContext()); |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | finalize(S.TemplateInstCallbacks, S); |
177 | |
178 | std::swap(OldCollectStats, S.CollectStats); |
179 | if (PrintStats) { |
180 | llvm::errs() << "\nSTATISTICS:\n"; |
181 | if (HaveLexer) P.getActions().PrintStats(); |
182 | S.getASTContext().PrintStats(); |
183 | Decl::PrintStats(); |
184 | Stmt::PrintStats(); |
185 | Consumer->PrintStats(); |
186 | } |
187 | } |
188 | |