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