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