1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Frontend/ASTUnit.h" |
14 | #include "clang/AST/ASTConsumer.h" |
15 | #include "clang/AST/ASTContext.h" |
16 | #include "clang/AST/CommentCommandTraits.h" |
17 | #include "clang/AST/Decl.h" |
18 | #include "clang/AST/DeclBase.h" |
19 | #include "clang/AST/DeclCXX.h" |
20 | #include "clang/AST/DeclGroup.h" |
21 | #include "clang/AST/DeclObjC.h" |
22 | #include "clang/AST/DeclTemplate.h" |
23 | #include "clang/AST/DeclarationName.h" |
24 | #include "clang/AST/ExternalASTSource.h" |
25 | #include "clang/AST/PrettyPrinter.h" |
26 | #include "clang/AST/Type.h" |
27 | #include "clang/AST/TypeOrdering.h" |
28 | #include "clang/Basic/Diagnostic.h" |
29 | #include "clang/Basic/FileManager.h" |
30 | #include "clang/Basic/IdentifierTable.h" |
31 | #include "clang/Basic/LLVM.h" |
32 | #include "clang/Basic/LangOptions.h" |
33 | #include "clang/Basic/Module.h" |
34 | #include "clang/Basic/SourceLocation.h" |
35 | #include "clang/Basic/SourceManager.h" |
36 | #include "clang/Basic/TargetInfo.h" |
37 | #include "clang/Basic/TargetOptions.h" |
38 | #include "clang/Frontend/CompilerInstance.h" |
39 | #include "clang/Frontend/CompilerInvocation.h" |
40 | #include "clang/Frontend/FrontendAction.h" |
41 | #include "clang/Frontend/FrontendActions.h" |
42 | #include "clang/Frontend/FrontendDiagnostic.h" |
43 | #include "clang/Frontend/FrontendOptions.h" |
44 | #include "clang/Frontend/MultiplexConsumer.h" |
45 | #include "clang/Frontend/PrecompiledPreamble.h" |
46 | #include "clang/Frontend/Utils.h" |
47 | #include "clang/Lex/HeaderSearch.h" |
48 | #include "clang/Lex/HeaderSearchOptions.h" |
49 | #include "clang/Lex/Lexer.h" |
50 | #include "clang/Lex/PPCallbacks.h" |
51 | #include "clang/Lex/PreprocessingRecord.h" |
52 | #include "clang/Lex/Preprocessor.h" |
53 | #include "clang/Lex/PreprocessorOptions.h" |
54 | #include "clang/Lex/Token.h" |
55 | #include "clang/Sema/CodeCompleteConsumer.h" |
56 | #include "clang/Sema/CodeCompleteOptions.h" |
57 | #include "clang/Sema/Sema.h" |
58 | #include "clang/Serialization/ASTBitCodes.h" |
59 | #include "clang/Serialization/ASTReader.h" |
60 | #include "clang/Serialization/ASTWriter.h" |
61 | #include "clang/Serialization/ContinuousRangeMap.h" |
62 | #include "clang/Serialization/InMemoryModuleCache.h" |
63 | #include "clang/Serialization/Module.h" |
64 | #include "clang/Serialization/PCHContainerOperations.h" |
65 | #include "llvm/ADT/ArrayRef.h" |
66 | #include "llvm/ADT/DenseMap.h" |
67 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
68 | #include "llvm/ADT/None.h" |
69 | #include "llvm/ADT/Optional.h" |
70 | #include "llvm/ADT/STLExtras.h" |
71 | #include "llvm/ADT/SmallString.h" |
72 | #include "llvm/ADT/SmallVector.h" |
73 | #include "llvm/ADT/StringMap.h" |
74 | #include "llvm/ADT/StringRef.h" |
75 | #include "llvm/ADT/StringSet.h" |
76 | #include "llvm/ADT/Twine.h" |
77 | #include "llvm/ADT/iterator_range.h" |
78 | #include "llvm/Bitcode/BitstreamWriter.h" |
79 | #include "llvm/Support/Allocator.h" |
80 | #include "llvm/Support/Casting.h" |
81 | #include "llvm/Support/CrashRecoveryContext.h" |
82 | #include "llvm/Support/DJB.h" |
83 | #include "llvm/Support/ErrorHandling.h" |
84 | #include "llvm/Support/ErrorOr.h" |
85 | #include "llvm/Support/FileSystem.h" |
86 | #include "llvm/Support/MemoryBuffer.h" |
87 | #include "llvm/Support/Mutex.h" |
88 | #include "llvm/Support/Timer.h" |
89 | #include "llvm/Support/VirtualFileSystem.h" |
90 | #include "llvm/Support/raw_ostream.h" |
91 | #include <algorithm> |
92 | #include <atomic> |
93 | #include <cassert> |
94 | #include <cstdint> |
95 | #include <cstdio> |
96 | #include <cstdlib> |
97 | #include <memory> |
98 | #include <string> |
99 | #include <tuple> |
100 | #include <utility> |
101 | #include <vector> |
102 | |
103 | using namespace clang; |
104 | |
105 | using llvm::TimeRecord; |
106 | |
107 | namespace { |
108 | |
109 | class SimpleTimer { |
110 | bool WantTiming; |
111 | TimeRecord Start; |
112 | std::string Output; |
113 | |
114 | public: |
115 | explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { |
116 | if (WantTiming) |
117 | Start = TimeRecord::getCurrentTime(); |
118 | } |
119 | |
120 | ~SimpleTimer() { |
121 | if (WantTiming) { |
122 | TimeRecord Elapsed = TimeRecord::getCurrentTime(); |
123 | Elapsed -= Start; |
124 | llvm::errs() << Output << ':'; |
125 | Elapsed.print(Elapsed, llvm::errs()); |
126 | llvm::errs() << '\n'; |
127 | } |
128 | } |
129 | |
130 | void setOutput(const Twine &Output) { |
131 | if (WantTiming) |
132 | this->Output = Output.str(); |
133 | } |
134 | }; |
135 | |
136 | } |
137 | |
138 | template <class T> |
139 | static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) { |
140 | if (!Val) |
141 | return nullptr; |
142 | return std::move(*Val); |
143 | } |
144 | |
145 | template <class T> |
146 | static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) { |
147 | if (!Val) |
148 | return false; |
149 | Output = std::move(*Val); |
150 | return true; |
151 | } |
152 | |
153 | |
154 | |
155 | static std::unique_ptr<llvm::MemoryBuffer> |
156 | getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation, |
157 | llvm::vfs::FileSystem *VFS, |
158 | StringRef FilePath, bool isVolatile) { |
159 | const auto &PreprocessorOpts = Invocation.getPreprocessorOpts(); |
160 | |
161 | |
162 | |
163 | |
164 | llvm::MemoryBuffer *Buffer = nullptr; |
165 | std::unique_ptr<llvm::MemoryBuffer> BufferOwner; |
166 | auto FileStatus = VFS->status(FilePath); |
167 | if (FileStatus) { |
168 | llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID(); |
169 | |
170 | |
171 | for (const auto &RF : PreprocessorOpts.RemappedFiles) { |
172 | std::string MPath(RF.first); |
173 | auto MPathStatus = VFS->status(MPath); |
174 | if (MPathStatus) { |
175 | llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID(); |
176 | if (MainFileID == MID) { |
177 | |
178 | BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second, -1, true, isVolatile)); |
179 | if (!BufferOwner) |
180 | return nullptr; |
181 | } |
182 | } |
183 | } |
184 | |
185 | |
186 | |
187 | for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) { |
188 | std::string MPath(RB.first); |
189 | auto MPathStatus = VFS->status(MPath); |
190 | if (MPathStatus) { |
191 | llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID(); |
192 | if (MainFileID == MID) { |
193 | |
194 | BufferOwner.reset(); |
195 | Buffer = const_cast<llvm::MemoryBuffer *>(RB.second); |
196 | } |
197 | } |
198 | } |
199 | } |
200 | |
201 | |
202 | if (!Buffer && !BufferOwner) { |
203 | BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath, -1, true, isVolatile)); |
204 | if (!BufferOwner) |
205 | return nullptr; |
206 | } |
207 | |
208 | if (BufferOwner) |
209 | return BufferOwner; |
210 | if (!Buffer) |
211 | return nullptr; |
212 | return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath); |
213 | } |
214 | |
215 | struct ASTUnit::ASTWriterData { |
216 | SmallString<128> Buffer; |
217 | llvm::BitstreamWriter Stream; |
218 | ASTWriter Writer; |
219 | |
220 | ASTWriterData(InMemoryModuleCache &ModuleCache) |
221 | : Stream(Buffer), Writer(Stream, Buffer, ModuleCache, {}) {} |
222 | }; |
223 | |
224 | void ASTUnit::clearFileLevelDecls() { |
225 | llvm::DeleteContainerSeconds(FileDecls); |
226 | } |
227 | |
228 | |
229 | |
230 | |
231 | |
232 | const unsigned DefaultPreambleRebuildInterval = 5; |
233 | |
234 | |
235 | |
236 | |
237 | static std::atomic<unsigned> ActiveASTUnitObjects; |
238 | |
239 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
240 | : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")), |
241 | ShouldCacheCodeCompletionResults(false), |
242 | IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), |
243 | UnsafeToFree(false) { |
244 | if (getenv("LIBCLANG_OBJTRACKING")) |
245 | fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects); |
246 | } |
247 | |
248 | ASTUnit::~ASTUnit() { |
249 | |
250 | if (MainFileIsAST && getDiagnostics().getClient()) { |
251 | getDiagnostics().getClient()->EndSourceFile(); |
252 | } |
253 | |
254 | clearFileLevelDecls(); |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | if (Invocation && OwnsRemappedFileBuffers) { |
261 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
262 | for (const auto &RB : PPOpts.RemappedFileBuffers) |
263 | delete RB.second; |
264 | } |
265 | |
266 | ClearCachedCompletionResults(); |
267 | |
268 | if (getenv("LIBCLANG_OBJTRACKING")) |
269 | fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects); |
270 | } |
271 | |
272 | void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) { |
273 | this->PP = std::move(PP); |
274 | } |
275 | |
276 | void ASTUnit::enableSourceFileDiagnostics() { |
277 | (0) . __assert_fail ("getDiagnostics().getClient() && Ctx && \"Bad context for source file\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 278, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getDiagnostics().getClient() && Ctx && |
278 | (0) . __assert_fail ("getDiagnostics().getClient() && Ctx && \"Bad context for source file\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 278, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Bad context for source file"); |
279 | getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get()); |
280 | } |
281 | |
282 | |
283 | |
284 | static uint64_t getDeclShowContexts(const NamedDecl *ND, |
285 | const LangOptions &LangOpts, |
286 | bool &IsNestedNameSpecifier) { |
287 | IsNestedNameSpecifier = false; |
288 | |
289 | if (isa<UsingShadowDecl>(ND)) |
290 | ND = ND->getUnderlyingDecl(); |
291 | if (!ND) |
292 | return 0; |
293 | |
294 | uint64_t Contexts = 0; |
295 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || |
296 | isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) || |
297 | isa<TypeAliasTemplateDecl>(ND)) { |
298 | |
299 | if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) |
300 | Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel) |
301 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
302 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
303 | | (1LL << CodeCompletionContext::CCC_Statement) |
304 | | (1LL << CodeCompletionContext::CCC_Type) |
305 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); |
306 | |
307 | |
308 | if (LangOpts.CPlusPlus) |
309 | Contexts |= (1LL << CodeCompletionContext::CCC_Expression); |
310 | |
311 | |
312 | |
313 | if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) |
314 | Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); |
315 | |
316 | |
317 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
318 | |
319 | if (ID->getDefinition()) |
320 | Contexts |= (1LL << CodeCompletionContext::CCC_Expression); |
321 | Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName); |
322 | } |
323 | |
324 | |
325 | if (isa<EnumDecl>(ND)) { |
326 | Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag); |
327 | |
328 | |
329 | if (LangOpts.CPlusPlus11) |
330 | IsNestedNameSpecifier = true; |
331 | } else if (const auto *Record = dyn_cast<RecordDecl>(ND)) { |
332 | if (Record->isUnion()) |
333 | Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag); |
334 | else |
335 | Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag); |
336 | |
337 | if (LangOpts.CPlusPlus) |
338 | IsNestedNameSpecifier = true; |
339 | } else if (isa<ClassTemplateDecl>(ND)) |
340 | IsNestedNameSpecifier = true; |
341 | } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
342 | |
343 | Contexts = (1LL << CodeCompletionContext::CCC_Statement) |
344 | | (1LL << CodeCompletionContext::CCC_Expression) |
345 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
346 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); |
347 | } else if (isa<ObjCProtocolDecl>(ND)) { |
348 | Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName); |
349 | } else if (isa<ObjCCategoryDecl>(ND)) { |
350 | Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName); |
351 | } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { |
352 | Contexts = (1LL << CodeCompletionContext::CCC_Namespace); |
353 | |
354 | |
355 | IsNestedNameSpecifier = true; |
356 | } |
357 | |
358 | return Contexts; |
359 | } |
360 | |
361 | void ASTUnit::CacheCodeCompletionResults() { |
362 | if (!TheSema) |
363 | return; |
364 | |
365 | SimpleTimer Timer(WantTiming); |
366 | Timer.setOutput("Cache global code completions for " + getMainFileName()); |
367 | |
368 | |
369 | ClearCachedCompletionResults(); |
370 | |
371 | |
372 | using Result = CodeCompletionResult; |
373 | SmallVector<Result, 8> Results; |
374 | CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); |
375 | CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator); |
376 | TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, |
377 | CCTUInfo, Results); |
378 | |
379 | |
380 | llvm::DenseMap<CanQualType, unsigned> CompletionTypes; |
381 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel); |
382 | |
383 | for (auto &R : Results) { |
384 | switch (R.Kind) { |
385 | case Result::RK_Declaration: { |
386 | bool IsNestedNameSpecifier = false; |
387 | CachedCodeCompletionResult CachedResult; |
388 | CachedResult.Completion = R.CreateCodeCompletionString( |
389 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
390 | IncludeBriefCommentsInCodeCompletion); |
391 | CachedResult.ShowInContexts = getDeclShowContexts( |
392 | R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier); |
393 | CachedResult.Priority = R.Priority; |
394 | CachedResult.Kind = R.CursorKind; |
395 | CachedResult.Availability = R.Availability; |
396 | |
397 | |
398 | |
399 | QualType UsageType = getDeclUsageType(*Ctx, R.Declaration); |
400 | if (UsageType.isNull()) { |
401 | CachedResult.TypeClass = STC_Void; |
402 | CachedResult.Type = 0; |
403 | } else { |
404 | CanQualType CanUsageType |
405 | = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); |
406 | CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); |
407 | |
408 | |
409 | |
410 | |
411 | unsigned &TypeValue = CompletionTypes[CanUsageType]; |
412 | if (TypeValue == 0) { |
413 | TypeValue = CompletionTypes.size(); |
414 | CachedCompletionTypes[QualType(CanUsageType).getAsString()] |
415 | = TypeValue; |
416 | } |
417 | |
418 | CachedResult.Type = TypeValue; |
419 | } |
420 | |
421 | CachedCompletionResults.push_back(CachedResult); |
422 | |
423 | |
424 | if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier && |
425 | !R.StartsNestedNameSpecifier) { |
426 | |
427 | uint64_t NNSContexts |
428 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
429 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
430 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
431 | | (1LL << CodeCompletionContext::CCC_Statement) |
432 | | (1LL << CodeCompletionContext::CCC_Expression) |
433 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
434 | | (1LL << CodeCompletionContext::CCC_EnumTag) |
435 | | (1LL << CodeCompletionContext::CCC_UnionTag) |
436 | | (1LL << CodeCompletionContext::CCC_ClassOrStructTag) |
437 | | (1LL << CodeCompletionContext::CCC_Type) |
438 | | (1LL << CodeCompletionContext::CCC_Symbol) |
439 | | (1LL << CodeCompletionContext::CCC_SymbolOrNewName) |
440 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); |
441 | |
442 | if (isa<NamespaceDecl>(R.Declaration) || |
443 | isa<NamespaceAliasDecl>(R.Declaration)) |
444 | NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace); |
445 | |
446 | if (uint64_t RemainingContexts |
447 | = NNSContexts & ~CachedResult.ShowInContexts) { |
448 | |
449 | |
450 | |
451 | R.StartsNestedNameSpecifier = true; |
452 | CachedResult.Completion = R.CreateCodeCompletionString( |
453 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
454 | IncludeBriefCommentsInCodeCompletion); |
455 | CachedResult.ShowInContexts = RemainingContexts; |
456 | CachedResult.Priority = CCP_NestedNameSpecifier; |
457 | CachedResult.TypeClass = STC_Void; |
458 | CachedResult.Type = 0; |
459 | CachedCompletionResults.push_back(CachedResult); |
460 | } |
461 | } |
462 | break; |
463 | } |
464 | |
465 | case Result::RK_Keyword: |
466 | case Result::RK_Pattern: |
467 | |
468 | |
469 | break; |
470 | |
471 | case Result::RK_Macro: { |
472 | CachedCodeCompletionResult CachedResult; |
473 | CachedResult.Completion = R.CreateCodeCompletionString( |
474 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
475 | IncludeBriefCommentsInCodeCompletion); |
476 | CachedResult.ShowInContexts |
477 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
478 | | (1LL << CodeCompletionContext::CCC_ObjCInterface) |
479 | | (1LL << CodeCompletionContext::CCC_ObjCImplementation) |
480 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
481 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
482 | | (1LL << CodeCompletionContext::CCC_Statement) |
483 | | (1LL << CodeCompletionContext::CCC_Expression) |
484 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
485 | | (1LL << CodeCompletionContext::CCC_MacroNameUse) |
486 | | (1LL << CodeCompletionContext::CCC_PreprocessorExpression) |
487 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
488 | | (1LL << CodeCompletionContext::CCC_OtherWithMacros); |
489 | |
490 | CachedResult.Priority = R.Priority; |
491 | CachedResult.Kind = R.CursorKind; |
492 | CachedResult.Availability = R.Availability; |
493 | CachedResult.TypeClass = STC_Void; |
494 | CachedResult.Type = 0; |
495 | CachedCompletionResults.push_back(CachedResult); |
496 | break; |
497 | } |
498 | } |
499 | } |
500 | |
501 | |
502 | CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue; |
503 | } |
504 | |
505 | void ASTUnit::ClearCachedCompletionResults() { |
506 | CachedCompletionResults.clear(); |
507 | CachedCompletionTypes.clear(); |
508 | CachedCompletionAllocator = nullptr; |
509 | } |
510 | |
511 | namespace { |
512 | |
513 | |
514 | |
515 | class ASTInfoCollector : public ASTReaderListener { |
516 | Preprocessor &PP; |
517 | ASTContext *Context; |
518 | HeaderSearchOptions &HSOpts; |
519 | PreprocessorOptions &PPOpts; |
520 | LangOptions &LangOpt; |
521 | std::shared_ptr<TargetOptions> &TargetOpts; |
522 | IntrusiveRefCntPtr<TargetInfo> &Target; |
523 | unsigned &Counter; |
524 | bool InitializedLanguage = false; |
525 | |
526 | public: |
527 | ASTInfoCollector(Preprocessor &PP, ASTContext *Context, |
528 | HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts, |
529 | LangOptions &LangOpt, |
530 | std::shared_ptr<TargetOptions> &TargetOpts, |
531 | IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter) |
532 | : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts), |
533 | LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target), |
534 | Counter(Counter) {} |
535 | |
536 | bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, |
537 | bool AllowCompatibleDifferences) override { |
538 | if (InitializedLanguage) |
539 | return false; |
540 | |
541 | LangOpt = LangOpts; |
542 | InitializedLanguage = true; |
543 | |
544 | updated(); |
545 | return false; |
546 | } |
547 | |
548 | bool (const HeaderSearchOptions &HSOpts, |
549 | StringRef SpecificModuleCachePath, |
550 | bool Complain) override { |
551 | this->HSOpts = HSOpts; |
552 | return false; |
553 | } |
554 | |
555 | bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain, |
556 | std::string &SuggestedPredefines) override { |
557 | this->PPOpts = PPOpts; |
558 | return false; |
559 | } |
560 | |
561 | bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, |
562 | bool AllowCompatibleDifferences) override { |
563 | |
564 | if (Target) |
565 | return false; |
566 | |
567 | this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts); |
568 | Target = |
569 | TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts); |
570 | |
571 | updated(); |
572 | return false; |
573 | } |
574 | |
575 | void ReadCounter(const serialization::ModuleFile &M, |
576 | unsigned Value) override { |
577 | Counter = Value; |
578 | } |
579 | |
580 | private: |
581 | void updated() { |
582 | if (!Target || !InitializedLanguage) |
583 | return; |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | Target->adjust(LangOpt); |
590 | |
591 | |
592 | PP.Initialize(*Target); |
593 | |
594 | if (!Context) |
595 | return; |
596 | |
597 | |
598 | Context->InitBuiltinTypes(*Target); |
599 | |
600 | |
601 | Context->setPrintingPolicy(PrintingPolicy(LangOpt)); |
602 | |
603 | |
604 | |
605 | Context->getCommentCommandTraits().registerCommentOptions( |
606 | LangOpt.CommentOpts); |
607 | } |
608 | }; |
609 | |
610 | |
611 | class StoredDiagnosticConsumer : public DiagnosticConsumer { |
612 | SmallVectorImpl<StoredDiagnostic> *StoredDiags; |
613 | SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags; |
614 | const LangOptions *LangOpts = nullptr; |
615 | SourceManager *SourceMgr = nullptr; |
616 | |
617 | public: |
618 | StoredDiagnosticConsumer( |
619 | SmallVectorImpl<StoredDiagnostic> *StoredDiags, |
620 | SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags) |
621 | : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags) { |
622 | (0) . __assert_fail ("(StoredDiags || StandaloneDiags) && \"No output collections were passed to StoredDiagnosticConsumer.\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 623, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((StoredDiags || StandaloneDiags) && |
623 | (0) . __assert_fail ("(StoredDiags || StandaloneDiags) && \"No output collections were passed to StoredDiagnosticConsumer.\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 623, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No output collections were passed to StoredDiagnosticConsumer."); |
624 | } |
625 | |
626 | void BeginSourceFile(const LangOptions &LangOpts, |
627 | const Preprocessor *PP = nullptr) override { |
628 | this->LangOpts = &LangOpts; |
629 | if (PP) |
630 | SourceMgr = &PP->getSourceManager(); |
631 | } |
632 | |
633 | void HandleDiagnostic(DiagnosticsEngine::Level Level, |
634 | const Diagnostic &Info) override; |
635 | }; |
636 | |
637 | |
638 | |
639 | class CaptureDroppedDiagnostics { |
640 | DiagnosticsEngine &Diags; |
641 | StoredDiagnosticConsumer Client; |
642 | DiagnosticConsumer *PreviousClient = nullptr; |
643 | std::unique_ptr<DiagnosticConsumer> OwningPreviousClient; |
644 | |
645 | public: |
646 | CaptureDroppedDiagnostics( |
647 | bool RequestCapture, DiagnosticsEngine &Diags, |
648 | SmallVectorImpl<StoredDiagnostic> *StoredDiags, |
649 | SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags) |
650 | : Diags(Diags), Client(StoredDiags, StandaloneDiags) { |
651 | if (RequestCapture || Diags.getClient() == nullptr) { |
652 | OwningPreviousClient = Diags.takeClient(); |
653 | PreviousClient = Diags.getClient(); |
654 | Diags.setClient(&Client, false); |
655 | } |
656 | } |
657 | |
658 | ~CaptureDroppedDiagnostics() { |
659 | if (Diags.getClient() == &Client) |
660 | Diags.setClient(PreviousClient, !!OwningPreviousClient.release()); |
661 | } |
662 | }; |
663 | |
664 | } |
665 | |
666 | static ASTUnit::StandaloneDiagnostic |
667 | makeStandaloneDiagnostic(const LangOptions &LangOpts, |
668 | const StoredDiagnostic &InDiag); |
669 | |
670 | void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level, |
671 | const Diagnostic &Info) { |
672 | |
673 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
674 | |
675 | |
676 | |
677 | |
678 | if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) { |
679 | StoredDiagnostic *ResultDiag = nullptr; |
680 | if (StoredDiags) { |
681 | StoredDiags->emplace_back(Level, Info); |
682 | ResultDiag = &StoredDiags->back(); |
683 | } |
684 | |
685 | if (StandaloneDiags) { |
686 | llvm::Optional<StoredDiagnostic> StoredDiag = None; |
687 | if (!ResultDiag) { |
688 | StoredDiag.emplace(Level, Info); |
689 | ResultDiag = StoredDiag.getPointer(); |
690 | } |
691 | StandaloneDiags->push_back( |
692 | makeStandaloneDiagnostic(*LangOpts, *ResultDiag)); |
693 | } |
694 | } |
695 | } |
696 | |
697 | IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const { |
698 | return Reader; |
699 | } |
700 | |
701 | ASTMutationListener *ASTUnit::getASTMutationListener() { |
702 | if (WriterData) |
703 | return &WriterData->Writer; |
704 | return nullptr; |
705 | } |
706 | |
707 | ASTDeserializationListener *ASTUnit::getDeserializationListener() { |
708 | if (WriterData) |
709 | return &WriterData->Writer; |
710 | return nullptr; |
711 | } |
712 | |
713 | std::unique_ptr<llvm::MemoryBuffer> |
714 | ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) { |
715 | assert(FileMgr); |
716 | auto Buffer = FileMgr->getBufferForFile(Filename, UserFilesAreVolatile); |
717 | if (Buffer) |
718 | return std::move(*Buffer); |
719 | if (ErrorStr) |
720 | *ErrorStr = Buffer.getError().message(); |
721 | return nullptr; |
722 | } |
723 | |
724 | |
725 | void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
726 | ASTUnit &AST, bool CaptureDiagnostics) { |
727 | (0) . __assert_fail ("Diags.get() && \"no DiagnosticsEngine was provided\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 727, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Diags.get() && "no DiagnosticsEngine was provided"); |
728 | if (CaptureDiagnostics) |
729 | Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr)); |
730 | } |
731 | |
732 | std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile( |
733 | const std::string &Filename, const PCHContainerReader &PCHContainerRdr, |
734 | WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
735 | const FileSystemOptions &FileSystemOpts, bool UseDebugInfo, |
736 | bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles, |
737 | bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors, |
738 | bool UserFilesAreVolatile) { |
739 | std::unique_ptr<ASTUnit> AST(new ASTUnit(true)); |
740 | |
741 | |
742 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
743 | ASTUnitCleanup(AST.get()); |
744 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
745 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>> |
746 | DiagCleanup(Diags.get()); |
747 | |
748 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
749 | |
750 | AST->LangOpts = std::make_shared<LangOptions>(); |
751 | AST->OnlyLocalDecls = OnlyLocalDecls; |
752 | AST->CaptureDiagnostics = CaptureDiagnostics; |
753 | AST->Diagnostics = Diags; |
754 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = |
755 | llvm::vfs::getRealFileSystem(); |
756 | AST->FileMgr = new FileManager(FileSystemOpts, VFS); |
757 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
758 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), |
759 | AST->getFileManager(), |
760 | UserFilesAreVolatile); |
761 | AST->ModuleCache = new InMemoryModuleCache; |
762 | AST->HSOpts = std::make_shared<HeaderSearchOptions>(); |
763 | AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat(); |
764 | AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts, |
765 | AST->getSourceManager(), |
766 | AST->getDiagnostics(), |
767 | AST->getLangOpts(), |
768 | )); |
769 | AST->PPOpts = std::make_shared<PreprocessorOptions>(); |
770 | |
771 | for (const auto &RemappedFile : RemappedFiles) |
772 | AST->PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second); |
773 | |
774 | |
775 | |
776 | HeaderSearch & = *AST->HeaderInfo; |
777 | unsigned Counter; |
778 | |
779 | AST->PP = std::make_shared<Preprocessor>( |
780 | AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts, |
781 | AST->getSourceManager(), HeaderInfo, AST->ModuleLoader, |
782 | , |
783 | ); |
784 | Preprocessor &PP = *AST->PP; |
785 | |
786 | if (ToLoad >= LoadASTOnly) |
787 | AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(), |
788 | PP.getIdentifierTable(), PP.getSelectorTable(), |
789 | PP.getBuiltinInfo()); |
790 | |
791 | bool disableValid = false; |
792 | if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) |
793 | disableValid = true; |
794 | AST->Reader = new ASTReader( |
795 | PP, *AST->ModuleCache, AST->Ctx.get(), PCHContainerRdr, {}, |
796 | , |
797 | disableValid, AllowPCHWithCompilerErrors); |
798 | |
799 | AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>( |
800 | *AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts, |
801 | AST->TargetOpts, AST->Target, Counter)); |
802 | |
803 | |
804 | |
805 | |
806 | |
807 | |
808 | if (AST->Ctx) |
809 | AST->Ctx->setExternalSource(AST->Reader); |
810 | |
811 | switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile, |
812 | SourceLocation(), ASTReader::ARR_None)) { |
813 | case ASTReader::Success: |
814 | break; |
815 | |
816 | case ASTReader::Failure: |
817 | case ASTReader::Missing: |
818 | case ASTReader::OutOfDate: |
819 | case ASTReader::VersionMismatch: |
820 | case ASTReader::ConfigurationMismatch: |
821 | case ASTReader::HadErrors: |
822 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
823 | return nullptr; |
824 | } |
825 | |
826 | AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile(); |
827 | |
828 | PP.setCounterValue(Counter); |
829 | |
830 | |
831 | if (ToLoad >= LoadASTOnly) |
832 | AST->Consumer.reset(new ASTConsumer); |
833 | |
834 | |
835 | if (ToLoad >= LoadEverything) { |
836 | AST->TheSema.reset(new Sema(PP, *AST->Ctx, *AST->Consumer)); |
837 | AST->TheSema->Initialize(); |
838 | AST->Reader->InitializeSema(*AST->TheSema); |
839 | } |
840 | |
841 | |
842 | AST->getDiagnostics().getClient()->BeginSourceFile(PP.getLangOpts(), &PP); |
843 | |
844 | return AST; |
845 | } |
846 | |
847 | |
848 | static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash) { |
849 | Hash = llvm::djbHash(MacroNameTok.getIdentifierInfo()->getName(), Hash); |
850 | } |
851 | |
852 | namespace { |
853 | |
854 | |
855 | |
856 | class MacroDefinitionTrackerPPCallbacks : public PPCallbacks { |
857 | unsigned &Hash; |
858 | |
859 | public: |
860 | explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {} |
861 | |
862 | void MacroDefined(const Token &MacroNameTok, |
863 | const MacroDirective *MD) override { |
864 | AddDefinedMacroToHash(MacroNameTok, Hash); |
865 | } |
866 | }; |
867 | |
868 | } |
869 | |
870 | |
871 | static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) { |
872 | if (!D) |
873 | return; |
874 | |
875 | DeclContext *DC = D->getDeclContext(); |
876 | if (!DC) |
877 | return; |
878 | |
879 | if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit())) |
880 | return; |
881 | |
882 | if (const auto *ND = dyn_cast<NamedDecl>(D)) { |
883 | if (const auto *EnumD = dyn_cast<EnumDecl>(D)) { |
884 | |
885 | |
886 | if (!EnumD->isScoped()) { |
887 | for (const auto *EI : EnumD->enumerators()) { |
888 | if (EI->getIdentifier()) |
889 | Hash = llvm::djbHash(EI->getIdentifier()->getName(), Hash); |
890 | } |
891 | } |
892 | } |
893 | |
894 | if (ND->getIdentifier()) |
895 | Hash = llvm::djbHash(ND->getIdentifier()->getName(), Hash); |
896 | else if (DeclarationName Name = ND->getDeclName()) { |
897 | std::string NameStr = Name.getAsString(); |
898 | Hash = llvm::djbHash(NameStr, Hash); |
899 | } |
900 | return; |
901 | } |
902 | |
903 | if (const auto *ImportD = dyn_cast<ImportDecl>(D)) { |
904 | if (const Module *Mod = ImportD->getImportedModule()) { |
905 | std::string ModName = Mod->getFullModuleName(); |
906 | Hash = llvm::djbHash(ModName, Hash); |
907 | } |
908 | return; |
909 | } |
910 | } |
911 | |
912 | namespace { |
913 | |
914 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
915 | ASTUnit &Unit; |
916 | unsigned &Hash; |
917 | |
918 | public: |
919 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash) |
920 | : Unit(_Unit), Hash(Hash) { |
921 | Hash = 0; |
922 | } |
923 | |
924 | void handleTopLevelDecl(Decl *D) { |
925 | if (!D) |
926 | return; |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | if (isa<ObjCMethodDecl>(D)) |
933 | return; |
934 | |
935 | AddTopLevelDeclarationToHash(D, Hash); |
936 | Unit.addTopLevelDecl(D); |
937 | |
938 | handleFileLevelDecl(D); |
939 | } |
940 | |
941 | void handleFileLevelDecl(Decl *D) { |
942 | Unit.addFileLevelDecl(D); |
943 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
944 | for (auto *I : NSD->decls()) |
945 | handleFileLevelDecl(I); |
946 | } |
947 | } |
948 | |
949 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
950 | for (auto *TopLevelDecl : D) |
951 | handleTopLevelDecl(TopLevelDecl); |
952 | return true; |
953 | } |
954 | |
955 | |
956 | void HandleInterestingDecl(DeclGroupRef) override {} |
957 | |
958 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
959 | for (auto *TopLevelDecl : D) |
960 | handleTopLevelDecl(TopLevelDecl); |
961 | } |
962 | |
963 | ASTMutationListener *GetASTMutationListener() override { |
964 | return Unit.getASTMutationListener(); |
965 | } |
966 | |
967 | ASTDeserializationListener *GetASTDeserializationListener() override { |
968 | return Unit.getDeserializationListener(); |
969 | } |
970 | }; |
971 | |
972 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
973 | public: |
974 | ASTUnit &Unit; |
975 | |
976 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
977 | StringRef InFile) override { |
978 | CI.getPreprocessor().addPPCallbacks( |
979 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
980 | Unit.getCurrentTopLevelHashValue())); |
981 | return llvm::make_unique<TopLevelDeclTrackerConsumer>( |
982 | Unit, Unit.getCurrentTopLevelHashValue()); |
983 | } |
984 | |
985 | public: |
986 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
987 | |
988 | bool hasCodeCompletionSupport() const override { return false; } |
989 | |
990 | TranslationUnitKind getTranslationUnitKind() override { |
991 | return Unit.getTranslationUnitKind(); |
992 | } |
993 | }; |
994 | |
995 | class ASTUnitPreambleCallbacks : public PreambleCallbacks { |
996 | public: |
997 | unsigned getHash() const { return Hash; } |
998 | |
999 | std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); } |
1000 | |
1001 | std::vector<serialization::DeclID> takeTopLevelDeclIDs() { |
1002 | return std::move(TopLevelDeclIDs); |
1003 | } |
1004 | |
1005 | void AfterPCHEmitted(ASTWriter &Writer) override { |
1006 | TopLevelDeclIDs.reserve(TopLevelDecls.size()); |
1007 | for (const auto *D : TopLevelDecls) { |
1008 | |
1009 | if (D->isInvalidDecl()) |
1010 | continue; |
1011 | TopLevelDeclIDs.push_back(Writer.getDeclID(D)); |
1012 | } |
1013 | } |
1014 | |
1015 | void HandleTopLevelDecl(DeclGroupRef DG) override { |
1016 | for (auto *D : DG) { |
1017 | |
1018 | |
1019 | |
1020 | |
1021 | if (isa<ObjCMethodDecl>(D)) |
1022 | continue; |
1023 | AddTopLevelDeclarationToHash(D, Hash); |
1024 | TopLevelDecls.push_back(D); |
1025 | } |
1026 | } |
1027 | |
1028 | std::unique_ptr<PPCallbacks> createPPCallbacks() override { |
1029 | return llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(Hash); |
1030 | } |
1031 | |
1032 | private: |
1033 | unsigned Hash = 0; |
1034 | std::vector<Decl *> TopLevelDecls; |
1035 | std::vector<serialization::DeclID> TopLevelDeclIDs; |
1036 | llvm::SmallVector<ASTUnit::StandaloneDiagnostic, 4> PreambleDiags; |
1037 | }; |
1038 | |
1039 | } |
1040 | |
1041 | static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) { |
1042 | return StoredDiag.getLocation().isValid(); |
1043 | } |
1044 | |
1045 | static void |
1046 | checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) { |
1047 | |
1048 | |
1049 | StoredDiags.erase( |
1050 | std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag), |
1051 | StoredDiags.end()); |
1052 | } |
1053 | |
1054 | static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> & |
1055 | StoredDiagnostics, |
1056 | SourceManager &SM) { |
1057 | |
1058 | |
1059 | |
1060 | |
1061 | |
1062 | for (auto &SD : StoredDiagnostics) { |
1063 | if (SD.getLocation().isValid()) { |
1064 | FullSourceLoc Loc(SD.getLocation(), SM); |
1065 | SD.setLocation(Loc); |
1066 | } |
1067 | } |
1068 | } |
1069 | |
1070 | |
1071 | |
1072 | |
1073 | |
1074 | |
1075 | bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1076 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer, |
1077 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
1078 | if (!Invocation) |
1079 | return true; |
1080 | |
1081 | if (VFS && FileMgr) |
1082 | (0) . __assert_fail ("VFS == &FileMgr->getVirtualFileSystem() && \"VFS passed to Parse and VFS in FileMgr are different\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1083, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VFS == &FileMgr->getVirtualFileSystem() && |
1083 | (0) . __assert_fail ("VFS == &FileMgr->getVirtualFileSystem() && \"VFS passed to Parse and VFS in FileMgr are different\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1083, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "VFS passed to Parse and VFS in FileMgr are different"); |
1084 | |
1085 | auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation); |
1086 | if (OverrideMainBuffer) { |
1087 | (0) . __assert_fail ("Preamble && \"No preamble was built, but OverrideMainBuffer is not null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1088, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Preamble && |
1088 | (0) . __assert_fail ("Preamble && \"No preamble was built, but OverrideMainBuffer is not null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1088, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No preamble was built, but OverrideMainBuffer is not null"); |
1089 | Preamble->AddImplicitPreamble(*CCInvocation, VFS, OverrideMainBuffer.get()); |
1090 | |
1091 | } |
1092 | |
1093 | |
1094 | std::unique_ptr<CompilerInstance> Clang( |
1095 | new CompilerInstance(std::move(PCHContainerOps))); |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS) |
1101 | Clang->setFileManager(&*FileMgr); |
1102 | else |
1103 | FileMgr = Clang->createFileManager(std::move(VFS)); |
1104 | |
1105 | |
1106 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
1107 | CICleanup(Clang.get()); |
1108 | |
1109 | Clang->setInvocation(CCInvocation); |
1110 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
1111 | |
1112 | |
1113 | |
1114 | Clang->setDiagnostics(&getDiagnostics()); |
1115 | |
1116 | |
1117 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
1118 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
1119 | if (!Clang->hasTarget()) |
1120 | return true; |
1121 | |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | Clang->getTarget().adjust(Clang->getLangOpts()); |
1127 | |
1128 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1129, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
1129 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1129, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invocation must have exactly one source file!"); |
1130 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1132, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
1131 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1132, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::Source && |
1132 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1132, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "FIXME: AST inputs not yet supported here!"); |
1133 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1135, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
1134 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1135, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::LLVM_IR && |
1135 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1135, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "IR inputs not support here!"); |
1136 | |
1137 | |
1138 | LangOpts = Clang->getInvocation().LangOpts; |
1139 | FileSystemOpts = Clang->getFileSystemOpts(); |
1140 | |
1141 | ResetForParse(); |
1142 | |
1143 | SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, |
1144 | UserFilesAreVolatile); |
1145 | if (!OverrideMainBuffer) { |
1146 | checkAndRemoveNonDriverDiags(StoredDiagnostics); |
1147 | TopLevelDeclsInPreamble.clear(); |
1148 | } |
1149 | |
1150 | |
1151 | Clang->setFileManager(&getFileManager()); |
1152 | |
1153 | |
1154 | Clang->setSourceManager(&getSourceManager()); |
1155 | |
1156 | |
1157 | |
1158 | if (OverrideMainBuffer) { |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | |
1164 | checkAndSanitizeDiags(StoredDiagnostics, getSourceManager()); |
1165 | |
1166 | |
1167 | SavedMainFileBuffer = std::move(OverrideMainBuffer); |
1168 | } |
1169 | |
1170 | std::unique_ptr<TopLevelDeclTrackerAction> Act( |
1171 | new TopLevelDeclTrackerAction(*this)); |
1172 | |
1173 | |
1174 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
1175 | ActCleanup(Act.get()); |
1176 | |
1177 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) |
1178 | goto error; |
1179 | |
1180 | if (SavedMainFileBuffer) |
1181 | TranslateStoredDiagnostics(getFileManager(), getSourceManager(), |
1182 | PreambleDiagnostics, StoredDiagnostics); |
1183 | else |
1184 | PreambleSrcLocCache.clear(); |
1185 | |
1186 | if (!Act->Execute()) |
1187 | goto error; |
1188 | |
1189 | transferASTDataFromCompilerInstance(*Clang); |
1190 | |
1191 | Act->EndSourceFile(); |
1192 | |
1193 | FailedParseDiagnostics.clear(); |
1194 | |
1195 | return false; |
1196 | |
1197 | error: |
1198 | |
1199 | SavedMainFileBuffer = nullptr; |
1200 | |
1201 | |
1202 | |
1203 | transferASTDataFromCompilerInstance(*Clang); |
1204 | FailedParseDiagnostics.swap(StoredDiagnostics); |
1205 | StoredDiagnostics.clear(); |
1206 | NumStoredDiagnosticsFromDriver = 0; |
1207 | return true; |
1208 | } |
1209 | |
1210 | static std::pair<unsigned, unsigned> |
1211 | makeStandaloneRange(CharSourceRange Range, const SourceManager &SM, |
1212 | const LangOptions &LangOpts) { |
1213 | CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts); |
1214 | unsigned Offset = SM.getFileOffset(FileRange.getBegin()); |
1215 | unsigned EndOffset = SM.getFileOffset(FileRange.getEnd()); |
1216 | return std::make_pair(Offset, EndOffset); |
1217 | } |
1218 | |
1219 | static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, |
1220 | const LangOptions &LangOpts, |
1221 | const FixItHint &InFix) { |
1222 | ASTUnit::StandaloneFixIt OutFix; |
1223 | OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts); |
1224 | OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM, |
1225 | LangOpts); |
1226 | OutFix.CodeToInsert = InFix.CodeToInsert; |
1227 | OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions; |
1228 | return OutFix; |
1229 | } |
1230 | |
1231 | static ASTUnit::StandaloneDiagnostic |
1232 | makeStandaloneDiagnostic(const LangOptions &LangOpts, |
1233 | const StoredDiagnostic &InDiag) { |
1234 | ASTUnit::StandaloneDiagnostic OutDiag; |
1235 | OutDiag.ID = InDiag.getID(); |
1236 | OutDiag.Level = InDiag.getLevel(); |
1237 | OutDiag.Message = InDiag.getMessage(); |
1238 | OutDiag.LocOffset = 0; |
1239 | if (InDiag.getLocation().isInvalid()) |
1240 | return OutDiag; |
1241 | const SourceManager &SM = InDiag.getLocation().getManager(); |
1242 | SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation()); |
1243 | OutDiag.Filename = SM.getFilename(FileLoc); |
1244 | if (OutDiag.Filename.empty()) |
1245 | return OutDiag; |
1246 | OutDiag.LocOffset = SM.getFileOffset(FileLoc); |
1247 | for (const auto &Range : InDiag.getRanges()) |
1248 | OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts)); |
1249 | for (const auto &FixIt : InDiag.getFixIts()) |
1250 | OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt)); |
1251 | |
1252 | return OutDiag; |
1253 | } |
1254 | |
1255 | |
1256 | |
1257 | |
1258 | |
1259 | |
1260 | |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | |
1274 | |
1275 | std::unique_ptr<llvm::MemoryBuffer> |
1276 | ASTUnit::getMainBufferWithPrecompiledPreamble( |
1277 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1278 | CompilerInvocation &PreambleInvocationIn, |
1279 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild, |
1280 | unsigned MaxLines) { |
1281 | auto MainFilePath = |
1282 | PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile(); |
1283 | std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer = |
1284 | getBufferForFileHandlingRemapping(PreambleInvocationIn, VFS.get(), |
1285 | MainFilePath, UserFilesAreVolatile); |
1286 | if (!MainFileBuffer) |
1287 | return nullptr; |
1288 | |
1289 | PreambleBounds Bounds = |
1290 | ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(), |
1291 | MainFileBuffer.get(), MaxLines); |
1292 | if (!Bounds.Size) |
1293 | return nullptr; |
1294 | |
1295 | if (Preamble) { |
1296 | if (Preamble->CanReuse(PreambleInvocationIn, MainFileBuffer.get(), Bounds, |
1297 | VFS.get())) { |
1298 | |
1299 | |
1300 | |
1301 | |
1302 | getDiagnostics().Reset(); |
1303 | ProcessWarningOptions(getDiagnostics(), |
1304 | PreambleInvocationIn.getDiagnosticOpts()); |
1305 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
1306 | |
1307 | PreambleRebuildCounter = 1; |
1308 | return MainFileBuffer; |
1309 | } else { |
1310 | Preamble.reset(); |
1311 | PreambleDiagnostics.clear(); |
1312 | TopLevelDeclsInPreamble.clear(); |
1313 | PreambleSrcLocCache.clear(); |
1314 | PreambleRebuildCounter = 1; |
1315 | } |
1316 | } |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | if (PreambleRebuildCounter > 1) { |
1322 | --PreambleRebuildCounter; |
1323 | return nullptr; |
1324 | } |
1325 | |
1326 | (0) . __assert_fail ("!Preamble && \"No Preamble should be stored at that point\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1326, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Preamble && "No Preamble should be stored at that point"); |
1327 | |
1328 | |
1329 | if (!AllowRebuild) |
1330 | return nullptr; |
1331 | |
1332 | SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone; |
1333 | SmallVector<StoredDiagnostic, 4> NewPreambleDiags; |
1334 | ASTUnitPreambleCallbacks Callbacks; |
1335 | { |
1336 | llvm::Optional<CaptureDroppedDiagnostics> Capture; |
1337 | if (CaptureDiagnostics) |
1338 | Capture.emplace(, *Diagnostics, &NewPreambleDiags, |
1339 | &NewPreambleDiagsStandalone); |
1340 | |
1341 | |
1342 | SimpleTimer PreambleTimer(WantTiming); |
1343 | PreambleTimer.setOutput("Precompiling preamble"); |
1344 | |
1345 | const bool PreviousSkipFunctionBodies = |
1346 | PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies; |
1347 | if (SkipFunctionBodies == SkipFunctionBodiesScope::Preamble) |
1348 | PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = true; |
1349 | |
1350 | llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build( |
1351 | PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS, |
1352 | PCHContainerOps, , Callbacks); |
1353 | |
1354 | PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = |
1355 | PreviousSkipFunctionBodies; |
1356 | |
1357 | if (NewPreamble) { |
1358 | Preamble = std::move(*NewPreamble); |
1359 | PreambleRebuildCounter = 1; |
1360 | } else { |
1361 | switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) { |
1362 | case BuildPreambleError::CouldntCreateTempFile: |
1363 | |
1364 | PreambleRebuildCounter = 1; |
1365 | return nullptr; |
1366 | case BuildPreambleError::CouldntCreateTargetInfo: |
1367 | case BuildPreambleError::BeginSourceFileFailed: |
1368 | case BuildPreambleError::CouldntEmitPCH: |
1369 | |
1370 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
1371 | return nullptr; |
1372 | } |
1373 | llvm_unreachable("unexpected BuildPreambleError"); |
1374 | } |
1375 | } |
1376 | |
1377 | (0) . __assert_fail ("Preamble && \"Preamble wasn't built\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1377, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Preamble && "Preamble wasn't built"); |
1378 | |
1379 | TopLevelDecls.clear(); |
1380 | TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs(); |
1381 | PreambleTopLevelHashValue = Callbacks.getHash(); |
1382 | |
1383 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
1384 | |
1385 | checkAndRemoveNonDriverDiags(NewPreambleDiags); |
1386 | StoredDiagnostics = std::move(NewPreambleDiags); |
1387 | PreambleDiagnostics = std::move(NewPreambleDiagsStandalone); |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) { |
1393 | CompletionCacheTopLevelHashValue = 0; |
1394 | PreambleTopLevelHashValue = CurrentTopLevelHashValue; |
1395 | } |
1396 | |
1397 | return MainFileBuffer; |
1398 | } |
1399 | |
1400 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
1401 | (0) . __assert_fail ("Preamble && \"Should only be called when preamble was built\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1401, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Preamble && "Should only be called when preamble was built"); |
1402 | |
1403 | std::vector<Decl *> Resolved; |
1404 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
1405 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
1406 | for (const auto TopLevelDecl : TopLevelDeclsInPreamble) { |
1407 | |
1408 | |
1409 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
1410 | Resolved.push_back(D); |
1411 | } |
1412 | TopLevelDeclsInPreamble.clear(); |
1413 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
1414 | } |
1415 | |
1416 | void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) { |
1417 | |
1418 | |
1419 | (0) . __assert_fail ("CI.hasInvocation() && \"missing invocation\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1419, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CI.hasInvocation() && "missing invocation"); |
1420 | LangOpts = CI.getInvocation().LangOpts; |
1421 | TheSema = CI.takeSema(); |
1422 | Consumer = CI.takeASTConsumer(); |
1423 | if (CI.hasASTContext()) |
1424 | Ctx = &CI.getASTContext(); |
1425 | if (CI.hasPreprocessor()) |
1426 | PP = CI.getPreprocessorPtr(); |
1427 | CI.setSourceManager(nullptr); |
1428 | CI.setFileManager(nullptr); |
1429 | if (CI.hasTarget()) |
1430 | Target = &CI.getTarget(); |
1431 | Reader = CI.getModuleManager(); |
1432 | HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure(); |
1433 | } |
1434 | |
1435 | StringRef ASTUnit::getMainFileName() const { |
1436 | if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) { |
1437 | const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0]; |
1438 | if (Input.isFile()) |
1439 | return Input.getFile(); |
1440 | else |
1441 | return Input.getBuffer()->getBufferIdentifier(); |
1442 | } |
1443 | |
1444 | if (SourceMgr) { |
1445 | if (const FileEntry * |
1446 | FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID())) |
1447 | return FE->getName(); |
1448 | } |
1449 | |
1450 | return {}; |
1451 | } |
1452 | |
1453 | StringRef ASTUnit::getASTFileName() const { |
1454 | if (!isMainFileAST()) |
1455 | return {}; |
1456 | |
1457 | serialization::ModuleFile & |
1458 | Mod = Reader->getModuleManager().getPrimaryModule(); |
1459 | return Mod.FileName; |
1460 | } |
1461 | |
1462 | std::unique_ptr<ASTUnit> |
1463 | ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, |
1464 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
1465 | bool CaptureDiagnostics, bool UserFilesAreVolatile) { |
1466 | std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); |
1467 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
1468 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = |
1469 | createVFSFromCompilerInvocation(*CI, *Diags); |
1470 | AST->Diagnostics = Diags; |
1471 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
1472 | AST->Invocation = std::move(CI); |
1473 | AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); |
1474 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
1475 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr, |
1476 | UserFilesAreVolatile); |
1477 | AST->ModuleCache = new InMemoryModuleCache; |
1478 | |
1479 | return AST; |
1480 | } |
1481 | |
1482 | ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( |
1483 | std::shared_ptr<CompilerInvocation> CI, |
1484 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1485 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action, |
1486 | ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath, |
1487 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
1488 | unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults, |
1489 | bool , bool UserFilesAreVolatile, |
1490 | std::unique_ptr<ASTUnit> *ErrAST) { |
1491 | (0) . __assert_fail ("CI && \"A CompilerInvocation is required\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1491, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CI && "A CompilerInvocation is required"); |
1492 | |
1493 | std::unique_ptr<ASTUnit> OwnAST; |
1494 | ASTUnit *AST = Unit; |
1495 | if (!AST) { |
1496 | |
1497 | OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile); |
1498 | AST = OwnAST.get(); |
1499 | if (!AST) |
1500 | return nullptr; |
1501 | } |
1502 | |
1503 | if (!ResourceFilesPath.empty()) { |
1504 | |
1505 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
1506 | } |
1507 | AST->OnlyLocalDecls = OnlyLocalDecls; |
1508 | AST->CaptureDiagnostics = CaptureDiagnostics; |
1509 | if (PrecompilePreambleAfterNParses > 0) |
1510 | AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses; |
1511 | AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete; |
1512 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
1513 | AST->IncludeBriefCommentsInCodeCompletion |
1514 | = IncludeBriefCommentsInCodeCompletion; |
1515 | |
1516 | |
1517 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
1518 | ASTUnitCleanup(OwnAST.get()); |
1519 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
1520 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>> |
1521 | DiagCleanup(Diags.get()); |
1522 | |
1523 | |
1524 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
1525 | CI->getFrontendOpts().DisableFree = false; |
1526 | ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts()); |
1527 | |
1528 | |
1529 | std::unique_ptr<CompilerInstance> Clang( |
1530 | new CompilerInstance(std::move(PCHContainerOps))); |
1531 | |
1532 | |
1533 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
1534 | CICleanup(Clang.get()); |
1535 | |
1536 | Clang->setInvocation(std::move(CI)); |
1537 | AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
1538 | |
1539 | |
1540 | |
1541 | Clang->setDiagnostics(&AST->getDiagnostics()); |
1542 | |
1543 | |
1544 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
1545 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
1546 | if (!Clang->hasTarget()) |
1547 | return nullptr; |
1548 | |
1549 | |
1550 | |
1551 | |
1552 | |
1553 | Clang->getTarget().adjust(Clang->getLangOpts()); |
1554 | |
1555 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1556, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
1556 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1556, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invocation must have exactly one source file!"); |
1557 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1559, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
1558 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1559, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::Source && |
1559 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1559, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "FIXME: AST inputs not yet supported here!"); |
1560 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1562, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
1561 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1562, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::LLVM_IR && |
1562 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1562, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "IR inputs not support here!"); |
1563 | |
1564 | |
1565 | AST->TheSema.reset(); |
1566 | AST->Ctx = nullptr; |
1567 | AST->PP = nullptr; |
1568 | AST->Reader = nullptr; |
1569 | |
1570 | |
1571 | Clang->setFileManager(&AST->getFileManager()); |
1572 | |
1573 | |
1574 | Clang->setSourceManager(&AST->getSourceManager()); |
1575 | |
1576 | FrontendAction *Act = Action; |
1577 | |
1578 | std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct; |
1579 | if (!Act) { |
1580 | TrackerAct.reset(new TopLevelDeclTrackerAction(*AST)); |
1581 | Act = TrackerAct.get(); |
1582 | } |
1583 | |
1584 | |
1585 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
1586 | ActCleanup(TrackerAct.get()); |
1587 | |
1588 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
1589 | AST->transferASTDataFromCompilerInstance(*Clang); |
1590 | if (OwnAST && ErrAST) |
1591 | ErrAST->swap(OwnAST); |
1592 | |
1593 | return nullptr; |
1594 | } |
1595 | |
1596 | if (Persistent && !TrackerAct) { |
1597 | Clang->getPreprocessor().addPPCallbacks( |
1598 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
1599 | AST->getCurrentTopLevelHashValue())); |
1600 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
1601 | if (Clang->hasASTConsumer()) |
1602 | Consumers.push_back(Clang->takeASTConsumer()); |
1603 | Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>( |
1604 | *AST, AST->getCurrentTopLevelHashValue())); |
1605 | Clang->setASTConsumer( |
1606 | llvm::make_unique<MultiplexConsumer>(std::move(Consumers))); |
1607 | } |
1608 | if (!Act->Execute()) { |
1609 | AST->transferASTDataFromCompilerInstance(*Clang); |
1610 | if (OwnAST && ErrAST) |
1611 | ErrAST->swap(OwnAST); |
1612 | |
1613 | return nullptr; |
1614 | } |
1615 | |
1616 | |
1617 | AST->transferASTDataFromCompilerInstance(*Clang); |
1618 | |
1619 | Act->EndSourceFile(); |
1620 | |
1621 | if (OwnAST) |
1622 | return OwnAST.release(); |
1623 | else |
1624 | return AST; |
1625 | } |
1626 | |
1627 | bool ASTUnit::LoadFromCompilerInvocation( |
1628 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1629 | unsigned PrecompilePreambleAfterNParses, |
1630 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
1631 | if (!Invocation) |
1632 | return true; |
1633 | |
1634 | (0) . __assert_fail ("VFS && \"VFS is null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1634, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VFS && "VFS is null"); |
1635 | |
1636 | |
1637 | Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
1638 | Invocation->getFrontendOpts().DisableFree = false; |
1639 | getDiagnostics().Reset(); |
1640 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
1641 | |
1642 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
1643 | if (PrecompilePreambleAfterNParses > 0) { |
1644 | PreambleRebuildCounter = PrecompilePreambleAfterNParses; |
1645 | OverrideMainBuffer = |
1646 | getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS); |
1647 | getDiagnostics().Reset(); |
1648 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
1649 | } |
1650 | |
1651 | SimpleTimer ParsingTimer(WantTiming); |
1652 | ParsingTimer.setOutput("Parsing " + getMainFileName()); |
1653 | |
1654 | |
1655 | llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer> |
1656 | MemBufferCleanup(OverrideMainBuffer.get()); |
1657 | |
1658 | return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS); |
1659 | } |
1660 | |
1661 | std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation( |
1662 | std::shared_ptr<CompilerInvocation> CI, |
1663 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1664 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr, |
1665 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
1666 | unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, |
1667 | bool CacheCodeCompletionResults, bool , |
1668 | bool UserFilesAreVolatile) { |
1669 | |
1670 | std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); |
1671 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
1672 | AST->Diagnostics = Diags; |
1673 | AST->OnlyLocalDecls = OnlyLocalDecls; |
1674 | AST->CaptureDiagnostics = CaptureDiagnostics; |
1675 | AST->TUKind = TUKind; |
1676 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
1677 | AST->IncludeBriefCommentsInCodeCompletion |
1678 | = IncludeBriefCommentsInCodeCompletion; |
1679 | AST->Invocation = std::move(CI); |
1680 | AST->FileSystemOpts = FileMgr->getFileSystemOpts(); |
1681 | AST->FileMgr = FileMgr; |
1682 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
1683 | |
1684 | |
1685 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
1686 | ASTUnitCleanup(AST.get()); |
1687 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
1688 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>> |
1689 | DiagCleanup(Diags.get()); |
1690 | |
1691 | if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), |
1692 | PrecompilePreambleAfterNParses, |
1693 | &AST->FileMgr->getVirtualFileSystem())) |
1694 | return nullptr; |
1695 | return AST; |
1696 | } |
1697 | |
1698 | ASTUnit *ASTUnit::LoadFromCommandLine( |
1699 | const char **ArgBegin, const char **ArgEnd, |
1700 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1701 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath, |
1702 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
1703 | ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName, |
1704 | unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, |
1705 | bool CacheCodeCompletionResults, bool , |
1706 | bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies, |
1707 | bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization, |
1708 | llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST, |
1709 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
1710 | (0) . __assert_fail ("Diags.get() && \"no DiagnosticsEngine was provided\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1710, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Diags.get() && "no DiagnosticsEngine was provided"); |
1711 | |
1712 | SmallVector<StoredDiagnostic, 4> StoredDiagnostics; |
1713 | |
1714 | std::shared_ptr<CompilerInvocation> CI; |
1715 | |
1716 | { |
1717 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags, |
1718 | &StoredDiagnostics, nullptr); |
1719 | |
1720 | CI = createInvocationFromCommandLine( |
1721 | llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS); |
1722 | if (!CI) |
1723 | return nullptr; |
1724 | } |
1725 | |
1726 | |
1727 | for (const auto &RemappedFile : RemappedFiles) { |
1728 | CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first, |
1729 | RemappedFile.second); |
1730 | } |
1731 | PreprocessorOptions &PPOpts = CI->getPreprocessorOpts(); |
1732 | PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName; |
1733 | PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors; |
1734 | PPOpts.SingleFileParseMode = SingleFileParse; |
1735 | |
1736 | |
1737 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
1738 | |
1739 | CI->getFrontendOpts().SkipFunctionBodies = |
1740 | SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile; |
1741 | |
1742 | if (ModuleFormat) |
1743 | CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue(); |
1744 | |
1745 | |
1746 | std::unique_ptr<ASTUnit> AST; |
1747 | AST.reset(new ASTUnit(false)); |
1748 | AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); |
1749 | AST->StoredDiagnostics.swap(StoredDiagnostics); |
1750 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
1751 | AST->Diagnostics = Diags; |
1752 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
1753 | if (!VFS) |
1754 | VFS = llvm::vfs::getRealFileSystem(); |
1755 | VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS); |
1756 | AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); |
1757 | AST->ModuleCache = new InMemoryModuleCache; |
1758 | AST->OnlyLocalDecls = OnlyLocalDecls; |
1759 | AST->CaptureDiagnostics = CaptureDiagnostics; |
1760 | AST->TUKind = TUKind; |
1761 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
1762 | AST->IncludeBriefCommentsInCodeCompletion |
1763 | = IncludeBriefCommentsInCodeCompletion; |
1764 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
1765 | AST->Invocation = CI; |
1766 | AST->SkipFunctionBodies = SkipFunctionBodies; |
1767 | if (ForSerialization) |
1768 | AST->WriterData.reset(new ASTWriterData(*AST->ModuleCache)); |
1769 | |
1770 | CI = nullptr; |
1771 | Diags = nullptr; |
1772 | |
1773 | |
1774 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
1775 | ASTUnitCleanup(AST.get()); |
1776 | |
1777 | if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), |
1778 | PrecompilePreambleAfterNParses, |
1779 | VFS)) { |
1780 | |
1781 | |
1782 | if (ErrAST) { |
1783 | AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics); |
1784 | ErrAST->swap(AST); |
1785 | } |
1786 | return nullptr; |
1787 | } |
1788 | |
1789 | return AST.release(); |
1790 | } |
1791 | |
1792 | bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
1793 | ArrayRef<RemappedFile> RemappedFiles, |
1794 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
1795 | if (!Invocation) |
1796 | return true; |
1797 | |
1798 | if (!VFS) { |
1799 | (0) . __assert_fail ("FileMgr && \"FileMgr is null on Reparse call\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 1799, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FileMgr && "FileMgr is null on Reparse call"); |
1800 | VFS = &FileMgr->getVirtualFileSystem(); |
1801 | } |
1802 | |
1803 | clearFileLevelDecls(); |
1804 | |
1805 | SimpleTimer ParsingTimer(WantTiming); |
1806 | ParsingTimer.setOutput("Reparsing " + getMainFileName()); |
1807 | |
1808 | |
1809 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
1810 | for (const auto &RB : PPOpts.RemappedFileBuffers) |
1811 | delete RB.second; |
1812 | |
1813 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
1814 | for (const auto &RemappedFile : RemappedFiles) { |
1815 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first, |
1816 | RemappedFile.second); |
1817 | } |
1818 | |
1819 | |
1820 | |
1821 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
1822 | if (Preamble || PreambleRebuildCounter > 0) |
1823 | OverrideMainBuffer = |
1824 | getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS); |
1825 | |
1826 | |
1827 | FileMgr.reset(); |
1828 | getDiagnostics().Reset(); |
1829 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
1830 | if (OverrideMainBuffer) |
1831 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
1832 | |
1833 | |
1834 | bool Result = |
1835 | Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS); |
1836 | |
1837 | |
1838 | |
1839 | if (!Result && ShouldCacheCodeCompletionResults && |
1840 | CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue) |
1841 | CacheCodeCompletionResults(); |
1842 | |
1843 | |
1844 | |
1845 | CCTUInfo.reset(); |
1846 | |
1847 | return Result; |
1848 | } |
1849 | |
1850 | void ASTUnit::ResetForParse() { |
1851 | SavedMainFileBuffer.reset(); |
1852 | |
1853 | SourceMgr.reset(); |
1854 | TheSema.reset(); |
1855 | Ctx.reset(); |
1856 | PP.reset(); |
1857 | Reader.reset(); |
1858 | |
1859 | TopLevelDecls.clear(); |
1860 | clearFileLevelDecls(); |
1861 | } |
1862 | |
1863 | |
1864 | |
1865 | |
1866 | |
1867 | namespace { |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { |
1873 | uint64_t NormalContexts; |
1874 | ASTUnit &AST; |
1875 | CodeCompleteConsumer &Next; |
1876 | |
1877 | public: |
1878 | AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, |
1879 | const CodeCompleteOptions &CodeCompleteOpts) |
1880 | : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()), |
1881 | AST(AST), Next(Next) { |
1882 | |
1883 | |
1884 | NormalContexts |
1885 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
1886 | | (1LL << CodeCompletionContext::CCC_ObjCInterface) |
1887 | | (1LL << CodeCompletionContext::CCC_ObjCImplementation) |
1888 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
1889 | | (1LL << CodeCompletionContext::CCC_Statement) |
1890 | | (1LL << CodeCompletionContext::CCC_Expression) |
1891 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
1892 | | (1LL << CodeCompletionContext::CCC_DotMemberAccess) |
1893 | | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess) |
1894 | | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess) |
1895 | | (1LL << CodeCompletionContext::CCC_ObjCProtocolName) |
1896 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
1897 | | (1LL << CodeCompletionContext::CCC_Recovery); |
1898 | |
1899 | if (AST.getASTContext().getLangOpts().CPlusPlus) |
1900 | NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag) |
1901 | | (1LL << CodeCompletionContext::CCC_UnionTag) |
1902 | | (1LL << CodeCompletionContext::CCC_ClassOrStructTag); |
1903 | } |
1904 | |
1905 | void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, |
1906 | CodeCompletionResult *Results, |
1907 | unsigned NumResults) override; |
1908 | |
1909 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
1910 | OverloadCandidate *Candidates, |
1911 | unsigned NumCandidates, |
1912 | SourceLocation OpenParLoc) override { |
1913 | Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates, |
1914 | OpenParLoc); |
1915 | } |
1916 | |
1917 | CodeCompletionAllocator &getAllocator() override { |
1918 | return Next.getAllocator(); |
1919 | } |
1920 | |
1921 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { |
1922 | return Next.getCodeCompletionTUInfo(); |
1923 | } |
1924 | }; |
1925 | |
1926 | } |
1927 | |
1928 | |
1929 | |
1930 | static void CalculateHiddenNames(const CodeCompletionContext &Context, |
1931 | CodeCompletionResult *Results, |
1932 | unsigned NumResults, |
1933 | ASTContext &Ctx, |
1934 | llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){ |
1935 | bool OnlyTagNames = false; |
1936 | switch (Context.getKind()) { |
1937 | case CodeCompletionContext::CCC_Recovery: |
1938 | case CodeCompletionContext::CCC_TopLevel: |
1939 | case CodeCompletionContext::CCC_ObjCInterface: |
1940 | case CodeCompletionContext::CCC_ObjCImplementation: |
1941 | case CodeCompletionContext::CCC_ObjCIvarList: |
1942 | case CodeCompletionContext::CCC_ClassStructUnion: |
1943 | case CodeCompletionContext::CCC_Statement: |
1944 | case CodeCompletionContext::CCC_Expression: |
1945 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
1946 | case CodeCompletionContext::CCC_DotMemberAccess: |
1947 | case CodeCompletionContext::CCC_ArrowMemberAccess: |
1948 | case CodeCompletionContext::CCC_ObjCPropertyAccess: |
1949 | case CodeCompletionContext::CCC_Namespace: |
1950 | case CodeCompletionContext::CCC_Type: |
1951 | case CodeCompletionContext::CCC_Symbol: |
1952 | case CodeCompletionContext::CCC_SymbolOrNewName: |
1953 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
1954 | case CodeCompletionContext::CCC_ObjCInterfaceName: |
1955 | break; |
1956 | |
1957 | case CodeCompletionContext::CCC_EnumTag: |
1958 | case CodeCompletionContext::CCC_UnionTag: |
1959 | case CodeCompletionContext::CCC_ClassOrStructTag: |
1960 | OnlyTagNames = true; |
1961 | break; |
1962 | |
1963 | case CodeCompletionContext::CCC_ObjCProtocolName: |
1964 | case CodeCompletionContext::CCC_MacroName: |
1965 | case CodeCompletionContext::CCC_MacroNameUse: |
1966 | case CodeCompletionContext::CCC_PreprocessorExpression: |
1967 | case CodeCompletionContext::CCC_PreprocessorDirective: |
1968 | case CodeCompletionContext::CCC_NaturalLanguage: |
1969 | case CodeCompletionContext::CCC_SelectorName: |
1970 | case CodeCompletionContext::CCC_TypeQualifiers: |
1971 | case CodeCompletionContext::CCC_Other: |
1972 | case CodeCompletionContext::CCC_OtherWithMacros: |
1973 | case CodeCompletionContext::CCC_ObjCInstanceMessage: |
1974 | case CodeCompletionContext::CCC_ObjCClassMessage: |
1975 | case CodeCompletionContext::CCC_ObjCCategoryName: |
1976 | case CodeCompletionContext::CCC_IncludedFile: |
1977 | case CodeCompletionContext::CCC_NewName: |
1978 | |
1979 | |
1980 | return; |
1981 | } |
1982 | |
1983 | using Result = CodeCompletionResult; |
1984 | for (unsigned I = 0; I != NumResults; ++I) { |
1985 | if (Results[I].Kind != Result::RK_Declaration) |
1986 | continue; |
1987 | |
1988 | unsigned IDNS |
1989 | = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); |
1990 | |
1991 | bool Hiding = false; |
1992 | if (OnlyTagNames) |
1993 | Hiding = (IDNS & Decl::IDNS_Tag); |
1994 | else { |
1995 | unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | |
1996 | Decl::IDNS_Namespace | Decl::IDNS_Ordinary | |
1997 | Decl::IDNS_NonMemberOperator); |
1998 | if (Ctx.getLangOpts().CPlusPlus) |
1999 | HiddenIDNS |= Decl::IDNS_Tag; |
2000 | Hiding = (IDNS & HiddenIDNS); |
2001 | } |
2002 | |
2003 | if (!Hiding) |
2004 | continue; |
2005 | |
2006 | DeclarationName Name = Results[I].Declaration->getDeclName(); |
2007 | if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) |
2008 | HiddenNames.insert(Identifier->getName()); |
2009 | else |
2010 | HiddenNames.insert(Name.getAsString()); |
2011 | } |
2012 | } |
2013 | |
2014 | void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, |
2015 | CodeCompletionContext Context, |
2016 | CodeCompletionResult *Results, |
2017 | unsigned NumResults) { |
2018 | |
2019 | bool AddedResult = false; |
2020 | uint64_t InContexts = |
2021 | Context.getKind() == CodeCompletionContext::CCC_Recovery |
2022 | ? NormalContexts : (1LL << Context.getKind()); |
2023 | |
2024 | llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames; |
2025 | using Result = CodeCompletionResult; |
2026 | SmallVector<Result, 8> AllResults; |
2027 | for (ASTUnit::cached_completion_iterator |
2028 | C = AST.cached_completion_begin(), |
2029 | CEnd = AST.cached_completion_end(); |
2030 | C != CEnd; ++C) { |
2031 | |
2032 | |
2033 | if ((C->ShowInContexts & InContexts) == 0) |
2034 | continue; |
2035 | |
2036 | |
2037 | if (!AddedResult) { |
2038 | CalculateHiddenNames(Context, Results, NumResults, S.Context, |
2039 | HiddenNames); |
2040 | AllResults.insert(AllResults.end(), Results, Results + NumResults); |
2041 | AddedResult = true; |
2042 | } |
2043 | |
2044 | |
2045 | |
2046 | if (C->Kind != CXCursor_MacroDefinition && |
2047 | HiddenNames.count(C->Completion->getTypedText())) |
2048 | continue; |
2049 | |
2050 | |
2051 | unsigned Priority = C->Priority; |
2052 | CodeCompletionString *Completion = C->Completion; |
2053 | if (!Context.getPreferredType().isNull()) { |
2054 | if (C->Kind == CXCursor_MacroDefinition) { |
2055 | Priority = getMacroUsagePriority(C->Completion->getTypedText(), |
2056 | S.getLangOpts(), |
2057 | Context.getPreferredType()->isAnyPointerType()); |
2058 | } else if (C->Type) { |
2059 | CanQualType Expected |
2060 | = S.Context.getCanonicalType( |
2061 | Context.getPreferredType().getUnqualifiedType()); |
2062 | SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); |
2063 | if (ExpectedSTC == C->TypeClass) { |
2064 | |
2065 | llvm::StringMap<unsigned> &CachedCompletionTypes |
2066 | = AST.getCachedCompletionTypes(); |
2067 | llvm::StringMap<unsigned>::iterator Pos |
2068 | = CachedCompletionTypes.find(QualType(Expected).getAsString()); |
2069 | if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) |
2070 | Priority /= CCF_ExactTypeMatch; |
2071 | else |
2072 | Priority /= CCF_SimilarTypeMatch; |
2073 | } |
2074 | } |
2075 | } |
2076 | |
2077 | |
2078 | if (C->Kind == CXCursor_MacroDefinition && |
2079 | Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { |
2080 | |
2081 | |
2082 | CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(), |
2083 | CCP_CodePattern, C->Availability); |
2084 | Builder.AddTypedTextChunk(C->Completion->getTypedText()); |
2085 | Priority = CCP_CodePattern; |
2086 | Completion = Builder.TakeString(); |
2087 | } |
2088 | |
2089 | AllResults.push_back(Result(Completion, Priority, C->Kind, |
2090 | C->Availability)); |
2091 | } |
2092 | |
2093 | |
2094 | |
2095 | if (!AddedResult) { |
2096 | Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); |
2097 | return; |
2098 | } |
2099 | |
2100 | Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), |
2101 | AllResults.size()); |
2102 | } |
2103 | |
2104 | void ASTUnit::CodeComplete( |
2105 | StringRef File, unsigned Line, unsigned Column, |
2106 | ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros, |
2107 | bool IncludeCodePatterns, bool , |
2108 | CodeCompleteConsumer &Consumer, |
2109 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
2110 | DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, |
2111 | FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, |
2112 | SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { |
2113 | if (!Invocation) |
2114 | return; |
2115 | |
2116 | SimpleTimer CompletionTimer(WantTiming); |
2117 | CompletionTimer.setOutput("Code completion @ " + File + ":" + |
2118 | Twine(Line) + ":" + Twine(Column)); |
2119 | |
2120 | auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation); |
2121 | |
2122 | FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts(); |
2123 | CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts; |
2124 | PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); |
2125 | |
2126 | CodeCompleteOpts.IncludeMacros = IncludeMacros && |
2127 | CachedCompletionResults.empty(); |
2128 | CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns; |
2129 | CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty(); |
2130 | CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments; |
2131 | CodeCompleteOpts.LoadExternal = Consumer.loadExternal(); |
2132 | CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts(); |
2133 | |
2134 | IncludeBriefCommentsInCodeCompletion", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2134, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion); |
2135 | |
2136 | FrontendOpts.CodeCompletionAt.FileName = File; |
2137 | FrontendOpts.CodeCompletionAt.Line = Line; |
2138 | FrontendOpts.CodeCompletionAt.Column = Column; |
2139 | |
2140 | |
2141 | LangOpts = *CCInvocation->getLangOpts(); |
2142 | |
2143 | |
2144 | LangOpts.SpellChecking = false; |
2145 | CCInvocation->getDiagnosticOpts().IgnoreWarnings = true; |
2146 | |
2147 | std::unique_ptr<CompilerInstance> Clang( |
2148 | new CompilerInstance(PCHContainerOps)); |
2149 | |
2150 | |
2151 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
2152 | CICleanup(Clang.get()); |
2153 | |
2154 | auto &Inv = *CCInvocation; |
2155 | Clang->setInvocation(std::move(CCInvocation)); |
2156 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
2157 | |
2158 | |
2159 | Clang->setDiagnostics(&Diag); |
2160 | CaptureDroppedDiagnostics Capture(true, |
2161 | Clang->getDiagnostics(), |
2162 | &StoredDiagnostics, nullptr); |
2163 | ProcessWarningOptions(Diag, Inv.getDiagnosticOpts()); |
2164 | |
2165 | |
2166 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
2167 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
2168 | if (!Clang->hasTarget()) { |
2169 | Clang->setInvocation(nullptr); |
2170 | return; |
2171 | } |
2172 | |
2173 | |
2174 | |
2175 | |
2176 | |
2177 | Clang->getTarget().adjust(Clang->getLangOpts()); |
2178 | |
2179 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2180, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
2180 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2180, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invocation must have exactly one source file!"); |
2181 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2183, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
2182 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2183, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::Source && |
2183 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2183, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "FIXME: AST inputs not yet supported here!"); |
2184 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
2185 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::LLVM_IR && |
2186 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "IR inputs not support here!"); |
2187 | |
2188 | |
2189 | Clang->setFileManager(&FileMgr); |
2190 | Clang->setSourceManager(&SourceMgr); |
2191 | |
2192 | |
2193 | PreprocessorOpts.clearRemappedFiles(); |
2194 | PreprocessorOpts.RetainRemappedFileBuffers = true; |
2195 | for (const auto &RemappedFile : RemappedFiles) { |
2196 | PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second); |
2197 | OwnedBuffers.push_back(RemappedFile.second); |
2198 | } |
2199 | |
2200 | |
2201 | |
2202 | AugmentedCodeCompleteConsumer *AugmentedConsumer |
2203 | = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts); |
2204 | Clang->setCodeCompletionConsumer(AugmentedConsumer); |
2205 | |
2206 | |
2207 | |
2208 | |
2209 | |
2210 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
2211 | if (Preamble) { |
2212 | std::string CompleteFilePath(File); |
2213 | |
2214 | auto &VFS = FileMgr.getVirtualFileSystem(); |
2215 | auto CompleteFileStatus = VFS.status(CompleteFilePath); |
2216 | if (CompleteFileStatus) { |
2217 | llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID(); |
2218 | |
2219 | std::string MainPath(OriginalSourceFile); |
2220 | auto MainStatus = VFS.status(MainPath); |
2221 | if (MainStatus) { |
2222 | llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID(); |
2223 | if (CompleteFileID == MainID && Line > 1) |
2224 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble( |
2225 | PCHContainerOps, Inv, &VFS, false, Line - 1); |
2226 | } |
2227 | } |
2228 | } |
2229 | |
2230 | |
2231 | |
2232 | if (OverrideMainBuffer) { |
2233 | (0) . __assert_fail ("Preamble && \"No preamble was built, but OverrideMainBuffer is not null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2234, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Preamble && |
2234 | (0) . __assert_fail ("Preamble && \"No preamble was built, but OverrideMainBuffer is not null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2234, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No preamble was built, but OverrideMainBuffer is not null"); |
2235 | |
2236 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = |
2237 | &FileMgr.getVirtualFileSystem(); |
2238 | Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS, |
2239 | OverrideMainBuffer.get()); |
2240 | |
2241 | |
2242 | |
2243 | |
2244 | OwnedBuffers.push_back(OverrideMainBuffer.release()); |
2245 | } else { |
2246 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
2247 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
2248 | } |
2249 | |
2250 | |
2251 | if (!Clang->getLangOpts().Modules) |
2252 | PreprocessorOpts.DetailedRecord = false; |
2253 | |
2254 | std::unique_ptr<SyntaxOnlyAction> Act; |
2255 | Act.reset(new SyntaxOnlyAction); |
2256 | if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
2257 | Act->Execute(); |
2258 | Act->EndSourceFile(); |
2259 | } |
2260 | } |
2261 | |
2262 | bool ASTUnit::Save(StringRef File) { |
2263 | if (HadModuleLoaderFatalFailure) |
2264 | return true; |
2265 | |
2266 | |
2267 | |
2268 | SmallString<128> TempPath; |
2269 | TempPath = File; |
2270 | TempPath += "-%%%%%%%%"; |
2271 | int fd; |
2272 | if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath)) |
2273 | return true; |
2274 | |
2275 | |
2276 | |
2277 | llvm::raw_fd_ostream Out(fd, ); |
2278 | |
2279 | serialize(Out); |
2280 | Out.close(); |
2281 | if (Out.has_error()) { |
2282 | Out.clear_error(); |
2283 | return true; |
2284 | } |
2285 | |
2286 | if (llvm::sys::fs::rename(TempPath, File)) { |
2287 | llvm::sys::fs::remove(TempPath); |
2288 | return true; |
2289 | } |
2290 | |
2291 | return false; |
2292 | } |
2293 | |
2294 | static bool serializeUnit(ASTWriter &Writer, |
2295 | SmallVectorImpl<char> &Buffer, |
2296 | Sema &S, |
2297 | bool hasErrors, |
2298 | raw_ostream &OS) { |
2299 | Writer.WriteAST(S, std::string(), nullptr, "", hasErrors); |
2300 | |
2301 | |
2302 | if (!Buffer.empty()) |
2303 | OS.write(Buffer.data(), Buffer.size()); |
2304 | |
2305 | return false; |
2306 | } |
2307 | |
2308 | bool ASTUnit::serialize(raw_ostream &OS) { |
2309 | |
2310 | bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred(); |
2311 | |
2312 | if (WriterData) |
2313 | return serializeUnit(WriterData->Writer, WriterData->Buffer, |
2314 | getSema(), hasErrors, OS); |
2315 | |
2316 | SmallString<128> Buffer; |
2317 | llvm::BitstreamWriter Stream(Buffer); |
2318 | InMemoryModuleCache ModuleCache; |
2319 | ASTWriter Writer(Stream, Buffer, ModuleCache, {}); |
2320 | return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS); |
2321 | } |
2322 | |
2323 | using SLocRemap = ContinuousRangeMap<unsigned, int, 2>; |
2324 | |
2325 | void ASTUnit::TranslateStoredDiagnostics( |
2326 | FileManager &FileMgr, |
2327 | SourceManager &SrcMgr, |
2328 | const SmallVectorImpl<StandaloneDiagnostic> &Diags, |
2329 | SmallVectorImpl<StoredDiagnostic> &Out) { |
2330 | |
2331 | |
2332 | |
2333 | |
2334 | SmallVector<StoredDiagnostic, 4> Result; |
2335 | Result.reserve(Diags.size()); |
2336 | |
2337 | for (const auto &SD : Diags) { |
2338 | |
2339 | if (SD.Filename.empty()) |
2340 | continue; |
2341 | const FileEntry *FE = FileMgr.getFile(SD.Filename); |
2342 | if (!FE) |
2343 | continue; |
2344 | SourceLocation FileLoc; |
2345 | auto ItFileID = PreambleSrcLocCache.find(SD.Filename); |
2346 | if (ItFileID == PreambleSrcLocCache.end()) { |
2347 | FileID FID = SrcMgr.translateFile(FE); |
2348 | FileLoc = SrcMgr.getLocForStartOfFile(FID); |
2349 | PreambleSrcLocCache[SD.Filename] = FileLoc; |
2350 | } else { |
2351 | FileLoc = ItFileID->getValue(); |
2352 | } |
2353 | |
2354 | if (FileLoc.isInvalid()) |
2355 | continue; |
2356 | SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset); |
2357 | FullSourceLoc Loc(L, SrcMgr); |
2358 | |
2359 | SmallVector<CharSourceRange, 4> Ranges; |
2360 | Ranges.reserve(SD.Ranges.size()); |
2361 | for (const auto &Range : SD.Ranges) { |
2362 | SourceLocation BL = FileLoc.getLocWithOffset(Range.first); |
2363 | SourceLocation EL = FileLoc.getLocWithOffset(Range.second); |
2364 | Ranges.push_back(CharSourceRange::getCharRange(BL, EL)); |
2365 | } |
2366 | |
2367 | SmallVector<FixItHint, 2> FixIts; |
2368 | FixIts.reserve(SD.FixIts.size()); |
2369 | for (const auto &FixIt : SD.FixIts) { |
2370 | FixIts.push_back(FixItHint()); |
2371 | FixItHint &FH = FixIts.back(); |
2372 | FH.CodeToInsert = FixIt.CodeToInsert; |
2373 | SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first); |
2374 | SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second); |
2375 | FH.RemoveRange = CharSourceRange::getCharRange(BL, EL); |
2376 | } |
2377 | |
2378 | Result.push_back(StoredDiagnostic(SD.Level, SD.ID, |
2379 | SD.Message, Loc, Ranges, FixIts)); |
2380 | } |
2381 | Result.swap(Out); |
2382 | } |
2383 | |
2384 | void ASTUnit::addFileLevelDecl(Decl *D) { |
2385 | assert(D); |
2386 | |
2387 | |
2388 | if (D->isFromASTFile()) |
2389 | return; |
2390 | |
2391 | SourceManager &SM = *SourceMgr; |
2392 | SourceLocation Loc = D->getLocation(); |
2393 | if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc)) |
2394 | return; |
2395 | |
2396 | |
2397 | if (!D->getLexicalDeclContext()->isFileContext()) |
2398 | return; |
2399 | |
2400 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
2401 | assert(SM.isLocalSourceLocation(FileLoc)); |
2402 | FileID FID; |
2403 | unsigned Offset; |
2404 | std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); |
2405 | if (FID.isInvalid()) |
2406 | return; |
2407 | |
2408 | LocDeclsTy *&Decls = FileDecls[FID]; |
2409 | if (!Decls) |
2410 | Decls = new LocDeclsTy(); |
2411 | |
2412 | std::pair<unsigned, Decl *> LocDecl(Offset, D); |
2413 | |
2414 | if (Decls->empty() || Decls->back().first <= Offset) { |
2415 | Decls->push_back(LocDecl); |
2416 | return; |
2417 | } |
2418 | |
2419 | LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(), |
2420 | LocDecl, llvm::less_first()); |
2421 | |
2422 | Decls->insert(I, LocDecl); |
2423 | } |
2424 | |
2425 | void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, |
2426 | SmallVectorImpl<Decl *> &Decls) { |
2427 | if (File.isInvalid()) |
2428 | return; |
2429 | |
2430 | if (SourceMgr->isLoadedFileID(File)) { |
2431 | (0) . __assert_fail ("Ctx->getExternalSource() && \"No external source!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2431, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Ctx->getExternalSource() && "No external source!"); |
2432 | return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length, |
2433 | Decls); |
2434 | } |
2435 | |
2436 | FileDeclsTy::iterator I = FileDecls.find(File); |
2437 | if (I == FileDecls.end()) |
2438 | return; |
2439 | |
2440 | LocDeclsTy &LocDecls = *I->second; |
2441 | if (LocDecls.empty()) |
2442 | return; |
2443 | |
2444 | LocDeclsTy::iterator BeginIt = |
2445 | std::lower_bound(LocDecls.begin(), LocDecls.end(), |
2446 | std::make_pair(Offset, (Decl *)nullptr), |
2447 | llvm::less_first()); |
2448 | if (BeginIt != LocDecls.begin()) |
2449 | --BeginIt; |
2450 | |
2451 | |
2452 | |
2453 | |
2454 | while (BeginIt != LocDecls.begin() && |
2455 | BeginIt->second->isTopLevelDeclInObjCContainer()) |
2456 | --BeginIt; |
2457 | |
2458 | LocDeclsTy::iterator EndIt = std::upper_bound( |
2459 | LocDecls.begin(), LocDecls.end(), |
2460 | std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first()); |
2461 | if (EndIt != LocDecls.end()) |
2462 | ++EndIt; |
2463 | |
2464 | for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt) |
2465 | Decls.push_back(DIt->second); |
2466 | } |
2467 | |
2468 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
2469 | unsigned Line, unsigned Col) const { |
2470 | const SourceManager &SM = getSourceManager(); |
2471 | SourceLocation Loc = SM.translateFileLineCol(File, Line, Col); |
2472 | return SM.getMacroArgExpandedLocation(Loc); |
2473 | } |
2474 | |
2475 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
2476 | unsigned Offset) const { |
2477 | const SourceManager &SM = getSourceManager(); |
2478 | SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1); |
2479 | return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset)); |
2480 | } |
2481 | |
2482 | |
2483 | |
2484 | |
2485 | SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) const { |
2486 | FileID PreambleID; |
2487 | if (SourceMgr) |
2488 | PreambleID = SourceMgr->getPreambleFileID(); |
2489 | |
2490 | if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid()) |
2491 | return Loc; |
2492 | |
2493 | unsigned Offs; |
2494 | if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) { |
2495 | SourceLocation FileLoc |
2496 | = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); |
2497 | return FileLoc.getLocWithOffset(Offs); |
2498 | } |
2499 | |
2500 | return Loc; |
2501 | } |
2502 | |
2503 | |
2504 | |
2505 | |
2506 | SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) const { |
2507 | FileID PreambleID; |
2508 | if (SourceMgr) |
2509 | PreambleID = SourceMgr->getPreambleFileID(); |
2510 | |
2511 | if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid()) |
2512 | return Loc; |
2513 | |
2514 | unsigned Offs; |
2515 | if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) && |
2516 | Offs < Preamble->getBounds().Size) { |
2517 | SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID); |
2518 | return FileLoc.getLocWithOffset(Offs); |
2519 | } |
2520 | |
2521 | return Loc; |
2522 | } |
2523 | |
2524 | bool ASTUnit::isInPreambleFileID(SourceLocation Loc) const { |
2525 | FileID FID; |
2526 | if (SourceMgr) |
2527 | FID = SourceMgr->getPreambleFileID(); |
2528 | |
2529 | if (Loc.isInvalid() || FID.isInvalid()) |
2530 | return false; |
2531 | |
2532 | return SourceMgr->isInFileID(Loc, FID); |
2533 | } |
2534 | |
2535 | bool ASTUnit::isInMainFileID(SourceLocation Loc) const { |
2536 | FileID FID; |
2537 | if (SourceMgr) |
2538 | FID = SourceMgr->getMainFileID(); |
2539 | |
2540 | if (Loc.isInvalid() || FID.isInvalid()) |
2541 | return false; |
2542 | |
2543 | return SourceMgr->isInFileID(Loc, FID); |
2544 | } |
2545 | |
2546 | SourceLocation ASTUnit::getEndOfPreambleFileID() const { |
2547 | FileID FID; |
2548 | if (SourceMgr) |
2549 | FID = SourceMgr->getPreambleFileID(); |
2550 | |
2551 | if (FID.isInvalid()) |
2552 | return {}; |
2553 | |
2554 | return SourceMgr->getLocForEndOfFile(FID); |
2555 | } |
2556 | |
2557 | SourceLocation ASTUnit::getStartOfMainFileID() const { |
2558 | FileID FID; |
2559 | if (SourceMgr) |
2560 | FID = SourceMgr->getMainFileID(); |
2561 | |
2562 | if (FID.isInvalid()) |
2563 | return {}; |
2564 | |
2565 | return SourceMgr->getLocForStartOfFile(FID); |
2566 | } |
2567 | |
2568 | llvm::iterator_range<PreprocessingRecord::iterator> |
2569 | ASTUnit::getLocalPreprocessingEntities() const { |
2570 | if (isMainFileAST()) { |
2571 | serialization::ModuleFile & |
2572 | Mod = Reader->getModuleManager().getPrimaryModule(); |
2573 | return Reader->getModulePreprocessedEntities(Mod); |
2574 | } |
2575 | |
2576 | if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord()) |
2577 | return llvm::make_range(PPRec->local_begin(), PPRec->local_end()); |
2578 | |
2579 | return llvm::make_range(PreprocessingRecord::iterator(), |
2580 | PreprocessingRecord::iterator()); |
2581 | } |
2582 | |
2583 | bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) { |
2584 | if (isMainFileAST()) { |
2585 | serialization::ModuleFile & |
2586 | Mod = Reader->getModuleManager().getPrimaryModule(); |
2587 | for (const auto *D : Reader->getModuleFileLevelDecls(Mod)) { |
2588 | if (!Fn(context, D)) |
2589 | return false; |
2590 | } |
2591 | |
2592 | return true; |
2593 | } |
2594 | |
2595 | for (ASTUnit::top_level_iterator TL = top_level_begin(), |
2596 | TLEnd = top_level_end(); |
2597 | TL != TLEnd; ++TL) { |
2598 | if (!Fn(context, *TL)) |
2599 | return false; |
2600 | } |
2601 | |
2602 | return true; |
2603 | } |
2604 | |
2605 | const FileEntry *ASTUnit::getPCHFile() { |
2606 | if (!Reader) |
2607 | return nullptr; |
2608 | |
2609 | serialization::ModuleFile *Mod = nullptr; |
2610 | Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) { |
2611 | switch (M.Kind) { |
2612 | case serialization::MK_ImplicitModule: |
2613 | case serialization::MK_ExplicitModule: |
2614 | case serialization::MK_PrebuiltModule: |
2615 | return true; |
2616 | case serialization::MK_PCH: |
2617 | Mod = &M; |
2618 | return true; |
2619 | case serialization::MK_Preamble: |
2620 | return false; |
2621 | case serialization::MK_MainFile: |
2622 | return false; |
2623 | } |
2624 | |
2625 | return true; |
2626 | }); |
2627 | if (Mod) |
2628 | return Mod->File; |
2629 | |
2630 | return nullptr; |
2631 | } |
2632 | |
2633 | bool ASTUnit::isModuleFile() const { |
2634 | return isMainFileAST() && getLangOpts().isCompilingModule(); |
2635 | } |
2636 | |
2637 | InputKind ASTUnit::getInputKind() const { |
2638 | auto &LangOpts = getLangOpts(); |
2639 | |
2640 | InputKind::Language Lang; |
2641 | if (LangOpts.OpenCL) |
2642 | Lang = InputKind::OpenCL; |
2643 | else if (LangOpts.CUDA) |
2644 | Lang = InputKind::CUDA; |
2645 | else if (LangOpts.RenderScript) |
2646 | Lang = InputKind::RenderScript; |
2647 | else if (LangOpts.CPlusPlus) |
2648 | Lang = LangOpts.ObjC ? InputKind::ObjCXX : InputKind::CXX; |
2649 | else |
2650 | Lang = LangOpts.ObjC ? InputKind::ObjC : InputKind::C; |
2651 | |
2652 | InputKind::Format Fmt = InputKind::Source; |
2653 | if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap) |
2654 | Fmt = InputKind::ModuleMap; |
2655 | |
2656 | |
2657 | bool PP = false; |
2658 | |
2659 | return InputKind(Lang, Fmt, PP); |
2660 | } |
2661 | |
2662 | #ifndef NDEBUG |
2663 | ASTUnit::ConcurrencyState::ConcurrencyState() { |
2664 | Mutex = new llvm::sys::MutexImpl(); |
2665 | } |
2666 | |
2667 | ASTUnit::ConcurrencyState::~ConcurrencyState() { |
2668 | delete static_cast<llvm::sys::MutexImpl *>(Mutex); |
2669 | } |
2670 | |
2671 | void ASTUnit::ConcurrencyState::start() { |
2672 | bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire(); |
2673 | (0) . __assert_fail ("acquired && \"Concurrent access to ASTUnit!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/ASTUnit.cpp", 2673, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(acquired && "Concurrent access to ASTUnit!"); |
2674 | } |
2675 | |
2676 | void ASTUnit::ConcurrencyState::finish() { |
2677 | static_cast<llvm::sys::MutexImpl *>(Mutex)->release(); |
2678 | } |
2679 | |
2680 | #else |
2681 | |
2682 | ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; } |
2683 | ASTUnit::ConcurrencyState::~ConcurrencyState() {} |
2684 | void ASTUnit::ConcurrencyState::start() {} |
2685 | void ASTUnit::ConcurrencyState::finish() {} |
2686 | |
2687 | #endif |
2688 | |