clang 8.0.0

ASTUnit.cpp
Go to the documentation of this file.
1 //===- ASTUnit.cpp - ASTUnit utility --------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // ASTUnit Implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/CommentCommandTraits.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/DeclarationName.h"
25 #include "clang/AST/ExternalASTSource.h"
26 #include "clang/AST/PrettyPrinter.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/TypeOrdering.h"
29 #include "clang/Basic/Diagnostic.h"
30 #include "clang/Basic/FileManager.h"
31 #include "clang/Basic/IdentifierTable.h"
32 #include "clang/Basic/LLVM.h"
33 #include "clang/Basic/LangOptions.h"
34 #include "clang/Basic/MemoryBufferCache.h"
35 #include "clang/Basic/Module.h"
36 #include "clang/Basic/SourceLocation.h"
37 #include "clang/Basic/SourceManager.h"
38 #include "clang/Basic/TargetInfo.h"
39 #include "clang/Basic/TargetOptions.h"
40 #include "clang/Frontend/CompilerInstance.h"
41 #include "clang/Frontend/CompilerInvocation.h"
42 #include "clang/Frontend/FrontendAction.h"
43 #include "clang/Frontend/FrontendActions.h"
44 #include "clang/Frontend/FrontendDiagnostic.h"
45 #include "clang/Frontend/FrontendOptions.h"
46 #include "clang/Frontend/MultiplexConsumer.h"
47 #include "clang/Frontend/PrecompiledPreamble.h"
48 #include "clang/Frontend/Utils.h"
49 #include "clang/Lex/HeaderSearch.h"
50 #include "clang/Lex/HeaderSearchOptions.h"
51 #include "clang/Lex/Lexer.h"
52 #include "clang/Lex/PPCallbacks.h"
53 #include "clang/Lex/PreprocessingRecord.h"
54 #include "clang/Lex/Preprocessor.h"
55 #include "clang/Lex/PreprocessorOptions.h"
56 #include "clang/Lex/Token.h"
57 #include "clang/Sema/CodeCompleteConsumer.h"
58 #include "clang/Sema/CodeCompleteOptions.h"
59 #include "clang/Sema/Sema.h"
60 #include "clang/Serialization/ASTBitCodes.h"
61 #include "clang/Serialization/ASTReader.h"
62 #include "clang/Serialization/ASTWriter.h"
63 #include "clang/Serialization/ContinuousRangeMap.h"
64 #include "clang/Serialization/Module.h"
65 #include "clang/Serialization/PCHContainerOperations.h"
66 #include "llvm/ADT/ArrayRef.h"
67 #include "llvm/ADT/DenseMap.h"
68 #include "llvm/ADT/IntrusiveRefCntPtr.h"
69 #include "llvm/ADT/None.h"
70 #include "llvm/ADT/Optional.h"
71 #include "llvm/ADT/STLExtras.h"
72 #include "llvm/ADT/SmallString.h"
73 #include "llvm/ADT/SmallVector.h"
74 #include "llvm/ADT/StringMap.h"
75 #include "llvm/ADT/StringRef.h"
76 #include "llvm/ADT/StringSet.h"
77 #include "llvm/ADT/Twine.h"
78 #include "llvm/ADT/iterator_range.h"
79 #include "llvm/Bitcode/BitstreamWriter.h"
80 #include "llvm/Support/Allocator.h"
81 #include "llvm/Support/Casting.h"
82 #include "llvm/Support/CrashRecoveryContext.h"
83 #include "llvm/Support/DJB.h"
84 #include "llvm/Support/ErrorHandling.h"
85 #include "llvm/Support/ErrorOr.h"
86 #include "llvm/Support/FileSystem.h"
87 #include "llvm/Support/MemoryBuffer.h"
88 #include "llvm/Support/Mutex.h"
89 #include "llvm/Support/Timer.h"
90 #include "llvm/Support/VirtualFileSystem.h"
91 #include "llvm/Support/raw_ostream.h"
92 #include <algorithm>
93 #include <atomic>
94 #include <cassert>
95 #include <cstdint>
96 #include <cstdio>
97 #include <cstdlib>
98 #include <memory>
99 #include <string>
100 #include <tuple>
101 #include <utility>
102 #include <vector>
103
104 using namespace clang;
105
106 using llvm::TimeRecord;
107
108 namespace {
109
110 class SimpleTimer {
111 bool WantTiming;
112 TimeRecord Start;
113 std::string Output;
114
115 public:
116 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
117 if (WantTiming)
118 Start = TimeRecord::getCurrentTime();
119 }
120
121 ~SimpleTimer() {
122 if (WantTiming) {
123 TimeRecord Elapsed = TimeRecord::getCurrentTime();
124 Elapsed -= Start;
125 llvm::errs() << Output << ':';
126 Elapsed.print(Elapsed, llvm::errs());
127 llvm::errs() << '\n';
128 }
129 }
130
131 void setOutput(const Twine &Output) {
132 if (WantTiming)
133 this->Output = Output.str();
134 }
135 };
136
137 } // namespace
138
139 template <class T>
140 static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) {
141 if (!Val)
142 return nullptr;
143 return std::move(*Val);
144 }
145
146 template <class T>
147 static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
148 if (!Val)
149 return false;
150 Output = std::move(*Val);
151 return true;
152 }
153
154 /// Get a source buffer for \p MainFilePath, handling all file-to-file
155 /// and file-to-buffer remappings inside \p Invocation.
156 static std::unique_ptr<llvm::MemoryBuffer>
157 getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation,
158 llvm::vfs::FileSystem *VFS,
159 StringRef FilePath, bool isVolatile) {
160 const auto &PreprocessorOpts = Invocation.getPreprocessorOpts();
161
162 // Try to determine if the main file has been remapped, either from the
163 // command line (to another file) or directly through the compiler
164 // invocation (to a memory buffer).
165 llvm::MemoryBuffer *Buffer = nullptr;
166 std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
167 auto FileStatus = VFS->status(FilePath);
168 if (FileStatus) {
169 llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID();
170
171 // Check whether there is a file-file remapping of the main file
172 for (const auto &RF : PreprocessorOpts.RemappedFiles) {
173 std::string MPath(RF.first);
174 auto MPathStatus = VFS->status(MPath);
175 if (MPathStatus) {
176 llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
177 if (MainFileID == MID) {
178 // We found a remapping. Try to load the resulting, remapped source.
179 BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second, -1, true, isVolatile));
180 if (!BufferOwner)
181 return nullptr;
182 }
183 }
184 }
185
186 // Check whether there is a file-buffer remapping. It supercedes the
187 // file-file remapping.
188 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
189 std::string MPath(RB.first);
190 auto MPathStatus = VFS->status(MPath);
191 if (MPathStatus) {
192 llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
193 if (MainFileID == MID) {
194 // We found a remapping.
195 BufferOwner.reset();
196 Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
197 }
198 }
199 }
200 }
201
202 // If the main source file was not remapped, load it now.
203 if (!Buffer && !BufferOwner) {
204 BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath, -1, true, isVolatile));
205 if (!BufferOwner)
206 return nullptr;
207 }
208
209 if (BufferOwner)
210 return BufferOwner;
211 if (!Buffer)
212 return nullptr;
213 return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath);
214 }
215
216 struct ASTUnit::ASTWriterData {
217 SmallString<128> Buffer;
218 llvm::BitstreamWriter Stream;
219 ASTWriter Writer;
220
221 ASTWriterData(MemoryBufferCache &PCMCache)
222 : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {}
223 };
224
225 void ASTUnit::clearFileLevelDecls() {
226 llvm::DeleteContainerSeconds(FileDecls);
227 }
228
229 /// After failing to build a precompiled preamble (due to
230 /// errors in the source that occurs in the preamble), the number of
231 /// reparses during which we'll skip even trying to precompile the
232 /// preamble.
233 const unsigned DefaultPreambleRebuildInterval = 5;
234
235 /// Tracks the number of ASTUnit objects that are currently active.
236 ///
237 /// Used for debugging purposes only.
238 static std::atomic<unsigned> ActiveASTUnitObjects;
239
240 ASTUnit::ASTUnit(bool _MainFileIsAST)
241 : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")),
242 ShouldCacheCodeCompletionResults(false),
243 IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
244 UnsafeToFree(false) {
245 if (getenv("LIBCLANG_OBJTRACKING"))
246 fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects);
247 }
248
249 ASTUnit::~ASTUnit() {
250 // If we loaded from an AST file, balance out the BeginSourceFile call.
251 if (MainFileIsAST && getDiagnostics().getClient()) {
252 getDiagnostics().getClient()->EndSourceFile();
253 }
254
255 clearFileLevelDecls();
256
257 // Free the buffers associated with remapped files. We are required to
258 // perform this operation here because we explicitly request that the
259 // compiler instance *not* free these buffers for each invocation of the
260 // parser.
261 if (Invocation && OwnsRemappedFileBuffers) {
262 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
263 for (const auto &RB : PPOpts.RemappedFileBuffers)
264 delete RB.second;
265 }
266
267 ClearCachedCompletionResults();
268
269 if (getenv("LIBCLANG_OBJTRACKING"))
270 fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects);
271 }
272
273 void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
274 this->PP = std::move(PP);
275 }
276
277 void ASTUnit::enableSourceFileDiagnostics() {
278 assert(getDiagnostics().getClient() && Ctx &&
279 "Bad context for source file");
280 getDiagnostics().getClient()->BeginSourceFile(Ctx->getLangOpts(), PP.get());
281 }
282
283 /// Determine the set of code-completion contexts in which this
284 /// declaration should be shown.
285 static uint64_t getDeclShowContexts(const NamedDecl *ND,
286 const LangOptions &LangOpts,
287 bool &IsNestedNameSpecifier) {
288 IsNestedNameSpecifier = false;
289
290 if (isa<UsingShadowDecl>(ND))
291 ND = ND->getUnderlyingDecl();
292 if (!ND)
293 return 0;
294
295 uint64_t Contexts = 0;
296 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
297 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) ||
298 isa<TypeAliasTemplateDecl>(ND)) {
299 // Types can appear in these contexts.
300 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
301 Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
302 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
303 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
304 | (1LL << CodeCompletionContext::CCC_Statement)
305 | (1LL << CodeCompletionContext::CCC_Type)
306 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
307
308 // In C++, types can appear in expressions contexts (for functional casts).
309 if (LangOpts.CPlusPlus)
310 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
311
312 // In Objective-C, message sends can send interfaces. In Objective-C++,
313 // all types are available due to functional casts.
314 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
315 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
316
317 // In Objective-C, you can only be a subclass of another Objective-C class
318 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
319 // Objective-C interfaces can be used in a class property expression.
320 if (ID->getDefinition())
321 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
322 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
323 }
324
325 // Deal with tag names.
326 if (isa<EnumDecl>(ND)) {
327 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
328
329 // Part of the nested-name-specifier in C++0x.
330 if (LangOpts.CPlusPlus11)
331 IsNestedNameSpecifier = true;
332 } else if (const auto *Record = dyn_cast<RecordDecl>(ND)) {
333 if (Record->isUnion())
334 Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
335 else
336 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
337
338 if (LangOpts.CPlusPlus)
339 IsNestedNameSpecifier = true;
340 } else if (isa<ClassTemplateDecl>(ND))
341 IsNestedNameSpecifier = true;
342 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
343 // Values can appear in these contexts.
344 Contexts = (1LL << CodeCompletionContext::CCC_Statement)
345 | (1LL << CodeCompletionContext::CCC_Expression)
346 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
347 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
348 } else if (isa<ObjCProtocolDecl>(ND)) {
349 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
350 } else if (isa<ObjCCategoryDecl>(ND)) {
351 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
352 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
353 Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
354
355 // Part of the nested-name-specifier.
356 IsNestedNameSpecifier = true;
357 }
358
359 return Contexts;
360 }
361
362 void ASTUnit::CacheCodeCompletionResults() {
363 if (!TheSema)
364 return;
365
366 SimpleTimer Timer(WantTiming);
367 Timer.setOutput("Cache global code completions for " + getMainFileName());
368
369 // Clear out the previous results.
370 ClearCachedCompletionResults();
371
372 // Gather the set of global code completions.
373 using Result = CodeCompletionResult;
374 SmallVector<Result, 8> Results;
375 CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
376 CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
377 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
378 CCTUInfo, Results);
379
380 // Translate global code completions into cached completions.
381 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
382 CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel);
383
384 for (auto &R : Results) {
385 switch (R.Kind) {
386 case Result::RK_Declaration: {
387 bool IsNestedNameSpecifier = false;
388 CachedCodeCompletionResult CachedResult;
389 CachedResult.Completion = R.CreateCodeCompletionString(
390 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
391 IncludeBriefCommentsInCodeCompletion);
392 CachedResult.ShowInContexts = getDeclShowContexts(
393 R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier);
394 CachedResult.Priority = R.Priority;
395 CachedResult.Kind = R.CursorKind;
396 CachedResult.Availability = R.Availability;
397
398 // Keep track of the type of this completion in an ASTContext-agnostic
399 // way.
400 QualType UsageType = getDeclUsageType(*Ctx, R.Declaration);
401 if (UsageType.isNull()) {
402 CachedResult.TypeClass = STC_Void;
403 CachedResult.Type = 0;
404 } else {
405 CanQualType CanUsageType
406 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
407 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
408
409 // Determine whether we have already seen this type. If so, we save
410 // ourselves the work of formatting the type string by using the
411 // temporary, CanQualType-based hash table to find the associated value.
412 unsigned &TypeValue = CompletionTypes[CanUsageType];
413 if (TypeValue == 0) {
414 TypeValue = CompletionTypes.size();
415 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
416 = TypeValue;
417 }
418
419 CachedResult.Type = TypeValue;
420 }
421
422 CachedCompletionResults.push_back(CachedResult);
423
424 /// Handle nested-name-specifiers in C++.
425 if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
426 !R.StartsNestedNameSpecifier) {
427 // The contexts in which a nested-name-specifier can appear in C++.
428 uint64_t NNSContexts
429 = (1LL << CodeCompletionContext::CCC_TopLevel)
430 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
431 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
432 | (1LL << CodeCompletionContext::CCC_Statement)
433 | (1LL << CodeCompletionContext::CCC_Expression)
434 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
435 | (1LL << CodeCompletionContext::CCC_EnumTag)
436 | (1LL << CodeCompletionContext::CCC_UnionTag)
437 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
438 | (1LL << CodeCompletionContext::CCC_Type)
439 | (1LL << CodeCompletionContext::CCC_Symbol)
440 | (1LL << CodeCompletionContext::CCC_SymbolOrNewName)
441 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
442
443 if (isa<NamespaceDecl>(R.Declaration) ||
444 isa<NamespaceAliasDecl>(R.Declaration))
445 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
446
447 if (uint64_t RemainingContexts
448 = NNSContexts & ~CachedResult.ShowInContexts) {
449 // If there any contexts where this completion can be a
450 // nested-name-specifier but isn't already an option, create a
451 // nested-name-specifier completion.
452 R.StartsNestedNameSpecifier = true;
453 CachedResult.Completion = R.CreateCodeCompletionString(
454 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
455 IncludeBriefCommentsInCodeCompletion);
456 CachedResult.ShowInContexts = RemainingContexts;
457 CachedResult.Priority = CCP_NestedNameSpecifier;
458 CachedResult.TypeClass = STC_Void;
459 CachedResult.Type = 0;
460 CachedCompletionResults.push_back(CachedResult);
461 }
462 }
463 break;
464 }
465
466 case Result::RK_Keyword:
467 case Result::RK_Pattern:
468 // Ignore keywords and patterns; we don't care, since they are so
469 // easily regenerated.
470 break;
471
472 case Result::RK_Macro: {
473 CachedCodeCompletionResult CachedResult;
474 CachedResult.Completion = R.CreateCodeCompletionString(
475 *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo,
476 IncludeBriefCommentsInCodeCompletion);
477 CachedResult.ShowInContexts
478 = (1LL << CodeCompletionContext::CCC_TopLevel)
479 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
480 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
481 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
482 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
483 | (1LL << CodeCompletionContext::CCC_Statement)
484 | (1LL << CodeCompletionContext::CCC_Expression)
485 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
486 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
487 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
488 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
489 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
490
491 CachedResult.Priority = R.Priority;
492 CachedResult.Kind = R.CursorKind;
493 CachedResult.Availability = R.Availability;
494 CachedResult.TypeClass = STC_Void;
495 CachedResult.Type = 0;
496 CachedCompletionResults.push_back(CachedResult);
497 break;
498 }
499 }
500 }
501
502 // Save the current top-level hash value.
503 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
504 }
505
506 void ASTUnit::ClearCachedCompletionResults() {
507 CachedCompletionResults.clear();
508 CachedCompletionTypes.clear();
509 CachedCompletionAllocator = nullptr;
510 }
511
512 namespace {
513
514 /// Gathers information from ASTReader that will be used to initialize
515 /// a Preprocessor.
516 class ASTInfoCollector : public ASTReaderListener {
517 Preprocessor &PP;
518 ASTContext *Context;
519 HeaderSearchOptions &HSOpts;
520 PreprocessorOptions &PPOpts;
521 LangOptions &LangOpt;
522 std::shared_ptr<TargetOptions> &TargetOpts;
523 IntrusiveRefCntPtr<TargetInfo> &Target;
524 unsigned &Counter;
525 bool InitializedLanguage = false;
526
527 public:
528 ASTInfoCollector(Preprocessor &PP, ASTContext *Context,
529 HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts,
530 LangOptions &LangOpt,
531 std::shared_ptr<TargetOptions> &TargetOpts,
532 IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
533 : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts),
534 LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target),
535 Counter(Counter) {}
536
537 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
538 bool AllowCompatibleDifferences) override {
539 if (InitializedLanguage)
540 return false;
541
542 LangOpt = LangOpts;
543 InitializedLanguage = true;
544
545 updated();
546 return false;
547 }
548
549 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
550 StringRef SpecificModuleCachePath,
551 bool Complain) override {
552 this->HSOpts = HSOpts;
553 return false;
554 }
555
556 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain,
557 std::string &SuggestedPredefines) override {
558 this->PPOpts = PPOpts;
559 return false;
560 }
561
562 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
563 bool AllowCompatibleDifferences) override {
564 // If we've already initialized the target, don't do it again.
565 if (Target)
566 return false;
567
568 this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
569 Target =
570 TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
571
572 updated();
573 return false;
574 }
575
576 void ReadCounter(const serialization::ModuleFile &M,
577 unsigned Value) override {
578 Counter = Value;
579 }
580
581 private:
582 void updated() {
583 if (!Target || !InitializedLanguage)
584 return;
585
586 // Inform the target of the language options.
587 //
588 // FIXME: We shouldn't need to do this, the target should be immutable once
589 // created. This complexity should be lifted elsewhere.
590 Target->adjust(LangOpt);
591
592 // Initialize the preprocessor.
593 PP.Initialize(*Target);
594
595 if (!Context)
596 return;
597
598 // Initialize the ASTContext
599 Context->InitBuiltinTypes(*Target);
600
601 // Adjust printing policy based on language options.
602 Context->setPrintingPolicy(PrintingPolicy(LangOpt));
603
604 // We didn't have access to the comment options when the ASTContext was
605 // constructed, so register them now.
606 Context->getCommentCommandTraits().registerCommentOptions(
607 LangOpt.CommentOpts);
608 }
609 };
610
611 /// Diagnostic consumer that saves each diagnostic it is given.
612 class StoredDiagnosticConsumer : public DiagnosticConsumer {
613 SmallVectorImpl<StoredDiagnostic> *StoredDiags;
614 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags;
615 const LangOptions *LangOpts = nullptr;
616 SourceManager *SourceMgr = nullptr;
617
618 public:
619 StoredDiagnosticConsumer(
620 SmallVectorImpl<StoredDiagnostic> *StoredDiags,
621 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
622 : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags) {
623 assert((StoredDiags || StandaloneDiags) &&
624 "No output collections were passed to StoredDiagnosticConsumer.");
625 }
626
627 void BeginSourceFile(const LangOptions &LangOpts,
628 const Preprocessor *PP = nullptr) override {
629 this->LangOpts = &LangOpts;
630 if (PP)
631 SourceMgr = &PP->getSourceManager();
632 }
633
634 void HandleDiagnostic(DiagnosticsEngine::Level Level,
635 const Diagnostic &Info) override;
636 };
637
638 /// RAII object that optionally captures diagnostics, if
639 /// there is no diagnostic client to capture them already.
640 class CaptureDroppedDiagnostics {
641 DiagnosticsEngine &Diags;
642 StoredDiagnosticConsumer Client;
643 DiagnosticConsumer *PreviousClient = nullptr;
644 std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
645
646 public:
647 CaptureDroppedDiagnostics(
648 bool RequestCapture, DiagnosticsEngine &Diags,
649 SmallVectorImpl<StoredDiagnostic> *StoredDiags,
650 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
651 : Diags(Diags), Client(StoredDiags, StandaloneDiags) {
652 if (RequestCapture || Diags.getClient() == nullptr) {
653 OwningPreviousClient = Diags.takeClient();
654 PreviousClient = Diags.getClient();
655 Diags.setClient(&Client, false);
656 }
657 }
658
659 ~CaptureDroppedDiagnostics() {
660 if (Diags.getClient() == &Client)
661 Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
662 }
663 };
664
665 } // namespace
666
667 static ASTUnit::StandaloneDiagnostic
668 makeStandaloneDiagnostic(const LangOptions &LangOpts,
669 const StoredDiagnostic &InDiag);
670
671 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
672 const Diagnostic &Info) {
673 // Default implementation (Warnings/errors count).
674 DiagnosticConsumer::HandleDiagnostic(Level, Info);
675
676 // Only record the diagnostic if it's part of the source manager we know
677 // about. This effectively drops diagnostics from modules we're building.
678 // FIXME: In the long run, ee don't want to drop source managers from modules.
679 if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) {
680 StoredDiagnostic *ResultDiag = nullptr;
681 if (StoredDiags) {
682 StoredDiags->emplace_back(Level, Info);
683 ResultDiag = &StoredDiags->back();
684 }
685
686 if (StandaloneDiags) {
687 llvm::Optional<StoredDiagnostic> StoredDiag = None;
688 if (!ResultDiag) {
689 StoredDiag.emplace(Level, Info);
690 ResultDiag = StoredDiag.getPointer();
691 }
692 StandaloneDiags->push_back(
693 makeStandaloneDiagnostic(*LangOpts, *ResultDiag));
694 }
695 }
696 }
697
698 IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const {
699 return Reader;
700 }
701
702 ASTMutationListener *ASTUnit::getASTMutationListener() {
703 if (WriterData)
704 return &WriterData->Writer;
705 return nullptr;
706 }
707
708 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
709 if (WriterData)
710 return &WriterData->Writer;
711 return nullptr;
712 }
713
714 std::unique_ptr<llvm::MemoryBuffer>
715 ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
716 assert(FileMgr);
717 auto Buffer = FileMgr->getBufferForFile(Filename, UserFilesAreVolatile);
718 if (Buffer)
719 return std::move(*Buffer);
720 if (ErrorStr)
721 *ErrorStr = Buffer.getError().message();
722 return nullptr;
723 }
724
725 /// Configure the diagnostics object for use with ASTUnit.
726 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
727 ASTUnit &AST, bool CaptureDiagnostics) {
728 assert(Diags.get() && "no DiagnosticsEngine was provided");
729 if (CaptureDiagnostics)
730 Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr));
731 }
732
733 std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
734 const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
735 WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
736 const FileSystemOptions &FileSystemOpts, bool UseDebugInfo,
737 bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles,
738 bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors,
739 bool UserFilesAreVolatile) {
740 std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
741
742 // Recover resources if we crash before exiting this method.
743 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
744 ASTUnitCleanup(AST.get());
745 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
746 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
747 DiagCleanup(Diags.get());
748
749 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
750
751 AST->LangOpts = std::make_shared<LangOptions>();
752 AST->OnlyLocalDecls = OnlyLocalDecls;
753 AST->CaptureDiagnostics = CaptureDiagnostics;
754 AST->Diagnostics = Diags;
755 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
756 llvm::vfs::getRealFileSystem();
757 AST->FileMgr = new FileManager(FileSystemOpts, VFS);
758 AST->UserFilesAreVolatile = UserFilesAreVolatile;
759 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
760 AST->getFileManager(),
761 UserFilesAreVolatile);
762 AST->PCMCache = new MemoryBufferCache;
763 AST->HSOpts = std::make_shared<HeaderSearchOptions>();
764 AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat();
765 AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
766 AST->getSourceManager(),
767 AST->getDiagnostics(),
768 AST->getLangOpts(),
769 /*Target=*/nullptr));
770 AST->PPOpts = std::make_shared<PreprocessorOptions>();
771
772 for (const auto &RemappedFile : RemappedFiles)
773 AST->PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second);
774
775 // Gather Info for preprocessor construction later on.
776
777 HeaderSearch &HeaderInfo = *AST->HeaderInfo;
778 unsigned Counter;
779
780 AST->PP = std::make_shared<Preprocessor>(
781 AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts,
782 AST->getSourceManager(), *AST->PCMCache, HeaderInfo, AST->ModuleLoader,
783 /*IILookup=*/nullptr,
784 /*OwnsHeaderSearch=*/false);
785 Preprocessor &PP = *AST->PP;
786
787 if (ToLoad >= LoadASTOnly)
788 AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
789 PP.getIdentifierTable(), PP.getSelectorTable(),
790 PP.getBuiltinInfo());
791
792 bool disableValid = false;
793 if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
794 disableValid = true;
795 AST->Reader = new ASTReader(PP, AST->Ctx.get(), PCHContainerRdr, {},
796 /*isysroot=*/"",
797 /*DisableValidation=*/disableValid,
798 AllowPCHWithCompilerErrors);
799
800 AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>(
801 *AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts,
802 AST->TargetOpts, AST->Target, Counter));
803
804 // Attach the AST reader to the AST context as an external AST
805 // source, so that declarations will be deserialized from the
806 // AST file as needed.
807 // We need the external source to be set up before we read the AST, because
808 // eagerly-deserialized declarations may use it.
809 if (AST->Ctx)
810 AST->Ctx->setExternalSource(AST->Reader);
811
812 switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile,
813 SourceLocation(), ASTReader::ARR_None)) {
814 case ASTReader::Success:
815 break;
816
817 case ASTReader::Failure:
818 case ASTReader::Missing:
819 case ASTReader::OutOfDate:
820 case ASTReader::VersionMismatch:
821 case ASTReader::ConfigurationMismatch:
822 case ASTReader::HadErrors:
823 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
824 return nullptr;
825 }
826
827 AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile();
828
829 PP.setCounterValue(Counter);
830
831 // Create an AST consumer, even though it isn't used.
832 if (ToLoad >= LoadASTOnly)
833 AST->Consumer.reset(new ASTConsumer);
834
835 // Create a semantic analysis object and tell the AST reader about it.
836 if (ToLoad >= LoadEverything) {
837 AST->TheSema.reset(new Sema(PP, *AST->Ctx, *AST->Consumer));
838 AST->TheSema->Initialize();
839 AST->Reader->InitializeSema(*AST->TheSema);
840 }
841
842 // Tell the diagnostic client that we have started a source file.
843 AST->getDiagnostics().getClient()->BeginSourceFile(PP.getLangOpts(), &PP);
844
845 return AST;
846 }
847
848 /// Add the given macro to the hash of all top-level entities.
849 static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash) {
850 Hash = llvm::djbHash(MacroNameTok.getIdentifierInfo()->getName(), Hash);
851 }
852
853 namespace {
854
855 /// Preprocessor callback class that updates a hash value with the names
856 /// of all macros that have been defined by the translation unit.
857 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
858 unsigned &Hash;
859
860 public:
861 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {}
862
863 void MacroDefined(const Token &MacroNameTok,
864 const MacroDirective *MD) override {
865 AddDefinedMacroToHash(MacroNameTok, Hash);
866 }
867 };
868
869 } // namespace
870
871 /// Add the given declaration to the hash of all top-level entities.
872 static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
873 if (!D)
874 return;
875
876 DeclContext *DC = D->getDeclContext();
877 if (!DC)
878 return;
879
880 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
881 return;
882
883 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
884 if (const auto *EnumD = dyn_cast<EnumDecl>(D)) {
885 // For an unscoped enum include the enumerators in the hash since they
886 // enter the top-level namespace.
887 if (!EnumD->isScoped()) {
888 for (const auto *EI : EnumD->enumerators()) {
889 if (EI->getIdentifier())
890 Hash = llvm::djbHash(EI->getIdentifier()->getName(), Hash);
891 }
892 }
893 }
894
895 if (ND->getIdentifier())
896 Hash = llvm::djbHash(ND->getIdentifier()->getName(), Hash);
897 else if (DeclarationName Name = ND->getDeclName()) {
898 std::string NameStr = Name.getAsString();
899 Hash = llvm::djbHash(NameStr, Hash);
900 }
901 return;
902 }
903
904 if (const auto *ImportD = dyn_cast<ImportDecl>(D)) {
905 if (const Module *Mod = ImportD->getImportedModule()) {
906 std::string ModName = Mod->getFullModuleName();
907 Hash = llvm::djbHash(ModName, Hash);
908 }
909 return;
910 }
911 }
912
913 namespace {
914
915 class TopLevelDeclTrackerConsumer : public ASTConsumer {
916 ASTUnit &Unit;
917 unsigned &Hash;
918
919 public:
920 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
921 : Unit(_Unit), Hash(Hash) {
922 Hash = 0;
923 }
924
925 void handleTopLevelDecl(Decl *D) {
926 if (!D)
927 return;
928
929 // FIXME: Currently ObjC method declarations are incorrectly being
930 // reported as top-level declarations, even though their DeclContext
931 // is the containing ObjC @interface/@implementation. This is a
932 // fundamental problem in the parser right now.
933 if (isa<ObjCMethodDecl>(D))
934 return;
935
936 AddTopLevelDeclarationToHash(D, Hash);
937 Unit.addTopLevelDecl(D);
938
939 handleFileLevelDecl(D);
940 }
941
942 void handleFileLevelDecl(Decl *D) {
943 Unit.addFileLevelDecl(D);
944 if (auto *NSD = dyn_cast<NamespaceDecl>(D)) {
945 for (auto *I : NSD->decls())
946 handleFileLevelDecl(I);
947 }
948 }
949
950 bool HandleTopLevelDecl(DeclGroupRef D) override {
951 for (auto *TopLevelDecl : D)
952 handleTopLevelDecl(TopLevelDecl);
953 return true;
954 }
955
956 // We're not interested in "interesting" decls.
957 void HandleInterestingDecl(DeclGroupRef) override {}
958
959 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
960 for (auto *TopLevelDecl : D)
961 handleTopLevelDecl(TopLevelDecl);
962 }
963
964 ASTMutationListener *GetASTMutationListener() override {
965 return Unit.getASTMutationListener();
966 }
967
968 ASTDeserializationListener *GetASTDeserializationListener() override {
969 return Unit.getDeserializationListener();
970 }
971 };
972
973 class TopLevelDeclTrackerAction : public ASTFrontendAction {
974 public:
975 ASTUnit &Unit;
976
977 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
978 StringRef InFile) override {
979 CI.getPreprocessor().addPPCallbacks(
980 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
981 Unit.getCurrentTopLevelHashValue()));
982 return llvm::make_unique<TopLevelDeclTrackerConsumer>(
983 Unit, Unit.getCurrentTopLevelHashValue());
984 }
985
986 public:
987 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
988
989 bool hasCodeCompletionSupport() const override { return false; }
990
991 TranslationUnitKind getTranslationUnitKind() override {
992 return Unit.getTranslationUnitKind();
993 }
994 };
995
996 class ASTUnitPreambleCallbacks : public PreambleCallbacks {
997 public:
998 unsigned getHash() const { return Hash; }
999
1000 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
1001
1002 std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
1003 return std::move(TopLevelDeclIDs);
1004 }
1005
1006 void AfterPCHEmitted(ASTWriter &Writer) override {
1007 TopLevelDeclIDs.reserve(TopLevelDecls.size());
1008 for (const auto *D : TopLevelDecls) {
1009 // Invalid top-level decls may not have been serialized.
1010 if (D->isInvalidDecl())
1011 continue;
1012 TopLevelDeclIDs.push_back(Writer.getDeclID(D));
1013 }
1014 }
1015
1016 void HandleTopLevelDecl(DeclGroupRef DG) override {
1017 for (auto *D : DG) {
1018 // FIXME: Currently ObjC method declarations are incorrectly being
1019 // reported as top-level declarations, even though their DeclContext
1020 // is the containing ObjC @interface/@implementation. This is a
1021 // fundamental problem in the parser right now.
1022 if (isa<ObjCMethodDecl>(D))
1023 continue;
1024 AddTopLevelDeclarationToHash(D, Hash);
1025 TopLevelDecls.push_back(D);
1026 }
1027 }
1028
1029 std::unique_ptr<PPCallbacks> createPPCallbacks() override {
1030 return llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(Hash);
1031 }
1032
1033 private:
1034 unsigned Hash = 0;
1035 std::vector<Decl *> TopLevelDecls;
1036 std::vector<serialization::DeclID> TopLevelDeclIDs;
1037 llvm::SmallVector<ASTUnit::StandaloneDiagnostic, 4> PreambleDiags;
1038 };
1039
1040 } // namespace
1041
1042 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1043 return StoredDiag.getLocation().isValid();
1044 }
1045
1046 static void
1047 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1048 // Get rid of stored diagnostics except the ones from the driver which do not
1049 // have a source location.
1050 StoredDiags.erase(
1051 std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1052 StoredDiags.end());
1053 }
1054
1055 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1056 StoredDiagnostics,
1057 SourceManager &SM) {
1058 // The stored diagnostic has the old source manager in it; update
1059 // the locations to refer into the new source manager. Since we've
1060 // been careful to make sure that the source manager's state
1061 // before and after are identical, so that we can reuse the source
1062 // location itself.
1063 for (auto &SD : StoredDiagnostics) {
1064 if (SD.getLocation().isValid()) {
1065 FullSourceLoc Loc(SD.getLocation(), SM);
1066 SD.setLocation(Loc);
1067 }
1068 }
1069 }
1070
1071 /// Parse the source file into a translation unit using the given compiler
1072 /// invocation, replacing the current translation unit.
1073 ///
1074 /// \returns True if a failure occurred that causes the ASTUnit not to
1075 /// contain any translation-unit information, false otherwise.
1076 bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1077 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
1078 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1079 if (!Invocation)
1080 return true;
1081
1082 auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
1083 if (OverrideMainBuffer) {
1084 assert(Preamble &&
1085 "No preamble was built, but OverrideMainBuffer is not null");
1086 IntrusiveRefCntPtr<llvm::vfs::FileSystem> OldVFS = VFS;
1087 Preamble->AddImplicitPreamble(*CCInvocation, VFS, OverrideMainBuffer.get());
1088 if (OldVFS != VFS && FileMgr) {
1089 assert(OldVFS == FileMgr->getVirtualFileSystem() &&
1090 "VFS passed to Parse and VFS in FileMgr are different");
1091 FileMgr = new FileManager(FileMgr->getFileSystemOpts(), VFS);
1092 }
1093 }
1094
1095 // Create the compiler instance to use for building the AST.
1096 std::unique_ptr<CompilerInstance> Clang(
1097 new CompilerInstance(std::move(PCHContainerOps)));
1098 if (FileMgr && VFS) {
1099 assert(VFS == FileMgr->getVirtualFileSystem() &&
1100 "VFS passed to Parse and VFS in FileMgr are different");
1101 } else if (VFS) {
1102 Clang->setVirtualFileSystem(VFS);
1103 }
1104
1105 // Recover resources if we crash before exiting this method.
1106 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1107 CICleanup(Clang.get());
1108
1109 Clang->setInvocation(CCInvocation);
1110 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1111
1112 // Set up diagnostics, capturing any diagnostics that would
1113 // otherwise be dropped.
1114 Clang->setDiagnostics(&getDiagnostics());
1115
1116 // Create the target instance.
1117 Clang->setTarget(TargetInfo::CreateTargetInfo(
1118 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1119 if (!Clang->hasTarget())
1120 return true;
1121
1122 // Inform the target of the language options.
1123 //
1124 // FIXME: We shouldn't need to do this, the target should be immutable once
1125 // created. This complexity should be lifted elsewhere.
1126 Clang->getTarget().adjust(Clang->getLangOpts());
1127
1128 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1129 "Invocation must have exactly one source file!");
1130 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1131 InputKind::Source &&
1132 "FIXME: AST inputs not yet supported here!");
1133 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1134 InputKind::LLVM_IR &&
1135 "IR inputs not support here!");
1136
1137 // Configure the various subsystems.
1138 LangOpts = Clang->getInvocation().LangOpts;
1139 FileSystemOpts = Clang->getFileSystemOpts();
1140 if (!FileMgr) {
1141 Clang->createFileManager();
1142 FileMgr = &Clang->getFileManager();
1143 }
1144
1145 ResetForParse();
1146
1147 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1148 UserFilesAreVolatile);
1149 if (!OverrideMainBuffer) {
1150 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1151 TopLevelDeclsInPreamble.clear();
1152 }
1153
1154 // Create a file manager object to provide access to and cache the filesystem.
1155 Clang->setFileManager(&getFileManager());
1156
1157 // Create the source manager.
1158 Clang->setSourceManager(&getSourceManager());
1159
1160 // If the main file has been overridden due to the use of a preamble,
1161 // make that override happen and introduce the preamble.
1162 if (OverrideMainBuffer) {
1163 // The stored diagnostic has the old source manager in it; update
1164 // the locations to refer into the new source manager. Since we've
1165 // been careful to make sure that the source manager's state
1166 // before and after are identical, so that we can reuse the source
1167 // location itself.
1168 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1169
1170 // Keep track of the override buffer;
1171 SavedMainFileBuffer = std::move(OverrideMainBuffer);
1172 }
1173
1174 std::unique_ptr<TopLevelDeclTrackerAction> Act(
1175 new TopLevelDeclTrackerAction(*this));
1176
1177 // Recover resources if we crash before exiting this method.
1178 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1179 ActCleanup(Act.get());
1180
1181 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1182 goto error;
1183
1184 if (SavedMainFileBuffer)
1185 TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
1186 PreambleDiagnostics, StoredDiagnostics);
1187 else
1188 PreambleSrcLocCache.clear();
1189
1190 if (!Act->Execute())
1191 goto error;
1192
1193 transferASTDataFromCompilerInstance(*Clang);
1194
1195 Act->EndSourceFile();
1196
1197 FailedParseDiagnostics.clear();
1198
1199 return false;
1200
1201 error:
1202 // Remove the overridden buffer we used for the preamble.
1203 SavedMainFileBuffer = nullptr;
1204
1205 // Keep the ownership of the data in the ASTUnit because the client may
1206 // want to see the diagnostics.
1207 transferASTDataFromCompilerInstance(*Clang);
1208 FailedParseDiagnostics.swap(StoredDiagnostics);
1209 StoredDiagnostics.clear();
1210 NumStoredDiagnosticsFromDriver = 0;
1211 return true;
1212 }
1213
1214 static std::pair<unsigned, unsigned>
1215 makeStandaloneRange(CharSourceRange Range, const SourceManager &SM,
1216 const LangOptions &LangOpts) {
1217 CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1218 unsigned Offset = SM.getFileOffset(FileRange.getBegin());
1219 unsigned EndOffset = SM.getFileOffset(FileRange.getEnd());
1220 return std::make_pair(Offset, EndOffset);
1221 }
1222
1223 static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM,
1224 const LangOptions &LangOpts,
1225 const FixItHint &InFix) {
1226 ASTUnit::StandaloneFixIt OutFix;
1227 OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts);
1228 OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM,
1229 LangOpts);
1230 OutFix.CodeToInsert = InFix.CodeToInsert;
1231 OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions;
1232 return OutFix;
1233 }
1234
1235 static ASTUnit::StandaloneDiagnostic
1236 makeStandaloneDiagnostic(const LangOptions &LangOpts,
1237 const StoredDiagnostic &InDiag) {
1238 ASTUnit::StandaloneDiagnostic OutDiag;
1239 OutDiag.ID = InDiag.getID();
1240 OutDiag.Level = InDiag.getLevel();
1241 OutDiag.Message = InDiag.getMessage();
1242 OutDiag.LocOffset = 0;
1243 if (InDiag.getLocation().isInvalid())
1244 return OutDiag;
1245 const SourceManager &SM = InDiag.getLocation().getManager();
1246 SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation());
1247 OutDiag.Filename = SM.getFilename(FileLoc);
1248 if (OutDiag.Filename.empty())
1249 return OutDiag;
1250 OutDiag.LocOffset = SM.getFileOffset(FileLoc);
1251 for (const auto &Range : InDiag.getRanges())
1252 OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts));
1253 for (const auto &FixIt : InDiag.getFixIts())
1254 OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt));
1255
1256 return OutDiag;
1257 }
1258
1259 /// Attempt to build or re-use a precompiled preamble when (re-)parsing
1260 /// the source file.
1261 ///
1262 /// This routine will compute the preamble of the main source file. If a
1263 /// non-trivial preamble is found, it will precompile that preamble into a
1264 /// precompiled header so that the precompiled preamble can be used to reduce
1265 /// reparsing time. If a precompiled preamble has already been constructed,
1266 /// this routine will determine if it is still valid and, if so, avoid
1267 /// rebuilding the precompiled preamble.
1268 ///
1269 /// \param AllowRebuild When true (the default), this routine is
1270 /// allowed to rebuild the precompiled preamble if it is found to be
1271 /// out-of-date.
1272 ///
1273 /// \param MaxLines When non-zero, the maximum number of lines that
1274 /// can occur within the preamble.
1275 ///
1276 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1277 /// buffer that should be used in place of the main file when doing so.
1278 /// Otherwise, returns a NULL pointer.
1279 std::unique_ptr<llvm::MemoryBuffer>
1280 ASTUnit::getMainBufferWithPrecompiledPreamble(
1281 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1282 CompilerInvocation &PreambleInvocationIn,
1283 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild,
1284 unsigned MaxLines) {
1285 auto MainFilePath =
1286 PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile();
1287 std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer =
1288 getBufferForFileHandlingRemapping(PreambleInvocationIn, VFS.get(),
1289 MainFilePath, UserFilesAreVolatile);
1290 if (!MainFileBuffer)
1291 return nullptr;
1292
1293 PreambleBounds Bounds =
1294 ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(),
1295 MainFileBuffer.get(), MaxLines);
1296 if (!Bounds.Size)
1297 return nullptr;
1298
1299 if (Preamble) {
1300 if (Preamble->CanReuse(PreambleInvocationIn, MainFileBuffer.get(), Bounds,
1301 VFS.get())) {
1302 // Okay! We can re-use the precompiled preamble.
1303
1304 // Set the state of the diagnostic object to mimic its state
1305 // after parsing the preamble.
1306 getDiagnostics().Reset();
1307 ProcessWarningOptions(getDiagnostics(),
1308 PreambleInvocationIn.getDiagnosticOpts());
1309 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1310
1311 PreambleRebuildCounter = 1;
1312 return MainFileBuffer;
1313 } else {
1314 Preamble.reset();
1315 PreambleDiagnostics.clear();
1316 TopLevelDeclsInPreamble.clear();
1317 PreambleSrcLocCache.clear();
1318 PreambleRebuildCounter = 1;
1319 }
1320 }
1321
1322 // If the preamble rebuild counter > 1, it's because we previously
1323 // failed to build a preamble and we're not yet ready to try
1324 // again. Decrement the counter and return a failure.
1325 if (PreambleRebuildCounter > 1) {
1326 --PreambleRebuildCounter;
1327 return nullptr;
1328 }
1329
1330 assert(!Preamble && "No Preamble should be stored at that point");
1331 // If we aren't allowed to rebuild the precompiled preamble, just
1332 // return now.
1333 if (!AllowRebuild)
1334 return nullptr;
1335
1336 SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone;
1337 SmallVector<StoredDiagnostic, 4> NewPreambleDiags;
1338 ASTUnitPreambleCallbacks Callbacks;
1339 {
1340 llvm::Optional<CaptureDroppedDiagnostics> Capture;
1341 if (CaptureDiagnostics)
1342 Capture.emplace(/*RequestCapture=*/true, *Diagnostics, &NewPreambleDiags,
1343 &NewPreambleDiagsStandalone);
1344
1345 // We did not previously compute a preamble, or it can't be reused anyway.
1346 SimpleTimer PreambleTimer(WantTiming);
1347 PreambleTimer.setOutput("Precompiling preamble");
1348
1349 const bool PreviousSkipFunctionBodies =
1350 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies;
1351 if (SkipFunctionBodies == SkipFunctionBodiesScope::Preamble)
1352 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = true;
1353
1354 llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build(
1355 PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS,
1356 PCHContainerOps, /*StoreInMemory=*/false, Callbacks);
1357
1358 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies =
1359 PreviousSkipFunctionBodies;
1360
1361 if (NewPreamble) {
1362 Preamble = std::move(*NewPreamble);
1363 PreambleRebuildCounter = 1;
1364 } else {
1365 switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) {
1366 case BuildPreambleError::CouldntCreateTempFile:
1367 // Try again next time.
1368 PreambleRebuildCounter = 1;
1369 return nullptr;
1370 case BuildPreambleError::CouldntCreateTargetInfo:
1371 case BuildPreambleError::BeginSourceFileFailed:
1372 case BuildPreambleError::CouldntEmitPCH:
1373 // These erros are more likely to repeat, retry after some period.
1374 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1375 return nullptr;
1376 }
1377 llvm_unreachable("unexpected BuildPreambleError");
1378 }
1379 }
1380
1381 assert(Preamble && "Preamble wasn't built");
1382
1383 TopLevelDecls.clear();
1384 TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs();
1385 PreambleTopLevelHashValue = Callbacks.getHash();
1386
1387 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1388
1389 checkAndRemoveNonDriverDiags(NewPreambleDiags);
1390 StoredDiagnostics = std::move(NewPreambleDiags);
1391 PreambleDiagnostics = std::move(NewPreambleDiagsStandalone);
1392
1393 // If the hash of top-level entities differs from the hash of the top-level
1394 // entities the last time we rebuilt the preamble, clear out the completion
1395 // cache.
1396 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1397 CompletionCacheTopLevelHashValue = 0;
1398 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1399 }
1400
1401 return MainFileBuffer;
1402 }
1403
1404 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1405 assert(Preamble && "Should only be called when preamble was built");
1406
1407 std::vector<Decl *> Resolved;
1408 Resolved.reserve(TopLevelDeclsInPreamble.size());
1409 ExternalASTSource &Source = *getASTContext().getExternalSource();
1410 for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
1411 // Resolve the declaration ID to an actual declaration, possibly
1412 // deserializing the declaration in the process.
1413 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
1414 Resolved.push_back(D);
1415 }
1416 TopLevelDeclsInPreamble.clear();
1417 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1418 }
1419
1420 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1421 // Steal the created target, context, and preprocessor if they have been
1422 // created.
1423 assert(CI.hasInvocation() && "missing invocation");
1424 LangOpts = CI.getInvocation().LangOpts;
1425 TheSema = CI.takeSema();
1426 Consumer = CI.takeASTConsumer();
1427 if (CI.hasASTContext())
1428 Ctx = &CI.getASTContext();
1429 if (CI.hasPreprocessor())
1430 PP = CI.getPreprocessorPtr();
1431 CI.setSourceManager(nullptr);
1432 CI.setFileManager(nullptr);
1433 if (CI.hasTarget())
1434 Target = &CI.getTarget();
1435 Reader = CI.getModuleManager();
1436 HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1437 }
1438
1439 StringRef ASTUnit::getMainFileName() const {
1440 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1441 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1442 if (Input.isFile())
1443 return Input.getFile();
1444 else
1445 return Input.getBuffer()->getBufferIdentifier();
1446 }
1447
1448 if (SourceMgr) {
1449 if (const FileEntry *
1450 FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1451 return FE->getName();
1452 }
1453
1454 return {};
1455 }
1456
1457 StringRef ASTUnit::getASTFileName() const {
1458 if (!isMainFileAST())
1459 return {};
1460
1461 serialization::ModuleFile &
1462 Mod = Reader->getModuleManager().getPrimaryModule();
1463 return Mod.FileName;
1464 }
1465
1466 std::unique_ptr<ASTUnit>
1467 ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
1468 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1469 bool CaptureDiagnostics, bool UserFilesAreVolatile) {
1470 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1471 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1472 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1473 createVFSFromCompilerInvocation(*CI, *Diags);
1474 AST->Diagnostics = Diags;
1475 AST->FileSystemOpts = CI->getFileSystemOpts();
1476 AST->Invocation = std::move(CI);
1477 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1478 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1479 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1480 UserFilesAreVolatile);
1481 AST->PCMCache = new MemoryBufferCache;
1482
1483 return AST;
1484 }
1485
1486 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
1487 std::shared_ptr<CompilerInvocation> CI,
1488 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1489 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action,
1490 ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1491 bool OnlyLocalDecls, bool CaptureDiagnostics,
1492 unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1493 bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
1494 std::unique_ptr<ASTUnit> *ErrAST) {
1495 assert(CI && "A CompilerInvocation is required");
1496
1497 std::unique_ptr<ASTUnit> OwnAST;
1498 ASTUnit *AST = Unit;
1499 if (!AST) {
1500 // Create the AST unit.
1501 OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile);
1502 AST = OwnAST.get();
1503 if (!AST)
1504 return nullptr;
1505 }
1506
1507 if (!ResourceFilesPath.empty()) {
1508 // Override the resources path.
1509 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1510 }
1511 AST->OnlyLocalDecls = OnlyLocalDecls;
1512 AST->CaptureDiagnostics = CaptureDiagnostics;
1513 if (PrecompilePreambleAfterNParses > 0)
1514 AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1515 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1516 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1517 AST->IncludeBriefCommentsInCodeCompletion
1518 = IncludeBriefCommentsInCodeCompletion;
1519
1520 // Recover resources if we crash before exiting this method.
1521 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1522 ASTUnitCleanup(OwnAST.get());
1523 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1524 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1525 DiagCleanup(Diags.get());
1526
1527 // We'll manage file buffers ourselves.
1528 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1529 CI->getFrontendOpts().DisableFree = false;
1530 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1531
1532 // Create the compiler instance to use for building the AST.
1533 std::unique_ptr<CompilerInstance> Clang(
1534 new CompilerInstance(std::move(PCHContainerOps)));
1535
1536 // Recover resources if we crash before exiting this method.
1537 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1538 CICleanup(Clang.get());
1539
1540 Clang->setInvocation(std::move(CI));
1541 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1542
1543 // Set up diagnostics, capturing any diagnostics that would
1544 // otherwise be dropped.
1545 Clang->setDiagnostics(&AST->getDiagnostics());
1546
1547 // Create the target instance.
1548 Clang->setTarget(TargetInfo::CreateTargetInfo(
1549 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
1550 if (!Clang->hasTarget())
1551 return nullptr;
1552
1553 // Inform the target of the language options.
1554 //
1555 // FIXME: We shouldn't need to do this, the target should be immutable once
1556 // created. This complexity should be lifted elsewhere.
1557 Clang->getTarget().adjust(Clang->getLangOpts());
1558
1559 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1560 "Invocation must have exactly one source file!");
1561 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1562 InputKind::Source &&
1563 "FIXME: AST inputs not yet supported here!");
1564 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1565 InputKind::LLVM_IR &&
1566 "IR inputs not support here!");
1567
1568 // Configure the various subsystems.
1569 AST->TheSema.reset();
1570 AST->Ctx = nullptr;
1571 AST->PP = nullptr;
1572 AST->Reader = nullptr;
1573
1574 // Create a file manager object to provide access to and cache the filesystem.
1575 Clang->setFileManager(&AST->getFileManager());
1576
1577 // Create the source manager.
1578 Clang->setSourceManager(&AST->getSourceManager());
1579
1580 FrontendAction *Act = Action;
1581
1582 std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1583 if (!Act) {
1584 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1585 Act = TrackerAct.get();
1586 }
1587
1588 // Recover resources if we crash before exiting this method.
1589 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1590 ActCleanup(TrackerAct.get());
1591
1592 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1593 AST->transferASTDataFromCompilerInstance(*Clang);
1594 if (OwnAST && ErrAST)
1595 ErrAST->swap(OwnAST);
1596
1597 return nullptr;
1598 }
1599
1600 if (Persistent && !TrackerAct) {
1601 Clang->getPreprocessor().addPPCallbacks(
1602 llvm::make_unique<MacroDefinitionTrackerPPCallbacks>(
1603 AST->getCurrentTopLevelHashValue()));
1604 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1605 if (Clang->hasASTConsumer())
1606 Consumers.push_back(Clang->takeASTConsumer());
1607 Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>(
1608 *AST, AST->getCurrentTopLevelHashValue()));
1609 Clang->setASTConsumer(
1610 llvm::make_unique<MultiplexConsumer>(std::move(Consumers)));
1611 }
1612 if (!Act->Execute()) {
1613 AST->transferASTDataFromCompilerInstance(*Clang);
1614 if (OwnAST && ErrAST)
1615 ErrAST->swap(OwnAST);
1616
1617 return nullptr;
1618 }
1619
1620 // Steal the created target, context, and preprocessor.
1621 AST->transferASTDataFromCompilerInstance(*Clang);
1622
1623 Act->EndSourceFile();
1624
1625 if (OwnAST)
1626 return OwnAST.release();
1627 else
1628 return AST;
1629 }
1630
1631 bool ASTUnit::LoadFromCompilerInvocation(
1632 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1633 unsigned PrecompilePreambleAfterNParses,
1634 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1635 if (!Invocation)
1636 return true;
1637
1638 assert(VFS && "VFS is null");
1639
1640 // We'll manage file buffers ourselves.
1641 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1642 Invocation->getFrontendOpts().DisableFree = false;
1643 getDiagnostics().Reset();
1644 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1645
1646 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1647 if (PrecompilePreambleAfterNParses > 0) {
1648 PreambleRebuildCounter = PrecompilePreambleAfterNParses;
1649 OverrideMainBuffer =
1650 getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1651 getDiagnostics().Reset();
1652 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1653 }
1654
1655 SimpleTimer ParsingTimer(WantTiming);
1656 ParsingTimer.setOutput("Parsing " + getMainFileName());
1657
1658 // Recover resources if we crash before exiting this method.
1659 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1660 MemBufferCleanup(OverrideMainBuffer.get());
1661
1662 return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1663 }
1664
1665 std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1666 std::shared_ptr<CompilerInvocation> CI,
1667 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1668 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1669 bool OnlyLocalDecls, bool CaptureDiagnostics,
1670 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1671 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1672 bool UserFilesAreVolatile) {
1673 // Create the AST unit.
1674 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1675 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1676 AST->Diagnostics = Diags;
1677 AST->OnlyLocalDecls = OnlyLocalDecls;
1678 AST->CaptureDiagnostics = CaptureDiagnostics;
1679 AST->TUKind = TUKind;
1680 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1681 AST->IncludeBriefCommentsInCodeCompletion
1682 = IncludeBriefCommentsInCodeCompletion;
1683 AST->Invocation = std::move(CI);
1684 AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1685 AST->FileMgr = FileMgr;
1686 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1687
1688 // Recover resources if we crash before exiting this method.
1689 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1690 ASTUnitCleanup(AST.get());
1691 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1692 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1693 DiagCleanup(Diags.get());
1694
1695 if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1696 PrecompilePreambleAfterNParses,
1697 AST->FileMgr->getVirtualFileSystem()))
1698 return nullptr;
1699 return AST;
1700 }
1701
1702 ASTUnit *ASTUnit::LoadFromCommandLine(
1703 const char **ArgBegin, const char **ArgEnd,
1704 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1705 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1706 bool OnlyLocalDecls, bool CaptureDiagnostics,
1707 ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1708 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1709 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1710 bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies,
1711 bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,
1712 llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,
1713 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1714 assert(Diags.get() && "no DiagnosticsEngine was provided");
1715
1716 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1717
1718 std::shared_ptr<CompilerInvocation> CI;
1719
1720 {
1721 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1722 &StoredDiagnostics, nullptr);
1723
1724 CI = createInvocationFromCommandLine(
1725 llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS);
1726 if (!CI)
1727 return nullptr;
1728 }
1729
1730 // Override any files that need remapping
1731 for (const auto &RemappedFile : RemappedFiles) {
1732 CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1733 RemappedFile.second);
1734 }
1735 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1736 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1737 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1738 PPOpts.SingleFileParseMode = SingleFileParse;
1739
1740 // Override the resources path.
1741 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1742
1743 CI->getFrontendOpts().SkipFunctionBodies =
1744 SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile;
1745
1746 if (ModuleFormat)
1747 CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue();
1748
1749 // Create the AST unit.
1750 std::unique_ptr<ASTUnit> AST;
1751 AST.reset(new ASTUnit(false));
1752 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1753 AST->StoredDiagnostics.swap(StoredDiagnostics);
1754 ConfigureDiags(Diags, *AST, CaptureDiagnostics);
1755 AST->Diagnostics = Diags;
1756 AST->FileSystemOpts = CI->getFileSystemOpts();
1757 if (!VFS)
1758 VFS = llvm::vfs::getRealFileSystem();
1759 VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS);
1760 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1761 AST->PCMCache = new MemoryBufferCache;
1762 AST->OnlyLocalDecls = OnlyLocalDecls;
1763 AST->CaptureDiagnostics = CaptureDiagnostics;
1764 AST->TUKind = TUKind;
1765 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1766 AST->IncludeBriefCommentsInCodeCompletion
1767 = IncludeBriefCommentsInCodeCompletion;
1768 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1769 AST->Invocation = CI;
1770 AST->SkipFunctionBodies = SkipFunctionBodies;
1771 if (ForSerialization)
1772 AST->WriterData.reset(new ASTWriterData(*AST->PCMCache));
1773 // Zero out now to ease cleanup during crash recovery.
1774 CI = nullptr;
1775 Diags = nullptr;
1776
1777 // Recover resources if we crash before exiting this method.
1778 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1779 ASTUnitCleanup(AST.get());
1780
1781 if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps),
1782 PrecompilePreambleAfterNParses,
1783 VFS)) {
1784 // Some error occurred, if caller wants to examine diagnostics, pass it the
1785 // ASTUnit.
1786 if (ErrAST) {
1787 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
1788 ErrAST->swap(AST);
1789 }
1790 return nullptr;
1791 }
1792
1793 return AST.release();
1794 }
1795
1796 bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1797 ArrayRef<RemappedFile> RemappedFiles,
1798 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1799 if (!Invocation)
1800 return true;
1801
1802 if (!VFS) {
1803 assert(FileMgr && "FileMgr is null on Reparse call");
1804 VFS = FileMgr->getVirtualFileSystem();
1805 }
1806
1807 clearFileLevelDecls();
1808
1809 SimpleTimer ParsingTimer(WantTiming);
1810 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1811
1812 // Remap files.
1813 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1814 for (const auto &RB : PPOpts.RemappedFileBuffers)
1815 delete RB.second;
1816
1817 Invocation->getPreprocessorOpts().clearRemappedFiles();
1818 for (const auto &RemappedFile : RemappedFiles) {
1819 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first,
1820 RemappedFile.second);
1821 }
1822
1823 // If we have a preamble file lying around, or if we might try to
1824 // build a precompiled preamble, do so now.
1825 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1826 if (Preamble || PreambleRebuildCounter > 0)
1827 OverrideMainBuffer =
1828 getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS);
1829
1830 // Clear out the diagnostics state.
1831 FileMgr.reset();
1832 getDiagnostics().Reset();
1833 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1834 if (OverrideMainBuffer)
1835 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1836
1837 // Parse the sources
1838 bool Result =
1839 Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS);
1840
1841 // If we're caching global code-completion results, and the top-level
1842 // declarations have changed, clear out the code-completion cache.
1843 if (!Result && ShouldCacheCodeCompletionResults &&
1844 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1845 CacheCodeCompletionResults();
1846
1847 // We now need to clear out the completion info related to this translation
1848 // unit; it'll be recreated if necessary.
1849 CCTUInfo.reset();
1850
1851 return Result;
1852 }
1853
1854 void ASTUnit::ResetForParse() {
1855 SavedMainFileBuffer.reset();
1856
1857 SourceMgr.reset();
1858 TheSema.reset();
1859 Ctx.reset();
1860 PP.reset();
1861 Reader.reset();
1862
1863 TopLevelDecls.clear();
1864 clearFileLevelDecls();
1865 }
1866
1867 //----------------------------------------------------------------------------//
1868 // Code completion
1869 //----------------------------------------------------------------------------//
1870
1871 namespace {
1872
1873 /// Code completion consumer that combines the cached code-completion
1874 /// results from an ASTUnit with the code-completion results provided to it,
1875 /// then passes the result on to
1876 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1877 uint64_t NormalContexts;
1878 ASTUnit &AST;
1879 CodeCompleteConsumer &Next;
1880
1881 public:
1882 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1883 const CodeCompleteOptions &CodeCompleteOpts)
1884 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
1885 AST(AST), Next(Next) {
1886 // Compute the set of contexts in which we will look when we don't have
1887 // any information about the specific context.
1888 NormalContexts
1889 = (1LL << CodeCompletionContext::CCC_TopLevel)
1890 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
1891 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
1892 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
1893 | (1LL << CodeCompletionContext::CCC_Statement)
1894 | (1LL << CodeCompletionContext::CCC_Expression)
1895 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
1896 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
1897 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
1898 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
1899 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
1900 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
1901 | (1LL << CodeCompletionContext::CCC_Recovery);
1902
1903 if (AST.getASTContext().getLangOpts().CPlusPlus)
1904 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
1905 | (1LL << CodeCompletionContext::CCC_UnionTag)
1906 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
1907 }
1908
1909 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1910 CodeCompletionResult *Results,
1911 unsigned NumResults) override;
1912
1913 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1914 OverloadCandidate *Candidates,
1915 unsigned NumCandidates,
1916 SourceLocation OpenParLoc) override {
1917 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates,
1918 OpenParLoc);
1919 }
1920
1921 CodeCompletionAllocator &getAllocator() override {
1922 return Next.getAllocator();
1923 }
1924
1925 CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
1926 return Next.getCodeCompletionTUInfo();
1927 }
1928 };
1929
1930 } // namespace
1931
1932 /// Helper function that computes which global names are hidden by the
1933 /// local code-completion results.
1934 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1935 CodeCompletionResult *Results,
1936 unsigned NumResults,
1937 ASTContext &Ctx,
1938 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1939 bool OnlyTagNames = false;
1940 switch (Context.getKind()) {
1941 case CodeCompletionContext::CCC_Recovery:
1942 case CodeCompletionContext::CCC_TopLevel:
1943 case CodeCompletionContext::CCC_ObjCInterface:
1944 case CodeCompletionContext::CCC_ObjCImplementation:
1945 case CodeCompletionContext::CCC_ObjCIvarList:
1946 case CodeCompletionContext::CCC_ClassStructUnion:
1947 case CodeCompletionContext::CCC_Statement:
1948 case CodeCompletionContext::CCC_Expression:
1949 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1950 case CodeCompletionContext::CCC_DotMemberAccess:
1951 case CodeCompletionContext::CCC_ArrowMemberAccess:
1952 case CodeCompletionContext::CCC_ObjCPropertyAccess:
1953 case CodeCompletionContext::CCC_Namespace:
1954 case CodeCompletionContext::CCC_Type:
1955 case CodeCompletionContext::CCC_Symbol:
1956 case CodeCompletionContext::CCC_SymbolOrNewName:
1957 case CodeCompletionContext::CCC_ParenthesizedExpression:
1958 case CodeCompletionContext::CCC_ObjCInterfaceName:
1959 break;
1960
1961 case CodeCompletionContext::CCC_EnumTag:
1962 case CodeCompletionContext::CCC_UnionTag:
1963 case CodeCompletionContext::CCC_ClassOrStructTag:
1964 OnlyTagNames = true;
1965 break;
1966
1967 case CodeCompletionContext::CCC_ObjCProtocolName:
1968 case CodeCompletionContext::CCC_MacroName:
1969 case CodeCompletionContext::CCC_MacroNameUse:
1970 case CodeCompletionContext::CCC_PreprocessorExpression:
1971 case CodeCompletionContext::CCC_PreprocessorDirective:
1972 case CodeCompletionContext::CCC_NaturalLanguage:
1973 case CodeCompletionContext::CCC_SelectorName:
1974 case CodeCompletionContext::CCC_TypeQualifiers:
1975 case CodeCompletionContext::CCC_Other:
1976 case CodeCompletionContext::CCC_OtherWithMacros:
1977 case CodeCompletionContext::CCC_ObjCInstanceMessage:
1978 case CodeCompletionContext::CCC_ObjCClassMessage:
1979 case CodeCompletionContext::CCC_ObjCCategoryName:
1980 case CodeCompletionContext::CCC_IncludedFile:
1981 case CodeCompletionContext::CCC_NewName:
1982 // We're looking for nothing, or we're looking for names that cannot
1983 // be hidden.
1984 return;
1985 }
1986
1987 using Result = CodeCompletionResult;
1988 for (unsigned I = 0; I != NumResults; ++I) {
1989 if (Results[I].Kind != Result::RK_Declaration)
1990 continue;
1991
1992 unsigned IDNS
1993 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1994
1995 bool Hiding = false;
1996 if (OnlyTagNames)
1997 Hiding = (IDNS & Decl::IDNS_Tag);
1998 else {
1999 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2000 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2001 Decl::IDNS_NonMemberOperator);
2002 if (Ctx.getLangOpts().CPlusPlus)
2003 HiddenIDNS |= Decl::IDNS_Tag;
2004 Hiding = (IDNS & HiddenIDNS);
2005 }
2006
2007 if (!Hiding)
2008 continue;
2009
2010 DeclarationName Name = Results[I].Declaration->getDeclName();
2011 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2012 HiddenNames.insert(Identifier->getName());
2013 else
2014 HiddenNames.insert(Name.getAsString());
2015 }
2016 }
2017
2018 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2019 CodeCompletionContext Context,
2020 CodeCompletionResult *Results,
2021 unsigned NumResults) {
2022 // Merge the results we were given with the results we cached.
2023 bool AddedResult = false;
2024 uint64_t InContexts =
2025 Context.getKind() == CodeCompletionContext::CCC_Recovery
2026 ? NormalContexts : (1LL << Context.getKind());
2027 // Contains the set of names that are hidden by "local" completion results.
2028 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2029 using Result = CodeCompletionResult;
2030 SmallVector<Result, 8> AllResults;
2031 for (ASTUnit::cached_completion_iterator
2032 C = AST.cached_completion_begin(),
2033 CEnd = AST.cached_completion_end();
2034 C != CEnd; ++C) {
2035 // If the context we are in matches any of the contexts we are
2036 // interested in, we'll add this result.
2037 if ((C->ShowInContexts & InContexts) == 0)
2038 continue;
2039
2040 // If we haven't added any results previously, do so now.
2041 if (!AddedResult) {
2042 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2043 HiddenNames);
2044 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2045 AddedResult = true;
2046 }
2047
2048 // Determine whether this global completion result is hidden by a local
2049 // completion result. If so, skip it.
2050 if (C->Kind != CXCursor_MacroDefinition &&
2051 HiddenNames.count(C->Completion->getTypedText()))
2052 continue;
2053
2054 // Adjust priority based on similar type classes.
2055 unsigned Priority = C->Priority;
2056 CodeCompletionString *Completion = C->Completion;
2057 if (!Context.getPreferredType().isNull()) {
2058 if (C->Kind == CXCursor_MacroDefinition) {
2059 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2060 S.getLangOpts(),
2061 Context.getPreferredType()->isAnyPointerType());
2062 } else if (C->Type) {
2063 CanQualType Expected
2064 = S.Context.getCanonicalType(
2065 Context.getPreferredType().getUnqualifiedType());
2066 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2067 if (ExpectedSTC == C->TypeClass) {
2068 // We know this type is similar; check for an exact match.
2069 llvm::StringMap<unsigned> &CachedCompletionTypes
2070 = AST.getCachedCompletionTypes();
2071 llvm::StringMap<unsigned>::iterator Pos
2072 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2073 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2074 Priority /= CCF_ExactTypeMatch;
2075 else
2076 Priority /= CCF_SimilarTypeMatch;
2077 }
2078 }
2079 }
2080
2081 // Adjust the completion string, if required.
2082 if (C->Kind == CXCursor_MacroDefinition &&
2083 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2084 // Create a new code-completion string that just contains the
2085 // macro name, without its arguments.
2086 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2087 CCP_CodePattern, C->Availability);
2088 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2089 Priority = CCP_CodePattern;
2090 Completion = Builder.TakeString();
2091 }
2092
2093 AllResults.push_back(Result(Completion, Priority, C->Kind,
2094 C->Availability));
2095 }
2096
2097 // If we did not add any cached completion results, just forward the
2098 // results we were given to the next consumer.
2099 if (!AddedResult) {
2100 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2101 return;
2102 }
2103
2104 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2105 AllResults.size());
2106 }
2107
2108 void ASTUnit::CodeComplete(
2109 StringRef File, unsigned Line, unsigned Column,
2110 ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2111 bool IncludeCodePatterns, bool IncludeBriefComments,
2112 CodeCompleteConsumer &Consumer,
2113 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2114 DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2115 FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2116 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2117 if (!Invocation)
2118 return;
2119
2120 SimpleTimer CompletionTimer(WantTiming);
2121 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2122 Twine(Line) + ":" + Twine(Column));
2123
2124 auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
2125
2126 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2127 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2128 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2129
2130 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2131 CachedCompletionResults.empty();
2132 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2133 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2134 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2135 CodeCompleteOpts.LoadExternal = Consumer.loadExternal();
2136 CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts();
2137
2138 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2139
2140 FrontendOpts.CodeCompletionAt.FileName = File;
2141 FrontendOpts.CodeCompletionAt.Line = Line;
2142 FrontendOpts.CodeCompletionAt.Column = Column;
2143
2144 // Set the language options appropriately.
2145 LangOpts = *CCInvocation->getLangOpts();
2146
2147 // Spell-checking and warnings are wasteful during code-completion.
2148 LangOpts.SpellChecking = false;
2149 CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2150
2151 std::unique_ptr<CompilerInstance> Clang(
2152 new CompilerInstance(PCHContainerOps));
2153
2154 // Recover resources if we crash before exiting this method.
2155 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2156 CICleanup(Clang.get());
2157
2158 auto &Inv = *CCInvocation;
2159 Clang->setInvocation(std::move(CCInvocation));
2160 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2161
2162 // Set up diagnostics, capturing any diagnostics produced.
2163 Clang->setDiagnostics(&Diag);
2164 CaptureDroppedDiagnostics Capture(true,
2165 Clang->getDiagnostics(),
2166 &StoredDiagnostics, nullptr);
2167 ProcessWarningOptions(Diag, Inv.getDiagnosticOpts());
2168
2169 // Create the target instance.
2170 Clang->setTarget(TargetInfo::CreateTargetInfo(
2171 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
2172 if (!Clang->hasTarget()) {
2173 Clang->setInvocation(nullptr);
2174 return;
2175 }
2176
2177 // Inform the target of the language options.
2178 //
2179 // FIXME: We shouldn't need to do this, the target should be immutable once
2180 // created. This complexity should be lifted elsewhere.
2181 Clang->getTarget().adjust(Clang->getLangOpts());
2182
2183 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2184 "Invocation must have exactly one source file!");
2185 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
2186 InputKind::Source &&
2187 "FIXME: AST inputs not yet supported here!");
2188 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
2189 InputKind::LLVM_IR &&
2190 "IR inputs not support here!");
2191
2192 // Use the source and file managers that we were given.
2193 Clang->setFileManager(&FileMgr);
2194 Clang->setSourceManager(&SourceMgr);
2195
2196 // Remap files.
2197 PreprocessorOpts.clearRemappedFiles();
2198 PreprocessorOpts.RetainRemappedFileBuffers = true;
2199 for (const auto &RemappedFile : RemappedFiles) {
2200 PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
2201 OwnedBuffers.push_back(RemappedFile.second);
2202 }
2203
2204 // Use the code completion consumer we were given, but adding any cached
2205 // code-completion results.
2206 AugmentedCodeCompleteConsumer *AugmentedConsumer
2207 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2208 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2209
2210 // If we have a precompiled preamble, try to use it. We only allow
2211 // the use of the precompiled preamble if we're if the completion
2212 // point is within the main file, after the end of the precompiled
2213 // preamble.
2214 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2215 if (Preamble) {
2216 std::string CompleteFilePath(File);
2217
2218 auto VFS = FileMgr.getVirtualFileSystem();
2219 auto CompleteFileStatus = VFS->status(CompleteFilePath);
2220 if (CompleteFileStatus) {
2221 llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID();
2222
2223 std::string MainPath(OriginalSourceFile);
2224 auto MainStatus = VFS->status(MainPath);
2225 if (MainStatus) {
2226 llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID();
2227 if (CompleteFileID == MainID && Line > 1)
2228 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2229 PCHContainerOps, Inv, VFS, false, Line - 1);
2230 }
2231 }
2232 }
2233
2234 // If the main file has been overridden due to the use of a preamble,
2235 // make that override happen and introduce the preamble.
2236 if (OverrideMainBuffer) {
2237 assert(Preamble &&
2238 "No preamble was built, but OverrideMainBuffer is not null");
2239
2240 auto VFS = FileMgr.getVirtualFileSystem();
2241 Preamble->AddImplicitPreamble(Clang->getInvocation(), VFS,
2242 OverrideMainBuffer.get());
2243 // FIXME: there is no way to update VFS if it was changed by
2244 // AddImplicitPreamble as FileMgr is accepted as a parameter by this method.
2245 // We use on-disk preambles instead and rely on FileMgr's VFS to ensure the
2246 // PCH files are always readable.
2247 OwnedBuffers.push_back(OverrideMainBuffer.release());
2248 } else {
2249 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2250 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2251 }
2252
2253 // Disable the preprocessing record if modules are not enabled.
2254 if (!Clang->getLangOpts().Modules)
2255 PreprocessorOpts.DetailedRecord = false;
2256
2257 std::unique_ptr<SyntaxOnlyAction> Act;
2258 Act.reset(new SyntaxOnlyAction);
2259 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2260 Act->Execute();
2261 Act->EndSourceFile();
2262 }
2263 }
2264
2265 bool ASTUnit::Save(StringRef File) {
2266 if (HadModuleLoaderFatalFailure)
2267 return true;
2268
2269 // Write to a temporary file and later rename it to the actual file, to avoid
2270 // possible race conditions.
2271 SmallString<128> TempPath;
2272 TempPath = File;
2273 TempPath += "-%%%%%%%%";
2274 int fd;
2275 if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath))
2276 return true;
2277
2278 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2279 // unconditionally create a stat cache when we parse the file?
2280 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2281
2282 serialize(Out);
2283 Out.close();
2284 if (Out.has_error()) {
2285 Out.clear_error();
2286 return true;
2287 }
2288
2289 if (llvm::sys::fs::rename(TempPath, File)) {
2290 llvm::sys::fs::remove(TempPath);
2291 return true;
2292 }
2293
2294 return false;
2295 }
2296
2297 static bool serializeUnit(ASTWriter &Writer,
2298 SmallVectorImpl<char> &Buffer,
2299 Sema &S,
2300 bool hasErrors,
2301 raw_ostream &OS) {
2302 Writer.WriteAST(S, std::string(), nullptr, "", hasErrors);
2303
2304 // Write the generated bitstream to "Out".
2305 if (!Buffer.empty())
2306 OS.write(Buffer.data(), Buffer.size());
2307
2308 return false;
2309 }
2310
2311 bool ASTUnit::serialize(raw_ostream &OS) {
2312 // For serialization we are lenient if the errors were only warn-as-error kind.
2313 bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred();
2314
2315 if (WriterData)
2316 return serializeUnit(WriterData->Writer, WriterData->Buffer,
2317 getSema(), hasErrors, OS);
2318
2319 SmallString<128> Buffer;
2320 llvm::BitstreamWriter Stream(Buffer);
2321 MemoryBufferCache PCMCache;
2322 ASTWriter Writer(Stream, Buffer, PCMCache, {});
2323 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2324 }
2325
2326 using SLocRemap = ContinuousRangeMap<unsigned, int, 2>;
2327
2328 void ASTUnit::TranslateStoredDiagnostics(
2329 FileManager &FileMgr,
2330 SourceManager &SrcMgr,
2331 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
2332 SmallVectorImpl<StoredDiagnostic> &Out) {
2333 // Map the standalone diagnostic into the new source manager. We also need to
2334 // remap all the locations to the new view. This includes the diag location,
2335 // any associated source ranges, and the source ranges of associated fix-its.
2336 // FIXME: There should be a cleaner way to do this.
2337 SmallVector<StoredDiagnostic, 4> Result;
2338 Result.reserve(Diags.size());
2339
2340 for (const auto &SD : Diags) {
2341 // Rebuild the StoredDiagnostic.
2342 if (SD.Filename.empty())
2343 continue;
2344 const FileEntry *FE = FileMgr.getFile(SD.Filename);
2345 if (!FE)
2346 continue;
2347 SourceLocation FileLoc;
2348 auto ItFileID = PreambleSrcLocCache.find(SD.Filename);
2349 if (ItFileID == PreambleSrcLocCache.end()) {
2350 FileID FID = SrcMgr.translateFile(FE);
2351 FileLoc = SrcMgr.getLocForStartOfFile(FID);
2352 PreambleSrcLocCache[SD.Filename] = FileLoc;
2353 } else {
2354 FileLoc = ItFileID->getValue();
2355 }
2356
2357 if (FileLoc.isInvalid())
2358 continue;
2359 SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset);
2360 FullSourceLoc Loc(L, SrcMgr);
2361
2362 SmallVector<CharSourceRange, 4> Ranges;
2363 Ranges.reserve(SD.Ranges.size());
2364 for (const auto &Range : SD.Ranges) {
2365 SourceLocation BL = FileLoc.getLocWithOffset(Range.first);
2366 SourceLocation EL = FileLoc.getLocWithOffset(Range.second);
2367 Ranges.push_back(CharSourceRange::getCharRange(BL, EL));
2368 }
2369
2370 SmallVector<FixItHint, 2> FixIts;
2371 FixIts.reserve(SD.FixIts.size());
2372 for (const auto &FixIt : SD.FixIts) {
2373 FixIts.push_back(FixItHint());
2374 FixItHint &FH = FixIts.back();
2375 FH.CodeToInsert = FixIt.CodeToInsert;
2376 SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first);
2377 SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second);
2378 FH.RemoveRange = CharSourceRange::getCharRange(BL, EL);
2379 }
2380
2381 Result.push_back(StoredDiagnostic(SD.Level, SD.ID,
2382 SD.Message, Loc, Ranges, FixIts));
2383 }
2384 Result.swap(Out);
2385 }
2386
2387 void ASTUnit::addFileLevelDecl(Decl *D) {
2388 assert(D);
2389
2390 // We only care about local declarations.
2391 if (D->isFromASTFile())
2392 return;
2393
2394 SourceManager &SM = *SourceMgr;
2395 SourceLocation Loc = D->getLocation();
2396 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2397 return;
2398
2399 // We only keep track of the file-level declarations of each file.
2400 if (!D->getLexicalDeclContext()->isFileContext())
2401 return;
2402
2403 SourceLocation FileLoc = SM.getFileLoc(Loc);
2404 assert(SM.isLocalSourceLocation(FileLoc));
2405 FileID FID;
2406 unsigned Offset;
2407 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2408 if (FID.isInvalid())
2409 return;
2410
2411 LocDeclsTy *&Decls = FileDecls[FID];
2412 if (!Decls)
2413 Decls = new LocDeclsTy();
2414
2415 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2416
2417 if (Decls->empty() || Decls->back().first <= Offset) {
2418 Decls->push_back(LocDecl);
2419 return;
2420 }
2421
2422 LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2423 LocDecl, llvm::less_first());
2424
2425 Decls->insert(I, LocDecl);
2426 }
2427
2428 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2429 SmallVectorImpl<Decl *> &Decls) {
2430 if (File.isInvalid())
2431 return;
2432
2433 if (SourceMgr->isLoadedFileID(File)) {
2434 assert(Ctx->getExternalSource() && "No external source!");
2435 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2436 Decls);
2437 }
2438
2439 FileDeclsTy::iterator I = FileDecls.find(File);
2440 if (I == FileDecls.end())
2441 return;
2442
2443 LocDeclsTy &LocDecls = *I->second;
2444 if (LocDecls.empty())
2445 return;
2446
2447 LocDeclsTy::iterator BeginIt =
2448 std::lower_bound(LocDecls.begin(), LocDecls.end(),
2449 std::make_pair(Offset, (Decl *)nullptr),
2450 llvm::less_first());
2451 if (BeginIt != LocDecls.begin())
2452 --BeginIt;
2453
2454 // If we are pointing at a top-level decl inside an objc container, we need
2455 // to backtrack until we find it otherwise we will fail to report that the
2456 // region overlaps with an objc container.
2457 while (BeginIt != LocDecls.begin() &&
2458 BeginIt->second->isTopLevelDeclInObjCContainer())
2459 --BeginIt;
2460
2461 LocDeclsTy::iterator EndIt = std::upper_bound(
2462 LocDecls.begin(), LocDecls.end(),
2463 std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first());
2464 if (EndIt != LocDecls.end())
2465 ++EndIt;
2466
2467 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2468 Decls.push_back(DIt->second);
2469 }
2470
2471 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2472 unsigned Line, unsigned Col) const {
2473 const SourceManager &SM = getSourceManager();
2474 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2475 return SM.getMacroArgExpandedLocation(Loc);
2476 }
2477
2478 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2479 unsigned Offset) const {
2480 const SourceManager &SM = getSourceManager();
2481 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2482 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2483 }
2484
2485 /// If \arg Loc is a loaded location from the preamble, returns
2486 /// the corresponding local location of the main file, otherwise it returns
2487 /// \arg Loc.
2488 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) const {
2489 FileID PreambleID;
2490 if (SourceMgr)
2491 PreambleID = SourceMgr->getPreambleFileID();
2492
2493 if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2494 return Loc;
2495
2496 unsigned Offs;
2497 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) {
2498 SourceLocation FileLoc
2499 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2500 return FileLoc.getLocWithOffset(Offs);
2501 }
2502
2503 return Loc;
2504 }
2505
2506 /// If \arg Loc is a local location of the main file but inside the
2507 /// preamble chunk, returns the corresponding loaded location from the
2508 /// preamble, otherwise it returns \arg Loc.
2509 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) const {
2510 FileID PreambleID;
2511 if (SourceMgr)
2512 PreambleID = SourceMgr->getPreambleFileID();
2513
2514 if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2515 return Loc;
2516
2517 unsigned Offs;
2518 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2519 Offs < Preamble->getBounds().Size) {
2520 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2521 return FileLoc.getLocWithOffset(Offs);
2522 }
2523
2524 return Loc;
2525 }
2526
2527 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) const {
2528 FileID FID;
2529 if (SourceMgr)
2530 FID = SourceMgr->getPreambleFileID();
2531
2532 if (Loc.isInvalid() || FID.isInvalid())
2533 return false;
2534
2535 return SourceMgr->isInFileID(Loc, FID);
2536 }
2537
2538 bool ASTUnit::isInMainFileID(SourceLocation Loc) const {
2539 FileID FID;
2540 if (SourceMgr)
2541 FID = SourceMgr->getMainFileID();
2542
2543 if (Loc.isInvalid() || FID.isInvalid())
2544 return false;
2545
2546 return SourceMgr->isInFileID(Loc, FID);
2547 }
2548
2549 SourceLocation ASTUnit::getEndOfPreambleFileID() const {
2550 FileID FID;
2551 if (SourceMgr)
2552 FID = SourceMgr->getPreambleFileID();
2553
2554 if (FID.isInvalid())
2555 return {};
2556
2557 return SourceMgr->getLocForEndOfFile(FID);
2558 }
2559
2560 SourceLocation ASTUnit::getStartOfMainFileID() const {
2561 FileID FID;
2562 if (SourceMgr)
2563 FID = SourceMgr->getMainFileID();
2564
2565 if (FID.isInvalid())
2566 return {};
2567
2568 return SourceMgr->getLocForStartOfFile(FID);
2569 }
2570
2571 llvm::iterator_range<PreprocessingRecord::iterator>
2572 ASTUnit::getLocalPreprocessingEntities() const {
2573 if (isMainFileAST()) {
2574 serialization::ModuleFile &
2575 Mod = Reader->getModuleManager().getPrimaryModule();
2576 return Reader->getModulePreprocessedEntities(Mod);
2577 }
2578
2579 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2580 return llvm::make_range(PPRec->local_begin(), PPRec->local_end());
2581
2582 return llvm::make_range(PreprocessingRecord::iterator(),
2583 PreprocessingRecord::iterator());
2584 }
2585
2586 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2587 if (isMainFileAST()) {
2588 serialization::ModuleFile &
2589 Mod = Reader->getModuleManager().getPrimaryModule();
2590 for (const auto *D : Reader->getModuleFileLevelDecls(Mod)) {
2591 if (!Fn(context, D))
2592 return false;
2593 }
2594
2595 return true;
2596 }
2597
2598 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2599 TLEnd = top_level_end();
2600 TL != TLEnd; ++TL) {
2601 if (!Fn(context, *TL))
2602 return false;
2603 }
2604
2605 return true;
2606 }
2607
2608 const FileEntry *ASTUnit::getPCHFile() {
2609 if (!Reader)
2610 return nullptr;
2611
2612 serialization::ModuleFile *Mod = nullptr;
2613 Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) {
2614 switch (M.Kind) {
2615 case serialization::MK_ImplicitModule:
2616 case serialization::MK_ExplicitModule:
2617 case serialization::MK_PrebuiltModule:
2618 return true; // skip dependencies.
2619 case serialization::MK_PCH:
2620 Mod = &M;
2621 return true; // found it.
2622 case serialization::MK_Preamble:
2623 return false; // look in dependencies.
2624 case serialization::MK_MainFile:
2625 return false; // look in dependencies.
2626 }
2627
2628 return true;
2629 });
2630 if (Mod)
2631 return Mod->File;
2632
2633 return nullptr;
2634 }
2635
2636 bool ASTUnit::isModuleFile() const {
2637 return isMainFileAST() && getLangOpts().isCompilingModule();
2638 }
2639
2640 InputKind ASTUnit::getInputKind() const {
2641 auto &LangOpts = getLangOpts();
2642
2643 InputKind::Language Lang;
2644 if (LangOpts.OpenCL)
2645 Lang = InputKind::OpenCL;
2646 else if (LangOpts.CUDA)
2647 Lang = InputKind::CUDA;
2648 else if (LangOpts.RenderScript)
2649 Lang = InputKind::RenderScript;
2650 else if (LangOpts.CPlusPlus)
2651 Lang = LangOpts.ObjC ? InputKind::ObjCXX : InputKind::CXX;
2652 else
2653 Lang = LangOpts.ObjC ? InputKind::ObjC : InputKind::C;
2654
2655 InputKind::Format Fmt = InputKind::Source;
2656 if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap)
2657 Fmt = InputKind::ModuleMap;
2658
2659 // We don't know if input was preprocessed. Assume not.
2660 bool PP = false;
2661
2662 return InputKind(Lang, Fmt, PP);
2663 }
2664
2665 #ifndef NDEBUG
2666 ASTUnit::ConcurrencyState::ConcurrencyState() {
2667 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2668 }
2669
2670 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2671 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2672 }
2673
2674 void ASTUnit::ConcurrencyState::start() {
2675 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2676 assert(acquired && "Concurrent access to ASTUnit!");
2677 }
2678
2679 void ASTUnit::ConcurrencyState::finish() {
2680 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2681 }
2682
2683 #else // NDEBUG
2684
2685 ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; }
2686 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2687 void ASTUnit::ConcurrencyState::start() {}
2688 void ASTUnit::ConcurrencyState::finish() {}
2689
2690 #endif // NDEBUG
clang::CodeCompletionContext::CCC_Recovery
An unknown context, in which we are recovering from a parsing error and don&#39;t know which completions ...
Definition: CodeCompleteConsumer.h:335
clang::SourceManager::getLocForStartOfFile
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
Definition: SourceManager.h:1067
clang::Builtin::ID
ID
Definition: Builtins.h:49
ASTContext.h
Defines the clang::ASTContext interface.
clang::PreambleBounds
Describes the bounds (start, size) of the preamble and a flag required by PreprocessorOptions::Precom...
Definition: Lexer.h:59
clang::StoredDiagnostic::getID
unsigned getID() const
Definition: Diagnostic.h:1458
clang::ASTUnit::ResetForParse
void ResetForParse()
Free data that will be re-generated on the next parse.
Definition: ASTUnit.cpp:1854
Diag
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Definition: LiteralSupport.cpp:78
clang::CompilerInstance::getInvocation
CompilerInvocation & getInvocation()
Definition: CompilerInstance.h:240
Identifier
StringRef Identifier
Definition: Format.cpp:1636
FrontendActions.h
clang::SourceLocation::getLocWithOffset
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
Definition: SourceLocation.h:134
clang::DiagnosticsEngine::getClient
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:510
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:638
clang::ExternalASTSource::GetExternalDecl
virtual Decl * GetExternalDecl(uint32_t ID)
Resolve a declaration ID into a declaration, potentially building a new declaration.
Definition: ExternalASTSource.cpp:83
valueOrNull
static std::unique_ptr< T > valueOrNull(llvm::ErrorOr< std::unique_ptr< T >> Val)
Definition: ASTUnit.cpp:140
clang::CodeCompleteOptions::IncludeBriefComments
unsigned IncludeBriefComments
Show brief documentation comments in code completion results.
Definition: CodeCompleteOptions.h:36
clang::PreprocessorOptions::RemappedFilesKeepOriginalName
bool RemappedFilesKeepOriginalName
True if the SourceManager should report the original file name for contents of files that were remapp...
Definition: PreprocessorOptions.h:125
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:121
clang::ASTUnit::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTUnit.h:456
clang::ASTConsumer
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:34
clang::ASTUnit::StandaloneFixIt::InsertFromRange
std::pair< unsigned, unsigned > InsertFromRange
Definition: ASTUnit.h:91
clang::ASTUnit::addTopLevelDecl
void addTopLevelDecl(Decl *D)
Add a new top-level declaration.
Definition: ASTUnit.h:522
FileManager.h
Defines the clang::FileManager interface and associated types.
clang::CodeCompletionContext::CCC_SelectorName
Code completion for a selector, as in an @selector expression.
Definition: CodeCompleteConsumer.h:307
clang::FrontendAction::EndSourceFile
void EndSourceFile()
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
Definition: FrontendAction.cpp:951
clang::CodeCompletionContext::CCC_Symbol
Code completion occurred where an existing name(such as type, function or variable) is expected...
Definition: CodeCompleteConsumer.h:283
clang::CodeCompleteOptions::IncludeGlobals
unsigned IncludeGlobals
Show top-level decls in code completion results.
Definition: CodeCompleteOptions.h:25
clang::CCP_CodePattern
Priority for a code pattern.
Definition: CodeCompleteConsumer.h:79
clang::SkipFunctionBodiesScope::PreambleAndMainFile
clang::getMacroUsagePriority
unsigned getMacroUsagePriority(StringRef MacroName, const LangOptions &LangOpts, bool PreferredTypeIsPointer=false)
Determine the priority to be given to a macro code completion result with the given name...
Definition: SemaCodeComplete.cpp:3338
clang::CodeCompletionContext::CCC_ObjCClassMessage
Code completion where an Objective-C class message is expected.
Definition: CodeCompleteConsumer.h:321
Type.h
C Language Family Type Representation.
SourceManager.h
Defines the SourceManager interface.
clang::CodeCompletionContext::CCC_TypeQualifiers
Code completion within a type-qualifier list.
Definition: CodeCompleteConsumer.h:310
clang::ASTUnit::LoadEverything
Load everything, including Sema.
Definition: ASTUnit.h:671
clang::FrontendAction
Abstract base class for actions which can be performed by the frontend.
Definition: FrontendAction.h:36
Module.h
Defines the clang::Module class, which describes a module in the source code.
clang::SourceManager::isLocalSourceLocation
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
Definition: SourceManager.h:1670
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
clang::serialization::ModuleFile::Kind
ModuleKind Kind
The type of this module.
Definition: Module.h:120
clang::ASTUnit::getFileManager
const FileManager & getFileManager() const
Definition: ASTUnit.h:471
DeclTemplate.h
Defines the C++ template declaration subclasses.
DefaultPreambleRebuildInterval
const unsigned DefaultPreambleRebuildInterval
After failing to build a precompiled preamble (due to errors in the source that occurs in the preambl...
Definition: ASTUnit.cpp:233
clang::StoredDiagnostic
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1437
CodeCompleteConsumer.h
clang::ASTUnit::getASTFileName
StringRef getASTFileName() const
If this ASTUnit came from an AST file, returns the filename for it.
Definition: ASTUnit.cpp:1457
clang::FixItHint::CodeToInsert
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
Definition: Diagnostic.h:78
ASTReader.h
clang::CodeCompletionContext::CCC_Other
An unspecified code-completion context.
Definition: CodeCompleteConsumer.h:192
clang::LangOptions::isCompilingModule
bool isCompilingModule() const
Are we compiling a module interface (.cppm or module map)?
Definition: LangOptions.h:263
clang::SyntaxOnlyAction
Definition: FrontendActions.h:158
PrecompiledPreamble.h
DeclObjC.h
HeaderSearch.h
clang::CompilerInvocationBase::LangOpts
std::shared_ptr< LangOptions > LangOpts
Options controlling the language variant.
Definition: CompilerInvocation.h:68
clang::BuildPreambleError::CouldntEmitPCH
clang::PreprocessorOptions
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Definition: PreprocessorOptions.h:43
clang::ASTUnit::LoadFromASTFile
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, bool UseDebugInfo=false, bool OnlyLocalDecls=false, ArrayRef< RemappedFile > RemappedFiles=None, bool CaptureDiagnostics=false, bool AllowPCHWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
Definition: ASTUnit.cpp:733
clang::InputKind::Format
Format
The input file format.
Definition: FrontendOptions.h:169
clang::CodeCompleteConsumer::loadExternal
bool loadExternal() const
Hint whether to load data from the external AST in order to provide full results. ...
Definition: CodeCompleteConsumer.h:1113
clang::DiagnosticsEngine::takeClient
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition: Diagnostic.h:518
clang::Decl::getIdentifierNamespace
unsigned getIdentifierNamespace() const
Definition: DeclBase.h:792
clang::CodeCompletionContext::CCC_ObjCMessageReceiver
Code completion occurred where an Objective-C message receiver is expected.
Definition: CodeCompleteConsumer.h:226
clang::CompilerInstance::getASTContext
ASTContext & getASTContext() const
Definition: CompilerInstance.h:469
Value
Value
Definition: UninitializedValues.cpp:102
clang::ASTReader::HadErrors
The AST file has errors.
Definition: ASTReader.h:404
clang::CodeCompletionContext::CCC_ArrowMemberAccess
Code completion occurred on the right-hand side of a member access expression using the arrow operato...
Definition: CodeCompleteConsumer.h:242
clang::CodeCompletionContext::CCC_EnumTag
Code completion occurred after the "enum" keyword, to indicate an enumeration name.
Definition: CodeCompleteConsumer.h:254
clang::CompilerInstance::setSourceManager
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
Definition: CompilerInstance.cpp:102
clang::DiagnosticConsumer::EndSourceFile
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1520
clang::CodeCompleteConsumer::isOutputBinary
bool isOutputBinary() const
Determine whether the output of this consumer is binary.
Definition: CodeCompleteConsumer.h:1118
clang::TargetOptions
Options for controlling the target.
Definition: TargetOptions.h:27
Sema.h
clang::MemoryBufferCache
Manage memory buffers across multiple users.
Definition: MemoryBufferCache.h:32
clang::ASTUnit::mapLocationToPreamble
SourceLocation mapLocationToPreamble(SourceLocation Loc) const
If Loc is a local location of the main file but inside the preamble chunk, returns the corresponding ...
Definition: ASTUnit.cpp:2509
PrettyPrinter.h
clang::PreprocessorOptions::addRemappedFile
void addRemappedFile(StringRef From, StringRef To)
Definition: PreprocessorOptions.h:179
clang::DiagnosticConsumer
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1488
clang::NamedDecl::getUnderlyingDecl
NamedDecl * getUnderlyingDecl()
Looks through UsingDecls and ObjCCompatibleAliasDecls for the underlying named decl.
Definition: Decl.h:432
checkAndSanitizeDiags
static void checkAndSanitizeDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SourceManager &SM)
Definition: ASTUnit.cpp:1055
clang::BuildPreambleError::CouldntCreateTargetInfo
clang::CodeCompletionContext::CCC_ObjCIvarList
Code completion occurred within the instance variable list of an Objective-C interface, implementation, or category implementation.
Definition: CodeCompleteConsumer.h:212
clang::CodeCompletionBuilder::AddTypedTextChunk
void AddTypedTextChunk(const char *Text)
Add a new typed-text chunk.
Definition: CodeCompleteConsumer.cpp:432
clang::PrintingPolicy
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
clang::Diagnostic::hasSourceManager
bool hasSourceManager() const
Definition: Diagnostic.h:1328
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:36
clang::MultiVersionKind::Target
clang::frontend::FixIt
Parse and apply any fixits to the source.
Definition: FrontendOptions.h:77
clang::ASTUnit::create
static std::unique_ptr< ASTUnit > create(std::shared_ptr< CompilerInvocation > CI, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, bool CaptureDiagnostics, bool UserFilesAreVolatile)
Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
Definition: ASTUnit.cpp:1467
clang::CodeCompletionContext::CCC_SymbolOrNewName
Code completion occurred where both a new name and an existing symbol is permissible.
Definition: CodeCompleteConsumer.h:279
clang::Decl::IDNS_Type
Types, declared with &#39;struct foo&#39;, typedefs, etc.
Definition: DeclBase.h:131
clang::ASTUnit::StandaloneDiagnostic::Ranges
std::vector< std::pair< unsigned, unsigned > > Ranges
Definition: ASTUnit.h:102
clang::NamedDecl::getDeclName
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:298
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:58
clang::CodeCompletionBuilder::TakeString
CodeCompletionString * TakeString()
Take the resulting completion string.
Definition: CodeCompleteConsumer.cpp:420
clang::ASTUnit::WhatToLoad
WhatToLoad
Definition: ASTUnit.h:663
clang::ASTUnit::cached_completion_end
cached_completion_iterator cached_completion_end()
Definition: ASTUnit.h:613
clang::DiagnosticsEngine::setClient
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:100
clang::CharSourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:266
clang::InputKind::CXX
Definition: FrontendOptions.h:158
clang::PreprocessingRecord::iterator
Iteration over the preprocessed entities.
Definition: PreprocessingRecord.h:431
clang::ASTUnit::ASTWriterData
Definition: ASTUnit.cpp:216
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::ASTUnit
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:87
llvm::Optional
Definition: LLVM.h:38
clang::FrontendAction::BeginSourceFile
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
Definition: FrontendAction.cpp:537
clang::SkipFunctionBodiesScope
SkipFunctionBodiesScope
Enumerates the available scopes for skipping function bodies.
Definition: ASTUnit.h:84
clang::CodeCompletionString
A "string" used to describe how code completion can be performed for an entity.
Definition: CodeCompleteConsumer.h:430
ActiveASTUnitObjects
static std::atomic< unsigned > ActiveASTUnitObjects
Tracks the number of ASTUnit objects that are currently active.
Definition: ASTUnit.cpp:238
clang::SourceManager::translateFileLineCol
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
Definition: SourceManager.cpp:1551
clang::ASTUnit::ASTWriterData::Writer
ASTWriter Writer
Definition: ASTUnit.cpp:219
moveOnNoError
static bool moveOnNoError(llvm::ErrorOr< T > Val, T &Output)
Definition: ASTUnit.cpp:147
clang::CodeCompletionContext::getKind
Kind getKind() const
Retrieve the kind of code-completion context.
Definition: CodeCompleteConsumer.h:378
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
clang::ASTUnit::isInPreambleFileID
bool isInPreambleFileID(SourceLocation Loc) const
Definition: ASTUnit.cpp:2527
clang::ASTUnit::getBufferForFile
std::unique_ptr< llvm::MemoryBuffer > getBufferForFile(StringRef Filename, std::string *ErrorStr=nullptr)
Definition: ASTUnit.cpp:715
clang::ASTContext::setPrintingPolicy
void setPrintingPolicy(const clang::PrintingPolicy &Policy)
Definition: ASTContext.h:658
clang::ASTUnit::StandaloneFixIt::BeforePreviousInsertions
bool BeforePreviousInsertions
Definition: ASTUnit.h:93
clang::CompilerInstance::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: CompilerInstance.h:454
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
clang::LangOptions::CMK_ModuleMap
Compiling a module from a module map.
Definition: LangOptions.h:79
clang::Module
Describes a module or submodule.
Definition: Module.h:65
llvm::SmallVectorImpl
Definition: LLVM.h:37
clang::ASTUnit::getMainFileName
StringRef getMainFileName() const
Definition: ASTUnit.cpp:1439
clang::InputKind::C
Languages that the frontend can parse and compile.
Definition: FrontendOptions.h:157
clang::CodeCompleteOptions::IncludeCodePatterns
unsigned IncludeCodePatterns
Show code patterns in code completion results.
Definition: CodeCompleteOptions.h:22
clang::CCF_ExactTypeMatch
Divide by this factor when a code-completion result&#39;s type exactly matches the type we expect...
Definition: CodeCompleteConsumer.h:140
clang::CodeCompletionAllocator
An allocator used specifically for the purpose of code completion.
Definition: CodeCompleteConsumer.h:627
clang::ASTUnit::StandaloneDiagnostic::Level
DiagnosticsEngine::Level Level
Definition: ASTUnit.h:98
clang::FileID::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:47
clang::ASTUnit::LoadASTOnly
Load the AST, but do not restore Sema state.
Definition: ASTUnit.h:668
clang::SourceManager::translateFile
FileID translateFile(const FileEntry *SourceFile) const
Get the FileID for the given file.
Definition: SourceManager.cpp:1565
clang::ASTUnit::setPreprocessor
void setPreprocessor(std::shared_ptr< Preprocessor > pp)
Definition: ASTUnit.cpp:273
llvm::Expected
Definition: LLVM.h:39
ASTWriter.h
clang::createInvocationFromCommandLine
std::unique_ptr< CompilerInvocation > createInvocationFromCommandLine(ArrayRef< const char *> Args, IntrusiveRefCntPtr< DiagnosticsEngine > Diags=IntrusiveRefCntPtr< DiagnosticsEngine >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
createInvocationFromCommandLine - Construct a compiler invocation object for a command line argument ...
Definition: CreateInvocationFromCommandLine.cpp:33
clang::PreprocessingRecord
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
Definition: PreprocessingRecord.h:309
clang::Decl::IDNS_Namespace
Namespaces, declared with &#39;namespace foo {}&#39;.
Definition: DeclBase.h:141
clang::BuildPreambleError::CouldntCreateTempFile
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::Preprocessor::Initialize
void Initialize(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize the preprocessor using information about the target.
Definition: Preprocessor.cpp:189
clang::ASTUnit::StandaloneFixIt::CodeToInsert
std::string CodeToInsert
Definition: ASTUnit.h:92
clang::ASTUnit::getPCHFile
const FileEntry * getPCHFile()
Get the PCH file if one was included.
Definition: ASTUnit.cpp:2608
clang::CodeCompletionContext::CCC_PreprocessorDirective
Code completion occurred where a preprocessor directive is expected.
Definition: CodeCompleteConsumer.h:297
clang::ASTUnit::serialize
bool serialize(raw_ostream &OS)
Serialize this translation unit with the given output stream.
Definition: ASTUnit.cpp:2311
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
CommentCommandTraits.h
clang::InputKind::Source
Definition: FrontendOptions.h:170
clang::HeaderSearchOptions::ResourceDir
std::string ResourceDir
The directory which holds the compiler resource files (builtin includes, etc.).
Definition: HeaderSearchOptions.h:109
FrontendOptions.h
clang::CodeCompleteConsumer::getCodeCompletionTUInfo
virtual CodeCompletionTUInfo & getCodeCompletionTUInfo()=0
clang::CodeCompletionContext::CCC_ObjCImplementation
Code completion occurred within an Objective-C implementation or category implementation.
Definition: CodeCompleteConsumer.h:208
clang::ASTUnit::StandaloneDiagnostic::ID
unsigned ID
Definition: ASTUnit.h:97
clang::StoredDiagnostic::getRanges
ArrayRef< CharSourceRange > getRanges() const
Definition: Diagnostic.h:1471
Diagnostic.h
Defines the Diagnostic-related interfaces.
clang::PreprocessorOptions::RetainRemappedFileBuffers
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
Definition: PreprocessorOptions.h:144
clang::ParsedSourceLocation::Column
unsigned Column
Definition: CommandLineSourceLoc.h:28
clang::FrontendOptions::SkipFunctionBodies
unsigned SkipFunctionBodies
Skip over function bodies to speed up parsing in cases you do not need them (e.g. ...
Definition: FrontendOptions.h:280
clang::ASTUnit::addFileLevelDecl
void addFileLevelDecl(Decl *D)
Add a new local file-level declaration.
Definition: ASTUnit.cpp:2387
Utils.h
clang::getDeclUsageType
QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND)
Determine the type that this declaration will have if it is used as a type or in an expression...
Definition: SemaCodeComplete.cpp:665
clang::CodeCompletionContext::CCC_Namespace
Code completion occurred where a namespace or namespace alias is expected.
Definition: CodeCompleteConsumer.h:269
clang::ASTUnit::getDeserializationListener
ASTDeserializationListener * getDeserializationListener()
Definition: ASTUnit.cpp:708
clang::Decl::getLexicalDeclContext
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC)...
Definition: DeclBase.h:821
clang::CompilerInstance::setFileManager
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
Definition: CompilerInstance.cpp:94
clang::Sema::getLangOpts
const LangOptions & getLangOpts() const
Definition: Sema.h:1231
isNonDriverDiag
static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag)
Definition: ASTUnit.cpp:1042
clang::StoredDiagnostic::getMessage
StringRef getMessage() const
Definition: Diagnostic.h:1461
clang::DiagnosticsEngine::Reset
void Reset()
Reset the state of the diagnostic object to its initial configuration.
Definition: Diagnostic.cpp:122
clang::PCHContainerReader
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
Definition: PCHContainerOperations.h:58
llvm::ArrayRef
Definition: LLVM.h:32
clang::ASTReader::Failure
The AST file itself appears corrupted.
Definition: ASTReader.h:387
llvm::SmallString
Definition: LLVM.h:35
clang::Decl::IDNS_Ordinary
Ordinary names.
Definition: DeclBase.h:145
clang::CodeCompleteConsumer::ProcessOverloadCandidates
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, SourceLocation OpenParLoc)
Definition: CodeCompleteConsumer.h:1149
FrontendDiagnostic.h
clang::ParsedSourceLocation::Line
unsigned Line
Definition: CommandLineSourceLoc.h:27
clang::PreprocessorOptions::RemappedFileBuffers
std::vector< std::pair< std::string, llvm::MemoryBuffer * > > RemappedFileBuffers
The set of file-to-buffer remappings, which take existing files on the system (the first part of each...
Definition: PreprocessorOptions.h:136
clang::HeaderSearch
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:148
ExternalASTSource.h
clang::CompilerInvocation::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInvocation.h:212
clang::PreambleCallbacks
A set of callbacks to gather useful information while building a preamble.
Definition: PrecompiledPreamble.h:264
clang::PreprocessorOptions::DetailedRecord
bool DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
Definition: PreprocessorOptions.h:55
clang::CodeCompletionContext::CCC_ObjCCategoryName
Code completion where an Objective-C category name is expected.
Definition: CodeCompleteConsumer.h:328
clang::FileManager::getFile
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
Definition: FileManager.cpp:183
checkAndRemoveNonDriverDiags
static void checkAndRemoveNonDriverDiags(SmallVectorImpl< StoredDiagnostic > &StoredDiags)
Definition: ASTUnit.cpp:1047
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:278
clang::CodeCompletionContext::CCC_TopLevel
Code completion occurred within a "top-level" completion context, e.g., at namespace or global scope...
Definition: CodeCompleteConsumer.h:200
CompilerInstance.h
AddDefinedMacroToHash
static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash)
Add the given macro to the hash of all top-level entities.
Definition: ASTUnit.cpp:849
clang::Decl::IDNS_NonMemberOperator
This declaration is a C++ operator declared in a non-class context.
Definition: DeclBase.h:169
Filename
StringRef Filename
Definition: Format.cpp:1629
clang::ASTUnit::isModuleFile
bool isModuleFile() const
Returns true if the ASTUnit was constructed from a serialized module file.
Definition: ASTUnit.cpp:2636
clang::CompilerInstance::getModuleManager
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
Definition: CompilerInstance.cpp:136
Offset
unsigned Offset
Definition: Format.cpp:1631
clang::CodeCompletionContext::CCC_ObjCProtocolName
Code completion occurred where a protocol name is expected.
Definition: CodeCompleteConsumer.h:265
TypeOrdering.h
Allows QualTypes to be sorted and hence used in maps and sets.
clang::LangOptions::CommentOpts
CommentOptions CommentOpts
Options for parsing comments.
Definition: LangOptions.h:236
clang::CodeCompletionContext::getPreferredType
QualType getPreferredType() const
Retrieve the type that this expression would prefer to have, e.g., if the expression is a variable in...
Definition: CodeCompleteConsumer.h:383
clang::FrontendInputFile::getBuffer
llvm::MemoryBuffer * getBuffer() const
Definition: FrontendOptions.h:235
clang::ASTUnit::getCodeCompletionTUInfo
CodeCompletionTUInfo & getCodeCompletionTUInfo()
Definition: ASTUnit.h:311
clang::ASTUnit::getASTReader
IntrusiveRefCntPtr< ASTReader > getASTReader() const
Definition: ASTUnit.cpp:698
LangOptions.h
Defines the clang::LangOptions interface.
clang::ASTUnit::isMainFileAST
bool isMainFileAST() const
Definition: ASTUnit.h:419
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:236
clang::StoredDiagnostic::getLevel
DiagnosticsEngine::Level getLevel() const
Definition: Diagnostic.h:1459
clang::getAsString
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:51
clang::ASTUnit::StandaloneFixIt::RemoveRange
std::pair< unsigned, unsigned > RemoveRange
Definition: ASTUnit.h:90
AddTopLevelDeclarationToHash
static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash)
Add the given declaration to the hash of all top-level entities.
Definition: ASTUnit.cpp:872
clang::ASTUnit::getCurrentTopLevelHashValue
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
Definition: ASTUnit.h:538
clang::DiagnosticsEngine::hasUncompilableErrorOccurred
bool hasUncompilableErrorOccurred() const
Errors that actually prevent compilation, not those that are upgraded from a warning by -Werror...
Definition: Diagnostic.h:751
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:69
clang::StoredDiagnostic::getFixIts
ArrayRef< FixItHint > getFixIts() const
Definition: Diagnostic.h:1481
clang::FrontendInputFile::getFile
StringRef getFile() const
Definition: FrontendOptions.h:230
clang::ASTUnit::StandaloneDiagnostic::FixIts
std::vector< StandaloneFixIt > FixIts
Definition: ASTUnit.h:103
clang::DeclarationName::getAsString
std::string getAsString() const
Retrieve the human-readable string for this name.
Definition: DeclarationName.cpp:229
DeclBase.h
CodeCompleteOptions.h
Preprocessor.h
Defines the clang::Preprocessor interface.
clang::CodeCompletionResult::Declaration
const NamedDecl * Declaration
When Kind == RK_Declaration or RK_Pattern, the declaration we are referring to.
Definition: CodeCompleteConsumer.h:764
clang::DeclContext::isFileContext
bool isFileContext() const
Definition: DeclBase.h:1818
clang::ASTContext::getCommentCommandTraits
comments::CommandTraits & getCommentCommandTraits() const
Definition: ASTContext.h:875
clang::FullSourceLoc::getManager
const SourceManager & getManager() const
Definition: SourceLocation.h:357
clang::Decl::getDeclContext
DeclContext * getDeclContext()
Definition: DeclBase.h:427
Lexer.h
clang::ASTMutationListener
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
Definition: ASTMutationListener.h:47
CXCursor_MacroDefinition
Definition: Index.h:2593
clang::serialization::ModuleFile
Information about a module that has been loaded by the ASTReader.
Definition: Module.h:108
clang::DeclarationName::getAsIdentifierInfo
IdentifierInfo * getAsIdentifierInfo() const
Retrieve the IdentifierInfo * stored in this declaration name, or null if this declaration name isn&#39;t...
Definition: DeclarationName.h:411
clang::CodeCompletionResult
Captures a result of code completion.
Definition: CodeCompleteConsumer.h:745
IdentifierTable.h
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
ASTBitCodes.h
clang::CodeCompletionContext::CCC_ParenthesizedExpression
Code completion in a parenthesized expression, which means that we may also have types here in C and ...
Definition: CodeCompleteConsumer.h:314
clang::serialization::ModuleFile::FileName
std::string FileName
The file name of the module file.
Definition: Module.h:123
clang::FixItHint::BeforePreviousInsertions
bool BeforePreviousInsertions
Definition: Diagnostic.h:80
CompilerInvocation.h
clang::CodeCompletionContext::CCC_NaturalLanguage
Code completion occurred in a context where natural language is expected, e.g., a comment or string l...
Definition: CodeCompleteConsumer.h:304
clang::TargetInfo::CreateTargetInfo
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:608
clang::ComputePreambleBounds
PreambleBounds ComputePreambleBounds(const LangOptions &LangOpts, llvm::MemoryBuffer *Buffer, unsigned MaxLines)
Runs lexer to compute suggested preamble bounds.
Definition: PrecompiledPreamble.cpp:226
clang::ASTUnit::ASTWriterData::Buffer
SmallString< 128 > Buffer
Definition: ASTUnit.cpp:217
clang::FrontendInputFile
An input file for the front end.
Definition: FrontendOptions.h:199
clang::sema::Capture
Definition: ScopeInfo.h:493
makeStandaloneFixIt
static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, const LangOptions &LangOpts, const FixItHint &InFix)
Definition: ASTUnit.cpp:1223
ASTConsumer.h
clang::ASTUnit::top_level_begin
top_level_iterator top_level_begin()
Definition: ASTUnit.h:497
clang::ObjCSubstitutionContext::Result
The result type of a method or function.
clang::QualType::isNull
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:703
clang::SkipFunctionBodiesScope::Preamble
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::ASTDeserializationListener
Definition: ASTDeserializationListener.h:31
clang::ASTReader::ARR_None
The client can&#39;t handle any AST loading failures.
Definition: ASTReader.h:1508
clang::CCF_SimilarTypeMatch
Divide by this factor when a code-completion result&#39;s type is similar to the type we expect (e...
Definition: CodeCompleteConsumer.h:145
clang::ASTReader::Missing
The AST file was missing.
Definition: ASTReader.h:390
clang::FixItHint::InsertFromRange
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition: Diagnostic.h:74
clang::DiagnosticsEngine::getNumWarnings
unsigned getNumWarnings() const
Definition: Diagnostic.h:761
clang::CompilerInstance::takeSema
std::unique_ptr< Sema > takeSema()
Definition: CompilerInstance.cpp:132
clang::CharSourceRange::getCharRange
static CharSourceRange getCharRange(SourceRange R)
Definition: SourceLocation.h:248
clang::CodeCompletionContext
The context in which code completion occurred, so that the code-completion consumer can process the r...
Definition: CodeCompleteConsumer.h:188
clang::PreprocessorOptions::AllowPCHWithCompilerErrors
bool AllowPCHWithCompilerErrors
When true, a PCH with compiler errors will not be rejected.
Definition: PreprocessorOptions.h:85
clang::FixItHint::RemoveRange
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
Definition: Diagnostic.h:70
clang::SourceManager::getFileOffset
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
Definition: SourceManager.h:1250
clang::CodeCompletionTUInfo
Definition: CodeCompleteConsumer.h:636
clang::ExternalASTSource
Abstract interface for external sources of AST nodes.
Definition: ExternalASTSource.h:63
CalculateHiddenNames
static void CalculateHiddenNames(const CodeCompletionContext &Context, CodeCompletionResult *Results, unsigned NumResults, ASTContext &Ctx, llvm::StringSet< llvm::BumpPtrAllocator > &HiddenNames)
Helper function that computes which global names are hidden by the local code-completion results...
Definition: ASTUnit.cpp:1934
clang::SourceManager::getFilename
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Definition: SourceManager.h:1059
clang::ASTReader::Success
The control block was read successfully.
Definition: ASTReader.h:384
clang::CompilerInstance::hasPreprocessor
bool hasPreprocessor() const
Definition: CompilerInstance.h:446
clang::ASTUnit::RemappedFile
std::pair< std::string, llvm::MemoryBuffer * > RemappedFile
A mapping from a file name to the memory buffer that stores the remapped contents of that file...
Definition: ASTUnit.h:655
clang::CodeCompletionContext::CCC_ClassStructUnion
Code completion occurred within a class, struct, or union.
Definition: CodeCompleteConsumer.h:215
clang::MacroDirective
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:291
false
#define false
Definition: stdbool.h:33
Kind
Kind
Definition: ChrootChecker.cpp:29
clang::ASTUnit::mapLocationFromPreamble
SourceLocation mapLocationFromPreamble(SourceLocation Loc) const
If Loc is a loaded location from the preamble, returns the corresponding local location of the main f...
Definition: ASTUnit.cpp:2488
clang::OverloadCandidate
OverloadCandidate - A single candidate in an overload set (C++ 13.3).
Definition: Overload.h:730
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:45
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:73
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::CodeCompletionContext::CCC_NewName
Code completion occurred where a new name is expected.
Definition: CodeCompleteConsumer.h:275
clang::FrontendAction::Execute
bool Execute()
Set the source manager&#39;s main input file, and run the action.
Definition: FrontendAction.cpp:928
clang::ASTUnit::isInMainFileID
bool isInMainFileID(SourceLocation Loc) const
Definition: ASTUnit.cpp:2538
clang::Decl::IDNS_Member
Members, declared with object declarations within tag definitions.
Definition: DeclBase.h:137
clang::FileManager::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Definition: FileManager.h:222
clang::FrontendOptions::CodeCompletionAt
ParsedSourceLocation CodeCompletionAt
If given, enable code completion at the provided location.
Definition: FrontendOptions.h:391
PreprocessorOptions.h
clang::FrontendAction::getTranslationUnitKind
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we&#39;re handling.
Definition: FrontendAction.h:188
clang::ASTUnit::ASTWriterData::ASTWriterData
ASTWriterData(MemoryBufferCache &PCMCache)
Definition: ASTUnit.cpp:221
clang::ASTUnit::getCachedCompletionTypes
llvm::StringMap< unsigned > & getCachedCompletionTypes()
Retrieve the mapping from formatted type names to unique type identifiers.
Definition: ASTUnit.h:301
clang::InputKind::LLVM_IR
LLVM IR: we accept this so that we can run the optimizer on it, and compile it to assembly or object ...
Definition: FrontendOptions.h:154
clang::FrontendOptions::Inputs
std::vector< FrontendInputFile > Inputs
The input files and their types.
Definition: FrontendOptions.h:375
clang::ASTUnit::getSema
Sema & getSema() const
Definition: ASTUnit.h:451
clang::ASTUnit::CodeComplete
void CodeComplete(StringRef File, unsigned Line, unsigned Column, ArrayRef< RemappedFile > RemappedFiles, bool IncludeMacros, bool IncludeCodePatterns, bool IncludeBriefComments, CodeCompleteConsumer &Consumer, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, SmallVectorImpl< StoredDiagnostic > &StoredDiagnostics, SmallVectorImpl< const llvm::MemoryBuffer *> &OwnedBuffers)
Perform code completion at the given file, line, and column within this translation unit...
Definition: ASTUnit.cpp:2108
clang::Token::getIdentifierInfo
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:60
clang::InputKind
The kind of a file that we&#39;ve been handed as an input.
Definition: FrontendOptions.h:137
clang::InputKind::RenderScript
Definition: FrontendOptions.h:163
clang::CodeCompletionContext::CCC_ObjCInterfaceName
Code completion where the name of an Objective-C class is expected.
Definition: CodeCompleteConsumer.h:325
clang::ASTUnit::cached_completion_iterator
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
Definition: ASTUnit.h:607
clang::ProcessWarningOptions
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition: Warnings.cpp:44
DeclarationName.h
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::SourceManager::getFileLoc
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location...
Definition: SourceManager.h:1127
clang::CodeCompletionContext::CCC_ObjCInterface
Code completion occurred within an Objective-C interface, protocol, or category interface.
Definition: CodeCompleteConsumer.h:204
clang::ParsedSourceLocation::FileName
std::string FileName
Definition: CommandLineSourceLoc.h:26
TargetOptions.h
Defines the clang::TargetOptions class.
clang::CompilerInvocationBase::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts() const
Definition: CompilerInvocation.h:93
clang::CanQual< Type >
makeStandaloneRange
static std::pair< unsigned, unsigned > makeStandaloneRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Definition: ASTUnit.cpp:1215
clang::ASTUnit::LoadFromCommandLine
static ASTUnit * LoadFromCommandLine(const char **ArgBegin, const char **ArgEnd, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, StringRef ResourceFilesPath, bool OnlyLocalDecls=false, bool CaptureDiagnostics=false, ArrayRef< RemappedFile > RemappedFiles=None, bool RemappedFilesKeepOriginalName=true, unsigned PrecompilePreambleAfterNParses=0, TranslationUnitKind TUKind=TU_Complete, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool AllowPCHWithCompilerErrors=false, SkipFunctionBodiesScope SkipFunctionBodies=SkipFunctionBodiesScope::None, bool SingleFileParse=false, bool UserFilesAreVolatile=false, bool ForSerialization=false, llvm::Optional< StringRef > ModuleFormat=llvm::None, std::unique_ptr< ASTUnit > *ErrAST=nullptr, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
LoadFromCommandLine - Create an ASTUnit from a vector of command line arguments, which must specify e...
Definition: ASTUnit.cpp:1702
llvm::SmallVector
Definition: LLVM.h:36
clang::Type::isAnyPointerType
bool isAnyPointerType() const
Definition: Type.h:6300
clang::ASTUnit::StandaloneDiagnostic
Definition: ASTUnit.h:96
clang::Decl::isFromASTFile
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:695
clang::DiagnosticConsumer::HandleDiagnostic
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: Diagnostic.cpp:533
clang::ASTReader::setListener
void setListener(std::unique_ptr< ASTReaderListener > Listener)
Set the AST callbacks listener.
Definition: ASTReader.h:1575
clang::ASTContext::InitBuiltinTypes
void InitBuiltinTypes(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize built-in types.
Definition: ASTContext.cpp:1146
clang::Diagnostic::getSourceManager
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1329
clang::getSimplifiedTypeClass
SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T)
Determine the simplified type class of the given canonical type.
Definition: SemaCodeComplete.cpp:596
clang::ASTUnit::ASTWriterData::Stream
llvm::BitstreamWriter Stream
Definition: ASTUnit.cpp:218
clang::ASTUnit::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
Definition: ASTUnit.h:648
clang::DiagnosticsEngine::setNumWarnings
void setNumWarnings(unsigned NumWarnings)
Definition: Diagnostic.h:763
clang::ASTReader::ConfigurationMismatch
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:401
clang::ASTUnit::visitLocalTopLevelDecls
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
Definition: ASTUnit.cpp:2586
clang::serialization::MK_MainFile
File is a PCH file treated as the actual main file.
Definition: Module.h:57
clang::MultiVersionKind::None
clang::ASTUnit::LoadFromCompilerInvocationAction
static ASTUnit * LoadFromCompilerInvocationAction(std::shared_ptr< CompilerInvocation > CI, std::shared_ptr< PCHContainerOperations > PCHContainerOps, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, FrontendAction *Action=nullptr, ASTUnit *Unit=nullptr, bool Persistent=true, StringRef ResourceFilesPath=StringRef(), bool OnlyLocalDecls=false, bool CaptureDiagnostics=false, unsigned PrecompilePreambleAfterNParses=0, bool CacheCodeCompletionResults=false, bool IncludeBriefCommentsInCodeCompletion=false, bool UserFilesAreVolatile=false, std::unique_ptr< ASTUnit > *ErrAST=nullptr)
Create an ASTUnit from a source file, via a CompilerInvocation object, by invoking the optionally pro...
Definition: ASTUnit.cpp:1486
clang::comments::CommandTraits::registerCommentOptions
void registerCommentOptions(const CommentOptions &CommentOptions)
Definition: CommentCommandTraits.cpp:24
MemoryBufferCache.h
clang::IdentifierInfo::getName
StringRef getName() const
Return the actual identifier string.
Definition: IdentifierTable.h:156
clang::BuildPreambleError::BeginSourceFileFailed
clang::CCP_NestedNameSpecifier
Priority for a nested-name-specifier.
Definition: CodeCompleteConsumer.h:94
clang::CodeCompleteConsumer
Abstract interface for a consumer of code-completion information.
Definition: CodeCompleteConsumer.h:999
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang::ASTUnit::StandaloneDiagnostic::Message
std::string Message
Definition: ASTUnit.h:99
clang::CodeCompleteOptions
Options controlling the behavior of code completion.
Definition: CodeCompleteOptions.h:16
makeStandaloneDiagnostic
static ASTUnit::StandaloneDiagnostic makeStandaloneDiagnostic(const LangOptions &LangOpts, const StoredDiagnostic &InDiag)
Definition: ASTUnit.cpp:1236
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::PCHContainerReader::getFormat
virtual llvm::StringRef getFormat() const =0
Equivalent to the format passed to -fmodule-format=.
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:108
getBufferForFileHandlingRemapping
static std::unique_ptr< llvm::MemoryBuffer > getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation, llvm::vfs::FileSystem *VFS, StringRef FilePath, bool isVolatile)
Get a source buffer for MainFilePath, handling all file-to-file and file-to-buffer remappings inside ...
Definition: ASTUnit.cpp:157
clang::InputKind::ModuleMap
Definition: FrontendOptions.h:171
clang::CodeCompletionContext::CCC_MacroName
Code completion occurred where an macro is being defined.
Definition: CodeCompleteConsumer.h:286
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
clang::InputKind::ObjC
Definition: FrontendOptions.h:159
getDeclShowContexts
static uint64_t getDeclShowContexts(const NamedDecl *ND, const LangOptions &LangOpts, bool &IsNestedNameSpecifier)
Determine the set of code-completion contexts in which this declaration should be shown...
Definition: ASTUnit.cpp:285
clang::CompilerInstance::hasInvocation
bool hasInvocation() const
Definition: CompilerInstance.h:238
clang::CompilerInvocationBase::getPreprocessorOpts
PreprocessorOptions & getPreprocessorOpts()
Definition: CompilerInvocation.h:109
clang::Lexer::makeFileCharRange
static CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
Definition: Lexer.cpp:868
clang::CodeCompletionContext::CCC_ClassOrStructTag
Code completion occurred after the "struct" or "class" keyword, to indicate a struct or class name...
Definition: CodeCompleteConsumer.h:262
clang::ASTUnit::getLocalPreprocessingEntities
llvm::iterator_range< PreprocessingRecord::iterator > getLocalPreprocessingEntities() const
Returns an iterator range for the local preprocessing entities of the local Preprocessor, if this is a parsed source file, or the loaded preprocessing entities of the primary module if this is an AST file.
Definition: ASTUnit.cpp:2572
clang::ASTReader
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:355
clang::createVFSFromCompilerInvocation
IntrusiveRefCntPtr< llvm::vfs::FileSystem > createVFSFromCompilerInvocation(const CompilerInvocation &CI, DiagnosticsEngine &Diags)
Definition: CompilerInvocation.cpp:3465
clang::StoredDiagnostic::getLocation
const FullSourceLoc & getLocation() const
Definition: Diagnostic.h:1460
clang::CodeCompletionContext::CCC_UnionTag
Code completion occurred after the "union" keyword, to indicate a union name.
Definition: CodeCompleteConsumer.h:258
clang::CodeCompletionContext::CCC_MacroNameUse
Code completion occurred where a macro name is expected (without any arguments, in the case of a func...
Definition: CodeCompleteConsumer.h:290
clang::ASTUnit::DeclVisitorFn
bool(*)(void *context, const Decl *D) DeclVisitorFn
Type for a function iterating over a number of declarations.
Definition: ASTUnit.h:629
clang::CodeCompleteConsumer::includeFixIts
bool includeFixIts() const
Whether to include completion items with small fix-its, e.g.
Definition: CodeCompleteConsumer.h:1109
clang::CodeCompletionBuilder
A builder class used to construct new code-completion strings.
Definition: CodeCompleteConsumer.h:670
clang::CodeCompletionContext::CCC_ObjCInstanceMessage
Code completion where an Objective-C instance message is expected.
Definition: CodeCompleteConsumer.h:318
clang::DeclarationName
The name of a declaration.
Definition: DeclarationName.h:146
clang::DeclGroupRef
Definition: DeclGroup.h:52
clang::ASTUnit::getASTMutationListener
ASTMutationListener * getASTMutationListener()
Definition: ASTUnit.cpp:702
clang::InputKind::OpenCL
Definition: FrontendOptions.h:161
clang::QualType::getAsString
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:971
clang::ASTUnit::enableSourceFileDiagnostics
void enableSourceFileDiagnostics()
Enable source-range based diagnostic messages.
Definition: ASTUnit.cpp:277
clang::PreprocessorOptions::SingleFileParseMode
bool SingleFileParseMode
When enabled, preprocessor is in a mode for parsing a single file only.
Definition: PreprocessorOptions.h:118
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:121
clang::ASTUnit::getHeaderSearchOpts
const HeaderSearchOptions & getHeaderSearchOpts() const
Definition: ASTUnit.h:461
clang::InputKind::Language
Language
The language for the input, used to select and validate the language standard and possible actions...
Definition: FrontendOptions.h:146
clang::SourceManager::getMacroArgExpandedLocation
SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const
If Loc points inside a function macro argument, the returned location will be the macro location in w...
Definition: SourceManager.cpp:1888
clang::ASTUnit::top_level_end
top_level_iterator top_level_end()
Definition: ASTUnit.h:504
clang::ContinuousRangeMap
A map from continuous integer ranges to some value, with a very specialized interface.
Definition: ContinuousRangeMap.h:38
clang::Decl::IDNS_Tag
Tags, declared with &#39;struct foo;&#39; and referenced with &#39;struct foo&#39;.
Definition: DeclBase.h:126
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:242
clang::ASTContext::getExternalSource
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any...
Definition: ASTContext.h:1083
clang::ASTUnit::StandaloneDiagnostic::Filename
std::string Filename
Definition: ASTUnit.h:100
clang::ASTUnit::findFileRegionDecls
void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, SmallVectorImpl< Decl *> &Decls)
Get the decls that are contained in a file in the Offset/Length range.
Definition: ASTUnit.cpp:2428
clang::CodeCompletionContext::CCC_Statement
Code completion occurred where a statement (or declaration) is expected in a function, method, or block.
Definition: CodeCompleteConsumer.h:219
clang::PrecompiledPreamble::Build
static llvm::ErrorOr< PrecompiledPreamble > Build(const CompilerInvocation &Invocation, const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds, DiagnosticsEngine &Diagnostics, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::shared_ptr< PCHContainerOperations > PCHContainerOps, bool StoreInMemory, PreambleCallbacks &Callbacks)
Try to build PrecompiledPreamble for Invocation.
Definition: PrecompiledPreamble.cpp:232
clang::CodeCompletionContext::CCC_ObjCPropertyAccess
Code completion occurred on the right-hand side of an Objective-C property access expression...
Definition: CodeCompleteConsumer.h:250
clang::ASTUnit::Save
bool Save(StringRef File)
Save this translation unit to a file with the given name.
Definition: ASTUnit.cpp:2265
ASTUnit.h
clang::ASTUnit::Reparse
bool Reparse(std::shared_ptr< PCHContainerOperations > PCHContainerOps, ArrayRef< RemappedFile > RemappedFiles=None, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=nullptr)
Reparse the source files using the same command-line options that were originally used to produce thi...
Definition: ASTUnit.cpp:1796
clang::ASTUnit::cached_completion_begin
cached_completion_iterator cached_completion_begin()
Definition: ASTUnit.h:609
clang::ASTUnit::StandaloneDiagnostic::LocOffset
unsigned LocOffset
Definition: ASTUnit.h:101
clang::ASTReaderListener
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:127
clang::ASTUnit::getEndOfPreambleFileID
SourceLocation getEndOfPreambleFileID() const
Definition: ASTUnit.cpp:2549
clang::CompilerInstance::getTarget
TargetInfo & getTarget() const
Definition: CompilerInstance.h:365
clang::FrontendOptions::CodeCompleteOpts
CodeCompleteOptions CodeCompleteOpts
Definition: FrontendOptions.h:306
clang::PreambleBounds::Size
unsigned Size
Size of the preamble in bytes.
Definition: Lexer.h:61
clang::CharSourceRange::getEnd
SourceLocation getEnd() const
Definition: SourceLocation.h:267
clang::CompilerInstance::hasTarget
bool hasTarget() const
Definition: CompilerInstance.h:363
MultiplexConsumer.h
clang::ASTUnit::getASTContext
const ASTContext & getASTContext() const
Definition: ASTUnit.h:434
PPCallbacks.h
Defines the PPCallbacks interface.
clang::CompilerInstance::hasASTContext
bool hasASTContext() const
Definition: CompilerInstance.h:467
clang::InputKind::ObjCXX
Definition: FrontendOptions.h:160
HeaderSearchOptions.h
clang::FileSystemOptions
Keeps track of options that affect how file operations are performed.
Definition: FileSystemOptions.h:23
clang::ASTContext::getCanonicalType
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2253
serializeUnit
static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl< char > &Buffer, Sema &S, bool hasErrors, raw_ostream &OS)
Definition: ASTUnit.cpp:2297
PCHContainerOperations.h
clang::CompilerInstance::getPreprocessor
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Definition: CompilerInstance.h:449
clang::CodeCompletionContext::CCC_PreprocessorExpression
Code completion occurred within a preprocessor expression.
Definition: CodeCompleteConsumer.h:293
clang::CodeCompletionContext::CCC_Expression
Code completion occurred where an expression is expected.
Definition: CodeCompleteConsumer.h:222
DeclCXX.h
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::QualType::getUnqualifiedType
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6152
clang::ASTUnit::getDiagnostics
const DiagnosticsEngine & getDiagnostics() const
Definition: ASTUnit.h:424
clang::CodeCompletionContext::CCC_IncludedFile
Code completion inside the filename part of a #include directive.
Definition: CodeCompleteConsumer.h:331
clang::CodeCompleteConsumer::getAllocator
virtual CodeCompletionAllocator & getAllocator()=0
Retrieve the allocator that will be used to allocate code completion strings.
clang::Preprocessor::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:812
clang::ASTUnit::getSourceManager
const SourceManager & getSourceManager() const
Definition: ASTUnit.h:427
DeclGroup.h
clang::CodeCompletionContext::CCC_OtherWithMacros
An unspecified code-completion context where we should also add macro completions.
Definition: CodeCompleteConsumer.h:196
clang::SourceLocation::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:109
clang::CodeCompleteOptions::IncludeFixIts
unsigned IncludeFixIts
Include results after corrections (small fix-its), e.g.
Definition: CodeCompleteOptions.h:45
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
clang::AttrSyntax::C
clang::ASTWriter::WriteAST
ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, Module *WritingModule, StringRef isysroot, bool hasErrors=false)
Write a precompiled header for the given semantic analysis.
Definition: ASTWriter.cpp:4595
clang::FrontendInputFile::isFile
bool isFile() const
Definition: FrontendOptions.h:226
clang::TranslationUnitKind
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:362
clang::DeclContext::getLookupParent
DeclContext * getLookupParent()
Find the parent context of this context that will be used for unqualified name lookup.
Definition: DeclBase.cpp:1047
clang::ASTWriter
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:104
clang::ASTWriter::getDeclID
serialization::DeclID getDeclID(const Decl *D)
Determine the declaration ID of an already-emitted declaration.
Definition: ASTWriter.cpp:5544
clang::CompilerInstance::hadModuleLoaderFatalFailure
bool hadModuleLoaderFatalFailure() const
Definition: CompilerInstance.h:803
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1316
clang::PreprocessorOptions::PrecompiledPreambleBytes
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
Definition: PreprocessorOptions.h:99
TargetInfo.h
Defines the clang::TargetInfo interface.
clang::ASTReader::OutOfDate
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition: ASTReader.h:394
clang::FullSourceLoc
A SourceLocation and its associated SourceManager.
Definition: SourceLocation.h:340
clang::HeaderSearchOptions
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
Definition: HeaderSearchOptions.h:67
clang::TU_Complete
The translation unit is a complete translation unit.
Definition: LangOptions.h:364
clang::STC_Void
Definition: CodeCompleteConsumer.h:159
Module.h
clang::ASTUnit::StandaloneFixIt
Definition: ASTUnit.h:89
clang::ASTUnit::getStartOfMainFileID
SourceLocation getStartOfMainFileID() const
Definition: ASTUnit.cpp:2560
FrontendAction.h
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
clang::CompilerInstance::takeASTConsumer
std::unique_ptr< ASTConsumer > takeASTConsumer()
takeASTConsumer - Remove the current AST consumer and give ownership to the caller.
Definition: CompilerInstance.h:499
clang::CodeCompleteOptions::LoadExternal
unsigned LoadExternal
Hint whether to load data from the external AST to provide full results.
Definition: CodeCompleteOptions.h:41
clang::ASTReader::VersionMismatch
The AST file was written by a different version of Clang.
Definition: ASTReader.h:397
clang::CodeCompletionContext::CCC_DotMemberAccess
Code completion occurred on the right-hand side of a member access expression using the dot operator...
Definition: CodeCompleteConsumer.h:234
clang::PreprocessorOptions::clearRemappedFiles
void clearRemappedFiles()
Definition: PreprocessorOptions.h:187
clang::SimplifiedTypeClass
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
Definition: CodeCompleteConsumer.h:150
clang::CodeCompletionContext::CCC_Type
Code completion occurred where a type name is expected.
Definition: CodeCompleteConsumer.h:272
clang::InputKind::CUDA
Definition: FrontendOptions.h:162
clang::FixItHint
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:66
clang::CodeCompleteOptions::IncludeMacros
unsigned IncludeMacros
Show macros in code completion results.
Definition: CodeCompleteOptions.h:19
clang::Sema::Context
ASTContext & Context
Definition: Sema.h:324
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::DeclContext::isTranslationUnit
bool isTranslationUnit() const
Definition: DeclBase.h:1823
clang::CompilerInvocationBase::getLangOpts
LangOptions * getLangOpts()
Definition: CompilerInvocation.h:87
clang::ASTUnit::getInputKind
InputKind getInputKind() const
Determine the input kind this AST unit represents.
Definition: ASTUnit.cpp:2640
clang::DiagnosticConsumer::BeginSourceFile
virtual void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP=nullptr)
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: Diagnostic.h:1512
clang::prec::Level
Level
Definition: OperatorPrecedence.h:27
Token.h
clang::serialization::ModuleFile::File
const FileEntry * File
The file entry for the module file.
Definition: Module.h:164
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:707
PreprocessingRecord.h
clang::ASTUnit::getLocation
SourceLocation getLocation(const FileEntry *File, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
Definition: ASTUnit.cpp:2471
clang::Preprocessor::addPPCallbacks
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:908
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::FileManager::getVirtualFileSystem
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystem() const
Definition: FileManager.h:225
clang::Decl::getLocation
SourceLocation getLocation() const
Definition: DeclBase.h:418
clang::SourceManager::getDecomposedLoc
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
Definition: SourceManager.h:1196
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:125
clang::ASTUnit::top_level_iterator
std::vector< Decl * >::iterator top_level_iterator
Definition: ASTUnit.h:495
ContinuousRangeMap.h