Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===--- ASTUnit.cpp - ASTUnit utility --------------------------*- C++ -*-===// |
Argyrios Kyrtzidis | 3a08ec1 | 2009-06-20 08:27:14 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeOrdering.h" |
| 20 | #include "clang/Basic/Diagnostic.h" |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 21 | #include "clang/Basic/MemoryBufferCache.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "clang/Basic/TargetOptions.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 24 | #include "clang/Basic/VirtualFileSystem.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/CompilerInstance.h" |
| 26 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendOptions.h" |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/MultiplexConsumer.h" |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/Utils.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 1452ff1 | 2012-10-24 17:46:57 +0000 | [diff] [blame] | 33 | #include "clang/Lex/PreprocessorOptions.h" |
David Blaikie | 0a4e61f | 2013-09-13 18:32:52 +0000 | [diff] [blame] | 34 | #include "clang/Sema/Sema.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "clang/Serialization/ASTReader.h" |
| 36 | #include "clang/Serialization/ASTWriter.h" |
Chris Lattner | ce6c42f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/ArrayRef.h" |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 40a5a7d | 2010-08-16 23:08:34 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Host.h" |
| 42 | #include "llvm/Support/MemoryBuffer.h" |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Mutex.h" |
Ted Kremenek | bd307a5 | 2011-10-27 19:44:25 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MutexGuard.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Timer.h" |
| 46 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 47 | #include <atomic> |
Zhongxing Xu | 318e403 | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 48 | #include <cstdio> |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 49 | #include <cstdlib> |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 50 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 51 | using namespace clang; |
| 52 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 53 | using llvm::TimeRecord; |
| 54 | |
| 55 | namespace { |
| 56 | class SimpleTimer { |
| 57 | bool WantTiming; |
| 58 | TimeRecord Start; |
| 59 | std::string Output; |
| 60 | |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 61 | public: |
Douglas Gregor | 1cbdd95 | 2010-11-01 13:48:43 +0000 | [diff] [blame] | 62 | explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 63 | if (WantTiming) |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 64 | Start = TimeRecord::getCurrentTime(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | void setOutput(const Twine &Output) { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 68 | if (WantTiming) |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 69 | this->Output = Output.str(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 72 | ~SimpleTimer() { |
| 73 | if (WantTiming) { |
| 74 | TimeRecord Elapsed = TimeRecord::getCurrentTime(); |
| 75 | Elapsed -= Start; |
| 76 | llvm::errs() << Output << ':'; |
| 77 | Elapsed.print(Elapsed, llvm::errs()); |
| 78 | llvm::errs() << '\n'; |
| 79 | } |
| 80 | } |
| 81 | }; |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 82 | |
| 83 | template <class T> |
| 84 | std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) { |
| 85 | if (!Val) |
| 86 | return nullptr; |
| 87 | return std::move(*Val); |
| 88 | } |
| 89 | |
| 90 | template <class T> |
| 91 | bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) { |
| 92 | if (!Val) |
| 93 | return false; |
| 94 | Output = std::move(*Val); |
| 95 | return true; |
| 96 | } |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 97 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 98 | /// \brief Get a source buffer for \p MainFilePath, handling all file-to-file |
| 99 | /// and file-to-buffer remappings inside \p Invocation. |
| 100 | static std::unique_ptr<llvm::MemoryBuffer> |
| 101 | getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation, |
| 102 | vfs::FileSystem *VFS, |
| 103 | StringRef FilePath) { |
| 104 | const auto &PreprocessorOpts = Invocation.getPreprocessorOpts(); |
Ted Kremenek | bd307a5 | 2011-10-27 19:44:25 +0000 | [diff] [blame] | 105 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 106 | // Try to determine if the main file has been remapped, either from the |
| 107 | // command line (to another file) or directly through the compiler |
| 108 | // invocation (to a memory buffer). |
| 109 | llvm::MemoryBuffer *Buffer = nullptr; |
| 110 | std::unique_ptr<llvm::MemoryBuffer> BufferOwner; |
| 111 | auto FileStatus = VFS->status(FilePath); |
| 112 | if (FileStatus) { |
| 113 | llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 114 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 115 | // Check whether there is a file-file remapping of the main file |
| 116 | for (const auto &RF : PreprocessorOpts.RemappedFiles) { |
| 117 | std::string MPath(RF.first); |
| 118 | auto MPathStatus = VFS->status(MPath); |
| 119 | if (MPathStatus) { |
| 120 | llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID(); |
| 121 | if (MainFileID == MID) { |
| 122 | // We found a remapping. Try to load the resulting, remapped source. |
| 123 | BufferOwner = valueOrNull(VFS->getBufferForFile(RF.second)); |
| 124 | if (!BufferOwner) |
| 125 | return nullptr; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Check whether there is a file-buffer remapping. It supercedes the |
| 131 | // file-file remapping. |
| 132 | for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) { |
| 133 | std::string MPath(RB.first); |
| 134 | auto MPathStatus = VFS->status(MPath); |
| 135 | if (MPathStatus) { |
| 136 | llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID(); |
| 137 | if (MainFileID == MID) { |
| 138 | // We found a remapping. |
| 139 | BufferOwner.reset(); |
| 140 | Buffer = const_cast<llvm::MemoryBuffer *>(RB.second); |
| 141 | } |
| 142 | } |
| 143 | } |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 144 | } |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 145 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 146 | // If the main source file was not remapped, load it now. |
| 147 | if (!Buffer && !BufferOwner) { |
| 148 | BufferOwner = valueOrNull(VFS->getBufferForFile(FilePath)); |
| 149 | if (!BufferOwner) |
| 150 | return nullptr; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 151 | } |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 152 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 153 | if (BufferOwner) |
| 154 | return BufferOwner; |
| 155 | if (!Buffer) |
| 156 | return nullptr; |
| 157 | return llvm::MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(), FilePath); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 158 | } |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 161 | struct ASTUnit::ASTWriterData { |
| 162 | SmallString<128> Buffer; |
| 163 | llvm::BitstreamWriter Stream; |
| 164 | ASTWriter Writer; |
| 165 | |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 166 | ASTWriterData(MemoryBufferCache &PCMCache) |
| 167 | : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {} |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 170 | void ASTUnit::clearFileLevelDecls() { |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 171 | llvm::DeleteContainerSeconds(FileDecls); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 174 | /// \brief After failing to build a precompiled preamble (due to |
| 175 | /// errors in the source that occurs in the preamble), the number of |
| 176 | /// reparses during which we'll skip even trying to precompile the |
| 177 | /// preamble. |
| 178 | const unsigned DefaultPreambleRebuildInterval = 5; |
| 179 | |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 180 | /// \brief Tracks the number of ASTUnit objects that are currently active. |
| 181 | /// |
| 182 | /// Used for debugging purposes only. |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 183 | static std::atomic<unsigned> ActiveASTUnitObjects; |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 185 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 186 | : Reader(nullptr), HadModuleLoaderFatalFailure(false), |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 187 | OnlyLocalDecls(false), CaptureDiagnostics(false), |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 188 | MainFileIsAST(_MainFileIsAST), |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 189 | TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")), |
Argyrios Kyrtzidis | 4954bc1 | 2011-03-05 01:03:48 +0000 | [diff] [blame] | 190 | OwnsRemappedFileBuffers(true), |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 191 | NumStoredDiagnosticsFromDriver(0), |
Rafael Espindola | 4674a87 | 2014-08-13 17:08:22 +0000 | [diff] [blame] | 192 | PreambleRebuildCounter(0), |
Rafael Espindola | fa49c0b | 2014-08-13 16:47:00 +0000 | [diff] [blame] | 193 | NumWarningsInPreamble(0), |
Douglas Gregor | 2c8bd47 | 2010-08-17 00:40:40 +0000 | [diff] [blame] | 194 | ShouldCacheCodeCompletionResults(false), |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 195 | IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 196 | CompletionCacheTopLevelHashValue(0), |
| 197 | PreambleTopLevelHashValue(0), |
| 198 | CurrentTopLevelHashValue(0), |
Douglas Gregor | 4740c45 | 2010-08-19 00:45:44 +0000 | [diff] [blame] | 199 | UnsafeToFree(false) { |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 200 | if (getenv("LIBCLANG_OBJTRACKING")) |
| 201 | fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects); |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 202 | } |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 204 | ASTUnit::~ASTUnit() { |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 205 | // If we loaded from an AST file, balance out the BeginSourceFile call. |
| 206 | if (MainFileIsAST && getDiagnostics().getClient()) { |
| 207 | getDiagnostics().getClient()->EndSourceFile(); |
| 208 | } |
| 209 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 210 | clearFileLevelDecls(); |
| 211 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 212 | // Free the buffers associated with remapped files. We are required to |
| 213 | // perform this operation here because we explicitly request that the |
| 214 | // compiler instance *not* free these buffers for each invocation of the |
| 215 | // parser. |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 216 | if (Invocation && OwnsRemappedFileBuffers) { |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 217 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 218 | for (const auto &RB : PPOpts.RemappedFileBuffers) |
| 219 | delete RB.second; |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 220 | } |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 222 | ClearCachedCompletionResults(); |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 223 | |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 224 | if (getenv("LIBCLANG_OBJTRACKING")) |
| 225 | fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 226 | } |
| 227 | |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 228 | void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) { |
| 229 | this->PP = std::move(PP); |
| 230 | } |
Argyrios Kyrtzidis | da6e054 | 2012-01-17 18:48:07 +0000 | [diff] [blame] | 231 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 232 | /// \brief Determine the set of code-completion contexts in which this |
| 233 | /// declaration should be shown. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 234 | static unsigned getDeclShowContexts(const NamedDecl *ND, |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 235 | const LangOptions &LangOpts, |
| 236 | bool &IsNestedNameSpecifier) { |
| 237 | IsNestedNameSpecifier = false; |
| 238 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 239 | if (isa<UsingShadowDecl>(ND)) |
| 240 | ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 241 | if (!ND) |
| 242 | return 0; |
| 243 | |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 244 | uint64_t Contexts = 0; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 245 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || |
Erik Verbruggen | 51ee12a | 2017-09-08 09:31:13 +0000 | [diff] [blame^] | 246 | isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND) || |
| 247 | isa<TypeAliasTemplateDecl>(ND)) { |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 248 | // Types can appear in these contexts. |
| 249 | if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 250 | Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel) |
| 251 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 252 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
| 253 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 254 | | (1LL << CodeCompletionContext::CCC_Type) |
| 255 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 256 | |
| 257 | // In C++, types can appear in expressions contexts (for functional casts). |
| 258 | if (LangOpts.CPlusPlus) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 259 | Contexts |= (1LL << CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 260 | |
| 261 | // In Objective-C, message sends can send interfaces. In Objective-C++, |
| 262 | // all types are available due to functional casts. |
| 263 | if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 264 | Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 265 | |
| 266 | // In Objective-C, you can only be a subclass of another Objective-C class |
| 267 | if (isa<ObjCInterfaceDecl>(ND)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 268 | Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 269 | |
| 270 | // Deal with tag names. |
| 271 | if (isa<EnumDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 272 | Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 273 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 274 | // Part of the nested-name-specifier in C++0x. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 275 | if (LangOpts.CPlusPlus11) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 276 | IsNestedNameSpecifier = true; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 277 | } else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 278 | if (Record->isUnion()) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 279 | Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 280 | else |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 281 | Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 283 | if (LangOpts.CPlusPlus) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 284 | IsNestedNameSpecifier = true; |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 285 | } else if (isa<ClassTemplateDecl>(ND)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 286 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 287 | } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
| 288 | // Values can appear in these contexts. |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 289 | Contexts = (1LL << CodeCompletionContext::CCC_Statement) |
| 290 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 291 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
| 292 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 293 | } else if (isa<ObjCProtocolDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 294 | Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 295 | } else if (isa<ObjCCategoryDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 296 | Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 297 | } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 298 | Contexts = (1LL << CodeCompletionContext::CCC_Namespace); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 299 | |
| 300 | // Part of the nested-name-specifier. |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 301 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return Contexts; |
| 305 | } |
| 306 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 307 | void ASTUnit::CacheCodeCompletionResults() { |
| 308 | if (!TheSema) |
| 309 | return; |
| 310 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 311 | SimpleTimer Timer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 312 | Timer.setOutput("Cache global code completions for " + getMainFileName()); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 313 | |
| 314 | // Clear out the previous results. |
| 315 | ClearCachedCompletionResults(); |
| 316 | |
| 317 | // Gather the set of global code completions. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 318 | typedef CodeCompletionResult Result; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 319 | SmallVector<Result, 8> Results; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 320 | CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); |
Argyrios Kyrtzidis | 2bafa00 | 2012-11-16 03:34:57 +0000 | [diff] [blame] | 321 | CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 322 | TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, |
Argyrios Kyrtzidis | 2bafa00 | 2012-11-16 03:34:57 +0000 | [diff] [blame] | 323 | CCTUInfo, Results); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 324 | |
| 325 | // Translate global code completions into cached completions. |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 326 | llvm::DenseMap<CanQualType, unsigned> CompletionTypes; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 327 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 328 | |
| 329 | for (Result &R : Results) { |
| 330 | switch (R.Kind) { |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 331 | case Result::RK_Declaration: { |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 332 | bool IsNestedNameSpecifier = false; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 333 | CachedCodeCompletionResult CachedResult; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 334 | CachedResult.Completion = R.CreateCodeCompletionString( |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 335 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 336 | IncludeBriefCommentsInCodeCompletion); |
| 337 | CachedResult.ShowInContexts = getDeclShowContexts( |
| 338 | R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier); |
| 339 | CachedResult.Priority = R.Priority; |
| 340 | CachedResult.Kind = R.CursorKind; |
| 341 | CachedResult.Availability = R.Availability; |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 342 | |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 343 | // Keep track of the type of this completion in an ASTContext-agnostic |
| 344 | // way. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 345 | QualType UsageType = getDeclUsageType(*Ctx, R.Declaration); |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 346 | if (UsageType.isNull()) { |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 347 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 348 | CachedResult.Type = 0; |
| 349 | } else { |
| 350 | CanQualType CanUsageType |
| 351 | = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); |
| 352 | CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); |
| 353 | |
| 354 | // Determine whether we have already seen this type. If so, we save |
| 355 | // ourselves the work of formatting the type string by using the |
| 356 | // temporary, CanQualType-based hash table to find the associated value. |
| 357 | unsigned &TypeValue = CompletionTypes[CanUsageType]; |
| 358 | if (TypeValue == 0) { |
| 359 | TypeValue = CompletionTypes.size(); |
| 360 | CachedCompletionTypes[QualType(CanUsageType).getAsString()] |
| 361 | = TypeValue; |
| 362 | } |
| 363 | |
| 364 | CachedResult.Type = TypeValue; |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 365 | } |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 367 | CachedCompletionResults.push_back(CachedResult); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 368 | |
| 369 | /// Handle nested-name-specifiers in C++. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 370 | if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier && |
| 371 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 372 | // The contexts in which a nested-name-specifier can appear in C++. |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 373 | uint64_t NNSContexts |
| 374 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
| 375 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 376 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
| 377 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 378 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 379 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
| 380 | | (1LL << CodeCompletionContext::CCC_EnumTag) |
| 381 | | (1LL << CodeCompletionContext::CCC_UnionTag) |
| 382 | | (1LL << CodeCompletionContext::CCC_ClassOrStructTag) |
| 383 | | (1LL << CodeCompletionContext::CCC_Type) |
| 384 | | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName) |
| 385 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 386 | |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 387 | if (isa<NamespaceDecl>(R.Declaration) || |
| 388 | isa<NamespaceAliasDecl>(R.Declaration)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 389 | NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 390 | |
| 391 | if (unsigned RemainingContexts |
| 392 | = NNSContexts & ~CachedResult.ShowInContexts) { |
| 393 | // If there any contexts where this completion can be a |
| 394 | // nested-name-specifier but isn't already an option, create a |
| 395 | // nested-name-specifier completion. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 396 | R.StartsNestedNameSpecifier = true; |
| 397 | CachedResult.Completion = R.CreateCodeCompletionString( |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 398 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 399 | IncludeBriefCommentsInCodeCompletion); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 400 | CachedResult.ShowInContexts = RemainingContexts; |
| 401 | CachedResult.Priority = CCP_NestedNameSpecifier; |
| 402 | CachedResult.TypeClass = STC_Void; |
| 403 | CachedResult.Type = 0; |
| 404 | CachedCompletionResults.push_back(CachedResult); |
| 405 | } |
| 406 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 407 | break; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 410 | case Result::RK_Keyword: |
| 411 | case Result::RK_Pattern: |
| 412 | // Ignore keywords and patterns; we don't care, since they are so |
| 413 | // easily regenerated. |
| 414 | break; |
| 415 | |
| 416 | case Result::RK_Macro: { |
| 417 | CachedCodeCompletionResult CachedResult; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 418 | CachedResult.Completion = R.CreateCodeCompletionString( |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 419 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 420 | IncludeBriefCommentsInCodeCompletion); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 421 | CachedResult.ShowInContexts |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 422 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
| 423 | | (1LL << CodeCompletionContext::CCC_ObjCInterface) |
| 424 | | (1LL << CodeCompletionContext::CCC_ObjCImplementation) |
| 425 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 426 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
| 427 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 428 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 429 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
| 430 | | (1LL << CodeCompletionContext::CCC_MacroNameUse) |
| 431 | | (1LL << CodeCompletionContext::CCC_PreprocessorExpression) |
| 432 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
| 433 | | (1LL << CodeCompletionContext::CCC_OtherWithMacros); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 434 | |
| 435 | CachedResult.Priority = R.Priority; |
| 436 | CachedResult.Kind = R.CursorKind; |
| 437 | CachedResult.Availability = R.Availability; |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 438 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 439 | CachedResult.Type = 0; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 440 | CachedCompletionResults.push_back(CachedResult); |
| 441 | break; |
| 442 | } |
| 443 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 444 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 445 | |
| 446 | // Save the current top-level hash value. |
| 447 | CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void ASTUnit::ClearCachedCompletionResults() { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 451 | CachedCompletionResults.clear(); |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 452 | CachedCompletionTypes.clear(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 453 | CachedCompletionAllocator = nullptr; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 456 | namespace { |
| 457 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 458 | /// \brief Gathers information from ASTReader that will be used to initialize |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 459 | /// a Preprocessor. |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 460 | class ASTInfoCollector : public ASTReaderListener { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 461 | Preprocessor &PP; |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 462 | ASTContext *Context; |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 463 | HeaderSearchOptions &HSOpts; |
| 464 | PreprocessorOptions &PPOpts; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 465 | LangOptions &LangOpt; |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 466 | std::shared_ptr<TargetOptions> &TargetOpts; |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 467 | IntrusiveRefCntPtr<TargetInfo> &Target; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 468 | unsigned &Counter; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 470 | bool InitializedLanguage; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 471 | public: |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 472 | ASTInfoCollector(Preprocessor &PP, ASTContext *Context, |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 473 | HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts, |
| 474 | LangOptions &LangOpt, |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 475 | std::shared_ptr<TargetOptions> &TargetOpts, |
| 476 | IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter) |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 477 | : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts), |
| 478 | LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target), |
| 479 | Counter(Counter), InitializedLanguage(false) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Richard Smith | 1e2cf0d | 2014-10-31 02:28:58 +0000 | [diff] [blame] | 481 | bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, |
| 482 | bool AllowCompatibleDifferences) override { |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 483 | if (InitializedLanguage) |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 484 | return false; |
| 485 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 486 | LangOpt = LangOpts; |
| 487 | InitializedLanguage = true; |
| 488 | |
| 489 | updated(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 490 | return false; |
| 491 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 493 | virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, |
| 494 | StringRef SpecificModuleCachePath, |
| 495 | bool Complain) override { |
| 496 | this->HSOpts = HSOpts; |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | virtual bool |
| 501 | ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, bool Complain, |
| 502 | std::string &SuggestedPredefines) override { |
| 503 | this->PPOpts = PPOpts; |
| 504 | return false; |
| 505 | } |
| 506 | |
Chandler Carruth | 0d745bc | 2015-03-14 04:47:43 +0000 | [diff] [blame] | 507 | bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, |
| 508 | bool AllowCompatibleDifferences) override { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 509 | // If we've already initialized the target, don't do it again. |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 510 | if (Target) |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 511 | return false; |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 512 | |
| 513 | this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts); |
| 514 | Target = |
| 515 | TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts); |
Argyrios Kyrtzidis | 9e1fb56 | 2012-09-14 20:24:53 +0000 | [diff] [blame] | 516 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 517 | updated(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 518 | return false; |
| 519 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 521 | void ReadCounter(const serialization::ModuleFile &M, |
| 522 | unsigned Value) override { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 523 | Counter = Value; |
| 524 | } |
Argyrios Kyrtzidis | 9e1fb56 | 2012-09-14 20:24:53 +0000 | [diff] [blame] | 525 | |
| 526 | private: |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 527 | void updated() { |
| 528 | if (!Target || !InitializedLanguage) |
| 529 | return; |
| 530 | |
| 531 | // Inform the target of the language options. |
| 532 | // |
| 533 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 534 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 535 | Target->adjust(LangOpt); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 536 | |
| 537 | // Initialize the preprocessor. |
| 538 | PP.Initialize(*Target); |
| 539 | |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 540 | if (!Context) |
| 541 | return; |
| 542 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 543 | // Initialize the ASTContext |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 544 | Context->InitBuiltinTypes(*Target); |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 545 | |
Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 546 | // Adjust printing policy based on language options. |
| 547 | Context->setPrintingPolicy(PrintingPolicy(LangOpt)); |
| 548 | |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 549 | // We didn't have access to the comment options when the ASTContext was |
| 550 | // constructed, so register them now. |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 551 | Context->getCommentCommandTraits().registerCommentOptions( |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 552 | LangOpt.CommentOpts); |
Argyrios Kyrtzidis | 9e1fb56 | 2012-09-14 20:24:53 +0000 | [diff] [blame] | 553 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 554 | }; |
| 555 | |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 556 | /// \brief Diagnostic consumer that saves each diagnostic it is given. |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 557 | class StoredDiagnosticConsumer : public DiagnosticConsumer { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 558 | SmallVectorImpl<StoredDiagnostic> *StoredDiags; |
| 559 | SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags; |
| 560 | const LangOptions *LangOpts; |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 561 | SourceManager *SourceMgr; |
| 562 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 563 | public: |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 564 | StoredDiagnosticConsumer( |
| 565 | SmallVectorImpl<StoredDiagnostic> *StoredDiags, |
| 566 | SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags) |
| 567 | : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags), |
| 568 | LangOpts(nullptr), SourceMgr(nullptr) { |
| 569 | assert((StoredDiags || StandaloneDiags) && |
| 570 | "No output collections were passed to StoredDiagnosticConsumer."); |
| 571 | } |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 572 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 573 | void BeginSourceFile(const LangOptions &LangOpts, |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 574 | const Preprocessor *PP = nullptr) override { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 575 | this->LangOpts = &LangOpts; |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 576 | if (PP) |
| 577 | SourceMgr = &PP->getSourceManager(); |
| 578 | } |
| 579 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 580 | void HandleDiagnostic(DiagnosticsEngine::Level Level, |
| 581 | const Diagnostic &Info) override; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 582 | }; |
| 583 | |
| 584 | /// \brief RAII object that optionally captures diagnostics, if |
| 585 | /// there is no diagnostic client to capture them already. |
| 586 | class CaptureDroppedDiagnostics { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 587 | DiagnosticsEngine &Diags; |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 588 | StoredDiagnosticConsumer Client; |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 589 | DiagnosticConsumer *PreviousClient; |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 590 | std::unique_ptr<DiagnosticConsumer> OwningPreviousClient; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 591 | |
| 592 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 593 | CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags, |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 594 | SmallVectorImpl<StoredDiagnostic> *StoredDiags, |
| 595 | SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags) |
| 596 | : Diags(Diags), Client(StoredDiags, StandaloneDiags), PreviousClient(nullptr) |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 597 | { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 598 | if (RequestCapture || Diags.getClient() == nullptr) { |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 599 | OwningPreviousClient = Diags.takeClient(); |
| 600 | PreviousClient = Diags.getClient(); |
| 601 | Diags.setClient(&Client, false); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 602 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | ~CaptureDroppedDiagnostics() { |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 606 | if (Diags.getClient() == &Client) |
| 607 | Diags.setClient(PreviousClient, !!OwningPreviousClient.release()); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 608 | } |
| 609 | }; |
| 610 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 611 | } // anonymous namespace |
| 612 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 613 | static ASTUnit::StandaloneDiagnostic |
| 614 | makeStandaloneDiagnostic(const LangOptions &LangOpts, |
| 615 | const StoredDiagnostic &InDiag); |
| 616 | |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 617 | void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level, |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 618 | const Diagnostic &Info) { |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 619 | // Default implementation (Warnings/errors count). |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 620 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 621 | |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 622 | // Only record the diagnostic if it's part of the source manager we know |
| 623 | // about. This effectively drops diagnostics from modules we're building. |
| 624 | // FIXME: In the long run, ee don't want to drop source managers from modules. |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 625 | if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) { |
| 626 | StoredDiagnostic *ResultDiag = nullptr; |
| 627 | if (StoredDiags) { |
| 628 | StoredDiags->emplace_back(Level, Info); |
| 629 | ResultDiag = &StoredDiags->back(); |
| 630 | } |
| 631 | |
| 632 | if (StandaloneDiags) { |
| 633 | llvm::Optional<StoredDiagnostic> StoredDiag = llvm::None; |
| 634 | if (!ResultDiag) { |
| 635 | StoredDiag.emplace(Level, Info); |
| 636 | ResultDiag = StoredDiag.getPointer(); |
| 637 | } |
| 638 | StandaloneDiags->push_back( |
| 639 | makeStandaloneDiagnostic(*LangOpts, *ResultDiag)); |
| 640 | } |
| 641 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Argyrios Kyrtzidis | a38cb20 | 2017-01-30 06:05:58 +0000 | [diff] [blame] | 644 | IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const { |
| 645 | return Reader; |
| 646 | } |
| 647 | |
Argyrios Kyrtzidis | 1c7455f | 2013-05-10 01:28:51 +0000 | [diff] [blame] | 648 | ASTMutationListener *ASTUnit::getASTMutationListener() { |
| 649 | if (WriterData) |
| 650 | return &WriterData->Writer; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 651 | return nullptr; |
Argyrios Kyrtzidis | 1c7455f | 2013-05-10 01:28:51 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 654 | ASTDeserializationListener *ASTUnit::getDeserializationListener() { |
| 655 | if (WriterData) |
| 656 | return &WriterData->Writer; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 657 | return nullptr; |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Rafael Espindola | 16e1ba1 | 2014-08-26 20:17:44 +0000 | [diff] [blame] | 660 | std::unique_ptr<llvm::MemoryBuffer> |
| 661 | ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) { |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 662 | assert(FileMgr); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 663 | auto Buffer = FileMgr->getBufferForFile(Filename); |
| 664 | if (Buffer) |
| 665 | return std::move(*Buffer); |
| 666 | if (ErrorStr) |
| 667 | *ErrorStr = Buffer.getError().message(); |
| 668 | return nullptr; |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 671 | /// \brief Configure the diagnostics object for use with ASTUnit. |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 672 | void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 673 | ASTUnit &AST, bool CaptureDiagnostics) { |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 674 | assert(Diags.get() && "no DiagnosticsEngine was provided"); |
| 675 | if (CaptureDiagnostics) |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 676 | Diags->setClient(new StoredDiagnosticConsumer(&AST.StoredDiagnostics, nullptr)); |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 677 | } |
| 678 | |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 679 | std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile( |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 680 | const std::string &Filename, const PCHContainerReader &PCHContainerRdr, |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 681 | WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 682 | const FileSystemOptions &FileSystemOpts, bool UseDebugInfo, |
| 683 | bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles, |
| 684 | bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors, |
| 685 | bool UserFilesAreVolatile) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 686 | std::unique_ptr<ASTUnit> AST(new ASTUnit(true)); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 687 | |
| 688 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 689 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 690 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 691 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 692 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 693 | DiagCleanup(Diags.get()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 694 | |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 695 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 696 | |
Richard Smith | ab75597 | 2017-06-05 18:10:11 +0000 | [diff] [blame] | 697 | AST->LangOpts = std::make_shared<LangOptions>(); |
Douglas Gregor | 16bef85 | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 698 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 699 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 700 | AST->Diagnostics = Diags; |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 701 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem(); |
| 702 | AST->FileMgr = new FileManager(FileSystemOpts, VFS); |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 703 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 704 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 705 | AST->getFileManager(), |
| 706 | UserFilesAreVolatile); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 707 | AST->PCMCache = new MemoryBufferCache; |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 708 | AST->HSOpts = std::make_shared<HeaderSearchOptions>(); |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 709 | AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat(); |
Douglas Gregor | b85b9cc | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 710 | AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts, |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 711 | AST->getSourceManager(), |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 712 | AST->getDiagnostics(), |
Richard Smith | ab75597 | 2017-06-05 18:10:11 +0000 | [diff] [blame] | 713 | AST->getLangOpts(), |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 714 | /*Target=*/nullptr)); |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 715 | AST->PPOpts = std::make_shared<PreprocessorOptions>(); |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 716 | |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 717 | for (const auto &RemappedFile : RemappedFiles) |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 718 | AST->PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second); |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 719 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 720 | // Gather Info for preprocessor construction later on. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 722 | HeaderSearch &HeaderInfo = *AST->HeaderInfo; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 723 | unsigned Counter; |
| 724 | |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 725 | AST->PP = std::make_shared<Preprocessor>( |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 726 | AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts, |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 727 | AST->getSourceManager(), *AST->PCMCache, HeaderInfo, AST->ModuleLoader, |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 728 | /*IILookup=*/nullptr, |
| 729 | /*OwnsHeaderSearch=*/false); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 730 | Preprocessor &PP = *AST->PP; |
| 731 | |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 732 | if (ToLoad >= LoadASTOnly) |
| 733 | AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(), |
| 734 | PP.getIdentifierTable(), PP.getSelectorTable(), |
| 735 | PP.getBuiltinInfo()); |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 736 | |
Argyrios Kyrtzidis | 945a819 | 2012-09-15 01:10:20 +0000 | [diff] [blame] | 737 | bool disableValid = false; |
| 738 | if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) |
| 739 | disableValid = true; |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 740 | AST->Reader = new ASTReader(PP, AST->Ctx.get(), PCHContainerRdr, { }, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 741 | /*isysroot=*/"", |
| 742 | /*DisableValidation=*/disableValid, |
| 743 | AllowPCHWithCompilerErrors); |
Ted Kremenek | 2159b8d | 2011-05-04 23:27:12 +0000 | [diff] [blame] | 744 | |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 745 | AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 746 | *AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts, |
Richard Smith | 1893475 | 2017-06-06 00:32:01 +0000 | [diff] [blame] | 747 | AST->TargetOpts, AST->Target, Counter)); |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 748 | |
Argyrios Kyrtzidis | f0b4cd1 | 2015-03-03 08:04:19 +0000 | [diff] [blame] | 749 | // Attach the AST reader to the AST context as an external AST |
| 750 | // source, so that declarations will be deserialized from the |
| 751 | // AST file as needed. |
| 752 | // We need the external source to be set up before we read the AST, because |
| 753 | // eagerly-deserialized declarations may use it. |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 754 | if (AST->Ctx) |
| 755 | AST->Ctx->setExternalSource(AST->Reader); |
Argyrios Kyrtzidis | f0b4cd1 | 2015-03-03 08:04:19 +0000 | [diff] [blame] | 756 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 757 | switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile, |
Argyrios Kyrtzidis | 2ec2936 | 2012-11-15 18:57:22 +0000 | [diff] [blame] | 758 | SourceLocation(), ASTReader::ARR_None)) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 759 | case ASTReader::Success: |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 760 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 762 | case ASTReader::Failure: |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 763 | case ASTReader::Missing: |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 764 | case ASTReader::OutOfDate: |
| 765 | case ASTReader::VersionMismatch: |
| 766 | case ASTReader::ConfigurationMismatch: |
| 767 | case ASTReader::HadErrors: |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 768 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 769 | return nullptr; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 770 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 772 | AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile(); |
Daniel Dunbar | a8a5093 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 773 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 774 | PP.setCounterValue(Counter); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 776 | // Create an AST consumer, even though it isn't used. |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 777 | if (ToLoad >= LoadASTOnly) |
| 778 | AST->Consumer.reset(new ASTConsumer); |
| 779 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 780 | // Create a semantic analysis object and tell the AST reader about it. |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 781 | if (ToLoad >= LoadEverything) { |
| 782 | AST->TheSema.reset(new Sema(PP, *AST->Ctx, *AST->Consumer)); |
| 783 | AST->TheSema->Initialize(); |
| 784 | AST->Reader->InitializeSema(*AST->TheSema); |
| 785 | } |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 787 | // Tell the diagnostic client that we have started a source file. |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 788 | AST->getDiagnostics().getClient()->BeginSourceFile(PP.getLangOpts(), &PP); |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 789 | |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 790 | return AST; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 791 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 792 | |
| 793 | namespace { |
| 794 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 795 | /// \brief Add the given macro to the hash of all top-level entities. |
| 796 | void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash) { |
| 797 | Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash); |
| 798 | } |
| 799 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 800 | /// \brief Preprocessor callback class that updates a hash value with the names |
| 801 | /// of all macros that have been defined by the translation unit. |
| 802 | class MacroDefinitionTrackerPPCallbacks : public PPCallbacks { |
| 803 | unsigned &Hash; |
| 804 | |
| 805 | public: |
| 806 | explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 807 | |
| 808 | void MacroDefined(const Token &MacroNameTok, |
| 809 | const MacroDirective *MD) override { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 810 | AddDefinedMacroToHash(MacroNameTok, Hash); |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 811 | } |
| 812 | }; |
| 813 | |
| 814 | /// \brief Add the given declaration to the hash of all top-level entities. |
| 815 | void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) { |
| 816 | if (!D) |
| 817 | return; |
| 818 | |
| 819 | DeclContext *DC = D->getDeclContext(); |
| 820 | if (!DC) |
| 821 | return; |
| 822 | |
| 823 | if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit())) |
| 824 | return; |
| 825 | |
| 826 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
Argyrios Kyrtzidis | ca5c7be | 2013-10-15 17:37:55 +0000 | [diff] [blame] | 827 | if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) { |
| 828 | // For an unscoped enum include the enumerators in the hash since they |
| 829 | // enter the top-level namespace. |
| 830 | if (!EnumD->isScoped()) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 831 | for (const auto *EI : EnumD->enumerators()) { |
| 832 | if (EI->getIdentifier()) |
| 833 | Hash = llvm::HashString(EI->getIdentifier()->getName(), Hash); |
Argyrios Kyrtzidis | ca5c7be | 2013-10-15 17:37:55 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 838 | if (ND->getIdentifier()) |
| 839 | Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash); |
| 840 | else if (DeclarationName Name = ND->getDeclName()) { |
| 841 | std::string NameStr = Name.getAsString(); |
| 842 | Hash = llvm::HashString(NameStr, Hash); |
| 843 | } |
| 844 | return; |
Argyrios Kyrtzidis | 48d88de | 2013-06-24 21:19:12 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) { |
| 848 | if (Module *Mod = ImportD->getImportedModule()) { |
| 849 | std::string ModName = Mod->getFullModuleName(); |
| 850 | Hash = llvm::HashString(ModName, Hash); |
| 851 | } |
| 852 | return; |
| 853 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 856 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 857 | ASTUnit &Unit; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 858 | unsigned &Hash; |
| 859 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 860 | public: |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 861 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash) |
| 862 | : Unit(_Unit), Hash(Hash) { |
| 863 | Hash = 0; |
| 864 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 865 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 866 | void handleTopLevelDecl(Decl *D) { |
Argyrios Kyrtzidis | 516eec2 | 2011-11-16 02:35:10 +0000 | [diff] [blame] | 867 | if (!D) |
| 868 | return; |
| 869 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 870 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 871 | // reported as top-level declarations, even though their DeclContext |
| 872 | // is the containing ObjC @interface/@implementation. This is a |
| 873 | // fundamental problem in the parser right now. |
| 874 | if (isa<ObjCMethodDecl>(D)) |
| 875 | return; |
| 876 | |
| 877 | AddTopLevelDeclarationToHash(D, Hash); |
| 878 | Unit.addTopLevelDecl(D); |
| 879 | |
| 880 | handleFileLevelDecl(D); |
| 881 | } |
| 882 | |
| 883 | void handleFileLevelDecl(Decl *D) { |
| 884 | Unit.addFileLevelDecl(D); |
| 885 | if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) { |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 886 | for (auto *I : NSD->decls()) |
| 887 | handleFileLevelDecl(I); |
Ted Kremenek | acc59c3 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 888 | } |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 889 | } |
Sebastian Redl | eaa4ade | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 890 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 891 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 892 | for (Decl *TopLevelDecl : D) |
| 893 | handleTopLevelDecl(TopLevelDecl); |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 894 | return true; |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Sebastian Redl | eaa4ade | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 897 | // We're not interested in "interesting" decls. |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 898 | void HandleInterestingDecl(DeclGroupRef) override {} |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 899 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 900 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 901 | for (Decl *TopLevelDecl : D) |
| 902 | handleTopLevelDecl(TopLevelDecl); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 903 | } |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 904 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 905 | ASTMutationListener *GetASTMutationListener() override { |
Argyrios Kyrtzidis | 1c7455f | 2013-05-10 01:28:51 +0000 | [diff] [blame] | 906 | return Unit.getASTMutationListener(); |
| 907 | } |
| 908 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 909 | ASTDeserializationListener *GetASTDeserializationListener() override { |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 910 | return Unit.getDeserializationListener(); |
| 911 | } |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 912 | }; |
| 913 | |
| 914 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 915 | public: |
| 916 | ASTUnit &Unit; |
| 917 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 918 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 919 | StringRef InFile) override { |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 920 | CI.getPreprocessor().addPPCallbacks( |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 921 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
| 922 | Unit.getCurrentTopLevelHashValue())); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 923 | return llvm::make_unique<TopLevelDeclTrackerConsumer>( |
| 924 | Unit, Unit.getCurrentTopLevelHashValue()); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | public: |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 928 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 929 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 930 | bool hasCodeCompletionSupport() const override { return false; } |
| 931 | TranslationUnitKind getTranslationUnitKind() override { |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 932 | return Unit.getTranslationUnitKind(); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 933 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 934 | }; |
| 935 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 936 | class ASTUnitPreambleCallbacks : public PreambleCallbacks { |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 937 | public: |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 938 | unsigned getHash() const { return Hash; } |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 939 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 940 | std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); } |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 941 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 942 | std::vector<serialization::DeclID> takeTopLevelDeclIDs() { |
| 943 | return std::move(TopLevelDeclIDs); |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 944 | } |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 945 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 946 | void AfterPCHEmitted(ASTWriter &Writer) override { |
| 947 | TopLevelDeclIDs.reserve(TopLevelDecls.size()); |
| 948 | for (Decl *D : TopLevelDecls) { |
| 949 | // Invalid top-level decls may not have been serialized. |
| 950 | if (D->isInvalidDecl()) |
| 951 | continue; |
| 952 | TopLevelDeclIDs.push_back(Writer.getDeclID(D)); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | void HandleTopLevelDecl(DeclGroupRef DG) override { |
Benjamin Kramer | a401b9b | 2015-02-06 18:58:04 +0000 | [diff] [blame] | 957 | for (Decl *D : DG) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 958 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 959 | // reported as top-level declarations, even though their DeclContext |
| 960 | // is the containing ObjC @interface/@implementation. This is a |
| 961 | // fundamental problem in the parser right now. |
| 962 | if (isa<ObjCMethodDecl>(D)) |
| 963 | continue; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 964 | AddTopLevelDeclarationToHash(D, Hash); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 965 | TopLevelDecls.push_back(D); |
| 966 | } |
| 967 | } |
| 968 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 969 | void HandleMacroDefined(const Token &MacroNameTok, |
| 970 | const MacroDirective *MD) override { |
| 971 | AddDefinedMacroToHash(MacroNameTok, Hash); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 972 | } |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 973 | |
| 974 | private: |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 975 | unsigned Hash = 0; |
| 976 | std::vector<Decl *> TopLevelDecls; |
| 977 | std::vector<serialization::DeclID> TopLevelDeclIDs; |
| 978 | llvm::SmallVector<ASTUnit::StandaloneDiagnostic, 4> PreambleDiags; |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 981 | } // anonymous namespace |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 982 | |
Benjamin Kramer | 1ce5d80 | 2013-05-05 12:39:28 +0000 | [diff] [blame] | 983 | static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) { |
| 984 | return StoredDiag.getLocation().isValid(); |
| 985 | } |
| 986 | |
| 987 | static void |
| 988 | checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) { |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 989 | // Get rid of stored diagnostics except the ones from the driver which do not |
| 990 | // have a source location. |
Benjamin Kramer | 1ce5d80 | 2013-05-05 12:39:28 +0000 | [diff] [blame] | 991 | StoredDiags.erase( |
| 992 | std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag), |
| 993 | StoredDiags.end()); |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> & |
| 997 | StoredDiagnostics, |
| 998 | SourceManager &SM) { |
| 999 | // The stored diagnostic has the old source manager in it; update |
| 1000 | // the locations to refer into the new source manager. Since we've |
| 1001 | // been careful to make sure that the source manager's state |
| 1002 | // before and after are identical, so that we can reuse the source |
| 1003 | // location itself. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1004 | for (StoredDiagnostic &SD : StoredDiagnostics) { |
| 1005 | if (SD.getLocation().isValid()) { |
| 1006 | FullSourceLoc Loc(SD.getLocation(), SM); |
| 1007 | SD.setLocation(Loc); |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1012 | /// Parse the source file into a translation unit using the given compiler |
| 1013 | /// invocation, replacing the current translation unit. |
| 1014 | /// |
| 1015 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 1016 | /// contain any translation-unit information, false otherwise. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1017 | bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1018 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer, |
| 1019 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1020 | if (!Invocation) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1021 | return true; |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1023 | // Create the compiler instance to use for building the AST. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1024 | std::unique_ptr<CompilerInstance> Clang( |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1025 | new CompilerInstance(std::move(PCHContainerOps))); |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1026 | if (FileMgr && VFS) { |
| 1027 | assert(VFS == FileMgr->getVirtualFileSystem() && |
| 1028 | "VFS passed to Parse and VFS in FileMgr are different"); |
| 1029 | } else if (VFS) { |
| 1030 | Clang->setVirtualFileSystem(VFS); |
| 1031 | } |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1034 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1035 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1036 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1037 | Clang->setInvocation(std::make_shared<CompilerInvocation>(*Invocation)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1038 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1040 | // Set up diagnostics, capturing any diagnostics that would |
| 1041 | // otherwise be dropped. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1042 | Clang->setDiagnostics(&getDiagnostics()); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1043 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1044 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 1045 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 1046 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1047 | if (!Clang->hasTarget()) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1048 | return true; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1049 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1050 | // Inform the target of the language options. |
| 1051 | // |
| 1052 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1053 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 1054 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1055 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1056 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1057 | "Invocation must have exactly one source file!"); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1058 | assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
| 1059 | InputKind::Source && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1060 | "FIXME: AST inputs not yet supported here!"); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1061 | assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
| 1062 | InputKind::LLVM_IR && |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 1063 | "IR inputs not support here!"); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1064 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1065 | // Configure the various subsystems. |
Alp Toker | 269d840 | 2014-07-06 05:26:07 +0000 | [diff] [blame] | 1066 | LangOpts = Clang->getInvocation().LangOpts; |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1067 | FileSystemOpts = Clang->getFileSystemOpts(); |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1068 | if (!FileMgr) { |
| 1069 | Clang->createFileManager(); |
| 1070 | FileMgr = &Clang->getFileManager(); |
| 1071 | } |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 1072 | |
| 1073 | ResetForParse(); |
| 1074 | |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1075 | SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, |
| 1076 | UserFilesAreVolatile); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1077 | if (!OverrideMainBuffer) { |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1078 | checkAndRemoveNonDriverDiags(StoredDiagnostics); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1079 | TopLevelDeclsInPreamble.clear(); |
| 1080 | } |
| 1081 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1082 | // Create a file manager object to provide access to and cache the filesystem. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1083 | Clang->setFileManager(&getFileManager()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1084 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1085 | // Create the source manager. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1086 | Clang->setSourceManager(&getSourceManager()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1088 | // If the main file has been overridden due to the use of a preamble, |
| 1089 | // make that override happen and introduce the preamble. |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1090 | if (OverrideMainBuffer) { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1091 | assert(Preamble && "No preamble was built, but OverrideMainBuffer is not null"); |
| 1092 | Preamble->AddImplicitPreamble(Clang->getInvocation(), OverrideMainBuffer.get()); |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1094 | // The stored diagnostic has the old source manager in it; update |
| 1095 | // the locations to refer into the new source manager. Since we've |
| 1096 | // been careful to make sure that the source manager's state |
| 1097 | // before and after are identical, so that we can reuse the source |
| 1098 | // location itself. |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1099 | checkAndSanitizeDiags(StoredDiagnostics, getSourceManager()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1100 | |
| 1101 | // Keep track of the override buffer; |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1102 | SavedMainFileBuffer = std::move(OverrideMainBuffer); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1103 | } |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1104 | |
| 1105 | std::unique_ptr<TopLevelDeclTrackerAction> Act( |
| 1106 | new TopLevelDeclTrackerAction(*this)); |
| 1107 | |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1108 | // Recover resources if we crash before exiting this method. |
| 1109 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
| 1110 | ActCleanup(Act.get()); |
| 1111 | |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 1112 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1113 | goto error; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1114 | |
Richard Smith | 26b8f78 | 2016-03-25 21:46:44 +0000 | [diff] [blame] | 1115 | if (SavedMainFileBuffer) |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1116 | TranslateStoredDiagnostics(getFileManager(), getSourceManager(), |
| 1117 | PreambleDiagnostics, StoredDiagnostics); |
Erik Verbruggen | efe6fa5 | 2017-06-09 08:29:58 +0000 | [diff] [blame] | 1118 | else |
| 1119 | PreambleSrcLocCache.clear(); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1120 | |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 1121 | if (!Act->Execute()) |
| 1122 | goto error; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1123 | |
| 1124 | transferASTDataFromCompilerInstance(*Clang); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1125 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 1126 | Act->EndSourceFile(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1127 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1128 | FailedParseDiagnostics.clear(); |
| 1129 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1130 | return false; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1131 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1132 | error: |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1133 | // Remove the overridden buffer we used for the preamble. |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1134 | SavedMainFileBuffer = nullptr; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1135 | |
| 1136 | // Keep the ownership of the data in the ASTUnit because the client may |
| 1137 | // want to see the diagnostics. |
| 1138 | transferASTDataFromCompilerInstance(*Clang); |
| 1139 | FailedParseDiagnostics.swap(StoredDiagnostics); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1140 | StoredDiagnostics.clear(); |
Argyrios Kyrtzidis | 067cbfa | 2011-10-24 17:25:20 +0000 | [diff] [blame] | 1141 | NumStoredDiagnosticsFromDriver = 0; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1142 | return true; |
| 1143 | } |
| 1144 | |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1145 | static std::pair<unsigned, unsigned> |
| 1146 | makeStandaloneRange(CharSourceRange Range, const SourceManager &SM, |
| 1147 | const LangOptions &LangOpts) { |
| 1148 | CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts); |
| 1149 | unsigned Offset = SM.getFileOffset(FileRange.getBegin()); |
| 1150 | unsigned EndOffset = SM.getFileOffset(FileRange.getEnd()); |
| 1151 | return std::make_pair(Offset, EndOffset); |
| 1152 | } |
| 1153 | |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1154 | static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, |
| 1155 | const LangOptions &LangOpts, |
| 1156 | const FixItHint &InFix) { |
| 1157 | ASTUnit::StandaloneFixIt OutFix; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1158 | OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts); |
| 1159 | OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM, |
| 1160 | LangOpts); |
| 1161 | OutFix.CodeToInsert = InFix.CodeToInsert; |
| 1162 | OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions; |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1163 | return OutFix; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1166 | static ASTUnit::StandaloneDiagnostic |
| 1167 | makeStandaloneDiagnostic(const LangOptions &LangOpts, |
| 1168 | const StoredDiagnostic &InDiag) { |
| 1169 | ASTUnit::StandaloneDiagnostic OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1170 | OutDiag.ID = InDiag.getID(); |
| 1171 | OutDiag.Level = InDiag.getLevel(); |
| 1172 | OutDiag.Message = InDiag.getMessage(); |
| 1173 | OutDiag.LocOffset = 0; |
| 1174 | if (InDiag.getLocation().isInvalid()) |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1175 | return OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1176 | const SourceManager &SM = InDiag.getLocation().getManager(); |
| 1177 | SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation()); |
| 1178 | OutDiag.Filename = SM.getFilename(FileLoc); |
| 1179 | if (OutDiag.Filename.empty()) |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1180 | return OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1181 | OutDiag.LocOffset = SM.getFileOffset(FileLoc); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1182 | for (const CharSourceRange &Range : InDiag.getRanges()) |
| 1183 | OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts)); |
| 1184 | for (const FixItHint &FixIt : InDiag.getFixIts()) |
| 1185 | OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt)); |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1186 | |
| 1187 | return OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1190 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 1191 | /// the source file. |
| 1192 | /// |
| 1193 | /// This routine will compute the preamble of the main source file. If a |
| 1194 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 1195 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 1196 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 1197 | /// this routine will determine if it is still valid and, if so, avoid |
| 1198 | /// rebuilding the precompiled preamble. |
| 1199 | /// |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1200 | /// \param AllowRebuild When true (the default), this routine is |
| 1201 | /// allowed to rebuild the precompiled preamble if it is found to be |
| 1202 | /// out-of-date. |
| 1203 | /// |
| 1204 | /// \param MaxLines When non-zero, the maximum number of lines that |
| 1205 | /// can occur within the preamble. |
| 1206 | /// |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1207 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 1208 | /// buffer that should be used in place of the main file when doing so. |
| 1209 | /// Otherwise, returns a NULL pointer. |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 1210 | std::unique_ptr<llvm::MemoryBuffer> |
| 1211 | ASTUnit::getMainBufferWithPrecompiledPreamble( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1212 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1213 | const CompilerInvocation &PreambleInvocationIn, |
| 1214 | IntrusiveRefCntPtr<vfs::FileSystem> VFS, bool AllowRebuild, |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 1215 | unsigned MaxLines) { |
| 1216 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1217 | auto MainFilePath = |
| 1218 | PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile(); |
| 1219 | std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer = |
| 1220 | getBufferForFileHandlingRemapping(PreambleInvocationIn, VFS.get(), |
| 1221 | MainFilePath); |
| 1222 | if (!MainFileBuffer) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1223 | return nullptr; |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1224 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1225 | PreambleBounds Bounds = |
| 1226 | ComputePreambleBounds(*PreambleInvocationIn.getLangOpts(), |
| 1227 | MainFileBuffer.get(), MaxLines); |
| 1228 | if (!Bounds.Size) |
| 1229 | return nullptr; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1230 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1231 | if (Preamble) { |
| 1232 | if (Preamble->CanReuse(PreambleInvocationIn, MainFileBuffer.get(), Bounds, |
| 1233 | VFS.get())) { |
| 1234 | // Okay! We can re-use the precompiled preamble. |
Rafael Espindola | e4777f4 | 2013-07-29 18:22:23 +0000 | [diff] [blame] | 1235 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1236 | // Set the state of the diagnostic object to mimic its state |
| 1237 | // after parsing the preamble. |
| 1238 | getDiagnostics().Reset(); |
| 1239 | ProcessWarningOptions(getDiagnostics(), |
| 1240 | PreambleInvocationIn.getDiagnosticOpts()); |
| 1241 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1242 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1243 | PreambleRebuildCounter = 1; |
| 1244 | return MainFileBuffer; |
| 1245 | } else { |
| 1246 | Preamble.reset(); |
| 1247 | PreambleDiagnostics.clear(); |
| 1248 | TopLevelDeclsInPreamble.clear(); |
| 1249 | PreambleRebuildCounter = 1; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1250 | } |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1251 | } |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1252 | |
| 1253 | // If the preamble rebuild counter > 1, it's because we previously |
| 1254 | // failed to build a preamble and we're not yet ready to try |
| 1255 | // again. Decrement the counter and return a failure. |
| 1256 | if (PreambleRebuildCounter > 1) { |
| 1257 | --PreambleRebuildCounter; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1258 | return nullptr; |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1261 | assert(!Preamble && "No Preamble should be stored at that point"); |
| 1262 | // If we aren't allowed to rebuild the precompiled preamble, just |
| 1263 | // return now. |
| 1264 | if (!AllowRebuild) |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1265 | return nullptr; |
| 1266 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1267 | SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone; |
| 1268 | SmallVector<StoredDiagnostic, 4> NewPreambleDiags; |
Ilya Biryukov | f81d46f | 2017-06-21 12:34:27 +0000 | [diff] [blame] | 1269 | ASTUnitPreambleCallbacks Callbacks; |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1270 | { |
| 1271 | llvm::Optional<CaptureDroppedDiagnostics> Capture; |
| 1272 | if (CaptureDiagnostics) |
| 1273 | Capture.emplace(/*RequestCapture=*/true, *Diagnostics, &NewPreambleDiags, |
| 1274 | &NewPreambleDiagsStandalone); |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1275 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1276 | // We did not previously compute a preamble, or it can't be reused anyway. |
| 1277 | SimpleTimer PreambleTimer(WantTiming); |
| 1278 | PreambleTimer.setOutput("Precompiling preamble"); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1279 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1280 | llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build( |
| 1281 | PreambleInvocationIn, MainFileBuffer.get(), Bounds, *Diagnostics, VFS, |
| 1282 | PCHContainerOps, Callbacks); |
| 1283 | if (NewPreamble) { |
| 1284 | Preamble = std::move(*NewPreamble); |
| 1285 | PreambleRebuildCounter = 1; |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1286 | } else { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1287 | switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) { |
| 1288 | case BuildPreambleError::CouldntCreateTempFile: |
| 1289 | case BuildPreambleError::PreambleIsEmpty: |
| 1290 | // Try again next time. |
| 1291 | PreambleRebuildCounter = 1; |
Ilya Biryukov | f81d46f | 2017-06-21 12:34:27 +0000 | [diff] [blame] | 1292 | return nullptr; |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1293 | case BuildPreambleError::CouldntCreateTargetInfo: |
| 1294 | case BuildPreambleError::BeginSourceFileFailed: |
| 1295 | case BuildPreambleError::CouldntEmitPCH: |
| 1296 | case BuildPreambleError::CouldntCreateVFSOverlay: |
| 1297 | // These erros are more likely to repeat, retry after some period. |
| 1298 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Ilya Biryukov | f81d46f | 2017-06-21 12:34:27 +0000 | [diff] [blame] | 1299 | return nullptr; |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1300 | } |
Ilya Biryukov | f81d46f | 2017-06-21 12:34:27 +0000 | [diff] [blame] | 1301 | llvm_unreachable("unexpected BuildPreambleError"); |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1302 | } |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1303 | } |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 1304 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1305 | assert(Preamble && "Preamble wasn't built"); |
| 1306 | |
| 1307 | TopLevelDecls.clear(); |
| 1308 | TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs(); |
| 1309 | PreambleTopLevelHashValue = Callbacks.getHash(); |
| 1310 | |
| 1311 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
| 1312 | |
| 1313 | checkAndRemoveNonDriverDiags(NewPreambleDiags); |
| 1314 | StoredDiagnostics = std::move(NewPreambleDiags); |
| 1315 | PreambleDiagnostics = std::move(NewPreambleDiagsStandalone); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1316 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 1317 | // If the hash of top-level entities differs from the hash of the top-level |
| 1318 | // entities the last time we rebuilt the preamble, clear out the completion |
| 1319 | // cache. |
| 1320 | if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) { |
| 1321 | CompletionCacheTopLevelHashValue = 0; |
| 1322 | PreambleTopLevelHashValue = CurrentTopLevelHashValue; |
| 1323 | } |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 1324 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1325 | return MainFileBuffer; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1326 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1327 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1328 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1329 | assert(Preamble && "Should only be called when preamble was built"); |
| 1330 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1331 | std::vector<Decl *> Resolved; |
| 1332 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
| 1333 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1334 | for (serialization::DeclID TopLevelDecl : TopLevelDeclsInPreamble) { |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1335 | // Resolve the declaration ID to an actual declaration, possibly |
| 1336 | // deserializing the declaration in the process. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1337 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1338 | Resolved.push_back(D); |
| 1339 | } |
| 1340 | TopLevelDeclsInPreamble.clear(); |
| 1341 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 1342 | } |
| 1343 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1344 | void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 1345 | // Steal the created target, context, and preprocessor if they have been |
| 1346 | // created. |
| 1347 | assert(CI.hasInvocation() && "missing invocation"); |
Alp Toker | 269d840 | 2014-07-06 05:26:07 +0000 | [diff] [blame] | 1348 | LangOpts = CI.getInvocation().LangOpts; |
David Blaikie | ec99b5e | 2014-08-10 19:14:48 +0000 | [diff] [blame] | 1349 | TheSema = CI.takeSema(); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1350 | Consumer = CI.takeASTConsumer(); |
Ben Langmuir | 532fdc0 | 2014-04-18 20:39:48 +0000 | [diff] [blame] | 1351 | if (CI.hasASTContext()) |
| 1352 | Ctx = &CI.getASTContext(); |
| 1353 | if (CI.hasPreprocessor()) |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 1354 | PP = CI.getPreprocessorPtr(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1355 | CI.setSourceManager(nullptr); |
| 1356 | CI.setFileManager(nullptr); |
Ben Langmuir | 532fdc0 | 2014-04-18 20:39:48 +0000 | [diff] [blame] | 1357 | if (CI.hasTarget()) |
| 1358 | Target = &CI.getTarget(); |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1359 | Reader = CI.getModuleManager(); |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 1360 | HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure(); |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1363 | StringRef ASTUnit::getMainFileName() const { |
Argyrios Kyrtzidis | 928e1fd | 2013-01-11 22:11:14 +0000 | [diff] [blame] | 1364 | if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) { |
| 1365 | const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0]; |
| 1366 | if (Input.isFile()) |
| 1367 | return Input.getFile(); |
| 1368 | else |
| 1369 | return Input.getBuffer()->getBufferIdentifier(); |
| 1370 | } |
| 1371 | |
| 1372 | if (SourceMgr) { |
| 1373 | if (const FileEntry * |
| 1374 | FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID())) |
| 1375 | return FE->getName(); |
| 1376 | } |
| 1377 | |
| 1378 | return StringRef(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 1381 | StringRef ASTUnit::getASTFileName() const { |
| 1382 | if (!isMainFileAST()) |
| 1383 | return StringRef(); |
| 1384 | |
| 1385 | serialization::ModuleFile & |
| 1386 | Mod = Reader->getModuleManager().getPrimaryModule(); |
| 1387 | return Mod.FileName; |
| 1388 | } |
| 1389 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1390 | std::unique_ptr<ASTUnit> |
| 1391 | ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, |
| 1392 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
| 1393 | bool CaptureDiagnostics, bool UserFilesAreVolatile) { |
| 1394 | std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 1395 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1396 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = |
| 1397 | createVFSFromCompilerInvocation(*CI, *Diags); |
| 1398 | if (!VFS) |
| 1399 | return nullptr; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1400 | AST->Diagnostics = Diags; |
| 1401 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
| 1402 | AST->Invocation = std::move(CI); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1403 | AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1404 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
| 1405 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr, |
| 1406 | UserFilesAreVolatile); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 1407 | AST->PCMCache = new MemoryBufferCache; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1408 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1409 | return AST; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1412 | ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1413 | std::shared_ptr<CompilerInvocation> CI, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1414 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Argyrios Kyrtzidis | c382abf | 2016-02-09 19:07:13 +0000 | [diff] [blame] | 1415 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1416 | ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1417 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
| 1418 | unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults, |
| 1419 | bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile, |
| 1420 | std::unique_ptr<ASTUnit> *ErrAST) { |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1421 | assert(CI && "A CompilerInvocation is required"); |
| 1422 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1423 | std::unique_ptr<ASTUnit> OwnAST; |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1424 | ASTUnit *AST = Unit; |
| 1425 | if (!AST) { |
| 1426 | // Create the AST unit. |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1427 | OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile); |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1428 | AST = OwnAST.get(); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1429 | if (!AST) |
| 1430 | return nullptr; |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1433 | if (!ResourceFilesPath.empty()) { |
| 1434 | // Override the resources path. |
| 1435 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
| 1436 | } |
| 1437 | AST->OnlyLocalDecls = OnlyLocalDecls; |
| 1438 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1439 | if (PrecompilePreambleAfterNParses > 0) |
| 1440 | AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1441 | AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1442 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1443 | AST->IncludeBriefCommentsInCodeCompletion |
| 1444 | = IncludeBriefCommentsInCodeCompletion; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Recover resources if we crash before exiting this method. |
| 1447 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1448 | ASTUnitCleanup(OwnAST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1449 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1450 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1451 | DiagCleanup(Diags.get()); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1452 | |
| 1453 | // We'll manage file buffers ourselves. |
| 1454 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
| 1455 | CI->getFrontendOpts().DisableFree = false; |
| 1456 | ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts()); |
| 1457 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1458 | // Create the compiler instance to use for building the AST. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1459 | std::unique_ptr<CompilerInstance> Clang( |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1460 | new CompilerInstance(std::move(PCHContainerOps))); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Recover resources if we crash before exiting this method. |
| 1463 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1464 | CICleanup(Clang.get()); |
| 1465 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1466 | Clang->setInvocation(std::move(CI)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1467 | AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Set up diagnostics, capturing any diagnostics that would |
| 1470 | // otherwise be dropped. |
| 1471 | Clang->setDiagnostics(&AST->getDiagnostics()); |
| 1472 | |
| 1473 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 1474 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 1475 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1476 | if (!Clang->hasTarget()) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1477 | return nullptr; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1478 | |
| 1479 | // Inform the target of the language options. |
| 1480 | // |
| 1481 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1482 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 1483 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1484 | |
| 1485 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
| 1486 | "Invocation must have exactly one source file!"); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1487 | assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
| 1488 | InputKind::Source && |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1489 | "FIXME: AST inputs not yet supported here!"); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1490 | assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
| 1491 | InputKind::LLVM_IR && |
| 1492 | "IR inputs not support here!"); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1493 | |
| 1494 | // Configure the various subsystems. |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1495 | AST->TheSema.reset(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1496 | AST->Ctx = nullptr; |
| 1497 | AST->PP = nullptr; |
| 1498 | AST->Reader = nullptr; |
| 1499 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1500 | // Create a file manager object to provide access to and cache the filesystem. |
| 1501 | Clang->setFileManager(&AST->getFileManager()); |
| 1502 | |
| 1503 | // Create the source manager. |
| 1504 | Clang->setSourceManager(&AST->getSourceManager()); |
| 1505 | |
Argyrios Kyrtzidis | c382abf | 2016-02-09 19:07:13 +0000 | [diff] [blame] | 1506 | FrontendAction *Act = Action; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1507 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1508 | std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1509 | if (!Act) { |
| 1510 | TrackerAct.reset(new TopLevelDeclTrackerAction(*AST)); |
| 1511 | Act = TrackerAct.get(); |
| 1512 | } |
| 1513 | |
| 1514 | // Recover resources if we crash before exiting this method. |
| 1515 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
| 1516 | ActCleanup(TrackerAct.get()); |
| 1517 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1518 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
| 1519 | AST->transferASTDataFromCompilerInstance(*Clang); |
| 1520 | if (OwnAST && ErrAST) |
| 1521 | ErrAST->swap(OwnAST); |
| 1522 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1523 | return nullptr; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1524 | } |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1525 | |
| 1526 | if (Persistent && !TrackerAct) { |
| 1527 | Clang->getPreprocessor().addPPCallbacks( |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 1528 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
| 1529 | AST->getCurrentTopLevelHashValue())); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1530 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1531 | if (Clang->hasASTConsumer()) |
| 1532 | Consumers.push_back(Clang->takeASTConsumer()); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1533 | Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>( |
| 1534 | *AST, AST->getCurrentTopLevelHashValue())); |
| 1535 | Clang->setASTConsumer( |
| 1536 | llvm::make_unique<MultiplexConsumer>(std::move(Consumers))); |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1537 | } |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 1538 | if (!Act->Execute()) { |
| 1539 | AST->transferASTDataFromCompilerInstance(*Clang); |
| 1540 | if (OwnAST && ErrAST) |
| 1541 | ErrAST->swap(OwnAST); |
| 1542 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1543 | return nullptr; |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 1544 | } |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1545 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1546 | // Steal the created target, context, and preprocessor. |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1547 | AST->transferASTDataFromCompilerInstance(*Clang); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1548 | |
| 1549 | Act->EndSourceFile(); |
| 1550 | |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1551 | if (OwnAST) |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 1552 | return OwnAST.release(); |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1553 | else |
| 1554 | return AST; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1557 | bool ASTUnit::LoadFromCompilerInvocation( |
| 1558 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1559 | unsigned PrecompilePreambleAfterNParses, |
| 1560 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1561 | if (!Invocation) |
| 1562 | return true; |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1563 | |
| 1564 | assert(VFS && "VFS is null"); |
| 1565 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1566 | // We'll manage file buffers ourselves. |
| 1567 | Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
| 1568 | Invocation->getFrontendOpts().DisableFree = false; |
Benjamin Kramer | 8de9c9b | 2017-01-18 16:25:48 +0000 | [diff] [blame] | 1569 | getDiagnostics().Reset(); |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 1570 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1571 | |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1572 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1573 | if (PrecompilePreambleAfterNParses > 0) { |
| 1574 | PreambleRebuildCounter = PrecompilePreambleAfterNParses; |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1575 | OverrideMainBuffer = |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1576 | getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS); |
Benjamin Kramer | 8484a32 | 2017-02-13 16:16:43 +0000 | [diff] [blame] | 1577 | getDiagnostics().Reset(); |
| 1578 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1579 | } |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1581 | SimpleTimer ParsingTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1582 | ParsingTimer.setOutput("Parsing " + getMainFileName()); |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1583 | |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1584 | // Recover resources if we crash before exiting this method. |
| 1585 | llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer> |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1586 | MemBufferCleanup(OverrideMainBuffer.get()); |
| 1587 | |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1588 | return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 1591 | std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1592 | std::shared_ptr<CompilerInvocation> CI, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1593 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1594 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1595 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
| 1596 | unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, |
| 1597 | bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, |
| 1598 | bool UserFilesAreVolatile) { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1599 | // Create the AST unit. |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 1600 | std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 1601 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1602 | AST->Diagnostics = Diags; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1603 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1604 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1605 | AST->TUKind = TUKind; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1606 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1607 | AST->IncludeBriefCommentsInCodeCompletion |
| 1608 | = IncludeBriefCommentsInCodeCompletion; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1609 | AST->Invocation = std::move(CI); |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1610 | AST->FileSystemOpts = FileMgr->getFileSystemOpts(); |
| 1611 | AST->FileMgr = FileMgr; |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1612 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1613 | |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1614 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1615 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 1616 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1617 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1618 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1619 | DiagCleanup(Diags.get()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1620 | |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1621 | if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1622 | PrecompilePreambleAfterNParses, |
| 1623 | AST->FileMgr->getVirtualFileSystem())) |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 1624 | return nullptr; |
| 1625 | return AST; |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1626 | } |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1627 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1628 | ASTUnit *ASTUnit::LoadFromCommandLine( |
| 1629 | const char **ArgBegin, const char **ArgEnd, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1630 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1631 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath, |
| 1632 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
| 1633 | ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1634 | unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1635 | bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, |
| 1636 | bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies, |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 1637 | bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization, |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1638 | llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST, |
| 1639 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 1640 | assert(Diags.get() && "no DiagnosticsEngine was provided"); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1642 | SmallVector<StoredDiagnostic, 4> StoredDiagnostics; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1643 | |
| 1644 | std::shared_ptr<CompilerInvocation> CI; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1646 | { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1647 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1648 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags, |
| 1649 | &StoredDiagnostics, nullptr); |
Daniel Dunbar | fcf2d42 | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 1650 | |
Argyrios Kyrtzidis | 5cf423e | 2011-04-04 23:11:45 +0000 | [diff] [blame] | 1651 | CI = clang::createInvocationFromCommandLine( |
Ilya Biryukov | afdadf5 | 2017-06-28 15:06:34 +0000 | [diff] [blame] | 1652 | llvm::makeArrayRef(ArgBegin, ArgEnd), Diags, VFS); |
Argyrios Kyrtzidis | f606b82 | 2011-04-04 21:38:51 +0000 | [diff] [blame] | 1653 | if (!CI) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1654 | return nullptr; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1655 | } |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1657 | // Override any files that need remapping |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1658 | for (const auto &RemappedFile : RemappedFiles) { |
| 1659 | CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first, |
| 1660 | RemappedFile.second); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1661 | } |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 1662 | PreprocessorOptions &PPOpts = CI->getPreprocessorOpts(); |
| 1663 | PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName; |
| 1664 | PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors; |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 1665 | PPOpts.GeneratePreamble = PrecompilePreambleAfterNParses != 0; |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 1666 | PPOpts.SingleFileParseMode = SingleFileParse; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1667 | |
Daniel Dunbar | a5a166d | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 1668 | // Override the resources path. |
Daniel Dunbar | 6b03ece | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1669 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1670 | |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1671 | CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies; |
| 1672 | |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 1673 | if (ModuleFormat) |
| 1674 | CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue(); |
| 1675 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1676 | // Create the AST unit. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1677 | std::unique_ptr<ASTUnit> AST; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1678 | AST.reset(new ASTUnit(false)); |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 1679 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1680 | AST->Diagnostics = Diags; |
Anders Carlsson | c30dcec | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 1681 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1682 | if (!VFS) |
| 1683 | VFS = vfs::getRealFileSystem(); |
| 1684 | VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1685 | if (!VFS) |
| 1686 | return nullptr; |
| 1687 | AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 1688 | AST->PCMCache = new MemoryBufferCache; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1689 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1690 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1691 | AST->TUKind = TUKind; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1692 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1693 | AST->IncludeBriefCommentsInCodeCompletion |
| 1694 | = IncludeBriefCommentsInCodeCompletion; |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1695 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1696 | AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1697 | AST->StoredDiagnostics.swap(StoredDiagnostics); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1698 | AST->Invocation = CI; |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 1699 | if (ForSerialization) |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 1700 | AST->WriterData.reset(new ASTWriterData(*AST->PCMCache)); |
Alexey Samsonov | b4f99dd | 2014-08-28 23:51:01 +0000 | [diff] [blame] | 1701 | // Zero out now to ease cleanup during crash recovery. |
| 1702 | CI = nullptr; |
| 1703 | Diags = nullptr; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1704 | |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1705 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1706 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 1707 | ASTUnitCleanup(AST.get()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1708 | |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1709 | if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1710 | PrecompilePreambleAfterNParses, |
| 1711 | VFS)) { |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1712 | // Some error occurred, if caller wants to examine diagnostics, pass it the |
| 1713 | // ASTUnit. |
| 1714 | if (ErrAST) { |
| 1715 | AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics); |
| 1716 | ErrAST->swap(AST); |
| 1717 | } |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1718 | return nullptr; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 1721 | return AST.release(); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1722 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1723 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1724 | bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1725 | ArrayRef<RemappedFile> RemappedFiles, |
| 1726 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1727 | if (!Invocation) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1728 | return true; |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 1729 | |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1730 | if (!VFS) { |
| 1731 | assert(FileMgr && "FileMgr is null on Reparse call"); |
| 1732 | VFS = FileMgr->getVirtualFileSystem(); |
| 1733 | } |
| 1734 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 1735 | clearFileLevelDecls(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1736 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1737 | SimpleTimer ParsingTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1738 | ParsingTimer.setOutput("Reparsing " + getMainFileName()); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1740 | // Remap files. |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1741 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1742 | for (const auto &RB : PPOpts.RemappedFileBuffers) |
| 1743 | delete RB.second; |
| 1744 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1745 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1746 | for (const auto &RemappedFile : RemappedFiles) { |
| 1747 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first, |
| 1748 | RemappedFile.second); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1749 | } |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 1750 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1751 | // If we have a preamble file lying around, or if we might try to |
| 1752 | // build a precompiled preamble, do so now. |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1753 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 1754 | if (Preamble || PreambleRebuildCounter > 0) |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1755 | OverrideMainBuffer = |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1756 | getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation, VFS); |
| 1757 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1759 | // Clear out the diagnostics state. |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1760 | FileMgr.reset(); |
Argyrios Kyrtzidis | f50f7b2 | 2011-11-03 20:28:19 +0000 | [diff] [blame] | 1761 | getDiagnostics().Reset(); |
| 1762 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Argyrios Kyrtzidis | 462ff35 | 2011-11-03 20:57:33 +0000 | [diff] [blame] | 1763 | if (OverrideMainBuffer) |
| 1764 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
Argyrios Kyrtzidis | f50f7b2 | 2011-11-03 20:28:19 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1766 | // Parse the sources |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1767 | bool Result = |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 1768 | Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer), VFS); |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1769 | |
Argyrios Kyrtzidis | 3689337 | 2011-10-31 21:25:31 +0000 | [diff] [blame] | 1770 | // If we're caching global code-completion results, and the top-level |
| 1771 | // declarations have changed, clear out the code-completion cache. |
| 1772 | if (!Result && ShouldCacheCodeCompletionResults && |
| 1773 | CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue) |
| 1774 | CacheCodeCompletionResults(); |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 1775 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1776 | // We now need to clear out the completion info related to this translation |
| 1777 | // unit; it'll be recreated if necessary. |
| 1778 | CCTUInfo.reset(); |
Douglas Gregor | 3f35bb2 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1779 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1780 | return Result; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1781 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1782 | |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 1783 | void ASTUnit::ResetForParse() { |
| 1784 | SavedMainFileBuffer.reset(); |
| 1785 | |
| 1786 | SourceMgr.reset(); |
| 1787 | TheSema.reset(); |
| 1788 | Ctx.reset(); |
| 1789 | PP.reset(); |
| 1790 | Reader.reset(); |
| 1791 | |
| 1792 | TopLevelDecls.clear(); |
| 1793 | clearFileLevelDecls(); |
| 1794 | } |
| 1795 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1796 | //----------------------------------------------------------------------------// |
| 1797 | // Code completion |
| 1798 | //----------------------------------------------------------------------------// |
| 1799 | |
| 1800 | namespace { |
| 1801 | /// \brief Code completion consumer that combines the cached code-completion |
| 1802 | /// results from an ASTUnit with the code-completion results provided to it, |
| 1803 | /// then passes the result on to |
| 1804 | class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 1805 | uint64_t NormalContexts; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1806 | ASTUnit &AST; |
| 1807 | CodeCompleteConsumer &Next; |
| 1808 | |
| 1809 | public: |
| 1810 | AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1811 | const CodeCompleteOptions &CodeCompleteOpts) |
| 1812 | : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()), |
| 1813 | AST(AST), Next(Next) |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1814 | { |
| 1815 | // Compute the set of contexts in which we will look when we don't have |
| 1816 | // any information about the specific context. |
| 1817 | NormalContexts |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 1818 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
| 1819 | | (1LL << CodeCompletionContext::CCC_ObjCInterface) |
| 1820 | | (1LL << CodeCompletionContext::CCC_ObjCImplementation) |
| 1821 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 1822 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 1823 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 1824 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
| 1825 | | (1LL << CodeCompletionContext::CCC_DotMemberAccess) |
| 1826 | | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess) |
| 1827 | | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess) |
| 1828 | | (1LL << CodeCompletionContext::CCC_ObjCProtocolName) |
| 1829 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
| 1830 | | (1LL << CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1831 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1832 | if (AST.getASTContext().getLangOpts().CPlusPlus) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 1833 | NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag) |
| 1834 | | (1LL << CodeCompletionContext::CCC_UnionTag) |
| 1835 | | (1LL << CodeCompletionContext::CCC_ClassOrStructTag); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1836 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 1837 | |
| 1838 | void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, |
| 1839 | CodeCompletionResult *Results, |
| 1840 | unsigned NumResults) override; |
| 1841 | |
| 1842 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 1843 | OverloadCandidate *Candidates, |
| 1844 | unsigned NumCandidates) override { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1845 | Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); |
| 1846 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 1847 | |
| 1848 | CodeCompletionAllocator &getAllocator() override { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1849 | return Next.getAllocator(); |
| 1850 | } |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1851 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 1852 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1853 | return Next.getCodeCompletionTUInfo(); |
| 1854 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1855 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1856 | } // anonymous namespace |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1858 | /// \brief Helper function that computes which global names are hidden by the |
| 1859 | /// local code-completion results. |
Ted Kremenek | 6a15337 | 2010-11-07 06:11:36 +0000 | [diff] [blame] | 1860 | static void CalculateHiddenNames(const CodeCompletionContext &Context, |
| 1861 | CodeCompletionResult *Results, |
| 1862 | unsigned NumResults, |
| 1863 | ASTContext &Ctx, |
| 1864 | llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){ |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1865 | bool OnlyTagNames = false; |
| 1866 | switch (Context.getKind()) { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1867 | case CodeCompletionContext::CCC_Recovery: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1868 | case CodeCompletionContext::CCC_TopLevel: |
| 1869 | case CodeCompletionContext::CCC_ObjCInterface: |
| 1870 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 1871 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 1872 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 1873 | case CodeCompletionContext::CCC_Statement: |
| 1874 | case CodeCompletionContext::CCC_Expression: |
| 1875 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1876 | case CodeCompletionContext::CCC_DotMemberAccess: |
| 1877 | case CodeCompletionContext::CCC_ArrowMemberAccess: |
| 1878 | case CodeCompletionContext::CCC_ObjCPropertyAccess: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1879 | case CodeCompletionContext::CCC_Namespace: |
| 1880 | case CodeCompletionContext::CCC_Type: |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1881 | case CodeCompletionContext::CCC_Name: |
| 1882 | case CodeCompletionContext::CCC_PotentiallyQualifiedName: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1883 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 1884 | case CodeCompletionContext::CCC_ObjCInterfaceName: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1885 | break; |
| 1886 | |
| 1887 | case CodeCompletionContext::CCC_EnumTag: |
| 1888 | case CodeCompletionContext::CCC_UnionTag: |
| 1889 | case CodeCompletionContext::CCC_ClassOrStructTag: |
| 1890 | OnlyTagNames = true; |
| 1891 | break; |
| 1892 | |
| 1893 | case CodeCompletionContext::CCC_ObjCProtocolName: |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1894 | case CodeCompletionContext::CCC_MacroName: |
| 1895 | case CodeCompletionContext::CCC_MacroNameUse: |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 1896 | case CodeCompletionContext::CCC_PreprocessorExpression: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 1897 | case CodeCompletionContext::CCC_PreprocessorDirective: |
Douglas Gregor | ea14705 | 2010-08-25 18:04:30 +0000 | [diff] [blame] | 1898 | case CodeCompletionContext::CCC_NaturalLanguage: |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 1899 | case CodeCompletionContext::CCC_SelectorName: |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 1900 | case CodeCompletionContext::CCC_TypeQualifiers: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1901 | case CodeCompletionContext::CCC_Other: |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 1902 | case CodeCompletionContext::CCC_OtherWithMacros: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1903 | case CodeCompletionContext::CCC_ObjCInstanceMessage: |
| 1904 | case CodeCompletionContext::CCC_ObjCClassMessage: |
| 1905 | case CodeCompletionContext::CCC_ObjCCategoryName: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 1906 | // We're looking for nothing, or we're looking for names that cannot |
| 1907 | // be hidden. |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1908 | return; |
| 1909 | } |
| 1910 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1911 | typedef CodeCompletionResult Result; |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1912 | for (unsigned I = 0; I != NumResults; ++I) { |
| 1913 | if (Results[I].Kind != Result::RK_Declaration) |
| 1914 | continue; |
| 1915 | |
| 1916 | unsigned IDNS |
| 1917 | = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); |
| 1918 | |
| 1919 | bool Hiding = false; |
| 1920 | if (OnlyTagNames) |
| 1921 | Hiding = (IDNS & Decl::IDNS_Tag); |
| 1922 | else { |
| 1923 | unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 1924 | Decl::IDNS_Namespace | Decl::IDNS_Ordinary | |
| 1925 | Decl::IDNS_NonMemberOperator); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1926 | if (Ctx.getLangOpts().CPlusPlus) |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1927 | HiddenIDNS |= Decl::IDNS_Tag; |
| 1928 | Hiding = (IDNS & HiddenIDNS); |
| 1929 | } |
| 1930 | |
| 1931 | if (!Hiding) |
| 1932 | continue; |
| 1933 | |
| 1934 | DeclarationName Name = Results[I].Declaration->getDeclName(); |
| 1935 | if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) |
| 1936 | HiddenNames.insert(Identifier->getName()); |
| 1937 | else |
| 1938 | HiddenNames.insert(Name.getAsString()); |
| 1939 | } |
| 1940 | } |
| 1941 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1942 | void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, |
| 1943 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1944 | CodeCompletionResult *Results, |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1945 | unsigned NumResults) { |
| 1946 | // Merge the results we were given with the results we cached. |
| 1947 | bool AddedResult = false; |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 1948 | uint64_t InContexts = |
| 1949 | Context.getKind() == CodeCompletionContext::CCC_Recovery |
| 1950 | ? NormalContexts : (1LL << Context.getKind()); |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1951 | // Contains the set of names that are hidden by "local" completion results. |
Ted Kremenek | 6a15337 | 2010-11-07 06:11:36 +0000 | [diff] [blame] | 1952 | llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames; |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1953 | typedef CodeCompletionResult Result; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1954 | SmallVector<Result, 8> AllResults; |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1955 | for (ASTUnit::cached_completion_iterator |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1956 | C = AST.cached_completion_begin(), |
| 1957 | CEnd = AST.cached_completion_end(); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1958 | C != CEnd; ++C) { |
| 1959 | // If the context we are in matches any of the contexts we are |
| 1960 | // interested in, we'll add this result. |
| 1961 | if ((C->ShowInContexts & InContexts) == 0) |
| 1962 | continue; |
| 1963 | |
| 1964 | // If we haven't added any results previously, do so now. |
| 1965 | if (!AddedResult) { |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1966 | CalculateHiddenNames(Context, Results, NumResults, S.Context, |
| 1967 | HiddenNames); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1968 | AllResults.insert(AllResults.end(), Results, Results + NumResults); |
| 1969 | AddedResult = true; |
| 1970 | } |
| 1971 | |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1972 | // Determine whether this global completion result is hidden by a local |
| 1973 | // completion result. If so, skip it. |
| 1974 | if (C->Kind != CXCursor_MacroDefinition && |
| 1975 | HiddenNames.count(C->Completion->getTypedText())) |
| 1976 | continue; |
| 1977 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1978 | // Adjust priority based on similar type classes. |
| 1979 | unsigned Priority = C->Priority; |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1980 | CodeCompletionString *Completion = C->Completion; |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1981 | if (!Context.getPreferredType().isNull()) { |
| 1982 | if (C->Kind == CXCursor_MacroDefinition) { |
| 1983 | Priority = getMacroUsagePriority(C->Completion->getTypedText(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1984 | S.getLangOpts(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1985 | Context.getPreferredType()->isAnyPointerType()); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1986 | } else if (C->Type) { |
| 1987 | CanQualType Expected |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1988 | = S.Context.getCanonicalType( |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1989 | Context.getPreferredType().getUnqualifiedType()); |
| 1990 | SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); |
| 1991 | if (ExpectedSTC == C->TypeClass) { |
| 1992 | // We know this type is similar; check for an exact match. |
| 1993 | llvm::StringMap<unsigned> &CachedCompletionTypes |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1994 | = AST.getCachedCompletionTypes(); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1995 | llvm::StringMap<unsigned>::iterator Pos |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1996 | = CachedCompletionTypes.find(QualType(Expected).getAsString()); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1997 | if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) |
| 1998 | Priority /= CCF_ExactTypeMatch; |
| 1999 | else |
| 2000 | Priority /= CCF_SimilarTypeMatch; |
| 2001 | } |
| 2002 | } |
| 2003 | } |
| 2004 | |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2005 | // Adjust the completion string, if required. |
| 2006 | if (C->Kind == CXCursor_MacroDefinition && |
| 2007 | Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { |
| 2008 | // Create a new code-completion string that just contains the |
| 2009 | // macro name, without its arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2010 | CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(), |
| 2011 | CCP_CodePattern, C->Availability); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2012 | Builder.AddTypedTextChunk(C->Completion->getTypedText()); |
Douglas Gregor | 8850aa3 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 2013 | Priority = CCP_CodePattern; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2014 | Completion = Builder.TakeString(); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 2017 | AllResults.push_back(Result(Completion, Priority, C->Kind, |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 2018 | C->Availability)); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | // If we did not add any cached completion results, just forward the |
| 2022 | // results we were given to the next consumer. |
| 2023 | if (!AddedResult) { |
| 2024 | Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); |
| 2025 | return; |
| 2026 | } |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2028 | Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), |
| 2029 | AllResults.size()); |
| 2030 | } |
| 2031 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2032 | void ASTUnit::CodeComplete( |
| 2033 | StringRef File, unsigned Line, unsigned Column, |
| 2034 | ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros, |
| 2035 | bool IncludeCodePatterns, bool IncludeBriefComments, |
| 2036 | CodeCompleteConsumer &Consumer, |
| 2037 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 2038 | DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, |
| 2039 | FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, |
| 2040 | SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2041 | if (!Invocation) |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2042 | return; |
| 2043 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 2044 | SimpleTimer CompletionTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 2045 | CompletionTimer.setOutput("Code completion @ " + File + ":" + |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2046 | Twine(Line) + ":" + Twine(Column)); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2047 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2048 | auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2049 | |
| 2050 | FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts(); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2051 | CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2052 | PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); |
Douglas Gregor | b68bc59 | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 2053 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2054 | CodeCompleteOpts.IncludeMacros = IncludeMacros && |
| 2055 | CachedCompletionResults.empty(); |
| 2056 | CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns; |
| 2057 | CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty(); |
| 2058 | CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments; |
| 2059 | |
| 2060 | assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion); |
| 2061 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2062 | FrontendOpts.CodeCompletionAt.FileName = File; |
| 2063 | FrontendOpts.CodeCompletionAt.Line = Line; |
| 2064 | FrontendOpts.CodeCompletionAt.Column = Column; |
| 2065 | |
| 2066 | // Set the language options appropriately. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 2067 | LangOpts = *CCInvocation->getLangOpts(); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2068 | |
Argyrios Kyrtzidis | 06e8d69 | 2014-10-31 16:44:32 +0000 | [diff] [blame] | 2069 | // Spell-checking and warnings are wasteful during code-completion. |
| 2070 | LangOpts.SpellChecking = false; |
| 2071 | CCInvocation->getDiagnosticOpts().IgnoreWarnings = true; |
| 2072 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2073 | std::unique_ptr<CompilerInstance> Clang( |
| 2074 | new CompilerInstance(PCHContainerOps)); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2075 | |
| 2076 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2077 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 2078 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2079 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2080 | auto &Inv = *CCInvocation; |
| 2081 | Clang->setInvocation(std::move(CCInvocation)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 2082 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Set up diagnostics, capturing any diagnostics produced. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2085 | Clang->setDiagnostics(&Diag); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2086 | CaptureDroppedDiagnostics Capture(true, |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2087 | Clang->getDiagnostics(), |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2088 | &StoredDiagnostics, nullptr); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2089 | ProcessWarningOptions(Diag, Inv.getDiagnosticOpts()); |
| 2090 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2091 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 2092 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 2093 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2094 | if (!Clang->hasTarget()) { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2095 | Clang->setInvocation(nullptr); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 2096 | return; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | // Inform the target of the language options. |
| 2100 | // |
| 2101 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 2102 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 2103 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2104 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2105 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2106 | "Invocation must have exactly one source file!"); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 2107 | assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
| 2108 | InputKind::Source && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2109 | "FIXME: AST inputs not yet supported here!"); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 2110 | assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
| 2111 | InputKind::LLVM_IR && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2112 | "IR inputs not support here!"); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2113 | |
| 2114 | // Use the source and file managers that we were given. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2115 | Clang->setFileManager(&FileMgr); |
| 2116 | Clang->setSourceManager(&SourceMgr); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2117 | |
| 2118 | // Remap files. |
| 2119 | PreprocessorOpts.clearRemappedFiles(); |
Douglas Gregor | d8a5dba | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 2120 | PreprocessorOpts.RetainRemappedFileBuffers = true; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2121 | for (const auto &RemappedFile : RemappedFiles) { |
| 2122 | PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second); |
| 2123 | OwnedBuffers.push_back(RemappedFile.second); |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 2124 | } |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 2125 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2126 | // Use the code completion consumer we were given, but adding any cached |
| 2127 | // code-completion results. |
Douglas Gregor | e9186e6 | 2010-11-29 16:13:56 +0000 | [diff] [blame] | 2128 | AugmentedCodeCompleteConsumer *AugmentedConsumer |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2129 | = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2130 | Clang->setCodeCompletionConsumer(AugmentedConsumer); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2131 | |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2132 | // If we have a precompiled preamble, try to use it. We only allow |
| 2133 | // the use of the precompiled preamble if we're if the completion |
| 2134 | // point is within the main file, after the end of the precompiled |
| 2135 | // preamble. |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2136 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2137 | if (Preamble) { |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2138 | std::string CompleteFilePath(File); |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2139 | |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 2140 | auto VFS = FileMgr.getVirtualFileSystem(); |
| 2141 | auto CompleteFileStatus = VFS->status(CompleteFilePath); |
| 2142 | if (CompleteFileStatus) { |
| 2143 | llvm::sys::fs::UniqueID CompleteFileID = CompleteFileStatus->getUniqueID(); |
| 2144 | |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2145 | std::string MainPath(OriginalSourceFile); |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 2146 | auto MainStatus = VFS->status(MainPath); |
| 2147 | if (MainStatus) { |
| 2148 | llvm::sys::fs::UniqueID MainID = MainStatus->getUniqueID(); |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2149 | if (CompleteFileID == MainID && Line > 1) |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2150 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble( |
Ilya Biryukov | af69e40 | 2017-05-23 11:37:52 +0000 | [diff] [blame] | 2151 | PCHContainerOps, Inv, VFS, false, Line - 1); |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2152 | } |
| 2153 | } |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | // If the main file has been overridden due to the use of a preamble, |
| 2157 | // make that override happen and introduce the preamble. |
| 2158 | if (OverrideMainBuffer) { |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2159 | assert(Preamble && "No preamble was built, but OverrideMainBuffer is not null"); |
| 2160 | Preamble->AddImplicitPreamble(Clang->getInvocation(), OverrideMainBuffer.get()); |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2161 | OwnedBuffers.push_back(OverrideMainBuffer.release()); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 2162 | } else { |
| 2163 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 2164 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Argyrios Kyrtzidis | 870704f | 2012-11-02 22:18:44 +0000 | [diff] [blame] | 2167 | // Disable the preprocessing record if modules are not enabled. |
| 2168 | if (!Clang->getLangOpts().Modules) |
| 2169 | PreprocessorOpts.DetailedRecord = false; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 2170 | |
| 2171 | std::unique_ptr<SyntaxOnlyAction> Act; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2172 | Act.reset(new SyntaxOnlyAction); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 2173 | if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2174 | Act->Execute(); |
| 2175 | Act->EndSourceFile(); |
| 2176 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2177 | } |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2178 | |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2179 | bool ASTUnit::Save(StringRef File) { |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 2180 | if (HadModuleLoaderFatalFailure) |
| 2181 | return true; |
| 2182 | |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2183 | // Write to a temporary file and later rename it to the actual file, to avoid |
| 2184 | // possible race conditions. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2185 | SmallString<128> TempPath; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 2186 | TempPath = File; |
| 2187 | TempPath += "-%%%%%%%%"; |
| 2188 | int fd; |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 2189 | if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath)) |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2190 | return true; |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2192 | // FIXME: Can we somehow regenerate the stat cache here, or do we need to |
| 2193 | // unconditionally create a stat cache when we parse the file? |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 2194 | llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2195 | |
| 2196 | serialize(Out); |
| 2197 | Out.close(); |
Argyrios Kyrtzidis | eeea16a | 2012-03-13 02:17:06 +0000 | [diff] [blame] | 2198 | if (Out.has_error()) { |
| 2199 | Out.clear_error(); |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2200 | return true; |
Argyrios Kyrtzidis | eeea16a | 2012-03-13 02:17:06 +0000 | [diff] [blame] | 2201 | } |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2202 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 2203 | if (llvm::sys::fs::rename(TempPath, File)) { |
| 2204 | llvm::sys::fs::remove(TempPath); |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2205 | return true; |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2208 | return false; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2211 | static bool serializeUnit(ASTWriter &Writer, |
| 2212 | SmallVectorImpl<char> &Buffer, |
| 2213 | Sema &S, |
| 2214 | bool hasErrors, |
| 2215 | raw_ostream &OS) { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2216 | Writer.WriteAST(S, std::string(), nullptr, "", hasErrors); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2217 | |
| 2218 | // Write the generated bitstream to "Out". |
| 2219 | if (!Buffer.empty()) |
| 2220 | OS.write(Buffer.data(), Buffer.size()); |
| 2221 | |
| 2222 | return false; |
| 2223 | } |
| 2224 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2225 | bool ASTUnit::serialize(raw_ostream &OS) { |
Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 2226 | // For serialization we are lenient if the errors were only warn-as-error kind. |
| 2227 | bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred(); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2228 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2229 | if (WriterData) |
| 2230 | return serializeUnit(WriterData->Writer, WriterData->Buffer, |
| 2231 | getSema(), hasErrors, OS); |
| 2232 | |
Daniel Dunbar | 9a96386 | 2012-02-29 20:31:23 +0000 | [diff] [blame] | 2233 | SmallString<128> Buffer; |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2234 | llvm::BitstreamWriter Stream(Buffer); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 2235 | MemoryBufferCache PCMCache; |
| 2236 | ASTWriter Writer(Stream, Buffer, PCMCache, {}); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2237 | return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2238 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2239 | |
| 2240 | typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap; |
| 2241 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2242 | void ASTUnit::TranslateStoredDiagnostics( |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2243 | FileManager &FileMgr, |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2244 | SourceManager &SrcMgr, |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2245 | const SmallVectorImpl<StandaloneDiagnostic> &Diags, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2246 | SmallVectorImpl<StoredDiagnostic> &Out) { |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2247 | // Map the standalone diagnostic into the new source manager. We also need to |
| 2248 | // remap all the locations to the new view. This includes the diag location, |
| 2249 | // any associated source ranges, and the source ranges of associated fix-its. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2250 | // FIXME: There should be a cleaner way to do this. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2251 | SmallVector<StoredDiagnostic, 4> Result; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2252 | Result.reserve(Diags.size()); |
Erik Verbruggen | efe6fa5 | 2017-06-09 08:29:58 +0000 | [diff] [blame] | 2253 | |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2254 | for (const StandaloneDiagnostic &SD : Diags) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2255 | // Rebuild the StoredDiagnostic. |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2256 | if (SD.Filename.empty()) |
| 2257 | continue; |
| 2258 | const FileEntry *FE = FileMgr.getFile(SD.Filename); |
| 2259 | if (!FE) |
| 2260 | continue; |
Erik Verbruggen | efe6fa5 | 2017-06-09 08:29:58 +0000 | [diff] [blame] | 2261 | SourceLocation FileLoc; |
| 2262 | auto ItFileID = PreambleSrcLocCache.find(SD.Filename); |
| 2263 | if (ItFileID == PreambleSrcLocCache.end()) { |
| 2264 | FileID FID = SrcMgr.translateFile(FE); |
| 2265 | FileLoc = SrcMgr.getLocForStartOfFile(FID); |
| 2266 | PreambleSrcLocCache[SD.Filename] = FileLoc; |
| 2267 | } else { |
| 2268 | FileLoc = ItFileID->getValue(); |
Erik Verbruggen | 2c7c38d | 2017-02-16 09:49:30 +0000 | [diff] [blame] | 2269 | } |
Erik Verbruggen | efe6fa5 | 2017-06-09 08:29:58 +0000 | [diff] [blame] | 2270 | |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2271 | if (FileLoc.isInvalid()) |
| 2272 | continue; |
| 2273 | SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2274 | FullSourceLoc Loc(L, SrcMgr); |
| 2275 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2276 | SmallVector<CharSourceRange, 4> Ranges; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2277 | Ranges.reserve(SD.Ranges.size()); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2278 | for (const auto &Range : SD.Ranges) { |
| 2279 | SourceLocation BL = FileLoc.getLocWithOffset(Range.first); |
| 2280 | SourceLocation EL = FileLoc.getLocWithOffset(Range.second); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2281 | Ranges.push_back(CharSourceRange::getCharRange(BL, EL)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2284 | SmallVector<FixItHint, 2> FixIts; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2285 | FixIts.reserve(SD.FixIts.size()); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2286 | for (const StandaloneFixIt &FixIt : SD.FixIts) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2287 | FixIts.push_back(FixItHint()); |
| 2288 | FixItHint &FH = FixIts.back(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2289 | FH.CodeToInsert = FixIt.CodeToInsert; |
| 2290 | SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first); |
| 2291 | SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2292 | FH.RemoveRange = CharSourceRange::getCharRange(BL, EL); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2295 | Result.push_back(StoredDiagnostic(SD.Level, SD.ID, |
| 2296 | SD.Message, Loc, Ranges, FixIts)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2297 | } |
| 2298 | Result.swap(Out); |
| 2299 | } |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2300 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2301 | void ASTUnit::addFileLevelDecl(Decl *D) { |
| 2302 | assert(D); |
Douglas Gregor | 61d63d0 | 2011-11-07 18:53:57 +0000 | [diff] [blame] | 2303 | |
| 2304 | // We only care about local declarations. |
| 2305 | if (D->isFromASTFile()) |
| 2306 | return; |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2307 | |
| 2308 | SourceManager &SM = *SourceMgr; |
| 2309 | SourceLocation Loc = D->getLocation(); |
| 2310 | if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc)) |
| 2311 | return; |
| 2312 | |
| 2313 | // We only keep track of the file-level declarations of each file. |
| 2314 | if (!D->getLexicalDeclContext()->isFileContext()) |
| 2315 | return; |
| 2316 | |
| 2317 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
| 2318 | assert(SM.isLocalSourceLocation(FileLoc)); |
| 2319 | FileID FID; |
| 2320 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2321 | std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2322 | if (FID.isInvalid()) |
| 2323 | return; |
| 2324 | |
| 2325 | LocDeclsTy *&Decls = FileDecls[FID]; |
| 2326 | if (!Decls) |
| 2327 | Decls = new LocDeclsTy(); |
| 2328 | |
| 2329 | std::pair<unsigned, Decl *> LocDecl(Offset, D); |
| 2330 | |
| 2331 | if (Decls->empty() || Decls->back().first <= Offset) { |
| 2332 | Decls->push_back(LocDecl); |
| 2333 | return; |
| 2334 | } |
| 2335 | |
Benjamin Kramer | 45025c0 | 2013-08-24 13:22:59 +0000 | [diff] [blame] | 2336 | LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(), |
| 2337 | LocDecl, llvm::less_first()); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2338 | |
| 2339 | Decls->insert(I, LocDecl); |
| 2340 | } |
| 2341 | |
Argyrios Kyrtzidis | e968152 | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 2342 | void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, |
| 2343 | SmallVectorImpl<Decl *> &Decls) { |
| 2344 | if (File.isInvalid()) |
| 2345 | return; |
| 2346 | |
| 2347 | if (SourceMgr->isLoadedFileID(File)) { |
| 2348 | assert(Ctx->getExternalSource() && "No external source!"); |
| 2349 | return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length, |
| 2350 | Decls); |
| 2351 | } |
| 2352 | |
| 2353 | FileDeclsTy::iterator I = FileDecls.find(File); |
| 2354 | if (I == FileDecls.end()) |
| 2355 | return; |
| 2356 | |
| 2357 | LocDeclsTy &LocDecls = *I->second; |
| 2358 | if (LocDecls.empty()) |
| 2359 | return; |
| 2360 | |
Benjamin Kramer | e3e855b | 2013-08-24 13:12:34 +0000 | [diff] [blame] | 2361 | LocDeclsTy::iterator BeginIt = |
| 2362 | std::lower_bound(LocDecls.begin(), LocDecls.end(), |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2363 | std::make_pair(Offset, (Decl *)nullptr), |
| 2364 | llvm::less_first()); |
Argyrios Kyrtzidis | e968152 | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 2365 | if (BeginIt != LocDecls.begin()) |
| 2366 | --BeginIt; |
| 2367 | |
Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 2368 | // If we are pointing at a top-level decl inside an objc container, we need |
| 2369 | // to backtrack until we find it otherwise we will fail to report that the |
| 2370 | // region overlaps with an objc container. |
| 2371 | while (BeginIt != LocDecls.begin() && |
| 2372 | BeginIt->second->isTopLevelDeclInObjCContainer()) |
| 2373 | --BeginIt; |
| 2374 | |
Benjamin Kramer | e3e855b | 2013-08-24 13:12:34 +0000 | [diff] [blame] | 2375 | LocDeclsTy::iterator EndIt = std::upper_bound( |
| 2376 | LocDecls.begin(), LocDecls.end(), |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2377 | std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first()); |
Argyrios Kyrtzidis | e968152 | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 2378 | if (EndIt != LocDecls.end()) |
| 2379 | ++EndIt; |
| 2380 | |
| 2381 | for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt) |
| 2382 | Decls.push_back(DIt->second); |
| 2383 | } |
| 2384 | |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2385 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
| 2386 | unsigned Line, unsigned Col) const { |
| 2387 | const SourceManager &SM = getSourceManager(); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2388 | SourceLocation Loc = SM.translateFileLineCol(File, Line, Col); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2389 | return SM.getMacroArgExpandedLocation(Loc); |
| 2390 | } |
| 2391 | |
| 2392 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
| 2393 | unsigned Offset) const { |
| 2394 | const SourceManager &SM = getSourceManager(); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2395 | SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2396 | return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset)); |
| 2397 | } |
| 2398 | |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2399 | /// \brief If \arg Loc is a loaded location from the preamble, returns |
| 2400 | /// the corresponding local location of the main file, otherwise it returns |
| 2401 | /// \arg Loc. |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2402 | SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) const { |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2403 | FileID PreambleID; |
| 2404 | if (SourceMgr) |
| 2405 | PreambleID = SourceMgr->getPreambleFileID(); |
| 2406 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2407 | if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid()) |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2408 | return Loc; |
| 2409 | |
| 2410 | unsigned Offs; |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2411 | if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble->getBounds().Size) { |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2412 | SourceLocation FileLoc |
| 2413 | = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); |
| 2414 | return FileLoc.getLocWithOffset(Offs); |
| 2415 | } |
| 2416 | |
| 2417 | return Loc; |
| 2418 | } |
| 2419 | |
| 2420 | /// \brief If \arg Loc is a local location of the main file but inside the |
| 2421 | /// preamble chunk, returns the corresponding loaded location from the |
| 2422 | /// preamble, otherwise it returns \arg Loc. |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2423 | SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) const { |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2424 | FileID PreambleID; |
| 2425 | if (SourceMgr) |
| 2426 | PreambleID = SourceMgr->getPreambleFileID(); |
| 2427 | |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2428 | if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid()) |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2429 | return Loc; |
| 2430 | |
| 2431 | unsigned Offs; |
| 2432 | if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) && |
Ilya Biryukov | 200b328 | 2017-06-21 10:24:58 +0000 | [diff] [blame] | 2433 | Offs < Preamble->getBounds().Size) { |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2434 | SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID); |
| 2435 | return FileLoc.getLocWithOffset(Offs); |
| 2436 | } |
| 2437 | |
| 2438 | return Loc; |
| 2439 | } |
| 2440 | |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2441 | bool ASTUnit::isInPreambleFileID(SourceLocation Loc) const { |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2442 | FileID FID; |
| 2443 | if (SourceMgr) |
| 2444 | FID = SourceMgr->getPreambleFileID(); |
| 2445 | |
| 2446 | if (Loc.isInvalid() || FID.isInvalid()) |
| 2447 | return false; |
| 2448 | |
| 2449 | return SourceMgr->isInFileID(Loc, FID); |
| 2450 | } |
| 2451 | |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2452 | bool ASTUnit::isInMainFileID(SourceLocation Loc) const { |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2453 | FileID FID; |
| 2454 | if (SourceMgr) |
| 2455 | FID = SourceMgr->getMainFileID(); |
| 2456 | |
| 2457 | if (Loc.isInvalid() || FID.isInvalid()) |
| 2458 | return false; |
| 2459 | |
| 2460 | return SourceMgr->isInFileID(Loc, FID); |
| 2461 | } |
| 2462 | |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2463 | SourceLocation ASTUnit::getEndOfPreambleFileID() const { |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2464 | FileID FID; |
| 2465 | if (SourceMgr) |
| 2466 | FID = SourceMgr->getPreambleFileID(); |
| 2467 | |
| 2468 | if (FID.isInvalid()) |
| 2469 | return SourceLocation(); |
| 2470 | |
| 2471 | return SourceMgr->getLocForEndOfFile(FID); |
| 2472 | } |
| 2473 | |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2474 | SourceLocation ASTUnit::getStartOfMainFileID() const { |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2475 | FileID FID; |
| 2476 | if (SourceMgr) |
| 2477 | FID = SourceMgr->getMainFileID(); |
| 2478 | |
| 2479 | if (FID.isInvalid()) |
| 2480 | return SourceLocation(); |
| 2481 | |
| 2482 | return SourceMgr->getLocForStartOfFile(FID); |
| 2483 | } |
| 2484 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2485 | llvm::iterator_range<PreprocessingRecord::iterator> |
Argyrios Kyrtzidis | d4fcf580 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 2486 | ASTUnit::getLocalPreprocessingEntities() const { |
| 2487 | if (isMainFileAST()) { |
| 2488 | serialization::ModuleFile & |
| 2489 | Mod = Reader->getModuleManager().getPrimaryModule(); |
| 2490 | return Reader->getModulePreprocessedEntities(Mod); |
| 2491 | } |
| 2492 | |
| 2493 | if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord()) |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2494 | return llvm::make_range(PPRec->local_begin(), PPRec->local_end()); |
Argyrios Kyrtzidis | d4fcf580 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 2495 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2496 | return llvm::make_range(PreprocessingRecord::iterator(), |
| 2497 | PreprocessingRecord::iterator()); |
Argyrios Kyrtzidis | d4fcf580 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Argyrios Kyrtzidis | e514b20 | 2012-10-03 01:58:28 +0000 | [diff] [blame] | 2500 | bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) { |
Argyrios Kyrtzidis | 10e7846 | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 2501 | if (isMainFileAST()) { |
| 2502 | serialization::ModuleFile & |
| 2503 | Mod = Reader->getModuleManager().getPrimaryModule(); |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2504 | for (const Decl *D : Reader->getModuleFileLevelDecls(Mod)) { |
| 2505 | if (!Fn(context, D)) |
Argyrios Kyrtzidis | 10e7846 | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 2506 | return false; |
| 2507 | } |
| 2508 | |
| 2509 | return true; |
| 2510 | } |
| 2511 | |
| 2512 | for (ASTUnit::top_level_iterator TL = top_level_begin(), |
| 2513 | TLEnd = top_level_end(); |
| 2514 | TL != TLEnd; ++TL) { |
| 2515 | if (!Fn(context, *TL)) |
| 2516 | return false; |
| 2517 | } |
| 2518 | |
| 2519 | return true; |
| 2520 | } |
| 2521 | |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2522 | const FileEntry *ASTUnit::getPCHFile() { |
| 2523 | if (!Reader) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2524 | return nullptr; |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2525 | |
Benjamin Kramer | 9a9efba | 2015-07-25 12:14:04 +0000 | [diff] [blame] | 2526 | serialization::ModuleFile *Mod = nullptr; |
| 2527 | Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) { |
| 2528 | switch (M.Kind) { |
| 2529 | case serialization::MK_ImplicitModule: |
| 2530 | case serialization::MK_ExplicitModule: |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 2531 | case serialization::MK_PrebuiltModule: |
Benjamin Kramer | 9a9efba | 2015-07-25 12:14:04 +0000 | [diff] [blame] | 2532 | return true; // skip dependencies. |
| 2533 | case serialization::MK_PCH: |
| 2534 | Mod = &M; |
| 2535 | return true; // found it. |
| 2536 | case serialization::MK_Preamble: |
| 2537 | return false; // look in dependencies. |
| 2538 | case serialization::MK_MainFile: |
| 2539 | return false; // look in dependencies. |
| 2540 | } |
| 2541 | |
| 2542 | return true; |
| 2543 | }); |
| 2544 | if (Mod) |
| 2545 | return Mod->File; |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2546 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2547 | return nullptr; |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Vedant Kumar | 525a7f6 | 2017-07-25 19:53:27 +0000 | [diff] [blame] | 2550 | bool ASTUnit::isModuleFile() const { |
Richard Smith | ab75597 | 2017-06-05 18:10:11 +0000 | [diff] [blame] | 2551 | return isMainFileAST() && getLangOpts().isCompilingModule(); |
| 2552 | } |
| 2553 | |
| 2554 | InputKind ASTUnit::getInputKind() const { |
| 2555 | auto &LangOpts = getLangOpts(); |
| 2556 | |
| 2557 | InputKind::Language Lang; |
| 2558 | if (LangOpts.OpenCL) |
| 2559 | Lang = InputKind::OpenCL; |
| 2560 | else if (LangOpts.CUDA) |
| 2561 | Lang = InputKind::CUDA; |
| 2562 | else if (LangOpts.RenderScript) |
| 2563 | Lang = InputKind::RenderScript; |
| 2564 | else if (LangOpts.CPlusPlus) |
| 2565 | Lang = LangOpts.ObjC1 ? InputKind::ObjCXX : InputKind::CXX; |
| 2566 | else |
| 2567 | Lang = LangOpts.ObjC1 ? InputKind::ObjC : InputKind::C; |
| 2568 | |
| 2569 | InputKind::Format Fmt = InputKind::Source; |
| 2570 | if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap) |
| 2571 | Fmt = InputKind::ModuleMap; |
| 2572 | |
| 2573 | // We don't know if input was preprocessed. Assume not. |
| 2574 | bool PP = false; |
| 2575 | |
| 2576 | return InputKind(Lang, Fmt, PP); |
Argyrios Kyrtzidis | e445c72 | 2012-10-10 02:12:47 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 2579 | #ifndef NDEBUG |
| 2580 | ASTUnit::ConcurrencyState::ConcurrencyState() { |
| 2581 | Mutex = new llvm::sys::MutexImpl(/*recursive=*/true); |
| 2582 | } |
| 2583 | |
| 2584 | ASTUnit::ConcurrencyState::~ConcurrencyState() { |
| 2585 | delete static_cast<llvm::sys::MutexImpl *>(Mutex); |
| 2586 | } |
| 2587 | |
| 2588 | void ASTUnit::ConcurrencyState::start() { |
| 2589 | bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire(); |
| 2590 | assert(acquired && "Concurrent access to ASTUnit!"); |
| 2591 | } |
| 2592 | |
| 2593 | void ASTUnit::ConcurrencyState::finish() { |
| 2594 | static_cast<llvm::sys::MutexImpl *>(Mutex)->release(); |
| 2595 | } |
| 2596 | |
| 2597 | #else // NDEBUG |
| 2598 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2599 | ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; } |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 2600 | ASTUnit::ConcurrencyState::~ConcurrencyState() {} |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 2601 | void ASTUnit::ConcurrencyState::start() {} |
| 2602 | void ASTUnit::ConcurrencyState::finish() {} |
| 2603 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2604 | #endif // NDEBUG |