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